



exec("comchat.cs"); 
exec("nappy.cs"); 
$SensorNetworkEnabled = true; 
$GuiModePlay = 1; 
$GuiModeCommand = 2; 
$GuiModeVictory = 3; 
$GuiModeInventory = 4; 
$GuiModeObjectives = 5; 
$GuiModeLobby = 6; 
$DefaultTeamEnergy = "Infinite"; 
$TeamEnergy[-1] = $DefaultTeamEnergy; 
$TeamEnergy[0] = $DefaultTeamEnergy; 
$TeamEnergy[1] = $DefaultTeamEnergy; 
$TeamEnergy[2] = $DefaultTeamEnergy; 
$TeamEnergy[3] = $DefaultTeamEnergy; 
$TeamEnergy[4] = $DefaultTeamEnergy; 
$TeamEnergy[5] = $DefaultTeamEnergy; 
$TeamEnergy[6] = $DefaultTeamEnergy; 
$TeamEnergy[7] = $DefaultTeamEnergy; 
$TeamEnergyCheat = 0; 
$MaxTeamEnergy = 700000; 
$incTeamEnergy = 700; 
$secTeamEnergy = 30; 
$ItemRespawnTime = 30; 
$RemoteAmmoEnergy = 3500; 
$RemoteInvEnergy = 900000; 
$RemoteComEnergy = 4000; 
$TeammateSpending = -4000; 
$WarnEnergyLow = 4000; 
$InitialPlayerEnergy = 10000; 
$MaxNumTurretsInBox = 14; 
$TurretBoxMaxLength = 5; 
$TurretBoxMaxWidth = 5; 
$TurretBoxMaxHeight = 2; 
$TurretBoxMinLength = 1; 
$TurretBoxMinWidth = 1; 
$TurretBoxMinHeight = 1; 
$SimTerrainObjectType = 1 << 1; 
$SimInteriorObjectType = 1 << 2; 
$SimPlayerObjectType = 1 << 7; 
$MineObjectType = 1 << 26; 
$MoveableObjectType = 1 << 22; 
$VehicleObjectType = 1 << 29; 
$StaticObjectType = 1 << 23; 
$ItemObjectType = 1 << 21; 
$ServerCheats = 0; 
$TestCheats = 0; 
$AutoRespawn = 0; 
$deathMsg[$LandingDamageType, 0] = "%2 falls to %3 death."; 
$deathMsg[$LandingDamageType, 1] = "%2 forgot to tie %3 bungie cord."; $deathMsg[$LandingDamageType, 2] = "%2 bites the dust in a forceful manner."; 
$deathMsg[$LandingDamageType, 3] = "%2 fall down go boom."; 
$deathMsg[$ImpactDamageType, 0] = "%1 makes quite an impact on %2."; $deathMsg[$ImpactDamageType, 1] = "%2 becomes the victim of a fly-by from %1."; 
$deathMsg[$ImpactDamageType, 2] = "%2 leaves a nasty dent in %1's fender."; 
$deathMsg[$ImpactDamageType, 3] = "%1 says, 'Hey %2, you scratched my paint job!'"; 
$deathMsg[$BulletDamageType, 0] = "%2 got regulated by %1."; 
$deathMsg[$BulletDamageType, 1] = "%1 gives %2 an overdose of lead."; $deathMsg[$BulletDamageType, 2] = "%1 gave %2 a hole in %3 head that wasn't there before."; 
$deathMsg[$BulletDamageType, 3] = "%1 guns down %2."; 
$deathMsg[$EnergyDamageType, 0] = "%2 dies of poison."; 
$deathMsg[$EnergyDamageType, 1] = "%2 gets a lethal injection."; $deathMsg[$EnergyDamageType, 2] = "%2 falls victim to a deadly virus."; $deathMsg[$EnergyDamageType, 3] = "%2 could not find the vaccine in time."; 
$deathMsg[$PlasmaDamageType, 0] = "%2 bursts into flames."; 
$deathMsg[$PlasmaDamageType, 1] = "%1 gives %2 a white-hot plasma injection."; 
$deathMsg[$PlasmaDamageType, 2] = "%1 asks %2, 'Need a light?'"; $deathMsg[$PlasmaDamageType, 3] = "%1 lights %2 on fire."; 
$deathMsg[$ExplosionDamageType, 0] = "%2 gets blown apart by %1."; 
$deathMsg[$ExplosionDamageType, 1] = "%2 explodes all over the place from %1's rocket."; 
$deathMsg[$ExplosionDamageType, 2] = "%2 gets shot down by %1."; 
$deathMsg[$ExplosionDamageType, 3] = "%1 shows off his mad skills of body dismemberment on %2."; 
$deathMsg[$ShrapnelDamageType, 0] = "%1 blows %2 up real good."; 
$deathMsg[$ShrapnelDamageType, 1] = "%2 gets a taste of %1's explosive temper."; 
$deathMsg[$ShrapnelDamageType, 2] = "%1 gives %2 a fatal concussion."; 
$deathMsg[$ShrapnelDamageType, 3] = "%2 never saw it coming from %1."; 
$deathMsg[$LaserDamageType, 0] = "%1 adds %2 to %3 list of sniper victims."; 
$deathMsg[$LaserDamageType, 1] = "%1 fells %2 with a sniper shot."; 
$deathMsg[$LaserDamageType, 2] = "%2 was assassinated by %1."; 
$deathMsg[$LaserDamageType, 3] = "%2 stayed in %1's crosshairs for too long."; 
$deathMsg[$MortarDamageType, 0] = "%1 explodes %2 into oblivion."; 
$deathMsg[$MortarDamageType, 1] = "%2 found %1's silly putty."; 
$deathMsg[$MortarDamageType, 2] = "%1 placed the explosives where %2 could find them."; 
$deathMsg[$MortarDamageType, 3] = "%1's bomb takes out %2."; 
$deathMsg[$BlasterDamageType, 0] = "%2 gets capped by %1."; 
$deathMsg[$BlasterDamageType, 1] = "%2 succumbs to %1's rain of blaster fire."; 
$deathMsg[$BlasterDamageType, 2] = "%1's punks %2 ghetto style."; 
$deathMsg[$BlasterDamageType, 3] = "%2 meets %1's master blaster."; 
$deathMsg[$ElectricityDamageType, 0] = "%2 gets zapped with %1's thunderbolt."; 
$deathMsg[$ElectricityDamageType, 1] = "%1 gives %2 a nasty jolt."; 
$deathMsg[$ElectricityDamageType, 2] = "%2 gets a real shock out of meeting %1."; 
$deathMsg[$ElectricityDamageType, 3] = "%1 short-circuits %2's systems."; 
$deathMsg[$CrushDamageType, 0] = "%2 didn't stay away from the moving parts."; 
$deathMsg[$CrushDamageType, 1] = "%2 is crushed."; 
$deathMsg[$CrushDamageType, 2] = "%2 gets smushed flat."; 
$deathMsg[$CrushDamageType, 3] = "%2 gets caught in the machinery."; 
$deathMsg[$DebrisDamageType, 0] = "%2 is a victim among the wreckage."; 
$deathMsg[$DebrisDamageType, 1] = "%2 is killed by debris."; 
$deathMsg[$DebrisDamageType, 2] = "%2 becomes a victim of collateral damage."; 
$deathMsg[$DebrisDamageType, 3] = "%2 got too close to the exploding stuff."; 
$deathMsg[$MissileDamageType, 0] = "%2 gets blown apart."; 
$deathMsg[$MissileDamageType, 1] = "%2 gets shot down."; 
$deathMsg[$MissileDamageType, 2] = "%2 gets real friendly with a rocket."; 
$deathMsg[$MissileDamageType, 3] = "%2 feels the burn from a warhead."; 
$deathMsg[$MineDamageType, 0] = "%1 blows %2 up real good."; 
$deathMsg[$MineDamageType, 1] = "%2 gets a taste of %1's explosive temper."; 
$deathMsg[$MineDamageType, 2] = "%1 gives %2 a fatal concussion."; 
$deathMsg[$MineDamageType, 3] = "%2 never saw it coming from %1."; 
$deathMsg[$FlashDamageType, 0] = "%1 blows %2 up real good."; 
$deathMsg[$FlashDamageType, 1] = "%2 gets a taste of %1's explosive temper."; 
$deathMsg[$FlashDamageType, 2] = "%1 gives %2 a fatal concussion."; 
$deathMsg[$FlashDamageType, 3] = "%2 never saw it coming from %1."; 
$deathMsg[$SniperDamageType, 0] = "%1 adds %2 to %3 list of sniper victims."; 
$deathMsg[$SniperDamageType, 1] = "%1 fells %2 with a sniper shot."; 
$deathMsg[$SniperDamageType, 2] = "%2 was assassinated by %1."; 
$deathMsg[$SniperDamageType, 3] = "%2 stayed in %1's crosshairs for too long."; 
$deathMsg[-2,0] = "%1 goes to see Dr. Kevorkian."; 
$deathMsg[-2,1] = "%1 wanted to make sure %2 gun was loaded."; 
$deathMsg[-2,2] = "%1 kills %2 own dumb self."; 
$deathMsg[-2,3] = "%1 goes postal."; 
$numDeathMsgs = 4; 

