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

#include "MW4Headers.hpp"
#include "MWVideoRenderer.hpp"
#include "MWApplication.hpp"
#include "MWMission.hpp"
#include "MWMover.hpp"
#include "Vehicle.hpp"
#include "VehicleInterface.hpp"
#include "MWPlayer.hpp"
#include "Subsystem.hpp"
#include "Mech.hpp"
#include "Engine.hpp"
#include "NavPoint.hpp"
#include "Weapon.hpp"
#include "WeaponMover.hpp"
#include "BeamWeapon.hpp"
#include "Decal.hpp"
#include "Torso.hpp"
#include "BeamEntity.hpp"
#include "MW4AnimationSystem.hpp"
#include "AnimationState.hpp"
#include "Building.hpp"
#include "bridge.hpp"
#include "ProjectileWeapon.hpp"
#include "Turret.hpp"
#include "Missile.hpp"
#include "MissileWeapon.hpp"
#include "SRMWeaponSubsystem.hpp"
#include "LRMWeaponSubsystem.hpp"
#include "MRMWeaponSubsystem.hpp"
#include "SSRMWeaponSubsystem.hpp"
#include "combatai.hpp"
#include "shooterai.hpp"
#include "mech_ai.hpp"
#include "rail_move.hpp"
#include "playerai.hpp"
#include "MWEntityManager.hpp"
#include "GUIWeaponManager.hpp"
#include "Narc.hpp"
#include "NarcWeaponSubsystem.hpp"
#include "Sensor.hpp"
#include "Beagle.hpp"
#include "ECM.hpp"
#include "Path.hpp"
#include "objective.hpp"
#include "MWDamageObject.hpp"
#include "StickyMover.hpp"
#include "FlareWeapon.hpp"
#include "MWObject.hpp"
#include "cultural.hpp"
#include "Airplane.hpp"
#include "PlaneAI.hpp"
#include "Helicopter.hpp"
#include "AirplaneAnimationStateEngine.hpp"
#include "BombWeaponSubsystem.hpp"
#include "EffectGenerator.hpp"
#include "VOEntity.hpp"
#include "CameraShip.hpp"
#include "HeatManager.hpp"
#include "HeatSink.hpp"
#include "LongTomWeapon.hpp"
#include "AMS.hpp"
#include "FlamerWeapon.hpp"
#include "FlameMover.hpp"
#include "ArtilleryMark.hpp"
#include "ArtilleryWeapon.hpp"
#include "BombastWeapon.hpp"
#include "DeathEntity.hpp"
#include "dropship.hpp"
#include "SearchLight.hpp"
#include "LightEntity.hpp"
#include "Explosive.hpp"
#include "HighExplosiveWeapon.hpp"
#include "JumpJet.hpp"
#include "GUILightAmp.hpp"
#include "GUIStaticView.hpp"
#include "mwmap.hpp"
#include "HoverCraft.hpp"
#include "Tank.hpp"
#include "Truck.hpp"
#include "noncomai.hpp"
#include "noncom.hpp"
#include "Armor.hpp"
#include "Gyro.hpp"
#include "EyePointManager.hpp"
#include "lancemate.hpp"
#include "MFB.hpp"
#include "AI_Statistics.hpp"
#include "Salvage.hpp"
#include "boat.hpp"
#include "LBXMover.hpp"
#include "WaterCultural.hpp"
#include "flag.hpp"
#include "ObservationVehicle.hpp"
#include "MWCampaign.hpp"
#include "LBXWeaponSub.hpp"
#include "Team.hpp"
#include "hudcomp.hpp"
#include "field_base.hpp"

// MSL 5.02 SubSystems
//#include "EnhancedOptics.hpp"
#include "IFF_Jammer.hpp"
#include "AdvancedGyro.hpp"
//#include "ArrowWeaponSubsystem.hpp"

// MSL 5.03 RTX
//#include "RTXWeaponSub.hpp"

void InitComFuncs (void);
void KillComFuncs (void);

