// MissionData.cpp: implementation of the MissionData class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MW4GameEd2.h"
#include "MissionData.h"
#include <Adept\Mission.hpp>
#include <ElementRenderer\StateChange.hpp>
#include <MLR\MLRPrimitiveBase.hpp>

using namespace Stuff;
using namespace Adept;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

MissionData::MissionData()
{
	ModelData=NULL;
}

void MissionData::MakeDefault()
{
	Check_Object(ModelData);
	SetSearchLights(false);
	SetRunningLights(false);
	SetRespawn(false);
	SetHeatSinkEfficiency(1.0f);

	SetDayFogColor(RGBAColor(1.0f,1.0f,1.0f,1.0f));
	SetNightFogColor(RGBAColor(0.0f,0.0f,0.0f,0.0f));
	SetNightGroundColor(RGBAColor(0.0f,0.0f,0.0f,0.0f));

	SetGeneralFog(1,100,1000);
	SetCustomFog(1,100,1000);
	SetHeightFog(1,100,1000);
	SetLightFog(1,100,1000);


//	ModelData->m_canSetTime=false;

	
}

bool MissionData::SetAttribute(char *attributeName,bool value)
{
	return value?SetAttribute(attributeName,"true"):SetAttribute(attributeName,"false");

}

bool MissionData::SetAttribute(char* attributeName, Scalar value) 
{
	CString str;
	str.Format("%f",value);
	return SetAttribute(attributeName,str);
}

bool MissionData::SetAttribute(char* attributeName, RGBAColor value) 
{
	CString str;
	str.Format("%4.2f %4.2f %4.2f 1.0",value.red,value.green,value.blue);

	return SetAttribute(attributeName,str);
}

bool MissionData::SetAttribute(char *attributeName,LPCSTR value)
{
	char error_message[256];

	Mission::ClassData *class_data= Mission::GetInstance()->GetClassData();
	Check_Object(class_data);

	ModelAttributeEntry *attrib_entry=class_data->gameModelAttributeTable.GetAttributeEntry(attributeName);;
	Check_Object(attrib_entry);

	return (*class_data->modelVerifier)((Entity__GameModel *)ModelData, attrib_entry, value, (char **)&error_message,255);
}

bool MissionData::SetAttribute(char *attributeName,ResourceID &value)
{
//	SPEW(("tsteinke","Resource Problem Here"));
//	return true;
	if(value!=ResourceID::Null)
	{
	Resource res(value);
	res.LoadData();
	return SetAttribute(attributeName,res.GetName());
	}
	else
	return SetAttribute(attributeName,"Null");
}


void MissionData::UpdateVisualSettings()
{
	MidLevelRenderer::MLRPrimitiveBase::SetFogTableEntry(
		1,
		ModelData->m_generalFogStart,
		ModelData->m_generalFogEnd,
		ModelData->m_generalFogDensity
	);
	MidLevelRenderer::MLRPrimitiveBase::SetFogTableEntry(
		2,
		ModelData->m_lightFogStart,
		ModelData->m_lightFogEnd,
		ModelData->m_lightFogDensity
	);
	MidLevelRenderer::MLRPrimitiveBase::SetFogTableEntry(
		3,
		ModelData->m_customFogStart,
		ModelData->m_customFogEnd,
		ModelData->m_customFogDensity
	);
	MidLevelRenderer::MLRPrimitiveBase::SetFogTableEntry(
		MidLevelRenderer::Limits::Max_Number_Of_FogStates,
		ModelData->m_heightFogStart,
		ModelData->m_heightFogEnd,
		ModelData->m_heightFogOpacity
	);
}


void MissionData::SetDayFogColor(RGBAColor &col)
{
	SetAttribute("FogColor",col);
	UpdateVisualSettings();
}	

void MissionData::SetNightFogColor(RGBAColor &col)
{
	SetAttribute("NightFogColor",col);
	UpdateVisualSettings();
}	

void MissionData::SetNightGroundColor(RGBAColor &col)
{
	SetAttribute("NightGroundColor",col);
	UpdateVisualSettings();
}	

void MissionData::SetFogColorUnderWater(RGBAColor &col)
{
	SetAttribute("FogColorUnderwater",col);
	UpdateVisualSettings();
}	

void MissionData::SetFogColorSmoke(RGBAColor &col)
{
	SetAttribute("FogColorSmoke",col);
	UpdateVisualSettings();
}	


