#include "gen_bot_header.h"

LINK_ENTITY_TO_CLASS( info_bot, CBotEnt );
LINK_ENTITY_TO_CLASS( info_player_sp, CInfoPlayerSP );
LINK_ENTITY_TO_CLASS( info_bot_teacher, CInfoBotTeacher );
//harSens: Linux compile fix
extern CLeftOutput lcout;
// ==================================================================
// Code of CBotEnt - Begin
// ==================================================================

void CBotEnt::Spawn()
{
	pev->classname = MAKE_STRING("info_bot");
	pev->solid = SOLID_NOT;
	pev->nextthink = gpGlobals->time + 0.2f;
	//m_fSpawnTime = 0.0f;
	m_fSpawnTime = 10.0f;	//10 seconds until spawn
	m_bBotInGame = false;
	m_szDeathTrigger[0] = 0;
}

void CBotEnt::KeyValue(KeyValueData *pkvd)
{
	if (FStrEq(pkvd->szKeyName, "botteam")) 
	{
		m_bTeam = atoi(pkvd->szValue) + 1;
		pkvd->fHandled = TRUE;
	}
	else if (FStrEq(pkvd->szKeyName, "botclass")) 
	{
		m_sBotClass = atoi(pkvd->szValue) + 1;
		pkvd->fHandled = TRUE;
	}
	else if (FStrEq(pkvd->szKeyName, "startpl")) 
	{
		m_iStartPL = atoi(pkvd->szValue);
		pkvd->fHandled = TRUE;
	}
	else if (FStrEq(pkvd->szKeyName, "maxhealth")) 
	{
		m_iMaxHealth = atoi(pkvd->szValue);
		pkvd->fHandled = TRUE;
	}
	else if (FStrEq(pkvd->szKeyName, "maxspeed")) 
	{
		m_iMaxSpeed = atoi(pkvd->szValue);
		pkvd->fHandled = TRUE;
	}
	else if (FStrEq(pkvd->szKeyName, "persofile")) 
	{
		if (pkvd->szValue && (*pkvd->szValue))
			strcpy(m_szPersoFileName, pkvd->szValue);
		else
			m_szPersoFileName[0] = 0;
		pkvd->fHandled = TRUE;
	}
	else if (FStrEq(pkvd->szKeyName, "botlifes")) 
	{
		m_iLives = atoi(pkvd->szValue);
		if (m_iLives < 1)
			m_iLives = -1;

		pkvd->fHandled = TRUE;
	}
	else if (FStrEq(pkvd->szKeyName, "extraplgain")) 
	{
		m_iExtraPLGain = atoi(pkvd->szValue);
		pkvd->fHandled = TRUE;
	}
	else if (FStrEq(pkvd->szKeyName, "shouldchat")) 
	{
		m_bBotChat = (atoi(pkvd->szValue) != 0);
		pkvd->fHandled = TRUE;
	}
	else if (FStrEq(pkvd->szKeyName, "chatfile")) 
	{
		if (pkvd->szValue && (*pkvd->szValue))
			strcpy(m_szBotChatFileName, pkvd->szValue);
		else
			m_szBotChatFileName[0] = 0;

		pkvd->fHandled = TRUE;
	}
	else if (FStrEq(pkvd->szKeyName, "botname"))
	{
		if (pkvd->szValue && (*pkvd->szValue))
			strcpy(m_szBotName, pkvd->szValue);
		else
			m_szBotName[0] = 0;

		pkvd->fHandled = TRUE;
	}
	else if (FStrEq(pkvd->szKeyName, "skill"))
	{
		m_bBotSkill = atoi(pkvd->szValue) + 1;
		pkvd->fHandled = TRUE;
	}
}

void CBotEnt::Use(CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value)
{
	cout << "BotEnt \"" << STRING(pev->targetname) << "\" is triggered\n";

	if (!FStringNull(pev->target))
	{
		strcpy(m_szDeathTrigger, STRING(pev->target));
		cout << "BotEnt \"" << STRING(pev->targetname) << "\" has a targetname: " <<
				m_szDeathTrigger << "\n";
	}

	pev->target = iStringNull;

	m_fSpawnTime = gpGlobals->time + 0.2f;
}

void CBotEnt::Think()
{
	if ((m_fSpawnTime < 0.0f) && (m_fSpawnTime < gpGlobals->time) && !m_bBotInGame)
	{
		// Spawn the bot
		CSPBot::CreateMe(m_bTeam, m_sBotClass, m_iStartPL, int(m_eStartingForm), m_iMaxHealth,
						 m_iMaxSpeed, m_szPersoFileName, m_iLives, m_iExtraPLGain, m_bBotChat,
						 m_szBotChatFileName, m_szDeathTrigger, m_szBotName, m_bBotSkill, this);

		m_fSpawnTime = 0.0f;
		m_bBotInGame = true;
	}

	if(m_fSpawnTime >= 0.0f)
		m_fSpawnTime -= 0.2f;

	pev->nextthink = gpGlobals->time + 0.2f;
}

// ==================================================================
// Code of CBotEnt - End
// ==================================================================

// ==================================================================
// Code of CInfoPlayerSP - Begin
// ==================================================================

