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