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