]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/ctf.qc
now REALLY remove dead players
[divverent/nexuiz.git] / data / qcsrc / server / ctf.qc
1 .entity sprite;
2 entity ctf_worldflaglist; // CTF flags in the map
3 .entity ctf_worldflagnext;
4
5 .float next_take_time;                  // the next time a player can pick up a flag (time + blah)
6                                                                 /// I used this, in part, to fix the looping score bug. - avirox
7 //float FLAGSCORE_PICKUP        =  1;
8 //float FLAGSCORE_RETURN        =  5; // returned by owner team
9 //float FLAGSCORE_RETURNROGUE   = 10; // returned by rogue team
10 //float FLAGSCORE_CAPTURE       =  5;
11
12 #define FLAG_CARRY_POS '-15 0 7'
13
14 void FakeTimeLimit(entity e, float t)
15 {
16         msg_entity = e;
17         WriteByte(MSG_ONE, 3); // svc_updatestat
18         WriteByte(MSG_ONE, 236); // STAT_TIMELIMIT
19         if(t < 0)
20                 WriteCoord(MSG_ONE, cvar("timelimit"));
21         else
22                 WriteCoord(MSG_ONE, (t + 1) / 60);
23 }
24
25 float   flagcaptimerecord;
26 .float  flagpickuptime;
27 //.float  iscommander;
28 //.float  ctf_state;
29
30 void() FlagThink;
31 void() FlagTouch;
32
33 void place_flag()
34 {
35         if(!self.t_width)
36                 self.t_width = 0.1; // frame animation rate
37         if(!self.t_length)
38                 self.t_length = 119; // maximum frame
39
40         setattachment(self, world, "");
41         self.mdl = self.model;
42         self.flags = FL_ITEM;
43         self.solid = SOLID_TRIGGER;
44         self.movetype = MOVETYPE_NONE;
45         self.velocity = '0 0 0';
46         self.origin_z = self.origin_z + 6;
47         self.think = FlagThink;
48         self.touch = FlagTouch;
49         self.nextthink = time + 0.1;
50         self.cnt = FLAG_BASE;
51         self.mangle = self.angles;
52         self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP;
53         //self.effects = self.effects | EF_DIMLIGHT;
54         if(!self.noalign)
55         {
56                 self.movetype = MOVETYPE_TOSS;
57                 if (!droptofloor())
58                 {
59                         dprint("Flag fell out of level at ", vtos(self.origin), "\n");
60                         remove(self);
61                         return;
62                 }
63         }
64         self.oldorigin = self.origin;
65 };
66
67 void LogCTF(string mode, float flagteam, entity actor)
68 {
69         string s;
70         if(!cvar("sv_eventlog"))
71                 return;
72         s = strcat(":ctf:", mode);
73         s = strcat(s, ":", ftos(flagteam));
74         if(actor != world)
75                 s = strcat(s, ":", ftos(actor.playerid));
76         GameLogEcho(s);
77 }
78
79 void RegenFlag(entity e)
80 {
81         setattachment(e, world, "");
82         e.movetype = MOVETYPE_NONE;
83         if(!self.noalign)
84                 e.movetype = MOVETYPE_TOSS;
85         e.solid = SOLID_TRIGGER;
86         // TODO: play a sound here
87         setorigin(e, e.oldorigin);
88         e.angles = e.mangle;
89         e.cnt = FLAG_BASE;
90         e.owner = world;
91         e.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
92 };
93
94 void ReturnFlag(entity e)
95 {
96         if (e.owner)
97         if (e.owner.flagcarried == e)
98         {
99                 WaypointSprite_DetachCarrier(e.owner);
100                 e.owner.flagcarried = world;
101
102                 if(e.speedrunning)
103                         FakeTimeLimit(e.owner, -1);
104         }
105         e.owner = world;
106         RegenFlag(e);
107 };
108
109 void DropFlag(entity e, entity penalty_receiver, entity attacker)
110 {
111         local entity p;
112
113         if(e.speedrunning)
114         {
115                 ReturnFlag(e);
116                 return;
117         }
118
119         if (!e.owner)
120         {
121                 dprint("FLAG: drop - no owner?!?!\n");
122                 return;
123         }
124         p = e.owner;
125         if (p.flagcarried != e)
126         {
127                 dprint("FLAG: drop - owner is not carrying this flag??\n");
128                 return;
129         }
130         bprint(p.netname, "^7 lost the ", e.netname, "\n");
131
132         if(penalty_receiver)
133                 UpdateFrags(penalty_receiver, -cvar("g_ctf_flagpenalty_suicidedrop"));
134         else
135                 UpdateFrags(p, -cvar("g_ctf_flagpenalty_drop"));
136         PlayerScore_Add(p, SP_CTF_DROPS, +1);
137         e.playerid = attacker.playerid;
138         
139         WaypointSprite_Ping(p.waypointsprite_attachedforcarrier);
140         WaypointSprite_DetachCarrier(p);
141         LogCTF("dropped", p.team, p);
142
143         setattachment(e, world, "");
144
145         if (p.flagcarried == e)
146                 p.flagcarried = world;
147         e.owner = world;
148
149         e.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
150         e.solid = SOLID_TRIGGER;
151         e.movetype = MOVETYPE_TOSS;
152         // setsize(e, '-16 -16 0', '16 16 74');
153         setorigin(e, p.origin - '0 0 24' + '0 0 37');
154         e.cnt = FLAG_DROPPED;
155         e.velocity = '0 0 300';
156         e.pain_finished = time + cvar("g_ctf_flag_returntime");//30;
157
158         trace_startsolid = FALSE;
159         tracebox(e.origin, e.mins, e.maxs, e.origin, TRUE, e);
160         if(trace_startsolid)
161                 dprint("FLAG FALLTHROUGH will happen SOON\n");
162 };
163
164 void AnimateFlag()
165 {
166         if(self.delay > time)
167                 return;
168         self.delay = time + self.t_width;
169         if(self.nextthink > self.delay)
170                 self.nextthink = self.delay;
171
172         self.frame = self.frame + 1;
173         if(self.frame > self.t_length)
174                 self.frame = 0;
175 }
176
177 void FlagThink()
178 {
179         local entity e;
180
181         self.nextthink = time + 0.1;
182
183         AnimateFlag();
184
185         if(self.speedrunning)
186         if(self.cnt == FLAG_CARRY)
187         {
188                 if(self.owner)
189                 if(flagcaptimerecord)
190                 if(time >= self.flagpickuptime + flagcaptimerecord)
191                 {
192                         bprint("The ", self.netname, " became impatient after ", ftos_decimals(flagcaptimerecord, 2), " seconds and returned itself\n");
193
194                         self.owner.impulse = 77; // returning!
195                         e = self;
196                         self = self.owner;
197                         ReturnFlag(e);
198                         ImpulseCommands();
199                         self = e;
200                         return;
201                 }
202         }
203
204         if (self.cnt == FLAG_BASE)
205                 return;
206
207         if (self.cnt == FLAG_DROPPED)
208         {
209                 // flag fallthrough? FIXME remove this if bug is really fixed now
210                 if(self.origin_z < -131072)
211                 {
212                         dprint("FLAG FALLTHROUGH just happened\n");
213                         self.pain_finished = 0;
214                 }
215                 setattachment(self, world, "");
216                 if (time > self.pain_finished)
217                 {
218                         bprint("The ", self.netname, " has returned to base\n");
219                         sound (self, CHAN_TRIGGER, self.noise3, VOL_BASE, ATTN_NONE);
220                         LogCTF("returned", self.team, world);
221                         ReturnFlag(self);
222                 }
223                 return;
224         }
225
226         e = self.owner;
227         if (e.classname != "player" || (e.deadflag) || (e.flagcarried != self))
228         {
229                 dprint("CANNOT HAPPEN - player dead and STILL had a flag!\n");
230                 DropFlag(self, world, world);
231                 return;
232         }
233 };
234
235 void FlagTouch()
236 {
237         if(gameover) return;
238
239         local float t;
240         local entity player;
241         local string s, s0, h0, h1;
242         if (other.classname != "player")
243                 return;
244         if (other.health < 1) // ignore dead players
245                 return;
246
247         if (self.cnt == FLAG_CARRY)
248                 return;
249
250         if (self.cnt == FLAG_BASE)
251         if (other.team == self.team)
252         if (other.flagcarried) // he's got a flag
253         if (other.flagcarried.team != self.team) // capture
254         {
255                 if (other.flagcarried == world)
256                 {
257                         return;
258                 }
259                 t = time - other.flagcarried.flagpickuptime;
260                 s = ftos_decimals(t, 2);
261                 s0 = ftos_decimals(flagcaptimerecord, 2);
262                 h0 = db_get(ServerProgsDB, strcat(GetMapname(), "/captimerecord/netname"));
263                 h1 = other.netname;
264                 if(h0 == h1)
265                         h0 = "his";
266                 else
267                         h0 = strcat(h0, "^7's"); // h0: display text for previous netname
268                 if (flagcaptimerecord == 0)
269                 {
270                         bprint(other.netname, "^7 captured the ", other.flagcarried.netname, " in ", s, " seconds\n");
271                         flagcaptimerecord = t;
272                         db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time"), ftos(t));
273                         db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/netname"), h1);
274                         GameLogEcho(strcat(":recordset:", ftos(other.playerid), ":", ftos(t)));
275                 }
276                 else if (t < flagcaptimerecord)
277                 {
278                         bprint(other.netname, "^7 captured the ", other.flagcarried.netname, " in ", s, ", breaking ", strcat(h0, " previous record of ", s0, " seconds\n"));
279                         flagcaptimerecord = t;
280                         db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time"), ftos(t));
281                         db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/netname"), h1);
282                         GameLogEcho(strcat(":recordset:", ftos(other.playerid), ":", ftos(t)));
283                 }
284                 else
285                 {
286                         bprint(other.netname, "^7 captured the ", other.flagcarried.netname, " in ", s, ", failing to break ", strcat(h0, " record of ", s0, " seconds\n"));
287                 }
288
289                 PlayerTeamScore_Add(other, SP_CTF_CAPS, ST_CTF_CAPS, 1);
290                 LogCTF("capture", other.flagcarried.team, other);
291                 // give credit to the individual player
292                 UpdateFrags(other, cvar("g_ctf_flagscore_capture"));
293
294                 sound (other, CHAN_AUTO, self.noise2, VOL_BASE, ATTN_NONE);
295                 WaypointSprite_DetachCarrier(other);
296                 if(self.speedrunning)
297                         FakeTimeLimit(other, -1);
298                 RegenFlag (other.flagcarried);
299                 other.flagcarried = world;
300                 other.next_take_time = time + 1;
301         }
302         if (self.cnt == FLAG_BASE)
303         if (other.team == COLOR_TEAM1 || other.team == COLOR_TEAM2) // only red and blue team can steal flags
304         if (other.team != self.team)
305         if (!other.flagcarried)
306         {
307                 if (other.next_take_time > time)
308                         return;
309                 // pick up
310                 self.flagpickuptime = time; // used for timing runs
311                 self.speedrunning = other.speedrunning; // if speedrunning, flag will self-return and teleport the owner back after the record
312                 if(other.speedrunning)
313                 if(flagcaptimerecord)
314                         FakeTimeLimit(other, time + flagcaptimerecord);
315                 self.solid = SOLID_NOT;
316                 setorigin(self, self.origin); // relink
317                 self.owner = other;
318                 other.flagcarried = self;
319                 self.cnt = FLAG_CARRY;
320                 self.angles = '0 0 0';
321                 bprint(other.netname, "^7 got the ", self.netname, "\n");
322                 UpdateFrags(other, cvar("g_ctf_flagscore_pickup_base"));
323                 PlayerScore_Add(other, SP_CTF_PICKUPS, 1);
324                 LogCTF("steal", self.team, other);
325                 sound (other, CHAN_AUTO, self.noise, VOL_BASE, ATTN_NONE);
326
327                 FOR_EACH_PLAYER(player)
328                         if(player.team == self.team)
329                                 centerprint(player, "The enemy got your flag! Retrieve it!");
330
331                 self.movetype = MOVETYPE_NONE;
332                 setorigin(self, FLAG_CARRY_POS);
333                 setattachment(self, other, "");
334                 WaypointSprite_AttachCarrier("flagcarrier", other);
335                 WaypointSprite_UpdateTeamRadar(other.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, '1 1 0');
336                 WaypointSprite_Ping(self.sprite);
337
338                 return;
339         }
340
341         if (self.cnt == FLAG_DROPPED)
342         {
343                 self.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
344                 if (other.team == self.team || (other.team != COLOR_TEAM1 && other.team != COLOR_TEAM2))
345                 {
346                         // return flag
347                         bprint(other.netname, "^7 returned the ", self.netname, "\n");
348
349                         // punish the player who last had it
350                         FOR_EACH_PLAYER(player)
351                                 if(player.playerid == self.playerid)
352                                         PlayerScore_Add(player, SP_SCORE, -cvar("g_ctf_flagpenalty_returned"));
353
354                         // punish the team who was last carrying it
355                         if(self.team == COLOR_TEAM1)
356                                 TeamScore_AddToTeam(COLOR_TEAM2, ST_SCORE, -cvar("g_ctf_flagpenalty_returned"));
357                         else
358                                 TeamScore_AddToTeam(COLOR_TEAM1, ST_SCORE, -cvar("g_ctf_flagpenalty_returned"));
359
360                         // reward the player who returned it
361                         if(other.playerid == self.playerid) // is this the guy who killed the FC last?
362                         {
363                                 if (other.team == COLOR_TEAM1 || other.team == COLOR_TEAM2)
364                                         UpdateFrags(other, cvar("g_ctf_flagscore_return_by_killer"));
365                                 else
366                                         UpdateFrags(other, cvar("g_ctf_flagscore_return_rogue_by_killer"));
367                         }
368                         else
369                         {
370                                 if (other.team == COLOR_TEAM1 || other.team == COLOR_TEAM2)
371                                         UpdateFrags(other, cvar("g_ctf_flagscore_return"));
372                                 else
373                                         UpdateFrags(other, cvar("g_ctf_flagscore_return_rogue"));
374                         }
375                         PlayerScore_Add(other, SP_CTF_RETURNS, 1);
376                         LogCTF("return", self.team, other);
377                         sound (other, CHAN_AUTO, self.noise1, VOL_BASE, ATTN_NONE);
378                         ReturnFlag(self);
379                 }
380                 else if (!other.flagcarried)
381                 {
382                         // pick up
383                         self.solid = SOLID_NOT;
384                         setorigin(self, self.origin); // relink
385                         self.owner = other;
386                         other.flagcarried = self;
387                         self.cnt = FLAG_CARRY;
388                         bprint(other.netname, "^7 picked up the ", self.netname, "\n");
389
390                         float f;
391                         f = bound(0, (self.pain_finished - time) / cvar("g_ctf_flag_returntime"), 1);
392                         //print("factor is ", ftos(f), "\n");
393                         f = cvar("g_ctf_flagscore_pickup_dropped_late") * (1-f)
394                           + cvar("g_ctf_flagscore_pickup_dropped_early") * f;
395                         f = floor(f + 0.5);
396                         //print("score is ", ftos(f), "\n");
397
398                         UpdateFrags(other, f);
399                         PlayerScore_Add(other, SP_CTF_PICKUPS, 1);
400                         LogCTF("pickup", self.team, other);
401                         sound (other, CHAN_AUTO, self.noise, VOL_BASE, ATTN_NONE);
402
403                         FOR_EACH_PLAYER(player)
404                                 if(player.team == self.team)
405                                         centerprint(player, "The enemy got your flag! Retrieve it!");
406
407                         self.movetype = MOVETYPE_NONE;  // flag must have MOVETYPE_NONE here, otherwise it will drop through the floor...
408                         setorigin(self, FLAG_CARRY_POS);
409                         setattachment(self, other, "");
410                         WaypointSprite_AttachCarrier("flagcarrier", other);
411                         WaypointSprite_UpdateTeamRadar(other.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, '1 1 0');
412                 }
413         }
414 };
415
416 /*QUAKED spawnfunc_info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
417 CTF Starting point for a player
418 in team one (Red).
419
420 Keys:
421 "angle"
422  viewing angle when spawning
423 */
424 void spawnfunc_info_player_team1()
425 {
426         self.team = COLOR_TEAM1; // red
427         spawnfunc_info_player_deathmatch();
428 };
429 //self.team = 4;self.classname = "info_player_start";spawnfunc_info_player_start();};
430
431 /*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
432 CTF Starting point for a player in
433 team two (Blue).
434
435 Keys:
436 "angle"
437  viewing angle when spawning
438 */
439 void spawnfunc_info_player_team2()
440 {
441         self.team = COLOR_TEAM2; // blue
442         spawnfunc_info_player_deathmatch();
443 };
444 //self.team = 13;self.classname = "info_player_start";spawnfunc_info_player_start();};
445
446 /*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
447 CTF Starting point for a player in
448 team three (Magenta).
449
450 Keys:
451 "angle"
452  viewing angle when spawning
453 */
454 void spawnfunc_info_player_team3()
455 {
456         self.team = COLOR_TEAM3; // purple
457         spawnfunc_info_player_deathmatch();
458 };
459
460
461 /*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
462 CTF Starting point for a player in
463 team four (Yellow).
464
465 Keys:
466 "angle"
467  viewing angle when spawning
468 */
469 void spawnfunc_info_player_team4()
470 {
471         self.team = COLOR_TEAM4; // yellow
472         spawnfunc_info_player_deathmatch();
473 };
474
475
476
477
478 /*QUAKED spawnfunc_item_flag_team1 (0 0.5 0.8) (-48 -48 -37) (48 48 37)
479 CTF flag for team one (Red).
480 Multiple are allowed.
481
482 Keys:
483 "angle"
484  Angle the flag will point
485 (minus 90 degrees)
486 "model"
487  model to use, note this needs red and blue as skins 0 and 1
488  (default models/ctf/flag.md3)
489 "noise"
490  sound played when flag is picked up
491  (default ctf/take.wav)
492 "noise1"
493  sound played when flag is returned by a teammate
494  (default ctf/return.wav)
495 "noise2"
496  sound played when flag is captured
497  (default ctf/redcapture.wav)
498 "noise3"
499  sound played when flag is lost in the field and respawns itself
500  (default ctf/respawn.wav)
501 */
502
503 void spawnfunc_item_flag_team1()
504 {
505         if (!g_ctf)
506         {
507                 remove(self);
508                 return;
509         }
510
511         //if(!cvar("teamplay"))
512         //      cvar_set("teamplay", "3");
513
514         // link flag into ctf_worldflaglist
515         self.ctf_worldflagnext = ctf_worldflaglist;
516         ctf_worldflaglist = self;
517
518         self.classname = "item_flag_team";
519         self.team = COLOR_TEAM1; // color 4 team (red)
520         self.items = IT_KEY2; // gold key (redish enough)
521         self.netname = "^1RED^7 flag";
522         self.target = "###item###";
523         self.skin = 0;
524         if(self.spawnflags & 1)
525                 self.noalign = 1;
526         if (!self.model)
527                 self.model = "models/ctf/flag_red.md3";
528         if (!self.noise)
529                 self.noise = "ctf/take.wav";
530         if (!self.noise1)
531                 self.noise1 = "ctf/return.wav";
532         if (!self.noise2)
533                 self.noise2 = "ctf/redcapture.wav"; // blue team scores by capturing the red flag
534         if (!self.noise3)
535                 self.noise3 = "ctf/respawn.wav";
536         precache_model (self.model);
537         setmodel (self, self.model); // precision set below
538         precache_sound (self.noise);
539         precache_sound (self.noise1);
540         precache_sound (self.noise2);
541         precache_sound (self.noise3);
542         setsize(self, '-16 -16 -37', '16 16 37');
543         setorigin(self, self.origin + '0 0 37');
544         self.nextthink = time + 0.2; // start after doors etc
545         self.think = place_flag;
546
547         if(!self.scale)
548                 self.scale = 0.6;
549         //if(!self.glow_size)
550         //      self.glow_size = 50;
551
552         self.effects = self.effects | EF_FULLBRIGHT | EF_LOWPRECISION;
553         if(!self.noalign)
554                 droptofloor();
555
556         waypoint_spawnforitem(self);
557
558         WaypointSprite_SpawnFixed("redbase", self.origin + '0 0 37', self, sprite);
559         WaypointSprite_UpdateTeamRadar(self.sprite, RADARICON_FLAG, '1 0 0');
560 };
561
562 /*QUAKED spawnfunc_item_flag_team2 (0 0.5 0.8) (-48 -48 -24) (48 48 64)
563 CTF flag for team two (Blue).
564 Multiple are allowed.
565
566 Keys:
567 "angle"
568  Angle the flag will point
569 (minus 90 degrees)
570 "model"
571  model to use, note this needs red and blue as skins 0 and 1
572  (default models/ctf/flag.md3)
573 "noise"
574  sound played when flag is picked up
575  (default ctf/take.wav)
576 "noise1"
577  sound played when flag is returned by a teammate
578  (default ctf/return.wav)
579 "noise2"
580  sound played when flag is captured
581  (default ctf/bluecapture.wav)
582 "noise3"
583  sound played when flag is lost in the field and respawns itself
584  (default ctf/respawn.wav)
585 */
586
587 void spawnfunc_item_flag_team2()
588 {
589         if (!g_ctf)
590         {
591                 remove(self);
592                 return;
593         }
594         //if(!cvar("teamplay"))
595         //      cvar_set("teamplay", "3");
596
597         // link flag into ctf_worldflaglist
598         self.ctf_worldflagnext = ctf_worldflaglist;
599         ctf_worldflaglist = self;
600
601         self.classname = "item_flag_team";
602         self.team = COLOR_TEAM2; // color 13 team (blue)
603         self.items = IT_KEY1; // silver key (bluish enough)
604         self.netname = "^4BLUE^7 flag";
605         self.target = "###item###";
606         self.skin = 0;
607         if(self.spawnflags & 1)
608                 self.noalign = 1;
609         if (!self.model)
610                 self.model = "models/ctf/flag_blue.md3";
611         if (!self.noise)
612                 self.noise = "ctf/take.wav";
613         if (!self.noise1)
614                 self.noise1 = "ctf/return.wav";
615         if (!self.noise2)
616                 self.noise2 = "ctf/bluecapture.wav"; // red team scores by capturing the blue flag
617         if (!self.noise3)
618                 self.noise3 = "ctf/respawn.wav";
619         precache_model (self.model);
620         setmodel (self, self.model); // precision set below
621         precache_sound (self.noise);
622         precache_sound (self.noise1);
623         precache_sound (self.noise2);
624         precache_sound (self.noise3);
625         setsize(self, '-16 -16 -37', '16 16 37');
626         setorigin(self, self.origin + '0 0 37');
627         self.nextthink = time + 0.2; // start after doors etc
628         self.think = place_flag;
629
630         if(!self.scale)
631                 self.scale = 0.6;
632         //if(!self.glow_size)
633         //      self.glow_size = 50;
634
635         self.effects = self.effects | EF_FULLBRIGHT | EF_LOWPRECISION;
636         if(!self.noalign)
637                 droptofloor();
638
639         waypoint_spawnforitem(self);
640
641         WaypointSprite_SpawnFixed("bluebase", self.origin + '0 0 37', self, sprite);
642         WaypointSprite_UpdateTeamRadar(self.sprite, RADARICON_FLAG, '0 0 1');
643 };
644
645
646 /*QUAKED spawnfunc_ctf_team (0 .5 .8) (-16 -16 -24) (16 16 32)
647 Team declaration for CTF gameplay, this allows you to decide what team
648 names and control point models are used in your map.
649
650 Note: If you use spawnfunc_ctf_team entities you must define at least 2!  However, unlike
651 domination, you don't need to make a blank one too.
652
653 Keys:
654 "netname"
655  Name of the team (for example Red, Blue, Green, Yellow, Life, Death, Offense, Defense, etc)
656 "cnt"
657  Scoreboard color of the team (for example 4 is red and 13 is blue)
658
659 */
660
661 void spawnfunc_ctf_team()
662 {
663         if (!g_ctf)
664         {
665                 remove(self);
666                 return;
667         }
668         self.classname = "ctf_team";
669         self.team = self.cnt + 1;
670 };
671
672 // code from here on is just to support maps that don't have control point and team entities
673 void ctf_spawnteam (string teamname, float teamcolor)
674 {
675         local entity oldself;
676         oldself = self;
677         self = spawn();
678         self.classname = "ctf_team";
679         self.netname = teamname;
680         self.cnt = teamcolor;
681
682         spawnfunc_ctf_team();
683
684         self = oldself;
685 };
686
687 // spawn some default teams if the map is not set up for ctf
688 void ctf_spawnteams()
689 {
690         float numteams;
691
692         numteams = 2;//cvar("g_ctf_default_teams");
693
694         ctf_spawnteam("Red", COLOR_TEAM1 - 1);
695         ctf_spawnteam("Blue", COLOR_TEAM2 - 1);
696 };
697
698 void ctf_delayedinit()
699 {
700         // if no teams are found, spawn defaults
701         if (find(world, classname, "ctf_team") == world)
702                 ctf_spawnteams();
703
704         ScoreRules_ctf();
705 };
706
707 void ctf_init()
708 {
709         InitializeEntity(world, ctf_delayedinit, INITPRIO_GAMETYPE);
710         flagcaptimerecord = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time")));
711 };
712
713 void ctf_setstatus2(entity flag, float shift)
714 {
715         if (flag.cnt == FLAG_CARRY)
716                 if (flag.owner == self)
717                         self.items |= shift * 3;
718                 else
719                         self.items |= shift * 1;
720         else if (flag.cnt == FLAG_DROPPED)
721                 self.items |= shift * 2;
722         else
723         {
724                 // no status bits
725         }
726 };
727
728 void ctf_setstatus()
729 {
730         self.items = self.items - (self.items & IT_RED_FLAG_TAKEN);
731         self.items = self.items - (self.items & IT_RED_FLAG_LOST);
732         self.items = self.items - (self.items & IT_BLUE_FLAG_TAKEN);
733         self.items = self.items - (self.items & IT_BLUE_FLAG_LOST);
734
735         if (g_ctf) {
736                 local entity flag;
737                 float redflags, blueflags;
738
739                 redflags = 0;
740                 blueflags = 0;
741
742                 for (flag = ctf_worldflaglist; flag; flag = flag.ctf_worldflagnext) if(flag.cnt != FLAG_BASE)
743                 {
744                         if(flag.team == COLOR_TEAM1)
745                                 ++redflags;
746                         else if(flag.team == COLOR_TEAM2)
747                                 ++blueflags;
748                 }
749
750                 // blinking magic: if there is more than one flag, show one of these in a clever way
751                 if(redflags)
752                         redflags = mod(floor(time * redflags * 0.75), redflags);
753                 if(blueflags)
754                         blueflags = mod(floor(time * blueflags * 0.75), blueflags);
755
756                 for (flag = ctf_worldflaglist; flag; flag = flag.ctf_worldflagnext) if(flag.cnt != FLAG_BASE)
757                 {
758                         if(flag.team == COLOR_TEAM1)
759                         {
760                                 if(--redflags == -1) // happens exactly once (redflags is in 0..count-1, and will --'ed count times)
761                                         ctf_setstatus2(flag, IT_RED_FLAG_TAKEN);
762                         }
763                         else if(flag.team == COLOR_TEAM2)
764                         {
765                                 if(--blueflags == -1) // happens exactly once
766                                         ctf_setstatus2(flag, IT_BLUE_FLAG_TAKEN);
767                         }
768                 }
769         }
770 };
771 /*
772 entity(float cteam) ctf_team_has_commander =
773 {
774         entity pl;
775         if(cteam != COLOR_TEAM1 || cteam != COLOR_TEAM2)
776                 return world;
777         
778         FOR_EACH_REALPLAYER(pl) {
779                 if(pl.team == cteam && pl.iscommander) {
780                         return pl;
781                 }
782         }
783         return world;
784 };
785
786 void(entity e, float st) ctf_setstate =
787 {
788         e.ctf_state = st;
789         ++e.version;
790 };
791
792 void(float cteam) ctf_new_commander =
793 {
794         entity pl, plmax;
795         
796         plmax = world;
797         FOR_EACH_REALPLAYER(pl) {
798                 if(pl.team == cteam) {
799                         if(pl.iscommander) { // don't reassign if alreay there
800                                 return;
801                         }
802                         if(plmax == world || plmax.frags < pl.frags) <<<<<<<<<<<<<<<<< BROKEN in new scoring system
803                                 plmax = pl;
804                 }
805         }
806         if(plmax == world) {
807                 bprint(strcat(ColoredTeamName(cteam), " Team has no Commander!\n"));
808                 return;
809         }
810
811         plmax.iscommander = TRUE;
812         ctf_setstate(plmax, 3);
813         sprint(plmax, "^3You're the commander now!\n");
814         centerprint(plmax, "^3You're the commander now!\n");
815 };
816
817 void() ctf_clientconnect =
818 {
819         self.iscommander = FALSE;
820         
821         if(!self.team || self.classname != "player") {
822                 ctf_setstate(self, -1);
823         } else
824                 ctf_setstate(self, 0);
825
826         self.team_saved = self.team;
827         
828         if(self.team != 0 && self.classname == "player" && !ctf_team_has_commander(self.team)) {
829                 ctf_new_commander(self.team);
830         }
831 };
832
833 void() ctf_playerchanged =
834 {
835         if(!self.team || self.classname != "player") {
836                 ctf_setstate(self, -1);
837         } else if(self.ctf_state < 0 && self.classname == "player") {
838                 ctf_setstate(self, 0);
839         }
840
841         if(self.iscommander &&
842            (self.classname != "player" || self.team != self.team_saved)
843                 )
844         {
845                 self.iscommander = FALSE;
846                 if(self.classname == "player")
847                         ctf_setstate(self, 0);
848                 else
849                         ctf_setstate(self, -1);
850                 ctf_new_commander(self.team_saved);
851         }
852         
853         self.team_saved = self.team;
854         
855         ctf_new_commander(self.team);
856 };
857
858 void() ctf_clientdisconnect =
859 {
860         if(self.iscommander)
861         {
862                 ctf_new_commander(self.team);
863         }
864 };
865
866 entity GetPlayer(string);
867 float() ctf_clientcommand =
868 {
869         entity e;
870         if(argv(0) == "order") {
871                 if(!g_ctf) {
872                         sprint(self, "This command is not supported in this gamemode.\n");
873                         return TRUE;
874                 }
875                 if(!self.iscommander) {
876                         sprint(self, "^1You are not the commander!\n");
877                         return TRUE;
878                 }
879                 if(argv(2) == "") {
880                         sprint(self, "Usage: order #player status   - (playernumber as in status)\n");
881                         return TRUE;
882                 }
883                 e = GetPlayer(argv(1));
884                 if(e == world) {
885                         sprint(self, "Invalid player.\nUsage: order #player status   - (playernumber as in status)\n");
886                         return TRUE;
887                 }
888                 if(e.team != self.team) {
889                         sprint(self, "^3You can only give orders to your own team!\n");
890                         return TRUE;
891                 }
892                 if(argv(2) == "attack") {
893                         sprint(self, strcat("Ordering ", e.netname, " to attack!\n"));
894                         sprint(e, "^1Attack!\n");
895                         centerprint(e, "^7You've been ordered to^9\n^1Attack!\n");
896                         ctf_setstate(e, 1);
897                 } else if(argv(2) == "defend") {
898                         sprint(self, strcat("Ordering ", e.netname, " to defend!\n"));
899                         sprint(e, "^Defend!\n");
900                         centerprint(e, "^7You've been ordered to^9\n^2Defend!\n");
901                         ctf_setstate(e, 2);
902                 } else {
903                         sprint(self, "^7Invalid command, use ^3attack^7, or ^3defend^7.\n");
904                 }
905                 return TRUE;
906         }
907         return FALSE;
908 };
909 */