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