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