// Handles Inventory.
// Worthless Bums 2012.

/*-----------------------------------------------------------------------------------------------------
 *	Squad Inventory System
 *-----------------------------------------------------------------------------------------------------
 * - The entire Squad shares the inventory.
 * - Inventory size is limited to eight (8) items.
 * - Shape, size, and weight do not matter, only item count.
 * - Ammunition is its separate "inventory" (i.e. not implemented/changed in this file).
 * - Items can be picked up, dropped, equipped, used (and possibly combined).
 */


// Gets item tool tip information text.
function getInventoryItemToolTip (%itemSlot)
{//echo("getInventoryItemToolTip()");
	%item = $squadInventory.getObject(%itemSlot);
	if (%item.itemType == -1	||
		%item.itemType == -2)
	{
		return %item.name;
	}
	else
	{
		return getItemTypeTooltip(%item.itemType);
	}
}

// Handle mouse over events for squad inventory tool tips!
function itemIcon1::onMouseEnter ()
{//echo("itemIcon1::onMouseEnter()");
	itemIcon1.ToolTip = getInventoryItemToolTip(0);
}
function itemIcon2::onMouseEnter ()
{//echo("itemIcon2::onMouseEnter()");
	itemIcon2.ToolTip = getInventoryItemToolTip(1);
}
function itemIcon3::onMouseEnter ()
{//echo("itemIcon3::onMouseEnter()");
	itemIcon3.ToolTip = getInventoryItemToolTip(2);
}
function itemIcon4::onMouseEnter ()
{//echo("itemIcon4::onMouseEnter()");
	itemIcon4.ToolTip = getInventoryItemToolTip(3);
}
function itemIcon5::onMouseEnter ()
{//echo("itemIcon5::onMouseEnter()");
	itemIcon5.ToolTip = getInventoryItemToolTip(4);
}
function itemIcon6::onMouseEnter ()
{//echo("itemIcon6::onMouseEnter()");
	itemIcon6.ToolTip = getInventoryItemToolTip(5);
}
function itemIcon7::onMouseEnter ()
{//echo("itemIcon7::onMouseEnter()");
	itemIcon7.ToolTip = getInventoryItemToolTip(6);
}
function itemIcon8::onMouseEnter ()
{//echo("itemIcon8::onMouseEnter()");
	itemIcon8.ToolTip = getInventoryItemToolTip(7);
}
function itemIcon9::onMouseEnter ()
{//echo("itemIcon9::onMouseEnter()");
	itemIcon9.ToolTip = getInventoryItemToolTip(8);
}
function itemIcon10::onMouseEnter ()
{//echo("itemIcon10::onMouseEnter()");
	itemIcon10.ToolTip = getInventoryItemToolTip(9);
}
function itemIcon11::onMouseEnter ()
{//echo("itemIcon11::onMouseEnter()");
	itemIcon11.ToolTip = getInventoryItemToolTip(10);
}
function itemIcon12::onMouseEnter ()
{//echo("itemIcon12::onMouseEnter()");
	itemIcon12.ToolTip = getInventoryItemToolTip(11);
}


function itemIcon1::onRightClick ()
{//echo("itemIcon1::onRightClick()");
	dropItemFromInventory(0);
}
function itemIcon2::onRightClick ()
{//echo("itemIcon2::onRightClick()");
	dropItemFromInventory(1);
}
function itemIcon3::onRightClick ()
{//echo("itemIcon3::onRightClick()");
	dropItemFromInventory(2);
}
function itemIcon4::onRightClick ()
{//echo("itemIcon4::onRightClick()");
	dropItemFromInventory(3);
}
function itemIcon5::onRightClick ()
{//echo("itemIcon5::onRightClick()");
	dropItemFromInventory(4);
}
function itemIcon6::onRightClick ()
{//echo("itemIcon6::onRightClick()");
	dropItemFromInventory(5);
}
function itemIcon7::onRightClick ()
{//echo("itemIcon7::onRightClick()");
	dropItemFromInventory(6);
}
function itemIcon8::onRightClick ()
{//echo("itemIcon8::onRightClick()");
	dropItemFromInventory(7);
}
function itemIcon9::onRightClick ()
{//echo("itemIcon9::onRightClick()");
	dropItemFromInventory(8);
}
function itemIcon10::onRightClick ()
{//echo("itemIcon10::onRightClick()");
	dropItemFromInventory(9);
}
function itemIcon11::onRightClick ()
{//echo("itemIcon11::onRightClick()");
	dropItemFromInventory(10);
}
function itemIcon12::onRightClick ()
{//echo("itemIcon12::onRightClick()");
	dropItemFromInventory(11);
}

