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