]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/throwing.qc
Merge branch 'master' into TimePath/items
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / weapons / throwing.qc
1 #include "throwing.qh"
2
3 #include "weaponsystem.qh"
4 #include "../mutators/all.qh"
5 #include "../t_items.qh"
6 #include "../g_damage.qh"
7 #include "../../common/items/item.qh"
8 #include "../../common/mapinfo.qh"
9 #include "../../common/notifications.qh"
10 #include "../../common/triggers/subs.qh"
11 #include "../../common/util.qh"
12 #include "../../common/weapons/all.qh"
13
14 void thrown_wep_think()
15 {SELFPARAM();
16         self.nextthink = time;
17         if(self.oldorigin != self.origin)
18         {
19                 self.SendFlags |= ISF_LOCATION;
20                 self.oldorigin = self.origin;
21         }
22         self.owner = world;
23         float timeleft = self.savenextthink - time;
24         if(timeleft > 1)
25                 SUB_SetFade(self, self.savenextthink - 1, 1);
26         else if(timeleft > 0)
27                 SUB_SetFade(self, time, timeleft);
28         else
29                 SUB_VanishOrRemove(self);
30 }
31
32 // returns amount of ammo used as string, or -1 for failure, or 0 for no ammo count
33 string W_ThrowNewWeapon(entity own, float wpn, float doreduce, vector org, vector velo)
34 {SELFPARAM();
35         float thisammo, i;
36         string s;
37         Weapon info = get_weaponinfo(wpn);
38         var .int ammotype = info.ammo_field;
39
40         entity wep = spawn();
41
42         setorigin(wep, org);
43         wep.classname = "droppedweapon";
44         wep.velocity = velo;
45         wep.owner = wep.enemy = own;
46         wep.flags |= FL_TOSSED;
47         wep.colormap = own.colormap;
48
49         W_DropEvent(wr_drop,own,wpn,wep);
50
51         if(WepSet_FromWeapon(wpn) & WEPSET_SUPERWEAPONS)
52         {
53                 if(own.items & IT_UNLIMITED_SUPERWEAPONS)
54                 {
55                         wep.superweapons_finished = time + autocvar_g_balance_superweapons_time;
56                 }
57                 else
58                 {
59                         float superweapons = 1;
60                         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
61                                 if(WepSet_FromWeapon(i) & WEPSET_SUPERWEAPONS)
62                                         if(own.weapons & WepSet_FromWeapon(i))
63                                                 ++superweapons;
64                         if(superweapons <= 1)
65                         {
66                                 wep.superweapons_finished = own.superweapons_finished;
67                                 own.superweapons_finished = 0;
68                         }
69                         else
70                         {
71                                 float timeleft = own.superweapons_finished - time;
72                                 float weptimeleft = timeleft / superweapons;
73                                 wep.superweapons_finished = time + weptimeleft;
74                                 own.superweapons_finished -= weptimeleft;
75                         }
76                 }
77         }
78
79         weapon_defaultspawnfunc(wep, info);
80         if(startitem_failed)
81                 return string_null;
82         wep.glowmod = own.weaponentity_glowmod;
83         wep.think = thrown_wep_think;
84         wep.savenextthink = wep.nextthink;
85         wep.nextthink = min(wep.nextthink, time + 0.5);
86         wep.pickup_anyway = true; // these are ALWAYS pickable
87
88         //wa = W_AmmoItemCode(wpn);
89         if(ammotype == ammo_none)
90         {
91                 return "";
92         }
93         else
94         {
95                 s = "";
96
97                 if(doreduce && g_weapon_stay == 2)
98                 {
99                         // if our weapon is loaded, give its load back to the player
100                         if(self.(weapon_load[self.weapon]) > 0)
101                         {
102                                 own.(ammotype) += self.(weapon_load[self.weapon]);
103                                 self.(weapon_load[self.weapon]) = -1; // schedule the weapon for reloading
104                         }
105
106                         wep.(ammotype) = 0;
107                 }
108                 else if(doreduce)
109                 {
110                         // if our weapon is loaded, give its load back to the player
111                         if(self.(weapon_load[self.weapon]) > 0)
112                         {
113                                 own.(ammotype) += self.(weapon_load[self.weapon]);
114                                 self.(weapon_load[self.weapon]) = -1; // schedule the weapon for reloading
115                         }
116
117                         thisammo = min(own.(ammotype), wep.(ammotype));
118                         wep.(ammotype) = thisammo;
119                         own.(ammotype) -= thisammo;
120
121                         switch(ammotype)
122                         {
123                                 case ammo_shells:  s = sprintf("%s and %d shells", s, thisammo);  break;
124                                 case ammo_nails:   s = sprintf("%s and %d nails", s, thisammo);   break;
125                                 case ammo_rockets: s = sprintf("%s and %d rockets", s, thisammo); break;
126                                 case ammo_cells:   s = sprintf("%s and %d cells", s, thisammo);   break;
127                                 case ammo_plasma:  s = sprintf("%s and %d plasma", s, thisammo);  break;
128                                 case ammo_fuel:    s = sprintf("%s and %d fuel", s, thisammo);    break;
129                         }
130
131                         s = substring(s, 5, -1);
132                 }
133                 return s;
134         }
135 }
136
137 bool W_IsWeaponThrowable(bool w)
138 {
139         if (MUTATOR_CALLHOOK(ForbidDropCurrentWeapon))
140                 return false;
141         if (!autocvar_g_pickup_items)
142                 return false;
143         if (g_weaponarena)
144                 return 0;
145         if (g_cts)
146                 return 0;
147     if(w == WEP_Null.m_id)
148         return false;
149
150         #if 0
151         if(start_weapons & WepSet_FromWeapon(w))
152         {
153                 // start weapons that take no ammo can't be dropped (this prevents dropping the laser, as long as it continues to use no ammo)
154                 if(start_items & IT_UNLIMITED_WEAPON_AMMO)
155                         return false;
156                 if((get_weaponinfo(w)).ammo_field == ammo_none)
157                         return false;
158         }
159         return true;
160         #else
161         return (get_weaponinfo(w)).weaponthrowable;
162         #endif
163 }
164
165 // toss current weapon
166 void W_ThrowWeapon(vector velo, vector delta, float doreduce)
167 {SELFPARAM();
168         int w = self.weapon;
169         if (w == WEP_Null.m_id)
170                 return; // just in case
171         if(MUTATOR_CALLHOOK(ForbidThrowCurrentWeapon))
172                 return;
173         if(!autocvar_g_weapon_throwable)
174                 return;
175         int slot = 0; // TODO: unhardcode
176         if(self.weaponentity[slot].state != WS_READY)
177                 return;
178         if(!W_IsWeaponThrowable(w))
179                 return;
180
181         if(!(self.weapons & WepSet_FromWeapon(w)))
182                 return;
183         self.weapons &= ~WepSet_FromWeapon(w);
184
185         W_SwitchWeapon_Force(self, w_getbestweapon(self));
186         string a = W_ThrowNewWeapon(self, w, doreduce, self.origin + delta, velo);
187
188         if(!a) return;
189         Send_Notification(NOTIF_ONE, self, MSG_MULTI, ITEM_WEAPON_DROP, a, w);
190 }
191
192 void SpawnThrownWeapon(vector org, float w)
193 {SELFPARAM();
194         if(self.weapons & WepSet_FromWeapon(self.weapon))
195                 if(W_IsWeaponThrowable(self.weapon))
196                         W_ThrowNewWeapon(self, self.weapon, false, org, randomvec() * 125 + '0 0 200');
197 }