]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/gamemode_assault.qc
Merge branch 'master' into Mario/turrets
[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         entity ent, oldself;
174
175         //(Re)spawn all turrets
176         oldself = self;
177         ent = find(world, classname, "turret_main");
178         while(ent) {
179                 // Swap turret teams
180                 if(ent.team == NUM_TEAM_1)
181                         ent.team = NUM_TEAM_2;
182                 else
183                         ent.team = NUM_TEAM_1;
184
185                 self = ent;
186
187                 // Dubbles as teamchange
188                 turret_respawn();
189
190                 ent = find(ent, classname, "turret_main");
191         }
192         self = oldself;
193 }
194
195 void assault_wall_think()
196 {
197         if(self.enemy.health < 0)
198         {
199                 self.model = "";
200                 self.solid = SOLID_NOT;
201         }
202         else
203         {
204                 self.model = self.mdl;
205                 self.solid = SOLID_BSP;
206         }
207
208         self.nextthink = time + 0.2;
209 }
210
211 // trigger new round
212 // reset objectives, toggle spawnpoints, reset triggers, ...
213 void assault_new_round()
214 {
215         // up round counter
216         self.winning = self.winning + 1;
217
218         // swap attacker/defender roles
219         if(assault_attacker_team == NUM_TEAM_1)
220                 assault_attacker_team = NUM_TEAM_2;
221         else
222                 assault_attacker_team = NUM_TEAM_1;
223
224         entity ent;
225         for(ent = world; (ent = nextent(ent)); )
226         {
227                 if(clienttype(ent) == CLIENTTYPE_NOTACLIENT)
228                 {
229                         if(ent.team_saved == NUM_TEAM_1)
230                                 ent.team_saved = NUM_TEAM_2;
231                         else if(ent.team_saved == NUM_TEAM_2)
232                                 ent.team_saved = NUM_TEAM_1;
233                 }
234         }
235
236         // reset the level with a countdown
237         cvar_set("timelimit", ftos(ceil(time - game_starttime) / 60));
238         ReadyRestart_force(); // sets game_starttime
239 }
240
241 // spawnfuncs
242 void spawnfunc_info_player_attacker()
243 {
244         if (!g_assault) { remove(self); return; }
245
246         self.team = NUM_TEAM_1; // red, gets swapped every round
247         spawnfunc_info_player_deathmatch();
248 }
249
250 void spawnfunc_info_player_defender()
251 {
252         if (!g_assault) { remove(self); return; }
253
254         self.team = NUM_TEAM_2; // blue, gets swapped every round
255         spawnfunc_info_player_deathmatch();
256 }
257
258 void spawnfunc_target_objective()
259 {
260         if (!g_assault) { remove(self); return; }
261
262         self.classname = "target_objective";
263         self.use = assault_objective_use;
264         assault_objective_reset();
265         self.reset = assault_objective_reset;
266         self.spawn_evalfunc = target_objective_spawn_evalfunc;
267 }
268
269 void spawnfunc_target_objective_decrease()
270 {
271         if (!g_assault) { remove(self); return; }
272
273         self.classname = "target_objective_decrease";
274
275         if(!self.dmg)
276                 self.dmg = 101;
277
278         self.use = assault_objective_decrease_use;
279         self.health = ASSAULT_VALUE_INACTIVE;
280         self.max_health = ASSAULT_VALUE_INACTIVE;
281         self.enemy = world;
282
283         InitializeEntity(self, target_objective_decrease_findtarget, INITPRIO_FINDTARGET);
284 }
285
286 // destructible walls that can be used to trigger target_objective_decrease
287 void spawnfunc_func_assault_destructible()
288 {
289         if (!g_assault) { remove(self); return; }
290
291         self.spawnflags = 3;
292         self.classname = "func_assault_destructible";
293
294         if(assault_attacker_team == NUM_TEAM_1)
295                 self.team = NUM_TEAM_2;
296         else
297                 self.team = NUM_TEAM_1;
298
299         spawnfunc_func_breakable();
300 }
301
302 void spawnfunc_func_assault_wall()
303 {
304         if (!g_assault) { remove(self); return; }
305
306         self.classname = "func_assault_wall";
307         self.mdl = self.model;
308         setmodel(self, self.mdl);
309         self.solid = SOLID_BSP;
310         self.think = assault_wall_think;
311         self.nextthink = time;
312         InitializeEntity(self, assault_setenemytoobjective, INITPRIO_FINDTARGET);
313 }
314
315 void spawnfunc_target_assault_roundend()
316 {
317         if (!g_assault) { remove(self); return; }
318
319         self.winning = 0; // round not yet won by attackers
320         self.classname = "target_assault_roundend";
321         self.use = target_assault_roundend_use;
322         self.cnt = 0; // first round
323         self.reset = target_assault_roundend_reset;
324 }
325
326 void spawnfunc_target_assault_roundstart()
327 {
328         if (!g_assault) { remove(self); return; }
329
330         assault_attacker_team = NUM_TEAM_1;
331         self.classname = "target_assault_roundstart";
332         self.use = assault_roundstart_use;
333         self.reset2 = assault_roundstart_use;
334         InitializeEntity(self, assault_roundstart_use, INITPRIO_FINDTARGET);
335 }
336
337 // legacy bot code
338 void havocbot_goalrating_ast_targets(float ratingscale)
339 {
340         entity ad, best, wp, tod;
341         float radius, found, bestvalue;
342         vector p;
343
344         ad = findchain(classname, "func_assault_destructible");
345
346         for (; ad; ad = ad.chain)
347         {
348                 if (ad.target == "")
349                         continue;
350
351                 if (!ad.bot_attack)
352                         continue;
353
354                 found = FALSE;
355                 for(tod = world; (tod = find(tod, targetname, ad.target)); )
356                 {
357                         if(tod.classname == "target_objective_decrease")
358                         {
359                                 if(tod.enemy.health > 0 && tod.enemy.health < ASSAULT_VALUE_INACTIVE)
360                                 {
361                                 //      dprint(etos(ad),"\n");
362                                         found = TRUE;
363                                         break;
364                                 }
365                         }
366                 }
367
368                 if(!found)
369                 {
370                 ///     dprint("target not found\n");
371                         continue;
372                 }
373                 /// dprint("target #", etos(ad), " found\n");
374
375
376                 p = 0.5 * (ad.absmin + ad.absmax);
377         //      dprint(vtos(ad.origin), " ", vtos(ad.absmin), " ", vtos(ad.absmax),"\n");
378         //      te_knightspike(p);
379         //      te_lightning2(world, '0 0 0', p);
380
381                 // Find and rate waypoints around it
382                 found = FALSE;
383                 best = world;
384                 bestvalue = 99999999999;
385                 for(radius=0; radius<1500 && !found; radius+=500)
386                 {
387                         for(wp=findradius(p, radius); wp; wp=wp.chain)
388                         {
389                                 if(!(wp.wpflags & WAYPOINTFLAG_GENERATED))
390                                 if(wp.classname=="waypoint")
391                                 if(checkpvs(wp.origin, ad))
392                                 {
393                                         found = TRUE;
394                                         if(wp.cnt<bestvalue)
395                                         {
396                                                 best = wp;
397                                                 bestvalue = wp.cnt;
398                                         }
399                                 }
400                         }
401                 }
402
403                 if(best)
404                 {
405                 ///     dprint("waypoints around target were found\n");
406                 //      te_lightning2(world, '0 0 0', best.origin);
407                 //      te_knightspike(best.origin);
408
409                         navigation_routerating(best, ratingscale, 4000);
410                         best.cnt += 1;
411
412                         self.havocbot_attack_time = 0;
413
414                         if(checkpvs(self.view_ofs,ad))
415                         if(checkpvs(self.view_ofs,best))
416                         {
417                         //      dprint("increasing attack time for this target\n");
418                                 self.havocbot_attack_time = time + 2;
419                         }
420                 }
421         }
422 }
423
424 void havocbot_role_ast_offense()
425 {
426         if(self.deadflag != DEAD_NO)
427         {
428                 self.havocbot_attack_time = 0;
429                 havocbot_ast_reset_role(self);
430                 return;
431         }
432
433         // Set the role timeout if necessary
434         if (!self.havocbot_role_timeout)
435                 self.havocbot_role_timeout = time + 120;
436
437         if (time > self.havocbot_role_timeout)
438         {
439                 havocbot_ast_reset_role(self);
440                 return;
441         }
442
443         if(self.havocbot_attack_time>time)
444                 return;
445
446         if (self.bot_strategytime < time)
447         {
448                 navigation_goalrating_start();
449                 havocbot_goalrating_enemyplayers(20000, self.origin, 650);
450                 havocbot_goalrating_ast_targets(20000);
451                 havocbot_goalrating_items(15000, self.origin, 10000);
452                 navigation_goalrating_end();
453
454                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
455         }
456 }
457
458 void havocbot_role_ast_defense()
459 {
460         if(self.deadflag != DEAD_NO)
461         {
462                 self.havocbot_attack_time = 0;
463                 havocbot_ast_reset_role(self);
464                 return;
465         }
466
467         // Set the role timeout if necessary
468         if (!self.havocbot_role_timeout)
469                 self.havocbot_role_timeout = time + 120;
470
471         if (time > self.havocbot_role_timeout)
472         {
473                 havocbot_ast_reset_role(self);
474                 return;
475         }
476
477         if(self.havocbot_attack_time>time)
478                 return;
479
480         if (self.bot_strategytime < time)
481         {
482                 navigation_goalrating_start();
483                 havocbot_goalrating_enemyplayers(20000, self.origin, 3000);
484                 havocbot_goalrating_ast_targets(20000);
485                 havocbot_goalrating_items(15000, self.origin, 10000);
486                 navigation_goalrating_end();
487
488                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
489         }
490 }
491
492 void havocbot_role_ast_setrole(entity bot, float role)
493 {
494         switch(role)
495         {
496                 case HAVOCBOT_AST_ROLE_DEFENSE:
497                         bot.havocbot_role = havocbot_role_ast_defense;
498                         bot.havocbot_role_flags = HAVOCBOT_AST_ROLE_DEFENSE;
499                         bot.havocbot_role_timeout = 0;
500                         break;
501                 case HAVOCBOT_AST_ROLE_OFFENSE:
502                         bot.havocbot_role = havocbot_role_ast_offense;
503                         bot.havocbot_role_flags = HAVOCBOT_AST_ROLE_OFFENSE;
504                         bot.havocbot_role_timeout = 0;
505                         break;
506         }
507 }
508
509 void havocbot_ast_reset_role(entity bot)
510 {
511         if(self.deadflag != DEAD_NO)
512                 return;
513
514         if(bot.team == assault_attacker_team)
515                 havocbot_role_ast_setrole(bot, HAVOCBOT_AST_ROLE_OFFENSE);
516         else
517                 havocbot_role_ast_setrole(bot, HAVOCBOT_AST_ROLE_DEFENSE);
518 }
519
520 // mutator hooks
521 MUTATOR_HOOKFUNCTION(assault_PlayerSpawn)
522 {
523         if(self.team == assault_attacker_team)
524                 centerprint(self, "You are attacking!");
525         else
526                 centerprint(self, "You are defending!");
527
528         return FALSE;
529 }
530
531 MUTATOR_HOOKFUNCTION(assault_TurretSpawn)
532 {
533         if (!self.team)
534                 self.team = 14;
535
536         return FALSE;
537 }
538
539 MUTATOR_HOOKFUNCTION(assault_VehicleSpawn)
540 {
541         self.nextthink = time + 0.5;
542
543         return FALSE;
544 }
545
546 MUTATOR_HOOKFUNCTION(assault_BotRoles)
547 {
548         havocbot_ast_reset_role(self);
549         return TRUE;
550 }
551
552 // scoreboard setup
553 void assault_ScoreRules()
554 {
555         ScoreRules_basics(2, SFL_SORT_PRIO_SECONDARY, SFL_SORT_PRIO_SECONDARY, TRUE);
556         ScoreInfo_SetLabel_TeamScore(  ST_ASSAULT_OBJECTIVES,    "objectives",      SFL_SORT_PRIO_PRIMARY);
557         ScoreInfo_SetLabel_PlayerScore(SP_ASSAULT_OBJECTIVES,    "objectives",      SFL_SORT_PRIO_PRIMARY);
558         ScoreRules_basics_end();
559 }
560
561 MUTATOR_DEFINITION(gamemode_assault)
562 {
563         MUTATOR_HOOK(PlayerSpawn, assault_PlayerSpawn, CBC_ORDER_ANY);
564         MUTATOR_HOOK(TurretSpawn, assault_TurretSpawn, CBC_ORDER_ANY);
565         MUTATOR_HOOK(VehicleSpawn, assault_VehicleSpawn, CBC_ORDER_ANY);
566         MUTATOR_HOOK(HavocBot_ChooseRole, assault_BotRoles, CBC_ORDER_ANY);
567
568         MUTATOR_ONADD
569         {
570                 if(time > 1) // game loads at time 1
571                         error("This is a game type and it cannot be added at runtime.");
572                 assault_ScoreRules();
573         }
574
575         MUTATOR_ONROLLBACK_OR_REMOVE
576         {
577                 // we actually cannot roll back assault_Initialize here
578                 // BUT: we don't need to! If this gets called, adding always
579                 // succeeds.
580         }
581
582         MUTATOR_ONREMOVE
583         {
584                 print("This is a game type and it cannot be removed at runtime.");
585                 return -1;
586         }
587
588         return 0;
589 }