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, float penalty)
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");
135 UpdateFrags(p, -cvar("g_ctf_flagscore_pickup"));
136 PlayerScore_Add(p, SP_CTF_PICKUPS, -1);
139 if(cvar("g_ctf_flagpenalty_drop"))
140 UpdateFrags(p, -cvar("g_ctf_flagpenalty_drop"));
142 //if(e.enemy && e.enemy != e)
143 //UpdateFrags(e.enemy, cvar("g_ctf_flagscore_kill"));
144 WaypointSprite_DetachCarrier(p);
145 LogCTF("dropped", p.team, p.flagcarried);
147 setattachment(e, world, "");
149 if (p.flagcarried == e)
150 p.flagcarried = world;
153 e.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
154 e.solid = SOLID_TRIGGER;
155 e.movetype = MOVETYPE_TOSS;
156 // setsize(e, '-16 -16 0', '16 16 74');
157 setorigin(e, p.origin - '0 0 24' + '0 0 37');
158 e.cnt = FLAG_DROPPED;
159 e.velocity = '0 0 300';
160 e.pain_finished = time + cvar("g_ctf_flag_returntime");//30;
162 trace_startsolid = FALSE;
163 tracebox(e.origin, e.mins, e.maxs, e.origin, TRUE, e);
165 dprint("FLAG FALLTHROUGH will happen SOON\n");
170 if(self.delay > time)
172 self.delay = time + self.t_width;
173 if(self.nextthink > self.delay)
174 self.nextthink = self.delay;
176 self.frame = self.frame + 1;
177 if(self.frame > self.t_length)
185 self.nextthink = time + 0.1;
189 if(self.speedrunning)
190 if(self.cnt == FLAG_CARRY)
193 if(flagcaptimerecord)
194 if(time >= self.flagpickuptime + flagcaptimerecord)
196 bprint("The ", self.netname, " became impatient after ", ftos_decimals(flagcaptimerecord, 2), " seconds and returned itself\n");
198 self.owner.impulse = 77; // returning!
208 if (self.cnt == FLAG_BASE)
211 if (self.cnt == FLAG_DROPPED)
213 // flag fallthrough? FIXME remove this if bug is really fixed now
214 if(self.origin_z < -131072)
216 dprint("FLAG FALLTHROUGH just happened\n");
217 self.pain_finished = 0;
219 setattachment(self, world, "");
220 if (time > self.pain_finished)
222 bprint("The ", self.netname, " has returned to base\n");
223 sound (self, CHAN_TRIGGER, self.noise3, VOL_BASE, ATTN_NONE);
224 LogCTF("returned", self.team, world);
231 if (e.classname != "player" || (e.deadflag) || (e.flagcarried != self))
233 dprint("CANNOT HAPPEN - player dead and STILL had a flag!\n");
246 local string s, s0, h0, h1;
247 if (other.classname != "player")
249 if (other.health < 1) // ignore dead players
252 if (self.cnt == FLAG_CARRY)
255 if (self.cnt == FLAG_BASE)
256 if (other.team == self.team)
257 if (other.flagcarried) // he's got a flag
258 if (other.flagcarried.team != self.team) // capture
260 if (other.flagcarried == world)
264 t = time - other.flagcarried.flagpickuptime;
265 s = ftos_decimals(t, 2);
266 s0 = ftos_decimals(flagcaptimerecord, 2);
267 h0 = db_get(ServerProgsDB, strcat(GetMapname(), "/captimerecord/netname"));
272 h0 = strcat(h0, "^7's"); // h0: display text for previous netname
273 if (flagcaptimerecord == 0)
275 bprint(other.netname, "^7 captured the ", other.flagcarried.netname, " in ", s, " seconds\n");
276 flagcaptimerecord = t;
277 db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time"), ftos(t));
278 db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/netname"), h1);
279 GameLogEcho(strcat(":recordset:", ftos(other.playerid), ":", ftos(t)));
281 else if (t < flagcaptimerecord)
283 bprint(other.netname, "^7 captured the ", other.flagcarried.netname, " in ", s, ", breaking ", strcat(h0, " previous record of ", s0, " seconds\n"));
284 flagcaptimerecord = t;
285 db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time"), ftos(t));
286 db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/netname"), h1);
287 GameLogEcho(strcat(":recordset:", ftos(other.playerid), ":", ftos(t)));
291 bprint(other.netname, "^7 captured the ", other.flagcarried.netname, " in ", s, ", failing to break ", strcat(h0, " record of ", s0, " seconds\n"));
294 PlayerTeamScore_Add(other, SP_CTF_CAPS, ST_CTF_CAPS, 1);
295 LogCTF("capture", other.flagcarried.team, other);
296 // give credit to the individual player
297 UpdateFrags(other, cvar("g_ctf_flagscore_capture"));
299 // give credit to all players of the team (rewards large teams)
300 // NOTE: this defaults to 0
301 FOR_EACH_PLAYER(head)
302 if (head.team == self.team)
303 UpdateFrags(head, cvar("g_ctf_flagscore_capture_team"));
305 sound (other, CHAN_AUTO, self.noise2, VOL_BASE, ATTN_NONE);
306 WaypointSprite_DetachCarrier(other);
307 if(self.speedrunning)
308 FakeTimeLimit(other, -1);
309 RegenFlag (other.flagcarried);
310 other.flagcarried = world;
311 other.next_take_time = time + 1;
313 if (self.cnt == FLAG_BASE)
314 if (other.team == COLOR_TEAM1 || other.team == COLOR_TEAM2) // only red and blue team can steal flags
315 if (other.team != self.team)
316 if (!other.flagcarried)
318 if (other.next_take_time > time)
321 self.flagpickuptime = time; // used for timing runs
322 self.speedrunning = other.speedrunning; // if speedrunning, flag will self-return and teleport the owner back after the record
323 if(other.speedrunning)
324 if(flagcaptimerecord)
325 FakeTimeLimit(other, time + flagcaptimerecord);
326 self.solid = SOLID_NOT;
327 setorigin(self, self.origin); // relink
329 other.flagcarried = self;
330 self.cnt = FLAG_CARRY;
331 self.angles = '0 0 0';
332 bprint(other.netname, "^7 got the ", self.netname, "\n");
333 UpdateFrags(other, cvar("g_ctf_flagscore_pickup"));
334 PlayerScore_Add(other, SP_CTF_PICKUPS, 1);
335 LogCTF("steal", self.team, other);
336 sound (other, CHAN_AUTO, self.noise, VOL_BASE, ATTN_NONE);
338 FOR_EACH_PLAYER(player)
339 if(player.team == self.team)
340 centerprint(player, "The enemy got your flag! Retrieve it!");
342 self.movetype = MOVETYPE_NONE;
343 setorigin(self, FLAG_CARRY_POS);
344 setattachment(self, other, "");
345 WaypointSprite_AttachCarrier("flagcarrier", other);
350 if (self.cnt == FLAG_DROPPED)
352 self.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
353 if (other.team == self.team || (other.team != COLOR_TEAM1 && other.team != COLOR_TEAM2))
356 bprint(other.netname, "^7 returned the ", self.netname, "\n");
357 if (other.team == COLOR_TEAM1 || other.team == COLOR_TEAM2)
358 UpdateFrags(other, cvar("g_ctf_flagscore_return"));
360 UpdateFrags(other, cvar("g_ctf_flagscore_return_rogue"));
361 PlayerScore_Add(other, SP_CTF_RETURNS, 1);
362 LogCTF("return", self.team, other);
363 sound (other, CHAN_AUTO, self.noise1, VOL_BASE, ATTN_NONE);
366 else if (!other.flagcarried)
369 self.solid = SOLID_NOT;
370 setorigin(self, self.origin); // relink
372 other.flagcarried = self;
373 self.cnt = FLAG_CARRY;
374 bprint(other.netname, "^7 picked up the ", self.netname, "\n");
375 UpdateFrags(other, cvar("g_ctf_flagscore_pickup"));
376 LogCTF("pickup", self.team, other);
377 sound (other, CHAN_AUTO, self.noise, VOL_BASE, ATTN_NONE);
379 FOR_EACH_PLAYER(player)
380 if(player.team == self.team)
381 centerprint(player, "The enemy got your flag! Retrieve it!");
383 self.movetype = MOVETYPE_NONE; // flag must have MOVETYPE_NONE here, otherwise it will drop through the floor...
384 setorigin(self, FLAG_CARRY_POS);
385 setattachment(self, other, "");
386 WaypointSprite_AttachCarrier("flagcarrier", other);
391 /*QUAKED spawnfunc_info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
392 CTF Starting point for a player
397 viewing angle when spawning
399 void spawnfunc_info_player_team1()
401 self.team = COLOR_TEAM1; // red
402 spawnfunc_info_player_deathmatch();
404 //self.team = 4;self.classname = "info_player_start";spawnfunc_info_player_start();};
406 /*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
407 CTF Starting point for a player in
412 viewing angle when spawning
414 void spawnfunc_info_player_team2()
416 self.team = COLOR_TEAM2; // blue
417 spawnfunc_info_player_deathmatch();
419 //self.team = 13;self.classname = "info_player_start";spawnfunc_info_player_start();};
421 /*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
422 CTF Starting point for a player in
423 team three (Magenta).
427 viewing angle when spawning
429 void spawnfunc_info_player_team3()
431 self.team = COLOR_TEAM3; // purple
432 spawnfunc_info_player_deathmatch();
436 /*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
437 CTF Starting point for a player in
442 viewing angle when spawning
444 void spawnfunc_info_player_team4()
446 self.team = COLOR_TEAM4; // yellow
447 spawnfunc_info_player_deathmatch();
453 /*QUAKED spawnfunc_item_flag_team1 (0 0.5 0.8) (-48 -48 -37) (48 48 37)
454 CTF flag for team one (Red).
455 Multiple are allowed.
459 Angle the flag will point
462 model to use, note this needs red and blue as skins 0 and 1
463 (default models/ctf/flag.md3)
465 sound played when flag is picked up
466 (default ctf/take.wav)
468 sound played when flag is returned by a teammate
469 (default ctf/return.wav)
471 sound played when flag is captured
472 (default ctf/redcapture.wav)
474 sound played when flag is lost in the field and respawns itself
475 (default ctf/respawn.wav)
478 void spawnfunc_item_flag_team1()
486 //if(!cvar("teamplay"))
487 // cvar_set("teamplay", "3");
489 // link flag into ctf_worldflaglist
490 self.ctf_worldflagnext = ctf_worldflaglist;
491 ctf_worldflaglist = self;
493 self.classname = "item_flag_team";
494 self.team = COLOR_TEAM1; // color 4 team (red)
495 self.items = IT_KEY2; // gold key (redish enough)
496 self.netname = "^1RED^7 flag";
497 self.target = "###item###";
499 if(self.spawnflags & 1)
502 self.model = "models/ctf/flag_red.md3";
504 self.noise = "ctf/take.wav";
506 self.noise1 = "ctf/return.wav";
508 self.noise2 = "ctf/redcapture.wav"; // blue team scores by capturing the red flag
510 self.noise3 = "ctf/respawn.wav";
511 precache_model (self.model);
512 setmodel (self, self.model); // precision set below
513 precache_sound (self.noise);
514 precache_sound (self.noise1);
515 precache_sound (self.noise2);
516 precache_sound (self.noise3);
517 setsize(self, '-16 -16 -37', '16 16 37');
518 setorigin(self, self.origin + '0 0 37');
519 self.nextthink = time + 0.2; // start after doors etc
520 self.think = place_flag;
524 //if(!self.glow_size)
525 // self.glow_size = 50;
527 self.effects = self.effects | EF_FULLBRIGHT | EF_LOWPRECISION;
531 waypoint_spawnforitem(self);
533 WaypointSprite_SpawnFixed("redbase", self.origin + '0 0 37', self, sprite);
536 /*QUAKED spawnfunc_item_flag_team2 (0 0.5 0.8) (-48 -48 -24) (48 48 64)
537 CTF flag for team two (Blue).
538 Multiple are allowed.
542 Angle the flag will point
545 model to use, note this needs red and blue as skins 0 and 1
546 (default models/ctf/flag.md3)
548 sound played when flag is picked up
549 (default ctf/take.wav)
551 sound played when flag is returned by a teammate
552 (default ctf/return.wav)
554 sound played when flag is captured
555 (default ctf/bluecapture.wav)
557 sound played when flag is lost in the field and respawns itself
558 (default ctf/respawn.wav)
561 void spawnfunc_item_flag_team2()
568 //if(!cvar("teamplay"))
569 // cvar_set("teamplay", "3");
571 // link flag into ctf_worldflaglist
572 self.ctf_worldflagnext = ctf_worldflaglist;
573 ctf_worldflaglist = self;
575 self.classname = "item_flag_team";
576 self.team = COLOR_TEAM2; // color 13 team (blue)
577 self.items = IT_KEY1; // silver key (bluish enough)
578 self.netname = "^4BLUE^7 flag";
579 self.target = "###item###";
581 if(self.spawnflags & 1)
584 self.model = "models/ctf/flag_blue.md3";
586 self.noise = "ctf/take.wav";
588 self.noise1 = "ctf/return.wav";
590 self.noise2 = "ctf/bluecapture.wav"; // red team scores by capturing the blue flag
592 self.noise3 = "ctf/respawn.wav";
593 precache_model (self.model);
594 setmodel (self, self.model); // precision set below
595 precache_sound (self.noise);
596 precache_sound (self.noise1);
597 precache_sound (self.noise2);
598 precache_sound (self.noise3);
599 setsize(self, '-16 -16 -37', '16 16 37');
600 setorigin(self, self.origin + '0 0 37');
601 self.nextthink = time + 0.2; // start after doors etc
602 self.think = place_flag;
606 //if(!self.glow_size)
607 // self.glow_size = 50;
609 self.effects = self.effects | EF_FULLBRIGHT | EF_LOWPRECISION;
613 waypoint_spawnforitem(self);
615 WaypointSprite_SpawnFixed("bluebase", self.origin + '0 0 37', self, sprite);
619 /*QUAKED spawnfunc_ctf_team (0 .5 .8) (-16 -16 -24) (16 16 32)
620 Team declaration for CTF gameplay, this allows you to decide what team
621 names and control point models are used in your map.
623 Note: If you use spawnfunc_ctf_team entities you must define at least 2! However, unlike
624 domination, you don't need to make a blank one too.
628 Name of the team (for example Red, Blue, Green, Yellow, Life, Death, Offense, Defense, etc)
630 Scoreboard color of the team (for example 4 is red and 13 is blue)
634 void spawnfunc_ctf_team()
641 self.classname = "ctf_team";
642 self.team = self.cnt + 1;
645 // code from here on is just to support maps that don't have control point and team entities
646 void ctf_spawnteam (string teamname, float teamcolor)
648 local entity oldself;
651 self.classname = "ctf_team";
652 self.netname = teamname;
653 self.cnt = teamcolor;
655 spawnfunc_ctf_team();
660 // spawn some default teams if the map is not set up for ctf
661 void ctf_spawnteams()
665 numteams = 2;//cvar("g_ctf_default_teams");
667 ctf_spawnteam("Red", COLOR_TEAM1 - 1);
668 ctf_spawnteam("Blue", COLOR_TEAM2 - 1);
671 void ctf_delayedinit()
673 // if no teams are found, spawn defaults
674 if (find(world, classname, "ctf_team") == world)
682 InitializeEntity(world, ctf_delayedinit, INITPRIO_GAMETYPE);
683 flagcaptimerecord = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time")));
686 void ctf_setstatus2(entity flag, float shift)
688 if (flag.cnt == FLAG_CARRY)
689 if (flag.owner == self)
690 self.items |= shift * 3;
692 self.items |= shift * 1;
693 else if (flag.cnt == FLAG_DROPPED)
694 self.items |= shift * 2;
703 self.items = self.items - (self.items & IT_RED_FLAG_TAKEN);
704 self.items = self.items - (self.items & IT_RED_FLAG_LOST);
705 self.items = self.items - (self.items & IT_BLUE_FLAG_TAKEN);
706 self.items = self.items - (self.items & IT_BLUE_FLAG_LOST);
710 float redflags, blueflags;
715 for (flag = ctf_worldflaglist; flag; flag = flag.ctf_worldflagnext) if(flag.cnt != FLAG_BASE)
717 if(flag.team == COLOR_TEAM1)
719 else if(flag.team == COLOR_TEAM2)
723 // blinking magic: if there is more than one flag, show one of these in a clever way
725 redflags = mod(floor(time * redflags * 0.75), redflags);
727 blueflags = mod(floor(time * blueflags * 0.75), blueflags);
729 for (flag = ctf_worldflaglist; flag; flag = flag.ctf_worldflagnext) if(flag.cnt != FLAG_BASE)
731 if(flag.team == COLOR_TEAM1)
733 if(--redflags == -1) // happens exactly once (redflags is in 0..count-1, and will --'ed count times)
734 ctf_setstatus2(flag, IT_RED_FLAG_TAKEN);
736 else if(flag.team == COLOR_TEAM2)
738 if(--blueflags == -1) // happens exactly once
739 ctf_setstatus2(flag, IT_BLUE_FLAG_TAKEN);
745 entity(float cteam) ctf_team_has_commander =
748 if(cteam != COLOR_TEAM1 || cteam != COLOR_TEAM2)
751 FOR_EACH_REALPLAYER(pl) {
752 if(pl.team == cteam && pl.iscommander) {
759 void(entity e, float st) ctf_setstate =
765 void(float cteam) ctf_new_commander =
770 FOR_EACH_REALPLAYER(pl) {
771 if(pl.team == cteam) {
772 if(pl.iscommander) { // don't reassign if alreay there
775 if(plmax == world || plmax.frags < pl.frags) <<<<<<<<<<<<<<<<< BROKEN in new scoring system
780 bprint(strcat(ColoredTeamName(cteam), " Team has no Commander!\n"));
784 plmax.iscommander = TRUE;
785 ctf_setstate(plmax, 3);
786 sprint(plmax, "^3You're the commander now!\n");
787 centerprint(plmax, "^3You're the commander now!\n");
790 void() ctf_clientconnect =
792 self.iscommander = FALSE;
794 if(!self.team || self.classname != "player") {
795 ctf_setstate(self, -1);
797 ctf_setstate(self, 0);
799 self.team_saved = self.team;
801 if(self.team != 0 && self.classname == "player" && !ctf_team_has_commander(self.team)) {
802 ctf_new_commander(self.team);
806 void() ctf_playerchanged =
808 if(!self.team || self.classname != "player") {
809 ctf_setstate(self, -1);
810 } else if(self.ctf_state < 0 && self.classname == "player") {
811 ctf_setstate(self, 0);
814 if(self.iscommander &&
815 (self.classname != "player" || self.team != self.team_saved)
818 self.iscommander = FALSE;
819 if(self.classname == "player")
820 ctf_setstate(self, 0);
822 ctf_setstate(self, -1);
823 ctf_new_commander(self.team_saved);
826 self.team_saved = self.team;
828 ctf_new_commander(self.team);
831 void() ctf_clientdisconnect =
835 ctf_new_commander(self.team);
839 entity GetPlayer(string);
840 float() ctf_clientcommand =
843 if(argv(0) == "order") {
845 sprint(self, "This command is not supported in this gamemode.\n");
848 if(!self.iscommander) {
849 sprint(self, "^1You are not the commander!\n");
853 sprint(self, "Usage: order #player status - (playernumber as in status)\n");
856 e = GetPlayer(argv(1));
858 sprint(self, "Invalid player.\nUsage: order #player status - (playernumber as in status)\n");
861 if(e.team != self.team) {
862 sprint(self, "^3You can only give orders to your own team!\n");
865 if(argv(2) == "attack") {
866 sprint(self, strcat("Ordering ", e.netname, " to attack!\n"));
867 sprint(e, "^1Attack!\n");
868 centerprint(e, "^7You've been ordered to^9\n^1Attack!\n");
870 } else if(argv(2) == "defend") {
871 sprint(self, strcat("Ordering ", e.netname, " to defend!\n"));
872 sprint(e, "^Defend!\n");
873 centerprint(e, "^7You've been ordered to^9\n^2Defend!\n");
876 sprint(self, "^7Invalid command, use ^3attack^7, or ^3defend^7.\n");