// Handles Item Use.
// Worthless Bums 2012.


// Executes Medical Kit use effect.
function useMedicalKit ()
{//echo("useMedicalKit()");
	// Set active character's health to max.
	//$playerSquad[$activeCharacter].currentHealth = $playerSquad[$activeCharacter].maxHealth;
	if (($playerSquad[$activeCharacter].maxHealth - $playerSquad[$activeCharacter].currentHealth) >= $medkitHealthGain)
	{
		$playerSquad[$activeCharacter].currentHealth += $medkitHealthGain;
	}
	else
	{
		$playerSquad[$activeCharacter].currentHealth = $playerSquad[$activeCharacter].maxHealth;
	}
	playAudio("Audio:useMedkit", EffectsAudioVolume.value);
	updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "used" SPC $itemColor @ "Medkit" @ $defaultColor @ "!");
}

// Executes Ammo Crate use effect.
function useAmmoCrate ()
{//echo("useAmmoCrate()");
	%ammoMin = 1;
	%ammoMax = 3;
	if ($leaderPersist)
	{
		$playerSquad[0].ammo += getRandom(%ammoMin, %ammoMax);
	}
	if ($scoutPersist)
	{
		$playerSquad[1].ammo += getRandom(%ammoMin, %ammoMax);
	}
	if ($grenadierPersist)
	{
		$playerSquad[2].ammo += getRandom(%ammoMin, %ammoMax);
	}
	if ($supportPersist)
	{
		$playerSquad[3].ammo += getRandom(%ammoMin, %ammoMax);
	}
	// Active character gets an extra magazine because of autoreload.
	$playerSquad[$activeCharacter].ammo += 1;
	// Reload active character's weapon magazine.
	reloadWeapon();
	updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "used" SPC $itemColor @ "Ammo Crate" @ $defaultColor @ "!");
}

// Executes Bomb Box use effect.
function useBombBox ()
{//echo("useBombBox()");
	// Nothing here yet!
}

// Executes Sentry Case use effect.
function useSentryCase ()
{//echo("useSentryCase()");
	createSentryTurret($playerSquad[$activeCharacter].xIndex, $playerSquad[$activeCharacter].yIndex, 35, 4, 90, $playerSquad[$activeCharacter].facing);
	updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "used" SPC $itemColor @ "Sentry Case" @ $defaultColor @ "!");
}

// Executes Quik-Grow use effect.
function useQuikGrow ()
{//echo("useQuikGrow()");
	// Nothing here yet!
}

// Executes Adrenal Shot use effect.
function useAdrenalShot ()
{//echo("useAdrenalShot()");
	// Set active character's currentaction points to max.
	$playerSquad[$activeCharacter].currentActionPoints = $playerSquad[$activeCharacter].maxActionPoints;
	
	// Play use Adrenal Shot sound effect.
	playAudio("Audio:useAdrenalShot", EffectsAudioVolume.value);
	
	// Update floating text.
	updateFloatingActionPointText($activeCharacter);
	updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "used" SPC $itemColor @ "Adrenal Shot" @ $defaultColor @ "!");
}

// Executes usePain Pills use effect.
function usePainPills ()
{//echo("usePainPills()");
	// Nothing here yet!
}

// Executes Canteen use effect.
function useCanteen ()
{//echo("useCanteen()");
	// Set active character's health to max.
	if ($playerSquad[$activeCharacter].currentHealth + 2 > $playerSquad[$activeCharacter].maxHealth)
	{
		$playerSquad[$activeCharacter].currentHealth = $playerSquad[$activeCharacter].maxHealth;
	}
	else
	{
		$playerSquad[$activeCharacter].currentHealth += 2;
	}
	// Increase active character's action points by 2.
	if ($playerSquad[$activeCharacter].currentActionPoints + 2 > $playerSquad[$activeCharacter].maxActionPoints)
	{
		$playerSquad[$activeCharacter].currentActionPoints = $playerSquad[$activeCharacter].maxActionPoints;
	}
	else
	{
		$playerSquad[$activeCharacter].currentActionPoints += 2;
	}
	
	// Play canteen sound effect.
	playAudio("Audio:drinking", EffectsAudioVolume.value);
	updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "used" SPC $itemColor @ "Canteen" @ $defaultColor @ "!");
}

// Executes Flash Grenade use effect.
function useFlashGrenade ()
{//echo("useFlashGrenade()");
	// Nothing here yet!
}

// Executes Vac Cap use effect.
function useVacCap ()
{//echo("useVacCap()");
	// Nothing here yet!
}

// Executes Grenade use effect.
function useGrenade ()
{//echo("useGrenade()");
	// Throw the grenade!
	throwGrenade();
}

// Executes PDA use effect.
function usePDA ()
{//echo("usePDA()");
	// Use the PDA!
	%useType = getRandom(0, 1);
	switch (%useType)
	{
		case 0:
			// Inform the player of how many enemies are left on the level.
			setIndividualDialoguePopup($playerSquad[$activeCharacter], "Seems to be " @ $monsterList.getCount() @ " intruders left!", "dialogue");
	
		case 1:
			// Inform the player a path is attempting to be traced to the elevator.
			setIndividualDialoguePopup($playerSquad[$activeCharacter], "Attempting to locate elevator!", "dialogue");
			// Trace stairs.
			traceStairs();
			// Find Path PDA Clear.
			if ($stairsFound)
			{
				$fogOfWar.schedule(300, "findPathPDA");		// Bad timing mechanic!
			}
	}
	// Check for Engineering class talent "Hackery"!
	if ($playerSquad[$activeCharacter].characterClass == 4	&&
		getWord($playerSquad[$activeCharacter].talents, 3) == 1)
	{//echo("HACKERY DETECTED!");
		triggerHackInfo(1);
	}
	updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "used" SPC $itemColor @ "PDA" @ $defaultColor @ "!");
}

// Executes Oxygen Tank use effect.
function useOxygenTank ()
{//echo("useOxygenTank()");
	// Use the oxygen tank!
	// Increase active character's action points by 3.
	if ($playerSquad[$activeCharacter].currentActionPoints + 3 > $playerSquad[$activeCharacter].maxActionPoints)
	{
		$playerSquad[$activeCharacter].currentActionPoints = $playerSquad[$activeCharacter].maxActionPoints;
	}
	else
	{
		$playerSquad[$activeCharacter].currentActionPoints += 3;
	}
	updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "used" SPC $itemColor @ "Oxygen Tank" @ $defaultColor @ "!");
}

// Executes Power Core use effect.
function usePowerCore ()
{//echo("usePowerCore()");
	// Use the power core!
	// Nothing here yet!
}

// Executes Longsight use effect.
function useLongsight ()
{//echo("useLongsight()");
	// Play armor swap sound effect.
	playAudio("Audio:armorSwap", EffectsAudioVolume.value);
	// Increase weapon's range by 1.
	$playerSquad[$activeCharacter].weapon.range++;		// No max range cap?
	($playerSquad[$activeCharacter].weapon).weaponType = setWord(($playerSquad[$activeCharacter].weapon).weaponType, 3, $playerSquad[$activeCharacter].weapon.range);
	updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "used" SPC $itemColor @ "Longsight" @ $defaultColor @ "!");
}

