// Weapon Handler.
// Worthless Bums 2012.


// Returns a weapon's damage type.
function getWeaponDamageType (%characterIndex)
{
	%weapon = $playerSquad[%characterIndex].weapon;
	if (strstr(%weapon.name, "Inferno") >= 0)
	{
		return "fire";
	}
	else if (strstr(%weapon.name, "Frozen") >= 0)
	{
		return "frost";
	}
	else if (strstr(%weapon.name, "Acid") >= 0)
	{
		return "acid";
	}
	else if ($playerSquad[%characterIndex].characterClass == 2)
	{
		return "explosive";
	}
	else
	{
		return "damage";
	}
}

// Returns true iff the specified substring is found in the weapon name.
function hasWeaponName (%weaponName, %substring)
{//echo("hasWeaponName()");
	if (strstr(%weaponName, %substring) != -1)
	{
		
		return true;
	}
	else
	{
		
		return false;
	}
}

// Generates a two letter weapon code.
function generateWeaponCode ()
{//echo("generateWeaponCode()");
	%firstLetter = getRandom(1, 26);
	%secondLetter = getRandom(1, 26);
	
	switch (%firstLetter)
	{
		case  1: %firstLetter = "A";
		case  2: %firstLetter = "B";
		case  3: %firstLetter = "C";
		case  4: %firstLetter = "D";
		case  5: %firstLetter = "E";
		case  6: %firstLetter = "F";
		case  7: %firstLetter = "G";
		case  8: %firstLetter = "H";
		case  9: %firstLetter = "I";
		case 10: %firstLetter = "J";
		case 11: %firstLetter = "K";
		case 12: %firstLetter = "L";
		case 13: %firstLetter = "M";
		case 14: %firstLetter = "N";
		case 15: %firstLetter = "O";
		case 16: %firstLetter = "P";
		case 17: %firstLetter = "Q";
		case 18: %firstLetter = "R";
		case 19: %firstLetter = "S";
		case 20: %firstLetter = "T";
		case 21: %firstLetter = "U";
		case 22: %firstLetter = "V";
		case 23: %firstLetter = "X";
		case 24: %firstLetter = "X";
		case 25: %firstLetter = "Y";
		case 26: %firstLetter = "Z";
	}
	
	switch (%secondLetter)
	{
		case  1: %secondLetter = "A";
		case  2: %secondLetter = "B";
		case  3: %secondLetter = "C";
		case  4: %secondLetter = "D";
		case  5: %secondLetter = "E";
		case  6: %secondLetter = "F";
		case  7: %secondLetter = "G";
		case  8: %secondLetter = "H";
		case  9: %secondLetter = "I";
		case 10: %secondLetter = "J";
		case 11: %secondLetter = "K";
		case 12: %secondLetter = "L";
		case 13: %secondLetter = "M";
		case 14: %secondLetter = "N";
		case 15: %secondLetter = "O";
		case 16: %secondLetter = "P";
		case 17: %secondLetter = "Q";
		case 18: %secondLetter = "R";
		case 19: %secondLetter = "S";
		case 20: %secondLetter = "T";
		case 21: %secondLetter = "U";
		case 22: %secondLetter = "V";
		case 23: %secondLetter = "X";
		case 24: %secondLetter = "X";
		case 25: %secondLetter = "Y";
		case 26: %secondLetter = "Z";
	}
	
	return %firstLetter @ %secondLetter;
}

