1e0f9c02fbbc4ebe1405bc780f92ce3fc6560ff1
[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 "../../common/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;
36         string s;
37         Weapon info = Weapons_from(wpn);
38         var .int ammotype = info.ammo_field;
39
40         entity wep = new(droppedweapon);
41
42         setorigin(wep, org);
43         wep.velocity = velo;
44         wep.owner = wep.enemy = own;
45         wep.flags |= FL_TOSSED;
46         wep.colormap = own.colormap;
47
48         W_DropEvent(wr_drop,own,wpn,wep);
49
50         if(WepSet_FromWeapon(Weapons_from(wpn)) & WEPSET_SUPERWEAPONS)
51         {
52                 if(own.items & IT_UNLIMITED_SUPERWEAPONS)
53                 {
54                         wep.superweapons_finished = time + autocvar_g_balance_superweapons_time;
55                 }
56                 else
57                 {
58                         int superweapons = 1;
59                         FOREACH(Weapons, it != WEP_Null, LAMBDA(
60                                 WepSet set = it.m_wepset;
61                                 if((set & WEPSET_SUPERWEAPONS) && (own.weapons & set)) ++superweapons;
62                         ));
63                         if(superweapons <= 1)
64                         {
65                                 wep.superweapons_finished = own.superweapons_finished;
66                                 own.superweapons_finished = 0;
67                         }
68                         else
69                         {
70                                 float timeleft = own.superweapons_finished - time;
71                                 float weptimeleft = timeleft / superweapons;
72                                 wep.superweapons_finished = time + weptimeleft;
73                                 own.superweapons_finished -= weptimeleft;
74                         }
75                 }
76         }
77
78         weapon_defaultspawnfunc(wep, info);
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                         int i = PS(self).m_weapon.m_id;
100                         if(self.(weapon_load[i]) > 0)
101                         {
102                                 own.(ammotype) += self.(weapon_load[i]);
103                                 self.(weapon_load[i]) = -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                         int i = PS(self).m_weapon.m_id;
112                         if(self.(weapon_load[i]) > 0)
113                         {
114                                 own.(ammotype) += self.(weapon_load[i]);
115                                 self.(weapon_load[i]) = -1; // schedule the weapon for reloading
116                         }
117
118                         thisammo = min(own.(ammotype), wep.(ammotype));
119                         wep.(ammotype) = thisammo;
120                         own.(ammotype) -= thisammo;
121
122                         switch(ammotype)
123                         {
124                                 case ammo_shells:  s = sprintf("%s and %d shells", s, thisammo);  break;
125                                 case ammo_nails:   s = sprintf("%s and %d nails", s, thisammo);   break;
126                                 case ammo_rockets: s = sprintf("%s and %d rockets", s, thisammo); break;
127                                 case ammo_cells:   s = sprintf("%s and %d cells", s, thisammo);   break;
128                                 case ammo_plasma:  s = sprintf("%s and %d plasma", s, thisammo);  break;
129                                 case ammo_fuel:    s = sprintf("%s and %d fuel", s, thisammo);    break;
130                         }
131
132                         s = substring(s, 5, -1);
133                 }
134                 return s;
135         }
136 }
137
138 bool W_IsWeaponThrowable(bool w)
139 {
140         if (MUTATOR_CALLHOOK(ForbidDropCurrentWeapon))
141                 return false;
142         if (!autocvar_g_pickup_items)
143                 return false;
144         if (g_weaponarena)
145                 return 0;
146         if (g_cts)
147                 return 0;
148     if(w == WEP_Null.m_id)
149         return false;
150
151         #if 0
152         if(start_weapons & WepSet_FromWeapon(Weapons_from(w)))
153         {
154                 // start weapons that take no ammo can't be dropped (this prevents dropping the laser, as long as it continues to use no ammo)
155                 if(start_items & IT_UNLIMITED_WEAPON_AMMO)
156                         return false;
157                 if((Weapons_from(w)).ammo_field == ammo_none)
158                         return false;
159         }
160         return true;
161         #else
162         return (Weapons_from(w)).weaponthrowable;
163         #endif
164 }
165
166 // toss current weapon
167 void W_ThrowWeapon(vector velo, vector delta, float doreduce)
168 {SELFPARAM();
169         Weapon w = PS(self).m_weapon;
170         if (w == WEP_Null)
171                 return; // just in case
172         if(MUTATOR_CALLHOOK(ForbidThrowCurrentWeapon))
173                 return;
174         if(!autocvar_g_weapon_throwable)
175                 return;
176         .entity weaponentity = weaponentities[0]; // TODO: unhardcode
177         if(self.(weaponentity).state != WS_READY)
178                 return;
179         if(!W_IsWeaponThrowable(w.m_id))
180                 return;
181
182         WepSet set = WepSet_FromWeapon(w);
183         if(!(self.weapons & set)) return;
184         self.weapons &= ~set;
185
186         W_SwitchWeapon_Force(self, w_getbestweapon(self));
187         string a = W_ThrowNewWeapon(self, w.m_id, doreduce, self.origin + delta, velo);
188
189         if(!a) return;
190         Send_Notification(NOTIF_ONE, self, MSG_MULTI, ITEM_WEAPON_DROP, a, w.m_id);
191 }
192
193 void SpawnThrownWeapon(vector org, float w)
194 {SELFPARAM();
195         if(self.weapons & WepSet_FromWeapon(PS(self).m_weapon))
196                 if(W_IsWeaponThrowable(PS(self).m_weapon.m_id))
197                         W_ThrowNewWeapon(self, PS(self).m_weapon.m_id, false, org, randomvec() * 125 + '0 0 200');
198 }