// Executes Shortsight use effect.
function useShortsight ()
{//echo("useShortsight()");
	// Play armor swap sound effect.
	playAudio("Audio:armorSwap", EffectsAudioVolume.value);
	// Decrease weapon's range by 1.
	if ($playerSquad[$activeCharacter].weapon.range > 1)
	{
		$playerSquad[$activeCharacter].weapon.range--;
		($playerSquad[$activeCharacter].weapon).weaponType = setWord(($playerSquad[$activeCharacter].weapon).weaponType, 3, $playerSquad[$activeCharacter].weapon.range);
	}
	updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "used" SPC $itemColor @ "Shortsight" @ $defaultColor @ "!");
}

// Executes Armor Kit use effect.
function useArmorKit ()
{//echo("useArmorKit()");
	// Play armor swap sound effect.
	playAudio("Audio:armorSwap", EffectsAudioVolume.value);
	// Increases armor's armor rating by 1.
	$playerSquad[$activeCharacter].armor.armorRating++;
	($playerSquad[$activeCharacter].armor).armorType = setWord(($playerSquad[$activeCharacter].armor).armorType, 2, $playerSquad[$activeCharacter].armor.armorRating);
	updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "used" SPC $itemColor @ "Armor Kit" @ $defaultColor @ "!");
}

// Executes Lasershot use effect.
function useLasershot ()
{//echo("useLasershot()");
	// Play lasershot sound effect.
	playAudio("Audio:lasershot", EffectsAudioVolume.value);
	// Fire Lasershot!
	%lasershotDamage = 30;
	%lasershotDamageType = "fire";
	%lasershotHitType = "damage";//"crit";
	%lasershotRange = 4;
	// Hit every object on this cardinal direction!
	%direction = $playerSquad[$activeCharacter].facing;
	%xIndex = $playerSquad[$activeCharacter].xIndex;
	%yIndex = $playerSquad[$activeCharacter].yIndex;
	
	// Fire the laser!
	createLaserBeamAnimation(%xIndex, %yIndex, -1, -1, $playerSquad[$activeCharacter].facing, $playerSquad[$activeCharacter].weapon.range);
	
	for (%i = 0; %i < %lasershotRange; %i++)
	{
		switch (%direction)
		{
			case $northDirection:	%yIndex--;
			case $southDirection:	%yIndex++;
			case $eastDirection:	%xIndex++;
			case $westDirection:	%xIndex--;
		}
		if (%xIndex > 0					&&
			%xIndex < $mazeDimension	&&
			%yIndex > 0					&&
			%yIndex < $mazeDimension)
		{
			// Check squad members.
			if ($playerSquad[0].xIndex == %xIndex	&&
				$playerSquad[0].yIndex == %yIndex)
			{
				characterHit(0, %lasershotDamage, null, %lasershotDamageType, %lasershotHitType);
				if (%lasershotDamage >= $playerSquad[0].currentHealth)
				{
					triggerLasershotKill();
					$playerSquad[0].currentHealth = 0;
					// Squad member death!
					executeSquadMemberDeath(0, true, "[Lasershot]");
				}
				else
				{
					$playerSquad[0].currentHealth -= %lasershotDamage;
				}
			} else if ($playerSquad[1].xIndex == %xIndex	&&
				$playerSquad[1].yIndex == %yIndex)
			{
				characterHit(1, %lasershotDamage, null, %lasershotDamageType, %lasershotHitType);
				if (%lasershotDamage >= $playerSquad[0].currentHealth)
				{
					triggerLasershotKill();
					$playerSquad[1].currentHealth = 0;
					// Squad member death!
					executeSquadMemberDeath(1, true, "[Lasershot]");
				}
				else
				{
					$playerSquad[1].currentHealth -= %lasershotDamage;
				}
			} else if ($playerSquad[2].xIndex == %xIndex	&&
				$playerSquad[2].yIndex == %yIndex)
			{
				characterHit(2, %lasershotDamage, null, %lasershotDamageType, %lasershotHitType);
				if (%lasershotDamage >= $playerSquad[2].currentHealth)
				{
					triggerLasershotKill();
					$playerSquad[2].currentHealth = 0;
					// Squad member death!
					executeSquadMemberDeath(2, true, "[Lasershot]");
				}
				else
				{
					$playerSquad[2].currentHealth -= %lasershotDamage;
				}
			} else if ($playerSquad[3].xIndex == %xIndex	&&
				$playerSquad[3].yIndex == %yIndex)
			{
				characterHit(3, %lasershotDamage, null, %lasershotDamageType, %lasershotHitType);
				if (%lasershotDamage >= $playerSquad[3].currentHealth)
				{
					triggerLasershotKill();
					$playerSquad[3].currentHealth = 0;
					// Squad member death!
					executeSquadMemberDeath(3, true, "[Lasershot]");
				}
				else
				{
					$playerSquad[3].currentHealth -= %lasershotDamage;
				}
			}
			
			// Check monsters.
			for (%j = 0; %j < $monsterList.getCount(); %j++)
			{
				%monster = $monsterList.getObject(%j);
				if (%monster.xIndex == %xIndex	&&
					%monster.yIndex == %yIndex)
				{
					monsterHit(%j, %lasershotDamage, %direction, %lasershotDamageType);		// Beware knockback effects? Or happy chain reaction?
					if (%lasershotDamage >= %monster.currentHealth)
					{
						triggerLasershotKill();
						%monster.currentHealth = 0;
						handleMonsterDeath($activeCharacter, %j);
						%monster.safeDelete();	// Do we really need this?
					}
					else
					{
						%monster.currentHealth -= %lasershotDamage;
					}
					// Don't look any further! Monster stacking bug? Crap...
					//%j = $monsterList.getCount();
				}
			}
			
			// Check walls.
			if (isObject($mazeRoom[%xIndex, %yIndex]))
			{
				%roomPositionStatus = $mazeRoom[%xIndex, %yIndex].status;
				if (%roomPositionStatus == -3	||	// Closed door.
					%roomPositionStatus == -2)		// Wall.
				{
					destroyTerrainTile(%xIndex, %yIndex);
				}
			}
		}
	}
	updateSquadMemberFloatingText();
	updateMonsterFloatingText();
	updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "used" SPC $itemColor @ "Lasershot" @ $defaultColor @ "!");
}

