]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/gamemode_domination.qc
Merge branch 'master' into terencehill/ca_fixes
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / gamemode_domination.qc
1 void dom_EventLog(string mode, float team_before, entity actor) // use an alias for easy changing and quick editing later
2 {
3         if(autocvar_sv_eventlog)
4                 GameLogEcho(strcat(":dom:", mode, ":", ftos(team_before), ((actor != world) ? (strcat(":", ftos(actor.playerid))) : "")));
5 }
6
7 void set_dom_state(entity e)
8 {
9         e.dom_total_pps = total_pps;
10         e.dom_pps_red = pps_red;
11         e.dom_pps_blue = pps_blue;
12         if(domination_teams >= 3)
13                 e.dom_pps_yellow = pps_yellow;
14         if(domination_teams >= 4)
15                 e.dom_pps_pink = pps_pink;
16 }
17
18 void dompoint_captured ()
19 {
20         entity head;
21         float old_delay, old_team, real_team;
22         string msg = "dom-neut";
23
24         // now that the delay has expired, switch to the latest team to lay claim to this point
25         head = self.owner;
26
27         real_team = self.cnt;
28         self.cnt = -1;
29
30         dom_EventLog("taken", self.team, self.dmg_inflictor);
31         self.dmg_inflictor = world;
32
33         self.goalentity = head;
34         self.model = head.mdl;
35         self.modelindex = head.dmg;
36         self.skin = head.skin;
37
38         float points, wait_time;
39         if (autocvar_g_domination_point_amt)
40                 points = autocvar_g_domination_point_amt;
41         else
42                 points = self.frags;
43         if (autocvar_g_domination_point_rate)
44                 wait_time = autocvar_g_domination_point_rate;
45         else
46                 wait_time = self.wait;
47
48         if(domination_roundbased)
49                 bprint(sprintf("^3%s^3%s\n", head.netname, self.message));
50         else
51                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_DOMINATION_CAPTURE_TIME, head.netname, self.message, points, wait_time);
52
53         if(self.enemy.playerid == self.enemy_playerid)
54                 PlayerScore_Add(self.enemy, SP_DOM_TAKES, 1);
55         else
56                 self.enemy = world;
57
58         if (head.noise != "")
59                 if(self.enemy)
60                         sound(self.enemy, CH_TRIGGER, head.noise, VOL_BASE, ATTEN_NORM);
61                 else
62                         sound(self, CH_TRIGGER, head.noise, VOL_BASE, ATTEN_NORM);
63         if (head.noise1 != "")
64                 play2all(head.noise1);
65
66         self.delay = time + wait_time;
67
68         // do trigger work
69         old_delay = self.delay;
70         old_team = self.team;
71         self.team = real_team;
72         self.delay = 0;
73         activator = self;
74         SUB_UseTargets ();
75         self.delay = old_delay;
76         self.team = old_team;
77
78         switch(self.team)
79         {
80                 case NUM_TEAM_1: msg = "dom-red"; break;
81                 case NUM_TEAM_2: msg = "dom-blue"; break;
82                 case NUM_TEAM_3: msg = "dom-yellow"; break;
83                 case NUM_TEAM_4: msg = "dom-pink"; break;
84         }
85
86         WaypointSprite_UpdateSprites(self.sprite, msg, "", "");
87
88         total_pps = 0, pps_red = 0, pps_blue = 0, pps_yellow = 0, pps_pink = 0;
89         for(head = world; (head = find(head, classname, "dom_controlpoint")) != world; )
90         {
91                 if (autocvar_g_domination_point_amt)
92                         points = autocvar_g_domination_point_amt;
93                 else
94                         points = head.frags;
95                 if (autocvar_g_domination_point_rate)
96                         wait_time = autocvar_g_domination_point_rate;
97                 else
98                         wait_time = head.wait;
99                 switch(head.goalentity.team)
100                 {
101                         case NUM_TEAM_1:
102                                 pps_red += points/wait_time;
103                                 break;
104                         case NUM_TEAM_2:
105                                 pps_blue += points/wait_time;
106                                 break;
107                         case NUM_TEAM_3:
108                                 pps_yellow += points/wait_time;
109                                 break;
110                         case NUM_TEAM_4:
111                                 pps_pink += points/wait_time;
112                                 break;
113                 }
114                 total_pps += points/wait_time;
115         }
116
117         WaypointSprite_UpdateTeamRadar(self.sprite, RADARICON_DOMPOINT, colormapPaletteColor(self.goalentity.team - 1, 0));
118         WaypointSprite_Ping(self.sprite);
119
120         self.captime = time;
121
122         FOR_EACH_REALCLIENT(head)
123                 set_dom_state(head);
124 }
125
126 void AnimateDomPoint()
127 {
128         if(self.pain_finished > time)
129                 return;
130         self.pain_finished = time + self.t_width;
131         if(self.nextthink > self.pain_finished)
132                 self.nextthink = self.pain_finished;
133
134         self.frame = self.frame + 1;
135         if(self.frame > self.t_length)
136                 self.frame = 0;
137 }
138
139 void dompointthink()
140 {
141         float fragamt;
142
143         self.nextthink = time + 0.1;
144
145         //self.frame = self.frame + 1;
146         //if(self.frame > 119)
147         //      self.frame = 0;
148         AnimateDomPoint();
149
150         // give points
151
152         if (gameover || self.delay > time || time < game_starttime)     // game has ended, don't keep giving points
153                 return;
154
155         if(autocvar_g_domination_point_rate)
156                 self.delay = time + autocvar_g_domination_point_rate;
157         else
158                 self.delay = time + self.wait;
159
160         // give credit to the team
161         // NOTE: this defaults to 0
162         if (!domination_roundbased)
163         if (self.goalentity.netname != "")
164         {
165                 if(autocvar_g_domination_point_amt)
166                         fragamt = autocvar_g_domination_point_amt;
167                 else
168                         fragamt = self.frags;
169                 TeamScore_AddToTeam(self.goalentity.team, ST_SCORE, fragamt);
170                 TeamScore_AddToTeam(self.goalentity.team, ST_DOM_TICKS, fragamt);
171
172                 // give credit to the individual player, if he is still there
173                 if (self.enemy.playerid == self.enemy_playerid)
174                 {
175                         PlayerScore_Add(self.enemy, SP_SCORE, fragamt);
176                         PlayerScore_Add(self.enemy, SP_DOM_TICKS, fragamt);
177                 }
178                 else
179                         self.enemy = world;
180         }
181 }
182
183 void dompointtouch()
184 {
185         entity head;
186         if (!IS_PLAYER(other))
187                 return;
188         if (other.health < 1)
189                 return;
190
191         if(round_handler_IsActive() && !round_handler_IsRoundStarted())
192                 return;
193
194         if(time < self.captime + 0.3)
195                 return;
196
197         // only valid teams can claim it
198         head = find(world, classname, "dom_team");
199         while (head && head.team != other.team)
200                 head = find(head, classname, "dom_team");
201         if (!head || head.netname == "" || head == self.goalentity)
202                 return;
203
204         // delay capture
205
206         self.team = self.goalentity.team; // this stores the PREVIOUS team!
207
208         self.cnt = other.team;
209         self.owner = head; // team to switch to after the delay
210         self.dmg_inflictor = other;
211
212         // self.state = 1;
213         // self.delay = time + cvar("g_domination_point_capturetime");
214         //self.nextthink = time + cvar("g_domination_point_capturetime");
215         //self.think = dompoint_captured;
216
217         // go to neutral team in the mean time
218         head = find(world, classname, "dom_team");
219         while (head && head.netname != "")
220                 head = find(head, classname, "dom_team");
221         if(head == world)
222                 return;
223
224         WaypointSprite_UpdateSprites(self.sprite, "dom-neut", "", "");
225         WaypointSprite_UpdateTeamRadar(self.sprite, RADARICON_DOMPOINT, '0 1 1');
226         WaypointSprite_Ping(self.sprite);
227
228         self.goalentity = head;
229         self.model = head.mdl;
230         self.modelindex = head.dmg;
231         self.skin = head.skin;
232
233         self.enemy = other; // individual player scoring
234         self.enemy_playerid = other.playerid;
235         dompoint_captured();
236 }
237
238 void dom_controlpoint_setup()
239 {
240         entity head;
241         // find the spawnfunc_dom_team representing unclaimed points
242         head = find(world, classname, "dom_team");
243         while(head && head.netname != "")
244                 head = find(head, classname, "dom_team");
245         if (!head)
246                 objerror("no spawnfunc_dom_team with netname \"\" found\n");
247
248         // copy important properties from spawnfunc_dom_team entity
249         self.goalentity = head;
250         setmodel(self, head.mdl); // precision already set
251         self.skin = head.skin;
252
253         self.cnt = -1;
254
255         if(self.message == "")
256                 self.message = " has captured a control point";
257
258         if(self.frags <= 0)
259                 self.frags = 1;
260         if(self.wait <= 0)
261                 self.wait = 5;
262
263         float points, waittime;
264         if (autocvar_g_domination_point_amt)
265                 points = autocvar_g_domination_point_amt;
266         else
267                 points = self.frags;
268         if (autocvar_g_domination_point_rate)
269                 waittime = autocvar_g_domination_point_rate;
270         else
271                 waittime = self.wait;
272
273         total_pps += points/waittime;
274
275         if(!self.t_width)
276                 self.t_width = 0.02; // frame animation rate
277         if(!self.t_length)
278                 self.t_length = 239; // maximum frame
279
280         self.think = dompointthink;
281         self.nextthink = time;
282         self.touch = dompointtouch;
283         self.solid = SOLID_TRIGGER;
284         self.flags = FL_ITEM;
285         setsize(self, '-32 -32 -32', '32 32 32');
286         setorigin(self, self.origin + '0 0 20');
287         droptofloor();
288
289         waypoint_spawnforitem(self);
290         WaypointSprite_SpawnFixed("dom-neut", self.origin + '0 0 32', self, sprite, RADARICON_DOMPOINT, '0 1 1');
291 }
292
293 float total_controlpoints, redowned, blueowned, yellowowned, pinkowned;
294 void Domination_count_controlpoints()
295 {
296         entity e;
297         total_controlpoints = redowned = blueowned = yellowowned = pinkowned = 0;
298         for(e = world; (e = find(e, classname, "dom_controlpoint")) != world; )
299         {
300                 ++total_controlpoints;
301                 redowned += (e.goalentity.team == NUM_TEAM_1);
302                 blueowned += (e.goalentity.team == NUM_TEAM_2);
303                 yellowowned += (e.goalentity.team == NUM_TEAM_3);
304                 pinkowned += (e.goalentity.team == NUM_TEAM_4);
305         }
306 }
307
308 float Domination_GetWinnerTeam()
309 {
310         float winner_team = 0;
311         if(redowned == total_controlpoints)
312                 winner_team = NUM_TEAM_1;
313         if(blueowned == total_controlpoints)
314         {
315                 if(winner_team) return 0;
316                 winner_team = NUM_TEAM_2;
317         }
318         if(yellowowned == total_controlpoints)
319         {
320                 if(winner_team) return 0;
321                 winner_team = NUM_TEAM_3;
322         }
323         if(pinkowned == total_controlpoints)
324         {
325                 if(winner_team) return 0;
326                 winner_team = NUM_TEAM_4;
327         }
328         if(winner_team)
329                 return winner_team;
330         return -1; // no control points left?
331 }
332
333 #define DOM_OWNED_CONTROLPOINTS() ((redowned > 0) + (blueowned > 0) + (yellowowned > 0) + (pinkowned > 0))
334 #define DOM_OWNED_CONTROLPOINTS_OK() (DOM_OWNED_CONTROLPOINTS() < total_controlpoints)
335 float Domination_CheckWinner()
336 {
337         if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
338         {
339                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_OVER);
340                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_OVER);
341                 round_handler_Init(5, autocvar_g_domination_warmup, autocvar_g_domination_round_timelimit);
342                 return 1;
343         }
344
345         Domination_count_controlpoints();
346
347         float winner_team = Domination_GetWinnerTeam();
348
349         if(winner_team == -1)
350                 return 0;
351
352         if(winner_team > 0)
353         {
354                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, APP_TEAM_NUM_4(winner_team, CENTER_ROUND_TEAM_WIN_));
355                 Send_Notification(NOTIF_ALL, world, MSG_INFO, APP_TEAM_NUM_4(winner_team, INFO_ROUND_TEAM_WIN_));
356                 TeamScore_AddToTeam(winner_team, ST_DOM_CAPS, +1);
357         }
358         else if(winner_team == -1)
359         {
360                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_TIED);
361                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_TIED);
362         }
363
364         round_handler_Init(5, autocvar_g_domination_warmup, autocvar_g_domination_round_timelimit);
365
366         return 1;
367 }
368
369 float Domination_CheckPlayers()
370 {
371         return 1;
372 }
373
374 void Domination_RoundStart()
375 {
376         entity e;
377         FOR_EACH_PLAYER(e)
378                 e.player_blocked = 0;
379 }
380
381 //go to best items, or control points you don't own
382 void havocbot_role_dom()
383 {
384         if(self.deadflag != DEAD_NO)
385                 return;
386
387         if (self.bot_strategytime < time)
388         {
389                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
390                 navigation_goalrating_start();
391                 havocbot_goalrating_controlpoints(10000, self.origin, 15000);
392                 havocbot_goalrating_items(8000, self.origin, 8000);
393                 //havocbot_goalrating_enemyplayers(3000, self.origin, 2000);
394                 //havocbot_goalrating_waypoints(1, self.origin, 1000);
395                 navigation_goalrating_end();
396         }
397 }
398
399 MUTATOR_HOOKFUNCTION(dom_GetTeamCount)
400 {
401         ret_float = domination_teams;
402         return 0;
403 }
404
405 MUTATOR_HOOKFUNCTION(dom_ResetMap)
406 {
407         total_pps = 0, pps_red = 0, pps_blue = 0, pps_yellow = 0, pps_pink = 0;
408         FOR_EACH_PLAYER(self)
409         {
410                 PutClientInServer();
411                 self.player_blocked = 1;
412                 if(IS_REAL_CLIENT(self))
413                         set_dom_state(self);
414         }
415         return 1;
416 }
417
418 MUTATOR_HOOKFUNCTION(dom_PlayerSpawn)
419 {
420         if(domination_roundbased)
421         if(!round_handler_IsRoundStarted())
422                 self.player_blocked = 1;
423         else
424                 self.player_blocked = 0;
425         return FALSE;
426 }
427
428 MUTATOR_HOOKFUNCTION(dom_ClientConnect)
429 {
430         set_dom_state(self);
431         return FALSE;
432 }
433
434 MUTATOR_HOOKFUNCTION(dom_BotRoles)
435 {
436         self.havocbot_role = havocbot_role_dom;
437         return TRUE;
438 }
439
440 /*QUAKED spawnfunc_dom_controlpoint (0 .5 .8) (-16 -16 -24) (16 16 32)
441 Control point for Domination gameplay.
442 */
443 void spawnfunc_dom_controlpoint()
444 {
445         if(!g_domination)
446         {
447                 remove(self);
448                 return;
449         }
450         self.think = dom_controlpoint_setup;
451         self.nextthink = time + 0.1;
452         self.reset = dom_controlpoint_setup;
453
454         if(!self.scale)
455                 self.scale = 0.6;
456
457         self.effects = self.effects | EF_LOWPRECISION;
458         if (autocvar_g_domination_point_fullbright)
459                 self.effects |= EF_FULLBRIGHT;
460 }
461
462 /*QUAKED spawnfunc_dom_team (0 .5 .8) (-32 -32 -24) (32 32 32)
463 Team declaration for Domination gameplay, this allows you to decide what team
464 names and control point models are used in your map.
465
466 Note: If you use spawnfunc_dom_team entities you must define at least 3 and only two
467 can have netname set!  The nameless team owns all control points at start.
468
469 Keys:
470 "netname"
471  Name of the team (for example Red Team, Blue Team, Green Team, Yellow Team, Life, Death, etc)
472 "cnt"
473  Scoreboard color of the team (for example 4 is red and 13 is blue)
474 "model"
475  Model to use for control points owned by this team (for example
476  "progs/b_g_key.mdl" is a gold keycard, and "progs/b_s_key.mdl" is a silver
477  keycard)
478 "skin"
479  Skin of the model to use (for team skins on a single model)
480 "noise"
481  Sound to play when this team captures a point.
482  (this is a localized sound, like a small alarm or other effect)
483 "noise1"
484  Narrator speech to play when this team captures a point.
485  (this is a global sound, like "Red team has captured a control point")
486 */
487
488 void spawnfunc_dom_team()
489 {
490         if(!g_domination || autocvar_g_domination_teams_override >= 2)
491         {
492                 remove(self);
493                 return;
494         }
495         precache_model(self.model);
496         if (self.noise != "")
497                 precache_sound(self.noise);
498         if (self.noise1 != "")
499                 precache_sound(self.noise1);
500         self.classname = "dom_team";
501         setmodel(self, self.model); // precision not needed
502         self.mdl = self.model;
503         self.dmg = self.modelindex;
504         self.model = "";
505         self.modelindex = 0;
506         // this would have to be changed if used in quakeworld
507         if(self.cnt)
508                 self.team = self.cnt + 1; // WHY are these different anyway?
509 }
510
511 // scoreboard setup
512 void ScoreRules_dom(float teams)
513 {
514         if(domination_roundbased)
515         {
516                 ScoreRules_basics(teams, SFL_SORT_PRIO_PRIMARY, 0, TRUE);
517                 ScoreInfo_SetLabel_TeamScore  (ST_DOM_CAPS, "caps", SFL_SORT_PRIO_PRIMARY);
518                 ScoreInfo_SetLabel_PlayerScore(SP_DOM_TAKES, "takes", 0);
519                 ScoreRules_basics_end();
520         }
521         else
522         {
523                 float sp_domticks, sp_score;
524                 sp_score = sp_domticks = 0;
525                 if(autocvar_g_domination_disable_frags)
526                         sp_domticks = SFL_SORT_PRIO_PRIMARY;
527                 else
528                         sp_score = SFL_SORT_PRIO_PRIMARY;
529                 ScoreRules_basics(teams, sp_score, sp_score, TRUE);
530                 ScoreInfo_SetLabel_TeamScore  (ST_DOM_TICKS,    "ticks",     sp_domticks);
531                 ScoreInfo_SetLabel_PlayerScore(SP_DOM_TICKS,    "ticks",     sp_domticks);
532                 ScoreInfo_SetLabel_PlayerScore(SP_DOM_TAKES,    "takes",     0);
533                 ScoreRules_basics_end();
534         }
535 }
536
537 // code from here on is just to support maps that don't have control point and team entities
538 void dom_spawnteam (string teamname, float teamcolor, string pointmodel, float pointskin, string capsound, string capnarration, string capmessage)
539 {
540         entity oldself;
541         oldself = self;
542         self = spawn();
543         self.classname = "dom_team";
544         self.netname = teamname;
545         self.cnt = teamcolor;
546         self.model = pointmodel;
547         self.skin = pointskin;
548         self.noise = capsound;
549         self.noise1 = capnarration;
550         self.message = capmessage;
551
552         // this code is identical to spawnfunc_dom_team
553         setmodel(self, self.model); // precision not needed
554         self.mdl = self.model;
555         self.dmg = self.modelindex;
556         self.model = "";
557         self.modelindex = 0;
558         // this would have to be changed if used in quakeworld
559         self.team = self.cnt + 1;
560
561         //eprint(self);
562         self = oldself;
563 }
564
565 void dom_spawnpoint(vector org)
566 {
567         entity oldself;
568         oldself = self;
569         self = spawn();
570         self.classname = "dom_controlpoint";
571         self.think = spawnfunc_dom_controlpoint;
572         self.nextthink = time;
573         setorigin(self, org);
574         spawnfunc_dom_controlpoint();
575         self = oldself;
576 }
577
578 // spawn some default teams if the map is not set up for domination
579 void dom_spawnteams(float teams)
580 {
581         dom_spawnteam("Red", NUM_TEAM_1-1, "models/domination/dom_red.md3", 0, "domination/claim.wav", "", "Red team has captured a control point");
582         dom_spawnteam("Blue", NUM_TEAM_2-1, "models/domination/dom_blue.md3", 0, "domination/claim.wav", "", "Blue team has captured a control point");
583         if(teams >= 3)
584                 dom_spawnteam("Yellow", NUM_TEAM_3-1, "models/domination/dom_yellow.md3", 0, "domination/claim.wav", "", "Yellow team has captured a control point");
585         if(teams >= 4)
586                 dom_spawnteam("Pink", NUM_TEAM_4-1, "models/domination/dom_pink.md3", 0, "domination/claim.wav", "", "Pink team has captured a control point");
587         dom_spawnteam("", 0, "models/domination/dom_unclaimed.md3", 0, "", "", "");
588 }
589
590 void dom_DelayedInit() // Do this check with a delay so we can wait for teams to be set up.
591 {
592         // if no teams are found, spawn defaults
593         if(find(world, classname, "dom_team") == world || autocvar_g_domination_teams_override >= 2)
594         {
595                 print("No ""dom_team"" entities found on this map, creating them anyway.\n");
596                 domination_teams = bound(2, ((autocvar_g_domination_teams_override < 2) ? autocvar_g_domination_default_teams : autocvar_g_domination_teams_override), 4);
597                 dom_spawnteams(domination_teams);
598         }
599         
600         CheckAllowedTeams(world);
601         domination_teams = ((c4>=0) ? 4 : (c3>=0) ? 3 : 2);
602
603         addstat(STAT_DOM_TOTAL_PPS, AS_FLOAT, dom_total_pps);
604         addstat(STAT_DOM_PPS_RED, AS_FLOAT, dom_pps_red);
605         addstat(STAT_DOM_PPS_BLUE, AS_FLOAT, dom_pps_blue);
606         if(domination_teams >= 3) addstat(STAT_DOM_PPS_YELLOW, AS_FLOAT, dom_pps_yellow);
607         if(domination_teams >= 4) addstat(STAT_DOM_PPS_PINK, AS_FLOAT, dom_pps_pink);
608
609         domination_roundbased = autocvar_g_domination_roundbased;
610
611         ScoreRules_dom(domination_teams);
612
613         if(domination_roundbased)
614         {
615                 round_handler_Spawn(Domination_CheckPlayers, Domination_CheckWinner, Domination_RoundStart);
616                 round_handler_Init(5, autocvar_g_domination_warmup, autocvar_g_domination_round_timelimit);
617         }
618 }
619
620 void dom_Initialize()
621 {
622         precache_model("models/domination/dom_red.md3");
623         precache_model("models/domination/dom_blue.md3");
624         precache_model("models/domination/dom_yellow.md3");
625         precache_model("models/domination/dom_pink.md3");
626         precache_model("models/domination/dom_unclaimed.md3");
627         precache_sound("domination/claim.wav");
628
629         InitializeEntity(world, dom_DelayedInit, INITPRIO_GAMETYPE);
630 }
631
632
633 MUTATOR_DEFINITION(gamemode_domination)
634 {
635         MUTATOR_HOOK(GetTeamCount, dom_GetTeamCount, CBC_ORDER_ANY);
636         MUTATOR_HOOK(reset_map_players, dom_ResetMap, CBC_ORDER_ANY);
637         MUTATOR_HOOK(PlayerSpawn, dom_PlayerSpawn, CBC_ORDER_ANY);
638         MUTATOR_HOOK(ClientConnect, dom_ClientConnect, CBC_ORDER_ANY);
639         MUTATOR_HOOK(HavocBot_ChooseRole, dom_BotRoles, CBC_ORDER_ANY);
640
641         MUTATOR_ONADD
642         {
643                 if(time > 1) // game loads at time 1
644                         error("This is a game type and it cannot be added at runtime.");
645                 dom_Initialize();
646         }
647
648         MUTATOR_ONREMOVE
649         {
650                 error("This is a game type and it cannot be removed at runtime.");
651         }
652
653         return 0;
654 }