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