Skip to content

Commit

Permalink
Move defs to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
FileEX committed Sep 27, 2024
1 parent 6919ff4 commit fdddb29
Show file tree
Hide file tree
Showing 7 changed files with 347 additions and 294 deletions.
1 change: 1 addition & 0 deletions Client/mods/deathmatch/StdInc.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
#include <luadefs/CLuaWeaponDefs.h>
#include <luadefs/CLuaWorldDefs.h>
#include <luadefs/CLuaBuildingDefs.h>
#include <luadefs/CLua2DFXDefs.h>
#include <CRemoteCalls.h>

// Shared includes
Expand Down
1 change: 1 addition & 0 deletions Client/mods/deathmatch/logic/lua/CLuaMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ void CLuaMain::InitClasses(lua_State* luaVM)
CLuaWaterDefs::AddClass(luaVM);
CLuaWeaponDefs::AddClass(luaVM);
CLuaBuildingDefs::AddClass(luaVM);
CLua2DFXDefs::AddClass(luaVM);

CLuaShared::AddClasses(luaVM);
}
Expand Down
1 change: 1 addition & 0 deletions Client/mods/deathmatch/logic/lua/CLuaManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,5 @@ void CLuaManager::LoadCFunctions()
CLuaClientDefs::LoadFunctions();
CLuaDiscordDefs::LoadFunctions();
CLuaBuildingDefs::LoadFunctions();
CLua2DFXDefs::LoadFunctions();
}
301 changes: 301 additions & 0 deletions Client/mods/deathmatch/logic/luadefs/CLua2DFXDefs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,301 @@
/*****************************************************************************
*
* PROJECT: Multi Theft Auto
* LICENSE: See LICENSE in the top level directory
* FILE: mods/shared_logic/luadefs/CLua2DFXDefs.cpp
* PURPOSE: Lua definitions class
*
* Multi Theft Auto is available from https://www.multitheftauto.com/
*
*****************************************************************************/

#include "StdInc.h"
#include "CLua2DFXDefs.h"
#include <lua/CLuaFunctionParser.h>

void CLua2DFXDefs::LoadFunctions()
{
constexpr static const std::pair<const char*, lua_CFunction> functions[]{
// Create / destroy functions
{"addModel2DFX", ArgumentParser<AddModel2DFX>},
{"removeModel2DFX", ArgumentParser<RemoveModel2DFX>},
{"resetModel2DFX", ArgumentParser<ResetModel2DFX>},

// Set functions
{"setModel2DFXProperties", ArgumentParser<SetModel2DFXProperties>},
{"set2DFXProperties", ArgumentParser<Set2DFXProperties>},
{"setModel2DFXProperty", ArgumentParser<SetModel2DFXProperty>},
{"set2DFXProperty", ArgumentParser<Set2DFXProperty>},
{"setModel2DFXPosition", ArgumentParser<SetModel2DFXPosition>},
{"set2DFXPosition", ArgumentParser<Set2DFXPosition>},

// Get functions
{"getModel2DFXProperties", ArgumentParser<GetModel2DFXProperties>},
{"get2DFXProperties", ArgumentParser<Get2DFXProperties>},
{"getModel2DFXProperty", ArgumentParser<GetModel2DFXProperty>},
{"get2DFXProperty", ArgumentParser<Get2DFXProperty>},
{"getModel2DFXPosition", ArgumentParser<GetModel2DFXPosition>},
{"get2DFXPosition", ArgumentParser<Get2DFXPosition>},
{"getModel2DFXCount", ArgumentParser<GetModel2DFXCount>},
};

// Add functions
for (const auto& [name, func] : functions)
CLuaCFunctions::AddFunction(name, func);
}

void CLua2DFXDefs::AddClass(lua_State* luaVM)
{
lua_newclass(luaVM);

lua_classfunction(luaVM, "add", "addModel2DFX");

lua_classfunction(luaVM, "setProperties", "set2DFXProperties");
lua_classfunction(luaVM, "setProperty", "set2DFXProperty");
lua_classfunction(luaVM, "setPosition", "set2DFXPosition");

lua_classfunction(luaVM, "getProperties", "get2DFXProperties");
lua_classfunction(luaVM, "getProperty", "get2DFXProperty");
lua_classfunction(luaVM, "getPosition", "get2DFXPosition");

lua_classvariable(luaVM, "properties", "get2DFXProperties", "set2DFXProperties");
lua_classvariable(luaVM, "position", "get2DFXPosition", "set2DFXPosition");

lua_registerclass(luaVM, "2DFX", "Element");
}

