//////////////////////////////////////////////////////////////////////////
// high level scripting interface
//////////////////////////////////////////////////////////////////////////
HRESULT CAdLayer::ScCallMethod(CScScript* Script, CScStack *Stack, CScStack *ThisStack, char *Name)
{
	//////////////////////////////////////////////////////////////////////////
	// GetNode
	//////////////////////////////////////////////////////////////////////////
	if(strcmp(Name, "GetNode")==0)
	{
		Stack->CorrectParams(1);
		CScValue* val = Stack->Pop();
		int node = -1;
		
		if(val->m_Type==VAL_INT) node = val->GetInt();
		else{ // get by name
			for(int i=0; i<m_Nodes.GetSize(); i++){
				if( (m_Nodes[i]->m_Type==OBJECT_ENTITY && CBPlatform::stricmp(m_Nodes[i]->m_Entity->m_Name, val->GetString())==0) ||
					(m_Nodes[i]->m_Type==OBJECT_REGION && CBPlatform::stricmp(m_Nodes[i]->m_Region->m_Name, val->GetString())==0)){
					node = i;
					break;
				}					
			}
		}

		if(node<0 || node>= m_Nodes.GetSize()) Stack->PushNULL();
		else{
			switch(m_Nodes[node]->m_Type){
				case OBJECT_ENTITY:
					Stack->PushNative(m_Nodes[node]->m_Entity, true);
					break;
				case OBJECT_REGION:
					Stack->PushNative(m_Nodes[node]->m_Region, true);
					break;
				default:
					Stack->PushNULL();
			}
		}
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// AddRegion / AddEntity
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "AddRegion")==0 || strcmp(Name, "AddEntity")==0)
	{
		Stack->CorrectParams(1);
		CScValue* Val = Stack->Pop();

		CAdSceneNode* Node = new CAdSceneNode(Game);
		if(strcmp(Name, "AddRegion")==0)
		{
			CAdRegion* Region = new CAdRegion(Game);
			if(!Val->IsNULL()) Region->SetName(Val->GetString());
			Node->SetRegion(Region);
			Stack->PushNative(Region, true);
		}
		else
		{
			CAdEntity* Entity = new CAdEntity(Game);
			if(!Val->IsNULL()) Entity->SetName(Val->GetString());
			Node->SetEntity(Entity);
			Stack->PushNative(Entity, true);
		}
		m_Nodes.Add(Node);
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// InsertRegion / InsertEntity
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "InsertRegion")==0 || strcmp(Name, "InsertEntity")==0)
	{
		Stack->CorrectParams(2);
		int Index = Stack->Pop()->GetInt();
		CScValue* Val = Stack->Pop();

		CAdSceneNode* Node = new CAdSceneNode(Game);
		if(strcmp(Name, "InsertRegion")==0)
		{
			CAdRegion* Region = new CAdRegion(Game);
			if(!Val->IsNULL()) Region->SetName(Val->GetString());
			Node->SetRegion(Region);
			Stack->PushNative(Region, true);
		}
		else
		{
			CAdEntity* Entity = new CAdEntity(Game);
			if(!Val->IsNULL()) Entity->SetName(Val->GetString());
			Node->SetEntity(Entity);
			Stack->PushNative(Entity, true);
		}
		if(Index < 0) Index = 0;
		if(Index <= m_Nodes.GetSize() - 1) m_Nodes.InsertAt(Index, Node);
		else m_Nodes.Add(Node);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// DeleteNode
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "DeleteNode")==0)
	{
		Stack->CorrectParams(1);
		CScValue* Val = Stack->Pop();

		CAdSceneNode* ToDelete = NULL;
		if(Val->IsNative())
		{
			CBScriptable* Temp = Val->GetNative();
			for(int i=0; i<m_Nodes.GetSize(); i++)
			{
				if(m_Nodes[i]->m_Region==Temp || m_Nodes[i]->m_Entity==Temp)
				{
					ToDelete = m_Nodes[i];
					break;
				}
			}
		}
		else
		{
			int Index = Val->GetInt();
			if(Index >= 0 && Index < m_Nodes.GetSize())
			{
				ToDelete = m_Nodes[Index];
			}
		}
		if(ToDelete==NULL)
		{
			Stack->PushBool(false);
			return S_OK;
		}

		for(int i=0; i<m_Nodes.GetSize(); i++)
		{
			if(m_Nodes[i]==ToDelete)
			{				
				SAFE_DELETE(m_Nodes[i]);
				m_Nodes.RemoveAt(i);
				break;
			}
		}
		Stack->PushBool(true);
		return S_OK;
	}

	else return CBObject::ScCallMethod(Script, Stack, ThisStack, Name);
}
//////////////////////////////////////////////////////////////////////////
// high level scripting interface
//////////////////////////////////////////////////////////////////////////
HRESULT CUIObject::ScCallMethod(CScScript* Script, CScStack *Stack, CScStack *ThisStack, char *Name)
{
	//////////////////////////////////////////////////////////////////////////
	// SetFont
	//////////////////////////////////////////////////////////////////////////
	if(strcmp(Name, "SetFont")==0){
		Stack->CorrectParams(1);
		CScValue* Val = Stack->Pop();

		if(m_Font) Game->m_FontStorage->RemoveFont(m_Font);
		if(Val->IsNULL())
		{
			m_Font = NULL;
			Stack->PushBool(true);
		}
		else
		{
			m_Font = Game->m_FontStorage->AddFont(Val->GetString());
			Stack->PushBool(m_Font!=NULL);
		}
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// SetImage
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "SetImage")==0){
		Stack->CorrectParams(1);
		CScValue* Val = Stack->Pop();

		SAFE_DELETE(m_Image);		
		if(Val->IsNULL()){
			Stack->PushBool(true);
			return S_OK;
		}
		
		m_Image = new CBSprite(Game);
		if(!m_Image || FAILED(m_Image->LoadFile(Val->GetString()))){
			SAFE_DELETE(m_Image);
			Stack->PushBool(false);			
		}
		else Stack->PushBool(true);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// GetImage
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "GetImage")==0){
		Stack->CorrectParams(0);
		if(!m_Image || !m_Image->m_Filename) Stack->PushNULL();
		else Stack->PushString(m_Image->m_Filename);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// GetImageObject
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "GetImageObject")==0){
		Stack->CorrectParams(0);
		if(!m_Image) Stack->PushNULL();
		else Stack->PushNative(m_Image, true);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// Focus
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "Focus")==0){
		Stack->CorrectParams(0);
		Focus();
		Stack->PushNULL();
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// MoveAfter / MoveBefore
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "MoveAfter")==0 || strcmp(Name, "MoveBefore")==0){
		Stack->CorrectParams(1);

		if(m_Parent && m_Parent->m_Type == UI_WINDOW){
			CUIWindow* win = (CUIWindow*)m_Parent;

			int i;
			bool found = false;			
			CScValue* val = Stack->Pop();
			// find directly
			if(val->IsNative()){
				CUIObject* widget = (CUIObject*)val->GetNative();
				for(i=0; i<win->m_Widgets.GetSize(); i++){
					if(win->m_Widgets[i]==widget){
						found = true;
						break;
					}
				}
			}
			// find by name
			else{
				char* name = val->GetString();
				for(i=0; i<win->m_Widgets.GetSize(); i++){
					if(CBPlatform::stricmp(win->m_Widgets[i]->m_Name, name)==0){
						found = true;
						break;
					}
				}
			}

			if(found){
				bool done=false;
				for(int j=0; j<win->m_Widgets.GetSize(); j++){
					if(win->m_Widgets[j]==this){
						if(strcmp(Name, "MoveAfter")==0) i++;
						if(j>=i) j++;

						win->m_Widgets.InsertAt(i, this);
						win->m_Widgets.RemoveAt(j);						

						done = true;
						Stack->PushBool(true);
						break;
					}
				}
				if(!done) Stack->PushBool(false);
			}
			else Stack->PushBool(false);

		}
		else Stack->PushBool(false);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// MoveToBottom
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "MoveToBottom")==0){
		Stack->CorrectParams(0);

		if(m_Parent && m_Parent->m_Type == UI_WINDOW){
			CUIWindow* win = (CUIWindow*)m_Parent;
			for(int i=0; i<win->m_Widgets.GetSize(); i++){
				if(win->m_Widgets[i]==this){
					win->m_Widgets.RemoveAt(i);
					win->m_Widgets.InsertAt(0, this);
					break;
				}
			}
			Stack->PushBool(true);
		}
		else Stack->PushBool(false);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// MoveToTop
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "MoveToTop")==0){
		Stack->CorrectParams(0);

		if(m_Parent && m_Parent->m_Type == UI_WINDOW){
			CUIWindow* win = (CUIWindow*)m_Parent;
			for(int i=0; i<win->m_Widgets.GetSize(); i++){
				if(win->m_Widgets[i]==this){
					win->m_Widgets.RemoveAt(i);
					win->m_Widgets.Add(this);
					break;
				}
			}
			Stack->PushBool(true);
		}
		else Stack->PushBool(false);

		return S_OK;
	}

	else return CBObject::ScCallMethod(Script, Stack, ThisStack, Name);
}
//////////////////////////////////////////////////////////////////////////
// high level scripting interface
//////////////////////////////////////////////////////////////////////////
HRESULT CAdActor::ScCallMethod(CScScript* Script, CScStack *Stack, CScStack *ThisStack, char *Name)
{
	//////////////////////////////////////////////////////////////////////////
	// GoTo / GoToAsync
	//////////////////////////////////////////////////////////////////////////
	if(strcmp(Name, "GoTo")==0 || strcmp(Name, "GoToAsync")==0)
	{
		Stack->CorrectParams(2);
		int X = Stack->Pop()->GetInt();
		int Y = Stack->Pop()->GetInt();
		GoTo(X, Y);
		if(strcmp(Name, "GoToAsync")!=0) Script->WaitForExclusive(this);
		Stack->PushNULL();
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// GoToObject / GoToObjectAsync
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "GoToObject")==0 || strcmp(Name, "GoToObjectAsync")==0)
	{
		Stack->CorrectParams(1);
		CScValue* Val = Stack->Pop();
		if(!Val->IsNative())
		{
			Script->RuntimeError("actor.%s method accepts an entity refrence only", Name);
			Stack->PushNULL();
			return S_OK;
		}
		CAdObject* Obj = (CAdObject*)Val->GetNative();
		if(!Obj || Obj->m_Type != OBJECT_ENTITY)
		{
			Script->RuntimeError("actor.%s method accepts an entity refrence only", Name);
			Stack->PushNULL();
			return S_OK;
		}
		CAdEntity* Ent = (CAdEntity*)Obj;
		if(Ent->m_WalkToX==0 && Ent->m_WalkToY==0) GoTo(Ent->m_PosX, Ent->m_PosY);
		else GoTo(Ent->m_WalkToX, Ent->m_WalkToY, Ent->m_WalkToDir);
		if(strcmp(Name, "GoToObjectAsync")!=0) Script->WaitForExclusive(this);
		Stack->PushNULL();
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// TurnTo / TurnToAsync
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "TurnTo")==0 || strcmp(Name, "TurnToAsync")==0)
	{
		Stack->CorrectParams(1);
		int dir;
		CScValue* val = Stack->Pop();
		
		// turn to object?
		if(val->IsNative() && Game->ValidObject((CBObject*)val->GetNative()))
		{
			CBObject* obj = (CBObject*)val->GetNative();
			int angle = (int)(atan2((double)(obj->m_PosY - m_PosY), (double)(obj->m_PosX - m_PosX)) * (180/3.14));
			dir = (int)AngleToDirection(angle);
		}
		// otherwise turn to direction
		else dir = val->GetInt();

		if(dir >= 0 && dir < NUM_DIRECTIONS)
		{
			TurnTo((TDirection)dir);
			if(strcmp(Name, "TurnToAsync")!=0) Script->WaitForExclusive(this);
		}
		Stack->PushNULL();
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// IsWalking
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "IsWalking")==0)
	{
		Stack->CorrectParams(0);
		Stack->PushBool(m_State==STATE_FOLLOWING_PATH);
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// MergeAnims
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "MergeAnims")==0)
	{
		Stack->CorrectParams(1);
		Stack->PushBool(SUCCEEDED(MergeAnims(Stack->Pop()->GetString())));
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// UnloadAnim
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "UnloadAnim")==0)
	{
		Stack->CorrectParams(1);
		char* AnimName = Stack->Pop()->GetString();

		bool Found = false;
		for(int i=0; i<m_Anims.GetSize(); i++)
		{
			if(stricmp(m_Anims[i]->m_Name, AnimName)==0)
			{
				// invalidate sprites in use
				if(m_Anims[i]->ContainsSprite(m_TempSprite2)) m_TempSprite2 = NULL;
				if(m_Anims[i]->ContainsSprite(m_CurrentSprite)) m_CurrentSprite = NULL;
				if(m_Anims[i]->ContainsSprite(m_AnimSprite2)) m_AnimSprite2 = NULL;

				SAFE_DELETE(m_Anims[i]);
				m_Anims.RemoveAt(i);
				i--;
				Found = true;
			}
		}
		Stack->PushBool(Found);
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// HasAnim
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "HasAnim")==0)
	{
		Stack->CorrectParams(1);
		char* AnimName = Stack->Pop()->GetString();
		Stack->PushBool(GetAnimByName(AnimName) != NULL);
		return S_OK;
	}

	else return CAdTalkHolder::ScCallMethod(Script, Stack, ThisStack, Name);
}