// Test function to print current inventory.
function printInventory ()
{//echo("printInventory()");echo("$squadInventory.getCount() =" SPC $squadInventory.getCount());echo("squadInventory:");
	for (%i = 0; %i < $squadInventory.getCount(); %i++)
	{
		echo($squadInventory.getObject(%i));
	}
}
 
// Called from squadInfoGui when the player clicks on an inventory item.
function useInventoryItem (%itemSlot)
{//echo("useInventoryItem()");
	// Make sure the active character has enough action points to use an item!
	%actionPointCost = getActionPointCost(6);
	if ($playerSquad[$activeCharacter].currentActionPoints >= %actionPointCost)
	{
		setCurrentActionPoints($activeCharacter, $playerSquad[$activeCharacter].currentActionPoints - %actionPointCost);
		%item = $squadInventory.getObject(%itemSlot);
		%itemIndex = getWord(%item.itemType, 0);
		if (%itemIndex == -1)		// Weapons.
		{
			// Set weapon code.
			$currentWeaponCode = %item;
			$currentItemSlot = %itemSlot;
			// Bring up weapon swap gui.
			%weaponGuiPushed = pushWeaponSwapGuiFromInventory(%item);
			if (!%weaponGuiPushed)
			{updateEventText($whiteColor @ "No one can use this weapon" @ $defaultColor @ "!");
				playAudio("Audio:negatory", EffectsAudioVolume.value);
				return false;
			}
			return true;
		}
		else if (%itemIndex == -2)	// Armor.
		{
			// Set armor code.
			$currentArmorCode = %item;
			$currentItemSlot = %itemSlot;
			// Bring up armor swap gui.
			%armorGuiPushed = pushArmorSwapGuiFromInventory(%item);
			if (!%armorGuiPushed)
			{updateEventText($whiteColor @ "No one can use this armor" @ $defaultColor @ "!");
				playAudio("Audio:negatory", EffectsAudioVolume.value);
				return false;
			}
			return true;
		}
		else
		{
			useItemByType(%item.itemType);
		}
		$squadInventory.remove(%item);
		%item.safeDelete();
		updateSquadInfoGui();
		if ($playerSquad[$activeCharacter].currentActionPoints == 0)
		{
			autoSwitchSquadmember();
		}
	}
	else
	{
		playAudio("Audio:negatory", EffectsAudioVolume.value);
	}
}