$spawnBuyList[0] = MediumArmor; 
$spawnBuyList[1] = HyperB; 
$spawnBuyList[2] = Chaingun; 
$spawnBuyList[3] = Disclauncher; 
$spawnBuyList[4] = RepairKit; 
$spawnBuyList[5] = Grenade; 
$spawnBuyList[6] = Grenade; 
$spawnBuyList[7] = Grenade; 
$spawnBuyList[8] = Grenade; 
$spawnBuyList[9] = Beacon; 
$spawnBuyList[10] = Beacon; 
$spawnBuyList[11] = Beacon; 
$spawnBuyList[12] = Beacon; 
$spawnBuyList[13] = TargetingLaser; 
$spawnBuyList[14] = EnergyPack; 
$spawnBuyList[15] = ""; 

function remotePlayMode(%clientId)
{
   if(!%clientId.guiLock)
   {
      remoteSCOM(%clientId, -1);
      Client::setGuiMode(%clientId, $GuiModePlay);
   }
}

function remoteCommandMode(%clientId)
{
   // can't switch to command mode while a server menu is up
   if(!%clientId.guiLock)
   {
      remoteSCOM(%clientId, -1);  // force the bandwidth to be full command
		if(%clientId.observerMode != "pregame")
		   checkControlUnmount(%clientId);
		Client::setGuiMode(%clientId, $GuiModeCommand);
   }
}

