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