// Generates a random damage amount for a weapon given the squad member and dungeon level.
function generateRandomDamage (%squadMember, %dungeonLevel)
{//echo("generateRandomDamage()");
	// The damage to give the weapon.
	%damage = 0;
	%effectiveDungeonCap = 100;
	if (%dungeonLevel < %effectiveDungeonCap)
	{
		%effectiveDungeonCap = %dungeonLevel;
	}
	
	// Dispatch based on squad member.
	switch (%squadMember)
	{
		case 0:	// Leader.
			%baseDamage = 6;
			%effectiveDungeonLevel = %dungeonLevel;
			// Cap the Effective Dungeon Level for bonus damage calculations.
			if (%effectiveDungeonLevel > %effectiveDungeonCap)
			{
				%effectiveDungeonLevel = %effectiveDungeonCap;
			}
			// Randomize two bonus damage values.
			%bonusDamage1 = getRandom(0, %effectiveDungeonLevel);
			%bonusDamage2 = getRandom(0, %effectiveDungeonLevel);
			// Take the absolute difference between the two values as the weapon damage.
			if (%bonusDamage1 >= %bonusDamage2)
			{
				%damage = %bonusDamage1 - %bonusDamage2;
			}
			else
			{
				%damage = %bonusDamage2 - %bonusDamage1;
			}
			%damage += %baseDamage;
		
		case 1:	// Scout.
			%baseDamage = 9;
			%effectiveDungeonLevel = %dungeonLevel;
			// Cap the Effective Dungeon Level for bonus damage calculations.
			if (%effectiveDungeonLevel > %effectiveDungeonCap)
			{
				%effectiveDungeonLevel = %effectiveDungeonCap;
			}
			// Randomize damage value.
			%damage = mRound(getRandom(0, %effectiveDungeonLevel / 2));
			%damage += %baseDamage;
		
		case 2:	// Grenadier.
			%baseDamage = 6;
			%effectiveDungeonLevel = %dungeonLevel;
			// Cap the Effective Dungeon Level for bonus damage calculations.
			if (%effectiveDungeonLevel > %effectiveDungeonCap)
			{
				%effectiveDungeonLevel = %effectiveDungeonCap;
			}
			// Randomize damage value.
			%damage = mRound(getRandom(0, %effectiveDungeonLevel / 3));
			%damage += %baseDamage;
		
		case 3:	// Support.
			%baseDamage = 8;
			%effectiveDungeonLevel = %dungeonLevel;
			// Cap the Effective Dungeon Level for bonus damage calculations.
			if (%effectiveDungeonLevel > %effectiveDungeonCap)
			{
				%effectiveDungeonLevel = %effectiveDungeonCap;
			}
			// Randomize damage value.
			%damage = mRound(getRandom(0, %effectiveDungeonLevel / 3));
			%damage += %baseDamage;
		
		case 4:	// Engineer.
			%baseDamage = 5;
			/*%effectiveDungeonLevel = %dungeonLevel;
			// Cap the Effective Dungeon Level for bonus damage calculations.
			if (%effectiveDungeonLevel > %effectiveDungeonCap)
			{
				%effectiveDungeonLevel = %effectiveDungeonCap;
			}
			// Randomize damage value.
			%damage = mRound(getRandom(0, %effectiveDungeonLevel / 3));*/
			%baseDamage += getRandom(0, 3);
			%damage += %baseDamage;
	}
	
	// Increase weapon damage based on the deck the player is currently on.
	switch$ ($overworldLocation)
	{
		case "Command Deck":
			%damage += getRandom(0, 1);
			
		case "Engineering Deck":
			%damage += getRandom(1, 3);
			
		case "Medical Deck":
			%damage += getRandom(2, 4);
			
		case "Armory Deck":
			%damage += getRandom(4, 8);
			
		case "Bridge Deck":
			%damage += getRandom(3, 6);
	}
	//echo("%damage =" SPC %damage);
	// Return the weapon damage.
	return %damage;
}