// hyun begin
typedef long LONG;
typedef struct DIJOYSTATE { 
    LONG    lX; 
    LONG    lY; 
    LONG    lZ; 
    LONG    lRx; 
    LONG    lRy; 
    LONG    lRz; 
    LONG    rglSlider[2];
    DWORD   rgdwPOV[4];
    BYTE    rgbButtons[32];
} DIJOYSTATE, *LPDIJOYSTATE; 

extern int g_bUseOrgJoy;
extern void (__stdcall *g_pfnRIO_Joy)(DIJOYSTATE& js);
extern void (__stdcall *g_pfnRIO_ButtonEvent)(BYTE* by);
extern void __stdcall RIO_Joy(DIJOYSTATE& js);
extern void __stdcall RIO_ButEvent(BYTE* by);

// hyun end

namespace MW4AI
{
 extern	DWORD Obstacle_Size;
}

HGOSHEAP
	MechWarrior4::Heap = NULL,
	MechWarrior4::g_LibraryHeap = NULL;

Scalar
	MechWarrior4::g_Gravity = 9.8f;

#if 0
	extern bool g_AbzugFlag;				// true if abzug is running the game
	bool s_NoAbzug = false;
#endif

namespace ABL
{
	extern bool g_MissionMusic;
}


#define SKIPPY_CAST(Type,var)	(*((Type*)&(var)))


int MechWarrior4::ENCRYPT (int value)

{
	return value ^ CODE_KEY;
}

int MechWarrior4::DECRYPT (int value)

{
	return value ^ CODE_KEY;
}

Stuff::Scalar MechWarrior4::ENCRYPT (Stuff::Scalar value)
{
	DWORD cast = SKIPPY_CAST( DWORD , value );
	cast = cast ^ CODE_KEY;
	return SKIPPY_CAST( Stuff::Scalar , cast );
}

