Declare more ints as ints
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator_overkill.qc
1 void ok_DecreaseCharge(entity ent, int wep)
2 {
3         if(!ent.ok_use_ammocharge) return;
4
5         entity wepent = get_weaponinfo(wep);
6
7         if(wepent.weapon == 0)
8                 return; // dummy
9
10         ent.ammo_charge[wep] -= max(0, cvar(sprintf("g_overkill_ammo_decharge_%s", wepent.netname)));
11 }
12
13 void ok_IncreaseCharge(entity ent, int wep)
14 {
15         entity wepent = get_weaponinfo(wep);
16
17         if(wepent.weapon == 0)
18                 return; // dummy
19
20         if(ent.ok_use_ammocharge)
21         if(!ent.BUTTON_ATCK) // not while attacking?
22                 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);
23 }
24
25 float ok_CheckWeaponCharge(entity ent, int wep)
26 {
27         if(!ent.ok_use_ammocharge) return true;
28
29         entity wepent = get_weaponinfo(wep);
30
31         if(wepent.weapon == 0)
32                 return 0; // dummy
33
34         return (ent.ammo_charge[wep] >= cvar(sprintf("g_overkill_ammo_decharge_%s", wepent.netname)));
35 }
36
37 MUTATOR_HOOKFUNCTION(ok_PlayerDamage_Calculate)
38 {
39         if(IS_PLAYER(frag_attacker) && IS_PLAYER(frag_target))
40         if(DEATH_ISWEAPON(frag_deathtype, WEP_BLASTER))
41         {
42                 frag_damage = 0;
43
44                 if(frag_attacker != frag_target)
45                 if(frag_target.health > 0)
46                 if(frag_target.frozen == 0)
47                 if(frag_target.deadflag == DEAD_NO)
48                 {
49                         Send_Notification(NOTIF_ONE, frag_attacker, MSG_CENTER, CENTER_SECONDARY_NODAMAGE);
50                         frag_force = '0 0 0';
51                 }
52         }
53
54         return false;
55 }
56
57 MUTATOR_HOOKFUNCTION(ok_PlayerDamage_SplitHealthArmor)
58 {
59         if(damage_take)
60                 self.ok_pauseregen_finished = max(self.ok_pauseregen_finished, time + 2);
61         return false;
62 }
63
64 MUTATOR_HOOKFUNCTION(ok_PlayerDies)
65 {
66         entity oldself = self;
67         entity targ = ((frag_attacker) ? frag_attacker : frag_target);
68
69         if(self.flags & FL_MONSTER)
70         {
71                 remove(other); // remove default item
72                 other = world;
73         }
74
75         self = spawn();
76         self.ok_item = true;
77         self.noalign = true;
78         self.pickup_anyway = true;
79         spawnfunc_item_armor_small();
80         self.movetype = MOVETYPE_TOSS;
81         self.gravity = 1;
82         self.reset = SUB_Remove;
83         setorigin(self, frag_target.origin + '0 0 32');
84         self.velocity = '0 0 200' + normalize(targ.origin - self.origin) * 500;
85         self.classname = "droppedweapon"; // hax
86         SUB_SetFade(self, time + 5, 1);
87         self = oldself;
88
89         self.ok_lastwep = self.switchweapon;
90
91         return false;
92 }
93
94 MUTATOR_HOOKFUNCTION(ok_PlayerRegen)
95 {
96         // overkill's values are different, so use custom regen
97         if(!self.frozen)
98         {
99                 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);
100                 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);
101
102                 float minf, maxf, limitf;
103
104                 maxf = autocvar_g_balance_fuel_rotstable;
105                 minf = autocvar_g_balance_fuel_regenstable;
106                 limitf = autocvar_g_balance_fuel_limit;
107
108                 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 & IT_FUEL_REGEN) != 0), maxf, autocvar_g_balance_fuel_rot, autocvar_g_balance_fuel_rotlinear, frametime * (time > self.pauserotfuel_finished), limitf);
109         }
110         return true; // return true anyway, as frozen uses no regen
111 }
112
113 MUTATOR_HOOKFUNCTION(ok_ForbidThrowCurrentWeapon)
114 {
115         return true;
116 }
117
118 MUTATOR_HOOKFUNCTION(ok_PlayerPreThink)
119 {
120         if(intermission_running || gameover)
121                 return false;
122
123         if(self.deadflag != DEAD_NO || !IS_PLAYER(self) || self.frozen)
124                 return false;
125
126         if(self.ok_lastwep)
127         {
128                 self.switchweapon = self.ok_lastwep;
129                 self.ok_lastwep = 0;
130         }
131
132         ok_IncreaseCharge(self, self.weapon);
133
134         if(self.BUTTON_ATCK2)
135         if(!forbidWeaponUse() || self.weapon_blocked) // allow if weapon is blocked
136         if(time >= self.jump_interval)
137         {
138                 self.jump_interval = time + WEP_CVAR_PRI(blaster, refire) * W_WeaponRateFactor();
139                 makevectors(self.v_angle);
140
141                 int oldwep = self.weapon;
142                 self.weapon = WEP_BLASTER;
143                 W_Blaster_Attack(
144                         WEP_BLASTER | HITTYPE_SECONDARY,
145                         WEP_CVAR_SEC(vaporizer, shotangle),
146                         WEP_CVAR_SEC(vaporizer, damage),
147                         WEP_CVAR_SEC(vaporizer, edgedamage),
148                         WEP_CVAR_SEC(vaporizer, radius),
149                         WEP_CVAR_SEC(vaporizer, force),
150                         WEP_CVAR_SEC(vaporizer, speed),
151                         WEP_CVAR_SEC(vaporizer, spread),
152                         WEP_CVAR_SEC(vaporizer, delay),
153                         WEP_CVAR_SEC(vaporizer, lifetime)
154                 );
155                 self.weapon = oldwep;
156         }
157
158         self.weapon_blocked = false;
159
160         self.ok_ammo_charge = self.ammo_charge[self.weapon];
161
162         if(self.ok_use_ammocharge)
163         if(!ok_CheckWeaponCharge(self, self.weapon))
164         {
165                 if(autocvar_g_overkill_ammo_charge_notice && time > self.ok_notice_time && self.BUTTON_ATCK && IS_REAL_CLIENT(self) && self.weapon == self.switchweapon)
166                 {
167                         //Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_OVERKILL_CHARGE);
168                         self.ok_notice_time = time + 2;
169                         play2(self, "weapons/dryfire.wav");
170                 }
171                 if(self.weaponentity.state != WS_CLEAR)
172                         w_ready();
173
174                 self.weapon_blocked = true;
175         }
176
177         self.BUTTON_ATCK2 = 0;
178
179         return false;
180 }
181
182 MUTATOR_HOOKFUNCTION(ok_PlayerSpawn)
183 {
184         if(autocvar_g_overkill_ammo_charge)
185         {
186                 for(int i = WEP_FIRST; i <= WEP_LAST; ++i)
187                         self.ammo_charge[i] = autocvar_g_overkill_ammo_charge_limit;
188
189                 self.ok_use_ammocharge = 1;
190                 self.ok_notice_time = time;
191         }
192         else
193                 self.ok_use_ammocharge = 0;
194
195         self.ok_pauseregen_finished = time + 2;
196
197         return false;
198 }
199
200 MUTATOR_HOOKFUNCTION(ok_OnEntityPreSpawn)
201 {
202         if(autocvar_g_powerups)
203         if(autocvar_g_overkill_powerups_replace)
204         {
205                 if(self.classname == "item_strength")
206                 {
207                         entity wep = spawn();
208                         setorigin(wep, self.origin);
209                         setmodel(wep, "models/weapons/g_ok_hmg.md3");
210                         wep.classname = "weapon_hmg";
211                         wep.ok_item = true;
212                         wep.noalign = self.noalign;
213                         wep.cnt = self.cnt;
214                         wep.team = self.team;
215                         wep.respawntime = autocvar_g_overkill_superguns_respawn_time;
216                         wep.pickup_anyway = true;
217                         wep.think = spawnfunc_weapon_hmg;
218                         wep.nextthink = time + 0.1;
219                         return true;
220                 }
221
222                 if(self.classname == "item_invincible")
223                 {
224                         entity wep = spawn();
225                         setorigin(wep, self.origin);
226                         setmodel(wep, "models/weapons/g_ok_rl.md3");
227                         wep.classname = "weapon_rpc";
228                         wep.ok_item = true;
229                         wep.noalign = self.noalign;
230                         wep.cnt = self.cnt;
231                         wep.team = self.team;
232                         wep.respawntime = autocvar_g_overkill_superguns_respawn_time;
233                         wep.pickup_anyway = true;
234                         wep.think = spawnfunc_weapon_rpc;
235                         wep.nextthink = time + 0.1;
236                         return true;
237                 }
238         }
239
240         return false;
241 }
242
243 MUTATOR_HOOKFUNCTION(ok_ItemRemove)
244 {
245         if(self.ok_item)
246                 return false;
247
248         switch(self.items)
249         {
250                 case IT_HEALTH: return !(autocvar_g_overkill_100h_anyway);
251                 case IT_ARMOR: return !(autocvar_g_overkill_100a_anyway);
252         }
253
254         return true;
255 }
256
257 MUTATOR_HOOKFUNCTION(ok_SpectateCopy)
258 {
259         self.ammo_charge[self.weapon] = other.ammo_charge[other.weapon];
260         self.ok_use_ammocharge = other.ok_use_ammocharge;
261
262         return false;
263 }
264
265 MUTATOR_HOOKFUNCTION(ok_StartItems)
266 {
267         WepSet ok_start_items = (WEPSET_MACHINEGUN | WEPSET_VORTEX | WEPSET_SHOTGUN);
268
269         if((get_weaponinfo(WEP_RPC)).weaponstart > 0) { ok_start_items |= WEPSET_RPC; }
270         if((get_weaponinfo(WEP_HMG)).weaponstart > 0) { ok_start_items |= WEPSET_HMG; }
271
272         start_items |= IT_UNLIMITED_WEAPON_AMMO;
273         start_weapons = warmup_start_weapons = ok_start_items;
274
275         return false;
276 }
277
278 MUTATOR_HOOKFUNCTION(ok_BuildMutatorsString)
279 {
280         ret_string = strcat(ret_string, ":OK");
281         return false;
282 }
283
284 MUTATOR_HOOKFUNCTION(ok_BuildMutatorsPrettyString)
285 {
286         ret_string = strcat(ret_string, ", Overkill");
287         return false;
288 }
289
290 MUTATOR_HOOKFUNCTION(ok_SetModname)
291 {
292         modname = "Overkill";
293         return true;
294 }
295
296 void ok_SetCvars()
297 {
298         // hack to force overkill playermodels
299         cvar_settemp("sv_defaultcharacter", "1");
300         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");
301         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");
302         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");
303 }
304
305 void ok_Initialize()
306 {
307         ok_SetCvars();
308
309         precache_all_playermodels("models/ok_player/*.dpm");
310
311         precache_model("models/weapons/h_ok_mg.iqm");
312         precache_model("models/weapons/v_ok_mg.md3");
313         precache_model("models/weapons/g_ok_mg.md3");
314
315         precache_model("models/weapons/h_ok_shotgun.iqm");
316         precache_model("models/weapons/v_ok_shotgun.md3");
317         precache_model("models/weapons/g_ok_shotgun.md3");
318
319         precache_model("models/weapons/h_ok_sniper.iqm");
320         precache_model("models/weapons/v_ok_sniper.md3");
321         precache_model("models/weapons/g_ok_sniper.md3");
322
323         precache_sound("weapons/dryfire.wav");
324
325         addstat(STAT_OK_AMMO_CHARGE, AS_FLOAT, ok_use_ammocharge);
326         addstat(STAT_OK_AMMO_CHARGEPOOL, AS_FLOAT, ok_ammo_charge);
327
328         (get_weaponinfo(WEP_RPC)).spawnflags &= ~WEP_FLAG_MUTATORBLOCKED;
329         (get_weaponinfo(WEP_HMG)).spawnflags &= ~WEP_FLAG_MUTATORBLOCKED;
330
331         (get_weaponinfo(WEP_SHOTGUN)).mdl = "ok_shotgun";
332         (get_weaponinfo(WEP_MACHINEGUN)).mdl = "ok_mg";
333         (get_weaponinfo(WEP_VORTEX)).mdl = "ok_sniper";
334 }
335
336 MUTATOR_DEFINITION(mutator_overkill)
337 {
338         MUTATOR_HOOK(ForbidThrowCurrentWeapon, ok_ForbidThrowCurrentWeapon, CBC_ORDER_ANY);
339         MUTATOR_HOOK(PlayerPreThink, ok_PlayerPreThink, CBC_ORDER_LAST);
340         MUTATOR_HOOK(PlayerSpawn, ok_PlayerSpawn, CBC_ORDER_ANY);
341         MUTATOR_HOOK(PlayerDamage_Calculate, ok_PlayerDamage_Calculate, CBC_ORDER_LAST);
342         MUTATOR_HOOK(PlayerDamage_SplitHealthArmor, ok_PlayerDamage_SplitHealthArmor, CBC_ORDER_ANY);
343         MUTATOR_HOOK(PlayerDies, ok_PlayerDies, CBC_ORDER_ANY);
344         MUTATOR_HOOK(PlayerRegen, ok_PlayerRegen, CBC_ORDER_ANY);
345         MUTATOR_HOOK(OnEntityPreSpawn, ok_OnEntityPreSpawn, CBC_ORDER_ANY);
346         MUTATOR_HOOK(FilterItem, ok_ItemRemove, CBC_ORDER_ANY);
347         MUTATOR_HOOK(MonsterDropItem, ok_PlayerDies, CBC_ORDER_ANY);
348         MUTATOR_HOOK(SpectateCopy, ok_SpectateCopy, CBC_ORDER_ANY);
349         MUTATOR_HOOK(SetStartItems, ok_StartItems, CBC_ORDER_ANY);
350         MUTATOR_HOOK(BuildMutatorsString, ok_BuildMutatorsString, CBC_ORDER_ANY);
351         MUTATOR_HOOK(BuildMutatorsPrettyString, ok_BuildMutatorsPrettyString, CBC_ORDER_ANY);
352         MUTATOR_HOOK(SetModname, ok_SetModname, CBC_ORDER_ANY);
353
354         MUTATOR_ONADD
355         {
356                 ok_Initialize();
357         }
358
359         MUTATOR_ONREMOVE
360         {
361                 (get_weaponinfo(WEP_RPC)).spawnflags |= WEP_FLAG_MUTATORBLOCKED;
362                 (get_weaponinfo(WEP_HMG)).spawnflags |= WEP_FLAG_MUTATORBLOCKED;
363         }
364
365         return false;
366 }