]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator/gamemode_assault.qc
Merge branch 'master' into terencehill/gameover_stuff
[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_Assault, 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 vehicles_clearreturn(entity veh);
184 void vehicles_spawn(entity this);
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         FOREACH_ENTITY_FLOAT(pure_data, false,
199         {
200                 if(IS_CLIENT(it))
201                         continue;
202
203                 if (it.team_saved == NUM_TEAM_1) it.team_saved = NUM_TEAM_2;
204                 else if (it.team_saved == NUM_TEAM_2) it.team_saved = NUM_TEAM_1;
205         });
206
207         // reset the level with a countdown
208         cvar_set("timelimit", ftos(ceil(time - AS_ROUND_DELAY - game_starttime) / 60));
209         ReadyRestart_force(); // sets game_starttime
210 }
211
212 entity as_round;
213 .entity ent_winning;
214 void as_round_think()
215 {
216         gameover = false;
217         assault_new_round(as_round.ent_winning);
218         delete(as_round);
219         as_round = NULL;
220 }
221
222 // Assault winning condition: If the attackers triggered a round end (by fulfilling all objectives)
223 // they win. Otherwise the defending team wins once the timelimit passes.
224 int WinningCondition_Assault()
225 {
226         if(as_round)
227                 return WINNING_NO;
228
229         WinningConditionHelper(NULL); // set worldstatus
230
231         int status = WINNING_NO;
232         // as the timelimit has not yet passed just assume the defending team will win
233         if(assault_attacker_team == NUM_TEAM_1)
234         {
235                 SetWinners(team, NUM_TEAM_2);
236         }
237         else
238         {
239                 SetWinners(team, NUM_TEAM_1);
240         }
241
242         entity ent;
243         ent = find(NULL, classname, "target_assault_roundend");
244         if(ent)
245         {
246                 if(ent.winning) // round end has been triggered by attacking team
247                 {
248                         bprint("Assault: round completed.\n");
249                         SetWinners(team, assault_attacker_team);
250
251                         TeamScore_AddToTeam(assault_attacker_team, ST_ASSAULT_OBJECTIVES, 666 - TeamScore_AddToTeam(assault_attacker_team, ST_ASSAULT_OBJECTIVES, 0));
252
253                         if(ent.cnt == 1 || autocvar_g_campaign) // this was the second round
254                         {
255                                 status = WINNING_YES;
256                         }
257                         else
258                         {
259                                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ASSAULT_OBJ_DESTROYED, ceil(time - game_starttime));
260                                 as_round = new(as_round);
261                                 as_round.think = as_round_think;
262                                 as_round.ent_winning = ent;
263                                 as_round.nextthink = time + AS_ROUND_DELAY;
264                                 gameover = true;
265
266                                 // make sure timelimit isn't hit while the game is blocked
267                                 if(autocvar_timelimit > 0)
268                                 if(time + AS_ROUND_DELAY >= game_starttime + autocvar_timelimit * 60)
269                                         cvar_set("timelimit", ftos(autocvar_timelimit + AS_ROUND_DELAY / 60));
270                         }
271                 }
272         }
273
274         return status;
275 }
276
277 // spawnfuncs
278 spawnfunc(info_player_attacker)
279 {
280         if (!g_assault) { delete(this); return; }
281
282         this.team = NUM_TEAM_1; // red, gets swapped every round
283         spawnfunc_info_player_deathmatch(this);
284 }
285
286 spawnfunc(info_player_defender)
287 {
288         if (!g_assault) { delete(this); return; }
289
290         this.team = NUM_TEAM_2; // blue, gets swapped every round
291         spawnfunc_info_player_deathmatch(this);
292 }
293
294 spawnfunc(target_objective)
295 {
296         if (!g_assault) { delete(this); return; }
297
298         this.classname = "target_objective";
299         IL_PUSH(g_assault_objectives, this);
300         this.use = assault_objective_use;
301         this.reset = assault_objective_reset;
302         this.reset(this);
303         this.spawn_evalfunc = target_objective_spawn_evalfunc;
304 }
305
306 spawnfunc(target_objective_decrease)
307 {
308         if (!g_assault) { delete(this); return; }
309
310         this.classname = "target_objective_decrease";
311         IL_PUSH(g_assault_objectivedecreasers, this);
312
313         if(!this.dmg)
314                 this.dmg = 101;
315
316         this.use = assault_objective_decrease_use;
317         this.health = ASSAULT_VALUE_INACTIVE;
318         this.max_health = ASSAULT_VALUE_INACTIVE;
319         this.enemy = NULL;
320
321         InitializeEntity(this, target_objective_decrease_findtarget, INITPRIO_FINDTARGET);
322 }
323
324 // destructible walls that can be used to trigger target_objective_decrease
325 spawnfunc(func_breakable);
326 spawnfunc(func_assault_destructible)
327 {
328         if (!g_assault) { delete(this); return; }
329
330         this.spawnflags = 3;
331         this.classname = "func_assault_destructible";
332         IL_PUSH(g_assault_destructibles, this);
333
334         if(assault_attacker_team == NUM_TEAM_1)
335                 this.team = NUM_TEAM_2;
336         else
337                 this.team = NUM_TEAM_1;
338
339         spawnfunc_func_breakable(this);
340 }
341
342 spawnfunc(func_assault_wall)
343 {
344         if (!g_assault) { delete(this); return; }
345
346         this.classname = "func_assault_wall";
347         this.mdl = this.model;
348         _setmodel(this, this.mdl);
349         this.solid = SOLID_BSP;
350         setthink(this, assault_wall_think);
351         this.nextthink = time;
352         InitializeEntity(this, assault_setenemytoobjective, INITPRIO_FINDTARGET);
353 }
354
355 spawnfunc(target_assault_roundend)
356 {
357         if (!g_assault) { delete(this); return; }
358
359         this.winning = 0; // round not yet won by attackers
360         this.classname = "target_assault_roundend";
361         this.use = target_assault_roundend_use;
362         this.cnt = 0; // first round
363         this.reset = target_assault_roundend_reset;
364 }
365
366 spawnfunc(target_assault_roundstart)
367 {
368         if (!g_assault) { delete(this); return; }
369
370         assault_attacker_team = NUM_TEAM_1;
371         this.classname = "target_assault_roundstart";
372         this.use = assault_roundstart_use;
373         this.reset2 = assault_roundstart_use_this;
374         InitializeEntity(this, assault_roundstart_use_this, INITPRIO_FINDTARGET);
375 }
376
377 // legacy bot code
378 void havocbot_goalrating_ast_targets(entity this, float ratingscale)
379 {
380         IL_EACH(g_assault_destructibles, it.bot_attack,
381         {
382                 if (it.target == "")
383                         continue;
384
385                 bool found = false;
386                 entity destr = it;
387                 IL_EACH(g_assault_objectivedecreasers, it.targetname == destr.target,
388                 {
389                         if(it.enemy.health > 0 && it.enemy.health < ASSAULT_VALUE_INACTIVE)
390                         {
391                                 found = true;
392                                 break;
393                         }
394                 });
395
396                 if(!found)
397                         continue;
398
399                 vector p = 0.5 * (it.absmin + it.absmax);
400
401                 // Find and rate waypoints around it
402                 found = false;
403                 entity best = NULL;
404                 float bestvalue = 99999999999;
405                 entity des = it;
406                 for(float radius = 0; radius < 1500 && !found; radius += 500)
407                 {
408                         FOREACH_ENTITY_RADIUS(p, radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
409                         {
410                                 if(checkpvs(it.origin, des))
411                                 {
412                                         found = true;
413                                         if(it.cnt < bestvalue)
414                                         {
415                                                 best = it;
416                                                 bestvalue = it.cnt;
417                                         }
418                                 }
419                         });
420                 }
421
422                 if(best)
423                 {
424                 ///     dprint("waypoints around target were found\n");
425                 //      te_lightning2(NULL, '0 0 0', best.origin);
426                 //      te_knightspike(best.origin);
427
428                         navigation_routerating(this, best, ratingscale, 4000);
429                         best.cnt += 1;
430
431                         this.havocbot_attack_time = 0;
432
433                         if(checkpvs(this.view_ofs,it))
434                         if(checkpvs(this.view_ofs,best))
435                         {
436                         //      dprint("increasing attack time for this target\n");
437                                 this.havocbot_attack_time = time + 2;
438                         }
439                 }
440         });
441 }
442
443 void havocbot_role_ast_offense(entity this)
444 {
445         if(IS_DEAD(this))
446         {
447                 this.havocbot_attack_time = 0;
448                 havocbot_ast_reset_role(this);
449                 return;
450         }
451
452         // Set the role timeout if necessary
453         if (!this.havocbot_role_timeout)
454                 this.havocbot_role_timeout = time + 120;
455
456         if (time > this.havocbot_role_timeout)
457         {
458                 havocbot_ast_reset_role(this);
459                 return;
460         }
461
462         if(this.havocbot_attack_time>time)
463                 return;
464
465         if (this.bot_strategytime < time)
466         {
467                 navigation_goalrating_start(this);
468                 havocbot_goalrating_enemyplayers(this, 20000, this.origin, 650);
469                 havocbot_goalrating_ast_targets(this, 20000);
470                 havocbot_goalrating_items(this, 15000, this.origin, 10000);
471                 navigation_goalrating_end(this);
472
473                 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
474         }
475 }
476
477 void havocbot_role_ast_defense(entity this)
478 {
479         if(IS_DEAD(this))
480         {
481                 this.havocbot_attack_time = 0;
482                 havocbot_ast_reset_role(this);
483                 return;
484         }
485
486         // Set the role timeout if necessary
487         if (!this.havocbot_role_timeout)
488                 this.havocbot_role_timeout = time + 120;
489
490         if (time > this.havocbot_role_timeout)
491         {
492                 havocbot_ast_reset_role(this);
493                 return;
494         }
495
496         if(this.havocbot_attack_time>time)
497                 return;
498
499         if (this.bot_strategytime < time)
500         {
501                 navigation_goalrating_start(this);
502                 havocbot_goalrating_enemyplayers(this, 20000, this.origin, 3000);
503                 havocbot_goalrating_ast_targets(this, 20000);
504                 havocbot_goalrating_items(this, 15000, this.origin, 10000);
505                 navigation_goalrating_end(this);
506
507                 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
508         }
509 }
510
511 void havocbot_role_ast_setrole(entity this, float role)
512 {
513         switch(role)
514         {
515                 case HAVOCBOT_AST_ROLE_DEFENSE:
516                         this.havocbot_role = havocbot_role_ast_defense;
517                         this.havocbot_role_flags = HAVOCBOT_AST_ROLE_DEFENSE;
518                         this.havocbot_role_timeout = 0;
519                         break;
520                 case HAVOCBOT_AST_ROLE_OFFENSE:
521                         this.havocbot_role = havocbot_role_ast_offense;
522                         this.havocbot_role_flags = HAVOCBOT_AST_ROLE_OFFENSE;
523                         this.havocbot_role_timeout = 0;
524                         break;
525         }
526 }
527
528 void havocbot_ast_reset_role(entity this)
529 {
530         if(IS_DEAD(this))
531                 return;
532
533         if(this.team == assault_attacker_team)
534                 havocbot_role_ast_setrole(this, HAVOCBOT_AST_ROLE_OFFENSE);
535         else
536                 havocbot_role_ast_setrole(this, HAVOCBOT_AST_ROLE_DEFENSE);
537 }
538
539 // mutator hooks
540 MUTATOR_HOOKFUNCTION(as, PlayerSpawn)
541 {
542         entity player = M_ARGV(0, entity);
543
544         if(player.team == assault_attacker_team)
545                 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_ASSAULT_ATTACKING);
546         else
547                 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_ASSAULT_DEFENDING);
548 }
549
550 MUTATOR_HOOKFUNCTION(as, TurretSpawn)
551 {
552         entity turret = M_ARGV(0, entity);
553
554         if(!turret.team || turret.team == MAX_SHOT_DISTANCE)
555                 turret.team = 5; // this gets reversed when match starts?
556 }
557
558 MUTATOR_HOOKFUNCTION(as, VehicleInit)
559 {
560         entity veh = M_ARGV(0, entity);
561
562         if(veh.active != ACTIVE_NOT)
563                 veh.nextthink = time + 0.5;
564         return true;
565 }
566
567 MUTATOR_HOOKFUNCTION(as, HavocBot_ChooseRole)
568 {
569         entity bot = M_ARGV(0, entity);
570
571         havocbot_ast_reset_role(bot);
572         return true;
573 }
574
575 MUTATOR_HOOKFUNCTION(as, PlayHitsound)
576 {
577         entity frag_victim = M_ARGV(0, entity);
578
579         return (frag_victim.classname == "func_assault_destructible");
580 }
581
582 MUTATOR_HOOKFUNCTION(as, CheckAllowedTeams)
583 {
584         // assault always has 2 teams
585         c1 = c2 = 0;
586         return true;
587 }
588
589 MUTATOR_HOOKFUNCTION(as, CheckRules_World)
590 {
591         M_ARGV(0, float) = WinningCondition_Assault();
592         return true;
593 }
594
595 MUTATOR_HOOKFUNCTION(as, ReadLevelCvars)
596 {
597         // no assault warmups
598         warmup_stage = 0;
599 }
600
601 MUTATOR_HOOKFUNCTION(as, OnEntityPreSpawn)
602 {
603     entity ent = M_ARGV(0, entity);
604
605         switch(ent.classname)
606         {
607                 case "info_player_team1":
608                 case "info_player_team2":
609                 case "info_player_team3":
610                 case "info_player_team4":
611                         return true;
612         }
613 }
614
615 // scoreboard setup
616 void assault_ScoreRules()
617 {
618         int teams = 0;
619         teams |= BIT(0);
620         teams |= BIT(1); // always red vs blue
621
622         ScoreRules_basics(teams, SFL_SORT_PRIO_SECONDARY, SFL_SORT_PRIO_SECONDARY, true);
623         ScoreInfo_SetLabel_TeamScore(  ST_ASSAULT_OBJECTIVES,    "objectives",      SFL_SORT_PRIO_PRIMARY);
624         ScoreInfo_SetLabel_PlayerScore(SP_ASSAULT_OBJECTIVES,    "objectives",      SFL_SORT_PRIO_PRIMARY);
625         ScoreRules_basics_end();
626 }