function remoteInventoryMode(%clientId)
{
   if(!%clientId.guiLock && !Observer::isObserver(%clientId))
   {
      remoteSCOM(%clientId, -1);
      Client::setGuiMode(%clientId, $GuiModeInventory);
   }
}

function remoteObjectivesMode(%clientId)
{
   if(!%clientId.guiLock)
   {
      remoteSCOM(%clientId, -1);
      Client::setGuiMode(%clientId, $GuiModeObjectives);
   }
}

function remoteScoresOn(%clientId)
{
   if(!%clientId.menuMode)
      Game::menuRequest(%clientId);
}

function remoteScoresOff(%clientId)
{
   Client::cancelMenu(%clientId);
}

function remoteToggleCommandMode(%clientId)
{
	if (Client::getGuiMode(%clientId) != $GuiModeCommand)
		remoteCommandMode(%clientId);
	else
		remotePlayMode(%clientId);
}

function remoteToggleInventoryMode(%clientId)
{
	if (Client::getGuiMode(%clientId) != $GuiModeInventory)
		remoteInventoryMode(%clientId);
	else
		remotePlayMode(%clientId);
}

function remoteToggleObjectivesMode(%clientId)
{
	if (Client::getGuiMode(%clientId) != $GuiModeObjectives)
		remoteObjectivesMode(%clientId);
	else
		remotePlayMode(%clientId);
}

