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