/***********************************************
 * weapon_bow#.zes	Bow script
 * 
 * Author: Luke X based of GD fire wand, optimised by Satanman
 * Date:   31st December 1969
 *
 * Desc:   A Bow weapon script for the player
 *
 * Usage:  Shoot it
 *     
 * Sprites: _WeaponSheet1.spt
 *         
 ***********************************************/
#include <animation>
#include <entity>
#include <general>
#include <counter>
#include <core>

new arrowNeeded = 1;	// arrows needed to fire the weapon
new magicNeeded;		// magic needed to fire the weapon
new param;
// Create 4 strings to store the Identifiers of the Player animations for each direction
new Anim[4][20];
// Create an array of strings to hold the names
new wimage[4][] = {"_bow1n", "_bow1e", "_bow1s", "_bow1w" };
new PlayerSuit = 1; 			// Player suit (Mar. 03)
new LastPlayerSuit = 1;
main()
{
	if (FirstRun())
	{
		new n;
		
		// Set this entity's basic type
		SetType("this", weaponType);
  	
		// Get the scripts parameter
		param = GetParam("this");

		// Create 2 string for the weapons descriptions
		AllocateStrings("this", 2, 100);
		if (param == 'n')
		{
			SetString("this", 0,  "You got the bow! Give it a try!");		// For Chests
			SetString("this", 1,  "Normal Bow");	// For menu      
			SetImage("this", "_bowweaponn");
			magicNeeded = 0;
		} else if (param == 'f')
		{
			SetString("this", 0,  "You got fire arrows! These magic arrows leave a trail of fire in their wake!");		// For Chests
			SetString("this", 1,  "Fire Arrows");	// For menu      
			SetImage("this", "_bowweaponf");
			magicNeeded = 2;
		} else if (param == 'i')
		{
			SetString("this", 0,  "You got ice arrows! These magic arrows freeze anything they come across!");		// For Chests
			SetString("this", 1,  "Ice arrows");	// For menu
			SetImage("this", "_bowweaponi");
			magicNeeded = 2;
		} else if (param == 'l')
		{
			SetString("this", 0,  "You got light arrows! The light of justice shalt smite evil in ye olde english!");		// For Chests
			SetString("this", 1,  "Light Arrows");	// For menu      
			SetImage("this", "_bowweaponl");
			magicNeeded = 6;
		}  else if (param == 'b')
		{
			SetString("this", 0,  "You got bomb arrows! These arrows explode on contact, so don't drop them!");		// For Chests
			SetString("this", 1,  "Bomb Arrows");	// For menu      
			SetImage("this", "_bowweaponb");
			magicNeeded = 4;
		}
		SetOwnedFlag("this", 1);

		// Create the Animations
		for ( n = 0; n < 4; n++ )
		{
			CreateAnim(6, Anim[n]);
			SetAnimLoop(Anim[n], false);
		}

	SetPlayerSuit(PlayerSuit);
	}
	SetActiveDist("this", -1);
}

//----------------------------------------
// Name: Init()
//----------------------------------------
public Init()
{

	new n;
	/* This function should be called by the Player script every
		time just before the weapon is used, it resets all animations
		and makes sure everything is ready to go */

	if ( GetCounterValue("arrows") < arrowNeeded || GetCounterValue("magicMeter") < magicNeeded)
	{
		// Player doesn't have enough of something
		PlaySound("_error.wav", 240);
		SetState("player1", standing);
		return;	
	}
	else
	{
		// Decrease arrow + magic counters
		IncCounterTarget("arrows", -arrowNeeded);
		IncCounterTarget("magicMeter", -magicNeeded);
	}


	// Reset all animations
	for ( n = 0; n < 4; n++ )
		SetAnimCount(Anim[n], 0);

	// Set the Position and Direction of this entity to match the player's
	SetX("this", GetX("player1"));
	SetY("this", GetY("player1"));
	SetDirection("this", GetDirection("player1"));
	//PlaySound("ArrowLaunch.wav", 240); 
	CreateArrow();
	
	 if (param == 'n')
	{
		if (GetCounterValue("arrows") > 0)
			SetImage("this", "_bowiconfull");
		else
			SetImage("this", "_bowiconempty");
	}
	if ( LastPlayerSuit != PlayerSuit )
 		SetPlayerSuit(PlayerSuit);
	
}

