]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/throwing.qc
462af14d8e0ca151f366f9daa611f1caf3c00cc7
[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 "../items.qh"
6 #include <server/mutators/_mod.qh>
7 #include <common/t_items.qh>
8 #include "../g_damage.qh"
9 #include <common/items/item.qh>
10 #include <common/mapinfo.qh>
11 #include <common/notifications/all.qh>
12 #include <common/mapobjects/subs.qh>
13 #include <common/util.qh>
14 #include <common/weapons/_all.qh>
15 #include <common/state.qh>
16 #include <common/wepent.qh>
17
18 void thrown_wep_think(entity this)
19 {
20         this.nextthink = time;
21         if(this.oldorigin != this.origin)
22         {
23                 this.SendFlags |= ISF_LOCATION;
24                 this.oldorigin = this.origin;
25         }
26         this.owner = NULL;
27         float timeleft = this.savenextthink - time;
28         if(timeleft > 1)
29                 SUB_SetFade(this, this.savenextthink - 1, 1);
30         else if(timeleft > 0)
31                 SUB_SetFade(this, time, timeleft);
32         else
33                 SUB_VanishOrRemove(this);
34 }
35
36 // returns amount of ammo used as string, or -1 for failure, or 0 for no ammo count
37 string W_ThrowNewWeapon(entity own, float wpn, float doreduce, vector org, vector velo, .entity weaponentity)
38 {
39         float thisammo;
40         string s;
41         Weapon info = Weapons_from(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(Weapons_from(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 = own.superweapons_finished;
73                                 own.superweapons_finished = 0;
74                         }
75                         else
76                         {
77                                 float timeleft = own.superweapons_finished - time;
78                                 float weptimeleft = timeleft / superweapons;
79                                 wep.superweapons_finished = time + weptimeleft;
80                                 own.superweapons_finished -= weptimeleft;
81                         }
82                 }
83         }
84
85         weapon_defaultspawnfunc(wep, info);
86         if(startitem_failed)
87                 return string_null;
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 == RESOURCE_NONE)
95         {
96                 return "";
97         }
98         else
99         {
100                 s = "";
101
102                 if(doreduce && g_weapon_stay == 2)
103                 {
104                         // if our weapon is loaded, give its load back to the player
105                         int i = own.(weaponentity).m_weapon.m_id;
106                         if(own.(weaponentity).(weapon_load[i]) > 0)
107                         {
108                                 GiveResource(own, ammotype, own.(weaponentity).(weapon_load[i]));
109                                 own.(weaponentity).(weapon_load[i]) = -1; // schedule the weapon for reloading
110                         }
111                         SetResourceAmount(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                                 GiveResource(own, ammotype, own.(weaponentity).(weapon_load[i]));
120                                 own.(weaponentity).(weapon_load[i]) = -1; // schedule the weapon for reloading
121                         }
122
123                         float ownderammo = GetResourceAmount(own, ammotype);
124                         thisammo = min(ownderammo, GetResourceAmount(wep, ammotype));
125                         SetResourceAmount(wep, ammotype, thisammo);
126                         SetResourceAmount(own, ammotype, ownderammo - thisammo);
127
128                         switch (ammotype)
129                         {
130                                 case RESOURCE_SHELLS:  s = sprintf("%s and %d shells", s, thisammo);  break;
131                                 case RESOURCE_BULLETS: s = sprintf("%s and %d nails", s, thisammo);   break;
132                                 case RESOURCE_ROCKETS: s = sprintf("%s and %d rockets", s, thisammo); break;
133                                 case RESOURCE_CELLS:   s = sprintf("%s and %d cells", s, thisammo);   break;
134                                 case RESOURCE_PLASMA:  s = sprintf("%s and %d plasma", s, thisammo);  break;
135                                 case RESOURCE_FUEL:    s = sprintf("%s and %d fuel", s, thisammo);    break;
136                         }
137
138                         s = substring(s, 5, -1);
139                 }
140                 return s;
141         }
142 }
143
144 bool W_IsWeaponThrowable(entity this, int w)
145 {
146         if (MUTATOR_CALLHOOK(ForbidDropCurrentWeapon, this, w))
147                 return false;
148         if (!autocvar_g_pickup_items)
149                 return false;
150         if (g_weaponarena)
151                 return 0;
152     if(w == WEP_Null.m_id)
153         return false;
154
155         return (Weapons_from(w)).weaponthrowable;
156 }
157
158 // toss current weapon
159 void W_ThrowWeapon(entity this, .entity weaponentity, vector velo, vector delta, float doreduce)
160 {
161         Weapon w = this.(weaponentity).m_weapon;
162         if (w == WEP_Null)
163                 return; // just in case
164         if(MUTATOR_CALLHOOK(ForbidThrowCurrentWeapon, this, this.(weaponentity)))
165                 return;
166         if(!autocvar_g_weapon_throwable)
167                 return;
168         if(this.(weaponentity).state != WS_READY)
169                 return;
170         if(!W_IsWeaponThrowable(this, w.m_id))
171                 return;
172
173         WepSet set = WepSet_FromWeapon(w);
174         if(!(STAT(WEAPONS, this) & set)) return;
175         STAT(WEAPONS, this) &= ~set;
176
177         W_SwitchWeapon_Force(this, w_getbestweapon(this, weaponentity), weaponentity);
178         string a = W_ThrowNewWeapon(this, w.m_id, doreduce, this.origin + delta, velo, weaponentity);
179
180         if(!a) return;
181         Send_Notification(NOTIF_ONE, this, MSG_MULTI, ITEM_WEAPON_DROP, a, w.m_id);
182 }
183
184 void SpawnThrownWeapon(entity this, vector org, Weapon wep, .entity weaponentity)
185 {
186         //entity wep = this.(weaponentity).m_weapon;
187
188         if(STAT(WEAPONS, this) & WepSet_FromWeapon(wep))
189                 if(W_IsWeaponThrowable(this, wep.m_id))
190                         W_ThrowNewWeapon(this, wep.m_id, false, org, randomvec() * 125 + '0 0 200', weaponentity);
191 }