/***************************************************************************
 *
 * PROJECT: The Dark Mod
 * $Revision: 4519 $
 * $Date: 2011-02-01 10:44:51 +0100 (Di, 01 Feb 2011) $
 * $Author: greebo $
 *
 ***************************************************************************/
#include "../idlib/precompiled.h"
#pragma hdrstop

static bool init_version = FileVersionList("$Id: Response.cpp 4519 2011-02-01 09:44:51Z greebo $", init_version);

#include "Response.h"
#include "Stim.h"

#include <algorithm>

/********************************************************************/
/*                   CResponse                                      */
/********************************************************************/
CResponse::CResponse(idEntity* owner, StimType type, int uniqueId) : 
	CStimResponse(owner, type, uniqueId)
{
	m_ScriptFunction = NULL;
	m_MinDamage = 0.0f;
	m_MaxDamage = 0;
	m_NumRandomEffects = 0;
}

CResponse::~CResponse()
{
	// Remove all the allocated response effects from the heap
	m_ResponseEffects.DeleteContents(true);
}

void CResponse::Save(idSaveGame *savefile) const
{
	CStimResponse::Save(savefile);

	savefile->WriteString(m_ScriptFunction.c_str());
	savefile->WriteFloat(m_MinDamage);
	savefile->WriteFloat(m_MaxDamage);
	savefile->WriteInt(m_NumRandomEffects);

	savefile->WriteInt(m_ResponseEffects.Num());
	for (int i = 0; i < m_ResponseEffects.Num(); i++)
	{
		m_ResponseEffects[i]->Save(savefile);
	}
}

void CResponse::Restore(idRestoreGame *savefile)
{
	CStimResponse::Restore(savefile);

	savefile->ReadString(m_ScriptFunction);
	savefile->ReadFloat(m_MinDamage);
	savefile->ReadFloat(m_MaxDamage);
	savefile->ReadInt(m_NumRandomEffects);

	int num;
	savefile->ReadInt(num);
	m_ResponseEffects.SetNum(num);
	for (int i = 0; i < num; i++)
	{
		m_ResponseEffects[i] = new CResponseEffect(NULL, "", "", false);
		m_ResponseEffects[i]->Restore(savefile);
	}
}

void CResponse::TriggerResponse(idEntity *sourceEntity, const CStimPtr& stim)
{
	DM_LOG(LC_STIM_RESPONSE, LT_DEBUG)LOGSTRING("Response for Id %s triggered (Action: %s)\r", m_StimTypeName.c_str(), m_ScriptFunction.c_str());

	// Perform the probability check
	if (!CheckChance()) return;

	idEntity* owner = m_Owner.GetEntity();

	// Notify the owner entity
	owner->OnStim(stim, sourceEntity);

	const function_t* func = owner->scriptObject.GetFunction(m_ScriptFunction.c_str());
	if (func == NULL)
	{
		DM_LOG(LC_STIM_RESPONSE, LT_DEBUG)LOGSTRING("Action: %s not found in local space, checking for global.\r", m_ScriptFunction.c_str());
		func = gameLocal.program.FindFunction(m_ScriptFunction.c_str());
	}

	if (func != NULL)
	{
		DM_LOG(LC_STIM_RESPONSE, LT_DEBUG)LOGSTRING("Running ResponseScript\r");
		idThread *pThread = new idThread(func);
		int n = pThread->GetThreadNum();
		pThread->CallFunctionArgs(func, true, "eef", owner, sourceEntity, n);
		pThread->DelayedStart(0);
	}
	else
	{
		DM_LOG(LC_STIM_RESPONSE, LT_ERROR)LOGSTRING("ResponseActionScript not found! [%s]\r", m_ScriptFunction.c_str());
	}

	// Default magnitude (in case we have a NULL stim)
	float magnitude = 10;

	if (stim != NULL)
	{
		// We have a "real" stim causing this response, retrieve the properties

		// Calculate the magnitude of the stim based on the distance and the falloff model
		magnitude = stim->m_Magnitude;
		float distance = (owner->GetPhysics()->GetOrigin() - sourceEntity->GetPhysics()->GetOrigin()).LengthFast();
		
		using std::min;
		// greebo: Be sure to use GetRadius() to consider time-dependent radii
		float radius = stim->GetRadius();
		float base = 1 - min(radius, distance) / radius;
		
		// Calculate the falloff value (the magnitude is between [0, magnitude] for positive falloff exponents)
		magnitude *= pow(base, stim->m_FallOffExponent);
	}
	
	//DM_LOG(LC_STIM_RESPONSE, LT_ERROR)LOGSTRING("Available Response Effects: %u\r", m_ResponseEffects.Num());
	if (m_NumRandomEffects > 0) {
		// Random effect mode, choose exactly m_NumRandomEffects to fire
		for (int i = 1; i <= m_NumRandomEffects; i++) {
			// Get a random effectIndex in the range of [0, m_ResponseEffects.Num()[
			int effectIndex = gameLocal.random.RandomInt(m_ResponseEffects.Num());
			m_ResponseEffects[effectIndex]->runScript(owner, sourceEntity, magnitude);
		}
	}
	else {
		DM_LOG(LC_STIM_RESPONSE, LT_DEBUG)LOGSTRING("Iterating through ResponseEffects: %d\r", m_ResponseEffects.Num());
		// "Normal" mode, all the effects get fired in order
		for (int i = 0; i < m_ResponseEffects.Num(); i++) {
			m_ResponseEffects[i]->runScript(owner, sourceEntity, magnitude);
		}
	}
}

