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