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