//===========================================================================//
// File:	AI_Tool.cpp																	  //
//---------------------------------------------------------------------------//
//   Date    Who  Modification                                               //
// --------  ---  ---------------------------------------------------------- //
// 07/13/99  AHF  Created Files               //
//---------------------------------------------------------------------------//
// Copyright (C) 1999, Microsoft										 //
// All Rights reserved worldwide                                             //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL               //
//===========================================================================//

#include "MW4Headers.hpp"

#include "AI.hpp"
#include "adept\Tool.hpp"
#include "adept\Interface.hpp"
#include "adept\NameTable.hpp"
#include "AI_UserConstants.hpp"

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void AI__CreateMessage::ConstructCreateMessage(Script *script)
{
	Check_Object(script);
#if defined(_ARMOR)
	int stack_level = Tool::Instance->GetStackLevel();
#endif

	//
	//--------------
	// Set up stream
	//--------------
	//
	MemoryStream *message_stream = script->messageStream;
	Check_Object(message_stream);
	message_stream->AllocateBytes(sizeof(AI__CreateMessage));
	Driver::CreateMessage::ConstructCreateMessage(script);
	Verify(stack_level == Tool::Instance->GetStackLevel());
	AI__CreateMessage *message =
	Cast_Pointer(AI__CreateMessage*,message_stream->GetPointer());
	message->messageLength = sizeof(*message);

	//
	//---------------------------------------------------------------
	// Point at the notation file and make other files relative to it
	//---------------------------------------------------------------
	//
	Page *page = script->instancePage;
	Check_Object(page);
	Check_Object(Tool::Instance);

	//
	//----------------------------
	// Get the Driver's vehicle id
	//----------------------------
	//
	const char *entity_name;
	page->GetEntry("Entity", &entity_name, true);
	message->entityID = NameTable::GetInstance()->FindID(entity_name);

	const char *ablscript;
	message->scriptName[0] = 0;
	if (page->GetEntry("Script",&ablscript))
	{
/*
		if (strnicmp (ablscript,"Content",7))
		{
			Str_Copy (message->scriptName,"Content\\",sizeof (message->scriptName));		// hack because directories change when running the game.
			Str_Cat (message->scriptName,Tool::Instance->GetCurrentStackPath (),sizeof (message->scriptName));
			Str_Cat (message->scriptName,ablscript,sizeof (message->scriptName));
		}
		else
*/
		{
			if (gos_DoesFileExist (ablscript))
			{
				if (FileStream::IsRedirected)
				{
					Str_Copy (message->scriptName,FileStream::RedirectedName,sizeof (message->scriptName));
				}
				else
				{
					Str_Copy (message->scriptName,ablscript,sizeof (message->scriptName));
				}
				FileStream::IsRedirected = false;
			}
			else
				message->scriptName[0] = 0;
			
		}

	}

	MW4AI::UserConstants *data;
	data = MW4AI::UserConstants::Instance ();
	message->towardsPilotSkill = 0;
	message->towardsGunnerySkill = 0;
	message->towardsEliteSkill = 0;
	message->pilotSkill = -1;
	message->gunnerySkill = -1;
	message->eliteSkill = -1;
	message->minHeatSkill = -1;
	message->maxHeatSkill = -1;
	message->sensorSkill = -1;
	message->blindFightingSkill = -1;
	message->longRangeGunnerySkill = -1;
	message->shortRangeGunnerySkill = -1;

	page->GetEntry("Sensor",&message->sensorSkill,false);
	page->GetEntry("BlindFighting",&message->blindFightingSkill,false);
	page->GetEntry("LongRange",&message->longRangeGunnerySkill,false);
	page->GetEntry("ShortRange",&message->shortRangeGunnerySkill,false);

	page->GetEntry("Gunnery",&message->gunnerySkill,false);
	page->GetEntry("Pilot",&message->pilotSkill,false);
	page->GetEntry("Elite",&message->eliteSkill,false);
	page->GetEntry("MinHeat",&message->minHeatSkill,false);
	page->GetEntry("MaxHeat",&message->maxHeatSkill,false);
	page->GetEntry("TowardsGunnery",&message->towardsGunnerySkill,false);
	page->GetEntry("TowardsPilot",&message->towardsPilotSkill,false);
	page->GetEntry("TowardsElite",&message->towardsEliteSkill,false);

	int i;

	for (i=0;i<DEFAULT_MEMORYCELL_VALUES;i++)
	{
		char name[255];
		Stuff::Scalar value;
		sprintf (name,"Cell%d",i);
		value = 0;
		if (!page->GetEntry(name,&value))
			value = 0;
		message->cellValues[i] = value;
	}

//	const char* script_params_ptr;
//	page->GetEntry("ScriptParams",&script_params_ptr);
//	strncpy(message->scriptParams,script_params_ptr,2000);

	Verify(stack_level == Tool::Instance->GetStackLevel());
}

void AI::SaveInstanceText(Page *instance_page)
{
	Check_Object(this);
	Check_Object(instance_page);

	Entity::SaveInstanceText(instance_page);

	instance_page->SetEntry("Entity", vehicle->instanceName);
	instance_page->SetEntry("DropZone",dropZone);

	char name[255];
	int i,size;

	size = scriptName.size();
	if (size)			//handle case when no script attached
	{
		for (i=size;i>=0;i--)
		{
			if (scriptName[i] == '\\')
				break;
		}

		Str_Copy (name,"scripts",sizeof (name));
		Str_Cat (name,&(scriptName[i]),sizeof (name));
		instance_page->SetEntry("Script",name);
	}

	instance_page->SetEntry("Gunnery",m_GunnerySkill);
	instance_page->SetEntry("Pilot",m_PilotSkill);
	instance_page->SetEntry("Elite",m_EliteSkill);
	instance_page->SetEntry("MinHeat",m_MinHeatSkill);
	instance_page->SetEntry("MaxHeat",m_MaxHeatSkill);
	instance_page->SetEntry("Sensor",m_SensorSkill);
	instance_page->SetEntry("BlindFighting",m_BlindFightingSkill);
	instance_page->SetEntry("LongRange",m_LongRangeGunnerySkill);
	instance_page->SetEntry("ShortRange",m_ShortRangeGunnerySkill);

	instance_page->SetEntry("TowardsGunnery",m_TowardsGunnerySkill);
	instance_page->SetEntry("TowardsPilot",m_TowardsPilotSkill);
	instance_page->SetEntry("TowardsElite",m_TowardsEliteSkill);

	Verify (MW4AI::NUM_MEMORY_CELLS >= DEFAULT_MEMORYCELL_VALUES);
	for (i=0;i<DEFAULT_MEMORYCELL_VALUES;i++)
	{
		char name[255];
		sprintf (name,"Cell%d",i);
		instance_page->SetEntry(name,memory[i]);
	}

	instance_page->SetEntry("ScriptParams",""/* m_ScriptParams.c_str() */);
}