std::variant<bool, CClient2DFX*> CLua2DFXDefs::AddModel2DFX(lua_State* luaVM, std::uint32_t modelID, CVector position, e2dEffectType effectType, effectDataMap effectData)
{
// Only these effects make sense in MTA
if (effectType != e2dEffectType::LIGHT && effectType != e2dEffectType::PARTICLE && effectType != e2dEffectType::ROADSIGN &&
effectType != e2dEffectType::ESCALATOR && effectType != e2dEffectType::SUN_GLARE)
return false;

if (!CClient2DFXManager::IsValidModel(modelID))
throw std::invalid_argument("Invalid model ID");

const char* error = CClient2DFXManager::IsValidEffectData(effectType, effectData);
if (error)
throw LuaFunctionError(error);

CClient2DFX* effect = m_p2DFXManager->Add2DFX(modelID, position, effectType, effectData);
if (!effect)
return false;

CResource* resource = &lua_getownerresource(luaVM);
if (resource)
effect->SetParent(resource->GetResource2DFXRoot());

return effect;
}

bool CLua2DFXDefs::RemoveModel2DFX(std::uint32_t modelID, std::optional<std::uint32_t> index, std::optional<bool> includeDefault)
{
if (!CClient2DFXManager::IsValidModel(modelID))
throw std::invalid_argument("Invalid model ID");

CModelInfo* modelInfo = g_pGame->GetModelInfo(static_cast<DWORD>(modelID));
if (!modelInfo)
return false;

if (index.has_value())
{
auto count = GetModel2DFXCount(modelID);
if (std::holds_alternative<std::uint32_t>(count) && index >= std::get<std::uint32_t>(count))
throw std::invalid_argument("Invalid effect index");
}

return index.has_value() ? modelInfo->Remove2DFXEffectAtIndex(index.value(), includeDefault.value_or(false)) : modelInfo->RemoveAll2DFXEffects(includeDefault.value_or(false));
}

bool CLua2DFXDefs::ResetModel2DFX(std::uint32_t modelID, std::optional<bool> removeCustomEffects)
{
if (!CClient2DFXManager::IsValidModel(modelID))
throw std::invalid_argument("Invalid model ID");

CModelInfo* modelInfo = g_pGame->GetModelInfo(static_cast<DWORD>(modelID));
if (!modelInfo)
return false;

return modelInfo->Reset2DFXEffects(removeCustomEffects.value_or(true));
}

bool CLua2DFXDefs::SetModel2DFXProperties(std::uint32_t modelID, std::uint32_t index, effectDataMap effectData)
{
if (!CClient2DFXManager::IsValidModel(modelID))
throw std::invalid_argument("Invalid model ID");

auto count = GetModel2DFXCount(modelID);
if (std::holds_alternative<std::uint32_t>(count) && index >= std::get<std::uint32_t>(count))
throw std::invalid_argument("Invalid effect index");

CModelInfo* modelInfo = g_pGame->GetModelInfo(static_cast<DWORD>(modelID));
if (!modelInfo)
return false;

auto* effect = modelInfo->Get2DFXFromIndex(index);
if (!effect)
return false;

const char* error = CClient2DFXManager::IsValidEffectData(effect->type, effectData);
if (error)
throw LuaFunctionError(error);

modelInfo->StoreDefault2DFXEffect(effect);
return m_p2DFXManager->Set2DFXProperties(effect, effectData);
}

bool CLua2DFXDefs::Set2DFXProperties(CClient2DFX* effect, effectDataMap effectData)
{
const char* error = CClient2DFXManager::IsValidEffectData(effect->Get2DFXType(), effectData);
if (error)
throw LuaFunctionError(error);

return m_p2DFXManager->Set2DFXProperties(effect->Get2DFX(), effectData);
}