void MissionData::SetGeneralFog(Scalar start,Scalar end,Scalar density)
{
	SetAttribute("GeneralFogDensity",density);
	SetAttribute("GeneralFogEnd",end);
	SetAttribute("GeneralFogStart",start);
UpdateVisualSettings();
}	

void MissionData::SetCustomFog(Scalar start,Scalar end,Scalar density)
{
	SetAttribute("CustomFogDensity",density);
	SetAttribute("CustomFogEnd",end);
	SetAttribute("CustomFogStart",start);
UpdateVisualSettings();
}	

void MissionData::SetLightFog(Scalar start,Scalar end,Scalar density)
{
	SetAttribute("LightFogDensity",density);
	SetAttribute("LightFogEnd",end);
	SetAttribute("LightFogStart",start);
UpdateVisualSettings();
}	

void MissionData::SetHeightFog(Scalar start,Scalar end,Scalar density)
{
	if(density>0.0f && density<=1.0f)
		SetAttribute("HeightFogOpacity",density);
	SetAttribute("HeightFogEnd",end);
	SetAttribute("HeightFogStart",start);
UpdateVisualSettings();
}	

void MissionData::SetRespawn(bool val)
{
	SetAttribute("AllowRespawn",val);
}	

void MissionData::SetRunningLights(bool val)
{
	SetAttribute("AllowRunningLights",val);
}	

void MissionData::SetSearchLights(bool val)
{
	SetAttribute("AllowSearchLights",val);
}	

void MissionData::SetHeatSinkEfficiency(Scalar val)
{
	SetAttribute("HeatSinkEfficiency",val);
}	

void MissionData::SetWeather(ResourceID &val)
{
	SetAttribute("WeatherEffectResource",val);
}	

void MissionData::SetNightWeather(ResourceID &val)
{
	SetAttribute("NightWeatherEffectResource",val);
}	


RGBAColor MissionData::GetDayFogColor()
{
return ModelData->m_fogColor;
}	

RGBAColor MissionData::GetNightFogColor()
{
return ModelData->m_nightFogColor;
}	

RGBAColor MissionData::GetNightGroundColor()
{
return ModelData->m_nightGroundColor;
}	

RGBAColor MissionData::GetFogColorUnderWater()
{
return ModelData->m_fogColorUnderwater;
}	

RGBAColor MissionData::GetFogColorSmoke()
{
return ModelData->m_fogColorSmoke;
}	

ResourceID MissionData::GetWeather()
{
return ModelData->m_weatherEffectResource;
}	

ResourceID MissionData::GetNightWeather()
{
return ModelData->m_nightWeatherEffectResource;
}	

void MissionData::GetGeneralFog(Scalar *start,Scalar *endp,Scalar *density)
{
*start=ModelData->m_generalFogStart;
*endp=ModelData->m_generalFogEnd;
*density=ModelData->m_generalFogDensity;
}	

void MissionData::GetCustomFog(Scalar *start,Scalar *endp,Scalar *density)
{
*start=ModelData->m_customFogStart;
*endp=ModelData->m_customFogEnd;
*density=ModelData->m_customFogDensity;
}	

void MissionData::GetLightFog(Scalar *start,Scalar *endp,Scalar *density)
{
*start=ModelData->m_lightFogStart;
*endp=ModelData->m_lightFogEnd;
*density=ModelData->m_lightFogDensity;
}	

void MissionData::GetHeightFog(Scalar *start,Scalar *endp,Scalar *density)
{
*start=ModelData->m_heightFogStart;
*endp=ModelData->m_heightFogEnd;
*density=ModelData->m_heightFogOpacity;
}	

bool MissionData::GetRespawn()
{
return ModelData->m_allowRespawn;
}	

bool MissionData::GetRunningLights()
{
return ModelData->m_allowRunningLights;
}	

bool MissionData::GetSearchLights()
{
return ModelData->m_allowSearchLights;
}	

Scalar MissionData::GetHeatSinkEfficiency()
{
return ModelData->m_heatSinkEfficiency;
}	

void MissionData::SyncToGame()
{
	ModelData = Mission::GetInstance()->GetGameModel();
	

	
	Check_Object(ResourceManager::Instance);
	ResourceFile *res_file;
	res_file=ResourceManager::Instance->GetResourceFile(0);
	Check_Object(res_file);
	
	
	
}

void MissionData::SaveText(Stuff::NotationFile *not_file)
{
	Mission__GameModel::SaveGameModel((Mission__GameModel *)Mission::GetInstance()->GetGameModel(),not_file);
}

MissionData::~MissionData()
{

}
