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