//----------------------------------------
// Name: CreateArrow()
//----------------------------------------
CreateArrow()
{
	new x = GetX("player1");
	new y = GetY("player1");

	// Create a Arrow entity to fly from this bow
	if (param == 'f')
		CreateEntity("_warrowf", x + 4, y + 4, "firearrow");
	else if (param == 'i')
		CreateEntity("_warrowi", x + 4, y + 4, "icearrow");
	else  if (param == 'l')
		CreateEntity("_warrowl", x + 4, y + 4, "lightarrow");
	else if (param == 'n')
		CreateEntity("_warrown", x + 4, y + 4, "arrow");
	else if (param == 'b')
		CreateEntity("_warrowb", x + 4, y + 4, "bombarrow");
}


//----------------------------------------
// Name: DrawWeapon()
//----------------------------------------
public DrawWeapon()
{
	// Get the Correct Animation
	new dir = GetDirection("this");
	new width  = GetAnimWidth(Anim[dir]);
	new height = GetAnimHeight(Anim[dir]);
	new x = GetX("this");
	new y = GetY("this");
	new bowImages[2];
	new xoff[2];            // X and Y offsets for the bow images
	new yoff[2];	
	new sx;
	new sy;
	new AnimCount;
	new depth = y + height;

	if ( dir == north)
	{
		bowImages = {0,0};
		xoff = {-2, -2};
		yoff = {-2, -2};
		depth = y;
	}
	else if ( dir == east)
	{
		bowImages = {1,1};
		xoff = {11, 11};   
		yoff = {8, 8};   
	}
	else if ( dir == south)
	{
		bowImages = {2,2};
		xoff = {5, 5};
		yoff = {15, 15};
	}
	else if ( dir == west)
	{
		bowImages = {3,3};
		xoff = {-2, -2};
		yoff = {8, 8};      
	}

	// Draw the Player animation
	if (isVisible("player1"))
	{
		// Draw the Player
		DrawAnim(Anim[dir], x, y, y + height);
		AnimCount = GetAnimCount(Anim[dir]);

		// Draw the image of the weapon
		sx = x + xoff[AnimCount];
		sy = y + yoff[AnimCount] - 9;
		PutSprite(wimage[bowImages[AnimCount]], sx, sy, depth);

		// Draw the Player's shadow
		PutSprite("shadow1", x, y, 2);
	}
	else
		IncrementAnim(Anim[dir]);


	// Check if the weapon animation is over
	if (FinishedAnim(Anim[dir]))
	{
		// Return control back to player entity
		SetState("player1", standing);
	}
}
//-----------------------------------------------------
// Name: SetPlayerSuit()
// Desc: Changes the players Animation
//-----------------------------------------------------
public ChangePlayerSuit(ChangeTo) {
	PlayerSuit = ChangeTo;
}

public GetPlayerSuit() {
	return PlayerSuit;
}

SetPlayerSuit(NewPlayerSuit)
{
	LastPlayerSuit = NewPlayerSuit;
	for (new n = 0; n < 4; n++ )
	{
		DeleteAnim(Anim[n]);
		CreateAnim(6, Anim[n]);
	}
			
	if (NewPlayerSuit == 1)
	{
		// Add Frames to player Animations
		AddAnimframe(Anim[0], 0, -9, "__swdn7");
		AddAnimframe(Anim[1], 0, -9, "__swde4");
		AddAnimframe(Anim[2], 0, -9, "__swds3");
		AddAnimframe(Anim[3], 0, -9, "__swdw4");	
	}
	else if (NewPlayerSuit == 2)
	{
		// Add Frames to player Animations
		AddAnimframe(Anim[0], 0, -9, "__twdn7");
		AddAnimframe(Anim[1], 0, -9, "__twde4");
		AddAnimframe(Anim[2], 0, -9, "__twds3");
		AddAnimframe(Anim[3], 0, -9, "__twdw4");	
	}
	else if (NewPlayerSuit == 3)
	{
		// Add Frames to player Animations
		AddAnimframe(Anim[0], 0, -9, "__uwdn7");
		AddAnimframe(Anim[1], 0, -9, "__uwde4");
		AddAnimframe(Anim[2], 0, -9, "__uwds3");
		AddAnimframe(Anim[3], 0, -9, "__uwdw4");	
	}
}
