//=============================================================================
// Anniversary event script
// Author: GeckoN
//=============================================================================

#include "point_cake_slice"

int g_nItemCnt;

const string TARGET_PREFIX = "_Anniv_Item_";
const int TARGET_PREFIX_LEN = 12;

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void PluginInit()
{
	g_Module.ScriptInfo.SetAuthor( "Sven Co-op Development Team" );
	g_Module.ScriptInfo.SetContactInfo( "www.svencoop.com" );
	
	g_Hooks.RegisterHook( Hooks::PickupObject::Materialize, @PickupObjectMaterialize );
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
HookReturnCode PickupObjectMaterialize( CBaseEntity@ pEntity )
{
	if ( ( pEntity.pev.spawnflags & SF_CREATEDWEAPON ) != 0 )
		return HOOK_CONTINUE;
	
	string strTarget = pEntity.pev.target;
	if ( strTarget.IsEmpty() || strTarget.CompareN( TARGET_PREFIX, TARGET_PREFIX_LEN ) != 0 )
		return HOOK_CONTINUE;
	
	CBaseEntity@ pGift = g_EntityFuncs.FindEntityByTargetname( null, strTarget );
	if ( pGift is null )
		return HOOK_CONTINUE;
	
	pGift.pev.effects &= ~EF_NODRAW;
	g_EntityFuncs.SetModel( pEntity, "" );
	return HOOK_HANDLED;
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void MapInit()
{
	g_nItemCnt = 0;
	
	PointCakeSlice::Register();
	PointCakeSlice::PrecacheGlobal();
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void MapActivate()
{
	g_ReplacementMgr.ReplaceAll();
}

//=============================================================================
// Entity Replacement Manager
//=============================================================================
class CEntityReplacementManager
{
	void ReplaceAll()
	{
		Replace( "item_healthkit", PointCakeSlice::ENTITY_NAME );
	}
	
	void Replace( const string& in strClassname, const string& in strReplacement )
	{
		CBaseEntity@ ent = null;
		int nCount = 0;
		
		while( ( @ent = g_EntityFuncs.FindEntityByClassname( ent, strClassname ) ) !is null )
		{
			if ( Apply( ent, strReplacement ) )
				nCount++;
		}
		
		g_Game.AlertMessage( at_console, "Replaced %1 items\n", nCount );
	}
	
	bool Apply( CBaseEntity@ ent, const string& in strReplacement )
	{
		string strName = TARGET_PREFIX + g_nItemCnt;
		
		CBaseEntity@ pEntity = g_EntityFuncs.CreateEntity( strReplacement, null, false );
		if ( pEntity is null )
			return false;
		
		// Some basic properties
		pEntity.pev.targetname = strName;
		pEntity.pev.maxs = ent.pev.maxs;
		pEntity.pev.mins = ent.pev.mins;
		pEntity.pev.origin = ent.pev.origin;
		pEntity.pev.angles = ent.pev.angles;
		// Pass on the target
		pEntity.pev.target = ent.pev.target;
		// Scale, relative to the original entity
		pEntity.pev.scale = ent.pev.scale * 1.3;
		// Spawn!
		g_EntityFuncs.DispatchSpawn( pEntity.edict() );

		// Hide the original entity and link it to the item
		
		ent.pev.target = strName;
		g_EntityFuncs.SetModel( ent, "" );
		
		g_nItemCnt++;
		return true;
	}
};

CEntityReplacementManager g_ReplacementMgr;
