2 entity ctf_worldflaglist; // CTF flags in the map
3 .entity ctf_worldflagnext;
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;
12 #define FLAG_CARRY_POS '-15 0 7'
14 void FakeTimeLimit(entity e, float t)
17 WriteByte(MSG_ONE, 3); // svc_updatestat
18 WriteByte(MSG_ONE, 236); // STAT_TIMELIMIT
20 WriteCoord(MSG_ONE, cvar("timelimit"));
22 WriteCoord(MSG_ONE, (t + 1) / 60);
25 float flagcaptimerecord;
26 .float flagpickuptime;
36 self.t_width = 0.1; // frame animation rate
38 self.t_length = 119; // maximum frame
40 setattachment(self, world, "");
41 self.mdl = self.model;
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;
51 self.mangle = self.angles;
52 self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP;
53 //self.effects = self.effects | EF_DIMLIGHT;
56 self.movetype = MOVETYPE_TOSS;
59 dprint("Flag fell out of level at ", vtos(self.origin), "\n");
64 self.oldorigin = self.origin;
67 void LogCTF(string mode, float flagteam, entity actor)
70 if(!cvar("sv_eventlog"))
72 s = strcat(":ctf:", mode);
73 s = strcat(s, ":", ftos(flagteam));
75 s = strcat(s, ":", ftos(actor.playerid));
79 void RegenFlag(entity e)
81 setattachment(e, world, "");
82 e.movetype = MOVETYPE_NONE;
84 e.movetype = MOVETYPE_TOSS;
85 e.solid = SOLID_TRIGGER;
86 // TODO: play a sound here
87 setorigin(e, e.oldorigin);
91 e.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
94 void ReturnFlag(entity e)
97 if (e.owner.flagcarried == e)
99 WaypointSprite_DetachCarrier(e.owner);
100 e.owner.flagcarried = world;
103 FakeTimeLimit(e.owner, -1);
109 void DropFlag(entity e, entity penalty_receiver, entity attacker)
121 dprint("FLAG: drop - no owner?!?!\n");
125 if (p.flagcarried != e)
127 dprint("FLAG: drop - owner is not carrying this flag??\n");
130 bprint(p.netname, "^7 lost the ", e.netname, "\n");
133 UpdateFrags(penalty_receiver, -cvar("g_ctf_flagpenalty_suicidedrop"));
135 UpdateFrags(p, -cvar("g_ctf_flagpenalty_drop"));
136 PlayerScore_Add(p, SP_CTF_DROPS, +1);
137 e.playerid = attacker.playerid;
139 WaypointSprite_DetachCarrier(p);
140 LogCTF("dropped", p.team, p);
142 setattachment(e, world, "");
144 if (p.flagcarried == e)
145 p.flagcarried = world;
148 e.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
149 e.solid = SOLID_TRIGGER;
150 e.movetype = MOVETYPE_TOSS;
151 // setsize(e, '-16 -16 0', '16 16 74');
152 setorigin(e, p.origin - '0 0 24' + '0 0 37');
153 e.cnt = FLAG_DROPPED;
154 e.velocity = '0 0 300';
155 e.pain_finished = time + cvar("g_ctf_flag_returntime");//30;
157 trace_startsolid = FALSE;
158 tracebox(e.origin, e.mins, e.maxs, e.origin, TRUE, e);
160 dprint("FLAG FALLTHROUGH will happen SOON\n");
165 if(self.delay > time)
167 self.delay = time + self.t_width;
168 if(self.nextthink > self.delay)
169 self.nextthink = self.delay;
171 self.frame = self.frame + 1;
172 if(self.frame > self.t_length)
180 self.nextthink = time + 0.1;
184 if(self.speedrunning)
185 if(self.cnt == FLAG_CARRY)
188 if(flagcaptimerecord)
189 if(time >= self.flagpickuptime + flagcaptimerecord)
191 bprint("The ", self.netname, " became impatient after ", ftos_decimals(flagcaptimerecord, 2), " seconds and returned itself\n");
193 self.owner.impulse = 77; // returning!
203 if (self.cnt == FLAG_BASE)
206 if (self.cnt == FLAG_DROPPED)
208 // flag fallthrough? FIXME remove this if bug is really fixed now
209 if(self.origin_z < -131072)
211 dprint("FLAG FALLTHROUGH just happened\n");
212 self.pain_finished = 0;
214 setattachment(self, world, "");
215 if (time > self.pain_finished)
217 bprint("The ", self.netname, " has returned to base\n");
218 sound (self, CHAN_TRIGGER, self.noise3, VOL_BASE, ATTN_NONE);
219 LogCTF("returned", self.team, world);
226 if (e.classname != "player" || (e.deadflag) || (e.flagcarried != self))
228 dprint("CANNOT HAPPEN - player dead and STILL had a flag!\n");
229 DropFlag(self, world, world);
240 local string s, s0, h0, h1;
241 if (other.classname != "player")
243 if (other.health < 1) // ignore dead players
246 if (self.cnt == FLAG_CARRY)
249 if (self.cnt == FLAG_BASE)
250 if (other.team == self.team)
251 if (other.flagcarried) // he's got a flag
252 if (other.flagcarried.team != self.team) // capture
254 if (other.flagcarried == world)
258 t = time - other.flagcarried.flagpickuptime;
259 s = ftos_decimals(t, 2);
260 s0 = ftos_decimals(flagcaptimerecord, 2);
261 h0 = db_get(ServerProgsDB, strcat(GetMapname(), "/captimerecord/netname"));
266 h0 = strcat(h0, "^7's"); // h0: display text for previous netname
267 if (flagcaptimerecord == 0)
269 bprint(other.netname, "^7 captured the ", other.flagcarried.netname, " in ", s, " seconds\n");
270 flagcaptimerecord = t;
271 db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time"), ftos(t));
272 db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/netname"), h1);
273 GameLogEcho(strcat(":recordset:", ftos(other.playerid), ":", ftos(t)));
275 else if (t < flagcaptimerecord)
277 bprint(other.netname, "^7 captured the ", other.flagcarried.netname, " in ", s, ", breaking ", strcat(h0, " previous record of ", s0, " seconds\n"));
278 flagcaptimerecord = t;
279 db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time"), ftos(t));
280 db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/netname"), h1);
281 GameLogEcho(strcat(":recordset:", ftos(other.playerid), ":", ftos(t)));
285 bprint(other.netname, "^7 captured the ", other.flagcarried.netname, " in ", s, ", failing to break ", strcat(h0, " record of ", s0, " seconds\n"));
288 PlayerTeamScore_Add(other, SP_CTF_CAPS, ST_CTF_CAPS, 1);
289 LogCTF("capture", other.flagcarried.team, other);
290 // give credit to the individual player
291 UpdateFrags(other, cvar("g_ctf_flagscore_capture"));
293 sound (other, CHAN_AUTO, self.noise2, VOL_BASE, ATTN_NONE);
294 WaypointSprite_DetachCarrier(other);
295 if(self.speedrunning)
296 FakeTimeLimit(other, -1);
297 RegenFlag (other.flagcarried);
298 other.flagcarried = world;
299 other.next_take_time = time + 1;
301 if (self.cnt == FLAG_BASE)
302 if (other.team == COLOR_TEAM1 || other.team == COLOR_TEAM2) // only red and blue team can steal flags
303 if (other.team != self.team)
304 if (!other.flagcarried)
306 if (other.next_take_time > time)
309 self.flagpickuptime = time; // used for timing runs
310 self.speedrunning = other.speedrunning; // if speedrunning, flag will self-return and teleport the owner back after the record
311 if(other.speedrunning)
312 if(flagcaptimerecord)
313 FakeTimeLimit(other, time + flagcaptimerecord);
314 self.solid = SOLID_NOT;
315 setorigin(self, self.origin); // relink
317 other.flagcarried = self;
318 self.cnt = FLAG_CARRY;
319 self.angles = '0 0 0';
320 bprint(other.netname, "^7 got the ", self.netname, "\n");
321 UpdateFrags(other, cvar("g_ctf_flagscore_pickup_base"));
322 PlayerScore_Add(other, SP_CTF_PICKUPS, 1);
323 LogCTF("steal", self.team, other);
324 sound (other, CHAN_AUTO, self.noise, VOL_BASE, ATTN_NONE);
326 FOR_EACH_PLAYER(player)
327 if(player.team == self.team)
328 centerprint(player, "The enemy got your flag! Retrieve it!");
330 self.movetype = MOVETYPE_NONE;
331 setorigin(self, FLAG_CARRY_POS);
332 setattachment(self, other, "");
333 WaypointSprite_AttachCarrier("flagcarrier", other);
338 if (self.cnt == FLAG_DROPPED)
340 self.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
341 if (other.team == self.team || (other.team != COLOR_TEAM1 && other.team != COLOR_TEAM2))
344 bprint(other.netname, "^7 returned the ", self.netname, "\n");
345 if(other.playerid == self.playerid) // is this the guy who killed the FC last?
347 if (other.team == COLOR_TEAM1 || other.team == COLOR_TEAM2)
348 UpdateFrags(other, cvar("g_ctf_flagscore_return_by_killer"));
350 UpdateFrags(other, cvar("g_ctf_flagscore_return_rogue_by_killer"));
354 if (other.team == COLOR_TEAM1 || other.team == COLOR_TEAM2)
355 UpdateFrags(other, cvar("g_ctf_flagscore_return"));
357 UpdateFrags(other, cvar("g_ctf_flagscore_return_rogue"));
359 PlayerScore_Add(other, SP_CTF_RETURNS, 1);
360 LogCTF("return", self.team, other);
361 sound (other, CHAN_AUTO, self.noise1, VOL_BASE, ATTN_NONE);
364 else if (!other.flagcarried)
367 self.solid = SOLID_NOT;
368 setorigin(self, self.origin); // relink
370 other.flagcarried = self;
371 self.cnt = FLAG_CARRY;
372 bprint(other.netname, "^7 picked up the ", self.netname, "\n");
375 f = (time - self.pain_finished) / cvar("g_ctf_flag_returntime");
376 f = cvar("g_ctf_flagscore_pickup_dropped_late") * (1-f)
377 + cvar("g_ctf_flagscore_pickup_dropped_early") * f;
380 UpdateFrags(other, f);
381 PlayerScore_Add(other, SP_CTF_PICKUPS, 1);
382 LogCTF("pickup", self.team, other);
383 sound (other, CHAN_AUTO, self.noise, VOL_BASE, ATTN_NONE);
385 FOR_EACH_PLAYER(player)
386 if(player.team == self.team)
387 centerprint(player, "The enemy got your flag! Retrieve it!");
389 self.movetype = MOVETYPE_NONE; // flag must have MOVETYPE_NONE here, otherwise it will drop through the floor...
390 setorigin(self, FLAG_CARRY_POS);
391 setattachment(self, other, "");
392 WaypointSprite_AttachCarrier("flagcarrier", other);
397 /*QUAKED spawnfunc_info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
398 CTF Starting point for a player
403 viewing angle when spawning
405 void spawnfunc_info_player_team1()
407 self.team = COLOR_TEAM1; // red
408 spawnfunc_info_player_deathmatch();
410 //self.team = 4;self.classname = "info_player_start";spawnfunc_info_player_start();};
412 /*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
413 CTF Starting point for a player in
418 viewing angle when spawning
420 void spawnfunc_info_player_team2()
422 self.team = COLOR_TEAM2; // blue
423 spawnfunc_info_player_deathmatch();
425 //self.team = 13;self.classname = "info_player_start";spawnfunc_info_player_start();};
427 /*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
428 CTF Starting point for a player in
429 team three (Magenta).
433 viewing angle when spawning
435 void spawnfunc_info_player_team3()
437 self.team = COLOR_TEAM3; // purple
438 spawnfunc_info_player_deathmatch();
442 /*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
443 CTF Starting point for a player in
448 viewing angle when spawning
450 void spawnfunc_info_player_team4()
452 self.team = COLOR_TEAM4; // yellow
453 spawnfunc_info_player_deathmatch();
459 /*QUAKED spawnfunc_item_flag_team1 (0 0.5 0.8) (-48 -48 -37) (48 48 37)
460 CTF flag for team one (Red).
461 Multiple are allowed.
465 Angle the flag will point
468 model to use, note this needs red and blue as skins 0 and 1
469 (default models/ctf/flag.md3)
471 sound played when flag is picked up
472 (default ctf/take.wav)
474 sound played when flag is returned by a teammate
475 (default ctf/return.wav)
477 sound played when flag is captured
478 (default ctf/redcapture.wav)
480 sound played when flag is lost in the field and respawns itself
481 (default ctf/respawn.wav)
484 void spawnfunc_item_flag_team1()
492 //if(!cvar("teamplay"))
493 // cvar_set("teamplay", "3");
495 // link flag into ctf_worldflaglist
496 self.ctf_worldflagnext = ctf_worldflaglist;
497 ctf_worldflaglist = self;
499 self.classname = "item_flag_team";
500 self.team = COLOR_TEAM1; // color 4 team (red)
501 self.items = IT_KEY2; // gold key (redish enough)
502 self.netname = "^1RED^7 flag";
503 self.target = "###item###";
505 if(self.spawnflags & 1)
508 self.model = "models/ctf/flag_red.md3";
510 self.noise = "ctf/take.wav";
512 self.noise1 = "ctf/return.wav";
514 self.noise2 = "ctf/redcapture.wav"; // blue team scores by capturing the red flag
516 self.noise3 = "ctf/respawn.wav";
517 precache_model (self.model);
518 setmodel (self, self.model); // precision set below
519 precache_sound (self.noise);
520 precache_sound (self.noise1);
521 precache_sound (self.noise2);
522 precache_sound (self.noise3);
523 setsize(self, '-16 -16 -37', '16 16 37');
524 setorigin(self, self.origin + '0 0 37');
525 self.nextthink = time + 0.2; // start after doors etc
526 self.think = place_flag;
530 //if(!self.glow_size)
531 // self.glow_size = 50;
533 self.effects = self.effects | EF_FULLBRIGHT | EF_LOWPRECISION;
537 waypoint_spawnforitem(self);
539 WaypointSprite_SpawnFixed("redbase", self.origin + '0 0 37', self, sprite);
542 /*QUAKED spawnfunc_item_flag_team2 (0 0.5 0.8) (-48 -48 -24) (48 48 64)
543 CTF flag for team two (Blue).
544 Multiple are allowed.
548 Angle the flag will point
551 model to use, note this needs red and blue as skins 0 and 1
552 (default models/ctf/flag.md3)
554 sound played when flag is picked up
555 (default ctf/take.wav)
557 sound played when flag is returned by a teammate
558 (default ctf/return.wav)
560 sound played when flag is captured
561 (default ctf/bluecapture.wav)
563 sound played when flag is lost in the field and respawns itself
564 (default ctf/respawn.wav)
567 void spawnfunc_item_flag_team2()
574 //if(!cvar("teamplay"))
575 // cvar_set("teamplay", "3");
577 // link flag into ctf_worldflaglist
578 self.ctf_worldflagnext = ctf_worldflaglist;
579 ctf_worldflaglist = self;
581 self.classname = "item_flag_team";
582 self.team = COLOR_TEAM2; // color 13 team (blue)
583 self.items = IT_KEY1; // silver key (bluish enough)
584 self.netname = "^4BLUE^7 flag";
585 self.target = "###item###";
587 if(self.spawnflags & 1)
590 self.model = "models/ctf/flag_blue.md3";
592 self.noise = "ctf/take.wav";
594 self.noise1 = "ctf/return.wav";
596 self.noise2 = "ctf/bluecapture.wav"; // red team scores by capturing the blue flag
598 self.noise3 = "ctf/respawn.wav";
599 precache_model (self.model);
600 setmodel (self, self.model); // precision set below
601 precache_sound (self.noise);
602 precache_sound (self.noise1);
603 precache_sound (self.noise2);
604 precache_sound (self.noise3);
605 setsize(self, '-16 -16 -37', '16 16 37');
606 setorigin(self, self.origin + '0 0 37');
607 self.nextthink = time + 0.2; // start after doors etc
608 self.think = place_flag;
612 //if(!self.glow_size)
613 // self.glow_size = 50;
615 self.effects = self.effects | EF_FULLBRIGHT | EF_LOWPRECISION;
619 waypoint_spawnforitem(self);
621 WaypointSprite_SpawnFixed("bluebase", self.origin + '0 0 37', self, sprite);
625 /*QUAKED spawnfunc_ctf_team (0 .5 .8) (-16 -16 -24) (16 16 32)
626 Team declaration for CTF gameplay, this allows you to decide what team
627 names and control point models are used in your map.
629 Note: If you use spawnfunc_ctf_team entities you must define at least 2! However, unlike
630 domination, you don't need to make a blank one too.
634 Name of the team (for example Red, Blue, Green, Yellow, Life, Death, Offense, Defense, etc)
636 Scoreboard color of the team (for example 4 is red and 13 is blue)
640 void spawnfunc_ctf_team()
647 self.classname = "ctf_team";
648 self.team = self.cnt + 1;
651 // code from here on is just to support maps that don't have control point and team entities
652 void ctf_spawnteam (string teamname, float teamcolor)
654 local entity oldself;
657 self.classname = "ctf_team";
658 self.netname = teamname;
659 self.cnt = teamcolor;
661 spawnfunc_ctf_team();
666 // spawn some default teams if the map is not set up for ctf
667 void ctf_spawnteams()
671 numteams = 2;//cvar("g_ctf_default_teams");
673 ctf_spawnteam("Red", COLOR_TEAM1 - 1);
674 ctf_spawnteam("Blue", COLOR_TEAM2 - 1);
677 void ctf_delayedinit()
679 // if no teams are found, spawn defaults
680 if (find(world, classname, "ctf_team") == world)
688 InitializeEntity(world, ctf_delayedinit, INITPRIO_GAMETYPE);
689 flagcaptimerecord = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time")));
692 void ctf_setstatus2(entity flag, float shift)
694 if (flag.cnt == FLAG_CARRY)
695 if (flag.owner == self)
696 self.items |= shift * 3;
698 self.items |= shift * 1;
699 else if (flag.cnt == FLAG_DROPPED)
700 self.items |= shift * 2;
709 self.items = self.items - (self.items & IT_RED_FLAG_TAKEN);
710 self.items = self.items - (self.items & IT_RED_FLAG_LOST);
711 self.items = self.items - (self.items & IT_BLUE_FLAG_TAKEN);
712 self.items = self.items - (self.items & IT_BLUE_FLAG_LOST);
716 float redflags, blueflags;
721 for (flag = ctf_worldflaglist; flag; flag = flag.ctf_worldflagnext) if(flag.cnt != FLAG_BASE)
723 if(flag.team == COLOR_TEAM1)
725 else if(flag.team == COLOR_TEAM2)
729 // blinking magic: if there is more than one flag, show one of these in a clever way
731 redflags = mod(floor(time * redflags * 0.75), redflags);
733 blueflags = mod(floor(time * blueflags * 0.75), blueflags);
735 for (flag = ctf_worldflaglist; flag; flag = flag.ctf_worldflagnext) if(flag.cnt != FLAG_BASE)
737 if(flag.team == COLOR_TEAM1)
739 if(--redflags == -1) // happens exactly once (redflags is in 0..count-1, and will --'ed count times)
740 ctf_setstatus2(flag, IT_RED_FLAG_TAKEN);
742 else if(flag.team == COLOR_TEAM2)
744 if(--blueflags == -1) // happens exactly once
745 ctf_setstatus2(flag, IT_BLUE_FLAG_TAKEN);
751 entity(float cteam) ctf_team_has_commander =
754 if(cteam != COLOR_TEAM1 || cteam != COLOR_TEAM2)
757 FOR_EACH_REALPLAYER(pl) {
758 if(pl.team == cteam && pl.iscommander) {
765 void(entity e, float st) ctf_setstate =
771 void(float cteam) ctf_new_commander =
776 FOR_EACH_REALPLAYER(pl) {
777 if(pl.team == cteam) {
778 if(pl.iscommander) { // don't reassign if alreay there
781 if(plmax == world || plmax.frags < pl.frags) <<<<<<<<<<<<<<<<< BROKEN in new scoring system
786 bprint(strcat(ColoredTeamName(cteam), " Team has no Commander!\n"));
790 plmax.iscommander = TRUE;
791 ctf_setstate(plmax, 3);
792 sprint(plmax, "^3You're the commander now!\n");
793 centerprint(plmax, "^3You're the commander now!\n");
796 void() ctf_clientconnect =
798 self.iscommander = FALSE;
800 if(!self.team || self.classname != "player") {
801 ctf_setstate(self, -1);
803 ctf_setstate(self, 0);
805 self.team_saved = self.team;
807 if(self.team != 0 && self.classname == "player" && !ctf_team_has_commander(self.team)) {
808 ctf_new_commander(self.team);
812 void() ctf_playerchanged =
814 if(!self.team || self.classname != "player") {
815 ctf_setstate(self, -1);
816 } else if(self.ctf_state < 0 && self.classname == "player") {
817 ctf_setstate(self, 0);
820 if(self.iscommander &&
821 (self.classname != "player" || self.team != self.team_saved)
824 self.iscommander = FALSE;
825 if(self.classname == "player")
826 ctf_setstate(self, 0);
828 ctf_setstate(self, -1);
829 ctf_new_commander(self.team_saved);
832 self.team_saved = self.team;
834 ctf_new_commander(self.team);
837 void() ctf_clientdisconnect =
841 ctf_new_commander(self.team);
845 entity GetPlayer(string);
846 float() ctf_clientcommand =
849 if(argv(0) == "order") {
851 sprint(self, "This command is not supported in this gamemode.\n");
854 if(!self.iscommander) {
855 sprint(self, "^1You are not the commander!\n");
859 sprint(self, "Usage: order #player status - (playernumber as in status)\n");
862 e = GetPlayer(argv(1));
864 sprint(self, "Invalid player.\nUsage: order #player status - (playernumber as in status)\n");
867 if(e.team != self.team) {
868 sprint(self, "^3You can only give orders to your own team!\n");
871 if(argv(2) == "attack") {
872 sprint(self, strcat("Ordering ", e.netname, " to attack!\n"));
873 sprint(e, "^1Attack!\n");
874 centerprint(e, "^7You've been ordered to^9\n^1Attack!\n");
876 } else if(argv(2) == "defend") {
877 sprint(self, strcat("Ordering ", e.netname, " to defend!\n"));
878 sprint(e, "^Defend!\n");
879 centerprint(e, "^7You've been ordered to^9\n^2Defend!\n");
882 sprint(self, "^7Invalid command, use ^3attack^7, or ^3defend^7.\n");