]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/mutators/gamemode_ctf.qc
Fix balancing with drop penalties; add assist points for previous carriers
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / gamemode_ctf.qc
index 2c268730143797755bb8f89f95142991b4b55ab0..15e62c5b7e9f51a845859ecbd8d361ffa424116b 100644 (file)
 // ================================================================
 //  Official capture the flag game mode coding, reworked by Samual
-//  Last updated: March 27th, 2011
+//  Last updated: March 30th, 2012
 // ================================================================
 
-// Flag constants 
-#define FLAG_MIN (PL_MIN + '0 0 -13')
-#define FLAG_MAX (PL_MAX + '0 0 -13')
-#define FLAG_CARRY_POS '-15 0 7'
+void ctf_FakeTimeLimit(entity e, float t)
+{
+       msg_entity = e;
+       WriteByte(MSG_ONE, 3); // svc_updatestat
+       WriteByte(MSG_ONE, 236); // STAT_TIMELIMIT
+       if(t < 0)
+               WriteCoord(MSG_ONE, autocvar_timelimit);
+       else
+               WriteCoord(MSG_ONE, (t + 1) / 60);
+}
+
+void ctf_EventLog(string mode, float flagteam, entity actor) // use an alias for easy changing and quick editing later
+{
+       if(autocvar_sv_eventlog)
+               GameLogEcho(strcat(":ctf:", mode, ":", ftos(flagteam), ((actor != world) ? (strcat(":", ftos(actor.playerid))) : "")));
+}
 
-.entity bot_basewaypoint; // Flag waypointsprite
-.entity wps_flagbase; 
-.entity wps_flagcarrier;
-.entity wps_flagdropped;
+string ctf_CaptureRecord(entity flag, entity player)
+{
+       float cap_time, cap_record, success;
+       string cap_message, refername;
+       
+       if((autocvar_g_ctf_captimerecord_always) || (player_count - currentbots)) 
+       {
+               cap_record = ctf_captimerecord;
+               cap_time = (time - flag.ctf_pickuptime);
 
-entity ctf_worldflaglist; // CTF flags in the map
-.entity ctf_worldflagnext;
+               refername = db_get(ServerProgsDB, strcat(GetMapname(), "/captimerecord/netname"));
+               refername = ((refername == player.netname) ? "their" : strcat(refername, "^7's"));
 
-.float ctf_dropperid; // Don't allow spam of dropping the flag
-.float ctf_droptime;
-.float ctf_status; // status of the flag (FLAG_BASE, FLAG_DROPPED, FLAG_CARRY declared globally)
+               if(!ctf_captimerecord) 
+                       { cap_message = strcat(" in ", ftos_decimals(cap_time, 2), " seconds"); success = TRUE; }
+               else if(cap_time < cap_record) 
+                       { cap_message = strcat(" in ", ftos_decimals(cap_time, 2), " seconds, breaking ", refername, " previous record of ", ftos_decimals(cap_record, 2), " seconds"); success = TRUE; }
+               else
+                       { cap_message = strcat(" in ", ftos_decimals(cap_time, 2), " seconds, failing to break ", refername, " record of ", ftos_decimals(cap_record, 2), " seconds"); success = FALSE; }
 
-// Delay between when the person can pick up a flag // replace with .wait? 
-.float next_take_time;
+               if(success) 
+               {
+                       ctf_captimerecord = cap_time;
+                       db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time"), ftos(cap_time));
+                       db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/netname"), player.netname);
+                       write_recordmarker(player, (time - cap_time), cap_time); 
+               } 
+       }
+       
+       return cap_message;
+}
+
+void ctf_FlagcarrierWaypoints(entity player)
+{
+       WaypointSprite_Spawn("flagcarrier", 0, 0, player, FLAG_WAYPOINT_OFFSET, world, player.team, player, wps_flagcarrier, TRUE, RADARICON_FLAG, WPCOLOR_FLAGCARRIER(player.team));
+       WaypointSprite_UpdateMaxHealth(player.wps_flagcarrier, '1 0 0' * healtharmor_maxdamage(start_health, start_armorvalue, autocvar_g_balance_armor_blockpercent) * 2);
+       WaypointSprite_UpdateHealth(player.wps_flagcarrier, '1 0 0' * healtharmor_maxdamage(player.health, player.armorvalue, autocvar_g_balance_armor_blockpercent));
+       WaypointSprite_UpdateTeamRadar(player.wps_flagcarrier, RADARICON_FLAGCARRIER, WPCOLOR_FLAGCARRIER(player.team));
+}
 
-// Record time for capturing the flag
-float flagcaptimerecord;
-.float flagpickuptime;
 
-// CaptureShield: If the player is too bad to be allowed to capture, shield them from taking the flag.
-.float ctf_captureshielded; // set to 1 if the player is too bad to be allowed to capture
-float captureshield_min_negscore; // punish at -20 points
-float captureshield_max_ratio; // punish at most 30% of each team
-float captureshield_force; // push force of the shield
+// =======================
+// CaptureShield Functions 
+// =======================
 
+float ctf_CaptureShield_CheckStatus(entity p) 
+{
+       float s, se;
+       entity e;
+       float players_worseeq, players_total;
 
-// declare functions so they can be used in any order in the file
-void ctf_FlagTouch(void);
-void ctf_FlagThink(void);
-void ctf_SetupFlag(float, entity);
-void ctf_RespawnFlag(entity);
-float ctf_CaptureShield_CheckStatus(entity);
-void ctf_CaptureShield_Update(entity, float);
-float ctf_CaptureShield_Customize(void);
-void ctf_CaptureShield_Touch(void);
-void ctf_CaptureShield_Spawn(entity);
+       if(ctf_captureshield_max_ratio <= 0)
+               return FALSE;
 
+       s = PlayerScore_Add(p, SP_SCORE, 0);
+       if(s >= -ctf_captureshield_min_negscore)
+               return FALSE;
 
-// ==================
-// Misc CTF functions
-// ==================
+       players_total = players_worseeq = 0;
+       FOR_EACH_PLAYER(e)
+       {
+               if(IsDifferentTeam(e, p))
+                       continue;
+               se = PlayerScore_Add(e, SP_SCORE, 0);
+               if(se <= s)
+                       ++players_worseeq;
+               ++players_total;
+       }
 
-float ctf_ReadScore(string parameter) // make this obsolete
+       // player is in the worse half, if >= half the players are better than him, or consequently, if < half of the players are worse
+       // use this rule here
+       
+       if(players_worseeq >= players_total * ctf_captureshield_max_ratio)
+               return FALSE;
+
+       return TRUE;
+}
+
+void ctf_CaptureShield_Update(entity player, float wanted_status)
 {
-       if(g_ctf_win_mode != 2)
-               return cvar(strcat("g_ctf_personal", parameter));
-       else
-               return cvar(strcat("g_ctf_flag", parameter));
+       float updated_status = ctf_CaptureShield_CheckStatus(player);
+       if((wanted_status == player.ctf_captureshielded) && (updated_status != wanted_status)) // 0: shield only, 1: unshield only
+       {
+               if(updated_status) // TODO csqc notifier for this // Samual: How?
+                       Send_CSQC_Centerprint_Generic(player, CPID_CTF_CAPTURESHIELD, "^3You are now ^4shielded^3 from the flag\n^3for ^1too many unsuccessful attempts^3 to capture.\n\n^3Make some defensive scores before trying again.", 5, 0);
+               else
+                       Send_CSQC_Centerprint_Generic(player, CPID_CTF_CAPTURESHIELD, "^3You are now free.\n\n^3Feel free to ^1try to capture^3 the flag again\n^3if you think you will succeed.", 5, 0);
+                       
+               player.ctf_captureshielded = updated_status;
+       }
 }
 
-void ctf_FakeTimeLimit(entity e, float t)
+float ctf_CaptureShield_Customize()
 {
-       msg_entity = e;
-       WriteByte(MSG_ONE, 3); // svc_updatestat
-       WriteByte(MSG_ONE, 236); // STAT_TIMELIMIT
-       if(t < 0)
-               WriteCoord(MSG_ONE, autocvar_timelimit);
-       else
-               WriteCoord(MSG_ONE, (t + 1) / 60);
+       if(!other.ctf_captureshielded) { return FALSE; }
+       if(!IsDifferentTeam(self, other)) { return FALSE; }
+       
+       return TRUE;
 }
 
-void ctf_EventLog(string mode, float flagteam, entity actor)
+void ctf_CaptureShield_Touch()
 {
-       string s;
-       if(!autocvar_sv_eventlog)
-               return;
-       s = strcat(":ctf:", mode);
-       s = strcat(s, ":", ftos(flagteam));
-       if(actor != world)
-               s = strcat(s, ":", ftos(actor.playerid));
-       GameLogEcho(s);
+       if(!other.ctf_captureshielded) { return; }
+       if(!IsDifferentTeam(self, other)) { return; }
+       
+       vector mymid = (self.absmin + self.absmax) * 0.5;
+       vector othermid = (other.absmin + other.absmax) * 0.5;
+
+       Damage(other, self, self, 0, DEATH_HURTTRIGGER, mymid, normalize(othermid - mymid) * ctf_captureshield_force);
+       Send_CSQC_Centerprint_Generic(other, CPID_CTF_CAPTURESHIELD, "^3You are ^4shielded^3 from the flag\n^3for ^1too many unsuccessful attempts^3 to capture.\n\n^3Get some defensive scores before trying again.", 5, 0);
 }
 
-void ctf_CaptureShockwave(vector org)
+void ctf_CaptureShield_Spawn(entity flag)
 {
-       shockwave_spawn("models/ctf/shockwavetransring.md3", org - '0 0 15', -0.8, 0, 1);
+       entity shield = spawn();
+       
+       shield.enemy = self;
+       shield.team = self.team;
+       shield.touch = ctf_CaptureShield_Touch;
+       shield.customizeentityforclient = ctf_CaptureShield_Customize;
+       shield.classname = "ctf_captureshield";
+       shield.effects = EF_ADDITIVE;
+       shield.movetype = MOVETYPE_NOCLIP;
+       shield.solid = SOLID_TRIGGER;
+       shield.avelocity = '7 0 11';
+       shield.scale = 0.5;
+       
+       setorigin(shield, self.origin);
+       setmodel(shield, "models/ctf/shield.md3");
+       setsize(shield, shield.scale * shield.mins, shield.scale * shield.maxs);
 }
 
