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