//===========================================================================//
// File:	Torso_Tool.cpp										
//---------------------------------------------------------------------------//
//   Date    Who  Modification                                               //
// --------  ---  ---------------------------------------------------------- //
// 02/12/99  DPB  Created File
//---------------------------------------------------------------------------//
// Copyright (C) 1998, Fasa Interactive										 //
// All Rights reserved worldwide                                             //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL               //
//===========================================================================//

#include "MW4Headers.hpp"

#include "Torso.hpp"
#include "MWTool.hpp"

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

	//
	//--------------
	// Set up stream
	//--------------
	//
	MemoryStream *model_stream = script->modelStream;
	Check_Object(model_stream);
	model_stream->AllocateBytes(sizeof(Torso__GameModel));
	Subsystem__GameModel::ConstructGameModel(script);

}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
bool
	Torso__GameModel::ReadAndVerify(
		Torso__GameModel *model,
		ModelAttributeEntry *attribute_entry, 
		const char *data,
		char **error,
		int error_buffer
	)
{
	Check_Object(attribute_entry);

	bool result = false;
	bool valid_data = true;

	if(!strcmp(data, ""))
	{
		valid_data = false;
	}

	//
	//----------------------------------------
	//Read in the values from the data entered
	//----------------------------------------
	//
	result = 
		Subsystem__GameModel::ReadAndVerify(
			model, 
			attribute_entry, 
			data,
			error,
			error_buffer
		);


	//
	//---------------------------
	//Verify all the model values
	//---------------------------
	//
	switch(attribute_entry->attributeID)
	{


	case TwistSpeedAttributeID:
		{
			if(!valid_data)
			{
				Scalar value = 80.0f;
				attribute_entry->SetValue(model, &value);
				result = true;
			}
			if (model->twistSpeed < 0)
			{
				_snprintf(
					*error,
					error_buffer,
					"{[GameData]TwistSpeed=%f}: value must be >= 0!",
					model->twistSpeed
				);
				result = false;
			}
			else
			{
				model->twistSpeed = model->twistSpeed * Radians_Per_Degree;
			}

			break;
		}
	
	case PitchSpeedAttributeID:
		{
			if(!valid_data)
			{
				Scalar value = 80.0f;
				attribute_entry->SetValue(model, &value);
				result = true;
			}
			if (model->pitchSpeed < 0)
			{
				_snprintf(
					*error,
					error_buffer,
					"{[GameData]PitchSpeed=%f}: value must be >= 0!",
					model->pitchSpeed
				);
				result = false;
			}
			else
			{
				model->pitchSpeed = model->pitchSpeed * Radians_Per_Degree;
			}

			break;
		}

	case TwistRadiusAttributeID:
		{
			if(!valid_data)
			{
				Scalar value = 100.0f;
				attribute_entry->SetValue(model, &value);
				result = true;
			}
			if (model->twistRadius < 0)
			{
				_snprintf(
					*error,
					error_buffer,
					"{[GameData]TwistRadius=%f}: value must be >= 0!",
					model->twistRadius
				);
				result = false;
			}
			else
			{
				model->twistRadius = model->twistRadius * Radians_Per_Degree;
			}

			break;
		}
	
	case PitchRadiusAttributeID:
		{
			if(!valid_data)
			{
				Scalar value = 80.0f;
				attribute_entry->SetValue(model, &value);
				result = true;
			}
			if (model->pitchRadius < 0)
			{
				_snprintf(
					*error,
					error_buffer,
					"{[GameData]PitchRadius=%f}: value must be >= 0!",
					model->pitchRadius
				);
				result = false;
			}
			else
			{
				model->pitchRadius = model->pitchRadius * Radians_Per_Degree;
			}

			break;
		}
	case TwistJointNameAttributeID:
		{
			if(!valid_data)
			{
				_snprintf(
					*error,
					error_buffer,
					"{[GameData]TwistJointName}: Entry Must Exisit"
				);
			}
			break;
		}
	case PitchJointNameAttributeID:
		{
			if(!valid_data)
			{
				_snprintf(
					*error,
					error_buffer,
					"{[GameData]PitchJointName}: Entry Must Exisit"
				);
			}
			break;
		}

	case LeftArmJointNameAttributeID:
		{
			if (!valid_data)
			{
				char *value;
				value = new char[MaxStringLength];
				Register_Pointer(value);
				Str_Copy(value, "0", MaxStringLength);
				attribute_entry->SetValue(model, value);
				result = true;
				Unregister_Pointer(value);
				delete value;
			}
			break;
		}

	case RightArmJointNameAttributeID:
		{
			if (!valid_data)
			{
				char *value;
				value = new char[MaxStringLength];
				Register_Pointer(value);
				Str_Copy(value, "0", MaxStringLength);
				attribute_entry->SetValue(model, value);
				result = true;
				Unregister_Pointer(value);
				delete value;
				
			}
			break;
		}	

	case EyeJointNameAttributeID:
		{
			if (!valid_data)
			{
				char *value;
				value = new char[MaxStringLength];
				Register_Pointer(value);
				Str_Copy(value, "0", MaxStringLength);
				attribute_entry->SetValue(model, value);
				result = true;
				Unregister_Pointer(value);
				delete value;
			
			}
			break;
		}

	case CageJointNameAttributeID:
		{
			if (!valid_data)
			{
				char *value;
				value = new char[MaxStringLength];
				Register_Pointer(value);
				Str_Copy(value, "0", MaxStringLength);
				attribute_entry->SetValue(model, value);
				result = true;
				Unregister_Pointer(value);
				delete value;
			
			}
			break;
		}

	case ArmRatioAngleAttributeID:
		{
			if(!valid_data)
			{
				Scalar value = 20.0f;
				attribute_entry->SetValue(model, &value);
				result = true;
			}
			if (model->armRatioAngle < 0)
			{
				_snprintf(
					*error,
					error_buffer,
					"{[GameData]ArmRatioAngle=%f}: value must be >= 0!",
					model->armRatioAngle
				);
				result = false;
			}
			else
			{
				model->armRatioAngle = model->armRatioAngle * Radians_Per_Degree;
			}

			break;
		}
	

	case CageRatioAngleAttributeID:
		{
			if(!valid_data)
			{
				Scalar value = 1.0f;
				attribute_entry->SetValue(model, &value);
				result = true;
			}
			if (model->cageRatioAngle < 0)
			{
				_snprintf(
					*error,
					error_buffer,
					"{[GameData]CageRatioAngle=%f}: value must be >= 0!",
					model->cageRatioAngle
				);
				result = false;
			}
			

			break;
		}
	}
	return result;
}