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