// Executes Gunship Beam Shot use effect.
function useGunshipBeamShot (%unit)
{//echo("useGunshipBeamShot()");
	// Play lasershot sound effect.
	playAudio("Audio:lasershot", EffectsAudioVolume.value);
	// Fire Lasershot!
	%lasershotDamage = %unit.baseRangeDamage;//20;
	%lasershotDamageType = %unit.baseRangeDamageType;//"energy";
	%lasershotHitType = "damage";
	%lasershotRange = 12;
	// Hit every object on this cardinal direction!
	%direction = %unit.facing;
	%xIndex = %unit.xIndex;
	%yIndex = %unit.yIndex;
	
	for (%i = 0; %i < %lasershotRange; %i++)
	{
		switch (%direction)
		{
			case $northDirection:	%yIndex--;
			case $southDirection:	%yIndex++;
			case $eastDirection:	%xIndex++;
			case $westDirection:	%xIndex--;
		}
		if (%xIndex > 0					&&
			%xIndex < $mazeDimension	&&
			%yIndex > 0					&&
			%yIndex < $mazeDimension)
		{
			// Check squad members.
			if ($playerSquad[0].xIndex == %xIndex	&&
				$playerSquad[0].yIndex == %yIndex)
			{
				createGunshipExplosionAnimation(%xIndex, %yIndex);
				characterHit(0, %lasershotDamage, null, %lasershotDamageType, %lasershotHitType);
				if (%lasershotDamage >= $playerSquad[0].currentHealth)
				{
					$playerSquad[0].currentHealth = 0;
					// Squad member death!
					executeSquadMemberDeath(0, true, "[Beam Shot]");
				}
				else
				{
					$playerSquad[0].currentHealth -= %lasershotDamage;
				}
			} else if ($playerSquad[1].xIndex == %xIndex	&&
				$playerSquad[1].yIndex == %yIndex)
			{
				createGunshipExplosionAnimation(%xIndex, %yIndex);
				characterHit(1, %lasershotDamage, null, %lasershotDamageType, %lasershotHitType);
				if (%lasershotDamage >= $playerSquad[0].currentHealth)
				{
					$playerSquad[1].currentHealth = 0;
					// Squad member death!
					executeSquadMemberDeath(1, true, "[Beam Shot]");
				}
				else
				{
					$playerSquad[1].currentHealth -= %lasershotDamage;
				}
			} else if ($playerSquad[2].xIndex == %xIndex	&&
				$playerSquad[2].yIndex == %yIndex)
			{
				createGunshipExplosionAnimation(%xIndex, %yIndex);
				characterHit(2, %lasershotDamage, null, %lasershotDamageType, %lasershotHitType);
				if (%lasershotDamage >= $playerSquad[2].currentHealth)
				{
					$playerSquad[2].currentHealth = 0;
					// Squad member death!
					executeSquadMemberDeath(2, true, "[Beam Shot]");
				}
				else
				{
					$playerSquad[2].currentHealth -= %lasershotDamage;
				}
			} else if ($playerSquad[3].xIndex == %xIndex	&&
				$playerSquad[3].yIndex == %yIndex)
			{
				createGunshipExplosionAnimation(%xIndex, %yIndex);
				characterHit(3, %lasershotDamage, null, %lasershotDamageType, %lasershotHitType);
				if (%lasershotDamage >= $playerSquad[3].currentHealth)
				{
					$playerSquad[3].currentHealth = 0;
					// Squad member death!
					executeSquadMemberDeath(3, true, "[Beam Shot]");
				}
				else
				{
					$playerSquad[3].currentHealth -= %lasershotDamage;
				}
			}
			
			// Check monsters.
			for (%j = 0; %j < $monsterList.getCount(); %j++)
			{
				%monster = $monsterList.getObject(%j);
				if (%monster.xIndex == %xIndex	&&
					%monster.yIndex == %yIndex)
				{
					createGunshipExplosionAnimation(%xIndex, %yIndex);
					monsterHit(%j, %lasershotDamage, %direction, %lasershotDamageType);		// Beware knockback effects? Or happy chain reaction?
					if (%lasershotDamage >= %monster.currentHealth)
					{
						%monster.currentHealth = 0;
						handleMonsterDeath($activeCharacter, %j);
						%monster.safeDelete();		// Do we really need this?
					}
					else
					{
						%monster.currentHealth -= %lasershotDamage;
					}
				}
			}
			
			// Check walls.
			if (isObject($mazeRoom[%xIndex, %yIndex]))
			{
				%roomPositionStatus = $mazeRoom[%xIndex, %yIndex].status;
				if (%roomPositionStatus == -3	||	// Closed door.
					%roomPositionStatus == -2)		// Wall.
				{
					createGunshipExplosionAnimation(%xIndex, %yIndex);
					destroyTerrainTile(%xIndex, %yIndex);
				}
			}
		}
	}
	updateSquadMemberFloatingText();
	updateMonsterFloatingText();
}

// Executes Hourglass use effect.
function useHourglass ()
{//echo("useHourglass()");
	// Set squad's ap to max.
	$playerSquad[0].currentActionPoints = $playerSquad[0].maxActionPoints;
	$playerSquad[1].currentActionPoints = $playerSquad[1].maxActionPoints;
	$playerSquad[2].currentActionPoints = $playerSquad[2].maxActionPoints;
	$playerSquad[3].currentActionPoints = $playerSquad[3].maxActionPoints;
	
	// Play use Adrenal Shot sound effect.
	playAudio("Audio:useAdrenalShot", EffectsAudioVolume.value);
	
	// Increase all enemy ap by 1.
	for (%i = 0; %i < $monsterList.getCount(); %i++)
	{
		%monster = $monsterList.getObject(%i);
		if (isObject(%monster))
		{
			%monster.currentActionPoints++;
		}
	}
	// Update floating text.
	updateSquadMemberFloatingActionPoints();
	updateAllMonstersFloatingActionPointText();
	updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "used" SPC $itemColor @ "Hourglass" @ $defaultColor @ "!");
}

// Executes Painting Vox use effect.
function usePaintingVox ()
{//echo("usePaintingVox()");
	%randomEffect = getRandom(0, 9);
	//%randomEffect = 0;	// For testing purposes only!
	switch (%randomEffect)
	{
		case 0:		// Randomize chests!
			// Randomly spawn chests around the active character!
			%xIndex = $playerSquad[$activeCharacter].xIndex;
			%yIndex = $playerSquad[$activeCharacter].yIndex;
			if (tileIsClearOfObstructions(%xIndex + 1, %yIndex))
			{
				spawnTreasureChest(%xIndex + 1, %yIndex);
			}
			if (tileIsClearOfObstructions(%xIndex - 1, %yIndex))
			{
				spawnTreasureChest(%xIndex - 1, %yIndex);
			}
			if (tileIsClearOfObstructions(%xIndex, %yIndex + 1))
			{
				spawnTreasureChest(%xIndex, %yIndex + 1);
			}
			if (tileIsClearOfObstructions(%xIndex, %yIndex - 1))
			{
				spawnTreasureChest(%xIndex, %yIndex - 1);
			}
			playAudio("Audio:treasureSpawn", EffectsAudioVolume.value);
			updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "uses" @ $whiteColor @ " the power of the painting - " @  $friendlyActionColor @ "mimic" @ $defaultColor @ "!");
			
		case 1:
			useMedicalKit();
			updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "uses" @ $whiteColor @ " the power of the painting - " @  $friendlyActionColor @ "med kit" @ $defaultColor @ "!");
			
		case 2:
			useAmmoCrate();
			updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "uses" @ $whiteColor @ " the power of the painting - " @  $friendlyActionColor @ "ammo crate" @ $defaultColor @ "!");
			
		case 3:
			useAdrenalShot();
			updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "uses" @ $whiteColor @ " the power of the painting - " @  $friendlyActionColor @ "adrenal shot" @ $defaultColor @ "!");
			
		case 4:
			useCanteen();
			updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "uses" @ $whiteColor @ " the power of the painting - " @  $friendlyActionColor @ "canteen" @ $defaultColor @ "!");
			
		case 5:
			useGrenade();
			updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "uses" @ $whiteColor @ " the power of the painting - " @  $friendlyActionColor @ "grenade" @ $defaultColor @ "!");
			
		case 6:
			usePDA();
			updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "uses" @ $whiteColor @ " the power of the painting - " @  $friendlyActionColor @ "PDA" @ $defaultColor @ "!");
			
		case 7:
			useArmorKit();
			updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "uses" @ $whiteColor @ " the power of the painting - " @  $friendlyActionColor @ "armor kit" @ $defaultColor @ "!");
			
		case 8:
			useLasershot();
			updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "uses" @ $whiteColor @ " the power of the painting - " @  $friendlyActionColor @ "lasershot" @ $defaultColor @ "!");
			
		case 9:
			useHourglass();
			updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "uses" @ $whiteColor @ " the power of the painting - " @  $friendlyActionColor @ "hourglass" @ $defaultColor @ "!");
	}
	updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "used" SPC $itemColor @ "Vox Painting" @ $defaultColor @ "!");
}

