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