Merge branch 'Mirio/balance' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mutators / mutator / overkill / sv_overkill.qc
1 #include "sv_overkill.qh"
2
3 #include "hmg.qh"
4 #include "rpc.qh"
5
6 bool autocvar_g_overkill_powerups_replace;
7 bool autocvar_g_overkill_ammo_charge;
8 float autocvar_g_overkill_ammo_charge_notice;
9 float autocvar_g_overkill_ammo_charge_limit;
10
11 bool autocvar_g_overkill_filter_healthmega;
12 bool autocvar_g_overkill_filter_armormedium;
13 bool autocvar_g_overkill_filter_armorbig;
14 bool autocvar_g_overkill_filter_armormega;
15
16 .float ok_lastwep;
17 .float ok_item;
18
19 .float ok_notice_time;
20 .float ammo_charge[Weapons_MAX];
21 .float ok_use_ammocharge = _STAT(OK_AMMO_CHARGE);
22 .float ok_ammo_charge = _STAT(OK_AMMO_CHARGEPOOL);
23
24 void(entity ent, float wep) ok_DecreaseCharge;
25
26 void ok_Initialize();
27
28 REGISTER_MUTATOR(ok, cvar("g_overkill") && !cvar("g_instagib") && !g_nexball && cvar_string("g_mod_balance") == "Overkill")
29 {
30         MUTATOR_ONADD
31         {
32                 ok_Initialize();
33         }
34
35         MUTATOR_ONREMOVE
36         {
37                 WEP_RPC.spawnflags |= WEP_FLAG_MUTATORBLOCKED;
38                 WEP_HMG.spawnflags |= WEP_FLAG_MUTATORBLOCKED;
39         }
40 }
41
42 MUTATOR_HOOKFUNCTION(ok, W_DecreaseAmmo)
43 {
44         entity actor = M_ARGV(0, entity);
45         if (actor.ok_use_ammocharge)
46         {
47                 ok_DecreaseCharge(actor, PS(actor).m_weapon.m_id);
48                 return true;
49         }
50 }
51
52 MUTATOR_HOOKFUNCTION(ok, W_Reload)
53 {
54         entity actor = M_ARGV(0, entity);
55         return actor.ok_use_ammocharge;
56 }
57
58 void W_Blaster_Attack(entity, .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 == WEP_Null) return;  // dummy
69
70         ent.ammo_charge[wep] -= max(0, cvar(sprintf("g_overkill_ammo_decharge_%s", wepent.netname)));
71 }
72
73 void ok_IncreaseCharge(entity ent, int wep)
74 {
75         entity wepent = Weapons_from(wep);
76
77         if (wepent == WEP_Null) return;  // dummy
78
79         if(ent.ok_use_ammocharge)
80         if(!PHYS_INPUT_BUTTON_ATCK(ent)) // not while attacking?
81                 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);
82 }
83
84 float ok_CheckWeaponCharge(entity ent, int wep)
85 {
86         if(!ent.ok_use_ammocharge) return true;
87
88         entity wepent = Weapons_from(wep);
89
90         if(wepent == WEP_Null) return false;  // dummy
91
92         return (ent.ammo_charge[wep] >= cvar(sprintf("g_overkill_ammo_decharge_%s", wepent.netname)));
93 }
94
95 MUTATOR_HOOKFUNCTION(ok, Damage_Calculate, CBC_ORDER_LAST)
96 {
97         entity frag_attacker = M_ARGV(1, entity);
98         entity frag_target = M_ARGV(2, entity);
99         float frag_deathtype = M_ARGV(3, float);
100
101         if(IS_PLAYER(frag_attacker) && (IS_PLAYER(frag_target) || IS_VEHICLE(frag_target) || IS_TURRET(frag_target)))
102         if(DEATH_ISWEAPON(frag_deathtype, WEP_BLASTER))
103         {
104                 if(frag_attacker != frag_target)
105                 if(!STAT(FROZEN, frag_target))
106                 if(!IS_DEAD(frag_target))
107                 {
108                         Send_Notification(NOTIF_ONE, frag_attacker, MSG_CENTER, CENTER_SECONDARY_NODAMAGE);
109                         M_ARGV(6, vector) = '0 0 0'; // force
110                 }
111
112                 M_ARGV(4, float) = 0; // damage
113         }
114 }
115
116 void ok_DropItem(entity this, entity targ)
117 {
118         entity e = new(droppedweapon); // hax
119         e.ok_item = true;
120         e.noalign = true;
121         e.pickup_anyway = true;
122         e.spawnfunc_checked = true;
123         spawnfunc_item_armor_small(e);
124         if (!wasfreed(e)) { // might have been blocked by a mutator
125         set_movetype(e, MOVETYPE_TOSS);
126         e.gravity = 1;
127         e.reset = SUB_Remove;
128         setorigin(e, this.origin + '0 0 32');
129         e.velocity = '0 0 200' + normalize(targ.origin - this.origin) * 500;
130         SUB_SetFade(e, time + 5, 1);
131         }
132 }
133
134 MUTATOR_HOOKFUNCTION(ok, PlayerDies)
135 {
136         entity frag_attacker = M_ARGV(1, entity);
137         entity frag_target = M_ARGV(2, entity);
138
139         entity targ = ((frag_attacker) ? frag_attacker : frag_target);
140
141         ok_DropItem(frag_target, targ);
142
143         frag_target.ok_lastwep = PS(frag_target).m_switchweapon.m_id;
144 }
145
146 MUTATOR_HOOKFUNCTION(ok, MonsterDropItem)
147 {
148         entity mon = M_ARGV(0, entity);
149         entity olditem = M_ARGV(1, entity);
150         entity frag_attacker = M_ARGV(2, entity);
151
152         delete(olditem);
153
154         M_ARGV(1, entity) = NULL;
155
156         ok_DropItem(mon, frag_attacker);
157 }
158
159 MUTATOR_HOOKFUNCTION(ok, ForbidThrowCurrentWeapon)
160 {
161         return true;
162 }
163
164 MUTATOR_HOOKFUNCTION(ok, PlayerPreThink)
165 {
166         if(gameover)
167                 return;
168
169         entity player = M_ARGV(0, entity);
170
171         if(IS_DEAD(player) || !IS_PLAYER(player) || STAT(FROZEN, player))
172                 return;
173
174         if(player.ok_lastwep)
175         {
176                 Weapon newwep = Weapons_from(player.ok_lastwep);
177                 if(player.ok_lastwep == WEP_HMG.m_id)
178                         newwep = WEP_MACHINEGUN;
179                 if(player.ok_lastwep == WEP_RPC.m_id)
180                         newwep = WEP_VORTEX;
181                 PS(player).m_switchweapon = newwep;
182                 player.ok_lastwep = 0;
183         }
184
185         ok_IncreaseCharge(player, PS(player).m_weapon.m_id);
186
187         if(PHYS_INPUT_BUTTON_ATCK2(player))
188         if( !forbidWeaponUse(player) || player.weapon_blocked // allow if weapon is blocked
189                 || (round_handler_IsActive() && !round_handler_IsRoundStarted()) )
190         if(time >= player.jump_interval)
191         {
192                 player.jump_interval = time + WEP_CVAR_PRI(blaster, refire) * W_WeaponRateFactor(player);
193                 makevectors(player.v_angle);
194
195                 Weapon oldwep = PS(player).m_weapon;
196                 PS(player).m_weapon = WEP_BLASTER;
197                 W_Blaster_Attack(
198                         player,
199                         weaponentities[0], // TODO: unhardcode
200                         WEP_BLASTER.m_id | HITTYPE_SECONDARY,
201                         WEP_CVAR_SEC(vaporizer, shotangle),
202                         WEP_CVAR_SEC(vaporizer, damage),
203                         WEP_CVAR_SEC(vaporizer, edgedamage),
204                         WEP_CVAR_SEC(vaporizer, radius),
205                         WEP_CVAR_SEC(vaporizer, force),
206                         WEP_CVAR_SEC(vaporizer, speed),
207                         WEP_CVAR_SEC(vaporizer, spread),
208                         WEP_CVAR_SEC(vaporizer, delay),
209                         WEP_CVAR_SEC(vaporizer, lifetime)
210                 );
211                 PS(player).m_weapon = oldwep;
212         }
213
214         player.weapon_blocked = false;
215
216         player.ok_ammo_charge = player.ammo_charge[PS(player).m_weapon.m_id];
217
218         if(player.ok_use_ammocharge)
219         if(!ok_CheckWeaponCharge(player, PS(player).m_weapon.m_id))
220         {
221                 if(autocvar_g_overkill_ammo_charge_notice && time > player.ok_notice_time && PHYS_INPUT_BUTTON_ATCK(player) && IS_REAL_CLIENT(player) && PS(player).m_weapon == PS(player).m_switchweapon)
222                 {
223                         //Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_OVERKILL_CHARGE);
224                         player.ok_notice_time = time + 2;
225                         play2(player, SND(DRYFIRE));
226                 }
227                 Weapon wpn = PS(player).m_weapon;
228                 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
229                 if(player.(weaponentity).state != WS_CLEAR)
230                         w_ready(wpn, player, weaponentity, PHYS_INPUT_BUTTON_ATCK(player) | (PHYS_INPUT_BUTTON_ATCK2(player) << 1));
231
232                 player.weapon_blocked = true;
233         }
234
235         PHYS_INPUT_BUTTON_ATCK2(player) = false;
236 }
237
238 MUTATOR_HOOKFUNCTION(ok, PlayerSpawn)
239 {
240         entity player = M_ARGV(0, entity);
241
242         if(autocvar_g_overkill_ammo_charge)
243         {
244                 FOREACH(Weapons, it != WEP_Null, LAMBDA(player.ammo_charge[it.m_id] = autocvar_g_overkill_ammo_charge_limit));
245
246                 player.ok_use_ammocharge = 1;
247                 player.ok_notice_time = time;
248         }
249         else
250                 player.ok_use_ammocharge = 0;
251
252         // if player changed their weapon while dead, don't switch to their death weapon
253         if(player.impulse)
254                 player.ok_lastwep = 0;
255 }
256
257 void self_spawnfunc_weapon_hmg(entity this) { spawnfunc_weapon_hmg(this); }
258 void self_spawnfunc_weapon_rpc(entity this) { spawnfunc_weapon_rpc(this); }
259
260 MUTATOR_HOOKFUNCTION(ok, OnEntityPreSpawn)
261 {
262         entity ent = M_ARGV(0, entity);
263
264         if(autocvar_g_powerups)
265         if(autocvar_g_overkill_powerups_replace)
266         {
267                 if(ent.classname == "item_strength")
268                 {
269                         entity wep = new(weapon_hmg);
270                         setorigin(wep, ent.origin);
271                         setmodel(wep, MDL_OK_HMG);
272                         wep.ok_item = true;
273                         wep.noalign = ent.noalign;
274                         wep.cnt = ent.cnt;
275                         wep.team = ent.team;
276                         wep.respawntime = g_pickup_respawntime_superweapon;
277                         wep.pickup_anyway = true;
278                         wep.spawnfunc_checked = true;
279                         setthink(wep, self_spawnfunc_weapon_hmg);
280                         wep.nextthink = time + 0.1;
281                         return true;
282                 }
283                 else if(ent.classname == "item_invincible")
284                 {
285                         entity wep = new(weapon_rpc);
286                         setorigin(wep, ent.origin);
287                         setmodel(wep, MDL_OK_RPC);
288                         wep.ok_item = true;
289                         wep.noalign = ent.noalign;
290                         wep.cnt = ent.cnt;
291                         wep.team = ent.team;
292                         wep.respawntime = g_pickup_respawntime_superweapon;
293                         wep.pickup_anyway = true;
294                         wep.spawnfunc_checked = true;
295                         setthink(wep, self_spawnfunc_weapon_rpc);
296                         wep.nextthink = time + 0.1;
297                         return true;
298                 }
299         }
300 }
301
302 MUTATOR_HOOKFUNCTION(ok, FilterItem)
303 {
304         entity item = M_ARGV(0, entity);
305
306         if(item.ok_item)
307                 return false;
308
309         switch(item.itemdef)
310         {
311                 case ITEM_HealthMega: return autocvar_g_overkill_filter_healthmega;
312                 case ITEM_ArmorMedium: return autocvar_g_overkill_filter_armormedium;
313                 case ITEM_ArmorBig: return autocvar_g_overkill_filter_armorbig;
314                 case ITEM_ArmorMega: return autocvar_g_overkill_filter_armormega;
315         }
316
317         return true;
318 }
319
320 MUTATOR_HOOKFUNCTION(ok, SpectateCopy)
321 {
322         entity spectatee = M_ARGV(0, entity);
323         entity client = M_ARGV(1, entity);
324
325         client.ammo_charge[PS(client).m_weapon.m_id] = spectatee.ammo_charge[PS(spectatee).m_weapon.m_id];
326         client.ok_use_ammocharge = spectatee.ok_use_ammocharge;
327 }
328
329 MUTATOR_HOOKFUNCTION(ok, SetStartItems, CBC_ORDER_LAST)
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
340 MUTATOR_HOOKFUNCTION(ok, SetWeaponArena)
341 {
342         // turn weapon arena off
343         M_ARGV(0, string) = "off";
344 }
345
346 MUTATOR_HOOKFUNCTION(ok, BuildMutatorsString)
347 {
348         M_ARGV(0, string) = strcat(M_ARGV(0, string), ":OK");
349 }
350
351 MUTATOR_HOOKFUNCTION(ok, BuildMutatorsPrettyString)
352 {
353         M_ARGV(0, string) = strcat(M_ARGV(0, string), ", Overkill");
354 }
355
356 MUTATOR_HOOKFUNCTION(ok, SetModname)
357 {
358         M_ARGV(0, string) = "Overkill";
359         return true;
360 }
361
362 void ok_SetCvars()
363 {
364         // hack to force overkill playermodels
365         cvar_settemp("sv_defaultcharacter", "1");
366         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");
367         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");
368         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");
369 }
370
371 void ok_Initialize()
372 {
373         ok_SetCvars();
374
375         precache_all_playermodels("models/ok_player/*.dpm");
376
377         WEP_RPC.spawnflags &= ~WEP_FLAG_MUTATORBLOCKED;
378         WEP_HMG.spawnflags &= ~WEP_FLAG_MUTATORBLOCKED;
379
380         WEP_SHOTGUN.mdl = "ok_shotgun";
381         WEP_MACHINEGUN.mdl = "ok_mg";
382         WEP_VORTEX.mdl = "ok_sniper";
383 }