// Executes Plastic Explosive use effect.
function usePlasticExplosive ()
{//echo("usePlasticExplosive()");
	// Check if the active character is facing an adjacent CLOSED door tile.
	%indices = getFacingAdjacentTile($playerSquad[$activeCharacter]);
	%xIndex = getWord(%indices, 0);
	%yIndex = getWord(%indices, 1);
	if (isClosedDoorTile(%xIndex, %yIndex))
	{
		if (!isObject($mazeRoom[%xIndex, %yIndex].plasticExplosive))
		{
			updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "used" @ $itemColor @ " plastic explosive" @ $defaultColor @ "!");
			createPlasticExplosiveAnimation(%xIndex, %yIndex);
			playAudio("Audio:setPlasticExplosive", EffectsAudioVolume.value);
		}
		else
		{
			updateEventText($friendlyActionColor @ "Door" @ $whiteColor @ " already has attached" @ $itemColor @ " plastic explosive" @ $defaultColor @ "!");
			// Really should just avoid calling this function if we can't use the plastic explosive.
			// Instead we just add the item back to the player's inventory!
			%item = createRandomItem(20, getTilePositionFromIndices(9999, 9999), 9999, 9999);
			addItemToInventory(%item, 0);
			playAudio("Audio:plasticExplosiveSet", EffectsAudioVolume.value);
		}
	}
	else
	{
		updateEventText($whiteColor @ "Need to face a " @ $friendlyActionColor @ "closed door" @ $whitecolor @ " to use " @ $itemColor @ "plastic explosive" @ $defaultColor @ "!");
		// Really should just avoid calling this function if we can't use the plastic explosive.
		// Instead we just add the item back to the player's inventory!
		%item = createRandomItem(20, getTilePositionFromIndices(9999, 9999), 9999, 9999);
		addItemToInventory(%item, 0);
		playAudio("Audio:failPlasticExplosive", EffectsAudioVolume.value);
	}
	updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "used" SPC $itemColor @ "Plastic Explosive" @ $defaultColor @ "!");
}

// Executes Hulk Suit use effect.
function useHulkSuit ()
{//echo("useHulkSuit()");
	if (isLargeUnit($playerSquad[$activeCharacter]))
	{
		$playerSquad[$activeCharacter].currentHealth = $playerSquad[$activeCharacter].maxHealth;
	}
	else
	{
		$playerSquad[$activeCharacter].currentHealth 	 += $hulkSuitHealth;
		$playerSquad[$activeCharacter].maxHealth 		 += $hulkSuitHealth;
	}
	setUnitInHulkSuit($playerSquad[$activeCharacter]);
	$playerSquad[$activeCharacter].armor.armorRating += $hulkSuitArmor;
	($playerSquad[$activeCharacter].armor).armorType = setWord(($playerSquad[$activeCharacter].armor).armorType, 2, $playerSquad[$activeCharacter].armor.armorRating);
	updateFloatingHealthPointText($activeCharacter);
	playAudio("Audio:armorSwap", EffectsAudioVolume.value);
	updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "used" SPC $itemColor @ "Hulk Suit" @ $defaultColor @ "!");
	updateUnitIndicatorPosition();
}

// Executes Multipass use effect.
function useMultipass ()
{//echo("useMultipass()");
	$multipassUsed = true;
	powerElevatorOn();
	playAudio("Audio:plasmaMod", EffectsAudioVolume.value);
	updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "used" SPC $itemColor @ "Multipass" @ $defaultColor @ "!");
}

// Executes Laser Mod use effect.
function useLaserMod ()
{//echo("useLaserMod()");
	// Engineer.
	%accuracyBoost = 0;
	if ($playerSquad[$activeCharacter].characterClass == 4)
	{
		if (getWord($playerSquad[$activeCharacter].talents, 7) == 1)		// Tech Upgrade.
		{
			%accuracyBoost += $techUpgradeLaserModAccuracyBoost;
		}
	}
	if ($playerSquad[$activeCharacter].weapon.projectileType == $weaponProjectileLaserType - 1)
	{
		$playerSquad[$activeCharacter].weapon.projectileType = $weaponProjectileLaserType;
		%newAccuracy = getWord(($playerSquad[$activeCharacter].weapon).weaponType, 4) + getRandom(10, 15) + %accuracyBoost;
		$playerSquad[$activeCharacter].weapon.accuracy = %newAccuracy;
		($playerSquad[$activeCharacter].weapon).weaponType = getWord(($playerSquad[$activeCharacter].weapon).weaponType,  0)	SPC		// Name.
															 getWord(($playerSquad[$activeCharacter].weapon).weaponType,  1)	SPC		// Squad Member.
															 getWord(($playerSquad[$activeCharacter].weapon).weaponType,  2)	SPC		// Damage.
															 getWord(($playerSquad[$activeCharacter].weapon).weaponType,  3)	SPC		// Range.
															 %newAccuracy														SPC		// Accuracy.
															 getWord(($playerSquad[$activeCharacter].weapon).weaponType,  5)	SPC		// Imagemap.
															 getWord(($playerSquad[$activeCharacter].weapon).weaponType,  6)	SPC		// Ammo/Magazine Capacity.
															 getWord(($playerSquad[$activeCharacter].weapon).weaponType,  7)	SPC		// Capacity/Max Ammo.
															 getWord(($playerSquad[$activeCharacter].weapon).weaponType,  8)	SPC		// Damage Range.
															 ($playerSquad[$activeCharacter].weapon).currentAmmo				SPC		// Current Ammo/In Magazine.
															 getWord(($playerSquad[$activeCharacter].weapon).weaponType, 10)	SPC		// Reload/AP Cost.
															 getWord(($playerSquad[$activeCharacter].weapon).weaponType, 11)	SPC		// Fire Cost.
															 $playerSquad[$activeCharacter].weapon.projectileType;						// Projectile Type.
		randomLaserModChatter($activeCharacter);
		playAudio("Audio:plasmaMod", EffectsAudioVolume.value);
		setSquadMemberIndexFacing($playerSquad[$activeCharacter].facing, $playerSquad[$activeCharacter], $activeCharacter);	// To enforce new weapon animation sprite.
		// Visual effect!
		// Nothing here yet!
	}
	else
	{
		$playerSquad[$activeCharacter].currentActionPoints++;
		schedule(50, 0, "giveItem", 23);
		randomCantUseModUpgradeChatter($activeCharacter);
		playAudio("Audio:grabItem", EffectsAudioVolume.value);
	}
	updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "used" SPC $itemColor @ "Laser Mod" @ $defaultColor @ "!");
	// Check to trigger Laser Squad achievement!
	checkLaserSquadAchievement();
}
function setSquadMemberLaserWeapon (%characterIndex)
{
	$playerSquad[%characterIndex].weapon.projectileType = $weaponProjectileLaserType;
	%newAccuracy = getWord(($playerSquad[%characterIndex].weapon).weaponType, 4) + getRandom(10, 15) + %accuracyBoost;
	$playerSquad[%characterIndex].weapon.accuracy = %newAccuracy;
	($playerSquad[%characterIndex].weapon).weaponType = getWord(($playerSquad[%characterIndex].weapon).weaponType,  0)	SPC		// Name.
														 getWord(($playerSquad[%characterIndex].weapon).weaponType,  1)	SPC		// Squad Member.
														 getWord(($playerSquad[%characterIndex].weapon).weaponType,  2)	SPC		// Damage.
														 getWord(($playerSquad[%characterIndex].weapon).weaponType,  3)	SPC		// Range.
														 %newAccuracy													SPC		// Accuracy.
														 getWord(($playerSquad[%characterIndex].weapon).weaponType,  5)	SPC		// Imagemap.
														 getWord(($playerSquad[%characterIndex].weapon).weaponType,  6)	SPC		// Ammo/Magazine Capacity.
														 getWord(($playerSquad[%characterIndex].weapon).weaponType,  7)	SPC		// Capacity/Max Ammo.
														 getWord(($playerSquad[%characterIndex].weapon).weaponType,  8)	SPC		// Damage Range.
														 ($playerSquad[%characterIndex].weapon).currentAmmo				SPC		// Current Ammo/In Magazine.
														 getWord(($playerSquad[%characterIndex].weapon).weaponType, 10)	SPC		// Reload/AP Cost.
														 getWord(($playerSquad[%characterIndex].weapon).weaponType, 11)	SPC		// Fire Cost.
														 $playerSquad[%characterIndex].weapon.projectileType;						// Projectile Type.
	randomLaserModChatter(%characterIndex);
	setSquadMemberIndexFacing($playerSquad[%characterIndex].facing, $playerSquad[%characterIndex], %characterIndex);	// To enforce new weapon animation sprite.
	//playAudio("Audio:plasmaMod", EffectsAudioVolume.value);
}