// Returns true iff there is an item to grab on the specified tile.
function tileHasObjectToGrab (%xIndex, %yIndex)
{//echo("tileHasObjectToGrab()");
	%item = isItemAtLocation(%xIndex, %yIndex);
	if (isObject(%item))
	{//echo("has item!");
		return true;
	}
	else if (isTreasureChestOnTile(%xIndex, %yIndex))
	{//echo("has treasure!");
		return true;
	}
	//echo("no object to grab!");
	return false;
}
// Returns true iff there is an item to grab under the active character.
function hasObjectToGrab ()
{//echo("hasObjectToGrab()");
	%item = isItemAtLocation($playerSquad[$activeCharacter].xIndex, $playerSquad[$activeCharacter].yIndex);
	if (isObject(%item))
	{//echo("has item!");
		return true;
	}
	else if (isTreasureChestOnTile($playerSquad[$activeCharacter].xIndex, $playerSquad[$activeCharacter].yIndex))
	{//echo("has treasure!");
		return true;
	}
	//echo("no object to grab!");
	return false;
}
// Attempts to pick up an item.
function grabItem ()
{//echo("grabItem()");
	// Hold this information for an if statement below!
	%actionMember = $activeCharacter;
	// Check map tile for item.
	%item = isItemAtLocation($playerSquad[%actionMember].xIndex, $playerSquad[%actionMember].yIndex);
	// x and y indicies for detecting treasure chest location below!
	%xIndex = $playerSquad[%actionMember].xIndex;
	%yIndex = $playerSquad[%actionMember].yIndex;
	// Check for item.
	if (isObject(%item))
	{//echo("There is a" SPC (%mapTile.item).name SPC "here!");
		$GlobalStats.totalItemsGrabbed++;
		if (!addItemToInventory(%item, %item.itemType))
		{
			playAudio("Audio:nothingToGrab", EffectsAudioVolume.value);
			return;
		}
		
		if (%item.itemType == 1 	||
			%item.itemType == 5		||
			%item.itemType == 12)
		{updateEventText($squadColor @ $playerSquad[%actionMember].name SPC $friendlyActionColor @ "picked up an" SPC $itemColor @ %item.name @ $defaultColor @ "!");
		}
		else
		{updateEventText($squadColor @ $playerSquad[%actionMember].name SPC $friendlyActionColor @ "picked up a" SPC $itemColor @ %item.name @ $defaultColor @ "!");
		}
		playAudio("Audio:grabItem", EffectsAudioVolume.value);
		$mapItems.remove(%item);
		initializeReadout();
		updateSquadInfoGui();
	}
	// Check for treasure.
	else if (isTreasureChestOnTile(%xIndex, %yIndex))
	{
		getTreasureOnTile(%xIndex, %yIndex);
		initializeReadout();
		updateSquadInfoGui();
	}
	// No items or treasure!
	else
	{updateEventText($whiteColor @ "There is nothing to grab here.");
		playAudio("Audio:nothingToGrab", EffectsAudioVolume.value);
		return;
	}
	%actionPointCost = getActionPointCost(5);
	if ($playerSquad[$activeCharacter].currentActionPoints >= %actionPointCost)
	{
		setCurrentActionPoints($activeCharacter, $playerSquad[$activeCharacter].currentActionPoints - %actionPointCost);
		initializeReadout();
		if ($playerSquad[$activeCharacter].currentActionPoints == 0)
		{
			autoSwitchSquadmember();
		}
	}
	else
	{updateEventText($whiteColor @ "Not enough action points to grab item" @ $defaultColor @ "!");
		playAudio("Audio:nothingToGrab", EffectsAudioVolume.value);
		return;
	}
	triggerContextSensitiveUI(false);
	scheduleMonsterTurn();
}
// Attempts to auto-grab up an item.
function autoGrabItem ()
{//echo("autoGrabItem()");
	if ($Game::AutoLoot)
	{
		%actionMember = $activeCharacter;
		%item = isItemAtLocation($playerSquad[%actionMember].xIndex, $playerSquad[%actionMember].yIndex);
		%xIndex = $playerSquad[%actionMember].xIndex;
		%yIndex = $playerSquad[%actionMember].yIndex;
		if (isObject(%item))
		{//echo("There is a" SPC (%mapTile.item).name SPC "here!");
			$GlobalStats.totalItemsGrabbed++;
			if (!addItemToInventory(%item, %item.itemType))
			{
				$mapItems.remove(%item);
				return;
			}
			if (%item.itemType == 1 	||
				%item.itemType == 5		||
				%item.itemType == 12)
			{
				updateEventText($squadColor @ $playerSquad[%actionMember].name SPC $friendlyActionColor @ "picked up an" SPC $itemColor @ %item.name @ $defaultColor @ "!");
			}
			else
			{
				updateEventText($squadColor @ $playerSquad[%actionMember].name SPC $friendlyActionColor @ "picked up a" SPC $itemColor @ %item.name @ $defaultColor @ "!");
			}
			playAudio("Audio:grabItem", EffectsAudioVolume.value);
			$mapItems.remove(%item);
			initializeReadout();
			updateSquadInfoGui();
			autoGrabItem();
		}
		else if (isTreasureChestOnTile(%xIndex, %yIndex))
		{
			%autograbAgain = getTreasureOnTile(%xIndex, %yIndex);
			initializeReadout();
			updateSquadInfoGui();
			if (%autograbAgain)
			{
				schedule(100, 0, "autoGrabItem");
			}
		}
		else
		{//echo("There is nothing to grab here!");
			return;
		}
	}
}

// Returns true iff the monster has stepped on a booby trapped item!
function monsterOnTrap (%monster)
{//echo("monsterOnTrap()");
//echo("%monster.getPosition() =" SPC %monster.getPosition());
	for (%i = 0; %i < $mapItems.getCount(); %i++)
	{
		%item = $mapItems.getObject(%i);
		if (%item.boobyTrapped)
		{//echo("found booby trapped item!");
			if (%monster.getPosition() $= %item.getPosition())
			{updateEventText($monsterColor @ %monster.name SPC $friendlyActionColor @ " takes " @ $damageColor @ $dummySuitDamage @ " damage " @ $friendlyActionColor @ " from Dummy Suit" @ $defaultColor @ "!");
				%item.setVisible(false);
				%item.safeDelete();
				dealEnvironmentalDamage(%monster, $dummySuitDamage, true);
			}
		}
	}
}

