]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/throwing.qc
More work on switching over to using entity field instead of item field
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / weapons / throwing.qc
1 void thrown_wep_think()
2 {
3         self.nextthink = time;
4         if(self.oldorigin != self.origin)
5         {
6                 self.SendFlags |= ISF_LOCATION;
7                 self.oldorigin = self.origin;
8         }
9         self.owner = world;
10         float timeleft = self.savenextthink - time;
11         if(timeleft > 1)
12                 SUB_SetFade(self, self.savenextthink - 1, 1);
13         else if(timeleft > 0)
14                 SUB_SetFade(self, time, timeleft);
15         else
16                 SUB_VanishOrRemove(self);
17 }
18
19 // returns amount of ammo used as string, or -1 for failure, or 0 for no ammo count
20 string W_ThrowNewWeapon(entity own, float wpn, float doreduce, vector org, vector velo)
21 {
22         entity oldself, wep;
23         float thisammo, i;
24         string s;
25         var .float ammofield = (get_weaponinfo(wpn)).current_ammo;
26
27         wep = spawn();
28
29         setorigin(wep, org);
30         wep.classname = "droppedweapon";
31         wep.velocity = velo;
32         wep.owner = wep.enemy = own;
33         wep.flags |= FL_TOSSED;
34         wep.colormap = own.colormap;
35
36         if(WepSet_FromWeapon(wpn) & WEPSET_SUPERWEAPONS)
37         {
38                 if(own.items & IT_UNLIMITED_SUPERWEAPONS)
39                 {
40                         wep.superweapons_finished = time + autocvar_g_balance_superweapons_time;
41                 }
42                 else
43                 {
44                         float superweapons = 1;
45                         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
46                                 if(WepSet_FromWeapon(i) & WEPSET_SUPERWEAPONS)
47                                         if(own.weapons & WepSet_FromWeapon(i))
48                                                 ++superweapons;
49                         if(superweapons <= 1)
50                         {
51                                 wep.superweapons_finished = own.superweapons_finished;
52                                 own.superweapons_finished = 0;
53                         }
54                         else
55                         {
56                                 float timeleft = own.superweapons_finished - time;
57                                 float weptimeleft = timeleft / superweapons;
58                                 wep.superweapons_finished = time + weptimeleft;
59                                 own.superweapons_finished -= weptimeleft;
60                         }
61                 }
62         }
63
64         //wa = W_AmmoItemCode(wpn);
65         if(ammofield == ammo_none)
66         {
67                 oldself = self;
68                 self = wep;
69                 weapon_defaultspawnfunc(wpn);
70                 self = oldself;
71                 if(startitem_failed)
72                         return string_null;
73                 wep.glowmod = own.weaponentity_glowmod;
74                 wep.think = thrown_wep_think;
75                 wep.savenextthink = wep.nextthink;
76                 wep.nextthink = min(wep.nextthink, time + 0.5);
77                 wep.pickup_anyway = TRUE; // these are ALWAYS pickable
78                 return "";
79         }
80         else
81         {
82                 s = "";
83                 oldself = self;
84                 self = wep;
85                 weapon_defaultspawnfunc(wpn);
86                 self = oldself;
87                 if(startitem_failed)
88                         return string_null;
89                 if(doreduce && g_weapon_stay == 2)
90                 {
91                         // if our weapon is loaded, give its load back to the player
92                         if(self.(weapon_load[self.weapon]) > 0)
93                         {
94                                 own.ammofield += self.(weapon_load[self.weapon]);
95                                 self.(weapon_load[self.weapon]) = -1; // schedule the weapon for reloading
96                         }
97
98                         wep.ammofield = 0;
99                 }
100                 else if(doreduce)
101                 {
102                         // if our weapon is loaded, give its load back to the player
103                         if(self.(weapon_load[self.weapon]) > 0)
104                         {
105                                 own.ammofield += self.(weapon_load[self.weapon]);
106                                 self.(weapon_load[self.weapon]) = -1; // schedule the weapon for reloading
107                         }
108
109                         // stuff
110                         thisammo = min(own.ammofield, wep.ammofield);
111                         wep.ammofield = thisammo;
112                         own.ammofield -= thisammo;
113
114                         //print(sprintf("W_ThrowNewWeapon: wep ammo count: %f, own ammo count: %f, thisammo = %f\n", wep.ammofield, own.ammofield, thisammo));
115
116                         switch(ammofield)
117                         {
118                                 case ammo_shells:  s = sprintf("%s and %d shells", s, thisammo);  break;
119                                 case ammo_nails:   s = sprintf("%s and %d nails", s, thisammo);   break;
120                                 case ammo_rockets: s = sprintf("%s and %d rockets", s, thisammo); break;
121                                 case ammo_cells:   s = sprintf("%s and %d cells", s, thisammo);   break;
122                                 case ammo_fuel:    s = sprintf("%s and %d fuel", s, thisammo);    break;
123                         }
124
125                         s = substring(s, 5, -1);
126                 }
127                 wep.glowmod = own.weaponentity_glowmod;
128                 wep.think = thrown_wep_think;
129                 wep.savenextthink = wep.nextthink;
130                 wep.nextthink = min(wep.nextthink, time + 0.5);
131                 wep.pickup_anyway = TRUE; // these are ALWAYS pickable
132
133                 return s;
134         }
135 }
136
137 float W_IsWeaponThrowable(float w)
138 {
139         if (!autocvar_g_pickup_items)
140                 return 0;
141         if (g_weaponarena)
142                 return 0;
143         if (g_cts)
144                 return 0;
145         if (g_nexball && w == WEP_MORTAR)
146                 return 0;
147     if(w == 0)
148         return 0;
149         
150         if(start_weapons & WepSet_FromWeapon(w))
151         {
152                 // start weapons that take no ammo can't be dropped (this prevents dropping the laser, as long as it continues to use no ammo)
153                 if(start_items & IT_UNLIMITED_WEAPON_AMMO)
154                         return 0;
155                 if((get_weaponinfo(w)).current_ammo == ammo_none)
156                         return 0;
157         }
158
159         return 1;
160 }
161
162 // toss current weapon
163 void W_ThrowWeapon(vector velo, vector delta, float doreduce)
164 {
165         float w;
166         string a;
167
168         w = self.weapon;
169         if (w == 0)
170                 return; // just in case
171         if(MUTATOR_CALLHOOK(ForbidThrowCurrentWeapon))
172                 return;
173         if(!autocvar_g_weapon_throwable)
174                 return;
175         if(self.weaponentity.state != WS_READY)
176                 return;
177         if(!W_IsWeaponThrowable(w))
178                 return;
179
180         if(!(self.weapons & WepSet_FromWeapon(w)))
181                 return;
182         self.weapons &= ~WepSet_FromWeapon(w);
183
184         W_SwitchWeapon_Force(self, w_getbestweapon(self));
185         a = W_ThrowNewWeapon(self, w, doreduce, self.origin + delta, velo);
186         
187         if(!a) return;
188         Send_Notification(NOTIF_ONE, self, MSG_MULTI, ITEM_WEAPON_DROP, a, w);
189 }