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