// Attempts to drop an item.
function dropItemFromInventory (%itemSlot)
{//echo("dropItemFromInventory()");
	// Hold this information for an if statement below!
	%actionMember = $activeCharacter;
	// Check if the player has enough AP to pick up an item.
	%actionPointCost = getActionPointCost(7);
	if ($playerSquad[$activeCharacter].currentActionPoints >= %actionPointCost)
	{
		// Deduct action point cost.
		setCurrentActionPoints($activeCharacter, $playerSquad[$activeCharacter].currentActionPoints - %actionPointCost);
		
		if ($playerSquad[$activeCharacter].currentActionPoints == 0)
		{
			autoSwitchSquadmember();
		}
	}
	else
	{updateEventText($whiteColor @ "Not enough action points to drop item!");
		return;
	}
	// Check map tile for item.
	// Crap. This can fail because of the auto switch function call above!
	//%mapTile = $mazeRoom[$playerSquad[%actionMember].xIndex, $playerSquad[%actionMember].yIndex];
	//echo("$playerSquad[%actionMember].getPosition() =" SPC $playerSquad[%actionMember].getPosition());
	//%item = isItemAtLocation($playerSquad[%actionMember].getPosition());
	%item = isItemAtLocation($playerSquad[%actionMember].xIndex, $playerSquad[%actionMember].yIndex);
	//if (!isObject(%item))
	//{//echo("There is no item on this tile.");
		// Grab the item.
		%item = $squadInventory.getObject(%itemSlot);
		if (!isObject(%item))
		{
			return;
		}
		// Grab relevant data to respawn the item on the map.
		%itemName = %item.name;
		%itemType = %item.itemType;
		%itemPosition = $playerSquad[%actionMember].getPosition();//%item.getPosition();
		%itemXIndex = $playerSquad[%actionMember].xIndex;//%item.xIndex;
		%itemYIndex = $playerSquad[%actionMember].yIndex;//%item.yIndex;
		
		// Delete the item from the squad inventory.
		$squadInventory.remove(%item);
		%item.safeDelete();
		
		// If tile is an open grate, don't place the item on the floor tile!
		if (isTileBurstGrating(%itemXIndex, %itemYIndex))
		{//echo("ITEM DROPPED INTO BURST GRATING TILE!");
			playAudio("Audio:uiCrash", EffectsAudioVolume.value);
			setIndividualDialoguePopup($playerSquad[$activeCharacter], "...oops.", "dialogue");
			updateSquadInfoGui();
			initializeReadout();
			updateSquadInfoGui();
			return;
		}
		
		/*
		// Mark map tile as having an item.
		%item = createRandomItem(%itemType, %itemPosition, %itemXIndex, %itemYIndex);
		%item.setVisible(true);
		$mapItems.add(%item);
		*/
		
		if (%itemType == -1)		// Weapon.
		{
			// Check for Sentry Turret talent status!
			if ($squadSentryTurret &&
				$playerSquad[$activeCharacter].characterClass == 0)
			{
				// Create sentry turret.
				%weapon = $playerSquad[$activeCharacter].weapon;
				createSentryTurret($playerSquad[$activeCharacter].getPosition(), %weapon.damage, %weapon.range, %weapon.accuracy, $playerSquad[$activeCharacter].facing);
				
				// Update display guis.
				initializeReadout();
				updateSquadInfoGui();
				// Autoswitch squadmember if this character is out of action points.
				if ($playerSquad[$activeCharacter].currentActionPoints == 0)
				{
					autoSwitchSquadmember();
				}
				updateEventText($whiteColor @ "Sentry Turret dropped" @ $defaultColor @ "!");
				return;
			}
			
			// Create weapon.
			// Create proper imagemap string for weapon icon.
			%imageIconName = %item.imageMapName;
			// Strip "ImageMap".
			%imageIconName = strreplace(%imageIconName, "ImageMap", "");
			// Add "Icon".
			//%imageIconName = %imageIconName @ "Icon";
			// "Add "ImageMap".
			%imageIconName = %imageIconName @ "ImageMap";
			%weaponItem = new Sprite()
			{
				name = %item.name;
				SceneLayer = $ItemLayer;
				scenegraph = levelSceneGraph;
				itemType = %itemType;					// 0 is a medical kit.
				value = 0;
				imageMap = %imageIconName;
				position = %itemPosition;
				equipSlot = -1;							// -1 means can't be equipped.
				canBeUsed = 0;
				size = "4 4";
				xIndex = %itemXIndex;
				yIndex = %itemYIndex;
				visible = 1;
				
				weapon = %item;
			};
			addObjectToScene(%weaponItem);
			%item = %weaponItem;
		}
		else if (%itemType == -2)	// Armor.
		{
			%boobyTrapped = false;
			if (%actionMember == 0	&&
				$squadDummySuit)
			{
				%boobyTrapped = true;
			}
			// Create armor.
			%armorItem = new Sprite()
			{
				name = %item.name;
				SceneLayer = $ItemLayer;
				scenegraph = levelSceneGraph;
				itemType = %itemType;					// 0 is a medical kit.
				value = 0;
				imageMap = %item.imageMap;
				position = %itemPosition;
				equipSlot = -1;							// -1 means can't be equipped.
				canBeUsed = 0;
				size = "4 4";
				xIndex = %itemXIndex;
				yIndex = %itemYIndex;
				visible = 1;
				
				armor = %item;
				
				boobyTrapped = %boobyTrapped;
			};
			addObjectToScene(%armorItem);
			%item = %armorItem;
		}
		else
		{
			// Create other item.
			%item = createRandomItem(%itemType, %itemPosition, %itemXIndex, %itemYIndex);
		}
		
		// Make item visible.
		%item.setVisible(true);
		// Add item to mapItems!
		$mapItems.add(%item);
		
		// Update the Squad Info Gui.
		updateSquadInfoGui();
		
		if (%item.itemType == 1 ||
			%item.itemType == 5)
		{
			// Update event log.
			updateEventText($squadColor @ $playerSquad[%actionMember].name SPC $friendlyActionColor @ "dropped an" SPC $itemColor @ %item.name @ $defaultColor @ "!");
		}
		else
		{
			// Update event log.
			updateEventText($squadColor @ $playerSquad[%actionMember].name SPC $friendlyActionColor @ "dropped a" SPC $itemColor @ %item.name @ $defaultColor @ "!");
		}
		
		// Update display guis.
		initializeReadout();
		updateSquadInfoGui();
		
		/*
		// Check if there is an adjacent Bloat to eat the dropped item!
		%character = $playerSquad[%actionMember];
		if (isMonsterTypeNearTile(2, %character.xIndex, %character.yIndex))
		{
			// Eat the item!
			%itemName = %item.name;
			$mapItems.remove(%item);
			%item.safeDelete();
			%item = null;
			
			// Pop the squad information gui!
			canvas.popDialog(squadInfoGui);
			// Unpause game.
			$timescale = 1;
			// Play Bloat eat item sound effect.
			playAudio("Audio:bloatEatItem", EffectsAudioVolume.value);
			// Update event log.
			updateEventText($monsterColor @ "Bloat" SPC $hostileActionColor @ "ate the" SPC $itemColor @ %itemName @ $defaultColor @ "!");
			updateEventText($monsterColor @ "Bloat" SPC $hostileActionColor @"looks like it might explode" @ $defaultColor @ "!");
		}
		*/
		
		// Autoswitch squadmember if this character is out of action points.
		if ($playerSquad[$activeCharacter].currentActionPoints == 0)
		{
			autoSwitchSquadmember();
		}
	/*}
	else
	{//echo("You can't drop an item here!");
	//echo("$playerSquad[%actionMember].getPosition() =" SPC$playerSquad[%actionMember].getPosition());
	//echo("%item.name =" SPC %item.name);
	//echo("%item.getPosition() =" SPC %item.getPosition());
		updateEventText($whiteColor @ "You can't drop an item here" @ $defaultColor @ ".");
	}*/
	
	// Can certain nearby monsters eat dropped items?!
	
	// Trigger context sensitive UI.
	triggerContextSensitiveUI(false);
	
	// Call to monster AI code.
	scheduleMonsterTurn();
}
function isMonsterTypeNearTile (%monsterType, %xIndex, %yIndex)
{//echo("isMonsterTypeNearTile()");
	for (%i = 0; %i < $monsterList.getCount(); %i++)
	{
		if (($monsterList.getObject(%i)).name $= "Bloat")
		{
			if (areTilesAdjacent(%xIndex, %yIndex, ($monsterList.getObject(%i)).xIndex, ($monsterList.getObject(%i)).yIndex))
			{
				return true;
			}
		}
	}
	return false;
}
function createSquadInventory ()
{//echo("createSquadInventory()");
	if (!isObject($squadInventory))
	{//echo("making new inventory!");
		$squadInventory = new SimSet();
	}
}