function Time::getMinutes(%simTime)
{
   return floor(%simTime / 60);
}

function Time::getSeconds(%simTime)
{
   return %simTime % 60;
}

function Game::pickRandomSpawn(%team)
{
   %group = nameToID("MissionGroup/Teams/team" @ %team @ "/DropPoints/Random");
   %count = Group::objectCount(%group);
   if(!%count)
      return -1;
  	%spawnIdx = floor(getRandom() * (%count - 0.1));
  	%value = %count;
	for(%i = %spawnIdx; %i < %value; %i++) {
		%set = newObject("set",SimSet);
		%obj = Group::getObject(%group, %i);
		if(containerBoxFillSet(%set,$SimPlayerObjectType|$VehicleObjectType,GameBase::getPosition(%obj),2,2,4,0) == 0) {
			deleteObject(%set);
			return %obj;		
		}
		if(%i == %count - 1) {
			%i = -1;
			%value = %spawnIdx;
		}
		deleteObject(%set);
	}
   return false;
}

function Game::pickStartSpawn(%team)
{
   %group = nameToID("MissionGroup\\Teams\\team" @ %team @ "\\DropPoints\\Start");
   %count = Group::objectCount(%group);
   if(!%count)
      return -1;

   %spawnIdx = $lastTeamSpawn[%team] + 1;
   if(%spawnIdx >= %count)
      %spawnIdx = 0;
   $lastTeamSpawn[%team] = %spawnIdx;
   return Group::getObject(%group, %spawnIdx);
}

function Game::pickTeamSpawn(%team, %respawn)
{
   if(%respawn)
      return Game::pickRandomSpawn(%team);
   else
   {
      %spawn = Game::pickStartSpawn(%team);
      if(%spawn == -1)
         return Game::pickRandomSpawn(%team);
      return %spawn;
   }
}

function Game::pickObserverSpawn(%client)
{
   %group = nameToID("MissionGroup\\ObserverDropPoints");
   %count = Group::objectCount(%group);
   if(%group == -1 || !%count)
      %group = nameToID("MissionGroup\\Teams\\team" @ Client::getTeam(%client) @ "\\DropPoints\\Random");
   %count = Group::objectCount(%group);
   if(%group == -1 || !%count)
      %group = nameToID("MissionGroup\\Teams\\team0\\DropPoints\\Random");
   %count = Group::objectCount(%group);
   if(%group == -1 || !%count)
      return -1;
   %spawnIdx = %client.lastObserverSpawn + 1;
   if(%spawnIdx >= %count)
      %spawnIdx = 0;
   %client.lastObserverSpawn = %spawnIdx;
	return Group::getObject(%group, %spawnIdx);
}

function UpdateClientTimes(%time)
{
   for(%cl = Client::getFirst(); %cl != -1; %cl = Client::getNext(%cl))
      remoteEval(%cl, "setTime", -%time);
}

function Game::notifyMatchStart(%time)
{
   messageAll(0, "Match starts in " @ %time @ " seconds.");
   UpdateClientTimes(%time);
}

function Game::startMatch()
{
   $matchStarted = true;
   $missionStartTime = getSimTime();
   messageAll(0, "Match started.");
	Game::resetScores();	

   %numTeams = getNumTeams();
   for(%i = 0; %i < %numTeams; %i = %i + 1) {
		if($TeamEnergy[%i] != "Infinite")
			schedule("replenishTeamEnergy(" @ %i @ ");", $secTeamEnergy);
	}

   for(%cl = Client::getFirst(); %cl != -1; %cl = Client::getNext(%cl))
   {
		if(%cl.observerMode == "pregame")
      {
         %cl.observerMode = "";
         Client::setControlObject(%cl, Client::getOwnedObject(%cl));
      }
   	Game::refreshClientScore(%cl);
	}
   Game::checkTimeLimit();
}

function Game::pickPlayerSpawn(%clientId, %respawn)
{
   return Game::pickTeamSpawn(Client::getTeam(%clientId), %respawn);
}

