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