// Add item to inventory from a treasure chest.
// NOTE: It is this function's responsibility to determine if the inventory has enough space to add another item!
function addItemToInventoryFromChest (%item, %itemType)
{//echo("addItemToInventoryFromChest()");
	// Check current inventory size.
	if ($squadInventory.getCount() < $squadInventoryMaxItems)
	{//echo("adding item!");
		//echo("%item =" SPC %item);
		//echo("%itemType =" SPC %itemType);
		// Check if this item is a weapon or armor.
		if (!isObject(%item))
		{
			// Create the object to add to the squad inventory!
			if (%itemType == -1)			// Weapon.
			{//echo("adding weapon!");
				//%imagemap = getWord(%item, 5);
				%item = createNewWeapon(%item);
			}
			else if (%itemType == -2)		// Armor.
			{//echo("adding armor!");
				//%imagemap = getWord(%item, 4);
				%item = createNewArmor(%item);
			}
		}
		
		// Add the item to inventory.
		$squadInventory.add(%item);
		
		// Remove the item sprite from the map.
	//%item.setVisible(false);	// "Unknown command setVisible"?!?
		
		return true;
	}
	// Not enough space!
	else
	{
		updateEventText($whiteColor @ "Inventory is full!");
		
		// Play inventory full sound.
		playAudio("Audio:inventoryFull", EffectsAudioVolume.value);
		
		return false;
	}
}

