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