]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/ctf.qc
add :recordset:<ID>:<time> event
[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);
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 (self, CHAN_TRIGGER, self.noise3, VOL_BASE, 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                         GameLogEcho(strcat(":recordset:", ftos(other.playerid), ":", ftos(t)));
274                 }
275                 else if (t < flagcaptimerecord)
276                 {
277                         bprint(other.netname, "^7 captured the ", other.flagcarried.netname, " in ", s, ", breaking ", strcat(h0, " previous record of ", s0, " seconds\n"));
278                         flagcaptimerecord = t;
279                         db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time"), ftos(t));
280                         db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/netname"), h1);
281                         GameLogEcho(strcat(":recordset:", ftos(other.playerid), ":", ftos(t)));
282                 }
283                 else
284                 {
285                         bprint(other.netname, "^7 captured the ", other.flagcarried.netname, " in ", s, ", failing to break ", strcat(h0, " record of ", s0, " seconds\n"));
286                 }
287
288                 PlayerTeamScore_Add(other, SP_CTF_CAPS, ST_CTF_CAPS, 1);
289                 LogCTF("capture", other.flagcarried.team, other);
290                 // give credit to the individual player
291                 UpdateFrags(other, cvar("g_ctf_flagscore_capture"));
292
293                 // give credit to all players of the team (rewards large teams)
294                 // NOTE: this defaults to 0
295                 FOR_EACH_PLAYER(head)
296                         if (head.team == self.team)
297                                 UpdateFrags(head, cvar("g_ctf_flagscore_capture_team"));
298
299                 sound (other, CHAN_AUTO, self.noise2, VOL_BASE, ATTN_NONE);
300                 WaypointSprite_DetachCarrier(other);
301                 if(self.speedrunning)
302                         FakeTimeLimit(other, -1);
303                 RegenFlag (other.flagcarried);
304                 other.flagcarried = world;
305                 other.next_take_time = time + 1;
306         }
307         if (self.cnt == FLAG_BASE)
308         if (other.team == COLOR_TEAM1 || other.team == COLOR_TEAM2) // only red and blue team can steal flags
309         if (other.team != self.team)
310         if (!other.flagcarried)
311         {
312                 if (other.next_take_time > time)
313                         return;
314                 // pick up
315                 self.flagpickuptime = time; // used for timing runs
316                 self.speedrunning = other.speedrunning; // if speedrunning, flag will self-return and teleport the owner back after the record
317                 if(other.speedrunning)
318                 if(flagcaptimerecord)
319                         FakeTimeLimit(other, time + flagcaptimerecord);
320                 self.solid = SOLID_NOT;
321                 setorigin(self, self.origin); // relink
322                 self.owner = other;
323                 other.flagcarried = self;
324                 self.cnt = FLAG_CARRY;
325                 self.angles = '0 0 0';
326                 bprint(other.netname, "^7 got the ", self.netname, "\n");
327                 UpdateFrags(other, cvar("g_ctf_flagscore_pickup"));
328                 PlayerScore_Add(other, SP_CTF_PICKUPS, 1);
329                 LogCTF("steal", self.team, other);
330                 sound (other, CHAN_AUTO, self.noise, VOL_BASE, ATTN_NONE);
331
332                 FOR_EACH_PLAYER(player)
333                         if(player.team == self.team)
334                                 centerprint(player, "The enemy got your flag! Retrieve it!");
335
336                 self.movetype = MOVETYPE_NONE;
337                 setorigin(self, FLAG_CARRY_POS);
338                 setattachment(self, other, "");
339                 WaypointSprite_AttachCarrier("flagcarrier", other);
340
341                 return;
342         }
343
344         if (self.cnt == FLAG_DROPPED)
345         {
346                 self.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
347                 if (other.team == self.team || (other.team != COLOR_TEAM1 && other.team != COLOR_TEAM2))
348                 {
349                         // return flag
350                         bprint(other.netname, "^7 returned the ", self.netname, "\n");
351                         if (other.team == COLOR_TEAM1 || other.team == COLOR_TEAM2)
352                                 UpdateFrags(other, cvar("g_ctf_flagscore_return"));
353                         else
354                                 UpdateFrags(other, cvar("g_ctf_flagscore_return_rogue"));
355                         PlayerScore_Add(other, SP_CTF_RETURNS, 1);
356                         LogCTF("return", self.team, other);
357                         sound (other, CHAN_AUTO, self.noise1, VOL_BASE, ATTN_NONE);
358                         ReturnFlag(self);
359                 }
360                 else if (!other.flagcarried)
361                 {
362                         // pick up
363                         self.solid = SOLID_NOT;
364                         setorigin(self, self.origin); // relink
365                         self.owner = other;
366                         other.flagcarried = self;
367                         self.cnt = FLAG_CARRY;
368                         bprint(other.netname, "^7 picked up the ", self.netname, "\n");
369                         UpdateFrags(other, cvar("g_ctf_flagscore_pickup"));
370                         LogCTF("pickup", self.team, other);
371                         sound (other, CHAN_AUTO, self.noise, VOL_BASE, ATTN_NONE);
372
373                         FOR_EACH_PLAYER(player)
374                                 if(player.team == self.team)
375                                         centerprint(player, "The enemy got your flag! Retrieve it!");
376
377                         self.movetype = MOVETYPE_NONE;  // flag must have MOVETYPE_NONE here, otherwise it will drop through the floor...
378                         setorigin(self, FLAG_CARRY_POS);
379                         setattachment(self, other, "");
380                         WaypointSprite_AttachCarrier("flagcarrier", other);
381                 }
382         }
383 };
384
385 /*QUAKED spawnfunc_info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
386 CTF Starting point for a player
387 in team one (Red).
388
389 Keys:
390 "angle"
391  viewing angle when spawning
392 */
393 void spawnfunc_info_player_team1()
394 {
395         self.team = COLOR_TEAM1; // red
396         spawnfunc_info_player_deathmatch();
397 };
398 //self.team = 4;self.classname = "info_player_start";spawnfunc_info_player_start();};
399
400 /*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
401 CTF Starting point for a player in
402 team two (Blue).
403
404 Keys:
405 "angle"
406  viewing angle when spawning
407 */
408 void spawnfunc_info_player_team2()
409 {
410         self.team = COLOR_TEAM2; // blue
411         spawnfunc_info_player_deathmatch();
412 };
413 //self.team = 13;self.classname = "info_player_start";spawnfunc_info_player_start();};
414
415 /*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
416 CTF Starting point for a player in
417 team three (Magenta).
418
419 Keys:
420 "angle"
421  viewing angle when spawning
422 */
423 void spawnfunc_info_player_team3()
424 {
425         self.team = COLOR_TEAM3; // purple
426         spawnfunc_info_player_deathmatch();
427 };
428
429
430 /*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
431 CTF Starting point for a player in
432 team four (Yellow).
433
434 Keys:
435 "angle"
436  viewing angle when spawning
437 */
438 void spawnfunc_info_player_team4()
439 {
440         self.team = COLOR_TEAM4; // yellow
441         spawnfunc_info_player_deathmatch();
442 };
443
444
445
446
447 /*QUAKED spawnfunc_item_flag_team1 (0 0.5 0.8) (-48 -48 -37) (48 48 37)
448 CTF flag for team one (Red).
449 Multiple are allowed.
450
451 Keys:
452 "angle"
453  Angle the flag will point
454 (minus 90 degrees)
455 "model"
456  model to use, note this needs red and blue as skins 0 and 1
457  (default models/ctf/flag.md3)
458 "noise"
459  sound played when flag is picked up
460  (default ctf/take.wav)
461 "noise1"
462  sound played when flag is returned by a teammate
463  (default ctf/return.wav)
464 "noise2"
465  sound played when flag is captured
466  (default ctf/redcapture.wav)
467 "noise3"
468  sound played when flag is lost in the field and respawns itself
469  (default ctf/respawn.wav)
470 */
471
472 void spawnfunc_item_flag_team1()
473 {
474         if (!g_ctf)
475         {
476                 remove(self);
477                 return;
478         }
479
480         //if(!cvar("teamplay"))
481         //      cvar_set("teamplay", "3");
482
483         // link flag into ctf_worldflaglist
484         self.ctf_worldflagnext = ctf_worldflaglist;
485         ctf_worldflaglist = self;
486
487         self.classname = "item_flag_team";
488         self.team = COLOR_TEAM1; // color 4 team (red)
489         self.items = IT_KEY2; // gold key (redish enough)
490         self.netname = "^1RED^7 flag";
491         self.target = "###item###";
492         self.skin = 0;
493         if(self.spawnflags & 1)
494                 self.noalign = 1;
495         if (!self.model)
496                 self.model = "models/ctf/flag_red.md3";
497         if (!self.noise)
498                 self.noise = "ctf/take.wav";
499         if (!self.noise1)
500                 self.noise1 = "ctf/return.wav";
501         if (!self.noise2)
502                 self.noise2 = "ctf/redcapture.wav"; // blue team scores by capturing the red flag
503         if (!self.noise3)
504                 self.noise3 = "ctf/respawn.wav";
505         precache_model (self.model);
506         setmodel (self, self.model); // precision set below
507         precache_sound (self.noise);
508         precache_sound (self.noise1);
509         precache_sound (self.noise2);
510         precache_sound (self.noise3);
511         setsize(self, '-16 -16 -37', '16 16 37');
512         setorigin(self, self.origin + '0 0 37');
513         self.nextthink = time + 0.2; // start after doors etc
514         self.think = place_flag;
515
516         if(!self.scale)
517                 self.scale = 0.6;
518         //if(!self.glow_size)
519         //      self.glow_size = 50;
520
521         self.effects = self.effects | EF_FULLBRIGHT | EF_LOWPRECISION;
522         if(!self.noalign)
523                 droptofloor();
524
525         waypoint_spawnforitem(self);
526
527         WaypointSprite_SpawnFixed("redbase", self.origin + '0 0 37', self, sprite);
528 };
529
530 /*QUAKED spawnfunc_item_flag_team2 (0 0.5 0.8) (-48 -48 -24) (48 48 64)
531 CTF flag for team two (Blue).
532 Multiple are allowed.
533
534 Keys:
535 "angle"
536  Angle the flag will point
537 (minus 90 degrees)
538 "model"
539  model to use, note this needs red and blue as skins 0 and 1
540  (default models/ctf/flag.md3)
541 "noise"
542  sound played when flag is picked up
543  (default ctf/take.wav)
544 "noise1"
545  sound played when flag is returned by a teammate
546  (default ctf/return.wav)
547 "noise2"
548  sound played when flag is captured
549  (default ctf/bluecapture.wav)
550 "noise3"
551  sound played when flag is lost in the field and respawns itself
552  (default ctf/respawn.wav)
553 */
554
555 void spawnfunc_item_flag_team2()
556 {
557         if (!g_ctf)
558         {
559                 remove(self);
560                 return;
561         }
562         //if(!cvar("teamplay"))
563         //      cvar_set("teamplay", "3");
564
565         // link flag into ctf_worldflaglist
566         self.ctf_worldflagnext = ctf_worldflaglist;
567         ctf_worldflaglist = self;
568
569         self.classname = "item_flag_team";
570         self.team = COLOR_TEAM2; // color 13 team (blue)
571         self.items = IT_KEY1; // silver key (bluish enough)
572         self.netname = "^4BLUE^7 flag";
573         self.target = "###item###";
574         self.skin = 0;
575         if(self.spawnflags & 1)
576                 self.noalign = 1;
577         if (!self.model)
578                 self.model = "models/ctf/flag_blue.md3";
579         if (!self.noise)
580                 self.noise = "ctf/take.wav";
581         if (!self.noise1)
582                 self.noise1 = "ctf/return.wav";
583         if (!self.noise2)
584                 self.noise2 = "ctf/bluecapture.wav"; // red team scores by capturing the blue flag
585         if (!self.noise3)
586                 self.noise3 = "ctf/respawn.wav";
587         precache_model (self.model);
588         setmodel (self, self.model); // precision set below
589         precache_sound (self.noise);
590         precache_sound (self.noise1);
591         precache_sound (self.noise2);
592         precache_sound (self.noise3);
593         setsize(self, '-16 -16 -37', '16 16 37');
594         setorigin(self, self.origin + '0 0 37');
595         self.nextthink = time + 0.2; // start after doors etc
596         self.think = place_flag;
597
598         if(!self.scale)
599                 self.scale = 0.6;
600         //if(!self.glow_size)
601         //      self.glow_size = 50;
602
603         self.effects = self.effects | EF_FULLBRIGHT | EF_LOWPRECISION;
604         if(!self.noalign)
605                 droptofloor();
606
607         waypoint_spawnforitem(self);
608
609         WaypointSprite_SpawnFixed("bluebase", self.origin + '0 0 37', self, sprite);
610 };
611
612
613 /*QUAKED spawnfunc_ctf_team (0 .5 .8) (-16 -16 -24) (16 16 32)
614 Team declaration for CTF gameplay, this allows you to decide what team
615 names and control point models are used in your map.
616
617 Note: If you use spawnfunc_ctf_team entities you must define at least 2!  However, unlike
618 domination, you don't need to make a blank one too.
619
620 Keys:
621 "netname"
622  Name of the team (for example Red, Blue, Green, Yellow, Life, Death, Offense, Defense, etc)
623 "cnt"
624  Scoreboard color of the team (for example 4 is red and 13 is blue)
625
626 */
627
628 void spawnfunc_ctf_team()
629 {
630         if (!g_ctf)
631         {
632                 remove(self);
633                 return;
634         }
635         self.classname = "ctf_team";
636         self.team = self.cnt + 1;
637 };
638
639 // code from here on is just to support maps that don't have control point and team entities
640 void ctf_spawnteam (string teamname, float teamcolor)
641 {
642         local entity oldself;
643         oldself = self;
644         self = spawn();
645         self.classname = "ctf_team";
646         self.netname = teamname;
647         self.cnt = teamcolor;
648
649         spawnfunc_ctf_team();
650
651         self = oldself;
652 };
653
654 // spawn some default teams if the map is not set up for ctf
655 void ctf_spawnteams()
656 {
657         float numteams;
658
659         numteams = 2;//cvar("g_ctf_default_teams");
660
661         ctf_spawnteam("Red", COLOR_TEAM1 - 1);
662         ctf_spawnteam("Blue", COLOR_TEAM2 - 1);
663 };
664
665 void ctf_delayedinit()
666 {
667         // if no teams are found, spawn defaults
668         if (find(world, classname, "ctf_team") == world)
669                 ctf_spawnteams();
670
671         ScoreRules_ctf();
672 };
673
674 void ctf_init()
675 {
676         InitializeEntity(world, ctf_delayedinit, INITPRIO_GAMETYPE);
677         flagcaptimerecord = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time")));
678 };
679
680 void ctf_setstatus2(entity flag, float shift)
681 {
682         if (flag.cnt == FLAG_CARRY)
683                 if (flag.owner == self)
684                         self.items |= shift * 3;
685                 else
686                         self.items |= shift * 1;
687         else if (flag.cnt == FLAG_DROPPED)
688                 self.items |= shift * 2;
689         else
690         {
691                 // no status bits
692         }
693 };
694
695 void ctf_setstatus()
696 {
697         self.items = self.items - (self.items & IT_RED_FLAG_TAKEN);
698         self.items = self.items - (self.items & IT_RED_FLAG_LOST);
699         self.items = self.items - (self.items & IT_BLUE_FLAG_TAKEN);
700         self.items = self.items - (self.items & IT_BLUE_FLAG_LOST);
701
702         if (g_ctf) {
703                 local entity flag;
704                 float redflags, blueflags;
705
706                 redflags = 0;
707                 blueflags = 0;
708
709                 for (flag = ctf_worldflaglist; flag; flag = flag.ctf_worldflagnext) if(flag.cnt != FLAG_BASE)
710                 {
711                         if(flag.team == COLOR_TEAM1)
712                                 ++redflags;
713                         else if(flag.team == COLOR_TEAM2)
714                                 ++blueflags;
715                 }
716
717                 // blinking magic: if there is more than one flag, show one of these in a clever way
718                 if(redflags)
719                         redflags = mod(floor(time * redflags * 0.75), redflags);
720                 if(blueflags)
721                         blueflags = mod(floor(time * blueflags * 0.75), blueflags);
722
723                 for (flag = ctf_worldflaglist; flag; flag = flag.ctf_worldflagnext) if(flag.cnt != FLAG_BASE)
724                 {
725                         if(flag.team == COLOR_TEAM1)
726                         {
727                                 if(--redflags == -1) // happens exactly once (redflags is in 0..count-1, and will --'ed count times)
728                                         ctf_setstatus2(flag, IT_RED_FLAG_TAKEN);
729                         }
730                         else if(flag.team == COLOR_TEAM2)
731                         {
732                                 if(--blueflags == -1) // happens exactly once
733                                         ctf_setstatus2(flag, IT_BLUE_FLAG_TAKEN);
734                         }
735                 }
736         }
737 };
738 /*
739 entity(float cteam) ctf_team_has_commander =
740 {
741         entity pl;
742         if(cteam != COLOR_TEAM1 || cteam != COLOR_TEAM2)
743                 return world;
744         
745         FOR_EACH_REALPLAYER(pl) {
746                 if(pl.team == cteam && pl.iscommander) {
747                         return pl;
748                 }
749         }
750         return world;
751 };
752
753 void(entity e, float st) ctf_setstate =
754 {
755         e.ctf_state = st;
756         ++e.version;
757 };
758
759 void(float cteam) ctf_new_commander =
760 {
761         entity pl, plmax;
762         
763         plmax = world;
764         FOR_EACH_REALPLAYER(pl) {
765                 if(pl.team == cteam) {
766                         if(pl.iscommander) { // don't reassign if alreay there
767                                 return;
768                         }
769                         if(plmax == world || plmax.frags < pl.frags) <<<<<<<<<<<<<<<<< BROKEN in new scoring system
770                                 plmax = pl;
771                 }
772         }
773         if(plmax == world) {
774                 bprint(strcat(ColoredTeamName(cteam), " Team has no Commander!\n"));
775                 return;
776         }
777
778         plmax.iscommander = TRUE;
779         ctf_setstate(plmax, 3);
780         sprint(plmax, "^3You're the commander now!\n");
781         centerprint(plmax, "^3You're the commander now!\n");
782 };
783
784 void() ctf_clientconnect =
785 {
786         self.iscommander = FALSE;
787         
788         if(!self.team || self.classname != "player") {
789                 ctf_setstate(self, -1);
790         } else
791                 ctf_setstate(self, 0);
792
793         self.team_saved = self.team;
794         
795         if(self.team != 0 && self.classname == "player" && !ctf_team_has_commander(self.team)) {
796                 ctf_new_commander(self.team);
797         }
798 };
799
800 void() ctf_playerchanged =
801 {
802         if(!self.team || self.classname != "player") {
803                 ctf_setstate(self, -1);
804         } else if(self.ctf_state < 0 && self.classname == "player") {
805                 ctf_setstate(self, 0);
806         }
807
808         if(self.iscommander &&
809            (self.classname != "player" || self.team != self.team_saved)
810                 )
811         {
812                 self.iscommander = FALSE;
813                 if(self.classname == "player")
814                         ctf_setstate(self, 0);
815                 else
816                         ctf_setstate(self, -1);
817                 ctf_new_commander(self.team_saved);
818         }
819         
820         self.team_saved = self.team;
821         
822         ctf_new_commander(self.team);
823 };
824
825 void() ctf_clientdisconnect =
826 {
827         if(self.iscommander)
828         {
829                 ctf_new_commander(self.team);
830         }
831 };
832
833 entity GetPlayer(string);
834 float() ctf_clientcommand =
835 {
836         entity e;
837         if(argv(0) == "order") {
838                 if(!g_ctf) {
839                         sprint(self, "This command is not supported in this gamemode.\n");
840                         return TRUE;
841                 }
842                 if(!self.iscommander) {
843                         sprint(self, "^1You are not the commander!\n");
844                         return TRUE;
845                 }
846                 if(argv(2) == "") {
847                         sprint(self, "Usage: order #player status   - (playernumber as in status)\n");
848                         return TRUE;
849                 }
850                 e = GetPlayer(argv(1));
851                 if(e == world) {
852                         sprint(self, "Invalid player.\nUsage: order #player status   - (playernumber as in status)\n");
853                         return TRUE;
854                 }
855                 if(e.team != self.team) {
856                         sprint(self, "^3You can only give orders to your own team!\n");
857                         return TRUE;
858                 }
859                 if(argv(2) == "attack") {
860                         sprint(self, strcat("Ordering ", e.netname, " to attack!\n"));
861                         sprint(e, "^1Attack!\n");
862                         centerprint(e, "^7You've been ordered to^9\n^1Attack!\n");
863                         ctf_setstate(e, 1);
864                 } else if(argv(2) == "defend") {
865                         sprint(self, strcat("Ordering ", e.netname, " to defend!\n"));
866                         sprint(e, "^Defend!\n");
867                         centerprint(e, "^7You've been ordered to^9\n^2Defend!\n");
868                         ctf_setstate(e, 2);
869                 } else {
870                         sprint(self, "^7Invalid command, use ^3attack^7, or ^3defend^7.\n");
871                 }
872                 return TRUE;
873         }
874         return FALSE;
875 };
876 */