
#include "MW4Headers.hpp"

#include "BeamEntity.hpp"
#include "MWObject.hpp"
#include "MWTool.hpp"

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

	//
	//--------------
	// Set up stream
	//--------------
	//
	MemoryStream *model_stream = script->modelStream;
	Check_Object(model_stream);
	model_stream->AllocateBytes(sizeof(BeamEntity__GameModel));
	Entity__GameModel::ConstructGameModel(script);
	BeamEntity__GameModel *model =
		Cast_Pointer(BeamEntity__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);
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
bool
	BeamEntity__GameModel::ReadAndVerify(
		BeamEntity__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 = 
		Entity__GameModel::ReadAndVerify(
			model, 
			attribute_entry, 
			data,
			error,
			error_buffer
		);
	//
	//---------------------------
	//Verify all the model values
	//---------------------------
	//
	switch(attribute_entry->attributeID)
	{
	case HitEffectResourceAttributeID:
		{
			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;
		}
	}

	return result;
}