void CInfoPlayerSP::KeyValue(KeyValueData *pkvd)
{
	if (FStrEq(pkvd->szKeyName, "playerclass"))
	{
		m_sPlayerClass = atoi(pkvd->szValue);
		pkvd->fHandled = TRUE;
	}
	else if (FStrEq(pkvd->szKeyName, "startpl"))
	{
		m_iStartPL = atoi(pkvd->szValue);
		pkvd->fHandled = TRUE;
	}
	else if (FStrEq(pkvd->szKeyName, "maxhealth"))
	{
		m_sStartHealth = atoi(pkvd->szValue);
		pkvd->fHandled = TRUE;
	}
}

void CInfoPlayerSP::SelectClass(CBasePlayer *pPlayer)
{
	if (!pPlayer->m_pClass || (pPlayer->m_pClass->Classify() != m_sPlayerClass) ||
		pPlayer->IsSpectator() || pPlayer->m_fObserverFlag)
	{
		switch(m_sPlayerClass)
		{
		case PC_BUU:
			pPlayer->ChangeClass("buu");
			break;
		case PC_GOKU:
		default:
			pPlayer->ChangeClass("goku");
			break;
		case PC_GOHAN:
			pPlayer->ChangeClass("gohan");
			break;
		case PC_KRILLIN:
			pPlayer->ChangeClass("krillin");
			break;
		case PC_FRIEZA:
			pPlayer->ChangeClass("frieza");
			break;
		case PC_PICCOLO:
			pPlayer->ChangeClass("piccolo");
			break;
		case PC_TRUNKS:
			pPlayer->ChangeClass("trunks");
			break;
		case PC_VEGETA:
			pPlayer->ChangeClass("vegeta");
			break;
		case PC_CELL:
			pPlayer->ChangeClass("cell");
			break;
		}
	}
}

void CInfoPlayerSP::GivePlayerStats(CBasePlayer *pPlayer)
{
	if (m_iStartPL >= 1)
	{
		pPlayer->SetMaxPowerLevel(m_iStartPL);
		pPlayer->SetPowerLevel(m_iStartPL);
	}
	else
	{
		pPlayer->SetPowerLevel(pPlayer->m_pClass->GetStartPowerLevel()); //UNDONE
	}

	if (m_sStartHealth >= 1)
	{
		pPlayer->m_iMaxHealth = m_sStartHealth;
		pPlayer->SetMaxHealth(m_sStartHealth);
		pPlayer->pev->health = m_sStartHealth;
	}
	else
	{
		int	iMaxHealth = pPlayer->m_pClass->GetStartHealth();
		pPlayer->m_iMaxHealth = iMaxHealth;
		pPlayer->SetMaxHealth(iMaxHealth);
		pPlayer->pev->health = iMaxHealth;
	}

	pPlayer->pev->fuser4 = 1000;

	if (pPlayer->m_pClass)
		pPlayer->m_iMaxSpeed = pPlayer->m_pClass->GetStartSpeed();

	if (!FStringNull(pev->target))
	{
		CBaseEntity *pTarget = UTIL_FindEntityByTargetname(NULL, STRING(pev->target));
		if (pTarget)
			pTarget->Use(this, this, USE_TOGGLE, 0.0f);
	}
}

void CInfoPlayerSP::Spawn()
{
	pev->classname = MAKE_STRING("info_player_sp");
	pev->solid = SOLID_NOT;
}

// ==================================================================
// Code of CInfoPlayerSP - End
// ==================================================================

// ==================================================================
// Code of CInfoBotTeacher - Begin
// ==================================================================

void CInfoBotTeacher::Spawn()
{
	pev->classname = MAKE_STRING("info_player_sp");
	pev->solid = SOLID_NOT;

	m_eBotTeacherType = BOTTEACHER_ROSHI;
	m_szScriptFile[0] = m_szEndTrigger[0] = 0;
	m_fSpawnTime = 0.0f;
}

void CInfoBotTeacher::KeyValue(KeyValueData *pkvd)
{
	if (FStrEq(pkvd->szKeyName, "type"))
	{
		if (pkvd->szValue && *pkvd->szValue)		
			m_eBotTeacherType = EBotTeacherType(atoi(pkvd->szValue));
	}

	if (FStrEq(pkvd->szKeyName, "scriptfile"))
	{
		if (pkvd->szValue && *pkvd->szValue)
			strcpy(m_szScriptFile, pkvd->szValue);
	}
}

void CInfoBotTeacher::Use(CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value)
{
	cout << "BotTeacher \"" << STRING(pev->targetname) << "\" is triggered\n";

	if (!FStringNull(pev->target))
	{
		strcpy(m_szEndTrigger, STRING(pev->target));
		cout << "BotEnt \"" << STRING(pev->targetname) << "\" has a targetname: " <<
				m_szEndTrigger << "\n";
	}

	pev->target = iStringNull;

	m_fSpawnTime = gpGlobals->time + 0.2f;
}

void CInfoBotTeacher::Think()
{
	if ((m_fSpawnTime > 0.0f) && (m_fSpawnTime < gpGlobals->time))
	{
		// Spawn the bot
		CBotTeacher::CreateMe(m_eBotTeacherType, m_szScriptFile, m_szEndTrigger, this);
		m_fSpawnTime = 0.0f;
	}

	pev->nextthink = gpGlobals->time + 0.2f;
}

// ==================================================================
// Code of CInfoBotTeacher - End
// ==================================================================