function Game::playerSpawn(%clientId, %respawn)
{
   if(!$ghosting)
      return false;

	Client::clearItemShopping(%clientId);
   %spawnMarker = Game::pickPlayerSpawn(%clientId, %respawn);
   if(!%respawn)
   {
      // initial drop
      bottomprint(%clientId, "<jc><f0>Mission: <f1>" @ $missionName @ "   <f0>Mission Type: <f1>" @ $Game::missionType @ "\n<f0>Press <f1>'O'<f0> for specific objectives.", 5);
   }
	if(%spawnMarker) {   
		%clientId.guiLock = "";
	 	%clientId.dead = "";
	   if(%spawnMarker == -1)
	   {
	      %spawnPos = "0 0 300";
	      %spawnRot = "0 0 0";
	   }
	   else
	   {
	      %spawnPos = GameBase::getPosition(%spawnMarker);
	      %spawnRot = GameBase::getRotation(%spawnMarker);
	   }

		if(!String::ICompare(Client::getGender(%clientId), "Male"))
	      %armor = "larmor";
	   else
	      %armor = "lfemale";

	   %pl = spawnPlayer(%armor, %spawnPos, %spawnRot);
	   echo("SPAWN: cl:" @ %clientId @ " pl:" @ %pl @ " marker:" @ %spawnMarker @ " armor:" @ %armor);
	   if(%pl != -1)
	   {
	      GameBase::setTeam(%pl, Client::getTeam(%clientId));
	      Client::setOwnedObject(%clientId, %pl);
	      Game::playerSpawned(%pl, %clientId, %armor, %respawn);
	      
	      if($matchStarted)
	         Client::setControlObject(%clientId, %pl);
	      else
	      {
	         %clientId.observerMode = "pregame";
	         Client::setControlObject(%clientId, Client::getObserverCamera(%clientId));
	         Observer::setOrbitObject(%clientId, %pl, 3, 3, 3);
	      }
	   }
      return true;
	}
	else {
		Client::sendMessage(%clientId,0,"Sorry No Respawn Positions Are Empty - Try again later ");
      return false;
	}
}

function Game::playerSpawned(%pl, %clientId, %armor)
{	if(%clientId.custom){
        Client::setSkin(%clientId, $Client::info[%clientId, 0]);
	}	  
	%clientId.spawn= 1;
	%max = getNumItems();
   for(%i = 0; (%item = $spawnBuyList[%i]) != ""; %i++)
   {
		buyItem(%clientId,%item);	
		if(%item.className == Weapon) 
			%clientId.spawnWeapon = %item;
	}
	%clientId.spawn= "";
	if(%clientId.spawnWeapon != "") {
		Player::useItem(%pl,%clientId.spawnWeapon);	
   	%clientId.spawnWeapon="";
	}
} 

function Game::autoRespawn(%client)
{
	if(%client.dead == 1)
		Game::playerSpawn(%client, "true");
}

function onServerGhostAlwaysDone()
{
}

