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