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