// Handles character spawning.
// Worthless Bums 2012.


function makeSquadMemberAnimation (%characterClass,
								   %characterName,
								   %characterGender,
								   %characterImageMap,
								   %characterExperience,
								   %characterCurrentHealth,
								   %characterMaxHealth,
								   %characterArmorRating,
								   %characterCurrentActionPoints,
								   %characterMaxActionPoints,
								   %characterStats,
								   %characterSkills,
								   %characterWeapon,
								   %characterArmor,
								   %characterAmmo,
								   %characterKills,
								   %characterTalentPoints,
								   %characterTalents,
								   %characterPerks)
{//echo("makeSquadMemberAnimation()");//echo("%characterPerks =" SPC %characterPerks);
/*echo(%characterClass);
echo(%characterName);
echo(%characterGender);
echo(%characterImageMap);
echo(%characterExperience);
echo(%characterCurrentHealth);
echo(%characterMaxHealth);
echo(%characterArmorRating);
echo(%characterCurrentActionPoints);
echo(%characterMaxActionPoints);
echo(%characterStats);
echo(%characterSkills);
echo(%characterWeapon);
echo(%characterArmor);
echo(%characterAmmo);
echo(%characterKills);
echo(%characterTalentPoints);
echo(%characterTalents);
echo(%characterPerks);
echo("END");*/
	%characterAnimation = "Sprites:" @ getCharacterClassName (%characterClass) @ "BallisticIdleSouthAnimation";
	%sight = 4;
	switch (%characterClass)
	{
		case 0:	// Leader.
			%sight = 5;
		case 1:	// Scout.
			%sight = 5;
		case 2:	// Grenadier.
			%sight = 4;
		case 3:	// Support.
			%sight = 4;
		case 4:	// Engineer.
			%sight = 4;
	}
	%characterSprite = new Sprite();
	%characterSprite.SceneLayer = $SquadMemberLayer;
	%characterSprite.group = $SquadMemberGroup;			// Crashes with this commented D:
	%characterSprite.characterClass = %characterClass;	// Crashes with this commented D:
	%characterSprite.name = %characterName;				// Crashes with this commented D:
	%characterSprite.gender = %characterGender;			// Crashes with this commented D:
	%characterSprite.scenegraph = levelSceneGraph;		// Crashes with this commented D:
	%characterSprite.config = "";						// Crashes with this commented D:
	%characterSprite.Animation = %characterAnimation;	// Crashes with this commented D:
	%characterSprite.position = "2 2";										// Offset by 2 internal Torque units to space the character avatar on the square tilemaps properly.
	%characterSprite.size = "4 4";
	%characterSprite.visible = true;
	%characterSprite.xIndex = 0;											// By default set player on the upper left hand corner of the map!
	%characterSprite.yIndex = 0;											// By default set player on the upper left hand corner of the map!
	%characterSprite.rank = 0;
	%characterSprite.level = getLevelFromExperience(%characterExperience);
	%characterSprite.experience = %characterExperience;
	%characterSprite.currentHealth = %characterCurrentHealth;				// Character's current health.
	%characterSprite.maxHealth = %characterMaxHealth;						// Character's maximum health.
	%characterSprite.armorRating = %characterArmorRating;					// Character's armor rating.
	%characterSprite.currentActionPoints = %characterCurrentActionPoints;	// Character's current action points.
	%characterSprite.maxActionPoints = %characterMaxActionPoints;			// Character's max action points.
	%characterSprite.actionPointReduction = 0;								// Used for combat turns where squad members lose action points.
	%characterSprite.sight = %sight;
	// Statistics:
	%characterSprite.power = %characterPower;								// Increases the power of bash and charge.
	%characterSprite.speed = %characterSpeed;								// Increases movement, reload, and bomb placement speed.
	%characterSprite.endurance = %characterndurance;							// Increases health.
	// Skills:
	%characterSprite.bash = %characterBash;									// By default the only skill a starting player has. Allows character to melee monsters and bash destructible objects.
	%characterSprite.demolitions = %characterDemolitions;					// Used to activate explosives.
	%characterSprite.arms = %characterArms;									// Allows character to use better ranged weapons.
	// Retraining.
	%characterSprite.retraining = "-1";
	// Inventory:
	%characterSprite.weapon = createNewWeapon(%characterWeapon);
	%characterSprite.armor = createNewArmor(%characterArmor);
	%characterSprite.ammo = %characterammo;
	%characterSprite.kills = %characterKills;
	// Talents:
	%characterSprite.characterTalentPoints = %characterTalentPoints;
	%characterSprite.talents = %characterTalents;
	// Perks:
	%characterSprite.perks = %characterPerks;
	// Weapon attachments:
	%characterSprite.weaponAttachment = 0;		// 0 - Flashlight, 1 - Laser Sight.
	%characterSprite.weaponAttachmentSprite = null;
	%characterSprite.evasion = 0;
	%characterSprite.overkill = false;
	%characterSprite.facing = $southDirection;
	%characterSprite.vehicle = -1;
	%characterSprite.oxygen = (%characterSprite.armor).oxygen;	// Oxygen is based on armor oxygen stat!
	handleLiftedTileMarker(%characterSprite);
	if (%characterPerks !$= "Perkless")	// Survivor spawn!
	{
		addObjectToScene(%characterSprite);
	}
	return %characterSprite;
}
function spawnCharacterClass (%characterClass,
							  %characterName,
							  %characterGender,
							  %characterImageMap,
							  %characterExperience,
							  %characterCurrentHealth,
							  %characterMaxHealth,
							  %characterArmorRating,
							  %characterCurrentActionPoints,
							  %characterMaxActionPoints,
							  %characterStats,
							  %characterSkills,
							  %characterWeapon,
							  %characterArmor,
							  %characterRetraining,
							  %characterAmmo,
							  %characterKills,
							  %characterTalentPoints,
							  %characterTalents,
							  %characterPerks
							 )
{//echo("spawnCharacterClass()");echo("%characterPerks =" SPC %characterPerks);
//echo("%characterWeapon =" SPC %characterWeapon);
//echo("%characterArmor =" SPC %characterArmor);
	%characterAnimation = makeSquadMemberAnimation(%characterClass,
												   %characterName,
												   %characterGender,
												   %characterImageMap,
												   %characterExperience,
												   %characterCurrentHealth,
												   %characterMaxHealth,
												   %characterArmorRating,
												   %characterCurrentActionPoints,
												   %characterMaxActionPoints,
												   %characterStats,
												   %characterSkills,
												   %characterWeapon,
												   %characterArmor,
												   %characterAmmo,
												   %characterKills,
												   %characterTalentPoints,
												   %characterTalents,
												   %characterPerks);
	return %characterAnimation;
}
function spawnPersistentCharacter ()
{//echo("spawnPersistentCharacter()");echo($leaderPerks SPC $scoutPerks SPC $grenadierPerks SPC $supportPerks);//echo($playerSquad[0].perks SPC $playerSquad[1].perks SPC $playerSquad[2].perks SPC $playerSquad[3].perks);
	$activeCharacter = -1;
	if ($leaderPersist)
	{//echo("Creating persistent Squad Leader!");
		%squadLeader = spawnCharacterClass(0,
										   $leaderName,
										   $leaderGender,
										   $leaderImageMap,
									  	   $leaderExperience,
									 	   $leaderCurrentHealth,
									  	   $leaderMaxHealth,
									  	   $leaderArmorRating,
									 	   $leaderCurrentActionPoints,
									  	   $leaderMaxActionPoints,
									  	   $leaderPower SPC
									  	   $leaderSpeed SPC
									  	   $leaderEndurance,
									  	   $leaderBash SPC
									  	   $leaderDemolitions SPC
									  	   $leaderArms,
										   $leaderRetraining,
									  	   $leaderWeapon,
									  	   $leaderArmor,
									  	   $leaderammo,
									  	   $leaderKills,
										   $leaderTalentPoints,
										   $leaderTalents,
										   $leaderPerks
									  	   );
		// Save the character.
		$playerSquad[0] = %squadLeader;
		if ($activeCharacter == -1)
		{
			$activeCharacter = 0;
		}
		// Move the leader to a correct spawn location.
		$playerSquad[0].setPosition("10 10");
		$playerSquad[0].xIndex = 2;
		$playerSquad[0].yIndex = 2;
	}
	if ($scoutPersist)
	{//echo("Creating persistent Squad Scout!");
		// Spawn a character with the given values.
		%squadScout = spawnCharacterClass(1,
									 $scoutName,
									 $scoutGender,
									 $scoutImageMap,
									 $scoutExperience,
									 $scoutCurrentHealth,
									 $scoutMaxHealth,
									 $scoutArmorRating,
									 $scoutCurrentActionPoints,
									 $scoutMaxActionPoints,
									 $scoutPower SPC
									 $scoutSpeed SPC
									 $scoutEndurance,
									 $scoutBash SPC
									 $scoutDemolitions SPC
									 $scoutArms,
									 $scoutRetraining,
									 $scoutWeapon,
									 $scoutArmor,
									 $scoutammo,
									 $scoutKills,
									 $scoutTalentPoints,
									 $scoutTalents,
									 $scoutPerks
									 );
		// Save the character.
		$playerSquad[1] = %squadScout;
		if ($activeCharacter == -1)
		{
			$activeCharacter = 1;
		}
		// Move the leader to a correct spawn location.
		$playerSquad[1].setPosition("10 6");
		$playerSquad[1].xIndex = 2;
		$playerSquad[1].yIndex = 1;
	}
	if ($grenadierPersist)
	{//echo("Creating persistent Squad Grenadier!");
		%squadGrenadier = spawnCharacterClass(2,
										 $grenadierName,
										 $grenadierGender,
										 $grenadierImageMap,
										 $grenadierExperience,
										 $grenadierCurrentHealth,
										 $grenadierMaxHealth,
										 $grenadierArmorRating,
										 $grenadierCurrentActionPoints,
										 $grenadierMaxActionPoints,
										 $grenadierPower SPC
										 $grenadierSpeed SPC
										 $grenadierEndurance,
										 $grenadierBash SPC 
										 $grenadierDemolitions SPC
										 $grenadierArms,
										 $grenadierRetraining,
										 $grenadierWeapon,
										 $grenadierArmor,
										 $grenadierammo,
										 $grenadierKills,
										 $grenadierTalentPoints,
										 $grenadierTalents,
										 $grenadierPerks
										 );
		// Save the character.
		$playerSquad[2] = %squadGrenadier;
		if ($activeCharacter == -1)
		{
			$activeCharacter = 2;
		}
		// Move the leader to a correct spawn location.
		$playerSquad[2].setPosition("6 10");
		$playerSquad[2].xIndex = 1;
		$playerSquad[2].yIndex = 2;
	}
	if ($supportPersist)
	{//echo("Creating persistent Squad Support!");
		%squadSupport = spawnCharacterClass(3,
									   $supportName,
									   $supportGender,
									   $supportImageMap,
									   $supportExperience,
									   $supportCurrentHealth,
									   $supportMaxHealth,
									   $supportArmorRating,
									   $supportCurrentActionPoints,
									   $supportMaxActionPoints,
									   $supportPower SPC
									   $supportSpeed SPC
									   $supportEndurance,
									   $supportBash SPC
									   $supportDemolitions SPC
									   $supportArms,
									   $supportRetraining,
									   $supportWeapon,
									   $supportArmor,
									   $supportammo,
									   $supportKills,
									   $supportTalentPoints,
									   $supportTalents,
									   $supportPerks
									   );
		
		// Save the character.
		$playerSquad[3] = %squadSupport;
		if ($activeCharacter == -1)
		{
			$activeCharacter = 3;
		}
		// Move the leader to a correct spawn location.
		$playerSquad[3].setPosition("6 6");
		$playerSquad[3].xIndex = 1;
		$playerSquad[3].yIndex = 1;
	}
}
function spawnCharacterAtLocation (%sceneGraph, %characterClass,
												%gender,
												%characterWeapon,
												%characterArmor,
												%imageMap,
												%x, %y,
												%xIndex, %yIndex,
												%mountCamera,
												%characterPerks)
{//echo("spawnCharacterAtLocation()");
	%characterAnimation = makeSquadMemberAnimation(%characterClass,
									  %characterName,
									  %characterGender,
									  %characterImageMap,
									  %characterExperience,
									  %characterCurrentHealth,
									  %characterMaxHealth,
									  %characterArmorRating,
									  %characterCurrentActionPoints,
									  %characterMaxActionPoints,
									  %characterStats,
									  %characterSkills,
									  %characterWeapon,
									  %characterArmor,
									  %characterAmmo,
									  %characterKills,
									  %characterTalentPoints,
									  %characterTalents,
									  %characterPerks);
	// Add other information to anmation.
	%characterAnimation.name = generateName(%gender);
	%characterAnimation.characterClass = %characterClass;
	%characterAnimation.level = 1;
	%characterAnimation.experience = 0;
	%characterAnimation.gender = %gender;
	%characterAnimation.scenegraph = %sceneGraph;
	%characterAnimation.config = "";
	%characterAnimation.position = %x SPC %y;
	%characterAnimation.size = "4 4";
	%characterAnimation.visible = true;
	%characterAnimation.xIndex = %xIndex;										// By default set player on the upper left hand corner of the map!
	%characterAnimation.yIndex = %yIndex;										// By default set player on the upper left hand corner of the map!
	// Character health, armor, statistics, skills, and inventory!
	// Health and Armor:
	%characterAnimation.currentHealth = $characterStartingHealth;				// Character's current health.
	%characterAnimation.maxHealth = $characterStartingHealth;					// Character's maximum health.
	%characterAnimation.armorRating = 0;										// Character's armor rating.
	%characterAnimation.currentActionPoints = 4;								// Character's current action points.
	%characterAnimation.maxActionPoints = 4;									// Character's max action points.
	// Statistics:
	%characterAnimation.power = 0;												// Increases the power of bash and charge.
	%characterAnimation.speed = 0;												// Increases movement, reload, and bomb placement speed.
	%characterAnimation.endurance = 0;											// Increases health.
	// Skills:
	%characterAnimation.bash = 0;												// By default the only skill a starting player has. Allows character to melee monsters and bash destructible objects.
	%characterAnimation.demolitions = 0;										// Used to activate explosives.
	%characterAnimation.arms = 0;												// Allows character to use better ranged weapons.
	// Inventory:
	%characterAnimation.weapon = createNewWeapon(%characterWeapon);
	%characterAnimation.armor = createNewArmor(%characterArmor);
	%characterAnimation.ammo = getRandom(3, 7);//getRandom(9, 15);	// Should just remove this line...
	switch (%characterClass)
	{
		case 0:	%characterAnimation.ammo = getRandom(3, 5);
		case 1:	%characterAnimation.ammo = getRandom(3, 5);
		case 2:	%characterAnimation.ammo = getRandom(4, 6);
		case 3:	%characterAnimation.ammo = getRandom(5, 8);
	}
	// Kills:
	%characterAnimation.kills = 0;
	// Talents:
	%characterAnimation.talentPoints = 0;
	%characterAnimation.talents = "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0";
	// Set the character animation at the specified position.
	%characterAnimation.setPosition(%x, %y);
	// Character class specific code!
	switch (%characterClass)
	{
		case 0:	// Leader.
			%randomHealth = getRandom(0, 4);
			%characterAnimation.currentHealth += %randomHealth;
			%characterAnimation.maxHealth 	  += %randomHealth;
		case 1:	// Scout.
			%randomHealth = getRandom(0, 3);
			%characterAnimation.currentHealth -= %randomHealth;
			%characterAnimation.maxHealth 	  -= %randomHealth;
		case 2:	// Grenadier.
			%randomHealth = getRandom(0, 2);
			%characterAnimation.currentHealth += %randomHealth;
			%characterAnimation.maxHealth 	  += %randomHealth;
		case 3:	// Support.
			%randomHealth = getRandom(0, 3);
			%characterAnimation.currentHealth += %randomHealth;
			%characterAnimation.maxHealth 	  += %randomHealth;
		case 4:	// Engineer.
			%randomHealth = getRandom(0, 2);
			%characterAnimation.currentHealth -= %randomHealth;
			%characterAnimation.maxHealth 	  -= %randomHealth;
	}
	// Scale with difficulty!
	switch ($difficultyLevel)
	{
		case 0:	// Easy.
			%characterAnimation.currentHealth 		+= 1;
			%characterAnimation.maxHealth	  		+= 1;
			%characterAnimation.currentActionPoints += 2;
			%characterAnimation.maxActionPoints 	+= 2;
		case 1:	// Normal.
			%characterAnimation.currentActionPoints += 1;
			%characterAnimation.maxActionPoints 	+= 1;
		case 2:	// Hard.
			%characterAnimation.currentHealth 		-= 1;
			%characterAnimation.maxHealth	 		-= 1;
			%characterAnimation.ammo		 		-= 1;
		case 3:	// Brutal.
			%characterAnimation.currentHealth 		-= 2;
			%characterAnimation.maxHealth	 		-= 2;
			%characterAnimation.ammo		 		-= 2;
	}
	// Sanity checks!
	if (%characterAnimation.currentHealth <= 0	||
		%characterAnimation.maxHealth	  <= 0)
	{
		%characterAnimation.currentHealth = 1;			// Floor health at 1!
		%characterAnimation.maxHealth	  = 1;			// Floor health at 1!
	}
	if (%characterAnimation.currentActionPoints <= 2	||
		%characterAnimation.maxActionPoints	 	<= 2)
	{
		%characterAnimation.currentActionPoints = 3;	// Floor action points at 3!
		%characterAnimation.maxActionPoints		= 3;	// Floor action points at 3!
	}
	if (%mountCamera)
	{
		if ($sceneWindowMountParent != $playerSquad[$activeCharacter])
		{//echo("MOUNTING!");
			sceneWindow2D.mount($playerSquad[$activeCharacter], $sceneWindow2DxOffset SPC $sceneWindow2DyOffset, $cameraMountForce, false);		// Parameter list is: object to mount to, offset, force, do not send to mount!
			$sceneWindowMountParent = $playerSquad[$activeCharacter];
		}
	}
	return %characterAnimation;
}