-void ctf_CreateBaseWaypoints(entity flag, float teamnumber)
+
+// ====================
+// Drop/Pass/Throw Code
+// ====================
+
+void ctf_Handle_Drop(entity flag, entity player, float droptype)
 {
-       // for bots
-       waypoint_spawnforitem_force(flag, flag.origin);
-       flag.nearestwaypointtimeout = 0; // activate waypointing again
-       flag.bot_basewaypoint = flag.nearestwaypoint;
+       // declarations
+       player = (player ? player : flag.pass_sender);
 
-       // waypointsprites
-       WaypointSprite_SpawnFixed(((teamnumber) ? "redbase" : "bluebase"), flag.origin + '0 0 61', flag, wps_flagbase);
-       WaypointSprite_UpdateTeamRadar(flag.wps_flagbase, RADARICON_FLAG, colormapPaletteColor(((teamnumber) ? COLOR_TEAM1 : COLOR_TEAM2) - 1, FALSE));
+       // main
+       flag.movetype = MOVETYPE_TOSS;
+       flag.takedamage = DAMAGE_YES;
+       flag.health = flag.max_flag_health;
+       flag.ctf_droptime = time;
+       flag.ctf_dropper = player;
+       flag.ctf_status = FLAG_DROPPED;
+       
+       // messages and sounds
+       Send_KillNotification(player.netname, flag.netname, "", INFO_LOSTFLAG, MSG_INFO);
+       sound(flag, CH_TRIGGER, flag.snd_flag_dropped, VOL_BASE, ATTN_NONE);
+       ctf_EventLog("dropped", player.team, player);
+
+       // scoring
+       PlayerTeamScore_AddScore(player, -autocvar_g_ctf_score_penalty_drop);   
+       PlayerScore_Add(player, SP_CTF_DROPS, 1);
+       
+       // waypoints
+       if(autocvar_g_ctf_flag_dropped_waypoint)
+               WaypointSprite_Spawn("flagdropped", 0, 0, flag, FLAG_WAYPOINT_OFFSET, world, ((autocvar_g_ctf_flag_dropped_waypoint == 2) ? 0 : player.team), flag, wps_flagdropped, TRUE, RADARICON_FLAG, WPCOLOR_DROPPEDFLAG(flag.team));
+
+       if(autocvar_g_ctf_flag_return_time || (autocvar_g_ctf_flag_return_damage && autocvar_g_ctf_flag_health))
+       {
+               WaypointSprite_UpdateMaxHealth(flag.wps_flagdropped, flag.max_flag_health);
+               WaypointSprite_UpdateHealth(flag.wps_flagdropped, flag.health);
+       }
+       
+       player.throw_antispam = time + autocvar_g_ctf_pass_wait;
+       
+       if(droptype == DROP_PASS)
+       {
+               flag.pass_sender = world;
+               flag.pass_target = world;
+       }
 }
 
-void ctf_SetStatus_ForType(entity flag, float type)
+void ctf_Handle_Retrieve(entity flag, entity player)
 {
-       if(flag.cnt ==  FLAG_CARRY)
+       entity tmp_player; // temporary entity which the FOR_EACH_PLAYER loop uses to scan players
+       entity sender = flag.pass_sender;
+       
+       // transfer flag to player
+       flag.owner = player;
+       flag.owner.flagcarried = flag;
+       
+       // reset flag
+       setattachment(flag, player, "");
+       setorigin(flag, FLAG_CARRY_OFFSET);
+       flag.movetype = MOVETYPE_NONE;
+       flag.takedamage = DAMAGE_NO;
+       flag.solid = SOLID_NOT;
+       flag.ctf_carrier = player;
+       flag.ctf_status = FLAG_CARRY;
+
+       // messages and sounds
+       sound(player, CH_TRIGGER, flag.snd_flag_pass, VOL_BASE, ATTN_NORM);
+       ctf_EventLog("recieve", flag.team, player);
+       
+       FOR_EACH_REALPLAYER(tmp_player)
        {
-               if(flag.owner == self)
-                       self.items |= type * 3; // carrying: self is currently carrying the flag
-               else 
-                       self.items |= type * 1; // taken: someone on self's team is carrying the flag
+               if(tmp_player == sender)
+                       centerprint(tmp_player, strcat("You passed the ", flag.netname, " to ", player.netname));
+               else if(tmp_player == player)
+                       centerprint(tmp_player, strcat("You recieved the ", flag.netname, " from ", sender.netname));
+               else if(!IsDifferentTeam(tmp_player, sender))
+                       centerprint(tmp_player, strcat(sender.netname, " passed the ", flag.netname, " to ", player.netname));
        }
-       else if(flag.cnt == FLAG_DROPPED) 
-               self.items |= type * 2; // lost: the flag is dropped somewhere on the map
+       
+       // create new waypoint
+       ctf_FlagcarrierWaypoints(player);
+       
+       sender.throw_antispam = time + autocvar_g_ctf_pass_wait;
+       player.throw_antispam = sender.throw_antispam;
+       
+       flag.pass_sender = world;
+       flag.pass_target = world;
 }
 
-void ctf_SetStatus() // re-write this in some less shitty way
+void ctf_Handle_Throw(entity player, entity reciever, float droptype)
 {
-       // declarations 
-       float redflags, blueflags;
-       local entity flag;
+       entity flag = player.flagcarried;
+       vector targ_origin;
        
-       // initially clear items so they can be set as necessary later.
-       self.items &~= (IT_RED_FLAG_TAKEN | IT_RED_FLAG_LOST | IT_BLUE_FLAG_TAKEN | IT_BLUE_FLAG_LOST | IT_CTF_SHIELDED);
+       if(!flag) { return; }
+       if((droptype == DROP_PASS) && !reciever) { return; }
+       
+       if(flag.speedrunning) { ctf_RespawnFlag(flag); return; }
+       
+       // reset the flag
+       setattachment(flag, world, "");
+       setorigin(flag, player.origin + FLAG_DROP_OFFSET);
+       flag.owner.flagcarried = world;
+       flag.owner = world;
+       flag.solid = SOLID_TRIGGER;
+       flag.ctf_droptime = time;
+       
+       flag.flags = FL_ITEM | FL_NOTARGET; // clear FL_ONGROUND for MOVETYPE_TOSS
+       
+       switch(droptype)
+       {
+               case DROP_PASS:
+               {
+                       WarpZone_RefSys_MakeSameRefSys(flag, player);
+                       targ_origin = WarpZone_RefSys_TransformOrigin(reciever, flag, (0.5 * (reciever.absmin + reciever.absmax)));
+                       flag.velocity = (normalize(targ_origin - player.origin) * autocvar_g_ctf_pass_velocity);
+                       break;
+               }
+               
+               case DROP_THROW:
+               {
+                       makevectors((player.v_angle_y * '0 1 0') + (player.v_angle_x * '0.5 0 0'));
+                       flag.velocity = W_CalculateProjectileVelocity(player.velocity, ('0 0 200' + (v_forward * autocvar_g_ctf_drop_velocity)), FALSE);
+                       break;
+               }
+               
+               case DROP_RESET:
+               {
+                       flag.velocity = '0 0 0'; // do nothing
+                       break;
+               }
+               
+               default:
+               case DROP_NORMAL:
+               {
+                       flag.velocity = W_CalculateProjectileVelocity(player.velocity, ('0 0 200' + ('0 100 0' * crandom()) + ('100 0 0' * crandom())), FALSE);
+                       break;
+               }
+       }
+       
+       switch(droptype)
+       {
+               case DROP_PASS:
+               {
+                       // main
+                       flag.movetype = MOVETYPE_FLY;
+                       flag.takedamage = DAMAGE_NO;
+                       flag.pass_sender = player;
+                       flag.pass_target = reciever;
+                       flag.ctf_status = FLAG_PASSING;
+                       
+                       // other
+                       sound(player, CH_TRIGGER, flag.snd_flag_touch, VOL_BASE, ATTN_NORM);
+                       WarpZone_TrailParticles(world, particleeffectnum(flag.passeffect), targ_origin, player.origin);
+                       ctf_EventLog("pass", flag.team, player);
+                       break;
+               }
 
-       // item for stopping players from capturing the flag too often
-       if(self.ctf_captureshielded)
-               self.items |= IT_CTF_SHIELDED;
+               case DROP_RESET: 
+               {
+                       // do nothing
+                       break;
+               }
+               
+               default:
+               case DROP_THROW:
+               case DROP_NORMAL:
+               {
+                       ctf_Handle_Drop(flag, player, droptype);
+                       break;
+               }
+       }
+
+       // kill old waypointsprite
+       WaypointSprite_Ping(player.wps_flagcarrier);
+       WaypointSprite_Kill(player.wps_flagcarrier);
+       
+       if(player.wps_enemyflagcarrier)
+               WaypointSprite_Kill(player.wps_enemyflagcarrier);
+       
+       // captureshield
+       ctf_CaptureShield_Update(player, 0); // shield player from picking up flag
+}
+
+
+// ==============
+// Event Handlers
+// ==============
+
+void ctf_Handle_Capture(entity flag, entity toucher, float capturetype)
+{
+       entity enemy_flag = ((capturetype == CAPTURE_NORMAL) ? toucher.flagcarried : toucher);
+       entity player = ((capturetype == CAPTURE_NORMAL) ? toucher : enemy_flag.ctf_dropper);
+       float old_time, new_time; 
+       
+       if not(player) { return; } // without someone to give the reward to, we can't possibly cap
+       
+       // messages and sounds
+       Send_KillNotification(player.netname, enemy_flag.netname, ctf_CaptureRecord(enemy_flag, player), INFO_CAPTUREFLAG, MSG_INFO);
+       sound(player, CH_TRIGGER, flag.snd_flag_capture, VOL_BASE, ATTN_NONE);
+       
+       switch(capturetype)
+       {
+               case CAPTURE_NORMAL: ctf_EventLog("capture", enemy_flag.team, player); break;
+               case CAPTURE_DROPPED: ctf_EventLog("droppedcapture", enemy_flag.team, player); break;
+               default: break;
+       }
+       
+       // scoring
+       PlayerTeamScore_AddScore(player, autocvar_g_ctf_score_capture);
+       PlayerTeamScore_Add(player, SP_CTF_CAPS, ST_CTF_CAPS, 1);
+
+       old_time = PlayerScore_Add(player, SP_CTF_CAPTIME, 0);
+       new_time = TIME_ENCODE(time - enemy_flag.ctf_pickuptime);
+       if(!old_time || new_time < old_time)
+               PlayerScore_Add(player, SP_CTF_CAPTIME, new_time - old_time);
+
+       // effects
+       if(autocvar_g_ctf_flag_capture_effects) 
+       {
+               pointparticles(particleeffectnum((player.team == COLOR_TEAM1) ? "red_ground_quake" : "blue_ground_quake"), flag.origin, '0 0 0', 1);
+               shockwave_spawn("models/ctf/shockwavetransring.md3", flag.origin - '0 0 15', -0.8, 0, 1);
+       }
+
+       // other
+       if(capturetype == CAPTURE_NORMAL)
+       {
+               WaypointSprite_Kill(player.wps_flagcarrier);
+               if(flag.speedrunning) { ctf_FakeTimeLimit(player, -1); }
+               if(enemy_flag.ctf_dropper) { PlayerTeamScore_AddScore(enemy_flag.ctf_dropper, autocvar_g_ctf_score_capture_assist); }
+       }
+       
+       // reset the flag
+       player.next_take_time = time + autocvar_g_ctf_flag_collect_delay;
+       ctf_RespawnFlag(enemy_flag);
+}
+
+void ctf_Handle_Return(entity flag, entity player)
+{
+       // messages and sounds
+       //centerprint(player, strcat("You returned the ", flag.netname));
+       Send_KillNotification(player.netname, flag.netname, "", INFO_RETURNFLAG, MSG_INFO);
+       sound(player, CH_TRIGGER, flag.snd_flag_returned, VOL_BASE, ATTN_NONE);
+       ctf_EventLog("return", flag.team, player);
+
+       // scoring
+       PlayerTeamScore_AddScore(player, autocvar_g_ctf_score_return); // reward for return
+       PlayerScore_Add(player, SP_CTF_RETURNS, 1); // add to count of returns
+
+       TeamScore_AddToTeam(flag.team, ST_SCORE, -autocvar_g_ctf_score_penalty_returned); // punish the team who was last carrying it
+       
+       if(flag.ctf_dropper) 
+       {
+               PlayerScore_Add(flag.ctf_dropper, SP_SCORE, -autocvar_g_ctf_score_penalty_returned); // punish the player who dropped the flag
+               ctf_CaptureShield_Update(flag.ctf_dropper, 0); // shield player from picking up flag 
+               flag.ctf_dropper.next_take_time = time + autocvar_g_ctf_flag_collect_delay; // set next take time
+       }
+       
+       // reset the flag
+       ctf_RespawnFlag(flag);
+}
 
