2020-12-21 13:16:29 -03:00
|
|
|
#include "framework.h"
|
|
|
|
#include "GameLogicScript.h"
|
|
|
|
#include "items.h"
|
|
|
|
#include "box.h"
|
|
|
|
#include "lara.h"
|
|
|
|
#include "savegame.h"
|
|
|
|
#include "lot.h"
|
|
|
|
#include "sound.h"
|
|
|
|
#include "setup.h"
|
|
|
|
#include "level.h"
|
2021-06-29 05:28:17 +02:00
|
|
|
#include "tomb4fx.h"
|
|
|
|
#include "effect2.h"
|
|
|
|
#include "pickup.h"
|
2021-07-10 14:05:01 +01:00
|
|
|
#include "newinv2.h"
|
|
|
|
#include <iostream>
|
2021-07-13 13:21:13 +01:00
|
|
|
#include "InventorySlots.h"
|
2021-07-20 00:16:15 +01:00
|
|
|
#include "ObjectIDs.h"
|
2021-06-29 05:00:15 +02:00
|
|
|
|
2021-07-13 22:29:47 -05:00
|
|
|
#ifndef _DEBUG
|
|
|
|
#include <iostream>
|
|
|
|
#endif
|
2021-06-29 05:00:15 +02:00
|
|
|
|
2020-12-21 13:16:29 -03:00
|
|
|
extern GameFlow* g_GameFlow;
|
|
|
|
GameScript* g_GameScript;
|
2021-07-01 19:29:58 +01:00
|
|
|
extern bool const WarningsAsErrors = true;
|
2020-12-21 13:16:29 -03:00
|
|
|
|
2021-06-16 14:39:43 +01:00
|
|
|
GameScript::GameScript(sol::state* lua) : LuaHandler{ lua }
|
2020-12-21 13:16:29 -03:00
|
|
|
{
|
2021-07-04 14:33:57 +01:00
|
|
|
m_lua->set_function("SetAmbientTrack", &GameScript::SetAmbientTrack);
|
|
|
|
m_lua->set_function("PlayAudioTrack", &GameScript::PlayAudioTrack);
|
|
|
|
|
2021-07-10 14:05:01 +01:00
|
|
|
m_lua->set_function("GiveInvItem", &GameScript::InventoryAdd);
|
|
|
|
m_lua->set_function("TakeInvItem", &GameScript::InventoryRemove);
|
|
|
|
|
|
|
|
m_lua->set_function("GetInvItemCount", &GameScript::InventoryGetCount);
|
|
|
|
m_lua->set_function("SetInvItemCount", &GameScript::InventorySetCount);
|
2021-07-05 18:18:20 +01:00
|
|
|
|
2021-07-20 17:58:11 +01:00
|
|
|
m_lua->set_function("GetItemByName", &GameScript::GetItemByName, this);
|
|
|
|
m_lua->set_function("GetItemByID", &GameScript::GetItemById, this);
|
2021-07-20 00:16:15 +01:00
|
|
|
auto makeReadOnlyTable = [this](std::string const & tableName, auto const& container)
|
|
|
|
{
|
|
|
|
auto mt = tableName + "Meta";
|
|
|
|
// Put all the data in the metatable
|
|
|
|
m_lua->set(mt, sol::as_table(container));
|
|
|
|
|
|
|
|
// Make the metatable's __index refer to itself so that requests
|
|
|
|
// to the main table will go through to the metatable (and thus the
|
|
|
|
// container's members)
|
|
|
|
m_lua->safe_script(mt + ".__index = " + mt);
|
2021-07-13 13:21:13 +01:00
|
|
|
|
2021-07-20 00:16:15 +01:00
|
|
|
// Don't allow the table to have new elements put into it
|
|
|
|
m_lua->safe_script(mt + ".__newindex = function() error('" + tableName + " is read-only') end");
|
2021-07-13 13:21:13 +01:00
|
|
|
|
2021-07-20 00:16:15 +01:00
|
|
|
// Protect the metatable
|
|
|
|
m_lua->safe_script(mt + ".__metatable = 'metatable is protected'");
|
2021-07-13 13:21:13 +01:00
|
|
|
|
2021-07-20 00:16:15 +01:00
|
|
|
auto tab = m_lua->create_named_table(tableName);
|
|
|
|
m_lua->safe_script("setmetatable(" + tableName + ", " + mt + ")");
|
2021-07-13 13:21:13 +01:00
|
|
|
|
2021-07-20 00:16:15 +01:00
|
|
|
// point the initial metatable variable away from its contents. this is just for cleanliness
|
|
|
|
m_lua->safe_script(mt + "= nil");
|
|
|
|
};
|
2021-07-13 13:21:13 +01:00
|
|
|
|
2021-07-20 00:16:15 +01:00
|
|
|
makeReadOnlyTable("InvItem", kInventorySlots);
|
|
|
|
makeReadOnlyTable("ObjID", kObjIDs);
|
2021-07-13 13:21:13 +01:00
|
|
|
|
2021-06-28 18:35:16 +01:00
|
|
|
GameScriptItemInfo::Register(m_lua);
|
2021-07-21 18:12:17 +01:00
|
|
|
GameScriptItemInfo::SetNameCallbacks(
|
|
|
|
[this](std::string const& str, short num) { return AddLuaName(str, num); },
|
|
|
|
[this](std::string const& str) { return RemoveLuaName(str); }
|
|
|
|
);
|
|
|
|
|
|
|
|
GameScriptMeshInfo::Register(m_lua);
|
|
|
|
GameScriptMeshInfo::SetNameCallbacks(
|
|
|
|
[this](std::string const& str, MESH_INFO & info) { return AddLuaNameMesh(str, info); },
|
|
|
|
[this](std::string const& str) { return RemoveLuaNameMesh(str); }
|
|
|
|
);
|
2021-07-17 22:26:07 +01:00
|
|
|
|
2021-07-01 19:29:58 +01:00
|
|
|
GameScriptPosition::Register(m_lua);
|
|
|
|
GameScriptRotation::Register(m_lua);
|
2021-07-20 17:58:11 +01:00
|
|
|
GameScriptColor::Register(m_lua);
|
2021-07-01 19:29:58 +01:00
|
|
|
|
2020-12-21 13:16:29 -03:00
|
|
|
m_lua->new_enum<GAME_OBJECT_ID>("Object", {
|
|
|
|
{"LARA", ID_LARA}
|
|
|
|
});
|
|
|
|
|
|
|
|
m_lua->new_usertype<LuaVariables>("Variable",
|
|
|
|
sol::meta_function::index, &LuaVariables::GetVariable,
|
|
|
|
sol::meta_function::new_index, &LuaVariables::SetVariable,
|
|
|
|
"new", sol::no_constructor
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameScript::AddTrigger(LuaFunction* function)
|
|
|
|
{
|
|
|
|
m_triggers.push_back(function);
|
|
|
|
(*m_lua).script(function->Code);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameScript::AddLuaId(int luaId, short itemNumber)
|
|
|
|
{
|
2021-06-29 05:00:15 +02:00
|
|
|
m_itemsMapId.insert(std::pair<int, short>(luaId, itemNumber));
|
2020-12-21 13:16:29 -03:00
|
|
|
}
|
|
|
|
|
2021-07-17 22:26:07 +01:00
|
|
|
bool GameScript::RemoveLuaName(std::string const & luaName)
|
2020-12-21 13:16:29 -03:00
|
|
|
{
|
2021-07-17 22:26:07 +01:00
|
|
|
return m_itemsMapName.erase(luaName);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GameScript::AddLuaName(std::string const & luaName, short itemNumber)
|
|
|
|
{
|
|
|
|
return m_itemsMapName.insert(std::pair<std::string, short>(luaName, itemNumber)).second;
|
2020-12-21 13:16:29 -03:00
|
|
|
}
|
|
|
|
|
2021-07-21 18:12:17 +01:00
|
|
|
bool GameScript::RemoveLuaNameMesh(std::string const & luaName)
|
|
|
|
{
|
|
|
|
return m_meshesMapName.erase(luaName);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GameScript::AddLuaNameMesh(std::string const& luaName, MESH_INFO& infoRef)
|
|
|
|
{
|
|
|
|
return m_meshesMapName.insert(std::pair<std::string, MESH_INFO&>(luaName, infoRef)).second;
|
|
|
|
}
|
|
|
|
|
2020-12-21 13:16:29 -03:00
|
|
|
void GameScript::FreeLevelScripts()
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
// Delete all triggers
|
|
|
|
for (int i = 0; i < m_triggers.size(); i++)
|
|
|
|
{
|
|
|
|
LuaFunction* trigger = m_triggers[i];
|
|
|
|
char* name = (char*)trigger->Name.c_str();
|
|
|
|
(*m_lua)[name] = NULL;
|
|
|
|
delete m_triggers[i];
|
|
|
|
}
|
|
|
|
m_triggers.clear();
|
|
|
|
|
|
|
|
// Clear the items mapping
|
|
|
|
m_itemsMap.clear();
|
|
|
|
|
|
|
|
(*m_lua)["Lara"] = NULL;
|
|
|
|
//delete m_Lara;
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GameScript::ExecuteTrigger(short index)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
/*
|
|
|
|
// Is this a valid trigger?
|
|
|
|
if (index >= m_triggers.size())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
LuaFunction* trigger = m_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
|
|
|
|
trigger->Executed = result;
|
|
|
|
|
|
|
|
m_locals.for_each([&](sol::object const& key, sol::object const& value) {
|
|
|
|
if (value.is<bool>())
|
2021-06-29 05:00:15 +02:00
|
|
|
std::cout << key.as<std::string>() << " " << value.as<bool>() << std::endl;
|
|
|
|
else if (value.is<std::string>())
|
|
|
|
std::cout << key.as<std::string>() << " " << value.as<std::string>() << std::endl;
|
2020-12-21 13:16:29 -03:00
|
|
|
else
|
2021-06-29 05:00:15 +02:00
|
|
|
std::cout << key.as<std::string>() << " " << value.as<int>() << std::endl;
|
2020-12-21 13:16:29 -03:00
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameScript::JumpToLevel(int levelNum)
|
|
|
|
{
|
|
|
|
if (levelNum >= g_GameFlow->GetNumLevels())
|
|
|
|
return;
|
|
|
|
LevelComplete = levelNum;
|
|
|
|
}
|
|
|
|
|
|
|
|
int GameScript::GetSecretsCount()
|
|
|
|
{
|
|
|
|
return Savegame.Level.Secrets;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameScript::SetSecretsCount(int secretsNum)
|
|
|
|
{
|
|
|
|
if (secretsNum > 255)
|
|
|
|
return;
|
|
|
|
Savegame.Level.Secrets = secretsNum;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameScript::AddOneSecret()
|
|
|
|
{
|
|
|
|
if (Savegame.Level.Secrets >= 255)
|
|
|
|
return;
|
|
|
|
Savegame.Level.Secrets++;
|
2021-06-29 05:00:15 +02:00
|
|
|
S_CDPlay(TRACK_FOUND_SECRET, 0);
|
2020-12-21 13:16:29 -03:00
|
|
|
}
|
2021-06-29 05:00:15 +02:00
|
|
|
|
2020-12-21 13:16:29 -03:00
|
|
|
/*
|
|
|
|
void GameScript::MakeItemInvisible(short id)
|
|
|
|
{
|
|
|
|
if (m_itemsMap.find(id) == m_itemsMap.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
short itemNum = m_itemsMap[id];
|
|
|
|
|
|
|
|
ITEM_INFO* item = &g_Level.Items[itemNum];
|
|
|
|
|
|
|
|
if (item->active)
|
|
|
|
{
|
|
|
|
if (Objects[item->objectNumber].intelligent)
|
|
|
|
{
|
|
|
|
if (item->status == ITEM_ACTIVE)
|
|
|
|
{
|
|
|
|
item->touchBits = 0;
|
|
|
|
item->status = ITEM_INVISIBLE;
|
|
|
|
DisableBaddieAI(itemNum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
item->touchBits = 0;
|
|
|
|
item->status = ITEM_INVISIBLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2021-06-29 05:00:15 +02:00
|
|
|
void GameScript::GetVariables(std::map<std::string, T>& locals, std::map<std::string, T>& globals)
|
2020-12-21 13:16:29 -03:00
|
|
|
{
|
|
|
|
for (const auto& it : m_locals.variables)
|
|
|
|
{
|
|
|
|
if (it.second.is<T>())
|
2021-06-29 05:00:15 +02:00
|
|
|
locals.insert(std::pair<std::string, T>(it.first, it.second.as<T>()));
|
2020-12-21 13:16:29 -03:00
|
|
|
}
|
|
|
|
for (const auto& it : m_globals.variables)
|
|
|
|
{
|
|
|
|
if (it.second.is<T>())
|
2021-06-29 05:00:15 +02:00
|
|
|
globals.insert(std::pair<std::string, T>(it.first, it.second.as<T>()));
|
2020-12-21 13:16:29 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-29 05:00:15 +02:00
|
|
|
template void GameScript::GetVariables<bool>(std::map<std::string, bool>& locals, std::map<std::string, bool>& globals);
|
|
|
|
template void GameScript::GetVariables<float>(std::map<std::string, float>& locals, std::map<std::string, float>& globals);
|
|
|
|
template void GameScript::GetVariables<std::string>(std::map<std::string, std::string>& locals, std::map<std::string, std::string>& globals);
|
2020-12-21 13:16:29 -03:00
|
|
|
|
|
|
|
template <typename T>
|
2021-06-29 05:00:15 +02:00
|
|
|
void GameScript::SetVariables(std::map<std::string, T>& locals, std::map<std::string, T>& globals)
|
2020-12-21 13:16:29 -03:00
|
|
|
{
|
|
|
|
m_locals.variables.clear();
|
|
|
|
for (const auto& it : locals)
|
|
|
|
{
|
2021-06-29 05:00:15 +02:00
|
|
|
m_locals.variables.insert(std::pair<std::string, sol::object>(it.first, sol::object(m_lua->lua_state(), sol::in_place, it.second)));
|
2020-12-21 13:16:29 -03:00
|
|
|
}
|
|
|
|
for (const auto& it : globals)
|
|
|
|
{
|
2021-06-29 05:00:15 +02:00
|
|
|
m_globals.variables.insert(std::pair<std::string, sol::object>(it.first, sol::object(m_lua->lua_state(), sol::in_place, it.second)));
|
2020-12-21 13:16:29 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-29 05:00:15 +02:00
|
|
|
template void GameScript::SetVariables<bool>(std::map<std::string, bool>& locals, std::map<std::string, bool>& globals);
|
|
|
|
template void GameScript::SetVariables<float>(std::map<std::string, float>& locals, std::map<std::string, float>& globals);
|
|
|
|
template void GameScript::SetVariables<std::string>(std::map<std::string, std::string>& locals, std::map<std::string, std::string>& globals);
|
2020-12-21 13:16:29 -03:00
|
|
|
|
2021-07-01 19:29:58 +01:00
|
|
|
std::unique_ptr<GameScriptItemInfo> GameScript::GetItemById(int id)
|
2020-12-21 13:16:29 -03:00
|
|
|
{
|
|
|
|
if (m_itemsMapId.find(id) == m_itemsMapId.end())
|
|
|
|
{
|
|
|
|
if (WarningsAsErrors)
|
|
|
|
throw "item id not found";
|
2021-07-01 19:29:58 +01:00
|
|
|
return std::unique_ptr<GameScriptItemInfo>(nullptr);
|
2020-12-21 13:16:29 -03:00
|
|
|
}
|
|
|
|
|
2021-07-20 17:58:11 +01:00
|
|
|
return std::make_unique<GameScriptItemInfo>(m_itemsMapId[id], false);
|
2020-12-21 13:16:29 -03:00
|
|
|
}
|
|
|
|
|
2021-07-20 17:58:11 +01:00
|
|
|
std::unique_ptr<GameScriptItemInfo> GameScript::GetItemByName(std::string const & name)
|
2020-12-21 13:16:29 -03:00
|
|
|
{
|
|
|
|
if (m_itemsMapName.find(name) == m_itemsMapName.end())
|
|
|
|
{
|
|
|
|
if (WarningsAsErrors)
|
2021-07-20 17:58:11 +01:00
|
|
|
{
|
|
|
|
std::string error{ "Item name not found: " };
|
|
|
|
error += name;
|
|
|
|
throw std::runtime_error{error};
|
|
|
|
}
|
2021-07-01 19:29:58 +01:00
|
|
|
return std::unique_ptr<GameScriptItemInfo>(nullptr);
|
2020-12-21 13:16:29 -03:00
|
|
|
}
|
|
|
|
|
2021-07-20 17:58:11 +01:00
|
|
|
return std::make_unique<GameScriptItemInfo>(m_itemsMapName[name], false);
|
2020-12-21 13:16:29 -03:00
|
|
|
}
|
|
|
|
|
2021-07-04 14:33:57 +01:00
|
|
|
void GameScript::PlayAudioTrack(std::string const & trackName, bool looped)
|
2021-06-29 05:00:15 +02:00
|
|
|
{
|
2021-07-04 14:33:57 +01:00
|
|
|
S_CDPlay(trackName, looped);
|
2021-06-29 05:00:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GameScript::PlaySoundEffect(int id, GameScriptPosition p, int flags)
|
2020-12-21 13:16:29 -03:00
|
|
|
{
|
|
|
|
PHD_3DPOS pos;
|
|
|
|
|
2021-07-03 23:18:10 +01:00
|
|
|
pos.xPos = p.x;
|
|
|
|
pos.yPos = p.y;
|
|
|
|
pos.zPos = p.z;
|
2020-12-21 13:16:29 -03:00
|
|
|
pos.xRot = 0;
|
|
|
|
pos.yRot = 0;
|
|
|
|
pos.zRot = 0;
|
|
|
|
|
|
|
|
SoundEffect(id, &pos, flags);
|
|
|
|
}
|
|
|
|
|
2021-06-29 05:00:15 +02:00
|
|
|
void GameScript::PlaySoundEffect(int id, int flags)
|
2020-12-21 13:16:29 -03:00
|
|
|
{
|
|
|
|
SoundEffect(id, NULL, flags);
|
|
|
|
}
|
|
|
|
|
2021-07-03 23:18:10 +01:00
|
|
|
void GameScript::SetAmbientTrack(std::string const & trackName)
|
2020-12-21 13:16:29 -03:00
|
|
|
{
|
2021-06-29 05:00:15 +02:00
|
|
|
CurrentAtmosphere = trackName;
|
|
|
|
S_CDPlay(CurrentAtmosphere, 1);
|
2020-12-21 13:16:29 -03:00
|
|
|
}
|
|
|
|
|
2021-06-29 05:00:15 +02:00
|
|
|
void GameScript::AddLightningArc(GameScriptPosition src, GameScriptPosition dest, GameScriptColor color, int lifetime, int amplitude, int beamWidth, int segments, int flags)
|
2020-12-21 13:16:29 -03:00
|
|
|
{
|
2021-06-29 05:00:15 +02:00
|
|
|
PHD_VECTOR p1;
|
2021-07-03 23:18:10 +01:00
|
|
|
p1.x = src.x;
|
|
|
|
p1.y = src.y;
|
|
|
|
p1.z = src.z;
|
2021-06-29 05:00:15 +02:00
|
|
|
|
|
|
|
PHD_VECTOR p2;
|
2021-07-03 23:18:10 +01:00
|
|
|
p2.x = dest.x;
|
|
|
|
p2.y = dest.y;
|
|
|
|
p2.z = dest.z;
|
2021-06-29 05:00:15 +02:00
|
|
|
|
|
|
|
TriggerLightning(&p1, &p2, amplitude, color.GetR(), color.GetG(), color.GetB(), lifetime, flags, beamWidth, segments);
|
2020-12-21 13:16:29 -03:00
|
|
|
}
|
|
|
|
|
2021-06-29 05:00:15 +02:00
|
|
|
void GameScript::AddShockwave(GameScriptPosition pos, int innerRadius, int outerRadius, GameScriptColor color, int lifetime, int speed, int angle, int flags)
|
2020-12-21 13:16:29 -03:00
|
|
|
{
|
2021-06-29 05:00:15 +02:00
|
|
|
PHD_3DPOS p;
|
2021-07-03 23:18:10 +01:00
|
|
|
p.xPos = pos.x;
|
|
|
|
p.yPos = pos.y;
|
|
|
|
p.zPos = pos.z;
|
2021-06-29 05:00:15 +02:00
|
|
|
|
|
|
|
TriggerShockwave(&p, innerRadius, outerRadius, speed, color.GetR(), color.GetG(), color.GetB(), lifetime, FROM_DEGREES(angle), flags);
|
2020-12-21 13:16:29 -03:00
|
|
|
}
|
|
|
|
|
2021-06-29 05:00:15 +02:00
|
|
|
void GameScript::AddDynamicLight(GameScriptPosition pos, GameScriptColor color, int radius, int lifetime)
|
2020-12-21 13:16:29 -03:00
|
|
|
{
|
2021-07-03 23:18:10 +01:00
|
|
|
TriggerDynamicLight(pos.x, pos.y, pos.z, radius, color.GetR(), color.GetG(), color.GetB());
|
2020-12-21 13:16:29 -03:00
|
|
|
}
|
|
|
|
|
2021-06-29 05:00:15 +02:00
|
|
|
void GameScript::AddBlood(GameScriptPosition pos, int num)
|
2020-12-21 13:16:29 -03:00
|
|
|
{
|
2021-07-03 23:18:10 +01:00
|
|
|
TriggerBlood(pos.x, pos.y, pos.z, -1, num);
|
2020-12-21 13:16:29 -03:00
|
|
|
}
|
|
|
|
|
2021-06-29 05:00:15 +02:00
|
|
|
void GameScript::AddFireFlame(GameScriptPosition pos, int size)
|
2020-12-21 13:16:29 -03:00
|
|
|
{
|
2021-07-03 23:18:10 +01:00
|
|
|
AddFire(pos.x, pos.y, pos.z, size, FindRoomNumber(pos), true);
|
2020-12-21 13:16:29 -03:00
|
|
|
}
|
|
|
|
|
2021-06-29 05:00:15 +02:00
|
|
|
void GameScript::Earthquake(int strength)
|
2020-12-21 13:16:29 -03:00
|
|
|
{
|
2021-06-29 05:00:15 +02:00
|
|
|
Camera.bounce = -strength;
|
2020-12-21 13:16:29 -03:00
|
|
|
}
|
|
|
|
|
2021-06-29 05:00:15 +02:00
|
|
|
// Inventory
|
2021-07-13 13:21:13 +01:00
|
|
|
void GameScript::InventoryAdd(GAME_OBJECT_ID slot, sol::optional<int> count)
|
2020-12-21 13:16:29 -03:00
|
|
|
{
|
2021-07-13 13:21:13 +01:00
|
|
|
PickedUpObject(slot, count.value_or(0));
|
2021-06-29 05:00:15 +02:00
|
|
|
}
|
2020-12-21 13:16:29 -03:00
|
|
|
|
2021-07-13 13:21:13 +01:00
|
|
|
void GameScript::InventoryRemove(GAME_OBJECT_ID slot, sol::optional<int> count)
|
2021-06-29 05:00:15 +02:00
|
|
|
{
|
2021-07-10 14:05:01 +01:00
|
|
|
RemoveObjectFromInventory(static_cast<GAME_OBJECT_ID>(inventry_objects_list[slot].object_number), count.value_or(0));
|
2020-12-21 13:16:29 -03:00
|
|
|
}
|
|
|
|
|
2021-07-13 13:21:13 +01:00
|
|
|
int GameScript::InventoryGetCount(GAME_OBJECT_ID slot)
|
2020-12-21 13:16:29 -03:00
|
|
|
{
|
2021-07-13 13:21:13 +01:00
|
|
|
return GetInventoryCount(slot);
|
2020-12-21 13:16:29 -03:00
|
|
|
}
|
|
|
|
|
2021-07-13 13:21:13 +01:00
|
|
|
void GameScript::InventorySetCount(GAME_OBJECT_ID slot, int count)
|
2020-12-21 13:16:29 -03:00
|
|
|
{
|
2021-07-10 14:05:01 +01:00
|
|
|
// add the amount we'd need to add to get to count
|
2021-07-13 13:21:13 +01:00
|
|
|
int currAmt = GetInventoryCount(slot);
|
2021-07-10 14:05:01 +01:00
|
|
|
InventoryAdd(slot, count - currAmt);
|
2020-12-21 13:16:29 -03:00
|
|
|
}
|
|
|
|
|
2021-06-29 05:00:15 +02:00
|
|
|
void GameScript::InventoryCombine(int slot1, int slot2)
|
2020-12-21 13:16:29 -03:00
|
|
|
{
|
2021-06-29 05:00:15 +02:00
|
|
|
|
2020-12-21 13:16:29 -03:00
|
|
|
}
|
|
|
|
|
2021-07-05 18:18:20 +01:00
|
|
|
void GameScript::InventorySeparate(int slot)
|
2020-12-21 13:16:29 -03:00
|
|
|
{
|
2021-06-29 05:00:15 +02:00
|
|
|
|
2020-12-21 13:16:29 -03:00
|
|
|
}
|
|
|
|
|
2021-06-29 05:00:15 +02:00
|
|
|
// Misc
|
|
|
|
void GameScript::PrintString(std::string key, GameScriptPosition pos, GameScriptColor color, int lifetime, int flags)
|
2020-12-21 13:16:29 -03:00
|
|
|
{
|
2021-06-29 05:00:15 +02:00
|
|
|
|
2020-12-21 13:16:29 -03:00
|
|
|
}
|
|
|
|
|
2021-06-29 05:00:15 +02:00
|
|
|
int GameScript::FindRoomNumber(GameScriptPosition pos)
|
2020-12-21 13:16:29 -03:00
|
|
|
{
|
2021-06-29 05:00:15 +02:00
|
|
|
return 0;
|
2020-12-21 13:16:29 -03:00
|
|
|
}
|
|
|
|
|
2021-06-29 05:00:15 +02:00
|
|
|
void GameScript::AssignItemsAndLara()
|
2020-12-21 13:16:29 -03:00
|
|
|
{
|
2021-06-29 05:00:15 +02:00
|
|
|
m_lua->set("Level", m_locals);
|
|
|
|
m_lua->set("Game", m_globals);
|
2021-07-21 18:09:53 +01:00
|
|
|
m_lua->set("Lara", GameScriptItemInfo(Lara.itemNumber, false)); // do we need GetLara if we have this?
|
2020-12-21 13:16:29 -03:00
|
|
|
}
|
|
|
|
|
2021-06-29 05:00:15 +02:00
|
|
|
void GameScript::ResetVariables()
|
2020-12-21 13:16:29 -03:00
|
|
|
{
|
2021-06-29 05:00:15 +02:00
|
|
|
(*m_lua)["Lara"] = NULL;
|
|
|
|
}
|
2020-12-21 13:16:29 -03:00
|
|
|
|
2021-06-29 05:00:15 +02:00
|
|
|
int GameScript::CalculateDistance(GameScriptPosition pos1, GameScriptPosition pos2)
|
|
|
|
{
|
2021-07-03 23:18:10 +01:00
|
|
|
return sqrt(SQUARE(pos1.x - pos2.x) + SQUARE(pos1.y - pos2.y) + SQUARE(pos1.z - pos2.z));
|
2020-12-21 13:16:29 -03:00
|
|
|
}
|
|
|
|
|
2021-06-29 05:00:15 +02:00
|
|
|
int GameScript::CalculateHorizontalDistance(GameScriptPosition pos1, GameScriptPosition pos2)
|
2020-12-21 13:16:29 -03:00
|
|
|
{
|
2021-07-03 23:18:10 +01:00
|
|
|
return sqrt(SQUARE(pos1.x - pos2.x) + SQUARE(pos1.z - pos2.z));
|
2021-06-29 05:00:15 +02:00
|
|
|
}
|
2020-12-21 13:16:29 -03:00
|
|
|
|
2021-06-29 05:00:15 +02:00
|
|
|
sol::object LuaVariables::GetVariable(std::string key)
|
2020-12-21 13:16:29 -03:00
|
|
|
{
|
|
|
|
if (variables.find(key) == variables.end())
|
|
|
|
return sol::lua_nil;
|
|
|
|
return variables[key];
|
|
|
|
}
|
|
|
|
|
2021-06-29 05:00:15 +02:00
|
|
|
void LuaVariables::SetVariable(std::string key, sol::object value)
|
2020-12-21 13:16:29 -03:00
|
|
|
{
|
|
|
|
switch (value.get_type())
|
|
|
|
{
|
|
|
|
case sol::type::lua_nil:
|
|
|
|
variables.erase(key);
|
|
|
|
break;
|
|
|
|
case sol::type::boolean:
|
|
|
|
case sol::type::number:
|
|
|
|
case sol::type::string:
|
|
|
|
variables[key] = value;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (WarningsAsErrors)
|
2021-06-29 05:00:15 +02:00
|
|
|
throw std::runtime_error("unsupported variable type");
|
2020-12-21 13:16:29 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-06-29 05:00:15 +02:00
|
|
|
|
2021-07-17 22:26:07 +01:00
|
|
|
void GameScript::ExecuteFunction(std::string const & name)
|
2021-07-17 05:56:40 +02:00
|
|
|
{
|
2021-07-17 22:26:07 +01:00
|
|
|
sol::protected_function func = (*m_lua)[name.c_str()];
|
2021-07-17 05:56:40 +02:00
|
|
|
auto r = func();
|
|
|
|
if (WarningsAsErrors && !r.valid())
|
|
|
|
{
|
|
|
|
sol::error err = r;
|
|
|
|
std::cerr << "An error occurred: " << err.what() << "\n";
|
|
|
|
throw std::runtime_error(err.what());
|
2021-07-17 22:26:07 +01:00
|
|
|
}
|
2021-07-17 05:56:40 +02:00
|
|
|
}
|
|
|
|
|
2021-07-03 23:16:28 +01:00
|
|
|
static void doCallback(sol::protected_function const & func) {
|
|
|
|
auto r = func();
|
|
|
|
if (WarningsAsErrors && !r.valid())
|
|
|
|
{
|
|
|
|
sol::error err = r;
|
|
|
|
std::cerr << "An error occurred: " << err.what() << "\n";
|
|
|
|
throw std::runtime_error(err.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:33:48 +01:00
|
|
|
void GameScript::OnStart()
|
|
|
|
{
|
2021-07-06 11:54:34 +02:00
|
|
|
if (m_onStart.valid())
|
|
|
|
doCallback(m_onStart);
|
2021-07-01 19:33:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GameScript::OnLoad()
|
|
|
|
{
|
2021-07-06 11:54:34 +02:00
|
|
|
if (m_onLoad.valid())
|
|
|
|
doCallback(m_onLoad);
|
2021-07-01 19:33:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GameScript::OnControlPhase()
|
|
|
|
{
|
2021-07-06 11:54:34 +02:00
|
|
|
if (m_onControlPhase.valid())
|
|
|
|
doCallback(m_onControlPhase);
|
2021-07-01 19:33:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GameScript::OnSave()
|
|
|
|
{
|
2021-07-06 11:54:34 +02:00
|
|
|
if (m_onSave.valid())
|
|
|
|
doCallback(m_onSave);
|
2021-07-01 19:33:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GameScript::OnEnd()
|
|
|
|
{
|
2021-07-06 11:54:34 +02:00
|
|
|
if (m_onEnd.valid())
|
|
|
|
doCallback(m_onEnd);
|
2021-07-01 19:33:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GameScript::InitCallbacks()
|
|
|
|
{
|
|
|
|
auto assignCB = [this](sol::protected_function& func, char const* luaFunc) {
|
|
|
|
func = (*m_lua)[luaFunc];
|
|
|
|
if (WarningsAsErrors && !func.valid())
|
|
|
|
{
|
|
|
|
std::string err{ "Level's script file requires callback \"" };
|
|
|
|
err += std::string{ luaFunc };
|
|
|
|
err += "\"";
|
|
|
|
throw std::runtime_error(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
assignCB(m_onStart, "OnStart");
|
|
|
|
assignCB(m_onLoad, "OnLoad");
|
|
|
|
assignCB(m_onControlPhase, "OnControlPhase");
|
|
|
|
assignCB(m_onSave, "OnSave");
|
|
|
|
assignCB(m_onEnd, "OnEnd");
|
|
|
|
}
|