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