// Generates a random range value for a weapon given the squad member and dungeon level.
function generateRandomRange (%squadMember, %dungeonLevel)
{//echo("generateRandomRange()");
	// The range to give the weapon.
	%range = 0;
	%effectiveDungeonCap = 100;
	if (%dungeonLevel < %effectiveDungeonCap)
	{
		%effectiveDungeonCap = %dungeonLevel;
	}
	
	// Dispatch based on squad member.
	switch (%squadMember)
	{
		case 0:	// Leader.
			%range = getRandom(2, 4);
			%rangeRoll = getRandom(0, %effectiveDungeonCap);
			%rangeRoll = %rangeRoll / 20;
			%range += mRound(%rangeRoll);
			
		case 1:	// Scout.
			%range = getRandom(4, 6);
			%rangeRoll = getRandom(0, %effectiveDungeonCap);
			%rangeRoll = %rangeRoll / 10;
			%range += mRound(%rangeRoll);
			
		case 2:	// Grenadier.
			%range = getRandom(3, 4);
			%rangeRoll = getRandom(0, %effectiveDungeonCap);
			%rangeRoll = %rangeRoll / 30;
			%range += mRound(%rangeRoll);
			
		case 3:	// Support.
			%range = getRandom(2, 4);
			%rangeRoll = getRandom(0, %effectiveDungeonCap);
			%rangeRoll = %rangeRoll / 30;
			%range += mRound(%rangeRoll);
			
		case 4:	// Engineer.
			%range = getRandom(2, 3);
			%rangeRoll = getRandom(0, %effectiveDungeonCap);
			%rangeRoll = %rangeRoll / 30;
			%range += mRound(%rangeRoll);
	}
	
	// Return the weapon range.
	return %range;
}

// Generates a random accuracy value for a weapon given the squad member and dungeon level.
function generateRandomAccuracy (%squadMember, %dungeonLevel)
{//echo("generateRandomAccuracy()");
	// The range to give the weapon.
	%accuracy = 0;
	%effectiveDungeonCap = 100;
	if (%dungeonLevel < %effectiveDungeonCap)
	{
		%effectiveDungeonCap = %dungeonLevel;
	}
	
	// Dispatch based on squad member.
	switch (%squadMember)
	{
		case 0:	// Leader.
			%accuracy = getRandom(50, 75);
			%accuracyRoll = getRandom(0, %effectiveDungeonCap);
			%accuracyRoll = %accuracyRoll / 10;
			%accuracy += mRound(%accuracyRoll);
			
		case 1:	// Scout.
			%accuracy = getRandom(65, 80);
			%accuracyRoll = getRandom(0, %effectiveDungeonCap);
			%accuracyRoll = %accuracyRoll / 5;
			%accuracy += mRound(%accuracyRoll);
			
		case 2:	// Grenadier.
			%accuracy = getRandom(50, 75);
			%accuracyRoll = getRandom(0, %effectiveDungeonCap);
			%accuracyRoll = %accuracyRoll / 10;
			%accuracy += mRound(%accuracyRoll);
			
		case 3:	// Support.
			%accuracy = getRandom(45, 70);
			%accuracyRoll = getRandom(0, %effectiveDungeonCap);
			%accuracyRoll = %accuracyRoll / 8;
			%accuracy += mRound(%accuracyRoll);
			
		case 4:	// Engineer.
			%accuracy = getRandom(40, 60);
			%accuracyRoll = getRandom(0, %effectiveDungeonCap);
			%accuracyRoll = %accuracyRoll / 8;
			%accuracy += mRound(%accuracyRoll);
	}
	
	// Increase weapon accuracy based on the deck the player is currently on.
	switch$ ($overworldLocation)
	{
		case "Command Deck":
			%accuracy += getRandom(0, 5);
			
		case "Engineering Deck":
			%accuracy += getRandom(3, 8);
			
		case "Medical Deck":
			%accuracy += getRandom(6, 12);
			
		case "Armory Deck":
			%accuracy += getRandom(10, 20);
			
		case "Bridge Deck":
			%accuracy += getRandom(10, 15);
	}
	
	// Return the weapon accuracy.
	return %accuracy;
}