bool CLua2DFXDefs::SetModel2DFXProperty(std::uint32_t modelID, std::uint32_t index, e2dEffectProperty property, std::variant<bool, float, std::string> propertyValue)
{
if (!CClient2DFXManager::IsValidModel(modelID))
throw std::invalid_argument("Invalid model ID");

auto count = GetModel2DFXCount(modelID);
if (std::holds_alternative<std::uint32_t>(count) && index >= std::get<std::uint32_t>(count))
throw std::invalid_argument("Invalid effect index");

CModelInfo* modelInfo = g_pGame->GetModelInfo(static_cast<DWORD>(modelID));
if (!modelInfo)
return false;

auto* effect = modelInfo->Get2DFXFromIndex(index);
if (!effect)
return false;

modelInfo->StoreDefault2DFXEffect(effect);
return m_p2DFXManager->Set2DFXProperty(effect, property, propertyValue);
}

bool CLua2DFXDefs::Set2DFXProperty(CClient2DFX* effect, e2dEffectProperty property, std::variant<bool, float, std::string> propertyValue)
{
return m_p2DFXManager->Set2DFXProperty(effect->Get2DFX(), property, propertyValue);
}

bool CLua2DFXDefs::SetModel2DFXPosition(std::uint32_t modelID, std::uint32_t index, CVector position)
{
if (!CClient2DFXManager::IsValidModel(modelID))
throw std::invalid_argument("Invalid model ID");

auto count = GetModel2DFXCount(modelID);
if (std::holds_alternative<std::uint32_t>(count) && index >= std::get<std::uint32_t>(count))
throw std::invalid_argument("Invalid effect index");

CModelInfo* modelInfo = g_pGame->GetModelInfo(static_cast<DWORD>(modelID));
if (!modelInfo)
return false;

auto* effect = modelInfo->Get2DFXFromIndex(index);
if (!effect)
return false;

modelInfo->StoreDefault2DFXEffect(effect);
m_p2DFXManager->Set2DFXPosition(effect, position);
return true;
}

bool CLua2DFXDefs::Set2DFXPosition(CClient2DFX* effect, CVector position)
{
m_p2DFXManager->Set2DFXPosition(effect->Get2DFX(), position);
return true;
}

std::variant<bool, CLuaMultiReturn<float,float,float>> CLua2DFXDefs::GetModel2DFXPosition(std::uint32_t modelID, std::uint32_t index)
{
if (!CClient2DFXManager::IsValidModel(modelID))
throw std::invalid_argument("Invalid model ID");

auto count = GetModel2DFXCount(modelID);
if (std::holds_alternative<std::uint32_t>(count) && index >= std::get<std::uint32_t>(count))
throw std::invalid_argument("Invalid effect index");

CModelInfo* modelInfo = g_pGame->GetModelInfo(static_cast<DWORD>(modelID));
if (!modelInfo)
return false;

auto* effect = modelInfo->Get2DFXFromIndex(index);
if (!effect)
return false;

CVector* position = m_p2DFXManager->Get2DFXPosition(effect);
return std::make_tuple(position->fX, position->fY, position->fZ);
}

std::variant<bool, CLuaMultiReturn<float, float, float>> CLua2DFXDefs::Get2DFXPosition(CClient2DFX* effect)
{
CVector* position = m_p2DFXManager->Get2DFXPosition(effect->Get2DFX());
if (!position)
return false;

return std::make_tuple(position->fX, position->fY, position->fZ);
}

std::variant<bool, effectDataMap> CLua2DFXDefs::GetModel2DFXProperties(std::uint32_t modelID, std::uint32_t index)
{
if (!CClient2DFXManager::IsValidModel(modelID))
throw std::invalid_argument("Invalid model ID");

auto count = GetModel2DFXCount(modelID);
if (std::holds_alternative<std::uint32_t>(count) && index >= std::get<std::uint32_t>(count))
throw std::invalid_argument("Invalid effect index");

CModelInfo* modelInfo = g_pGame->GetModelInfo(static_cast<DWORD>(modelID));
if (!modelInfo)
return false;

auto* effect = modelInfo->Get2DFXFromIndex(index);
if (!effect)
return false;

return m_p2DFXManager->Get2DFXProperties(effect);
}

std::variant<bool, effectDataMap> CLua2DFXDefs::Get2DFXProperties(CClient2DFX* effect)
{
return m_p2DFXManager->Get2DFXProperties(effect->Get2DFX());
}

