|
| 1 | +/***************************************************************************** |
| 2 | + * |
| 3 | + * PROJECT: Multi Theft Auto |
| 4 | + * LICENSE: See LICENSE in the top level directory |
| 5 | + * FILE: mods/shared_logic/luadefs/CLua2DFXDefs.cpp |
| 6 | + * PURPOSE: Lua definitions class |
| 7 | + * |
| 8 | + * Multi Theft Auto is available from https://www.multitheftauto.com/ |
| 9 | + * |
| 10 | + *****************************************************************************/ |
| 11 | + |
| 12 | +#include "StdInc.h" |
| 13 | +#include "CLua2DFXDefs.h" |
| 14 | +#include <lua/CLuaFunctionParser.h> |
| 15 | + |
| 16 | +void CLua2DFXDefs::LoadFunctions() |
| 17 | +{ |
| 18 | + constexpr static const std::pair<const char*, lua_CFunction> functions[]{ |
| 19 | + // Create / destroy functions |
| 20 | + {"addModel2DFX", ArgumentParser<AddModel2DFX>}, |
| 21 | + {"removeModel2DFX", ArgumentParser<RemoveModel2DFX>}, |
| 22 | + {"resetModel2DFX", ArgumentParser<ResetModel2DFX>}, |
| 23 | + |
| 24 | + // Set functions |
| 25 | + {"setModel2DFXProperties", ArgumentParser<SetModel2DFXProperties>}, |
| 26 | + {"set2DFXProperties", ArgumentParser<Set2DFXProperties>}, |
| 27 | + {"setModel2DFXProperty", ArgumentParser<SetModel2DFXProperty>}, |
| 28 | + {"set2DFXProperty", ArgumentParser<Set2DFXProperty>}, |
| 29 | + {"setModel2DFXPosition", ArgumentParser<SetModel2DFXPosition>}, |
| 30 | + {"set2DFXPosition", ArgumentParser<Set2DFXPosition>}, |
| 31 | + |
| 32 | + // Get functions |
| 33 | + {"getModel2DFXProperties", ArgumentParser<GetModel2DFXProperties>}, |
| 34 | + {"get2DFXProperties", ArgumentParser<Get2DFXProperties>}, |
| 35 | + {"getModel2DFXProperty", ArgumentParser<GetModel2DFXProperty>}, |
| 36 | + {"get2DFXProperty", ArgumentParser<Get2DFXProperty>}, |
| 37 | + {"getModel2DFXPosition", ArgumentParser<GetModel2DFXPosition>}, |
| 38 | + {"get2DFXPosition", ArgumentParser<Get2DFXPosition>}, |
| 39 | + {"getModel2DFXCount", ArgumentParser<GetModel2DFXCount>}, |
| 40 | + }; |
| 41 | + |
| 42 | + // Add functions |
| 43 | + for (const auto& [name, func] : functions) |
| 44 | + CLuaCFunctions::AddFunction(name, func); |
| 45 | +} |
| 46 | + |
| 47 | +void CLua2DFXDefs::AddClass(lua_State* luaVM) |
| 48 | +{ |
| 49 | + lua_newclass(luaVM); |
| 50 | + |
| 51 | + lua_classfunction(luaVM, "add", "addModel2DFX"); |
| 52 | + |
| 53 | + lua_classfunction(luaVM, "setProperties", "set2DFXProperties"); |
| 54 | + lua_classfunction(luaVM, "setProperty", "set2DFXProperty"); |
| 55 | + lua_classfunction(luaVM, "setPosition", "set2DFXPosition"); |
| 56 | + |
| 57 | + lua_classfunction(luaVM, "getProperties", "get2DFXProperties"); |
| 58 | + lua_classfunction(luaVM, "getProperty", "get2DFXProperty"); |
| 59 | + lua_classfunction(luaVM, "getPosition", "get2DFXPosition"); |
| 60 | + |
| 61 | + lua_classvariable(luaVM, "properties", "get2DFXProperties", "set2DFXProperties"); |
| 62 | + lua_classvariable(luaVM, "position", "get2DFXPosition", "set2DFXPosition"); |
| 63 | + |
| 64 | + lua_registerclass(luaVM, "2DFX", "Element"); |
| 65 | +} |
| 66 | + |
| 67 | +std::variant<bool, CClient2DFX*> CLua2DFXDefs::AddModel2DFX(lua_State* luaVM, std::uint32_t modelID, CVector position, e2dEffectType effectType, effectDataMap effectData) |
| 68 | +{ |
| 69 | + // Only these effects make sense in MTA |
| 70 | + if (effectType != e2dEffectType::LIGHT && effectType != e2dEffectType::PARTICLE && effectType != e2dEffectType::ROADSIGN && |
| 71 | + effectType != e2dEffectType::ESCALATOR && effectType != e2dEffectType::SUN_GLARE) |
| 72 | + return false; |
| 73 | + |
| 74 | + if (!CClient2DFXManager::IsValidModel(modelID)) |
| 75 | + throw std::invalid_argument("Invalid model ID"); |
| 76 | + |
| 77 | + const char* error = CClient2DFXManager::IsValidEffectData(effectType, effectData); |
| 78 | + if (error) |
| 79 | + throw LuaFunctionError(error); |
| 80 | + |
| 81 | + CClient2DFX* effect = m_p2DFXManager->Add2DFX(modelID, position, effectType, effectData); |
| 82 | + if (!effect) |
| 83 | + return false; |
| 84 | + |
| 85 | + CResource* resource = &lua_getownerresource(luaVM); |
| 86 | + if (resource) |
| 87 | + effect->SetParent(resource->GetResource2DFXRoot()); |
| 88 | + |
| 89 | + return effect; |
| 90 | +} |
| 91 | + |
| 92 | +bool CLua2DFXDefs::RemoveModel2DFX(std::uint32_t modelID, std::optional<std::uint32_t> index, std::optional<bool> includeDefault) |
| 93 | +{ |
| 94 | + if (!CClient2DFXManager::IsValidModel(modelID)) |
| 95 | + throw std::invalid_argument("Invalid model ID"); |
| 96 | + |
| 97 | + CModelInfo* modelInfo = g_pGame->GetModelInfo(static_cast<DWORD>(modelID)); |
| 98 | + if (!modelInfo) |
| 99 | + return false; |
| 100 | + |
| 101 | + if (index.has_value()) |
| 102 | + { |
| 103 | + auto count = GetModel2DFXCount(modelID); |
| 104 | + if (std::holds_alternative<std::uint32_t>(count) && index >= std::get<std::uint32_t>(count)) |
| 105 | + throw std::invalid_argument("Invalid effect index"); |
| 106 | + } |
| 107 | + |
| 108 | + return index.has_value() ? modelInfo->Remove2DFXEffectAtIndex(index.value(), includeDefault.value_or(false)) : modelInfo->RemoveAll2DFXEffects(includeDefault.value_or(false)); |
| 109 | +} |
| 110 | + |
| 111 | +bool CLua2DFXDefs::ResetModel2DFX(std::uint32_t modelID, std::optional<bool> removeCustomEffects) |
| 112 | +{ |
| 113 | + if (!CClient2DFXManager::IsValidModel(modelID)) |
| 114 | + throw std::invalid_argument("Invalid model ID"); |
| 115 | + |
| 116 | + CModelInfo* modelInfo = g_pGame->GetModelInfo(static_cast<DWORD>(modelID)); |
| 117 | + if (!modelInfo) |
| 118 | + return false; |
| 119 | + |
| 120 | + return modelInfo->Reset2DFXEffects(removeCustomEffects.value_or(true)); |
| 121 | +} |
| 122 | + |
| 123 | +bool CLua2DFXDefs::SetModel2DFXProperties(std::uint32_t modelID, std::uint32_t index, effectDataMap effectData) |
| 124 | +{ |
| 125 | + if (!CClient2DFXManager::IsValidModel(modelID)) |
| 126 | + throw std::invalid_argument("Invalid model ID"); |
| 127 | + |
| 128 | + auto count = GetModel2DFXCount(modelID); |
| 129 | + if (std::holds_alternative<std::uint32_t>(count) && index >= std::get<std::uint32_t>(count)) |
| 130 | + throw std::invalid_argument("Invalid effect index"); |
| 131 | + |
| 132 | + CModelInfo* modelInfo = g_pGame->GetModelInfo(static_cast<DWORD>(modelID)); |
| 133 | + if (!modelInfo) |
| 134 | + return false; |
| 135 | + |
| 136 | + auto* effect = modelInfo->Get2DFXFromIndex(index); |
| 137 | + if (!effect) |
| 138 | + return false; |
| 139 | + |
| 140 | + const char* error = CClient2DFXManager::IsValidEffectData(effect->type, effectData); |
| 141 | + if (error) |
| 142 | + throw LuaFunctionError(error); |
| 143 | + |
| 144 | + modelInfo->StoreDefault2DFXEffect(effect); |
| 145 | + return m_p2DFXManager->Set2DFXProperties(effect, effectData); |
| 146 | +} |
| 147 | + |
| 148 | +bool CLua2DFXDefs::Set2DFXProperties(CClient2DFX* effect, effectDataMap effectData) |
| 149 | +{ |
| 150 | + const char* error = CClient2DFXManager::IsValidEffectData(effect->Get2DFXType(), effectData); |
| 151 | + if (error) |
| 152 | + throw LuaFunctionError(error); |
| 153 | + |
| 154 | + return m_p2DFXManager->Set2DFXProperties(effect->Get2DFX(), effectData); |
| 155 | +} |
| 156 | + |
| 157 | +bool CLua2DFXDefs::SetModel2DFXProperty(std::uint32_t modelID, std::uint32_t index, e2dEffectProperty property, std::variant<bool, float, std::string> propertyValue) |
| 158 | +{ |
| 159 | + if (!CClient2DFXManager::IsValidModel(modelID)) |
| 160 | + throw std::invalid_argument("Invalid model ID"); |
| 161 | + |
| 162 | + auto count = GetModel2DFXCount(modelID); |
| 163 | + if (std::holds_alternative<std::uint32_t>(count) && index >= std::get<std::uint32_t>(count)) |
| 164 | + throw std::invalid_argument("Invalid effect index"); |
| 165 | + |
| 166 | + CModelInfo* modelInfo = g_pGame->GetModelInfo(static_cast<DWORD>(modelID)); |
| 167 | + if (!modelInfo) |
| 168 | + return false; |
| 169 | + |
| 170 | + auto* effect = modelInfo->Get2DFXFromIndex(index); |
| 171 | + if (!effect) |
| 172 | + return false; |
| 173 | + |
| 174 | + modelInfo->StoreDefault2DFXEffect(effect); |
| 175 | + return m_p2DFXManager->Set2DFXProperty(effect, property, propertyValue); |
| 176 | +} |
| 177 | + |
| 178 | +bool CLua2DFXDefs::Set2DFXProperty(CClient2DFX* effect, e2dEffectProperty property, std::variant<bool, float, std::string> propertyValue) |
| 179 | +{ |
| 180 | + return m_p2DFXManager->Set2DFXProperty(effect->Get2DFX(), property, propertyValue); |
| 181 | +} |
| 182 | + |
| 183 | +bool CLua2DFXDefs::SetModel2DFXPosition(std::uint32_t modelID, std::uint32_t index, CVector position) |
| 184 | +{ |
| 185 | + if (!CClient2DFXManager::IsValidModel(modelID)) |
| 186 | + throw std::invalid_argument("Invalid model ID"); |
| 187 | + |
| 188 | + auto count = GetModel2DFXCount(modelID); |
| 189 | + if (std::holds_alternative<std::uint32_t>(count) && index >= std::get<std::uint32_t>(count)) |
| 190 | + throw std::invalid_argument("Invalid effect index"); |
| 191 | + |
| 192 | + CModelInfo* modelInfo = g_pGame->GetModelInfo(static_cast<DWORD>(modelID)); |
| 193 | + if (!modelInfo) |
| 194 | + return false; |
| 195 | + |
| 196 | + auto* effect = modelInfo->Get2DFXFromIndex(index); |
| 197 | + if (!effect) |
| 198 | + return false; |
| 199 | + |
| 200 | + modelInfo->StoreDefault2DFXEffect(effect); |
| 201 | + m_p2DFXManager->Set2DFXPosition(effect, position); |
| 202 | + return true; |
| 203 | +} |
| 204 | + |
| 205 | +bool CLua2DFXDefs::Set2DFXPosition(CClient2DFX* effect, CVector position) |
| 206 | +{ |
| 207 | + m_p2DFXManager->Set2DFXPosition(effect->Get2DFX(), position); |
| 208 | + return true; |
| 209 | +} |
| 210 | + |
| 211 | +std::variant<bool, CLuaMultiReturn<float,float,float>> CLua2DFXDefs::GetModel2DFXPosition(std::uint32_t modelID, std::uint32_t index) |
| 212 | +{ |
| 213 | + if (!CClient2DFXManager::IsValidModel(modelID)) |
| 214 | + throw std::invalid_argument("Invalid model ID"); |
| 215 | + |
| 216 | + auto count = GetModel2DFXCount(modelID); |
| 217 | + if (std::holds_alternative<std::uint32_t>(count) && index >= std::get<std::uint32_t>(count)) |
| 218 | + throw std::invalid_argument("Invalid effect index"); |
| 219 | + |
| 220 | + CModelInfo* modelInfo = g_pGame->GetModelInfo(static_cast<DWORD>(modelID)); |
| 221 | + if (!modelInfo) |
| 222 | + return false; |
| 223 | + |
| 224 | + auto* effect = modelInfo->Get2DFXFromIndex(index); |
| 225 | + if (!effect) |
| 226 | + return false; |
| 227 | + |
| 228 | + CVector* position = m_p2DFXManager->Get2DFXPosition(effect); |
| 229 | + return std::make_tuple(position->fX, position->fY, position->fZ); |
| 230 | +} |
| 231 | + |
| 232 | +std::variant<bool, CLuaMultiReturn<float, float, float>> CLua2DFXDefs::Get2DFXPosition(CClient2DFX* effect) |
| 233 | +{ |
| 234 | + CVector* position = m_p2DFXManager->Get2DFXPosition(effect->Get2DFX()); |
| 235 | + if (!position) |
| 236 | + return false; |
| 237 | + |
| 238 | + return std::make_tuple(position->fX, position->fY, position->fZ); |
| 239 | +} |
| 240 | + |
| 241 | +std::variant<bool, effectDataMap> CLua2DFXDefs::GetModel2DFXProperties(std::uint32_t modelID, std::uint32_t index) |
| 242 | +{ |
| 243 | + if (!CClient2DFXManager::IsValidModel(modelID)) |
| 244 | + throw std::invalid_argument("Invalid model ID"); |
| 245 | + |
| 246 | + auto count = GetModel2DFXCount(modelID); |
| 247 | + if (std::holds_alternative<std::uint32_t>(count) && index >= std::get<std::uint32_t>(count)) |
| 248 | + throw std::invalid_argument("Invalid effect index"); |
| 249 | + |
| 250 | + CModelInfo* modelInfo = g_pGame->GetModelInfo(static_cast<DWORD>(modelID)); |
| 251 | + if (!modelInfo) |
| 252 | + return false; |
| 253 | + |
| 254 | + auto* effect = modelInfo->Get2DFXFromIndex(index); |
| 255 | + if (!effect) |
| 256 | + return false; |
| 257 | + |
| 258 | + return m_p2DFXManager->Get2DFXProperties(effect); |
| 259 | +} |
| 260 | + |
| 261 | +std::variant<bool, effectDataMap> CLua2DFXDefs::Get2DFXProperties(CClient2DFX* effect) |
| 262 | +{ |
| 263 | + return m_p2DFXManager->Get2DFXProperties(effect->Get2DFX()); |
| 264 | +} |
| 265 | + |
| 266 | +std::variant<float, bool, std::string> CLua2DFXDefs::GetModel2DFXProperty(std::uint32_t modelID, std::uint32_t index, e2dEffectProperty property) |
| 267 | +{ |
| 268 | + if (!CClient2DFXManager::IsValidModel(modelID)) |
| 269 | + throw std::invalid_argument("Invalid model ID"); |
| 270 | + |
| 271 | + auto count = GetModel2DFXCount(modelID); |
| 272 | + if (std::holds_alternative<std::uint32_t>(count) && index >= std::get<std::uint32_t>(count)) |
| 273 | + throw std::invalid_argument("Invalid effect index"); |
| 274 | + |
| 275 | + CModelInfo* modelInfo = g_pGame->GetModelInfo(static_cast<DWORD>(modelID)); |
| 276 | + if (!modelInfo) |
| 277 | + return false; |
| 278 | + |
| 279 | + auto* effect = modelInfo->Get2DFXFromIndex(index); |
| 280 | + if (!effect) |
| 281 | + return false; |
| 282 | + |
| 283 | + return m_p2DFXManager->Get2DFXProperty(effect, property); |
| 284 | +} |
| 285 | + |
| 286 | +std::variant<float, bool, std::string> CLua2DFXDefs::Get2DFXProperty(CClient2DFX* effect, e2dEffectProperty property) |
| 287 | +{ |
| 288 | + return m_p2DFXManager->Get2DFXProperty(effect->Get2DFX(), property); |
| 289 | +} |
| 290 | + |
| 291 | +std::variant<bool, std::uint32_t> CLua2DFXDefs::GetModel2DFXCount(std::uint32_t modelID) |
| 292 | +{ |
| 293 | + if (!CClient2DFXManager::IsValidModel(modelID)) |
| 294 | + throw std::invalid_argument("Invalid model ID"); |
| 295 | + |
| 296 | + CModelInfo* modelInfo = g_pGame->GetModelInfo(static_cast<DWORD>(modelID)); |
| 297 | + if (!modelInfo) |
| 298 | + return false; |
| 299 | + |
| 300 | + return modelInfo->Get2DFXCount(); |
| 301 | +} |
0 commit comments