-       // figure out what flags we already own
-       for (flag = ctf_worldflaglist; flag; flag = flag.ctf_worldflagnext) if(flag.cnt != FLAG_BASE)
+void ctf_Handle_Pickup(entity flag, entity player, float pickuptype)
+{
+       // declarations
+       entity tmp_player; // temporary entity which the FOR_EACH_PLAYER loop uses to scan players
+       string verbosename; // holds the name of the player OR no name at all for printing in the centerprints
+       float pickup_dropped_score; // used to calculate dropped pickup score
+       
+       // attach the flag to the player
+       flag.owner = player;
+       player.flagcarried = flag;
+       setattachment(flag, player, "");
+       setorigin(flag, FLAG_CARRY_OFFSET);
+       
+       // flag setup
+       flag.movetype = MOVETYPE_NONE;
+       flag.takedamage = DAMAGE_NO;
+       flag.solid = SOLID_NOT;
+       flag.angles = '0 0 0';
+       flag.ctf_carrier = player;
+       flag.ctf_status = FLAG_CARRY;
+       
+       switch(pickuptype)
        {
-               if(flag.items & IT_KEY2) // blue
-                       ++redflags;
-               else if(flag.items & IT_KEY1) // red
-                       ++blueflags;
+               case PICKUP_BASE: flag.ctf_pickuptime = time; break; // used for timing runs
+               case PICKUP_DROPPED: flag.health = flag.max_flag_health; break; // reset health/return timelimit
+               default: break;
        }
 
-       // blinking magic: if there is more than one flag, show one of these in a clever way // wtf?
-       if(redflags)
-               redflags = mod(floor(time * redflags * 0.75), redflags);
+       // messages and sounds
+       Send_KillNotification (player.netname, flag.netname, "", INFO_GOTFLAG, MSG_INFO);
+       sound(player, CH_TRIGGER, flag.snd_flag_taken, VOL_BASE, ATTN_NONE);
+       verbosename = ((autocvar_g_ctf_flag_pickup_verbosename) ? strcat(Team_ColorCode(player.team), "(^7", player.netname, Team_ColorCode(player.team), ") ") : "");
+       
+       FOR_EACH_REALPLAYER(tmp_player)
+       {
+               if(tmp_player == player)
+                       centerprint(tmp_player, strcat("You got the ", flag.netname, "!"));
+               else if(!IsDifferentTeam(tmp_player, player))
+                       centerprint(tmp_player, strcat("Your ", Team_ColorCode(player.team), "team mate ", verbosename, "^7got the flag! Protect them!"));
+               else if(!IsDifferentTeam(tmp_player, flag))
+                       centerprint(tmp_player, strcat("The ", Team_ColorCode(player.team), "enemy ", verbosename, "^7got your flag! Retrieve it!"));
+       }
                
-       if(blueflags)
-               blueflags = mod(floor(time * blueflags * 0.75), blueflags);
+       switch(pickuptype)
+       {
+               case PICKUP_BASE: ctf_EventLog("steal", flag.team, player); break;
+               case PICKUP_DROPPED: ctf_EventLog("pickup", flag.team, player); break;
+               default: break;
+       }
+       
+       // scoring
+       PlayerScore_Add(player, SP_CTF_PICKUPS, 1);
+       switch(pickuptype)
+       {               
+               case PICKUP_BASE:
+               {
+                       PlayerTeamScore_AddScore(player, autocvar_g_ctf_score_pickup_base);
+                       break;
+               }
+               
+               case PICKUP_DROPPED:
+               {
+                       pickup_dropped_score = (autocvar_g_ctf_flag_return_time ? bound(0, ((flag.ctf_droptime + autocvar_g_ctf_flag_return_time) - time) / autocvar_g_ctf_flag_return_time, 1) : 1);
+                       pickup_dropped_score = floor((autocvar_g_ctf_score_pickup_dropped_late * (1 - pickup_dropped_score) + autocvar_g_ctf_score_pickup_dropped_early * pickup_dropped_score) + 0.5);
+                       print("pickup_dropped_score is ", ftos(pickup_dropped_score), "\n");
+                       PlayerTeamScore_AddScore(player, pickup_dropped_score);
+                       break;
+               }
+               
+               default: break;
+       }
+       
+       // speedrunning
+       if(pickuptype == PICKUP_BASE)
+       {
+               flag.speedrunning = player.speedrunning; // if speedrunning, flag will flag-return and teleport the owner back after the record
+               if((player.speedrunning) && (ctf_captimerecord))
+                       ctf_FakeTimeLimit(player, time + ctf_captimerecord);
+       }
+               
+       // effects
+       if(autocvar_g_ctf_flag_pickup_effects)
+               pointparticles(particleeffectnum("smoke_ring"), 0.5 * (flag.absmin + flag.absmax), '0 0 0', 1);
+       
+       // waypoints 
+       if(pickuptype == PICKUP_DROPPED) { WaypointSprite_Kill(flag.wps_flagdropped); }
+       ctf_FlagcarrierWaypoints(player);
+       WaypointSprite_Ping(player.wps_flagcarrier);
+}
 