// Add item to inventory.
// NOTE: It is this function's responsibility to determine if the inventory has enough space to add another item!
function addItemToInventory (%item, %itemType)
{//echo("addItemToInventory()");
	if ($squadInventory.getCount() < $squadInventoryMaxItems)
	{//echo("adding item!");
		if (%itemType == -1)			// Weapon.
		{//echo("adding weapon!");
			%itemToAdd = %item.weapon;
		}
		else if (%itemType == -2)		// Armor.
		{//echo("adding armor!");
			%itemToAdd = %item.armor;
		}
		else
		{
			%itemToAdd = %item;
		}
		$squadInventory.add(%itemToAdd);
		%item.setVisible(false);
		%item.setPosition(-9999, -9999);
		return true;
	}
	else
	{//echo("not enough inventory space! dropping on ground!");
		updateEventText($whiteColor @ "Inventory is full!");
		playAudio("Audio:inventoryFull", EffectsAudioVolume.value);
		%item.safeDelete();
		%item = createRandomItem(%itemType, getTilePositionFromIndices($playerSquad[$activeCharacter].xIndex, $playerSquad[$activeCharacter].yIndex), $playerSquad[$activeCharacter].xIndex, $playerSquad[$activeCharacter].yIndex);
		$mapItems.add(%item);
		return false;
	}
}

// Clears squad inventory.
function clearSquadInventory ()
{//echo("clearSquadInventory()");
	$inventorySlot1	 = -1;
	$inventorySlot2	 = -1;
	$inventorySlot3	 = -1;
	$inventorySlot4	 = -1;
	$inventorySlot5	 = -1;
	$inventorySlot6	 = -1;
	$inventorySlot7	 = -1;
	$inventorySlot8  = -1;
	$inventorySlot9  = -1;
	$inventorySlot10 = -1;
	$inventorySlot11 = -1;
	$inventorySlot12 = -1;
	if (!isObject($squadInventory))
	{
		return;
	}
	else
	{
		for (%i = 0; %i < $squadInventory.getCount(); %i++)
		{
			%item = $squadInventory.getObject(%i);
			if (isObject(%item))
			{
				%item.safeDelete();
			}
		}
		$squadInventory = null;
	}
}