// Executes Plasma Mod use effect.
function usePlasmaMod ()
{//echo("usePlasmaMod()");
	// Engineer.
	%damageBoost = 0;
	if ($playerSquad[$activeCharacter].characterClass == 4)
	{
		if (getWord($playerSquad[$activeCharacter].talents, 7) == 1)		// Tech Upgrade.
		{
			%damageBoost += $techUpgradePlasmaModDamageBoost;
		}
	}
	if ($playerSquad[$activeCharacter].weapon.projectileType == $weaponProjectilePlasmaType - 1)
	{
		$playerSquad[$activeCharacter].weapon.projectileType = $weaponProjectilePlasmaType;
		%newDamage = getWord(($playerSquad[$activeCharacter].weapon).weaponType, 2) + getRandom(3, 6) + %damageBoost;
		$playerSquad[$activeCharacter].weapon.damage = %newDamage;
		($playerSquad[$activeCharacter].weapon).weaponType = getWord(($playerSquad[$activeCharacter].weapon).weaponType,  0)	SPC		// Name.
															 getWord(($playerSquad[$activeCharacter].weapon).weaponType,  1)	SPC		// Squad Member.
															 %newDamage															SPC		// Damage.
															 getWord(($playerSquad[$activeCharacter].weapon).weaponType,  3)	SPC		// Range.
															 getWord(($playerSquad[$activeCharacter].weapon).weaponType,  4)	SPC		// Accuracy.
															 getWord(($playerSquad[$activeCharacter].weapon).weaponType,  5)	SPC		// Imagemap.
															 getWord(($playerSquad[$activeCharacter].weapon).weaponType,  6)	SPC		// Ammo/Magazine Capacity.
															 getWord(($playerSquad[$activeCharacter].weapon).weaponType,  7)	SPC		// Capacity/Max Ammo.
															 getWord(($playerSquad[$activeCharacter].weapon).weaponType,  8)	SPC		// Damage Range.
															 ($playerSquad[$activeCharacter].weapon).currentAmmo				SPC		// Current Ammo/In Magazine.
															 getWord(($playerSquad[$activeCharacter].weapon).weaponType, 10)	SPC		// Reload/AP Cost.
															 getWord(($playerSquad[$activeCharacter].weapon).weaponType, 11)	SPC		// Fire Cost.
															 $playerSquad[$activeCharacter].weapon.projectileType;						// Projectile Type.
		randomPlasmaModChatter($activeCharacter);
		playAudio("Audio:plasmaMod", EffectsAudioVolume.value);
		setSquadMemberIndexFacing($playerSquad[$activeCharacter].facing, $playerSquad[$activeCharacter], $activeCharacter);	// To enforce new weapon animation sprite.
		// Visual effect!
	}
	else
	{
		$playerSquad[$activeCharacter].currentActionPoints++;
		schedule(50, 0, "giveItem", 24);
		randomCantUseModUpgradeChatter($activeCharacter);
		playAudio("Audio:grabItem", EffectsAudioVolume.value);
	}
	updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "used" SPC $itemColor @ "Plasma Mod" @ $defaultColor @ "!");
}
function setSquadMemberPlasmaWeapon (%characterIndex)
{
	$playerSquad[%characterIndex].weapon.projectileType = $weaponProjectilePlasmaType;
	%newDamage = getWord(($playerSquad[%characterIndex].weapon).weaponType, 2) + getRandom(3, 6) + %damageBoost;
	$playerSquad[%characterIndex].weapon.damage = %newDamage;
	($playerSquad[%characterIndex].weapon).weaponType = getWord(($playerSquad[%characterIndex].weapon).weaponType,  0)	SPC		// Name.
														 getWord(($playerSquad[%characterIndex].weapon).weaponType,  1)	SPC		// Squad Member.
														 %newDamage														SPC		// Damage.
														 getWord(($playerSquad[%characterIndex].weapon).weaponType,  3)	SPC		// Range.
														 getWord(($playerSquad[%characterIndex].weapon).weaponType,  4)	SPC		// Accuracy.
														 getWord(($playerSquad[%characterIndex].weapon).weaponType,  5)	SPC		// Imagemap.
														 getWord(($playerSquad[%characterIndex].weapon).weaponType,  6)	SPC		// Ammo/Magazine Capacity.
														 getWord(($playerSquad[%characterIndex].weapon).weaponType,  7)	SPC		// Capacity/Max Ammo.
														 getWord(($playerSquad[%characterIndex].weapon).weaponType,  8)	SPC		// Damage Range.
														 ($playerSquad[%characterIndex].weapon).currentAmmo				SPC		// Current Ammo/In Magazine.
														 getWord(($playerSquad[%characterIndex].weapon).weaponType, 10)	SPC		// Reload/AP Cost.
														 getWord(($playerSquad[%characterIndex].weapon).weaponType, 11)	SPC		// Fire Cost.
														 $playerSquad[%characterIndex].weapon.projectileType;						// Projectile Type.
	randomPlasmaModChatter(%characterIndex);
	setSquadMemberIndexFacing($playerSquad[%characterIndex].facing, $playerSquad[%characterIndex], %characterIndex);	// To enforce new weapon animation sprite.
	//playAudio("Audio:plasmaMod", EffectsAudioVolume.value);
}