function Game::initialMissionDrop(%clientId)
{
	Client::setGuiMode(%clientId, $GuiModePlay);

   if($Server::TourneyMode)
      GameBase::setTeam(%clientId, -1);
   else
   {
      if(%clientId.observerMode == "observerFly" || %clientId.observerMode == "observerOrbit")
      {
	      %clientId.observerMode = "observerOrbit";
	      %clientId.guiLock = "";
         Observer::jump(%clientId);
         return;
      }
      %numTeams = getNumTeams();
      %curTeam = Client::getTeam(%clientId);

      if(%curTeam >= %numTeams || (%curTeam == -1 && (%numTeams < 2 || $Server::AutoAssignTeams)) )
         Game::assignClientTeam(%clientId);
   }    
	Client::setControlObject(%clientId, Client::getObserverCamera(%clientId));
   %camSpawn = Game::pickObserverSpawn(%clientId);
   Observer::setFlyMode(%clientId, GameBase::getPosition(%camSpawn), 
	   GameBase::getRotation(%camSpawn), true, true);

   if(Client::getTeam(%clientId) == -1)
   {
      %clientId.observerMode = "pickingTeam";

      if($Server::TourneyMode && ($matchStarted || $matchStarting))
      {
         %clientId.observerMode = "observerFly";
         return;
      }
      else if($Server::TourneyMode)
      {
         if($Server::TeamDamageScale)
            %td = "ENABLED";
         else
            %td = "DISABLED";
         bottomprint(%clientId, "<jc><f1>Server is running in Competition Mode\nPick a team.\nTeam damage is " @ %td, 0);
      }
      Client::buildMenu(%clientId, "Pick a team:", "InitialPickTeam");
      Client::addMenuItem(%clientId, "0Observe", -2);
      Client::addMenuItem(%clientId, "1Automatic", -1);
      for(%i = 0; %i < getNumTeams(); %i = %i + 1)
         Client::addMenuItem(%clientId, (%i+2) @ getTeamName(%i), %i);
      %clientId.justConnected = "";
   }
   else 
   {
        Client::setSkin(%clientId, $Server::teamSkin[Client::getTeam(%clientId)]);
      if(%clientId.justConnected)
      {
      Client::buildMenu(%clientId, "Pick a team:", "usecustom");
      Client::addMenuItem(%clientId, "0Use Personal Skin", 0);
      Client::addMenuItem(%clientId, "1Use Team Default", 1);
      bottomprint(%clientId, $Server::JoinMOTD, 0);
      %clientId.justConnected = "";
      }
      else if(%clientId.observerMode == "justJoined")
      {
         centerprint(%clientId, "");
         %clientId.observerMode = "";
         Game::playerSpawn(%clientId, false);
      }
      else
         Game::playerSpawn(%clientId, false);
}
	if($TeamEnergy[Client::getTeam(%clientId)] != "Infinite")
		$TeamEnergy[Client::getTeam(%clientId)] += $InitialPlayerEnergy;
	%clientId.teamEnergy = 0;
}

function processMenuuseCustom(%clientId, %skin)
{
   if(%skin == 0)
   {
      %clientId.custom = True;
   }
   if(%skin == 1)
   {
    %clientId.custom = False;
   }
   if(%team != -2)
   {
		if($TeamEnergy[%team] != "Infinite")
			$TeamEnergy[%team] += $InitialPlayerEnergy;
      %clientId.teamEnergy = 0;
      Client::setControlObject(%clientId, -1);
      Game::playerSpawn(%clientId, false);
   }
}


function processMenuInitialPickTeam(%clientId, %team)
{
   if($Server::TourneyMode && $matchStarted)
      %team = -2;

   if(%team == -2)
   {
      Observer::enterObserverMode(%clientId);
   }
   if(%team == -1)
   {
      Game::assignClientTeam(%clientId);
      %team = Client::getTeam(%clientId);
   }
   if(%team != -2)
   {
      GameBase::setTeam(%clientId, %team);
		if($TeamEnergy[%team] != "Infinite")
			$TeamEnergy[%team] += $InitialPlayerEnergy;
      %clientId.teamEnergy = 0;
      Client::setControlObject(%clientId, -1);
      Game::playerSpawn(%clientId, false);
   }
   if($Server::TourneyMode && !$CountdownStarted)
   {
      if(%team != -2)
      {
         bottomprint(%clientId, "<f1><jc>Press FIRE when ready.", 0);
         %clientId.notready = true;
         %clientId.notreadyCount = "";
      }
      else
      {
         bottomprint(%clientId, "", 0);
         %clientId.notready = "";
         %clientId.notreadyCount = "";
      }
   }
}

function Game::ForceTourneyMatchStart()
{
   %playerCount = 0;
   for(%cl = Client::getFirst(); %cl != -1; %cl = Client::getNext(%cl))
   {
      if(%cl.observerMode == "pregame")
         %playerCount++;
   }
   if(%playerCount == 0)
      return;

   for(%cl = Client::getFirst(); %cl != -1; %cl = Client::getNext(%cl))
   {
      if(%cl.observerMode == "pickingTeam")   
         processMenuInitialPickTeam(%cl, -2); // throw these guys into observer
      for(%cl = Client::getFirst(); %cl != -1; %cl = Client::getNext(%cl))
      {
         %cl.notready = "";
         %cl.notreadyCount = "";
         bottomprint(%cl, "", 0);
      }
   }
   Server::Countdown(30);
}

