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