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