soundlist: set initial nItems value to 0 and do a small code cleanup
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mutators / mutator / overkill / overkill.qc
1 #ifdef IMPLEMENTATION
2 bool autocvar_g_overkill_powerups_replace;
3 float autocvar_g_overkill_superguns_respawn_time;
4 bool autocvar_g_overkill_100h_anyway;
5 bool autocvar_g_overkill_100a_anyway;
6 bool autocvar_g_overkill_ammo_charge;
7 float autocvar_g_overkill_ammo_charge_notice;
8 float autocvar_g_overkill_ammo_charge_limit;
9
10 .vector ok_deathloc;
11 .float ok_spawnsys_timer;
12 .float ok_lastwep;
13 .float ok_item;
14
15 .float ok_notice_time;
16 .float ammo_charge[Weapons_MAX];
17 .float ok_use_ammocharge = _STAT(OK_AMMO_CHARGE);
18 .float ok_ammo_charge = _STAT(OK_AMMO_CHARGEPOOL);
19
20 .float ok_pauseregen_finished;
21
22 void(entity ent, float wep) ok_DecreaseCharge;
23
24 void ok_Initialize();
25
26 REGISTER_MUTATOR(ok, cvar("g_overkill") && !cvar("g_instagib") && !g_nexball && cvar_string("g_mod_balance") == "Overkill")
27 {
28         MUTATOR_ONADD
29         {
30                 ok_Initialize();
31         }
32
33         MUTATOR_ONREMOVE
34         {
35                 WEP_RPC.spawnflags |= WEP_FLAG_MUTATORBLOCKED;
36                 WEP_HMG.spawnflags |= WEP_FLAG_MUTATORBLOCKED;
37         }
38
39         return false;
40 }
41
42 MUTATOR_HOOKFUNCTION(ok, W_DecreaseAmmo)
43 {
44         entity actor = MUTATOR_ARGV(0, entity);
45         if (actor.ok_use_ammocharge)
46         {
47                 ok_DecreaseCharge(actor, actor.weapon);
48                 return true;
49         }
50 }
51
52 MUTATOR_HOOKFUNCTION(ok, W_Reload)
53 {
54         entity actor = MUTATOR_ARGV(0, entity);
55         return actor.ok_use_ammocharge;
56 }
57
58 void W_Blaster_Attack(entity, float, float, float, float, float, float, float, float, float, float);
59 spawnfunc(weapon_hmg);
60 spawnfunc(weapon_rpc);
61
62 void ok_DecreaseCharge(entity ent, int wep)
63 {
64         if(!ent.ok_use_ammocharge) return;
65
66         entity wepent = Weapons_from(wep);
67
68         if(wepent.weapon == 0)
69                 return; // dummy
70
71         ent.ammo_charge[wep] -= max(0, cvar(sprintf("g_overkill_ammo_decharge_%s", wepent.netname)));
72 }
73
74 void ok_IncreaseCharge(entity ent, int wep)
75 {
76         entity wepent = Weapons_from(wep);
77
78         if(wepent.weapon == 0)
79                 return; // dummy
80
81         if(ent.ok_use_ammocharge)
82         if(!ent.BUTTON_ATCK) // not while attacking?
83                 ent.ammo_charge[wep] = min(autocvar_g_overkill_ammo_charge_limit, ent.ammo_charge[wep] + cvar(sprintf("g_overkill_ammo_charge_rate_%s", wepent.netname)) * frametime / W_TICSPERFRAME);
84 }
85
86 float ok_CheckWeaponCharge(entity ent, int wep)
87 {
88         if(!ent.ok_use_ammocharge) return true;
89
90         entity wepent = Weapons_from(wep);
91
92         if(wepent.weapon == 0)
93                 return 0; // dummy
94
95         return (ent.ammo_charge[wep] >= cvar(sprintf("g_overkill_ammo_decharge_%s", wepent.netname)));
96 }
97
98 MUTATOR_HOOKFUNCTION(ok, PlayerDamage_Calculate, CBC_ORDER_LAST)
99 {
100         if(IS_PLAYER(frag_attacker) && IS_PLAYER(frag_target))
101         if(DEATH_ISWEAPON(frag_deathtype, WEP_BLASTER))
102         {
103                 frag_damage = 0;
104
105                 if(frag_attacker != frag_target)
106                 if(frag_target.health > 0)
107                 if(frag_target.frozen == 0)
108                 if(frag_target.deadflag == DEAD_NO)
109                 {
110                         Send_Notification(NOTIF_ONE, frag_attacker, MSG_CENTER, CENTER_SECONDARY_NODAMAGE);
111                         frag_force = '0 0 0';
112                 }
113         }
114
115         return false;
116 }
117
118 MUTATOR_HOOKFUNCTION(ok, PlayerDamage_SplitHealthArmor)
119 {SELFPARAM();
120         if(damage_take)
121                 self.ok_pauseregen_finished = max(self.ok_pauseregen_finished, time + 2);
122         return false;
123 }
124
125 MUTATOR_HOOKFUNCTION(ok, PlayerDies)
126 {SELFPARAM();
127         entity targ = ((frag_attacker) ? frag_attacker : frag_target);
128
129         if(IS_MONSTER(self))
130         {
131                 remove(other); // remove default item
132                 other = world;
133         }
134
135         setself(new(droppedweapon)); // hax
136         self.ok_item = true;
137         self.noalign = true;
138         self.pickup_anyway = true;
139         spawnfunc_item_armor_small(this);
140         self.movetype = MOVETYPE_TOSS;
141         self.gravity = 1;
142         self.reset = SUB_Remove;
143         setorigin(self, frag_target.origin + '0 0 32');
144         self.velocity = '0 0 200' + normalize(targ.origin - self.origin) * 500;
145         SUB_SetFade(self, time + 5, 1);
146         setself(this);
147
148         self.ok_lastwep = self.switchweapon;
149
150         return false;
151 }
152 MUTATOR_HOOKFUNCTION(ok, MonsterDropItem) { ok_PlayerDies(); }
153
154 MUTATOR_HOOKFUNCTION(ok, PlayerRegen)
155 {SELFPARAM();
156         // overkill's values are different, so use custom regen
157         if(!self.frozen)
158         {
159                 self.armorvalue = CalcRotRegen(self.armorvalue, autocvar_g_balance_armor_regenstable, autocvar_g_balance_armor_regen, autocvar_g_balance_armor_regenlinear, 1 * frametime * (time > self.ok_pauseregen_finished), 0, 0, 1, 1 * frametime * (time > self.pauserotarmor_finished), autocvar_g_balance_armor_limit);
160                 self.health = CalcRotRegen(self.health, autocvar_g_balance_health_regenstable, 0, 100, 1 * frametime * (time > self.ok_pauseregen_finished), 200, 0, autocvar_g_balance_health_rotlinear, 1 * frametime * (time > self.pauserothealth_finished), autocvar_g_balance_health_limit);
161
162                 float minf, maxf, limitf;
163
164                 maxf = autocvar_g_balance_fuel_rotstable;
165                 minf = autocvar_g_balance_fuel_regenstable;
166                 limitf = autocvar_g_balance_fuel_limit;
167
168                 self.ammo_fuel = CalcRotRegen(self.ammo_fuel, minf, autocvar_g_balance_fuel_regen, autocvar_g_balance_fuel_regenlinear, frametime * (time > self.pauseregen_finished) * ((self.items & ITEM_JetpackRegen.m_itemid) != 0), maxf, autocvar_g_balance_fuel_rot, autocvar_g_balance_fuel_rotlinear, frametime * (time > self.pauserotfuel_finished), limitf);
169         }
170         return true; // return true anyway, as frozen uses no regen
171 }
172
173 MUTATOR_HOOKFUNCTION(ok, ForbidThrowCurrentWeapon)
174 {
175         return true;
176 }
177
178 MUTATOR_HOOKFUNCTION(ok, PlayerPreThink)
179 {SELFPARAM();
180         if(intermission_running || gameover)
181                 return false;
182
183         if(self.deadflag != DEAD_NO || !IS_PLAYER(self) || self.frozen)
184                 return false;
185
186         if(self.ok_lastwep)
187         {
188                 self.switchweapon = self.ok_lastwep;
189                 self.ok_lastwep = 0;
190         }
191
192         ok_IncreaseCharge(self, self.weapon);
193
194         if(self.BUTTON_ATCK2)
195         if(!forbidWeaponUse(self) || self.weapon_blocked) // allow if weapon is blocked
196         if(time >= self.jump_interval)
197         {
198                 self.jump_interval = time + WEP_CVAR_PRI(blaster, refire) * W_WeaponRateFactor();
199                 makevectors(self.v_angle);
200
201                 int oldwep = self.weapon;
202                 self.weapon = WEP_BLASTER.m_id;
203                 W_Blaster_Attack(
204                         self,
205                         WEP_BLASTER.m_id | HITTYPE_SECONDARY,
206                         WEP_CVAR_SEC(vaporizer, shotangle),
207                         WEP_CVAR_SEC(vaporizer, damage),
208                         WEP_CVAR_SEC(vaporizer, edgedamage),
209                         WEP_CVAR_SEC(vaporizer, radius),
210                         WEP_CVAR_SEC(vaporizer, force),
211                         WEP_CVAR_SEC(vaporizer, speed),
212                         WEP_CVAR_SEC(vaporizer, spread),
213                         WEP_CVAR_SEC(vaporizer, delay),
214                         WEP_CVAR_SEC(vaporizer, lifetime)
215                 );
216                 self.weapon = oldwep;
217         }
218
219         self.weapon_blocked = false;
220
221         self.ok_ammo_charge = self.ammo_charge[self.weapon];
222
223         if(self.ok_use_ammocharge)
224         if(!ok_CheckWeaponCharge(self, self.weapon))
225         {
226                 if(autocvar_g_overkill_ammo_charge_notice && time > self.ok_notice_time && self.BUTTON_ATCK && IS_REAL_CLIENT(self) && self.weapon == self.switchweapon)
227                 {
228                         //Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_OVERKILL_CHARGE);
229                         self.ok_notice_time = time + 2;
230                         play2(self, SND(DRYFIRE));
231                 }
232                 Weapon wpn = Weapons_from(self.weapon);
233                 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
234                 if(self.(weaponentity).state != WS_CLEAR)
235                         w_ready(wpn, self, weaponentity, (self.BUTTON_ATCK ? 1 : 0) | (self.BUTTON_ATCK2 ? 2 : 0));
236
237                 self.weapon_blocked = true;
238         }
239
240         self.BUTTON_ATCK2 = 0;
241
242         return false;
243 }
244
245 MUTATOR_HOOKFUNCTION(ok, PlayerSpawn)
246 {SELFPARAM();
247         if(autocvar_g_overkill_ammo_charge)
248         {
249                 for(int i = WEP_FIRST; i <= WEP_LAST; ++i)
250                         self.ammo_charge[i] = autocvar_g_overkill_ammo_charge_limit;
251
252                 self.ok_use_ammocharge = 1;
253                 self.ok_notice_time = time;
254         }
255         else
256                 self.ok_use_ammocharge = 0;
257
258         self.ok_pauseregen_finished = time + 2;
259
260         return false;
261 }
262
263 void self_spawnfunc_weapon_hmg() { SELFPARAM(); spawnfunc_weapon_hmg(this); }
264 void self_spawnfunc_weapon_rpc() { SELFPARAM(); spawnfunc_weapon_rpc(this); }
265
266 MUTATOR_HOOKFUNCTION(ok, OnEntityPreSpawn)
267 {SELFPARAM();
268         if(autocvar_g_powerups)
269         if(autocvar_g_overkill_powerups_replace)
270         {
271                 if(self.classname == "item_strength")
272                 {
273                         entity wep = new(weapon_hmg);
274                         setorigin(wep, self.origin);
275                         setmodel(wep, MDL_OK_HMG);
276                         wep.ok_item = true;
277                         wep.noalign = self.noalign;
278                         wep.cnt = self.cnt;
279                         wep.team = self.team;
280                         wep.respawntime = autocvar_g_overkill_superguns_respawn_time;
281                         wep.pickup_anyway = true;
282                         wep.think = self_spawnfunc_weapon_hmg;
283                         wep.nextthink = time + 0.1;
284                         return true;
285                 }
286
287                 if(self.classname == "item_invincible")
288                 {
289                         entity wep = new(weapon_rpc);
290                         setorigin(wep, self.origin);
291                         setmodel(wep, MDL_OK_RPC);
292                         wep.ok_item = true;
293                         wep.noalign = self.noalign;
294                         wep.cnt = self.cnt;
295                         wep.team = self.team;
296                         wep.respawntime = autocvar_g_overkill_superguns_respawn_time;
297                         wep.pickup_anyway = true;
298                         wep.think = self_spawnfunc_weapon_rpc;
299                         wep.nextthink = time + 0.1;
300                         return true;
301                 }
302         }
303
304         return false;
305 }
306
307 MUTATOR_HOOKFUNCTION(ok, FilterItem)
308 {SELFPARAM();
309         if(self.ok_item)
310                 return false;
311
312         switch(self.items)
313         {
314                 case ITEM_HealthMega.m_itemid: return !(autocvar_g_overkill_100h_anyway);
315                 case ITEM_ArmorMega.m_itemid: return !(autocvar_g_overkill_100a_anyway);
316         }
317
318         return true;
319 }
320
321 MUTATOR_HOOKFUNCTION(ok, SpectateCopy)
322 {SELFPARAM();
323         self.ammo_charge[self.weapon] = other.ammo_charge[other.weapon];
324         self.ok_use_ammocharge = other.ok_use_ammocharge;
325
326         return false;
327 }
328
329 MUTATOR_HOOKFUNCTION(ok, SetStartItems)
330 {
331         WepSet ok_start_items = (WEPSET(MACHINEGUN) | WEPSET(VORTEX) | WEPSET(SHOTGUN));
332
333         if(WEP_RPC.weaponstart > 0) { ok_start_items |= WEPSET(RPC); }
334         if(WEP_HMG.weaponstart > 0) { ok_start_items |= WEPSET(HMG); }
335
336         start_items |= IT_UNLIMITED_WEAPON_AMMO;
337         start_weapons = warmup_start_weapons = ok_start_items;
338
339         return false;
340 }
341
342 MUTATOR_HOOKFUNCTION(ok, BuildMutatorsString)
343 {
344         ret_string = strcat(ret_string, ":OK");
345         return false;
346 }
347
348 MUTATOR_HOOKFUNCTION(ok, BuildMutatorsPrettyString)
349 {
350         ret_string = strcat(ret_string, ", Overkill");
351         return false;
352 }
353
354 MUTATOR_HOOKFUNCTION(ok, SetModname)
355 {
356         modname = "Overkill";
357         return true;
358 }
359
360 void ok_SetCvars()
361 {
362         // hack to force overkill playermodels
363         cvar_settemp("sv_defaultcharacter", "1");
364         cvar_settemp("sv_defaultplayermodel", "models/ok_player/okrobot1.dpm models/ok_player/okrobot2.dpm models/ok_player/okrobot3.dpm models/ok_player/okrobot4.dpm models/ok_player/okmale1.dpm models/ok_player/okmale2.dpm models/ok_player/okmale3.dpm models/ok_player/okmale4.dpm");
365         cvar_settemp("sv_defaultplayermodel_red", "models/ok_player/okrobot1.dpm models/ok_player/okrobot2.dpm models/ok_player/okrobot3.dpm models/ok_player/okrobot4.dpm");
366         cvar_settemp("sv_defaultplayermodel_blue", "models/ok_player/okmale1.dpm models/ok_player/okmale2.dpm models/ok_player/okmale3.dpm models/ok_player/okmale4.dpm");
367 }
368
369 void ok_Initialize()
370 {
371         ok_SetCvars();
372
373         precache_all_playermodels("models/ok_player/*.dpm");
374
375         WEP_RPC.spawnflags &= ~WEP_FLAG_MUTATORBLOCKED;
376         WEP_HMG.spawnflags &= ~WEP_FLAG_MUTATORBLOCKED;
377
378         WEP_SHOTGUN.mdl = "ok_shotgun";
379         WEP_MACHINEGUN.mdl = "ok_mg";
380         WEP_VORTEX.mdl = "ok_sniper";
381 }
382 #endif