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
8 //float FLAGSCORE_PICKUP = 1;
9 //float FLAGSCORE_RETURN = 5; // returned by owner team
10 //float FLAGSCORE_RETURNROGUE = 10; // returned by rogue team
11 //float FLAGSCORE_CAPTURE = 5;
12 //float FLAGSCORE_CAPTURE_TEAM = 20;
14 #define FLAG_CARRY_POS '-15 0 7'
16 void FakeTimeLimit(entity e, float t)
19 WriteByte(MSG_ONE, 3); // svc_updatestat
20 WriteByte(MSG_ONE, 236); // STAT_TIMELIMIT
22 WriteCoord(MSG_ONE, cvar("timelimit"));
24 WriteCoord(MSG_ONE, (t + 1) / 60);
27 float flagcaptimerecord;
28 .float flagpickuptime;
38 self.t_width = 0.1; // frame animation rate
40 self.t_length = 119; // maximum frame
42 setattachment(self, world, "");
43 self.mdl = self.model;
45 self.solid = SOLID_TRIGGER;
46 self.movetype = MOVETYPE_NONE;
47 self.velocity = '0 0 0';
48 self.origin_z = self.origin_z + 6;
49 self.think = FlagThink;
50 self.touch = FlagTouch;
51 self.nextthink = time + 0.1;
53 self.mangle = self.angles;
54 self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP;
55 //self.effects = self.effects | EF_DIMLIGHT;
58 self.movetype = MOVETYPE_TOSS;
61 dprint("Flag fell out of level at ", vtos(self.origin), "\n");
66 self.oldorigin = self.origin;
69 void LogCTF(string mode, float flagteam, entity actor)
72 if(!cvar("sv_eventlog"))
74 s = strcat(":ctf:", mode);
75 s = strcat(s, ":", ftos(flagteam));
77 s = strcat(s, ":", ftos(actor.playerid));
78 GameLogEcho(s, FALSE);
81 void RegenFlag(entity e)
83 setattachment(e, world, "");
84 e.movetype = MOVETYPE_NONE;
86 e.movetype = MOVETYPE_TOSS;
87 e.solid = SOLID_TRIGGER;
88 // TODO: play a sound here
89 setorigin(e, e.oldorigin);
93 e.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
96 void ReturnFlag(entity e)
99 if (e.owner.flagcarried == e)
101 WaypointSprite_DetachCarrier(e.owner);
102 e.owner.flagcarried = world;
105 FakeTimeLimit(e.owner, -1);
111 void DropFlag(entity e)
123 dprint("FLAG: drop - no owner?!?!\n");
127 if (p.flagcarried != e)
129 dprint("FLAG: drop - owner is not carrying this flag??\n");
132 bprint(p.netname, "^7 lost the ", e.netname, "\n");
133 WaypointSprite_DetachCarrier(p);
134 LogCTF("dropped", p.team, p.flagcarried);
136 setattachment(e, world, "");
138 if (p.flagcarried == e)
139 p.flagcarried = world;
142 e.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
143 e.solid = SOLID_TRIGGER;
144 e.movetype = MOVETYPE_TOSS;
145 // setsize(e, '-16 -16 0', '16 16 74');
146 setorigin(e, p.origin - '0 0 24' + '0 0 37');
147 e.cnt = FLAG_DROPPED;
148 e.velocity = '0 0 300';
149 e.pain_finished = time + cvar("g_ctf_flag_returntime");//30;
151 trace_startsolid = FALSE;
152 tracebox(e.origin, e.mins, e.maxs, e.origin, TRUE, e);
154 dprint("FLAG FALLTHROUGH will happen SOON\n");
159 if(self.delay > time)
161 self.delay = time + self.t_width;
162 if(self.nextthink > self.delay)
163 self.nextthink = self.delay;
165 self.frame = self.frame + 1;
166 if(self.frame > self.t_length)
174 self.nextthink = time + 0.1;
178 if(self.speedrunning)
179 if(self.cnt == FLAG_CARRY)
182 if(flagcaptimerecord)
183 if(time >= self.flagpickuptime + flagcaptimerecord)
185 bprint("The ", self.netname, " became impatient after ", ftos_decimals(flagcaptimerecord, 2), " seconds and returned itself\n");
187 self.owner.impulse = 77; // returning!
197 if (self.cnt == FLAG_BASE)
200 if (self.cnt == FLAG_DROPPED)
202 // flag fallthrough? FIXME remove this if bug is really fixed now
203 if(self.origin_z < -131072)
205 dprint("FLAG FALLTHROUGH just happened\n");
206 self.pain_finished = 0;
208 setattachment(self, world, "");
209 if (time > self.pain_finished)
211 bprint("The ", self.netname, " has returned to base\n");
212 sound (e, CHAN_AUTO, self.noise3, 1, ATTN_NONE);
213 LogCTF("returned", self.team, world);
220 if (e.classname != "player" || (e.deadflag) || (e.flagcarried != self))
222 dprint("CANNOT HAPPEN - player dead and STILL had a flag!\n");
235 local string s, s0, h0, h1;
236 if (other.classname != "player")
238 if (other.health < 1) // ignore dead players
241 if (self.cnt == FLAG_CARRY)
244 if (self.cnt == FLAG_BASE)
245 if (other.team == self.team)
246 if (other.flagcarried) // he's got a flag
247 if (other.flagcarried.team != self.team) // capture
249 if (other.flagcarried == world)
253 t = time - other.flagcarried.flagpickuptime;
254 s = ftos_decimals(t, 2);
255 s0 = ftos_decimals(flagcaptimerecord, 2);
256 h0 = db_get(ServerProgsDB, strcat(GetMapname(), "/captimerecord/netname"));
261 h0 = strcat(h0, "^7's"); // h0: display text for previous netname
262 if (flagcaptimerecord == 0)
264 bprint(other.netname, "^7 captured the ", other.flagcarried.netname, " in ", s, " seconds\n");
265 flagcaptimerecord = t;
266 db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time"), ftos(t));
267 db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/netname"), h1);
269 else if (t < flagcaptimerecord)
271 bprint(other.netname, "^7 captured the ", other.flagcarried.netname, " in ", s, ", breaking ", strcat(h0, " previous record of ", s0, " seconds\n"));
272 flagcaptimerecord = t;
273 db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time"), ftos(t));
274 db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/netname"), h1);
278 bprint(other.netname, "^7 captured the ", other.flagcarried.netname, " in ", s, ", failing to break ", strcat(h0, " record of ", s0, " seconds\n"));
281 LogCTF("capture", other.flagcarried.team, other);
282 // give credit to the individual player
283 UpdateFrags(other, cvar("g_ctf_flagscore_capture"));
285 // give credit to all players of the team (rewards large teams)
286 // NOTE: this defaults to 0
287 FOR_EACH_PLAYER(head)
288 if (head.team == self.team)
289 UpdateFrags(head, cvar("g_ctf_flagscore_capture_team"));
291 sound (self, CHAN_AUTO, self.noise2, 1, ATTN_NONE);
292 WaypointSprite_DetachCarrier(other);
293 if(self.speedrunning)
294 FakeTimeLimit(other, -1);
295 RegenFlag (other.flagcarried);
296 other.flagcarried = world;
297 other.next_take_time = time + 1;
299 if (self.cnt == FLAG_BASE)
300 if (other.team == COLOR_TEAM1 || other.team == COLOR_TEAM2) // only red and blue team can steal flags
301 if (other.team != self.team)
302 if (!other.flagcarried)
304 if (other.next_take_time > time)
307 self.flagpickuptime = time; // used for timing runs
308 self.speedrunning = other.speedrunning; // if speedrunning, flag will self-return and teleport the owner back after the record
309 if(other.speedrunning)
310 if(flagcaptimerecord)
311 FakeTimeLimit(other, time + flagcaptimerecord);
312 self.solid = SOLID_NOT;
313 setorigin(self, self.origin); // relink
315 other.flagcarried = self;
316 self.cnt = FLAG_CARRY;
317 self.angles = '0 0 0';
318 bprint(other.netname, "^7 got the ", self.netname, "\n");
319 UpdateFrags(other, cvar("g_ctf_flagscore_pickup"));
320 LogCTF("steal", self.team, other);
321 sound (self, CHAN_AUTO, self.noise, 1, ATTN_NONE);
323 FOR_EACH_PLAYER(player)
324 if(player.team == self.team)
325 centerprint(player, "The enemy got your flag! Retrieve it!");
327 self.movetype = MOVETYPE_NONE;
328 setorigin(self, FLAG_CARRY_POS);
329 setattachment(self, other, "");
330 WaypointSprite_AttachCarrier("flagcarrier", other);
335 if (self.cnt == FLAG_DROPPED)
337 self.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
338 if (other.team == self.team || (other.team != COLOR_TEAM1 && other.team != COLOR_TEAM2))
341 bprint(other.netname, "^7 returned the ", self.netname, "\n");
342 if (other.team == COLOR_TEAM1 || other.team == COLOR_TEAM2)
343 UpdateFrags(other, cvar("g_ctf_flagscore_return"));
345 UpdateFrags(other, cvar("g_ctf_flagscore_return_rogue"));
346 LogCTF("return", self.team, other);
347 sound (self, CHAN_AUTO, self.noise1, 1, ATTN_NONE);
350 else if (!other.flagcarried)
353 self.solid = SOLID_NOT;
354 setorigin(self, self.origin); // relink
356 other.flagcarried = self;
357 self.cnt = FLAG_CARRY;
358 bprint(other.netname, "^7 picked up the ", self.netname, "\n");
359 UpdateFrags(other, cvar("g_ctf_flagscore_pickup"));
360 LogCTF("pickup", self.team, other);
361 sound (self, CHAN_AUTO, self.noise, 1, ATTN_NONE);
363 FOR_EACH_PLAYER(player)
364 if(player.team == self.team)
365 centerprint(player, "The enemy got your flag! Retrieve it!");
367 self.movetype = MOVETYPE_NONE; // flag must have MOVETYPE_NONE here, otherwise it will drop through the floor...
368 setorigin(self, FLAG_CARRY_POS);
369 setattachment(self, other, "");
370 WaypointSprite_AttachCarrier("flagcarrier", other);
375 /*QUAKED spawnfunc_info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
376 CTF Starting point for a player
381 viewing angle when spawning
383 void spawnfunc_info_player_team1()
385 self.team = COLOR_TEAM1; // red
386 spawnfunc_info_player_deathmatch();
388 //self.team = 4;self.classname = "info_player_start";spawnfunc_info_player_start();};
390 /*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
391 CTF Starting point for a player in
396 viewing angle when spawning
398 void spawnfunc_info_player_team2()
400 self.team = COLOR_TEAM2; // blue
401 spawnfunc_info_player_deathmatch();
403 //self.team = 13;self.classname = "info_player_start";spawnfunc_info_player_start();};
405 /*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
406 CTF Starting point for a player in
407 team three (Magenta).
411 viewing angle when spawning
413 void spawnfunc_info_player_team3()
415 self.team = COLOR_TEAM3; // purple
416 spawnfunc_info_player_deathmatch();
420 /*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
421 CTF Starting point for a player in
426 viewing angle when spawning
428 void spawnfunc_info_player_team4()
430 self.team = COLOR_TEAM4; // yellow
431 spawnfunc_info_player_deathmatch();
437 /*QUAKED spawnfunc_item_flag_team1 (0 0.5 0.8) (-48 -48 -37) (48 48 37)
438 CTF flag for team one (Red).
439 Multiple are allowed.
443 Angle the flag will point
446 model to use, note this needs red and blue as skins 0 and 1
447 (default models/ctf/flag.md3)
449 sound played when flag is picked up
450 (default ctf/take.wav)
452 sound played when flag is returned by a teammate
453 (default ctf/return.wav)
455 sound played when flag is captured
456 (default ctf/redcapture.wav)
458 sound played when flag is lost in the field and respawns itself
459 (default ctf/respawn.wav)
462 void spawnfunc_item_flag_team1()
470 //if(!cvar("teamplay"))
471 // cvar_set("teamplay", "3");
473 // link flag into ctf_worldflaglist
474 self.ctf_worldflagnext = ctf_worldflaglist;
475 ctf_worldflaglist = self;
477 self.classname = "item_flag_team";
478 self.team = COLOR_TEAM1; // color 4 team (red)
479 self.items = IT_KEY2; // gold key (redish enough)
480 self.netname = "^1RED^7 flag";
481 self.target = "###item###";
483 if(self.spawnflags & 1)
486 self.model = "models/ctf/flag_red.md3";
488 self.noise = "ctf/take.wav";
490 self.noise1 = "ctf/return.wav";
492 self.noise2 = "ctf/redcapture.wav"; // blue team scores by capturing the red flag
494 self.noise3 = "ctf/respawn.wav";
495 precache_model (self.model);
496 setmodel (self, self.model); // precision set below
497 precache_sound (self.noise);
498 precache_sound (self.noise1);
499 precache_sound (self.noise2);
500 precache_sound (self.noise3);
501 setsize(self, '-16 -16 -37', '16 16 37');
502 setorigin(self, self.origin + '0 0 37');
503 self.nextthink = time + 0.2; // start after doors etc
504 self.think = place_flag;
508 //if(!self.glow_size)
509 // self.glow_size = 50;
511 self.effects = self.effects | EF_FULLBRIGHT | EF_LOWPRECISION;
515 waypoint_spawnforitem(self);
517 WaypointSprite_SpawnFixed("redbase", self.origin + '0 0 37', self, sprite);
520 /*QUAKED spawnfunc_item_flag_team2 (0 0.5 0.8) (-48 -48 -24) (48 48 64)
521 CTF flag for team two (Blue).
522 Multiple are allowed.
526 Angle the flag will point
529 model to use, note this needs red and blue as skins 0 and 1
530 (default models/ctf/flag.md3)
532 sound played when flag is picked up
533 (default ctf/take.wav)
535 sound played when flag is returned by a teammate
536 (default ctf/return.wav)
538 sound played when flag is captured
539 (default ctf/bluecapture.wav)
541 sound played when flag is lost in the field and respawns itself
542 (default ctf/respawn.wav)
545 void spawnfunc_item_flag_team2()
552 //if(!cvar("teamplay"))
553 // cvar_set("teamplay", "3");
555 // link flag into ctf_worldflaglist
556 self.ctf_worldflagnext = ctf_worldflaglist;
557 ctf_worldflaglist = self;
559 self.classname = "item_flag_team";
560 self.team = COLOR_TEAM2; // color 13 team (blue)
561 self.items = IT_KEY1; // silver key (bluish enough)
562 self.netname = "^4BLUE^7 flag";
563 self.target = "###item###";
565 if(self.spawnflags & 1)
568 self.model = "models/ctf/flag_blue.md3";
570 self.noise = "ctf/take.wav";
572 self.noise1 = "ctf/return.wav";
574 self.noise2 = "ctf/bluecapture.wav"; // red team scores by capturing the blue flag
576 self.noise3 = "ctf/respawn.wav";
577 precache_model (self.model);
578 setmodel (self, self.model); // precision set below
579 precache_sound (self.noise);
580 precache_sound (self.noise1);
581 precache_sound (self.noise2);
582 precache_sound (self.noise3);
583 setsize(self, '-16 -16 -37', '16 16 37');
584 setorigin(self, self.origin + '0 0 37');
585 self.nextthink = time + 0.2; // start after doors etc
586 self.think = place_flag;
590 //if(!self.glow_size)
591 // self.glow_size = 50;
593 self.effects = self.effects | EF_FULLBRIGHT | EF_LOWPRECISION;
597 waypoint_spawnforitem(self);
599 WaypointSprite_SpawnFixed("bluebase", self.origin + '0 0 37', self, sprite);
603 /*QUAKED spawnfunc_ctf_team (0 .5 .8) (-16 -16 -24) (16 16 32)
604 Team declaration for CTF gameplay, this allows you to decide what team
605 names and control point models are used in your map.
607 Note: If you use spawnfunc_ctf_team entities you must define at least 2! However, unlike
608 domination, you don't need to make a blank one too.
612 Name of the team (for example Red, Blue, Green, Yellow, Life, Death, Offense, Defense, etc)
614 Scoreboard color of the team (for example 4 is red and 13 is blue)
618 void spawnfunc_ctf_team()
625 self.classname = "ctf_team";
626 self.team = self.cnt + 1;
629 // code from here on is just to support maps that don't have control point and team entities
630 void ctf_spawnteam (string teamname, float teamcolor)
632 local entity oldself;
635 self.classname = "ctf_team";
636 self.netname = teamname;
637 self.cnt = teamcolor;
639 spawnfunc_ctf_team();
644 // spawn some default teams if the map is not set up for ctf
645 void ctf_spawnteams()
649 numteams = 2;//cvar("g_ctf_default_teams");
651 ctf_spawnteam("Red", COLOR_TEAM1 - 1);
652 ctf_spawnteam("Blue", COLOR_TEAM2 - 1);
655 void ctf_delayedinit()
657 self.think = SUB_Remove;
658 self.nextthink = time;
659 // if no teams are found, spawn defaults
660 if (find(world, classname, "ctf_team") == world)
668 //addstat(STAT_CTF_STATE, AS_FLOAT_TRUNCATED, ctf_state);
670 e.think = ctf_delayedinit;
671 e.nextthink = time + 0.1;
672 flagcaptimerecord = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time")));
675 void ctf_setstatus2(entity flag, float shift)
677 if (flag.cnt == FLAG_CARRY)
678 if (flag.owner == self)
679 self.items |= shift * 3;
681 self.items |= shift * 1;
682 else if (flag.cnt == FLAG_DROPPED)
683 self.items |= shift * 2;
692 self.items = self.items - (self.items & IT_RED_FLAG_TAKEN);
693 self.items = self.items - (self.items & IT_RED_FLAG_LOST);
694 self.items = self.items - (self.items & IT_BLUE_FLAG_TAKEN);
695 self.items = self.items - (self.items & IT_BLUE_FLAG_LOST);
699 float redflags, blueflags;
704 for (flag = ctf_worldflaglist; flag; flag = flag.ctf_worldflagnext) if(flag.cnt != FLAG_BASE)
706 if(flag.team == COLOR_TEAM1)
708 else if(flag.team == COLOR_TEAM2)
712 // blinking magic: if there is more than one flag, show one of these in a clever way
714 redflags = mod(floor(time * redflags * 0.75), redflags);
716 blueflags = mod(floor(time * blueflags * 0.75), blueflags);
718 for (flag = ctf_worldflaglist; flag; flag = flag.ctf_worldflagnext) if(flag.cnt != FLAG_BASE)
720 if(flag.team == COLOR_TEAM1)
722 if(redflags-- == 0) // happens exactly once
723 ctf_setstatus2(flag, IT_RED_FLAG_TAKEN);
725 else if(flag.team == COLOR_TEAM2)
727 if(blueflags-- == 0) // happens exactly once
728 ctf_setstatus2(flag, IT_BLUE_FLAG_TAKEN);
734 entity(float cteam) ctf_team_has_commander =
737 if(cteam != COLOR_TEAM1 || cteam != COLOR_TEAM2)
740 FOR_EACH_REALPLAYER(pl) {
741 if(pl.team == cteam && pl.iscommander) {
748 void(entity e, float st) ctf_setstate =
754 void(float cteam) ctf_new_commander =
759 FOR_EACH_REALPLAYER(pl) {
760 if(pl.team == cteam) {
761 if(pl.iscommander) { // don't reassign if alreay there
764 if(plmax == world || plmax.frags < pl.frags)
769 bprint(strcat(ColoredTeamName(cteam), " Team has no Commander!\n"));
773 plmax.iscommander = TRUE;
774 ctf_setstate(plmax, 3);
775 sprint(plmax, "^3You're the commander now!\n");
776 centerprint(plmax, "^3You're the commander now!\n");
779 void() ctf_clientconnect =
781 self.iscommander = FALSE;
783 if(!self.team || self.classname != "player") {
784 ctf_setstate(self, -1);
786 ctf_setstate(self, 0);
788 self.team_saved = self.team;
790 if(self.team != 0 && self.classname == "player" && !ctf_team_has_commander(self.team)) {
791 ctf_new_commander(self.team);
795 void() ctf_playerchanged =
797 if(!self.team || self.classname != "player") {
798 ctf_setstate(self, -1);
799 } else if(self.ctf_state < 0 && self.classname == "player") {
800 ctf_setstate(self, 0);
803 if(self.iscommander &&
804 (self.classname != "player" || self.team != self.team_saved)
807 self.iscommander = FALSE;
808 if(self.classname == "player")
809 ctf_setstate(self, 0);
811 ctf_setstate(self, -1);
812 ctf_new_commander(self.team_saved);
815 self.team_saved = self.team;
817 ctf_new_commander(self.team);
820 void() ctf_clientdisconnect =
824 ctf_new_commander(self.team);
828 entity GetPlayer(string);
829 float() ctf_clientcommand =
832 if(argv(0) == "order") {
834 sprint(self, "This command is not supported in this gamemode.\n");
837 if(!self.iscommander) {
838 sprint(self, "^1You are not the commander!\n");
842 sprint(self, "Usage: order #player status - (playernumber as in status)\n");
845 e = GetPlayer(argv(1));
847 sprint(self, "Invalid player.\nUsage: order #player status - (playernumber as in status)\n");
850 if(e.team != self.team) {
851 sprint(self, "^3You can only give orders to your own team!\n");
854 if(argv(2) == "attack") {
855 sprint(self, strcat("Ordering ", e.netname, " to attack!\n"));
856 sprint(e, "^1Attack!\n");
857 centerprint(e, "^7You've been ordered to^9\n^1Attack!\n");
859 } else if(argv(2) == "defend") {
860 sprint(self, strcat("Ordering ", e.netname, " to defend!\n"));
861 sprint(e, "^Defend!\n");
862 centerprint(e, "^7You've been ordered to^9\n^2Defend!\n");
865 sprint(self, "^7Invalid command, use ^3attack^7, or ^3defend^7.\n");