function Game::CheckTourneyMatchStart()
{
   if($CountdownStarted || $matchStarted)
      return;
   
   // loop through all the clients and see if any are still notready
   %playerCount = 0;
   %notReadyCount = 0;

   for(%cl = Client::getFirst(); %cl != -1; %cl = Client::getNext(%cl))
   {
      if(%cl.observerMode == "pickingTeam")
      {
         %notReady[%notReadyCount] = %cl;
         %notReadyCount++;
      }   
      else if(%cl.observerMode == "pregame")
      {
         if(%cl.notready)
         {
            %notReady[%notReadyCount] = %cl;
            %notReadyCount++;
         }
         else
            %playerCount++;
      }
   }
   if(%notReadyCount)
   {
      if(%notReadyCount == 1)
         MessageAll(0, Client::getName(%notReady[0]) @ " is holding things up!");
      else if(%notReadyCount < 4)
      {
         for(%i = 0; %i < %notReadyCount - 2; %i++)
            %str = Client::getName(%notReady[%i]) @ ", " @ %str;

         %str = %str @ Client::getName(%notReady[%i]) @ " and " @ Client::getName(%notReady[%i+1]) 
                     @ " are holding things up!";
         MessageAll(0, %str);
      }
      return;
   }

   if(%playerCount != 0)
   {
      for(%cl = Client::getFirst(); %cl != -1; %cl = Client::getNext(%cl))
      {
         %cl.notready = "";
         %cl.notreadyCount = "";
         bottomprint(%cl, "", 0);
      }
      Server::Countdown(30);
   }
}


function Game::checkTimeLimit()
{
   // if no timeLimit set or timeLimit set to 0,
   // just reschedule the check for a minute hence
   $timeLimitReached = false;

   if(!$Server::timeLimit)
   {
      schedule("Game::checkTimeLimit();", 60);
      return;
   }
   %curTimeLeft = ($Server::timeLimit * 60) + $missionStartTime - getSimTime();
   if(%curTimeLeft <= 0 && $matchStarted)
   {
      echo("GAME: Timelimit reached.");
      $timeLimitReached = true;
      Server::nextMission();
   }
   else
   {
      schedule("Game::checkTimeLimit();", 20);
      UpdateClientTimes(%curTimeLeft);
   }
}

function Game::resetScores(%client)
{
	if(%client == "") {
	   for(%cl = Client::getFirst(); %cl != -1; %cl = Client::getNext(%cl)) {
	      %cl.scoreKills = 0;
   	   %cl.scoreDeaths = 0;
			%cl.ratio = 0;
      	%cl.score = 0;
		}
	}
	else {
      %client.scoreKills = 0;
  	   %client.scoreDeaths = 0;
		%client.ratio = 0;
     	%client.score = 0;
	}
}

function remoteSetArmor(%player, %armorType)
{
	if ($ServerCheats) {
		checkMax(Player::getClient(%player),%armorType);
	   Player::setArmor(%player, %armorType);
	}
	else if($TestCheats) {
	   Player::setArmor(%player, %armorType);
	}
}


function Game::onPlayerConnected(%playerId)
{
   %playerId.scoreKills = 0;
   %playerId.scoreDeaths = 0;
	%playerId.score = 0;
   %playerId.justConnected = true;
   $menuMode[%playerId] = "None";
   Game::refreshClientScore(%playerId);
}