std::variant<float, bool, std::string> CLua2DFXDefs::GetModel2DFXProperty(std::uint32_t modelID, std::uint32_t index, e2dEffectProperty property)
{
if (!CClient2DFXManager::IsValidModel(modelID))
throw std::invalid_argument("Invalid model ID");

auto count = GetModel2DFXCount(modelID);
if (std::holds_alternative<std::uint32_t>(count) && index >= std::get<std::uint32_t>(count))
throw std::invalid_argument("Invalid effect index");

CModelInfo* modelInfo = g_pGame->GetModelInfo(static_cast<DWORD>(modelID));
if (!modelInfo)
return false;

auto* effect = modelInfo->Get2DFXFromIndex(index);
if (!effect)
return false;

return m_p2DFXManager->Get2DFXProperty(effect, property);
}

std::variant<float, bool, std::string> CLua2DFXDefs::Get2DFXProperty(CClient2DFX* effect, e2dEffectProperty property)
{
return m_p2DFXManager->Get2DFXProperty(effect->Get2DFX(), property);
}

std::variant<bool, std::uint32_t> CLua2DFXDefs::GetModel2DFXCount(std::uint32_t modelID)
{
if (!CClient2DFXManager::IsValidModel(modelID))
throw std::invalid_argument("Invalid model ID");

CModelInfo* modelInfo = g_pGame->GetModelInfo(static_cast<DWORD>(modelID));
if (!modelInfo)
return false;

return modelInfo->Get2DFXCount();
}
43 changes: 43 additions & 0 deletions Client/mods/deathmatch/logic/luadefs/CLua2DFXDefs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*****************************************************************************
*
* PROJECT: Multi Theft Auto
* LICENSE: See LICENSE in the top level directory
* FILE: mods/shared_logic/luadefs/CLua2DFXDefs.cpp
* PURPOSE: Lua definitions class
*
* Multi Theft Auto is available from https://www.multitheftauto.com/
*
*****************************************************************************/

#pragma once
#include "CLuaDefs.h"
#include <lua/CLuaMultiReturn.h>

class CLua2DFXDefs : public CLuaDefs
{
public:
static void LoadFunctions();
static void AddClass(lua_State* luaVM);

// Create/destroy functions
static std::variant<bool, CClient2DFX*> AddModel2DFX(lua_State* luaVM, std::uint32_t modelID, CVector position, e2dEffectType effectType, effectDataMap effectData);
static bool RemoveModel2DFX(std::uint32_t modelID, std::optional<std::uint32_t> index, std::optional<bool> includeDefault);
static bool ResetModel2DFX(std::uint32_t modelID, std::optional<bool> removeCustomEffects);

// Set functions
static bool SetModel2DFXProperties(std::uint32_t modelID, std::uint32_t index, effectDataMap effectData);
static bool Set2DFXProperties(CClient2DFX* effect, effectDataMap effectData);
static bool SetModel2DFXProperty(std::uint32_t modelID, std::uint32_t index, e2dEffectProperty property, std::variant<bool, float, std::string> propertyValue);
static bool Set2DFXProperty(CClient2DFX* effect, e2dEffectProperty property, std::variant<bool, float, std::string> propertyValue);
static bool SetModel2DFXPosition(std::uint32_t modelID, std::uint32_t index, CVector position);
static bool Set2DFXPosition(CClient2DFX* effect, CVector position);

// Get functions
static std::variant<bool, CLuaMultiReturn<float, float, float>> GetModel2DFXPosition(std::uint32_t modelID, std::uint32_t index);
static std::variant<bool, CLuaMultiReturn<float, float, float>> Get2DFXPosition(CClient2DFX* effect);
static std::variant<float, bool, std::string> GetModel2DFXProperty(std::uint32_t modelID, std::uint32_t index, e2dEffectProperty property);
static std::variant<float, bool, std::string> Get2DFXProperty(CClient2DFX* effect, e2dEffectProperty property);
static std::variant<bool, effectDataMap> GetModel2DFXProperties(std::uint32_t modelID, std::uint32_t index);
static std::variant<bool, effectDataMap> Get2DFXProperties(CClient2DFX* effect);
static std::variant<bool, std::uint32_t> GetModel2DFXCount(std::uint32_t modelID);
};
Loading

0 comments on commit fdddb29

Please sign in to comment.