]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/throwing.qc
Entity debugger
[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         var .int ammotype = (get_weaponinfo(wpn)).ammo_field;
38
39         entity wep = new(droppedweapon);
40
41         setorigin(wep, org);
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         WITH(entity, self, wep, weapon_defaultspawnfunc(wpn));
78         if(startitem_failed)
79                 return string_null;
80         wep.glowmod = own.weaponentity_glowmod;
81         wep.think = thrown_wep_think;
82         wep.savenextthink = wep.nextthink;
83         wep.nextthink = min(wep.nextthink, time + 0.5);
84         wep.pickup_anyway = true; // these are ALWAYS pickable
85
86         //wa = W_AmmoItemCode(wpn);
87         if(ammotype == ammo_none)
88         {
89                 return "";
90         }
91         else
92         {
93                 s = "";
94
95                 if(doreduce && g_weapon_stay == 2)
96                 {
97                         // if our weapon is loaded, give its load back to the player
98                         if(self.(weapon_load[self.weapon]) > 0)
99                         {
100                                 own.(ammotype) += self.(weapon_load[self.weapon]);
101                                 self.(weapon_load[self.weapon]) = -1; // schedule the weapon for reloading
102                         }
103
104                         wep.(ammotype) = 0;
105                 }
106                 else if(doreduce)
107                 {
108                         // if our weapon is loaded, give its load back to the player
109                         if(self.(weapon_load[self.weapon]) > 0)
110                         {
111                                 own.(ammotype) += self.(weapon_load[self.weapon]);
112                                 self.(weapon_load[self.weapon]) = -1; // schedule the weapon for reloading
113                         }
114
115                         thisammo = min(own.(ammotype), wep.(ammotype));
116                         wep.(ammotype) = thisammo;
117                         own.(ammotype) -= thisammo;
118
119                         switch(ammotype)
120                         {
121                                 case ammo_shells:  s = sprintf("%s and %d shells", s, thisammo);  break;
122                                 case ammo_nails:   s = sprintf("%s and %d nails", s, thisammo);   break;
123                                 case ammo_rockets: s = sprintf("%s and %d rockets", s, thisammo); break;
124                                 case ammo_cells:   s = sprintf("%s and %d cells", s, thisammo);   break;
125                                 case ammo_plasma:  s = sprintf("%s and %d plasma", s, thisammo);  break;
126                                 case ammo_fuel:    s = sprintf("%s and %d fuel", s, thisammo);    break;
127                         }
128
129                         s = substring(s, 5, -1);
130                 }
131                 return s;
132         }
133 }
134
135 bool W_IsWeaponThrowable(bool w)
136 {
137         if (MUTATOR_CALLHOOK(ForbidDropCurrentWeapon))
138                 return false;
139         if (!autocvar_g_pickup_items)
140                 return false;
141         if (g_weaponarena)
142                 return 0;
143         if (g_cts)
144                 return 0;
145     if(w == WEP_Null.m_id)
146         return false;
147
148         #if 0
149         if(start_weapons & WepSet_FromWeapon(w))
150         {
151                 // start weapons that take no ammo can't be dropped (this prevents dropping the laser, as long as it continues to use no ammo)
152                 if(start_items & IT_UNLIMITED_WEAPON_AMMO)
153                         return false;
154                 if((get_weaponinfo(w)).ammo_field == ammo_none)
155                         return false;
156         }
157         return true;
158         #else
159         return (get_weaponinfo(w)).weaponthrowable;
160         #endif
161 }
162
163 // toss current weapon
164 void W_ThrowWeapon(vector velo, vector delta, float doreduce)
165 {SELFPARAM();
166         int w = self.weapon;
167         if (w == WEP_Null.m_id)
168                 return; // just in case
169         if(MUTATOR_CALLHOOK(ForbidThrowCurrentWeapon))
170                 return;
171         if(!autocvar_g_weapon_throwable)
172                 return;
173         int slot = 0; // TODO: unhardcode
174         if(self.weaponentity[slot].state != WS_READY)
175                 return;
176         if(!W_IsWeaponThrowable(w))
177                 return;
178
179         if(!(self.weapons & WepSet_FromWeapon(w)))
180                 return;
181         self.weapons &= ~WepSet_FromWeapon(w);
182
183         W_SwitchWeapon_Force(self, w_getbestweapon(self));
184         string a = W_ThrowNewWeapon(self, w, doreduce, self.origin + delta, velo);
185
186         if(!a) return;
187         Send_Notification(NOTIF_ONE, self, MSG_MULTI, ITEM_WEAPON_DROP, a, w);
188 }
189
190 void SpawnThrownWeapon(vector org, float w)
191 {SELFPARAM();
192         if(self.weapons & WepSet_FromWeapon(self.weapon))
193                 if(W_IsWeaponThrowable(self.weapon))
194                         W_ThrowNewWeapon(self, self.weapon, false, org, randomvec() * 125 + '0 0 200');
195 }