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