]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/domination.qc
Fix calculation of the initial total_pps
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / domination.qc
1
2 /*
3 Domination as a plugin for netquake mods
4 by LordHavoc (lordhavoc@ghdigital.com)
5
6 How to add domination points to a mod:
7 1. Add this line to progs.src above world.qc:
8 domination.qc
9 2. Comment out all lines in ClientObituary in client.qc that begin with targ.frags  or attacker.frags.
10 3. Add this above spawnfunc_worldspawn in world.qc:
11 void() dom_init;
12 4. Add this line to the end of spawnfunc_worldspawn in world.qc:
13 dom_init();
14
15 Note: The only teams who can use dom control points are identified by spawnfunc_dom_team entities (if none exist these default to red and blue and use only quake models/sounds).
16 */
17
18 #define DOMPOINTFRAGS frags
19
20 .float enemy_playerid;
21 .entity sprite;
22 .float captime;
23
24 // pps: points per second
25 .float dom_total_pps;
26 .float dom_pps_red;
27 .float dom_pps_blue;
28 .float dom_pps_yellow;
29 .float dom_pps_pink;
30 float total_pps;
31 float pps_red;
32 float pps_blue;
33 float pps_yellow;
34 float pps_pink;
35 void set_dom_state(entity e)
36 {
37         e.dom_total_pps = total_pps;
38         e.dom_pps_red = pps_red;
39         e.dom_pps_blue = pps_blue;
40         if(c3 >= 0)
41                 e.dom_pps_yellow = pps_yellow;
42         if(c4 >= 0)
43                 e.dom_pps_pink = pps_pink;
44 }
45
46 void() dom_controlpoint_setup;
47
48 void LogDom(string mode, float team_before, entity actor)
49 {
50         string s;
51         if(!autocvar_sv_eventlog)
52                 return;
53         s = strcat(":dom:", mode);
54         s = strcat(s, ":", ftos(team_before));
55         s = strcat(s, ":", ftos(actor.playerid));
56         GameLogEcho(s);
57 }
58
59 void() dom_spawnteams;
60
61 void dompoint_captured ()
62 {
63         local entity head;
64         local float old_delay, old_team, real_team;
65
66         // now that the delay has expired, switch to the latest team to lay claim to this point
67         head = self.owner;
68
69         real_team = self.cnt;
70         self.cnt = -1;
71
72         LogDom("taken", self.team, self.dmg_inflictor);
73         self.dmg_inflictor = world;
74
75         self.goalentity = head;
76         self.model = head.mdl;
77         self.modelindex = head.dmg;
78         self.skin = head.skin;
79
80         //bprint(head.message);
81         //bprint("\n");
82
83         //bprint(^3head.netname);
84         //bprint(head.netname);
85         //bprint(self.message);
86         //bprint("\n");
87
88         float points, wait_time;
89         if (autocvar_g_domination_point_amt)
90                 points = autocvar_g_domination_point_amt;
91         else
92                 points = self.frags;
93         if (autocvar_g_domination_point_rate)
94                 wait_time = autocvar_g_domination_point_rate;
95         else
96                 wait_time = self.wait;
97
98         bprint("^3", head.netname, "^3", self.message);
99         if (points != 1)
100                 bprint(" ^7(", ftos(points), " points every ", ftos(wait_time), " seconds)\n");
101         else
102                 bprint(" ^7(", ftos(points), " point every ", ftos(wait_time), " seconds)\n");
103
104         if(self.enemy.playerid == self.enemy_playerid)
105                 PlayerScore_Add(self.enemy, SP_DOM_TAKES, 1);
106         else
107                 self.enemy = world;
108
109         if (head.noise != "")
110                 if(self.enemy)
111                         sound(self.enemy, CH_TRIGGER, head.noise, VOL_BASE, ATTN_NORM);
112                 else
113                         sound(self, CH_TRIGGER, head.noise, VOL_BASE, ATTN_NORM);
114         if (head.noise1 != "")
115                 play2all(head.noise1);
116
117         //self.nextthink = time + autocvar_g_domination_point_rate;
118         //self.think = dompointthink;
119
120         self.delay = time + wait_time;
121
122         // do trigger work
123         old_delay = self.delay;
124         old_team = self.team;
125         self.team = real_team;
126         self.delay = 0;
127         activator = self;
128         SUB_UseTargets ();
129         self.delay = old_delay;
130         self.team = old_team;
131
132         total_pps = 0, pps_red = 0, pps_blue = 0, pps_yellow = 0, pps_pink = 0;
133         for(head = world; (head = find(head, classname, "dom_controlpoint")) != world; )
134         {
135                 if (autocvar_g_domination_point_amt)
136                         points = autocvar_g_domination_point_amt;
137                 else
138                         points = head.frags;
139                 if (autocvar_g_domination_point_rate)
140                         wait_time = autocvar_g_domination_point_rate;
141                 else
142                         wait_time = head.wait;
143                 switch(head.goalentity.team)
144                 {
145                         case COLOR_TEAM1:
146                                 pps_red += points/wait_time;
147                                 break;
148                         case COLOR_TEAM2:
149                                 pps_blue += points/wait_time;
150                                 break;
151                         case COLOR_TEAM3:
152                                 pps_yellow += points/wait_time;
153                                 break;
154                         case COLOR_TEAM4:
155                                 pps_pink += points/wait_time;
156                 }
157                 total_pps += points/wait_time;
158         }
159
160         WaypointSprite_UpdateTeamRadar(self.sprite, RADARICON_DOMPOINT, colormapPaletteColor(self.goalentity.team - 1, 0));
161         WaypointSprite_Ping(self.sprite);
162
163         self.captime = time;
164
165         FOR_EACH_REALCLIENT(head)
166                 set_dom_state(head);
167 };
168
169 void AnimateDomPoint()
170 {
171         if(self.pain_finished > time)
172                 return;
173         self.pain_finished = time + self.t_width;
174         if(self.nextthink > self.pain_finished)
175                 self.nextthink = self.pain_finished;
176
177         self.frame = self.frame + 1;
178         if(self.frame > self.t_length)
179                 self.frame = 0;
180 }
181
182 void dompointthink()
183 {
184         local float fragamt;
185
186         self.nextthink = time + 0.1;
187
188         //self.frame = self.frame + 1;
189         //if(self.frame > 119)
190         //      self.frame = 0;
191         AnimateDomPoint();
192
193         // give points
194
195         if (gameover || self.delay > time || time < game_starttime)     // game has ended, don't keep giving points
196                 return;
197
198         if(autocvar_g_domination_point_rate)
199                 self.delay = time + autocvar_g_domination_point_rate;
200         else
201                 self.delay = time + self.wait;
202
203         // give credit to the team
204         // NOTE: this defaults to 0
205         if (self.goalentity.netname != "")
206         {
207                 if(autocvar_g_domination_point_amt)
208                         fragamt = autocvar_g_domination_point_amt;
209                 else
210                         fragamt = self.DOMPOINTFRAGS;
211                 TeamScore_AddToTeam(self.goalentity.team, ST_SCORE, fragamt);
212                 TeamScore_AddToTeam(self.goalentity.team, ST_DOM_TICKS, fragamt);
213
214                 // give credit to the individual player, if he is still there
215                 if (self.enemy.playerid == self.enemy_playerid)
216                 {
217                         PlayerScore_Add(self.enemy, SP_SCORE, fragamt);
218                         PlayerScore_Add(self.enemy, SP_DOM_TICKS, fragamt);
219                 }
220                 else
221                         self.enemy = world;
222         }
223 }
224
225 void dompointtouch()
226 {
227         local entity head;
228         if (other.classname != "player")
229                 return;
230         if (other.health < 1)
231                 return;
232
233         if(time < self.captime + 0.3)
234                 return;
235
236         // only valid teams can claim it
237         head = find(world, classname, "dom_team");
238         while (head && head.team != other.team)
239                 head = find(head, classname, "dom_team");
240         if (!head || head.netname == "" || head == self.goalentity)
241                 return;
242
243         // delay capture
244
245         self.team = self.goalentity.team; // this stores the PREVIOUS team!
246
247         self.cnt = other.team;
248         self.owner = head; // team to switch to after the delay
249         self.dmg_inflictor = other;
250
251         // self.state = 1;
252         // self.delay = time + cvar("g_domination_point_capturetime");
253         //self.nextthink = time + cvar("g_domination_point_capturetime");
254         //self.think = dompoint_captured;
255
256         // go to neutral team in the mean time
257         head = find(world, classname, "dom_team");
258         while (head && head.netname != "")
259                 head = find(head, classname, "dom_team");
260         if(head == world)
261                 return;
262
263         WaypointSprite_UpdateSprites(self.sprite, "dom-neut", "", "");
264         WaypointSprite_UpdateTeamRadar(self.sprite, RADARICON_DOMPOINT, '0 1 1');
265         WaypointSprite_Ping(self.sprite);
266
267         self.goalentity = head;
268         self.model = head.mdl;
269         self.modelindex = head.dmg;
270         self.skin = head.skin;
271
272         self.enemy = other; // individual player scoring
273         self.enemy_playerid = other.playerid;
274         dompoint_captured();
275 };
276
277 /*QUAKED spawnfunc_dom_team (0 .5 .8) (-32 -32 -24) (32 32 32)
278 Team declaration for Domination gameplay, this allows you to decide what team
279 names and control point models are used in your map.
280
281 Note: If you use spawnfunc_dom_team entities you must define at least 3 and only two
282 can have netname set!  The nameless team owns all control points at start.
283
284 Keys:
285 "netname"
286  Name of the team (for example Red Team, Blue Team, Green Team, Yellow Team, Life, Death, etc)
287 "cnt"
288  Scoreboard color of the team (for example 4 is red and 13 is blue)
289 "model"
290  Model to use for control points owned by this team (for example
291  "progs/b_g_key.mdl" is a gold keycard, and "progs/b_s_key.mdl" is a silver
292  keycard)
293 "skin"
294  Skin of the model to use (for team skins on a single model)
295 "noise"
296  Sound to play when this team captures a point.
297  (this is a localized sound, like a small alarm or other effect)
298 "noise1"
299  Narrator speech to play when this team captures a point.
300  (this is a global sound, like "Red team has captured a control point")
301 */
302
303 void spawnfunc_dom_team()
304 {
305         if(!g_domination || autocvar_g_domination_teams_override >= 2)
306         {
307                 remove(self);
308                 return;
309         }
310         precache_model(self.model);
311         if (self.noise != "")
312                 precache_sound(self.noise);
313         if (self.noise1 != "")
314                 precache_sound(self.noise1);
315         self.classname = "dom_team";
316         setmodel(self, self.model); // precision not needed
317         self.mdl = self.model;
318         self.dmg = self.modelindex;
319         self.model = "";
320         self.modelindex = 0;
321         // this would have to be changed if used in quakeworld
322         if(self.cnt)
323                 self.team = self.cnt + 1; // WHY are these different anyway?
324 };
325
326 void dom_controlpoint_setup()
327 {
328         local entity head;
329         // find the spawnfunc_dom_team representing unclaimed points
330         head = find(world, classname, "dom_team");
331         while(head && head.netname != "")
332                 head = find(head, classname, "dom_team");
333         if (!head)
334                 objerror("no spawnfunc_dom_team with netname \"\" found\n");
335
336         // copy important properties from spawnfunc_dom_team entity
337         self.goalentity = head;
338         setmodel(self, head.mdl); // precision already set
339         self.skin = head.skin;
340
341         self.cnt = -1;
342
343         if(!self.message)
344                 self.message = " has captured a control point";
345
346         if(self.DOMPOINTFRAGS <= 0)
347                 self.DOMPOINTFRAGS = 1;
348         if(self.wait <= 0)
349                 self.wait = 5;
350
351         float points, waittime;
352         if (autocvar_g_domination_point_amt)
353                 points = autocvar_g_domination_point_amt;
354         else
355                 points = self.frags;
356         if (autocvar_g_domination_point_rate)
357                 waittime = autocvar_g_domination_point_rate;
358         else
359                 waittime = self.wait;
360
361         total_pps += points/waittime;
362
363         if(!self.t_width)
364                 self.t_width = 0.02; // frame animation rate
365         if(!self.t_length)
366                 self.t_length = 239; // maximum frame
367
368         self.think = dompointthink;
369         self.nextthink = time;
370         self.touch = dompointtouch;
371         self.solid = SOLID_TRIGGER;
372         self.flags = FL_ITEM;
373         setsize(self, '-32 -32 -32', '32 32 32');
374         setorigin(self, self.origin + '0 0 20');
375         droptofloor();
376
377         waypoint_spawnforitem(self);
378         WaypointSprite_SpawnFixed("dom-neut", self.origin + '0 0 32', self, sprite, RADARICON_DOMPOINT, '0 1 1');
379 };
380
381
382
383 // player has joined game, get him on a team
384 // depreciated
385 /*void dom_player_join_team(entity pl)
386 {
387         entity head;
388         float c1, c2, c3, c4, totalteams, smallestteam, smallestteam_count, selectedteam;
389         float balance_teams, force_balance, balance_type;
390
391         balance_teams = autocvar_g_balance_teams;
392         balance_teams = autocvar_g_balance_teams_force;
393
394         c1 = c2 = c3 = c4 = -1;
395         totalteams = 0;
396
397         // first find out what teams are allowed
398         head = find(world, classname, "dom_team");
399         while(head)
400         {
401                 if(head.netname != "")
402                 {
403                         //if(head.team == pl.team)
404                         //      selected = head;
405                         if(head.team == COLOR_TEAM1)
406                         {
407                                         c1 = 0;
408                         }
409                         if(head.team == COLOR_TEAM2)
410                         {
411                                         c2 = 0;
412                         }
413                         if(head.team == COLOR_TEAM3)
414                         {
415                                         c3 = 0;
416                         }
417                         if(head.team == COLOR_TEAM4)
418                         {
419                                         c4 = 0;
420                         }
421                 }
422                 head = find(head, classname, "dom_team");
423         }
424
425         // make sure there are at least 2 teams to join
426         if(c1 >= 0)
427                 totalteams = totalteams + 1;
428         if(c2 >= 0)
429                 totalteams = totalteams + 1;
430         if(c3 >= 0)
431                 totalteams = totalteams + 1;
432         if(c4 >= 0)
433                 totalteams = totalteams + 1;
434
435         if(totalteams <= 1)
436                 error("dom_player_join_team: Too few teams available for domination\n");
437
438         // whichever teams that are available are set to 0 instead of -1
439
440         // if we don't care what team he ends up on, put him on whatever team he entered as.
441         // if he's not on a valid team, then put him on the smallest team
442         if(!balance_teams && !force_balance)
443         {
444                 if(     c1 >= 0 && pl.team == COLOR_TEAM1)
445                         selectedteam = pl.team;
446                 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
447                         selectedteam = pl.team;
448                 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
449                         selectedteam = pl.team;
450                 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
451                         selectedteam = pl.team;
452                 else
453                         selectedteam = -1;
454                 if(selectedteam > 0)
455                 {
456                         SetPlayerColors(pl, selectedteam - 1);
457                         return;
458                 }
459                 // otherwise end up on the smallest team (handled below)
460         }
461
462         // now count how many players are on each team already
463
464         head = find(world, classname, "player");
465         while(head)
466         {
467                 //if(head.netname != "")
468                 {
469                         if(head.team == COLOR_TEAM1)
470                         {
471                                 if(c1 >= 0)
472                                         c1 = c1 + 1;
473                         }
474                         if(head.team == COLOR_TEAM2)
475                         {
476                                 if(c2 >= 0)
477                                         c2 = c2 + 1;
478                         }
479                         if(head.team == COLOR_TEAM3)
480                         {
481                                 if(c3 >= 0)
482                                         c3 = c3 + 1;
483                         }
484                         if(head.team == COLOR_TEAM4)
485                         {
486                                 if(c4 >= 0)
487                                         c4 = c4 + 1;
488                         }
489                 }
490                 head = find(head, classname, "player");
491         }
492
493         // c1...c4 now have counts of each team
494         // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
495
496         smallestteam = 0;
497         smallestteam_count = 999;
498
499         // 2 gives priority to what team you're already on, 1 goes in order
500         balance_type = 1;
501
502         if(balance_type == 1)
503         {
504                 if(c1 >= 0 && c1 < smallestteam_count)
505                 {
506                         smallestteam = 1;
507                         smallestteam_count = c1;
508                 }
509                 if(c2 >= 0 && c2 < smallestteam_count)
510                 {
511                         smallestteam = 2;
512                         smallestteam_count = c2;
513                 }
514                 if(c3 >= 0 && c3 < smallestteam_count)
515                 {
516                         smallestteam = 3;
517                         smallestteam_count = c3;
518                 }
519                 if(c4 >= 0 && c4 < smallestteam_count)
520                 {
521                         smallestteam = 4;
522                         smallestteam_count = c4;
523                 }
524         }
525         else
526         {
527                 if(c1 >= 0 && (c1 < smallestteam_count ||
528                                         (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
529                 {
530                         smallestteam = 1;
531                         smallestteam_count = c1;
532                 }
533                 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
534                                         (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
535                 {
536                         smallestteam = 2;
537                         smallestteam_count = c2;
538                 }
539                 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
540                                         (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
541                 {
542                         smallestteam = 3;
543                         smallestteam_count = c3;
544                 }
545                 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
546                                         (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
547                 {
548                         smallestteam = 4;
549                         smallestteam_count = c4;
550                 }
551         }
552
553         if(smallestteam == 1)
554         {
555                 selectedteam = COLOR_TEAM1 - 1;
556         }
557         if(smallestteam == 2)
558         {
559                 selectedteam = COLOR_TEAM2 - 1;
560         }
561         if(smallestteam == 3)
562         {
563                 selectedteam = COLOR_TEAM3 - 1;
564         }
565         if(smallestteam == 4)
566         {
567                 selectedteam = COLOR_TEAM4 - 1;
568         }
569
570         SetPlayerColors(pl, selectedteam);
571 }
572 */
573 /*QUAKED spawnfunc_dom_controlpoint (0 .5 .8) (-16 -16 -24) (16 16 32)
574 Control point for Domination gameplay.
575 */
576 void spawnfunc_dom_controlpoint()
577 {
578         if(!g_domination)
579         {
580                 remove(self);
581                 return;
582         }
583         self.think = dom_controlpoint_setup;
584         self.nextthink = time + 0.1;
585         self.reset = dom_controlpoint_setup;
586
587         if(!self.scale)
588                 self.scale = 0.6;
589
590         //if(!self.glow_size)
591         //      self.glow_size = cvar("g_domination_point_glow");
592         self.effects = self.effects | EF_LOWPRECISION;
593         if (autocvar_g_domination_point_fullbright)
594                 self.effects |= EF_FULLBRIGHT;
595 };
596
597 // code from here on is just to support maps that don't have control point and team entities
598 void dom_spawnteam (string teamname, float teamcolor, string pointmodel, float pointskin, string capsound, string capnarration, string capmessage)
599 {
600         local entity oldself;
601         oldself = self;
602         self = spawn();
603         self.classname = "dom_team";
604         self.netname = teamname;
605         self.cnt = teamcolor;
606         self.model = pointmodel;
607         self.skin = pointskin;
608         self.noise = capsound;
609         self.noise1 = capnarration;
610         self.message = capmessage;
611
612         // this code is identical to spawnfunc_dom_team
613         setmodel(self, self.model); // precision not needed
614         self.mdl = self.model;
615         self.dmg = self.modelindex;
616         self.model = "";
617         self.modelindex = 0;
618         // this would have to be changed if used in quakeworld
619         self.team = self.cnt + 1;
620
621         //eprint(self);
622         self = oldself;
623 };
624
625 void dom_spawnpoint(vector org)
626 {
627         local entity oldself;
628         oldself = self;
629         self = spawn();
630         self.classname = "dom_controlpoint";
631         self.think = spawnfunc_dom_controlpoint;
632         self.nextthink = time;
633         setorigin(self, org);
634         spawnfunc_dom_controlpoint();
635         self = oldself;
636 };
637
638 // spawn some default teams if the map is not set up for domination
639 void dom_spawnteams()
640 {
641         float numteams;
642         if(autocvar_g_domination_teams_override < 2)
643                 numteams = autocvar_g_domination_default_teams;
644         else
645                 numteams = autocvar_g_domination_teams_override;
646         // LordHavoc: edit this if you want to change defaults
647         dom_spawnteam("Red", COLOR_TEAM1-1, "models/domination/dom_red.md3", 0, "domination/claim.wav", "", "Red team has captured a control point");
648         dom_spawnteam("Blue", COLOR_TEAM2-1, "models/domination/dom_blue.md3", 0, "domination/claim.wav", "", "Blue team has captured a control point");
649         if(numteams > 2)
650                 dom_spawnteam("Yellow", COLOR_TEAM3-1, "models/domination/dom_yellow.md3", 0, "domination/claim.wav", "", "Yellow team has captured a control point");
651         if(numteams > 3)
652                 dom_spawnteam("Pink", COLOR_TEAM4-1, "models/domination/dom_pink.md3", 0, "domination/claim.wav", "", "Pink team has captured a control point");
653         dom_spawnteam("", 0, "models/domination/dom_unclaimed.md3", 0, "", "", "");
654 };
655
656 void dom_delayedinit()
657 {
658         local entity head;
659
660         // if no teams are found, spawn defaults, if custom teams are set, use them
661         if (find(world, classname, "dom_team") == world || autocvar_g_domination_teams_override >= 2)
662                 dom_spawnteams();
663         // if no control points are found, spawn defaults
664         if (find(world, classname, "dom_controlpoint") == world)
665         {
666                 // TODO in a few months (maybe 2011/08): change this into error() and remove this very poor dom point selection
667                 backtrace("This map contains no dom_controlpoint entities. A very poor dom point placement will be chosen. Please fix the map.");
668
669                 // if no supported map was found, make every deathmatch spawn a point
670                 head = find(world, classname, "info_player_deathmatch");
671                 while (head)
672                 {
673                         dom_spawnpoint(head.origin);
674                         head = find(head, classname, "info_player_deathmatch");
675                 }
676         }
677
678         ScoreRules_dom();
679 };
680
681 void dom_init()
682 {
683         // we have to precache default models/sounds even if they might not be
684         // used because spawnfunc_worldspawn is executed before any other entities are read,
685         // so we don't even know yet if this map is set up for domination...
686         precache_model("models/domination/dom_red.md3");
687         precache_model("models/domination/dom_blue.md3");
688         precache_model("models/domination/dom_yellow.md3");
689         precache_model("models/domination/dom_pink.md3");
690         precache_model("models/domination/dom_unclaimed.md3");
691         precache_sound("domination/claim.wav");
692         InitializeEntity(world, dom_delayedinit, INITPRIO_GAMETYPE);
693
694         addstat(STAT_DOM_TOTAL_PPS, AS_FLOAT, dom_total_pps);
695         addstat(STAT_DOM_PPS_RED, AS_FLOAT, dom_pps_red);
696         addstat(STAT_DOM_PPS_BLUE, AS_FLOAT, dom_pps_blue);
697         if(c3 >= 0) addstat(STAT_DOM_PPS_YELLOW, AS_FLOAT, dom_pps_yellow);
698         if(c4 >= 0) addstat(STAT_DOM_PPS_PINK, AS_FLOAT, dom_pps_pink);
699 };
700