-       for (flag = ctf_worldflaglist; flag; flag = flag.ctf_worldflagnext) if(flag.cnt != FLAG_BASE)
+
+// ===================
+// Main Flag Functions
+// ===================
+
+void ctf_CheckFlagReturn(entity flag, float returntype)
+{
+       if(flag.wps_flagdropped) { WaypointSprite_UpdateHealth(flag.wps_flagdropped, flag.health); }
+       
+       if((flag.health <= 0) || (time >= flag.ctf_droptime + autocvar_g_ctf_flag_return_time))
        {
-               if(flag.items & IT_KEY2) // blue
+               switch(returntype)
+               {
+                       case RETURN_DROPPED: bprint("The ", flag.netname, " was dropped in the base and returned itself\n"); break;
+                       case RETURN_DAMAGE: bprint("The ", flag.netname, " was destroyed and returned to base\n"); break;
+                       case RETURN_SPEEDRUN: bprint("The ", flag.netname, " became impatient after ", ftos_decimals(ctf_captimerecord, 2), " seconds and returned itself\n"); break;
+                       case RETURN_NEEDKILL: bprint("The ", flag.netname, " fell somewhere it couldn't be reached and returned to base\n"); break;
+                       
+                       default:
+                       case RETURN_TIMEOUT:
+                               { bprint("The ", flag.netname, " has returned to base\n"); break; }
+               }
+               sound(flag, CH_TRIGGER, flag.snd_flag_respawn, VOL_BASE, ATTN_NONE);
+               ctf_EventLog("returned", flag.team, world);
+               ctf_RespawnFlag(flag);
+       }
+}
+
+void ctf_CheckStalemate(void)
+{
+       // declarations
+       float stale_red_flags, stale_blue_flags;
+       entity tmp_entity;
+
+       entity ctf_staleflaglist; // reset the list, we need to build the list each time this function runs
+
+       // build list of stale flags
+       for(tmp_entity = ctf_worldflaglist; tmp_entity; tmp_entity = tmp_entity.ctf_worldflagnext)
+       {
+               if(autocvar_g_ctf_flagcarrier_waypointforenemy_stalemate)
+               if(tmp_entity.ctf_status != FLAG_BASE)
+               if(time >= tmp_entity.ctf_pickuptime + autocvar_g_ctf_flagcarrier_waypointforenemy_stalemate)
+               {
+                       tmp_entity.ctf_staleflagnext = ctf_staleflaglist; // link flag into staleflaglist
+                       ctf_staleflaglist = tmp_entity;
+                       
+                       switch(tmp_entity.team)
+                       {
+                               case COLOR_TEAM1: ++stale_red_flags; break;
+                               case COLOR_TEAM2: ++stale_blue_flags; break;
+                       }
+               }
+       }
+
+       if(stale_red_flags && stale_blue_flags)
+               ctf_stalemate = TRUE;
+       else if(!stale_red_flags && !stale_blue_flags)
+               ctf_stalemate = FALSE;
+       
+       // if sufficient stalemate, then set up the waypointsprite and announce the stalemate if necessary
+       if(ctf_stalemate)
+       {
+               for(tmp_entity = ctf_staleflaglist; tmp_entity; tmp_entity = tmp_entity.ctf_staleflagnext)
+               {
+                       if((tmp_entity.owner) && (!tmp_entity.owner.wps_enemyflagcarrier))
+                               WaypointSprite_Spawn("enemyflagcarrier", 0, 0, tmp_entity.owner, FLAG_WAYPOINT_OFFSET, world, tmp_entity.team, tmp_entity.owner, wps_enemyflagcarrier, TRUE, RADARICON_FLAG, WPCOLOR_ENEMYFC(tmp_entity.owner.team));
+               }
+               
+               if not(wpforenemy_announced)
+               {
+                       FOR_EACH_REALPLAYER(tmp_entity)
+                               if(tmp_entity.flagcarried)
+                                       centerprint(tmp_entity, "Stalemate! Enemies can now see you on radar!");
+                               else
+                                       centerprint(tmp_entity, "Stalemate! Flag carriers can now be seen by enemies on radar!");
+                       
+                       wpforenemy_announced = TRUE;
+               }
+       }
+}
+
+void ctf_FlagDamage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
+{
+       if(ITEM_DAMAGE_NEEDKILL(deathtype))
+       {
+               // automatically kill the flag and return it
+               self.health = 0;
+               ctf_CheckFlagReturn(self, RETURN_NEEDKILL);
+               return;
+       }
+       if(autocvar_g_ctf_flag_return_damage) 
+       {
+               // reduce health and check if it should be returned
+               self.health = self.health - damage;
+               ctf_CheckFlagReturn(self, RETURN_DAMAGE);
+               return;
+       }
+}
+
+void ctf_FlagThink()
+{
+       // declarations
+       entity tmp_entity;
+
+       self.nextthink = time + FLAG_THINKRATE; // only 5 fps, more is unnecessary.
+
+       // captureshield
+       if(self == ctf_worldflaglist) // only for the first flag
+               FOR_EACH_CLIENT(tmp_entity)
+                       ctf_CaptureShield_Update(tmp_entity, 1); // release shield only
+
+       // sanity checks
+       if(self.mins != FLAG_MIN || self.maxs != FLAG_MAX) { // reset the flag boundaries in case it got squished
+               dprint("wtf the flag got squashed?\n");
+               tracebox(self.origin, FLAG_MIN, FLAG_MAX, self.origin, MOVE_NOMONSTERS, self);
+               if(!trace_startsolid) // can we resize it without getting stuck?
+                       setsize(self, FLAG_MIN, FLAG_MAX); }
+                       
+       switch(self.ctf_status) // reset flag angles in case warpzones adjust it
+       {
+               case FLAG_DROPPED:
+               case FLAG_PASSING:
+               {
+                       self.angles = '0 0 0';
+                       break;
+               }
+               
+               default: break;
+       }
+
+       // main think method
+       switch(self.ctf_status)
+       {       
+               case FLAG_BASE:
+               {
+                       if(autocvar_g_ctf_dropped_capture_radius)
+                       {
+                               for(tmp_entity = ctf_worldflaglist; tmp_entity; tmp_entity = tmp_entity.ctf_worldflagnext)
+                                       if(tmp_entity.ctf_status == FLAG_DROPPED)
+                                               if(vlen(self.origin - tmp_entity.origin) < autocvar_g_ctf_dropped_capture_radius)
+                                                       ctf_Handle_Capture(self, tmp_entity, CAPTURE_DROPPED);
+                       }
+                       return;
+               }
+               
+               case FLAG_DROPPED:
+               {
+                       if(autocvar_g_ctf_flag_dropped_floatinwater)
+                       {
+                               vector midpoint = ((self.absmin + self.absmax) * 0.5);
+                               if(pointcontents(midpoint) == CONTENT_WATER)
+                               {
+                                       self.velocity = self.velocity * 0.5;
+                                       
+                                       if(pointcontents(midpoint + FLAG_FLOAT_OFFSET) == CONTENT_WATER)
+                                               { self.velocity_z = autocvar_g_ctf_flag_dropped_floatinwater; }
+                                       else
+                                               { self.movetype = MOVETYPE_FLY; }
+                               }
+                               else if(self.movetype == MOVETYPE_FLY) { self.movetype = MOVETYPE_TOSS; }
+                       }
+                       if(autocvar_g_ctf_flag_return_dropped)
+                       {
+                               if((vlen(self.origin - self.ctf_spawnorigin) <= autocvar_g_ctf_flag_return_dropped) || (autocvar_g_ctf_flag_return_dropped == -1))
+                               {
+                                       self.health = 0;
+                                       ctf_CheckFlagReturn(self, RETURN_DROPPED);
+                                       return;
+                               }
+                       }
+                       if(autocvar_g_ctf_flag_return_time)
+                       {
+                               self.health -= ((self.max_flag_health / autocvar_g_ctf_flag_return_time) * FLAG_THINKRATE);
+                               ctf_CheckFlagReturn(self, RETURN_TIMEOUT);
+                               return;
+                       } 
+                       return;
+               }
+                       
+               case FLAG_CARRY:
+               {
+                       if(self.speedrunning && ctf_captimerecord && (time >= self.ctf_pickuptime + ctf_captimerecord)) 
+                       {
+                               self.health = 0;
+                               ctf_CheckFlagReturn(self, RETURN_SPEEDRUN);
+
+                               tmp_entity = self;
+                               self = self.owner;
+                               self.impulse = CHIMPULSE_SPEEDRUN; // move the player back to the waypoint they set
+                               ImpulseCommands();
+                               self = tmp_entity;
+                       }
+                       if(autocvar_g_ctf_flagcarrier_waypointforenemy_stalemate)
+                       {
+                               if(time >= wpforenemy_nextthink)
+                               {
+                                       ctf_CheckStalemate();
+                                       wpforenemy_nextthink = time + WPFE_THINKRATE; // waypoint for enemy think rate (to reduce unnecessary spam of this check)
+                               }
+                       }
+                       return;
+               }
+               
+               case FLAG_PASSING: // todo make work with warpzones
+               {
+                       vector targ_origin = ((self.pass_target.absmin + self.pass_target.absmax) * 0.5);
+                       vector old_targ_origin = targ_origin;
+                       targ_origin = WarpZone_RefSys_TransformOrigin(self.pass_target, self, targ_origin);
+                       WarpZone_TraceLine(self.origin, targ_origin, MOVE_NOMONSTERS, self);
+
+                       print(strcat("self: ", vtos(self.origin), ", old: ", vtos(old_targ_origin), " (", ftos(vlen(self.origin - old_targ_origin)), "qu)"), ", transformed: ", vtos(targ_origin), " (", ftos(vlen(self.origin - targ_origin)), "qu)", ".\n");
+                       
+                       if((self.pass_target.deadflag != DEAD_NO)
+                               || (vlen(self.origin - targ_origin) > autocvar_g_ctf_pass_radius)
+                               || ((trace_fraction < 1) && (trace_ent != self.pass_target))
+                               || (time > self.ctf_droptime + autocvar_g_ctf_pass_timelimit))
+                       {
+                               ctf_Handle_Drop(self, world, DROP_PASS);
+                       }
+                       else // still a viable target, go for it
+                       {
+                               vector desired_direction = normalize(targ_origin - self.origin);
+                               vector current_direction = normalize(self.velocity);
+
+                               self.velocity = (normalize(current_direction + (desired_direction * autocvar_g_ctf_pass_turnrate)) * autocvar_g_ctf_pass_velocity); 
+                       }
+                       return;
+               }
+
+               default: // this should never happen
+               {
+                       dprint("ctf_FlagThink(): Flag exists with no status?\n");
+                       return;
+               }
+       }
+}
+
+void ctf_FlagTouch()
+{
+       if(gameover) { return; }
+       
+       entity toucher = other;
+       
+       // automatically kill the flag and return it if it touched lava/slime/nodrop surfaces
+       if(ITEM_TOUCH_NEEDKILL())
+       {
+               self.health = 0;
+               ctf_CheckFlagReturn(self, RETURN_NEEDKILL);
+               return;
+       }
+       
+       // special touch behaviors
+       if(toucher.vehicle_flags & VHF_ISVEHICLE)
+       {
+               if(autocvar_g_ctf_allow_vehicle_touch)
+                       toucher = toucher.owner; // the player is actually the vehicle owner, not other
+               else
+                       return; // do nothing
+       }
+       else if(toucher.classname != "player") // The flag just touched an object, most likely the world
+       {
+               if(time > self.wait) // if we haven't in a while, play a sound/effect
+               {
+                       pointparticles(particleeffectnum(self.toucheffect), self.origin, '0 0 0', 1);
+                       sound(self, CH_TRIGGER, self.snd_flag_touch, VOL_BASE, ATTN_NORM);
+                       self.wait = time + FLAG_TOUCHRATE;
+               }
+               return;
+       }
+       else if(toucher.deadflag != DEAD_NO) { return; }
+
+       switch(self.ctf_status) 
+       {       
+               case FLAG_BASE:
+               {
+                       if(!IsDifferentTeam(toucher, self) && (toucher.flagcarried) && IsDifferentTeam(toucher.flagcarried, self))
+                               ctf_Handle_Capture(self, toucher, CAPTURE_NORMAL); // toucher just captured the enemies flag to his base
+                       else if(IsDifferentTeam(toucher, self) && (!toucher.flagcarried) && (!toucher.ctf_captureshielded) && (time > toucher.next_take_time))
+                               ctf_Handle_Pickup(self, toucher, PICKUP_BASE); // toucher just stole the enemies flag
+                       break;
+               }
+               
+               case FLAG_DROPPED:
+               {
+                       if(!IsDifferentTeam(toucher, self))
+                               ctf_Handle_Return(self, toucher); // toucher just returned his own flag
+                       else if((!toucher.flagcarried) && ((toucher != self.ctf_dropper) || (time > self.ctf_droptime + autocvar_g_ctf_flag_collect_delay)))
+                               ctf_Handle_Pickup(self, toucher, PICKUP_DROPPED); // toucher just picked up a dropped enemy flag
+                       break;
+               }
+                       
+               case FLAG_CARRY:
                {
-                       if(--redflags == -1) // happens exactly once (redflags is in 0..count-1, and will --'ed count times) // WHAT THE FUCK DOES THIS MEAN? whoever wrote this is shitty at explaining things.
-                               ctf_SetStatus_ForType(flag, IT_RED_FLAG_TAKEN);
+                       dprint("Someone touched a flag even though it was being carried?\n");
+                       break;
                }
-               else if(flag.items & IT_KEY1) // red
+               
+               case FLAG_PASSING:
                {
-                       if(--blueflags == -1) // happens exactly once
-                               ctf_SetStatus_ForType(flag, IT_BLUE_FLAG_TAKEN);
+                       if((toucher.classname == "player") && (toucher.deadflag == DEAD_NO) && (toucher != self.pass_sender))
+                       {
+                               if(IsDifferentTeam(toucher, self.pass_sender))
+                                       ctf_Handle_Return(self, toucher);
+                               else
+                                       ctf_Handle_Retrieve(self, toucher);
+                       }
+                       break;
                }
        }
 }
 
