]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/instagib/sv_instagib.qc
Make pure more entities and don't link them into the world
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mutators / mutator / instagib / sv_instagib.qc
1 #include "sv_instagib.qh"
2
3 #include <server/client.qh>
4 #include <common/items/_mod.qh>
5 #include "../random_items/sv_random_items.qh"
6
7 bool autocvar_g_instagib_damagedbycontents = true;
8 bool autocvar_g_instagib_blaster_keepdamage = false;
9 bool autocvar_g_instagib_blaster_keepforce = false;
10 bool autocvar_g_instagib_mirrordamage;
11 bool autocvar_g_instagib_friendlypush = true;
12 //int autocvar_g_instagib_ammo_drop;
13 bool autocvar_g_instagib_ammo_convert_cells;
14 bool autocvar_g_instagib_ammo_convert_rockets;
15 bool autocvar_g_instagib_ammo_convert_shells;
16 bool autocvar_g_instagib_ammo_convert_bullets;
17 float autocvar_g_instagib_speed_highspeed;
18
19 IntrusiveList g_instagib_items;
20 STATIC_INIT(instagib)
21 {
22         g_instagib_items = IL_NEW();
23         IL_PUSH(g_instagib_items, ITEM_VaporizerCells);
24         IL_PUSH(g_instagib_items, ITEM_ExtraLife);
25         IL_PUSH(g_instagib_items, ITEM_Invisibility);
26         IL_PUSH(g_instagib_items, ITEM_Speed);
27 }
28
29 void instagib_invisibility(entity this)
30 {
31         this.strength_finished = autocvar_g_instagib_invisibility_time;
32         StartItem(this, ITEM_Invisibility);
33 }
34
35 void instagib_extralife(entity this)
36 {
37         StartItem(this, ITEM_ExtraLife);
38 }
39
40 void instagib_speed(entity this)
41 {
42         this.invincible_finished = autocvar_g_instagib_speed_time;
43         StartItem(this, ITEM_Speed);
44 }
45
46 /// \brief Returns a random classname of the instagib item.
47 /// \param[in] prefix Prefix of the cvars that hold probabilities.
48 /// \return Random classname of the instagib item.
49 string RandomItems_GetRandomInstagibItemClassName(string prefix)
50 {
51         RandomSelection_Init();
52         IL_EACH(g_instagib_items, Item_IsDefinitionAllowed(it),
53         {
54                 string cvar_name = sprintf("g_%s_%s_probability", prefix,
55                         it.m_canonical_spawnfunc);
56                 if (!(cvar_type(cvar_name) & CVAR_TYPEFLAG_EXISTS))
57                 {
58                         LOG_WARNF("Random items: cvar %s doesn't exist.", cvar_name);
59                         continue;
60                 }
61                 RandomSelection_AddString(it.m_canonical_spawnfunc, cvar(cvar_name), 1);
62         });
63         return RandomSelection_chosen_string;
64 }
65
66 .float instagib_nextthink;
67 .float instagib_needammo;
68 void instagib_stop_countdown(entity e)
69 {
70         if (!e.instagib_needammo)
71                 return;
72         Kill_Notification(NOTIF_ONE_ONLY, e, MSG_CENTER, CPID_INSTAGIB_FINDAMMO);
73         e.instagib_needammo = false;
74 }
75 void instagib_ammocheck(entity this)
76 {
77         if(time < this.instagib_nextthink)
78                 return;
79         if(!IS_PLAYER(this))
80                 return; // not a player
81
82         if(IS_DEAD(this) || game_stopped)
83                 instagib_stop_countdown(this);
84         else if (GetResource(this, RES_CELLS) > 0 || (this.items & IT_UNLIMITED_AMMO) || (this.flags & FL_GODMODE))
85                 instagib_stop_countdown(this);
86         else if(autocvar_g_rm && autocvar_g_rm_laser)
87         {
88                 if(!this.instagib_needammo)
89                 {
90                         Send_Notification(NOTIF_ONE, this, MSG_CENTER, CENTER_INSTAGIB_DOWNGRADE);
91                         this.instagib_needammo = true;
92                 }
93         }
94         else
95         {
96                 float hp = GetResource(this, RES_HEALTH);
97                 this.instagib_needammo = true;
98                 if (hp <= 5)
99                 {
100                         Damage(this, this, this, 5, DEATH_NOAMMO.m_id, DMG_NOWEP, this.origin, '0 0 0');
101                         Send_Notification(NOTIF_ONE, this, MSG_ANNCE, ANNCE_INSTAGIB_TERMINATED);
102                 }
103                 else if (hp <= 10)
104                 {
105                         Damage(this, this, this, 5, DEATH_NOAMMO.m_id, DMG_NOWEP, this.origin, '0 0 0');
106                         Send_Notification(NOTIF_ONE, this, MSG_ANNCE, ANNCE_NUM_1);
107                 }
108                 else if (hp <= 20)
109                 {
110                         Damage(this, this, this, 10, DEATH_NOAMMO.m_id, DMG_NOWEP, this.origin, '0 0 0');
111                         Send_Notification(NOTIF_ONE, this, MSG_ANNCE, ANNCE_NUM_2);
112                 }
113                 else if (hp <= 30)
114                 {
115                         Damage(this, this, this, 10, DEATH_NOAMMO.m_id, DMG_NOWEP, this.origin, '0 0 0');
116                         Send_Notification(NOTIF_ONE, this, MSG_ANNCE, ANNCE_NUM_3);
117                 }
118                 else if (hp <= 40)
119                 {
120                         Damage(this, this, this, 10, DEATH_NOAMMO.m_id, DMG_NOWEP, this.origin, '0 0 0');
121                         Send_Notification(NOTIF_ONE, this, MSG_ANNCE, ANNCE_NUM_4);
122                 }
123                 else if (hp <= 50)
124                 {
125                         Damage(this, this, this, 10, DEATH_NOAMMO.m_id, DMG_NOWEP, this.origin, '0 0 0');
126                         Send_Notification(NOTIF_ONE, this, MSG_ANNCE, ANNCE_NUM_5);
127                 }
128                 else if (hp <= 60)
129                 {
130                         Damage(this, this, this, 10, DEATH_NOAMMO.m_id, DMG_NOWEP, this.origin, '0 0 0');
131                         Send_Notification(NOTIF_ONE, this, MSG_ANNCE, ANNCE_NUM_6);
132                 }
133                 else if (hp <= 70)
134                 {
135                         Damage(this, this, this, 10, DEATH_NOAMMO.m_id, DMG_NOWEP, this.origin, '0 0 0');
136                         Send_Notification(NOTIF_ONE, this, MSG_ANNCE, ANNCE_NUM_7);
137                 }
138                 else if (hp <= 80)
139                 {
140                         Damage(this, this, this, 10, DEATH_NOAMMO.m_id, DMG_NOWEP, this.origin, '0 0 0');
141                         Send_Notification(NOTIF_ONE, this, MSG_ANNCE, ANNCE_NUM_8);
142                 }
143                 else if (hp <= 90)
144                 {
145                         Send_Notification(NOTIF_ONE_ONLY, this, MSG_CENTER, CENTER_INSTAGIB_FINDAMMO);
146                         Damage(this, this, this, 10, DEATH_NOAMMO.m_id, DMG_NOWEP, this.origin, '0 0 0');
147                         Send_Notification(NOTIF_ONE, this, MSG_ANNCE, ANNCE_NUM_9);
148                 }
149                 else
150                 {
151                         Send_Notification(NOTIF_ONE_ONLY, this, MSG_MULTI, MULTI_INSTAGIB_FINDAMMO);
152                         Damage(this, this, this, 10, DEATH_NOAMMO.m_id, DMG_NOWEP, this.origin, '0 0 0');
153                 }
154         }
155         this.instagib_nextthink = time + 1;
156 }
157
158 MUTATOR_HOOKFUNCTION(mutator_instagib, MatchEnd)
159 {
160         FOREACH_CLIENT(IS_PLAYER(it), { instagib_stop_countdown(it); });
161 }
162
163 MUTATOR_HOOKFUNCTION(mutator_instagib, RandomItems_GetRandomItemClassName)
164 {
165         M_ARGV(1, string) = RandomItems_GetRandomInstagibItemClassName(
166                 M_ARGV(0, string));
167         return true;
168 }
169
170 MUTATOR_HOOKFUNCTION(mutator_instagib, MonsterDropItem)
171 {
172         entity item = M_ARGV(1, entity);
173
174         item.monster_loot = ITEM_VaporizerCells;
175 }
176
177 MUTATOR_HOOKFUNCTION(mutator_instagib, MonsterSpawn)
178 {
179         entity mon = M_ARGV(0, entity);
180
181         // always refill ammo
182         if(mon.monsterdef == MON_MAGE)
183                 mon.skin = 1;
184 }
185
186 MUTATOR_HOOKFUNCTION(mutator_instagib, BotShouldAttack)
187 {
188         entity targ = M_ARGV(1, entity);
189
190         if (targ.items & ITEM_Invisibility.m_itemid)
191                 return true;
192 }
193
194 MUTATOR_HOOKFUNCTION(mutator_instagib, MakePlayerObserver)
195 {
196         entity player = M_ARGV(0, entity);
197
198         instagib_stop_countdown(player);
199 }
200
201 MUTATOR_HOOKFUNCTION(mutator_instagib, ForbidRandomStartWeapons)
202 {
203         return true;
204 }
205
206 MUTATOR_HOOKFUNCTION(mutator_instagib, PlayerSpawn)
207 {
208         entity player = M_ARGV(0, entity);
209
210         player.effects |= EF_FULLBRIGHT;
211 }
212
213 MUTATOR_HOOKFUNCTION(mutator_instagib, PlayerPreThink)
214 {
215         entity player = M_ARGV(0, entity);
216
217         instagib_ammocheck(player);
218 }
219
220 MUTATOR_HOOKFUNCTION(mutator_instagib, PlayerRegen)
221 {
222         // no regeneration in instagib
223         return true;
224 }
225
226 MUTATOR_HOOKFUNCTION(mutator_instagib, PlayerPowerups)
227 {
228         entity player = M_ARGV(0, entity);
229
230         if (!(player.effects & EF_FULLBRIGHT))
231                 player.effects |= EF_FULLBRIGHT;
232
233         if (player.items & ITEM_Invisibility.m_itemid)
234         {
235                 play_countdown(player, STAT(STRENGTH_FINISHED, player), SND_POWEROFF);
236                 if (time > STAT(STRENGTH_FINISHED, player))
237                 {
238                         if(!player.vehicle) // already reset upon exit
239                         {
240                                 player.alpha = default_player_alpha;
241                                 player.exteriorweaponentity.alpha = default_weapon_alpha;
242                         }
243                         player.items &= ~ITEM_Invisibility.m_itemid;
244                         Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_POWERDOWN_INVISIBILITY);
245                 }
246         }
247         else
248         {
249                 if (time < STAT(STRENGTH_FINISHED, player))
250                 {
251                         if(!player.vehicle) // incase the player is given powerups while inside a vehicle
252                         {
253                                 player.alpha = autocvar_g_instagib_invis_alpha;
254                                 player.exteriorweaponentity.alpha = autocvar_g_instagib_invis_alpha;
255                         }
256                         player.items |= ITEM_Invisibility.m_itemid;
257                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_POWERUP_INVISIBILITY, player.netname);
258                         Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_POWERUP_INVISIBILITY);
259                 }
260         }
261
262         if (player.items & ITEM_Speed.m_itemid)
263         {
264                 play_countdown(player, STAT(INVINCIBLE_FINISHED, player), SND_POWEROFF);
265                 if (time > STAT(INVINCIBLE_FINISHED, player))
266                 {
267                         player.items &= ~ITEM_Speed.m_itemid;
268                         Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_POWERDOWN_SPEED);
269                 }
270         }
271         else
272         {
273                 if (time < STAT(INVINCIBLE_FINISHED, player))
274                 {
275                         player.items |= ITEM_Speed.m_itemid;
276                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_POWERUP_SPEED, player.netname);
277                         Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_POWERUP_SPEED);
278                 }
279         }
280 }
281
282 MUTATOR_HOOKFUNCTION(mutator_instagib, PlayerPhysics_UpdateStats)
283 {
284         entity player = M_ARGV(0, entity);
285         // these automatically reset, no need to worry
286
287         if(player.items & ITEM_Speed.m_itemid)
288                 STAT(MOVEVARS_HIGHSPEED, player) *= autocvar_g_instagib_speed_highspeed;
289 }
290
291 MUTATOR_HOOKFUNCTION(mutator_instagib, PlayerDamage_SplitHealthArmor)
292 {
293         M_ARGV(4, float) = M_ARGV(7, float); // take = damage
294         M_ARGV(5, float) = 0; // save
295 }
296
297 MUTATOR_HOOKFUNCTION(mutator_instagib, ForbidThrowCurrentWeapon)
298 {
299         // weapon dropping on death handled by FilterItem
300         return true;
301 }
302
303 MUTATOR_HOOKFUNCTION(mutator_instagib, Damage_Calculate)
304 {
305         entity frag_attacker = M_ARGV(1, entity);
306         entity frag_target = M_ARGV(2, entity);
307         float frag_deathtype = M_ARGV(3, float);
308         float frag_damage = M_ARGV(4, float);
309         float frag_mirrordamage = M_ARGV(5, float);
310         vector frag_force = M_ARGV(6, vector);
311
312         if(autocvar_g_friendlyfire == 0 && SAME_TEAM(frag_target, frag_attacker) && IS_PLAYER(frag_target) && IS_PLAYER(frag_attacker))
313                 frag_damage = 0;
314
315         if(IS_PLAYER(frag_target))
316         {
317                 if(frag_deathtype == DEATH_FALL.m_id)
318                         frag_damage = 0; // never count fall damage
319
320                 if(!autocvar_g_instagib_damagedbycontents)
321                 switch(DEATH_ENT(frag_deathtype))
322                 {
323                         case DEATH_DROWN:
324                         case DEATH_SLIME:
325                         case DEATH_LAVA:
326                                 frag_damage = 0;
327                                 break;
328                 }
329
330                 if(IS_PLAYER(frag_attacker))
331                 if(DEATH_ISWEAPON(frag_deathtype, WEP_VAPORIZER))
332                 {
333                         if(!autocvar_g_instagib_friendlypush && SAME_TEAM(frag_target, frag_attacker))
334                                 frag_force = '0 0 0';
335
336                         float armor = GetResource(frag_target, RES_ARMOR);
337                         if(armor)
338                         {
339                                 armor -= 1;
340                                 SetResource(frag_target, RES_ARMOR, armor);
341                                 frag_damage = 0;
342                                 frag_target.damage_dealt += 1;
343                                 frag_attacker.damage_dealt += 1;
344                                 Send_Notification(NOTIF_ONE, frag_target, MSG_CENTER, CENTER_INSTAGIB_LIVES_REMAINING, armor);
345                         }
346                 }
347
348                 if(IS_PLAYER(frag_attacker) && DEATH_ISWEAPON(frag_deathtype, WEP_BLASTER))
349                 {
350                         if(frag_deathtype & HITTYPE_SECONDARY)
351                         {
352                                 if(!autocvar_g_instagib_blaster_keepdamage || frag_attacker == frag_target)
353                                 {
354                                         frag_damage = 0;
355                                         if(!autocvar_g_instagib_mirrordamage)
356                                                 frag_mirrordamage = 0; // never do mirror damage on enemies
357                                 }
358
359                                 if(frag_target != frag_attacker)
360                                 {
361                                         if(!autocvar_g_instagib_blaster_keepforce)
362                                                 frag_force = '0 0 0';
363                                 }
364                         }
365                 }
366         }
367
368         if(!autocvar_g_instagib_mirrordamage) // only apply the taking lives hack if we don't want to support real damage mirroring
369         if(IS_PLAYER(frag_attacker))
370         if(frag_mirrordamage > 0)
371         {
372                 // just lose extra LIVES, don't kill the player for mirror damage
373                 float armor = GetResource(frag_attacker, RES_ARMOR);
374                 if(armor > 0)
375                 {
376                         armor -= 1;
377                         SetResource(frag_attacker, RES_ARMOR, armor);
378                         Send_Notification(NOTIF_ONE, frag_attacker, MSG_CENTER, CENTER_INSTAGIB_LIVES_REMAINING, armor);
379                         frag_attacker.damage_dealt += frag_mirrordamage;
380                 }
381                 frag_mirrordamage = 0;
382         }
383
384         if(frag_target.alpha && frag_target.alpha < 1)
385         if(IS_PLAYER(frag_target))
386                 yoda = 1;
387
388         M_ARGV(4, float) = frag_damage;
389         M_ARGV(5, float) = frag_mirrordamage;
390         M_ARGV(6, vector) = frag_force;
391 }
392
393 MUTATOR_HOOKFUNCTION(mutator_instagib, SetStartItems, CBC_ORDER_LAST)
394 {
395         start_health       = warmup_start_health       = 100;
396         start_armorvalue   = warmup_start_armorvalue   = 0;
397
398         start_ammo_shells  = warmup_start_ammo_shells  = 0;
399         start_ammo_nails   = warmup_start_ammo_nails   = 0;
400         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_instagib_ammo_start");
401         start_ammo_plasma  = warmup_start_ammo_plasma  = 0;
402         start_ammo_rockets = warmup_start_ammo_rockets = 0;
403         //start_ammo_fuel    = warmup_start_ammo_fuel    = 0;
404
405         start_weapons = warmup_start_weapons = WEPSET(VAPORIZER);
406         start_items |= IT_UNLIMITED_SUPERWEAPONS;
407 }
408
409 MUTATOR_HOOKFUNCTION(mutator_instagib, SetWeaponArena)
410 {
411         // turn weapon arena off
412         M_ARGV(0, string) = "off";
413 }
414
415 void replace_with_insta_cells(entity item)
416 {
417         entity e = new(item_vaporizer_cells);
418         setorigin(e, item.origin);
419         e.noalign = Item_ShouldKeepPosition(item);
420         e.cnt = item.cnt;
421         e.team = item.team;
422         e.spawnfunc_checked = true;
423         spawnfunc_item_vaporizer_cells(e);
424 }
425
426 MUTATOR_HOOKFUNCTION(mutator_instagib, FilterItem)
427 {
428         entity item = M_ARGV(0, entity);
429
430         if(item.classname == "item_cells")
431         {
432                 if(autocvar_g_instagib_ammo_convert_cells)
433                 {
434                         replace_with_insta_cells(item);
435                 }
436                 return true;
437         }
438         else if(item.classname == "item_rockets")
439         {
440                 if(autocvar_g_instagib_ammo_convert_rockets)
441                 {
442                         replace_with_insta_cells(item);
443                 }
444                 return true;
445         }
446         else if(item.classname == "item_shells")
447         {
448                 if(autocvar_g_instagib_ammo_convert_shells)
449                 {
450                         replace_with_insta_cells(item);
451                 }
452                 return true;
453         }
454         else if(item.classname == "item_bullets")
455         {
456                 if(autocvar_g_instagib_ammo_convert_bullets)
457                 {
458                         replace_with_insta_cells(item);
459                 }
460                 return true;
461         }
462
463         if(item.weapon == WEP_VAPORIZER.m_id && Item_IsLoot(item))
464         {
465                 SetResource(item, RES_CELLS, autocvar_g_instagib_ammo_drop);
466                 return false;
467         }
468
469         if(item.weapon == WEP_DEVASTATOR.m_id || item.weapon == WEP_VORTEX.m_id)
470         {
471                 replace_with_insta_cells(item);
472                 return true;
473         }
474
475         if(item.flags & FL_POWERUP)
476                 return false;
477
478         float cells = GetResource(item, RES_CELLS);
479         if(cells > autocvar_g_instagib_ammo_drop && item.classname != "item_vaporizer_cells")
480                 SetResource(item, RES_CELLS, autocvar_g_instagib_ammo_drop);
481
482         if(cells && !item.weapon)
483                 return false;
484
485         return true;
486 }
487
488 MUTATOR_HOOKFUNCTION(mutator_instagib, CustomizeWaypoint)
489 {
490         entity wp = M_ARGV(0, entity);
491         entity player = M_ARGV(1, entity);
492
493         entity e = WaypointSprite_getviewentity(player);
494
495         // if you have the invisibility powerup, sprites ALWAYS are restricted to your team
496         // but only apply this to real players, not to spectators
497         if((wp.owner.flags & FL_CLIENT) && (wp.owner.items & ITEM_Invisibility.m_itemid) && (e == player))
498         if(DIFF_TEAM(wp.owner, e))
499                 return true;
500 }
501
502 MUTATOR_HOOKFUNCTION(mutator_instagib, PlayerDies)
503 {
504         float frag_deathtype = M_ARGV(3, float);
505
506         if(DEATH_ISWEAPON(frag_deathtype, WEP_VAPORIZER))
507                 M_ARGV(4, float) = 1000; // always gib if it was a vaporizer death
508 }
509
510 MUTATOR_HOOKFUNCTION(mutator_instagib, ItemTouch)
511 {
512         if(MUTATOR_RETURNVALUE) return false;
513
514         entity item = M_ARGV(0, entity);
515         entity toucher = M_ARGV(1, entity);
516
517         if(GetResource(item, RES_CELLS))
518         {
519                 // play some cool sounds ;)
520                 float hp = GetResource(toucher, RES_HEALTH);
521                 if (IS_CLIENT(toucher))
522                 {
523                         if(hp <= 5)
524                                 Send_Notification(NOTIF_ONE, toucher, MSG_ANNCE, ANNCE_INSTAGIB_LASTSECOND);
525                         else if(hp < 50)
526                                 Send_Notification(NOTIF_ONE, toucher, MSG_ANNCE, ANNCE_INSTAGIB_NARROWLY);
527                 }
528
529                 if(hp < 100)
530                         SetResource(toucher, RES_HEALTH, 100);
531
532                 return MUT_ITEMTOUCH_CONTINUE;
533         }
534
535         if(item.itemdef == ITEM_ExtraLife)
536         {
537                 GiveResource(toucher, RES_ARMOR, autocvar_g_instagib_extralives);
538                 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES, autocvar_g_instagib_extralives);
539                 return MUT_ITEMTOUCH_PICKUP;
540         }
541
542         return MUT_ITEMTOUCH_CONTINUE;
543 }
544
545 MUTATOR_HOOKFUNCTION(mutator_instagib, OnEntityPreSpawn)
546 {
547         if (MUTATOR_RETURNVALUE) return false;
548         if (!autocvar_g_powerups) { return; }
549         entity ent = M_ARGV(0, entity);
550         // Can't use .itemdef here
551         if (!(ent.classname == "item_strength" || ent.classname == "item_shield" || ent.classname == "item_health_mega"))
552                 return;
553
554         entity e = spawn();
555
556         float r = random();
557         if (r < 0.3)
558         {
559                 e.classname = "item_invisibility";
560                 setthink(e, instagib_invisibility);
561         }
562         else if (r < 0.6)
563         {
564                 e.classname = "item_extralife";
565                 setthink(e, instagib_extralife);
566         }
567         else
568         {
569                 e.classname = "item_speed";
570                 setthink(e, instagib_speed);
571         }
572
573         e.nextthink = time + 0.1;
574         e.spawnflags = ent.spawnflags;
575         e.noalign = ent.noalign;
576         setorigin(e, ent.origin);
577
578         return true;
579 }
580
581 MUTATOR_HOOKFUNCTION(mutator_instagib, BuildMutatorsString)
582 {
583         M_ARGV(0, string) = strcat(M_ARGV(0, string), ":instagib");
584 }
585
586 MUTATOR_HOOKFUNCTION(mutator_instagib, BuildMutatorsPrettyString)
587 {
588         M_ARGV(0, string) = strcat(M_ARGV(0, string), ", instagib");
589 }
590
591 MUTATOR_HOOKFUNCTION(mutator_instagib, SetModname)
592 {
593         M_ARGV(0, string) = "InstaGib";
594         return true;
595 }