Merge branch 'master' into terencehill/menu_optimization
[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, PS(actor).m_weapon.m_id);
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 == 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(!ent.BUTTON_ATCK) // 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 0;  // 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         if(IS_PLAYER(frag_attacker) && IS_PLAYER(frag_target))
98         if(DEATH_ISWEAPON(frag_deathtype, WEP_BLASTER))
99         {
100                 frag_damage = 0;
101
102                 if(frag_attacker != frag_target)
103                 if(frag_target.health > 0)
104                 if(frag_target.frozen == 0)
105                 if(frag_target.deadflag == DEAD_NO)
106                 {
107                         Send_Notification(NOTIF_ONE, frag_attacker, MSG_CENTER, CENTER_SECONDARY_NODAMAGE);
108                         frag_force = '0 0 0';
109                 }
110         }
111
112         return false;
113 }
114
115 MUTATOR_HOOKFUNCTION(ok, PlayerDamage_SplitHealthArmor)
116 {SELFPARAM();
117         if(damage_take)
118                 self.ok_pauseregen_finished = max(self.ok_pauseregen_finished, time + 2);
119         return false;
120 }
121
122 MUTATOR_HOOKFUNCTION(ok, PlayerDies)
123 {
124         SELFPARAM();
125         entity targ = ((frag_attacker) ? frag_attacker : frag_target);
126
127         if(IS_MONSTER(this))
128         {
129                 remove(other); // remove default item
130                 other = NULL;
131         }
132
133         entity e = new(droppedweapon); // hax
134         e.ok_item = true;
135         e.noalign = true;
136         e.pickup_anyway = true;
137         e.spawnfunc_checked = true;
138         spawnfunc_item_armor_small(e);
139         e.movetype = MOVETYPE_TOSS;
140         e.gravity = 1;
141         e.reset = SUB_Remove;
142         setorigin(e, frag_target.origin + '0 0 32');
143         e.velocity = '0 0 200' + normalize(targ.origin - self.origin) * 500;
144         SUB_SetFade(e, time + 5, 1);
145
146         this.ok_lastwep = PS(this).m_switchweapon.m_id;
147
148         return false;
149 }
150 MUTATOR_HOOKFUNCTION(ok, MonsterDropItem) { ok_PlayerDies(); }
151
152 MUTATOR_HOOKFUNCTION(ok, PlayerRegen)
153 {SELFPARAM();
154         // overkill's values are different, so use custom regen
155         if(!self.frozen)
156         {
157                 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);
158                 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);
159
160                 float minf, maxf, limitf;
161
162                 maxf = autocvar_g_balance_fuel_rotstable;
163                 minf = autocvar_g_balance_fuel_regenstable;
164                 limitf = autocvar_g_balance_fuel_limit;
165
166                 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);
167         }
168         return true; // return true anyway, as frozen uses no regen
169 }
170
171 MUTATOR_HOOKFUNCTION(ok, ForbidThrowCurrentWeapon)
172 {
173         return true;
174 }
175
176 MUTATOR_HOOKFUNCTION(ok, PlayerPreThink)
177 {SELFPARAM();
178         if(intermission_running || gameover)
179                 return false;
180
181         if(self.deadflag != DEAD_NO || !IS_PLAYER(self) || self.frozen)
182                 return false;
183
184         if(self.ok_lastwep)
185         {
186                 PS(self).m_switchweapon = Weapons_from(self.ok_lastwep);
187                 self.ok_lastwep = 0;
188         }
189
190         ok_IncreaseCharge(self, PS(self).m_weapon.m_id);
191
192         if(self.BUTTON_ATCK2)
193         if(!forbidWeaponUse(self) || self.weapon_blocked) // allow if weapon is blocked
194         if(time >= self.jump_interval)
195         {
196                 self.jump_interval = time + WEP_CVAR_PRI(blaster, refire) * W_WeaponRateFactor();
197                 makevectors(self.v_angle);
198
199                 Weapon oldwep = PS(self).m_weapon;
200                 PS(self).m_weapon = WEP_BLASTER;
201                 W_Blaster_Attack(
202                         self,
203                         WEP_BLASTER.m_id | HITTYPE_SECONDARY,
204                         WEP_CVAR_SEC(vaporizer, shotangle),
205                         WEP_CVAR_SEC(vaporizer, damage),
206                         WEP_CVAR_SEC(vaporizer, edgedamage),
207                         WEP_CVAR_SEC(vaporizer, radius),
208                         WEP_CVAR_SEC(vaporizer, force),
209                         WEP_CVAR_SEC(vaporizer, speed),
210                         WEP_CVAR_SEC(vaporizer, spread),
211                         WEP_CVAR_SEC(vaporizer, delay),
212                         WEP_CVAR_SEC(vaporizer, lifetime)
213                 );
214                 PS(self).m_weapon = oldwep;
215         }
216
217         self.weapon_blocked = false;
218
219         self.ok_ammo_charge = self.ammo_charge[PS(self).m_weapon.m_id];
220
221         if(self.ok_use_ammocharge)
222         if(!ok_CheckWeaponCharge(self, PS(self).m_weapon.m_id))
223         {
224                 if(autocvar_g_overkill_ammo_charge_notice && time > self.ok_notice_time && self.BUTTON_ATCK && IS_REAL_CLIENT(self) && PS(self).m_weapon == PS(self).m_switchweapon)
225                 {
226                         //Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_OVERKILL_CHARGE);
227                         self.ok_notice_time = time + 2;
228                         play2(self, SND(DRYFIRE));
229                 }
230                 Weapon wpn = PS(self).m_weapon;
231                 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
232                 if(self.(weaponentity).state != WS_CLEAR)
233                         w_ready(wpn, self, weaponentity, (self.BUTTON_ATCK ? 1 : 0) | (self.BUTTON_ATCK2 ? 2 : 0));
234
235                 self.weapon_blocked = true;
236         }
237
238         self.BUTTON_ATCK2 = 0;
239
240         return false;
241 }
242
243 MUTATOR_HOOKFUNCTION(ok, PlayerSpawn)
244 {SELFPARAM();
245         if(autocvar_g_overkill_ammo_charge)
246         {
247                 for(int i = WEP_FIRST; i <= WEP_LAST; ++i)
248                         self.ammo_charge[i] = autocvar_g_overkill_ammo_charge_limit;
249
250                 self.ok_use_ammocharge = 1;
251                 self.ok_notice_time = time;
252         }
253         else
254                 self.ok_use_ammocharge = 0;
255
256         self.ok_pauseregen_finished = time + 2;
257
258         return false;
259 }
260
261 void self_spawnfunc_weapon_hmg() { SELFPARAM(); spawnfunc_weapon_hmg(this); }
262 void self_spawnfunc_weapon_rpc() { SELFPARAM(); spawnfunc_weapon_rpc(this); }
263
264 MUTATOR_HOOKFUNCTION(ok, OnEntityPreSpawn)
265 {SELFPARAM();
266         if(autocvar_g_powerups)
267         if(autocvar_g_overkill_powerups_replace)
268         {
269                 if(self.classname == "item_strength")
270                 {
271                         entity wep = new(weapon_hmg);
272                         setorigin(wep, self.origin);
273                         setmodel(wep, MDL_OK_HMG);
274                         wep.ok_item = true;
275                         wep.noalign = self.noalign;
276                         wep.cnt = self.cnt;
277                         wep.team = self.team;
278                         wep.respawntime = autocvar_g_overkill_superguns_respawn_time;
279                         wep.pickup_anyway = true;
280                         wep.spawnfunc_checked = true;
281                         wep.think = self_spawnfunc_weapon_hmg;
282                         wep.nextthink = time + 0.1;
283                         return true;
284                 }
285
286                 if(self.classname == "item_invincible")
287                 {
288                         entity wep = new(weapon_rpc);
289                         setorigin(wep, self.origin);
290                         setmodel(wep, MDL_OK_RPC);
291                         wep.ok_item = true;
292                         wep.noalign = self.noalign;
293                         wep.cnt = self.cnt;
294                         wep.team = self.team;
295                         wep.respawntime = autocvar_g_overkill_superguns_respawn_time;
296                         wep.pickup_anyway = true;
297                         wep.spawnfunc_checked = 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[PS(self).m_weapon.m_id] = other.ammo_charge[PS(other).m_weapon.m_id];
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