]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/assault/sv_assault.qc
Merge branch 'master' into DefaultUser/func_button_relay
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / gamemodes / gamemode / assault / sv_assault.qc
1 #include "sv_assault.qh"
2
3 .entity sprite;
4 #define AS_ROUND_DELAY 5
5
6 IntrusiveList g_assault_destructibles;
7 IntrusiveList g_assault_objectivedecreasers;
8 IntrusiveList g_assault_objectives;
9 STATIC_INIT(g_assault)
10 {
11         g_assault_destructibles = IL_NEW();
12         g_assault_objectivedecreasers = IL_NEW();
13         g_assault_objectives = IL_NEW();
14 }
15
16 // random functions
17 void assault_objective_use(entity this, entity actor, entity trigger)
18 {
19         // activate objective
20         SetResourceAmountExplicit(this, RESOURCE_HEALTH, 100);
21         //print("^2Activated objective ", this.targetname, "=", etos(this), "\n");
22         //print("Activator is ", actor.classname, "\n");
23
24         IL_EACH(g_assault_objectivedecreasers, it.target == this.targetname,
25         {
26                 target_objective_decrease_activate(it);
27         });
28 }
29
30 vector target_objective_spawn_evalfunc(entity this, entity player, entity spot, vector current)
31 {
32         float hlth = GetResourceAmount(this, RESOURCE_HEALTH);
33         if (hlth < 0 || hlth >= ASSAULT_VALUE_INACTIVE)
34                 return '-1 0 0';
35         return current;
36 }
37
38 // reset this objective. Used when spawning an objective
39 // and when a new round starts
40 void assault_objective_reset(entity this)
41 {
42         SetResourceAmountExplicit(this, RESOURCE_HEALTH, ASSAULT_VALUE_INACTIVE);
43 }
44
45 // decrease the health of targeted objectives
46 void assault_objective_decrease_use(entity this, entity actor, entity trigger)
47 {
48         if(actor.team != assault_attacker_team)
49         {
50                 // wrong team triggered decrease
51                 return;
52         }
53
54         if(trigger.assault_sprite)
55         {
56                 WaypointSprite_Disown(trigger.assault_sprite, waypointsprite_deadlifetime);
57                 if(trigger.classname == "func_assault_destructible")
58                         trigger.sprite = NULL; // TODO: just unsetting it?!
59         }
60         else
61                 return; // already activated! cannot activate again!
62
63         float hlth = GetResourceAmount(this.enemy, RESOURCE_HEALTH);
64         if (hlth < ASSAULT_VALUE_INACTIVE)
65         {
66                 if (hlth - this.dmg > 0.5)
67                 {
68                         GameRules_scoring_add_team(actor, SCORE, this.dmg);
69                         TakeResource(this.enemy, RESOURCE_HEALTH, this.dmg);
70                 }
71                 else
72                 {
73                         GameRules_scoring_add_team(actor, SCORE, hlth);
74                         GameRules_scoring_add_team(actor, ASSAULT_OBJECTIVES, 1);
75                         SetResourceAmountExplicit(this.enemy, RESOURCE_HEALTH, -1);
76
77                         if(this.enemy.message)
78                                 FOREACH_CLIENT(IS_PLAYER(it), { centerprint(it, this.enemy.message); });
79
80                         SUB_UseTargets(this.enemy, this, trigger);
81                 }
82         }
83 }
84
85 void assault_setenemytoobjective(entity this)
86 {
87         IL_EACH(g_assault_objectives, it.targetname == this.target,
88         {
89                 if(this.enemy == NULL)
90                         this.enemy = it;
91                 else
92                         objerror(this, "more than one objective as target - fix the map!");
93                 break;
94         });
95
96         if(this.enemy == NULL)
97                 objerror(this, "no objective as target - fix the map!");
98 }
99
100 bool assault_decreaser_sprite_visible(entity this, entity player, entity view)
101 {
102         if(GetResourceAmount(this.assault_decreaser.enemy, RESOURCE_HEALTH) >= ASSAULT_VALUE_INACTIVE)
103                 return false;
104
105         return true;
106 }
107
108 void target_objective_decrease_activate(entity this)
109 {
110         entity spr;
111         this.owner = NULL;
112         FOREACH_ENTITY_STRING(target, this.targetname,
113         {
114                 if(it.assault_sprite != NULL)
115                 {
116                         WaypointSprite_Disown(it.assault_sprite, waypointsprite_deadlifetime);
117                         if(it.classname == "func_assault_destructible")
118                                 it.sprite = NULL; // TODO: just unsetting it?!
119                 }
120
121                 spr = WaypointSprite_SpawnFixed(WP_AssaultDefend, 0.5 * (it.absmin + it.absmax), it, assault_sprite, RADARICON_OBJECTIVE);
122                 spr.assault_decreaser = this;
123                 spr.waypointsprite_visible_for_player = assault_decreaser_sprite_visible;
124                 spr.classname = "sprite_waypoint";
125                 WaypointSprite_UpdateRule(spr, assault_attacker_team, SPRITERULE_TEAMPLAY);
126                 if(it.classname == "func_assault_destructible")
127                 {
128                         WaypointSprite_UpdateSprites(spr, WP_AssaultDefend, WP_AssaultDestroy, WP_AssaultDestroy);
129                         WaypointSprite_UpdateMaxHealth(spr, it.max_health);
130                         WaypointSprite_UpdateHealth(spr, GetResourceAmount(it, RESOURCE_HEALTH));
131                         it.sprite = spr;
132                 }
133                 else
134                         WaypointSprite_UpdateSprites(spr, WP_AssaultDefend, WP_AssaultPush, WP_AssaultPush);
135         });
136 }
137
138 void target_objective_decrease_findtarget(entity this)
139 {
140         assault_setenemytoobjective(this);
141 }
142
143 void target_assault_roundend_reset(entity this)
144 {
145         //print("round end reset\n");
146         ++this.cnt; // up round counter
147         this.winning = false; // up round
148 }
149
150 void target_assault_roundend_use(entity this, entity actor, entity trigger)
151 {
152         this.winning = 1; // round has been won by attackers
153 }
154
155 void assault_roundstart_use(entity this, entity actor, entity trigger)
156 {
157         SUB_UseTargets(this, this, trigger);
158
159         //(Re)spawn all turrets
160         IL_EACH(g_turrets, true,
161         {
162                 // Swap turret teams
163                 if(it.team == NUM_TEAM_1)
164                         it.team = NUM_TEAM_2;
165                 else
166                         it.team = NUM_TEAM_1;
167
168                 // Doubles as teamchange
169                 turret_respawn(it);
170         });
171 }
172 void assault_roundstart_use_this(entity this)
173 {
174         assault_roundstart_use(this, NULL, NULL);
175 }
176
177 void assault_wall_think(entity this)
178 {
179         if(GetResourceAmount(this.enemy, RESOURCE_HEALTH) < 0)
180         {
181                 this.model = "";
182                 this.solid = SOLID_NOT;
183         }
184         else
185         {
186                 this.model = this.mdl;
187                 this.solid = SOLID_BSP;
188         }
189
190         this.nextthink = time + 0.2;
191 }
192
193 // trigger new round
194 // reset objectives, toggle spawnpoints, reset triggers, ...
195 void assault_new_round(entity this)
196 {
197         //bprint("ASSAULT: new round\n");
198
199         // up round counter
200         this.winning = this.winning + 1;
201
202         // swap attacker/defender roles
203         if(assault_attacker_team == NUM_TEAM_1)
204                 assault_attacker_team = NUM_TEAM_2;
205         else
206                 assault_attacker_team = NUM_TEAM_1;
207
208         IL_EACH(g_saved_team, !IS_CLIENT(it),
209         {
210                 if(it.team_saved == NUM_TEAM_1)
211                         it.team_saved = NUM_TEAM_2;
212                 else if(it.team_saved == NUM_TEAM_2)
213                         it.team_saved = NUM_TEAM_1;
214         });
215
216         // reset the level with a countdown
217         cvar_set("timelimit", ftos(ceil(time - AS_ROUND_DELAY - game_starttime) / 60));
218         ReadyRestart_force(); // sets game_starttime
219 }
220
221 entity as_round;
222 .entity ent_winning;
223 void as_round_think()
224 {
225         game_stopped = false;
226         assault_new_round(as_round.ent_winning);
227         delete(as_round);
228         as_round = NULL;
229 }
230
231 // Assault winning condition: If the attackers triggered a round end (by fulfilling all objectives)
232 // they win. Otherwise the defending team wins once the timelimit passes.
233 int WinningCondition_Assault()
234 {
235         if(as_round)
236                 return WINNING_NO;
237
238         WinningConditionHelper(NULL); // set worldstatus
239
240         int status = WINNING_NO;
241         // as the timelimit has not yet passed just assume the defending team will win
242         if(assault_attacker_team == NUM_TEAM_1)
243         {
244                 SetWinners(team, NUM_TEAM_2);
245         }
246         else
247         {
248                 SetWinners(team, NUM_TEAM_1);
249         }
250
251         entity ent;
252         ent = find(NULL, classname, "target_assault_roundend");
253         if(ent)
254         {
255                 if(ent.winning) // round end has been triggered by attacking team
256                 {
257                         bprint("Assault: round completed.\n");
258                         SetWinners(team, assault_attacker_team);
259
260                         TeamScore_AddToTeam(assault_attacker_team, ST_ASSAULT_OBJECTIVES, 666 - TeamScore_AddToTeam(assault_attacker_team, ST_ASSAULT_OBJECTIVES, 0));
261
262                         if(ent.cnt == 1 || autocvar_g_campaign) // this was the second round
263                         {
264                                 status = WINNING_YES;
265                         }
266                         else
267                         {
268                                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ASSAULT_OBJ_DESTROYED, ceil(time - game_starttime));
269                                 as_round = new(as_round);
270                                 as_round.think = as_round_think;
271                                 as_round.ent_winning = ent;
272                                 as_round.nextthink = time + AS_ROUND_DELAY;
273                                 game_stopped = true;
274
275                                 // make sure timelimit isn't hit while the game is blocked
276                                 if(autocvar_timelimit > 0)
277                                 if(time + AS_ROUND_DELAY >= game_starttime + autocvar_timelimit * 60)
278                                         cvar_set("timelimit", ftos(autocvar_timelimit + AS_ROUND_DELAY / 60));
279                         }
280                 }
281         }
282
283         return status;
284 }
285
286 // spawnfuncs
287 spawnfunc(info_player_attacker)
288 {
289         if (!g_assault) { delete(this); return; }
290
291         this.team = NUM_TEAM_1; // red, gets swapped every round
292         spawnfunc_info_player_deathmatch(this);
293 }
294
295 spawnfunc(info_player_defender)
296 {
297         if (!g_assault) { delete(this); return; }
298
299         this.team = NUM_TEAM_2; // blue, gets swapped every round
300         spawnfunc_info_player_deathmatch(this);
301 }
302
303 spawnfunc(target_objective)
304 {
305         if (!g_assault) { delete(this); return; }
306
307         this.classname = "target_objective";
308         IL_PUSH(g_assault_objectives, this);
309         this.use = assault_objective_use;
310         this.reset = assault_objective_reset;
311         this.reset(this);
312         this.spawn_evalfunc = target_objective_spawn_evalfunc;
313 }
314
315 spawnfunc(target_objective_decrease)
316 {
317         if (!g_assault) { delete(this); return; }
318
319         this.classname = "target_objective_decrease";
320         IL_PUSH(g_assault_objectivedecreasers, this);
321
322         if(!this.dmg)
323                 this.dmg = 101;
324
325         this.use = assault_objective_decrease_use;
326         SetResourceAmountExplicit(this, RESOURCE_HEALTH, ASSAULT_VALUE_INACTIVE);
327         this.max_health = ASSAULT_VALUE_INACTIVE;
328         this.enemy = NULL;
329
330         InitializeEntity(this, target_objective_decrease_findtarget, INITPRIO_FINDTARGET);
331 }
332
333 // destructible walls that can be used to trigger target_objective_decrease
334 bool destructible_heal(entity targ, entity inflictor, float amount, float limit)
335 {
336         float true_limit = ((limit != RESOURCE_LIMIT_NONE) ? limit : targ.max_health);
337         float hlth = GetResourceAmount(targ, RESOURCE_HEALTH);
338         if (hlth <= 0 || hlth >= true_limit)
339                 return false;
340
341         GiveResourceWithLimit(targ, RESOURCE_HEALTH, amount, true_limit);
342         if(targ.sprite)
343         {
344                 WaypointSprite_UpdateHealth(targ.sprite, GetResourceAmount(targ, RESOURCE_HEALTH));
345         }
346         func_breakable_colormod(targ);
347         return true;
348 }
349
350 spawnfunc(func_breakable);
351 spawnfunc(func_assault_destructible)
352 {
353         if (!g_assault) { delete(this); return; }
354
355         this.spawnflags = 3;
356         this.classname = "func_assault_destructible";
357         this.event_heal = destructible_heal;
358         IL_PUSH(g_assault_destructibles, this);
359
360         if(assault_attacker_team == NUM_TEAM_1)
361                 this.team = NUM_TEAM_2;
362         else
363                 this.team = NUM_TEAM_1;
364
365         spawnfunc_func_breakable(this);
366 }
367
368 spawnfunc(func_assault_wall)
369 {
370         if (!g_assault) { delete(this); return; }
371
372         this.classname = "func_assault_wall";
373         this.mdl = this.model;
374         _setmodel(this, this.mdl);
375         this.solid = SOLID_BSP;
376         setthink(this, assault_wall_think);
377         this.nextthink = time;
378         InitializeEntity(this, assault_setenemytoobjective, INITPRIO_FINDTARGET);
379 }
380
381 spawnfunc(target_assault_roundend)
382 {
383         if (!g_assault) { delete(this); return; }
384
385         this.winning = 0; // round not yet won by attackers
386         this.classname = "target_assault_roundend";
387         this.use = target_assault_roundend_use;
388         this.cnt = 0; // first round
389         this.reset = target_assault_roundend_reset;
390 }
391
392 spawnfunc(target_assault_roundstart)
393 {
394         if (!g_assault) { delete(this); return; }
395
396         assault_attacker_team = NUM_TEAM_1;
397         this.classname = "target_assault_roundstart";
398         this.use = assault_roundstart_use;
399         this.reset2 = assault_roundstart_use_this;
400         InitializeEntity(this, assault_roundstart_use_this, INITPRIO_FINDTARGET);
401 }
402
403 // legacy bot code
404 void havocbot_goalrating_ast_targets(entity this, float ratingscale)
405 {
406         IL_EACH(g_assault_destructibles, it.bot_attack,
407         {
408                 if (it.target == "")
409                         continue;
410
411                 bool found = false;
412                 entity destr = it;
413                 IL_EACH(g_assault_objectivedecreasers, it.targetname == destr.target,
414                 {
415                         float hlth = GetResourceAmount(it.enemy, RESOURCE_HEALTH);
416                         if (hlth > 0 && hlth < ASSAULT_VALUE_INACTIVE)
417                         {
418                                 found = true;
419                                 break;
420                         }
421                 });
422
423                 if(!found)
424                         continue;
425
426                 vector p = 0.5 * (it.absmin + it.absmax);
427
428                 // Find and rate waypoints around it
429                 found = false;
430                 entity best = NULL;
431                 float bestvalue = FLOAT_MAX;
432                 entity des = it;
433                 for (float radius = 500; radius <= 1500 && !found; radius += 500)
434                 {
435                         FOREACH_ENTITY_RADIUS(p, radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
436                         {
437                                 if(checkpvs(it.origin, des))
438                                 {
439                                         found = true;
440                                         if(it.cnt < bestvalue)
441                                         {
442                                                 best = it;
443                                                 bestvalue = it.cnt;
444                                         }
445                                 }
446                         });
447                 }
448
449                 if(best)
450                 {
451                 ///     dprint("waypoints around target were found\n");
452                 //      te_lightning2(NULL, '0 0 0', best.origin);
453                 //      te_knightspike(best.origin);
454
455                         navigation_routerating(this, best, ratingscale, 4000);
456                         best.cnt += 1;
457
458                         this.havocbot_attack_time = 0;
459
460                         if(checkpvs(this.origin + this.view_ofs, it))
461                         if(checkpvs(this.origin + this.view_ofs, best))
462                         {
463                         //      dprint("increasing attack time for this target\n");
464                                 this.havocbot_attack_time = time + 2;
465                         }
466                 }
467         });
468 }
469
470 void havocbot_role_ast_offense(entity this)
471 {
472         if(IS_DEAD(this))
473         {
474                 this.havocbot_attack_time = 0;
475                 havocbot_ast_reset_role(this);
476                 return;
477         }
478
479         // Set the role timeout if necessary
480         if (!this.havocbot_role_timeout)
481                 this.havocbot_role_timeout = time + 120;
482
483         if (time > this.havocbot_role_timeout)
484         {
485                 havocbot_ast_reset_role(this);
486                 return;
487         }
488
489         if(this.havocbot_attack_time>time)
490                 return;
491
492         if (navigation_goalrating_timeout(this))
493         {
494                 // role: offense
495                 navigation_goalrating_start(this);
496                 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 650);
497                 havocbot_goalrating_ast_targets(this, 20000);
498                 havocbot_goalrating_items(this, 30000, this.origin, 10000);
499                 navigation_goalrating_end(this);
500
501                 navigation_goalrating_timeout_set(this);
502         }
503 }
504
505 void havocbot_role_ast_defense(entity this)
506 {
507         if(IS_DEAD(this))
508         {
509                 this.havocbot_attack_time = 0;
510                 havocbot_ast_reset_role(this);
511                 return;
512         }
513
514         // Set the role timeout if necessary
515         if (!this.havocbot_role_timeout)
516                 this.havocbot_role_timeout = time + 120;
517
518         if (time > this.havocbot_role_timeout)
519         {
520                 havocbot_ast_reset_role(this);
521                 return;
522         }
523
524         if(this.havocbot_attack_time>time)
525                 return;
526
527         if (navigation_goalrating_timeout(this))
528         {
529                 // role: defense
530                 navigation_goalrating_start(this);
531                 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 3000);
532                 havocbot_goalrating_ast_targets(this, 20000);
533                 havocbot_goalrating_items(this, 30000, this.origin, 10000);
534                 navigation_goalrating_end(this);
535
536                 navigation_goalrating_timeout_set(this);
537         }
538 }
539
540 void havocbot_role_ast_setrole(entity this, float role)
541 {
542         switch(role)
543         {
544                 case HAVOCBOT_AST_ROLE_DEFENSE:
545                         this.havocbot_role = havocbot_role_ast_defense;
546                         this.havocbot_role_timeout = 0;
547                         break;
548                 case HAVOCBOT_AST_ROLE_OFFENSE:
549                         this.havocbot_role = havocbot_role_ast_offense;
550                         this.havocbot_role_timeout = 0;
551                         break;
552         }
553 }
554
555 void havocbot_ast_reset_role(entity this)
556 {
557         if(IS_DEAD(this))
558                 return;
559
560         if(this.team == assault_attacker_team)
561                 havocbot_role_ast_setrole(this, HAVOCBOT_AST_ROLE_OFFENSE);
562         else
563                 havocbot_role_ast_setrole(this, HAVOCBOT_AST_ROLE_DEFENSE);
564 }
565
566 // mutator hooks
567 MUTATOR_HOOKFUNCTION(as, PlayerSpawn)
568 {
569         entity player = M_ARGV(0, entity);
570
571         if(player.team == assault_attacker_team)
572                 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_ASSAULT_ATTACKING);
573         else
574                 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_ASSAULT_DEFENDING);
575 }
576
577 MUTATOR_HOOKFUNCTION(as, TurretSpawn)
578 {
579         entity turret = M_ARGV(0, entity);
580
581         if(!turret.team || turret.team == FLOAT_MAX)
582                 turret.team = 5; // this gets reversed when match starts?
583 }
584
585 MUTATOR_HOOKFUNCTION(as, VehicleInit)
586 {
587         entity veh = M_ARGV(0, entity);
588
589         veh.nextthink = time + 0.5;
590 }
591
592 MUTATOR_HOOKFUNCTION(as, HavocBot_ChooseRole)
593 {
594         entity bot = M_ARGV(0, entity);
595
596         havocbot_ast_reset_role(bot);
597         return true;
598 }
599
600 MUTATOR_HOOKFUNCTION(as, PlayHitsound)
601 {
602         entity frag_victim = M_ARGV(0, entity);
603
604         return (frag_victim.classname == "func_assault_destructible");
605 }
606
607 MUTATOR_HOOKFUNCTION(as, TeamBalance_CheckAllowedTeams)
608 {
609         // assault always has 2 teams
610         M_ARGV(0, float) = BIT(0) | BIT(1);
611         return true;
612 }
613
614 MUTATOR_HOOKFUNCTION(as, CheckRules_World)
615 {
616         M_ARGV(0, float) = WinningCondition_Assault();
617         return true;
618 }
619
620 MUTATOR_HOOKFUNCTION(as, ReadLevelCvars)
621 {
622         // incompatible
623         warmup_stage = 0;
624         sv_ready_restart_after_countdown = 0;
625 }
626
627 MUTATOR_HOOKFUNCTION(as, OnEntityPreSpawn)
628 {
629     entity ent = M_ARGV(0, entity);
630
631         switch(ent.classname)
632         {
633                 case "info_player_team1":
634                 case "info_player_team2":
635                 case "info_player_team3":
636                 case "info_player_team4":
637                         return true;
638         }
639 }
640
641 MUTATOR_HOOKFUNCTION(as, ReadyRestart_Deny)
642 {
643         // readyrestart not supported (yet)
644         return true;
645 }