/*************************************************************************************************
* _goodguard# Entity Script for NPC Guard's
* 
* Author: VIRUS
* Fixed By: G8orByte & Eternal-rest
*
* Date:   05 August 2002
*
* Desc:    Used to make the NPC Guards Watch the Player
*			
* Usage:   N/A
*
* Sprites That Use Script: _GoodGuard.spt
*         
**************************************************************************************************/
#include <entity>
#include <general>
#include <animation>

new bodyDist        = 0;        // NPCs Body to Ground
new Talking 	    = false;    // Is the player currently talking to this NPC?
new BodyDirection   = south;    // Set NPCs Body Position
new param	    	= 0;
new IsGuard         = false;    // Is the NPC a Guard?
new LooksAround	    = true;

new body[4][20];    // 4 strings to hold each image of the NPCs body
new first[20];      // Guards First Position

//----------------------------------------
// 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, first);
			
		// Parameters
		if ( param == '1' ) // Gold Guard
		{
			AddAnimframe(first, 0, 0, "_goodguardd"); // Down
			body[0] = "_goodguardu";    // Up
			body[1] = "_goodguardr";    // Right
			body[2] = "_goodguardd";    // Down
			body[3] = "_goodguardl";    // Left
			IsGuard = true;
		}
		if ( param == '2' ) // Red Guard
		{
			AddAnimframe(first, 0, 0, "_goodguardrd"); // Down
			body[0] = "_goodguardru";    // Up
			body[1] = "_goodguardrr";    // Right
			body[2] = "_goodguardrd";    // Down
			body[3] = "_goodguardrl";    // Left
			IsGuard = true;
		}
	}
	// 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;
	}
	
	Watch();
}

//----------------------------------------
// Name: Watch()
//----------------------------------------
Watch()
{
	new x = GetX("this");
	new y = GetY("this");
	
	// Make the NPC watch the player
	if ( IsGuard && LooksAround )
	{
		CallFunction("_npclib", true, "FacePlayer", "NULL");
		BodyDirection = GetDirection("this");
	}
			
	// Get the width and height of the Current Image
	new width  = GetAnimWidth(first);
	new height = GetAnimHeight(first);

	if ( IsGuard )
		PutSprite( body[BodyDirection], x, y - bodyDist, y + height); 	// Draw Body

	SetCollisionRect("this", 0, true, x, y + 8, x + width - 2, y + height - 4);
}
//----------------------------------------
//              VERSION 1.2
//----------------------------------------