// Generates a random ammo amount for a weapon given the squad member and dungeon level.
function generateRandomAmmo (%squadMember, %dungeonLevel)
{//echo("generateRandomAmmo()");
	// The ammo to give the weapon.
	%ammo = 1;
	%effectiveDungeonCap = 100;
	if (%dungeonLevel < %effectiveDungeonCap)
	{
		%effectiveDungeonCap = %dungeonLevel;
	}
	
	// Dispatch based on squad member.
	switch (%squadMember)
	{
		case 0:	// Leader.
			%ammo = 1;
			
		case 1:	// Scout.
			%ammo = 1;
			
		case 2:	// Grenadier.
			%ammo = 1;
			
		case 3:	// Support.
			%ammo = 3;
			
		case 4:	// Engineer.
			%ammo = 4;
	}
	
	// Return the weapon ammo.
	return %ammo;
}

// Generates a random magazine capacity for a weapon given the squad member and dungeon level.
function generateRandomCapacity (%squadMember, %dungeonLevel, %ammo)
{//echo("generateRandomCapacity()");
	// The range to give the weapon.
	%capacity = 0;
	%effectiveDungeonCap = 100;
	if (%dungeonLevel < %effectiveDungeonCap)
	{
		%effectiveDungeonCap = %dungeonLevel;
	}
	
	// Dispatch based on squad member.
	switch (%squadMember)
	{
		case 0:	// Leader.
			%capacity = %ammo * getRandom(8, 15);
			
		case 1:	// Scout.
			%capacity = %ammo * getRandom(4, 8);
			
		case 2:	// Grenadier.
			%capacity = %ammo * getRandom(3, 7);
			
		case 3:	// Support.
			%capacity = %ammo * getRandom(10, 20);
			
		case 4:	// Engineer.
			%capacity = %ammo * getRandom(15, 30);
	}
	
	// Return the weapon capacity.
	return %capacity;
}

// Generates a random damage range for a weapon given the squad member and dungeon level.
function generateRandomDamageRange (%squadMember, %dungeonLevel)
{//echo("generateRandomDamageRange()");
	// The range to give the weapon.
	%damageRange = 0;
	%effectiveDungeonCap = 100;
	if (%dungeonLevel < %effectiveDungeonCap)
	{
		%effectiveDungeonCap = %dungeonLevel;
	}
	
	// Dispatch based on squad member.
	switch (%squadMember)
	{
		case 0:	// Leader.
			%damageRange = 1;//getRandom(1, 3);
			
		case 1:	// Scout.
			%damageRange = 1;//getRandom(0, 1);
			
		case 2:	// Grenadier.
			%damageRange = 1;//getRandom(1, 3);
			
		case 3:	// Support.
			%damageRange = 1;//getRandom(1, 2);
			
		case 4:	// Engineer.
			%damageRange = 1;//getRandom(2, 5);
	}
	
	// Return the weapon damage range.
	return %damageRange;
}

// Generates a random reload cost for a weapon given the squad member and dungeon level.
function generateRandomReload (%squadMember, %dungeonLevel)
{//echo("generateRandomReload()");
	// The range to give the weapon.
	%reload = 0;
	%effectiveDungeonCap = 100;
	if (%dungeonLevel < %effectiveDungeonCap)
	{
		%effectiveDungeonCap = %dungeonLevel;
	}
	
	%randomReloadChance = 75;
	
	// Dispatch based on squad member.
	switch (%squadMember)
	{
		case 0:	// Leader.
			%reload = 1;
			%reloadChance = getRandom(1, 100);
			if (%reloadChance >= %randomReloadChance)
			{
				%reload += getRandom(1, 2);
			}
			
		case 1:	// Scout.
			%reload = 1;
			%reloadChance = getRandom(1, 100);
			if (%reloadChance >= %randomReloadChance)
			{
				%reload += getRandom(1, 2);
			}
		
		case 2:	// Grenadier.
			%reload = 1;
			%reloadChance = getRandom(1, 100);
			if (%reloadChance >= %randomReloadChance)
			{
				%reload += getRandom(1, 3);
			}
			
		case 3:	// Support.
			%reload = 1;
			%reloadChance = getRandom(1, 100);
			if (%reloadChance >= %randomReloadChance)
			{
				%reload += getRandom(1, 2);
			}
			
		case 4:	// Engineer.
			%reload = 1;
			%reloadChance = getRandom(1, 100);
			if (%reloadChance >= %randomReloadChance)
			{
				%reload += getRandom(1, 2);
			}
	}
	
	// Return the weapon reload.
	//return %reload;
	return 1;	// Short-circuit random stat generation and have it always cost 1 AP!
}