// Executes MedStim use effect.
function useMedStim ()
{//echo("useMedStim()");
	playAudio("Audio:olivineRain", EffectsAudioVolume.value);
	$playerSquad[$activeCharacter].maxHealth++;
	$playerSquad[$activeCharacter].currentHealth = $playerSquad[$activeCharacter].maxHealth;
	$playerSquad[$activeCharacter].maxActionPoints++;
	$playerSquad[$activeCharacter].currentActionPoints = $playerSquad[$activeCharacter].maxActionPoints;
	updateSquadMemberFloatingText();
	setIndividualDialoguePopup($playerSquad[$activeCharacter], "Heavy stuff...", "dialogue");
	//scrollInfoDescriptionText($playerSquad[$activeCharacter], "ACTION UP!");
	scrollInfoText($playerSquad[$activeCharacter], "HEALTH UP!");
	updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "used" SPC $itemColor @ "Med Stim" @ $defaultColor @ "!");
}
// Executes Statbooster use effect.
function useStatbooster ()
{//echo("useStatbooster()");
	playAudio("Audio:olivineRain", EffectsAudioVolume.value);
	$playerSquad[$activeCharacter].statpoints++;
	setIndividualDialoguePopup($playerSquad[$activeCharacter], "Good stuff!", "dialogue");
	//scrollInfoDescriptionText($playerSquad[$activeCharacter], "STAT UP!");
	scrollInfoText($playerSquad[$activeCharacter], "STAT UP!");
	updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "used" SPC $itemColor @ "Statbooster" @ $defaultColor @ "!");
}
// Executes Skillbooster use effect.
function useSkillbooster ()
{//echo("Skillbooster()");
	playAudio("Audio:olivineRain", EffectsAudioVolume.value);
	$playerSquad[$activeCharacter].skillpoints++;
	setIndividualDialoguePopup($playerSquad[$activeCharacter], "Good stuff!", "dialogue");
	//scrollInfoDescriptionText($playerSquad[$activeCharacter], "SKILL UP!");
	scrollInfoText($playerSquad[$activeCharacter], "SKILL UP!");
	updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "used" SPC $itemColor @ "Skillbooster" @ $defaultColor @ "!");
}
// Executes Health Serum use effect.
function useHealthSerum ()
{//echo("useHealthSerum()");
	playAudio("Audio:olivineRain", EffectsAudioVolume.value);
	$playerSquad[$activeCharacter].currentHealth = $playerSquad[$activeCharacter].maxHealth + 5;
	setIndividualDialoguePopup($playerSquad[$activeCharacter], "Oh yeah. that hit the spot!", "dialogue");
	//scrollInfoDescriptionText($playerSquad[$activeCharacter], "HEALTH UP!");
	scrollInfoText($playerSquad[$activeCharacter], "HEALTH UP!");
	updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "used" SPC $itemColor @ "Health Serum" @ $defaultColor @ "!");
}
// Executes Action Serum use effect.
function useActionSerum ()
{//echo("useActionSerum()");
	playAudio("Audio:olivineRain", EffectsAudioVolume.value);
	$playerSquad[$activeCharacter].currentActionPoints = $playerSquad[$activeCharacter].maxActionPoints + 2;
	setIndividualDialoguePopup($playerSquad[$activeCharacter], "Tastes like... pineapple!", "dialogue");
	//scrollInfoDescriptionText($playerSquad[$activeCharacter], "ACTION UP!");
	scrollInfoText($playerSquad[$activeCharacter], "ACTION UP!");
	updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "used" SPC $itemColor @ "Action Serum" @ $defaultColor @ "!");
}
// Executes Hyper Health use effect.
function useHyperHealth ()
{//echo("useHyperHealth()");
	playAudio("Audio:olivineRain", EffectsAudioVolume.value);
	$playerSquad[$activeCharacter].currentHealth *= 2;
	setIndividualDialoguePopup($playerSquad[$activeCharacter], "HNNNGGGHHH!!", "dialogue");
	//scrollInfoDescriptionText($playerSquad[$activeCharacter], "HYPER HEALTH!");
	scrollInfoText($playerSquad[$activeCharacter], "HYPER HEALTH!");
	updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "used" SPC $itemColor @ "Hyper Health" @ $defaultColor @ "!");
}
// Executes Hyper Action use effect.
function useHyperAction ()
{//echo("useHyperAction()");
	playAudio("Audio:olivineRain", EffectsAudioVolume.value);
	$playerSquad[$activeCharacter].currentActionPoints++;		// HACK: To make it more intuitive what AP the marine actually recieves after item use!
	$playerSquad[$activeCharacter].currentActionPoints *= 2;
	setIndividualDialoguePopup($playerSquad[$activeCharacter], "HO-LY $#&T!!", "dialogue");
	//scrollInfoDescriptionText($playerSquad[$activeCharacter], "HYPER ACTION!");
	scrollInfoText($playerSquad[$activeCharacter], "HYPER ACTION!");
	updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "used" SPC $itemColor @ "Hyper Action" @ $defaultColor @ "!");
}

//-----------------------------------------------------

