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