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