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