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