From 9c5eb47e90db5a169778d1fc0cbd4ec48f2c6d94 Mon Sep 17 00:00:00 2001 From: David Cernat Date: Fri, 28 Apr 2017 16:33:58 +0300 Subject: [PATCH] [Server] Add and use Utils::getCellFromDescription() --- apps/openmw-mp/Script/Functions/Actors.cpp | 30 +++++----------------- apps/openmw-mp/Script/Functions/World.cpp | 30 +++++----------------- apps/openmw-mp/Utils.cpp | 30 +++++++++++++++++++++- apps/openmw-mp/Utils.hpp | 10 ++++++-- 4 files changed, 49 insertions(+), 51 deletions(-) diff --git a/apps/openmw-mp/Script/Functions/Actors.cpp b/apps/openmw-mp/Script/Functions/Actors.cpp index ee3a3fc21..5b0b14456 100644 --- a/apps/openmw-mp/Script/Functions/Actors.cpp +++ b/apps/openmw-mp/Script/Functions/Actors.cpp @@ -1,10 +1,11 @@ -#include +#include +#include +#include #include +#include #include -#include -#include -#include + #include "Actors.hpp" using namespace mwmp; @@ -86,26 +87,7 @@ double ActorFunctions::GetActorRotZ(unsigned int i) noexcept void ActorFunctions::SetScriptActorListCell(const char* cellDescription) noexcept { - static std::regex exteriorCellPattern("^(-?\\d+), (-?\\d+)$"); - std::string description = cellDescription; - std::smatch baseMatch; - - if (std::regex_match(description, baseMatch, exteriorCellPattern)) - { - scriptActorList.cell.mData.mFlags &= ~ESM::Cell::Interior; - - // The first sub match is the whole string, so check for a length of 3 - if (baseMatch.size() == 3) - { - scriptActorList.cell.mData.mX = stoi(baseMatch[1].str()); - scriptActorList.cell.mData.mY = stoi(baseMatch[2].str()); - } - } - else - { - scriptActorList.cell.mData.mFlags |= ESM::Cell::Interior; - scriptActorList.cell.mName = description; - } + scriptActorList.cell = Utils::getCellFromDescription(cellDescription); } void ActorFunctions::SetScriptActorListAction(unsigned char action) noexcept diff --git a/apps/openmw-mp/Script/Functions/World.cpp b/apps/openmw-mp/Script/Functions/World.cpp index c94aec515..be6426411 100644 --- a/apps/openmw-mp/Script/Functions/World.cpp +++ b/apps/openmw-mp/Script/Functions/World.cpp @@ -1,10 +1,11 @@ -#include +#include +#include +#include #include +#include #include -#include -#include -#include + #include "World.hpp" using namespace mwmp; @@ -143,26 +144,7 @@ int WorldFunctions::GetContainerItemActionCount(unsigned int objectIndex, unsign void WorldFunctions::SetScriptEventCell(const char* cellDescription) noexcept { - static std::regex exteriorCellPattern("^(-?\\d+), (-?\\d+)$"); - std::string description = cellDescription; - std::smatch baseMatch; - - if (std::regex_match(description, baseMatch, exteriorCellPattern)) - { - scriptEvent.cell.mData.mFlags &= ~ESM::Cell::Interior; - - // The first sub match is the whole string, so check for a length of 3 - if (baseMatch.size() == 3) - { - scriptEvent.cell.mData.mX = stoi(baseMatch[1].str()); - scriptEvent.cell.mData.mY = stoi(baseMatch[2].str()); - } - } - else - { - scriptEvent.cell.mData.mFlags |= ESM::Cell::Interior; - scriptEvent.cell.mName = description; - } + scriptEvent.cell = Utils::getCellFromDescription(cellDescription); } void WorldFunctions::SetScriptEventAction(unsigned char action) noexcept diff --git a/apps/openmw-mp/Utils.cpp b/apps/openmw-mp/Utils.cpp index fc1350284..d5d621e49 100644 --- a/apps/openmw-mp/Utils.cpp +++ b/apps/openmw-mp/Utils.cpp @@ -23,4 +23,32 @@ const vector Utils::split(const string &str, int delimiter) result.push_back(move(buffer)); return result; -} \ No newline at end of file +} + +ESM::Cell Utils::getCellFromDescription(std::string cellDescription) +{ + ESM::Cell cell; + cell.blank(); + + static std::regex exteriorCellPattern("^(-?\\d+), (-?\\d+)$"); + std::smatch baseMatch; + + if (std::regex_match(cellDescription, baseMatch, exteriorCellPattern)) + { + cell.mData.mFlags &= ~ESM::Cell::Interior; + + // The first sub match is the whole string, so check for a length of 3 + if (baseMatch.size() == 3) + { + cell.mData.mX = stoi(baseMatch[1].str()); + cell.mData.mY = stoi(baseMatch[2].str()); + } + } + else + { + cell.mData.mFlags |= ESM::Cell::Interior; + cell.mName = cellDescription; + } + + return cell; +} diff --git a/apps/openmw-mp/Utils.hpp b/apps/openmw-mp/Utils.hpp index 0bd29866c..b511a5b10 100644 --- a/apps/openmw-mp/Utils.hpp +++ b/apps/openmw-mp/Utils.hpp @@ -5,11 +5,15 @@ #ifndef OPENMW_UTILS_HPP #define OPENMW_UTILS_HPP -#include -#include #include +#include #include +#include + +#include +#include + #if (!defined(DEBUG_PRINTF) && defined(DEBUG)) #define DEBUG_PRINTF(...) LOG_MESSAGE_SIMPLE(Log::LOG_VERBOSE, __VA_ARGS__) #else @@ -21,6 +25,8 @@ namespace Utils { const std::vector split(const std::string &str, int delimiter); + ESM::Cell getCellFromDescription(std::string cellDescription); + template constexpr unsigned int hash(const char(&str)[N], size_t I = N) {