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;
11 //float FLAGSCORE_CAPTURE_TEAM = 20;
13 #define FLAG_CARRY_POS '-15 0 7'
15 void FakeTimeLimit(entity e, float t)
18 WriteByte(MSG_ONE, 3); // svc_updatestat
19 WriteByte(MSG_ONE, 236); // STAT_TIMELIMIT
21 WriteCoord(MSG_ONE, cvar("timelimit"));
23 WriteCoord(MSG_ONE, (t + 1) / 60);
26 float flagcaptimerecord;
27 .float flagpickuptime;
37 self.t_width = 0.1; // frame animation rate
39 self.t_length = 119; // maximum frame
41 setattachment(self, world, "");
42 self.mdl = self.model;
44 self.solid = SOLID_TRIGGER;
45 self.movetype = MOVETYPE_NONE;
46 self.velocity = '0 0 0';
47 self.origin_z = self.origin_z + 6;
48 self.think = FlagThink;
49 self.touch = FlagTouch;
50 self.nextthink = time + 0.1;
52 self.mangle = self.angles;
53 self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP;
54 //self.effects = self.effects | EF_DIMLIGHT;
57 self.movetype = MOVETYPE_TOSS;
60 dprint("Flag fell out of level at ", vtos(self.origin), "\n");
65 self.oldorigin = self.origin;
68 void LogCTF(string mode, float flagteam, entity actor)
71 if(!cvar("sv_eventlog"))
73 s = strcat(":ctf:", mode);
74 s = strcat(s, ":", ftos(flagteam));
76 s = strcat(s, ":", ftos(actor.playerid));
80 void RegenFlag(entity e)
82 setattachment(e, world, "");
83 e.movetype = MOVETYPE_NONE;
85 e.movetype = MOVETYPE_TOSS;
86 e.solid = SOLID_TRIGGER;
87 // TODO: play a sound here
88 setorigin(e, e.oldorigin);
92 e.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
95 void ReturnFlag(entity e)
98 if (e.owner.flagcarried == e)
100 WaypointSprite_DetachCarrier(e.owner);
101 e.owner.flagcarried = world;
104 FakeTimeLimit(e.owner, -1);
110 void DropFlag(entity e)
122 dprint("FLAG: drop - no owner?!?!\n");
126 if (p.flagcarried != e)
128 dprint("FLAG: drop - owner is not carrying this flag??\n");
131 bprint(p.netname, "^7 lost the ", e.netname, "\n");
133 if(cvar("g_ctf_flagpenalty_drop"))
134 UpdateFrags(p, -cvar("g_ctf_flagpenalty_drop"));
136 //if(e.enemy && e.enemy != e)
137 //UpdateFrags(e.enemy, cvar("g_ctf_flagscore_kill"));
138 WaypointSprite_DetachCarrier(p);
139 LogCTF("dropped", p.team, p.flagcarried);
141 setattachment(e, world, "");
143 if (p.flagcarried == e)
144 p.flagcarried = world;
147 e.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
148 e.solid = SOLID_TRIGGER;
149 e.movetype = MOVETYPE_TOSS;
150 // setsize(e, '-16 -16 0', '16 16 74');
151 setorigin(e, p.origin - '0 0 24' + '0 0 37');
152 e.cnt = FLAG_DROPPED;
153 e.velocity = '0 0 300';
154 e.pain_finished = time + cvar("g_ctf_flag_returntime");//30;
156 trace_startsolid = FALSE;
157 tracebox(e.origin, e.mins, e.maxs, e.origin, TRUE, e);
159 dprint("FLAG FALLTHROUGH will happen SOON\n");
164 if(self.delay > time)
166 self.delay = time + self.t_width;
167 if(self.nextthink > self.delay)
168 self.nextthink = self.delay;
170 self.frame = self.frame + 1;
171 if(self.frame > self.t_length)
179 self.nextthink = time + 0.1;
183 if(self.speedrunning)
184 if(self.cnt == FLAG_CARRY)
187 if(flagcaptimerecord)
188 if(time >= self.flagpickuptime + flagcaptimerecord)
190 bprint("The ", self.netname, " became impatient after ", ftos_decimals(flagcaptimerecord, 2), " seconds and returned itself\n");
192 self.owner.impulse = 77; // returning!
202 if (self.cnt == FLAG_BASE)
205 if (self.cnt == FLAG_DROPPED)
207 // flag fallthrough? FIXME remove this if bug is really fixed now
208 if(self.origin_z < -131072)
210 dprint("FLAG FALLTHROUGH just happened\n");
211 self.pain_finished = 0;
213 setattachment(self, world, "");
214 if (time > self.pain_finished)
216 bprint("The ", self.netname, " has returned to base\n");
217 sound (self, CHAN_TRIGGER, self.noise3, VOL_BASE, ATTN_NONE);
218 LogCTF("returned", self.team, world);
225 if (e.classname != "player" || (e.deadflag) || (e.flagcarried != self))
227 dprint("CANNOT HAPPEN - player dead and STILL had a flag!\n");
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 // give credit to all players of the team (rewards large teams)
294 // NOTE: this defaults to 0
295 FOR_EACH_PLAYER(head)
296 if (head.team == self.team)
297 UpdateFrags(head, cvar("g_ctf_flagscore_capture_team"));
299 sound (other, CHAN_AUTO, self.noise2, VOL_BASE, ATTN_NONE);
300 WaypointSprite_DetachCarrier(other);
301 if(self.speedrunning)
302 FakeTimeLimit(other, -1);
303 RegenFlag (other.flagcarried);
304 other.flagcarried = world;
305 other.next_take_time = time + 1;
307 if (self.cnt == FLAG_BASE)
308 if (other.team == COLOR_TEAM1 || other.team == COLOR_TEAM2) // only red and blue team can steal flags
309 if (other.team != self.team)
310 if (!other.flagcarried)
312 if (other.next_take_time > time)
315 self.flagpickuptime = time; // used for timing runs
316 self.speedrunning = other.speedrunning; // if speedrunning, flag will self-return and teleport the owner back after the record
317 if(other.speedrunning)
318 if(flagcaptimerecord)
319 FakeTimeLimit(other, time + flagcaptimerecord);
320 self.solid = SOLID_NOT;
321 setorigin(self, self.origin); // relink
323 other.flagcarried = self;
324 self.cnt = FLAG_CARRY;
325 self.angles = '0 0 0';
326 bprint(other.netname, "^7 got the ", self.netname, "\n");
327 UpdateFrags(other, cvar("g_ctf_flagscore_pickup"));
328 PlayerScore_Add(other, SP_CTF_PICKUPS, 1);
329 LogCTF("steal", self.team, other);
330 sound (other, CHAN_AUTO, self.noise, VOL_BASE, ATTN_NONE);
332 FOR_EACH_PLAYER(player)
333 if(player.team == self.team)
334 centerprint(player, "The enemy got your flag! Retrieve it!");
336 self.movetype = MOVETYPE_NONE;
337 setorigin(self, FLAG_CARRY_POS);
338 setattachment(self, other, "");
339 WaypointSprite_AttachCarrier("flagcarrier", other);
344 if (self.cnt == FLAG_DROPPED)
346 self.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
347 if (other.team == self.team || (other.team != COLOR_TEAM1 && other.team != COLOR_TEAM2))
350 bprint(other.netname, "^7 returned the ", self.netname, "\n");
351 if (other.team == COLOR_TEAM1 || other.team == COLOR_TEAM2)
352 UpdateFrags(other, cvar("g_ctf_flagscore_return"));
354 UpdateFrags(other, cvar("g_ctf_flagscore_return_rogue"));
355 PlayerScore_Add(other, SP_CTF_RETURNS, 1);
356 LogCTF("return", self.team, other);
357 sound (other, CHAN_AUTO, self.noise1, VOL_BASE, ATTN_NONE);
360 else if (!other.flagcarried)
363 self.solid = SOLID_NOT;
364 setorigin(self, self.origin); // relink
366 other.flagcarried = self;
367 self.cnt = FLAG_CARRY;
368 bprint(other.netname, "^7 picked up the ", self.netname, "\n");
369 UpdateFrags(other, cvar("g_ctf_flagscore_pickup"));
370 LogCTF("pickup", self.team, other);
371 sound (other, CHAN_AUTO, self.noise, VOL_BASE, ATTN_NONE);
373 FOR_EACH_PLAYER(player)
374 if(player.team == self.team)
375 centerprint(player, "The enemy got your flag! Retrieve it!");
377 self.movetype = MOVETYPE_NONE; // flag must have MOVETYPE_NONE here, otherwise it will drop through the floor...
378 setorigin(self, FLAG_CARRY_POS);
379 setattachment(self, other, "");
380 WaypointSprite_AttachCarrier("flagcarrier", other);
385 /*QUAKED spawnfunc_info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
386 CTF Starting point for a player
391 viewing angle when spawning
393 void spawnfunc_info_player_team1()
395 self.team = COLOR_TEAM1; // red
396 spawnfunc_info_player_deathmatch();
398 //self.team = 4;self.classname = "info_player_start";spawnfunc_info_player_start();};
400 /*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
401 CTF Starting point for a player in
406 viewing angle when spawning
408 void spawnfunc_info_player_team2()
410 self.team = COLOR_TEAM2; // blue
411 spawnfunc_info_player_deathmatch();
413 //self.team = 13;self.classname = "info_player_start";spawnfunc_info_player_start();};
415 /*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
416 CTF Starting point for a player in
417 team three (Magenta).
421 viewing angle when spawning
423 void spawnfunc_info_player_team3()
425 self.team = COLOR_TEAM3; // purple
426 spawnfunc_info_player_deathmatch();
430 /*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
431 CTF Starting point for a player in
436 viewing angle when spawning
438 void spawnfunc_info_player_team4()
440 self.team = COLOR_TEAM4; // yellow
441 spawnfunc_info_player_deathmatch();
447 /*QUAKED spawnfunc_item_flag_team1 (0 0.5 0.8) (-48 -48 -37) (48 48 37)
448 CTF flag for team one (Red).
449 Multiple are allowed.
453 Angle the flag will point
456 model to use, note this needs red and blue as skins 0 and 1
457 (default models/ctf/flag.md3)
459 sound played when flag is picked up
460 (default ctf/take.wav)
462 sound played when flag is returned by a teammate
463 (default ctf/return.wav)
465 sound played when flag is captured
466 (default ctf/redcapture.wav)
468 sound played when flag is lost in the field and respawns itself
469 (default ctf/respawn.wav)
472 void spawnfunc_item_flag_team1()
480 //if(!cvar("teamplay"))
481 // cvar_set("teamplay", "3");
483 // link flag into ctf_worldflaglist
484 self.ctf_worldflagnext = ctf_worldflaglist;
485 ctf_worldflaglist = self;
487 self.classname = "item_flag_team";
488 self.team = COLOR_TEAM1; // color 4 team (red)
489 self.items = IT_KEY2; // gold key (redish enough)
490 self.netname = "^1RED^7 flag";
491 self.target = "###item###";
493 if(self.spawnflags & 1)
496 self.model = "models/ctf/flag_red.md3";
498 self.noise = "ctf/take.wav";
500 self.noise1 = "ctf/return.wav";
502 self.noise2 = "ctf/redcapture.wav"; // blue team scores by capturing the red flag
504 self.noise3 = "ctf/respawn.wav";
505 precache_model (self.model);
506 setmodel (self, self.model); // precision set below
507 precache_sound (self.noise);
508 precache_sound (self.noise1);
509 precache_sound (self.noise2);
510 precache_sound (self.noise3);
511 setsize(self, '-16 -16 -37', '16 16 37');
512 setorigin(self, self.origin + '0 0 37');
513 self.nextthink = time + 0.2; // start after doors etc
514 self.think = place_flag;
518 //if(!self.glow_size)
519 // self.glow_size = 50;
521 self.effects = self.effects | EF_FULLBRIGHT | EF_LOWPRECISION;
525 waypoint_spawnforitem(self);
527 WaypointSprite_SpawnFixed("redbase", self.origin + '0 0 37', self, sprite);
530 /*QUAKED spawnfunc_item_flag_team2 (0 0.5 0.8) (-48 -48 -24) (48 48 64)
531 CTF flag for team two (Blue).
532 Multiple are allowed.
536 Angle the flag will point
539 model to use, note this needs red and blue as skins 0 and 1
540 (default models/ctf/flag.md3)
542 sound played when flag is picked up
543 (default ctf/take.wav)
545 sound played when flag is returned by a teammate
546 (default ctf/return.wav)
548 sound played when flag is captured
549 (default ctf/bluecapture.wav)
551 sound played when flag is lost in the field and respawns itself
552 (default ctf/respawn.wav)
555 void spawnfunc_item_flag_team2()
562 //if(!cvar("teamplay"))
563 // cvar_set("teamplay", "3");
565 // link flag into ctf_worldflaglist
566 self.ctf_worldflagnext = ctf_worldflaglist;
567 ctf_worldflaglist = self;
569 self.classname = "item_flag_team";
570 self.team = COLOR_TEAM2; // color 13 team (blue)
571 self.items = IT_KEY1; // silver key (bluish enough)
572 self.netname = "^4BLUE^7 flag";
573 self.target = "###item###";
575 if(self.spawnflags & 1)
578 self.model = "models/ctf/flag_blue.md3";
580 self.noise = "ctf/take.wav";
582 self.noise1 = "ctf/return.wav";
584 self.noise2 = "ctf/bluecapture.wav"; // red team scores by capturing the blue flag
586 self.noise3 = "ctf/respawn.wav";
587 precache_model (self.model);
588 setmodel (self, self.model); // precision set below
589 precache_sound (self.noise);
590 precache_sound (self.noise1);
591 precache_sound (self.noise2);
592 precache_sound (self.noise3);
593 setsize(self, '-16 -16 -37', '16 16 37');
594 setorigin(self, self.origin + '0 0 37');
595 self.nextthink = time + 0.2; // start after doors etc
596 self.think = place_flag;
600 //if(!self.glow_size)
601 // self.glow_size = 50;
603 self.effects = self.effects | EF_FULLBRIGHT | EF_LOWPRECISION;
607 waypoint_spawnforitem(self);
609 WaypointSprite_SpawnFixed("bluebase", self.origin + '0 0 37', self, sprite);
613 /*QUAKED spawnfunc_ctf_team (0 .5 .8) (-16 -16 -24) (16 16 32)
614 Team declaration for CTF gameplay, this allows you to decide what team
615 names and control point models are used in your map.
617 Note: If you use spawnfunc_ctf_team entities you must define at least 2! However, unlike
618 domination, you don't need to make a blank one too.
622 Name of the team (for example Red, Blue, Green, Yellow, Life, Death, Offense, Defense, etc)
624 Scoreboard color of the team (for example 4 is red and 13 is blue)
628 void spawnfunc_ctf_team()
635 self.classname = "ctf_team";
636 self.team = self.cnt + 1;
639 // code from here on is just to support maps that don't have control point and team entities
640 void ctf_spawnteam (string teamname, float teamcolor)
642 local entity oldself;
645 self.classname = "ctf_team";
646 self.netname = teamname;
647 self.cnt = teamcolor;
649 spawnfunc_ctf_team();
654 // spawn some default teams if the map is not set up for ctf
655 void ctf_spawnteams()
659 numteams = 2;//cvar("g_ctf_default_teams");
661 ctf_spawnteam("Red", COLOR_TEAM1 - 1);
662 ctf_spawnteam("Blue", COLOR_TEAM2 - 1);
665 void ctf_delayedinit()
667 // if no teams are found, spawn defaults
668 if (find(world, classname, "ctf_team") == world)
676 InitializeEntity(world, ctf_delayedinit, INITPRIO_GAMETYPE);
677 flagcaptimerecord = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time")));
680 void ctf_setstatus2(entity flag, float shift)
682 if (flag.cnt == FLAG_CARRY)
683 if (flag.owner == self)
684 self.items |= shift * 3;
686 self.items |= shift * 1;
687 else if (flag.cnt == FLAG_DROPPED)
688 self.items |= shift * 2;
697 self.items = self.items - (self.items & IT_RED_FLAG_TAKEN);
698 self.items = self.items - (self.items & IT_RED_FLAG_LOST);
699 self.items = self.items - (self.items & IT_BLUE_FLAG_TAKEN);
700 self.items = self.items - (self.items & IT_BLUE_FLAG_LOST);
704 float redflags, blueflags;
709 for (flag = ctf_worldflaglist; flag; flag = flag.ctf_worldflagnext) if(flag.cnt != FLAG_BASE)
711 if(flag.team == COLOR_TEAM1)
713 else if(flag.team == COLOR_TEAM2)
717 // blinking magic: if there is more than one flag, show one of these in a clever way
719 redflags = mod(floor(time * redflags * 0.75), redflags);
721 blueflags = mod(floor(time * blueflags * 0.75), blueflags);
723 for (flag = ctf_worldflaglist; flag; flag = flag.ctf_worldflagnext) if(flag.cnt != FLAG_BASE)
725 if(flag.team == COLOR_TEAM1)
727 if(--redflags == -1) // happens exactly once (redflags is in 0..count-1, and will --'ed count times)
728 ctf_setstatus2(flag, IT_RED_FLAG_TAKEN);
730 else if(flag.team == COLOR_TEAM2)
732 if(--blueflags == -1) // happens exactly once
733 ctf_setstatus2(flag, IT_BLUE_FLAG_TAKEN);
739 entity(float cteam) ctf_team_has_commander =
742 if(cteam != COLOR_TEAM1 || cteam != COLOR_TEAM2)
745 FOR_EACH_REALPLAYER(pl) {
746 if(pl.team == cteam && pl.iscommander) {
753 void(entity e, float st) ctf_setstate =
759 void(float cteam) ctf_new_commander =
764 FOR_EACH_REALPLAYER(pl) {
765 if(pl.team == cteam) {
766 if(pl.iscommander) { // don't reassign if alreay there
769 if(plmax == world || plmax.frags < pl.frags) <<<<<<<<<<<<<<<<< BROKEN in new scoring system
774 bprint(strcat(ColoredTeamName(cteam), " Team has no Commander!\n"));
778 plmax.iscommander = TRUE;
779 ctf_setstate(plmax, 3);
780 sprint(plmax, "^3You're the commander now!\n");
781 centerprint(plmax, "^3You're the commander now!\n");
784 void() ctf_clientconnect =
786 self.iscommander = FALSE;
788 if(!self.team || self.classname != "player") {
789 ctf_setstate(self, -1);
791 ctf_setstate(self, 0);
793 self.team_saved = self.team;
795 if(self.team != 0 && self.classname == "player" && !ctf_team_has_commander(self.team)) {
796 ctf_new_commander(self.team);
800 void() ctf_playerchanged =
802 if(!self.team || self.classname != "player") {
803 ctf_setstate(self, -1);
804 } else if(self.ctf_state < 0 && self.classname == "player") {
805 ctf_setstate(self, 0);
808 if(self.iscommander &&
809 (self.classname != "player" || self.team != self.team_saved)
812 self.iscommander = FALSE;
813 if(self.classname == "player")
814 ctf_setstate(self, 0);
816 ctf_setstate(self, -1);
817 ctf_new_commander(self.team_saved);
820 self.team_saved = self.team;
822 ctf_new_commander(self.team);
825 void() ctf_clientdisconnect =
829 ctf_new_commander(self.team);
833 entity GetPlayer(string);
834 float() ctf_clientcommand =
837 if(argv(0) == "order") {
839 sprint(self, "This command is not supported in this gamemode.\n");
842 if(!self.iscommander) {
843 sprint(self, "^1You are not the commander!\n");
847 sprint(self, "Usage: order #player status - (playernumber as in status)\n");
850 e = GetPlayer(argv(1));
852 sprint(self, "Invalid player.\nUsage: order #player status - (playernumber as in status)\n");
855 if(e.team != self.team) {
856 sprint(self, "^3You can only give orders to your own team!\n");
859 if(argv(2) == "attack") {
860 sprint(self, strcat("Ordering ", e.netname, " to attack!\n"));
861 sprint(e, "^1Attack!\n");
862 centerprint(e, "^7You've been ordered to^9\n^1Attack!\n");
864 } else if(argv(2) == "defend") {
865 sprint(self, strcat("Ordering ", e.netname, " to defend!\n"));
866 sprint(e, "^Defend!\n");
867 centerprint(e, "^7You've been ordered to^9\n^2Defend!\n");
870 sprint(self, "^7Invalid command, use ^3attack^7, or ^3defend^7.\n");