Merge remote-tracking branch 'remotes/origin/master' into TimePath/itemsys
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator_instagib.qc
1 #include "../_all.qh"
2
3 #include "mutator.qh"
4
5 #include "../cl_client.qh"
6 #include "../../common/buffs.qh"
7
8 #include "../../common/items/item.qh"
9
10 #define WITH(it) this.m_##it;
11 #define CONFIGURE(...) MAP(WITH, __VA_ARGS__)
12
13 float instagib_respawntime_ammo = 45;
14 float instagib_respawntimejitter_ammo = 0;
15 GETTER(float, instagib_respawntime_ammo)
16 GETTER(float, instagib_respawntimejitter_ammo)
17
18 REGISTER_ITEM(VaporizerCells, Pickup, APPLY(UNWORDS
19     ,APPLY(CONFIGURE
20     ,   model               =   "models/items/a_cells.md3"
21     ,   sound               =   "misc/itempickup.wav"
22     ,   name                =   "Vaporizer Ammo"
23     )
24     ,IF(SV, CONFIGURE
25     ,   botvalue            =   100
26     ,   itemid              =   IT_CELLS
27     ,   respawntime         =         GET(instagib_respawntime_ammo)
28     ,   respawntimejitter   =   GET(instagib_respawntimejitter_ammo)
29     )
30 ))
31
32 REGISTER_ITEM(ExtraLife, Pickup, APPLY(UNWORDS
33     ,APPLY(CONFIGURE
34     ,   model               =   "models/items/g_h100.md3"
35     ,   sound               =   "misc/megahealth.wav"
36     ,   name                =   "Extralife"
37     )
38     ,IF(SV, CONFIGURE
39     ,   botvalue            =   BOT_PICKUP_RATING_HIGH
40     ,   itemflags           =   FL_POWERUP
41     ,   itemid              =   IT_NAILS
42     ,   respawntime         =         GET(g_pickup_respawntime_powerup)
43     ,   respawntimejitter   =   GET(g_pickup_respawntimejitter_powerup)
44     )
45 ))
46
47 #undef WITH
48 #undef CONFIGURE
49
50 void spawnfunc_item_minst_cells (void)
51 {
52         if (!g_instagib) { remove(self); return; }
53         if (!self.ammo_cells)
54                 self.ammo_cells = autocvar_g_instagib_ammo_drop;
55
56         StartItemA (ITEM_VaporizerCells);
57 }
58
59 void instagib_health_mega()
60 {
61         self.max_health = 1;
62         StartItemA (ITEM_ExtraLife);
63 }
64
65 .float instagib_nextthink;
66 .float instagib_needammo;
67 void instagib_stop_countdown(entity e)
68 {
69         if (!e.instagib_needammo)
70                 return;
71         Kill_Notification(NOTIF_ONE_ONLY, e, MSG_CENTER_CPID, CPID_INSTAGIB_FINDAMMO);
72         e.instagib_needammo = false;
73 }
74 void instagib_ammocheck()
75 {
76         if(time < self.instagib_nextthink)
77                 return;
78         if(!IS_PLAYER(self))
79                 return; // not a player
80
81         if(self.deadflag || gameover)
82                 instagib_stop_countdown(self);
83         else if (self.ammo_cells > 0 || (self.items & IT_UNLIMITED_WEAPON_AMMO) || (self.flags & FL_GODMODE))
84                 instagib_stop_countdown(self);
85         else
86         {
87                 self.instagib_needammo = true;
88                 if (self.health <= 5)
89                 {
90                         Damage(self, self, self, 5, DEATH_NOAMMO, self.origin, '0 0 0');
91                         Send_Notification(NOTIF_ONE, self, MSG_ANNCE, ANNCE_INSTAGIB_TERMINATED);
92                 }
93                 else if (self.health <= 10)
94                 {
95                         Damage(self, self, self, 5, DEATH_NOAMMO, self.origin, '0 0 0');
96                         Send_Notification(NOTIF_ONE, self, MSG_ANNCE, ANNCE_NUM_1);
97                 }
98                 else if (self.health <= 20)
99                 {
100                         Damage(self, self, self, 10, DEATH_NOAMMO, self.origin, '0 0 0');
101                         Send_Notification(NOTIF_ONE, self, MSG_ANNCE, ANNCE_NUM_2);
102                 }
103                 else if (self.health <= 30)
104                 {
105                         Damage(self, self, self, 10, DEATH_NOAMMO, self.origin, '0 0 0');
106                         Send_Notification(NOTIF_ONE, self, MSG_ANNCE, ANNCE_NUM_3);
107                 }
108                 else if (self.health <= 40)
109                 {
110                         Damage(self, self, self, 10, DEATH_NOAMMO, self.origin, '0 0 0');
111                         Send_Notification(NOTIF_ONE, self, MSG_ANNCE, ANNCE_NUM_4);
112                 }
113                 else if (self.health <= 50)
114                 {
115                         Damage(self, self, self, 10, DEATH_NOAMMO, self.origin, '0 0 0');
116                         Send_Notification(NOTIF_ONE, self, MSG_ANNCE, ANNCE_NUM_5);
117                 }
118                 else if (self.health <= 60)
119                 {
120                         Damage(self, self, self, 10, DEATH_NOAMMO, self.origin, '0 0 0');
121                         Send_Notification(NOTIF_ONE, self, MSG_ANNCE, ANNCE_NUM_6);
122                 }
123                 else if (self.health <= 70)
124                 {
125                         Damage(self, self, self, 10, DEATH_NOAMMO, self.origin, '0 0 0');
126                         Send_Notification(NOTIF_ONE, self, MSG_ANNCE, ANNCE_NUM_7);
127                 }
128                 else if (self.health <= 80)
129                 {
130                         Damage(self, self, self, 10, DEATH_NOAMMO, self.origin, '0 0 0');
131                         Send_Notification(NOTIF_ONE, self, MSG_ANNCE, ANNCE_NUM_8);
132                 }
133                 else if (self.health <= 90)
134                 {
135                         Send_Notification(NOTIF_ONE_ONLY, self, MSG_CENTER, CENTER_INSTAGIB_FINDAMMO);
136                         Damage(self, self, self, 10, DEATH_NOAMMO, self.origin, '0 0 0');
137                         Send_Notification(NOTIF_ONE, self, MSG_ANNCE, ANNCE_NUM_9);
138                 }
139                 else
140                 {
141                         Send_Notification(NOTIF_ONE_ONLY, self, MSG_MULTI, MULTI_INSTAGIB_FINDAMMO);
142                         Damage(self, self, self, 10, DEATH_NOAMMO, self.origin, '0 0 0');
143                 }
144         }
145         self.instagib_nextthink = time + 1;
146 }
147
148 MUTATOR_HOOKFUNCTION(instagib_MatchEnd)
149 {
150         entity head;
151         FOR_EACH_PLAYER(head)
152                 instagib_stop_countdown(head);
153
154         return false;
155 }
156
157 MUTATOR_HOOKFUNCTION(instagib_MonsterLoot)
158 {
159         other.monster_loot = spawnfunc_item_minst_cells;
160
161         return false;
162 }
163
164 MUTATOR_HOOKFUNCTION(instagib_MonsterSpawn)
165 {
166         // always refill ammo
167         if(self.monsterid == MON_MAGE)
168                 self.skin = 1;
169
170         return false;
171 }
172
173 MUTATOR_HOOKFUNCTION(instagib_BotShouldAttack)
174 {
175         if(checkentity.items & IT_STRENGTH)
176                 return true;
177
178         return false;
179 }
180
181 MUTATOR_HOOKFUNCTION(instagib_MakePlayerObserver)
182 {
183         instagib_stop_countdown(self);
184         return false;
185 }
186
187 MUTATOR_HOOKFUNCTION(instagib_PlayerSpawn)
188 {
189         self.effects |= EF_FULLBRIGHT;
190         return false;
191 }
192
193 MUTATOR_HOOKFUNCTION(instagib_PlayerPreThink)
194 {
195         instagib_ammocheck();
196         return false;
197 }
198
199 MUTATOR_HOOKFUNCTION(instagib_PlayerRegen)
200 {
201         // no regeneration in instagib
202         return true;
203 }
204
205 MUTATOR_HOOKFUNCTION(instagib_PlayerPowerups)
206 {
207         if (!(self.effects & EF_FULLBRIGHT))
208                 self.effects |= EF_FULLBRIGHT;
209
210         if (self.items & IT_STRENGTH)
211         {
212                 play_countdown(self.strength_finished, "misc/poweroff.wav");
213                 if (time > self.strength_finished)
214                 {
215                         self.alpha = default_player_alpha;
216                         self.exteriorweaponentity.alpha = default_weapon_alpha;
217                         self.items &= ~IT_STRENGTH;
218                         Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_POWERDOWN_INVISIBILITY);
219                 }
220         }
221         else
222         {
223                 if (time < self.strength_finished)
224                 {
225                         self.alpha = autocvar_g_instagib_invis_alpha;
226                         self.exteriorweaponentity.alpha = autocvar_g_instagib_invis_alpha;
227                         self.items |= IT_STRENGTH;
228                         Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_POWERUP_INVISIBILITY, self.netname);
229                         Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_POWERUP_INVISIBILITY);
230                 }
231         }
232
233         if (self.items & IT_INVINCIBLE)
234         {
235                 play_countdown(self.invincible_finished, "misc/poweroff.wav");
236                 if (time > self.invincible_finished)
237                 {
238                         self.items &= ~IT_INVINCIBLE;
239                         Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_POWERDOWN_SPEED);
240                 }
241         }
242         else
243         {
244                 if (time < self.invincible_finished)
245                 {
246                         self.items |= IT_INVINCIBLE;
247                         Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_POWERUP_SPEED, self.netname);
248                         Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_POWERUP_SPEED);
249                 }
250         }
251         return false;
252 }
253
254 MUTATOR_HOOKFUNCTION(instagib_PlayerPhysics)
255 {
256         if(self.items & IT_INVINCIBLE)
257                 self.stat_sv_maxspeed = self.stat_sv_maxspeed * autocvar_g_instagib_speed_highspeed;
258
259         return false;
260 }
261
262 MUTATOR_HOOKFUNCTION(instagib_SplitHealthArmor)
263 {
264         damage_save = 0;
265         damage_take = frag_damage;
266
267         return false;
268 }
269
270 MUTATOR_HOOKFUNCTION(instagib_ForbidThrowing)
271 {
272         // weapon dropping on death handled by FilterItem
273
274         return true;
275 }
276
277 MUTATOR_HOOKFUNCTION(instagib_PlayerDamage)
278 {
279         if(autocvar_g_friendlyfire == 0 && SAME_TEAM(frag_target, frag_attacker) && IS_PLAYER(frag_target) && IS_PLAYER(frag_attacker))
280                 frag_damage = 0;
281
282         if(IS_PLAYER(frag_target))
283         {
284                 if ((frag_deathtype == DEATH_FALL)  ||
285                         (frag_deathtype == DEATH_DROWN) ||
286                         (frag_deathtype == DEATH_SLIME) ||
287                         (frag_deathtype == DEATH_LAVA))
288                 {
289                         frag_damage = 0;
290                 }
291
292                 if(IS_PLAYER(frag_attacker))
293                 if(DEATH_ISWEAPON(frag_deathtype, WEP_VAPORIZER))
294                 {
295                         if(frag_target.armorvalue)
296                         {
297                                 frag_target.armorvalue -= 1;
298                                 frag_damage = 0;
299                                 frag_target.damage_dealt += 1;
300                                 frag_attacker.damage_dealt += 1; // TODO: change this to a specific hitsound for armor hit
301                                 Send_Notification(NOTIF_ONE, frag_target, MSG_CENTER, CENTER_INSTAGIB_LIVES_REMAINING, frag_target.armorvalue);
302                         }
303                 }
304
305                 if(IS_PLAYER(frag_attacker) && DEATH_ISWEAPON(frag_deathtype, WEP_BLASTER))
306                 {
307                         if(frag_deathtype & HITTYPE_SECONDARY)
308                         {
309                                 frag_damage = frag_mirrordamage = 0;
310
311                                 if(frag_target != frag_attacker)
312                                 {
313                                         if(frag_target.health > 0) { Send_Notification(NOTIF_ONE, frag_attacker, MSG_CENTER, CENTER_SECONDARY_NODAMAGE); }
314                                         frag_force = '0 0 0';
315                                 }
316                         }
317                 }
318         }
319
320         if(IS_PLAYER(frag_attacker))
321         if(frag_mirrordamage > 0)
322         {
323                 // just lose extra LIVES, don't kill the player for mirror damage
324                 if(frag_attacker.armorvalue > 0)
325                 {
326                         frag_attacker.armorvalue -= 1;
327                         Send_Notification(NOTIF_ONE, frag_attacker, MSG_CENTER, CENTER_INSTAGIB_LIVES_REMAINING, frag_attacker.armorvalue);
328                         frag_attacker.damage_dealt += 1;
329                 }
330                 frag_mirrordamage = 0;
331         }
332
333         if((frag_target.buffs & BUFF_INVISIBLE) || (frag_target.items & IT_STRENGTH))
334                 yoda = 1;
335
336         return false;
337 }
338
339 MUTATOR_HOOKFUNCTION(instagib_SetStartItems)
340 {
341         start_health       = warmup_start_health       = 100;
342         start_armorvalue   = warmup_start_armorvalue   = 0;
343
344         start_ammo_shells  = warmup_start_ammo_shells  = 0;
345         start_ammo_nails   = warmup_start_ammo_nails   = 0;
346         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_instagib_ammo_start");
347         start_ammo_plasma  = warmup_start_ammo_plasma  = 0;
348         start_ammo_rockets = warmup_start_ammo_rockets = 0;
349         start_ammo_fuel    = warmup_start_ammo_fuel    = 0;
350
351         start_weapons = warmup_start_weapons = WEPSET_VAPORIZER;
352         start_items |= IT_UNLIMITED_SUPERWEAPONS;
353
354         return false;
355 }
356
357 MUTATOR_HOOKFUNCTION(instagib_FilterItem)
358 {
359         if(self.classname == "item_cells")
360                 return true; // no normal cells?
361
362         if(self.weapon == WEP_VAPORIZER && self.classname == "droppedweapon")
363         {
364                 self.ammo_cells = autocvar_g_instagib_ammo_drop;
365                 return false;
366         }
367
368         if(self.weapon == WEP_DEVASTATOR || self.weapon == WEP_VORTEX)
369         {
370                 entity e = spawn();
371                 setorigin(e, self.origin);
372                 entity oldself;
373                 oldself = self;
374                 self = e;
375                 spawnfunc_item_minst_cells();
376                 self = oldself;
377                 return true;
378         }
379
380         if(self.flags & FL_POWERUP)
381                 return false;
382
383         if(self.ammo_cells > autocvar_g_instagib_ammo_drop && self.classname != "item_minst_cells")
384                 self.ammo_cells = autocvar_g_instagib_ammo_drop;
385
386         if(self.ammo_cells && !self.weapon)
387                 return false;
388
389         return true;
390 }
391
392 MUTATOR_HOOKFUNCTION(instagib_CustomizeWaypoint)
393 {
394         entity e = WaypointSprite_getviewentity(other);
395
396         // if you have the invisibility powerup, sprites ALWAYS are restricted to your team
397         // but only apply this to real players, not to spectators
398         if((self.owner.flags & FL_CLIENT) && (self.owner.items & IT_STRENGTH) && (e == other))
399         if(DIFF_TEAM(self.owner, e))
400                 return true;
401
402         return false;
403 }
404
405 MUTATOR_HOOKFUNCTION(instagib_ItemCountdown)
406 {
407         switch(self.items)
408         {
409                 case IT_STRENGTH:   item_name = "item-invis"; item_color = '0 0 1'; break;
410                 case IT_NAILS:      item_name = "item-extralife"; item_color = '1 0 0'; break;
411                 case IT_INVINCIBLE: item_name = "item-speed"; item_color = '1 0 1'; break;
412         }
413         return false;
414 }
415
416 MUTATOR_HOOKFUNCTION(instagib_ItemTouch)
417 {
418         if(self.ammo_cells)
419         {
420                 // play some cool sounds ;)
421                 if (IS_CLIENT(other))
422                 {
423                         if(other.health <= 5)
424                                 Send_Notification(NOTIF_ONE, other, MSG_ANNCE, ANNCE_INSTAGIB_LASTSECOND);
425                         else if(other.health < 50)
426                                 Send_Notification(NOTIF_ONE, other, MSG_ANNCE, ANNCE_INSTAGIB_NARROWLY);
427                 }
428
429                 if(other.health < 100)
430                         other.health = 100;
431
432                 return MUT_ITEMTOUCH_CONTINUE;
433         }
434
435         if(self.max_health)
436         {
437                 other.armorvalue = bound(other.armorvalue, 999, other.armorvalue + autocvar_g_instagib_extralives);
438                 Send_Notification(NOTIF_ONE, other, MSG_CENTER, CENTER_EXTRALIVES);
439                 return MUT_ITEMTOUCH_PICKUP;
440         }
441
442         return MUT_ITEMTOUCH_CONTINUE;
443 }
444
445 MUTATOR_HOOKFUNCTION(instagib_OnEntityPreSpawn)
446 {
447         if (!autocvar_g_powerups) { return false; }
448         if (!(self.classname == "item_strength" || self.classname == "item_invincible" || self.classname == "item_health_mega"))
449                 return false;
450
451         entity e = spawn();
452
453         if(random() < 0.3)
454                 e.think = spawnfunc_item_strength;
455         else if(random() < 0.6)
456                 e.think = instagib_health_mega;
457         else
458                 e.think = spawnfunc_item_invincible;
459
460         e.nextthink = time + 0.1;
461         e.spawnflags = self.spawnflags;
462         e.noalign = self.noalign;
463         setorigin(e, self.origin);
464
465         return true;
466 }
467
468 MUTATOR_HOOKFUNCTION(instagib_BuildMutatorsString)
469 {
470         ret_string = strcat(ret_string, ":instagib");
471         return false;
472 }
473
474 MUTATOR_HOOKFUNCTION(instagib_BuildMutatorsPrettyString)
475 {
476         ret_string = strcat(ret_string, ", instagib");
477         return false;
478 }
479
480 MUTATOR_HOOKFUNCTION(instagib_SetModname)
481 {
482         modname = "instagib";
483         return true;
484 }
485
486 MUTATOR_DEFINITION(mutator_instagib)
487 {
488         MUTATOR_HOOK(MatchEnd, instagib_MatchEnd, CBC_ORDER_ANY);
489         MUTATOR_HOOK(MonsterDropItem, instagib_MonsterLoot, CBC_ORDER_ANY);
490         MUTATOR_HOOK(MonsterSpawn, instagib_MonsterSpawn, CBC_ORDER_ANY);
491         MUTATOR_HOOK(BotShouldAttack, instagib_BotShouldAttack, CBC_ORDER_ANY);
492         MUTATOR_HOOK(PlayerPhysics, instagib_PlayerPhysics, CBC_ORDER_ANY);
493         MUTATOR_HOOK(PlayerSpawn, instagib_PlayerSpawn, CBC_ORDER_ANY);
494         MUTATOR_HOOK(PlayerDamage_Calculate, instagib_PlayerDamage, CBC_ORDER_ANY);
495         MUTATOR_HOOK(MakePlayerObserver, instagib_MakePlayerObserver, CBC_ORDER_ANY);
496         MUTATOR_HOOK(SetStartItems, instagib_SetStartItems, CBC_ORDER_ANY);
497         MUTATOR_HOOK(ItemTouch, instagib_ItemTouch, CBC_ORDER_ANY);
498         MUTATOR_HOOK(FilterItem, instagib_FilterItem, CBC_ORDER_ANY);
499         MUTATOR_HOOK(CustomizeWaypoint, instagib_CustomizeWaypoint, CBC_ORDER_ANY);
500         MUTATOR_HOOK(Item_RespawnCountdown, instagib_ItemCountdown, CBC_ORDER_ANY);
501         MUTATOR_HOOK(PlayerDamage_SplitHealthArmor, instagib_SplitHealthArmor, CBC_ORDER_ANY);
502         MUTATOR_HOOK(PlayerPowerups, instagib_PlayerPowerups, CBC_ORDER_ANY);
503         MUTATOR_HOOK(ForbidThrowCurrentWeapon, instagib_ForbidThrowing, CBC_ORDER_ANY);
504         MUTATOR_HOOK(PlayerPreThink, instagib_PlayerPreThink, CBC_ORDER_ANY);
505         MUTATOR_HOOK(PlayerRegen, instagib_PlayerRegen, CBC_ORDER_ANY);
506         MUTATOR_HOOK(OnEntityPreSpawn, instagib_OnEntityPreSpawn, CBC_ORDER_ANY);
507         MUTATOR_HOOK(BuildMutatorsString, instagib_BuildMutatorsString, CBC_ORDER_ANY);
508         MUTATOR_HOOK(BuildMutatorsPrettyString, instagib_BuildMutatorsPrettyString, CBC_ORDER_ANY);
509         MUTATOR_HOOK(SetModname, instagib_SetModname, CBC_ORDER_ANY);
510
511         return false;
512 }