//===========================================================================//
// File:	WeaponMover_Tool.cpp																	 
//---------------------------------------------------------------------------//
//   Date    Who  Modification                                               //
// --------  ---  ---------------------------------------------------------- //
// 09/09/98  JSE  Inital base class based off of Shadowrun/Adept                                
// 09/25/98  BDB  Inital WeaponMover class based off of Vehicle_Tool 
//---------------------------------------------------------------------------//
// Copyright (C) 1998, Fasa Interactive										 //
// All Rights reserved worldwide                                             //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL               //
//===========================================================================//

#include "MW4Headers.hpp"

#include "WeaponMover.hpp"
#include "MWObject.hpp"
#include "MWTool.hpp"

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	WeaponMover__GameModel::ConstructGameModel(Script *script)
{
	Check_Object(script);

	//
	//--------------
	// Set up stream
	//--------------
	//
	MemoryStream *model_stream = script->modelStream;
	Check_Object(model_stream);
	model_stream->AllocateBytes(sizeof(WeaponMover__GameModel));
	Mover__GameModel::ConstructGameModel(script);
	WeaponMover__GameModel *model =
		Cast_Pointer(WeaponMover__GameModel*, model_stream->GetPointer());
	//
	//------------------------------
	// Set up the model file pointer
	//------------------------------
	//
	NotationFile *model_file = script->modelFile;
	Check_Object(model_file);
	model->hitEffectMaterialTable = ResourceID::Null;
	Page *page = model_file->GetPage("GameData");
	Check_Object(page);
	NotationFile effects_file;
	if (page->GetEntry("HitEffectsFile", &effects_file))
	{
		FileDependencies *old_deps = Resource::ParentFileDependencies;
		FileDependencies effect_deps(*effects_file.GetFileDependencies());
		Resource::ParentFileDependencies = &effect_deps;
		model->hitEffectMaterialTable = MWObject__GameModel::CreateEffectTableResource(&effects_file, MaterialCount);
		Resource::ParentFileDependencies = old_deps;
		old_deps->AddDependencies(&effect_deps);
	}
	model->secondaryHitEffectMaterialTable = ResourceID::Null;
	if (page->GetEntry("SecondaryHitEffectsFile", &effects_file))
	{
		FileDependencies *old_deps = Resource::ParentFileDependencies;
		FileDependencies effect_deps(*effects_file.GetFileDependencies());
		Resource::ParentFileDependencies = &effect_deps;
		model->secondaryHitEffectMaterialTable = MWObject__GameModel::CreateEffectTableResource(&effects_file, MaterialCount);
		Resource::ParentFileDependencies = old_deps;
		old_deps->AddDependencies(&effect_deps);
	}

	model->m_craterID = -1;
	const char *crater_string;
	if(page->GetEntry("CraterName", &crater_string))
	{
		MString crater_mstring = crater_string;
		model->m_craterID = crater_mstring.GetHashValue();
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
bool
	WeaponMover__GameModel::ReadAndVerify(
		WeaponMover__GameModel *model,
		ModelAttributeEntry *attribute_entry, 
		const char *data,
		char **error,
		int error_buffer
	)
{
	Check_Object(attribute_entry);
	
	bool result = false;
	bool valid_data = (*data != '\0');

	//
	//----------------------------------------
	//Read in the values from the data entered
	//----------------------------------------
	//
	result = 
		Mover__GameModel::ReadAndVerify(
			model, 
			attribute_entry, 
			data,
			error,
			error_buffer
		);
	//
	//---------------------------
	//Verify all the model values
	//---------------------------
	//
	switch(attribute_entry->attributeID)
	{
	case MaxDistanceAttributeID:
		{
			if(!valid_data)
			{
				Scalar value = 500.0f;
				attribute_entry->SetValue(model, &value);
				result = true;
			}
			if(model->maxDistance <= 0.0f)
			{
				_snprintf(
					*error,
					error_buffer,
					"{[GameData]MaxDistance=%f}: value must be > 0!",
					model->maxDistance
				);
				result = false;
			}
			break;
		}
// MSL 5.03 Min Distance
//	case MinDistanceAttributeID:
//		{
//			if(!valid_data)
//			{
//				Scalar value = 0.0f;
//				attribute_entry->SetValue(model, &value);
//				result = true;
//			}
//			if(model->minDistance < 0.0f)
//			{
//				_snprintf(
//					*error,
//					error_buffer,
//					"{[GameData]MinDistance=%f}: value must be >= 0!",
//					model->minDistance
//				);
//				result = false;
//			}
//			break;
//		}
	case TrailResourceAttributeID:
		{
			if(!valid_data)
			{
				ResourceID value = ResourceID::Null;
				attribute_entry->SetValue(model, (void *)&value);
				result = true;
			}
			break;
		}
	case DoesPlaceCraterAttributeID:
		{
			if(!valid_data)
			{
				bool value = false;
				attribute_entry->SetValue(model,&value);
				result = true;
			}
			break;
		}

	case HitEffectResourceAttributeID:
		{
			if(!valid_data)
			{
				ResourceID value = ResourceID::Null;
				attribute_entry->SetValue(model, (void *)&value);
				result = true;
			}
			break;
		}
	case SecondaryHitEffectResourceAttributeID:
		{
			if(!valid_data)
			{
				ResourceID value = ResourceID::Null;
				attribute_entry->SetValue(model, (void *)&value);
				result = true;
			}
			break;
		}
	case DamageDecalResourceAttributeID:
		{
			if(!valid_data)
			{
				ResourceID value = ResourceID::Null;
				attribute_entry->SetValue(model, (void *)&value);
				result = true;
			}
			break;
		}
	case LightAmpFlareOutAttributeID:
		{
			if(!valid_data)
			{
				Scalar value = 0.0f;
				attribute_entry->SetValue(model, &value);
				result = true;
			}
			if(model->m_lightAmpFlareOut < 0.0f)
			{
				_snprintf(
					*error,
					error_buffer,
					"{[GameData]LightAmpFlareOut=%f}: value must be >= 0!",
					model->m_lightAmpFlareOut
				);
				result = false;
			}
			break;
		}

	}
	return result;
}