]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/throwing.qc
1b3bb661c3a939358a31b212287de2946a3837f4
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / weapons / throwing.qc
1 #include "throwing.qh"
2
3 #include <common/items/item.qh>
4 #include <common/mapinfo.qh>
5 #include <common/mapobjects/subs.qh>
6 #include <common/notifications/all.qh>
7 #include <common/state.qh>
8 #include <common/util.qh>
9 #include <common/weapons/_all.qh>
10 #include <common/wepent.qh>
11 #include <server/damage.qh>
12 #include <server/items/items.qh>
13 #include <server/items/spawning.qh>
14 #include <server/mutators/_mod.qh>
15 #include <server/resources.qh>
16 #include <server/weapons/selection.qh>
17 #include <server/weapons/weaponsystem.qh>
18 #include <server/world.qh>
19
20 void thrown_wep_think(entity this)
21 {
22         this.nextthink = time;
23         if(this.oldorigin != this.origin)
24         {
25                 this.SendFlags |= ISF_LOCATION;
26                 this.oldorigin = this.origin;
27         }
28         this.owner = NULL;
29         float timeleft = this.savenextthink - time;
30         if(timeleft > 1)
31                 SUB_SetFade(this, this.savenextthink - 1, 1);
32         else if(timeleft > 0)
33                 SUB_SetFade(this, time, timeleft);
34         else
35                 SUB_VanishOrRemove(this);
36 }
37
38 // returns amount of ammo used, or -1 for failure, or 0 for no ammo count
39 float W_ThrowNewWeapon(entity own, float wpn, float doreduce, vector org, vector velo, .entity weaponentity)
40 {
41         Weapon info = REGISTRY_GET(Weapons, wpn);
42         int ammotype = info.ammo_type;
43
44         entity wep = spawn();
45         Item_SetLoot(wep, true);
46         setorigin(wep, org);
47         wep.velocity = velo;
48         wep.owner = wep.enemy = own;
49         wep.flags |= FL_TOSSED;
50         wep.colormap = own.colormap;
51         wep.glowmod = weaponentity_glowmod(info, own, own.clientcolors, own.(weaponentity));
52         navigation_dynamicgoal_init(wep, false);
53
54         W_DropEvent(wr_drop,own,wpn,wep,weaponentity);
55
56         if(WepSet_FromWeapon(REGISTRY_GET(Weapons, wpn)) & WEPSET_SUPERWEAPONS)
57         {
58                 Item_SetExpiring(wep, true);
59                 if(own.items & IT_UNLIMITED_SUPERWEAPONS)
60                 {
61                         wep.superweapons_finished = time + autocvar_g_balance_superweapons_time;
62                 }
63                 else
64                 {
65                         int superweapons = 1;
66                         FOREACH(Weapons, it != WEP_Null, {
67                                 WepSet set = it.m_wepset;
68                                 if((set & WEPSET_SUPERWEAPONS) && (STAT(WEAPONS, own) & set)) ++superweapons;
69                         });
70                         if(superweapons <= 1)
71                         {
72                                 wep.superweapons_finished = STAT(SUPERWEAPONS_FINISHED, own);
73                                 STAT(SUPERWEAPONS_FINISHED, own) = 0;
74                         }
75                         else
76                         {
77                                 float timeleft = STAT(SUPERWEAPONS_FINISHED, own) - time;
78                                 float weptimeleft = timeleft / superweapons;
79                                 wep.superweapons_finished = time + weptimeleft;
80                                 STAT(SUPERWEAPONS_FINISHED, own) -= weptimeleft;
81                         }
82                 }
83         }
84
85         weapon_defaultspawnfunc(wep, info);
86         if(startitem_failed)
87                 return -1;
88         setthink(wep, thrown_wep_think);
89         wep.savenextthink = wep.nextthink;
90         wep.nextthink = min(wep.nextthink, time + 0.5);
91         wep.pickup_anyway = true; // these are ALWAYS pickable
92
93         //wa = W_AmmoItemCode(wpn);
94         if(ammotype == RES_NONE)
95         {
96                 return 0;
97         }
98         else
99         {
100                 if(doreduce && g_weapon_stay == 2)
101                 {
102                         // if our weapon is loaded, give its load back to the player
103                         int i = own.(weaponentity).m_weapon.m_id;
104                         if(own.(weaponentity).(weapon_load[i]) > 0)
105                         {
106                                 GiveResource(own, ammotype, own.(weaponentity).(weapon_load[i]));
107                                 own.(weaponentity).(weapon_load[i]) = -1; // schedule the weapon for reloading
108                         }
109                         SetResource(wep, ammotype, 0);
110                 }
111                 else if(doreduce)
112                 {
113                         // if our weapon is loaded, give its load back to the player
114                         int i = own.(weaponentity).m_weapon.m_id;
115                         if(own.(weaponentity).(weapon_load[i]) > 0)
116                         {
117                                 GiveResource(own, ammotype, own.(weaponentity).(weapon_load[i]));
118                                 own.(weaponentity).(weapon_load[i]) = -1; // schedule the weapon for reloading
119                         }
120
121                         float ownderammo = GetResource(own, ammotype);
122                         float thisammo = min(ownderammo, GetResource(wep, ammotype));
123                         SetResource(wep, ammotype, thisammo);
124                         SetResource(own, ammotype, ownderammo - thisammo);
125
126                         return thisammo;
127                 }
128                 return 0;
129         }
130 }
131
132 bool W_IsWeaponThrowable(entity this, int w)
133 {
134         if (MUTATOR_CALLHOOK(ForbidDropCurrentWeapon, this, w))
135                 return false;
136         if (!autocvar_g_pickup_items)
137                 return false;
138         if (g_weaponarena)
139                 return false;
140         if (w == WEP_Null.m_id)
141                 return false;
142
143         return (REGISTRY_GET(Weapons, w)).weaponthrowable;
144 }
145
146 // toss current weapon
147 void W_ThrowWeapon(entity this, .entity weaponentity, vector velo, vector delta, float doreduce)
148 {
149         Weapon w = this.(weaponentity).m_weapon;
150         if (w == WEP_Null)
151                 return; // just in case
152         if (time < game_starttime)
153                 return;
154         if(MUTATOR_CALLHOOK(ForbidThrowCurrentWeapon, this, this.(weaponentity)))
155                 return;
156         if(!autocvar_g_weapon_throwable)
157                 return;
158         if(this.(weaponentity).state != WS_READY)
159                 return;
160         if(!W_IsWeaponThrowable(this, w.m_id))
161                 return;
162
163         WepSet set = WepSet_FromWeapon(w);
164         if(!(STAT(WEAPONS, this) & set)) return;
165         STAT(WEAPONS, this) &= ~set;
166
167         W_SwitchWeapon_Force(this, w_getbestweapon(this, weaponentity), weaponentity);
168         float a = W_ThrowNewWeapon(this, w.m_id, doreduce, this.origin + delta, velo, weaponentity);
169
170         if(a < 0) return;
171         Send_Notification(NOTIF_ONE, this, MSG_MULTI, ITEM_WEAPON_DROP, w.m_id, a);
172 }
173
174 void SpawnThrownWeapon(entity this, vector org, Weapon wep, .entity weaponentity)
175 {
176         //entity wep = this.(weaponentity).m_weapon;
177
178         if(STAT(WEAPONS, this) & WepSet_FromWeapon(wep))
179                 if(W_IsWeaponThrowable(this, wep.m_id))
180                         W_ThrowNewWeapon(this, wep.m_id, false, org, randomvec() * 125 + '0 0 200', weaponentity);
181 }