void CResponse::SetResponseAction(idStr const &action)
{
	m_ScriptFunction = action;
}

CResponseEffect* CResponse::AddResponseEffect(const idStr& effectEntityDef, 
											  const idStr& effectPostfix, 
											  const idDict& args)
{
	CResponseEffect* returnValue = NULL;
	
	DM_LOG(LC_STIM_RESPONSE, LT_DEBUG)LOGSTRING("Seeking EffectEntity [%s]\r", effectEntityDef.c_str());
	// Try to locate the specified entity definition
	const idDict* dict = gameLocal.FindEntityDefDict(effectEntityDef.c_str());

	if (effectEntityDef == "effect_script")
	{
		// We have a script effect, this is a special case
		idStr key;
		sprintf(key, "sr_effect_%s_arg1", effectPostfix.c_str());
		
		// Get the script argument from the entity's spawnargs
		idStr scriptStr = args.GetString(key);

		if (scriptStr != "")
		{
			bool isLocalScript = true;

			const function_t* scriptFunc = m_Owner.GetEntity()->scriptObject.GetFunction(scriptStr.c_str());
			if (scriptFunc == NULL)
			{
				DM_LOG(LC_STIM_RESPONSE, LT_DEBUG)LOGSTRING("CResponse: %s not found in local space, checking for global.\r", scriptStr.c_str());
				scriptFunc = gameLocal.program.FindFunction(scriptStr.c_str());
				isLocalScript = false;
			}

			if (scriptFunc != NULL)
			{
				DM_LOG(LC_STIM_RESPONSE, LT_DEBUG)LOGSTRING("CResponse: %s SCRIPTFUNC.\r", scriptFunc->Name());
				// Allocate a new effect object
				CResponseEffect* newEffect = new CResponseEffect(scriptFunc, effectPostfix, scriptStr, isLocalScript);
				// Add the item to the list
				m_ResponseEffects.Append(newEffect);

				returnValue = newEffect;
			}
			else
			{
				gameLocal.Printf("Warning: Script not found: %s!\n", scriptStr.c_str());
			}
		}
		else {
			gameLocal.Printf("Warning: Script argument not found!\n");
		}
	}
	else if (dict != NULL)
	{
		idStr scriptStr = dict->GetString("script");

		const function_t* scriptFunc = gameLocal.program.FindFunction(scriptStr.c_str());
		if (scriptFunc != NULL)
		{
			// Allocate a new effect object
			CResponseEffect* newEffect = new CResponseEffect(scriptFunc, effectPostfix, scriptStr, false);
			
			// Add the item to the list
			m_ResponseEffects.Append(newEffect);

			returnValue = newEffect;
		}
		else {
			DM_LOG(LC_STIM_RESPONSE, LT_DEBUG)LOGSTRING("Warning: Script Function not found: [%s]\r", scriptStr.c_str());
		}
	}
	else
	{
		// Entity not found, emit a warning
		gameLocal.Printf("Warning: EffectEntityDef not found: %s.\r", effectEntityDef.c_str());
		DM_LOG(LC_STIM_RESPONSE, LT_DEBUG)LOGSTRING("Warning: EffectEntityDef not found: %s\r", effectEntityDef.c_str());
	}

	//DM_LOG(LC_STIM_RESPONSE, LT_DEBUG)LOGSTRING("Items in the list: %u\r", m_ResponseEffects.Num());
	
	return returnValue;
}