// Spawns persistent squad inventory.
function savePersistentSquadInventory ()
{//echo("savePersistentSquadInventory()");
	if (isObject($squadInventory))
	{
		return;
	}
	else
	{
		$squadInventory = new SimSet();
	}
	if ($squadInventory.getCount() > 0)
	{
		$inventorySlot1 = ($squadInventory.getObject(0)).itemType;
	}
	if ($squadInventory.getCount() > 1)
	{
		$inventorySlot2 = ($squadInventory.getObject(1)).itemType;
	}
	if ($squadInventory.getCount() > 2)
	{
		$inventorySlot3 = ($squadInventory.getObject(2)).itemType;
	}
	if ($squadInventory.getCount() > 3)
	{
		$inventorySlot4 = ($squadInventory.getObject(3)).itemType;
	}
	if ($squadInventory.getCount() > 4)
	{
		$inventorySlot5 = ($squadInventory.getObject(4)).itemType;
	}
	if ($squadInventory.getCount() > 5)
	{
		$inventorySlot6 = ($squadInventory.getObject(5)).itemType;
	}
	if ($squadInventory.getCount() > 6)
	{
		$inventorySlot7 = ($squadInventory.getObject(6)).itemType;
	}
	if ($squadInventory.getCount() > 7)
	{
		$inventorySlot8 = ($squadInventory.getObject(7)).itemType;
	}
	if ($squadInventory.getCount() > 8)
	{
		$inventorySlot9 = ($squadInventory.getObject(8)).itemType;
	}
	if ($squadInventory.getCount() > 9)
	{
		$inventorySlot10 = ($squadInventory.getObject(9)).itemType;
	}
	if ($squadInventory.getCount() > 10)
	{
		$inventorySlot11 = ($squadInventory.getObject(10)).itemType;
	}
	if ($squadInventory.getCount() > 11)
	{
		$inventorySlot12 = ($squadInventory.getObject(11)).itemType;
	}
}

// Spawns persistent squad inventory.
function spawnPersistentSquadInventory ()
{//echo("spawnPersistentSquadInventory()");
	if ($inventorySlot1 != -1)
	{
		%item = createRandomItem($inventorySlot1, -500, -500, -500, -500);
		%item.visible = 0;
		addItemToInventory(%item, $inventorySlot1);
	}
	if ($inventorySlot2 != -1)
	{
		%item = createRandomItem($inventorySlot2, -500, -500, -500, -500);
		%item.visible = 0;
		addItemToInventory(%item, $inventorySlot2);
	}
	if ($inventorySlot3 != -1)
	{
		%item = createRandomItem($inventorySlot3, -500, -500, -500, -500);
		%item.visible = 0;
		addItemToInventory(%item, $inventorySlot3);
	}
	if ($inventorySlot4 != -1)
	{
		%item = createRandomItem($inventorySlot4, -500, -500, -500, -500);
		%item.visible = 0;
		addItemToInventory(%item, $inventorySlot4);
	}
	if ($inventorySlot5 != -1)
	{
		%item = createRandomItem($inventorySlot5, -500, -500, -500, -500);
		%item.visible = 0;
		addItemToInventory(%item, $inventorySlot5);
	}
	if ($inventorySlot6 != -1)
	{
		%item = createRandomItem($inventorySlot6, -500, -500, -500, -500);
		%item.visible = 0;
		addItemToInventory(%item, $inventorySlot6);
	}
	if ($inventorySlot7 != -1)
	{
		%item = createRandomItem($inventorySlot7, -500, -500, -500, -500);
		%item.visible = 0;
		addItemToInventory(%item, $inventorySlot7);
	}
	if ($inventorySlot8 != -1)
	{
		%item = createRandomItem($inventorySlot8, -500, -500, -500, -500);
		%item.visible = 0;
		addItemToInventory(%item, $inventorySlot8);
	}
	if ($inventorySlot9 != -1)
	{
		%item = createRandomItem($inventorySlot9, -500, -500, -500, -500);
		%item.visible = 0;
		addItemToInventory(%item, $inventorySlot9);
	}
	if ($inventorySlot10 != -1)
	{
		%item = createRandomItem($inventorySlot10, -500, -500, -500, -500);
		%item.visible = 0;
		addItemToInventory(%item, $inventorySlot10);
	}
	if ($inventorySlot11 != -1)
	{
		%item = createRandomItem($inventorySlot11, -500, -500, -500, -500);
		%item.visible = 0;
		addItemToInventory(%item, $inventorySlot11);
	}
	if ($inventorySlot12 != -1)
	{
		%item = createRandomItem($inventorySlot12, -500, -500, -500, -500);
		%item.visible = 0;
		addItemToInventory(%item, $inventorySlot12);
	}
}