Stuff::Scalar MechWarrior4::DECRYPT (Stuff::Scalar value)
{
	DWORD cast = SKIPPY_CAST( DWORD , value );
	cast = cast ^ CODE_KEY;
	return SKIPPY_CAST( Stuff::Scalar , cast );
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	MechWarrior4::InitializeClasses(Stuff::NotationFile *startup_ini)
{
// hyun begin
    if (g_bUseOrgJoy)
        g_pfnRIO_Joy = NULL;
    else
        g_pfnRIO_Joy = RIO_Joy;
	g_pfnRIO_ButtonEvent =  RIO_ButEvent;
// hyun end

	Verify(!g_LibraryHeap);
	g_LibraryHeap = gos_CreateMemoryHeap("MechWarrior4(All)");
	Check_Pointer(g_LibraryHeap);

	Verify(!Heap);
	Heap = gos_CreateMemoryHeap("MW4 Heap", 0, g_LibraryHeap);
	Check_Pointer(Heap);	

#if !defined(NO_TIMERS)
	StatisticFormat("");
	StatisticFormat("MW4");
	StatisticFormat("===");
	StatisticFormat("");
#endif

	gos_PushCurrentHeap(Heap);

	InitComFuncs ();

	Verify(FirstFreeMW4ClassID <= LastMW4ClassID);

	// initialize classes
	HUDComponent::InitializeClass ();
	MWMap::InitializeClass();
	MW4Animation::AnimInstanceManager::InitializeClass();
	MW4Animation::BlendBuffer::InitializeClass();
	MW4Animation::AnimHierarchyNode::InitializeClass();
	AnimationStateEngine::InitializeClass();
	MechAnimationStateEngine::InitializeClass();
	AirplaneAnimationStateEngine::InitializeClass();
	MWMission::InitializeClass();
	MWMover::ExecutionStateEngine::InitializeClass();
	MWMover::InitializeClass();
	MWObject::InitializeClass();
	Cultural::InitializeClass ();
	Vehicle::ExecutionStateEngine::InitializeClass();
	Vehicle::InitializeClass();
	VehicleInterface::ExecutionStateEngine::InitializeClass();
	VehicleInterface::InitializeClass();
	MWPlayer::InitializeClass();
	Subsystem::ExecutionStateEngine::InitializeClass();
	Subsystem::InitializeClass();
	Mech::ExecutionStateEngine::InitializeClass();
	Mech::InitializeClass();
	Engine::InitializeClass();
	Weapon::InitializeClass();
	WeaponMover::InitializeClass();
	BeamWeapon::ExecutionStateEngine::InitializeClass();
	BeamWeapon::InitializeClass();
	Decal::InitializeClass();
	Torso::InitializeClass();
	BeamEntity::InitializeClass();
	NonCom::InitializeClass();
	Building::InitializeClass();
	Bridge::InitializeClass();
	ProjectileWeapon::ExecutionStateEngine::InitializeClass();
	ProjectileWeapon::InitializeClass();
	Turret::InitializeClass();
	Missile::ExecutionStateEngine::InitializeClass();
	Missile::InitializeClass();
	MissileWeapon::InitializeClass();
	SRMWeaponSubsystem::InitializeClass();
	LRMWeaponSubsystem::InitializeClass();
	MRMWeaponSubsystem::InitializeClass();
	SSRMWeaponSubsystem::InitializeClass();
	NavPoint::InitializeClass();
	Flag::InitializeClass();
	LancemateManager::InitializeClass();
	AI::InitializeClass();
	MW4AI::CRailGraph::InitializeClass ();
	MW4AI::CPathManager::InitializeClass ();
	MoverAI::InitializeClass ();
	NonComAI::InitializeClass ();
	CombatAI::InitializeClass();
	MechAI::InitializeClass ();
	ShooterAI::InitializeClass ();
	PlayerAI::ExecutionStateEngine::InitializeClass();
	PlayerAI::InitializeClass ();
	GUIWeapon::InitializeClass();
	MWApplication::InitializeClass();
	MWEntityManager::InitializeClass();
	MWVideoRenderer::InitializeClass();
	StickyMover::ExecutionStateEngine::InitializeClass();
	StickyMover::InitializeClass();
	Narc::InitializeClass();
	NarcWeapon::InitializeClass();
	Sensor::ExecutionStateEngine::InitializeClass();
	Sensor::InitializeClass();
	Beagle::InitializeClass();
	ECM::InitializeClass();
	Path::InitializeClass();
	Objective::ExecutionStateEngine::InitializeClass();
	Objective::InitializeClass();
	MWInternalDamageObject::InitializeClass();
	FlareWeapon::InitializeClass();
	Airplane::ExecutionStateEngine::InitializeClass();
	Airplane::InitializeClass();
	PlaneAI::InitializeClass();
	Helicopter::InitializeClass();
	Dropship::ExecutionStateEngine::InitializeClass();
	Dropship::InitializeClass();
	BombWeaponSubsystem::InitializeClass();
	EffectGenerator::InitializeClass();
	VOEntity::InitializeClass();
	CameraShip::ExecutionStateEngine::InitializeClass();
	CameraShip::InitializeClass();
	HeatManager::InitializeClass();
	HeatSink::InitializeClass();
	LongTomWeaponSubsystem::InitializeClass();
	AMS::InitializeClass();
	FlameMover::InitializeClass();
	FlamerWeapon::InitializeClass();
	ArtilleryMark::InitializeClass();
	ArtilleryWeapon::InitializeClass();
	BombastWeapon::ExecutionStateEngine::InitializeClass();
	BombastWeapon::InitializeClass();
	DeathEntity::InitializeClass();
	LightEntity::InitializeClass();
	SearchLight::InitializeClass();
	Explosive::InitializeClass();
	HighExplosiveWeapon::InitializeClass();
	JumpJet::ExecutionStateEngine::InitializeClass();
	JumpJet::InitializeClass();
	Hovercraft::InitializeClass();
	Tank::InitializeClass();
	Truck::InitializeClass();
	Boat::InitializeClass();
	Armor::InitializeClass();
	Gyro::InitializeClass();
	EyePointManager::InitializeClass();
	MFB::ExecutionStateEngine::InitializeClass();
	MFB::InitializeClass();
	FieldBase::ExecutionStateEngine::InitializeClass();
	FieldBase::InitializeClass();
	SalvageManager::InitializeClass();

// MSL 5.02 SubSystems
	AdvancedGyro::InitializeClass();
//	ArrowWeaponSubsystem::InitializeClass();
//	EnhancedOptics::InitializeClass();
	IFF_Jammer::InitializeClass();

// MSL 5.03 RTX
//	RTXWeaponSub::ExecutionStateEngine::InitializeClass();
//	RTXWeaponSub::InitializeClass();


	LBXMover::InitializeClass();
	WaterCultural::InitializeClass();
	ObservationVehicle::InitializeClass();
	MWCampaign::InitializeClass();
	LBXWeaponSub::InitializeClass();
	Team::InitializeClass();
	
//	GUILightAmp::InitializeClass();


	if (startup_ini)
	{
		Check_Object(startup_ini);
		Page *page = startup_ini->FindPage("Network Options");
		if (page)
		{
			Check_Object(page);
			const char *text = NULL;
			
			if (page->GetEntry("ConnectionType", &text))
			{
				MString string = text;

				if (!stricmp(text,"modem"))
				{
					MWApplication::MyConnectionType = MWApplication::Modem;
				}
				else if (!stricmp(text,"isdn"))
				{
					MWApplication::MyConnectionType = MWApplication::ISDN;
				}
				else if (!stricmp(text,"xdsl"))
				{
					MWApplication::MyConnectionType = MWApplication::xDSL;
				}
				else if (!stricmp(text,"cable"))
				{
					MWApplication::MyConnectionType = MWApplication::Cable;
				}
				else if (!stricmp(text,"lan"))
				{
					MWApplication::MyConnectionType = MWApplication::LAN;
				}
			}

			text = NULL;

			if (page->GetEntry("ConnectionSpeed", &text))
			{
				MString string = text;
				if (!stricmp(text,"14"))
				{
					MWApplication::MyConnectionSpeed = MWApplication::c_14_4kbps;
				}
				else if (!stricmp(text,"28"))
				{
					MWApplication::MyConnectionSpeed = MWApplication::c_28_8kbps;
				}
				else if (!stricmp(text,"56"))
				{
					MWApplication::MyConnectionSpeed = MWApplication::c_56_6kbps;
				}
				else if (!stricmp(text,"64"))
				{
					MWApplication::MyConnectionSpeed = MWApplication::c_64_kbps;
				}
				else if (!stricmp(text,"128"))
				{
					MWApplication::MyConnectionSpeed = MWApplication::c_128_kbps;
				}
				else if (!stricmp(text,"384"))
				{
					MWApplication::MyConnectionSpeed = MWApplication::c_384_kbps;
				}
				else if (!stricmp(text,"768"))
				{
					MWApplication::MyConnectionSpeed = MWApplication::c_768_kbps;
				}
				else if (!stricmp(text,"1500"))
				{
					MWApplication::MyConnectionSpeed = MWApplication::c_1500_kbps;
				}
				else if (!stricmp(text,"10000"))
				{
					MWApplication::MyConnectionSpeed = MWApplication::c_10_Mbps;
				}
				else if (!stricmp(text,"100000"))
				{
					MWApplication::MyConnectionSpeed = MWApplication::c_100_Mbps;
				}
			}

			page->GetEntry("PacketSize", &MWApplication::DefaultPacketSize);

			Network::DefaultPacketSize = MWApplication::DefaultPacketSize;

			Max_Clamp(Network::DefaultPacketSize, 1024);
			Min_Clamp(Network::DefaultPacketSize, 128);

		}

	}


	if (startup_ini)
	{
		Check_Object(startup_ini);
		Page *page = startup_ini->FindPage("Graphics Options");
		if (page)
		{
			Check_Object(page);
			page->GetEntry("NoBlend", &Mech::DontLoadBlends);
		}
	}

	if (startup_ini)
	{
		Check_Object(startup_ini);
		Page *page = startup_ini->FindPage("Graphics Options");
		if (page)
		{
			Check_Object(page);
			page->GetEntry("MissionMusic", &ABL::g_MissionMusic);
		}
	}


#if !defined(NO_STATS)
	StatisticFormat("");
	StatisticFormat("Mission Entity Counts");
	StatisticFormat("=====================");
	StatisticFormat("");

	AddStatistic("Executed Buildings", "buildings", gos_DWORD, &Executed_Building_Count, Stat_AutoReset);
	AddStatistic("Executed Vehicles", "vehicles", gos_DWORD, &Executed_Vehicle_Count, Stat_AutoReset);
	AddStatistic("  Executed Mechs", "vehicles", gos_DWORD, &Executed_Mech_Count, Stat_AutoReset);
	AddStatistic("  Executed Tanks", "vehicles", gos_DWORD, &Executed_Tank_Count, Stat_AutoReset);
	AddStatistic("  Executed Trucks", "vehicles", gos_DWORD, &Executed_Truck_Count, Stat_AutoReset);
	AddStatistic("  Executed Boats", "vehicles", gos_DWORD, &Executed_Boat_Count, Stat_AutoReset);
	AddStatistic("  Executed MFBs", "vehicles", gos_DWORD, &Executed_MFB_Count, Stat_AutoReset);
	AddStatistic("  Executed Hovercraft", "vehicles", gos_DWORD, &Executed_Hovercraft_Count, Stat_AutoReset);
	AddStatistic("  Executed Airplanes", "vehicles", gos_DWORD, &Executed_Airplane_Count, Stat_AutoReset);
	AddStatistic("Executed WeaponMovers", "movers", gos_DWORD, &Executed_WeaponMover_Count, Stat_AutoReset);
	AddStatistic("  Executed Missiles", "missiles", gos_DWORD, &Executed_Missile_Count, Stat_AutoReset);
	AddStatistic("Executed Beams", "movers", gos_DWORD, &Executed_Beam_Count, Stat_AutoReset);
	AddStatistic("Executed NonComs", "NonComs", gos_DWORD, &Executed_NonCom_Count, Stat_AutoReset);
	AddStatistic("Executed AIs", "AIs", gos_DWORD, &Executed_AI_Count, Stat_AutoReset);

	StatisticFormat("");
	StatisticFormat("Path Statistics");
	StatisticFormat("=====================");
	StatisticFormat("");

	AddStatistic("Rail Calcs", "Paths", gos_DWORD, &Path_Calcs_Count, Stat_AutoReset);
	AddStatistic("   QuickRail Calcs", "Paths", gos_DWORD, &QuickRail_Calcs_Count, Stat_AutoReset);
	AddStatistic("GridPath Calcs", "Paths", gos_DWORD, &GridPath_Calcs_Count, Stat_AutoReset);
	AddStatistic("   QuickPath Calcs", "Paths", gos_DWORD, &QuickPath_Calcs_Count, Stat_AutoReset);
	AddStatistic("Failed Path Calcs", "Paths", gos_DWORD, &FailedPath_Calcs_Count, Stat_AutoReset);
	AddStatistic("Obstacle Size", "Paths", gos_DWORD, &MW4AI::Obstacle_Size, 0);
#endif

	gos_PopCurrentHeap();
	
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	MechWarrior4::TerminateClasses(Stuff::NotationFile *startup_ini)
{

	if (startup_ini)
	{
		Check_Object(startup_ini);
		Page *page = startup_ini->FindPage("Network Options");

		if (!page)
		{
			page = startup_ini->AddPage("Network Options");
		}
		
		Check_Object(page);
		
		switch(MWApplication::MyConnectionType)
		{
			case MWApplication::Modem:
				page->SetEntry("ConnectionType", "modem");
				break;

			case MWApplication::ISDN:
				page->SetEntry("ConnectionType", "isdn");
				break;

			case MWApplication::xDSL:
				page->SetEntry("ConnectionType", "xdsl");
				break;

			case MWApplication::Cable:
				page->SetEntry("ConnectionType", "cable");
				break;

			case MWApplication::LAN:
				page->SetEntry("ConnectionType", "lan");
				break;
		}

		switch(MWApplication::MyConnectionSpeed)
		{
			case MWApplication::c_14_4kbps:
				page->SetEntry("ConnectionSpeed", "14");
				break;
			case MWApplication::c_28_8kbps:
				page->SetEntry("ConnectionSpeed", "28");
				break;
			case MWApplication::c_56_6kbps:
				page->SetEntry("ConnectionSpeed", "56");
				break;
			case MWApplication::c_64_kbps:
				page->SetEntry("ConnectionSpeed", "64");
				break;
			case MWApplication::c_128_kbps:
				page->SetEntry("ConnectionSpeed", "128");
				break;
			case MWApplication::c_384_kbps:
				page->SetEntry("ConnectionSpeed", "384");
				break;
			case MWApplication::c_768_kbps:
				page->SetEntry("ConnectionSpeed", "768");
				break;
			case MWApplication::c_1500_kbps:
				page->SetEntry("ConnectionSpeed", "1500");
				break;
			case MWApplication::c_10_Mbps:
				page->SetEntry("ConnectionSpeed", "10000");
				break;
			case MWApplication::c_100_Mbps:
				page->SetEntry("ConnectionSpeed", "100000");
				break;
			
		}
		
		page->SetEntry("PacketSize", MWApplication::DefaultPacketSize);
	}

	if (startup_ini)
	{
		Check_Object(startup_ini);
		Page *page = startup_ini->SetPage("Graphics Options");
		Check_Object(page);

		page->SetEntry("NoBlend", Mech::DontLoadBlends);
	}


	// terminate classes
//	GUILightAmp::TerminateClass();
	Flag::TerminateClass();
	NavPoint::TerminateClass();
	Team::TerminateClass();
	LBXWeaponSub::TerminateClass();
	ObservationVehicle::TerminateClass();
	MWCampaign::TerminateClass();
	WaterCultural::TerminateClass();
	LBXMover::TerminateClass();

// MSL 5.02 SubSystems
//	ArrowWeaponSubsystem::TerminateClass();
	AdvancedGyro::TerminateClass();
	IFF_Jammer::TerminateClass();
//	EnhancedOptics::TerminateClass();

// MSL 5.03 RTX
//	RTXWeaponSub::TerminateClass();
//	RTXWeaponSub::ExecutionStateEngine::TerminateClass();

	SalvageManager::TerminateClass();
	FieldBase::TerminateClass();
	FieldBase::ExecutionStateEngine::TerminateClass();
	MFB::TerminateClass();
	MFB::ExecutionStateEngine::TerminateClass();
	EyePointManager::TerminateClass();
	Gyro::TerminateClass();
	Armor::TerminateClass();
	Boat::TerminateClass();
	Truck::TerminateClass();
	Tank::TerminateClass();
	Hovercraft::TerminateClass();
	JumpJet::TerminateClass();
	JumpJet::ExecutionStateEngine::TerminateClass();
	HighExplosiveWeapon::TerminateClass();
	Explosive::TerminateClass();
	SearchLight::TerminateClass();
	LightEntity::TerminateClass();
	DeathEntity::TerminateClass();
	BombastWeapon::TerminateClass();
	BombastWeapon::ExecutionStateEngine::TerminateClass();
	ArtilleryWeapon::TerminateClass();
	ArtilleryMark::TerminateClass();
	FlamerWeapon::TerminateClass();
	FlameMover::TerminateClass();
	AMS::TerminateClass();
	LongTomWeaponSubsystem::TerminateClass();
	HeatSink::TerminateClass();
	HeatManager::TerminateClass();
	CameraShip::TerminateClass();
	CameraShip::ExecutionStateEngine::TerminateClass();
	VOEntity::TerminateClass();
	EffectGenerator::TerminateClass();
	BombWeaponSubsystem::TerminateClass();
	Dropship::TerminateClass();
	Dropship::ExecutionStateEngine::TerminateClass();
	Helicopter::TerminateClass();
	PlaneAI::TerminateClass();
	Airplane::TerminateClass();
	Airplane::ExecutionStateEngine::TerminateClass();
	FlareWeapon::TerminateClass();
	MWInternalDamageObject::TerminateClass();
	Objective::TerminateClass();
	Objective::ExecutionStateEngine::TerminateClass();
	Path::TerminateClass();
	ECM::TerminateClass();
	Beagle::TerminateClass();
	Sensor::TerminateClass();
	Sensor::ExecutionStateEngine::TerminateClass();
	NarcWeapon::TerminateClass();
	Narc::TerminateClass();
	StickyMover::TerminateClass();
	StickyMover::ExecutionStateEngine::TerminateClass();
	MWVideoRenderer::TerminateClass();
	MWEntityManager::TerminateClass();
	MWApplication::TerminateClass();
	GUIWeapon::TerminateClass();
	PlayerAI::TerminateClass ();
	PlayerAI::ExecutionStateEngine::TerminateClass();
	ShooterAI::TerminateClass ();
	MechAI::TerminateClass ();
	CombatAI::TerminateClass();
	NonComAI::TerminateClass ();
	MoverAI::TerminateClass ();
	MW4AI::CPathManager::TerminateClass ();
	MW4AI::CRailGraph::TerminateClass ();
	AI::TerminateClass();
	LancemateManager::TerminateClass();
	
	SSRMWeaponSubsystem::TerminateClass();
	MRMWeaponSubsystem::TerminateClass();
	LRMWeaponSubsystem::TerminateClass();
	SRMWeaponSubsystem::TerminateClass();
	MissileWeapon::TerminateClass();
	Missile::TerminateClass();
	Missile::ExecutionStateEngine::TerminateClass();
	Turret::TerminateClass();
	ProjectileWeapon::TerminateClass();
	ProjectileWeapon::ExecutionStateEngine::TerminateClass();
	Bridge::TerminateClass();
	Building::TerminateClass();
	NonCom::TerminateClass();
	BeamEntity::TerminateClass();
	Torso::TerminateClass();
	Decal::TerminateClass();
	BeamWeapon::TerminateClass();
	BeamWeapon::ExecutionStateEngine::TerminateClass();
	WeaponMover::TerminateClass();
	Weapon::TerminateClass();
	Engine::TerminateClass();
	Mech::TerminateClass();
	Mech::ExecutionStateEngine::TerminateClass();
	Subsystem::TerminateClass();
	Subsystem::ExecutionStateEngine::TerminateClass();
	MWPlayer::TerminateClass();
	VehicleInterface::TerminateClass();
	VehicleInterface::ExecutionStateEngine::TerminateClass();
	Vehicle::ExecutionStateEngine::TerminateClass();
	Vehicle::TerminateClass();
	Cultural::TerminateClass ();
	MWObject::TerminateClass();
	MWMover::ExecutionStateEngine::TerminateClass();
	MWMover::TerminateClass();
	MWMission::TerminateClass();
	AirplaneAnimationStateEngine::TerminateClass();
	MechAnimationStateEngine::TerminateClass();
	AnimationStateEngine::TerminateClass();
	MW4Animation::AnimHierarchyNode::TerminateClass();
	MW4Animation::BlendBuffer::TerminateClass();
	MW4Animation::AnimInstanceManager::TerminateClass();
	MWMap::TerminateClass();
	HUDComponent::TerminateClass ();

	KillComFuncs ();

	Check_Pointer(Heap);
	gos_DestroyMemoryHeap(Heap);
	Heap = NULL;

	Check_Pointer(g_LibraryHeap);
	gos_DestroyMemoryHeap(g_LibraryHeap);
	g_LibraryHeap = NULL;

// hyun begin
    g_pfnRIO_ButtonEvent = NULL;
    g_pfnRIO_Joy = NULL;
// hyun end
}
