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;
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));
76 GameLogEcho(s, FALSE);
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)
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");
131 WaypointSprite_DetachCarrier(p);
132 LogCTF("dropped", p.team, p.flagcarried);
134 setattachment(e, world, "");
136 if (p.flagcarried == e)
137 p.flagcarried = world;
140 e.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
141 e.solid = SOLID_TRIGGER;
142 e.movetype = MOVETYPE_TOSS;
143 // setsize(e, '-16 -16 0', '16 16 74');
144 setorigin(e, p.origin - '0 0 24' + '0 0 37');
145 e.cnt = FLAG_DROPPED;
146 e.velocity = '0 0 300';
147 e.pain_finished = time + cvar("g_ctf_flag_returntime");//30;
149 trace_startsolid = FALSE;
150 tracebox(e.origin, e.mins, e.maxs, e.origin, TRUE, e);
152 dprint("FLAG FALLTHROUGH will happen SOON\n");
157 if(self.delay > time)
159 self.delay = time + self.t_width;
160 if(self.nextthink > self.delay)
161 self.nextthink = self.delay;
163 self.frame = self.frame + 1;
164 if(self.frame > self.t_length)
172 self.nextthink = time + 0.1;
176 if(self.speedrunning)
177 if(self.cnt == FLAG_CARRY)
180 if(flagcaptimerecord)
181 if(time >= self.flagpickuptime + flagcaptimerecord)
183 bprint("The ", self.netname, " became impatient after ", ftos_decimals(flagcaptimerecord, 2), " seconds and returned itself\n");
185 self.owner.impulse = 77; // returning!
195 if (self.cnt == FLAG_BASE)
198 if (self.cnt == FLAG_DROPPED)
200 // flag fallthrough? FIXME remove this if bug is really fixed now
201 if(self.origin_z < -131072)
203 dprint("FLAG FALLTHROUGH just happened\n");
204 self.pain_finished = 0;
206 setattachment(self, world, "");
207 if (time > self.pain_finished)
209 bprint("The ", self.netname, " has returned to base\n");
210 sound (e, CHAN_AUTO, self.noise3, 1, ATTN_NONE);
211 LogCTF("returned", self.team, world);
218 if (e.classname != "player" || (e.deadflag) || (e.flagcarried != self))
220 dprint("CANNOT HAPPEN - player dead and STILL had a flag!\n");
233 local string s, s0, h0, h1;
234 if (other.classname != "player")
236 if (other.health < 1) // ignore dead players
239 if (self.cnt == FLAG_CARRY)
242 if (self.cnt == FLAG_BASE)
243 if (other.team == self.team)
244 if (other.flagcarried) // he's got a flag
245 if (other.flagcarried.team != self.team) // capture
247 if (other.flagcarried == world)
251 t = time - other.flagcarried.flagpickuptime;
252 s = ftos_decimals(t, 2);
253 s0 = ftos_decimals(flagcaptimerecord, 2);
254 h0 = db_get(ServerProgsDB, strcat(GetMapname(), "/captimerecord/netname"));
259 h0 = strcat(h0, "^7's"); // h0: display text for previous netname
260 if (flagcaptimerecord == 0)
262 bprint(other.netname, "^7 captured the ", other.flagcarried.netname, " in ", s, " seconds\n");
263 flagcaptimerecord = t;
264 db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time"), ftos(t));
265 db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/netname"), h1);
267 else if (t < flagcaptimerecord)
269 bprint(other.netname, "^7 captured the ", other.flagcarried.netname, " in ", s, ", breaking ", strcat(h0, " previous record of ", s0, " seconds\n"));
270 flagcaptimerecord = t;
271 db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time"), ftos(t));
272 db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/netname"), h1);
276 bprint(other.netname, "^7 captured the ", other.flagcarried.netname, " in ", s, ", failing to break ", strcat(h0, " record of ", s0, " seconds\n"));
279 LogCTF("capture", other.flagcarried.team, other);
280 // give credit to the individual player
281 UpdateFrags(other, cvar("g_ctf_flagscore_capture"));
283 // give credit to all players of the team (rewards large teams)
284 // NOTE: this defaults to 0
285 FOR_EACH_PLAYER(head)
286 if (head.team == self.team)
287 UpdateFrags(head, cvar("g_ctf_flagscore_capture_team"));
289 sound (self, CHAN_AUTO, self.noise2, 1, ATTN_NONE);
290 WaypointSprite_DetachCarrier(other);
291 if(self.speedrunning)
292 FakeTimeLimit(other, -1);
293 RegenFlag (other.flagcarried);
294 other.flagcarried = world;
295 other.next_take_time = time + 1;
297 if (self.cnt == FLAG_BASE)
298 if (other.team == COLOR_TEAM1 || other.team == COLOR_TEAM2) // only red and blue team can steal flags
299 if (other.team != self.team)
300 if (!other.flagcarried)
302 if (other.next_take_time > time)
305 self.flagpickuptime = time; // used for timing runs
306 self.speedrunning = other.speedrunning; // if speedrunning, flag will self-return and teleport the owner back after the record
307 if(other.speedrunning)
308 if(flagcaptimerecord)
309 FakeTimeLimit(other, time + flagcaptimerecord);
310 self.solid = SOLID_NOT;
311 setorigin(self, self.origin); // relink
313 other.flagcarried = self;
314 self.cnt = FLAG_CARRY;
315 self.angles = '0 0 0';
316 bprint(other.netname, "^7 got the ", self.netname, "\n");
317 UpdateFrags(other, cvar("g_ctf_flagscore_pickup"));
318 LogCTF("steal", self.team, other);
319 sound (self, CHAN_AUTO, self.noise, 1, ATTN_NONE);
321 FOR_EACH_PLAYER(player)
322 if(player.team == self.team)
323 centerprint(player, "The enemy got your flag! Retrieve it!");
325 self.movetype = MOVETYPE_NONE;
326 setorigin(self, FLAG_CARRY_POS);
327 setattachment(self, other, "");
328 WaypointSprite_AttachCarrier("flagcarrier", other);
333 if (self.cnt == FLAG_DROPPED)
335 self.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
336 if (other.team == self.team || (other.team != COLOR_TEAM1 && other.team != COLOR_TEAM2))
339 bprint(other.netname, "^7 returned the ", self.netname, "\n");
340 if (other.team == COLOR_TEAM1 || other.team == COLOR_TEAM2)
341 UpdateFrags(other, cvar("g_ctf_flagscore_return"));
343 UpdateFrags(other, cvar("g_ctf_flagscore_return_rogue"));
344 LogCTF("return", self.team, other);
345 sound (self, CHAN_AUTO, self.noise1, 1, ATTN_NONE);
348 else if (!other.flagcarried)
351 self.solid = SOLID_NOT;
352 setorigin(self, self.origin); // relink
354 other.flagcarried = self;
355 self.cnt = FLAG_CARRY;
356 bprint(other.netname, "^7 picked up the ", self.netname, "\n");
357 UpdateFrags(other, cvar("g_ctf_flagscore_pickup"));
358 LogCTF("pickup", self.team, other);
359 sound (self, CHAN_AUTO, self.noise, 1, ATTN_NONE);
361 FOR_EACH_PLAYER(player)
362 if(player.team == self.team)
363 centerprint(player, "The enemy got your flag! Retrieve it!");
365 self.movetype = MOVETYPE_NONE; // flag must have MOVETYPE_NONE here, otherwise it will drop through the floor...
366 setorigin(self, FLAG_CARRY_POS);
367 setattachment(self, other, "");
368 WaypointSprite_AttachCarrier("flagcarrier", other);
373 /*QUAKED spawnfunc_info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
374 CTF Starting point for a player
379 viewing angle when spawning
381 void spawnfunc_info_player_team1()
383 self.team = COLOR_TEAM1; // red
384 spawnfunc_info_player_deathmatch();
386 //self.team = 4;self.classname = "info_player_start";spawnfunc_info_player_start();};
388 /*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
389 CTF Starting point for a player in
394 viewing angle when spawning
396 void spawnfunc_info_player_team2()
398 self.team = COLOR_TEAM2; // blue
399 spawnfunc_info_player_deathmatch();
401 //self.team = 13;self.classname = "info_player_start";spawnfunc_info_player_start();};
403 /*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
404 CTF Starting point for a player in
405 team three (Magenta).
409 viewing angle when spawning
411 void spawnfunc_info_player_team3()
413 self.team = COLOR_TEAM3; // purple
414 spawnfunc_info_player_deathmatch();
418 /*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
419 CTF Starting point for a player in
424 viewing angle when spawning
426 void spawnfunc_info_player_team4()
428 self.team = COLOR_TEAM4; // yellow
429 spawnfunc_info_player_deathmatch();
435 /*QUAKED spawnfunc_item_flag_team1 (0 0.5 0.8) (-48 -48 -37) (48 48 37)
436 CTF flag for team one (Red).
437 Multiple are allowed.
441 Angle the flag will point
444 model to use, note this needs red and blue as skins 0 and 1
445 (default models/ctf/flag.md3)
447 sound played when flag is picked up
448 (default ctf/take.wav)
450 sound played when flag is returned by a teammate
451 (default ctf/return.wav)
453 sound played when flag is captured
454 (default ctf/redcapture.wav)
456 sound played when flag is lost in the field and respawns itself
457 (default ctf/respawn.wav)
460 void spawnfunc_item_flag_team1()
468 //if(!cvar("teamplay"))
469 // cvar_set("teamplay", "3");
471 // link flag into ctf_worldflaglist
472 self.ctf_worldflagnext = ctf_worldflaglist;
473 ctf_worldflaglist = self;
475 self.classname = "item_flag_team";
476 self.team = COLOR_TEAM1; // color 4 team (red)
477 self.items = IT_KEY2; // gold key (redish enough)
478 self.netname = "^1RED^7 flag";
479 self.target = "###item###";
481 if(self.spawnflags & 1)
484 self.model = "models/ctf/flag_red.md3";
486 self.noise = "ctf/take.wav";
488 self.noise1 = "ctf/return.wav";
490 self.noise2 = "ctf/redcapture.wav"; // blue team scores by capturing the red flag
492 self.noise3 = "ctf/respawn.wav";
493 precache_model (self.model);
494 setmodel (self, self.model); // precision set below
495 precache_sound (self.noise);
496 precache_sound (self.noise1);
497 precache_sound (self.noise2);
498 precache_sound (self.noise3);
499 setsize(self, '-16 -16 -37', '16 16 37');
500 setorigin(self, self.origin + '0 0 37');
501 self.nextthink = time + 0.2; // start after doors etc
502 self.think = place_flag;
506 //if(!self.glow_size)
507 // self.glow_size = 50;
509 self.effects = self.effects | EF_FULLBRIGHT | EF_LOWPRECISION;
513 waypoint_spawnforitem(self);
515 WaypointSprite_SpawnFixed("redbase", self.origin + '0 0 37', self, sprite);
518 /*QUAKED spawnfunc_item_flag_team2 (0 0.5 0.8) (-48 -48 -24) (48 48 64)
519 CTF flag for team two (Blue).
520 Multiple are allowed.
524 Angle the flag will point
527 model to use, note this needs red and blue as skins 0 and 1
528 (default models/ctf/flag.md3)
530 sound played when flag is picked up
531 (default ctf/take.wav)
533 sound played when flag is returned by a teammate
534 (default ctf/return.wav)
536 sound played when flag is captured
537 (default ctf/bluecapture.wav)
539 sound played when flag is lost in the field and respawns itself
540 (default ctf/respawn.wav)
543 void spawnfunc_item_flag_team2()
550 //if(!cvar("teamplay"))
551 // cvar_set("teamplay", "3");
553 // link flag into ctf_worldflaglist
554 self.ctf_worldflagnext = ctf_worldflaglist;
555 ctf_worldflaglist = self;
557 self.classname = "item_flag_team";
558 self.team = COLOR_TEAM2; // color 13 team (blue)
559 self.items = IT_KEY1; // silver key (bluish enough)
560 self.netname = "^4BLUE^7 flag";
561 self.target = "###item###";
563 if(self.spawnflags & 1)
566 self.model = "models/ctf/flag_blue.md3";
568 self.noise = "ctf/take.wav";
570 self.noise1 = "ctf/return.wav";
572 self.noise2 = "ctf/bluecapture.wav"; // red team scores by capturing the blue flag
574 self.noise3 = "ctf/respawn.wav";
575 precache_model (self.model);
576 setmodel (self, self.model); // precision set below
577 precache_sound (self.noise);
578 precache_sound (self.noise1);
579 precache_sound (self.noise2);
580 precache_sound (self.noise3);
581 setsize(self, '-16 -16 -37', '16 16 37');
582 setorigin(self, self.origin + '0 0 37');
583 self.nextthink = time + 0.2; // start after doors etc
584 self.think = place_flag;
588 //if(!self.glow_size)
589 // self.glow_size = 50;
591 self.effects = self.effects | EF_FULLBRIGHT | EF_LOWPRECISION;
595 waypoint_spawnforitem(self);
597 WaypointSprite_SpawnFixed("bluebase", self.origin + '0 0 37', self, sprite);
601 /*QUAKED spawnfunc_ctf_team (0 .5 .8) (-16 -16 -24) (16 16 32)
602 Team declaration for CTF gameplay, this allows you to decide what team
603 names and control point models are used in your map.
605 Note: If you use spawnfunc_ctf_team entities you must define at least 2! However, unlike
606 domination, you don't need to make a blank one too.
610 Name of the team (for example Red, Blue, Green, Yellow, Life, Death, Offense, Defense, etc)
612 Scoreboard color of the team (for example 4 is red and 13 is blue)
616 void spawnfunc_ctf_team()
623 self.classname = "ctf_team";
624 self.team = self.cnt + 1;
627 // code from here on is just to support maps that don't have control point and team entities
628 void ctf_spawnteam (string teamname, float teamcolor)
630 local entity oldself;
633 self.classname = "ctf_team";
634 self.netname = teamname;
635 self.cnt = teamcolor;
637 spawnfunc_ctf_team();
642 // spawn some default teams if the map is not set up for ctf
643 void ctf_spawnteams()
647 numteams = 2;//cvar("g_ctf_default_teams");
649 ctf_spawnteam("Red", COLOR_TEAM1 - 1);
650 ctf_spawnteam("Blue", COLOR_TEAM2 - 1);
653 void ctf_delayedinit()
655 self.think = SUB_Remove;
656 self.nextthink = time;
657 // if no teams are found, spawn defaults
658 if (find(world, classname, "ctf_team") == world)
666 e.think = ctf_delayedinit;
667 e.nextthink = time + 0.1;
668 flagcaptimerecord = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time")));
671 void ctf_setstatus2(entity flag, float shift)
673 if (flag.cnt == FLAG_CARRY)
674 if (flag.owner == self)
675 self.items |= shift * 3;
677 self.items |= shift * 1;
678 else if (flag.cnt == FLAG_DROPPED)
679 self.items |= shift * 2;
688 self.items = self.items - (self.items & IT_RED_FLAG_TAKEN);
689 self.items = self.items - (self.items & IT_RED_FLAG_LOST);
690 self.items = self.items - (self.items & IT_BLUE_FLAG_TAKEN);
691 self.items = self.items - (self.items & IT_BLUE_FLAG_LOST);
695 float redflags, blueflags;
700 for (flag = ctf_worldflaglist; flag; flag = flag.ctf_worldflagnext) if(flag.cnt != FLAG_BASE)
702 if(flag.team == COLOR_TEAM1)
704 else if(flag.team == COLOR_TEAM2)
708 // blinking magic: if there is more than one flag, show one of these in a clever way
710 redflags = mod(floor(time * redflags * 0.75), redflags);
712 blueflags = mod(floor(time * blueflags * 0.75), blueflags);
714 for (flag = ctf_worldflaglist; flag; flag = flag.ctf_worldflagnext) if(flag.cnt != FLAG_BASE)
716 if(flag.team == COLOR_TEAM1)
718 if(redflags-- == 0) // happens exactly once
719 ctf_setstatus2(flag, IT_RED_FLAG_TAKEN);
721 else if(flag.team == COLOR_TEAM2)
723 if(blueflags-- == 0) // happens exactly once
724 ctf_setstatus2(flag, IT_BLUE_FLAG_TAKEN);