-void ctf_Reset()
+void ctf_RespawnFlag(entity flag)
 {
-       ctf_Handle_Drop(self);
-
-       ctf_RespawnFlag(self);
-}
-
+       // reset the player (if there is one)
+       if((flag.owner) && (flag.owner.flagcarried == flag))
+       {
+               if(flag.owner.wps_enemyflagcarrier)
+                       WaypointSprite_Kill(flag.owner.wps_enemyflagcarrier);
+                       
+               WaypointSprite_Kill(flag.wps_flagcarrier);
+               
+               flag.owner.flagcarried = world;
 
-// ==============
-// Event Handlers
-// ==============
+               if(flag.speedrunning)
+                       ctf_FakeTimeLimit(flag.owner, -1);
+       }
 
-void ctf_Handle_Drop(entity player) // make sure this works
-{
-       entity flag = player.flagcarried;
+       if((flag.ctf_status == FLAG_DROPPED) && (flag.wps_flagdropped))
+               { WaypointSprite_Kill(flag.wps_flagdropped); }
 
-       if(!flag) { return; }
-       if(flag.speedrunning) { ctf_RespawnFlag(flag); return; }
-       
        // reset the flag
        setattachment(flag, world, "");
-       flag.owner.flagcarried = world;
-       flag.owner = world;
-       flag.ctf_status = FLAG_DROPPED;
-       flag.movetype = MOVETYPE_TOSS;
-       flag.solid = SOLID_TRIGGER;
-       flag.takedamage = DAMAGE_YES;
-       flag.flags = FL_ITEM; // does this need set? same as above.
-       setorigin(flag, player.origin - '0 0 24' + '0 0 37'); // eh wtf is with these weird values?
-       flag.velocity = ('0 0 200' + ('0 100 0' * crandom()) + ('100 0 0' * crandom()));
-       flag.pain_finished = time + autocvar_g_ctf_flag_returntime;
+       setorigin(flag, flag.ctf_spawnorigin);
        
-       flag.ctf_droptime = time;
-       flag.ctf_dropperid = player.playerid;
-
-       // messages and sounds
-       Send_KillNotification(player.netname, flag.netname, "", INFO_LOSTFLAG, MSG_INFO);
-       sound(flag, CHAN_TRIGGER, flag.noise4, VOL_BASE, ATTN_NONE);
-       ctf_EventLog("dropped", player.team, player);
+       flag.movetype = ((flag.noalign) ? MOVETYPE_NONE : MOVETYPE_TOSS);
+       flag.takedamage = DAMAGE_NO;
+       flag.health = flag.max_flag_health;
+       flag.solid = SOLID_TRIGGER;
+       flag.velocity = '0 0 0';
+       flag.angles = flag.mangle;
+       flag.flags = FL_ITEM | FL_NOTARGET;
        
-       // scoring
-       PlayerScore_Add(player, SP_CTF_DROPS, 1);
-       UpdateFrags(player, -ctf_ReadScore("penalty_drop"));
-
-       // waypoints
-       WaypointSprite_Spawn("flagdropped", 0, 0, flag, '0 0 64', world, (COLOR_TEAM1 + COLOR_TEAM2 - flag.team), flag, wps_flagcarrier, FALSE);
-       WaypointSprite_Ping(player.wps_flagcarrier);
-       WaypointSprite_Kill(player.wps_flagcarrier);
-
-       ctf_CaptureShield_Update(player, 0); // shield only
-
-       // eh? 
-       trace_startsolid = FALSE;
-       tracebox(flag.origin, flag.mins, flag.maxs, flag.origin, TRUE, flag);
-       if(trace_startsolid)
-               dprint("FLAG FALLTHROUGH will happen SOON\n");
+       flag.ctf_status = FLAG_BASE;
+       flag.owner = world;
+       flag.pass_sender = world;
+       flag.pass_target = world;
+       flag.ctf_carrier = world;
+       flag.ctf_dropper = world;
+       flag.ctf_pickuptime = 0;
+       flag.ctf_droptime = 0;
+
+       wpforenemy_announced = FALSE;
 }
 
-// finish these
-
-void ctf_Handle_Capture(entity flag, entity player)
+void ctf_Reset()
 {
-       // blah blah blah
+       if(self.owner)
+               if(self.owner.classname == "player")
+                       ctf_Handle_Throw(self.owner, world, DROP_RESET);
+                       
+       ctf_RespawnFlag(self);
 }
 
-void ctf_Handle_Return(entity flag, entity player)
+void ctf_DelayedFlagSetup(void) // called after a flag is placed on a map by ctf_FlagSetup()
 {
-       // blah blah blah
-}
+       // bot waypoints
+       waypoint_spawnforitem_force(self, self.origin);
+       self.nearestwaypointtimeout = 0; // activate waypointing again
+       self.bot_basewaypoint = self.nearestwaypoint;
 
-void ctf_Handle_Pickup_Base(entity flag, entity player)
-{
-       // blah blah blah
-}
+       // waypointsprites
+       WaypointSprite_SpawnFixed(((self.team == COLOR_TEAM1) ? "redbase" : "bluebase"), self.origin + FLAG_WAYPOINT_OFFSET, self, wps_flagbase, RADARICON_FLAG, colormapPaletteColor(self.team - 1, FALSE));
+       WaypointSprite_UpdateTeamRadar(self.wps_flagbase, RADARICON_FLAG, colormapPaletteColor(self.team - 1, FALSE));
 
-void ctf_Handle_Pickup_Dropped(entity flag, entity player)
-{
-       // blah blah blah
+       // captureshield setup
+       ctf_CaptureShield_Spawn(self);
 }
 
