/*************************************************************************************************
* _npc2# Entity Script    NPC script2
* 
* Author: GD
* Date:   4 July 2002
*
* Desc:    An NPC (non-playable charater) script for simple NPCs, those which dont support Person Script.
			
* Usage:   Use the MessageMap() function to set the NPCs text (what they say to the player), see the
		    Open Zelda documentation for help on that function.
*
* Sprites: _NPCSheet1.spt
*         
**************************************************************************************************/
#include <entity>
#include <general>
#include <animation>

//   Global Data
new headDist 		= 8;        // Distance head is from the body
new shadowDist		= 1;
new Talking 		= false;    // Is the player currently talking to this NPC?
new HeadDirection   = south;	// Direction of NPCs Head
new param			= 0;
new hasHead			= false;	// not all NPCs have a seperate head
new LooksAround		= true;

new head[4][20];	// 4 strings to hold each image of the NPCs head
new stand[20];		// Holds the south standing animation ID

//----------------------------------------
// Name: main()
//----------------------------------------
main()
{
	if (FirstRun())
	{
		// Get the parameter for this script
		param = GetParam("this");
		
		// Set some general parameters
		SetActiveDist("this", 320);
		SetType("this", npcType);
		AllocateStrings("this", 10, 640 );   
		
		// Create the animation for the NPCs body
		CreateAnim(4, stand);
			
		// Add Different sprites depending on the parameter	
		// Theres a different parameter for each chatacter
		if ( param == 'a' ) // Tall Kid
		{
			AddAnimframe(stand, 0, 0, "_npc2aa1"); 
			head[1] = "_npc2ab";  // Assign the head sprites
			head[2] = "_npc2ac";
			head[3] = "_npc2ad";
			hasHead = true;
			headDist = 12;
		}
		if ( param == 'b' )	// Voodoo Man
		{
			AddAnimframe(stand, 0, 0, "_npc2b"); 
			AddAnimframe(stand, 0, 0, "_npc2b1");
			shadowDist = 10;
		}
		if ( param == 'c' )	// Fotune teller dude
		{
			AddAnimframe(stand, 0, 0, "_npc2c"); 
			AddAnimframe(stand, 0, 0, "_npc2c1");
			SetAnimSpeed(stand, 1);
			shadowDist = 10;
		}
		if ( param == 'd' )	// Sahasrahl
		{
			AddAnimframe(stand, 0, 0, "_npc2da1"); 
			AddAnimframe(stand, 0, 0, "_npc2da2");
			head[2] = "_npc2db";
			hasHead = true;
			headDist = 10;
			SetAnimSpeed(stand, 1);
			LooksAround = false;
		}
		if ( param == 'e' ) // prize guy
		{
			AddAnimframe(stand, 0, 0, "_npc2ea1"); 
			head[1] = "_npc2eb";  // Assign the head sprites
			head[2] = "_npc2ec";
			head[3] = "_npc2ed";
			hasHead = true;
			headDist = 11;
		}
		if ( param == 'f' ) // little kid
		{
			AddAnimframe(stand, 0, 0, "_npc2fa1"); 
			head[1] = "_npc2fb";  // Assign the head sprites
			head[2] = "_npc2fc";
			head[3] = "_npc2fd";
			hasHead = true;
			headDist = 9;
		}
		if ( param == 'g' ) // bug-catcher kid
		{
			AddAnimframe(stand, 0, 0, "_npc2ga1"); 
			head[1] = "_npc2gb";  // Assign the head sprites
			head[2] = "_npc2gc";
			head[3] = "_npc2gd";
			hasHead = true;
			headDist = 11;
		}
		if ( param == 'h' ) // Geeky looking guy
		{
			AddAnimframe(stand, 0, 0, "_npc2ha1"); 
			head[1] = "_npc2hb";  // Assign the head sprites
			head[2] = "_npc2hc";
			head[3] = "_npc2hd";
			hasHead = true;
			headDist = 10;
		}
	}
	
	// Check if the Player wants to talk to this NPC
	if ( CallFunction("_npclib", true, "CheckForTalk", "NULL") )
		Talking = true;
		
	// Check if the player has just finished talking to an NPC
	if (Talking && FinishedReading())
	{
		CallFunction("_npclib", true, "AfterTalk", "NULL");
		Talking = false;
	}
	
	Stand();
}

//----------------------------------------
// Name: Stand()
//----------------------------------------
Stand()
{
	new x = GetX("this");
	new y = GetY("this");
	
	// Make the NPC face the player if the NPC has a head
	if ( hasHead && LooksAround )
	{
		CallFunction("_npclib", true, "FacePlayer", "NULL");
		HeadDirection = GetDirection("this");
		if (HeadDirection == north)
			HeadDirection = south;
	}
			
	// Get the width and height of the Current Image
	new width  = GetAnimWidth(stand);
	new height = GetAnimHeight(stand);
	
	// Draw the Body and the Head images
	if (isVisible("this"))
	{
		DrawAnim( stand, x, y, y + height ); 								// Draw the body

		if ( hasHead )
			PutSprite( head[HeadDirection], x, y - headDist, y + height); 	// Draw head		
		PutSprite("shadow1", x, y + shadowDist, 2);							// Draw shadow
	}
	
	// Set a solid collision rectangle around the NPC
	SetCollisionRect("this", 0, true, x, y, x + width, y + height);
}
