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