2018-09-22 23:54:36 +02:00
|
|
|
#include "GameLogicScript.h"
|
|
|
|
#include "..\Game\items.h"
|
|
|
|
#include "..\Game\box.h"
|
|
|
|
#include "..\Game\lot.h"
|
|
|
|
#include "..\Game\sound.h"
|
|
|
|
|
|
|
|
GameScript::GameScript(sol::state* lua)
|
|
|
|
{
|
|
|
|
m_lua = lua;
|
|
|
|
|
|
|
|
// Gameflow type
|
|
|
|
m_lua->new_usertype<GameScript>("GameScript",
|
|
|
|
"EnableItem", &GameScript::EnableItem,
|
|
|
|
"DisableItem", &GameScript::DisableItem,
|
|
|
|
"PlayAudioTrack", &GameScript::PlayAudioTrack,
|
|
|
|
"ChangeAmbientSoundTrack", &GameScript::ChangeAmbientSoundTrack
|
|
|
|
);
|
|
|
|
|
|
|
|
(*m_lua)["TR"] = this;
|
2018-09-23 12:01:07 +02:00
|
|
|
|
|
|
|
// DEBUG: just for testing
|
|
|
|
LuaFunction* function = new LuaFunction();
|
|
|
|
function->Name = "Trigger_0";
|
|
|
|
function->Code = "function Trigger_0() \n TR:EnableItem(2); \n TR:PlayAudioTrack(15); \n return true; \n end";
|
|
|
|
m_lua->script(function->Code);
|
|
|
|
Triggers.push_back(function);
|
|
|
|
|
|
|
|
m_itemsMap.insert(pair<__int16, __int16>(2, 8));
|
2018-09-22 23:54:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
GameScript::~GameScript()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
string GameScript::loadScriptFromFile(char* luaFilename)
|
|
|
|
{
|
|
|
|
ifstream ifs(luaFilename, ios::in | ios::binary | ios::ate);
|
|
|
|
|
|
|
|
ifstream::pos_type fileSize = ifs.tellg();
|
|
|
|
ifs.seekg(0, ios::beg);
|
|
|
|
|
|
|
|
vector<char> bytes(fileSize);
|
|
|
|
ifs.read(bytes.data(), fileSize);
|
|
|
|
|
|
|
|
return string(bytes.data(), fileSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GameScript::ExecuteScript(char* luaFilename)
|
|
|
|
{
|
|
|
|
string script = loadScriptFromFile(luaFilename);
|
|
|
|
m_lua->script(script);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-23 12:01:07 +02:00
|
|
|
bool GameScript::ExecuteTrigger(__int16 index)
|
|
|
|
{
|
|
|
|
// Is this a valid trigger?
|
|
|
|
if (index >= Triggers.size())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
LuaFunction* trigger = Triggers[index];
|
|
|
|
|
|
|
|
// We want to execute a trigger just one time
|
|
|
|
// TODO: implement in the future continoous trigger?
|
|
|
|
if (trigger->Executed)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Get the trigger function name
|
|
|
|
char* name = (char*)trigger->Name.c_str();
|
|
|
|
|
|
|
|
// Execute trigger
|
|
|
|
bool result = (*m_lua)[name]();
|
|
|
|
|
|
|
|
// Trigger was executed, don't execute it anymore
|
|
|
|
Triggers[index]->Executed = result;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-09-22 23:54:36 +02:00
|
|
|
void GameScript::EnableItem(__int16 id)
|
|
|
|
{
|
|
|
|
if (m_itemsMap.find(id) == m_itemsMap.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
__int16 itemNum = m_itemsMap[id];
|
|
|
|
|
|
|
|
ITEM_INFO* item = &Items[itemNum];
|
|
|
|
|
|
|
|
if (!item->active)
|
|
|
|
{
|
|
|
|
if (Objects[item->objectNumber].intelligent)
|
|
|
|
{
|
|
|
|
if (item->status == ITEM_DEACTIVATED)
|
|
|
|
{
|
|
|
|
item->touchBits = 0;
|
|
|
|
item->status = ITEM_ACTIVE;
|
|
|
|
AddActiveItem(itemNum);
|
|
|
|
EnableBaddieAI(itemNum, 1);
|
|
|
|
}
|
|
|
|
else if (item->status == ITEM_INVISIBLE)
|
|
|
|
{
|
|
|
|
item->touchBits = 0;
|
|
|
|
if (EnableBaddieAI(itemNum, 0))
|
|
|
|
item->status = ITEM_ACTIVE;
|
|
|
|
else
|
|
|
|
item->status = ITEM_INVISIBLE;
|
|
|
|
AddActiveItem(itemNum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
item->touchBits = 0;
|
|
|
|
AddActiveItem(itemNum);
|
|
|
|
item->status = ITEM_ACTIVE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameScript::DisableItem(__int16 id)
|
|
|
|
{
|
|
|
|
if (m_itemsMap.find(id) == m_itemsMap.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
__int16 itemNum = m_itemsMap[id];
|
|
|
|
|
|
|
|
ITEM_INFO* item = &Items[itemNum];
|
|
|
|
|
|
|
|
if (item->active)
|
|
|
|
{
|
|
|
|
if (Objects[item->objectNumber].intelligent)
|
|
|
|
{
|
|
|
|
if (item->status == ITEM_ACTIVE)
|
|
|
|
{
|
|
|
|
item->touchBits = 0;
|
|
|
|
item->status = ITEM_DEACTIVATED;
|
|
|
|
RemoveActiveItem(itemNum);
|
|
|
|
DisableBaddieAI(itemNum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
item->touchBits = 0;
|
|
|
|
RemoveActiveItem(itemNum);
|
|
|
|
item->status = ITEM_DEACTIVATED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameScript::PlayAudioTrack(__int16 track)
|
|
|
|
{
|
|
|
|
S_CDPlay(track, SOUND_TRACK_ONESHOT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameScript::ChangeAmbientSoundTrack(__int16 track)
|
|
|
|
{
|
|
|
|
CurrentAtmosphere = track;
|
|
|
|
S_CDStop();
|
|
|
|
S_CDPlay(track, SOUND_TRACK_BGM);
|
|
|
|
}
|
|
|
|
|
|
|
|
GameScript* g_GameScript;
|