]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/throwing.qc
Merge branch 'master' into TimePath/modules
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / weapons / throwing.qc
1 #include "throwing.qh"
2
3 #include "weaponsystem.qh"
4 #include "../mutators/_mod.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/all.qh>
10 #include <common/triggers/subs.qh>
11 #include <common/util.qh>
12 #include <common/weapons/_all.qh>
13 #include <common/state.qh>
14
15 void thrown_wep_think(entity this)
16 {
17         this.nextthink = time;
18         if(this.oldorigin != this.origin)
19         {
20                 this.SendFlags |= ISF_LOCATION;
21                 this.oldorigin = this.origin;
22         }
23         this.owner = NULL;
24         float timeleft = this.savenextthink - time;
25         if(timeleft > 1)
26                 SUB_SetFade(this, this.savenextthink - 1, 1);
27         else if(timeleft > 0)
28                 SUB_SetFade(this, time, timeleft);
29         else
30                 SUB_VanishOrRemove(this);
31 }
32
33 // returns amount of ammo used as string, or -1 for failure, or 0 for no ammo count
34 string W_ThrowNewWeapon(entity own, float wpn, float doreduce, vector org, vector velo)
35 {
36         float thisammo;
37         string s;
38         Weapon info = Weapons_from(wpn);
39         var .int ammotype = info.ammo_field;
40
41         entity wep = new(droppedweapon);
42
43         setorigin(wep, org);
44         wep.velocity = velo;
45         wep.owner = wep.enemy = own;
46         wep.flags |= FL_TOSSED;
47         wep.colormap = own.colormap;
48         wep.glowmod = weaponentity_glowmod(info, own.clientcolors);
49
50         W_DropEvent(wr_drop,own,wpn,wep);
51
52         if(WepSet_FromWeapon(Weapons_from(wpn)) & WEPSET_SUPERWEAPONS)
53         {
54                 if(own.items & IT_UNLIMITED_SUPERWEAPONS)
55                 {
56                         wep.superweapons_finished = time + autocvar_g_balance_superweapons_time;
57                 }
58                 else
59                 {
60                         int superweapons = 1;
61                         FOREACH(Weapons, it != WEP_Null, LAMBDA(
62                                 WepSet set = it.m_wepset;
63                                 if((set & WEPSET_SUPERWEAPONS) && (own.weapons & set)) ++superweapons;
64                         ));
65                         if(superweapons <= 1)
66                         {
67                                 wep.superweapons_finished = own.superweapons_finished;
68                                 own.superweapons_finished = 0;
69                         }
70                         else
71                         {
72                                 float timeleft = own.superweapons_finished - time;
73                                 float weptimeleft = timeleft / superweapons;
74                                 wep.superweapons_finished = time + weptimeleft;
75                                 own.superweapons_finished -= weptimeleft;
76                         }
77                 }
78         }
79
80         weapon_defaultspawnfunc(wep, info);
81         if(startitem_failed)
82                 return string_null;
83         setthink(wep, thrown_wep_think);
84         wep.savenextthink = wep.nextthink;
85         wep.nextthink = min(wep.nextthink, time + 0.5);
86         wep.pickup_anyway = true; // these are ALWAYS pickable
87
88         //wa = W_AmmoItemCode(wpn);
89         if(ammotype == ammo_none)
90         {
91                 return "";
92         }
93         else
94         {
95                 s = "";
96
97                 if(doreduce && g_weapon_stay == 2)
98                 {
99                         // if our weapon is loaded, give its load back to the player
100                         int i = PS(own).m_weapon.m_id;
101                         if(own.(weapon_load[i]) > 0)
102                         {
103                                 own.(ammotype) += own.(weapon_load[i]);
104                                 own.(weapon_load[i]) = -1; // schedule the weapon for reloading
105                         }
106
107                         wep.(ammotype) = 0;
108                 }
109                 else if(doreduce)
110                 {
111                         // if our weapon is loaded, give its load back to the player
112                         int i = PS(own).m_weapon.m_id;
113                         if(own.(weapon_load[i]) > 0)
114                         {
115                                 own.(ammotype) += own.(weapon_load[i]);
116                                 own.(weapon_load[i]) = -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 bool W_IsWeaponThrowable(entity this, int w)
140 {
141         if (MUTATOR_CALLHOOK(ForbidDropCurrentWeapon, this))
142                 return false;
143         if (!autocvar_g_pickup_items)
144                 return false;
145         if (g_weaponarena)
146                 return 0;
147         if (g_cts)
148                 return 0;
149     if(w == WEP_Null.m_id)
150         return false;
151
152         #if 0
153         if(start_weapons & WepSet_FromWeapon(Weapons_from(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 false;
158                 if((Weapons_from(w)).ammo_field == ammo_none)
159                         return false;
160         }
161         return true;
162         #else
163         return (Weapons_from(w)).weaponthrowable;
164         #endif
165 }
166
167 // toss current weapon
168 void W_ThrowWeapon(entity this, .entity weaponentity, vector velo, vector delta, float doreduce)
169 {
170         Weapon w = PS(this).m_weapon;
171         if (w == WEP_Null)
172                 return; // just in case
173         if(MUTATOR_CALLHOOK(ForbidThrowCurrentWeapon, this))
174                 return;
175         if(!autocvar_g_weapon_throwable)
176                 return;
177         if(this.(weaponentity).state != WS_READY)
178                 return;
179         if(!W_IsWeaponThrowable(this, w.m_id))
180                 return;
181
182         WepSet set = WepSet_FromWeapon(w);
183         if(!(this.weapons & set)) return;
184         this.weapons &= ~set;
185
186         W_SwitchWeapon_Force(this, w_getbestweapon(this));
187         string a = W_ThrowNewWeapon(this, w.m_id, doreduce, this.origin + delta, velo);
188
189         if(!a) return;
190         Send_Notification(NOTIF_ONE, this, MSG_MULTI, ITEM_WEAPON_DROP, a, w.m_id);
191 }
192
193 void SpawnThrownWeapon(entity this, vector org, float w)
194 {
195         if(this.weapons & WepSet_FromWeapon(PS(this).m_weapon))
196                 if(W_IsWeaponThrowable(this, PS(this).m_weapon.m_id))
197                         W_ThrowNewWeapon(this, PS(this).m_weapon.m_id, false, org, randomvec() * 125 + '0 0 200');
198 }