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