// Generates a random fire cost for a weapon given the squad member and dungeon level.
function generateRandomFireCost (%squadMember, %dungeonLevel)
{//echo("generateRandomFireCost()");
	// The range to give the weapon.
	%fireCost = 0;
	%effectiveDungeonCap = 100;
	if (%dungeonLevel < %effectiveDungeonCap)
	{
		%effectiveDungeonCap = %dungeonLevel;
	}
	
	%randomFireChance = 75;
	
	// Dispatch based on squad member.
	switch (%squadMember)
	{
		case 0:	// Leader.
			%fireCost = 1;
			%fireCostChance = getRandom(1, 100);
			if (%fireCostChance >= %randomFireChance)
			{
				%fireCost += getRandom(1, 2);
			}
			
		case 1:	// Scout.
			%fireCost = 1;
			%fireCostChance = getRandom(1, 100);
			if (%fireCostChance >= %randomFireChance)
			{
				%fireCost += getRandom(1, 3);
			}
			
		case 2:	// Grenadier.
			%fireCost = 1;
			%fireCostChance = getRandom(1, 100);
			if (%fireCostChance >= %randomFireChance)
			{
				%fireCost += getRandom(1, 2);
			}
			
		case 3:	// Support.
			%fireCost = 1;
			%fireCostChance = getRandom(1, 100);
			if (%fireCostChance >= %randomFireChance)
			{
				%fireCost += getRandom(1, 2);
			}
			
		case 4:	// Engineer.
			%fireCost = 1;
			%fireCostChance = getRandom(1, 100);
			if (%fireCostChance >= %randomFireChance)
			{
				%fireCost += getRandom(1, 2);
			}
	}
	
	// Return the weapon fire cost.
	//return %fireCost;
	return 1;	// Short-circuit random stat generation and have it always cost 1 AP!
}

