8bf9fe4a4f43134f9a9e91f146594887cbfc1bfc
[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, PlayerDamage_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))
102         if(DEATH_ISWEAPON(frag_deathtype, WEP_BLASTER))
103         {
104                 if(frag_attacker != frag_target)
105                 if(frag_target.health > 0)
106                 if(STAT(FROZEN, frag_target) == 0)
107                 if(!IS_DEAD(frag_target))
108                 {
109                         Send_Notification(NOTIF_ONE, frag_attacker, MSG_CENTER, CENTER_SECONDARY_NODAMAGE);
110                         M_ARGV(6, vector) = '0 0 0'; // force
111                 }
112
113                 M_ARGV(4, float) = 0; // damage
114         }
115 }
116
117 void ok_DropItem(entity this, entity targ)
118 {
119         entity e = new(droppedweapon); // hax
120         e.ok_item = true;
121         e.noalign = true;
122         e.pickup_anyway = true;
123         e.spawnfunc_checked = true;
124         spawnfunc_item_armor_small(e);
125         if (!wasfreed(e)) { // might have been blocked by a mutator
126         set_movetype(e, MOVETYPE_TOSS);
127         e.gravity = 1;
128         e.reset = SUB_Remove;
129         setorigin(e, this.origin + '0 0 32');
130         e.velocity = '0 0 200' + normalize(targ.origin - this.origin) * 500;
131         SUB_SetFade(e, time + 5, 1);
132         }
133 }
134
135 MUTATOR_HOOKFUNCTION(ok, PlayerDies)
136 {
137         entity frag_attacker = M_ARGV(1, entity);
138         entity frag_target = M_ARGV(2, entity);
139
140         entity targ = ((frag_attacker) ? frag_attacker : frag_target);
141
142         ok_DropItem(frag_target, targ);
143
144         frag_target.ok_lastwep = PS(frag_target).m_switchweapon.m_id;
145 }
146
147 MUTATOR_HOOKFUNCTION(ok, MonsterDropItem)
148 {
149         entity mon = M_ARGV(0, entity);
150         entity olditem = M_ARGV(1, entity);
151         entity frag_attacker = M_ARGV(2, entity);
152
153         delete(olditem);
154
155         M_ARGV(1, entity) = NULL;
156
157         ok_DropItem(mon, frag_attacker);
158 }
159
160 MUTATOR_HOOKFUNCTION(ok, ForbidThrowCurrentWeapon)
161 {
162         return true;
163 }
164
165 MUTATOR_HOOKFUNCTION(ok, PlayerPreThink)
166 {
167         if(gameover)
168                 return;
169
170         entity player = M_ARGV(0, entity);
171
172         if(IS_DEAD(player) || !IS_PLAYER(player) || STAT(FROZEN, player))
173                 return;
174
175         if(player.ok_lastwep)
176         {
177                 Weapon newwep = Weapons_from(player.ok_lastwep);
178                 if(player.ok_lastwep == WEP_HMG.m_id)
179                         newwep = WEP_MACHINEGUN;
180                 if(player.ok_lastwep == WEP_RPC.m_id)
181                         newwep = WEP_VORTEX;
182                 PS(player).m_switchweapon = newwep;
183                 player.ok_lastwep = 0;
184         }
185
186         ok_IncreaseCharge(player, PS(player).m_weapon.m_id);
187
188         if(PHYS_INPUT_BUTTON_ATCK2(player))
189         if( !forbidWeaponUse(player) || player.weapon_blocked // allow if weapon is blocked
190                 || (round_handler_IsActive() && !round_handler_IsRoundStarted()) )
191         if(time >= player.jump_interval)
192         {
193                 player.jump_interval = time + WEP_CVAR_PRI(blaster, refire) * W_WeaponRateFactor(player);
194                 makevectors(player.v_angle);
195
196                 Weapon oldwep = PS(player).m_weapon;
197                 PS(player).m_weapon = WEP_BLASTER;
198                 W_Blaster_Attack(
199                         player,
200                         weaponentities[0], // TODO: unhardcode
201                         WEP_BLASTER.m_id | HITTYPE_SECONDARY,
202                         WEP_CVAR_SEC(vaporizer, shotangle),
203                         WEP_CVAR_SEC(vaporizer, damage),
204                         WEP_CVAR_SEC(vaporizer, edgedamage),
205                         WEP_CVAR_SEC(vaporizer, radius),
206                         WEP_CVAR_SEC(vaporizer, force),
207                         WEP_CVAR_SEC(vaporizer, speed),
208                         WEP_CVAR_SEC(vaporizer, spread),
209                         WEP_CVAR_SEC(vaporizer, delay),
210                         WEP_CVAR_SEC(vaporizer, lifetime)
211                 );
212                 PS(player).m_weapon = oldwep;
213         }
214
215         player.weapon_blocked = false;
216
217         player.ok_ammo_charge = player.ammo_charge[PS(player).m_weapon.m_id];
218
219         if(player.ok_use_ammocharge)
220         if(!ok_CheckWeaponCharge(player, PS(player).m_weapon.m_id))
221         {
222                 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)
223                 {
224                         //Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_OVERKILL_CHARGE);
225                         player.ok_notice_time = time + 2;
226                         play2(player, SND(DRYFIRE));
227                 }
228                 Weapon wpn = PS(player).m_weapon;
229                 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
230                 if(player.(weaponentity).state != WS_CLEAR)
231                         w_ready(wpn, player, weaponentity, PHYS_INPUT_BUTTON_ATCK(player) | (PHYS_INPUT_BUTTON_ATCK2(player) << 1));
232
233                 player.weapon_blocked = true;
234         }
235
236         PHYS_INPUT_BUTTON_ATCK2(player) = false;
237 }
238
239 MUTATOR_HOOKFUNCTION(ok, PlayerSpawn)
240 {
241         entity player = M_ARGV(0, entity);
242
243         if(autocvar_g_overkill_ammo_charge)
244         {
245                 FOREACH(Weapons, it != WEP_Null, LAMBDA(player.ammo_charge[it.m_id] = autocvar_g_overkill_ammo_charge_limit));
246
247                 player.ok_use_ammocharge = 1;
248                 player.ok_notice_time = time;
249         }
250         else
251                 player.ok_use_ammocharge = 0;
252
253         // if player changed their weapon while dead, don't switch to their death weapon
254         if(player.impulse)
255                 player.ok_lastwep = 0;
256 }
257
258 void self_spawnfunc_weapon_hmg(entity this) { spawnfunc_weapon_hmg(this); }
259 void self_spawnfunc_weapon_rpc(entity this) { spawnfunc_weapon_rpc(this); }
260
261 MUTATOR_HOOKFUNCTION(ok, OnEntityPreSpawn)
262 {
263         entity ent = M_ARGV(0, entity);
264
265         if(autocvar_g_powerups)
266         if(autocvar_g_overkill_powerups_replace)
267         {
268                 if(ent.classname == "item_strength")
269                 {
270                         entity wep = new(weapon_hmg);
271                         setorigin(wep, ent.origin);
272                         setmodel(wep, MDL_OK_HMG);
273                         wep.ok_item = true;
274                         wep.noalign = ent.noalign;
275                         wep.cnt = ent.cnt;
276                         wep.team = ent.team;
277                         wep.respawntime = g_pickup_respawntime_superweapon;
278                         wep.pickup_anyway = true;
279                         wep.spawnfunc_checked = true;
280                         setthink(wep, self_spawnfunc_weapon_hmg);
281                         wep.nextthink = time + 0.1;
282                         return true;
283                 }
284                 else if(ent.classname == "item_invincible")
285                 {
286                         entity wep = new(weapon_rpc);
287                         setorigin(wep, ent.origin);
288                         setmodel(wep, MDL_OK_RPC);
289                         wep.ok_item = true;
290                         wep.noalign = ent.noalign;
291                         wep.cnt = ent.cnt;
292                         wep.team = ent.team;
293                         wep.respawntime = g_pickup_respawntime_superweapon;
294                         wep.pickup_anyway = true;
295                         wep.spawnfunc_checked = true;
296                         setthink(wep, self_spawnfunc_weapon_rpc);
297                         wep.nextthink = time + 0.1;
298                         return true;
299                 }
300         }
301 }
302
303 MUTATOR_HOOKFUNCTION(ok, FilterItem)
304 {
305         entity item = M_ARGV(0, entity);
306
307         if(item.ok_item)
308                 return false;
309
310         switch(item.itemdef)
311         {
312                 case ITEM_HealthMega: return autocvar_g_overkill_filter_healthmega;
313                 case ITEM_ArmorMedium: return autocvar_g_overkill_filter_armormedium;
314                 case ITEM_ArmorBig: return autocvar_g_overkill_filter_armorbig;
315                 case ITEM_ArmorMega: return autocvar_g_overkill_filter_armormega;
316         }
317
318         return true;
319 }
320
321 MUTATOR_HOOKFUNCTION(ok, SpectateCopy)
322 {
323         entity spectatee = M_ARGV(0, entity);
324         entity client = M_ARGV(1, entity);
325
326         client.ammo_charge[PS(client).m_weapon.m_id] = spectatee.ammo_charge[PS(spectatee).m_weapon.m_id];
327         client.ok_use_ammocharge = spectatee.ok_use_ammocharge;
328 }
329
330 MUTATOR_HOOKFUNCTION(ok, SetStartItems, CBC_ORDER_LAST)
331 {
332         WepSet ok_start_items = (WEPSET(MACHINEGUN) | WEPSET(VORTEX) | WEPSET(SHOTGUN));
333
334         if(WEP_RPC.weaponstart > 0) { ok_start_items |= WEPSET(RPC); }
335         if(WEP_HMG.weaponstart > 0) { ok_start_items |= WEPSET(HMG); }
336
337         start_items |= IT_UNLIMITED_WEAPON_AMMO;
338         start_weapons = warmup_start_weapons = ok_start_items;
339 }
340
341 MUTATOR_HOOKFUNCTION(ok, SetWeaponArena)
342 {
343         // turn weapon arena off
344         M_ARGV(0, string) = "off";
345 }
346
347 MUTATOR_HOOKFUNCTION(ok, BuildMutatorsString)
348 {
349         M_ARGV(0, string) = strcat(M_ARGV(0, string), ":OK");
350 }
351
352 MUTATOR_HOOKFUNCTION(ok, BuildMutatorsPrettyString)
353 {
354         M_ARGV(0, string) = strcat(M_ARGV(0, string), ", Overkill");
355 }
356
357 MUTATOR_HOOKFUNCTION(ok, SetModname)
358 {
359         M_ARGV(0, string) = "Overkill";
360         return true;
361 }
362
363 void ok_SetCvars()
364 {
365         // hack to force overkill playermodels
366         cvar_settemp("sv_defaultcharacter", "1");
367         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");
368         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");
369         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");
370 }
371
372 void ok_Initialize()
373 {
374         ok_SetCvars();
375
376         precache_all_playermodels("models/ok_player/*.dpm");
377
378         WEP_RPC.spawnflags &= ~WEP_FLAG_MUTATORBLOCKED;
379         WEP_HMG.spawnflags &= ~WEP_FLAG_MUTATORBLOCKED;
380
381         WEP_SHOTGUN.mdl = "ok_shotgun";
382         WEP_MACHINEGUN.mdl = "ok_mg";
383         WEP_VORTEX.mdl = "ok_sniper";
384 }