-
-// ===================
-// Main Flag Functions
-// ===================
-
-void ctf_SetupFlag(float teamnumber, entity flag) // called when spawning a flag entity on the map as a spawnfunc 
-{
+void ctf_FlagSetup(float teamnumber, entity flag) // called when spawning a flag entity on the map as a spawnfunc 
+{      
        // declarations
-       teamnumber = fabs(teamnumber - bound(0, g_ctf_reverse, 1)); // if we were originally 1, this will become 0. If we were originally 0, this will become 1. 
+       teamnumber = fabs(teamnumber - bound(0, autocvar_g_ctf_reverse, 1)); // if we were originally 1, this will become 0. If we were originally 0, this will become 1. 
+       self = flag; // for later usage with droptofloor()
        
        // main setup
-       flag.ctf_worldflagnext = ctf_worldflaglist; // link flag into ctf_worldflaglist // todo: find out if this can be simplified
+       flag.ctf_worldflagnext = ctf_worldflaglist; // link flag into ctf_worldflaglist
        ctf_worldflaglist = flag;
 
        setattachment(flag, world, ""); 
@@ -256,40 +898,56 @@ void ctf_SetupFlag(float teamnumber, entity flag) // called when spawning a flag
        flag.items = ((teamnumber) ? IT_KEY2 : IT_KEY1); // IT_KEY2: gold key (redish enough) - IT_KEY1: silver key (bluish enough)
        flag.classname = "item_flag_team";
        flag.target = "###item###"; // wut?
-       flag.flags = FL_ITEM;
+       flag.flags = FL_ITEM | FL_NOTARGET;
        flag.solid = SOLID_TRIGGER;
+       flag.takedamage = DAMAGE_NO;
+       flag.damageforcescale = autocvar_g_ctf_flag_damageforcescale;   
+       flag.max_flag_health = ((autocvar_g_ctf_flag_return_damage && autocvar_g_ctf_flag_health) ? autocvar_g_ctf_flag_health : 100);
+       flag.health = flag.max_flag_health;
+       flag.event_damage = ctf_FlagDamage;
+       flag.pushable = TRUE;
+       flag.teleportable = TELEPORT_NORMAL;
+       flag.damagedbytriggers = autocvar_g_ctf_flag_return_when_unreachable;
+       flag.damagedbycontents = autocvar_g_ctf_flag_return_when_unreachable;
        flag.velocity = '0 0 0';
-       flag.ctf_status = FLAG_BASE;
        flag.mangle = flag.angles;
-       flag.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
-       
-       if(flag.spawnflags & 1) // I don't understand what all this is about.
-       {       
-               flag.noalign = TRUE;
-               flag.dropped_origin = flag.origin; 
-               flag.movetype = MOVETYPE_NONE;
-       }
-       else 
-       { 
-               flag.noalign = FALSE;
-               droptofloor(); 
-               flag.movetype = MOVETYPE_TOSS; 
-       }       
-       
        flag.reset = ctf_Reset;
        flag.touch = ctf_FlagTouch;
-       flag.think = ctf_RespawnFlag;
-       flag.nextthink = time + 0.2; // start after doors etc // Samual: 0.2 though? Why? 
+       flag.think = ctf_FlagThink;
+       flag.nextthink = time + FLAG_THINKRATE;
+       flag.ctf_status = FLAG_BASE;
+       
+       if(!flag.model) { flag.model = ((teamnumber) ? autocvar_g_ctf_flag_red_model : autocvar_g_ctf_flag_blue_model); }
+       if(!flag.scale) { flag.scale = FLAG_SCALE; }
+       if(!flag.skin) { flag.skin = ((teamnumber) ? autocvar_g_ctf_flag_red_skin : autocvar_g_ctf_flag_blue_skin); }
+       if(!flag.toucheffect) { flag.toucheffect = ((teamnumber) ? "redflag_touch" : "blueflag_touch"); }
+       if(!flag.passeffect) { flag.passeffect = ((!teamnumber) ? "red_pass" : "blue_pass"); } // invert the team number of the flag to pass as enemy team color
+       
+       // sound 
+       if(!flag.snd_flag_taken) { flag.snd_flag_taken  = ((teamnumber) ? "ctf/red_taken.wav" : "ctf/blue_taken.wav"); }
+       if(!flag.snd_flag_returned) { flag.snd_flag_returned = ((teamnumber) ? "ctf/red_returned.wav" : "ctf/blue_returned.wav"); }
+       if(!flag.snd_flag_capture) { flag.snd_flag_capture = ((teamnumber) ? "ctf/red_capture.wav" : "ctf/blue_capture.wav"); } // blue team scores by capturing the red flag
+       if(!flag.snd_flag_respawn) { flag.snd_flag_respawn = "ctf/flag_respawn.wav"; } // if there is ever a team-based sound for this, update the code to match.
+       if(!flag.snd_flag_dropped) { flag.snd_flag_dropped = ((teamnumber) ? "ctf/red_dropped.wav" : "ctf/blue_dropped.wav"); }
+       if(!flag.snd_flag_touch) { flag.snd_flag_touch = "ctf/touch.wav"; } // again has no team-based sound
+       if(!flag.snd_flag_pass) { flag.snd_flag_pass = "ctf/pass.wav"; } // same story here
+       
+       // precache
+       precache_sound(flag.snd_flag_taken);
+       precache_sound(flag.snd_flag_returned);
+       precache_sound(flag.snd_flag_capture);
+       precache_sound(flag.snd_flag_respawn);
+       precache_sound(flag.snd_flag_dropped);
+       precache_sound(flag.snd_flag_touch);
+       precache_sound(flag.snd_flag_pass);
+       precache_model(flag.model);
+       precache_model("models/ctf/shield.md3");
+       precache_model("models/ctf/shockwavetransring.md3");
 
        // appearence
-       if(!flag.model) { flag.model = ((teamnumber) ? autocvar_g_ctf_flag_red_model : autocvar_g_ctf_flag_blue_model); }
-       setmodel (flag, flag.model); // precision set below
+       setmodel(flag, flag.model); // precision set below
        setsize(flag, FLAG_MIN, FLAG_MAX);
-       setorigin(flag, flag.origin + '0 0 37');
-       flag.origin_z = flag.origin_z + 6; // why 6?
-       if(!flag.scale) { flag.scale = 0.6; }
-       
-       flag.skin = ((teamnumber) ? autocvar_g_ctf_flag_red_skin : autocvar_g_ctf_flag_blue_skin);
+       setorigin(flag, (flag.origin + FLAG_SPAWN_OFFSET));
        
        if(autocvar_g_ctf_flag_glowtrails)
        {
@@ -302,289 +960,278 @@ void ctf_SetupFlag(float teamnumber, entity flag) // called when spawning a flag
        if(autocvar_g_ctf_fullbrightflags) { flag.effects |= EF_FULLBRIGHT; }
        if(autocvar_g_ctf_dynamiclights)   { flag.effects |= ((teamnumber) ? EF_RED : EF_BLUE); }
        
-       // sound 
-       if(!flag.noise)  { flag.noise  = ((teamnumber) ? "ctf/red_taken.wav" : "ctf/blue_taken.wav"); }
-       if(!flag.noise1) { flag.noise1 = ((teamnumber) ? "ctf/red_returned.wav" : "ctf/blue_returned.wav"); }
-       if(!flag.noise2) { flag.noise2 = ((teamnumber) ? "ctf/red_capture.wav" : "ctf/blue_capture.wav"); } // blue team scores by capturing the red flag
-       if(!flag.noise3) { flag.noise3 = "ctf/flag_respawn.wav"; } // if there is ever a team-based sound for this, update the code to match.
-       if(!flag.noise4) { flag.noise4 = ((teamnumber) ? "ctf/red_dropped.wav" : "ctf/blue_dropped.wav"); }
-       
-       // precache
-       precache_sound(flag.noise);
-       precache_sound(flag.noise1);
-       precache_sound(flag.noise2);
-       precache_sound(flag.noise3);
-       precache_sound(flag.noise4);
-       precache_model(flag.model);
-       precache_model("models/ctf/shield.md3");
-       precache_model("models/ctf/shockwavetransring.md3");
-
-       // other initialization stuff
-       ctf_CreateBaseWaypoints(flag, teamnumber);
-       ctf_CaptureShield_Spawn(flag);
-       //InitializeEntity(self, ctf_CaptureShield_Spawn, INITPRIO_SETLOCATION);
-}
-
-void ctf_RespawnFlag(entity flag) // re-write this
-{
-       if(flag.classname != "item_flag_team") { backtrace("ctf_RespawnFlag was called incorrectly."); return; }
-
-       if(flag.owner)
-       if(flag.owner.flagcarried == flag)
-       {
-               WaypointSprite_DetachCarrier(flag.owner);
-               flag.owner.flagcarried = world;
-
-               if(flag.speedrunning)
-                       ctf_FakeTimeLimit(flag.owner, -1);
+       // flag placement
+       if((flag.spawnflags & 1) || flag.noalign) // don't drop to floor, just stay at fixed location
+       {       
+               flag.dropped_origin = flag.origin; 
+               flag.noalign = TRUE;
+               flag.movetype = MOVETYPE_NONE;
        }
-       flag.owner = world;
+       else // drop to floor, automatically find a platform and set that as spawn origin
+       { 
+               flag.noalign = FALSE;
+               self = flag;
+               droptofloor();
+               flag.movetype = MOVETYPE_TOSS; 
+       }       
        
-       if(flag.waypointsprite_attachedforcarrier)
-               WaypointSprite_DetachCarrier(flag);
-
-       setattachment(flag, world, "");
-       flag.damageforcescale = 0;
-       flag.takedamage = DAMAGE_NO;
-       flag.movetype = MOVETYPE_NONE;
-       if(!flag.noalign)
-               flag.movetype = MOVETYPE_TOSS;
-       flag.velocity = '0 0 0';
-       flag.solid = SOLID_TRIGGER;
-       // TODO: play a sound here
-       setorigin(flag, flag.dropped_origin);
-       flag.angles = flag.mangle;
-       flag.ctf_status = FLAG_BASE;
-       flag.owner = world;
-       flag.flags = FL_ITEM; // clear FL_ONGROUND and any other junk // there shouldn't be any "junk" set on this... look into it and make sure it's kept clean. 
+       InitializeEntity(flag, ctf_DelayedFlagSetup, INITPRIO_SETLOCATION);
 }
 
-void ctf_FlagThink() // re-write this
-{
-       local entity e;
-
-       self.nextthink = time + 0.1;
 
-       // sorry, we have to reset the flag size if it got squished by something
-       if(self.mins != FLAG_MIN || self.maxs != FLAG_MAX)
-       {
-               // if we can grow back, grow back
-               tracebox(self.origin, FLAG_MIN, FLAG_MAX, self.origin, MOVE_NOMONSTERS, self);
-               if(!trace_startsolid)
-                       setsize(self, FLAG_MIN, FLAG_MAX);
-       }
+// ==============
+// Hook Functions
+// ==============
 
-       if(self == ctf_worldflaglist) // only for the first flag
-       {
-               FOR_EACH_CLIENT(e)
-                       ctf_CaptureShield_Update(e, 1); // release shield only
-       }
+MUTATOR_HOOKFUNCTION(ctf_PlayerPreThink)
+{
+       entity flag;
+       
+       // initially clear items so they can be set as necessary later.
+       self.items &~= (IT_RED_FLAG_CARRYING | IT_RED_FLAG_TAKEN | IT_RED_FLAG_LOST 
+               | IT_BLUE_FLAG_CARRYING | IT_BLUE_FLAG_TAKEN | IT_BLUE_FLAG_LOST | IT_CTF_SHIELDED);
 
-       if(self.speedrunning)
-       if(self.cnt == FLAG_CARRY)
+       // scan through all the flags and notify the client about them 
+       for(flag = ctf_worldflaglist; flag; flag = flag.ctf_worldflagnext)
        {
-               if(self.owner)
-               if(flagcaptimerecord)
-               if(time >= self.flagpickuptime + flagcaptimerecord)
+               switch(flag.ctf_status)
                {
-                       bprint("The ", self.netname, " became impatient after ", ftos_decimals(flagcaptimerecord, 2), " seconds and returned itself\n");
-
-                       sound (self, CHAN_TRIGGER, self.noise3, VOL_BASE, ATTN_NONE);
-                       self.owner.impulse = 141; // returning!
-
-                       e = self;
-                       self = self.owner;
-                       ctf_RespawnFlag(e);
-                       ImpulseCommands();
-                       self = e;
-                       return;
+                       case FLAG_PASSING:
+                       case FLAG_CARRY:
+                       {
+                               if((flag.owner == self) || (flag.pass_sender == self))
+                                       self.items |= ((flag.items & IT_KEY2) ? IT_RED_FLAG_CARRYING : IT_BLUE_FLAG_CARRYING); // carrying: self is currently carrying the flag
+                               else 
+                                       self.items |= ((flag.items & IT_KEY2) ? IT_RED_FLAG_TAKEN : IT_BLUE_FLAG_TAKEN); // taken: someone on self's team is carrying the flag
+                               break;
+                       }
+                       case FLAG_DROPPED:
+                       {
+                               self.items |= ((flag.items & IT_KEY2) ? IT_RED_FLAG_LOST : IT_BLUE_FLAG_LOST); // lost: the flag is dropped somewhere on the map
+                               break;
+                       }
                }
        }
+       
+       // item for stopping players from capturing the flag too often
+       if(self.ctf_captureshielded)
+               self.items |= IT_CTF_SHIELDED;
+       
+       // update the health of the flag carrier waypointsprite
+       if(self.wps_flagcarrier) 
+               WaypointSprite_UpdateHealth(self.wps_flagcarrier, '1 0 0' * healtharmor_maxdamage(self.health, self.armorvalue, autocvar_g_balance_armor_blockpercent));
+       
+       return 0;
+}
 
-       if(self.cnt == FLAG_BASE)
-               return;
-
-       if(self.cnt == FLAG_DROPPED)
+MUTATOR_HOOKFUNCTION(ctf_PlayerDamage) // for changing damage and force values that are applied to players in g_damage.qc
+{
+       if(frag_attacker.flagcarried) // if the attacker is a flagcarrier
        {
-               // flag fallthrough? FIXME remove this if bug is really fixed now
-               if(self.origin_z < -131072)
+               if(frag_target == frag_attacker) // damage done to yourself
                {
-                       dprint("FLAG FALLTHROUGH just happened\n");
-                       self.pain_finished = 0;
+                       frag_damage *= autocvar_g_ctf_flagcarrier_selfdamagefactor;
+                       frag_force *= autocvar_g_ctf_flagcarrier_selfforcefactor;
                }
-               setattachment(self, world, "");
-               if(time > self.pain_finished)
+               else // damage done to everyone else
                {
-                       bprint("The ", self.netname, " has returned to base\n");
-                       sound (self, CHAN_TRIGGER, self.noise3, VOL_BASE, ATTN_NONE);
-                       ctf_EventLog("returned", self.team, world);
-                       ctf_RespawnFlag(self);
+                       frag_damage *= autocvar_g_ctf_flagcarrier_damagefactor;
+                       frag_force *= autocvar_g_ctf_flagcarrier_forcefactor;
                }
-               return;
        }
-
-       e = self.owner;
-       if(e.classname != "player" || (e.deadflag) || (e.flagcarried != self))
+       else if(frag_target.flagcarried && (frag_target.deadflag == DEAD_NO) && IsDifferentTeam(frag_target, frag_attacker)) // if the target is a flagcarrier
        {
-               dprint("CANNOT HAPPEN - player dead and STILL had a flag!\n");
-               ctf_Handle_Drop(e);
-               return;
+               if(autocvar_g_ctf_flagcarrier_auto_helpme_when_damaged > ('1 0 0' * healtharmor_maxdamage(frag_target.health, frag_target.armorvalue, autocvar_g_balance_armor_blockpercent)))
+                       WaypointSprite_HelpMePing(frag_target.wps_flagcarrier); // TODO: only do this if there is a significant loss of health?
        }
-
-       if(autocvar_g_ctf_allow_drop)
-       if(e.BUTTON_USE)
-               ctf_Handle_Drop(self);
+       return 0;
 }
 
-void ctf_FlagTouch()
+MUTATOR_HOOKFUNCTION(ctf_PlayerDies)
 {
-       if(gameover) { return; }
-       if(!self) { return; }
-       if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
-       { // The ball fell off the map, respawn it since players can't get to it
-               ctf_RespawnFlag(self);
-               return;
-       }
-       if(other.deadflag != DEAD_NO) { return; }
-       if(other.classname != "player") 
-       {  // the flag just touched an object, most likely the world
-               pointparticles(particleeffectnum("kaball_sparks"), self.origin, '0 0 0', 1);
-               sound(self, CHAN_AUTO, "keepaway/touch.wav", VOL_BASE, ATTN_NORM);
-               return; 
+       if((frag_attacker != frag_target) && (frag_attacker.classname == "player") && (frag_target.flagcarried))
+       {
+               PlayerTeamScore_AddScore(frag_attacker, autocvar_g_ctf_score_kill);
+               PlayerScore_Add(frag_attacker, SP_CTF_FCKILLS, 1);
        }
-       else if(self.wait > time) { return; }
-
-       switch(self.ctf_status) 
-       {       
-               case FLAG_BASE:
-                       if((other.team == self.team) && (other.flagcarried) && (other.flagcarried.team != self.team))
-                               ctf_Handle_Capture(self, other); // other just captured the enemies flag to his base
-                       else if((other.team != self.team) && (!other.flagcarried) && (!other.ctf_captureshielded))
-                               ctf_Handle_Pickup_Base(self, other); // other just stole the enemies flag
-                       break;
-               
-               case FLAG_DROPPED:
-                       if(other.team == self.team)
-                               ctf_Handle_Return(self, other); // other just returned his own flag
-                       else if((!other.flagcarried) && ((other.playerid != self.ctf_dropperid) || (time > self.ctf_droptime + autocvar_g_balance_ctf_delay_collect)))
-                               ctf_Handle_Pickup_Dropped(self, other); // other just picked up a dropped enemy flag
-                       break;
                                
-               case FLAG_CARRY:
-               default:
-                       dprint("Someone touched a flag even though it was being carried? wtf?\n");
-                       break; // this should never happen
-       }
+       if(frag_target.flagcarried)
+               { ctf_Handle_Throw(frag_target, world, DROP_NORMAL); }
+               
+       return 0;
 }
 
+MUTATOR_HOOKFUNCTION(ctf_GiveFragsForKill)
+{
+       frag_score = 0;
+       return (autocvar_g_ctf_ignore_frags); // no frags counted in ctf if this is true
+}
 
-// =======================
-// CaptureShield Functions 
-// =======================
+MUTATOR_HOOKFUNCTION(ctf_RemovePlayer)
+{
+       if(self.flagcarried)
+               { ctf_Handle_Throw(self, world, DROP_NORMAL); }
+               
+       return 0;
+}
 
-float ctf_CaptureShield_CheckStatus(entity p) // check to see 
+MUTATOR_HOOKFUNCTION(ctf_PortalTeleport)
 {
-       float s, se;
-       entity e;
-       float players_worseeq, players_total;
+       if(self.flagcarried) 
+       if(!autocvar_g_ctf_portalteleport)
+               { ctf_Handle_Throw(self, world, DROP_NORMAL); }
 
-       if(captureshield_max_ratio <= 0)
-               return FALSE;
+       return 0;
+}
 
-       s = PlayerScore_Add(p, SP_SCORE, 0);
-       if(s >= -captureshield_min_negscore)
-               return FALSE;
+MUTATOR_HOOKFUNCTION(ctf_PlayerUseKey)
+{
+       entity player = self;
 
-       players_total = players_worseeq = 0;
-       FOR_EACH_PLAYER(e)
+       if((time > player.throw_antispam) && (player.deadflag == DEAD_NO) && !player.speedrunning && (!player.vehicle || autocvar_g_ctf_allow_vehicle_touch))
        {
-               if(e.team != p.team)
-                       continue;
-               se = PlayerScore_Add(e, SP_SCORE, 0);
-               if(se <= s)
-                       ++players_worseeq;
-               ++players_total;
+               // pass the flag to a team mate
+               if(autocvar_g_ctf_pass)
+               {
+                       entity head, closest_target;
+                       head = WarpZone_FindRadius(player.origin, autocvar_g_ctf_pass_radius, TRUE);
+                       
+                       while(head) // find the closest acceptable target to pass to
+                       {
+                               if(head.classname == "player" && head.deadflag == DEAD_NO)
+                               if(head != player && !IsDifferentTeam(head, player))
+                               if(!head.speedrunning && (!head.vehicle || autocvar_g_ctf_allow_vehicle_touch))
+                               {
+                                       if(autocvar_g_ctf_pass_request && !player.flagcarried && head.flagcarried) 
+                                       { 
+                                               if(clienttype(head) == CLIENTTYPE_BOT)
+                                               {
+                                                       centerprint(player, strcat("Requesting ", head.netname, " to pass you the ", head.flagcarried.netname)); 
+                                                       ctf_Handle_Throw(head, player, DROP_PASS);
+                                               }
+                                               else
+                                               {
+                                                       centerprint(head, strcat(player.netname, " requests you to pass the ", head.flagcarried.netname)); 
+                                                       centerprint(player, strcat("Requesting ", head.netname, " to pass you the ", head.flagcarried.netname)); 
+                                               }
+                                               player.throw_antispam = time + autocvar_g_ctf_pass_wait; 
+                                               return 0; 
+                                       }
+                                       else if(player.flagcarried)
+                                       {
+                                               if(closest_target)
+                                               {
+                                                       if(vlen(player.origin - WarpZone_UnTransformOrigin(head, head.origin)) < vlen(player.origin - WarpZone_UnTransformOrigin(closest_target, closest_target.origin)))
+                                                               { closest_target = head; }
+                                               }
+                                               else { closest_target = head; }
+                                       }
+                               }
+                               head = head.chain;
+                       }
+                       
+                       if(closest_target) { ctf_Handle_Throw(player, closest_target, DROP_PASS); return 0; }
+               }
+               
+               // throw the flag in front of you
+               if(autocvar_g_ctf_drop && player.flagcarried)
+                       { ctf_Handle_Throw(player, world, DROP_THROW); }
        }
+               
+       return 0;
+}
 
-       // player is in the worse half, if >= half the players are better than him, or consequently, if < half of the players are worse
-       // use this rule here
-       
-       if(players_worseeq >= players_total * captureshield_max_ratio)
-               return FALSE;
+MUTATOR_HOOKFUNCTION(ctf_HelpMePing)
+{
+       if(self.wps_flagcarrier) // update the flagcarrier waypointsprite with "NEEDING HELP" notification
+       {
+               WaypointSprite_HelpMePing(self.wps_flagcarrier);
+       } 
+       else // create a normal help me waypointsprite
+       {
+               WaypointSprite_Spawn("helpme", waypointsprite_deployed_lifetime, waypointsprite_limitedrange, self, FLAG_WAYPOINT_OFFSET, world, self.team, self, wps_helpme, FALSE, RADARICON_HELPME, '1 0.5 0');
+               WaypointSprite_Ping(self.wps_helpme);
+       }
 
-       return TRUE;
+       return 1;
 }
 
-void ctf_CaptureShield_Update(entity p, float dir)
+MUTATOR_HOOKFUNCTION(ctf_VehicleEnter)
 {
-       float should;
-       if(dir == p.ctf_captureshielded) // 0: shield only, 1: unshield only
+       if(vh_player.flagcarried)
        {
-               should = ctf_CaptureShield_CheckStatus(p);
-               if(should != dir)
+               if(!autocvar_g_ctf_flagcarrier_allow_vehicle_carry)
                {
-                       if(should) // TODO csqc notifier for this
-                               centerprint_atprio(p, CENTERPRIO_SHIELDING, "^3You are now ^4shielded^3 from the flag\n^3for ^1too many unsuccessful attempts^3 to capture.\n\n^3Make some defensive scores before trying again.");
-                       else
-                               centerprint_atprio(p, CENTERPRIO_SHIELDING, "^3You are now free.\n\n^3Feel free to ^1try to capture^3 the flag again\n^3if you think you will succeed.");
-                       
-                       p.ctf_captureshielded = should;
+                       ctf_Handle_Throw(vh_player, world, DROP_NORMAL);
+               }
+               else
+               {            
+                       setattachment(vh_player.flagcarried, vh_vehicle, ""); 
+                       setorigin(vh_player.flagcarried, VEHICLE_FLAG_OFFSET);
+                       vh_player.flagcarried.scale = VEHICLE_FLAG_SCALE;
+                       //vh_player.flagcarried.angles = '0 0 0';       
                }
        }
+               
+       return 0;
 }
 
-float ctf_CaptureShield_Customize()
+MUTATOR_HOOKFUNCTION(ctf_VehicleExit)
 {
-       if not(other.ctf_captureshielded)
-               return FALSE;
-       if(self.team == other.team)
-               return FALSE;
-       return TRUE;
-}
+       if(vh_player.flagcarried)
+       {
+               setattachment(vh_player.flagcarried, vh_player, ""); 
+               setorigin(vh_player.flagcarried, FLAG_CARRY_OFFSET);
+               vh_player.flagcarried.scale = FLAG_SCALE;
+               vh_player.flagcarried.angles = '0 0 0'; 
+       }
 
-void ctf_CaptureShield_Touch()
-{
-       if not(other.ctf_captureshielded)
-               return;
-       if(self.team == other.team)
-               return;
-       vector mymid;
-       vector othermid;
-       mymid = (self.absmin + self.absmax) * 0.5;
-       othermid = (other.absmin + other.absmax) * 0.5;
-       Damage(other, self, self, 0, DEATH_HURTTRIGGER, mymid, normalize(othermid - mymid) * captureshield_force);
-       centerprint_atprio(other, CENTERPRIO_SHIELDING, "^3You are ^4shielded^3 from the flag\n^3for ^1too many unsuccessful attempts^3 to capture.\n\n^3Get some defensive scores before trying again.");
+       return 0;
 }
 
-void ctf_CaptureShield_Spawn(entity flag)
+MUTATOR_HOOKFUNCTION(ctf_AbortSpeedrun)
 {
-       entity e;
-       e = spawn();
-       e.enemy = self;
-       e.team = self.team;
-       e.touch = ctf_CaptureShield_Touch;
-       e.customizeentityforclient = ctf_CaptureShield_Customize;
-       e.classname = "ctf_captureshield";
-       e.effects = EF_ADDITIVE;
-       e.movetype = MOVETYPE_NOCLIP;
-       e.solid = SOLID_TRIGGER;
-       e.avelocity = '7 0 11';
-       setorigin(e, self.origin);
-       setmodel(e, "models/ctf/shield.md3");
-       e.scale = 0.5;
-       setsize(e, e.scale * e.mins, e.scale * e.maxs);
+       if(self.flagcarried)
+       {
+               bprint("The ", self.flagcarried.netname, " was returned to base by its carrier\n");
+               ctf_RespawnFlag(self);
+       }
+       
+       return 0;
 }
 
-
-// ==============
-// Hook Functions
-// ==============
-
-MUTATOR_HOOKFUNCTION(ctf_RemovePlayer)
+MUTATOR_HOOKFUNCTION(ctf_MatchEnd)
 {
-       if(self.flagcarried) { ctf_Handle_Drop(self); } // figure this out
+       entity flag; // temporary entity for the search method
        
-       return TRUE;
+       for(flag = ctf_worldflaglist; flag; flag = flag.ctf_worldflagnext)
+       {
+               switch(flag.ctf_status)
+               {
+                       case FLAG_DROPPED:
+                       case FLAG_PASSING:
+                       {
+                               // lock the flag, game is over
+                               flag.movetype = MOVETYPE_NONE;
+                               flag.takedamage = DAMAGE_NO;
+                               flag.solid = SOLID_NOT;
+                               flag.nextthink = 0; // stop thinking
+                               
+                               print("stopping the ", flag.netname, " from moving.\n");
+                               break;
+                       }
+                       
+                       default:
+                       case FLAG_BASE:
+                       case FLAG_CARRY:
+                       {
+                               // do nothing for these flags
+                               break;
+                       }
+               }
+       }
+       
+       return 0;
 }
 
 
@@ -639,35 +1286,39 @@ void spawnfunc_info_player_team4()
 }
 
 /*QUAKED spawnfunc_item_flag_team1 (0 0.5 0.8) (-48 -48 -37) (48 48 37)
-CTF flag for team one (Red). Multiple flags are allowed.
+CTF flag for team one (Red).
 Keys: 
 "angle" Angle the flag will point (minus 90 degrees)... 
-"model" model to use, note this needs red and blue as skins 0 and 1 (default models/ctf/flag.md3)...
-"noise" sound played when flag is picked up (default ctf/take.wav)...
-"noise1" sound played when flag is returned by a teammate (default ctf/return.wav)...
-"noise2" sound played when flag is captured (default ctf/redcapture.wav)...
-"noise3" sound played when flag is lost in the field and respawns itself (default ctf/respawn.wav)... */
+"model" model to use, note this needs red and blue as skins 0 and 1...
+"noise" sound played when flag is picked up...
+"noise1" sound played when flag is returned by a teammate...
+"noise2" sound played when flag is captured...
+"noise3" sound played when flag is lost in the field and respawns itself... 
+"noise4" sound played when flag is dropped by a player...
+"noise5" sound played when flag touches the ground... */
 void spawnfunc_item_flag_team1()
 {
        if(!g_ctf) { remove(self); return; }
 
-       ctf_SetupFlag(1, self);
+       ctf_FlagSetup(1, self); // 1 = red
 }
 
 /*QUAKED spawnfunc_item_flag_team2 (0 0.5 0.8) (-48 -48 -37) (48 48 37)
-CTF flag for team two (Blue). Multiple flags are allowed.
+CTF flag for team two (Blue).
 Keys: 
 "angle" Angle the flag will point (minus 90 degrees)... 
-"model" model to use, note this needs red and blue as skins 0 and 1 (default models/ctf/flag.md3)...
-"noise" sound played when flag is picked up (default ctf/take.wav)...
-"noise1" sound played when flag is returned by a teammate (default ctf/return.wav)...
-"noise2" sound played when flag is captured (default ctf/redcapture.wav)...
-"noise3" sound played when flag is lost in the field and respawns itself (default ctf/respawn.wav)... */
+"model" model to use, note this needs red and blue as skins 0 and 1...
+"noise" sound played when flag is picked up...
+"noise1" sound played when flag is returned by a teammate...
+"noise2" sound played when flag is captured...
+"noise3" sound played when flag is lost in the field and respawns itself... 
+"noise4" sound played when flag is dropped by a player...
+"noise5" sound played when flag touches the ground... */
 void spawnfunc_item_flag_team2()
 {
        if(!g_ctf) { remove(self); return; }
 
-       ctf_SetupFlag(0, self);
+       ctf_FlagSetup(0, self); // the 0 is misleading, but -- 0 = blue.
 }
 
 /*QUAKED spawnfunc_ctf_team (0 .5 .8) (-16 -16 -24) (16 16 32)
@@ -692,7 +1343,7 @@ void spawnfunc_ctf_team()
 // code from here on is just to support maps that don't have flag and team entities
 void ctf_SpawnTeam (string teamname, float teamcolor)
 {
-       local entity oldself;
+       entity oldself;
        oldself = self;
        self = spawn();
        self.classname = "ctf_team";
@@ -704,27 +1355,26 @@ void ctf_SpawnTeam (string teamname, float teamcolor)
        self = oldself;
 }
 
-void ctf_DelayedInit()
+void ctf_DelayedInit() // Do this check with a delay so we can wait for teams to be set up.
 {
        // if no teams are found, spawn defaults
        if(find(world, classname, "ctf_team") == world)
        {
+               print("No ""ctf_team"" entities found on this map, creating them anyway.\n");
                ctf_SpawnTeam("Red", COLOR_TEAM1 - 1);
                ctf_SpawnTeam("Blue", COLOR_TEAM2 - 1);
        }
+       
+       ScoreRules_ctf();
 }
 
 void ctf_Initialize()
 {
-       flagcaptimerecord = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time")));
+       ctf_captimerecord = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time")));
 
-       captureshield_min_negscore = autocvar_g_ctf_shield_min_negscore;
-       captureshield_max_ratio = autocvar_g_ctf_shield_max_ratio;
-       captureshield_force = autocvar_g_ctf_shield_force;
-
-       g_ctf_win_mode = cvar("g_ctf_win_mode");
-       
-       ScoreRules_ctf();
+       ctf_captureshield_min_negscore = autocvar_g_ctf_shield_min_negscore;
+       ctf_captureshield_max_ratio = autocvar_g_ctf_shield_max_ratio;
+       ctf_captureshield_force = autocvar_g_ctf_shield_force;
        
        InitializeEntity(world, ctf_DelayedInit, INITPRIO_GAMETYPE);
 }
@@ -734,12 +1384,18 @@ MUTATOR_DEFINITION(gamemode_ctf)
 {
        MUTATOR_HOOK(MakePlayerObserver, ctf_RemovePlayer, CBC_ORDER_ANY);
        MUTATOR_HOOK(ClientDisconnect, ctf_RemovePlayer, CBC_ORDER_ANY);
-       MUTATOR_HOOK(PlayerDies, ctf_RemovePlayer, CBC_ORDER_ANY);
-       //MUTATOR_HOOK(GiveFragsForKill, ctf_GiveFragsForKill, CBC_ORDER_ANY);
-       //MUTATOR_HOOK(PlayerPreThink, ctf_PlayerPreThink, CBC_ORDER_ANY);
-       //MUTATOR_HOOK(PlayerDamage_Calculate, ctf_PlayerDamage, CBC_ORDER_ANY);
-       //MUTATOR_HOOK(PlayerPowerups, ctf_PlayerPowerups, CBC_ORDER_ANY);
-
+       MUTATOR_HOOK(PlayerDies, ctf_PlayerDies, CBC_ORDER_ANY);
+       MUTATOR_HOOK(MatchEnd, ctf_MatchEnd, CBC_ORDER_ANY);
+       MUTATOR_HOOK(PortalTeleport, ctf_PortalTeleport, CBC_ORDER_ANY);
+       MUTATOR_HOOK(GiveFragsForKill, ctf_GiveFragsForKill, CBC_ORDER_ANY);
+       MUTATOR_HOOK(PlayerPreThink, ctf_PlayerPreThink, CBC_ORDER_ANY);
+       MUTATOR_HOOK(PlayerDamage_Calculate, ctf_PlayerDamage, CBC_ORDER_ANY);
+       MUTATOR_HOOK(PlayerUseKey, ctf_PlayerUseKey, CBC_ORDER_ANY);
+       MUTATOR_HOOK(HelpMePing, ctf_HelpMePing, CBC_ORDER_ANY);
+       MUTATOR_HOOK(VehicleEnter, ctf_VehicleEnter, CBC_ORDER_ANY);
+       MUTATOR_HOOK(VehicleExit, ctf_VehicleExit, CBC_ORDER_ANY);
+       MUTATOR_HOOK(AbortSpeedrun, ctf_AbortSpeedrun, CBC_ORDER_ANY);
+       
        MUTATOR_ONADD
        {
                if(time > 1) // game loads at time 1
@@ -754,5 +1410,5 @@ MUTATOR_DEFINITION(gamemode_ctf)
                error("This is a game type and it cannot be removed at runtime.");
        }
 
-       return TRUE;
+       return 0;
 }