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