context: if(isPackage(GameModeDeathMatchPackage))
  	deactivatePackage(GameModeDeathMatchPackage);
  package GameModeDeathMatchPackage
  {
  	//this is called when save loading finishes 
  	function GameModeInitialResetCheck()
  	{
  		Parent::GameModeInitialResetCheck();
  		
  		//if there is no map list, attempt to create it
  		if($DM::numMaps == 0)
  			DM_BuildMapList();
  		
  		//if maplist is still empty, there are no maps
  		if($DM::numMaps == 0)
  		{
  			//default track config
  			$DM::Item0 = GunItem;
  			$DM::Item1 = redShotgunItem;
  			$DM::Item2 = 0;
  			$DM::Item3 = 0;
  			$DM::Item4 = 0;
  			$DM::Item0alt = SMGItem;
  			$DM::Item1alt = blueSniperItem;
  			$DM::Item2alt = 0;
  			$DM::Item3alt = 0;
  			$DM::Item4alt = 0;
  			$DM::BrickDamage = 1;
  			$DM::MapMusic = "";
  			$DM::MapMusicAll = 0;
  			$DM::killWater = 0;
  			$DM::allowAltLoadout = 1;
  			$DM::mapTeam1 = "RED";
  			$DM::mapTeam2 = "BLUE";
  			$DM::mapTeam3 = "YELLOW";
  			$DM::mapTeam4 = "GREEN";
  			$DM::force2teams = 0;
  			$DM::force4teams = 0;
  			$DM::mapCredits = "NONE";
  			$DM::mapDescription = "NONE";
  			$DM::mapEnvironment = 0;
  			$DM::mapFile = "Add-Ons/GameMode_DeathMatch/save.bls";
  			
  			//message
  			messageAll('', "\c5No DeathMatch maps available!");
  			messageAll('', "\c5You can find where maps are hosted by typing this command into chat: \c3/download");
  			return;
  		}
  		
  		if($DM::Initialized)
  			return;
  		
  		$DM::Initialized = true;
  		$DM::CurrentMap = -1;
  		
  		DM_NextMap();
  	}
  	
  	//when we're done loading a new map, reset the minigame
  	function ServerLoadSaveFile_End()
  	{
  		Parent::ServerLoadSaveFile_End();
  		
  		//new map has loaded, reset minigame
  		$DefaultMiniGame.scheduleReset(); //don't do it instantly, to give people a little bit of time to ghost
  	}
  	
  	function GameConnection::AutoAdminCheck(%this)
  	{
  		Parent::AutoAdminCheck(%this);
  		$DeathMatch_AmountClients[%this.bl_id]++;
  		
  		//load their save when they join
  		if($DeathMatch_AmountClients[%this.bl_id] <= 1)
  		{
  			if(isFile("config/server/DeathMatch/playersaves/" @ %this.bl_id @ ".cs"))
  			{
  				echo(" +- has DM save data");
  				exec("config/server/DeathMatch/playersaves/" @ %this.bl_id @ ".cs");
  			}
  			else
  				echo(" +- no DM save data");
  		}
  		else
  			echo(" +- " @ $DeathMatch_AmountClients[%this.bl_id] @ " clients with same bl_id");
  		
  		if($DeathMatch_Player[%this.bl_id, "PlayerName"] !$= %this.getPlayerName() && $DeathMatch_Player[%this.bl_id, "PlayerName"] !$= "")
  		{
  			messageAll('', "\c1" @ %this.getPlayerName() @ " last played as " @ $DeathMatch_Player[%this.bl_id, "PlayerName"] @ ".");
  			echo(" +- last played as " @ $DeathMatch_Player[%this.bl_id, "PlayerName"]);
  		}
  		
  		%this.DM_SaveData("SAVE");
  	}
  	
  	function brickTreasureChestData::openTreasureChest(%this, %obj, %player)
  	{
  		if(isObject(%player.client))
  			%player.client.incScore(1);
  		
  		Parent::openTreasureChest(%this, %obj, %player);
  	}
  	
  	function GameConnection::incScore(%this, %amount)
  	{
  		if(%this.DMTeam > 0)
  		{
  			$DM::TeamScore[%this.DMTeam] += %amount;
  			
  			for(%a = 0; %a < $DefaultMinigame.numMembers; %a++)
  			{
  				%member = $DefaultMinigame.member[%a];
  				if(isObject(%member) && %member.DMTeam == %this.DMTeam)
  				{
  					%member.setScore($DM::TeamScore[%this.DMTeam]);
  					
  					if(%amount > 0)
  					{
  						$DeathMatch_Player[%this.bl_id, "TeamPointsEarned"] += %amount;
  						centerPrint(%member, "<just:right>\c7(" @ %this.getPlayerName() @ ") \c3+" @ %amount SPC DM_getTeamColorString($DM::teamColor[%this.DMTeam]) @ DM_getTeamNameString($DM::teamColor[%this.DMTeam]) @ " \c6points ", 5);
  					}
  					else if(%amount < 0)
  					{
  						$DeathMatch_Player[%this.bl_id, "TeamPointsLost"] += %amount;
  						centerPrint(%member, "<just:right>\c7(" @ %this.getPlayerName() @ ") \c0" @ %amount SPC DM_getTeamColorString($DM::teamColor[%this.DMTeam]) @ DM_getTeamNameString($DM::teamColor[%this.DMTeam]) @ " \c6points ", 5);
  					}
  					
  					$DeathMatch_Player[%this.bl_id, "TotalTeamScore"] += %amount;
  				}
  			}
  		}
  		else
  		{
  			Parent::incScore(%this, %amount);
  			
  			if(%amount > 0)
  			{
  				$DeathMatch_Player[%this.bl_id, "PointsEarned"] += %amount;
  				centerPrint(%this, "<just:right>\c3+" @ %amount @ " \c6points ", 5);
  			}
  			else if(%amount < 0)
  			{
  				$DeathMatch_Player[%this.bl_id, "PointsLost"] -= %amount;
  				centerPrint(%this, "<just:right>\c0" @ %amount @ " \c6points ", 5);
  			}
  			
  			$DeathMatch_Player[%this.bl_id, "TotalScore"] += %amount;
  		}
  	}
  	
  	function minigameCanDamage(%client, %player2)
  	{
  		%client2 = %player2.client;
  		if(isObject(%client) && isObject(%client2))
  		{
  			if(%client != %client2)
  			{
  				if(%client.DMTeam > 0 && %client2.DMTeam > 0)
  				{
  					if(%client.DMTeam == %client2.DMTeam)
  					{
  						centerPrint(%client, DM_getTeamColorString($DM::teamColor[%client.DMTeam]) @ %client2.getPlayerName() @ " is your teammate!", 3);
  						return false;
  					}
  				}
  			}
  		}
  
  		return Parent::minigameCanDamage(%client, %player2);
  	}
  	
  	function Armor::damage(%this, %obj, %sourceObject, %position, %damage, %damageType)
  	{
  		if(mabs(%obj.DM_HealthShield) > 0)
  		{
  			if(%damage > %obj.DM_HealthShield)
  			{
  				%damage -= %obj.DM_HealthShield;
  				%obj.DM_HealthShield = 0;
  			}
  			else
  			{
  				%obj.DM_HealthShield -= %damage;
  				%damage = 0;
  			}
  		}
  		
  		Parent::damage(%this, %obj, %sourceObject, %position, %damage, %damageType);
  	}
  	
  	function gameConnection::onDeath(%this, %killerPlayer, %killer, %damageType, %a)
  	{
  		if(isObject(%this) && %this != %killer)
  			$DeathMatch_Player[%this.bl_id, "Deaths"]++;
  		if(isObject(%killer) && %this != %killer)
  			$DeathMatch_Player[%killer.bl_id, "Kills"]++;
  		if(isObject(%this) && %this == %killer)
  			$DeathMatch_Player[%this.bl_id, "SelfKills"]++;
  		
  		parent::onDeath(%this, %killerPlayer, %killer, %damageType, %a);
  		%this.deathTick = 0;
  	}
  	
  	function gameConnection::onClientLeaveGame(%this)
  	{
  		%this.DM_SaveData("SAVE");
  		$DeathMatch_AmountClients[%this.bl_id]--;
  		
  		if($DeathMatch_AmountClients[%this.bl_id] <= 0)
  		{
  			deleteVariables("$DeathMatch_AmountClients" @ %this.BL_ID);
  			deleteVariables("$DeathMatch_Player" @ %this.BL_ID @ "_*");
  		}
  		
  		if(%this.DMTeam > 0)
  		{
  			$DM::TeamPlayers[%this.DMTeam]--;
  			if($DM::TeamPlayers[%this.DMTeam] <= 0)
  			{
  				messageAll('', DM_getTeamColorString($DM::teamColor[%this.DMTeam]) @ DM_getTeamNameString($DM::teamColor[%this.DMTeam]) @ " Team \c2ran out of players.");
  				if(DM_getTeamsActive() <= 1)
  				{
  					cancel($DM::Tick);
  					DM_Tick(0);
  				}
  			}
  		}
  		
  		parent::onClientLeaveGame(%this);
  	}
  
  	function serverCmdmessageSent(%client, %text)
  	{
  		//repeat check
  		%trimText = trim(%text);
  		if(%client.lastChatText $= %trimText)
  		{
  			%chatDelta = (getSimTime() - %client.lastChatTime) / getTimeScale();
  			if(%chatDelta < 15000.0)
  			{
  				%client.spamMessageCount = $SPAM_MESSAGE_THRESHOLD;
  				messageClient(%client, '', '\c5Do not repeat yourself.');
  			}
  		}
  		%client.lastChatTime = getSimTime();
  		%client.lastChatText = %trimText;
  		
  		//player talk animation
  		%player = %client.Player;
  		if(isObject(%player))
  		{
  			%player.playThread(3, talk);
  			%player.schedule(strlen(%text) * 50.0, playThread, 3, root);
  		}
  		
  		//text filtering
  		%text = chatWhiteListFilter(%text);
  		%text = StripMLControlChars(%text);
  		%text = trim(%text);
  		if(strlen(%text) <= 0.0)
  			return;
  		if($Pref::Server::MaxChatLen > 0.0)
  		{
  			if(strlen(%text) >= $Pref::Server::MaxChatLen)
  				%text = getSubStr(%text, 0, $Pref::Server::MaxChatLen);
  		}
  		
  		//links
  		%protocol = "http://";
  		%protocolLen = strlen(%protocol);
  		%urlStart = strpos(%text, %protocol);
  		if(%urlStart == -1.0)
  		{
  			%protocol = "https://";
  			%protocolLen = strlen(%protocol);
  			%urlStart = strpos(%text, %protocol);
  		}
  		if(%urlStart == -1.0)
  		{
  			%protocol = "ftp://";
  			%protocolLen = strlen(%protocol);
  			%urlStart = strpos(%text, %protocol);
  		}
  		if(%urlStart != -1.0)
  		{
  			%urlEnd = strpos(%text, " ", %urlStart + 1.0);
  			%skipProtocol = 0;
  			if(%protocol $= "http://")
  				%skipProtocol = 1;
  			if(%urlEnd == -1.0)
  			{
  				%fullUrl = getSubStr(%text, %urlStart, strlen(%text) - %urlStart);
  				%url = getSubStr(%text, %urlStart + %protocolLen, strlen(%text) - %urlStart - %protocolLen);
  			}
  			else
  			{
  				%fullUrl = getSubStr(%text, %urlStart, %urlEnd - %urlStart);
  				%url = getSubStr(%text, %urlStart + %protocolLen, %urlEnd - %urlStart - %protocolLen);
  			}
  			if(strlen(%url) > 0.0)
  			{
  				%url = strreplace(%url, "<", "");
  				%url = strreplace(%url, ">", "");
  				if (%skipProtocol)
  					%newText = strreplace(%text, %fullUrl, "<a:" @ %url @ ">" @ %url @ "</a>\c6");
  				else
  					%newText = strreplace(%text, %fullUrl, "<a:" @ %protocol @ %url @ ">" @ %url @ "</a>\c6");
  				%text = %newText;
  			}
  		}
  		
  		//etard
  		if($Pref::Server::ETardFilter)
  		{
  			if(!chatFilter(%client, %text, $Pref::Server::ETardList, '\c5This is a civilized game.  Please use full words.'))
  				return;
  		}
  		
  		//get team color
  		if(%client.DMTeam > 0)
  			%color = DM_getTeamColorString($DM::teamColor[%client.DMTeam]);
  		else
  			%color = "\c3";
  		
  		//chat emotes compatability
  		if(isFunction(peReplace))
  			%text = peReplace(%text, "\c6");
  		
  		//send the message
  		chatMessageAll(%client, '\c7%1%2%3\c7%4\c6: %5', %client.clanPrefix, %color, %client.getPlayerName(), %client.clanSuffix, %text);
  		echo(%client.getSimpleName() @ ": " @ %text);
  	}
  	
  	function serverCmdTeamMessageSent(%client, %text)
  	{
  		//check if team chat is enabled
  		if(!$Pref::Server::DeathMatch::TeamChat)
  		{
  			messageClient(%client, '', "\c5Team chat disabled");
  			return;
  		}
  		
  		//check if they cant use team chat
  		%player = %client.player;
  		if(!isObject(%player))
  		{
  			if(%client.minigame != $DefaultMinigame)
  				messageClient(%client, '', "\c5Team chat disabled - You are not in a mini-game.");
  			else if(!%client.hasSpawnedOnce)
  				messageClient(%client, '', "\c5Team chat disabled - You have not spawned yet.");
  			else
  				messageClient(%client, '', "\c5Team chat disabled - You are dead.");
  			return;
  		}
  		else if(%client.DMTeam <= 0)
  		{
  			messageClient(%client, '', "\c5Team chat disabled - You are not in a team.");
  			return;
  		}
  		
  		//repeat check
  		%trimText = trim(%text);
  		if(%client.lastChatText $= %trimText)
  		{
  			%chatDelta = (getSimTime() - %client.lastChatTime) / getTimeScale();
  			if(%chatDelta < 15000.0)
  			{
  				%client.spamMessageCount = $SPAM_MESSAGE_THRESHOLD;
  				messageClient(%client, '', '\c5Do not repeat yourself.');
  			}
  		}
  		%client.lastChatTime = getSimTime();
  		%client.lastChatText = %trimText;
  		
  		//player talk animation
  		//%player.playThread(3, talk);
  		//%player.schedule(strlen(%text) * 50.0, playThread, 3, root);
  		
  		//text filtering
  		%text = chatWhiteListFilter(%text);
  		%text = StripMLControlChars(%text);
  		%text = trim(%text);
  		if(strlen(%text) <= 0.0)
  			return;
  		if($Pref::Server::MaxChatLen > 0.0)
  		{
  			if(strlen(%text) >= $Pref::Server::MaxChatLen)
  				%text = getSubStr(%text, 0, $Pref::Server::MaxChatLen);
  		}
  		
  		//links
  		%protocol = "http://";
  		%protocolLen = strlen(%protocol);
  		%urlStart = strpos(%text, %protocol);
  		if(%urlStart == -1.0)
  		{
  			%protocol = "https://";
  			%protocolLen = strlen(%protocol);
  			%urlStart = strpos(%text, %protocol);
  		}
  		if(%urlStart == -1.0)
  		{
  			%protocol = "ftp://";
  			%protocolLen = strlen(%protocol);
  			%urlStart = strpos(%text, %protocol);
  		}
  		if(%urlStart != -1.0)
  		{
  			%urlEnd = strpos(%text, " ", %urlStart + 1.0);
  			%skipProtocol = 0;
  			if(%protocol $= "http://")
  				%skipProtocol = 1;
  			if(%urlEnd == -1.0)
  			{
  				%fullUrl = getSubStr(%text, %urlStart, strlen(%text) - %urlStart);
  				%url = getSubStr(%text, %urlStart + %protocolLen, strlen(%text) - %urlStart - %protocolLen);
  			}
  			else
  			{
  				%fullUrl = getSubStr(%text, %urlStart, %urlEnd - %urlStart);
  				%url = getSubStr(%text, %urlStart + %protocolLen, %urlEnd - %urlStart - %protocolLen);
  			}
  			if(strlen(%url) > 0.0)
  			{
  				%url = strreplace(%url, "<", "");
  				%url = strreplace(%url, ">", "");
  				if (%skipProtocol)
  					%newText = strreplace(%text, %fullUrl, "<a:" @ %url @ ">" @ %url @ "</a>\c6");
  				else
  					%newText = strreplace(%text, %fullUrl, "<a:" @ %protocol @ %url @ ">" @ %url @ "</a>\c6");
  				%text = %newText;
  			}
  		}
  		
  		//etard
  		if($Pref::Server::ETardFilter)
  		{
  			if(!chatFilter(%client, %text, $Pref::Server::ETardList, '\c5This is a civilized game.  Please use full words.'))
  				return;
  		}
  		
  		//get team color
  		if(%client.DMTeam > 0)
  			%color = DM_getTeamColorString($DM::teamColor[%client.DMTeam]);
  		else
  			%color = "\c3";
  		
  		//chat emotes compatability
  		if(isFunction(peReplace))
  			%text = peReplace(%text, %color);
  		
  		//send the message
  		for(%a = 0; %a < $DefaultMinigame.numMembers; %a++)
  		{
  			%member = $DefaultMinigame.member[%a];
  			
  			if(isObject(%member) && %member.DMTeam == %client.DMTeam)
  				messageClient(%member, '', '\c7%1%2%3\c7%4%2: %5', %client.clanPrefix, %color, %client.getPlayerName(), %client.clanSuffix, %text);
  		}
  		
  		echo("(T" @ %client.DMTeam @ ") " @ %client.getSimpleName() @ ": " @ %text);
  	}
  	
  	function VehicleData::onEnterLiquid(%data, %obj, %coverage, %type)
  	{
  		Parent::onEnterLiquid(%data, %obj, %coverage, %type);
  		if($DM::killWater)
  		{
  			if(getWord(%obj.getPosition(), 2) < 10)
  			{
  				%obj.damage(%obj, %obj.getPosition(), 10000, $DamageType::Fall);
  				%obj.finalExplosion();
  			}
  		}
  	}
  	
  	function Armor::onEnterLiquid(%data, %obj, %coverage, %type)
  	{
  		Parent::onEnterLiquid(%data, %obj, %coverage, %type);
  		
  		if($DM::killWater)
  		{
  			%client = %obj.client;
  			if(isObject(%client))
  			{
  				if(%client.minigame != $DefaultMinigame)
  					return;
  			}
  			
  			if(getWord(%obj.getPosition(), 2) < 5.05)
  			{
  				%obj.hasShotOnce = true;
  				%obj.invulnerable = false;
  				%obj.damage(%obj, %obj.getPosition(), 10000, $DamageType::Fall);
  			}
  		}
  	}
  	
  	function MiniGameSO::Reset(%obj, %client)
  	{
  		$Pref::Server::DeathMatch::RoundLimit = mFloor($Pref::Server::DeathMatch::RoundLimit);
  		if($Pref::Server::DeathMatch::RoundLimit == 0 || $Pref::Server::DeathMatch::RoundLimit < -1)
  			$Pref::Server::DeathMatch::RoundLimit = -1;
  		
  		$Pref::Server::DeathMatch::TimeLimit = mFloor($Pref::Server::DeathMatch::TimeLimit);
  		if($Pref::Server::DeathMatch::TimeLimit < 30)
  			$Pref::Server::DeathMatch::TimeLimit = 30;
  		
  		//count number of minigame resets, when we reach the limit, go to next map
  		$DM::ResetCount++;
  		
  		//force round number to the last round to effectively stop map rotation
  		if($DM::ResetCount > $Pref::Server::DeathMatch::RoundLimit && ($Pref::Server::DeathMatch::RoundLimit <= 0 || $DM::numMaps <= 0))
  			$DM::ResetCount = $Pref::Server::DeathMatch::RoundLimit;
  		
  		if($DM::ResetCount > $Pref::Server::DeathMatch::RoundLimit && $Pref::Server::DeathMatch::RoundLimit != -1)
  		{
  			$DM::ResetCount = 0;
  			DM_NextMap();
  		}
  		else
  		{
  			echo("Beginning round...");
  			DM_DetermineRoundType();
  			
  			%roundMsg = "\c5Beginning ";
  			if(DM_getRoundTypesAllowed() > 1)
  				%roundMsg = %roundMsg @ "\c3" @ $DM::RoundName @ " \c5round";
  			else
  				%roundMsg = %roundMsg @ "round";
  			
  			if($Pref::Server::DeathMatch::RoundLimit > 0 && $DM::numMaps > 0)
  				%roundMsg = %roundMsg SPC $DM::ResetCount @ " of " @ $Pref::Server::DeathMatch::RoundLimit @ ".";
  			else
  				%roundMsg = %roundMsg @ ".";
  			
  			messageAll('', %roundMsg);
  			
  			DM_Tick(($Pref::Server::DeathMatch::TimeLimit * 10));
  			DM_PrepareRound();
  			
  			Parent::Reset(%obj, %client);
  			
  			if($DM::numMaps == 0)
  			{
  				messageAll('', "\c5No DeathMatch maps available!");
  				messageAll('', "\c5You can find where maps are hosted by typing this command into chat: \c3/download");
  				return;
  			}
  			
  			if(DM_getRoundTypesAllowed() > 1)
  			{
  				for(%a = 0; %a < $DefaultMinigame.numMembers; %a++)
  				{
  					%member = $DefaultMinigame.member[%a];
  					if(isObject(%member))
  					{
  						if(%member.DMTeam > 0)
  							centerPrint(%member, "<font:palatino linotype:64>" @ DM_getTeamColorString($DM::teamColor[%member.DMTeam]) @ $DM::RoundName @ "<br><font:palatino linotype:24>\c5" @ $DM::RoundDesc @ "<br>" @ DM_getTeamColorString($DM::teamColor[%member.DMTeam]) @ "You're in " @ DM_getTeamNameString($DM::teamColor[%member.DMTeam]) @ " Team!", 7);
  						else
  							centerPrint(%member, "<font:palatino linotype:64>\c3" @ $DM::RoundName @ "<br><font:palatino linotype:24>\c5" @ $DM::RoundDesc, 7);
  					}
  				}
  			}
  			
  			if($Pref::Server::DeathMatch::PlayMusic && !$DM::IsMusicless)
  				DM_PlaySong();
  		}
  	}
  	
  	function MinigameSO::pickSpawnPoint(%this, %client)
  	{
  		if($DM::TeamRoundType !$= "NONE")
  		{
  			%client.DM_ApplyTeam();
  			
  			if(%client.DMTeam > 0)
  			{
  				if($Pref::Server::DeathMatch::TeamSpawns)
  				{
  					if(%client.DMTeam == 1)
  						%spawns = Brickgroup_888888.ntObjectCount_1TeamSpawn;
  					else if(%client.DMTeam == 2)
  						%spawns = Brickgroup_888888.ntObjectCount_2TeamSpawn;
  					else if(%client.DMTeam == 3)
  						%spawns = Brickgroup_888888.ntObjectCount_3TeamSpawn;
  					else if(%client.DMTeam == 4)
  						%spawns = Brickgroup_888888.ntObjectCount_4TeamSpawn;
  					
  					%amtSpawns = 0;
  					for(%i = %spawns - 1; %i > -1; %i--)
  					{
  						if(%client.DMTeam == 1)
  							%spawn[%i] = Brickgroup_888888.ntObject_1TeamSpawn_[%i];
  
gt: 						else if(%client.DMTeam == 2)