function Game::assignClientTeam(%playerId)
{
   if($teamplay)
   {
      %name = Client::getName(%playerId);
      %numTeams = getNumTeams();
      if($teamPreset[%name] != "")
      {
         if($teamPreset[%name] < %numTeams)
         {
            GameBase::setTeam(%playerId, $teamPreset[%name]);
            echo(Client::getName(%playerId), " was preset to team ", $teamPreset[%name]);
            return;
         }            
      }
      %numPlayers = getNumClients();
      for(%i = 0; %i < %numTeams; %i = %i + 1)
         %numTeamPlayers[%i] = 0;

      for(%i = 0; %i < %numPlayers; %i = %i + 1)
      {
         %pl = getClientByIndex(%i);
         if(%pl != %playerId)
         {
            %team = Client::getTeam(%pl);
            %numTeamPlayers[%team] = %numTeamPlayers[%team] + 1;
         }
      }
      %leastPlayers = %numTeamPlayers[0];
      %leastTeam = 0;
      for(%i = 1; %i < %numTeams; %i = %i + 1)
      {
         if( (%numTeamPlayers[%i] < %leastPlayers) || 
            ( (%numTeamPlayers[%i] == %leastPlayers) && 
            ($teamScore[%i] < $teamScore[%leastTeam] ) ))
         {
            %leastTeam = %i;
            %leastPlayers = %numTeamPlayers;
         }
      }
      GameBase::setTeam(%playerId, %leastTeam);
      echo(Client::getName(%playerId), " was automatically assigned to team ", %leastTeam);
   }
   else
   {
      GameBase::setTeam(%playerId, 0);
   }
}

function Client::onKilled(%playerId, %killerId, %damageType)
{
   echo("GAME: kill " @ %killerId @ " " @ %playerId @ " " @ %damageType);
   %playerId.guiLock = true;
   Client::setGuiMode(%playerId, $GuiModePlay);
	if(!String::ICompare(Client::getGender(%playerId), "Male"))
   {
      %playerGender = "his";
   }
	else
	{
		%playerGender = "her";
	}
	%ridx = floor(getRandom() * ($numDeathMsgs - 0.01));
	%victimName = Client::getName(%playerId);


   if(!%killerId)
   {
      messageAll(0, strcat(%victimName, " dies."), $DeathMessageMask);
      %playerId.scoreDeaths++;
  }
   else if(%killerId == %playerId)
   {
	  %oopsMsg = sprintf($deathMsg[-2, %ridx], %victimName, %playerGender);
      messageAll(0, %oopsMsg, $DeathMessageMask);
      %playerId.scoreDeaths++;
      %playerId.score--;
      Game::refreshClientScore(%playerId);
   }
   else
   {
		if(!String::ICompare(Client::getGender(%killerId), "Male"))
		{
			%killerGender = "his";
		}
		else
		{
			%killerGender = "her";
		}
      if($teamplay && (Client::getTeam(%killerId) == Client::getTeam(%playerId)))
      {
			if(%damageType != $MineDamageType) 
	         messageAll(0, strcat(Client::getName(%killerId), 
   	         " mows down ", %killerGender, " teammate, ", %victimName), $DeathMessageMask);
			else 
	         messageAll(0, strcat(Client::getName(%killerId), 
   	         " killed ", %killerGender, " teammate, ", %victimName ," with a mine."), $DeathMessageMask);
			%killerId.scoreDeaths++;
         %playerId.score--;
         Game::refreshClientScore(%killerId);
      }
      else
      {
	     %obitMsg = sprintf($deathMsg[%damageType, %ridx], Client::getName(%killerId),
	       %victimName, %killerGender, %playerGender);
         messageAll(0, %obitMsg, $DeathMessageMask);
         %killerId.scoreKills++;
         %playerId.scoreDeaths++;  // test play mode
         %killerId.score++;
         Game::refreshClientScore(%killerId);
         Game::refreshClientScore(%playerId);
      }
   }
   Game::clientKilled(%playerId, %killerId);
}

function Game::clientKilled(%playerId, %killerId)
{
   // do nothing
}

function Client::leaveGame(%clientId)
{
   // do nothing
}

function Player::enterMissionArea(%player)
{
   echo("Player " @ %player @ " entered the mission area.");
}

function Player::leaveMissionArea(%player)
{
   echo("Player " @ %player @ " left the mission area.");
}

function GameBase::getHeatFactor(%this)
{
   return 0.0;
}