function useItemByType (%itemType)
{//echo("useItemByType()");
	$GlobalStats.totalItemsUsed++;
	switch (%itemType)
	{
		case  0:	useMedicalKit();
		case  1:	useAmmoCrate();
		case  2:	useBombBox();
		case  3:	useSentryCase();
		case  4:	useQuikGrow();
		case  5:	useAdrenalShot();
		case  6:	usePainPills();
		case  7:	useCanteen();
		case  8:	useFlashGrenade();
		case  9:	useVacCap();
		case 10:	useGrenade();
		case 11:	usePDA();
		case 12:	useOxygenTank();
		case 13:	usePowerCore();
		case 14:	useLongsight();
		case 15:	useShortsight();
		case 16:	useArmorKit();
		case 17:	useLasershot();
		case 18:	useHourglass();
		case 19:	usePaintingVox();
		case 20:	usePlasticExplosive();
		case 21:	useHulkSuit();
		case 22:	useMultipass();
		case 23:	useLaserMod();
		case 24:	usePlasmaMod();
		case 25:	useMedStim();
		case 26:	useStatbooster();
		case 27:	useSkillbooster();
		case 28:	useHealthSerum();
		case 29:	useActionSerum();
		case 30:	useHyperHealth();
		case 31:	useHyperAction();
	}
	
	// Clear fire control.
	clearFireControl();
	
	// Update display guis.
	initializeReadout();
	updateSquadInfoGui();
	
	// Call to monster AI code.
	scheduleMonsterTurn();
}
function getRandomTreasureItemType ()
{//echo("getRandomTreasureItemType()");
	%itemType = getRandom(0, 11);
	switch (%itemType)
	{
		case  0: %itemType = 14;		// Longsight.
		case  1: %itemType = 15;		// Shortsight.
		case  2: %itemType = 18;		// Hourglasss.
		case  3: %itemType =  5;		// Adrenal Shot.
		case  4: %itemType =  7;		// Canteen.
		case  5: %itemType = 10;		// Grenade.
		case  6: %itemType = 11;		// PDA.
		case  7: %itemType = 16;		// Armor Kit.
		case  8: %itemType = 17;		// Lasershot.
		case  9: %itemType = 20;		// Plastic Explosive.
		case 10: %itemType = 26;		// Statboost.
		case 11: %itemType = 27;		// Skillboost.
		case 12: %itemType = 28;		// Health Serum.
		case 13: %itemType = 29;		// Action Serum.
		case 14: %itemType = 30;		// Hyper Health.
		case 15: %itemType = 31;		// Hyper Action.
	}
	//%itemType = 26;		// For testing purposes only!
	return %itemType;
}
function getItemTypeTooltip (%itemType)
{
	switch (%itemType)
	{
		case -1:	// Weapon.
			%toolTip = %item.name;
			
		case -2:	// Armor.
			%toolTip = %item.name;
			
		case  0:	%toolTip = "Medkit: Restores up to " @ $medkitHealthGain @ " HP.";//useMedicalKit();
		case  1:	%toolTip = "Ammo Crate: Gives each squad member some ammunition.";//useAmmoCrate();
		case  2:	%toolTip = "";//useBombBox();
		case  3:	%toolTip = "MK-44 Sentry Turret";//useSentryCase();
		case  4:	%toolTip = "";//useQuikGrow();
		case  5:	%toolTip = "Adrenal Shot: Restores all AP.";//useAdrenalShot();
		case  6:	%toolTip = "";//usePainPills();
		case  7:	%toolTip = "Canteen: Restores 2 health and 2 AP.";//useCanteen();
		case  8:	%toolTip = "";//useFlashGrenade();
		case  9:	%toolTip = "";//useVacCap();
		case 10:	%toolTip = "Grenade: Explosive damage up to two tiles away. Hippie approved - cannot kill aoe targets.";//useVacCap();
		case 11:	%toolTip = "PDA: Access personal information.";//useVacCap();
		case 12:	%toolTip = "Oxygen Tank: Restores all oxygen and 3 AP.";
		case 13:	%toolTip = "Power Core: Powers the ship's systems.";
		case 14:	%toolTip = "Longsight: Mod weapon range by +1.";
		case 15:	%toolTip = "Shortsight: Mod weapon range by -1.";
		case 16:	%toolTip = "Armor Kit: Mod armor by +1.";
		case 17:	%toolTip = "Lasershot: Single shot, 4 range, 30 damage.";
		case 18:	%toolTip = "Hourglass: Restores squad AP. Enemies temporarily gain 1 AP.";
		case 19:	%toolTip = "Mona Vox: ???";
		case 20:	%toolTip = "Plastic Explosive: Place on door. Destroy stuff near door.";
		case 21:	%toolTip = "Hulk Suit: It looks heavy.";
		case 22:	%toolTip = "Multipass: Powers on the elevator.";
		case 23:	%toolTip = "Laser Mod: Upgrades Ballistic weapons with random accuracy.";
		case 24:	%toolTip = "Plasma Mod: Upgrades Laser weapons with random damage.";
		case 25:	%toolTip = "Medstim: Increases HP and AP by 1 and restores both.";
		case 26:	%toolTip = "Statboost: Grants 1 stat point.";
		case 27:	%toolTip = "Skillboost: Grants 1 skill point.";
		case 28:	%toolTip = "Health Serum: Restores HP to max +5.";
		case 29:	%toolTip = "Action Serum: Restores AP to max +2.";
		case 30:	%toolTip = "Hyper Health: Grants 200% of current HP.";
		case 31:	%toolTip = "Hyper Action: Grants 200% of current AP.";
	}
	return %toolTip;
}
function getItemName (%itemType)
{//echo("getItemName()");
	switch (%itemType)
	{
		case  0:	return "Medkit";
		case  1:	return "Ammo-Crate";
		case  2:	return "Bomb-Box";
		case  3:	return "Sentry-Case";
		case  4:	return "Quik-Grow";
		case  5:	return "Adrenal-Shot";
		case  6:	return "Pain-Pills";
		case  7:	return "Canteen";
		case  8:	return "Flash-Grenade";
		case  9:	return "Vac-Cap";
		case 10:	return "Grenade";
		case 11:	return "PDA";
		case 12:	return "Oxygen-Tank";
		case 13:	return "Power-Core";
		case 14:	return "Longsight";
		case 15:	return "Shortsight";
		case 16:	return "Armor-Kit";
		case 17:	return "Lasershot";
		case 18:	return "Hourglass";
		case 19:	return "Mona-Vox";
		case 20:	return "Plastic Explosive";
		case 21:	return "Hulk-Suit";
		case 22:	return "Multipass";
		case 23:	return "Lasermod";
		case 24:	return "Plasmamod";
		case 25:	return "Medstim";
		case 26:	return "Statbooster";
		case 27:	return "Skillbooster";
		case 28:	return "Health-Serum";
		case 29:	return "Action-Serum";
		case 30:	return "Hyper-Health";
		case 31:	return "Hyper-Action";
	}
}
function getItemBitmap (%itemType, %itemImageMap)
{//echo("getItemBitmap()");//echo("%itemType =" SPC %itemType);//echo("%itemImageMap =" SPC %itemImageMap);
	switch (%itemType)
	{
		case -1:	// Weapon.
			%weaponBitmap = getWeaponBitmapFromImagemap(%itemImageMap);
			return %weaponBitmap;
		case -2:	// Armor.
			%armorBitmap = getArmorBitmapFromImagemap(%itemImageMap);
			return %armorBitmap;
		
		case  0:	return "../../../Sprites/1/assets/images/Items/Item-Medkit.png";
		case  1:	return "../../../Sprites/1/assets/images/Items/Item-Ammokit.png";
		case  2:	return "../../../Sprites/1/assets/images/Bomb Box (32x32).png";
		case  3:	return "../../../Sprites/1/assets/images/Sentry Case (32x32).png";
		case  4:	return "../../../Sprites/1/assets/images/Quik-Grow (32x32).png";
		case  5:	return "../../../Sprites/1/assets/images/Items/Item-AdrenalShot.png";
		case  6:	return "../../../Sprites/1/assets/images/Pain Pills (32x32).png";
		case  7:	return "../../../Sprites/1/assets/images/Items/Item-Canteen.png";
		case  8:	return "../../../Sprites/1/assets/images/Flash Grenade (32x32).png";
		case  9:	return "../../../Sprites/1/assets/images/Vac Cap (32x32).png";
		case 10:	return "../../../Sprites/1/assets/images/Items/Item-Grenade.png";
		case 11:	return "../../../Sprites/1/assets/images/Environment/PDA-1.png";
		case 12:	return "../../../Sprites/1/assets/images/Items/OxygenTank.png";
		case 13:	return "../../../Sprites/1/assets/images/Items/PowerCore.png";
		case 14:	return "../../../Sprites/1/assets/images/Items/Longsight.png";
		case 15:	return "../../../Sprites/1/assets/images/Items/Shortsight.png";
		case 16:	return "../../../Sprites/1/assets/images/Items/Plating.png";
		case 17:	return "../../../Sprites/1/assets/images/Items/Lasershot.png";
		case 18:	return "../../../Sprites/1/assets/images/Items/Hourglass.png";
		case 19:	return "../../../Sprites/1/assets/images/Items/Paintings/PaintingVox.png";
		case 20:	return "../../../Sprites/1/assets/images/Items/PlasticExplosive.png";
		case 21:	return "../../../Sprites/1/assets/images/Items/Item-HulkSuit.png";
		case 22:	return "../../../Sprites/1/assets/images/Items/MultipassPickup.png";
		case 23:	return "../../../Sprites/1/assets/images/Items/LaserModPickup.png";
		case 24:	return "../../../Sprites/1/assets/images/Items/PlasmaModPickup.png";
		case 25:	return "../../../Sprites/1/assets/images/Items/Item-MedStim.png";
		case 26:	return "../../../Sprites/1/assets/images/Items/Item-Statbooster.png";
		case 27:	return "../../../Sprites/1/assets/images/Items/Item-Skillbooster.png";
		case 28:	return "../../../Sprites/1/assets/images/Items/Item-HealthSerum.png";
		case 29:	return "../../../Sprites/1/assets/images/Items/Item-ActionSerum.png";
		case 30:	return "../../../Sprites/1/assets/images/Items/Item-HyperHealth.png";
		case 31:	return "../../../Sprites/1/assets/images/Items/Item-HyperAction.png";
	}
}
function getItemPickupImage (%itemType)
{//echo("getItemPickupImage()");
	// Switch based on item type.
	switch (%itemType)
	{
		case 0:		return "Sprites:MedkitPickup";
		case 1:		return "Sprites:AmmokitPickup";
		case 2:		return "Sprites:Bomb_Box_32x32";
		case 3:		return "Sprites:SentryCasePickup";
		case 4:		return "Sprites:QuikGrow_32x32";
		case 5:		return "Sprites:AdrenalShotPickup";
		case 6:		return "Sprites:Pain_Pills_32x32";
		case 7:		return "Sprites:CanteenPickup";
		case 8:		return "Sprites:Flash_Grenade_32x32";
		case 9:		return "Sprites:Vac_Cap_32x32";	
		case 10:	return "Sprites:GrenadePickup";		
		case 11:	return "Sprites:PDAPickup";		
		case 12:	return "Sprites:OxygenTankPickup";		
		case 13:	return "Sprites:PowerCorePickup";		
		case 14:	return "Sprites:LongsightPickup";		
		case 15:	return "Sprites:ShortsightPickup";		
		case 16:	return "Sprites:PlatingPickup";		
		case 17:	return "Sprites:LasershotPickup";		
		case 18:	return "Sprites:HourglassPickup";		
		case 19:	return "Sprites:PaintingVoxPickup";		
		case 20:	return "Sprites:PlasticExplosivePickup";		
		case 21:	return "Sprites:HulkSuitPickup";		
		case 22:	return "Sprites:MultipassPickup";		
		case 23:	return "Sprites:LaserModPickup";
		case 24:	return "Sprites:PlasmaModPickup";
		case 25:	return "Sprites:MedStimPickup";
		case 26:	return "Sprites:StatboosterPickup";
		case 27:	return "Sprites:SkillboosterPickup";
		case 28:	return "Sprites:HealthSerum";
		case 29:	return "Sprites:ActionSerum";
		case 30:	return "Sprites:HyperHealth";
		case 31:	return "Sprites:HyperAction";
	}
}
function createRandomItem (%itemType, %position, %xIndex, %yIndex)
{//echo("createRandomItem()");
	// We also use this function to create inventory item sprites. If the x and y indices come in as -500, -500 don't change the positions!
	if (%xIndex != -500	&&
		%yIndex != -500)
	{
		%position = getTilePositionFromIndices(%xIndex, %yIndex);
	}
	
	%randomItem = new Sprite()
	{
		name = getItemName(%itemType);
		SceneLayer = $ItemLayer;
		scenegraph = levelSceneGraph;
		itemType = %itemType;
		value = 0;
		image = getItemPickupImage(%itemType);
		position = %position;
		equipSlot = -1;									// -1 means can't be equipped.
		canBeUsed = 0;
		size = "4 4";
		xIndex = %xIndex;
		yIndex = %yIndex;
		visible = 1;
	};
	addObjectToScene(%randomItem);
	return %randomItem;
}
function isItemAtLocation (%xIndex, %yIndex)
{//echo("isItemAtLocation()");
	for (%i = 0; %i < $mapItems.getCount(); %i++)
	{
		%item = $mapItems.getObject(%i);
		if (%item.xIndex == %xIndex	&&
			%item.yIndex == %yIndex)
		{
			return %item;
		}
	}
	return null;
}
function tileClearOfItems (%xIndex, %yIndex)
{//echo("tileClearOfItems()");
	for (%i = 0; %i < $mapItems.getCount(); %i++)
	{
		%item = $mapItems.getObject(%i);
		if ($item.xIndex == %xIndex	&&
			$item.yIndex == %yIndex)
		{
			return false;
		}
	}
	return true;
}
function spawnItems ()
{//echo("spawnItems()");
	// Global that holds all map items.
	$mapItems = new SimSet();
return;	// Don't spawn any more loose items on the ground!
	// The number of items to spawn on the map.
	%itemsToSpawn = $baseItemSpawn + getRandom(0, $dungeonLevel);
	%attempts = 0;
	// Spawn all the items.
	for (%i = 0; %i < %itemsToSpawn; %i++)
	{
		// Pick a random location.
		%xIndex = getRandom(2, $mazeDimension - 1);	// Upper left hand corner is the squad! Don't spawn items on top of them.
		%yIndex = getRandom(2, $mazeDimension - 1);	// Upper left hand corner is the squad! Don't spawn items on top of them.
		
		// Make sure we spawn items in appropriate locations.
		if (!isObject($mazeRoom[%xIndex, %yIndex])			&&					// No maze room object at that index means the tile is walkable, but not necessarily open for item spawning!
			tileClearOfItems(%xIndex, %yIndex)				&&					// Check if there is already an item at that location.
			tileClearOfTreasure(%xIndex, %yIndex))
		{
			// Randomize an item.
			%itemType = getRandom(0, 100);		// Currently only 0, 5, 7, 10, and 11 are implemented!
			if (%itemType < 6)
			{
				// Med Kit.
				%itemType = 0;
			}
			else if (%itemType < 16)
			{
				// Adrenal Shot.
				%itemType = 5;
			}
			else if (%itemType < 36)
			{
				// Canteen.
				%itemType = 7;
			}
			else if (%itemType < 50)
			{
				// Grenade.
				%itemType = 10;
			}
			else if (%itemType < 65)
			{
				// Ammo Crate.
				%itemType = 1;
			}
			else if (%itemType < $medkitBaseDropChance)//70)
			{
				// DON'T SPAWN OXYGEN TANKS FOR NOW!
				// Oxygen Tank.
				//%itemType = 12;
				
				// Med Kit.
				%itemType = 0;
			}
			else if (%itemType < 80)
			{
				// DON'T SPAWN POWER CORES FOR NOW!
				// Power Core.
				//%itemType = 13;
				
				// PDA.
				%itemType = 11;
			}
			else if (%itemType < 85)
			{
				// PDA.
				%itemType = 11;
			}
			else if (%itemType < 90)
			{
				// Longsight.
				%itemType = 14;
			}
			else if (%itemType < 95)
			{
				// Shortsight.
				%itemType = 15;
			}
			else
			{
				// Plating.
				%itemType = 16;
			}
			
			// Special case some items (e.g. Med Kits on the Medical Deck).
			if (%itemType != 0	&&
				$overworldLocation $= "Medical Deck")
			{
				%medkitChance = getRandom(1, 100);
				if (%medkitChance <= $medicalDeckMedkitDropChance)
				{
					// Med Kit.
					%itemType = 0;
				}
			}//echo("%itemType =" SPC %itemType);
			//%itemType = 17;		// TESTING PURPOSES ONLY!
			// Spawn a random item at this location.
			%item = createRandomItem(%itemType, getTilePositionFromIndices(%xIndex, %yIndex), %xIndex, %yIndex);
			$mapItems.add(%item);
		}
		else
		{
			%attempts++;
			if (%attempts <= 64)
			{
				%i--;
			}
		}
	}
}
function spawnRandomItem (%xIndex, %yIndex)
{//echo9"spawnRandomItem()");
	// Randomize an item - no weights yet!
	%itemType = getRandom(0, 100);		// Currently only 0, 5, 7, 10, and 11 are implemented!
	if (%itemType < 10)
	{
		// Med Kit.
		%itemType = 0;
	}
	else if (%itemType < 30)
	{
		// Adrenal Shot.
		%itemType = 5;
	}
	else if (%itemType < 50)
	{
		// Canteen.
		%itemType = 7;
	}
	else if (%itemType < 70)
	{
		// Grenade.
		%itemType = 10;
	}
	else if (%itemType < 90)
	{
		// Ammo Crate.
		%itemType = 1;
	}
	else
	{
		// Ammo Crate.
		%itemType = 11;
	}
	//%itemType = 10;		// For testing purposes only!
	%item = createRandomItem(%itemType, getTilePositionFromIndices(%xIndex, %yIndex), %xIndex, %yIndex);
	$mapItems.add(%item);
}