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