// Generates a random weapon name.
function generateRandomWeaponName ()
{//echo("generateRandomWeaponName()");
	// Picks two words randomly from two separate lists.
	%firstWord = getRandom(0, 1);
	%secondWord = getRandom(0, 17);
	
	// Don't generate weapons with special properties (first word) until later in the game!
	%firstWord = "";
	if ($overworldLocation !$= "Command Deck"	||
		($overworldLocation $= "Command Deck"	&&	($dungeonLevel > 1)))
	{
		%specialChance = getRandom(1, 100);
		if (%specialChance <= 100)//25)
		{
			%firstWord = getRandom(0, 2);
			switch (%firstWord)
			{
				case  0:	%firstWord = "Inferno";
				case  1:	%firstWord = "Frozen";
				case  2:	%firstWord = "Acid";
				
				/*
				case  0:	%firstWord = "Battle";
				case  1:	%firstWord = "Combat";
				case  2:	%firstWord = "Rough";
				case  3:	%firstWord = "Enduring";
				case  4:	%firstWord = "Compact";
				case  5:	%firstWord = "Berserk";
				case  6:	%firstWord = "Steady";
				case  7:	%firstWord = "Rugged";
				case  8:	%firstWord = "Furious";
				case  9:	%firstWord = "Calm";
				case 10:	%firstWord = "Inferno";
				case 11:	%firstWord = "Hammer";
				case 12:	%firstWord = "Kicking";
				case 13:	%firstWord = "Hateful";
				case 14:	%firstWord = "Steel";
				case 15:	%firstWord = "Iron";
				case 16:	%firstWord = "Precise";
				case 17:	%firstWord = "Spit";
				*/
			}
		}
	}
	
	switch (%secondWord)
	{
		case  0:	%secondWord = "Rifle";
		case  1:	%secondWord = "Gun";
		case  2:	%secondWord = "Firearm";
		case  3:	%secondWord = "Longarm";
		case  4:	%secondWord = "Fire";
		case  5:	%secondWord = "Aim";
		case  6:	%secondWord = "Monster";
		case  7:	%secondWord = "Beast";
		case  8:	%secondWord = "Thrower";
		case  9:	%secondWord = "Betty";
		case 10:	%secondWord = "Anne";
		case 11:	%secondWord = "Martha";
		case 12:	%secondWord = "Rachel";
		case 13:	%secondWord = "Wendy";
		case 14:	%secondWord = "Fiona";
		case 15:	%secondWord = "Enola";
		case 16:	%secondWord = "Lisa";
		case 17:	%secondWord = "Nancy";
	}
	
	// Return the two word name.
	return %firstWord @ %secondWord;
}
function getProjectileTypeName (%projectileType)
{
	%projectileTypeName = "";
	switch (%projectileType)
	{
		case 0:	%projectileTypeName = "Ballistic";
		case 1:	%projectileTypeName = "Laser";
		case 2:	%projectileTypeName = "Plasma";
		case 3:	%projectileTypeName = "Beam";
	}
	return %projectileTypeName;
}
// Generates a random weapon image map for the squad member weapon type.
function generateRandomWeaponImagemap (%squadMember, %dungeonLevel, %projectileType)
{//echo("generateRandomWeaponImagemap()");
	%characterString = getCharacterClassName(%squadMember);
	%projectileString = getProjectileTypeName(%projectileType);
	%imageMap = %characterString @ "Weapon" @ %projectileString @ "ImageMap";
	return %imageMap;
}

function generateRandomProjectileType (%squadMember, %dungeonLevel)
{//echo("generateRandomProjectileType()");
	%projectileType = $weaponProjectileBallisticType;
	if ($overworldLocation !$= "Command Deck")
	{
		%projectileTypeChanceBonus = %dungeonLevel;
		switch$ ($overworldLocation)
		{
			case "Command Deck":		%projectileTypeChanceBonus += 0;		// Doesn't matter anyway!
			case "Engineering Deck":	%projectileTypeChanceBonus += 5;
			case "Medical Deck":		%projectileTypeChanceBonus -= 50;
			case "Armory Deck":			%projectileTypeChanceBonus += 25;
			case "Bridge Deck":			%projectileTypeChanceBonus += 10;
		}
		if (getRandom(1, 100) <= $SpecialProjectileBaseChance + %projectileTypeChanceBonus)
		{
			// No beam implemented yet!
			%projectileType = $weaponProjectileLaserType;
			//%projectileType = $weaponProjectilePlasmaType;
			//%projectileType = $weaponProjectileBeamType;
		}
	}
	//echo("%projectileType =" SPC %projectileType);
	//return $weaponProjectileLaserType;	// For testing purposes only!
	return %projectileType;
}

