713866d97af123850d2a4d42956fa6a6290469b3
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator_overkill.qc
1 void ok_DecreaseCharge(entity ent, float wep)
2 {
3         if not(autocvar_g_overkill_ammo_charge) return;
4
5         entity wepent = get_weaponinfo(wep);
6
7         if(wepent.netname == "")
8                 return; // dummy
9
10         ent.ammo_charge[wep] -= 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.netname == "")
18                 return; // dummy
19
20         if(autocvar_g_overkill_ammo_charge)
21         if not(ent.BUTTON_ATCK)
22         if(ent.ammo_charge[wep] <= autocvar_g_overkill_ammo_charge_limit)
23                 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);
24 }
25
26 float ok_CheckWeaponCharge(entity ent, float wep, float swep)
27 {
28         if not(autocvar_g_overkill_ammo_charge) return TRUE;
29         //if(wep != swep) return TRUE; // switching, don't even bother checking
30
31         entity wepent = get_weaponinfo(wep);
32
33         if(wepent.netname == "")
34                 return 0; // dummy
35
36         return !(ent.ammo_charge[wep] < cvar(sprintf("g_overkill_ammo_decharge_%s", wepent.netname)));
37 }
38
39 MUTATOR_HOOKFUNCTION(ok_PlayerDamage_Calculate)
40 {
41         if(DEATH_ISWEAPON(frag_deathtype, WEP_LASER) && frag_target == frag_attacker)
42                 frag_damage = 0;
43
44         if(!IS_PLAYER(frag_target) || frag_target == frag_attacker)
45                 return FALSE;
46
47         if not (DEATH_ISWEAPON(frag_deathtype, WEP_LASER))
48                 return FALSE;
49
50         frag_damage = 0;
51         if(frag_target.health >= 1 && !frag_target.freezetag_frozen)
52                 Send_Notification(NOTIF_ONE, frag_attacker, MSG_CENTER, CENTER_MINSTA_SECONDARY);
53
54         frag_force = '0 0 0';
55
56         return FALSE;
57 }
58
59 MUTATOR_HOOKFUNCTION(ok_BuildMutatorsString)
60 {
61         ret_string = strcat(ret_string, ":OK");
62         return FALSE;
63 }
64
65 MUTATOR_HOOKFUNCTION(ok_BuildMutatorsPrettyString)
66 {
67         ret_string = strcat(ret_string, ", Overkill");
68         return FALSE;
69 }
70
71 void ok_Item_Touch()
72 {
73         if(IS_PLAYER(other) && other.deadflag == DEAD_NO && !other.freezetag_frozen)
74         {
75                 Item_Touch();
76                 remove(self);
77         }
78 }
79
80 MUTATOR_HOOKFUNCTION(ok_PlayerDies)
81 {
82         self.ok_lastwep = self.weapon;
83         self.ok_deathloc = self.origin;
84
85         entity oldself = self;
86         float gpi = autocvar_g_pickup_items;
87         
88         cvar_set("g_pickup_items", "1");
89         self = spawn();
90         self.ok_item = TRUE;
91         self.noalign = TRUE;
92         spawnfunc_item_armor_small();
93         self.movetype = MOVETYPE_TOSS;
94         self.gravity = 1;
95         setorigin(self, frag_target.origin + '0 0 32');
96         self.velocity = '0 0 200' + normalize(frag_attacker.origin - self.origin) * 500;
97         self.touch = ok_Item_Touch;
98         SUB_SetFade(self, time + 5, 1);
99         self = oldself;
100         cvar_set("g_pickup_items", ftos(gpi));
101
102         return FALSE;
103 }
104
105 MUTATOR_HOOKFUNCTION(ok_ForbidThrowCurrentWeapon)
106 {
107         nades_CheckThrow();
108
109         return TRUE;
110 }
111
112 MUTATOR_HOOKFUNCTION(ok_PlayerPreThink)
113 {
114         if(intermission_running || gameover)
115                 return FALSE;
116
117         if(self.deadflag != DEAD_NO || !IS_PLAYER(self))
118                 return FALSE;
119
120         if(self.freezetag_frozen)
121                 return FALSE;
122
123         ok_IncreaseCharge(self, self.weapon);
124
125         if(self.BUTTON_ATCK2)
126                 if(self.jump_interval <= time)
127                 {
128                         self.jump_interval = time + autocvar_g_balance_laser_primary_refire * W_WeaponRateFactor();
129                         makevectors(self.v_angle);
130                         float w = self.weapon;
131                         self.weapon = WEP_LASER;
132                         W_Laser_Attack(3);
133                         self.weapon = w;
134                 }
135
136         self.weapon_blocked = FALSE;
137
138         self.ok_ammo_charge = self.ammo_charge[self.weapon];
139
140         if(autocvar_g_overkill_ammo_charge)
141         if(!ok_CheckWeaponCharge(self, self.weapon, self.switchweapon))
142         {
143                 if(autocvar_g_overkill_ammo_charge_notice && time > self.ok_notice_time && self.BUTTON_ATCK && IS_REAL_CLIENT(self))
144                 {
145                         //Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_OVERKILL_CHARGE);
146                         self.ok_notice_time = time + 2;
147                         play2(self, "weapons/dryfire.wav");
148                 }
149                 if(self.weaponentity.state != WS_CLEAR)
150                         w_ready();
151
152                 self.weapon_blocked = TRUE;
153         }
154
155         self.BUTTON_ATCK2 = 0;
156
157         return FALSE;
158 }
159
160 MUTATOR_HOOKFUNCTION(ok_PlayerSpawn)
161 {
162         if(autocvar_g_overkill_ammo_charge)
163         {
164                 float i;
165
166                 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
167                         self.ammo_charge[i] = autocvar_g_overkill_ammo_charge_limit;
168
169                 self.ok_use_ammocharge = 1;
170                 self.ok_notice_time = time;
171         }
172         else
173                 self.ok_use_ammocharge = 0;
174
175         return FALSE;
176 }
177
178 void start_hmg()
179 {
180         float gpi = autocvar_g_pickup_items;
181         cvar_set("g_pickup_items", "1");
182         self.classname = "weapon_hmg";
183         self.ok_item = TRUE;
184         //self.weapons = WEP_HMG;
185         //self.flags |= FL_POWERUP;
186         self.respawntime = autocvar_g_overkill_superguns_respawn_time;
187         setmodel(self, "models/weapons/g_ok_hmg.md3");
188         spawnfunc_weapon_hmg();
189
190         cvar_set("g_pickup_items", ftos(gpi));
191 }
192
193 void start_rpc()
194 {
195         float gpi = autocvar_g_pickup_items;
196         cvar_set("g_pickup_items", "1");
197         self.classname = "weapon_rpc";
198         self.respawntime = autocvar_g_overkill_superguns_respawn_time;
199         self.ok_item = TRUE;
200         //self.weapons = WEP_RPC;
201         //self.flags |= FL_POWERUP;
202         setmodel(self, "models/weapons/g_ok_rl.md3");
203         spawnfunc_weapon_rpc();
204         cvar_set("g_pickup_items", ftos(gpi));
205 }
206
207 void start_mh_anyway()
208 {
209         cvar_set("g_pickup_items", "1");
210         self.classname = "item_health_mega";
211         self.ok_item = TRUE;
212         spawnfunc_item_health_mega();
213         cvar_set("g_pickup_items", "0");
214 }
215
216 void start_ma_anyway()
217 {
218         cvar_set("g_pickup_items", "1");
219         self.classname = "item_armor_large";
220         self.ok_item = TRUE;
221         spawnfunc_item_armor_large();
222         cvar_set("g_pickup_items", "0");
223 }
224
225 MUTATOR_HOOKFUNCTION(ok_OnEntityPreSpawn)
226 {
227         if(autocvar_g_powerups)
228         if(autocvar_g_overkill_powerups_replace)
229         if(self.classname == "item_strength")
230         {
231                 entity wep = spawn();
232                 setorigin(wep, self.origin);
233                 wep.ok_item = TRUE;
234                 wep.think = start_hmg;
235                 wep.nextthink = time + 0.1;
236                 return TRUE;
237         }
238
239         if(autocvar_g_powerups)
240         if(autocvar_g_overkill_powerups_replace)
241         if(self.classname == "item_invincible")
242         {
243                 entity wep = spawn();
244                 setorigin(wep, self.origin);
245                 wep.ok_item = TRUE;
246                 wep.think = start_rpc;
247                 wep.nextthink = time + 0.1;
248                 return TRUE;
249         }
250
251         return FALSE;
252 }
253
254 MUTATOR_HOOKFUNCTION(ok_ItemRemove)
255 {
256         if(self.ok_item)
257                 return FALSE;
258
259         switch(self.items)
260         {
261                 case IT_HEALTH: return !(autocvar_g_overkill_100h_anyway);
262                 case IT_ARMOR: return !(autocvar_g_overkill_100a_anyway);
263         }
264         
265         return TRUE;
266 }
267
268 MUTATOR_HOOKFUNCTION(ok_SpectateCopy)
269 {
270         self.ammo_charge[self.weapon] = other.ammo_charge[other.weapon];
271         self.ok_use_ammocharge = other.ok_use_ammocharge;
272
273         return FALSE;
274 }
275
276 MUTATOR_HOOKFUNCTION(ok_SetModname)
277 {
278         modname = "Overkill";
279         return TRUE;
280 }
281
282 MUTATOR_HOOKFUNCTION(ok_StartItems)
283 {
284         start_items |= IT_UNLIMITED_WEAPON_AMMO;
285         start_weapons = (WEPSET_UZI | WEPSET_NEX | WEPSET_SHOTGUN);
286
287         start_ammo_nails = start_ammo_cells = start_ammo_shells = start_ammo_rockets =
288         warmup_start_ammo_nails = warmup_start_ammo_cells = warmup_start_ammo_shells = warmup_start_ammo_rockets = autocvar_g_overkill_ammo_start;
289
290         return FALSE;
291 }
292
293 void ok_Initialize()
294 {
295         precache_all_playermodels("models/ok_player/*.dpm");
296
297         precache_model("models/weapons/h_ok_rl.iqm");
298         precache_model("models/weapons/v_ok_rl.md3");
299         precache_model("models/weapons/g_ok_rl.md3");
300         precache_model("models/weapons/ok_rocket.md3");
301
302         precache_model("models/weapons/h_ok_mg.iqm");
303         precache_model("models/weapons/v_ok_mg.md3");
304         precache_model("models/weapons/g_ok_mg.md3");
305
306         precache_model("models/weapons/h_ok_hmg.iqm");
307         precache_model("models/weapons/v_ok_hmg.md3");
308         precache_model("models/weapons/g_ok_hmg.md3");
309
310         precache_model("models/weapons/h_ok_shotgun.iqm");
311         precache_model("models/weapons/v_ok_shotgun.md3");
312         precache_model("models/weapons/g_ok_shotgun.md3");
313
314         precache_model("models/weapons/h_ok_sniper.iqm");
315         precache_model("models/weapons/v_ok_sniper.md3");
316         precache_model("models/weapons/g_ok_sniper.md3");
317
318         precache_sound("weapons/dryfire.wav");
319         
320         addstat(STAT_OK_AMMO_CHARGE, AS_FLOAT, ok_use_ammocharge);
321         addstat(STAT_OK_AMMO_CHARGEPOOl, AS_FLOAT, ok_ammo_charge);
322
323         w_uzi(WR_PRECACHE);
324         w_nex(WR_PRECACHE);
325         w_shotgun(WR_PRECACHE);
326         w_laser(WR_PRECACHE);
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_UZI)).mdl = "ok_mg";
333         (get_weaponinfo(WEP_NEX)).mdl = "ok_sniper";
334
335         string s;
336         float fh = fopen("overkill.cfg", FILE_READ);
337         if(fh >= 0)
338         {
339                 while((s = fgets(fh)))
340                 {
341                         tokenize_console(s);
342                         if not(argv(0) == "" || argv(1) == "//" || argv(1) == "")
343                                 cvar_settemp(argv(0), argv(1));
344                 }
345                 fclose(fh);
346         }
347         else
348                 dprint("^1Mutator Overkill: WARNING! overkill.cfg NOT found, things will be strange!\n");
349 }
350
351 MUTATOR_DEFINITION(mutator_overkill)
352 {
353         MUTATOR_HOOK(ForbidThrowCurrentWeapon, ok_ForbidThrowCurrentWeapon, CBC_ORDER_ANY);
354         MUTATOR_HOOK(BuildMutatorsString, ok_BuildMutatorsString, CBC_ORDER_ANY);
355         MUTATOR_HOOK(BuildMutatorsPrettyString, ok_BuildMutatorsPrettyString, CBC_ORDER_ANY);
356         MUTATOR_HOOK(PlayerPreThink, ok_PlayerPreThink, CBC_ORDER_LAST);
357         MUTATOR_HOOK(PlayerSpawn, ok_PlayerSpawn, CBC_ORDER_LAST);
358         MUTATOR_HOOK(PlayerDamage_Calculate, ok_PlayerDamage_Calculate, CBC_ORDER_LAST);
359         MUTATOR_HOOK(PlayerDies, ok_PlayerDies, CBC_ORDER_ANY);
360         MUTATOR_HOOK(OnEntityPreSpawn, ok_OnEntityPreSpawn, CBC_ORDER_ANY);
361         MUTATOR_HOOK(SetModname, ok_SetModname, CBC_ORDER_ANY);
362         MUTATOR_HOOK(FilterItem, ok_ItemRemove, CBC_ORDER_ANY);
363         MUTATOR_HOOK(SpectateCopy, ok_SpectateCopy, CBC_ORDER_ANY);
364         MUTATOR_HOOK(SetStartItems, ok_StartItems, CBC_ORDER_ANY);
365
366         MUTATOR_ONADD
367         {
368                 ok_Initialize();
369         }
370
371         MUTATOR_ONREMOVE
372         {
373                 (get_weaponinfo(WEP_RPC)).spawnflags |= WEP_FLAG_MUTATORBLOCKED;
374                 (get_weaponinfo(WEP_HMG)).spawnflags |= WEP_FLAG_MUTATORBLOCKED;
375         }
376
377         return FALSE;
378 }