// Creates a random weapon.
function createRandomWeapon (%squadMember, %dungeonLevel)//echo(createRandomWeapon(getRandom(0, 3), getRandom(1, 5)));
{//echo("createRandomWeapon()");
	// Generate a two letter weapon code.
	%weaponCode = generateWeaponCode();
	// Append the hyphen.
	%weaponCode = %weaponCode @ "-";
	// Generate a random weapon name.
	%weaponCode = %weaponCode @ generateRandomWeaponName();
	
	// Append the squad member index.
	%weaponCode = %weaponCode SPC %squadMember;
	
	// Projectile Type.
	%projectileType = generateRandomProjectileType(%squadMember, %dungeonLevel);
	//%projectileType = 1;	// For testing purposes only!
	%damageModifier = 0;
	%rangeModifier = 0;
	%accuracyModifier = 0;
	%capacityModifier = 0;
	// Skew stats depending on projectile type.
	switch (%projectileType)
	{
		case $weaponProjectileBallisticType:
			// Nothing here yet!
		
		case $weaponProjectileLaserType:
			%damageModifier = 2;
			%rangeModifier = 1;
			%accuracyModifier = 10;
			%capacityModifier = 3;
		
		case $weaponProjectilePlasmaType:
			%damageModifier = 6;
			%rangeModifier = -1;
			%accuracyModifier = -20;
			%capacityModifier = -3;
		
		case $weaponProjectileBeamType:
			%damageModifier = 4;
			%rangeModifier = 2;
			%accuracyModifier = 20;
			%capacityModifier = 6;
	}
	
	// Use the dungeon level to randomize damage, range, and accuracy stats.
	// Damage.
	%weaponCode = %weaponCode SPC (generateRandomDamage(%squadMember, %dungeonLevel) + %damageModifier);
	// Range.
	%weaponCode = %weaponCode SPC (generateRandomRange(%squadMember, %dungeonLevel) + %rangeModifier);
	// Accuracy.
	%weaponCode = %weaponCode SPC (generateRandomAccuracy(%squadMember, %dungeonLevel) + %accuracyModifier);
	// Image Map.
	%weaponCode = %weaponCode SPC generateRandomWeaponImagemap(%squadMember, %dungeonLevel, %projectileType);
	// Ammo.
	%ammo = generateRandomAmmo(%squadMember, %dungeonLevel);
	%weaponCode = %weaponCode SPC %ammo;
	// Capacity.
	%capacity = generateRandomCapacity(%squadMember, %dungeonLevel, %ammo) + %capacityModifier;
	%weaponCode = %weaponCode SPC %capacity;
	// Damage Range.
	%weaponCode = %weaponCode SPC generateRandomDamageRange(%squadMember, %dungeonLevel);
	// "Current" Ammo.
	%weaponCode = %weaponCode SPC %capacity;		// Start with a full magazine!
	// Reload.
	%weaponCode = %weaponCode SPC generateRandomReload(%squadMember, %dungeonLevel);
	// Fire Cost.
	%weaponCode = %weaponCode SPC generateRandomFireCost(%squadMember, %dungeonLevel);
	
	// Finally add the projectile type!
	%weaponCode = %weaponCode SPC %projectileType;
	
	// Return the weapon code.
	//echo("%weaponCode =" SPC %weaponCode);
	return %weaponCode;
}

// Creates a new squad weapon.
function createNewWeapon (%weaponType)
{//echo("createNewWeapon()");
	// Weapon information.
	%name = getWord(%weaponType, 0);
	%squadMember = getWord(%weaponType, 1);
	%damage = getWord(%weaponType, 2);
	%range = getWord(%weaponType, 3);
	%accuracy = getWord(%weaponType, 4);
	%imageMap = getWord(%weaponType, 5);
	%ammo = getWord(%weaponType, 6);
	%capacity = getWord(%weaponType, 7);
	%damageRange = getWord(%weaponType, 8);
	%currentAmmo = getWord(%weaponType, 9);
	%reload = getWord(%weaponType, 10);
	%fireCost = getWord(%weaponType, 11);
	%projectileType = getWord(%weaponType, 12);
	
	// Create the weapon object.
	%weapon = new ScriptObject("Weapon")
	{
		// Item Type information.
		itemType = -1;
		
		// Squad Member.
		squadMember = %squadMember;
		
		// Weapon information.
		weaponType = %weaponType;
		name = %name;
		imageMapName = %imageMap;
		
		// Weapon statistics.
		damage = %damage;
		range = %range;
		accuracy = %accuracy;
		ammo = %ammo;
		capacity = %capacity;
		damageRange = %damageRange;
		currentAmmo = %currentAmmo;
		reload = %reload;
		fireCost = %fireCost;
		
		projectileType = %projectileType;
	};
	
	// Return the weapon.
	return %weapon;
}