diff --git a/.env.example b/.env.example index 462fb17f..5e84184c 100644 --- a/.env.example +++ b/.env.example @@ -1,9 +1,7 @@ # Full path to the LEGO Universe client -CLIENT_PATH=/Users/someuser/LEGO Universe -# Can improve build time -BUILD_THREADS=1 +CLIENT_PATH=./client # Updates NET_VERSION in CMakeVariables.txt -BUILD_VERSION=171022 +NET_VERSION=171022 # make sure this is a long random string # grab a "SHA 256-bit Key" from here: https://keygen.io/ ACCOUNT_MANAGER_SECRET= @@ -12,6 +10,5 @@ EXTERNAL_IP=localhost # Database values # Be careful with special characters here. It is more safe to use normal characters and/or numbers. MARIADB_USER=darkflame -MARIADB_PASSWORD=SECRET_VALUE_CHANGE_ME -MARIADB_ROOT_PASSWORD=SECRET_VALUE_CHANGE_ME +MARIADB_PASSWORD= MARIADB_DATABASE=darkflame diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 8a81def7..fc44885d 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -13,7 +13,7 @@ jobs: continue-on-error: true strategy: matrix: - os: [ windows-2022, ubuntu-20.04, macos-11 ] + os: [ windows-2022, ubuntu-22.04, macos-13 ] steps: - uses: actions/checkout@v3 @@ -25,9 +25,11 @@ jobs: with: vs-version: '[17,18)' msbuild-architecture: x64 - - name: Install libssl (Mac Only) - if: ${{ matrix.os == 'macos-11' }} - run: brew install openssl@3 + - name: Install libssl and switch to XCode 15.2 (Mac Only) + if: ${{ matrix.os == 'macos-13' }} + run: | + brew install openssl@3 + sudo xcode-select -s /Applications/Xcode_15.2.app/Contents/Developer - name: cmake uses: lukka/run-cmake@v10 with: diff --git a/CMakeLists.txt b/CMakeLists.txt index cd657772..e085bfe7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,7 @@ project(Darkflame) include(CTest) set(CMAKE_CXX_STANDARD 20) +set(CXX_STANDARD_REQUIRED ON) set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake") # Read variables from file @@ -51,7 +52,7 @@ set(RECASTNAVIGATION_EXAMPLES OFF CACHE BOOL "" FORCE) # Disabled no-register # Disabled unknown pragmas because Linux doesn't understand Windows pragmas. if(UNIX) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++17 -O2 -Wuninitialized -fPIC") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -Wuninitialized -fPIC") add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0 _GLIBCXX_USE_CXX17_ABI=0) if(NOT APPLE) @@ -244,6 +245,7 @@ set(INCLUDED_DIRECTORIES "thirdparty/SQLite" "thirdparty/cpplinq" "thirdparty/cpp-httplib" + "thirdparty/MD5" "tests" "tests/dCommonTests" @@ -272,7 +274,9 @@ link_directories(${PROJECT_BINARY_DIR}) # Load all of our third party directories add_subdirectory(thirdparty) - +if (UNIX) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror") +endif() # Glob together all headers that need to be precompiled file( GLOB HEADERS_DDATABASE @@ -317,7 +321,7 @@ add_subdirectory(dPhysics) add_subdirectory(dServer) # Create a list of common libraries shared between all binaries -set(COMMON_LIBRARIES "dCommon" "dDatabase" "dNet" "raknet" "mariadbConnCpp" "magic_enum") +set(COMMON_LIBRARIES "dCommon" "dDatabase" "dNet" "raknet" "mariadbConnCpp" "magic_enum" "MD5") # Add platform specific common libraries if(UNIX) diff --git a/CMakePresets.json b/CMakePresets.json index f8170755..2feabc53 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -14,13 +14,13 @@ "generator": "Unix Makefiles" }, { - "name": "ci-ubuntu-20.04", + "name": "ci-ubuntu-22.04", "displayName": "CI configure step for Ubuntu", "description": "Same as default, Used in GitHub actions workflow", "inherits": "default" }, { - "name": "ci-macos-11", + "name": "ci-macos-13", "displayName": "CI configure step for MacOS", "description": "Same as default, Used in GitHub actions workflow", "inherits": "default" @@ -67,15 +67,15 @@ "jobs": 2 }, { - "name": "ci-ubuntu-20.04", - "configurePreset": "ci-ubuntu-20.04", + "name": "ci-ubuntu-22.04", + "configurePreset": "ci-ubuntu-22.04", "displayName": "Linux CI Build", "description": "This preset is used by the CI build on linux", "jobs": 2 }, { - "name": "ci-macos-11", - "configurePreset": "ci-macos-11", + "name": "ci-macos-13", + "configurePreset": "ci-macos-13", "displayName": "MacOS CI Build", "description": "This preset is used by the CI build on MacOS", "jobs": 2 @@ -83,8 +83,8 @@ ], "testPresets": [ { - "name": "ci-ubuntu-20.04", - "configurePreset": "ci-ubuntu-20.04", + "name": "ci-ubuntu-22.04", + "configurePreset": "ci-ubuntu-22.04", "displayName": "CI Tests on Linux", "description": "Runs all tests on a linux configuration", "execution": { @@ -95,8 +95,8 @@ } }, { - "name": "ci-macos-11", - "configurePreset": "ci-macos-11", + "name": "ci-macos-13", + "configurePreset": "ci-macos-13", "displayName": "CI Tests on MacOS", "description": "Runs all tests on a Mac configuration", "execution": { diff --git a/README.md b/README.md index 72bacc95..f0f2ac36 100644 --- a/README.md +++ b/README.md @@ -348,12 +348,44 @@ certutil -hashfile SHA1 Known good *SHA1* checksum of the Darkflame Universe client: - `91498e09b83ce69f46baf9e521d48f23fe502985` (packed client, zip compressed) + # Docker -## Standalone +The Darkflame Server is automatically built and published as a Docker Container / [OCI](https://opencontainers.org/) Image to the GitHub Container Registry at: +[`ghcr.io/darkflameuniverse/darkflameserver`](https://github.com/DarkflameUniverse/DarkflameServer/pkgs/container/darkflameserver). -For standalone deployment, you can use the image provided via Github's Container Repository: -`ghcr.io/darkflameuniverse/darkflameserver` +## Compose + +> [!WARNING] +> It seems that Docker Desktop on Windows with the WSL 2 backend has some issues with MariaDB (c.f. [mariadb-docker#331](https://github.com/MariaDB/mariadb-docker/issues/331)) triggered by NexusDashboard +> migrations, so this setup may not work for you. If that is the case, please tell us about your setup in [NexusDashboard#92](https://github.com/DarkflameUniverse/NexusDashboard/issues/92). + +You can use the `docker-compose` tool to [setup a MariaDB database](#database-setup), run the Darkflame Server and manage it with [Nexus Dashboard](https://github.com/DarkflameUniverse/NexusDashboard) all +at once. For that: + +- [Install Docker Desktop](https://docs.docker.com/get-docker/) +- Open the directory that contains your LU Client + - If the `legouniverse.exe` is in a subfolder called `client`, you're good to go. There may also be a folder `versions`. + - Otherwise, create a new `client` folder and move the exe and everything else (e.g. `res` and `locale`) in there. This is necessary to work around a bug in the client that will prevent that you to log back in after getting disconnected. +- Download the [docker-compose.yml](docker-compose.yml) file and place it next to `client`. +- Download the [.env.example](.env.example) file and place it next to `client` with the file name `.env` + - You may get warnings that this name starts with a dot, acknowledge those, this is intentional. Depending on your operating system, you may need to activate showing hidden files (e.g. Ctrl-H in Gnome on Linux) and/or file extensions ("File name extensions" in the "View" tab on Windows). + - Update the `ACCOUNT_MANAGER_SECRET` and `MARIADB_PASSWORD` with strong random passwords. + - Use a password generator like + - Avoid `:` and `@` characters + - Once the database user is created, changing the password will not update it, so the server will just fail to connect. + - Set `EXTERNAL_IP` to your LAN IP or public IP if you want to host the game for friends & family +- Open a terminal in the folder with the `docker-compose.yml` and `client` +- Run `docker compose up -d` + - This might require `sudo` on Linux, and a recent version of [docker compose](https://docs.docker.com/compose/install/) +- Run `docker exec -it dlu-darkflameserver-1 /app/MasterServer -a` and follow the instructions to create the initial admin account +- Open to access Nexus Dashboard with the admin account to create normal users +- Set `AUTHSERVERIP=0:localhost` in `client/boot.cfg` + - Replace `localhost` with the value of `EXTERNAL_IP` if you changed that earlier. + - Also make sure `UGCUSE3DSERVICES=7:` is set to `0` +- Launch `legouniverse.exe` + +## Standalone This assumes that you have a database deployed to your host or in another docker container. @@ -376,14 +408,6 @@ You will need to replace the `/path/to/`'s to reflect the paths on your host. Any config option in the `.ini`'s can be overridden with environmental variables: Ex: `log_to_console=1` from `shared_config.ini` would be overidden like `-e LOG_TO_CONSOLE=0` -## Compose - -See the [compose](docker-compose.yml) file in the root of the repo. - -This compose file is for a full deployment: MariaDB, DarkflameServer, and Nexus Dashboard. - -All of the environmental options are listed in the compose file so the can be pass through, or you can edit the compose file to suit your specific needs - # Development Documentation This is a Work in Progress, but below are some quick links to documentaion for systems and structs in the server [Networked message structs](https://lcdruniverse.org/lu_packets/lu_packets/index.html) diff --git a/dAuthServer/AuthServer.cpp b/dAuthServer/AuthServer.cpp index 5593f0e1..476e1a68 100644 --- a/dAuthServer/AuthServer.cpp +++ b/dAuthServer/AuthServer.cpp @@ -82,11 +82,11 @@ int main(int argc, char** argv) { Game::randomEngine = std::mt19937(time(0)); //It's safe to pass 'localhost' here, as the IP is only used as the external IP. - uint32_t maxClients = 999; - uint32_t ourPort = 1001; //LU client is hardcoded to use this for auth port, so I'm making it the default. std::string ourIP = "localhost"; - GeneralUtils::TryParse(Game::config->GetValue("max_clients"), maxClients); - GeneralUtils::TryParse(Game::config->GetValue("auth_server_port"), ourPort); + const uint32_t maxClients = GeneralUtils::TryParse(Game::config->GetValue("max_clients")).value_or(999); + + //LU client is hardcoded to use this for auth port, so I'm making it the default. + const uint32_t ourPort = GeneralUtils::TryParse(Game::config->GetValue("auth_server_port")).value_or(1001); const auto externalIPString = Game::config->GetValue("external_ip"); if (!externalIPString.empty()) ourIP = externalIPString; diff --git a/dChatServer/ChatIgnoreList.cpp b/dChatServer/ChatIgnoreList.cpp index 3b5103f0..d77eeeed 100644 --- a/dChatServer/ChatIgnoreList.cpp +++ b/dChatServer/ChatIgnoreList.cpp @@ -2,7 +2,6 @@ #include "PlayerContainer.h" #include "eChatInternalMessageType.h" #include "BitStreamUtils.h" -#include "PacketUtils.h" #include "Game.h" #include "Logger.h" #include "eObjectBits.h" @@ -82,7 +81,7 @@ void ChatIgnoreList::AddIgnore(Packet* packet) { inStream.IgnoreBytes(4); // ignore some garbage zeros idk - LUWString toIgnoreName(33); + LUWString toIgnoreName; inStream.Read(toIgnoreName); std::string toIgnoreStr = toIgnoreName.GetAsString(); @@ -148,7 +147,7 @@ void ChatIgnoreList::RemoveIgnore(Packet* packet) { inStream.IgnoreBytes(4); // ignore some garbage zeros idk - LUWString removedIgnoreName(33); + LUWString removedIgnoreName; inStream.Read(removedIgnoreName); std::string removedIgnoreStr = removedIgnoreName.GetAsString(); diff --git a/dChatServer/ChatPacketHandler.cpp b/dChatServer/ChatPacketHandler.cpp index 4bdddd68..5e2e58d7 100644 --- a/dChatServer/ChatPacketHandler.cpp +++ b/dChatServer/ChatPacketHandler.cpp @@ -2,7 +2,6 @@ #include "PlayerContainer.h" #include "Database.h" #include -#include "PacketUtils.h" #include "BitStreamUtils.h" #include "Game.h" #include "dServer.h" @@ -18,6 +17,8 @@ #include "eChatInternalMessageType.h" #include "eClientMessageType.h" #include "eGameMessageType.h" +#include "StringifiedEnum.h" +#include "eGameMasterLevel.h" void ChatPacketHandler::HandleFriendlistRequest(Packet* packet) { //Get from the packet which player we want to do something with: @@ -78,31 +79,27 @@ void ChatPacketHandler::HandleFriendlistRequest(Packet* packet) { void ChatPacketHandler::HandleFriendRequest(Packet* packet) { CINSTREAM_SKIP_HEADER; + LWOOBJID requestorPlayerID; - inStream.Read(requestorPlayerID); - uint32_t spacing{}; - inStream.Read(spacing); - std::string playerName = ""; - uint16_t character; - bool noMoreLettersInName = false; - - for (uint32_t j = 0; j < 33; j++) { - inStream.Read(character); - if (character == '\0') noMoreLettersInName = true; - if (!noMoreLettersInName) playerName.push_back(static_cast(character)); - } - + LUWString LUplayerName; char isBestFriendRequest{}; + + inStream.Read(requestorPlayerID); + inStream.IgnoreBytes(4); + inStream.Read(LUplayerName); inStream.Read(isBestFriendRequest); + auto playerName = LUplayerName.GetAsString(); + auto& requestor = Game::playerContainer.GetPlayerDataMutable(requestorPlayerID); if (!requestor) { LOG("No requestor player %llu sent to %s found.", requestorPlayerID, playerName.c_str()); return; } + // you cannot friend yourself if (requestor.playerName == playerName) { - SendFriendResponse(requestor, requestor, eAddFriendResponseType::MYTHRAN); + SendFriendResponse(requestor, requestor, eAddFriendResponseType::GENERALERROR); return; }; @@ -141,6 +138,13 @@ void ChatPacketHandler::HandleFriendRequest(Packet* packet) { return; } + // Prevent GM friend spam + // If the player we are trying to be friends with is not a civilian and we are a civilian, abort the process + if (requestee.gmLevel > eGameMasterLevel::CIVILIAN && requestor.gmLevel == eGameMasterLevel::CIVILIAN ) { + SendFriendResponse(requestor, requestee, eAddFriendResponseType::MYTHRAN); + return; + } + if (isBestFriendRequest) { uint8_t oldBestFriendStatus{}; @@ -218,15 +222,19 @@ void ChatPacketHandler::HandleFriendRequest(Packet* packet) { void ChatPacketHandler::HandleFriendResponse(Packet* packet) { CINSTREAM_SKIP_HEADER; - LWOOBJID playerID; - inStream.Read(playerID); - eAddFriendResponseCode clientResponseCode = static_cast(packet->data[0x14]); - std::string friendName = PacketUtils::ReadString(0x15, packet, true); + LWOOBJID playerID; + eAddFriendResponseCode clientResponseCode; + LUWString friendName; + + inStream.Read(playerID); + inStream.IgnoreBytes(4); + inStream.Read(clientResponseCode); + inStream.Read(friendName); //Now to try and find both of these: auto& requestor = Game::playerContainer.GetPlayerDataMutable(playerID); - auto& requestee = Game::playerContainer.GetPlayerDataMutable(friendName); + auto& requestee = Game::playerContainer.GetPlayerDataMutable(friendName.GetAsString()); if (!requestor || !requestee) return; eAddFriendResponseType serverResponseCode{}; @@ -288,8 +296,11 @@ void ChatPacketHandler::HandleFriendResponse(Packet* packet) { void ChatPacketHandler::HandleRemoveFriend(Packet* packet) { CINSTREAM_SKIP_HEADER; LWOOBJID playerID; + LUWString LUFriendName; inStream.Read(playerID); - std::string friendName = PacketUtils::ReadString(0x14, packet, true); + inStream.IgnoreBytes(4); + inStream.Read(LUFriendName); + auto friendName = LUFriendName.GetAsString(); //we'll have to query the db here to find the user, since you can delete them while they're offline. //First, we need to find their ID: @@ -335,123 +346,144 @@ void ChatPacketHandler::HandleRemoveFriend(Packet* packet) { SendRemoveFriend(goonB, goonAName, true); } -void ChatPacketHandler::HandleChatMessage(Packet* packet) { - CINSTREAM_SKIP_HEADER; - LWOOBJID playerID = LWOOBJID_EMPTY; - inStream.Read(playerID); - - const auto& sender = Game::playerContainer.GetPlayerData(playerID); - - if (!sender) return; - - if (sender.GetIsMuted()) return; - - inStream.SetReadOffset(0x14 * 8); - - uint8_t channel = 0; - inStream.Read(channel); - - std::string message = PacketUtils::ReadString(0x66, packet, true, 512); - - LOG("Got a message from (%s) [%d]: %s", sender.playerName.c_str(), channel, message.c_str()); - - if (channel != 8) return; - - auto* team = Game::playerContainer.GetTeam(playerID); - - if (team == nullptr) return; - - for (const auto memberId : team->memberIDs) { - const auto& otherMember = Game::playerContainer.GetPlayerData(memberId); - - if (!otherMember) return; - - CBITSTREAM; - BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT_INTERNAL, eChatInternalMessageType::ROUTE_TO_PLAYER); - bitStream.Write(otherMember.playerID); - - BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, eChatMessageType::PRIVATE_CHAT_MESSAGE); - bitStream.Write(otherMember.playerID); - bitStream.Write(8); - bitStream.Write(69); - bitStream.Write(LUWString(sender.playerName)); - bitStream.Write(sender.playerID); - bitStream.Write(0); - bitStream.Write(0); //not mythran nametag - bitStream.Write(LUWString(otherMember.playerName)); - bitStream.Write(0); //not mythran for receiver - bitStream.Write(0); //teams? - bitStream.Write(LUWString(message, 512)); - - SystemAddress sysAddr = otherMember.sysAddr; - SEND_PACKET; - } -} - -void ChatPacketHandler::HandlePrivateChatMessage(Packet* packet) { - LWOOBJID senderID = PacketUtils::ReadS64(0x08, packet); - std::string receiverName = PacketUtils::ReadString(0x66, packet, true); - std::string message = PacketUtils::ReadString(0xAA, packet, true, 512); - - //Get the bois: - const auto& goonA = Game::playerContainer.GetPlayerData(senderID); - const auto& goonB = Game::playerContainer.GetPlayerData(receiverName); - if (!goonA || !goonB) return; - - if (goonA.GetIsMuted()) return; - - //To the sender: - { - CBITSTREAM; - BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT_INTERNAL, eChatInternalMessageType::ROUTE_TO_PLAYER); - bitStream.Write(goonA.playerID); - - BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, eChatMessageType::PRIVATE_CHAT_MESSAGE); - bitStream.Write(goonA.playerID); - bitStream.Write(7); - bitStream.Write(69); - bitStream.Write(LUWString(goonA.playerName)); - bitStream.Write(goonA.playerID); - bitStream.Write(0); - bitStream.Write(0); //not mythran nametag - bitStream.Write(LUWString(goonB.playerName)); - bitStream.Write(0); //not mythran for receiver - bitStream.Write(0); //success - bitStream.Write(LUWString(message, 512)); - - SystemAddress sysAddr = goonA.sysAddr; - SEND_PACKET; - } - - //To the receiver: - { - CBITSTREAM; - BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT_INTERNAL, eChatInternalMessageType::ROUTE_TO_PLAYER); - bitStream.Write(goonB.playerID); - - BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, eChatMessageType::PRIVATE_CHAT_MESSAGE); - bitStream.Write(goonA.playerID); - bitStream.Write(7); - bitStream.Write(69); - bitStream.Write(LUWString(goonA.playerName)); - bitStream.Write(goonA.playerID); - bitStream.Write(0); - bitStream.Write(0); //not mythran nametag - bitStream.Write(LUWString(goonB.playerName)); - bitStream.Write(0); //not mythran for receiver - bitStream.Write(3); //new whisper - bitStream.Write(LUWString(message, 512)); - - SystemAddress sysAddr = goonB.sysAddr; - SEND_PACKET; - } -} - -void ChatPacketHandler::HandleTeamInvite(Packet* packet) { +void ChatPacketHandler::HandleGMLevelUpdate(Packet* packet) { CINSTREAM_SKIP_HEADER; LWOOBJID playerID; inStream.Read(playerID); - std::string invitedPlayer = PacketUtils::ReadString(0x14, packet, true); + auto& player = Game::playerContainer.GetPlayerData(playerID); + if (!player) return; + inStream.Read(player.gmLevel); +} + +// the structure the client uses to send this packet is shared in many chat messages +// that are sent to the server. Because of this, there are large gaps of unused data in chat messages +void ChatPacketHandler::HandleChatMessage(Packet* packet) { + CINSTREAM_SKIP_HEADER; + LWOOBJID playerID; + inStream.Read(playerID); + + const auto& sender = Game::playerContainer.GetPlayerData(playerID); + if (!sender || sender.GetIsMuted()) return; + + eChatChannel channel; + uint32_t size; + + inStream.IgnoreBytes(4); + inStream.Read(channel); + inStream.Read(size); + inStream.IgnoreBytes(77); + + LUWString message(size); + inStream.Read(message); + + LOG("Got a message from (%s) via [%s]: %s", sender.playerName.c_str(), StringifiedEnum::ToString(channel).data(), message.GetAsString().c_str()); + + switch (channel) { + case eChatChannel::TEAM: { + auto* team = Game::playerContainer.GetTeam(playerID); + if (team == nullptr) return; + + for (const auto memberId : team->memberIDs) { + const auto& otherMember = Game::playerContainer.GetPlayerData(memberId); + if (!otherMember) return; + SendPrivateChatMessage(sender, otherMember, otherMember, message, eChatChannel::TEAM, eChatMessageResponseCode::SENT); + } + break; + } + default: + LOG("Unhandled Chat channel [%s]", StringifiedEnum::ToString(channel).data()); + break; + } +} + +// the structure the client uses to send this packet is shared in many chat messages +// that are sent to the server. Because of this, there are large gaps of unused data in chat messages +void ChatPacketHandler::HandlePrivateChatMessage(Packet* packet) { + CINSTREAM_SKIP_HEADER; + LWOOBJID playerID; + inStream.Read(playerID); + + const auto& sender = Game::playerContainer.GetPlayerData(playerID); + if (!sender || sender.GetIsMuted()) return; + + eChatChannel channel; + uint32_t size; + LUWString LUReceiverName; + + inStream.IgnoreBytes(4); + inStream.Read(channel); + if (channel != eChatChannel::PRIVATE_CHAT) LOG("WARNING: Received Private chat with the wrong channel!"); + + inStream.Read(size); + inStream.IgnoreBytes(77); + + inStream.Read(LUReceiverName); + auto receiverName = LUReceiverName.GetAsString(); + inStream.IgnoreBytes(2); + + LUWString message(size); + inStream.Read(message); + + LOG("Got a message from (%s) via [%s]: %s to %s", sender.playerName.c_str(), StringifiedEnum::ToString(channel).data(), message.GetAsString().c_str(), receiverName.c_str()); + + const auto& receiver = Game::playerContainer.GetPlayerData(receiverName); + if (!receiver) { + PlayerData otherPlayer; + otherPlayer.playerName = receiverName; + auto responseType = Database::Get()->GetCharacterInfo(receiverName) + ? eChatMessageResponseCode::NOTONLINE + : eChatMessageResponseCode::GENERALERROR; + + SendPrivateChatMessage(sender, otherPlayer, sender, message, eChatChannel::GENERAL, responseType); + return; + } + + // Check to see if they are friends + // only freinds can whispr each other + for (const auto& fr : receiver.friends) { + if (fr.friendID == sender.playerID) { + //To the sender: + SendPrivateChatMessage(sender, receiver, sender, message, eChatChannel::PRIVATE_CHAT, eChatMessageResponseCode::SENT); + //To the receiver: + SendPrivateChatMessage(sender, receiver, receiver, message, eChatChannel::PRIVATE_CHAT, eChatMessageResponseCode::RECEIVEDNEWWHISPER); + return; + } + } + SendPrivateChatMessage(sender, receiver, sender, message, eChatChannel::GENERAL, eChatMessageResponseCode::NOTFRIENDS); +} + +void ChatPacketHandler::SendPrivateChatMessage(const PlayerData& sender, const PlayerData& receiver, const PlayerData& routeTo, const LUWString& message, const eChatChannel channel, const eChatMessageResponseCode responseCode) { + CBITSTREAM; + BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT_INTERNAL, eChatInternalMessageType::ROUTE_TO_PLAYER); + bitStream.Write(routeTo.playerID); + + BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, eChatMessageType::PRIVATE_CHAT_MESSAGE); + bitStream.Write(sender.playerID); + bitStream.Write(channel); + bitStream.Write(0); // not used + bitStream.Write(LUWString(sender.playerName)); + bitStream.Write(sender.playerID); + bitStream.Write(0); // sourceID + bitStream.Write(sender.gmLevel); + bitStream.Write(LUWString(receiver.playerName)); + bitStream.Write(receiver.gmLevel); + bitStream.Write(responseCode); + bitStream.Write(message); + + SystemAddress sysAddr = routeTo.sysAddr; + SEND_PACKET; +} + + +void ChatPacketHandler::HandleTeamInvite(Packet* packet) { + CINSTREAM_SKIP_HEADER; + + LWOOBJID playerID; + LUWString invitedPlayer; + + inStream.Read(playerID); + inStream.IgnoreBytes(4); + inStream.Read(invitedPlayer); const auto& player = Game::playerContainer.GetPlayerData(playerID); @@ -463,7 +495,7 @@ void ChatPacketHandler::HandleTeamInvite(Packet* packet) { team = Game::playerContainer.CreateTeam(playerID); } - const auto& other = Game::playerContainer.GetPlayerData(invitedPlayer); + const auto& other = Game::playerContainer.GetPlayerData(invitedPlayer.GetAsString()); if (!other) return; @@ -480,7 +512,7 @@ void ChatPacketHandler::HandleTeamInvite(Packet* packet) { SendTeamInvite(other, player); - LOG("Got team invite: %llu -> %s", playerID, invitedPlayer.c_str()); + LOG("Got team invite: %llu -> %s", playerID, invitedPlayer.GetAsString().c_str()); } void ChatPacketHandler::HandleTeamInviteResponse(Packet* packet) { @@ -534,21 +566,25 @@ void ChatPacketHandler::HandleTeamLeave(Packet* packet) { void ChatPacketHandler::HandleTeamKick(Packet* packet) { CINSTREAM_SKIP_HEADER; + LWOOBJID playerID = LWOOBJID_EMPTY; + LUWString kickedPlayer; + inStream.Read(playerID); + inStream.IgnoreBytes(4); + inStream.Read(kickedPlayer); - std::string kickedPlayer = PacketUtils::ReadString(0x14, packet, true); - LOG("(%llu) kicking (%s) from team", playerID, kickedPlayer.c_str()); + LOG("(%llu) kicking (%s) from team", playerID, kickedPlayer.GetAsString().c_str()); - const auto& kicked = Game::playerContainer.GetPlayerData(kickedPlayer); + const auto& kicked = Game::playerContainer.GetPlayerData(kickedPlayer.GetAsString()); LWOOBJID kickedId = LWOOBJID_EMPTY; if (kicked) { kickedId = kicked.playerID; } else { - kickedId = Game::playerContainer.GetId(GeneralUtils::UTF8ToUTF16(kickedPlayer)); + kickedId = Game::playerContainer.GetId(kickedPlayer.string); } if (kickedId == LWOOBJID_EMPTY) return; @@ -564,14 +600,17 @@ void ChatPacketHandler::HandleTeamKick(Packet* packet) { void ChatPacketHandler::HandleTeamPromote(Packet* packet) { CINSTREAM_SKIP_HEADER; + LWOOBJID playerID = LWOOBJID_EMPTY; + LUWString promotedPlayer; + inStream.Read(playerID); + inStream.IgnoreBytes(4); + inStream.Read(promotedPlayer); - std::string promotedPlayer = PacketUtils::ReadString(0x14, packet, true); + LOG("(%llu) promoting (%s) to team leader", playerID, promotedPlayer.GetAsString().c_str()); - LOG("(%llu) promoting (%s) to team leader", playerID, promotedPlayer.c_str()); - - const auto& promoted = Game::playerContainer.GetPlayerData(promotedPlayer); + const auto& promoted = Game::playerContainer.GetPlayerData(promotedPlayer.GetAsString()); if (!promoted) return; diff --git a/dChatServer/ChatPacketHandler.h b/dChatServer/ChatPacketHandler.h index 6c9c2de6..847fc899 100644 --- a/dChatServer/ChatPacketHandler.h +++ b/dChatServer/ChatPacketHandler.h @@ -7,14 +7,53 @@ struct PlayerData; enum class eAddFriendResponseType : uint8_t; +enum class eChatChannel : uint8_t { + SYSTEMNOTIFY = 0, + SYSTEMWARNING, + SYSTEMERROR, + BROADCAST, + LOCAL, + LOCALNOANIM, + EMOTE, + PRIVATE_CHAT, + TEAM, + TEAMLOCAL, + GUILD, + GUILDNOTIFY, + PROPERTY, + ADMIN, + COMBATDAMAGE, + COMBATHEALING, + COMBATLOOT, + COMBATEXP, + COMBATDEATH, + GENERAL, + TRADE, + LFG, + USER +}; + + +enum class eChatMessageResponseCode : uint8_t { + SENT = 0, + NOTONLINE, + GENERALERROR, + RECEIVEDNEWWHISPER, + NOTFRIENDS, + SENDERFREETRIAL, + RECEIVERFREETRIAL, +}; + namespace ChatPacketHandler { void HandleFriendlistRequest(Packet* packet); void HandleFriendRequest(Packet* packet); void HandleFriendResponse(Packet* packet); void HandleRemoveFriend(Packet* packet); + void HandleGMLevelUpdate(Packet* packet); void HandleChatMessage(Packet* packet); void HandlePrivateChatMessage(Packet* packet); + void SendPrivateChatMessage(const PlayerData& sender, const PlayerData& receiver, const PlayerData& routeTo, const LUWString& message, const eChatChannel channel, const eChatMessageResponseCode responseCode); void HandleTeamInvite(Packet* packet); void HandleTeamInviteResponse(Packet* packet); diff --git a/dChatServer/ChatServer.cpp b/dChatServer/ChatServer.cpp index 8ab66d73..44957042 100644 --- a/dChatServer/ChatServer.cpp +++ b/dChatServer/ChatServer.cpp @@ -20,6 +20,7 @@ #include "eChatInternalMessageType.h" #include "eWorldMessageType.h" #include "ChatIgnoreList.h" +#include "StringifiedEnum.h" #include "Game.h" #include "Server.h" @@ -98,18 +99,15 @@ int main(int argc, char** argv) { masterPort = masterInfo->port; } //It's safe to pass 'localhost' here, as the IP is only used as the external IP. - uint32_t maxClients = 999; - uint32_t ourPort = 1501; std::string ourIP = "localhost"; - GeneralUtils::TryParse(Game::config->GetValue("max_clients"), maxClients); - GeneralUtils::TryParse(Game::config->GetValue("chat_server_port"), ourPort); + const uint32_t maxClients = GeneralUtils::TryParse(Game::config->GetValue("max_clients")).value_or(999); + const uint32_t ourPort = GeneralUtils::TryParse(Game::config->GetValue("chat_server_port")).value_or(1501); const auto externalIPString = Game::config->GetValue("external_ip"); if (!externalIPString.empty()) ourIP = externalIPString; Game::server = new dServer(ourIP, ourPort, 0, maxClients, false, true, Game::logger, masterIP, masterPort, ServerType::Chat, Game::config, &Game::lastSignal); - bool dontGenerateDCF = false; - GeneralUtils::TryParse(Game::config->GetValue("dont_generate_dcf"), dontGenerateDCF); + const bool dontGenerateDCF = GeneralUtils::TryParse(Game::config->GetValue("dont_generate_dcf")).value_or(false); Game::chatFilter = new dChatFilter(Game::assetManager->GetResPath().string() + "/chatplus_en_us", dontGenerateDCF); Game::randomEngine = std::mt19937(time(0)); @@ -223,7 +221,8 @@ void HandlePacket(Packet* packet) { } if (static_cast(packet->data[1]) == eConnectionType::CHAT) { - switch (static_cast(packet->data[3])) { + eChatMessageType chat_message_type = static_cast(packet->data[3]); + switch (chat_message_type) { case eChatMessageType::GET_FRIENDS_LIST: ChatPacketHandler::HandleFriendlistRequest(packet); break; @@ -293,9 +292,61 @@ void HandlePacket(Packet* packet) { case eChatMessageType::TEAM_SET_LOOT: ChatPacketHandler::HandleTeamLootOption(packet); break; - + case eChatMessageType::GMLEVEL_UPDATE: + ChatPacketHandler::HandleGMLevelUpdate(packet); + break; + case eChatMessageType::LOGIN_SESSION_NOTIFY: + case eChatMessageType::USER_CHANNEL_CHAT_MESSAGE: + case eChatMessageType::WORLD_DISCONNECT_REQUEST: + case eChatMessageType::WORLD_PROXIMITY_RESPONSE: + case eChatMessageType::WORLD_PARCEL_RESPONSE: + case eChatMessageType::TEAM_MISSED_INVITE_CHECK: + case eChatMessageType::GUILD_CREATE: + case eChatMessageType::GUILD_INVITE: + case eChatMessageType::GUILD_INVITE_RESPONSE: + case eChatMessageType::GUILD_LEAVE: + case eChatMessageType::GUILD_KICK: + case eChatMessageType::GUILD_GET_STATUS: + case eChatMessageType::GUILD_GET_ALL: + case eChatMessageType::SHOW_ALL: + case eChatMessageType::BLUEPRINT_MODERATED: + case eChatMessageType::BLUEPRINT_MODEL_READY: + case eChatMessageType::PROPERTY_READY_FOR_APPROVAL: + case eChatMessageType::PROPERTY_MODERATION_CHANGED: + case eChatMessageType::PROPERTY_BUILDMODE_CHANGED: + case eChatMessageType::PROPERTY_BUILDMODE_CHANGED_REPORT: + case eChatMessageType::MAIL: + case eChatMessageType::WORLD_INSTANCE_LOCATION_REQUEST: + case eChatMessageType::REPUTATION_UPDATE: + case eChatMessageType::SEND_CANNED_TEXT: + case eChatMessageType::CHARACTER_NAME_CHANGE_REQUEST: + case eChatMessageType::CSR_REQUEST: + case eChatMessageType::CSR_REPLY: + case eChatMessageType::GM_KICK: + case eChatMessageType::GM_ANNOUNCE: + case eChatMessageType::WORLD_ROUTE_PACKET: + case eChatMessageType::GET_ZONE_POPULATIONS: + case eChatMessageType::REQUEST_MINIMUM_CHAT_MODE: + case eChatMessageType::MATCH_REQUEST: + case eChatMessageType::UGCMANIFEST_REPORT_MISSING_FILE: + case eChatMessageType::UGCMANIFEST_REPORT_DONE_FILE: + case eChatMessageType::UGCMANIFEST_REPORT_DONE_BLUEPRINT: + case eChatMessageType::UGCC_REQUEST: + case eChatMessageType::WHO: + case eChatMessageType::WORLD_PLAYERS_PET_MODERATED_ACKNOWLEDGE: + case eChatMessageType::ACHIEVEMENT_NOTIFY: + case eChatMessageType::GM_CLOSE_PRIVATE_CHAT_WINDOW: + case eChatMessageType::UNEXPECTED_DISCONNECT: + case eChatMessageType::PLAYER_READY: + case eChatMessageType::GET_DONATION_TOTAL: + case eChatMessageType::UPDATE_DONATION: + case eChatMessageType::PRG_CSR_COMMAND: + case eChatMessageType::HEARTBEAT_REQUEST_FROM_WORLD: + case eChatMessageType::UPDATE_FREE_TRIAL_STATUS: + LOG("Unhandled CHAT Message id: %s (%i)", StringifiedEnum::ToString(chat_message_type).data(), chat_message_type); + break; default: - LOG("Unknown CHAT id: %i", int(packet->data[3])); + LOG("Unknown CHAT Message id: %i", chat_message_type); } } diff --git a/dChatServer/PlayerContainer.cpp b/dChatServer/PlayerContainer.cpp index 242ccad1..dbbaeb9e 100644 --- a/dChatServer/PlayerContainer.cpp +++ b/dChatServer/PlayerContainer.cpp @@ -14,12 +14,10 @@ #include "dConfig.h" void PlayerContainer::Initialize() { - GeneralUtils::TryParse(Game::config->GetValue("max_number_of_best_friends"), m_MaxNumberOfBestFriends); - GeneralUtils::TryParse(Game::config->GetValue("max_number_of_friends"), m_MaxNumberOfFriends); -} - -PlayerContainer::~PlayerContainer() { - m_Players.clear(); + m_MaxNumberOfBestFriends = + GeneralUtils::TryParse(Game::config->GetValue("max_number_of_best_friends")).value_or(m_MaxNumberOfBestFriends); + m_MaxNumberOfFriends = + GeneralUtils::TryParse(Game::config->GetValue("max_number_of_friends")).value_or(m_MaxNumberOfFriends); } TeamData::TeamData() { @@ -47,6 +45,7 @@ void PlayerContainer::InsertPlayer(Packet* packet) { inStream.Read(data.zoneID); inStream.Read(data.muteExpire); + inStream.Read(data.gmLevel); data.sysAddr = packet->systemAddress; m_Names[data.playerID] = GeneralUtils::UTF8ToUTF16(data.playerName); diff --git a/dChatServer/PlayerContainer.h b/dChatServer/PlayerContainer.h index 3cacc62d..3f2d783a 100644 --- a/dChatServer/PlayerContainer.h +++ b/dChatServer/PlayerContainer.h @@ -7,8 +7,10 @@ #include "dServer.h" #include +enum class eGameMasterLevel : uint8_t; + struct IgnoreData { - IgnoreData(const std::string& name, const LWOOBJID& id) : playerName(name), playerId(id) {} + IgnoreData(const std::string& name, const LWOOBJID& id) : playerName{ name }, playerId{ id } {} inline bool operator==(const std::string& other) const noexcept { return playerName == other; } @@ -42,6 +44,8 @@ struct PlayerData { std::string playerName; std::vector friends; std::vector ignoredPlayers; + eGameMasterLevel gmLevel = static_cast(0); // CIVILLIAN + bool isFTP = false; }; struct TeamData { @@ -56,8 +60,6 @@ struct TeamData { class PlayerContainer { public: - ~PlayerContainer(); - void Initialize(); void InsertPlayer(Packet* packet); void RemovePlayer(Packet* packet); diff --git a/dCommon/Amf3.h b/dCommon/Amf3.h index 967313c7..dbafba1f 100644 --- a/dCommon/Amf3.h +++ b/dCommon/Amf3.h @@ -31,54 +31,68 @@ enum class eAmf : uint8_t { class AMFBaseValue { public: - virtual eAmf GetValueType() { return eAmf::Undefined; }; - AMFBaseValue() {}; - virtual ~AMFBaseValue() {}; + [[nodiscard]] constexpr virtual eAmf GetValueType() const noexcept { return eAmf::Undefined; } + constexpr AMFBaseValue() noexcept = default; + constexpr virtual ~AMFBaseValue() noexcept = default; }; -template +// AMFValue template class instantiations +template class AMFValue : public AMFBaseValue { public: - AMFValue() {}; - AMFValue(ValueType value) { SetValue(value); }; - virtual ~AMFValue() override {}; + AMFValue() = default; + AMFValue(const ValueType value) { m_Data = value; } + virtual ~AMFValue() override = default; - eAmf GetValueType() override { return eAmf::Undefined; }; + [[nodiscard]] constexpr eAmf GetValueType() const noexcept override; + + [[nodiscard]] const ValueType& GetValue() const { return m_Data; } + void SetValue(const ValueType value) { m_Data = value; } - const ValueType& GetValue() { return data; }; - void SetValue(ValueType value) { data = value; }; protected: - ValueType data; + ValueType m_Data; }; +// Explicit template class instantiations +template class AMFValue; +template class AMFValue; +template class AMFValue; +template class AMFValue; +template class AMFValue; +template class AMFValue; + +// AMFValue template class member function instantiations +template <> [[nodiscard]] constexpr eAmf AMFValue::GetValueType() const noexcept { return eAmf::Null; } +template <> [[nodiscard]] constexpr eAmf AMFValue::GetValueType() const noexcept { return m_Data ? eAmf::True : eAmf::False; } +template <> [[nodiscard]] constexpr eAmf AMFValue::GetValueType() const noexcept { return eAmf::Integer; } +template <> [[nodiscard]] constexpr eAmf AMFValue::GetValueType() const noexcept { return eAmf::Integer; } +template <> [[nodiscard]] constexpr eAmf AMFValue::GetValueType() const noexcept { return eAmf::String; } +template <> [[nodiscard]] constexpr eAmf AMFValue::GetValueType() const noexcept { return eAmf::Double; } + +template +[[nodiscard]] constexpr eAmf AMFValue::GetValueType() const noexcept { return eAmf::Undefined; } + // As a string this is much easier to write and read from a BitStream. -template<> +template <> class AMFValue : public AMFBaseValue { public: - AMFValue() {}; - AMFValue(const char* value) { SetValue(std::string(value)); }; - virtual ~AMFValue() override {}; + AMFValue() = default; + AMFValue(const char* value) { m_Data = value; } + virtual ~AMFValue() override = default; - eAmf GetValueType() override { return eAmf::String; }; + [[nodiscard]] constexpr eAmf GetValueType() const noexcept override { return eAmf::String; } - const std::string& GetValue() { return data; }; - void SetValue(std::string value) { data = value; }; + [[nodiscard]] const std::string& GetValue() const { return m_Data; } + void SetValue(const std::string& value) { m_Data = value; } protected: - std::string data; + std::string m_Data; }; -typedef AMFValue AMFNullValue; -typedef AMFValue AMFBoolValue; -typedef AMFValue AMFIntValue; -typedef AMFValue AMFStringValue; -typedef AMFValue AMFDoubleValue; - -template<> inline eAmf AMFValue::GetValueType() { return eAmf::Null; }; -template<> inline eAmf AMFValue::GetValueType() { return this->data ? eAmf::True : eAmf::False; }; -template<> inline eAmf AMFValue::GetValueType() { return eAmf::Integer; }; -template<> inline eAmf AMFValue::GetValueType() { return eAmf::Integer; }; -template<> inline eAmf AMFValue::GetValueType() { return eAmf::String; }; -template<> inline eAmf AMFValue::GetValueType() { return eAmf::Double; }; +using AMFNullValue = AMFValue; +using AMFBoolValue = AMFValue; +using AMFIntValue = AMFValue; +using AMFStringValue = AMFValue; +using AMFDoubleValue = AMFValue; /** * The AMFArrayValue object holds 2 types of lists: @@ -89,12 +103,11 @@ template<> inline eAmf AMFValue::GetValueType() { return eAmf::Double; } * and are not to be deleted by a caller. */ class AMFArrayValue : public AMFBaseValue { - - typedef std::unordered_map AMFAssociative; - typedef std::vector AMFDense; + using AMFAssociative = std::unordered_map; + using AMFDense = std::vector; public: - eAmf GetValueType() override { return eAmf::Array; }; + [[nodiscard]] constexpr eAmf GetValueType() const noexcept override { return eAmf::Array; } ~AMFArrayValue() override { for (auto valueToDelete : GetDense()) { @@ -109,17 +122,17 @@ public: valueToDelete.second = nullptr; } } - }; + } /** * Returns the Associative portion of the object */ - inline AMFAssociative& GetAssociative() { return this->associative; }; + [[nodiscard]] inline AMFAssociative& GetAssociative() noexcept { return this->associative; } /** * Returns the dense portion of the object */ - inline AMFDense& GetDense() { return this->dense; }; + [[nodiscard]] inline AMFDense& GetDense() noexcept { return this->dense; } /** * Inserts an AMFValue into the associative portion with the given key. @@ -135,8 +148,8 @@ public: * @return The inserted element if the type matched, * or nullptr if a key existed and was not the same type */ - template - std::pair*, bool> Insert(const std::string& key, ValueType value) { + template + [[maybe_unused]] std::pair*, bool> Insert(const std::string& key, const ValueType value) { auto element = associative.find(key); AMFValue* val = nullptr; bool found = true; @@ -148,10 +161,10 @@ public: found = false; } return std::make_pair(val, found); - }; + } // Associates an array with a string key - std::pair Insert(const std::string& key) { + [[maybe_unused]] std::pair Insert(const std::string& key) { auto element = associative.find(key); AMFArrayValue* val = nullptr; bool found = true; @@ -163,10 +176,10 @@ public: found = false; } return std::make_pair(val, found); - }; + } // Associates an array with an integer key - std::pair Insert(const uint32_t& index) { + [[maybe_unused]] std::pair Insert(const size_t index) { AMFArrayValue* val = nullptr; bool inserted = false; if (index >= dense.size()) { @@ -176,7 +189,7 @@ public: inserted = true; } return std::make_pair(dynamic_cast(dense.at(index)), inserted); - }; + } /** * @brief Inserts an AMFValue into the AMFArray key'd by index. @@ -188,8 +201,8 @@ public: * @return The inserted element, or nullptr if the type did not match * what was at the index. */ - template - std::pair*, bool> Insert(const uint32_t& index, ValueType value) { + template + [[maybe_unused]] std::pair*, bool> Insert(const size_t index, const ValueType value) { AMFValue* val = nullptr; bool inserted = false; if (index >= this->dense.size()) { @@ -199,7 +212,7 @@ public: inserted = true; } return std::make_pair(dynamic_cast*>(this->dense.at(index)), inserted); - }; + } /** * Inserts an AMFValue into the associative portion with the given key. @@ -210,7 +223,7 @@ public: * @param key The key to associate with the value * @param value The value to insert */ - void Insert(const std::string& key, AMFBaseValue* value) { + void Insert(const std::string& key, AMFBaseValue* const value) { auto element = associative.find(key); if (element != associative.end() && element->second) { delete element->second; @@ -218,7 +231,7 @@ public: } else { associative.insert(std::make_pair(key, value)); } - }; + } /** * Inserts an AMFValue into the associative portion with the given index. @@ -229,7 +242,7 @@ public: * @param key The key to associate with the value * @param value The value to insert */ - void Insert(const uint32_t index, AMFBaseValue* value) { + void Insert(const size_t index, AMFBaseValue* const value) { if (index < dense.size()) { AMFDense::iterator itr = dense.begin() + index; if (*itr) delete dense.at(index); @@ -237,7 +250,7 @@ public: dense.resize(index + 1); } dense.at(index) = value; - }; + } /** * Pushes an AMFValue into the back of the dense portion. @@ -249,10 +262,10 @@ public: * * @return The inserted pointer, or nullptr should the key already be in use. */ - template - inline AMFValue* Push(ValueType value) { + template + [[maybe_unused]] inline AMFValue* Push(const ValueType value) { return Insert(this->dense.size(), value).first; - }; + } /** * Removes the key from the associative portion @@ -261,7 +274,7 @@ public: * * @param key The key to remove from the associative portion */ - void Remove(const std::string& key, bool deleteValue = true) { + void Remove(const std::string& key, const bool deleteValue = true) { AMFAssociative::iterator it = this->associative.find(key); if (it != this->associative.end()) { if (deleteValue) delete it->second; @@ -272,7 +285,7 @@ public: /** * Pops the last element in the dense portion, deleting it in the process. */ - void Remove(const uint32_t index) { + void Remove(const size_t index) { if (!this->dense.empty() && index < this->dense.size()) { auto itr = this->dense.begin() + index; if (*itr) delete (*itr); @@ -284,29 +297,29 @@ public: if (!this->dense.empty()) Remove(this->dense.size() - 1); } - AMFArrayValue* GetArray(const std::string& key) { + [[nodiscard]] AMFArrayValue* GetArray(const std::string& key) { AMFAssociative::const_iterator it = this->associative.find(key); if (it != this->associative.end()) { return dynamic_cast(it->second); } return nullptr; - }; + } - AMFArrayValue* GetArray(const uint32_t index) { + [[nodiscard]] AMFArrayValue* GetArray(const size_t index) { return index >= this->dense.size() ? nullptr : dynamic_cast(this->dense.at(index)); - }; + } - inline AMFArrayValue* InsertArray(const std::string& key) { + [[maybe_unused]] inline AMFArrayValue* InsertArray(const std::string& key) { return static_cast(Insert(key).first); - }; + } - inline AMFArrayValue* InsertArray(const uint32_t index) { + [[maybe_unused]] inline AMFArrayValue* InsertArray(const size_t index) { return static_cast(Insert(index).first); - }; + } - inline AMFArrayValue* PushArray() { + [[maybe_unused]] inline AMFArrayValue* PushArray() { return static_cast(Insert(this->dense.size()).first); - }; + } /** * Gets an AMFValue by the key from the associative portion and converts it @@ -318,18 +331,18 @@ public: * @return The AMFValue */ template - AMFValue* Get(const std::string& key) const { + [[nodiscard]] AMFValue* Get(const std::string& key) const { AMFAssociative::const_iterator it = this->associative.find(key); return it != this->associative.end() ? dynamic_cast*>(it->second) : nullptr; - }; + } // Get from the array but dont cast it - AMFBaseValue* Get(const std::string& key) const { + [[nodiscard]] AMFBaseValue* Get(const std::string& key) const { AMFAssociative::const_iterator it = this->associative.find(key); return it != this->associative.end() ? it->second : nullptr; - }; + } /** * @brief Get an AMFValue object at a position in the dense portion. @@ -341,16 +354,17 @@ public: * @return The casted object, or nullptr. */ template - AMFValue* Get(uint32_t index) const { + [[nodiscard]] AMFValue* Get(const size_t index) const { return index < this->dense.size() ? dynamic_cast*>(this->dense.at(index)) : nullptr; - }; + } // Get from the dense but dont cast it - AMFBaseValue* Get(const uint32_t index) const { + [[nodiscard]] AMFBaseValue* Get(const size_t index) const { return index < this->dense.size() ? this->dense.at(index) : nullptr; - }; + } + private: /** * The associative portion. These values are key'd with strings to an AMFValue. diff --git a/dCommon/CMakeLists.txt b/dCommon/CMakeLists.txt index 5300a4f2..c5fff63a 100644 --- a/dCommon/CMakeLists.txt +++ b/dCommon/CMakeLists.txt @@ -8,11 +8,9 @@ set(DCOMMON_SOURCES "Game.cpp" "GeneralUtils.cpp" "LDFFormat.cpp" - "MD5.cpp" "Metrics.cpp" "NiPoint3.cpp" "NiQuaternion.cpp" - "SHA512.cpp" "Demangler.cpp" "ZCompression.cpp" "BrickByBrickFix.cpp" @@ -20,6 +18,12 @@ set(DCOMMON_SOURCES "FdbToSqlite.cpp" ) +# Workaround for compiler bug where the optimized code could result in a memcpy of 0 bytes, even though that isnt possible. +# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97185 +if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + set_source_files_properties("FdbToSqlite.cpp" PROPERTIES COMPILE_FLAGS "-Wno-stringop-overflow") +endif() + add_subdirectory(dClient) foreach(file ${DCOMMON_DCLIENT_SOURCES}) diff --git a/dCommon/GeneralUtils.cpp b/dCommon/GeneralUtils.cpp index b45165fa..78cf4f48 100644 --- a/dCommon/GeneralUtils.cpp +++ b/dCommon/GeneralUtils.cpp @@ -319,7 +319,3 @@ std::vector GeneralUtils::GetSqlFileNamesFromFolder(const std::stri return sortedFiles; } - -bool GeneralUtils::TryParse(const std::string& x, const std::string& y, const std::string& z, NiPoint3& dst) { - return TryParse(x.c_str(), dst.x) && TryParse(y.c_str(), dst.y) && TryParse(z.c_str(), dst.z); -} diff --git a/dCommon/GeneralUtils.h b/dCommon/GeneralUtils.h index 37291ab8..15659912 100644 --- a/dCommon/GeneralUtils.h +++ b/dCommon/GeneralUtils.h @@ -1,17 +1,20 @@ #pragma once // C++ -#include +#include +#include #include -#include +#include #include -#include +#include +#include #include #include #include #include "BitStream.h" #include "NiPoint3.h" +#include "dPlatforms.h" #include "Game.h" #include "Logger.h" @@ -123,90 +126,125 @@ namespace GeneralUtils { std::vector GetSqlFileNamesFromFolder(const std::string& folder); + // Concept constraining to enum types template - T Parse(const char* value); - - template <> - inline bool Parse(const char* value) { - return std::stoi(value); - } - - template <> - inline int32_t Parse(const char* value) { - return std::stoi(value); - } - - template <> - inline int64_t Parse(const char* value) { - return std::stoll(value); - } - - template <> - inline float Parse(const char* value) { - return std::stof(value); - } - - template <> - inline double Parse(const char* value) { - return std::stod(value); - } - - template <> - inline uint16_t Parse(const char* value) { - return std::stoul(value); - } - - template <> - inline uint32_t Parse(const char* value) { - return std::stoul(value); - } - - template <> - inline uint64_t Parse(const char* value) { - return std::stoull(value); - } - - template <> - inline eInventoryType Parse(const char* value) { - return static_cast(std::stoul(value)); - } - - template <> - inline eReplicaComponentType Parse(const char* value) { - return static_cast(std::stoul(value)); - } + concept Enum = std::is_enum_v; + // Concept constraining to numeric types template - bool TryParse(const char* value, T& dst) { - try { - dst = Parse(value); + concept Numeric = std::integral || Enum || std::floating_point; - return true; - } catch (...) { - return false; + // Concept trickery to enable parsing underlying numeric types + template + struct numeric_parse { using type = T; }; + + // If an enum, present an alias to its underlying type for parsing + template requires Enum + struct numeric_parse { using type = std::underlying_type_t; }; + + // If a boolean, present an alias to an intermediate integral type for parsing + template requires std::same_as + struct numeric_parse { using type = uint32_t; }; + + // Shorthand type alias + template + using numeric_parse_t = numeric_parse::type; + + /** + * For numeric values: Parses a string_view and returns an optional variable depending on the result. + * @param str The string_view to be evaluated + * @returns An std::optional containing the desired value if it is equivalent to the string + */ + template + [[nodiscard]] std::optional TryParse(const std::string_view str) { + numeric_parse_t result; + + const char* const strEnd = str.data() + str.size(); + const auto [parseEnd, ec] = std::from_chars(str.data(), strEnd, result); + const bool isParsed = parseEnd == strEnd && ec == std::errc{}; + + return isParsed ? static_cast(result) : std::optional{}; + } + +#ifdef DARKFLAME_PLATFORM_MACOS + + // Anonymous namespace containing MacOS floating-point parse function specializations + namespace { + template + [[nodiscard]] T Parse(const std::string_view str, size_t* parseNum); + + template <> + [[nodiscard]] float Parse(const std::string_view str, size_t* parseNum) { + return std::stof(std::string{ str }, parseNum); + } + + template <> + [[nodiscard]] double Parse(const std::string_view str, size_t* parseNum) { + return std::stod(std::string{ str }, parseNum); + } + + template <> + [[nodiscard]] long double Parse(const std::string_view str, size_t* parseNum) { + return std::stold(std::string{ str }, parseNum); } } + /** + * For floating-point values: Parses a string_view and returns an optional variable depending on the result. + * Note that this function overload is only included for MacOS, as from_chars will fulfill its purpose otherwise. + * @param str The string_view to be evaluated + * @returns An std::optional containing the desired value if it is equivalent to the string + */ + template + [[nodiscard]] std::optional TryParse(const std::string_view str) noexcept try { + size_t parseNum; + const T result = Parse(str, &parseNum); + const bool isParsed = str.length() == parseNum; + + return isParsed ? result : std::optional{}; + } catch (...) { + return std::nullopt; + } + +#endif + + /** + * The TryParse overload for handling NiPoint3 by passing 3 seperate string references + * @param strX The string representing the X coordinate + * @param strY The string representing the Y coordinate + * @param strZ The string representing the Z coordinate + * @returns An std::optional containing the desired NiPoint3 if it can be constructed from the string parameters + */ template - T Parse(const std::string& value) { - return Parse(value.c_str()); + [[nodiscard]] std::optional TryParse(const std::string& strX, const std::string& strY, const std::string& strZ) { + const auto x = TryParse(strX); + if (!x) return std::nullopt; + + const auto y = TryParse(strY); + if (!y) return std::nullopt; + + const auto z = TryParse(strZ); + return z ? std::make_optional(x.value(), y.value(), z.value()) : std::nullopt; + } + + /** + * The TryParse overload for handling NiPoint3 by passingn a reference to a vector of three strings + * @param str The string vector representing the X, Y, and Xcoordinates + * @returns An std::optional containing the desired NiPoint3 if it can be constructed from the string parameters + */ + template + [[nodiscard]] std::optional TryParse(const std::vector& str) { + return (str.size() == 3) ? TryParse(str[0], str[1], str[2]) : std::nullopt; } template - bool TryParse(const std::string& value, T& dst) { - return TryParse(value.c_str(), dst); - } - - bool TryParse(const std::string& x, const std::string& y, const std::string& z, NiPoint3& dst); - - template std::u16string to_u16string(T value) { return GeneralUtils::ASCIIToUTF16(std::to_string(value)); } // From boost::hash_combine template - void hash_combine(std::size_t& s, const T& v) { + constexpr void hash_combine(std::size_t& s, const T& v) { std::hash h; s ^= h(v) + 0x9e3779b9 + (s << 6) + (s >> 2); } @@ -239,10 +277,8 @@ namespace GeneralUtils { * @param entry Enum entry to cast * @returns The enum entry's value in its underlying type */ - template - inline constexpr typename std::underlying_type_t CastUnderlyingType(const eType entry) { - static_assert(std::is_enum_v, "Not an enum"); - + template + constexpr typename std::underlying_type_t CastUnderlyingType(const eType entry) noexcept { return static_cast>(entry); } diff --git a/dCommon/LDFFormat.cpp b/dCommon/LDFFormat.cpp index 67f6a630..da28ae6e 100644 --- a/dCommon/LDFFormat.cpp +++ b/dCommon/LDFFormat.cpp @@ -61,33 +61,33 @@ LDFBaseData* LDFBaseData::DataFromString(const std::string_view& format) { } case LDF_TYPE_S32: { - int32_t data; - if (!GeneralUtils::TryParse(ldfTypeAndValue.second.data(), data)) { + const auto data = GeneralUtils::TryParse(ldfTypeAndValue.second); + if (!data) { LOG("Warning: Attempted to process invalid int32 value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data()); return nullptr; } - returnValue = new LDFData(key, data); + returnValue = new LDFData(key, data.value()); break; } case LDF_TYPE_FLOAT: { - float data; - if (!GeneralUtils::TryParse(ldfTypeAndValue.second.data(), data)) { + const auto data = GeneralUtils::TryParse(ldfTypeAndValue.second); + if (!data) { LOG("Warning: Attempted to process invalid float value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data()); return nullptr; } - returnValue = new LDFData(key, data); + returnValue = new LDFData(key, data.value()); break; } case LDF_TYPE_DOUBLE: { - double data; - if (!GeneralUtils::TryParse(ldfTypeAndValue.second.data(), data)) { + const auto data = GeneralUtils::TryParse(ldfTypeAndValue.second); + if (!data) { LOG("Warning: Attempted to process invalid double value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data()); return nullptr; } - returnValue = new LDFData(key, data); + returnValue = new LDFData(key, data.value()); break; } @@ -100,10 +100,12 @@ LDFBaseData* LDFBaseData::DataFromString(const std::string_view& format) { } else if (ldfTypeAndValue.second == "false") { data = 0; } else { - if (!GeneralUtils::TryParse(ldfTypeAndValue.second.data(), data)) { + const auto dataOptional = GeneralUtils::TryParse(ldfTypeAndValue.second); + if (!dataOptional) { LOG("Warning: Attempted to process invalid uint32 value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data()); return nullptr; } + data = dataOptional.value(); } returnValue = new LDFData(key, data); @@ -118,10 +120,12 @@ LDFBaseData* LDFBaseData::DataFromString(const std::string_view& format) { } else if (ldfTypeAndValue.second == "false") { data = false; } else { - if (!GeneralUtils::TryParse(ldfTypeAndValue.second.data(), data)) { + const auto dataOptional = GeneralUtils::TryParse(ldfTypeAndValue.second); + if (!dataOptional) { LOG("Warning: Attempted to process invalid bool value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data()); return nullptr; } + data = dataOptional.value(); } returnValue = new LDFData(key, data); @@ -129,22 +133,22 @@ LDFBaseData* LDFBaseData::DataFromString(const std::string_view& format) { } case LDF_TYPE_U64: { - uint64_t data; - if (!GeneralUtils::TryParse(ldfTypeAndValue.second.data(), data)) { + const auto data = GeneralUtils::TryParse(ldfTypeAndValue.second); + if (!data) { LOG("Warning: Attempted to process invalid uint64 value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data()); return nullptr; } - returnValue = new LDFData(key, data); + returnValue = new LDFData(key, data.value()); break; } case LDF_TYPE_OBJID: { - LWOOBJID data; - if (!GeneralUtils::TryParse(ldfTypeAndValue.second.data(), data)) { + const auto data = GeneralUtils::TryParse(ldfTypeAndValue.second); + if (!data) { LOG("Warning: Attempted to process invalid LWOOBJID value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data()); return nullptr; } - returnValue = new LDFData(key, data); + returnValue = new LDFData(key, data.value()); break; } diff --git a/dCommon/LDFFormat.h b/dCommon/LDFFormat.h index 7bcc91a2..3a4f2ea7 100644 --- a/dCommon/LDFFormat.h +++ b/dCommon/LDFFormat.h @@ -162,7 +162,7 @@ public: return new LDFData(key, value); } - inline static T Default = {}; + inline static const T Default = {}; }; // LDF Types diff --git a/dCommon/NiPoint3.cpp b/dCommon/NiPoint3.cpp index 3394deb7..b8546fdd 100644 --- a/dCommon/NiPoint3.cpp +++ b/dCommon/NiPoint3.cpp @@ -1,210 +1,24 @@ #include "NiPoint3.h" -#include "NiQuaternion.h" // C++ #include -// Static Variables -const NiPoint3 NiPoint3::ZERO(0.0f, 0.0f, 0.0f); -const NiPoint3 NiPoint3::UNIT_X(1.0f, 0.0f, 0.0f); -const NiPoint3 NiPoint3::UNIT_Y(0.0f, 1.0f, 0.0f); -const NiPoint3 NiPoint3::UNIT_Z(0.0f, 0.0f, 1.0f); -const NiPoint3 NiPoint3::UNIT_ALL(1.0f, 1.0f, 1.0f); - -//! Initializer -NiPoint3::NiPoint3(void) { - this->x = 0; - this->y = 0; - this->z = 0; -} - -//! Initializer -NiPoint3::NiPoint3(float x, float y, float z) { - this->x = x; - this->y = y; - this->z = z; -} - -//! Copy Constructor -NiPoint3::NiPoint3(const NiPoint3& point) { - this->x = point.x; - this->y = point.y; - this->z = point.z; -} - -//! Destructor -NiPoint3::~NiPoint3(void) {} - -// MARK: Getters / Setters - -//! Gets the X coordinate -float NiPoint3::GetX(void) const { - return this->x; -} - -//! Sets the X coordinate -void NiPoint3::SetX(float x) { - this->x = x; -} - -//! Gets the Y coordinate -float NiPoint3::GetY(void) const { - return this->y; -} - -//! Sets the Y coordinate -void NiPoint3::SetY(float y) { - this->y = y; -} - -//! Gets the Z coordinate -float NiPoint3::GetZ(void) const { - return this->z; -} - -//! Sets the Z coordinate -void NiPoint3::SetZ(float z) { - this->z = z; -} - -// MARK: Functions +// MARK: Member Functions //! Gets the length of the vector -float NiPoint3::Length(void) const { - return sqrt(x * x + y * y + z * z); -} - -//! Gets the squared length of a vector -float NiPoint3::SquaredLength(void) const { - return (x * x + y * y + z * z); -} - -//! Returns the dot product of the vector dotted with another vector -float NiPoint3::DotProduct(const Vector3& vec) const { - return ((this->x * vec.x) + (this->y * vec.y) + (this->z * vec.z)); -} - -//! Returns the cross product of the vector crossed with another vector -Vector3 NiPoint3::CrossProduct(const Vector3& vec) const { - return Vector3(((this->y * vec.z) - (this->z * vec.y)), - ((this->z * vec.x) - (this->x * vec.z)), - ((this->x * vec.y) - (this->y * vec.x))); +float NiPoint3::Length() const { + return std::sqrt(x * x + y * y + z * z); } //! Unitize the vector -NiPoint3 NiPoint3::Unitize(void) const { +NiPoint3 NiPoint3::Unitize() const { float length = this->Length(); - return length != 0 ? *this / length : NiPoint3::ZERO; + return length != 0 ? *this / length : NiPoint3Constant::ZERO; } - -// MARK: Operators - -//! Operator to check for equality -bool NiPoint3::operator==(const NiPoint3& point) const { - return point.x == this->x && point.y == this->y && point.z == this->z; -} - -//! Operator to check for inequality -bool NiPoint3::operator!=(const NiPoint3& point) const { - return !(*this == point); -} - -//! Operator for subscripting -float& NiPoint3::operator[](int i) { - float* base = &x; - return base[i]; -} - -//! Operator for subscripting -const float& NiPoint3::operator[](int i) const { - const float* base = &x; - return base[i]; -} - -//! Operator for addition of vectors -NiPoint3 NiPoint3::operator+(const NiPoint3& point) const { - return NiPoint3(this->x + point.x, this->y + point.y, this->z + point.z); -} - -//! Operator for addition of vectors -NiPoint3& NiPoint3::operator+=(const NiPoint3& point) { - this->x += point.x; - this->y += point.y; - this->z += point.z; - return *this; -} - -NiPoint3& NiPoint3::operator*=(const float scalar) { - this->x *= scalar; - this->y *= scalar; - this->z *= scalar; - return *this; -} - -//! Operator for subtraction of vectors -NiPoint3 NiPoint3::operator-(const NiPoint3& point) const { - return NiPoint3(this->x - point.x, this->y - point.y, this->z - point.z); -} - -//! Operator for addition of a scalar on all vector components -NiPoint3 NiPoint3::operator+(float fScalar) const { - return NiPoint3(this->x + fScalar, this->y + fScalar, this->z + fScalar); -} - -//! Operator for subtraction of a scalar on all vector components -NiPoint3 NiPoint3::operator-(float fScalar) const { - return NiPoint3(this->x - fScalar, this->y - fScalar, this->z - fScalar); -} - -//! Operator for scalar multiplication of a vector -NiPoint3 NiPoint3::operator*(float fScalar) const { - return NiPoint3(this->x * fScalar, this->y * fScalar, this->z * fScalar); -} - -//! Operator for scalar division of a vector -NiPoint3 NiPoint3::operator/(float fScalar) const { - float retX = this->x != 0 ? this->x / fScalar : 0; - float retY = this->y != 0 ? this->y / fScalar : 0; - float retZ = this->z != 0 ? this->z / fScalar : 0; - return NiPoint3(retX, retY, retZ); -} - - // MARK: Helper Functions -//! Checks to see if the point (or vector) is with an Axis-Aligned Bounding Box -bool NiPoint3::IsWithinAxisAlignedBox(const NiPoint3& minPoint, const NiPoint3& maxPoint) { - if (this->x < minPoint.x) return false; - if (this->x > maxPoint.x) return false; - if (this->y < minPoint.y) return false; - if (this->y > maxPoint.y) return false; - - return (this->z < maxPoint.z && this->z > minPoint.z); -} - -//! Checks to see if the point (or vector) is within a sphere -bool NiPoint3::IsWithinSpehere(const NiPoint3& sphereCenter, float radius) { - Vector3 diffVec = Vector3(x - sphereCenter.GetX(), y - sphereCenter.GetY(), z - sphereCenter.GetZ()); - return (diffVec.SquaredLength() <= (radius * radius)); -} - -NiPoint3 NiPoint3::ClosestPointOnLine(const NiPoint3& a, const NiPoint3& b, const NiPoint3& p) { - if (a == b) return a; - - const auto pa = p - a; - const auto ab = b - a; - - const auto t = pa.DotProduct(ab) / ab.SquaredLength(); - - if (t <= 0.0f) return a; - - if (t >= 1.0f) return b; - - return a + ab * t; -} - float NiPoint3::Angle(const NiPoint3& a, const NiPoint3& b) { const auto dot = a.DotProduct(b); const auto lenA = a.SquaredLength(); @@ -220,15 +34,7 @@ float NiPoint3::Distance(const NiPoint3& a, const NiPoint3& b) { return std::sqrt(dx * dx + dy * dy + dz * dz); } -float NiPoint3::DistanceSquared(const NiPoint3& a, const NiPoint3& b) { - const auto dx = a.x - b.x; - const auto dy = a.y - b.y; - const auto dz = a.z - b.z; - - return dx * dx + dy * dy + dz * dz; -} - -NiPoint3 NiPoint3::MoveTowards(const NiPoint3& current, const NiPoint3& target, float maxDistanceDelta) { +NiPoint3 NiPoint3::MoveTowards(const NiPoint3& current, const NiPoint3& target, const float maxDistanceDelta) { float dx = target.x - current.x; float dy = target.y - current.y; float dz = target.z - current.z; @@ -249,29 +55,3 @@ NiPoint3 NiPoint3::MoveTowards(const NiPoint3& current, const NiPoint3& target, float length = std::sqrt(lengthSquared); return NiPoint3(current.x + dx / length * maxDistanceDelta, current.y + dy / length * maxDistanceDelta, current.z + dz / length * maxDistanceDelta); } - -//This code is yoinked from the MS XNA code, so it should be right, even if it's horrible. -NiPoint3 NiPoint3::RotateByQuaternion(const NiQuaternion& rotation) { - Vector3 vector; - float num12 = rotation.x + rotation.x; - float num2 = rotation.y + rotation.y; - float num = rotation.z + rotation.z; - float num11 = rotation.w * num12; - float num10 = rotation.w * num2; - float num9 = rotation.w * num; - float num8 = rotation.x * num12; - float num7 = rotation.x * num2; - float num6 = rotation.x * num; - float num5 = rotation.y * num2; - float num4 = rotation.y * num; - float num3 = rotation.z * num; - - NiPoint3 value = *this; - float num15 = ((value.x * ((1.0f - num5) - num3)) + (value.y * (num7 - num9))) + (value.z * (num6 + num10)); - float num14 = ((value.x * (num7 + num9)) + (value.y * ((1.0f - num8) - num3))) + (value.z * (num4 - num11)); - float num13 = ((value.x * (num6 - num10)) + (value.y * (num4 + num11))) + (value.z * ((1.0f - num8) - num5)); - vector.x = num15; - vector.y = num14; - vector.z = num13; - return vector; -} diff --git a/dCommon/NiPoint3.h b/dCommon/NiPoint3.h index 44c3c383..00d09083 100644 --- a/dCommon/NiPoint3.h +++ b/dCommon/NiPoint3.h @@ -1,4 +1,5 @@ -#pragma once +#ifndef __NIPOINT3_H__ +#define __NIPOINT3_H__ /*! \file NiPoint3.hpp @@ -12,13 +13,13 @@ typedef NiPoint3 Vector3; //!< The Vector3 class is technically the NiPoin //! A custom class the defines a point in space class NiPoint3 { public: - float x; //!< The x position - float y; //!< The y position - float z; //!< The z position + float x{ 0 }; //!< The x position + float y{ 0 }; //!< The y position + float z{ 0 }; //!< The z position //! Initializer - NiPoint3(void); + constexpr NiPoint3() = default; //! Initializer /*! @@ -26,23 +27,21 @@ public: \param y The y coordinate \param z The z coordinate */ - NiPoint3(float x, float y, float z); + constexpr NiPoint3(const float x, const float y, const float z) noexcept + : x{ x } + , y{ y } + , z{ z } { + } //! Copy Constructor /*! \param point The point to copy */ - NiPoint3(const NiPoint3& point); - - //! Destructor - ~NiPoint3(void); - - // MARK: Constants - static const NiPoint3 ZERO; //!< Point(0, 0, 0) - static const NiPoint3 UNIT_X; //!< Point(1, 0, 0) - static const NiPoint3 UNIT_Y; //!< Point(0, 1, 0) - static const NiPoint3 UNIT_Z; //!< Point(0, 0, 1) - static const NiPoint3 UNIT_ALL; //!< Point(1, 1, 1) + constexpr NiPoint3(const NiPoint3& point) noexcept + : x{ point.x } + , y{ point.y } + , z{ point.z } { + } // MARK: Getters / Setters @@ -50,38 +49,37 @@ public: /*! \return The x coordinate */ - float GetX(void) const; + [[nodiscard]] constexpr float GetX() const noexcept; //! Sets the X coordinate /*! \param x The x coordinate */ - void SetX(float x); + constexpr void SetX(const float x) noexcept; //! Gets the Y coordinate /*! \return The y coordinate */ - float GetY(void) const; + [[nodiscard]] constexpr float GetY() const noexcept; //! Sets the Y coordinate /*! \param y The y coordinate */ - void SetY(float y); + constexpr void SetY(const float y) noexcept; //! Gets the Z coordinate /*! \return The z coordinate */ - float GetZ(void) const; + [[nodiscard]] constexpr float GetZ() const noexcept; //! Sets the Z coordinate /*! \param z The z coordinate */ - void SetZ(float z); - + constexpr void SetZ(const float z) noexcept; // MARK: Member Functions @@ -89,72 +87,70 @@ public: /*! \return The scalar length of the vector */ - float Length(void) const; + [[nodiscard]] float Length() const; //! Gets the squared length of a vector /*! \return The squared length of a vector */ - float SquaredLength(void) const; + [[nodiscard]] constexpr float SquaredLength() const noexcept; //! Returns the dot product of the vector dotted with another vector /*! \param vec The second vector \return The dot product of the two vectors */ - float DotProduct(const Vector3& vec) const; + [[nodiscard]] constexpr float DotProduct(const Vector3& vec) const noexcept; //! Returns the cross product of the vector crossed with another vector /*! \param vec The second vector \return The cross product of the two vectors */ - Vector3 CrossProduct(const Vector3& vec) const; + [[nodiscard]] constexpr Vector3 CrossProduct(const Vector3& vec) const noexcept; //! Unitize the vector /*! \returns The current vector */ - NiPoint3 Unitize(void) const; - + [[nodiscard]] NiPoint3 Unitize() const; // MARK: Operators //! Operator to check for equality - bool operator==(const NiPoint3& point) const; + constexpr bool operator==(const NiPoint3& point) const noexcept; //! Operator to check for inequality - bool operator!=(const NiPoint3& point) const; + constexpr bool operator!=(const NiPoint3& point) const noexcept; //! Operator for subscripting - float& operator[](int i); + constexpr float& operator[](const int i) noexcept; //! Operator for subscripting - const float& operator[](int i) const; + constexpr const float& operator[](const int i) const noexcept; //! Operator for addition of vectors - NiPoint3 operator+(const NiPoint3& point) const; + constexpr NiPoint3 operator+(const NiPoint3& point) const noexcept; //! Operator for addition of vectors - NiPoint3& operator+=(const NiPoint3& point); + constexpr NiPoint3& operator+=(const NiPoint3& point) noexcept; - NiPoint3& operator*=(const float scalar); + constexpr NiPoint3& operator*=(const float scalar) noexcept; //! Operator for subtraction of vectors - NiPoint3 operator-(const NiPoint3& point) const; + constexpr NiPoint3 operator-(const NiPoint3& point) const noexcept; //! Operator for addition of a scalar on all vector components - NiPoint3 operator+(float fScalar) const; + constexpr NiPoint3 operator+(const float fScalar) const noexcept; //! Operator for subtraction of a scalar on all vector components - NiPoint3 operator-(float fScalar) const; + constexpr NiPoint3 operator-(const float fScalar) const noexcept; //! Operator for scalar multiplication of a vector - NiPoint3 operator*(float fScalar) const; + constexpr NiPoint3 operator*(const float fScalar) const noexcept; //! Operator for scalar division of a vector - NiPoint3 operator/(float fScalar) const; - + constexpr NiPoint3 operator/(const float fScalar) const noexcept; // MARK: Helper Functions @@ -164,14 +160,14 @@ public: \param maxPoint The maximum point of the bounding box \return Whether or not this point lies within the box */ - bool IsWithinAxisAlignedBox(const NiPoint3& minPoint, const NiPoint3& maxPoint); + [[nodiscard]] constexpr bool IsWithinAxisAlignedBox(const NiPoint3& minPoint, const NiPoint3& maxPoint) noexcept; //! Checks to see if the point (or vector) is within a sphere /*! \param sphereCenter The sphere center \param radius The radius */ - bool IsWithinSpehere(const NiPoint3& sphereCenter, float radius); + [[nodiscard]] constexpr bool IsWithinSphere(const NiPoint3& sphereCenter, const float radius) noexcept; /*! \param a Start of line @@ -179,15 +175,30 @@ public: \param p Refrence point \return The point of line AB which is closest to P */ - static NiPoint3 ClosestPointOnLine(const NiPoint3& a, const NiPoint3& b, const NiPoint3& p); + [[nodiscard]] static constexpr NiPoint3 ClosestPointOnLine(const NiPoint3& a, const NiPoint3& b, const NiPoint3& p) noexcept; - static float Angle(const NiPoint3& a, const NiPoint3& b); + [[nodiscard]] static float Angle(const NiPoint3& a, const NiPoint3& b); - static float Distance(const NiPoint3& a, const NiPoint3& b); + [[nodiscard]] static float Distance(const NiPoint3& a, const NiPoint3& b); - static float DistanceSquared(const NiPoint3& a, const NiPoint3& b); + [[nodiscard]] static constexpr float DistanceSquared(const NiPoint3& a, const NiPoint3& b) noexcept; - static NiPoint3 MoveTowards(const NiPoint3& current, const NiPoint3& target, float maxDistanceDelta); + [[nodiscard]] static NiPoint3 MoveTowards(const NiPoint3& current, const NiPoint3& target, const float maxDistanceDelta); - NiPoint3 RotateByQuaternion(const NiQuaternion& rotation); + //This code is yoinked from the MS XNA code, so it should be right, even if it's horrible. + [[nodiscard]] constexpr NiPoint3 RotateByQuaternion(const NiQuaternion& rotation) noexcept; }; + +// Static Variables +namespace NiPoint3Constant { + constexpr NiPoint3 ZERO(0.0f, 0.0f, 0.0f); + constexpr NiPoint3 UNIT_X(1.0f, 0.0f, 0.0f); + constexpr NiPoint3 UNIT_Y(0.0f, 1.0f, 0.0f); + constexpr NiPoint3 UNIT_Z(0.0f, 0.0f, 1.0f); + constexpr NiPoint3 UNIT_ALL(1.0f, 1.0f, 1.0f); +} + +// .inl file needed for code organization and to circumvent circular dependency issues +#include "NiPoint3.inl" + +#endif // !__NIPOINT3_H__ diff --git a/dCommon/NiPoint3.inl b/dCommon/NiPoint3.inl new file mode 100644 index 00000000..48aa3ae1 --- /dev/null +++ b/dCommon/NiPoint3.inl @@ -0,0 +1,196 @@ +#pragma once +#ifndef __NIPOINT3_H__ +#error "This should only be included inline in NiPoint3.h: Do not include directly!" +#endif + +#include "NiQuaternion.h" + +// MARK: Getters / Setters + +//! Gets the X coordinate +constexpr float NiPoint3::GetX() const noexcept { + return this->x; +} + +//! Sets the X coordinate +constexpr void NiPoint3::SetX(const float x) noexcept { + this->x = x; +} + +//! Gets the Y coordinate +constexpr float NiPoint3::GetY() const noexcept { + return this->y; +} + +//! Sets the Y coordinate +constexpr void NiPoint3::SetY(const float y) noexcept { + this->y = y; +} + +//! Gets the Z coordinate +constexpr float NiPoint3::GetZ() const noexcept { + return this->z; +} + +//! Sets the Z coordinate +constexpr void NiPoint3::SetZ(const float z) noexcept { + this->z = z; +} + +// MARK: Member Functions + +//! Gets the squared length of a vector +constexpr float NiPoint3::SquaredLength() const noexcept { + return (x * x + y * y + z * z); +} + +//! Returns the dot product of the vector dotted with another vector +constexpr float NiPoint3::DotProduct(const Vector3& vec) const noexcept { + return ((this->x * vec.x) + (this->y * vec.y) + (this->z * vec.z)); +} + +//! Returns the cross product of the vector crossed with another vector +constexpr Vector3 NiPoint3::CrossProduct(const Vector3& vec) const noexcept { + return Vector3(((this->y * vec.z) - (this->z * vec.y)), + ((this->z * vec.x) - (this->x * vec.z)), + ((this->x * vec.y) - (this->y * vec.x))); +} + +// MARK: Operators + +//! Operator to check for equality +constexpr bool NiPoint3::operator==(const NiPoint3& point) const noexcept { + return point.x == this->x && point.y == this->y && point.z == this->z; +} + +//! Operator to check for inequality +constexpr bool NiPoint3::operator!=(const NiPoint3& point) const noexcept { + return !(*this == point); +} + +//! Operator for subscripting +constexpr float& NiPoint3::operator[](const int i) noexcept { + float* base = &x; + return base[i]; +} + +//! Operator for subscripting +constexpr const float& NiPoint3::operator[](const int i) const noexcept { + const float* base = &x; + return base[i]; +} + +//! Operator for addition of vectors +constexpr NiPoint3 NiPoint3::operator+(const NiPoint3& point) const noexcept { + return NiPoint3(this->x + point.x, this->y + point.y, this->z + point.z); +} + +//! Operator for addition of vectors +constexpr NiPoint3& NiPoint3::operator+=(const NiPoint3& point) noexcept { + this->x += point.x; + this->y += point.y; + this->z += point.z; + return *this; +} + +constexpr NiPoint3& NiPoint3::operator*=(const float scalar) noexcept { + this->x *= scalar; + this->y *= scalar; + this->z *= scalar; + return *this; +} + +//! Operator for subtraction of vectors +constexpr NiPoint3 NiPoint3::operator-(const NiPoint3& point) const noexcept { + return NiPoint3(this->x - point.x, this->y - point.y, this->z - point.z); +} + +//! Operator for addition of a scalar on all vector components +constexpr NiPoint3 NiPoint3::operator+(const float fScalar) const noexcept { + return NiPoint3(this->x + fScalar, this->y + fScalar, this->z + fScalar); +} + +//! Operator for subtraction of a scalar on all vector components +constexpr NiPoint3 NiPoint3::operator-(const float fScalar) const noexcept { + return NiPoint3(this->x - fScalar, this->y - fScalar, this->z - fScalar); +} + +//! Operator for scalar multiplication of a vector +constexpr NiPoint3 NiPoint3::operator*(const float fScalar) const noexcept { + return NiPoint3(this->x * fScalar, this->y * fScalar, this->z * fScalar); +} + +//! Operator for scalar division of a vector +constexpr NiPoint3 NiPoint3::operator/(const float fScalar) const noexcept { + float retX = this->x != 0 ? this->x / fScalar : 0; + float retY = this->y != 0 ? this->y / fScalar : 0; + float retZ = this->z != 0 ? this->z / fScalar : 0; + return NiPoint3(retX, retY, retZ); +} + +// MARK: Helper Functions + +//! Checks to see if the point (or vector) is with an Axis-Aligned Bounding Box +constexpr bool NiPoint3::IsWithinAxisAlignedBox(const NiPoint3& minPoint, const NiPoint3& maxPoint) noexcept { + if (this->x < minPoint.x) return false; + if (this->x > maxPoint.x) return false; + if (this->y < minPoint.y) return false; + if (this->y > maxPoint.y) return false; + + return (this->z < maxPoint.z && this->z > minPoint.z); +} + +//! Checks to see if the point (or vector) is within a sphere +constexpr bool NiPoint3::IsWithinSphere(const NiPoint3& sphereCenter, const float radius) noexcept { + Vector3 diffVec = Vector3(x - sphereCenter.GetX(), y - sphereCenter.GetY(), z - sphereCenter.GetZ()); + return (diffVec.SquaredLength() <= (radius * radius)); +} + +constexpr NiPoint3 NiPoint3::ClosestPointOnLine(const NiPoint3& a, const NiPoint3& b, const NiPoint3& p) noexcept { + if (a == b) return a; + + const auto pa = p - a; + const auto ab = b - a; + + const auto t = pa.DotProduct(ab) / ab.SquaredLength(); + + if (t <= 0.0f) return a; + + if (t >= 1.0f) return b; + + return a + ab * t; +} + +constexpr float NiPoint3::DistanceSquared(const NiPoint3& a, const NiPoint3& b) noexcept { + const auto dx = a.x - b.x; + const auto dy = a.y - b.y; + const auto dz = a.z - b.z; + + return dx * dx + dy * dy + dz * dz; +} + +//This code is yoinked from the MS XNA code, so it should be right, even if it's horrible. +constexpr NiPoint3 NiPoint3::RotateByQuaternion(const NiQuaternion& rotation) noexcept { + Vector3 vector; + float num12 = rotation.x + rotation.x; + float num2 = rotation.y + rotation.y; + float num = rotation.z + rotation.z; + float num11 = rotation.w * num12; + float num10 = rotation.w * num2; + float num9 = rotation.w * num; + float num8 = rotation.x * num12; + float num7 = rotation.x * num2; + float num6 = rotation.x * num; + float num5 = rotation.y * num2; + float num4 = rotation.y * num; + float num3 = rotation.z * num; + + NiPoint3 value = *this; + float num15 = ((value.x * ((1.0f - num5) - num3)) + (value.y * (num7 - num9))) + (value.z * (num6 + num10)); + float num14 = ((value.x * (num7 + num9)) + (value.y * ((1.0f - num8) - num3))) + (value.z * (num4 - num11)); + float num13 = ((value.x * (num6 - num10)) + (value.y * (num4 + num11))) + (value.z * ((1.0f - num8) - num5)); + vector.x = num15; + vector.y = num14; + vector.z = num13; + return vector; +} diff --git a/dCommon/NiQuaternion.cpp b/dCommon/NiQuaternion.cpp index 33c5c976..b12d3991 100644 --- a/dCommon/NiQuaternion.cpp +++ b/dCommon/NiQuaternion.cpp @@ -3,89 +3,8 @@ // C++ #include -// Static Variables -const NiQuaternion NiQuaternion::IDENTITY(1, 0, 0, 0); - -//! The initializer -NiQuaternion::NiQuaternion(void) { - this->w = 1; - this->x = 0; - this->y = 0; - this->z = 0; -} - -//! The initializer -NiQuaternion::NiQuaternion(float w, float x, float y, float z) { - this->w = w; - this->x = x; - this->y = y; - this->z = z; -} - -//! Destructor -NiQuaternion::~NiQuaternion(void) {} - - -// MARK: Setters / Getters - -//! Gets the W coordinate -float NiQuaternion::GetW(void) const { - return this->w; -} - -//! Sets the W coordinate -void NiQuaternion::SetW(float w) { - this->w = w; -} - -//! Gets the X coordinate -float NiQuaternion::GetX(void) const { - return this->x; -} - -//! Sets the X coordinate -void NiQuaternion::SetX(float x) { - this->x = x; -} - -//! Gets the Y coordinate -float NiQuaternion::GetY(void) const { - return this->y; -} - -//! Sets the Y coordinate -void NiQuaternion::SetY(float y) { - this->y = y; -} - -//! Gets the Z coordinate -float NiQuaternion::GetZ(void) const { - return this->z; -} - -//! Sets the Z coordinate -void NiQuaternion::SetZ(float z) { - this->z = z; -} - - // MARK: Member Functions -//! Returns the forward vector from the quaternion -Vector3 NiQuaternion::GetForwardVector(void) const { - return Vector3(2 * (x * z + w * y), 2 * (y * z - w * x), 1 - 2 * (x * x + y * y)); -} - -//! Returns the up vector from the quaternion -Vector3 NiQuaternion::GetUpVector(void) const { - return Vector3(2 * (x * y - w * z), 1 - 2 * (x * x + z * z), 2 * (y * z + w * x)); -} - -//! Returns the right vector from the quaternion -Vector3 NiQuaternion::GetRightVector(void) const { - return Vector3(1 - 2 * (y * y + z * z), 2 * (x * y + w * z), 2 * (x * z - w * y)); -} - Vector3 NiQuaternion::GetEulerAngles() const { Vector3 angles; @@ -111,22 +30,9 @@ Vector3 NiQuaternion::GetEulerAngles() const { return angles; } -// MARK: Operators - -//! Operator to check for equality -bool NiQuaternion::operator==(const NiQuaternion& rot) const { - return rot.x == this->x && rot.y == this->y && rot.z == this->z && rot.w == this->w; -} - -//! Operator to check for inequality -bool NiQuaternion::operator!=(const NiQuaternion& rot) const { - return !(*this == rot); -} - - // MARK: Helper Functions -//! Look from a specific point in space to another point in space +//! Look from a specific point in space to another point in space (Y-locked) NiQuaternion NiQuaternion::LookAt(const NiPoint3& sourcePoint, const NiPoint3& destPoint) { //To make sure we don't orient around the X/Z axis: NiPoint3 source = sourcePoint; @@ -136,7 +42,7 @@ NiQuaternion NiQuaternion::LookAt(const NiPoint3& sourcePoint, const NiPoint3& d NiPoint3 forwardVector = NiPoint3(dest - source).Unitize(); - NiPoint3 posZ = NiPoint3::UNIT_Z; + NiPoint3 posZ = NiPoint3Constant::UNIT_Z; NiPoint3 vecA = posZ.CrossProduct(forwardVector).Unitize(); float dot = posZ.DotProduct(forwardVector); @@ -148,10 +54,11 @@ NiQuaternion NiQuaternion::LookAt(const NiPoint3& sourcePoint, const NiPoint3& d return NiQuaternion::CreateFromAxisAngle(vecA, rotAngle); } +//! Look from a specific point in space to another point in space NiQuaternion NiQuaternion::LookAtUnlocked(const NiPoint3& sourcePoint, const NiPoint3& destPoint) { NiPoint3 forwardVector = NiPoint3(destPoint - sourcePoint).Unitize(); - NiPoint3 posZ = NiPoint3::UNIT_Z; + NiPoint3 posZ = NiPoint3Constant::UNIT_Z; NiPoint3 vecA = posZ.CrossProduct(forwardVector).Unitize(); float dot = posZ.DotProduct(forwardVector); diff --git a/dCommon/NiQuaternion.h b/dCommon/NiQuaternion.h index b7d60f4e..482b86fa 100644 --- a/dCommon/NiQuaternion.h +++ b/dCommon/NiQuaternion.h @@ -1,4 +1,5 @@ -#pragma once +#ifndef __NIQUATERNION_H__ +#define __NIQUATERNION_H__ // Custom Classes #include "NiPoint3.h" @@ -14,14 +15,14 @@ typedef NiQuaternion Quaternion; //!< A typedef for a shorthand version o //! A class that defines a rotation in space class NiQuaternion { public: - float w; //!< The w coordinate - float x; //!< The x coordinate - float y; //!< The y coordinate - float z; //!< The z coordinate + float w{ 1 }; //!< The w coordinate + float x{ 0 }; //!< The x coordinate + float y{ 0 }; //!< The y coordinate + float z{ 0 }; //!< The z coordinate //! The initializer - NiQuaternion(void); + constexpr NiQuaternion() = default; //! The initializer /*! @@ -30,13 +31,12 @@ public: \param y The y coordinate \param z The z coordinate */ - NiQuaternion(float w, float x, float y, float z); - - //! Destructor - ~NiQuaternion(void); - - // MARK: Constants - static const NiQuaternion IDENTITY; //!< Quaternion(1, 0, 0, 0) + constexpr NiQuaternion(const float w, const float x, const float y, const float z) noexcept + : w{ w } + , x{ x } + , y{ y } + , z{ z } { + } // MARK: Setters / Getters @@ -44,50 +44,49 @@ public: /*! \return The w coordinate */ - float GetW(void) const; + [[nodiscard]] constexpr float GetW() const noexcept; //! Sets the W coordinate /*! \param w The w coordinate */ - void SetW(float w); + constexpr void SetW(const float w) noexcept; //! Gets the X coordinate /*! \return The x coordinate */ - float GetX(void) const; + [[nodiscard]] constexpr float GetX() const noexcept; //! Sets the X coordinate /*! \param x The x coordinate */ - void SetX(float x); + constexpr void SetX(const float x) noexcept; //! Gets the Y coordinate /*! \return The y coordinate */ - float GetY(void) const; + [[nodiscard]] constexpr float GetY() const noexcept; //! Sets the Y coordinate /*! \param y The y coordinate */ - void SetY(float y); + constexpr void SetY(const float y) noexcept; //! Gets the Z coordinate /*! \return The z coordinate */ - float GetZ(void) const; + [[nodiscard]] constexpr float GetZ() const noexcept; //! Sets the Z coordinate /*! \param z The z coordinate */ - void SetZ(float z); - + constexpr void SetZ(const float z) noexcept; // MARK: Member Functions @@ -95,31 +94,29 @@ public: /*! \return The forward vector of the quaternion */ - Vector3 GetForwardVector(void) const; + [[nodiscard]] constexpr Vector3 GetForwardVector() const noexcept; //! Returns the up vector from the quaternion /*! \return The up vector fo the quaternion */ - Vector3 GetUpVector(void) const; + [[nodiscard]] constexpr Vector3 GetUpVector() const noexcept; //! Returns the right vector from the quaternion /*! \return The right vector of the quaternion */ - Vector3 GetRightVector(void) const; - - Vector3 GetEulerAngles() const; + [[nodiscard]] constexpr Vector3 GetRightVector() const noexcept; + [[nodiscard]] Vector3 GetEulerAngles() const; // MARK: Operators //! Operator to check for equality - bool operator==(const NiQuaternion& rot) const; + constexpr bool operator==(const NiQuaternion& rot) const noexcept; //! Operator to check for inequality - bool operator!=(const NiQuaternion& rot) const; - + constexpr bool operator!=(const NiQuaternion& rot) const noexcept; // MARK: Helper Functions @@ -129,7 +126,7 @@ public: \param destPoint The destination location \return The Quaternion with the rotation towards the destination */ - static NiQuaternion LookAt(const NiPoint3& sourcePoint, const NiPoint3& destPoint); + [[nodiscard]] static NiQuaternion LookAt(const NiPoint3& sourcePoint, const NiPoint3& destPoint); //! Look from a specific point in space to another point in space /*! @@ -137,7 +134,7 @@ public: \param destPoint The destination location \return The Quaternion with the rotation towards the destination */ - static NiQuaternion LookAtUnlocked(const NiPoint3& sourcePoint, const NiPoint3& destPoint); + [[nodiscard]] static NiQuaternion LookAtUnlocked(const NiPoint3& sourcePoint, const NiPoint3& destPoint); //! Creates a Quaternion from a specific axis and angle relative to that axis /*! @@ -145,7 +142,17 @@ public: \param angle The angle relative to this axis \return A quaternion created from the axis and angle */ - static NiQuaternion CreateFromAxisAngle(const Vector3& axis, float angle); + [[nodiscard]] static NiQuaternion CreateFromAxisAngle(const Vector3& axis, float angle); - static NiQuaternion FromEulerAngles(const NiPoint3& eulerAngles); + [[nodiscard]] static NiQuaternion FromEulerAngles(const NiPoint3& eulerAngles); }; + +// Static Variables +namespace NiQuaternionConstant { + constexpr NiQuaternion IDENTITY(1, 0, 0, 0); +} + +// Include constexpr and inline function definitions in a seperate file for readability +#include "NiQuaternion.inl" + +#endif // !__NIQUATERNION_H__ diff --git a/dCommon/NiQuaternion.inl b/dCommon/NiQuaternion.inl new file mode 100644 index 00000000..9ed84ffc --- /dev/null +++ b/dCommon/NiQuaternion.inl @@ -0,0 +1,75 @@ +#pragma once +#ifndef __NIQUATERNION_H__ +#error "This should only be included inline in NiQuaternion.h: Do not include directly!" +#endif + +// MARK: Setters / Getters + +//! Gets the W coordinate +constexpr float NiQuaternion::GetW() const noexcept { + return this->w; +} + +//! Sets the W coordinate +constexpr void NiQuaternion::SetW(const float w) noexcept { + this->w = w; +} + +//! Gets the X coordinate +constexpr float NiQuaternion::GetX() const noexcept { + return this->x; +} + +//! Sets the X coordinate +constexpr void NiQuaternion::SetX(const float x) noexcept { + this->x = x; +} + +//! Gets the Y coordinate +constexpr float NiQuaternion::GetY() const noexcept { + return this->y; +} + +//! Sets the Y coordinate +constexpr void NiQuaternion::SetY(const float y) noexcept { + this->y = y; +} + +//! Gets the Z coordinate +constexpr float NiQuaternion::GetZ() const noexcept { + return this->z; +} + +//! Sets the Z coordinate +constexpr void NiQuaternion::SetZ(const float z) noexcept { + this->z = z; +} + +// MARK: Member Functions + +//! Returns the forward vector from the quaternion +constexpr Vector3 NiQuaternion::GetForwardVector() const noexcept { + return Vector3(2 * (x * z + w * y), 2 * (y * z - w * x), 1 - 2 * (x * x + y * y)); +} + +//! Returns the up vector from the quaternion +constexpr Vector3 NiQuaternion::GetUpVector() const noexcept { + return Vector3(2 * (x * y - w * z), 1 - 2 * (x * x + z * z), 2 * (y * z + w * x)); +} + +//! Returns the right vector from the quaternion +constexpr Vector3 NiQuaternion::GetRightVector() const noexcept { + return Vector3(1 - 2 * (y * y + z * z), 2 * (x * y + w * z), 2 * (x * z - w * y)); +} + +// MARK: Operators + +//! Operator to check for equality +constexpr bool NiQuaternion::operator==(const NiQuaternion& rot) const noexcept { + return rot.x == this->x && rot.y == this->y && rot.z == this->z && rot.w == this->w; +} + +//! Operator to check for inequality +constexpr bool NiQuaternion::operator!=(const NiQuaternion& rot) const noexcept { + return !(*this == rot); +} diff --git a/dCommon/PositionUpdate.h b/dCommon/PositionUpdate.h new file mode 100644 index 00000000..4d591a97 --- /dev/null +++ b/dCommon/PositionUpdate.h @@ -0,0 +1,50 @@ +#ifndef __POSITIONUPDATE__H__ +#define __POSITIONUPDATE__H__ + +#include "NiPoint3.h" +#include "NiQuaternion.h" + + +struct RemoteInputInfo { + RemoteInputInfo() { + m_RemoteInputX = 0; + m_RemoteInputY = 0; + m_IsPowersliding = false; + m_IsModified = false; + } + + void operator=(const RemoteInputInfo& other) { + m_RemoteInputX = other.m_RemoteInputX; + m_RemoteInputY = other.m_RemoteInputY; + m_IsPowersliding = other.m_IsPowersliding; + m_IsModified = other.m_IsModified; + } + + bool operator==(const RemoteInputInfo& other) { + return m_RemoteInputX == other.m_RemoteInputX && m_RemoteInputY == other.m_RemoteInputY && m_IsPowersliding == other.m_IsPowersliding && m_IsModified == other.m_IsModified; + } + + float m_RemoteInputX; + float m_RemoteInputY; + bool m_IsPowersliding; + bool m_IsModified; +}; + +struct LocalSpaceInfo { + LWOOBJID objectId = LWOOBJID_EMPTY; + NiPoint3 position = NiPoint3Constant::ZERO; + NiPoint3 linearVelocity = NiPoint3Constant::ZERO; +}; + +struct PositionUpdate { + NiPoint3 position = NiPoint3Constant::ZERO; + NiQuaternion rotation = NiQuaternionConstant::IDENTITY; + bool onGround = false; + bool onRail = false; + NiPoint3 velocity = NiPoint3Constant::ZERO; + NiPoint3 angularVelocity = NiPoint3Constant::ZERO; + LocalSpaceInfo localSpaceInfo; + RemoteInputInfo remoteInputInfo; +}; + +#endif //!__POSITIONUPDATE__H__ diff --git a/dCommon/SHA512.cpp b/dCommon/SHA512.cpp deleted file mode 100644 index e3c2d9f7..00000000 --- a/dCommon/SHA512.cpp +++ /dev/null @@ -1,154 +0,0 @@ -// Source: http://www.zedwood.com/article/cpp-sha512-function - -#include "SHA512.h" - -#include -#include - -const unsigned long long SHA512::sha512_k[80] = //ULL = uint64 -{ 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, - 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, - 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, - 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, - 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, - 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, - 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, - 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL, - 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, - 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, - 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, - 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, - 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, - 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, - 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, - 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, - 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, - 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, - 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, - 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL, - 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, - 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, - 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, - 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, - 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, - 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, - 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, - 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, - 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, - 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, - 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, - 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL, - 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, - 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, - 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, - 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, - 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, - 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL, - 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, - 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL }; - -void SHA512::transform(const unsigned char* message, unsigned int block_nb) { - uint64 w[80]; - uint64 wv[8]; - uint64 t1, t2; - const unsigned char* sub_block; - int i, j; - for (i = 0; i < (int)block_nb; i++) { - sub_block = message + (i << 7); - for (j = 0; j < 16; j++) { - SHA2_PACK64(&sub_block[j << 3], &w[j]); - } - for (j = 16; j < 80; j++) { - w[j] = SHA512_F4(w[j - 2]) + w[j - 7] + SHA512_F3(w[j - 15]) + w[j - 16]; - } - for (j = 0; j < 8; j++) { - wv[j] = m_h[j]; - } - for (j = 0; j < 80; j++) { - t1 = wv[7] + SHA512_F2(wv[4]) + SHA2_CH(wv[4], wv[5], wv[6]) - + sha512_k[j] + w[j]; - t2 = SHA512_F1(wv[0]) + SHA2_MAJ(wv[0], wv[1], wv[2]); - wv[7] = wv[6]; - wv[6] = wv[5]; - wv[5] = wv[4]; - wv[4] = wv[3] + t1; - wv[3] = wv[2]; - wv[2] = wv[1]; - wv[1] = wv[0]; - wv[0] = t1 + t2; - } - for (j = 0; j < 8; j++) { - m_h[j] += wv[j]; - } - - } -} - -void SHA512::init() { - m_h[0] = 0x6a09e667f3bcc908ULL; - m_h[1] = 0xbb67ae8584caa73bULL; - m_h[2] = 0x3c6ef372fe94f82bULL; - m_h[3] = 0xa54ff53a5f1d36f1ULL; - m_h[4] = 0x510e527fade682d1ULL; - m_h[5] = 0x9b05688c2b3e6c1fULL; - m_h[6] = 0x1f83d9abfb41bd6bULL; - m_h[7] = 0x5be0cd19137e2179ULL; - m_len = 0; - m_tot_len = 0; -} - -void SHA512::update(const unsigned char* message, unsigned int len) { - unsigned int block_nb; - unsigned int new_len, rem_len, tmp_len; - const unsigned char* shifted_message; - tmp_len = SHA384_512_BLOCK_SIZE - m_len; - rem_len = len < tmp_len ? len : tmp_len; - memcpy(&m_block[m_len], message, rem_len); - if (m_len + len < SHA384_512_BLOCK_SIZE) { - m_len += len; - return; - } - new_len = len - rem_len; - block_nb = new_len / SHA384_512_BLOCK_SIZE; - shifted_message = message + rem_len; - transform(m_block, 1); - transform(shifted_message, block_nb); - rem_len = new_len % SHA384_512_BLOCK_SIZE; - memcpy(m_block, &shifted_message[block_nb << 7], rem_len); - m_len = rem_len; - m_tot_len += (block_nb + 1) << 7; -} - -void SHA512::final(unsigned char* digest) { - unsigned int block_nb; - unsigned int pm_len; - unsigned int len_b; - int i; - block_nb = 1 + ((SHA384_512_BLOCK_SIZE - 17) - < (m_len % SHA384_512_BLOCK_SIZE)); - len_b = (m_tot_len + m_len) << 3; - pm_len = block_nb << 7; - memset(m_block + m_len, 0, pm_len - m_len); - m_block[m_len] = 0x80; - SHA2_UNPACK32(len_b, m_block + pm_len - 4); - transform(m_block, block_nb); - for (i = 0; i < 8; i++) { - SHA2_UNPACK64(m_h[i], &digest[i << 3]); - } -} - -std::string sha512(std::string input) { - unsigned char digest[SHA512::DIGEST_SIZE]; - memset(digest, 0, SHA512::DIGEST_SIZE); - class SHA512 ctx; - ctx.init(); - ctx.update((unsigned char*)input.c_str(), input.length()); - ctx.final(digest); - - char buf[2 * SHA512::DIGEST_SIZE + 1]; - buf[2 * SHA512::DIGEST_SIZE] = 0; - for (int i = 0; i < SHA512::DIGEST_SIZE; i++) - sprintf(buf + i * 2, "%02x", digest[i]); - - return std::string(buf); -} diff --git a/dCommon/SHA512.h b/dCommon/SHA512.h deleted file mode 100644 index 512fa645..00000000 --- a/dCommon/SHA512.h +++ /dev/null @@ -1,68 +0,0 @@ -#pragma once - -// C++ -#include - -class SHA512 { -protected: - typedef unsigned char uint8; - typedef unsigned int uint32; - typedef unsigned long long uint64; - - const static uint64 sha512_k[]; - static const unsigned int SHA384_512_BLOCK_SIZE = (1024 / 8); - -public: - void init(); - void update(const unsigned char* message, unsigned int len); - void final(unsigned char* digest); - static const unsigned int DIGEST_SIZE = (512 / 8); - -protected: - void transform(const unsigned char* message, unsigned int block_nb); - unsigned int m_tot_len; - unsigned int m_len; - unsigned char m_block[2 * SHA384_512_BLOCK_SIZE]; - uint64 m_h[8]; -}; - -std::string sha512(std::string input); - -#define SHA2_SHFR(x, n) (x >> n) -#define SHA2_ROTR(x, n) ((x >> n) | (x << ((sizeof(x) << 3) - n))) -#define SHA2_ROTL(x, n) ((x << n) | (x >> ((sizeof(x) << 3) - n))) -#define SHA2_CH(x, y, z) ((x & y) ^ (~x & z)) -#define SHA2_MAJ(x, y, z) ((x & y) ^ (x & z) ^ (y & z)) -#define SHA512_F1(x) (SHA2_ROTR(x, 28) ^ SHA2_ROTR(x, 34) ^ SHA2_ROTR(x, 39)) -#define SHA512_F2(x) (SHA2_ROTR(x, 14) ^ SHA2_ROTR(x, 18) ^ SHA2_ROTR(x, 41)) -#define SHA512_F3(x) (SHA2_ROTR(x, 1) ^ SHA2_ROTR(x, 8) ^ SHA2_SHFR(x, 7)) -#define SHA512_F4(x) (SHA2_ROTR(x, 19) ^ SHA2_ROTR(x, 61) ^ SHA2_SHFR(x, 6)) -#define SHA2_UNPACK32(x, str) \ -{ \ -*((str) + 3) = (uint8) ((x) ); \ -*((str) + 2) = (uint8) ((x) >> 8); \ -*((str) + 1) = (uint8) ((x) >> 16); \ -*((str) + 0) = (uint8) ((x) >> 24); \ -} -#define SHA2_UNPACK64(x, str) \ -{ \ -*((str) + 7) = (uint8) ((x) ); \ -*((str) + 6) = (uint8) ((x) >> 8); \ -*((str) + 5) = (uint8) ((x) >> 16); \ -*((str) + 4) = (uint8) ((x) >> 24); \ -*((str) + 3) = (uint8) ((x) >> 32); \ -*((str) + 2) = (uint8) ((x) >> 40); \ -*((str) + 1) = (uint8) ((x) >> 48); \ -*((str) + 0) = (uint8) ((x) >> 56); \ -} -#define SHA2_PACK64(str, x) \ -{ \ -*(x) = ((uint64) *((str) + 7) ) \ -| ((uint64) *((str) + 6) << 8) \ -| ((uint64) *((str) + 5) << 16) \ -| ((uint64) *((str) + 4) << 24) \ -| ((uint64) *((str) + 3) << 32) \ -| ((uint64) *((str) + 2) << 40) \ -| ((uint64) *((str) + 1) << 48) \ -| ((uint64) *((str) + 0) << 56); \ -} diff --git a/dCommon/dClient/CMakeLists.txt b/dCommon/dClient/CMakeLists.txt index 69bb1712..017a22a5 100644 --- a/dCommon/dClient/CMakeLists.txt +++ b/dCommon/dClient/CMakeLists.txt @@ -1,6 +1,6 @@ set(DCOMMON_DCLIENT_SOURCES + "AssetManager.cpp" "PackIndex.cpp" "Pack.cpp" - "AssetManager.cpp" PARENT_SCOPE ) diff --git a/dCommon/dClient/ClientVersion.h b/dCommon/dClient/ClientVersion.h new file mode 100644 index 00000000..393103ab --- /dev/null +++ b/dCommon/dClient/ClientVersion.h @@ -0,0 +1,12 @@ +#ifndef __CLIENTVERSION_H__ +#define __CLIENTVERSION_H__ + +#include + +namespace ClientVersion { + constexpr uint16_t major = 1; + constexpr uint16_t current = 10; + constexpr uint16_t minor = 64; +} + +#endif // !__CLIENTVERSION_H__ diff --git a/dCommon/dEnums/StringifiedEnum.h b/dCommon/dEnums/StringifiedEnum.h index 16eafdca..1816d705 100644 --- a/dCommon/dEnums/StringifiedEnum.h +++ b/dCommon/dEnums/StringifiedEnum.h @@ -9,15 +9,15 @@ namespace StringifiedEnum { const std::string_view ToString(const T e) { static_assert(std::is_enum_v, "Not an enum"); // Check type - constexpr auto sv = &magic_enum::enum_entries(); + constexpr auto& sv = magic_enum::enum_entries(); const auto it = std::lower_bound( - sv->begin(), sv->end(), e, + sv.begin(), sv.end(), e, [&](const std::pair& lhs, const T rhs) { return lhs.first < rhs; } ); std::string_view output; - if (it != sv->end() && it->first == e) { + if (it != sv.end() && it->first == e) { output = it->second; } else { output = "UNKNOWN"; diff --git a/dCommon/dEnums/dCommonVars.h b/dCommon/dEnums/dCommonVars.h index f4f8fdfb..d871e267 100644 --- a/dCommon/dEnums/dCommonVars.h +++ b/dCommon/dEnums/dCommonVars.h @@ -39,86 +39,84 @@ constexpr uint32_t lowFrameDelta = FRAMES_TO_MS(lowFramerate); //=========== TYPEDEFS ========== -typedef int32_t LOT; //!< A LOT -typedef int64_t LWOOBJID; //!< An object ID (should be unsigned actually but ok) -typedef int32_t TSkillID; //!< A skill ID -typedef uint32_t LWOCLONEID; //!< Used for Clone IDs -typedef uint16_t LWOMAPID; //!< Used for Map IDs -typedef uint16_t LWOINSTANCEID; //!< Used for Instance IDs -typedef uint32_t PROPERTYCLONELIST; //!< Used for Property Clone IDs -typedef uint32_t StripId; +using LOT = int32_t; //!< A LOT +using LWOOBJID = int64_t; //!< An object ID (should be unsigned actually but ok) +using TSkillID = int32_t; //!< A skill ID +using LWOCLONEID = uint32_t; //!< Used for Clone IDs +using LWOMAPID = uint16_t; //!< Used for Map IDs +using LWOINSTANCEID = uint16_t; //!< Used for Instance IDs +using PROPERTYCLONELIST = uint32_t; //!< Used for Property Clone IDs +using StripId = uint32_t; -const LWOOBJID LWOOBJID_EMPTY = 0; //!< An empty object ID -const LOT LOT_NULL = -1; //!< A null LOT -const int32_t LOOTTYPE_NONE = 0; //!< No loot type available -const float SECONDARY_PRIORITY = 1.0f; //!< Secondary Priority -const uint32_t INVENTORY_MAX = 9999999; //!< The Maximum Inventory Size -const uint32_t LWOCLONEID_INVALID = -1; //!< Invalid LWOCLONEID -const uint16_t LWOINSTANCEID_INVALID = -1; //!< Invalid LWOINSTANCEID -const uint16_t LWOMAPID_INVALID = -1; //!< Invalid LWOMAPID -const uint64_t LWOZONEID_INVALID = 0; //!< Invalid LWOZONEID +constexpr LWOOBJID LWOOBJID_EMPTY = 0; //!< An empty object ID +constexpr LOT LOT_NULL = -1; //!< A null LOT +constexpr int32_t LOOTTYPE_NONE = 0; //!< No loot type available +constexpr float SECONDARY_PRIORITY = 1.0f; //!< Secondary Priority +constexpr uint32_t INVENTORY_MAX = 9999999; //!< The Maximum Inventory Size +constexpr LWOCLONEID LWOCLONEID_INVALID = -1; //!< Invalid LWOCLONEID +constexpr LWOINSTANCEID LWOINSTANCEID_INVALID = -1; //!< Invalid LWOINSTANCEID +constexpr LWOMAPID LWOMAPID_INVALID = -1; //!< Invalid LWOMAPID +constexpr uint64_t LWOZONEID_INVALID = 0; //!< Invalid LWOZONEID -const float PI = 3.14159f; +constexpr float PI = 3.14159f; //============ STRUCTS ============== struct LWOSCENEID { public: - LWOSCENEID() { m_sceneID = -1; m_layerID = 0; } - LWOSCENEID(int sceneID) { m_sceneID = sceneID; m_layerID = 0; } - LWOSCENEID(int sceneID, unsigned int layerID) { m_sceneID = sceneID; m_layerID = layerID; } + constexpr LWOSCENEID() noexcept { m_sceneID = -1; m_layerID = 0; } + constexpr LWOSCENEID(int32_t sceneID) noexcept { m_sceneID = sceneID; m_layerID = 0; } + constexpr LWOSCENEID(int32_t sceneID, uint32_t layerID) noexcept { m_sceneID = sceneID; m_layerID = layerID; } - LWOSCENEID& operator=(const LWOSCENEID& rhs) { m_sceneID = rhs.m_sceneID; m_layerID = rhs.m_layerID; return *this; } - LWOSCENEID& operator=(const int rhs) { m_sceneID = rhs; m_layerID = 0; return *this; } + constexpr LWOSCENEID& operator=(const LWOSCENEID& rhs) noexcept { m_sceneID = rhs.m_sceneID; m_layerID = rhs.m_layerID; return *this; } + constexpr LWOSCENEID& operator=(const int32_t rhs) noexcept { m_sceneID = rhs; m_layerID = 0; return *this; } - bool operator<(const LWOSCENEID& rhs) const { return (m_sceneID < rhs.m_sceneID || (m_sceneID == rhs.m_sceneID && m_layerID < rhs.m_layerID)); } - bool operator<(const int rhs) const { return m_sceneID < rhs; } + constexpr bool operator<(const LWOSCENEID& rhs) const noexcept { return (m_sceneID < rhs.m_sceneID || (m_sceneID == rhs.m_sceneID && m_layerID < rhs.m_layerID)); } + constexpr bool operator<(const int32_t rhs) const noexcept { return m_sceneID < rhs; } - bool operator==(const LWOSCENEID& rhs) const { return (m_sceneID == rhs.m_sceneID && m_layerID == rhs.m_layerID); } - bool operator==(const int rhs) const { return m_sceneID == rhs; } + constexpr bool operator==(const LWOSCENEID& rhs) const noexcept { return (m_sceneID == rhs.m_sceneID && m_layerID == rhs.m_layerID); } + constexpr bool operator==(const int32_t rhs) const noexcept { return m_sceneID == rhs; } - const int GetSceneID() const { return m_sceneID; } - const unsigned int GetLayerID() const { return m_layerID; } + constexpr int32_t GetSceneID() const noexcept { return m_sceneID; } + constexpr uint32_t GetLayerID() const noexcept { return m_layerID; } - void SetSceneID(const int sceneID) { m_sceneID = sceneID; } - void SetLayerID(const unsigned int layerID) { m_layerID = layerID; } + constexpr void SetSceneID(const int32_t sceneID) noexcept { m_sceneID = sceneID; } + constexpr void SetLayerID(const uint32_t layerID) noexcept { m_layerID = layerID; } private: - int m_sceneID; - unsigned int m_layerID; + int32_t m_sceneID; + uint32_t m_layerID; }; struct LWOZONEID { public: - const LWOMAPID& GetMapID() const { return m_MapID; } - const LWOINSTANCEID& GetInstanceID() const { return m_InstanceID; } - const LWOCLONEID& GetCloneID() const { return m_CloneID; } + constexpr const LWOMAPID& GetMapID() const noexcept { return m_MapID; } + constexpr const LWOINSTANCEID& GetInstanceID() const noexcept { return m_InstanceID; } + constexpr const LWOCLONEID& GetCloneID() const noexcept { return m_CloneID; } //In order: def constr, constr, assign op - LWOZONEID() { m_MapID = LWOMAPID_INVALID; m_InstanceID = LWOINSTANCEID_INVALID; m_CloneID = LWOCLONEID_INVALID; } - LWOZONEID(const LWOMAPID& mapID, const LWOINSTANCEID& instanceID, const LWOCLONEID& cloneID) { m_MapID = mapID; m_InstanceID = instanceID; m_CloneID = cloneID; } - LWOZONEID(const LWOZONEID& replacement) { *this = replacement; } + constexpr LWOZONEID() noexcept = default; + constexpr LWOZONEID(const LWOMAPID& mapID, const LWOINSTANCEID& instanceID, const LWOCLONEID& cloneID) noexcept { m_MapID = mapID; m_InstanceID = instanceID; m_CloneID = cloneID; } + constexpr LWOZONEID(const LWOZONEID& replacement) noexcept { *this = replacement; } private: - LWOMAPID m_MapID; //1000 for VE, 1100 for AG, etc... - LWOINSTANCEID m_InstanceID; //Instances host the same world, but on a different dWorld process. - LWOCLONEID m_CloneID; //To differentiate between "your property" and "my property". Always 0 for non-prop worlds. + LWOMAPID m_MapID = LWOMAPID_INVALID; //1000 for VE, 1100 for AG, etc... + LWOINSTANCEID m_InstanceID = LWOINSTANCEID_INVALID; //Instances host the same world, but on a different dWorld process. + LWOCLONEID m_CloneID = LWOCLONEID_INVALID; //To differentiate between "your property" and "my property". Always 0 for non-prop worlds. }; -const LWOSCENEID LWOSCENEID_INVALID = -1; +constexpr LWOSCENEID LWOSCENEID_INVALID = -1; struct LWONameValue { uint32_t length = 0; //!< The length of the name std::u16string name; //!< The name - LWONameValue(void) {} + LWONameValue() = default; LWONameValue(const std::u16string& name) { this->name = name; this->length = static_cast(name.length()); } - - ~LWONameValue(void) {} }; struct FriendData { diff --git a/dCommon/dEnums/eGameMessageType.h b/dCommon/dEnums/eGameMessageType.h index 14673fac..e3fc22b6 100644 --- a/dCommon/dEnums/eGameMessageType.h +++ b/dCommon/dEnums/eGameMessageType.h @@ -629,7 +629,7 @@ enum class eGameMessageType : uint16_t { GET_INSTRUCTION_COUNT = 676, GET_IS_NPC = 677, ACTIVATE_BUBBLE_BUFF = 678, - DECTIVATE_BUBBLE_BUFF = 679, // thanks netdevil + DECTIVATE_BUBBLE_BUFF = 679, // This is spelled wrong in the client, so we misspell it here. EXHIBIT_VOTE = 680, ADD_PET_TO_PLAYER = 681, REMOVE_PET_FROM_PLAYER = 682, diff --git a/dCommon/dEnums/ePetAbilityType.h b/dCommon/dEnums/ePetAbilityType.h index 3b2e5520..0cc6d6bd 100644 --- a/dCommon/dEnums/ePetAbilityType.h +++ b/dCommon/dEnums/ePetAbilityType.h @@ -1,7 +1,9 @@ #ifndef __EPETABILITYTYPE__H__ #define __EPETABILITYTYPE__H__ -enum class ePetAbilityType : int32_t { +#include + +enum class ePetAbilityType : uint32_t { Invalid, GoToObject, JumpOnObject, diff --git a/dDatabase/CDClientDatabase/CDClientDatabase.cpp b/dDatabase/CDClientDatabase/CDClientDatabase.cpp index 4c2df1d2..886030a1 100644 --- a/dDatabase/CDClientDatabase/CDClientDatabase.cpp +++ b/dDatabase/CDClientDatabase/CDClientDatabase.cpp @@ -4,9 +4,13 @@ // Static Variables static CppSQLite3DB* conn = new CppSQLite3DB(); +// Status Variables +bool CDClientDatabase::isConnected = false; + //! Opens a connection with the CDClient void CDClientDatabase::Connect(const std::string& filename) { conn->open(filename.c_str()); + isConnected = true; } //! Queries the CDClient diff --git a/dDatabase/CDClientDatabase/CDClientDatabase.h b/dDatabase/CDClientDatabase/CDClientDatabase.h index 7f42918d..fa58906c 100644 --- a/dDatabase/CDClientDatabase/CDClientDatabase.h +++ b/dDatabase/CDClientDatabase/CDClientDatabase.h @@ -15,6 +15,10 @@ //! The CDClient Database namespace namespace CDClientDatabase { + /** + * Boolean defining the connection status of CDClient + */ + extern bool isConnected; //! Opens a connection with the CDClient /*! diff --git a/dDatabase/CDClientDatabase/CDClientManager.cpp b/dDatabase/CDClientDatabase/CDClientManager.cpp index c30e598f..0e05c0b8 100644 --- a/dDatabase/CDClientDatabase/CDClientManager.cpp +++ b/dDatabase/CDClientDatabase/CDClientManager.cpp @@ -3,6 +3,7 @@ #include "CDAnimationsTable.h" #include "CDBehaviorParameterTable.h" #include "CDBehaviorTemplateTable.h" +#include "CDClientDatabase.h" #include "CDComponentsRegistryTable.h" #include "CDCurrencyTableTable.h" #include "CDDestructibleComponentTable.h" @@ -38,6 +39,9 @@ #include "CDFeatureGatingTable.h" #include "CDRailActivatorComponent.h" #include "CDRewardCodesTable.h" +#include "CDPetComponentTable.h" + +#include #ifndef CDCLIENT_CACHE_ALL // Uncomment this to cache the full cdclient database into memory. This will make the server load faster, but will use more memory. @@ -51,7 +55,65 @@ #define CDCLIENT_DONT_CACHE_TABLE(x) #endif -CDClientManager::CDClientManager() { +class CDClientConnectionException : public std::exception { +public: + virtual const char* what() const throw() { + return "CDClientDatabase is not connected!"; + } +}; + +// Using a macro to reduce repetitive code and issues from copy and paste. +// As a note, ## in a macro is used to concatenate two tokens together. + +#define SPECIALIZE_TABLE_STORAGE(table) \ + template<> typename table::StorageType& CDClientManager::GetEntriesMutable() { return table##Entries; }; + +#define DEFINE_TABLE_STORAGE(table) namespace { table::StorageType table##Entries; }; SPECIALIZE_TABLE_STORAGE(table) + +DEFINE_TABLE_STORAGE(CDActivityRewardsTable); +DEFINE_TABLE_STORAGE(CDActivitiesTable); +DEFINE_TABLE_STORAGE(CDAnimationsTable); +DEFINE_TABLE_STORAGE(CDBehaviorParameterTable); +DEFINE_TABLE_STORAGE(CDBehaviorTemplateTable); +DEFINE_TABLE_STORAGE(CDBrickIDTableTable); +DEFINE_TABLE_STORAGE(CDComponentsRegistryTable); +DEFINE_TABLE_STORAGE(CDCurrencyTableTable); +DEFINE_TABLE_STORAGE(CDDestructibleComponentTable); +DEFINE_TABLE_STORAGE(CDEmoteTableTable); +DEFINE_TABLE_STORAGE(CDFeatureGatingTable); +DEFINE_TABLE_STORAGE(CDInventoryComponentTable); +DEFINE_TABLE_STORAGE(CDItemComponentTable); +DEFINE_TABLE_STORAGE(CDItemSetSkillsTable); +DEFINE_TABLE_STORAGE(CDItemSetsTable); +DEFINE_TABLE_STORAGE(CDLevelProgressionLookupTable); +DEFINE_TABLE_STORAGE(CDLootMatrixTable); +DEFINE_TABLE_STORAGE(CDLootTableTable); +DEFINE_TABLE_STORAGE(CDMissionEmailTable); +DEFINE_TABLE_STORAGE(CDMissionNPCComponentTable); +DEFINE_TABLE_STORAGE(CDMissionTasksTable); +DEFINE_TABLE_STORAGE(CDMissionsTable); +DEFINE_TABLE_STORAGE(CDMovementAIComponentTable); +DEFINE_TABLE_STORAGE(CDObjectSkillsTable); +DEFINE_TABLE_STORAGE(CDObjectsTable); +DEFINE_TABLE_STORAGE(CDPhysicsComponentTable); +DEFINE_TABLE_STORAGE(CDPackageComponentTable); +DEFINE_TABLE_STORAGE(CDPetComponentTable); +DEFINE_TABLE_STORAGE(CDProximityMonitorComponentTable); +DEFINE_TABLE_STORAGE(CDPropertyEntranceComponentTable); +DEFINE_TABLE_STORAGE(CDPropertyTemplateTable); +DEFINE_TABLE_STORAGE(CDRailActivatorComponentTable); +DEFINE_TABLE_STORAGE(CDRarityTableTable); +DEFINE_TABLE_STORAGE(CDRebuildComponentTable); +DEFINE_TABLE_STORAGE(CDRewardCodesTable); +DEFINE_TABLE_STORAGE(CDRewardsTable); +DEFINE_TABLE_STORAGE(CDScriptComponentTable); +DEFINE_TABLE_STORAGE(CDSkillBehaviorTable); +DEFINE_TABLE_STORAGE(CDVendorComponentTable); +DEFINE_TABLE_STORAGE(CDZoneTableTable); + +void CDClientManager::LoadValuesFromDatabase() { + if (!CDClientDatabase::isConnected) throw CDClientConnectionException(); + CDActivityRewardsTable::Instance().LoadValuesFromDatabase(); CDActivitiesTable::Instance().LoadValuesFromDatabase(); CDCLIENT_DONT_CACHE_TABLE(CDAnimationsTable::Instance().LoadValuesFromDatabase()); @@ -93,3 +155,9 @@ CDClientManager::CDClientManager() { CDVendorComponentTable::Instance().LoadValuesFromDatabase(); CDZoneTableTable::Instance().LoadValuesFromDatabase(); } + +void CDClientManager::LoadValuesFromDefaults() { + LOG("Loading default CDClient tables!"); + + CDPetComponentTable::Instance().LoadValuesFromDefaults(); +} diff --git a/dDatabase/CDClientDatabase/CDClientManager.h b/dDatabase/CDClientDatabase/CDClientManager.h index 74069ff4..c1c4443d 100644 --- a/dDatabase/CDClientDatabase/CDClientManager.h +++ b/dDatabase/CDClientDatabase/CDClientManager.h @@ -1,17 +1,14 @@ -#pragma once - -#include "CDTable.h" - -#include "Singleton.h" +#ifndef __CDCLIENTMANAGER__H__ +#define __CDCLIENTMANAGER__H__ #define UNUSED_TABLE(v) /** * Initialize the CDClient tables so they are all loaded into memory. */ -class CDClientManager : public Singleton { -public: - CDClientManager(); +namespace CDClientManager { + void LoadValuesFromDatabase(); + void LoadValuesFromDefaults(); /** * Fetch a table from CDClient @@ -20,7 +17,28 @@ public: * @return A pointer to the requested table. */ template - T* GetTable() { - return &T::Instance(); - } + T* GetTable(); + + /** + * Fetch a table from CDClient + * Note: Calling this function without a template specialization in CDClientManager.cpp will cause a linker error. + * + * @tparam Table type to fetch + * @return A pointer to the requested table. + */ + template + typename T::StorageType& GetEntriesMutable(); }; + + +// These are included after the CDClientManager namespace declaration as CDTable as of Jan 29 2024 relies on CDClientManager in Templated code. +#include "CDTable.h" + +#include "Singleton.h" + +template +T* CDClientManager::GetTable() { + return &T::Instance(); +}; + +#endif //!__CDCLIENTMANAGER__H__ diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDActivitiesTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDActivitiesTable.cpp index c967f724..19111490 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDActivitiesTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDActivitiesTable.cpp @@ -1,8 +1,9 @@ #include "CDActivitiesTable.h" + void CDActivitiesTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM Activities"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -13,7 +14,8 @@ void CDActivitiesTable::LoadValuesFromDatabase() { tableSize.finalize(); // Reserve the size - this->entries.reserve(size); + auto& entries = GetEntriesMutable(); + entries.reserve(size); // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM Activities"); @@ -39,7 +41,7 @@ void CDActivitiesTable::LoadValuesFromDatabase() { entry.noTeamLootOnDeath = tableData.getIntField("noTeamLootOnDeath", -1); entry.optionalPercentage = tableData.getFloatField("optionalPercentage", -1.0f); - this->entries.push_back(entry); + entries.push_back(entry); tableData.nextRow(); } @@ -48,7 +50,7 @@ void CDActivitiesTable::LoadValuesFromDatabase() { std::vector CDActivitiesTable::Query(std::function predicate) { - std::vector data = cpplinq::from(this->entries) + std::vector data = cpplinq::from(GetEntries()) >> cpplinq::where(predicate) >> cpplinq::to_vector(); diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDActivitiesTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDActivitiesTable.h index 2e39d595..3e1d4c37 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDActivitiesTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDActivitiesTable.h @@ -4,36 +4,31 @@ #include "CDTable.h" struct CDActivities { - unsigned int ActivityID; - unsigned int locStatus; - unsigned int instanceMapID; - unsigned int minTeams; - unsigned int maxTeams; - unsigned int minTeamSize; - unsigned int maxTeamSize; - unsigned int waitTime; - unsigned int startDelay; + uint32_t ActivityID; + uint32_t locStatus; + uint32_t instanceMapID; + uint32_t minTeams; + uint32_t maxTeams; + uint32_t minTeamSize; + uint32_t maxTeamSize; + uint32_t waitTime; + uint32_t startDelay; bool requiresUniqueData; - unsigned int leaderboardType; + uint32_t leaderboardType; bool localize; - int optionalCostLOT; - int optionalCostCount; + int32_t optionalCostLOT; + int32_t optionalCostCount; bool showUIRewards; - unsigned int CommunityActivityFlagID; + uint32_t CommunityActivityFlagID; std::string gate_version; bool noTeamLootOnDeath; float optionalPercentage; }; -class CDActivitiesTable : public CDTable { -private: - std::vector entries; - +class CDActivitiesTable : public CDTable> { public: void LoadValuesFromDatabase(); // Queries the table with a custom "where" clause std::vector Query(std::function predicate); - - const std::vector& GetEntries() const { return this->entries; } }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDActivityRewardsTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDActivityRewardsTable.cpp index a2434d19..abe0c50c 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDActivityRewardsTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDActivityRewardsTable.cpp @@ -1,9 +1,10 @@ #include "CDActivityRewardsTable.h" + void CDActivityRewardsTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM ActivityRewards"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -14,7 +15,8 @@ void CDActivityRewardsTable::LoadValuesFromDatabase() { tableSize.finalize(); // Reserve the size - this->entries.reserve(size); + auto& entries = GetEntriesMutable(); + entries.reserve(size); // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM ActivityRewards"); @@ -28,7 +30,7 @@ void CDActivityRewardsTable::LoadValuesFromDatabase() { entry.ChallengeRating = tableData.getIntField("ChallengeRating", -1); entry.description = tableData.getStringField("description", ""); - this->entries.push_back(entry); + entries.push_back(entry); tableData.nextRow(); } @@ -37,7 +39,7 @@ void CDActivityRewardsTable::LoadValuesFromDatabase() { std::vector CDActivityRewardsTable::Query(std::function predicate) { - std::vector data = cpplinq::from(this->entries) + std::vector data = cpplinq::from(GetEntries()) >> cpplinq::where(predicate) >> cpplinq::to_vector(); diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDActivityRewardsTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDActivityRewardsTable.h index a177a3c0..8d07a718 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDActivityRewardsTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDActivityRewardsTable.h @@ -4,24 +4,18 @@ #include "CDTable.h" struct CDActivityRewards { - unsigned int objectTemplate; //!< The object template (?) - unsigned int ActivityRewardIndex; //!< The activity reward index - int activityRating; //!< The activity rating - unsigned int LootMatrixIndex; //!< The loot matrix index - unsigned int CurrencyIndex; //!< The currency index - unsigned int ChallengeRating; //!< The challenge rating + uint32_t objectTemplate; //!< The object template (?) + uint32_t ActivityRewardIndex; //!< The activity reward index + int32_t activityRating; //!< The activity rating + uint32_t LootMatrixIndex; //!< The loot matrix index + uint32_t CurrencyIndex; //!< The currency index + uint32_t ChallengeRating; //!< The challenge rating std::string description; //!< The description }; -class CDActivityRewardsTable : public CDTable { -private: - std::vector entries; - +class CDActivityRewardsTable : public CDTable> { public: void LoadValuesFromDatabase(); // Queries the table with a custom "where" clause std::vector Query(std::function predicate); - - std::vector GetEntries() const; - }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDAnimationsTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDAnimationsTable.cpp index 7244ddee..cf461fc9 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDAnimationsTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDAnimationsTable.cpp @@ -5,6 +5,7 @@ void CDAnimationsTable::LoadValuesFromDatabase() { auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM Animations"); + auto& animations = GetEntriesMutable(); while (!tableData.eof()) { std::string animation_type = tableData.getStringField("animation_type", ""); DluAssert(!animation_type.empty()); @@ -24,7 +25,7 @@ void CDAnimationsTable::LoadValuesFromDatabase() { UNUSED_COLUMN(entry.priority = tableData.getFloatField("priority", 0.0f);) UNUSED_COLUMN(entry.blendTime = tableData.getFloatField("blendTime", 0.0f);) - this->animations[CDAnimationKey(animation_type, animationGroupID)].push_back(entry); + animations[CDAnimationKey(animation_type, animationGroupID)].push_back(entry); tableData.nextRow(); } @@ -35,6 +36,7 @@ bool CDAnimationsTable::CacheData(CppSQLite3Statement& queryToCache) { auto tableData = queryToCache.execQuery(); // If we received a bad lookup, cache it anyways so we do not run the query again. if (tableData.eof()) return false; + auto& animations = GetEntriesMutable(); do { std::string animation_type = tableData.getStringField("animation_type", ""); @@ -55,7 +57,7 @@ bool CDAnimationsTable::CacheData(CppSQLite3Statement& queryToCache) { UNUSED_COLUMN(entry.priority = tableData.getFloatField("priority", 0.0f);) UNUSED_COLUMN(entry.blendTime = tableData.getFloatField("blendTime", 0.0f);) - this->animations[CDAnimationKey(animation_type, animationGroupID)].push_back(entry); + animations[CDAnimationKey(animation_type, animationGroupID)].push_back(entry); tableData.nextRow(); } while (!tableData.eof()); @@ -68,15 +70,17 @@ void CDAnimationsTable::CacheAnimations(const CDAnimationKey animationKey) { auto query = CDClientDatabase::CreatePreppedStmt("SELECT * FROM Animations WHERE animationGroupID = ? and animation_type = ?"); query.bind(1, static_cast(animationKey.second)); query.bind(2, animationKey.first.c_str()); + auto& animations = GetEntriesMutable(); // If we received a bad lookup, cache it anyways so we do not run the query again. if (!CacheData(query)) { - this->animations[animationKey]; + animations[animationKey]; } } void CDAnimationsTable::CacheAnimationGroup(AnimationGroupID animationGroupID) { - auto animationEntryCached = this->animations.find(CDAnimationKey("", animationGroupID)); - if (animationEntryCached != this->animations.end()) { + auto& animations = GetEntriesMutable(); + auto animationEntryCached = animations.find(CDAnimationKey("", animationGroupID)); + if (animationEntryCached != animations.end()) { return; } @@ -85,28 +89,29 @@ void CDAnimationsTable::CacheAnimationGroup(AnimationGroupID animationGroupID) { // Cache the query so we don't run the query again. CacheData(query); - this->animations[CDAnimationKey("", animationGroupID)]; + animations[CDAnimationKey("", animationGroupID)]; } -CDAnimationLookupResult CDAnimationsTable::GetAnimation(const AnimationID& animationType, const std::string& previousAnimationName, const AnimationGroupID animationGroupID) { +std::optional CDAnimationsTable::GetAnimation(const AnimationID& animationType, const std::string& previousAnimationName, const AnimationGroupID animationGroupID) { + auto& animations = GetEntriesMutable(); CDAnimationKey animationKey(animationType, animationGroupID); - auto animationEntryCached = this->animations.find(animationKey); - if (animationEntryCached == this->animations.end()) { + auto animationEntryCached = animations.find(animationKey); + if (animationEntryCached == animations.end()) { this->CacheAnimations(animationKey); } - auto animationEntry = this->animations.find(animationKey); + auto animationEntry = animations.find(animationKey); // If we have only one animation, return it regardless of the chance to play. if (animationEntry->second.size() == 1) { - return CDAnimationLookupResult(animationEntry->second.front()); + return animationEntry->second.front(); } auto randomAnimation = GeneralUtils::GenerateRandomNumber(0, 1); for (auto& animationEntry : animationEntry->second) { randomAnimation -= animationEntry.chance_to_play; // This is how the client gets the random animation. - if (animationEntry.animation_name != previousAnimationName && randomAnimation <= 0.0f) return CDAnimationLookupResult(animationEntry); + if (animationEntry.animation_name != previousAnimationName && randomAnimation <= 0.0f) return animationEntry; } - return CDAnimationLookupResult(); + return std::nullopt; } diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDAnimationsTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDAnimationsTable.h index 494d5cde..643ef98f 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDAnimationsTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDAnimationsTable.h @@ -2,15 +2,20 @@ #include "CDTable.h" #include +#include + +typedef int32_t AnimationGroupID; +typedef std::string AnimationID; +typedef std::pair CDAnimationKey; struct CDAnimation { - // unsigned int animationGroupID; + // uint32_t animationGroupID; // std::string animation_type; // The above two are a pair to represent a primary key in the map. std::string animation_name; //!< The animation name float chance_to_play; //!< The chance to play the animation - UNUSED_COLUMN(unsigned int min_loops;) //!< The minimum number of loops - UNUSED_COLUMN(unsigned int max_loops;) //!< The maximum number of loops + UNUSED_COLUMN(uint32_t min_loops;) //!< The minimum number of loops + UNUSED_COLUMN(uint32_t max_loops;) //!< The maximum number of loops float animation_length; //!< The animation length UNUSED_COLUMN(bool hideEquip;) //!< Whether or not to hide the equip UNUSED_COLUMN(bool ignoreUpperBody;) //!< Whether or not to ignore the upper body @@ -20,12 +25,7 @@ struct CDAnimation { UNUSED_COLUMN(float blendTime;) //!< The blend time }; -typedef LookupResult CDAnimationLookupResult; - -class CDAnimationsTable : public CDTable { - typedef int32_t AnimationGroupID; - typedef std::string AnimationID; - typedef std::pair CDAnimationKey; +class CDAnimationsTable : public CDTable>> { public: void LoadValuesFromDatabase(); /** @@ -38,7 +38,7 @@ public: * @param animationGroupID The animationGroupID to lookup * @return CDAnimationLookupResult */ - [[nodiscard]] CDAnimationLookupResult GetAnimation(const AnimationID& animationType, const std::string& previousAnimationName, const AnimationGroupID animationGroupID); + [[nodiscard]] std::optional GetAnimation(const AnimationID& animationType, const std::string& previousAnimationName, const AnimationGroupID animationGroupID); /** * Cache a full AnimationGroup by its ID. @@ -58,10 +58,4 @@ private: * @return false */ bool CacheData(CppSQLite3Statement& queryToCache); - - /** - * Each animation is key'd by its animationName and its animationGroupID. Each - * animation has a possible list of animations. This is because there can be animations have a percent chance to play so one is selected at random. - */ - std::map> animations; }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDBehaviorParameterTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDBehaviorParameterTable.cpp index 708bec4c..57187c7c 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDBehaviorParameterTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDBehaviorParameterTable.cpp @@ -1,6 +1,10 @@ #include "CDBehaviorParameterTable.h" #include "GeneralUtils.h" +namespace { + std::unordered_map m_ParametersList; +}; + uint64_t GetKey(const uint32_t behaviorID, const uint32_t parameterID) { uint64_t key = behaviorID; key <<= 31U; @@ -11,6 +15,7 @@ uint64_t GetKey(const uint32_t behaviorID, const uint32_t parameterID) { void CDBehaviorParameterTable::LoadValuesFromDatabase() { auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM BehaviorParameter"); + auto& entries = GetEntriesMutable(); while (!tableData.eof()) { uint32_t behaviorID = tableData.getIntField("behaviorID", -1); auto candidateStringToAdd = std::string(tableData.getStringField("parameterID", "")); @@ -24,7 +29,7 @@ void CDBehaviorParameterTable::LoadValuesFromDatabase() { uint64_t hash = GetKey(behaviorID, parameterId); float value = tableData.getFloatField("value", -1.0f); - m_Entries.insert(std::make_pair(hash, value)); + entries.insert(std::make_pair(hash, value)); tableData.nextRow(); } @@ -32,22 +37,24 @@ void CDBehaviorParameterTable::LoadValuesFromDatabase() { } float CDBehaviorParameterTable::GetValue(const uint32_t behaviorID, const std::string& name, const float defaultValue) { - auto parameterID = this->m_ParametersList.find(name); - if (parameterID == this->m_ParametersList.end()) return defaultValue; + auto parameterID = m_ParametersList.find(name); + if (parameterID == m_ParametersList.end()) return defaultValue; auto hash = GetKey(behaviorID, parameterID->second); // Search for specific parameter - auto it = m_Entries.find(hash); - return it != m_Entries.end() ? it->second : defaultValue; + auto& entries = GetEntriesMutable(); + auto it = entries.find(hash); + return it != entries.end() ? it->second : defaultValue; } std::map CDBehaviorParameterTable::GetParametersByBehaviorID(uint32_t behaviorID) { + auto& entries = GetEntriesMutable(); uint64_t hashBase = behaviorID; std::map returnInfo; for (auto& [parameterString, parameterId] : m_ParametersList) { uint64_t hash = GetKey(hashBase, parameterId); - auto infoCandidate = m_Entries.find(hash); - if (infoCandidate != m_Entries.end()) { + auto infoCandidate = entries.find(hash); + if (infoCandidate != entries.end()) { returnInfo.insert(std::make_pair(parameterString, infoCandidate->second)); } } diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDBehaviorParameterTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDBehaviorParameterTable.h index 3daa3aa3..ba6ad6c1 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDBehaviorParameterTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDBehaviorParameterTable.h @@ -5,12 +5,10 @@ #include #include -class CDBehaviorParameterTable : public CDTable { -private: - typedef uint64_t BehaviorParameterHash; - typedef float BehaviorParameterValue; - std::unordered_map m_Entries; - std::unordered_map m_ParametersList; +typedef uint64_t BehaviorParameterHash; +typedef float BehaviorParameterValue; + +class CDBehaviorParameterTable : public CDTable> { public: void LoadValuesFromDatabase(); diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDBehaviorTemplateTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDBehaviorTemplateTable.cpp index c343ef85..983156e3 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDBehaviorTemplateTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDBehaviorTemplateTable.cpp @@ -1,9 +1,13 @@ #include "CDBehaviorTemplateTable.h" +namespace { + std::unordered_set m_EffectHandles; +}; + void CDBehaviorTemplateTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM BehaviorTemplate"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -13,11 +17,9 @@ void CDBehaviorTemplateTable::LoadValuesFromDatabase() { tableSize.finalize(); - // Reserve the size - this->entries.reserve(size); - // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM BehaviorTemplate"); + auto& entries = GetEntriesMutable(); while (!tableData.eof()) { CDBehaviorTemplate entry; entry.behaviorID = tableData.getIntField("behaviorID", -1); @@ -31,30 +33,17 @@ void CDBehaviorTemplateTable::LoadValuesFromDatabase() { entry.effectHandle = m_EffectHandles.insert(candidateToAdd).first; } - this->entries.push_back(entry); - this->entriesMappedByBehaviorID.insert(std::make_pair(entry.behaviorID, entry)); + entries.insert(std::make_pair(entry.behaviorID, entry)); tableData.nextRow(); } tableData.finalize(); } -std::vector CDBehaviorTemplateTable::Query(std::function predicate) { - - std::vector data = cpplinq::from(this->entries) - >> cpplinq::where(predicate) - >> cpplinq::to_vector(); - - return data; -} - -const std::vector& CDBehaviorTemplateTable::GetEntries() const { - return this->entries; -} - const CDBehaviorTemplate CDBehaviorTemplateTable::GetByBehaviorID(uint32_t behaviorID) { - auto entry = this->entriesMappedByBehaviorID.find(behaviorID); - if (entry == this->entriesMappedByBehaviorID.end()) { + auto& entries = GetEntriesMutable(); + auto entry = entries.find(behaviorID); + if (entry == entries.end()) { CDBehaviorTemplate entryToReturn; entryToReturn.behaviorID = 0; entryToReturn.effectHandle = m_EffectHandles.end(); diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDBehaviorTemplateTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDBehaviorTemplateTable.h index 49ce11f2..367f5f0a 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDBehaviorTemplateTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDBehaviorTemplateTable.h @@ -6,25 +6,15 @@ #include struct CDBehaviorTemplate { - unsigned int behaviorID; //!< The Behavior ID - unsigned int templateID; //!< The Template ID (LOT) - unsigned int effectID; //!< The Effect ID attached + uint32_t behaviorID; //!< The Behavior ID + uint32_t templateID; //!< The Template ID (LOT) + uint32_t effectID; //!< The Effect ID attached std::unordered_set::iterator effectHandle; //!< The effect handle }; - -class CDBehaviorTemplateTable : public CDTable { -private: - std::vector entries; - std::unordered_map entriesMappedByBehaviorID; - std::unordered_set m_EffectHandles; +class CDBehaviorTemplateTable : public CDTable> { public: void LoadValuesFromDatabase(); - // Queries the table with a custom "where" clause - std::vector Query(std::function predicate); - - const std::vector& GetEntries(void) const; - const CDBehaviorTemplate GetByBehaviorID(uint32_t behaviorID); }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDBrickIDTableTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDBrickIDTableTable.cpp index 9be75f0b..c2714396 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDBrickIDTableTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDBrickIDTableTable.cpp @@ -3,7 +3,7 @@ void CDBrickIDTableTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM BrickIDTable"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -14,7 +14,8 @@ void CDBrickIDTableTable::LoadValuesFromDatabase() { tableSize.finalize(); // Reserve the size - this->entries.reserve(size); + auto& entries = GetEntriesMutable(); + entries.reserve(size); // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM BrickIDTable"); @@ -23,7 +24,7 @@ void CDBrickIDTableTable::LoadValuesFromDatabase() { entry.NDObjectID = tableData.getIntField("NDObjectID", -1); entry.LEGOBrickID = tableData.getIntField("LEGOBrickID", -1); - this->entries.push_back(entry); + entries.push_back(entry); tableData.nextRow(); } @@ -31,15 +32,9 @@ void CDBrickIDTableTable::LoadValuesFromDatabase() { } std::vector CDBrickIDTableTable::Query(std::function predicate) { - - std::vector data = cpplinq::from(this->entries) + std::vector data = cpplinq::from(GetEntries()) >> cpplinq::where(predicate) >> cpplinq::to_vector(); return data; } - -const std::vector& CDBrickIDTableTable::GetEntries() const { - return this->entries; -} - diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDBrickIDTableTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDBrickIDTableTable.h index 68c0b1b6..9a2c2523 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDBrickIDTableTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDBrickIDTableTable.h @@ -10,20 +10,15 @@ //! BrickIDTable Entry Struct struct CDBrickIDTable { - unsigned int NDObjectID; - unsigned int LEGOBrickID; + uint32_t NDObjectID; + uint32_t LEGOBrickID; }; //! BrickIDTable table -class CDBrickIDTableTable : public CDTable { -private: - std::vector entries; - +class CDBrickIDTableTable : public CDTable> { public: void LoadValuesFromDatabase(); // Queries the table with a custom "where" clause std::vector Query(std::function predicate); - - const std::vector& GetEntries() const; }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDComponentsRegistryTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDComponentsRegistryTable.cpp index 3cf7ac62..4944c13b 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDComponentsRegistryTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDComponentsRegistryTable.cpp @@ -4,14 +4,15 @@ void CDComponentsRegistryTable::LoadValuesFromDatabase() { // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM ComponentsRegistry"); + auto& entries = GetEntriesMutable(); while (!tableData.eof()) { CDComponentsRegistry entry; entry.id = tableData.getIntField("id", -1); entry.component_type = static_cast(tableData.getIntField("component_type", 0)); entry.component_id = tableData.getIntField("component_id", -1); - this->mappedEntries.insert_or_assign(static_cast(entry.component_type) << 32 | static_cast(entry.id), entry.component_id); - this->mappedEntries.insert_or_assign(entry.id, 0); + entries.insert_or_assign(static_cast(entry.component_type) << 32 | static_cast(entry.id), entry.component_id); + entries.insert_or_assign(entry.id, 0); tableData.nextRow(); } @@ -20,10 +21,11 @@ void CDComponentsRegistryTable::LoadValuesFromDatabase() { } int32_t CDComponentsRegistryTable::GetByIDAndType(uint32_t id, eReplicaComponentType componentType, int32_t defaultValue) { - auto exists = mappedEntries.find(id); - if (exists != mappedEntries.end()) { - auto iter = mappedEntries.find(static_cast(componentType) << 32 | static_cast(id)); - return iter == mappedEntries.end() ? defaultValue : iter->second; + auto& entries = GetEntriesMutable(); + auto exists = entries.find(id); + if (exists != entries.end()) { + auto iter = entries.find(static_cast(componentType) << 32 | static_cast(id)); + return iter == entries.end() ? defaultValue : iter->second; } // Now get the data. Get all components of this entity so we dont do a query for each component @@ -38,14 +40,14 @@ int32_t CDComponentsRegistryTable::GetByIDAndType(uint32_t id, eReplicaComponent entry.component_type = static_cast(tableData.getIntField("component_type", 0)); entry.component_id = tableData.getIntField("component_id", -1); - this->mappedEntries.insert_or_assign(static_cast(entry.component_type) << 32 | static_cast(entry.id), entry.component_id); + entries.insert_or_assign(static_cast(entry.component_type) << 32 | static_cast(entry.id), entry.component_id); tableData.nextRow(); } - mappedEntries.insert_or_assign(id, 0); + entries.insert_or_assign(id, 0); - auto iter = this->mappedEntries.find(static_cast(componentType) << 32 | static_cast(id)); + auto iter = entries.find(static_cast(componentType) << 32 | static_cast(id)); - return iter == this->mappedEntries.end() ? defaultValue : iter->second; + return iter == entries.end() ? defaultValue : iter->second; } diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDComponentsRegistryTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDComponentsRegistryTable.h index fce4f6aa..2165f907 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDComponentsRegistryTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDComponentsRegistryTable.h @@ -7,16 +7,13 @@ enum class eReplicaComponentType : uint32_t; struct CDComponentsRegistry { - unsigned int id; //!< The LOT is used as the ID + uint32_t id; //!< The LOT is used as the ID eReplicaComponentType component_type; //!< See ComponentTypes enum for values - unsigned int component_id; //!< The ID used within the component's table (0 may either mean it's non-networked, or that the ID is actually 0 + uint32_t component_id; //!< The ID used within the component's table (0 may either mean it's non-networked, or that the ID is actually 0 }; -class CDComponentsRegistryTable : public CDTable { -private: - std::unordered_map mappedEntries; //id, component_type, component_id - +class CDComponentsRegistryTable : public CDTable> { public: void LoadValuesFromDatabase(); int32_t GetByIDAndType(uint32_t id, eReplicaComponentType componentType, int32_t defaultValue = 0); diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDCurrencyTableTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDCurrencyTableTable.cpp index 58c517f2..19ac7da0 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDCurrencyTableTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDCurrencyTableTable.cpp @@ -4,7 +4,7 @@ void CDCurrencyTableTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM CurrencyTable"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -15,7 +15,8 @@ void CDCurrencyTableTable::LoadValuesFromDatabase() { tableSize.finalize(); // Reserve the size - this->entries.reserve(size); + auto& entries = GetEntriesMutable(); + entries.reserve(size); // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM CurrencyTable"); @@ -27,7 +28,7 @@ void CDCurrencyTableTable::LoadValuesFromDatabase() { entry.maxvalue = tableData.getIntField("maxvalue", -1); entry.id = tableData.getIntField("id", -1); - this->entries.push_back(entry); + entries.push_back(entry); tableData.nextRow(); } @@ -35,15 +36,9 @@ void CDCurrencyTableTable::LoadValuesFromDatabase() { } std::vector CDCurrencyTableTable::Query(std::function predicate) { - - std::vector data = cpplinq::from(this->entries) + std::vector data = cpplinq::from(GetEntries()) >> cpplinq::where(predicate) >> cpplinq::to_vector(); return data; } - -const std::vector& CDCurrencyTableTable::GetEntries() const { - return this->entries; -} - diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDCurrencyTableTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDCurrencyTableTable.h index fd70a968..1cd6c142 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDCurrencyTableTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDCurrencyTableTable.h @@ -10,22 +10,17 @@ //! CurrencyTable Struct struct CDCurrencyTable { - unsigned int currencyIndex; //!< The Currency Index - unsigned int npcminlevel; //!< The minimum level of the npc - unsigned int minvalue; //!< The minimum currency - unsigned int maxvalue; //!< The maximum currency - unsigned int id; //!< The ID of the currency index + uint32_t currencyIndex; //!< The Currency Index + uint32_t npcminlevel; //!< The minimum level of the npc + uint32_t minvalue; //!< The minimum currency + uint32_t maxvalue; //!< The maximum currency + uint32_t id; //!< The ID of the currency index }; //! CurrencyTable table -class CDCurrencyTableTable : public CDTable { -private: - std::vector entries; - +class CDCurrencyTableTable : public CDTable> { public: void LoadValuesFromDatabase(); // Queries the table with a custom "where" clause std::vector Query(std::function predicate); - - const std::vector& GetEntries() const; }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDDestructibleComponentTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDDestructibleComponentTable.cpp index f92aec94..b1a6f699 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDDestructibleComponentTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDDestructibleComponentTable.cpp @@ -2,7 +2,7 @@ void CDDestructibleComponentTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM DestructibleComponent"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -13,7 +13,8 @@ void CDDestructibleComponentTable::LoadValuesFromDatabase() { tableSize.finalize(); // Reserve the size - this->entries.reserve(size); + auto& entries = GetEntriesMutable(); + entries.reserve(size); // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM DestructibleComponent"); @@ -34,7 +35,7 @@ void CDDestructibleComponentTable::LoadValuesFromDatabase() { entry.isSmashable = tableData.getIntField("isSmashable", -1) == 1 ? true : false; entry.difficultyLevel = tableData.getIntField("difficultyLevel", -1); - this->entries.push_back(entry); + entries.push_back(entry); tableData.nextRow(); } @@ -42,15 +43,9 @@ void CDDestructibleComponentTable::LoadValuesFromDatabase() { } std::vector CDDestructibleComponentTable::Query(std::function predicate) { - - std::vector data = cpplinq::from(this->entries) + std::vector data = cpplinq::from(GetEntries()) >> cpplinq::where(predicate) >> cpplinq::to_vector(); return data; } - -const std::vector& CDDestructibleComponentTable::GetEntries() const { - return this->entries; -} - diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDDestructibleComponentTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDDestructibleComponentTable.h index fb6ee4cd..3319907b 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDDestructibleComponentTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDDestructibleComponentTable.h @@ -4,30 +4,25 @@ #include "CDTable.h" struct CDDestructibleComponent { - unsigned int id; //!< The component ID from the ComponentsRegistry Table - int faction; //!< The Faction ID of the object + uint32_t id; //!< The component ID from the ComponentsRegistry Table + int32_t faction; //!< The Faction ID of the object std::string factionList; //!< A list of the faction IDs - int life; //!< The amount of life of the object - unsigned int imagination; //!< The amount of imagination of the object - int LootMatrixIndex; //!< The Loot Matrix Index - int CurrencyIndex; //!< The Currency Index - unsigned int level; //!< ??? + int32_t life; //!< The amount of life of the object + uint32_t imagination; //!< The amount of imagination of the object + int32_t LootMatrixIndex; //!< The Loot Matrix Index + int32_t CurrencyIndex; //!< The Currency Index + uint32_t level; //!< ??? float armor; //!< The amount of armor of the object - unsigned int death_behavior; //!< The behavior ID of the death behavior + uint32_t death_behavior; //!< The behavior ID of the death behavior bool isnpc; //!< Whether or not the object is an NPC - unsigned int attack_priority; //!< ??? + uint32_t attack_priority; //!< ??? bool isSmashable; //!< Whether or not the object is smashable - int difficultyLevel; //!< ??? + int32_t difficultyLevel; //!< ??? }; -class CDDestructibleComponentTable : public CDTable { -private: - std::vector entries; - +class CDDestructibleComponentTable : public CDTable> { public: void LoadValuesFromDatabase(); // Queries the table with a custom "where" clause std::vector Query(std::function predicate); - - const std::vector& GetEntries(void) const; }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDEmoteTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDEmoteTable.cpp index 77aa226c..28052819 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDEmoteTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDEmoteTable.cpp @@ -2,6 +2,7 @@ void CDEmoteTableTable::LoadValuesFromDatabase() { auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM Emotes"); + auto& entries = GetEntriesMutable(); while (!tableData.eof()) { CDEmoteTable entry; entry.ID = tableData.getIntField("id", -1); @@ -20,7 +21,8 @@ void CDEmoteTableTable::LoadValuesFromDatabase() { tableData.finalize(); } -CDEmoteTable* CDEmoteTableTable::GetEmote(int id) { +CDEmoteTable* CDEmoteTableTable::GetEmote(int32_t id) { + auto& entries = GetEntriesMutable(); auto itr = entries.find(id); return itr != entries.end() ? &itr->second : nullptr; } diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDEmoteTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDEmoteTable.h index a49d1c45..ff0b28d1 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDEmoteTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDEmoteTable.h @@ -16,22 +16,19 @@ struct CDEmoteTable { gateVersion = ""; } - int ID; + int32_t ID; std::string animationName; std::string iconFilename; - int locState; - int channel; + int32_t locState; + int32_t channel; bool locked; bool localize; std::string gateVersion; }; -class CDEmoteTableTable : public CDTable { -private: - std::map entries; - +class CDEmoteTableTable : public CDTable> { public: void LoadValuesFromDatabase(); // Returns an emote by ID - CDEmoteTable* GetEmote(int id); + CDEmoteTable* GetEmote(int32_t id); }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDFeatureGatingTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDFeatureGatingTable.cpp index 9b133155..407efff7 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDFeatureGatingTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDFeatureGatingTable.cpp @@ -3,7 +3,7 @@ void CDFeatureGatingTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM FeatureGating"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -14,7 +14,8 @@ void CDFeatureGatingTable::LoadValuesFromDatabase() { tableSize.finalize(); // Reserve the size - this->entries.reserve(size); + auto& entries = GetEntriesMutable(); + entries.reserve(size); // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM FeatureGating"); @@ -26,7 +27,7 @@ void CDFeatureGatingTable::LoadValuesFromDatabase() { entry.minor = tableData.getIntField("minor", -1); entry.description = tableData.getStringField("description", ""); - this->entries.push_back(entry); + entries.push_back(entry); tableData.nextRow(); } @@ -35,7 +36,8 @@ void CDFeatureGatingTable::LoadValuesFromDatabase() { std::vector CDFeatureGatingTable::Query(std::function predicate) { - std::vector data = cpplinq::from(this->entries) + auto& entries = GetEntriesMutable(); + std::vector data = cpplinq::from(entries) >> cpplinq::where(predicate) >> cpplinq::to_vector(); @@ -43,6 +45,7 @@ std::vector CDFeatureGatingTable::Query(std::function= entry) { return true; @@ -51,8 +54,3 @@ bool CDFeatureGatingTable::FeatureUnlocked(const CDFeatureGating& feature) const return false; } - -const std::vector& CDFeatureGatingTable::GetEntries() const { - return this->entries; -} - diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDFeatureGatingTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDFeatureGatingTable.h index 5df202e3..65f33395 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDFeatureGatingTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDFeatureGatingTable.h @@ -17,10 +17,7 @@ struct CDFeatureGating { } }; -class CDFeatureGatingTable : public CDTable { -private: - std::vector entries; - +class CDFeatureGatingTable : public CDTable> { public: void LoadValuesFromDatabase(); @@ -28,6 +25,4 @@ public: std::vector Query(std::function predicate); bool FeatureUnlocked(const CDFeatureGating& feature) const; - - const std::vector& GetEntries(void) const; }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDInventoryComponentTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDInventoryComponentTable.cpp index ffc8fee6..8cf3d8da 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDInventoryComponentTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDInventoryComponentTable.cpp @@ -3,7 +3,7 @@ void CDInventoryComponentTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM InventoryComponent"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -14,7 +14,8 @@ void CDInventoryComponentTable::LoadValuesFromDatabase() { tableSize.finalize(); // Reserve the size - this->entries.reserve(size); + auto& entries = GetEntriesMutable(); + entries.reserve(size); // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM InventoryComponent"); @@ -25,7 +26,7 @@ void CDInventoryComponentTable::LoadValuesFromDatabase() { entry.count = tableData.getIntField("count", -1); entry.equip = tableData.getIntField("equip", -1) == 1 ? true : false; - this->entries.push_back(entry); + entries.push_back(entry); tableData.nextRow(); } @@ -33,15 +34,9 @@ void CDInventoryComponentTable::LoadValuesFromDatabase() { } std::vector CDInventoryComponentTable::Query(std::function predicate) { - - std::vector data = cpplinq::from(this->entries) + std::vector data = cpplinq::from(GetEntries()) >> cpplinq::where(predicate) >> cpplinq::to_vector(); return data; } - -const std::vector& CDInventoryComponentTable::GetEntries() const { - return this->entries; -} - diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDInventoryComponentTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDInventoryComponentTable.h index 26d47ffe..361e1a90 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDInventoryComponentTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDInventoryComponentTable.h @@ -4,20 +4,15 @@ #include "CDTable.h" struct CDInventoryComponent { - unsigned int id; //!< The component ID for this object - unsigned int itemid; //!< The LOT of the object - unsigned int count; //!< The count of the items the object has + uint32_t id; //!< The component ID for this object + uint32_t itemid; //!< The LOT of the object + uint32_t count; //!< The count of the items the object has bool equip; //!< Whether or not to equip the item }; -class CDInventoryComponentTable : public CDTable { -private: - std::vector entries; - +class CDInventoryComponentTable : public CDTable> { public: void LoadValuesFromDatabase(); // Queries the table with a custom "where" clause std::vector Query(std::function predicate); - - const std::vector& GetEntries() const; }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDItemComponentTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDItemComponentTable.cpp index 5d6722f9..0d8b1ad9 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDItemComponentTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDItemComponentTable.cpp @@ -5,7 +5,7 @@ CDItemComponent CDItemComponentTable::Default = {}; void CDItemComponentTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM ItemComponent"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -17,6 +17,7 @@ void CDItemComponentTable::LoadValuesFromDatabase() { // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM ItemComponent"); + auto& entries = GetEntriesMutable(); while (!tableData.eof()) { CDItemComponent entry; entry.id = tableData.getIntField("id", -1); @@ -62,16 +63,17 @@ void CDItemComponentTable::LoadValuesFromDatabase() { entry.forgeType = tableData.getIntField("forgeType", -1); entry.SellMultiplier = tableData.getFloatField("SellMultiplier", -1.0f); - this->entries.insert(std::make_pair(entry.id, entry)); + entries.insert(std::make_pair(entry.id, entry)); tableData.nextRow(); } tableData.finalize(); } -const CDItemComponent& CDItemComponentTable::GetItemComponentByID(unsigned int skillID) { - const auto& it = this->entries.find(skillID); - if (it != this->entries.end()) { +const CDItemComponent& CDItemComponentTable::GetItemComponentByID(uint32_t skillID) { + auto& entries = GetEntriesMutable(); + const auto& it = entries.find(skillID); + if (it != entries.end()) { return it->second; } @@ -129,12 +131,12 @@ const CDItemComponent& CDItemComponentTable::GetItemComponentByID(unsigned int s entry.forgeType = tableData.getIntField("forgeType", -1); entry.SellMultiplier = tableData.getFloatField("SellMultiplier", -1.0f); - this->entries.insert(std::make_pair(entry.id, entry)); + entries.insert(std::make_pair(entry.id, entry)); tableData.nextRow(); } - const auto& it2 = this->entries.find(skillID); - if (it2 != this->entries.end()) { + const auto& it2 = entries.find(skillID); + if (it2 != entries.end()) { return it2->second; } diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDItemComponentTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDItemComponentTable.h index 685e5acd..60a3e412 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDItemComponentTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDItemComponentTable.h @@ -5,60 +5,57 @@ #include "dCommonVars.h" struct CDItemComponent { - unsigned int id; //!< The Component ID + uint32_t id; //!< The Component ID std::string equipLocation; //!< The equip location - unsigned int baseValue; //!< The monetary base value of the item + uint32_t baseValue; //!< The monetary base value of the item bool isKitPiece; //!< Whether or not the item belongs to a kit - unsigned int rarity; //!< The rarity of the item - unsigned int itemType; //!< The item type + uint32_t rarity; //!< The rarity of the item + uint32_t itemType; //!< The item type int64_t itemInfo; //!< The item info bool inLootTable; //!< Whether or not the item is in a loot table bool inVendor; //!< Whether or not the item is in a vendor inventory bool isUnique; //!< ??? bool isBOP; //!< ??? bool isBOE; //!< ??? - unsigned int reqFlagID; //!< User must have completed this flag to get the item - unsigned int reqSpecialtyID; //!< ??? - unsigned int reqSpecRank; //!< ??? - unsigned int reqAchievementID; //!< The required achievement must be completed - unsigned int stackSize; //!< The stack size of the item - unsigned int color1; //!< Something to do with item color... - unsigned int decal; //!< The decal of the item - unsigned int offsetGroupID; //!< Something to do with group IDs - unsigned int buildTypes; //!< Something to do with building + uint32_t reqFlagID; //!< User must have completed this flag to get the item + uint32_t reqSpecialtyID; //!< ??? + uint32_t reqSpecRank; //!< ??? + uint32_t reqAchievementID; //!< The required achievement must be completed + uint32_t stackSize; //!< The stack size of the item + uint32_t color1; //!< Something to do with item color... + uint32_t decal; //!< The decal of the item + uint32_t offsetGroupID; //!< Something to do with group IDs + uint32_t buildTypes; //!< Something to do with building std::string reqPrecondition; //!< The required precondition - unsigned int animationFlag; //!< The Animation Flag - unsigned int equipEffects; //!< The effect played when the item is equipped + uint32_t animationFlag; //!< The Animation Flag + uint32_t equipEffects; //!< The effect played when the item is equipped bool readyForQA; //!< ??? - unsigned int itemRating; //!< ??? + uint32_t itemRating; //!< ??? bool isTwoHanded; //!< Whether or not the item is double handed - unsigned int minNumRequired; //!< Maybe the minimum number required for a mission, or to own this object? - unsigned int delResIndex; //!< ??? - unsigned int currencyLOT; //!< ??? - unsigned int altCurrencyCost; //!< ??? + uint32_t minNumRequired; //!< Maybe the minimum number required for a mission, or to own this object? + uint32_t delResIndex; //!< ??? + uint32_t currencyLOT; //!< ??? + uint32_t altCurrencyCost; //!< ??? std::string subItems; //!< A comma seperate string of sub items (maybe for multi-itemed things like faction test gear set) UNUSED(std::string audioEventUse); //!< ??? bool noEquipAnimation; //!< Whether or not there is an equip animation - unsigned int commendationLOT; //!< The commendation LOT - unsigned int commendationCost; //!< The commendation cost + uint32_t commendationLOT; //!< The commendation LOT + uint32_t commendationCost; //!< The commendation cost UNUSED(std::string audioEquipMetaEventSet); //!< ??? std::string currencyCosts; //!< Used for crafting UNUSED(std::string ingredientInfo); //!< Unused - unsigned int locStatus; //!< ??? - unsigned int forgeType; //!< Forge Type + uint32_t locStatus; //!< ??? + uint32_t forgeType; //!< Forge Type float SellMultiplier; //!< Something to do with early vendors perhaps (but replaced) }; -class CDItemComponentTable : public CDTable { -private: - std::map entries; - +class CDItemComponentTable : public CDTable> { public: void LoadValuesFromDatabase(); static std::map ParseCraftingCurrencies(const CDItemComponent& itemComponent); // Gets an entry by ID - const CDItemComponent& GetItemComponentByID(unsigned int skillID); + const CDItemComponent& GetItemComponentByID(uint32_t skillID); static CDItemComponent Default; }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDItemSetSkillsTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDItemSetSkillsTable.cpp index 6fb1689e..79bd69ae 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDItemSetSkillsTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDItemSetSkillsTable.cpp @@ -3,7 +3,7 @@ void CDItemSetSkillsTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM ItemSetSkills"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -14,7 +14,8 @@ void CDItemSetSkillsTable::LoadValuesFromDatabase() { tableSize.finalize(); // Reserve the size - this->entries.reserve(size); + auto& entries = GetEntriesMutable(); + entries.reserve(size); // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM ItemSetSkills"); @@ -24,7 +25,7 @@ void CDItemSetSkillsTable::LoadValuesFromDatabase() { entry.SkillID = tableData.getIntField("SkillID", -1); entry.SkillCastType = tableData.getIntField("SkillCastType", -1); - this->entries.push_back(entry); + entries.push_back(entry); tableData.nextRow(); } @@ -32,22 +33,17 @@ void CDItemSetSkillsTable::LoadValuesFromDatabase() { } std::vector CDItemSetSkillsTable::Query(std::function predicate) { - - std::vector data = cpplinq::from(this->entries) + std::vector data = cpplinq::from(GetEntries()) >> cpplinq::where(predicate) >> cpplinq::to_vector(); return data; } -const std::vector& CDItemSetSkillsTable::GetEntries() const { - return this->entries; -} - -std::vector CDItemSetSkillsTable::GetBySkillID(unsigned int SkillSetID) { +std::vector CDItemSetSkillsTable::GetBySkillID(uint32_t SkillSetID) { std::vector toReturn; - for (CDItemSetSkills entry : this->entries) { + for (const auto& entry : GetEntries()) { if (entry.SkillSetID == SkillSetID) toReturn.push_back(entry); if (entry.SkillSetID > SkillSetID) return toReturn; //stop seeking in the db if it's not needed. } diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDItemSetSkillsTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDItemSetSkillsTable.h index 07321a7f..78e708ae 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDItemSetSkillsTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDItemSetSkillsTable.h @@ -4,21 +4,16 @@ #include "CDTable.h" struct CDItemSetSkills { - unsigned int SkillSetID; //!< The skill set ID - unsigned int SkillID; //!< The skill ID - unsigned int SkillCastType; //!< The skill cast type + uint32_t SkillSetID; //!< The skill set ID + uint32_t SkillID; //!< The skill ID + uint32_t SkillCastType; //!< The skill cast type }; -class CDItemSetSkillsTable : public CDTable { -private: - std::vector entries; - +class CDItemSetSkillsTable : public CDTable> { public: void LoadValuesFromDatabase(); // Queries the table with a custom "where" clause std::vector Query(std::function predicate); - const std::vector& GetEntries() const; - - std::vector GetBySkillID(unsigned int SkillSetID); + std::vector GetBySkillID(uint32_t SkillSetID); }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDItemSetsTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDItemSetsTable.cpp index de70e180..77b3b1e7 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDItemSetsTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDItemSetsTable.cpp @@ -3,7 +3,7 @@ void CDItemSetsTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM ItemSets"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -14,7 +14,8 @@ void CDItemSetsTable::LoadValuesFromDatabase() { tableSize.finalize(); // Reserve the size - this->entries.reserve(size); + auto& entries = GetEntriesMutable(); + entries.reserve(size); // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM ItemSets"); @@ -36,7 +37,7 @@ void CDItemSetsTable::LoadValuesFromDatabase() { entry.kitID = tableData.getIntField("kitID", -1); entry.priority = tableData.getFloatField("priority", -1.0f); - this->entries.push_back(entry); + entries.push_back(entry); tableData.nextRow(); } @@ -45,14 +46,9 @@ void CDItemSetsTable::LoadValuesFromDatabase() { std::vector CDItemSetsTable::Query(std::function predicate) { - std::vector data = cpplinq::from(this->entries) + std::vector data = cpplinq::from(GetEntries()) >> cpplinq::where(predicate) >> cpplinq::to_vector(); return data; } - -const std::vector& CDItemSetsTable::GetEntries() const { - return this->entries; -} - diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDItemSetsTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDItemSetsTable.h index a3a738b1..45b91590 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDItemSetsTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDItemSetsTable.h @@ -4,32 +4,27 @@ #include "CDTable.h" struct CDItemSets { - unsigned int setID; //!< The item set ID - unsigned int locStatus; //!< The loc status + uint32_t setID; //!< The item set ID + uint32_t locStatus; //!< The loc status std::string itemIDs; //!< THe item IDs - unsigned int kitType; //!< The item kit type - unsigned int kitRank; //!< The item kit rank - unsigned int kitImage; //!< The item kit image - unsigned int skillSetWith2; //!< The skill set with 2 - unsigned int skillSetWith3; //!< The skill set with 3 - unsigned int skillSetWith4; //!< The skill set with 4 - unsigned int skillSetWith5; //!< The skill set with 5 - unsigned int skillSetWith6; //!< The skill set with 6 + uint32_t kitType; //!< The item kit type + uint32_t kitRank; //!< The item kit rank + uint32_t kitImage; //!< The item kit image + uint32_t skillSetWith2; //!< The skill set with 2 + uint32_t skillSetWith3; //!< The skill set with 3 + uint32_t skillSetWith4; //!< The skill set with 4 + uint32_t skillSetWith5; //!< The skill set with 5 + uint32_t skillSetWith6; //!< The skill set with 6 bool localize; //!< Whether or localize std::string gate_version; //!< The gate version - unsigned int kitID; //!< The kit ID + uint32_t kitID; //!< The kit ID float priority; //!< The priority }; -class CDItemSetsTable : public CDTable { -private: - std::vector entries; - +class CDItemSetsTable : public CDTable> { public: void LoadValuesFromDatabase(); // Queries the table with a custom "where" clause std::vector Query(std::function predicate); - - const std::vector& GetEntries(void) const; }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDLevelProgressionLookupTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDLevelProgressionLookupTable.cpp index d0a6ca93..284cf484 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDLevelProgressionLookupTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDLevelProgressionLookupTable.cpp @@ -3,7 +3,7 @@ void CDLevelProgressionLookupTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM LevelProgressionLookup"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -14,7 +14,8 @@ void CDLevelProgressionLookupTable::LoadValuesFromDatabase() { tableSize.finalize(); // Reserve the size - this->entries.reserve(size); + auto& entries = GetEntriesMutable(); + entries.reserve(size); // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM LevelProgressionLookup"); @@ -24,7 +25,7 @@ void CDLevelProgressionLookupTable::LoadValuesFromDatabase() { entry.requiredUScore = tableData.getIntField("requiredUScore", -1); entry.BehaviorEffect = tableData.getStringField("BehaviorEffect", ""); - this->entries.push_back(entry); + entries.push_back(entry); tableData.nextRow(); } @@ -33,14 +34,9 @@ void CDLevelProgressionLookupTable::LoadValuesFromDatabase() { std::vector CDLevelProgressionLookupTable::Query(std::function predicate) { - std::vector data = cpplinq::from(this->entries) + std::vector data = cpplinq::from(GetEntries()) >> cpplinq::where(predicate) >> cpplinq::to_vector(); return data; } - -const std::vector& CDLevelProgressionLookupTable::GetEntries() const { - return this->entries; -} - diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDLevelProgressionLookupTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDLevelProgressionLookupTable.h index 6b68bd0d..050d910e 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDLevelProgressionLookupTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDLevelProgressionLookupTable.h @@ -4,20 +4,15 @@ #include "CDTable.h" struct CDLevelProgressionLookup { - unsigned int id; //!< The Level ID - unsigned int requiredUScore; //!< The required LEGO Score + uint32_t id; //!< The Level ID + uint32_t requiredUScore; //!< The required LEGO Score std::string BehaviorEffect; //!< The behavior effect attached to this }; -class CDLevelProgressionLookupTable : public CDTable { -private: - std::vector entries; - +class CDLevelProgressionLookupTable : public CDTable> { public: void LoadValuesFromDatabase(); // Queries the table with a custom "where" clause std::vector Query(std::function predicate); - - const std::vector& GetEntries() const; }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDLootMatrixTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDLootMatrixTable.cpp index 4755c116..cd8ae5c4 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDLootMatrixTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDLootMatrixTable.cpp @@ -16,7 +16,7 @@ CDLootMatrix CDLootMatrixTable::ReadRow(CppSQLite3Query& tableData) const { void CDLootMatrixTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM LootMatrix"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -25,7 +25,8 @@ void CDLootMatrixTable::LoadValuesFromDatabase() { } // Reserve the size - this->entries.reserve(size); + auto& entries = GetEntriesMutable(); + entries.reserve(size); // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM LootMatrix"); @@ -33,14 +34,15 @@ void CDLootMatrixTable::LoadValuesFromDatabase() { CDLootMatrix entry; uint32_t lootMatrixIndex = tableData.getIntField("LootMatrixIndex", -1); - this->entries[lootMatrixIndex].push_back(ReadRow(tableData)); + entries[lootMatrixIndex].push_back(ReadRow(tableData)); tableData.nextRow(); } } const LootMatrixEntries& CDLootMatrixTable::GetMatrix(uint32_t matrixId) { - auto itr = this->entries.find(matrixId); - if (itr != this->entries.end()) { + auto& entries = GetEntriesMutable(); + auto itr = entries.find(matrixId); + if (itr != entries.end()) { return itr->second; } @@ -49,10 +51,10 @@ const LootMatrixEntries& CDLootMatrixTable::GetMatrix(uint32_t matrixId) { auto tableData = query.execQuery(); while (!tableData.eof()) { - this->entries[matrixId].push_back(ReadRow(tableData)); + entries[matrixId].push_back(ReadRow(tableData)); tableData.nextRow(); } - return this->entries[matrixId]; + return entries[matrixId]; } diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDLootMatrixTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDLootMatrixTable.h index 551583f6..b0ce7e0f 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDLootMatrixTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDLootMatrixTable.h @@ -4,19 +4,19 @@ #include "CDTable.h" struct CDLootMatrix { - unsigned int LootTableIndex; //!< The Loot Table Index - unsigned int RarityTableIndex; //!< The Rarity Table Index + uint32_t LootTableIndex; //!< The Loot Table Index + uint32_t RarityTableIndex; //!< The Rarity Table Index float percent; //!< The percent that this matrix is used? - unsigned int minToDrop; //!< The minimum amount of loot from this matrix to drop - unsigned int maxToDrop; //!< The maximum amount of loot from this matrix to drop - unsigned int flagID; //!< ??? + uint32_t minToDrop; //!< The minimum amount of loot from this matrix to drop + uint32_t maxToDrop; //!< The maximum amount of loot from this matrix to drop + uint32_t flagID; //!< ??? UNUSED(std::string gate_version); //!< The Gate Version }; typedef uint32_t LootMatrixIndex; typedef std::vector LootMatrixEntries; -class CDLootMatrixTable : public CDTable { +class CDLootMatrixTable : public CDTable> { public: void LoadValuesFromDatabase(); @@ -24,6 +24,5 @@ public: const LootMatrixEntries& GetMatrix(uint32_t matrixId); private: CDLootMatrix ReadRow(CppSQLite3Query& tableData) const; - std::unordered_map entries; }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDLootTableTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDLootTableTable.cpp index 9ee875df..d5e9d4dc 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDLootTableTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDLootTableTable.cpp @@ -6,8 +6,8 @@ // Sort the tables by their rarity so the highest rarity items are first. void SortTable(LootTableEntries& table) { - auto* componentsRegistryTable = CDClientManager::Instance().GetTable(); - auto* itemComponentTable = CDClientManager::Instance().GetTable(); + auto* componentsRegistryTable = CDClientManager::GetTable(); + auto* itemComponentTable = CDClientManager::GetTable(); // We modify the table in place so the outer loop keeps track of what is sorted // and the inner loop finds the highest rarity item and swaps it with the current position // of the outer loop. @@ -40,7 +40,7 @@ CDLootTable CDLootTableTable::ReadRow(CppSQLite3Query& tableData) const { void CDLootTableTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM LootTable"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -49,7 +49,8 @@ void CDLootTableTable::LoadValuesFromDatabase() { } // Reserve the size - this->entries.reserve(size); + auto& entries = GetEntriesMutable(); + entries.reserve(size); // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM LootTable"); @@ -57,17 +58,18 @@ void CDLootTableTable::LoadValuesFromDatabase() { CDLootTable entry; uint32_t lootTableIndex = tableData.getIntField("LootTableIndex", -1); - this->entries[lootTableIndex].push_back(ReadRow(tableData)); + entries[lootTableIndex].push_back(ReadRow(tableData)); tableData.nextRow(); } - for (auto& [id, table] : this->entries) { + for (auto& [id, table] : entries) { SortTable(table); } } const LootTableEntries& CDLootTableTable::GetTable(uint32_t tableId) { - auto itr = this->entries.find(tableId); - if (itr != this->entries.end()) { + auto& entries = GetEntriesMutable(); + auto itr = entries.find(tableId); + if (itr != entries.end()) { return itr->second; } @@ -77,10 +79,10 @@ const LootTableEntries& CDLootTableTable::GetTable(uint32_t tableId) { while (!tableData.eof()) { CDLootTable entry; - this->entries[tableId].push_back(ReadRow(tableData)); + entries[tableId].push_back(ReadRow(tableData)); tableData.nextRow(); } - SortTable(this->entries[tableId]); + SortTable(entries[tableId]); - return this->entries[tableId]; + return entries[tableId]; } diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDLootTableTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDLootTableTable.h index b8ac2066..416bd87a 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDLootTableTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDLootTableTable.h @@ -4,19 +4,18 @@ #include "CDTable.h" struct CDLootTable { - unsigned int itemid; //!< The LOT of the item - unsigned int LootTableIndex; //!< The Loot Table Index + uint32_t itemid; //!< The LOT of the item + uint32_t LootTableIndex; //!< The Loot Table Index bool MissionDrop; //!< Whether or not this loot table is a mission drop - unsigned int sortPriority; //!< The sorting priority + uint32_t sortPriority; //!< The sorting priority }; typedef uint32_t LootTableIndex; typedef std::vector LootTableEntries; -class CDLootTableTable : public CDTable { +class CDLootTableTable : public CDTable> { private: CDLootTable ReadRow(CppSQLite3Query& tableData) const; - std::unordered_map entries; public: void LoadValuesFromDatabase(); diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDMissionEmailTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDMissionEmailTable.cpp index 0babef13..1123bfec 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDMissionEmailTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDMissionEmailTable.cpp @@ -1,9 +1,9 @@ #include "CDMissionEmailTable.h" -void CDMissionEmailTable::LoadValuesFromDatabase() { +void CDMissionEmailTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM MissionEmail"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -14,7 +14,8 @@ void CDMissionEmailTable::LoadValuesFromDatabase() { tableSize.finalize(); // Reserve the size - this->entries.reserve(size); + auto& entries = GetEntriesMutable(); + entries.reserve(size); // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM MissionEmail"); @@ -29,7 +30,7 @@ void CDMissionEmailTable::LoadValuesFromDatabase() { entry.locStatus = tableData.getIntField("locStatus", -1); entry.gate_version = tableData.getStringField("gate_version", ""); - this->entries.push_back(entry); + entries.push_back(entry); tableData.nextRow(); } @@ -38,15 +39,9 @@ void CDMissionEmailTable::LoadValuesFromDatabase() { //! Queries the table with a custom "where" clause std::vector CDMissionEmailTable::Query(std::function predicate) { - - std::vector data = cpplinq::from(this->entries) + std::vector data = cpplinq::from(GetEntries()) >> cpplinq::where(predicate) >> cpplinq::to_vector(); return data; } - -//! Gets all the entries in the table -const std::vector& CDMissionEmailTable::GetEntries() const { - return this->entries; -} diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDMissionEmailTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDMissionEmailTable.h index 954da78e..ac2dba81 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDMissionEmailTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDMissionEmailTable.h @@ -4,25 +4,20 @@ #include "CDTable.h" struct CDMissionEmail { - unsigned int ID; - unsigned int messageType; - unsigned int notificationGroup; - unsigned int missionID; - unsigned int attachmentLOT; + uint32_t ID; + uint32_t messageType; + uint32_t notificationGroup; + uint32_t missionID; + uint32_t attachmentLOT; bool localize; - unsigned int locStatus; + uint32_t locStatus; std::string gate_version; }; -class CDMissionEmailTable : public CDTable { -private: - std::vector entries; - +class CDMissionEmailTable : public CDTable> { public: void LoadValuesFromDatabase(); // Queries the table with a custom "where" clause std::vector Query(std::function predicate); - - const std::vector& GetEntries() const; }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDMissionNPCComponentTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDMissionNPCComponentTable.cpp index 59ea40b7..efe284d4 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDMissionNPCComponentTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDMissionNPCComponentTable.cpp @@ -3,7 +3,7 @@ void CDMissionNPCComponentTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM MissionNPCComponent"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -14,7 +14,8 @@ void CDMissionNPCComponentTable::LoadValuesFromDatabase() { tableSize.finalize(); // Reserve the size - this->entries.reserve(size); + auto& entries = GetEntriesMutable(); + entries.reserve(size); // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM MissionNPCComponent"); @@ -26,7 +27,7 @@ void CDMissionNPCComponentTable::LoadValuesFromDatabase() { entry.acceptsMission = tableData.getIntField("acceptsMission", -1) == 1 ? true : false; entry.gate_version = tableData.getStringField("gate_version", ""); - this->entries.push_back(entry); + entries.push_back(entry); tableData.nextRow(); } @@ -36,15 +37,9 @@ void CDMissionNPCComponentTable::LoadValuesFromDatabase() { //! Queries the table with a custom "where" clause std::vector CDMissionNPCComponentTable::Query(std::function predicate) { - std::vector data = cpplinq::from(this->entries) + std::vector data = cpplinq::from(GetEntries()) >> cpplinq::where(predicate) >> cpplinq::to_vector(); return data; } - -//! Gets all the entries in the table -const std::vector& CDMissionNPCComponentTable::GetEntries() const { - return this->entries; -} - diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDMissionNPCComponentTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDMissionNPCComponentTable.h index 2b2b3303..1eba2fad 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDMissionNPCComponentTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDMissionNPCComponentTable.h @@ -4,24 +4,17 @@ #include "CDTable.h" struct CDMissionNPCComponent { - unsigned int id; //!< The ID - unsigned int missionID; //!< The Mission ID + uint32_t id; //!< The ID + uint32_t missionID; //!< The Mission ID bool offersMission; //!< Whether or not this NPC offers a mission bool acceptsMission; //!< Whether or not this NPC accepts a mission std::string gate_version; //!< The gate version }; -class CDMissionNPCComponentTable : public CDTable { -private: - std::vector entries; - +class CDMissionNPCComponentTable : public CDTable> { public: void LoadValuesFromDatabase(); // Queries the table with a custom "where" clause std::vector Query(std::function predicate); - - // Gets all the entries in the table - const std::vector& GetEntries() const; - }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDMissionTasksTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDMissionTasksTable.cpp index 9795ea8f..c5b6620f 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDMissionTasksTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDMissionTasksTable.cpp @@ -3,7 +3,7 @@ void CDMissionTasksTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM MissionTasks"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -14,7 +14,8 @@ void CDMissionTasksTable::LoadValuesFromDatabase() { tableSize.finalize(); // Reserve the size - this->entries.reserve(size); + auto& entries = GetEntriesMutable(); + entries.reserve(size); // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM MissionTasks"); @@ -34,7 +35,7 @@ void CDMissionTasksTable::LoadValuesFromDatabase() { UNUSED(entry.localize = tableData.getIntField("localize", -1) == 1 ? true : false); UNUSED(entry.gate_version = tableData.getStringField("gate_version", "")); - this->entries.push_back(entry); + entries.push_back(entry); tableData.nextRow(); } @@ -43,7 +44,7 @@ void CDMissionTasksTable::LoadValuesFromDatabase() { std::vector CDMissionTasksTable::Query(std::function predicate) { - std::vector data = cpplinq::from(this->entries) + std::vector data = cpplinq::from(GetEntries()) >> cpplinq::where(predicate) >> cpplinq::to_vector(); @@ -53,7 +54,8 @@ std::vector CDMissionTasksTable::Query(std::function CDMissionTasksTable::GetByMissionID(uint32_t missionID) { std::vector tasks; - for (auto& entry : this->entries) { + // TODO: this should not be linear(?) and also shouldnt need to be a pointer + for (auto& entry : GetEntriesMutable()) { if (entry.id == missionID) { tasks.push_back(&entry); } @@ -62,7 +64,6 @@ std::vector CDMissionTasksTable::GetByMissionID(uint32_t missio return tasks; } -const std::vector& CDMissionTasksTable::GetEntries() const { - return this->entries; +const typename CDMissionTasksTable::StorageType& CDMissionTasksTable::GetEntries() const { + return CDTable::GetEntries(); } - diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDMissionTasksTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDMissionTasksTable.h index 0b169db8..97553359 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDMissionTasksTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDMissionTasksTable.h @@ -4,25 +4,22 @@ #include "CDTable.h" struct CDMissionTasks { - unsigned int id; //!< The Mission ID that the task belongs to - UNUSED(unsigned int locStatus); //!< ??? - unsigned int taskType; //!< The task type - unsigned int target; //!< The mission target + uint32_t id; //!< The Mission ID that the task belongs to + UNUSED(uint32_t locStatus); //!< ??? + uint32_t taskType; //!< The task type + uint32_t target; //!< The mission target std::string targetGroup; //!< The mission target group - int targetValue; //!< The target value + int32_t targetValue; //!< The target value std::string taskParam1; //!< The task param 1 UNUSED(std::string largeTaskIcon); //!< ??? - UNUSED(unsigned int IconID); //!< ??? - unsigned int uid; //!< ??? - UNUSED(unsigned int largeTaskIconID); //!< ??? + UNUSED(uint32_t IconID); //!< ??? + uint32_t uid; //!< ??? + UNUSED(uint32_t largeTaskIconID); //!< ??? UNUSED(bool localize); //!< Whether or not the task should be localized UNUSED(std::string gate_version); //!< ??? }; -class CDMissionTasksTable : public CDTable { -private: - std::vector entries; - +class CDMissionTasksTable : public CDTable> { public: void LoadValuesFromDatabase(); // Queries the table with a custom "where" clause @@ -30,6 +27,7 @@ public: std::vector GetByMissionID(uint32_t missionID); - const std::vector& GetEntries() const; + // TODO: Remove this and replace it with a proper lookup function. + const CDTable::StorageType& GetEntries() const; }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDMissionsTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDMissionsTable.cpp index 37f0c81c..8862b1db 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDMissionsTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDMissionsTable.cpp @@ -5,7 +5,7 @@ CDMissions CDMissionsTable::Default = {}; void CDMissionsTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM Missions"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -16,7 +16,8 @@ void CDMissionsTable::LoadValuesFromDatabase() { tableSize.finalize(); // Reserve the size - this->entries.reserve(size); + auto& entries = GetEntriesMutable(); + entries.reserve(size); // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM Missions"); @@ -75,7 +76,7 @@ void CDMissionsTable::LoadValuesFromDatabase() { UNUSED(entry.locStatus = tableData.getIntField("locStatus", -1)); entry.reward_bankinventory = tableData.getIntField("reward_bankinventory", -1); - this->entries.push_back(entry); + entries.push_back(entry); tableData.nextRow(); } @@ -86,19 +87,15 @@ void CDMissionsTable::LoadValuesFromDatabase() { std::vector CDMissionsTable::Query(std::function predicate) { - std::vector data = cpplinq::from(this->entries) + std::vector data = cpplinq::from(GetEntries()) >> cpplinq::where(predicate) >> cpplinq::to_vector(); return data; } -const std::vector& CDMissionsTable::GetEntries(void) const { - return this->entries; -} - const CDMissions* CDMissionsTable::GetPtrByMissionID(uint32_t missionID) const { - for (const auto& entry : entries) { + for (const auto& entry : GetEntries()) { if (entry.id == missionID) { return const_cast(&entry); } @@ -108,7 +105,7 @@ const CDMissions* CDMissionsTable::GetPtrByMissionID(uint32_t missionID) const { } const CDMissions& CDMissionsTable::GetByMissionID(uint32_t missionID, bool& found) const { - for (const auto& entry : entries) { + for (const auto& entry : GetEntries()) { if (entry.id == missionID) { found = true; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDMissionsTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDMissionsTable.h index c8ddc2a3..6ba7b19e 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDMissionsTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDMissionsTable.h @@ -6,72 +6,66 @@ #include struct CDMissions { - int id; //!< The Mission ID + int32_t id; //!< The Mission ID std::string defined_type; //!< The type of mission std::string defined_subtype; //!< The subtype of the mission - int UISortOrder; //!< The UI Sort Order for the mission - int offer_objectID; //!< The LOT of the mission giver - int target_objectID; //!< The LOT of the mission's target + int32_t UISortOrder; //!< The UI Sort Order for the mission + int32_t offer_objectID; //!< The LOT of the mission giver + int32_t target_objectID; //!< The LOT of the mission's target int64_t reward_currency; //!< The amount of currency to reward the player - int LegoScore; //!< The amount of LEGO Score to reward the player + int32_t LegoScore; //!< The amount of LEGO Score to reward the player int64_t reward_reputation; //!< The reputation to award the player bool isChoiceReward; //!< Whether or not the user has the option to choose their loot - int reward_item1; //!< The first rewarded item - int reward_item1_count; //!< The count of the first item to be rewarded - int reward_item2; //!< The second rewarded item - int reward_item2_count; //!< The count of the second item to be rewarded - int reward_item3; //!< The third rewarded item - int reward_item3_count; //!< The count of the third item to be rewarded - int reward_item4; //!< The fourth rewarded item - int reward_item4_count; //!< The count of the fourth item to be rewarded - int reward_emote; //!< The first emote to be rewarded - int reward_emote2; //!< The second emote to be rewarded - int reward_emote3; //!< The third emote to be rewarded - int reward_emote4; //!< The fourth emote to be rewarded - int reward_maximagination; //!< The amount of max imagination to reward - int reward_maxhealth; //!< The amount of max health to reward - int reward_maxinventory; //!< The amount of max inventory to reward - int reward_maxmodel; //!< ??? - int reward_maxwidget; //!< ??? - int reward_maxwallet; //!< ??? + int32_t reward_item1; //!< The first rewarded item + int32_t reward_item1_count; //!< The count of the first item to be rewarded + int32_t reward_item2; //!< The second rewarded item + int32_t reward_item2_count; //!< The count of the second item to be rewarded + int32_t reward_item3; //!< The third rewarded item + int32_t reward_item3_count; //!< The count of the third item to be rewarded + int32_t reward_item4; //!< The fourth rewarded item + int32_t reward_item4_count; //!< The count of the fourth item to be rewarded + int32_t reward_emote; //!< The first emote to be rewarded + int32_t reward_emote2; //!< The second emote to be rewarded + int32_t reward_emote3; //!< The third emote to be rewarded + int32_t reward_emote4; //!< The fourth emote to be rewarded + int32_t reward_maximagination; //!< The amount of max imagination to reward + int32_t reward_maxhealth; //!< The amount of max health to reward + int32_t reward_maxinventory; //!< The amount of max inventory to reward + int32_t reward_maxmodel; //!< ??? + int32_t reward_maxwidget; //!< ??? + int32_t reward_maxwallet; //!< ??? bool repeatable; //!< Whether or not this mission can be repeated (for instance, is it a daily mission) int64_t reward_currency_repeatable; //!< The repeatable reward - int reward_item1_repeatable; //!< The first rewarded item - int reward_item1_repeat_count; //!< The count of the first item to be rewarded - int reward_item2_repeatable; //!< The second rewarded item - int reward_item2_repeat_count; //!< The count of the second item to be rewarded - int reward_item3_repeatable; //!< The third rewarded item - int reward_item3_repeat_count; //!< The count of the third item to be rewarded - int reward_item4_repeatable; //!< The fourth rewarded item - int reward_item4_repeat_count; //!< The count of the fourth item to be rewarded - int time_limit; //!< The time limit of the mission + int32_t reward_item1_repeatable; //!< The first rewarded item + int32_t reward_item1_repeat_count; //!< The count of the first item to be rewarded + int32_t reward_item2_repeatable; //!< The second rewarded item + int32_t reward_item2_repeat_count; //!< The count of the second item to be rewarded + int32_t reward_item3_repeatable; //!< The third rewarded item + int32_t reward_item3_repeat_count; //!< The count of the third item to be rewarded + int32_t reward_item4_repeatable; //!< The fourth rewarded item + int32_t reward_item4_repeat_count; //!< The count of the fourth item to be rewarded + int32_t time_limit; //!< The time limit of the mission bool isMission; //!< Maybe to differentiate between missions and achievements? - int missionIconID; //!< The mission icon ID + int32_t missionIconID; //!< The mission icon ID std::string prereqMissionID; //!< A '|' seperated list of prerequisite missions bool localize; //!< Whether or not to localize the mission bool inMOTD; //!< In Match of the Day(?) int64_t cooldownTime; //!< The mission cooldown time bool isRandom; //!< ??? std::string randomPool; //!< ??? - int UIPrereqID; //!< ??? + int32_t UIPrereqID; //!< ??? UNUSED(std::string gate_version); //!< The gate version UNUSED(std::string HUDStates); //!< ??? - UNUSED(int locStatus); //!< ??? - int reward_bankinventory; //!< The amount of bank space this mission rewards + UNUSED(int32_t locStatus); //!< ??? + int32_t reward_bankinventory; //!< The amount of bank space this mission rewards }; -class CDMissionsTable : public CDTable { -private: - std::vector entries; - +class CDMissionsTable : public CDTable> { public: void LoadValuesFromDatabase(); // Queries the table with a custom "where" clause std::vector Query(std::function predicate); - // Gets all the entries in the table - const std::vector& GetEntries() const; - const CDMissions* GetPtrByMissionID(uint32_t missionID) const; const CDMissions& GetByMissionID(uint32_t missionID, bool& found) const; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDMovementAIComponentTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDMovementAIComponentTable.cpp index 35b46416..48964a59 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDMovementAIComponentTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDMovementAIComponentTable.cpp @@ -3,7 +3,7 @@ void CDMovementAIComponentTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM MovementAIComponent"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -14,7 +14,8 @@ void CDMovementAIComponentTable::LoadValuesFromDatabase() { tableSize.finalize(); // Reserve the size - this->entries.reserve(size); + auto& entries = GetEntriesMutable(); + entries.reserve(size); // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM MovementAIComponent"); @@ -29,7 +30,7 @@ void CDMovementAIComponentTable::LoadValuesFromDatabase() { entry.WanderRadius = tableData.getFloatField("WanderRadius", -1.0f); entry.attachedPath = tableData.getStringField("attachedPath", ""); - this->entries.push_back(entry); + entries.push_back(entry); tableData.nextRow(); } @@ -38,14 +39,9 @@ void CDMovementAIComponentTable::LoadValuesFromDatabase() { std::vector CDMovementAIComponentTable::Query(std::function predicate) { - std::vector data = cpplinq::from(this->entries) + std::vector data = cpplinq::from(GetEntries()) >> cpplinq::where(predicate) >> cpplinq::to_vector(); return data; } - -const std::vector& CDMovementAIComponentTable::GetEntries(void) const { - return this->entries; -} - diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDMovementAIComponentTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDMovementAIComponentTable.h index b40694bd..34d01e3d 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDMovementAIComponentTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDMovementAIComponentTable.h @@ -4,7 +4,7 @@ #include "CDTable.h" struct CDMovementAIComponent { - unsigned int id; + uint32_t id; std::string MovementType; float WanderChance; float WanderDelayMin; @@ -14,15 +14,9 @@ struct CDMovementAIComponent { std::string attachedPath; }; -class CDMovementAIComponentTable : public CDTable { -private: - std::vector entries; - +class CDMovementAIComponentTable : public CDTable> { public: void LoadValuesFromDatabase(); // Queries the table with a custom "where" clause std::vector Query(std::function predicate); - - // Gets all the entries in the table - const std::vector& GetEntries() const; }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDObjectSkillsTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDObjectSkillsTable.cpp index 2439622c..9933fe7f 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDObjectSkillsTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDObjectSkillsTable.cpp @@ -3,7 +3,7 @@ void CDObjectSkillsTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM ObjectSkills"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -14,7 +14,8 @@ void CDObjectSkillsTable::LoadValuesFromDatabase() { tableSize.finalize(); // Reserve the size - this->entries.reserve(size); + auto& entries = GetEntriesMutable(); + entries.reserve(size); // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM ObjectSkills"); @@ -25,7 +26,7 @@ void CDObjectSkillsTable::LoadValuesFromDatabase() { entry.castOnType = tableData.getIntField("castOnType", -1); entry.AICombatWeight = tableData.getIntField("AICombatWeight", -1); - this->entries.push_back(entry); + entries.push_back(entry); tableData.nextRow(); } @@ -34,13 +35,9 @@ void CDObjectSkillsTable::LoadValuesFromDatabase() { std::vector CDObjectSkillsTable::Query(std::function predicate) { - std::vector data = cpplinq::from(this->entries) + std::vector data = cpplinq::from(GetEntries()) >> cpplinq::where(predicate) >> cpplinq::to_vector(); return data; } - -const std::vector& CDObjectSkillsTable::GetEntries() const { - return this->entries; -} diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDObjectSkillsTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDObjectSkillsTable.h index bd9929e2..a2a8d440 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDObjectSkillsTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDObjectSkillsTable.h @@ -4,23 +4,16 @@ #include "CDTable.h" struct CDObjectSkills { - unsigned int objectTemplate; //!< The LOT of the item - unsigned int skillID; //!< The Skill ID of the object - unsigned int castOnType; //!< ??? - unsigned int AICombatWeight; //!< ??? + uint32_t objectTemplate; //!< The LOT of the item + uint32_t skillID; //!< The Skill ID of the object + uint32_t castOnType; //!< ??? + uint32_t AICombatWeight; //!< ??? }; -class CDObjectSkillsTable : public CDTable { -private: - std::vector entries; - +class CDObjectSkillsTable : public CDTable> { public: void LoadValuesFromDatabase(); // Queries the table with a custom "where" clause std::vector Query(std::function predicate); - - // Gets all the entries in the table - const std::vector& GetEntries() const; - }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDObjectsTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDObjectsTable.cpp index d3094b68..d1f6771e 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDObjectsTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDObjectsTable.cpp @@ -1,8 +1,12 @@ #include "CDObjectsTable.h" +namespace { + CDObjects m_default; +}; + void CDObjectsTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM Objects"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -14,6 +18,7 @@ void CDObjectsTable::LoadValuesFromDatabase() { // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM Objects"); + auto& entries = GetEntriesMutable(); while (!tableData.eof()) { CDObjects entry; entry.id = tableData.getIntField("id", -1); @@ -31,7 +36,7 @@ void CDObjectsTable::LoadValuesFromDatabase() { UNUSED_COLUMN(entry.gate_version = tableData.getStringField("gate_version", "");) UNUSED_COLUMN(entry.HQ_valid = tableData.getIntField("HQ_valid", -1);) - this->entries.insert(std::make_pair(entry.id, entry)); + entries.insert(std::make_pair(entry.id, entry)); tableData.nextRow(); } @@ -40,9 +45,10 @@ void CDObjectsTable::LoadValuesFromDatabase() { m_default.id = 0; } -const CDObjects& CDObjectsTable::GetByID(unsigned int LOT) { - const auto& it = this->entries.find(LOT); - if (it != this->entries.end()) { +const CDObjects& CDObjectsTable::GetByID(uint32_t LOT) { + auto& entries = GetEntriesMutable(); + const auto& it = entries.find(LOT); + if (it != entries.end()) { return it->second; } @@ -51,7 +57,7 @@ const CDObjects& CDObjectsTable::GetByID(unsigned int LOT) { auto tableData = query.execQuery(); if (tableData.eof()) { - this->entries.insert(std::make_pair(LOT, m_default)); + entries.insert(std::make_pair(LOT, m_default)); return m_default; } @@ -73,7 +79,7 @@ const CDObjects& CDObjectsTable::GetByID(unsigned int LOT) { UNUSED(entry.gate_version = tableData.getStringField("gate_version", "")); UNUSED(entry.HQ_valid = tableData.getIntField("HQ_valid", -1)); - this->entries.insert(std::make_pair(entry.id, entry)); + entries.insert(std::make_pair(entry.id, entry)); tableData.nextRow(); } diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDObjectsTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDObjectsTable.h index 3a776684..add21c8f 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDObjectsTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDObjectsTable.h @@ -4,30 +4,26 @@ #include "CDTable.h" struct CDObjects { - unsigned int id; //!< The LOT of the object + uint32_t id; //!< The LOT of the object std::string name; //!< The internal name of the object - UNUSED(unsigned int placeable); //!< Whether or not the object is placable + UNUSED(uint32_t placeable); //!< Whether or not the object is placable std::string type; //!< The object type UNUSED(std::string description); //!< An internal description of the object - UNUSED(unsigned int localize); //!< Whether or not the object should localize - UNUSED(unsigned int npcTemplateID); //!< Something related to NPCs... + UNUSED(uint32_t localize); //!< Whether or not the object should localize + UNUSED(uint32_t npcTemplateID); //!< Something related to NPCs... UNUSED(std::string displayName); //!< The display name of the object float interactionDistance; //!< The interaction distance of the object - UNUSED(unsigned int nametag); //!< ??? + UNUSED(uint32_t nametag); //!< ??? UNUSED(std::string _internalNotes); //!< Some internal notes (rarely used) - UNUSED(unsigned int locStatus); //!< ??? + UNUSED(uint32_t locStatus); //!< ??? UNUSED(std::string gate_version); //!< The gate version for the object - UNUSED(unsigned int HQ_valid); //!< Probably used for the Nexus HQ database on LEGOUniverse.com + UNUSED(uint32_t HQ_valid); //!< Probably used for the Nexus HQ database on LEGOUniverse.com }; -class CDObjectsTable : public CDTable { -private: - std::map entries; - CDObjects m_default; - +class CDObjectsTable : public CDTable> { public: void LoadValuesFromDatabase(); // Gets an entry by ID - const CDObjects& GetByID(unsigned int LOT); + const CDObjects& GetByID(uint32_t LOT); }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDPackageComponentTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDPackageComponentTable.cpp index 8b955162..da100026 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDPackageComponentTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDPackageComponentTable.cpp @@ -3,7 +3,7 @@ void CDPackageComponentTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM PackageComponent"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -13,8 +13,8 @@ void CDPackageComponentTable::LoadValuesFromDatabase() { tableSize.finalize(); - // Reserve the size - this->entries.reserve(size); + auto& entries = GetEntriesMutable(); + entries.reserve(size); // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM PackageComponent"); @@ -24,7 +24,7 @@ void CDPackageComponentTable::LoadValuesFromDatabase() { entry.LootMatrixIndex = tableData.getIntField("LootMatrixIndex", -1); entry.packageType = tableData.getIntField("packageType", -1); - this->entries.push_back(entry); + entries.push_back(entry); tableData.nextRow(); } @@ -34,15 +34,9 @@ void CDPackageComponentTable::LoadValuesFromDatabase() { //! Queries the table with a custom "where" clause std::vector CDPackageComponentTable::Query(std::function predicate) { - std::vector data = cpplinq::from(this->entries) + std::vector data = cpplinq::from(GetEntries()) >> cpplinq::where(predicate) >> cpplinq::to_vector(); return data; } - -//! Gets all the entries in the table -const std::vector& CDPackageComponentTable::GetEntries() const { - return this->entries; -} - diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDPackageComponentTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDPackageComponentTable.h index 7ee58761..ad7003df 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDPackageComponentTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDPackageComponentTable.h @@ -4,19 +4,14 @@ #include "CDTable.h" struct CDPackageComponent { - unsigned int id; - unsigned int LootMatrixIndex; - unsigned int packageType; + uint32_t id; + uint32_t LootMatrixIndex; + uint32_t packageType; }; -class CDPackageComponentTable : public CDTable { -private: - std::vector entries; - +class CDPackageComponentTable : public CDTable> { public: void LoadValuesFromDatabase(); // Queries the table with a custom "where" clause std::vector Query(std::function predicate); - - const std::vector& GetEntries() const; }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDPetComponentTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDPetComponentTable.cpp index 151e5156..f3371ecb 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDPetComponentTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDPetComponentTable.cpp @@ -2,56 +2,61 @@ namespace { // Default entries for fallback - CDPetComponent defaultEntry { - .id = static_cast(-1), - UNUSED_DEFAULT(.minTameUpdateTime = 60.0f,) - UNUSED_DEFAULT(.maxTameUpdateTime = 300.0f,) - UNUSED_DEFAULT(.percentTameChance = 101.0f,) - UNUSED_DEFAULT(.tameability = 100.0f,) - UNUSED_DEFAULT(.elementType = 1,) + CDPetComponent defaultEntry{ + .id = 0, + UNUSED_ENTRY(.minTameUpdateTime = 60.0f,) + UNUSED_ENTRY(.maxTameUpdateTime = 300.0f,) + UNUSED_ENTRY(.percentTameChance = 101.0f,) + UNUSED_ENTRY(.tameability = 100.0f,) + UNUSED_ENTRY(.elementType = 1,) .walkSpeed = 2.5f, .runSpeed = 5.0f, .sprintSpeed = 10.0f, - UNUSED_DEFAULT(.idleTimeMin = 60.0f,) - UNUSED_DEFAULT(.idleTimeMax = 300.0f,) - UNUSED_DEFAULT(.petForm = 0,) + UNUSED_ENTRY(.idleTimeMin = 60.0f,) + UNUSED_ENTRY(.idleTimeMax = 300.0f,) + UNUSED_ENTRY(.petForm = 0,) .imaginationDrainRate = 60.0f, - UNUSED_DEFAULT(.AudioMetaEventSet = "",) - UNUSED_DEFAULT(.buffIDs = "",) + UNUSED_ENTRY(.AudioMetaEventSet = "",) + UNUSED_ENTRY(.buffIDs = "",) }; } void CDPetComponentTable::LoadValuesFromDatabase() { auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM PetComponent"); + auto& entries = GetEntriesMutable(); while (!tableData.eof()) { - CDPetComponent entry; - entry.id = tableData.getIntField("id", defaultEntry.id); - UNUSED_COLUMN(entry.minTameUpdateTime = tableData.getFloatField("minTameUpdateTime", defaultEntry.minTameUpdateTime);) - UNUSED_COLUMN(entry.maxTameUpdateTime = tableData.getFloatField("maxTameUpdateTime", defaultEntry.maxTameUpdateTime);) - UNUSED_COLUMN(entry.percentTameChance = tableData.getFloatField("percentTameChance", defaultEntry.percentTameChance);) - UNUSED_COLUMN(entry.tameability = tableData.getFloatField("tamability", defaultEntry.tameability);) // Mispelled as "tamability" in CDClient - UNUSED_COLUMN(entry.elementType = tableData.getIntField("elementType", defaultEntry.elementType);) - entry.walkSpeed = tableData.getFloatField("walkSpeed", defaultEntry.walkSpeed); - entry.runSpeed = tableData.getFloatField("runSpeed", defaultEntry.runSpeed); - entry.sprintSpeed = tableData.getFloatField("sprintSpeed", defaultEntry.sprintSpeed); - UNUSED_COLUMN(entry.idleTimeMin = tableData.getFloatField("idleTimeMin", defaultEntry.idleTimeMin);) - UNUSED_COLUMN(entry.idleTimeMax = tableData.getFloatField("idleTimeMax", defaultEntry.idleTimeMax);) - UNUSED_COLUMN(entry.petForm = tableData.getIntField("petForm", 0);) - entry.imaginationDrainRate = tableData.getFloatField("imaginationDrainRate", defaultEntry.imaginationDrainRate); - UNUSED_COLUMN(entry.AudioMetaEventSet = tableData.getStringField("AudioMetaEventSet", defaultEntry.AudioMetaEventSet);) - UNUSED_COLUMN(entry.buffIDs = tableData.getStringField("buffIDs", defaultEntry.buffIDs);) + const uint32_t componentID = tableData.getIntField("id", defaultEntry.id); + + auto& entry = entries[componentID]; + + entry.id = componentID; + UNUSED_COLUMN(entry.minTameUpdateTime = tableData.getFloatField("minTameUpdateTime", defaultEntry.minTameUpdateTime)); + UNUSED_COLUMN(entry.maxTameUpdateTime = tableData.getFloatField("maxTameUpdateTime", defaultEntry.maxTameUpdateTime)); + UNUSED_COLUMN(entry.percentTameChance = tableData.getFloatField("percentTameChance", defaultEntry.percentTameChance)); + UNUSED_COLUMN(entry.tameability = tableData.getFloatField("tamability", defaultEntry.tameability)); // Mispelled as "tamability" in CDClient + UNUSED_COLUMN(entry.elementType = tableData.getIntField("elementType", defaultEntry.elementType)); + entry.walkSpeed = static_cast(tableData.getFloatField("walkSpeed", defaultEntry.walkSpeed)); + entry.runSpeed = static_cast(tableData.getFloatField("runSpeed", defaultEntry.runSpeed)); + entry.sprintSpeed = static_cast(tableData.getFloatField("sprintSpeed", defaultEntry.sprintSpeed)); + UNUSED_COLUMN(entry.idleTimeMin = tableData.getFloatField("idleTimeMin", defaultEntry.idleTimeMin)); + UNUSED_COLUMN(entry.idleTimeMax = tableData.getFloatField("idleTimeMax", defaultEntry.idleTimeMax)); + UNUSED_COLUMN(entry.petForm = tableData.getIntField("petForm", defaultEntry.petForm)); + entry.imaginationDrainRate = static_cast(tableData.getFloatField("imaginationDrainRate", defaultEntry.imaginationDrainRate)); + UNUSED_COLUMN(entry.AudioMetaEventSet = tableData.getStringField("AudioMetaEventSet", defaultEntry.AudioMetaEventSet)); + UNUSED_COLUMN(entry.buffIDs = tableData.getStringField("buffIDs", defaultEntry.buffIDs)); - m_entries.insert(std::make_pair(entry.id, entry)); tableData.nextRow(); } - - tableData.finalize(); - } -CDPetComponent& CDPetComponentTable::GetByID(unsigned int componentID) { - auto itr = m_entries.find(componentID); - if (itr == m_entries.end()) { +void CDPetComponentTable::LoadValuesFromDefaults() { + GetEntriesMutable().insert(std::make_pair(defaultEntry.id, defaultEntry)); +} + +CDPetComponent& CDPetComponentTable::GetByID(const uint32_t componentID) { + auto& entries = GetEntriesMutable(); + auto itr = entries.find(componentID); + if (itr == entries.end()) { LOG("Unable to load pet component (ID %i) values from database! Using default values instead.", componentID); return defaultEntry; } diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDPetComponentTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDPetComponentTable.h index bb37edab..42890253 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDPetComponentTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDPetComponentTable.h @@ -1,32 +1,42 @@ #pragma once #include "CDTable.h" +#include #include struct CDPetComponent { - unsigned int id; + uint32_t id; UNUSED_COLUMN(float minTameUpdateTime;) UNUSED_COLUMN(float maxTameUpdateTime;) UNUSED_COLUMN(float percentTameChance;) UNUSED_COLUMN(float tameability;) // Mispelled as "tamability" in CDClient - UNUSED_COLUMN(unsigned int elementType;) + UNUSED_COLUMN(uint32_t elementType;) float walkSpeed; float runSpeed; float sprintSpeed; UNUSED_COLUMN(float idleTimeMin;) UNUSED_COLUMN(float idleTimeMax;) - UNUSED_COLUMN(unsigned int petForm;) + UNUSED_COLUMN(uint32_t petForm;) float imaginationDrainRate; UNUSED_COLUMN(std::string AudioMetaEventSet;) UNUSED_COLUMN(std::string buffIDs;) }; -class CDPetComponentTable : public CDTable { +class CDPetComponentTable : public CDTable> { public: - void LoadValuesFromDatabase(); - static const std::string GetTableName() { return "PetComponent"; }; - CDPetComponent& GetByID(unsigned int componentID); + /** + * Load values from the CD client database + */ + void LoadValuesFromDatabase(); -private: - std::map m_entries; + /** + * Load the default values into memory instead of attempting to connect to the CD client database + */ + void LoadValuesFromDefaults(); + + /** + * Gets the pet component table corresponding to the pet component ID + * @returns A reference to the corresponding table, or the default if one could not be found + */ + CDPetComponent& GetByID(const uint32_t componentID); }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDPhysicsComponentTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDPhysicsComponentTable.cpp index e17be4df..34671f3c 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDPhysicsComponentTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDPhysicsComponentTable.cpp @@ -2,6 +2,7 @@ void CDPhysicsComponentTable::LoadValuesFromDatabase() { auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM PhysicsComponent"); + auto& entries = GetEntriesMutable(); while (!tableData.eof()) { CDPhysicsComponent entry; entry.id = tableData.getIntField("id", -1); @@ -21,15 +22,16 @@ void CDPhysicsComponentTable::LoadValuesFromDatabase() { UNUSED(entry->friction = tableData.getFloatField("friction")); UNUSED(entry->gravityVolumeAsset = tableData.getStringField("gravityVolumeAsset")); - m_entries.insert(std::make_pair(entry.id, entry)); + entries.insert(std::make_pair(entry.id, entry)); tableData.nextRow(); } tableData.finalize(); } -CDPhysicsComponent* CDPhysicsComponentTable::GetByID(unsigned int componentID) { - auto itr = m_entries.find(componentID); - return itr != m_entries.end() ? &itr->second : nullptr; +CDPhysicsComponent* CDPhysicsComponentTable::GetByID(uint32_t componentID) { + auto& entries = GetEntriesMutable(); + auto itr = entries.find(componentID); + return itr != entries.end() ? &itr->second : nullptr; } diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDPhysicsComponentTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDPhysicsComponentTable.h index 49f3b4c3..f0a62139 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDPhysicsComponentTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDPhysicsComponentTable.h @@ -3,7 +3,7 @@ #include struct CDPhysicsComponent { - int id; + int32_t id; bool bStatic; std::string physicsAsset; UNUSED(bool jump); @@ -12,8 +12,8 @@ struct CDPhysicsComponent { UNUSED(float rotSpeed); float playerHeight; float playerRadius; - int pcShapeType; - int collisionGroup; + int32_t pcShapeType; + int32_t collisionGroup; UNUSED(float airSpeed); UNUSED(std::string boundaryAsset); UNUSED(float jumpAirSpeed); @@ -21,13 +21,10 @@ struct CDPhysicsComponent { UNUSED(std::string gravityVolumeAsset); }; -class CDPhysicsComponentTable : public CDTable { +class CDPhysicsComponentTable : public CDTable> { public: void LoadValuesFromDatabase(); static const std::string GetTableName() { return "PhysicsComponent"; }; - CDPhysicsComponent* GetByID(unsigned int componentID); - -private: - std::map m_entries; + CDPhysicsComponent* GetByID(uint32_t componentID); }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDPropertyEntranceComponentTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDPropertyEntranceComponentTable.cpp index c1532c86..dfb591d1 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDPropertyEntranceComponentTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDPropertyEntranceComponentTable.cpp @@ -1,5 +1,9 @@ #include "CDPropertyEntranceComponentTable.h" +namespace { + CDPropertyEntranceComponent defaultEntry{}; +}; + void CDPropertyEntranceComponentTable::LoadValuesFromDatabase() { // First, get the size of the table @@ -12,7 +16,8 @@ void CDPropertyEntranceComponentTable::LoadValuesFromDatabase() { tableSize.finalize(); - this->entries.reserve(size); + auto& entries = GetEntriesMutable(); + entries.reserve(size); auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM PropertyEntranceComponent;"); while (!tableData.eof()) { @@ -24,7 +29,7 @@ void CDPropertyEntranceComponentTable::LoadValuesFromDatabase() { tableData.getStringField("groupType", "") }; - this->entries.push_back(entry); + entries.push_back(entry); tableData.nextRow(); } @@ -32,11 +37,10 @@ void CDPropertyEntranceComponentTable::LoadValuesFromDatabase() { } CDPropertyEntranceComponent CDPropertyEntranceComponentTable::GetByID(uint32_t id) { - for (const auto& entry : entries) { + for (const auto& entry : GetEntries()) { if (entry.id == id) return entry; } return defaultEntry; } - diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDPropertyEntranceComponentTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDPropertyEntranceComponentTable.h index 5c7d0965..f687b881 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDPropertyEntranceComponentTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDPropertyEntranceComponentTable.h @@ -9,15 +9,9 @@ struct CDPropertyEntranceComponent { std::string groupType; }; -class CDPropertyEntranceComponentTable : public CDTable { +class CDPropertyEntranceComponentTable : public CDTable> { public: void LoadValuesFromDatabase(); // Queries the table with a custom "where" clause CDPropertyEntranceComponent GetByID(uint32_t id); - - // Gets all the entries in the table - [[nodiscard]] const std::vector& GetEntries() const { return entries; } -private: - std::vector entries{}; - CDPropertyEntranceComponent defaultEntry{}; }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDPropertyTemplateTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDPropertyTemplateTable.cpp index 4a1d666e..4046b6fa 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDPropertyTemplateTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDPropertyTemplateTable.cpp @@ -1,5 +1,9 @@ #include "CDPropertyTemplateTable.h" +namespace { + CDPropertyTemplate defaultEntry{}; +}; + void CDPropertyTemplateTable::LoadValuesFromDatabase() { // First, get the size of the table @@ -12,7 +16,8 @@ void CDPropertyTemplateTable::LoadValuesFromDatabase() { tableSize.finalize(); - this->entries.reserve(size); + auto& entries = GetEntriesMutable(); + entries.reserve(size); auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM PropertyTemplate;"); while (!tableData.eof()) { @@ -23,7 +28,7 @@ void CDPropertyTemplateTable::LoadValuesFromDatabase() { tableData.getStringField("spawnName", "") }; - this->entries.push_back(entry); + entries.push_back(entry); tableData.nextRow(); } @@ -31,7 +36,7 @@ void CDPropertyTemplateTable::LoadValuesFromDatabase() { } CDPropertyTemplate CDPropertyTemplateTable::GetByMapID(uint32_t mapID) { - for (const auto& entry : entries) { + for (const auto& entry : GetEntries()) { if (entry.mapID == mapID) return entry; } diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDPropertyTemplateTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDPropertyTemplateTable.h index 7261bdf9..e0c6c485 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDPropertyTemplateTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDPropertyTemplateTable.h @@ -8,13 +8,9 @@ struct CDPropertyTemplate { std::string spawnName; }; -class CDPropertyTemplateTable : public CDTable { +class CDPropertyTemplateTable : public CDTable> { public: void LoadValuesFromDatabase(); - static const std::string GetTableName() { return "PropertyTemplate"; }; CDPropertyTemplate GetByMapID(uint32_t mapID); -private: - std::vector entries{}; - CDPropertyTemplate defaultEntry{}; }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDProximityMonitorComponentTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDProximityMonitorComponentTable.cpp index ae0abac8..279d6408 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDProximityMonitorComponentTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDProximityMonitorComponentTable.cpp @@ -3,7 +3,7 @@ void CDProximityMonitorComponentTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM ProximityMonitorComponent"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -14,7 +14,8 @@ void CDProximityMonitorComponentTable::LoadValuesFromDatabase() { tableSize.finalize(); // Reserve the size - this->entries.reserve(size); + auto& entries = GetEntriesMutable(); + entries.reserve(size); // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM ProximityMonitorComponent"); @@ -25,7 +26,7 @@ void CDProximityMonitorComponentTable::LoadValuesFromDatabase() { entry.LoadOnClient = tableData.getIntField("LoadOnClient", -1); entry.LoadOnServer = tableData.getIntField("LoadOnServer", -1); - this->entries.push_back(entry); + entries.push_back(entry); tableData.nextRow(); } @@ -34,14 +35,9 @@ void CDProximityMonitorComponentTable::LoadValuesFromDatabase() { std::vector CDProximityMonitorComponentTable::Query(std::function predicate) { - std::vector data = cpplinq::from(this->entries) + std::vector data = cpplinq::from(GetEntries()) >> cpplinq::where(predicate) >> cpplinq::to_vector(); return data; } - -const std::vector& CDProximityMonitorComponentTable::GetEntries() const { - return this->entries; -} - diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDProximityMonitorComponentTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDProximityMonitorComponentTable.h index a50dd37e..af2c385e 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDProximityMonitorComponentTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDProximityMonitorComponentTable.h @@ -4,20 +4,15 @@ #include "CDTable.h" struct CDProximityMonitorComponent { - unsigned int id; + uint32_t id; std::string Proximities; bool LoadOnClient; bool LoadOnServer; }; -class CDProximityMonitorComponentTable : public CDTable { -private: - std::vector entries; - +class CDProximityMonitorComponentTable : public CDTable> { public: void LoadValuesFromDatabase(); //! Queries the table with a custom "where" clause std::vector Query(std::function predicate); - - const std::vector& GetEntries() const; }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDRailActivatorComponent.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDRailActivatorComponent.cpp index 34ec5826..72a26beb 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDRailActivatorComponent.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDRailActivatorComponent.cpp @@ -3,6 +3,7 @@ void CDRailActivatorComponentTable::LoadValuesFromDatabase() { auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM RailActivatorComponent;"); + auto& entries = GetEntriesMutable(); while (!tableData.eof()) { CDRailActivatorComponent entry; @@ -36,7 +37,7 @@ void CDRailActivatorComponentTable::LoadValuesFromDatabase() { entry.showNameBillboard = tableData.getIntField("ShowNameBillboard", 0); - m_Entries.push_back(entry); + entries.push_back(entry); tableData.nextRow(); } @@ -44,7 +45,7 @@ void CDRailActivatorComponentTable::LoadValuesFromDatabase() { } CDRailActivatorComponent CDRailActivatorComponentTable::GetEntryByID(int32_t id) const { - for (const auto& entry : m_Entries) { + for (const auto& entry : GetEntries()) { if (entry.id == id) return entry; } @@ -52,10 +53,6 @@ CDRailActivatorComponent CDRailActivatorComponentTable::GetEntryByID(int32_t id) return {}; } -const std::vector& CDRailActivatorComponentTable::GetEntries() const { - return m_Entries; -} - std::pair CDRailActivatorComponentTable::EffectPairFromString(std::string& str) { const auto split = GeneralUtils::SplitString(str, ':'); if (split.size() == 2) { diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDRailActivatorComponent.h b/dDatabase/CDClientDatabase/CDClientTables/CDRailActivatorComponent.h index d06b2d36..d9a94d37 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDRailActivatorComponent.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDRailActivatorComponent.h @@ -20,13 +20,10 @@ struct CDRailActivatorComponent { bool showNameBillboard; }; -class CDRailActivatorComponentTable : public CDTable { +class CDRailActivatorComponentTable : public CDTable> { public: void LoadValuesFromDatabase(); - static const std::string GetTableName() { return "RailActivatorComponent"; }; [[nodiscard]] CDRailActivatorComponent GetEntryByID(int32_t id) const; - [[nodiscard]] const std::vector& GetEntries() const; private: static std::pair EffectPairFromString(std::string& str); - std::vector m_Entries{}; }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDRarityTableTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDRarityTableTable.cpp index aa451ae3..def27339 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDRarityTableTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDRarityTableTable.cpp @@ -3,7 +3,7 @@ void CDRarityTableTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM RarityTable"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -14,7 +14,8 @@ void CDRarityTableTable::LoadValuesFromDatabase() { tableSize.finalize(); // Reserve the size - this->entries.reserve(size); + auto& entries = GetEntriesMutable(); + entries.reserve(size); // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM RarityTable order by randmax desc;"); @@ -30,5 +31,5 @@ void CDRarityTableTable::LoadValuesFromDatabase() { } const std::vector& CDRarityTableTable::GetRarityTable(uint32_t id) { - return entries[id]; + return GetEntriesMutable()[id]; } diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDRarityTableTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDRarityTableTable.h index e2053da7..006ac986 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDRarityTableTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDRarityTableTable.h @@ -5,16 +5,14 @@ struct CDRarityTable { float randmax; - unsigned int rarity; + uint32_t rarity; + + typedef uint32_t Index; }; typedef std::vector RarityTable; -class CDRarityTableTable : public CDTable { -private: - typedef uint32_t RarityTableIndex; - std::unordered_map> entries; - +class CDRarityTableTable : public CDTable>> { public: void LoadValuesFromDatabase(); diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDRebuildComponentTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDRebuildComponentTable.cpp index f5706a28..8a07db88 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDRebuildComponentTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDRebuildComponentTable.cpp @@ -3,7 +3,7 @@ void CDRebuildComponentTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM RebuildComponent"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -14,7 +14,8 @@ void CDRebuildComponentTable::LoadValuesFromDatabase() { tableSize.finalize(); // Reserve the size - this->entries.reserve(size); + auto& entries = GetEntriesMutable(); + entries.reserve(size); // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM RebuildComponent"); @@ -31,7 +32,7 @@ void CDRebuildComponentTable::LoadValuesFromDatabase() { entry.post_imagination_cost = tableData.getIntField("post_imagination_cost", -1); entry.time_before_smash = tableData.getFloatField("time_before_smash", -1.0f); - this->entries.push_back(entry); + entries.push_back(entry); tableData.nextRow(); } @@ -40,14 +41,9 @@ void CDRebuildComponentTable::LoadValuesFromDatabase() { std::vector CDRebuildComponentTable::Query(std::function predicate) { - std::vector data = cpplinq::from(this->entries) + std::vector data = cpplinq::from(GetEntries()) >> cpplinq::where(predicate) >> cpplinq::to_vector(); return data; } - -const std::vector& CDRebuildComponentTable::GetEntries() const { - return this->entries; -} - diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDRebuildComponentTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDRebuildComponentTable.h index fc78e904..0eeb62ae 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDRebuildComponentTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDRebuildComponentTable.h @@ -4,27 +4,22 @@ #include "CDTable.h" struct CDRebuildComponent { - unsigned int id; //!< The component Id + uint32_t id; //!< The component Id float reset_time; //!< The reset time float complete_time; //!< The complete time - unsigned int take_imagination; //!< The amount of imagination it costs + uint32_t take_imagination; //!< The amount of imagination it costs bool interruptible; //!< Whether or not the rebuild is interruptible bool self_activator; //!< Whether or not the rebuild is a rebuild activator itself std::string custom_modules; //!< The custom modules - unsigned int activityID; //!< The activity ID - unsigned int post_imagination_cost; //!< The post imagination cost + uint32_t activityID; //!< The activity ID + uint32_t post_imagination_cost; //!< The post imagination cost float time_before_smash; //!< The time before smash }; -class CDRebuildComponentTable : public CDTable { -private: - std::vector entries; - +class CDRebuildComponentTable : public CDTable> { public: void LoadValuesFromDatabase(); // Queries the table with a custom "where" clause std::vector Query(std::function predicate); - - const std::vector& GetEntries() const; }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDRewardCodesTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDRewardCodesTable.cpp index 2bda73f4..8ade60a9 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDRewardCodesTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDRewardCodesTable.cpp @@ -3,7 +3,7 @@ void CDRewardCodesTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM RewardCodes"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -14,7 +14,8 @@ void CDRewardCodesTable::LoadValuesFromDatabase() { tableSize.finalize(); // Reserve the size - this->entries.reserve(size); + auto& entries = GetEntriesMutable(); + entries.reserve(size); // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM RewardCodes"); @@ -26,20 +27,20 @@ void CDRewardCodesTable::LoadValuesFromDatabase() { UNUSED_COLUMN(entry.locStatus = tableData.getIntField("locStatus", -1)); UNUSED_COLUMN(entry.gate_version = tableData.getStringField("gate_version", "")); - this->entries.push_back(entry); + entries.push_back(entry); tableData.nextRow(); } } LOT CDRewardCodesTable::GetAttachmentLOT(uint32_t rewardCodeId) const { - for (auto const &entry : this->entries){ + for (auto const &entry : GetEntries()){ if (rewardCodeId == entry.id) return entry.attachmentLOT; } return LOT_NULL; } uint32_t CDRewardCodesTable::GetCodeID(std::string code) const { - for (auto const &entry : this->entries){ + for (auto const &entry : GetEntries()){ if (code == entry.code) return entry.id; } return -1; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDRewardCodesTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDRewardCodesTable.h index 1010a572..aa64c3bb 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDRewardCodesTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDRewardCodesTable.h @@ -13,13 +13,9 @@ struct CDRewardCode { }; -class CDRewardCodesTable : public CDTable { -private: - std::vector entries; - +class CDRewardCodesTable : public CDTable> { public: void LoadValuesFromDatabase(); - const std::vector& GetEntries() const; LOT GetAttachmentLOT(uint32_t rewardCodeId) const; uint32_t GetCodeID(std::string code) const; }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDRewardsTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDRewardsTable.cpp index 27c2344a..4539e417 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDRewardsTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDRewardsTable.cpp @@ -2,6 +2,7 @@ void CDRewardsTable::LoadValuesFromDatabase() { auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM Rewards"); + auto& entries = GetEntriesMutable(); while (!tableData.eof()) { CDRewards entry; entry.id = tableData.getIntField("id", -1); @@ -11,7 +12,7 @@ void CDRewardsTable::LoadValuesFromDatabase() { entry.value = tableData.getIntField("value", -1); entry.count = tableData.getIntField("count", -1); - m_entries.insert(std::make_pair(entry.id, entry)); + entries.insert(std::make_pair(entry.id, entry)); tableData.nextRow(); } @@ -20,7 +21,7 @@ void CDRewardsTable::LoadValuesFromDatabase() { std::vector CDRewardsTable::GetByLevelID(uint32_t levelID) { std::vector result{}; - for (const auto& e : m_entries) { + for (const auto& e : GetEntries()) { if (e.second.levelID == levelID) result.push_back(e.second); } diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDRewardsTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDRewardsTable.h index 9c24397b..cec787cf 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDRewardsTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDRewardsTable.h @@ -11,13 +11,9 @@ struct CDRewards { int32_t count; }; -class CDRewardsTable : public CDTable { +class CDRewardsTable : public CDTable> { public: void LoadValuesFromDatabase(); - static const std::string GetTableName() { return "Rewards"; }; std::vector GetByLevelID(uint32_t levelID); - -private: - std::map m_entries; }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDScriptComponentTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDScriptComponentTable.cpp index 1a922199..02e3e29c 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDScriptComponentTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDScriptComponentTable.cpp @@ -1,9 +1,13 @@ #include "CDScriptComponentTable.h" +namespace { + CDScriptComponent m_ToReturnWhenNoneFound; +}; + void CDScriptComponentTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM ScriptComponent"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -15,22 +19,24 @@ void CDScriptComponentTable::LoadValuesFromDatabase() { // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM ScriptComponent"); + auto& entries = GetEntriesMutable(); while (!tableData.eof()) { CDScriptComponent entry; entry.id = tableData.getIntField("id", -1); entry.script_name = tableData.getStringField("script_name", ""); entry.client_script_name = tableData.getStringField("client_script_name", ""); - this->entries.insert(std::make_pair(entry.id, entry)); + entries.insert(std::make_pair(entry.id, entry)); tableData.nextRow(); } tableData.finalize(); } -const CDScriptComponent& CDScriptComponentTable::GetByID(unsigned int id) { - std::map::iterator it = this->entries.find(id); - if (it != this->entries.end()) { +const CDScriptComponent& CDScriptComponentTable::GetByID(uint32_t id) { + auto& entries = GetEntries(); + auto it = entries.find(id); + if (it != entries.end()) { return it->second; } diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDScriptComponentTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDScriptComponentTable.h index d2b7e7ae..96c1b5a5 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDScriptComponentTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDScriptComponentTable.h @@ -4,19 +4,15 @@ #include "CDTable.h" struct CDScriptComponent { - unsigned int id; //!< The component ID + uint32_t id; //!< The component ID std::string script_name; //!< The script name std::string client_script_name; //!< The client script name }; -class CDScriptComponentTable : public CDTable { -private: - std::map entries; - CDScriptComponent m_ToReturnWhenNoneFound; - +class CDScriptComponentTable : public CDTable> { public: void LoadValuesFromDatabase(); // Gets an entry by scriptID - const CDScriptComponent& GetByID(unsigned int id); + const CDScriptComponent& GetByID(uint32_t id); }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDSkillBehaviorTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDSkillBehaviorTable.cpp index 8ffbb5ce..0df67884 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDSkillBehaviorTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDSkillBehaviorTable.cpp @@ -1,10 +1,12 @@ #include "CDSkillBehaviorTable.h" -void CDSkillBehaviorTable::LoadValuesFromDatabase() { - m_empty = CDSkillBehavior(); +namespace { + CDSkillBehavior m_empty = CDSkillBehavior(); +}; +void CDSkillBehaviorTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM SkillBehavior"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -14,8 +16,7 @@ void CDSkillBehaviorTable::LoadValuesFromDatabase() { tableSize.finalize(); - // Reserve the size - //this->entries.reserve(size); + auto& entries = GetEntriesMutable(); // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM SkillBehavior"); @@ -41,7 +42,7 @@ void CDSkillBehaviorTable::LoadValuesFromDatabase() { UNUSED(entry.gate_version = tableData.getStringField("gate_version", "")); UNUSED(entry.cancelType = tableData.getIntField("cancelType", -1)); - this->entries.insert(std::make_pair(entry.skillID, entry)); + entries.insert(std::make_pair(entry.skillID, entry)); //this->entries.push_back(entry); tableData.nextRow(); } @@ -49,9 +50,10 @@ void CDSkillBehaviorTable::LoadValuesFromDatabase() { tableData.finalize(); } -const CDSkillBehavior& CDSkillBehaviorTable::GetSkillByID(unsigned int skillID) { - std::map::iterator it = this->entries.find(skillID); - if (it != this->entries.end()) { +const CDSkillBehavior& CDSkillBehaviorTable::GetSkillByID(uint32_t skillID) { + auto& entries = GetEntries(); + auto it = entries.find(skillID); + if (it != entries.end()) { return it->second; } diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDSkillBehaviorTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDSkillBehaviorTable.h index 5b1081cd..19225d19 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDSkillBehaviorTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDSkillBehaviorTable.h @@ -4,36 +4,32 @@ #include "CDTable.h" struct CDSkillBehavior { - unsigned int skillID; //!< The Skill ID of the skill - UNUSED(unsigned int locStatus); //!< ?? - unsigned int behaviorID; //!< The Behavior ID of the skill - unsigned int imaginationcost; //!< The imagination cost of the skill - unsigned int cooldowngroup; //!< The cooldown group ID of the skill + uint32_t skillID; //!< The Skill ID of the skill + UNUSED(uint32_t locStatus); //!< ?? + uint32_t behaviorID; //!< The Behavior ID of the skill + uint32_t imaginationcost; //!< The imagination cost of the skill + uint32_t cooldowngroup; //!< The cooldown group ID of the skill float cooldown; //!< The cooldown time of the skill UNUSED(bool isNpcEditor); //!< ??? - UNUSED(unsigned int skillIcon); //!< The Skill Icon ID + UNUSED(uint32_t skillIcon); //!< The Skill Icon ID UNUSED(std::string oomSkillID); //!< ??? - UNUSED(unsigned int oomBehaviorEffectID); //!< ??? - UNUSED(unsigned int castTypeDesc); //!< The cast type description(?) - UNUSED(unsigned int imBonusUI); //!< The imagination bonus of the skill - UNUSED(nsigned int lifeBonusUI); //!< The life bonus of the skill - UNUSED(unsigned int armorBonusUI); //!< The armor bonus of the skill - UNUSED(unsigned int damageUI); //!< ??? + UNUSED(uint32_t oomBehaviorEffectID); //!< ??? + UNUSED(uint32_t castTypeDesc); //!< The cast type description(?) + UNUSED(uint32_t imBonusUI); //!< The imagination bonus of the skill + UNUSED(nint32_t lifeBonusUI); //!< The life bonus of the skill + UNUSED(uint32_t armorBonusUI); //!< The armor bonus of the skill + UNUSED(uint32_t damageUI); //!< ??? UNUSED(bool hideIcon); //!< Whether or not to show the icon UNUSED(bool localize); //!< ??? UNUSED(std::string gate_version); //!< ??? - UNUSED(unsigned int cancelType); //!< The cancel type (?) + UNUSED(uint32_t cancelType); //!< The cancel type (?) }; -class CDSkillBehaviorTable : public CDTable { -private: - std::map entries; - CDSkillBehavior m_empty; - +class CDSkillBehaviorTable : public CDTable> { public: void LoadValuesFromDatabase(); // Gets an entry by skillID - const CDSkillBehavior& GetSkillByID(unsigned int skillID); + const CDSkillBehavior& GetSkillByID(uint32_t skillID); }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDTable.h index 3569a861..4e896dbb 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDTable.h @@ -1,6 +1,7 @@ #pragma once #include "CDClientDatabase.h" +#include "CDClientManager.h" #include "Singleton.h" #include "DluAssert.h" @@ -23,26 +24,27 @@ // Enable this to skip some unused columns in some tables #define UNUSED_COLUMN(v) -// Use this to skip unused defaults for unused columns in some tables -#define UNUSED_DEFAULT(v, x) +// Use this to skip unused defaults for unused entries in some tables +#define UNUSED_ENTRY(v, x) #pragma warning (disable : 4244) //Disable double to float conversion warnings -#pragma warning (disable : 4715) //Disable "not all control paths return a value" +// #pragma warning (disable : 4715) //Disable "not all control paths return a value" -template +template class CDTable : public Singleton
{ +public: + typedef Storage StorageType; + protected: virtual ~CDTable() = default; -}; -template -class LookupResult { - typedef std::pair DataType; -public: - LookupResult() { m_data.first = T(); m_data.second = false; }; - LookupResult(T& data) { m_data.first = data; m_data.second = true; }; - inline const T& Data() { return m_data.first; }; - inline const bool& FoundData() { return m_data.second; }; -private: - DataType m_data; + // If you need these for a specific table, override it such that there is a public variant. + [[nodiscard]] StorageType& GetEntriesMutable() const { + return CDClientManager::GetEntriesMutable
(); + } + + // If you need these for a specific table, override it such that there is a public variant. + [[nodiscard]] const StorageType& GetEntries() const { + return GetEntriesMutable(); + } }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDVendorComponentTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDVendorComponentTable.cpp index 0f963b04..f639a7e3 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDVendorComponentTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDVendorComponentTable.cpp @@ -3,7 +3,7 @@ void CDVendorComponentTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM VendorComponent"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -14,7 +14,8 @@ void CDVendorComponentTable::LoadValuesFromDatabase() { tableSize.finalize(); // Reserve the size - this->entries.reserve(size); + auto& entries = GetEntriesMutable(); + entries.reserve(size); // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM VendorComponent"); @@ -26,7 +27,7 @@ void CDVendorComponentTable::LoadValuesFromDatabase() { entry.refreshTimeSeconds = tableData.getFloatField("refreshTimeSeconds", -1.0f); entry.LootMatrixIndex = tableData.getIntField("LootMatrixIndex", -1); - this->entries.push_back(entry); + entries.push_back(entry); tableData.nextRow(); } @@ -36,15 +37,9 @@ void CDVendorComponentTable::LoadValuesFromDatabase() { //! Queries the table with a custom "where" clause std::vector CDVendorComponentTable::Query(std::function predicate) { - std::vector data = cpplinq::from(this->entries) + std::vector data = cpplinq::from(GetEntries()) >> cpplinq::where(predicate) >> cpplinq::to_vector(); return data; } - -//! Gets all the entries in the table -const std::vector& CDVendorComponentTable::GetEntries() const { - return this->entries; -} - diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDVendorComponentTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDVendorComponentTable.h index 29854d49..cbed2d13 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDVendorComponentTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDVendorComponentTable.h @@ -4,22 +4,17 @@ #include "CDTable.h" struct CDVendorComponent { - unsigned int id; //!< The Component ID + uint32_t id; //!< The Component ID float buyScalar; //!< Buy Scalar (what does that mean?) float sellScalar; //!< Sell Scalar (what does that mean?) float refreshTimeSeconds; //!< The refresh time - unsigned int LootMatrixIndex; //!< LootMatrixIndex of the vendor's items + uint32_t LootMatrixIndex; //!< LootMatrixIndex of the vendor's items }; -class CDVendorComponentTable : public CDTable { -private: - std::vector entries; - +class CDVendorComponentTable : public CDTable> { public: void LoadValuesFromDatabase(); // Queries the table with a custom "where" clause std::vector Query(std::function predicate); - - const std::vector& GetEntries(void) const; }; diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDZoneTableTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDZoneTableTable.cpp index 2793ccb9..6aaeb854 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDZoneTableTable.cpp +++ b/dDatabase/CDClientDatabase/CDClientTables/CDZoneTableTable.cpp @@ -3,7 +3,7 @@ void CDZoneTableTable::LoadValuesFromDatabase() { // First, get the size of the table - unsigned int size = 0; + uint32_t size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM ZoneTable"); while (!tableSize.eof()) { size = tableSize.getIntField(0, 0); @@ -15,6 +15,7 @@ void CDZoneTableTable::LoadValuesFromDatabase() { // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM ZoneTable"); + auto& entries = GetEntriesMutable(); while (!tableData.eof()) { CDZoneTable entry; entry.zoneID = tableData.getIntField("zoneID", -1); @@ -45,7 +46,7 @@ void CDZoneTableTable::LoadValuesFromDatabase() { UNUSED(entry.gate_version = tableData.getStringField("gate_version", "")); entry.mountsAllowed = tableData.getIntField("mountsAllowed", -1) == 1 ? true : false; - this->m_Entries.insert(std::make_pair(entry.zoneID, entry)); + entries.insert(std::make_pair(entry.zoneID, entry)); tableData.nextRow(); } @@ -53,7 +54,8 @@ void CDZoneTableTable::LoadValuesFromDatabase() { } //! Queries the table with a zoneID to find. -const CDZoneTable* CDZoneTableTable::Query(unsigned int zoneID) { +const CDZoneTable* CDZoneTableTable::Query(uint32_t zoneID) { + auto& m_Entries = GetEntries(); const auto& iter = m_Entries.find(zoneID); if (iter != m_Entries.end()) { diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDZoneTableTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDZoneTableTable.h index c3f51aa6..b1e8b1ba 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CDZoneTableTable.h +++ b/dDatabase/CDClientDatabase/CDClientTables/CDZoneTableTable.h @@ -4,14 +4,14 @@ #include "CDTable.h" struct CDZoneTable { - unsigned int zoneID; //!< The Zone ID of the object - unsigned int locStatus; //!< The Locale Status(?) + uint32_t zoneID; //!< The Zone ID of the object + uint32_t locStatus; //!< The Locale Status(?) std::string zoneName; //!< The name of the zone - unsigned int scriptID; //!< The Script ID of the zone (ScriptsTable) + uint32_t scriptID; //!< The Script ID of the zone (ScriptsTable) float ghostdistance_min; //!< The minimum ghosting distance float ghostdistance; //!< The ghosting distance - unsigned int population_soft_cap; //!< The "soft cap" on the world population - unsigned int population_hard_cap; //!< The "hard cap" on the world population + uint32_t population_soft_cap; //!< The "soft cap" on the world population + uint32_t population_hard_cap; //!< The "hard cap" on the world population UNUSED(std::string DisplayDescription); //!< The display description of the world UNUSED(std::string mapFolder); //!< ??? float smashableMinDistance; //!< The minimum smashable distance? @@ -19,9 +19,9 @@ struct CDZoneTable { UNUSED(std::string mixerProgram); //!< ??? UNUSED(std::string clientPhysicsFramerate); //!< The client physics framerate std::string serverPhysicsFramerate; //!< The server physics framerate - unsigned int zoneControlTemplate; //!< The Zone Control template - unsigned int widthInChunks; //!< The width of the world in chunks - unsigned int heightInChunks; //!< The height of the world in chunks + uint32_t zoneControlTemplate; //!< The Zone Control template + uint32_t widthInChunks; //!< The width of the world in chunks + uint32_t heightInChunks; //!< The height of the world in chunks bool petsAllowed; //!< Whether or not pets are allowed in the world bool localize; //!< Whether or not the world should be localized float fZoneWeight; //!< ??? @@ -33,13 +33,10 @@ struct CDZoneTable { bool mountsAllowed; //!< Whether or not mounts are allowed }; -class CDZoneTableTable : public CDTable { -private: - std::map m_Entries; - +class CDZoneTableTable : public CDTable> { public: void LoadValuesFromDatabase(); // Queries the table with a zoneID to find. - const CDZoneTable* Query(unsigned int zoneID); + const CDZoneTable* Query(uint32_t zoneID); }; diff --git a/dGame/CMakeLists.txt b/dGame/CMakeLists.txt index ac7f38cc..627f163a 100644 --- a/dGame/CMakeLists.txt +++ b/dGame/CMakeLists.txt @@ -2,7 +2,7 @@ set(DGAME_SOURCES "Character.cpp" "Entity.cpp" "EntityManager.cpp" "LeaderboardManager.cpp" - "Player.cpp" + "PlayerManager.cpp" "TeamManager.cpp" "TradingManager.cpp" "User.cpp" diff --git a/dGame/Character.cpp b/dGame/Character.cpp index ee04710a..eab7583f 100644 --- a/dGame/Character.cpp +++ b/dGame/Character.cpp @@ -451,7 +451,7 @@ void Character::LoadXmlRespawnCheckpoints() { auto* r = points->FirstChildElement("r"); while (r != nullptr) { int32_t map = 0; - NiPoint3 point = NiPoint3::ZERO; + NiPoint3 point = NiPoint3Constant::ZERO; r->QueryAttribute("w", &map); r->QueryAttribute("x", &point.x); @@ -513,7 +513,7 @@ void Character::SetRespawnPoint(LWOMAPID map, const NiPoint3& point) { const NiPoint3& Character::GetRespawnPoint(LWOMAPID map) const { const auto& pair = m_WorldRespawnCheckpoints.find(map); - if (pair == m_WorldRespawnCheckpoints.end()) return NiPoint3::ZERO; + if (pair == m_WorldRespawnCheckpoints.end()) return NiPoint3Constant::ZERO; return pair->second; } diff --git a/dGame/Character.h b/dGame/Character.h index 6c8ead30..b994fb61 100644 --- a/dGame/Character.h +++ b/dGame/Character.h @@ -457,6 +457,8 @@ public: void SetBillboardVisible(bool visible); + User* GetParentUser() const { return m_ParentUser; } + private: void UpdateInfoFromDatabase(); /** diff --git a/dGame/Entity.cpp b/dGame/Entity.cpp index 347b4caa..269b4cc4 100644 --- a/dGame/Entity.cpp +++ b/dGame/Entity.cpp @@ -3,7 +3,6 @@ #include "CDClientManager.h" #include "Game.h" #include "Logger.h" -#include "PacketUtils.h" #include #include "CDDestructibleComponentTable.h" #include "CDClientDatabase.h" @@ -16,7 +15,6 @@ #include "Spawner.h" #include "UserManager.h" #include "dpWorld.h" -#include "Player.h" #include "LUTriggers.h" #include "User.h" #include "EntityTimer.h" @@ -25,6 +23,9 @@ #include "eMissionTaskType.h" #include "eTriggerEventType.h" #include "eObjectBits.h" +#include "PositionUpdate.h" +#include "eChatMessageType.h" +#include "PlayerManager.h" //Component includes: #include "Component.h" @@ -80,6 +81,7 @@ #include "RacingStatsComponent.h" #include "CollectibleComponent.h" #include "ItemComponent.h" +#include "GhostComponent.h" // Table includes #include "CDComponentsRegistryTable.h" @@ -93,7 +95,7 @@ #include "CDSkillBehaviorTable.h" #include "CDZoneTableTable.h" -Entity::Entity(const LWOOBJID& objectID, EntityInfo info, Entity* parentEntity) { +Entity::Entity(const LWOOBJID& objectID, EntityInfo info, User* parentUser, Entity* parentEntity) { m_ObjectID = objectID; m_TemplateID = info.lot; m_ParentEntity = parentEntity; @@ -122,9 +124,42 @@ Entity::Entity(const LWOOBJID& objectID, EntityInfo info, Entity* parentEntity) m_SpawnerNodeID = info.spawnerNodeID; if (info.lot != 1) m_PlayerIsReadyForUpdates = true; + if (parentUser) { + m_Character = parentUser->GetLastUsedChar(); + parentUser->SetLoggedInChar(objectID); + m_GMLevel = m_Character->GetGMLevel(); + + m_Character->SetEntity(this); + + PlayerManager::AddPlayer(this); + } } Entity::~Entity() { + if (IsPlayer()) { + LOG("Deleted player"); + + // Make sure the player exists first. Remove afterwards to prevent the OnPlayerExist functions from not being able to find the player. + if (!PlayerManager::RemovePlayer(this)) { + LOG("Unable to find player to remove from manager."); + return; + } + + Entity* zoneControl = Game::entityManager->GetZoneControlEntity(); + for (CppScripts::Script* script : CppScripts::GetEntityScripts(zoneControl)) { + script->OnPlayerExit(zoneControl, this); + } + + std::vector scriptedActs = Game::entityManager->GetEntitiesByComponent(eReplicaComponentType::SCRIPTED_ACTIVITY); + for (Entity* scriptEntity : scriptedActs) { + if (scriptEntity->GetObjectID() != zoneControl->GetObjectID()) { // Don't want to trigger twice on instance worlds + for (CppScripts::Script* script : CppScripts::GetEntityScripts(scriptEntity)) { + script->OnPlayerExit(scriptEntity, this); + } + } + } + } + if (m_Character) { m_Character->SaveXMLToDatabase(); } @@ -180,7 +215,7 @@ void Entity::Initialize() { } // Get the registry table - CDComponentsRegistryTable* compRegistryTable = CDClientManager::Instance().GetTable(); + CDComponentsRegistryTable* compRegistryTable = CDClientManager::GetTable(); /** * Special case for BBB models. They have components not corresponding to the registry. @@ -210,7 +245,7 @@ void Entity::Initialize() { * Not all components are implemented. Some are represented by a nullptr, as they hold no data. */ - if (GetParentUser()) { + if (m_Character && m_Character->GetParentUser()) { AddComponent()->LoadFromXml(m_Character->GetXMLDoc()); } @@ -334,7 +369,7 @@ void Entity::Initialize() { if (quickBuildComponentID > 0) componentID = quickBuildComponentID; if (buffComponentID > 0) componentID = buffComponentID; - CDDestructibleComponentTable* destCompTable = CDClientManager::Instance().GetTable(); + CDDestructibleComponentTable* destCompTable = CDClientManager::GetTable(); std::vector destCompData = destCompTable->Query([=](CDDestructibleComponent entry) { return (entry.id == componentID); }); bool isSmashable = GetVarAs(u"is_smashable") != 0; @@ -369,7 +404,7 @@ void Entity::Initialize() { uint32_t npcMinLevel = destCompData[0].level; uint32_t currencyIndex = destCompData[0].CurrencyIndex; - CDCurrencyTableTable* currencyTable = CDClientManager::Instance().GetTable(); + CDCurrencyTableTable* currencyTable = CDClientManager::GetTable(); std::vector currencyValues = currencyTable->Query([=](CDCurrencyTable entry) { return (entry.currencyIndex == currencyIndex && entry.npcminlevel == npcMinLevel); }); if (currencyValues.size() > 0) { @@ -417,10 +452,10 @@ void Entity::Initialize() { if (!setFaction.empty()) { // TODO also split on space here however we do not have a general util for splitting on multiple characters yet. std::vector factionsToAdd = GeneralUtils::SplitString(setFaction, ';'); - int32_t factionToAdd; for (const auto faction : factionsToAdd) { - if (GeneralUtils::TryParse(faction, factionToAdd)) { - comp->AddFaction(factionToAdd, true); + const auto factionToAdd = GeneralUtils::TryParse(faction); + if (factionToAdd) { + comp->AddFaction(factionToAdd.value(), true); } } } @@ -435,7 +470,10 @@ void Entity::Initialize() { AddComponent(); - AddComponent(m_Character)->LoadFromXml(m_Character->GetXMLDoc()); + auto& systemAddress = m_Character->GetParentUser() ? m_Character->GetParentUser()->GetSystemAddress() : UNASSIGNED_SYSTEM_ADDRESS; + AddComponent(m_Character, systemAddress)->LoadFromXml(m_Character->GetXMLDoc()); + + AddComponent(); } if (compRegistryTable->GetByIDAndType(m_TemplateID, eReplicaComponentType::INVENTORY) > 0 || m_Character) { @@ -451,7 +489,7 @@ void Entity::Initialize() { * This is a bit of a mess */ - CDScriptComponentTable* scriptCompTable = CDClientManager::Instance().GetTable(); + CDScriptComponentTable* scriptCompTable = CDClientManager::GetTable(); int32_t scriptComponentID = compRegistryTable->GetByIDAndType(m_TemplateID, eReplicaComponentType::SCRIPT, -1); std::string scriptName = ""; @@ -500,7 +538,7 @@ void Entity::Initialize() { // ZoneControl script if (m_TemplateID == 2365) { - CDZoneTableTable* zoneTable = CDClientManager::Instance().GetTable(); + CDZoneTableTable* zoneTable = CDClientManager::GetTable(); const auto zoneID = Game::zoneManager->GetZoneID(); const CDZoneTable* zoneData = zoneTable->Query(zoneID.GetMapID()); @@ -523,7 +561,7 @@ void Entity::Initialize() { if (int componentID = compRegistryTable->GetByIDAndType(m_TemplateID, eReplicaComponentType::QUICK_BUILD) > 0) { auto* quickBuildComponent = AddComponent(); - CDRebuildComponentTable* rebCompTable = CDClientManager::Instance().GetTable(); + CDRebuildComponentTable* rebCompTable = CDClientManager::GetTable(); std::vector rebCompData = rebCompTable->Query([=](CDRebuildComponent entry) { return (entry.id == quickBuildComponentID); }); if (rebCompData.size() > 0) { @@ -647,7 +685,7 @@ void Entity::Initialize() { int movementAIID = compRegistryTable->GetByIDAndType(m_TemplateID, eReplicaComponentType::MOVEMENT_AI); if (movementAIID > 0) { - CDMovementAIComponentTable* moveAITable = CDClientManager::Instance().GetTable(); + CDMovementAIComponentTable* moveAITable = CDClientManager::GetTable(); std::vector moveAIComp = moveAITable->Query([=](CDMovementAIComponent entry) {return (entry.id == movementAIID); }); if (moveAIComp.size() > 0) { @@ -711,7 +749,7 @@ void Entity::Initialize() { int proximityMonitorID = compRegistryTable->GetByIDAndType(m_TemplateID, eReplicaComponentType::PROXIMITY_MONITOR); if (proximityMonitorID > 0) { - CDProximityMonitorComponentTable* proxCompTable = CDClientManager::Instance().GetTable(); + CDProximityMonitorComponentTable* proxCompTable = CDClientManager::GetTable(); std::vector proxCompData = proxCompTable->Query([=](CDProximityMonitorComponent entry) { return (entry.id == proximityMonitorID); }); if (proxCompData.size() > 0) { std::vector proximityStr = GeneralUtils::SplitString(proxCompData[0].Proximities, ','); @@ -784,14 +822,6 @@ bool Entity::operator!=(const Entity& other) const { return other.m_ObjectID != m_ObjectID; } -User* Entity::GetParentUser() const { - if (!IsPlayer()) { - return nullptr; - } - - return static_cast(this)->GetParentUser(); -} - Component* Entity::GetComponent(eReplicaComponentType componentID) const { const auto& index = m_Components.find(componentID); @@ -846,18 +876,24 @@ void Entity::SetProximityRadius(dpEntity* entity, std::string name) { void Entity::SetGMLevel(eGameMasterLevel value) { m_GMLevel = value; - if (GetParentUser()) { - Character* character = GetParentUser()->GetLastUsedChar(); + if (m_Character) m_Character->SetGMLevel(value); - if (character) { - character->SetGMLevel(value); - } - } + auto* characterComponent = GetComponent(); + if (!characterComponent) return; - CharacterComponent* character = GetComponent(); - if (character) character->SetGMLevel(value); + characterComponent->SetGMLevel(value); GameMessages::SendGMLevelBroadcast(m_ObjectID, value); + + // Update the chat server of our GM Level + { + CBITSTREAM; + BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, eChatMessageType::GMLEVEL_UPDATE); + bitStream.Write(m_ObjectID); + bitStream.Write(m_GMLevel); + + Game::chatServer->Send(&bitStream, SYSTEM_PRIORITY, RELIABLE, 0, Game::chatSysAddr, false); + } } void Entity::WriteBaseReplicaData(RakNet::BitStream* outBitStream, eReplicaPacketType packetType) { @@ -1342,17 +1378,11 @@ void Entity::OnCollisionPhantom(const LWOOBJID otherEntity) { } if (!other->GetIsDead()) { - auto* combat = GetComponent(); - - if (combat != nullptr) { + if (GetComponent() != nullptr) { const auto index = std::find(m_TargetsInPhantom.begin(), m_TargetsInPhantom.end(), otherEntity); if (index != m_TargetsInPhantom.end()) return; - const auto valid = combat->IsEnemy(otherEntity); - - if (!valid) return; - m_TargetsInPhantom.push_back(otherEntity); } } @@ -1621,18 +1651,23 @@ bool Entity::GetIsDead() const { void Entity::AddLootItem(const Loot::Info& info) { if (!IsPlayer()) return; - auto& droppedLoot = static_cast(this)->GetDroppedLoot(); + + auto* characterComponent = GetComponent(); + if (!characterComponent) return; + + auto& droppedLoot = characterComponent->GetDroppedLoot(); droppedLoot.insert(std::make_pair(info.id, info)); } void Entity::PickupItem(const LWOOBJID& objectID) { if (!IsPlayer()) return; InventoryComponent* inv = GetComponent(); - if (!inv) return; + auto* characterComponent = GetComponent(); + if (!inv || !characterComponent) return; - CDObjectsTable* objectsTable = CDClientManager::Instance().GetTable(); + CDObjectsTable* objectsTable = CDClientManager::GetTable(); - auto& droppedLoot = static_cast(this)->GetDroppedLoot(); + auto& droppedLoot = characterComponent->GetDroppedLoot(); for (const auto& p : droppedLoot) { if (p.first == objectID) { @@ -1643,13 +1678,13 @@ void Entity::PickupItem(const LWOOBJID& objectID) { const CDObjects& object = objectsTable->GetByID(p.second.lot); if (object.id != 0 && object.type == "Powerup") { - CDObjectSkillsTable* skillsTable = CDClientManager::Instance().GetTable(); + CDObjectSkillsTable* skillsTable = CDClientManager::GetTable(); std::vector skills = skillsTable->Query([=](CDObjectSkills entry) {return (entry.objectTemplate == p.second.lot); }); for (CDObjectSkills skill : skills) { - CDSkillBehaviorTable* skillBehTable = CDClientManager::Instance().GetTable(); - CDSkillBehavior behaviorData = skillBehTable->GetSkillByID(skill.skillID); + CDSkillBehaviorTable* skillBehTable = CDClientManager::GetTable(); - SkillComponent::HandleUnmanaged(behaviorData.behaviorID, GetObjectID()); + auto* skillComponent = GetComponent(); + if (skillComponent) skillComponent->CastSkill(skill.skillID, GetObjectID(), GetObjectID()); auto* missionComponent = GetComponent(); @@ -1668,22 +1703,28 @@ void Entity::PickupItem(const LWOOBJID& objectID) { bool Entity::CanPickupCoins(uint64_t count) { if (!IsPlayer()) return false; - auto* player = static_cast(this); - auto droppedCoins = player->GetDroppedCoins(); + + auto* characterComponent = GetComponent(); + if (!characterComponent) return false; + + auto droppedCoins = characterComponent->GetDroppedCoins(); if (count > droppedCoins) { return false; } else { - player->SetDroppedCoins(droppedCoins - count); + characterComponent->SetDroppedCoins(droppedCoins - count); return true; } } void Entity::RegisterCoinDrop(uint64_t count) { if (!IsPlayer()) return; - auto* player = static_cast(this); - auto droppedCoins = player->GetDroppedCoins(); + + auto* characterComponent = GetComponent(); + if (!characterComponent) return; + + auto droppedCoins = characterComponent->GetDroppedCoins(); droppedCoins += count; - player->SetDroppedCoins(droppedCoins); + characterComponent->SetDroppedCoins(droppedCoins); } void Entity::AddChild(Entity* child) { @@ -1848,7 +1889,7 @@ const NiPoint3& Entity::GetPosition() const { return vehicel->GetPosition(); } - return NiPoint3::ZERO; + return NiPoint3Constant::ZERO; } const NiQuaternion& Entity::GetRotation() const { @@ -1876,10 +1917,10 @@ const NiQuaternion& Entity::GetRotation() const { return vehicel->GetRotation(); } - return NiQuaternion::IDENTITY; + return NiQuaternionConstant::IDENTITY; } -void Entity::SetPosition(NiPoint3 position) { +void Entity::SetPosition(const NiPoint3& position) { auto* controllable = GetComponent(); if (controllable != nullptr) { @@ -1907,7 +1948,7 @@ void Entity::SetPosition(NiPoint3 position) { Game::entityManager->SerializeEntity(this); } -void Entity::SetRotation(NiQuaternion rotation) { +void Entity::SetRotation(const NiQuaternion& rotation) { auto* controllable = GetComponent(); if (controllable != nullptr) { @@ -1977,25 +2018,21 @@ void Entity::SetNetworkId(const uint16_t id) { m_NetworkID = id; } -std::vector& Entity::GetTargetsInPhantom() { - std::vector valid; - +std::vector Entity::GetTargetsInPhantom() { // Clean up invalid targets, like disconnected players - for (auto i = 0u; i < m_TargetsInPhantom.size(); ++i) { - const auto id = m_TargetsInPhantom.at(i); + m_TargetsInPhantom.erase(std::remove_if(m_TargetsInPhantom.begin(), m_TargetsInPhantom.end(), [](const LWOOBJID id) { + return !Game::entityManager->GetEntity(id); + }), m_TargetsInPhantom.end()); - auto* entity = Game::entityManager->GetEntity(id); + std::vector enemies; + for (const auto id : m_TargetsInPhantom) { + auto* combat = GetComponent(); + if (!combat || !combat->IsEnemy(id)) continue; - if (entity == nullptr) { - continue; - } - - valid.push_back(id); + enemies.push_back(id); } - m_TargetsInPhantom = valid; - - return m_TargetsInPhantom; + return enemies; } void Entity::SendNetworkVar(const std::string& data, const SystemAddress& sysAddr) { @@ -2056,3 +2093,99 @@ uint8_t Entity::GetCollectibleID() const { auto* collectible = GetComponent(); return collectible ? collectible->GetCollectibleId() : 0; } + +void Entity::ProcessPositionUpdate(PositionUpdate& update) { + if (!IsPlayer()) return; + auto* controllablePhysicsComponent = GetComponent(); + if (!controllablePhysicsComponent) return; + + auto* possessorComponent = GetComponent(); + bool updateChar = true; + + if (possessorComponent) { + auto* possassableEntity = Game::entityManager->GetEntity(possessorComponent->GetPossessable()); + + if (possassableEntity) { + auto* possessableComponent = possassableEntity->GetComponent(); + + // While possessing something, only update char if we are attached to the thing we are possessing + updateChar = possessableComponent && possessableComponent->GetPossessionType() == ePossessionType::ATTACHED_VISIBLE; + + auto* havokVehiclePhysicsComponent = possassableEntity->GetComponent(); + if (havokVehiclePhysicsComponent) { + havokVehiclePhysicsComponent->SetPosition(update.position); + havokVehiclePhysicsComponent->SetRotation(update.rotation); + havokVehiclePhysicsComponent->SetIsOnGround(update.onGround); + havokVehiclePhysicsComponent->SetIsOnRail(update.onRail); + havokVehiclePhysicsComponent->SetVelocity(update.velocity); + havokVehiclePhysicsComponent->SetDirtyVelocity(update.velocity != NiPoint3Constant::ZERO); + havokVehiclePhysicsComponent->SetAngularVelocity(update.angularVelocity); + havokVehiclePhysicsComponent->SetDirtyAngularVelocity(update.angularVelocity != NiPoint3Constant::ZERO); + havokVehiclePhysicsComponent->SetRemoteInputInfo(update.remoteInputInfo); + } else { + // Need to get the mount's controllable physics + auto* possessedControllablePhysicsComponent = possassableEntity->GetComponent(); + if (!possessedControllablePhysicsComponent) return; + possessedControllablePhysicsComponent->SetPosition(update.position); + possessedControllablePhysicsComponent->SetRotation(update.rotation); + possessedControllablePhysicsComponent->SetIsOnGround(update.onGround); + possessedControllablePhysicsComponent->SetIsOnRail(update.onRail); + possessedControllablePhysicsComponent->SetVelocity(update.velocity); + possessedControllablePhysicsComponent->SetDirtyVelocity(update.velocity != NiPoint3Constant::ZERO); + possessedControllablePhysicsComponent->SetAngularVelocity(update.angularVelocity); + possessedControllablePhysicsComponent->SetDirtyAngularVelocity(update.angularVelocity != NiPoint3Constant::ZERO); + } + Game::entityManager->SerializeEntity(possassableEntity); + } + } + + if (!updateChar) { + update.velocity = NiPoint3Constant::ZERO; + update.angularVelocity = NiPoint3Constant::ZERO; + } + + // Handle statistics + auto* characterComponent = GetComponent(); + if (characterComponent) { + characterComponent->TrackPositionUpdate(update.position); + } + + controllablePhysicsComponent->SetPosition(update.position); + controllablePhysicsComponent->SetRotation(update.rotation); + controllablePhysicsComponent->SetIsOnGround(update.onGround); + controllablePhysicsComponent->SetIsOnRail(update.onRail); + controllablePhysicsComponent->SetVelocity(update.velocity); + controllablePhysicsComponent->SetDirtyVelocity(update.velocity != NiPoint3Constant::ZERO); + controllablePhysicsComponent->SetAngularVelocity(update.angularVelocity); + controllablePhysicsComponent->SetDirtyAngularVelocity(update.angularVelocity != NiPoint3Constant::ZERO); + + auto* ghostComponent = GetComponent(); + if (ghostComponent) ghostComponent->SetGhostReferencePoint(update.position); + Game::entityManager->QueueGhostUpdate(GetObjectID()); + + if (updateChar) Game::entityManager->SerializeEntity(this); +} + +const SystemAddress& Entity::GetSystemAddress() const { + auto* characterComponent = GetComponent(); + return characterComponent ? characterComponent->GetSystemAddress() : UNASSIGNED_SYSTEM_ADDRESS; +} + +const NiPoint3& Entity::GetRespawnPosition() const { + auto* characterComponent = GetComponent(); + return characterComponent ? characterComponent->GetRespawnPosition() : NiPoint3Constant::ZERO; +} + +const NiQuaternion& Entity::GetRespawnRotation() const { + auto* characterComponent = GetComponent(); + return characterComponent ? characterComponent->GetRespawnRotation() : NiQuaternionConstant::IDENTITY; +} + +void Entity::SetRespawnPos(const NiPoint3& position) { + auto* characterComponent = GetComponent(); + if (characterComponent) characterComponent->SetRespawnPos(position); +} +void Entity::SetRespawnRot(const NiQuaternion& rotation) { + auto* characterComponent = GetComponent(); + if (characterComponent) characterComponent->SetRespawnRot(rotation); +} diff --git a/dGame/Entity.h b/dGame/Entity.h index 83e8dc3f..6546e458 100644 --- a/dGame/Entity.h +++ b/dGame/Entity.h @@ -31,6 +31,7 @@ class Component; class Item; class Character; class EntityCallbackTimer; +class PositionUpdate; enum class eTriggerEventType; enum class eGameMasterLevel : uint8_t; enum class eReplicaComponentType : uint32_t; @@ -46,10 +47,10 @@ namespace CppScripts { */ class Entity { public: - explicit Entity(const LWOOBJID& objectID, EntityInfo info, Entity* parentEntity = nullptr); - virtual ~Entity(); + explicit Entity(const LWOOBJID& objectID, EntityInfo info, User* parentUser = nullptr, Entity* parentEntity = nullptr); + ~Entity(); - virtual void Initialize(); + void Initialize(); bool operator==(const Entity& other) const; bool operator!=(const Entity& other) const; @@ -103,9 +104,7 @@ public: const NiQuaternion& GetRotation() const; - virtual User* GetParentUser() const; - - virtual SystemAddress GetSystemAddress() const { return UNASSIGNED_SYSTEM_ADDRESS; }; + const SystemAddress& GetSystemAddress() const; /** * Setters @@ -123,15 +122,13 @@ public: void SetNetworkId(uint16_t id); - void SetPosition(NiPoint3 position); + void SetPosition(const NiPoint3& position); - void SetRotation(NiQuaternion rotation); + void SetRotation(const NiQuaternion& rotation); - virtual void SetRespawnPos(NiPoint3 position) {} + void SetRespawnPos(const NiPoint3& position); - virtual void SetRespawnRot(NiQuaternion rotation) {} - - virtual void SetSystemAddress(const SystemAddress& value) {}; + void SetRespawnRot(const NiQuaternion& rotation); /** * Component management @@ -228,8 +225,8 @@ public: void TriggerEvent(eTriggerEventType event, Entity* optionalTarget = nullptr); void ScheduleDestructionAfterUpdate() { m_ShouldDestroyAfterUpdate = true; } - virtual NiPoint3 GetRespawnPosition() const { return NiPoint3::ZERO; } - virtual NiQuaternion GetRespawnRotation() const { return NiQuaternion::IDENTITY; } + const NiPoint3& GetRespawnPosition() const; + const NiQuaternion& GetRespawnRotation() const; void Sleep(); void Wake(); @@ -292,10 +289,12 @@ public: /* * Collision */ - std::vector& GetTargetsInPhantom(); + std::vector GetTargetsInPhantom(); Entity* GetScheduledKiller() { return m_ScheduleKiller; } + void ProcessPositionUpdate(PositionUpdate& update); + protected: LWOOBJID m_ObjectID; @@ -396,14 +395,8 @@ const T& Entity::GetVar(const std::u16string& name) const { template T Entity::GetVarAs(const std::u16string& name) const { const auto data = GetVarAsString(name); - - T value; - - if (!GeneralUtils::TryParse(data, value)) { - return LDFData::Default; - } - - return value; + + return GeneralUtils::TryParse(data).value_or(LDFData::Default); } template diff --git a/dGame/EntityManager.cpp b/dGame/EntityManager.cpp index a098dbcf..fc9fa0f0 100644 --- a/dGame/EntityManager.cpp +++ b/dGame/EntityManager.cpp @@ -7,11 +7,9 @@ #include "GeneralUtils.h" #include "dServer.h" #include "Spawner.h" -#include "Player.h" #include "SkillComponent.h" #include "SwitchComponent.h" #include "UserManager.h" -#include "PacketUtils.h" #include "Metrics.hpp" #include "dZoneManager.h" #include "MissionComponent.h" @@ -24,6 +22,9 @@ #include "eGameMasterLevel.h" #include "eReplicaComponentType.h" #include "eReplicaPacketType.h" +#include "PlayerManager.h" +#include "GhostComponent.h" +#include // Configure which zones have ghosting disabled, mostly small worlds. std::vector EntityManager::m_GhostingExcludedZones = { @@ -117,14 +118,7 @@ Entity* EntityManager::CreateEntity(EntityInfo info, User* user, Entity* parentE info.id = id; - Entity* entity; - - // Check if the entitty if a player, in case use the extended player entity class - if (user != nullptr) { - entity = new Player(id, info, user, parentEntity); - } else { - entity = new Entity(id, info, parentEntity); - } + Entity* entity = new Entity(id, info, user, parentEntity); // Initialize the entity entity->Initialize(); @@ -172,8 +166,8 @@ void EntityManager::DestroyEntity(Entity* entity) { } void EntityManager::SerializeEntities() { - for (int32_t i = 0; i < m_EntitiesToSerialize.size(); i++) { - const LWOOBJID toSerialize = m_EntitiesToSerialize.at(i); + for (size_t i = 0; i < m_EntitiesToSerialize.size(); i++) { + const LWOOBJID toSerialize = m_EntitiesToSerialize[i]; auto* entity = GetEntity(toSerialize); if (!entity) continue; @@ -188,8 +182,9 @@ void EntityManager::SerializeEntities() { entity->WriteComponents(&stream, eReplicaPacketType::SERIALIZATION); if (entity->GetIsGhostingCandidate()) { - for (auto* player : Player::GetAllPlayers()) { - if (player->IsObserved(toSerialize)) { + for (auto* player : PlayerManager::GetAllPlayers()) { + auto* ghostComponent = player->GetComponent(); + if (ghostComponent && ghostComponent->IsObserved(toSerialize)) { Game::server->Send(&stream, player->GetSystemAddress(), false); } } @@ -201,8 +196,8 @@ void EntityManager::SerializeEntities() { } void EntityManager::KillEntities() { - for (int32_t i = 0; i < m_EntitiesToKill.size(); i++) { - const LWOOBJID toKill = m_EntitiesToKill.at(i); + for (size_t i = 0; i < m_EntitiesToKill.size(); i++) { + const LWOOBJID toKill = m_EntitiesToKill[i]; auto* entity = GetEntity(toKill); if (!entity) { @@ -220,8 +215,8 @@ void EntityManager::KillEntities() { } void EntityManager::DeleteEntities() { - for (int32_t i = 0; i < m_EntitiesToDelete.size(); i++) { - const LWOOBJID toDelete = m_EntitiesToDelete.at(i); + for (size_t i = 0; i < m_EntitiesToDelete.size(); i++) { + const LWOOBJID toDelete = m_EntitiesToDelete[i]; auto entityToDelete = GetEntity(toDelete); if (entityToDelete) { // Get all this info first before we delete the player. @@ -244,8 +239,8 @@ void EntityManager::DeleteEntities() { } void EntityManager::UpdateEntities(const float deltaTime) { - for (const auto& e : m_Entities) { - e.second->Update(deltaTime); + for (auto* entity : m_Entities | std::views::values) { + entity->Update(deltaTime); } SerializeEntities(); @@ -265,10 +260,10 @@ Entity* EntityManager::GetEntity(const LWOOBJID& objectId) const { std::vector EntityManager::GetEntitiesInGroup(const std::string& group) { std::vector entitiesInGroup; - for (const auto& entity : m_Entities) { - for (const auto& entityGroup : entity.second->GetGroups()) { + for (auto* entity : m_Entities | std::views::values) { + for (const auto& entityGroup : entity->GetGroups()) { if (entityGroup == group) { - entitiesInGroup.push_back(entity.second); + entitiesInGroup.push_back(entity); } } } @@ -278,10 +273,12 @@ std::vector EntityManager::GetEntitiesInGroup(const std::string& group) std::vector EntityManager::GetEntitiesByComponent(const eReplicaComponentType componentType) const { std::vector withComp; - for (const auto& entity : m_Entities) { - if (componentType != eReplicaComponentType::INVALID && !entity.second->HasComponent(componentType)) continue; + if (componentType != eReplicaComponentType::INVALID) { + for (auto* entity : m_Entities | std::views::values) { + if (!entity->HasComponent(componentType)) continue; - withComp.push_back(entity.second); + withComp.push_back(entity); + } } return withComp; } @@ -289,19 +286,19 @@ std::vector EntityManager::GetEntitiesByComponent(const eReplicaCompone std::vector EntityManager::GetEntitiesByLOT(const LOT& lot) const { std::vector entities; - for (const auto& entity : m_Entities) { - if (entity.second->GetLOT() == lot) - entities.push_back(entity.second); + for (auto* entity : m_Entities | std::views::values) { + if (entity->GetLOT() == lot) entities.push_back(entity); } return entities; } -std::vector EntityManager::GetEntitiesByProximity(NiPoint3 reference, float radius) const{ - std::vector entities = {}; - if (radius > 1000.0f) return entities; - for (const auto& entity : m_Entities) { - if (NiPoint3::Distance(reference, entity.second->GetPosition()) <= radius) entities.push_back(entity.second); +std::vector EntityManager::GetEntitiesByProximity(NiPoint3 reference, float radius) const { + std::vector entities; + if (radius <= 1000.0f) { // The client has a 1000 unit limit on this same logic, so we'll use the same limit + for (auto* entity : m_Entities | std::views::values) { + if (NiPoint3::Distance(reference, entity->GetPosition()) <= radius) entities.push_back(entity); + } } return entities; } @@ -315,12 +312,8 @@ Entity* EntityManager::GetSpawnPointEntity(const std::string& spawnName) const { // Lookup the spawn point entity in the map const auto& spawnPoint = m_SpawnPoints.find(spawnName); - if (spawnPoint == m_SpawnPoints.end()) { - return nullptr; - } - // Check if the spawn point entity is valid just in case - return GetEntity(spawnPoint->second); + return spawnPoint == m_SpawnPoints.end() ? nullptr : GetEntity(spawnPoint->second); } const std::unordered_map& EntityManager::GetSpawnPointEntities() const { @@ -346,29 +339,25 @@ void EntityManager::ConstructEntity(Entity* entity, const SystemAddress& sysAddr entity->SetNetworkId(networkId); } - const auto checkGhosting = entity->GetIsGhostingCandidate(); - - if (checkGhosting) { - const auto& iter = std::find(m_EntitiesToGhost.begin(), m_EntitiesToGhost.end(), entity); - - if (iter == m_EntitiesToGhost.end()) { + if (entity->GetIsGhostingCandidate()) { + if (std::find(m_EntitiesToGhost.begin(), m_EntitiesToGhost.end(), entity) == m_EntitiesToGhost.end()) { m_EntitiesToGhost.push_back(entity); } - } - if (checkGhosting && sysAddr == UNASSIGNED_SYSTEM_ADDRESS) { - CheckGhosting(entity); + if (sysAddr == UNASSIGNED_SYSTEM_ADDRESS) { + CheckGhosting(entity); - return; + return; + } } m_SerializationCounter++; RakNet::BitStream stream; - stream.Write(ID_REPLICA_MANAGER_CONSTRUCTION); + stream.Write(ID_REPLICA_MANAGER_CONSTRUCTION); stream.Write(true); - stream.Write(entity->GetNetworkId()); + stream.Write(entity->GetNetworkId()); entity->WriteBaseReplicaData(&stream, eReplicaPacketType::CONSTRUCTION); entity->WriteComponents(&stream, eReplicaPacketType::CONSTRUCTION); @@ -377,11 +366,12 @@ void EntityManager::ConstructEntity(Entity* entity, const SystemAddress& sysAddr if (skipChecks) { Game::server->Send(&stream, UNASSIGNED_SYSTEM_ADDRESS, true); } else { - for (auto* player : Player::GetAllPlayers()) { + for (auto* player : PlayerManager::GetAllPlayers()) { if (player->GetPlayerReadyForUpdates()) { Game::server->Send(&stream, player->GetSystemAddress(), false); } else { - player->AddLimboConstruction(entity->GetObjectID()); + auto* ghostComponent = player->GetComponent(); + if (ghostComponent) ghostComponent->AddLimboConstruction(entity->GetObjectID()); } } } @@ -389,8 +379,6 @@ void EntityManager::ConstructEntity(Entity* entity, const SystemAddress& sysAddr Game::server->Send(&stream, sysAddr, false); } - // PacketUtils::SavePacket("[24]_"+std::to_string(entity->GetObjectID()) + "_" + std::to_string(m_SerializationCounter) + ".bin", (char*)stream.GetData(), stream.GetNumberOfBytesUsed()); - if (entity->IsPlayer()) { if (entity->GetGMLevel() > eGameMasterLevel::CIVILIAN) { GameMessages::SendToggleGMInvis(entity->GetObjectID(), true, sysAddr); @@ -402,13 +390,13 @@ void EntityManager::ConstructAllEntities(const SystemAddress& sysAddr) { //ZoneControl is special: ConstructEntity(m_ZoneControlEntity, sysAddr); - for (const auto& e : m_Entities) { - if (e.second && (e.second->GetSpawnerID() != 0 || e.second->GetLOT() == 1) && !e.second->GetIsGhostingCandidate()) { - ConstructEntity(e.second, sysAddr); + for (auto* entity : m_Entities | std::views::values) { + if (entity && (entity->GetSpawnerID() != 0 || entity->GetLOT() == 1) && !entity->GetIsGhostingCandidate()) { + ConstructEntity(entity, sysAddr); } } - UpdateGhosting(Player::GetPlayer(sysAddr)); + UpdateGhosting(PlayerManager::GetPlayer(sysAddr)); } void EntityManager::DestructEntity(Entity* entity, const SystemAddress& sysAddr) { @@ -416,14 +404,15 @@ void EntityManager::DestructEntity(Entity* entity, const SystemAddress& sysAddr) RakNet::BitStream stream; - stream.Write(ID_REPLICA_MANAGER_DESTRUCTION); - stream.Write(entity->GetNetworkId()); + stream.Write(ID_REPLICA_MANAGER_DESTRUCTION); + stream.Write(entity->GetNetworkId()); Game::server->Send(&stream, sysAddr, sysAddr == UNASSIGNED_SYSTEM_ADDRESS); - for (auto* player : Player::GetAllPlayers()) { + for (auto* player : PlayerManager::GetAllPlayers()) { if (!player->GetPlayerReadyForUpdates()) { - player->RemoveLimboConstruction(entity->GetObjectID()); + auto* ghostComponent = player->GetComponent(); + if (ghostComponent) ghostComponent->RemoveLimboConstruction(entity->GetObjectID()); } } } @@ -434,13 +423,11 @@ void EntityManager::SerializeEntity(Entity* entity) { if (std::find(m_EntitiesToSerialize.begin(), m_EntitiesToSerialize.end(), entity->GetObjectID()) == m_EntitiesToSerialize.end()) { m_EntitiesToSerialize.push_back(entity->GetObjectID()); } - - //PacketUtils::SavePacket(std::to_string(m_SerializationCounter) + "_[27]_"+std::to_string(entity->GetObjectID()) + ".bin", (char*)stream.GetData(), stream.GetNumberOfBytesUsed()); } void EntityManager::DestructAllEntities(const SystemAddress& sysAddr) { - for (const auto& e : m_Entities) { - DestructEntity(e.second, sysAddr); + for (auto* entity : m_Entities | std::views::values) { + DestructEntity(entity, sysAddr); } } @@ -448,29 +435,19 @@ void EntityManager::SetGhostDistanceMax(float value) { m_GhostDistanceMaxSquared = value * value; } -float EntityManager::GetGhostDistanceMax() const { - return std::sqrt(m_GhostDistanceMaxSquared); -} - void EntityManager::SetGhostDistanceMin(float value) { m_GhostDistanceMinSqaured = value * value; } -float EntityManager::GetGhostDistanceMin() const { - return std::sqrt(m_GhostDistanceMinSqaured); -} - void EntityManager::QueueGhostUpdate(LWOOBJID playerID) { - const auto& iter = std::find(m_PlayersToUpdateGhosting.begin(), m_PlayersToUpdateGhosting.end(), playerID); - - if (iter == m_PlayersToUpdateGhosting.end()) { + if (std::find(m_PlayersToUpdateGhosting.begin(), m_PlayersToUpdateGhosting.end(), playerID) == m_PlayersToUpdateGhosting.end()) { m_PlayersToUpdateGhosting.push_back(playerID); } } void EntityManager::UpdateGhosting() { for (const auto playerID : m_PlayersToUpdateGhosting) { - auto* player = Player::GetPlayer(playerID); + auto* player = PlayerManager::GetPlayer(playerID); if (player == nullptr) { continue; @@ -482,40 +459,36 @@ void EntityManager::UpdateGhosting() { m_PlayersToUpdateGhosting.clear(); } -void EntityManager::UpdateGhosting(Player* player) { - if (player == nullptr) { - return; - } +void EntityManager::UpdateGhosting(Entity* player) { + if (!player) return; auto* missionComponent = player->GetComponent(); + auto* ghostComponent = player->GetComponent(); - if (missionComponent == nullptr) { - return; - } + if (!missionComponent || !ghostComponent) return; - const auto& referencePoint = player->GetGhostReferencePoint(); - const auto isOverride = player->GetGhostOverride(); + const auto& referencePoint = ghostComponent->GetGhostReferencePoint(); + const auto isOverride = ghostComponent->GetGhostOverride(); for (auto* entity : m_EntitiesToGhost) { - const auto isAudioEmitter = entity->GetLOT() == 6368; - const auto& entityPoint = entity->GetPosition(); - const int32_t id = entity->GetObjectID(); + const auto id = entity->GetObjectID(); - const auto observed = player->IsObserved(id); + const auto observed = ghostComponent->IsObserved(id); const auto distance = NiPoint3::DistanceSquared(referencePoint, entityPoint); auto ghostingDistanceMax = m_GhostDistanceMaxSquared; auto ghostingDistanceMin = m_GhostDistanceMinSqaured; + const auto isAudioEmitter = entity->GetLOT() == 6368; // https://explorer.lu/objects/6368 if (isAudioEmitter) { ghostingDistanceMax = ghostingDistanceMin; } if (observed && distance > ghostingDistanceMax && !isOverride) { - player->GhostEntity(id); + ghostComponent->GhostEntity(id); DestructEntity(entity, player->GetSystemAddress()); @@ -532,7 +505,7 @@ void EntityManager::UpdateGhosting(Player* player) { } } - player->ObserveEntity(id); + ghostComponent->ObserveEntity(id); ConstructEntity(entity, player->GetSystemAddress()); @@ -548,28 +521,26 @@ void EntityManager::CheckGhosting(Entity* entity) { const auto& referencePoint = entity->GetPosition(); - auto ghostingDistanceMax = m_GhostDistanceMaxSquared; - auto ghostingDistanceMin = m_GhostDistanceMinSqaured; + for (auto* player : PlayerManager::GetAllPlayers()) { + auto* ghostComponent = player->GetComponent(); + if (!ghostComponent) continue; - const auto isAudioEmitter = entity->GetLOT() == 6368; + const auto& entityPoint = ghostComponent->GetGhostReferencePoint(); - for (auto* player : Player::GetAllPlayers()) { - const auto& entityPoint = player->GetGhostReferencePoint(); + const auto id = entity->GetObjectID(); - const int32_t id = entity->GetObjectID(); - - const auto observed = player->IsObserved(id); + const auto observed = ghostComponent->IsObserved(id); const auto distance = NiPoint3::DistanceSquared(referencePoint, entityPoint); - if (observed && distance > ghostingDistanceMax) { - player->GhostEntity(id); + if (observed && distance > m_GhostDistanceMaxSquared) { + ghostComponent->GhostEntity(id); DestructEntity(entity, player->GetSystemAddress()); entity->SetObservers(entity->GetObservers() - 1); - } else if (!observed && ghostingDistanceMin > distance) { - player->ObserveEntity(id); + } else if (!observed && m_GhostDistanceMinSqaured > distance) { + ghostComponent->ObserveEntity(id); ConstructEntity(entity, player->GetSystemAddress()); @@ -578,7 +549,7 @@ void EntityManager::CheckGhosting(Entity* entity) { } } -Entity* EntityManager::GetGhostCandidate(int32_t id) { +Entity* EntityManager::GetGhostCandidate(LWOOBJID id) const { for (auto* entity : m_EntitiesToGhost) { if (entity->GetObjectID() == id) { return entity; @@ -604,26 +575,22 @@ void EntityManager::ScheduleForKill(Entity* entity) { const auto objectId = entity->GetObjectID(); - if (std::count(m_EntitiesToKill.begin(), m_EntitiesToKill.end(), objectId)) { - return; + if (std::find(m_EntitiesToKill.begin(), m_EntitiesToKill.end(), objectId) != m_EntitiesToKill.end()) { + m_EntitiesToKill.push_back(objectId); } - - m_EntitiesToKill.push_back(objectId); } void EntityManager::ScheduleForDeletion(LWOOBJID entity) { - if (std::count(m_EntitiesToDelete.begin(), m_EntitiesToDelete.end(), entity)) { - return; + if (std::find(m_EntitiesToDelete.begin(), m_EntitiesToDelete.end(), entity) != m_EntitiesToDelete.end()) { + m_EntitiesToDelete.push_back(entity); } - - m_EntitiesToDelete.push_back(entity); } void EntityManager::FireEventServerSide(Entity* origin, std::string args) { - for (std::pair e : m_Entities) { - if (e.second) { - e.second->OnFireEventServerSide(origin, args); + for (const auto entity : m_Entities | std::views::values) { + if (entity) { + entity->OnFireEventServerSide(origin, args); } } } diff --git a/dGame/EntityManager.h b/dGame/EntityManager.h index 33d7aaff..abffe546 100644 --- a/dGame/EntityManager.h +++ b/dGame/EntityManager.h @@ -50,14 +50,12 @@ public: void DestructAllEntities(const SystemAddress& sysAddr); void SetGhostDistanceMax(float value); - float GetGhostDistanceMax() const; void SetGhostDistanceMin(float value); - float GetGhostDistanceMin() const; void QueueGhostUpdate(LWOOBJID playerID); void UpdateGhosting(); - void UpdateGhosting(Player* player); + void UpdateGhosting(Entity* player); void CheckGhosting(Entity* entity); - Entity* GetGhostCandidate(int32_t id); + Entity* GetGhostCandidate(LWOOBJID id) const; bool GetGhostingEnabled() const; void ScheduleForKill(Entity* entity); diff --git a/dGame/LeaderboardManager.cpp b/dGame/LeaderboardManager.cpp index 1fd49d26..ba9055a7 100644 --- a/dGame/LeaderboardManager.cpp +++ b/dGame/LeaderboardManager.cpp @@ -402,7 +402,7 @@ Leaderboard::Type LeaderboardManager::GetLeaderboardType(const GameID gameID) { auto lookup = leaderboardCache.find(gameID); if (lookup != leaderboardCache.end()) return lookup->second; - auto* activitiesTable = CDClientManager::Instance().GetTable(); + auto* activitiesTable = CDClientManager::GetTable(); std::vector activities = activitiesTable->Query([gameID](const CDActivities& entry) { return entry.ActivityID == gameID; }); diff --git a/dGame/LeaderboardManager.h b/dGame/LeaderboardManager.h index e2ce3f97..89537ba0 100644 --- a/dGame/LeaderboardManager.h +++ b/dGame/LeaderboardManager.h @@ -6,7 +6,6 @@ #include #include -#include "Singleton.h" #include "dCommonVars.h" #include "LDFFormat.h" diff --git a/dGame/Player.cpp b/dGame/Player.cpp deleted file mode 100644 index abbbf059..00000000 --- a/dGame/Player.cpp +++ /dev/null @@ -1,306 +0,0 @@ -#include "Player.h" - -#include - -#include "Character.h" -#include "Database.h" -#include "MissionComponent.h" -#include "UserManager.h" -#include "EntityManager.h" -#include "Logger.h" -#include "ZoneInstanceManager.h" -#include "WorldPackets.h" -#include "dZoneManager.h" -#include "CharacterComponent.h" -#include "Mail.h" -#include "User.h" -#include "CppScripts.h" -#include "Loot.h" -#include "eReplicaComponentType.h" - -std::vector Player::m_Players = {}; - -Player::Player(const LWOOBJID& objectID, const EntityInfo info, User* user, Entity* parentEntity) : Entity(objectID, info, parentEntity) { - m_ParentUser = user; - m_Character = m_ParentUser->GetLastUsedChar(); - m_ParentUser->SetLoggedInChar(objectID); - m_GMLevel = m_Character->GetGMLevel(); - m_SystemAddress = m_ParentUser->GetSystemAddress(); - m_DroppedLoot = {}; - m_DroppedCoins = 0; - - m_GhostReferencePoint = NiPoint3::ZERO; - m_GhostOverridePoint = NiPoint3::ZERO; - m_GhostOverride = false; - m_ObservedEntitiesLength = 256; - m_ObservedEntitiesUsed = 0; - m_ObservedEntities.resize(m_ObservedEntitiesLength); - - m_Character->SetEntity(this); - - const auto& iter = std::find(m_Players.begin(), m_Players.end(), this); - - if (iter != m_Players.end()) { - return; - } - - m_Players.push_back(this); -} - -User* Player::GetParentUser() const { - return m_ParentUser; -} - -SystemAddress Player::GetSystemAddress() const { - return m_SystemAddress; -} - -void Player::SetSystemAddress(const SystemAddress& value) { - m_SystemAddress = value; -} - -void Player::SetRespawnPos(const NiPoint3 position) { - if (!m_Character) return; - - m_respawnPos = position; - - m_Character->SetRespawnPoint(Game::zoneManager->GetZone()->GetWorldID(), position); -} - -void Player::SetRespawnRot(const NiQuaternion rotation) { - m_respawnRot = rotation; -} - -NiPoint3 Player::GetRespawnPosition() const { - return m_respawnPos; -} - -NiQuaternion Player::GetRespawnRotation() const { - return m_respawnRot; -} - -void Player::SendMail(const LWOOBJID sender, const std::string& senderName, const std::string& subject, const std::string& body, LOT attachment, uint16_t attachmentCount) const { - Mail::SendMail(sender, senderName, this, subject, body, attachment, attachmentCount); -} - -void Player::SendToZone(LWOMAPID zoneId, LWOCLONEID cloneId) { - const auto objid = GetObjectID(); - - ZoneInstanceManager::Instance()->RequestZoneTransfer(Game::server, zoneId, cloneId, false, [objid](bool mythranShift, uint32_t zoneID, uint32_t zoneInstance, uint32_t zoneClone, std::string serverIP, uint16_t serverPort) { - auto* entity = Game::entityManager->GetEntity(objid); - - if (entity == nullptr) { - return; - } - - const auto sysAddr = entity->GetSystemAddress(); - - auto* character = entity->GetCharacter(); - auto* characterComponent = entity->GetComponent(); - - if (character != nullptr && characterComponent != nullptr) { - character->SetZoneID(zoneID); - character->SetZoneInstance(zoneInstance); - character->SetZoneClone(zoneClone); - - characterComponent->SetLastRocketConfig(u""); - - character->SaveXMLToDatabase(); - } - - WorldPackets::SendTransferToWorld(sysAddr, serverIP, serverPort, mythranShift); - - Game::entityManager->DestructEntity(entity); - return; - }); -} - -void Player::AddLimboConstruction(LWOOBJID objectId) { - const auto& iter = std::find(m_LimboConstructions.begin(), m_LimboConstructions.end(), objectId); - - if (iter != m_LimboConstructions.end()) { - return; - } - - m_LimboConstructions.push_back(objectId); -} - -void Player::RemoveLimboConstruction(LWOOBJID objectId) { - const auto& iter = std::find(m_LimboConstructions.begin(), m_LimboConstructions.end(), objectId); - - if (iter == m_LimboConstructions.end()) { - return; - } - - m_LimboConstructions.erase(iter); -} - -void Player::ConstructLimboEntities() { - for (const auto objectId : m_LimboConstructions) { - auto* entity = Game::entityManager->GetEntity(objectId); - - if (entity == nullptr) { - continue; - } - - Game::entityManager->ConstructEntity(entity, m_SystemAddress); - } - - m_LimboConstructions.clear(); -} - -std::map& Player::GetDroppedLoot() { - return m_DroppedLoot; -} - -const NiPoint3& Player::GetGhostReferencePoint() const { - return m_GhostOverride ? m_GhostOverridePoint : m_GhostReferencePoint; -} - -const NiPoint3& Player::GetOriginGhostReferencePoint() const { - return m_GhostReferencePoint; -} - -void Player::SetGhostReferencePoint(const NiPoint3& value) { - m_GhostReferencePoint = value; -} - -void Player::SetGhostOverridePoint(const NiPoint3& value) { - m_GhostOverridePoint = value; -} - -const NiPoint3& Player::GetGhostOverridePoint() const { - return m_GhostOverridePoint; -} - -void Player::SetGhostOverride(bool value) { - m_GhostOverride = value; -} - -bool Player::GetGhostOverride() const { - return m_GhostOverride; -} - -void Player::ObserveEntity(int32_t id) { - for (int32_t i = 0; i < m_ObservedEntitiesUsed; i++) { - if (m_ObservedEntities[i] == 0 || m_ObservedEntities[i] == id) { - m_ObservedEntities[i] = id; - - return; - } - } - - const auto index = m_ObservedEntitiesUsed++; - - if (m_ObservedEntitiesUsed > m_ObservedEntitiesLength) { - m_ObservedEntities.resize(m_ObservedEntitiesLength + m_ObservedEntitiesLength); - - m_ObservedEntitiesLength = m_ObservedEntitiesLength + m_ObservedEntitiesLength; - } - - m_ObservedEntities[index] = id; -} - -bool Player::IsObserved(int32_t id) { - for (int32_t i = 0; i < m_ObservedEntitiesUsed; i++) { - if (m_ObservedEntities[i] == id) { - return true; - } - } - - return false; -} - -void Player::GhostEntity(int32_t id) { - for (int32_t i = 0; i < m_ObservedEntitiesUsed; i++) { - if (m_ObservedEntities[i] == id) { - m_ObservedEntities[i] = 0; - } - } -} - -Player* Player::GetPlayer(const SystemAddress& sysAddr) { - auto* entity = UserManager::Instance()->GetUser(sysAddr)->GetLastUsedChar()->GetEntity(); - - return static_cast(entity); -} - -Player* Player::GetPlayer(const std::string& name) { - const auto characters = Game::entityManager->GetEntitiesByComponent(eReplicaComponentType::CHARACTER); - - for (auto* character : characters) { - if (!character->IsPlayer()) continue; - - if (GeneralUtils::CaseInsensitiveStringCompare(name, character->GetCharacter()->GetName())) { - return dynamic_cast(character); - } - } - - return nullptr; -} - -Player* Player::GetPlayer(LWOOBJID playerID) { - for (auto* player : m_Players) { - if (player->GetObjectID() == playerID) { - return player; - } - } - - return nullptr; -} - -const std::vector& Player::GetAllPlayers() { - return m_Players; -} - -uint64_t Player::GetDroppedCoins() { - return m_DroppedCoins; -} - -void Player::SetDroppedCoins(uint64_t value) { - m_DroppedCoins = value; -} - -Player::~Player() { - LOG("Deleted player"); - - for (int32_t i = 0; i < m_ObservedEntitiesUsed; i++) { - const auto id = m_ObservedEntities[i]; - - if (id == 0) { - continue; - } - - auto* entity = Game::entityManager->GetGhostCandidate(id); - - if (entity != nullptr) { - entity->SetObservers(entity->GetObservers() - 1); - } - } - - m_LimboConstructions.clear(); - - const auto& iter = std::find(m_Players.begin(), m_Players.end(), this); - - if (iter == m_Players.end()) { - return; - } - - if (IsPlayer()) { - Entity* zoneControl = Game::entityManager->GetZoneControlEntity(); - for (CppScripts::Script* script : CppScripts::GetEntityScripts(zoneControl)) { - script->OnPlayerExit(zoneControl, this); - } - - std::vector scriptedActs = Game::entityManager->GetEntitiesByComponent(eReplicaComponentType::SCRIPTED_ACTIVITY); - for (Entity* scriptEntity : scriptedActs) { - if (scriptEntity->GetObjectID() != zoneControl->GetObjectID()) { // Don't want to trigger twice on instance worlds - for (CppScripts::Script* script : CppScripts::GetEntityScripts(scriptEntity)) { - script->OnPlayerExit(scriptEntity, this); - } - } - } - } - - m_Players.erase(iter); -} diff --git a/dGame/Player.h b/dGame/Player.h deleted file mode 100644 index 287ee613..00000000 --- a/dGame/Player.h +++ /dev/null @@ -1,136 +0,0 @@ -#pragma once - -#include "Entity.h" - -/** - * Extended Entity for player data and behavior. - * - * Contains properties only a player entity would require, like associated SystemAddress and User. - * - * Keeps track of which entities are observed by this user for ghosting. - */ -class Player final : public Entity -{ -public: - explicit Player(const LWOOBJID& objectID, EntityInfo info, User* user, Entity* parentEntity = nullptr); - - /** - * Getters - */ - - User* GetParentUser() const override; - - SystemAddress GetSystemAddress() const override; - - NiPoint3 GetRespawnPosition() const override; - - NiQuaternion GetRespawnRotation() const override; - - const NiPoint3& GetGhostReferencePoint() const; - - const NiPoint3& GetOriginGhostReferencePoint() const; - - const NiPoint3& GetGhostOverridePoint() const; - - bool GetGhostOverride() const; - - std::map& GetDroppedLoot(); - - uint64_t GetDroppedCoins(); - - /** - * Setters - */ - - void SetSystemAddress(const SystemAddress& value) override; - - void SetRespawnPos(NiPoint3 position) override; - - void SetRespawnRot(NiQuaternion rotation) override; - - void SetGhostReferencePoint(const NiPoint3& value); - - void SetGhostOverridePoint(const NiPoint3& value); - - void SetGhostOverride(bool value); - - void SetDroppedCoins(uint64_t value); - - /** - * Wrapper for sending an in-game mail. - * - * @param sender id of the sender. LWOOBJID_EMPTY for system mail - * @param senderName name of the sender. Max 32 characters. - * @param subject mail subject. Max 50 characters. - * @param body mail body. Max 400 characters. - * @param attachment LOT of the attached item. LOT_NULL if no attachment. - * @param attachmentCount stack size for attachment. - */ - void SendMail(LWOOBJID sender, const std::string& senderName, const std::string& subject, const std::string& body, LOT attachment, uint16_t attachmentCount) const; - - /** - * Wrapper for transfering the player to another instance. - * - * @param zoneId zoneID for the new instance. - * @param cloneId cloneID for the new instance. - */ - void SendToZone(LWOMAPID zoneId, LWOCLONEID cloneId = 0); - - /** - * Ghosting - */ - - void AddLimboConstruction(LWOOBJID objectId); - - void RemoveLimboConstruction(LWOOBJID objectId); - - void ConstructLimboEntities(); - - void ObserveEntity(int32_t id); - - bool IsObserved(int32_t id); - - void GhostEntity(int32_t id); - - /** - * Static methods - */ - - static Player* GetPlayer(const SystemAddress& sysAddr); - - static Player* GetPlayer(const std::string& name); - - static Player* GetPlayer(LWOOBJID playerID); - - static const std::vector& GetAllPlayers(); - - ~Player() override; -private: - SystemAddress m_SystemAddress; - - NiPoint3 m_respawnPos; - - NiQuaternion m_respawnRot; - - User* m_ParentUser; - - NiPoint3 m_GhostReferencePoint; - - NiPoint3 m_GhostOverridePoint; - - bool m_GhostOverride; - - std::vector m_ObservedEntities; - - int32_t m_ObservedEntitiesLength; - - int32_t m_ObservedEntitiesUsed; - - std::vector m_LimboConstructions; - - std::map m_DroppedLoot; - - uint64_t m_DroppedCoins; - - static std::vector m_Players; -}; diff --git a/dGame/PlayerManager.cpp b/dGame/PlayerManager.cpp new file mode 100644 index 00000000..61e2568d --- /dev/null +++ b/dGame/PlayerManager.cpp @@ -0,0 +1,67 @@ +#include "PlayerManager.h" + +#include "Character.h" +#include "User.h" +#include "UserManager.h" +#include "eReplicaComponentType.h" + +namespace { + std::vector m_Players; +}; + +const std::vector& PlayerManager::GetAllPlayers() { + return m_Players; +} + +void PlayerManager::AddPlayer(Entity* player) { + const auto& iter = std::find(m_Players.begin(), m_Players.end(), player); + + if (iter == m_Players.end()) { + m_Players.push_back(player); + } +} + +bool PlayerManager::RemovePlayer(Entity* player) { + const auto iter = std::find(m_Players.begin(), m_Players.end(), player); + + const bool toReturn = iter != m_Players.end(); + if (toReturn) { + m_Players.erase(iter); + } + + return toReturn; +} + +Entity* PlayerManager::GetPlayer(const SystemAddress& sysAddr) { + auto* entity = UserManager::Instance()->GetUser(sysAddr)->GetLastUsedChar()->GetEntity(); + + return entity; +} + +Entity* PlayerManager::GetPlayer(const std::string& name) { + const auto characters = Game::entityManager->GetEntitiesByComponent(eReplicaComponentType::CHARACTER); + + Entity* player = nullptr; + for (auto* character : characters) { + if (!character->IsPlayer()) continue; + + if (GeneralUtils::CaseInsensitiveStringCompare(name, character->GetCharacter()->GetName())) { + player = character; + break; + } + } + + return player; +} + +Entity* PlayerManager::GetPlayer(LWOOBJID playerID) { + Entity* playerToReturn = nullptr; + for (auto* player : m_Players) { + if (player->GetObjectID() == playerID) { + playerToReturn = player; + break; + } + } + + return playerToReturn; +} diff --git a/dGame/PlayerManager.h b/dGame/PlayerManager.h new file mode 100644 index 00000000..7f6be5ba --- /dev/null +++ b/dGame/PlayerManager.h @@ -0,0 +1,25 @@ +#ifndef __PLAYERMANAGER__H__ +#define __PLAYERMANAGER__H__ + +#include "dCommonVars.h" + +#include + +class Entity; +struct SystemAddress; + +namespace PlayerManager { + void AddPlayer(Entity* player); + + bool RemovePlayer(Entity* player); + + Entity* GetPlayer(const SystemAddress& sysAddr); + + Entity* GetPlayer(const std::string& name); + + Entity* GetPlayer(LWOOBJID playerID); + + const std::vector& GetAllPlayers(); +}; + +#endif //!__PLAYERMANAGER__H__ diff --git a/dGame/UserManager.cpp b/dGame/UserManager.cpp index 736339a4..0fde2eb6 100644 --- a/dGame/UserManager.cpp +++ b/dGame/UserManager.cpp @@ -11,7 +11,6 @@ #include "WorldPackets.h" #include "Character.h" #include "BitStream.h" -#include "PacketUtils.h" #include "ObjectIDManager.h" #include "Logger.h" #include "GeneralUtils.h" @@ -216,31 +215,93 @@ void UserManager::RequestCharacterList(const SystemAddress& sysAddr) { chars.push_back(character); } - WorldPackets::SendCharacterList(sysAddr, u); + RakNet::BitStream bitStream; + BitStreamUtils::WriteHeader(bitStream, eConnectionType::CLIENT, eClientMessageType::CHARACTER_LIST_RESPONSE); + + std::vector characters = u->GetCharacters(); + bitStream.Write(characters.size()); + bitStream.Write(0); //TODO: Pick the most recent played index. character index in front, just picking 0 + + for (uint32_t i = 0; i < characters.size(); ++i) { + bitStream.Write(characters[i]->GetObjectID()); + bitStream.Write(0); + + bitStream.Write(LUWString(characters[i]->GetName())); + bitStream.Write(LUWString(characters[i]->GetUnapprovedName())); + + bitStream.Write(characters[i]->GetNameRejected()); + bitStream.Write(false); + + bitStream.Write(LUString("", 10)); + + bitStream.Write(characters[i]->GetShirtColor()); + bitStream.Write(characters[i]->GetShirtStyle()); + bitStream.Write(characters[i]->GetPantsColor()); + bitStream.Write(characters[i]->GetHairStyle()); + bitStream.Write(characters[i]->GetHairColor()); + bitStream.Write(characters[i]->GetLeftHand()); + bitStream.Write(characters[i]->GetRightHand()); + bitStream.Write(characters[i]->GetEyebrows()); + bitStream.Write(characters[i]->GetEyes()); + bitStream.Write(characters[i]->GetMouth()); + bitStream.Write(0); + + bitStream.Write(characters[i]->GetZoneID()); + bitStream.Write(characters[i]->GetZoneInstance()); + bitStream.Write(characters[i]->GetZoneClone()); + + bitStream.Write(characters[i]->GetLastLogin()); + + const auto& equippedItems = characters[i]->GetEquippedItems(); + bitStream.Write(equippedItems.size()); + + for (uint32_t j = 0; j < equippedItems.size(); ++j) { + bitStream.Write(equippedItems[j]); + } + } + + SEND_PACKET; } void UserManager::CreateCharacter(const SystemAddress& sysAddr, Packet* packet) { User* u = GetUser(sysAddr); if (!u) return; + + LUWString LUWStringName; + uint32_t firstNameIndex; + uint32_t middleNameIndex; + uint32_t lastNameIndex; + uint32_t shirtColor; + uint32_t shirtStyle; + uint32_t pantsColor; + uint32_t hairStyle; + uint32_t hairColor; + uint32_t lh; + uint32_t rh; + uint32_t eyebrows; + uint32_t eyes; + uint32_t mouth; - std::string name = PacketUtils::ReadString(8, packet, true); + CINSTREAM_SKIP_HEADER; + inStream.Read(LUWStringName); + inStream.Read(firstNameIndex); + inStream.Read(middleNameIndex); + inStream.Read(lastNameIndex); + inStream.IgnoreBytes(9); + inStream.Read(shirtColor); + inStream.Read(shirtStyle); + inStream.Read(pantsColor); + inStream.Read(hairStyle); + inStream.Read(hairColor); + inStream.Read(lh); + inStream.Read(rh); + inStream.Read(eyebrows); + inStream.Read(eyes); + inStream.Read(mouth); - uint32_t firstNameIndex = PacketUtils::ReadU32(74, packet); - uint32_t middleNameIndex = PacketUtils::ReadU32(78, packet); - uint32_t lastNameIndex = PacketUtils::ReadU32(82, packet); + const auto name = LUWStringName.GetAsString(); std::string predefinedName = GetPredefinedName(firstNameIndex, middleNameIndex, lastNameIndex); - uint32_t shirtColor = PacketUtils::ReadU32(95, packet); - uint32_t shirtStyle = PacketUtils::ReadU32(99, packet); - uint32_t pantsColor = PacketUtils::ReadU32(103, packet); - uint32_t hairStyle = PacketUtils::ReadU32(107, packet); - uint32_t hairColor = PacketUtils::ReadU32(111, packet); - uint32_t lh = PacketUtils::ReadU32(115, packet); - uint32_t rh = PacketUtils::ReadU32(119, packet); - uint32_t eyebrows = PacketUtils::ReadU32(123, packet); - uint32_t eyes = PacketUtils::ReadU32(127, packet); - uint32_t mouth = PacketUtils::ReadU32(131, packet); - LOT shirtLOT = FindCharShirtID(shirtColor, shirtStyle); LOT pantsLOT = FindCharPantsID(pantsColor); @@ -263,7 +324,7 @@ void UserManager::CreateCharacter(const SystemAddress& sysAddr, Packet* packet) } //Now that the name is ok, we can get an objectID from Master: - ObjectIDManager::RequestPersistentID([=, this](uint32_t objectID) { + ObjectIDManager::RequestPersistentID([=, this](uint32_t objectID) mutable { if (Database::Get()->GetCharacterInfo(objectID)) { LOG("Character object id unavailable, check object_id_tracker!"); WorldPackets::SendCharacterCreationResponse(sysAddr, eCharacterCreationResponse::OBJECT_ID_UNAVAILABLE); @@ -306,6 +367,14 @@ void UserManager::CreateCharacter(const SystemAddress& sysAddr, Packet* packet) bool nameOk = IsNamePreapproved(name); if (!nameOk && u->GetMaxGMLevel() > eGameMasterLevel::FORUM_MODERATOR) nameOk = true; + // If predefined name is invalid, change it to be their object id + // that way more than one player can create characters if the predefined name files are not provided + if (predefinedName == "INVALID") { + std::stringstream nameObjID; + nameObjID << "minifig" << objectID; + predefinedName = nameObjID.str(); + } + std::string_view nameToAssign = !name.empty() && nameOk ? name : predefinedName; std::string pendingName = !name.empty() && !nameOk ? name : ""; @@ -322,7 +391,7 @@ void UserManager::CreateCharacter(const SystemAddress& sysAddr, Packet* packet) WorldPackets::SendCharacterCreationResponse(sysAddr, eCharacterCreationResponse::SUCCESS); UserManager::RequestCharacterList(sysAddr); - }); + }); } void UserManager::DeleteCharacter(const SystemAddress& sysAddr, Packet* packet) { @@ -332,7 +401,9 @@ void UserManager::DeleteCharacter(const SystemAddress& sysAddr, Packet* packet) return; } - LWOOBJID objectID = PacketUtils::ReadS64(8, packet); + CINSTREAM_SKIP_HEADER; + LWOOBJID objectID; + inStream.Read(objectID); uint32_t charID = static_cast(objectID); LOG("Received char delete req for ID: %llu (%u)", objectID, charID); @@ -366,14 +437,18 @@ void UserManager::RenameCharacter(const SystemAddress& sysAddr, Packet* packet) return; } - LWOOBJID objectID = PacketUtils::ReadS64(8, packet); + CINSTREAM_SKIP_HEADER; + LWOOBJID objectID; + inStream.Read(objectID); GeneralUtils::ClearBit(objectID, eObjectBits::CHARACTER); GeneralUtils::ClearBit(objectID, eObjectBits::PERSISTENT); uint32_t charID = static_cast(objectID); LOG("Received char rename request for ID: %llu (%u)", objectID, charID); - std::string newName = PacketUtils::ReadString(16, packet, true); + LUWString LUWStringName; + inStream.Read(LUWStringName); + const auto newName = LUWStringName.GetAsString(); Character* character = nullptr; @@ -385,7 +460,7 @@ void UserManager::RenameCharacter(const SystemAddress& sysAddr, Packet* packet) "User %i tried to rename a character that it does not own!", u->GetAccountID()); - std::find_if(u->GetCharacters().begin(), u->GetCharacters().end(), [&](Character* c) { + auto unusedItr = std::find_if(u->GetCharacters().begin(), u->GetCharacters().end(), [&](Character* c) { if (c->GetID() == charID) { character = c; return true; diff --git a/dGame/dBehaviors/Behavior.cpp b/dGame/dBehaviors/Behavior.cpp index c70b34c7..64bb03f5 100644 --- a/dGame/dBehaviors/Behavior.cpp +++ b/dGame/dBehaviors/Behavior.cpp @@ -82,7 +82,7 @@ CDBehaviorParameterTable* Behavior::BehaviorParameterTable = nullptr; Behavior* Behavior::GetBehavior(const uint32_t behaviorId) { if (BehaviorParameterTable == nullptr) { - BehaviorParameterTable = CDClientManager::Instance().GetTable(); + BehaviorParameterTable = CDClientManager::GetTable(); } const auto pair = Cache.find(behaviorId); @@ -297,7 +297,7 @@ Behavior* Behavior::CreateBehavior(const uint32_t behaviorId) { } BehaviorTemplates Behavior::GetBehaviorTemplate(const uint32_t behaviorId) { - auto behaviorTemplateTable = CDClientManager::Instance().GetTable(); + auto behaviorTemplateTable = CDClientManager::GetTable(); BehaviorTemplates templateID = BehaviorTemplates::BEHAVIOR_EMPTY; // Find behavior template by its behavior id. Default to 0. @@ -405,7 +405,7 @@ void Behavior::PlayFx(std::u16string type, const LWOOBJID target, const LWOOBJID } Behavior::Behavior(const uint32_t behaviorId) { - auto behaviorTemplateTable = CDClientManager::Instance().GetTable(); + auto behaviorTemplateTable = CDClientManager::GetTable(); CDBehaviorTemplate templateInDatabase{}; @@ -448,7 +448,7 @@ Behavior::Behavior(const uint32_t behaviorId) { float Behavior::GetFloat(const std::string& name, const float defaultValue) const { // Get the behavior parameter entry and return its value. - if (!BehaviorParameterTable) BehaviorParameterTable = CDClientManager::Instance().GetTable(); + if (!BehaviorParameterTable) BehaviorParameterTable = CDClientManager::GetTable(); return BehaviorParameterTable->GetValue(this->m_behaviorId, name, defaultValue); } @@ -476,7 +476,7 @@ Behavior* Behavior::GetAction(float value) const { std::map Behavior::GetParameterNames() const { std::map templatesInDatabase; // Find behavior template by its behavior id. - if (!BehaviorParameterTable) BehaviorParameterTable = CDClientManager::Instance().GetTable(); + if (!BehaviorParameterTable) BehaviorParameterTable = CDClientManager::GetTable(); if (BehaviorParameterTable) { templatesInDatabase = BehaviorParameterTable->GetParametersByBehaviorID(this->m_behaviorId); } diff --git a/dGame/dBehaviors/OverTimeBehavior.cpp b/dGame/dBehaviors/OverTimeBehavior.cpp index bdef2eb6..f66a5253 100644 --- a/dGame/dBehaviors/OverTimeBehavior.cpp +++ b/dGame/dBehaviors/OverTimeBehavior.cpp @@ -41,7 +41,7 @@ void OverTimeBehavior::Load() { m_Action = GetInt("action"); // Since m_Action is a skillID and not a behavior, get is correlated behaviorID. - CDSkillBehaviorTable* skillTable = CDClientManager::Instance().GetTable(); + CDSkillBehaviorTable* skillTable = CDClientManager::GetTable(); m_ActionBehaviorId = skillTable->GetSkillByID(m_Action).behaviorID; m_Delay = GetFloat("delay"); diff --git a/dGame/dBehaviors/ProjectileAttackBehavior.cpp b/dGame/dBehaviors/ProjectileAttackBehavior.cpp index 732ff186..504afc69 100644 --- a/dGame/dBehaviors/ProjectileAttackBehavior.cpp +++ b/dGame/dBehaviors/ProjectileAttackBehavior.cpp @@ -33,7 +33,7 @@ void ProjectileAttackBehavior::Handle(BehaviorContext* context, RakNet::BitStrea } if (m_useMouseposit && !branch.isSync) { - NiPoint3 targetPosition = NiPoint3::ZERO; + NiPoint3 targetPosition = NiPoint3Constant::ZERO; if (!bitStream->Read(targetPosition)) { LOG("Unable to read targetPosition from bitStream, aborting Handle! %i", bitStream->GetNumberOfUnreadBits()); return; diff --git a/dGame/dComponents/ActivityComponent.cpp b/dGame/dComponents/ActivityComponent.cpp index 7ea7500b..49ca4faf 100644 --- a/dGame/dComponents/ActivityComponent.cpp +++ b/dGame/dComponents/ActivityComponent.cpp @@ -10,7 +10,6 @@ #include "WorldPackets.h" #include "EntityManager.h" #include "ChatPackets.h" -#include "Player.h" #include "BitStreamUtils.h" #include "dServer.h" #include "GeneralUtils.h" @@ -30,40 +29,36 @@ #include "LeaderboardManager.h" ActivityComponent::ActivityComponent(Entity* parent, int32_t activityID) : Component(parent) { - if (activityID > 0) m_ActivityID = activityID; - else m_ActivityID = parent->GetVar(u"activityID"); - CDActivitiesTable* activitiesTable = CDClientManager::Instance().GetTable(); - std::vector activities = activitiesTable->Query([this](CDActivities entry) {return (entry.ActivityID == m_ActivityID); }); + /* + * This is precisely what the client does functionally + * Use the component id as the default activity id and load its data from the database + * if activityID is specified and if that column exists in the activities table, update the activity info with that data. + */ - for (CDActivities activity : activities) { - m_ActivityInfo = activity; - if (static_cast(activity.leaderboardType) == Leaderboard::Type::Racing && Game::config->GetValue("solo_racing") == "1") { - m_ActivityInfo.minTeamSize = 1; - m_ActivityInfo.minTeams = 1; - } - if (m_ActivityInfo.instanceMapID == -1) { - const auto& transferOverride = parent->GetVarAsString(u"transferZoneID"); - if (!transferOverride.empty()) { - GeneralUtils::TryParse(transferOverride, m_ActivityInfo.instanceMapID); - } - } + m_ActivityID = activityID; + LoadActivityData(activityID); + if (m_Parent->HasVar(u"activityID")) { + m_ActivityID = parent->GetVar(u"activityID"); + LoadActivityData(m_ActivityID); } auto* destroyableComponent = m_Parent->GetComponent(); if (destroyableComponent) { - // check for LMIs and set the loot LMIs - CDActivityRewardsTable* activityRewardsTable = CDClientManager::Instance().GetTable(); + // First lookup the loot matrix id for this component id. + CDActivityRewardsTable* activityRewardsTable = CDClientManager::GetTable(); std::vector activityRewards = activityRewardsTable->Query([=](CDActivityRewards entry) {return (entry.LootMatrixIndex == destroyableComponent->GetLootMatrixID()); }); uint32_t startingLMI = 0; + // If we have one, set the starting loot matrix id to that. if (activityRewards.size() > 0) { startingLMI = activityRewards[0].LootMatrixIndex; } if (startingLMI > 0) { - // now time for bodge :) + // We may have more than 1 loot matrix index to use depending ont the size of the team that is looting the activity. + // So this logic will get the rest of the loot matrix indices for this activity. std::vector objectTemplateActivities = activityRewardsTable->Query([=](CDActivityRewards entry) {return (activityRewards[0].objectTemplate == entry.objectTemplate); }); for (const auto& item : objectTemplateActivities) { @@ -74,6 +69,26 @@ ActivityComponent::ActivityComponent(Entity* parent, int32_t activityID) : Compo } } } +void ActivityComponent::LoadActivityData(const int32_t activityId) { + CDActivitiesTable* activitiesTable = CDClientManager::GetTable(); + std::vector activities = activitiesTable->Query([activityId](CDActivities entry) {return (entry.ActivityID == activityId); }); + + bool soloRacing = Game::config->GetValue("solo_racing") == "1"; + for (CDActivities activity : activities) { + m_ActivityInfo = activity; + if (static_cast(activity.leaderboardType) == Leaderboard::Type::Racing && soloRacing) { + m_ActivityInfo.minTeamSize = 1; + m_ActivityInfo.minTeams = 1; + } + if (m_ActivityInfo.instanceMapID == -1) { + const auto& transferOverride = m_Parent->GetVarAsString(u"transferZoneID"); + if (!transferOverride.empty()) { + m_ActivityInfo.instanceMapID = + GeneralUtils::TryParse(transferOverride).value_or(m_ActivityInfo.instanceMapID); + } + } + } +} void ActivityComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate) { outBitStream->Write(m_DirtyActivityInfo); @@ -92,7 +107,7 @@ void ActivityComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsIniti } void ActivityComponent::ReloadConfig() { - CDActivitiesTable* activitiesTable = CDClientManager::Instance().GetTable(); + CDActivitiesTable* activitiesTable = CDClientManager::GetTable(); std::vector activities = activitiesTable->Query([this](CDActivities entry) {return (entry.ActivityID == m_ActivityID); }); for (auto activity : activities) { auto mapID = m_ActivityInfo.instanceMapID; @@ -531,14 +546,14 @@ void ActivityInstance::RewardParticipant(Entity* participant) { } // First, get the activity data - auto* activityRewardsTable = CDClientManager::Instance().GetTable(); + auto* activityRewardsTable = CDClientManager::GetTable(); std::vector activityRewards = activityRewardsTable->Query([this](CDActivityRewards entry) { return (entry.objectTemplate == m_ActivityInfo.ActivityID); }); if (!activityRewards.empty()) { uint32_t minCoins = 0; uint32_t maxCoins = 0; - auto* currencyTableTable = CDClientManager::Instance().GetTable(); + auto* currencyTableTable = CDClientManager::GetTable(); std::vector currencyTable = currencyTableTable->Query([=](CDCurrencyTable entry) { return (entry.currencyIndex == activityRewards[0].CurrencyIndex && entry.npcminlevel == 1); }); if (!currencyTable.empty()) { diff --git a/dGame/dComponents/ActivityComponent.h b/dGame/dComponents/ActivityComponent.h index 96dbd5fb..de63b343 100644 --- a/dGame/dComponents/ActivityComponent.h +++ b/dGame/dComponents/ActivityComponent.h @@ -152,6 +152,8 @@ class ActivityComponent : public Component { public: ActivityComponent(Entity* parent, int32_t activityID); + void LoadActivityData(const int32_t activityId); + void Update(float deltaTime) override; void Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate) override; diff --git a/dGame/dComponents/BaseCombatAIComponent.cpp b/dGame/dComponents/BaseCombatAIComponent.cpp index 9cc92c26..17183409 100644 --- a/dGame/dComponents/BaseCombatAIComponent.cpp +++ b/dGame/dComponents/BaseCombatAIComponent.cpp @@ -25,6 +25,7 @@ #include "Metrics.hpp" #include "CDComponentsRegistryTable.h" #include "CDPhysicsComponentTable.h" +#include "dNavMesh.h" BaseCombatAIComponent::BaseCombatAIComponent(Entity* parent, const uint32_t id): Component(parent) { m_Target = LWOOBJID_EMPTY; @@ -106,10 +107,10 @@ BaseCombatAIComponent::BaseCombatAIComponent(Entity* parent, const uint32_t id): int32_t collisionGroup = (COLLISION_GROUP_DYNAMIC | COLLISION_GROUP_ENEMY); - CDComponentsRegistryTable* componentRegistryTable = CDClientManager::Instance().GetTable(); + CDComponentsRegistryTable* componentRegistryTable = CDClientManager::GetTable(); auto componentID = componentRegistryTable->GetByIDAndType(parent->GetLOT(), eReplicaComponentType::CONTROLLABLE_PHYSICS); - CDPhysicsComponentTable* physicsComponentTable = CDClientManager::Instance().GetTable(); + CDPhysicsComponentTable* physicsComponentTable = CDClientManager::GetTable(); if (physicsComponentTable != nullptr) { auto* info = physicsComponentTable->GetByID(componentID); @@ -128,17 +129,17 @@ BaseCombatAIComponent::BaseCombatAIComponent(Entity* parent, const uint32_t id): m_dpEntity->SetPosition(m_Parent->GetPosition()); m_dpEntityEnemy->SetPosition(m_Parent->GetPosition()); - dpWorld::Instance().AddEntity(m_dpEntity); - dpWorld::Instance().AddEntity(m_dpEntityEnemy); + dpWorld::AddEntity(m_dpEntity); + dpWorld::AddEntity(m_dpEntityEnemy); } BaseCombatAIComponent::~BaseCombatAIComponent() { if (m_dpEntity) - dpWorld::Instance().RemoveEntity(m_dpEntity); + dpWorld::RemoveEntity(m_dpEntity); if (m_dpEntityEnemy) - dpWorld::Instance().RemoveEntity(m_dpEntityEnemy); + dpWorld::RemoveEntity(m_dpEntityEnemy); } void BaseCombatAIComponent::Update(const float deltaTime) { @@ -184,7 +185,7 @@ void BaseCombatAIComponent::Update(const float deltaTime) { bool stunnedThisFrame = m_Stunned; CalculateCombat(deltaTime); // Putting this here for now - if (m_StartPosition == NiPoint3::ZERO) { + if (m_StartPosition == NiPoint3Constant::ZERO) { m_StartPosition = m_Parent->GetPosition(); } @@ -653,8 +654,8 @@ void BaseCombatAIComponent::Wander() { auto destination = m_StartPosition + delta; - if (dpWorld::Instance().IsLoaded()) { - destination.y = dpWorld::Instance().GetNavMesh()->GetHeightAtPoint(destination); + if (dpWorld::IsLoaded()) { + destination.y = dpWorld::GetNavMesh()->GetHeightAtPoint(destination); } if (Vector3::DistanceSquared(destination, m_MovementAI->GetParent()->GetPosition()) < 2 * 2) { diff --git a/dGame/dComponents/BaseCombatAIComponent.h b/dGame/dComponents/BaseCombatAIComponent.h index 8ae04611..f00910e7 100644 --- a/dGame/dComponents/BaseCombatAIComponent.h +++ b/dGame/dComponents/BaseCombatAIComponent.h @@ -45,9 +45,9 @@ struct AiSkillEntry /** * Handles the AI of entities, making them wander, tether and attack their enemies */ -class BaseCombatAIComponent : public Component { +class BaseCombatAIComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::BASE_COMBAT_AI; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::BASE_COMBAT_AI; BaseCombatAIComponent(Entity* parentEntity, uint32_t id); ~BaseCombatAIComponent() override; diff --git a/dGame/dComponents/BouncerComponent.h b/dGame/dComponents/BouncerComponent.h index cb3d8df3..b41881c6 100644 --- a/dGame/dComponents/BouncerComponent.h +++ b/dGame/dComponents/BouncerComponent.h @@ -10,9 +10,9 @@ /** * Attached to bouncer entities, allowing other entities to bounce off of it */ -class BouncerComponent : public Component { +class BouncerComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::BOUNCER; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::BOUNCER; BouncerComponent(Entity* parentEntity); ~BouncerComponent() override; diff --git a/dGame/dComponents/BuffComponent.cpp b/dGame/dComponents/BuffComponent.cpp index 10ac4ebb..cdf1d5bc 100644 --- a/dGame/dComponents/BuffComponent.cpp +++ b/dGame/dComponents/BuffComponent.cpp @@ -164,7 +164,7 @@ void BuffComponent::ApplyBuff(const int32_t id, const float duration, const LWOO const auto& parameters = GetBuffParameters(id); for (const auto& parameter : parameters) { if (parameter.name == "overtime") { - auto* behaviorTemplateTable = CDClientManager::Instance().GetTable(); + auto* behaviorTemplateTable = CDClientManager::GetTable(); behaviorID = behaviorTemplateTable->GetSkillByID(parameter.values[0]).behaviorID; stacks = static_cast(parameter.values[1]); diff --git a/dGame/dComponents/BuffComponent.h b/dGame/dComponents/BuffComponent.h index 7f7b44d8..18aa7a42 100644 --- a/dGame/dComponents/BuffComponent.h +++ b/dGame/dComponents/BuffComponent.h @@ -47,9 +47,9 @@ struct Buff { /** * Allows for the application of buffs to the parent entity, altering health, armor and imagination. */ -class BuffComponent : public Component { +class BuffComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::BUFF; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::BUFF; explicit BuffComponent(Entity* parent); diff --git a/dGame/dComponents/BuildBorderComponent.cpp b/dGame/dComponents/BuildBorderComponent.cpp index 9e392e69..066a8c74 100644 --- a/dGame/dComponents/BuildBorderComponent.cpp +++ b/dGame/dComponents/BuildBorderComponent.cpp @@ -56,7 +56,7 @@ void BuildBorderComponent::OnUse(Entity* originator) { 4, 0, -1, - NiPoint3::ZERO, + NiPoint3Constant::ZERO, 0 ); } else { diff --git a/dGame/dComponents/BuildBorderComponent.h b/dGame/dComponents/BuildBorderComponent.h index 985c0388..a59ac363 100644 --- a/dGame/dComponents/BuildBorderComponent.h +++ b/dGame/dComponents/BuildBorderComponent.h @@ -14,9 +14,9 @@ /** * Component for the build border, allowing the user to start building when interacting with it */ -class BuildBorderComponent : public Component { +class BuildBorderComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::BUILD_BORDER; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::BUILD_BORDER; BuildBorderComponent(Entity* parent); ~BuildBorderComponent() override; diff --git a/dGame/dComponents/CMakeLists.txt b/dGame/dComponents/CMakeLists.txt index b14d0eb8..ac509e11 100644 --- a/dGame/dComponents/CMakeLists.txt +++ b/dGame/dComponents/CMakeLists.txt @@ -10,6 +10,7 @@ set(DGAME_DCOMPONENTS_SOURCES "ControllablePhysicsComponent.cpp" "DestroyableComponent.cpp" "DonationVendorComponent.cpp" + "GhostComponent.cpp" "InventoryComponent.cpp" "ItemComponent.cpp" "LevelProgressionComponent.cpp" @@ -26,7 +27,6 @@ set(DGAME_DCOMPONENTS_SOURCES "PlayerForcedMovementComponent.cpp" "PossessableComponent.cpp" "PossessorComponent.cpp" - "PropertyComponent.cpp" "PropertyEntranceComponent.cpp" "PropertyManagementComponent.cpp" "PropertyVendorComponent.cpp" diff --git a/dGame/dComponents/CharacterComponent.cpp b/dGame/dComponents/CharacterComponent.cpp index 702ab70f..e4e2ba4b 100644 --- a/dGame/dComponents/CharacterComponent.cpp +++ b/dGame/dComponents/CharacterComponent.cpp @@ -20,9 +20,11 @@ #include "Database.h" #include "CDRewardCodesTable.h" #include "Mail.h" +#include "ZoneInstanceManager.h" +#include "WorldPackets.h" #include -CharacterComponent::CharacterComponent(Entity* parent, Character* character) : Component(parent) { +CharacterComponent::CharacterComponent(Entity* parent, Character* character, const SystemAddress& systemAddress) : Component(parent) { m_Character = character; m_IsRacing = false; @@ -44,6 +46,7 @@ CharacterComponent::CharacterComponent(Entity* parent, Character* character) : C m_CurrentActivity = eGameActivity::NONE; m_CountryCode = 0; m_LastUpdateTimestamp = std::time(nullptr); + m_SystemAddress = systemAddress; } bool CharacterComponent::LandingAnimDisabled(int zoneID) { @@ -760,15 +763,15 @@ void CharacterComponent::UpdateClientMinimap(bool showFaction, std::string ventu } void CharacterComponent::AwardClaimCodes() { - if (!m_Parent) return; - auto* user = m_Parent->GetParentUser(); + if (!m_Parent || !m_Parent->GetCharacter()) return; + auto* user = m_Parent->GetCharacter()->GetParentUser(); if (!user) return; - + auto rewardCodes = Database::Get()->GetRewardCodesByAccountID(user->GetAccountID()); if (rewardCodes.empty()) return; - auto* cdrewardCodes = CDClientManager::Instance().GetTable(); - for (auto const rewardCode: rewardCodes){ + auto* cdrewardCodes = CDClientManager::GetTable(); + for (auto const rewardCode : rewardCodes) { LOG_DEBUG("Processing RewardCode %i", rewardCode); const uint32_t rewardCodeIndex = rewardCode >> 6; const uint32_t bitIndex = rewardCode % 64; @@ -786,3 +789,49 @@ void CharacterComponent::AwardClaimCodes() { Mail::SendMail(LWOOBJID_EMPTY, "%[MAIL_SYSTEM_NOTIFICATION]", m_Parent, subject.str(), body.str(), attachmentLOT, 1); } } + +void CharacterComponent::SendToZone(LWOMAPID zoneId, LWOCLONEID cloneId) const { + const auto objid = m_Parent->GetObjectID(); + + ZoneInstanceManager::Instance()->RequestZoneTransfer(Game::server, zoneId, cloneId, false, [objid](bool mythranShift, uint32_t zoneID, uint32_t zoneInstance, uint32_t zoneClone, std::string serverIP, uint16_t serverPort) { + auto* entity = Game::entityManager->GetEntity(objid); + + if (!entity) return; + + const auto sysAddr = entity->GetSystemAddress(); + + auto* character = entity->GetCharacter(); + auto* characterComponent = entity->GetComponent(); + + if (character && characterComponent) { + character->SetZoneID(zoneID); + character->SetZoneInstance(zoneInstance); + character->SetZoneClone(zoneClone); + + characterComponent->SetLastRocketConfig(u""); + + character->SaveXMLToDatabase(); + } + + WorldPackets::SendTransferToWorld(sysAddr, serverIP, serverPort, mythranShift); + + Game::entityManager->DestructEntity(entity); + }); +} + +const SystemAddress& CharacterComponent::GetSystemAddress() const { + return m_SystemAddress; +} + +void CharacterComponent::SetRespawnPos(const NiPoint3& position) { + if (!m_Character) return; + + m_respawnPos = position; + + m_Character->SetRespawnPoint(Game::zoneManager->GetZone()->GetWorldID(), position); + +} + +void CharacterComponent::SetRespawnRot(const NiQuaternion& rotation) { + m_respawnRot = rotation; +} diff --git a/dGame/dComponents/CharacterComponent.h b/dGame/dComponents/CharacterComponent.h index 158e011e..01c26f9a 100644 --- a/dGame/dComponents/CharacterComponent.h +++ b/dGame/dComponents/CharacterComponent.h @@ -11,6 +11,7 @@ #include "tinyxml2.h" #include "eReplicaComponentType.h" #include +#include "Loot.h" enum class eGameActivity : uint32_t; @@ -61,11 +62,11 @@ enum StatisticID { /** * Represents a character, including their rockets and stats */ -class CharacterComponent : public Component { +class CharacterComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::CHARACTER; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::CHARACTER; - CharacterComponent(Entity* parent, Character* character); + CharacterComponent(Entity* parent, Character* character, const SystemAddress& systemAddress); ~CharacterComponent() override; void LoadFromXml(tinyxml2::XMLDocument* doc) override; @@ -281,6 +282,30 @@ public: LWOOBJID GetCurrentInteracting() {return m_CurrentInteracting;}; + /** + * Sends a player to another zone with an optional clone ID + * + * @param zoneId zoneID for the new instance. + * @param cloneId cloneID for the new instance. + */ + void SendToZone(LWOMAPID zoneId, LWOCLONEID cloneId = 0) const; + + const SystemAddress& GetSystemAddress() const; + + const NiPoint3& GetRespawnPosition() const { return m_respawnPos; }; + + void SetRespawnPos(const NiPoint3& position); + + const NiQuaternion& GetRespawnRotation() const { return m_respawnRot; }; + + void SetRespawnRot(const NiQuaternion& rotation); + + std::map& GetDroppedLoot() { return m_DroppedLoot; }; + + uint64_t GetDroppedCoins() const { return m_DroppedCoins; }; + + void SetDroppedCoins(const uint64_t value) { m_DroppedCoins = value; }; + /** * Character info regarding this character, including clothing styles, etc. */ @@ -571,6 +596,16 @@ private: std::array m_ClaimCodes{}; void AwardClaimCodes(); + + SystemAddress m_SystemAddress; + + NiPoint3 m_respawnPos; + + NiQuaternion m_respawnRot; + + std::map m_DroppedLoot; + + uint64_t m_DroppedCoins = 0; }; #endif // CHARACTERCOMPONENT_H diff --git a/dGame/dComponents/CollectibleComponent.h b/dGame/dComponents/CollectibleComponent.h index 3ff71c6f..10a23293 100644 --- a/dGame/dComponents/CollectibleComponent.h +++ b/dGame/dComponents/CollectibleComponent.h @@ -4,9 +4,9 @@ #include "Component.h" #include "eReplicaComponentType.h" -class CollectibleComponent : public Component { +class CollectibleComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::COLLECTIBLE; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::COLLECTIBLE; CollectibleComponent(Entity* parentEntity, int32_t collectibleId) : Component(parentEntity), m_CollectibleId(collectibleId) {} int16_t GetCollectibleId() const { return m_CollectibleId; } diff --git a/dGame/dComponents/Component.h b/dGame/dComponents/Component.h index d1ad0860..70f30f76 100644 --- a/dGame/dComponents/Component.h +++ b/dGame/dComponents/Component.h @@ -7,8 +7,7 @@ class Entity; /** * Component base class, provides methods for game loop updates, usage events and loading and saving to XML. */ -class Component -{ +class Component { public: Component(Entity* parent); virtual ~Component(); diff --git a/dGame/dComponents/ControllablePhysicsComponent.cpp b/dGame/dComponents/ControllablePhysicsComponent.cpp index dd981f66..be5227a0 100644 --- a/dGame/dComponents/ControllablePhysicsComponent.cpp +++ b/dGame/dComponents/ControllablePhysicsComponent.cpp @@ -57,13 +57,13 @@ ControllablePhysicsComponent::ControllablePhysicsComponent(Entity* entity) : Phy float radius = 1.5f; m_dpEntity = new dpEntity(m_Parent->GetObjectID(), radius, false); m_dpEntity->SetCollisionGroup(COLLISION_GROUP_DYNAMIC | COLLISION_GROUP_FRIENDLY); - dpWorld::Instance().AddEntity(m_dpEntity); + dpWorld::AddEntity(m_dpEntity); } } ControllablePhysicsComponent::~ControllablePhysicsComponent() { if (m_dpEntity) { - dpWorld::Instance().RemoveEntity(m_dpEntity); + dpWorld::RemoveEntity(m_dpEntity); } } diff --git a/dGame/dComponents/ControllablePhysicsComponent.h b/dGame/dComponents/ControllablePhysicsComponent.h index e5c3f890..e850cfeb 100644 --- a/dGame/dComponents/ControllablePhysicsComponent.h +++ b/dGame/dComponents/ControllablePhysicsComponent.h @@ -21,7 +21,7 @@ enum class eStateChangeType : uint32_t; */ class ControllablePhysicsComponent : public PhysicsComponent { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::CONTROLLABLE_PHYSICS; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::CONTROLLABLE_PHYSICS; ControllablePhysicsComponent(Entity* entity); ~ControllablePhysicsComponent() override; diff --git a/dGame/dComponents/DestroyableComponent.cpp b/dGame/dComponents/DestroyableComponent.cpp index 64dca4f1..68271f26 100644 --- a/dGame/dComponents/DestroyableComponent.cpp +++ b/dGame/dComponents/DestroyableComponent.cpp @@ -81,7 +81,7 @@ DestroyableComponent::~DestroyableComponent() { } void DestroyableComponent::Reinitialize(LOT templateID) { - CDComponentsRegistryTable* compRegistryTable = CDClientManager::Instance().GetTable(); + CDComponentsRegistryTable* compRegistryTable = CDClientManager::GetTable(); int32_t buffComponentID = compRegistryTable->GetByIDAndType(templateID, eReplicaComponentType::BUFF); int32_t collectibleComponentID = compRegistryTable->GetByIDAndType(templateID, eReplicaComponentType::COLLECTIBLE); @@ -92,7 +92,7 @@ void DestroyableComponent::Reinitialize(LOT templateID) { if (quickBuildComponentID > 0) componentID = quickBuildComponentID; if (buffComponentID > 0) componentID = buffComponentID; - CDDestructibleComponentTable* destCompTable = CDClientManager::Instance().GetTable(); + CDDestructibleComponentTable* destCompTable = CDClientManager::GetTable(); std::vector destCompData = destCompTable->Query([=](CDDestructibleComponent entry) { return (entry.id == componentID); }); if (componentID > 0) { @@ -251,13 +251,14 @@ void DestroyableComponent::SetMaxHealth(float value, bool playAnim) { if (playAnim) { // Now update the player bar - if (!m_Parent->GetParentUser()) return; + auto* characterComponent = m_Parent->GetComponent(); + if (!characterComponent) return; AMFArrayValue args; args.Insert("amount", std::to_string(difference)); args.Insert("type", "health"); - GameMessages::SendUIMessageServerToSingleClient(m_Parent, m_Parent->GetParentUser()->GetSystemAddress(), "MaxPlayerBarUpdate", args); + GameMessages::SendUIMessageServerToSingleClient(m_Parent, characterComponent->GetSystemAddress(), "MaxPlayerBarUpdate", args); } Game::entityManager->SerializeEntity(m_Parent); @@ -292,13 +293,14 @@ void DestroyableComponent::SetMaxArmor(float value, bool playAnim) { if (playAnim) { // Now update the player bar - if (!m_Parent->GetParentUser()) return; + auto* characterComponent = m_Parent->GetComponent(); + if (!characterComponent) return; AMFArrayValue args; args.Insert("amount", std::to_string(value)); args.Insert("type", "armor"); - GameMessages::SendUIMessageServerToSingleClient(m_Parent, m_Parent->GetParentUser()->GetSystemAddress(), "MaxPlayerBarUpdate", args); + GameMessages::SendUIMessageServerToSingleClient(m_Parent, characterComponent->GetSystemAddress(), "MaxPlayerBarUpdate", args); } Game::entityManager->SerializeEntity(m_Parent); @@ -332,13 +334,14 @@ void DestroyableComponent::SetMaxImagination(float value, bool playAnim) { if (playAnim) { // Now update the player bar - if (!m_Parent->GetParentUser()) return; + auto* characterComponent = m_Parent->GetComponent(); + if (!characterComponent) return; AMFArrayValue args; args.Insert("amount", std::to_string(difference)); args.Insert("type", "imagination"); - GameMessages::SendUIMessageServerToSingleClient(m_Parent, m_Parent->GetParentUser()->GetSystemAddress(), "MaxPlayerBarUpdate", args); + GameMessages::SendUIMessageServerToSingleClient(m_Parent, characterComponent->GetSystemAddress(), "MaxPlayerBarUpdate", args); } Game::entityManager->SerializeEntity(m_Parent); } diff --git a/dGame/dComponents/DestroyableComponent.h b/dGame/dComponents/DestroyableComponent.h index b81ab9f3..1f45b43e 100644 --- a/dGame/dComponents/DestroyableComponent.h +++ b/dGame/dComponents/DestroyableComponent.h @@ -17,9 +17,9 @@ enum class eStateChangeType : uint32_t; * Represents the stats of an entity, for example its health, imagination and armor. Also handles factions, which * indicate which enemies this entity has. */ -class DestroyableComponent : public Component { +class DestroyableComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::DESTROYABLE; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::DESTROYABLE; DestroyableComponent(Entity* parentEntity); ~DestroyableComponent() override; diff --git a/dGame/dComponents/DonationVendorComponent.h b/dGame/dComponents/DonationVendorComponent.h index d1743118..7eb60849 100644 --- a/dGame/dComponents/DonationVendorComponent.h +++ b/dGame/dComponents/DonationVendorComponent.h @@ -8,7 +8,7 @@ class Entity; class DonationVendorComponent final : public VendorComponent { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::DONATION_VENDOR; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::DONATION_VENDOR; DonationVendorComponent(Entity* parent); void Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate) override; uint32_t GetActivityID() {return m_ActivityId;}; diff --git a/dGame/dComponents/GhostComponent.cpp b/dGame/dComponents/GhostComponent.cpp new file mode 100644 index 00000000..3aea329a --- /dev/null +++ b/dGame/dComponents/GhostComponent.cpp @@ -0,0 +1,57 @@ +#include "GhostComponent.h" + +GhostComponent::GhostComponent(Entity* parent) : Component(parent) { + m_GhostReferencePoint = NiPoint3Constant::ZERO; + m_GhostOverridePoint = NiPoint3Constant::ZERO; + m_GhostOverride = false; +} + +GhostComponent::~GhostComponent() { + for (auto& observedEntity : m_ObservedEntities) { + if (observedEntity == LWOOBJID_EMPTY) continue; + + auto* entity = Game::entityManager->GetGhostCandidate(observedEntity); + if (!entity) continue; + + entity->SetObservers(entity->GetObservers() - 1); + } +} + +void GhostComponent::SetGhostReferencePoint(const NiPoint3& value) { + m_GhostReferencePoint = value; +} + +void GhostComponent::SetGhostOverridePoint(const NiPoint3& value) { + m_GhostOverridePoint = value; +} + +void GhostComponent::AddLimboConstruction(LWOOBJID objectId) { + m_LimboConstructions.insert(objectId); +} + +void GhostComponent::RemoveLimboConstruction(LWOOBJID objectId) { + m_LimboConstructions.erase(objectId); +} + +void GhostComponent::ConstructLimboEntities() { + for (const auto& objectId : m_LimboConstructions) { + auto* entity = Game::entityManager->GetEntity(objectId); + if (!entity) continue; + + Game::entityManager->ConstructEntity(entity, m_Parent->GetSystemAddress()); + } + + m_LimboConstructions.clear(); +} + +void GhostComponent::ObserveEntity(LWOOBJID id) { + m_ObservedEntities.insert(id); +} + +bool GhostComponent::IsObserved(LWOOBJID id) { + return m_ObservedEntities.contains(id); +} + +void GhostComponent::GhostEntity(LWOOBJID id) { + m_ObservedEntities.erase(id); +} diff --git a/dGame/dComponents/GhostComponent.h b/dGame/dComponents/GhostComponent.h new file mode 100644 index 00000000..de0fb886 --- /dev/null +++ b/dGame/dComponents/GhostComponent.h @@ -0,0 +1,54 @@ +#ifndef __GHOSTCOMPONENT__H__ +#define __GHOSTCOMPONENT__H__ + +#include "Component.h" +#include "eReplicaComponentType.h" +#include + +class NiPoint3; + +class GhostComponent final : public Component { +public: + static inline const eReplicaComponentType ComponentType = eReplicaComponentType::GHOST; + GhostComponent(Entity* parent); + ~GhostComponent() override; + + void SetGhostOverride(bool value) { m_GhostOverride = value; }; + + const NiPoint3& GetGhostReferencePoint() const { return m_GhostOverride ? m_GhostOverridePoint : m_GhostReferencePoint; }; + + const NiPoint3& GetOriginGhostReferencePoint() const { return m_GhostReferencePoint; }; + + const NiPoint3& GetGhostOverridePoint() const { return m_GhostOverridePoint; }; + + bool GetGhostOverride() const { return m_GhostOverride; }; + + void SetGhostReferencePoint(const NiPoint3& value); + + void SetGhostOverridePoint(const NiPoint3& value); + + void AddLimboConstruction(const LWOOBJID objectId); + + void RemoveLimboConstruction(const LWOOBJID objectId); + + void ConstructLimboEntities(); + + void ObserveEntity(const LWOOBJID id); + + bool IsObserved(const LWOOBJID id); + + void GhostEntity(const LWOOBJID id); + +private: + NiPoint3 m_GhostReferencePoint; + + NiPoint3 m_GhostOverridePoint; + + std::unordered_set m_ObservedEntities; + + std::unordered_set m_LimboConstructions; + + bool m_GhostOverride; +}; + +#endif //!__GHOSTCOMPONENT__H__ diff --git a/dGame/dComponents/HavokVehiclePhysicsComponent.cpp b/dGame/dComponents/HavokVehiclePhysicsComponent.cpp index 62e49a56..8c8e14fe 100644 --- a/dGame/dComponents/HavokVehiclePhysicsComponent.cpp +++ b/dGame/dComponents/HavokVehiclePhysicsComponent.cpp @@ -2,8 +2,8 @@ #include "EntityManager.h" HavokVehiclePhysicsComponent::HavokVehiclePhysicsComponent(Entity* parent) : PhysicsComponent(parent) { - m_Velocity = NiPoint3::ZERO; - m_AngularVelocity = NiPoint3::ZERO; + m_Velocity = NiPoint3Constant::ZERO; + m_AngularVelocity = NiPoint3Constant::ZERO; m_IsOnGround = true; m_IsOnRail = false; m_DirtyPosition = true; diff --git a/dGame/dComponents/HavokVehiclePhysicsComponent.h b/dGame/dComponents/HavokVehiclePhysicsComponent.h index 2d04e0ac..85a0e279 100644 --- a/dGame/dComponents/HavokVehiclePhysicsComponent.h +++ b/dGame/dComponents/HavokVehiclePhysicsComponent.h @@ -4,38 +4,14 @@ #include "Entity.h" #include "PhysicsComponent.h" #include "eReplicaComponentType.h" - -struct RemoteInputInfo { - RemoteInputInfo() { - m_RemoteInputX = 0; - m_RemoteInputY = 0; - m_IsPowersliding = false; - m_IsModified = false; - } - - void operator=(const RemoteInputInfo& other) { - m_RemoteInputX = other.m_RemoteInputX; - m_RemoteInputY = other.m_RemoteInputY; - m_IsPowersliding = other.m_IsPowersliding; - m_IsModified = other.m_IsModified; - } - - bool operator==(const RemoteInputInfo& other) { - return m_RemoteInputX == other.m_RemoteInputX && m_RemoteInputY == other.m_RemoteInputY && m_IsPowersliding == other.m_IsPowersliding && m_IsModified == other.m_IsModified; - } - - float m_RemoteInputX; - float m_RemoteInputY; - bool m_IsPowersliding; - bool m_IsModified; -}; +#include "PositionUpdate.h" /** * Physics component for vehicles. */ class HavokVehiclePhysicsComponent : public PhysicsComponent { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::HAVOK_VEHICLE_PHYSICS; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::HAVOK_VEHICLE_PHYSICS; HavokVehiclePhysicsComponent(Entity* parentEntity); diff --git a/dGame/dComponents/InventoryComponent.cpp b/dGame/dComponents/InventoryComponent.cpp index 92b5171a..591e0505 100644 --- a/dGame/dComponents/InventoryComponent.cpp +++ b/dGame/dComponents/InventoryComponent.cpp @@ -14,7 +14,6 @@ #include "Character.h" #include "EntityManager.h" #include "ItemSet.h" -#include "Player.h" #include "PetComponent.h" #include "PossessorComponent.h" #include "PossessableComponent.h" @@ -31,6 +30,7 @@ #include "eMissionTaskType.h" #include "eStateChangeType.h" #include "eUseItemResponse.h" +#include "Mail.h" #include "CDComponentsRegistryTable.h" #include "CDInventoryComponentTable.h" @@ -55,10 +55,10 @@ InventoryComponent::InventoryComponent(Entity* parent, tinyxml2::XMLDocument* do return; } - auto* compRegistryTable = CDClientManager::Instance().GetTable(); + auto* compRegistryTable = CDClientManager::GetTable(); const auto componentId = compRegistryTable->GetByIDAndType(lot, eReplicaComponentType::INVENTORY); - auto* inventoryComponentTable = CDClientManager::Instance().GetTable(); + auto* inventoryComponentTable = CDClientManager::GetTable(); auto items = inventoryComponentTable->Query([=](const CDInventoryComponent entry) { return entry.id == componentId; }); auto slot = 0u; @@ -264,17 +264,11 @@ void InventoryComponent::AddItem( } if (slot == -1) { - auto* player = dynamic_cast(GetParent()); - - if (player == nullptr) { - return; - } - outOfSpace += size; switch (sourceType) { case 0: - player->SendMail(LWOOBJID_EMPTY, "Darkflame Universe", "Lost Reward", "You received an item and didn't have room for it.", lot, size); + Mail::SendMail(LWOOBJID_EMPTY, "Darkflame Universe", m_Parent, "Lost Reward", "You received an item and didn't have room for it.", lot, size); break; case 1: @@ -915,11 +909,11 @@ void InventoryComponent::UnEquipItem(Item* item) { void InventoryComponent::EquipScripts(Item* equippedItem) { - CDComponentsRegistryTable* compRegistryTable = CDClientManager::Instance().GetTable(); + CDComponentsRegistryTable* compRegistryTable = CDClientManager::GetTable(); if (!compRegistryTable) return; int32_t scriptComponentID = compRegistryTable->GetByIDAndType(equippedItem->GetLot(), eReplicaComponentType::SCRIPT, -1); if (scriptComponentID > -1) { - CDScriptComponentTable* scriptCompTable = CDClientManager::Instance().GetTable(); + CDScriptComponentTable* scriptCompTable = CDClientManager::GetTable(); CDScriptComponent scriptCompData = scriptCompTable->GetByID(scriptComponentID); auto* itemScript = CppScripts::GetScript(m_Parent, scriptCompData.script_name); if (!itemScript) { @@ -930,11 +924,11 @@ void InventoryComponent::EquipScripts(Item* equippedItem) { } void InventoryComponent::UnequipScripts(Item* unequippedItem) { - CDComponentsRegistryTable* compRegistryTable = CDClientManager::Instance().GetTable(); + CDComponentsRegistryTable* compRegistryTable = CDClientManager::GetTable(); if (!compRegistryTable) return; int32_t scriptComponentID = compRegistryTable->GetByIDAndType(unequippedItem->GetLot(), eReplicaComponentType::SCRIPT, -1); if (scriptComponentID > -1) { - CDScriptComponentTable* scriptCompTable = CDClientManager::Instance().GetTable(); + CDScriptComponentTable* scriptCompTable = CDClientManager::GetTable(); CDScriptComponent scriptCompData = scriptCompTable->GetByID(scriptComponentID); auto* itemScript = CppScripts::GetScript(m_Parent, scriptCompData.script_name); if (!itemScript) { @@ -1228,7 +1222,7 @@ void InventoryComponent::SpawnPet(Item* item) { EntityInfo info{}; info.lot = item->GetLot(); info.pos = m_Parent->GetPosition(); - info.rot = NiQuaternion::IDENTITY; + info.rot = NiQuaternionConstant::IDENTITY; info.spawnerID = m_Parent->GetObjectID(); auto* pet = Game::entityManager->CreateEntity(info); @@ -1286,7 +1280,7 @@ bool InventoryComponent::IsTransferInventory(eInventoryType type) { } uint32_t InventoryComponent::FindSkill(const LOT lot) { - auto* table = CDClientManager::Instance().GetTable(); + auto* table = CDClientManager::GetTable(); const auto results = table->Query([=](const CDObjectSkills& entry) { return entry.objectTemplate == static_cast(lot); @@ -1304,8 +1298,8 @@ uint32_t InventoryComponent::FindSkill(const LOT lot) { std::vector InventoryComponent::FindBuffs(Item* item, bool castOnEquip) const { std::vector buffs; if (item == nullptr) return buffs; - auto* table = CDClientManager::Instance().GetTable(); - auto* behaviors = CDClientManager::Instance().GetTable(); + auto* table = CDClientManager::GetTable(); + auto* behaviors = CDClientManager::GetTable(); const auto results = table->Query([=](const CDObjectSkills& entry) { return entry.objectTemplate == static_cast(item->GetLot()); diff --git a/dGame/dComponents/InventoryComponent.h b/dGame/dComponents/InventoryComponent.h index f4d38d43..e47e6a59 100644 --- a/dGame/dComponents/InventoryComponent.h +++ b/dGame/dComponents/InventoryComponent.h @@ -35,10 +35,9 @@ enum class eItemType : int32_t; * of different types, each type representing a different group of items, see `eInventoryType` for a list of * inventories. */ -class InventoryComponent : public Component -{ +class InventoryComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::INVENTORY; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::INVENTORY; explicit InventoryComponent(Entity* parent, tinyxml2::XMLDocument* document = nullptr); void Update(float deltaTime) override; diff --git a/dGame/dComponents/ItemComponent.h b/dGame/dComponents/ItemComponent.h index 3af6a91e..875ef0a5 100644 --- a/dGame/dComponents/ItemComponent.h +++ b/dGame/dComponents/ItemComponent.h @@ -4,9 +4,9 @@ #include "Component.h" #include "eReplicaComponentType.h" -class ItemComponent : public Component { +class ItemComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::ITEM; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::ITEM; ItemComponent(Entity* entity) : Component(entity) {} diff --git a/dGame/dComponents/LUPExhibitComponent.h b/dGame/dComponents/LUPExhibitComponent.h index 47b13a17..e6653868 100644 --- a/dGame/dComponents/LUPExhibitComponent.h +++ b/dGame/dComponents/LUPExhibitComponent.h @@ -11,10 +11,10 @@ * Component that handles the LOT that is shown in the LUP exhibit in the LUP world. Works by setting a timer and * switching the LOTs around that we'd like to display. */ -class LUPExhibitComponent : public Component +class LUPExhibitComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::LUP_EXHIBIT; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::LUP_EXHIBIT; LUPExhibitComponent(Entity* parent) : Component(parent) {}; void Update(float deltaTime) override; diff --git a/dGame/dComponents/LevelProgressionComponent.cpp b/dGame/dComponents/LevelProgressionComponent.cpp index 3a18b19c..d271f97b 100644 --- a/dGame/dComponents/LevelProgressionComponent.cpp +++ b/dGame/dComponents/LevelProgressionComponent.cpp @@ -44,7 +44,7 @@ void LevelProgressionComponent::Serialize(RakNet::BitStream* outBitStream, bool } void LevelProgressionComponent::HandleLevelUp() { - auto* rewardsTable = CDClientManager::Instance().GetTable(); + auto* rewardsTable = CDClientManager::GetTable(); const auto& rewards = rewardsTable->GetByLevelID(m_Level); bool rewardingItem = rewards.size() > 0; diff --git a/dGame/dComponents/LevelProgressionComponent.h b/dGame/dComponents/LevelProgressionComponent.h index 09ccec34..6083738c 100644 --- a/dGame/dComponents/LevelProgressionComponent.h +++ b/dGame/dComponents/LevelProgressionComponent.h @@ -11,9 +11,9 @@ * */ -class LevelProgressionComponent : public Component { +class LevelProgressionComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::LEVEL_PROGRESSION; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::LEVEL_PROGRESSION; /** * Constructor for this component diff --git a/dGame/dComponents/MiniGameControlComponent.h b/dGame/dComponents/MiniGameControlComponent.h index 06a9c24e..e2581b2d 100644 --- a/dGame/dComponents/MiniGameControlComponent.h +++ b/dGame/dComponents/MiniGameControlComponent.h @@ -6,7 +6,7 @@ class MiniGameControlComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::MINI_GAME_CONTROL; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::MINI_GAME_CONTROL; MiniGameControlComponent(Entity* parent) : Component(parent) {} void Serialize(RakNet::BitStream* outBitStream, bool isConstruction); diff --git a/dGame/dComponents/MissionComponent.cpp b/dGame/dComponents/MissionComponent.cpp index 7219589a..151fcf2f 100644 --- a/dGame/dComponents/MissionComponent.cpp +++ b/dGame/dComponents/MissionComponent.cpp @@ -266,7 +266,7 @@ void MissionComponent::ForceProgressValue(uint32_t missionId, uint32_t taskType, } bool MissionComponent::GetMissionInfo(uint32_t missionId, CDMissions& result) { - auto* missionsTable = CDClientManager::Instance().GetTable(); + auto* missionsTable = CDClientManager::GetTable(); const auto missions = missionsTable->Query([=](const CDMissions& entry) { return entry.id == static_cast(missionId); @@ -320,8 +320,8 @@ const std::vector MissionComponent::LookForAchievements(eMissionTaskTy return acceptedAchievements; #else - auto* missionTasksTable = CDClientManager::Instance().GetTable(); - auto* missionsTable = CDClientManager::Instance().GetTable(); + auto* missionTasksTable = CDClientManager::GetTable(); + auto* missionsTable = CDClientManager::GetTable(); auto tasks = missionTasksTable->Query([=](const CDMissionTasks& entry) { return entry.taskType == static_cast(type); @@ -407,8 +407,8 @@ const std::vector& MissionComponent::QueryAchievements(eMissionTaskTyp } // Find relevent tables - auto* missionTasksTable = CDClientManager::Instance().GetTable(); - auto* missionsTable = CDClientManager::Instance().GetTable(); + auto* missionTasksTable = CDClientManager::GetTable(); + auto* missionsTable = CDClientManager::GetTable(); std::vector result; diff --git a/dGame/dComponents/MissionComponent.h b/dGame/dComponents/MissionComponent.h index d53db64e..42c4df08 100644 --- a/dGame/dComponents/MissionComponent.h +++ b/dGame/dComponents/MissionComponent.h @@ -24,10 +24,9 @@ class AchievementCacheKey; * The mission inventory of an entity. Tracks mission state for each mission that can be accepted and allows for * progression of each of the mission task types (see eMissionTaskType). */ -class MissionComponent : public Component -{ +class MissionComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::MISSION; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::MISSION; explicit MissionComponent(Entity* parent); ~MissionComponent() override; diff --git a/dGame/dComponents/MissionOfferComponent.cpp b/dGame/dComponents/MissionOfferComponent.cpp index 25d4a739..7f26ed72 100644 --- a/dGame/dComponents/MissionOfferComponent.cpp +++ b/dGame/dComponents/MissionOfferComponent.cpp @@ -40,7 +40,7 @@ bool OfferedMission::GetAcceptsMission() const { //------------------------ MissionOfferComponent below ------------------------ MissionOfferComponent::MissionOfferComponent(Entity* parent, const LOT parentLot) : Component(parent) { - auto* compRegistryTable = CDClientManager::Instance().GetTable(); + auto* compRegistryTable = CDClientManager::GetTable(); auto value = compRegistryTable->GetByIDAndType(parentLot, eReplicaComponentType::MISSION_OFFER, -1); @@ -48,7 +48,7 @@ MissionOfferComponent::MissionOfferComponent(Entity* parent, const LOT parentLot const uint32_t componentId = value; // Now lookup the missions in the MissionNPCComponent table - auto* missionNpcComponentTable = CDClientManager::Instance().GetTable(); + auto* missionNpcComponentTable = CDClientManager::GetTable(); auto missions = missionNpcComponentTable->Query([=](const CDMissionNPCComponent& entry) { return entry.id == static_cast(componentId); diff --git a/dGame/dComponents/MissionOfferComponent.h b/dGame/dComponents/MissionOfferComponent.h index 0f0d23ba..d842a92e 100644 --- a/dGame/dComponents/MissionOfferComponent.h +++ b/dGame/dComponents/MissionOfferComponent.h @@ -59,9 +59,9 @@ private: /** * Allows entities to offer missions to other entities, depending on their mission inventory progression. */ -class MissionOfferComponent : public Component { +class MissionOfferComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::MISSION_OFFER; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::MISSION_OFFER; MissionOfferComponent(Entity* parent, LOT parentLot); diff --git a/dGame/dComponents/ModelComponent.h b/dGame/dComponents/ModelComponent.h index a36328be..0d720d04 100644 --- a/dGame/dComponents/ModelComponent.h +++ b/dGame/dComponents/ModelComponent.h @@ -22,9 +22,9 @@ class MoveToInventoryMessage; /** * Component that represents entities that are a model, e.g. collectible models and BBB models. */ -class ModelComponent : public Component { +class ModelComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::MODEL; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::MODEL; ModelComponent(Entity* parent); diff --git a/dGame/dComponents/ModuleAssemblyComponent.h b/dGame/dComponents/ModuleAssemblyComponent.h index 9e7301fe..47e7baa6 100644 --- a/dGame/dComponents/ModuleAssemblyComponent.h +++ b/dGame/dComponents/ModuleAssemblyComponent.h @@ -10,9 +10,9 @@ * same as having said items in your inventory (the subkey for this component) this component is the one that * renders the entity into the world. */ -class ModuleAssemblyComponent : public Component { +class ModuleAssemblyComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::MODULE_ASSEMBLY; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::MODULE_ASSEMBLY; ModuleAssemblyComponent(Entity* parent); ~ModuleAssemblyComponent() override; diff --git a/dGame/dComponents/MovementAIComponent.cpp b/dGame/dComponents/MovementAIComponent.cpp index 1966638d..8377031a 100644 --- a/dGame/dComponents/MovementAIComponent.cpp +++ b/dGame/dComponents/MovementAIComponent.cpp @@ -16,6 +16,8 @@ #include "CDComponentsRegistryTable.h" #include "CDPhysicsComponentTable.h" +#include "dNavMesh.h" + namespace { /** * Cache of all lots and their respective speeds @@ -41,7 +43,7 @@ MovementAIComponent::MovementAIComponent(Entity* parent, MovementAIInfo info) : m_NextWaypoint = m_Parent->GetPosition(); m_Acceleration = 0.4f; m_PullingToPoint = false; - m_PullPoint = NiPoint3::ZERO; + m_PullPoint = NiPoint3Constant::ZERO; m_HaltDistance = 0; m_TimeToTravel = 0; m_TimeTravelled = 0; @@ -86,7 +88,7 @@ void MovementAIComponent::Update(const float deltaTime) { SetPosition(source); - NiPoint3 velocity = NiPoint3::ZERO; + NiPoint3 velocity = NiPoint3Constant::ZERO; if (m_Acceleration > 0 && m_BaseSpeed > 0 && AdvanceWaypointIndex()) // Do we have another waypoint to seek? { @@ -169,8 +171,8 @@ NiPoint3 MovementAIComponent::ApproximateLocation() const { auto approximation = source + ((destination - source) * percentageToWaypoint); - if (dpWorld::Instance().IsLoaded()) { - approximation.y = dpWorld::Instance().GetNavMesh()->GetHeightAtPoint(approximation); + if (dpWorld::IsLoaded()) { + approximation.y = dpWorld::GetNavMesh()->GetHeightAtPoint(approximation); } return approximation; @@ -181,8 +183,8 @@ bool MovementAIComponent::Warp(const NiPoint3& point) { NiPoint3 destination = point; - if (dpWorld::Instance().IsLoaded()) { - destination.y = dpWorld::Instance().GetNavMesh()->GetHeightAtPoint(point); + if (dpWorld::IsLoaded()) { + destination.y = dpWorld::GetNavMesh()->GetHeightAtPoint(point); if (std::abs(destination.y - point.y) > 3) { return false; @@ -201,7 +203,7 @@ void MovementAIComponent::Stop() { SetPosition(ApproximateLocation()); - SetVelocity(NiPoint3::ZERO); + SetVelocity(NiPoint3Constant::ZERO); m_TimeToTravel = 0; m_TimeTravelled = 0; @@ -242,8 +244,8 @@ float MovementAIComponent::GetBaseSpeed(LOT lot) { return it->second; } - CDComponentsRegistryTable* componentRegistryTable = CDClientManager::Instance().GetTable(); - CDPhysicsComponentTable* physicsComponentTable = CDClientManager::Instance().GetTable(); + CDComponentsRegistryTable* componentRegistryTable = CDClientManager::GetTable(); + CDPhysicsComponentTable* physicsComponentTable = CDClientManager::GetTable(); int32_t componentID; CDPhysicsComponent* physicsComponent = nullptr; @@ -302,8 +304,8 @@ void MovementAIComponent::SetDestination(const NiPoint3& destination) { } std::vector computedPath; - if (dpWorld::Instance().IsLoaded()) { - computedPath = dpWorld::Instance().GetNavMesh()->GetPath(m_Parent->GetPosition(), destination, m_Info.wanderSpeed); + if (dpWorld::IsLoaded()) { + computedPath = dpWorld::GetNavMesh()->GetPath(m_Parent->GetPosition(), destination, m_Info.wanderSpeed); } // Somehow failed @@ -328,8 +330,8 @@ void MovementAIComponent::SetDestination(const NiPoint3& destination) { // Simply path for (auto& point : computedPath) { - if (dpWorld::Instance().IsLoaded()) { - point.y = dpWorld::Instance().GetNavMesh()->GetHeightAtPoint(point); + if (dpWorld::IsLoaded()) { + point.y = dpWorld::GetNavMesh()->GetHeightAtPoint(point); } m_InterpolatedWaypoints.push_back(point); diff --git a/dGame/dComponents/MovementAIComponent.h b/dGame/dComponents/MovementAIComponent.h index 0ae71dc6..852f7001 100644 --- a/dGame/dComponents/MovementAIComponent.h +++ b/dGame/dComponents/MovementAIComponent.h @@ -55,9 +55,9 @@ struct MovementAIInfo { * Component that handles the movement settings of an entity. Not to be confused with the BaseCombatAI component that * actually handles attackig and following enemy entities. */ -class MovementAIComponent : public Component { +class MovementAIComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::MOVEMENT_AI; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::MOVEMENT_AI; MovementAIComponent(Entity* parentEntity, MovementAIInfo info); diff --git a/dGame/dComponents/MovingPlatformComponent.h b/dGame/dComponents/MovingPlatformComponent.h index bb5821d4..cf47b9c3 100644 --- a/dGame/dComponents/MovingPlatformComponent.h +++ b/dGame/dComponents/MovingPlatformComponent.h @@ -104,9 +104,9 @@ public: * don't at all do what you expect them to as we don't instruct the client of changes made here. * ^^^ Trivia: This made the red blocks platform and property platforms a pain to implement. */ -class MovingPlatformComponent : public Component { +class MovingPlatformComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::MOVING_PLATFORM; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::MOVING_PLATFORM; MovingPlatformComponent(Entity* parent, const std::string& pathName); ~MovingPlatformComponent() override; diff --git a/dGame/dComponents/MultiZoneEntranceComponent.h b/dGame/dComponents/MultiZoneEntranceComponent.h index c65dc0fe..8928be27 100644 --- a/dGame/dComponents/MultiZoneEntranceComponent.h +++ b/dGame/dComponents/MultiZoneEntranceComponent.h @@ -8,9 +8,9 @@ * Component that handles the LUP/WBL rocket launchpad that can be interacted with to travel to WBL worlds. * */ -class MultiZoneEntranceComponent : public Component { +class MultiZoneEntranceComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::MULTI_ZONE_ENTRANCE; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::MULTI_ZONE_ENTRANCE; /** * Constructor for this component, builds the m_LUPWorlds vector diff --git a/dGame/dComponents/PetComponent.cpp b/dGame/dComponents/PetComponent.cpp index a79c5fe2..111aa895 100644 --- a/dGame/dComponents/PetComponent.cpp +++ b/dGame/dComponents/PetComponent.cpp @@ -34,6 +34,7 @@ #include "eObjectBits.h" #include "eGameMasterLevel.h" #include "eMissionState.h" +#include "dNavMesh.h" std::unordered_map PetComponent::buildCache{}; std::unordered_map PetComponent::currentActivities{}; @@ -73,8 +74,8 @@ std::map PetComponent::petFlags = { { 13067, 838 }, // Skeleton dragon }; -PetComponent::PetComponent(Entity* parentEntity, uint32_t componentId) : Component{ parentEntity }, - m_PetInfo{ CDClientManager::Instance().GetTable()->GetByID(componentId) } { +PetComponent::PetComponent(Entity* parentEntity, uint32_t componentId) : Component{ parentEntity } { + m_PetInfo = CDClientManager::GetTable()->GetByID(componentId); // TODO: Make reference when safe m_ComponentId = componentId; m_Interaction = LWOOBJID_EMPTY; m_InteractType = PetInteractType::none; @@ -273,17 +274,17 @@ void PetComponent::OnUse(Entity* originator) { NiPoint3 forward = NiQuaternion::LookAt(m_Parent->GetPosition(), originator->GetPosition()).GetForwardVector(); forward.y = 0; - if (dpWorld::Instance().IsLoaded()) { + if (dpWorld::IsLoaded()) { NiPoint3 attempt = petPosition + forward * interactionDistance; - float y = dpWorld::Instance().GetNavMesh()->GetHeightAtPoint(attempt); + float y = dpWorld::GetNavMesh()->GetHeightAtPoint(attempt); while (std::abs(y - petPosition.y) > 4 && interactionDistance > 10) { const NiPoint3 forward = m_Parent->GetRotation().GetForwardVector(); attempt = originatorPosition + forward * interactionDistance; - y = dpWorld::Instance().GetNavMesh()->GetHeightAtPoint(attempt); + y = dpWorld::GetNavMesh()->GetHeightAtPoint(attempt); interactionDistance -= 0.5f; } @@ -476,7 +477,7 @@ void PetComponent::NotifyTamingBuildSuccess(NiPoint3 position) { EntityInfo info{}; info.lot = cached->second.puzzleModelLot; info.pos = position; - info.rot = NiQuaternion::IDENTITY; + info.rot = NiQuaternionConstant::IDENTITY; info.spawnerID = tamer->GetObjectID(); auto* modelEntity = Game::entityManager->CreateEntity(info); @@ -531,9 +532,9 @@ void PetComponent::NotifyTamingBuildSuccess(NiPoint3 position) { LWOOBJID_EMPTY, false, ePetTamingNotifyType::NAMINGPET, - NiPoint3::ZERO, - NiPoint3::ZERO, - NiQuaternion::IDENTITY, + NiPoint3Constant::ZERO, + NiPoint3Constant::ZERO, + NiQuaternionConstant::IDENTITY, UNASSIGNED_SYSTEM_ADDRESS ); @@ -608,9 +609,9 @@ void PetComponent::RequestSetPetName(std::u16string name) { m_Tamer, false, ePetTamingNotifyType::SUCCESS, - NiPoint3::ZERO, - NiPoint3::ZERO, - NiQuaternion::IDENTITY, + NiPoint3Constant::ZERO, + NiPoint3Constant::ZERO, + NiQuaternionConstant::IDENTITY, UNASSIGNED_SYSTEM_ADDRESS ); @@ -649,9 +650,9 @@ void PetComponent::ClientExitTamingMinigame(bool voluntaryExit) { m_Tamer, false, ePetTamingNotifyType::QUIT, - NiPoint3::ZERO, - NiPoint3::ZERO, - NiQuaternion::IDENTITY, + NiPoint3Constant::ZERO, + NiPoint3Constant::ZERO, + NiQuaternionConstant::IDENTITY, UNASSIGNED_SYSTEM_ADDRESS ); @@ -700,9 +701,9 @@ void PetComponent::ClientFailTamingMinigame() { m_Tamer, false, ePetTamingNotifyType::FAILED, - NiPoint3::ZERO, - NiPoint3::ZERO, - NiQuaternion::IDENTITY, + NiPoint3Constant::ZERO, + NiPoint3Constant::ZERO, + NiQuaternionConstant::IDENTITY, UNASSIGNED_SYSTEM_ADDRESS ); @@ -746,8 +747,8 @@ void PetComponent::Wander() { auto destination = m_StartPosition + delta; - if (dpWorld::Instance().IsLoaded()) { - destination.y = dpWorld::Instance().GetNavMesh()->GetHeightAtPoint(destination); + if (dpWorld::IsLoaded()) { + destination.y = dpWorld::GetNavMesh()->GetHeightAtPoint(destination); } if (Vector3::DistanceSquared(destination, m_MovementAI->GetParent()->GetPosition()) < 2 * 2) { @@ -756,7 +757,7 @@ void PetComponent::Wander() { return; } - m_MovementAI->SetMaxSpeed(m_PetInfo.sprintSpeed); //info.wanderSpeed); + m_MovementAI->SetMaxSpeed(m_PetInfo.sprintSpeed); m_MovementAI->SetDestination(destination); @@ -1152,6 +1153,7 @@ void PetComponent::AddDrainImaginationTimer(Item* item, bool fromTaming) { if (!fromTaming) playerDestroyableComponent->Imagine(-1); // Set this to a variable so when this is called back from the player the timer doesn't fire off. + m_Parent->AddCallbackTimer(m_PetInfo.imaginationDrainRate, [playerDestroyableComponent, this, item]() { m_Parent->AddCallbackTimer(m_PetInfo.imaginationDrainRate, [playerDestroyableComponent, this, item]() { if (!playerDestroyableComponent) { LOG("No petComponent and/or no playerDestroyableComponent"); @@ -1209,7 +1211,7 @@ void PetComponent::Release() { item->SetCount(0, false, false); } -void PetComponent::Command(const NiPoint3& position, const LWOOBJID& source, int32_t commandType, int32_t typeId, bool overrideObey) { +void PetComponent::Command(const NiPoint3& position, const LWOOBJID source, const int32_t commandType, const int32_t typeId, const bool overrideObey) { auto* owner = GetOwner(); if (!owner) return; diff --git a/dGame/dComponents/PetComponent.h b/dGame/dComponents/PetComponent.h index 18f2a85c..001faa1e 100644 --- a/dGame/dComponents/PetComponent.h +++ b/dGame/dComponents/PetComponent.h @@ -5,6 +5,7 @@ #include "MovementAIComponent.h" #include "Component.h" #include "Preconditions.h" +#include "ePetAbilityType.h" #include "eReplicaComponentType.h" #include "ePetAbilityType.h" #include "CDPetComponentTable.h" @@ -64,9 +65,10 @@ enum PetEmote : int32_t { * Represents an entity that is a pet. This pet can be tamed and consequently follows the tamer around, allowing it * to dig for treasure and activate pet bouncers. */ -class PetComponent : public Component { +class PetComponent final : public Component +{ public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::PET; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::PET; /** * PetComponent constructor @@ -252,7 +254,7 @@ public: * @param typeId extra information about the command, e.g. the emote to play * @param overrideObey unused */ - void Command(const NiPoint3& position, const LWOOBJID& source, int32_t commandType, int32_t typeId, bool overrideObey); + void Command(const NiPoint3& position, const LWOOBJID source, const int32_t commandType, const int32_t typeId, const bool overrideObey); /** * Returns the ID of the owner of this pet (if any) @@ -583,8 +585,9 @@ private: /** * Pet information loaded from the CDClientDatabase + * TODO: Switch to a reference when safe to do so */ - CDPetComponent& m_PetInfo; + CDPetComponent m_PetInfo; }; -#endif // PETCOMPONENT_H +#endif // !PETCOMPONENT_H diff --git a/dGame/dComponents/PhantomPhysicsComponent.cpp b/dGame/dComponents/PhantomPhysicsComponent.cpp index 6248bfe9..592c2a6b 100644 --- a/dGame/dComponents/PhantomPhysicsComponent.cpp +++ b/dGame/dComponents/PhantomPhysicsComponent.cpp @@ -143,10 +143,10 @@ PhantomPhysicsComponent::PhantomPhysicsComponent(Entity* parent) : PhysicsCompon */ if (!m_HasCreatedPhysics) { - CDComponentsRegistryTable* compRegistryTable = CDClientManager::Instance().GetTable(); + CDComponentsRegistryTable* compRegistryTable = CDClientManager::GetTable(); auto componentID = compRegistryTable->GetByIDAndType(m_Parent->GetLOT(), eReplicaComponentType::PHANTOM_PHYSICS); - CDPhysicsComponentTable* physComp = CDClientManager::Instance().GetTable(); + CDPhysicsComponentTable* physComp = CDClientManager::GetTable(); if (physComp == nullptr) return; @@ -156,89 +156,48 @@ PhantomPhysicsComponent::PhantomPhysicsComponent(Entity* parent) : PhysicsCompon //temp test if (info->physicsAsset == "miscellaneous\\misc_phys_10x1x5.hkx") { m_dpEntity = new dpEntity(m_Parent->GetObjectID(), 10.0f, 5.0f, 1.0f); - - m_dpEntity->SetScale(m_Scale); - m_dpEntity->SetRotation(m_Rotation); - m_dpEntity->SetPosition(m_Position); - - dpWorld::Instance().AddEntity(m_dpEntity); } else if (info->physicsAsset == "miscellaneous\\misc_phys_640x640.hkx") { - // Move this down by 13.521004 units so it is still effectively at the same height as before - m_Position = m_Position - NiPoint3::UNIT_Y * 13.521004f; // TODO Fix physics simulation to do simulation at high velocities due to bullet through paper problem... m_dpEntity = new dpEntity(m_Parent->GetObjectID(), 1638.4f, 13.521004f * 2.0f, 1638.4f); - m_dpEntity->SetScale(m_Scale); - m_dpEntity->SetRotation(m_Rotation); - m_dpEntity->SetPosition(m_Position); - - dpWorld::Instance().AddEntity(m_dpEntity); + // Move this down by 13.521004 units so it is still effectively at the same height as before + m_Position = m_Position - NiPoint3Constant::UNIT_Y * 13.521004f; } else if (info->physicsAsset == "env\\trigger_wall_tall.hkx") { m_dpEntity = new dpEntity(m_Parent->GetObjectID(), 10.0f, 25.0f, 1.0f); - m_dpEntity->SetScale(m_Scale); - m_dpEntity->SetRotation(m_Rotation); - m_dpEntity->SetPosition(m_Position); - dpWorld::Instance().AddEntity(m_dpEntity); } else if (info->physicsAsset == "env\\env_gen_placeholderphysics.hkx") { m_dpEntity = new dpEntity(m_Parent->GetObjectID(), 20.0f, 20.0f, 20.0f); - m_dpEntity->SetScale(m_Scale); - m_dpEntity->SetRotation(m_Rotation); - m_dpEntity->SetPosition(m_Position); - dpWorld::Instance().AddEntity(m_dpEntity); } else if (info->physicsAsset == "env\\POI_trigger_wall.hkx") { m_dpEntity = new dpEntity(m_Parent->GetObjectID(), 1.0f, 12.5f, 20.0f); // Not sure what the real size is - m_dpEntity->SetScale(m_Scale); - m_dpEntity->SetRotation(m_Rotation); - m_dpEntity->SetPosition(m_Position); - dpWorld::Instance().AddEntity(m_dpEntity); } else if (info->physicsAsset == "env\\NG_NinjaGo\\env_ng_gen_gate_chamber_puzzle_ceiling_tile_falling_phantom.hkx") { m_dpEntity = new dpEntity(m_Parent->GetObjectID(), 18.0f, 5.0f, 15.0f); - m_dpEntity->SetScale(m_Scale); - m_dpEntity->SetRotation(m_Rotation); - m_dpEntity->SetPosition(m_Position + m_Rotation.GetForwardVector() * 7.5f); - dpWorld::Instance().AddEntity(m_dpEntity); + m_Position += m_Rotation.GetForwardVector() * 7.5f; } else if (info->physicsAsset == "env\\NG_NinjaGo\\ng_flamejet_brick_phantom.HKX") { m_dpEntity = new dpEntity(m_Parent->GetObjectID(), 1.0f, 1.0f, 12.0f); - m_dpEntity->SetScale(m_Scale); - m_dpEntity->SetRotation(m_Rotation); - m_dpEntity->SetPosition(m_Position + m_Rotation.GetForwardVector() * 6.0f); - dpWorld::Instance().AddEntity(m_dpEntity); + m_Position += m_Rotation.GetForwardVector() * 6.0f; } else if (info->physicsAsset == "env\\Ring_Trigger.hkx") { m_dpEntity = new dpEntity(m_Parent->GetObjectID(), 6.0f, 6.0f, 6.0f); - m_dpEntity->SetScale(m_Scale); - m_dpEntity->SetRotation(m_Rotation); - m_dpEntity->SetPosition(m_Position); - dpWorld::Instance().AddEntity(m_dpEntity); } else if (info->physicsAsset == "env\\vfx_propertyImaginationBall.hkx") { m_dpEntity = new dpEntity(m_Parent->GetObjectID(), 4.5f); - m_dpEntity->SetScale(m_Scale); - m_dpEntity->SetRotation(m_Rotation); - m_dpEntity->SetPosition(m_Position); - dpWorld::Instance().AddEntity(m_dpEntity); } else if (info->physicsAsset == "env\\env_won_fv_gas-blocking-volume.hkx") { m_dpEntity = new dpEntity(m_Parent->GetObjectID(), 390.496826f, 111.467964f, 600.821534f, true); - m_dpEntity->SetScale(m_Scale); - m_dpEntity->SetRotation(m_Rotation); m_Position.y -= (111.467964f * m_Scale) / 2; - m_dpEntity->SetPosition(m_Position); - dpWorld::Instance().AddEntity(m_dpEntity); } else { - //LOG("This one is supposed to have %s", info->physicsAsset.c_str()); + // LOG_DEBUG("This one is supposed to have %s", info->physicsAsset.c_str()); //add fallback cube: m_dpEntity = new dpEntity(m_Parent->GetObjectID(), 2.0f, 2.0f, 2.0f); - m_dpEntity->SetScale(m_Scale); - m_dpEntity->SetRotation(m_Rotation); - m_dpEntity->SetPosition(m_Position); - dpWorld::Instance().AddEntity(m_dpEntity); } - + + m_dpEntity->SetScale(m_Scale); + m_dpEntity->SetRotation(m_Rotation); + m_dpEntity->SetPosition(m_Position); + dpWorld::AddEntity(m_dpEntity); } } PhantomPhysicsComponent::~PhantomPhysicsComponent() { if (m_dpEntity) { - dpWorld::Instance().RemoveEntity(m_dpEntity); + dpWorld::RemoveEntity(m_dpEntity); } } @@ -260,10 +219,10 @@ void PhantomPhysicsComponent::CreatePhysics() { y = m_Parent->GetVar(u"primitiveModelValueY"); z = m_Parent->GetVar(u"primitiveModelValueZ"); } else { - CDComponentsRegistryTable* compRegistryTable = CDClientManager::Instance().GetTable(); + CDComponentsRegistryTable* compRegistryTable = CDClientManager::GetTable(); auto componentID = compRegistryTable->GetByIDAndType(m_Parent->GetLOT(), eReplicaComponentType::PHANTOM_PHYSICS); - CDPhysicsComponentTable* physComp = CDClientManager::Instance().GetTable(); + CDPhysicsComponentTable* physComp = CDClientManager::GetTable(); if (physComp == nullptr) return; @@ -300,7 +259,7 @@ void PhantomPhysicsComponent::CreatePhysics() { m_dpEntity->SetPosition({ m_Position.x, m_Position.y - (height / 2), m_Position.z }); - dpWorld::Instance().AddEntity(m_dpEntity); + dpWorld::AddEntity(m_dpEntity); m_HasCreatedPhysics = true; } diff --git a/dGame/dComponents/PhantomPhysicsComponent.h b/dGame/dComponents/PhantomPhysicsComponent.h index 5fcee004..2ea9e979 100644 --- a/dGame/dComponents/PhantomPhysicsComponent.h +++ b/dGame/dComponents/PhantomPhysicsComponent.h @@ -25,9 +25,9 @@ enum class ePhysicsEffectType : uint32_t ; * trigger gameplay events, for example the bus in Avant Gardens that moves around when the player touches its physics * body. Optionally this object can also have effects, like the fans in AG. */ -class PhantomPhysicsComponent : public PhysicsComponent { +class PhantomPhysicsComponent final : public PhysicsComponent { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::PHANTOM_PHYSICS; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::PHANTOM_PHYSICS; PhantomPhysicsComponent(Entity* parent); ~PhantomPhysicsComponent() override; diff --git a/dGame/dComponents/PhysicsComponent.cpp b/dGame/dComponents/PhysicsComponent.cpp index a66c422a..ea57735f 100644 --- a/dGame/dComponents/PhysicsComponent.cpp +++ b/dGame/dComponents/PhysicsComponent.cpp @@ -1,8 +1,8 @@ #include "PhysicsComponent.h" PhysicsComponent::PhysicsComponent(Entity* parent) : Component(parent) { - m_Position = NiPoint3::ZERO; - m_Rotation = NiQuaternion::IDENTITY; + m_Position = NiPoint3Constant::ZERO; + m_Rotation = NiQuaternionConstant::IDENTITY; m_DirtyPosition = false; } diff --git a/dGame/dComponents/PlayerForcedMovementComponent.h b/dGame/dComponents/PlayerForcedMovementComponent.h index 810b727c..e5aca37d 100644 --- a/dGame/dComponents/PlayerForcedMovementComponent.h +++ b/dGame/dComponents/PlayerForcedMovementComponent.h @@ -8,9 +8,9 @@ * Component that handles player forced movement * */ -class PlayerForcedMovementComponent : public Component { +class PlayerForcedMovementComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::PLAYER_FORCED_MOVEMENT; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::PLAYER_FORCED_MOVEMENT; /** * Constructor for this component diff --git a/dGame/dComponents/PossessableComponent.h b/dGame/dComponents/PossessableComponent.h index 9a767ba9..3b075d33 100644 --- a/dGame/dComponents/PossessableComponent.h +++ b/dGame/dComponents/PossessableComponent.h @@ -12,9 +12,9 @@ * Represents an entity that can be controlled by some other entity, generally used by cars to indicate that some * player is controlling it. */ -class PossessableComponent : public Component { +class PossessableComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::POSSESSABLE; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::POSSESSABLE; PossessableComponent(Entity* parentEntity, uint32_t componentId); diff --git a/dGame/dComponents/PossessorComponent.h b/dGame/dComponents/PossessorComponent.h index c225766b..e1aba048 100644 --- a/dGame/dComponents/PossessorComponent.h +++ b/dGame/dComponents/PossessorComponent.h @@ -16,9 +16,9 @@ enum class ePossessionType : uint8_t { /** * Represents an entity that can posess other entities. Generally used by players to drive a car. */ -class PossessorComponent : public Component { +class PossessorComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::POSSESSOR; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::POSSESSOR; PossessorComponent(Entity* parent); ~PossessorComponent() override; diff --git a/dGame/dComponents/PropertyComponent.cpp b/dGame/dComponents/PropertyComponent.cpp deleted file mode 100644 index 4f8df40c..00000000 --- a/dGame/dComponents/PropertyComponent.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "PropertyComponent.h" -#include "GameMessages.h" -#include "dZoneManager.h" - -PropertyComponent::PropertyComponent(Entity* parent) : Component(parent) { - m_PropertyName = parent->GetVar(u"propertyName"); - m_PropertyState = new PropertyState(); -} - -PropertyComponent::~PropertyComponent() = default; - diff --git a/dGame/dComponents/PropertyComponent.h b/dGame/dComponents/PropertyComponent.h index 135a1d26..1085aac4 100644 --- a/dGame/dComponents/PropertyComponent.h +++ b/dGame/dComponents/PropertyComponent.h @@ -1,34 +1,22 @@ /* * Darkflame Universe - * Copyright 2018 + * Copyright 2024 */ #ifndef PROPERTYCOMPONENT_H #define PROPERTYCOMPONENT_H -#include "BitStream.h" #include "Entity.h" #include "Component.h" #include "eReplicaComponentType.h" -struct PropertyState { - LWOOBJID ownerID; - LWOOBJID propertyID; - bool rented; -}; - /** * This component is unused and has no functionality */ -class PropertyComponent : public Component { +class PropertyComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::PROPERTY; - explicit PropertyComponent(Entity* parentEntity); - ~PropertyComponent() override; - [[nodiscard]] PropertyState* GetPropertyState() const { return m_PropertyState; }; -private: - PropertyState* m_PropertyState; - std::string m_PropertyName; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::PROPERTY; + explicit PropertyComponent(Entity* const parentEntity) noexcept : Component{ parentEntity } {} }; -#endif // PROPERTYCOMPONENT_H +#endif // !PROPERTYCOMPONENT_H diff --git a/dGame/dComponents/PropertyEntranceComponent.cpp b/dGame/dComponents/PropertyEntranceComponent.cpp index 2bb6ea30..ab3bb5da 100644 --- a/dGame/dComponents/PropertyEntranceComponent.cpp +++ b/dGame/dComponents/PropertyEntranceComponent.cpp @@ -18,7 +18,7 @@ PropertyEntranceComponent::PropertyEntranceComponent(Entity* parent, uint32_t componentID) : Component(parent) { this->propertyQueries = {}; - auto table = CDClientManager::Instance().GetTable(); + auto table = CDClientManager::GetTable(); const auto& entry = table->GetByID(componentID); this->m_MapID = entry.mapID; diff --git a/dGame/dComponents/PropertyEntranceComponent.h b/dGame/dComponents/PropertyEntranceComponent.h index ef8f9810..510bb489 100644 --- a/dGame/dComponents/PropertyEntranceComponent.h +++ b/dGame/dComponents/PropertyEntranceComponent.h @@ -11,10 +11,10 @@ /** * Represents the launch pad that's used to select and browse properties */ -class PropertyEntranceComponent : public Component { +class PropertyEntranceComponent final : public Component { public: explicit PropertyEntranceComponent(Entity* parent, uint32_t componentID); - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::PROPERTY_ENTRANCE; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::PROPERTY_ENTRANCE; /** * Handles an OnUse request for some other entity, rendering the property browse menu diff --git a/dGame/dComponents/PropertyManagementComponent.cpp b/dGame/dComponents/PropertyManagementComponent.cpp index 833b7d73..0dfc04af 100644 --- a/dGame/dComponents/PropertyManagementComponent.cpp +++ b/dGame/dComponents/PropertyManagementComponent.cpp @@ -14,12 +14,13 @@ #include "Item.h" #include "Database.h" #include "ObjectIDManager.h" -#include "Player.h" #include "RocketLaunchpadControlComponent.h" #include "PropertyEntranceComponent.h" #include "InventoryComponent.h" #include "eMissionTaskType.h" #include "eObjectBits.h" +#include "CharacterComponent.h" +#include "PlayerManager.h" #include #include "CppScripts.h" @@ -175,8 +176,6 @@ bool PropertyManagementComponent::Claim(const LWOOBJID playerId) { auto* entity = Game::entityManager->GetEntity(playerId); - auto* user = entity->GetParentUser(); - auto character = entity->GetCharacter(); if (!character) return false; @@ -226,7 +225,7 @@ void PropertyManagementComponent::OnStartBuilding() { if (ownerEntity == nullptr) return; - const auto players = Player::GetAllPlayers(); + const auto players = PlayerManager::GetAllPlayers(); LWOMAPID zoneId = 1100; @@ -247,7 +246,8 @@ void PropertyManagementComponent::OnStartBuilding() { for (auto* player : players) { if (player == ownerEntity) continue; - player->SendToZone(zoneId); + auto* characterComponent = player->GetComponent(); + if (characterComponent) characterComponent->SendToZone(zoneId); } auto inventoryComponent = ownerEntity->GetComponent(); @@ -294,7 +294,7 @@ void PropertyManagementComponent::UpdateModelPosition(const LWOOBJID id, const N const auto modelLOT = item->GetLot(); - if (rotation != NiQuaternion::IDENTITY) { + if (rotation != NiQuaternionConstant::IDENTITY) { rotation = { rotation.w, rotation.z, rotation.y, rotation.x }; } @@ -478,7 +478,7 @@ void PropertyManagementComponent::DeleteModel(const LWOOBJID id, const int delet GameMessages::SendGetModelsOnProperty(entity->GetObjectID(), GetModels(), UNASSIGNED_SYSTEM_ADDRESS); - GameMessages::SendPlaceModelResponse(entity->GetObjectID(), entity->GetSystemAddress(), NiPoint3::ZERO, LWOOBJID_EMPTY, 16, NiQuaternion::IDENTITY); + GameMessages::SendPlaceModelResponse(entity->GetObjectID(), entity->GetSystemAddress(), NiPoint3Constant::ZERO, LWOOBJID_EMPTY, 16, NiQuaternionConstant::IDENTITY); if (spawner != nullptr) { Game::zoneManager->RemoveSpawner(spawner->m_Info.spawnerID); @@ -519,7 +519,7 @@ void PropertyManagementComponent::DeleteModel(const LWOOBJID id, const int delet { item->SetCount(item->GetCount() - 1); - LOG("BODGE TIME, YES IT GOES HERE"); + LOG("DLU currently does not support breaking apart brick by brick models."); break; } @@ -531,7 +531,7 @@ void PropertyManagementComponent::DeleteModel(const LWOOBJID id, const int delet GameMessages::SendGetModelsOnProperty(entity->GetObjectID(), GetModels(), UNASSIGNED_SYSTEM_ADDRESS); - GameMessages::SendPlaceModelResponse(entity->GetObjectID(), entity->GetSystemAddress(), NiPoint3::ZERO, LWOOBJID_EMPTY, 16, NiQuaternion::IDENTITY); + GameMessages::SendPlaceModelResponse(entity->GetObjectID(), entity->GetSystemAddress(), NiPoint3Constant::ZERO, LWOOBJID_EMPTY, 16, NiQuaternionConstant::IDENTITY); if (spawner != nullptr) { Game::zoneManager->RemoveSpawner(spawner->m_Info.spawnerID); diff --git a/dGame/dComponents/PropertyManagementComponent.h b/dGame/dComponents/PropertyManagementComponent.h index d38437c4..6a9ed09d 100644 --- a/dGame/dComponents/PropertyManagementComponent.h +++ b/dGame/dComponents/PropertyManagementComponent.h @@ -8,8 +8,7 @@ /** * Information regarding which players may visit this property */ -enum class PropertyPrivacyOption -{ +enum class PropertyPrivacyOption { /** * Default, only you can visit your property */ @@ -29,10 +28,9 @@ enum class PropertyPrivacyOption /** * Main component that handles interactions with a property, generally the plaques you see on properties. */ -class PropertyManagementComponent : public Component -{ +class PropertyManagementComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::PROPERTY_MANAGEMENT; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::PROPERTY_MANAGEMENT; PropertyManagementComponent(Entity* parent); static PropertyManagementComponent* Instance(); diff --git a/dGame/dComponents/PropertyVendorComponent.h b/dGame/dComponents/PropertyVendorComponent.h index 0e6c6521..fee8af9c 100644 --- a/dGame/dComponents/PropertyVendorComponent.h +++ b/dGame/dComponents/PropertyVendorComponent.h @@ -7,10 +7,9 @@ /** * The property guard that stands on a property before it's claimed, allows entities to attempt claiming this property. */ -class PropertyVendorComponent : public Component -{ +class PropertyVendorComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::PROPERTY_VENDOR; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::PROPERTY_VENDOR; explicit PropertyVendorComponent(Entity* parent); /** diff --git a/dGame/dComponents/ProximityMonitorComponent.cpp b/dGame/dComponents/ProximityMonitorComponent.cpp index acc93fde..fbac8ddb 100644 --- a/dGame/dComponents/ProximityMonitorComponent.cpp +++ b/dGame/dComponents/ProximityMonitorComponent.cpp @@ -18,7 +18,7 @@ ProximityMonitorComponent::~ProximityMonitorComponent() { for (const auto& en : m_ProximitiesData) { if (!en.second) continue; - dpWorld::Instance().RemoveEntity(en.second); + dpWorld::RemoveEntity(en.second); } m_ProximitiesData.clear(); @@ -28,12 +28,12 @@ void ProximityMonitorComponent::SetProximityRadius(float proxRadius, const std:: dpEntity* en = new dpEntity(m_Parent->GetObjectID(), proxRadius); en->SetPosition(m_Parent->GetPosition()); - dpWorld::Instance().AddEntity(en); + dpWorld::AddEntity(en); m_ProximitiesData.insert(std::make_pair(name, en)); } void ProximityMonitorComponent::SetProximityRadius(dpEntity* entity, const std::string& name) { - dpWorld::Instance().AddEntity(entity); + dpWorld::AddEntity(entity); entity->SetPosition(m_Parent->GetPosition()); m_ProximitiesData.insert(std::make_pair(name, entity)); } diff --git a/dGame/dComponents/ProximityMonitorComponent.h b/dGame/dComponents/ProximityMonitorComponent.h index 90ba7d54..512b2848 100644 --- a/dGame/dComponents/ProximityMonitorComponent.h +++ b/dGame/dComponents/ProximityMonitorComponent.h @@ -17,9 +17,9 @@ * Utility component for detecting how close entities are to named proximities for this entity. Allows you to store * proximity checks for multiple ojects. */ -class ProximityMonitorComponent : public Component { +class ProximityMonitorComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::PROXIMITY_MONITOR; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::PROXIMITY_MONITOR; ProximityMonitorComponent(Entity* parentEntity, int smallRadius = -1, int largeRadius = -1); ~ProximityMonitorComponent() override; diff --git a/dGame/dComponents/QuickBuildComponent.cpp b/dGame/dComponents/QuickBuildComponent.cpp index 16263d81..6cef87d3 100644 --- a/dGame/dComponents/QuickBuildComponent.cpp +++ b/dGame/dComponents/QuickBuildComponent.cpp @@ -14,7 +14,6 @@ #include "eGameActivity.h" #include "dServer.h" -#include "PacketUtils.h" #include "Spawner.h" #include "MovingPlatformComponent.h" #include "Preconditions.h" @@ -24,7 +23,7 @@ #include "CppScripts.h" -QuickBuildComponent::QuickBuildComponent(Entity* entity) : Component(entity) { +QuickBuildComponent::QuickBuildComponent(Entity* const entity) : Component{ entity } { std::u16string checkPreconditions = entity->GetVar(u"CheckPrecondition"); if (!checkPreconditions.empty()) { @@ -34,10 +33,9 @@ QuickBuildComponent::QuickBuildComponent(Entity* entity) : Component(entity) { // Should a setting that has the build activator position exist, fetch that setting here and parse it for position. // It is assumed that the user who sets this setting uses the correct character delimiter (character 31 or in hex 0x1F) auto positionAsVector = GeneralUtils::SplitString(m_Parent->GetVarAsString(u"rebuild_activators"), 0x1F); - if (positionAsVector.size() == 3 && - GeneralUtils::TryParse(positionAsVector[0], m_ActivatorPosition.x) && - GeneralUtils::TryParse(positionAsVector[1], m_ActivatorPosition.y) && - GeneralUtils::TryParse(positionAsVector[2], m_ActivatorPosition.z)) { + const auto activatorPositionValid = GeneralUtils::TryParse(positionAsVector); + if (positionAsVector.size() == 3 && activatorPositionValid) { + m_ActivatorPosition = activatorPositionValid.value(); } else { LOG("Failed to find activator position for lot %i. Defaulting to parents position.", m_Parent->GetLOT()); m_ActivatorPosition = m_Parent->GetPosition(); @@ -254,13 +252,13 @@ void QuickBuildComponent::OnUse(Entity* originator) { } void QuickBuildComponent::SpawnActivator() { - if (!m_SelfActivator || m_ActivatorPosition != NiPoint3::ZERO) { + if (!m_SelfActivator || m_ActivatorPosition != NiPoint3Constant::ZERO) { if (!m_Activator) { EntityInfo info; info.lot = 6604; info.spawnerID = m_Parent->GetObjectID(); - info.pos = m_ActivatorPosition == NiPoint3::ZERO ? m_Parent->GetPosition() : m_ActivatorPosition; + info.pos = m_ActivatorPosition == NiPoint3Constant::ZERO ? m_Parent->GetPosition() : m_ActivatorPosition; m_Activator = Game::entityManager->CreateEntity(info, nullptr, m_Parent); if (m_Activator) { @@ -285,73 +283,73 @@ void QuickBuildComponent::DespawnActivator() { } } -Entity* QuickBuildComponent::GetActivator() { +Entity* QuickBuildComponent::GetActivator() const { return Game::entityManager->GetEntity(m_ActivatorId); } -NiPoint3 QuickBuildComponent::GetActivatorPosition() { +NiPoint3 QuickBuildComponent::GetActivatorPosition() const noexcept { return m_ActivatorPosition; } -float QuickBuildComponent::GetResetTime() { +float QuickBuildComponent::GetResetTime() const noexcept { return m_ResetTime; } -float QuickBuildComponent::GetCompleteTime() { +float QuickBuildComponent::GetCompleteTime() const noexcept { return m_CompleteTime; } -int QuickBuildComponent::GetTakeImagination() { +int32_t QuickBuildComponent::GetTakeImagination() const noexcept { return m_TakeImagination; } -bool QuickBuildComponent::GetInterruptible() { +bool QuickBuildComponent::GetInterruptible() const noexcept { return m_Interruptible; } -bool QuickBuildComponent::GetSelfActivator() { +bool QuickBuildComponent::GetSelfActivator() const noexcept { return m_SelfActivator; } -std::vector QuickBuildComponent::GetCustomModules() { +std::vector QuickBuildComponent::GetCustomModules() const noexcept { return m_CustomModules; } -int QuickBuildComponent::GetActivityId() { +int32_t QuickBuildComponent::GetActivityId() const noexcept { return m_ActivityId; } -int QuickBuildComponent::GetPostImaginationCost() { +int32_t QuickBuildComponent::GetPostImaginationCost() const noexcept { return m_PostImaginationCost; } -float QuickBuildComponent::GetTimeBeforeSmash() { +float QuickBuildComponent::GetTimeBeforeSmash() const noexcept { return m_TimeBeforeSmash; } -eQuickBuildState QuickBuildComponent::GetState() { +eQuickBuildState QuickBuildComponent::GetState() const noexcept { return m_State; } Entity* QuickBuildComponent::GetBuilder() const { - auto* builder = Game::entityManager->GetEntity(m_Builder); + auto* const builder = Game::entityManager->GetEntity(m_Builder); return builder; } -bool QuickBuildComponent::GetRepositionPlayer() const { +bool QuickBuildComponent::GetRepositionPlayer() const noexcept { return m_RepositionPlayer; } -void QuickBuildComponent::SetActivatorPosition(NiPoint3 value) { +void QuickBuildComponent::SetActivatorPosition(const NiPoint3& value) noexcept { m_ActivatorPosition = value; } -void QuickBuildComponent::SetResetTime(float value) { +void QuickBuildComponent::SetResetTime(const float value) noexcept { m_ResetTime = value; } -void QuickBuildComponent::SetCompleteTime(float value) { +void QuickBuildComponent::SetCompleteTime(const float value) noexcept { if (value < 0) { m_CompleteTime = 4.5f; } else { @@ -359,31 +357,31 @@ void QuickBuildComponent::SetCompleteTime(float value) { } } -void QuickBuildComponent::SetTakeImagination(int value) { +void QuickBuildComponent::SetTakeImagination(const int32_t value) noexcept { m_TakeImagination = value; } -void QuickBuildComponent::SetInterruptible(bool value) { +void QuickBuildComponent::SetInterruptible(const bool value) noexcept { m_Interruptible = value; } -void QuickBuildComponent::SetSelfActivator(bool value) { +void QuickBuildComponent::SetSelfActivator(const bool value) noexcept { m_SelfActivator = value; } -void QuickBuildComponent::SetCustomModules(std::vector value) { +void QuickBuildComponent::SetCustomModules(const std::vector& value) noexcept { m_CustomModules = value; } -void QuickBuildComponent::SetActivityId(int value) { +void QuickBuildComponent::SetActivityId(const int32_t value) noexcept { m_ActivityId = value; } -void QuickBuildComponent::SetPostImaginationCost(int value) { +void QuickBuildComponent::SetPostImaginationCost(const int32_t value) noexcept { m_PostImaginationCost = value; } -void QuickBuildComponent::SetTimeBeforeSmash(float value) { +void QuickBuildComponent::SetTimeBeforeSmash(const float value) noexcept { if (value < 0) { m_TimeBeforeSmash = 10.0f; } else { @@ -391,11 +389,11 @@ void QuickBuildComponent::SetTimeBeforeSmash(float value) { } } -void QuickBuildComponent::SetRepositionPlayer(bool value) { +void QuickBuildComponent::SetRepositionPlayer(const bool value) noexcept { m_RepositionPlayer = value; } -void QuickBuildComponent::StartQuickBuild(Entity* user) { +void QuickBuildComponent::StartQuickBuild(Entity* const user) { if (m_State == eQuickBuildState::OPEN || m_State == eQuickBuildState::COMPLETED || m_State == eQuickBuildState::INCOMPLETE) { m_Builder = user->GetObjectID(); @@ -428,10 +426,8 @@ void QuickBuildComponent::StartQuickBuild(Entity* user) { } } -void QuickBuildComponent::CompleteQuickBuild(Entity* user) { - if (user == nullptr) { - return; - } +void QuickBuildComponent::CompleteQuickBuild(Entity* const user) { + if (!user) return; auto* characterComponent = user->GetComponent(); if (characterComponent != nullptr) { @@ -520,7 +516,7 @@ void QuickBuildComponent::CompleteQuickBuild(Entity* user) { RenderComponent::PlayAnimation(user, u"rebuild-celebrate", 1.09f); } -void QuickBuildComponent::ResetQuickBuild(bool failed) { +void QuickBuildComponent::ResetQuickBuild(const bool failed) { Entity* builder = GetBuilder(); if (m_State == eQuickBuildState::BUILDING && builder) { @@ -555,7 +551,7 @@ void QuickBuildComponent::ResetQuickBuild(bool failed) { } } -void QuickBuildComponent::CancelQuickBuild(Entity* entity, eQuickBuildFailReason failReason, bool skipChecks) { +void QuickBuildComponent::CancelQuickBuild(Entity* const entity, const eQuickBuildFailReason failReason, const bool skipChecks) { if (m_State != eQuickBuildState::COMPLETED || skipChecks) { m_Builder = LWOOBJID_EMPTY; @@ -583,9 +579,7 @@ void QuickBuildComponent::CancelQuickBuild(Entity* entity, eQuickBuildFailReason Game::entityManager->SerializeEntity(m_Parent); } - if (entity == nullptr) { - return; - } + if (!entity) return; CharacterComponent* characterComponent = entity->GetComponent(); if (characterComponent) { diff --git a/dGame/dComponents/QuickBuildComponent.h b/dGame/dComponents/QuickBuildComponent.h index f1106a61..556264a8 100644 --- a/dGame/dComponents/QuickBuildComponent.h +++ b/dGame/dComponents/QuickBuildComponent.h @@ -20,11 +20,11 @@ enum class eQuickBuildFailReason : uint32_t; * consists of an activator that shows a popup and then the actual entity that the bricks are built into. Note * that quick builds are also scripted activities so this shared some logic with the ScriptedActivityComponent. */ -class QuickBuildComponent : public Component { +class QuickBuildComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::QUICK_BUILD; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::QUICK_BUILD; - QuickBuildComponent(Entity* entity); + QuickBuildComponent(Entity* const entity); ~QuickBuildComponent() override; void Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate) override; @@ -50,148 +50,148 @@ public: * Returns the entity that acts as the activator for this quickbuild * @return the entity that acts as the activator for this quickbuild */ - Entity* GetActivator(); + [[nodiscard]] Entity* GetActivator() const; /** * Returns the spawn position of the activator for this quickbuild, if any * @return the spawn position of the activator for this quickbuild, if any */ - NiPoint3 GetActivatorPosition(); + [[nodiscard]] NiPoint3 GetActivatorPosition() const noexcept; /** * Sets the spawn position for the activator of this quickbuild * @param value the spawn position to set for the activator */ - void SetActivatorPosition(NiPoint3 value); + void SetActivatorPosition(const NiPoint3& value) noexcept; /** * Returns the time it takes for the quickbuild to reset after being built * @return the time it takes for the quickbuild to reset after being built */ - float GetResetTime(); + [[nodiscard]] float GetResetTime() const noexcept; /** * Sets the time it takes for the quickbuild to reset after being built * @param value the reset time to set */ - void SetResetTime(float value); + void SetResetTime(const float value) noexcept; /** * Returns the time it takes to complete the quickbuild * @return the time it takes to complete the quickbuild */ - float GetCompleteTime(); + [[nodiscard]] float GetCompleteTime() const noexcept; /** * Sets the time it takes to complete the quickbuild * @param value the completion time to set */ - void SetCompleteTime(float value); + void SetCompleteTime(const float value) noexcept; /** * Returns the imagination that's taken when completing the quickbuild * @return the imagination that's taken when completing the quickbuild */ - int GetTakeImagination(); + [[nodiscard]] int32_t GetTakeImagination() const noexcept; /** * Sets the imagination that's taken when completing the quickbuild * @param value the imagination deduction to set */ - void SetTakeImagination(int value); + void SetTakeImagination(const int32_t value) noexcept; /** * Returns if the quickbuild can be interrupted, currently unused * @return if the quickbuild can be interrupted */ - bool GetInterruptible(); + [[nodiscard]] bool GetInterruptible() const noexcept; /** * Sets whether or not the quickbuild can be interrupted, currently unused * @param value true if the quickbuild may be interrupted, false otherwise */ - void SetInterruptible(bool value); + void SetInterruptible(const bool value) noexcept; /** * Returns whether or not this entity contains a built-in activator * @return whether or not this entity contains a built-in activator */ - bool GetSelfActivator(); + [[nodiscard]] bool GetSelfActivator() const noexcept; /** * Sets whether or not this entity contains a built-in activator. If set to false this will spawn activators on * each new quickbuild. * @param value whether or not this entity contains a built-in activator */ - void SetSelfActivator(bool value); + void SetSelfActivator(const bool value) noexcept; /** * Currently unused */ - std::vector GetCustomModules(); + [[nodiscard]] std::vector GetCustomModules() const noexcept; /** * Currently unused */ - void SetCustomModules(std::vector value); + void SetCustomModules(const std::vector& value) noexcept; /** * Returns the activity ID for participating in this quickbuild * @return the activity ID for participating in this quickbuild */ - int GetActivityId(); + [[nodiscard]] int32_t GetActivityId() const noexcept; /** * Sets the activity ID for participating in this quickbuild * @param value the activity ID to set */ - void SetActivityId(int value); + void SetActivityId(const int32_t value) noexcept; /** * Currently unused */ - int GetPostImaginationCost(); + [[nodiscard]] int32_t GetPostImaginationCost() const noexcept; /** * Currently unused */ - void SetPostImaginationCost(int value); + void SetPostImaginationCost(const int32_t value) noexcept; /** * Returns the time it takes for an incomplete quickbuild to be smashed automatically * @return the time it takes for an incomplete quickbuild to be smashed automatically */ - float GetTimeBeforeSmash(); + [[nodiscard]] float GetTimeBeforeSmash() const noexcept; /** * Sets the time it takes for an incomplete quickbuild to be smashed automatically * @param value the time to set */ - void SetTimeBeforeSmash(float value); + void SetTimeBeforeSmash(const float value) noexcept; /** * Returns the current quickbuild state * @return the current quickbuild state */ - eQuickBuildState GetState(); + [[nodiscard]] eQuickBuildState GetState() const noexcept; /** * Returns the player that is currently building this quickbuild * @return the player that is currently building this quickbuild */ - Entity* GetBuilder() const; + [[nodiscard]] Entity* GetBuilder() const; /** * Returns whether or not the player is repositioned when initiating the quickbuild * @return whether or not the player is repositioned when initiating the quickbuild */ - bool GetRepositionPlayer() const; + [[nodiscard]] bool GetRepositionPlayer() const noexcept; /** * Sets whether or not the player is repositioned when initiating the quickbuild * @param value whether or not the player is repositioned when initiating the quickbuild */ - void SetRepositionPlayer(bool value); + void SetRepositionPlayer(const bool value) noexcept; /** * Adds a callback that is called when the quickbuild is completed @@ -209,7 +209,7 @@ public: * Resets the quickbuild * @param failed whether or not the player failed to complete the quickbuild, triggers an extra animation */ - void ResetQuickBuild(bool failed); + void ResetQuickBuild(const bool failed); /** * Cancels the quickbuild if it wasn't completed @@ -217,7 +217,7 @@ public: * @param failReason the reason the quickbuild was cancelled * @param skipChecks whether or not to skip the check for the quickbuild not being completed */ - void CancelQuickBuild(Entity* builder, eQuickBuildFailReason failReason, bool skipChecks = false); + void CancelQuickBuild(Entity* const builder, const eQuickBuildFailReason failReason, const bool skipChecks = false); private: /** * Whether or not the quickbuild state has been changed since we last serialized it. @@ -242,7 +242,7 @@ private: /** * The position that the quickbuild activator is spawned at */ - NiPoint3 m_ActivatorPosition = NiPoint3::ZERO; + NiPoint3 m_ActivatorPosition = NiPoint3Constant::ZERO; /** * The entity that represents the quickbuild activator @@ -287,7 +287,7 @@ private: /** * The imagination that's deducted when completing the quickbuild */ - int m_TakeImagination = 0; + int32_t m_TakeImagination = 0; /** * Currently unused @@ -302,17 +302,17 @@ private: /** * Currently unused */ - std::vector m_CustomModules{}; + std::vector m_CustomModules{}; /** * The activity ID that players partake in when doing this quickbuild */ - int m_ActivityId = 0; + int32_t m_ActivityId = 0; /** * Currently unused */ - int m_PostImaginationCost = 0; + int32_t m_PostImaginationCost = 0; /** * The time it takes for the quickbuild to reset when it's not completed yet @@ -327,7 +327,7 @@ private: /** * The amount of imagination that was drained when building this quickbuild */ - int m_DrainedImagination = 0; + int32_t m_DrainedImagination = 0; /** * Whether to reposition the player or not when building @@ -337,7 +337,7 @@ private: /** * Currently unused */ - float m_SoftTimer = 0; + int32_t m_SoftTimer = 0; /** * The ID of the entity that's currently building the quickbuild @@ -353,13 +353,13 @@ private: * Starts the quickbuild for a certain entity * @param user the entity to start the quickbuild */ - void StartQuickBuild(Entity* user); + void StartQuickBuild(Entity* const user); /** * Completes the quickbuild for an entity, dropping loot and despawning the activator * @param user the entity that completed the quickbuild */ - void CompleteQuickBuild(Entity* user); + void CompleteQuickBuild(Entity* const user); }; #endif // QUICKBUILDCOMPONENT_H diff --git a/dGame/dComponents/RacingControlComponent.cpp b/dGame/dComponents/RacingControlComponent.cpp index 5b93dbb7..f3772aad 100644 --- a/dGame/dComponents/RacingControlComponent.cpp +++ b/dGame/dComponents/RacingControlComponent.cpp @@ -12,7 +12,6 @@ #include "Item.h" #include "MissionComponent.h" #include "ModuleAssemblyComponent.h" -#include "Player.h" #include "PossessableComponent.h" #include "PossessorComponent.h" #include "eRacingTaskParam.h" @@ -54,7 +53,7 @@ RacingControlComponent::RacingControlComponent(Entity* parent) if (Game::zoneManager->CheckIfAccessibleZone((worldID / 10) * 10)) m_MainWorld = (worldID / 10) * 10; m_ActivityID = 42; - CDActivitiesTable* activitiesTable = CDClientManager::Instance().GetTable(); + CDActivitiesTable* activitiesTable = CDClientManager::GetTable(); std::vector activities = activitiesTable->Query([=](CDActivities entry) {return (entry.instanceMapID == worldID); }); for (CDActivities activity : activities) m_ActivityID = activity.ActivityID; } @@ -71,10 +70,8 @@ void RacingControlComponent::OnPlayerLoaded(Entity* player) { // If the race has already started, send the player back to the main world. if (m_Loaded || !vehicle) { - auto* playerInstance = dynamic_cast(player); - if (playerInstance) { - playerInstance->SendToZone(m_MainWorld); - } + auto* characterComponent = player->GetComponent(); + if (characterComponent) characterComponent->SendToZone(m_MainWorld); return; } @@ -105,10 +102,11 @@ void RacingControlComponent::LoadPlayerVehicle(Entity* player, if (item == nullptr) { LOG("Failed to find item"); - auto* playerInstance = dynamic_cast(player); - if (playerInstance) { + auto* characterComponent = player->GetComponent(); + + if (characterComponent) { m_LoadedPlayers--; - playerInstance->SendToZone(m_MainWorld); + characterComponent->SendToZone(m_MainWorld); } return; @@ -120,8 +118,8 @@ void RacingControlComponent::LoadPlayerVehicle(Entity* player, GeneralUtils::UTF16ToWTF8(m_PathName)); auto spawnPointEntities = Game::entityManager->GetEntitiesByLOT(4843); - auto startPosition = NiPoint3::ZERO; - auto startRotation = NiQuaternion::IDENTITY; + auto startPosition = NiPoint3Constant::ZERO; + auto startRotation = NiQuaternionConstant::IDENTITY; const std::string placementAsString = std::to_string(positionNumber); for (auto entity : spawnPointEntities) { if (!entity) continue; @@ -427,9 +425,9 @@ void RacingControlComponent::HandleMessageBoxResponse(Entity* player, int32_t bu m_Parent->GetObjectID(), 3, 0, LWOOBJID_EMPTY, u"", player->GetObjectID(), UNASSIGNED_SYSTEM_ADDRESS); - auto* playerInstance = dynamic_cast(player); + auto* characterComponent = player->GetComponent(); - playerInstance->SendToZone(m_MainWorld); + if (characterComponent) characterComponent->SendToZone(m_MainWorld); vehicle->Kill(); } @@ -561,9 +559,9 @@ void RacingControlComponent::Update(float deltaTime) { continue; } - auto* playerInstance = dynamic_cast(playerEntity); + auto* characterComponent = playerEntity->GetComponent(); - playerInstance->SendToZone(m_MainWorld); + if (characterComponent) characterComponent->SendToZone(m_MainWorld); } m_LobbyPlayers.clear(); @@ -623,9 +621,9 @@ void RacingControlComponent::Update(float deltaTime) { continue; } - auto* playerInstance = dynamic_cast(playerEntity); + auto* characterComponent = playerEntity->GetComponent(); - playerInstance->SendToZone(m_MainWorld); + if (characterComponent) characterComponent->SendToZone(m_MainWorld); } return; @@ -819,7 +817,7 @@ void RacingControlComponent::Update(float deltaTime) { // Some offset up to make they don't fall through the terrain on a // respawn, seems to fix itself to the track anyhow - player.respawnPosition = position + NiPoint3::UNIT_Y * 5; + player.respawnPosition = position + NiPoint3Constant::UNIT_Y * 5; player.respawnRotation = vehicle->GetRotation(); player.respawnIndex = respawnIndex; diff --git a/dGame/dComponents/RacingControlComponent.h b/dGame/dComponents/RacingControlComponent.h index 47341aae..4a7d387f 100644 --- a/dGame/dComponents/RacingControlComponent.h +++ b/dGame/dComponents/RacingControlComponent.h @@ -103,9 +103,9 @@ struct RacingPlayerInfo { /** * Component that's attached to a manager entity in each race zone that loads player vehicles, keep scores, etc. */ -class RacingControlComponent : public Component { +class RacingControlComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::RACING_CONTROL; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::RACING_CONTROL; RacingControlComponent(Entity* parentEntity); ~RacingControlComponent(); diff --git a/dGame/dComponents/RacingSoundTriggerComponent.h b/dGame/dComponents/RacingSoundTriggerComponent.h index 0f79c67f..140bbe20 100644 --- a/dGame/dComponents/RacingSoundTriggerComponent.h +++ b/dGame/dComponents/RacingSoundTriggerComponent.h @@ -8,7 +8,7 @@ class Entity; class RacingSoundTriggerComponent : public SoundTriggerComponent { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::RACING_SOUND_TRIGGER; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::RACING_SOUND_TRIGGER; RacingSoundTriggerComponent(Entity* parent) : SoundTriggerComponent(parent){}; }; diff --git a/dGame/dComponents/RacingStatsComponent.h b/dGame/dComponents/RacingStatsComponent.h index 9349ce49..ad1b35bf 100644 --- a/dGame/dComponents/RacingStatsComponent.h +++ b/dGame/dComponents/RacingStatsComponent.h @@ -6,7 +6,7 @@ class RacingStatsComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::RACING_STATS; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::RACING_STATS; RacingStatsComponent(Entity* parent) : Component(parent) {} }; diff --git a/dGame/dComponents/RailActivatorComponent.cpp b/dGame/dComponents/RailActivatorComponent.cpp index e0eb035a..f269da49 100644 --- a/dGame/dComponents/RailActivatorComponent.cpp +++ b/dGame/dComponents/RailActivatorComponent.cpp @@ -13,7 +13,7 @@ RailActivatorComponent::RailActivatorComponent(Entity* parent, int32_t componentID) : Component(parent) { m_ComponentID = componentID; - const auto tableData = CDClientManager::Instance().GetTable()->GetEntryByID(componentID);; + const auto tableData = CDClientManager::GetTable()->GetEntryByID(componentID);; m_Path = parent->GetVar(u"rail_path"); m_PathDirection = parent->GetVar(u"rail_path_direction"); diff --git a/dGame/dComponents/RailActivatorComponent.h b/dGame/dComponents/RailActivatorComponent.h index 28b25073..015f36b7 100644 --- a/dGame/dComponents/RailActivatorComponent.h +++ b/dGame/dComponents/RailActivatorComponent.h @@ -15,7 +15,7 @@ public: explicit RailActivatorComponent(Entity* parent, int32_t componentID); ~RailActivatorComponent() override; - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::RAIL_ACTIVATOR; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::RAIL_ACTIVATOR; /** * Handles the OnUse event from some entity, initiates the rail movement diff --git a/dGame/dComponents/RenderComponent.cpp b/dGame/dComponents/RenderComponent.cpp index f1b8ea0c..118e4847 100644 --- a/dGame/dComponents/RenderComponent.cpp +++ b/dGame/dComponents/RenderComponent.cpp @@ -1,11 +1,12 @@ #include "RenderComponent.h" +#include #include #include +#include #include #include "Entity.h" -#include "PacketUtils.h" #include "CDClientManager.h" #include "GameMessages.h" @@ -15,8 +16,7 @@ std::unordered_map RenderComponent::m_DurationCache{}; -RenderComponent::RenderComponent(Entity* parent, int32_t componentId): Component(parent) { - m_Effects = std::vector(); +RenderComponent::RenderComponent(Entity* const parentEntity, const int32_t componentId) : Component{ parentEntity } { m_LastAnimationName = ""; if (componentId == -1) return; @@ -27,116 +27,69 @@ RenderComponent::RenderComponent(Entity* parent, int32_t componentId): Component if (!result.eof()) { auto animationGroupIDs = std::string(result.getStringField("animationGroupIDs", "")); if (!animationGroupIDs.empty()) { - auto* animationsTable = CDClientManager::Instance().GetTable(); + auto* animationsTable = CDClientManager::GetTable(); auto groupIdsSplit = GeneralUtils::SplitString(animationGroupIDs, ','); for (auto& groupId : groupIdsSplit) { - int32_t groupIdInt; - if (!GeneralUtils::TryParse(groupId, groupIdInt)) { + const auto groupIdInt = GeneralUtils::TryParse(groupId); + + if (!groupIdInt) { LOG("bad animation group Id %s", groupId.c_str()); continue; } - m_animationGroupIds.push_back(groupIdInt); - animationsTable->CacheAnimationGroup(groupIdInt); + + m_animationGroupIds.push_back(groupIdInt.value()); + animationsTable->CacheAnimationGroup(groupIdInt.value()); } } } result.finalize(); } -RenderComponent::~RenderComponent() { - for (Effect* eff : m_Effects) { - if (eff) { - delete eff; - eff = nullptr; - } - } - - m_Effects.clear(); -} - void RenderComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate) { if (!bIsInitialUpdate) return; outBitStream->Write(m_Effects.size()); - for (Effect* eff : m_Effects) { - // we still need to write 0 as the size for name if it is a nullptr - if (!eff) { - outBitStream->Write(0); - continue; - } - - outBitStream->Write(eff->name.size()); + for (auto& eff : m_Effects) { + outBitStream->Write(eff.name.size()); // if there is no name, then we don't write anything else - if (eff->name.empty()) continue; + if (eff.name.empty()) continue; - for (const auto& value : eff->name) outBitStream->Write(value); + for (const auto& value : eff.name) outBitStream->Write(value); - outBitStream->Write(eff->effectID); + outBitStream->Write(eff.effectID); - outBitStream->Write(eff->type.size()); - for (const auto& value : eff->type) outBitStream->Write(value); + outBitStream->Write(eff.type.size()); + for (const auto& value : eff.type) outBitStream->Write(value); - outBitStream->Write(eff->priority); - outBitStream->Write(eff->secondary); + outBitStream->Write(eff.priority); + outBitStream->Write(eff.secondary); } } -Effect* RenderComponent::AddEffect(const int32_t effectId, const std::string& name, const std::u16string& type, const float priority) { - auto* eff = new Effect(); - - eff->effectID = effectId; - eff->name = name; - eff->type = type; - eff->priority = priority; - m_Effects.push_back(eff); - - return eff; +Effect& RenderComponent::AddEffect(const int32_t effectId, const std::string& name, const std::u16string& type, const float priority) { + return m_Effects.emplace_back(effectId, name, type, priority); } void RenderComponent::RemoveEffect(const std::string& name) { - uint32_t index = -1; + if (m_Effects.empty()) return; - for (auto i = 0u; i < m_Effects.size(); ++i) { - auto* eff = m_Effects[i]; + const auto effectToRemove = std::ranges::find_if(m_Effects, [&name](auto&& effect) { return effect.name == name; }); + if (effectToRemove == m_Effects.end()) return; // Return early if effect is not present - if (eff->name == name) { - index = i; - - delete eff; - - break; - } - } - - if (index == -1) { - return; - } - - m_Effects.erase(m_Effects.begin() + index); + const auto lastEffect = m_Effects.rbegin(); + *effectToRemove = std::move(*lastEffect); // Move-overwrite + m_Effects.pop_back(); } -void RenderComponent::Update(const float deltaTime) { - std::vector dead; +void RenderComponent::Update(const float deltaTime) { + for (auto& effect : m_Effects) { + if (effect.time == 0) continue; // Skip persistent effects - for (auto* effect : m_Effects) { - if (effect->time == 0) { - continue; // Skip persistent effects - } + const auto result = effect.time - deltaTime; + if (result <= 0) continue; - const auto result = effect->time - deltaTime; - - if (result <= 0) { - dead.push_back(effect); - - continue; - } - - effect->time = result; - } - - for (auto* effect : dead) { - // StopEffect(effect->name); + effect.time = result; } } @@ -145,12 +98,12 @@ void RenderComponent::PlayEffect(const int32_t effectId, const std::u16string& e GameMessages::SendPlayFXEffect(m_Parent, effectId, effectType, name, secondary, priority, scale, serialize); - auto* effect = AddEffect(effectId, name, effectType, priority); + auto& effect = AddEffect(effectId, name, effectType, priority); const auto& pair = m_DurationCache.find(effectId); if (pair != m_DurationCache.end()) { - effect->time = pair->second; + effect.time = pair->second; return; } @@ -169,16 +122,16 @@ void RenderComponent::PlayEffect(const int32_t effectId, const std::u16string& e m_DurationCache[effectId] = 0; - effect->time = 0; // Persistent effect + effect.time = 0; // Persistent effect return; } - effect->time = static_cast(result.getFloatField(0)); + effect.time = static_cast(result.getFloatField(0)); result.finalize(); - m_DurationCache[effectId] = effect->time; + m_DurationCache[effectId] = effect.time; } void RenderComponent::StopEffect(const std::string& name, const bool killImmediate) { @@ -187,11 +140,6 @@ void RenderComponent::StopEffect(const std::string& name, const bool killImmedia RemoveEffect(name); } -std::vector& RenderComponent::GetEffects() { - return m_Effects; -} - - float RenderComponent::PlayAnimation(Entity* self, const std::u16string& animation, float priority, float scale) { if (!self) return 0.0f; return RenderComponent::PlayAnimation(self, GeneralUtils::UTF16ToWTF8(animation), priority, scale); @@ -219,13 +167,12 @@ float RenderComponent::DoAnimation(Entity* self, const std::string& animation, b auto* renderComponent = self->GetComponent(); if (!renderComponent) return returnlength; - auto* animationsTable = CDClientManager::Instance().GetTable(); + auto* animationsTable = CDClientManager::GetTable(); for (auto& groupId : renderComponent->m_animationGroupIds) { auto animationGroup = animationsTable->GetAnimation(animation, renderComponent->GetLastAnimationName(), groupId); - if (animationGroup.FoundData()) { - auto data = animationGroup.Data(); - renderComponent->SetLastAnimationName(data.animation_name); - returnlength = data.animation_length; + if (animationGroup) { + renderComponent->SetLastAnimationName(animationGroup->animation_name); + returnlength = animationGroup->animation_length; } } if (sendAnimation) GameMessages::SendPlayAnimation(self, GeneralUtils::ASCIIToUTF16(animation), priority, scale); diff --git a/dGame/dComponents/RenderComponent.h b/dGame/dComponents/RenderComponent.h index 74f39625..87b210d7 100644 --- a/dGame/dComponents/RenderComponent.h +++ b/dGame/dComponents/RenderComponent.h @@ -17,7 +17,12 @@ class Entity; * here. */ struct Effect { - Effect() { priority = 1.0f; } + explicit Effect(const int32_t effectID, const std::string& name, const std::u16string& type, const float priority = 1.0f) noexcept + : effectID{ effectID } + , name{ name } + , type{ type } + , priority{ priority } { + } /** * The ID of the effect @@ -54,12 +59,11 @@ struct Effect { * Determines that a component should be visibly rendered into the world, most entities have this. This component * also handles effects that play for entities. */ -class RenderComponent : public Component { +class RenderComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::RENDER; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::RENDER; - RenderComponent(Entity* entity, int32_t componentId = -1); - ~RenderComponent() override; + RenderComponent(Entity* const parentEntity, const int32_t componentId = -1); void Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate) override; void Update(float deltaTime) override; @@ -72,7 +76,7 @@ public: * @param priority the priority of the effect * @return if successful, the effect that was created */ - Effect* AddEffect(int32_t effectId, const std::string& name, const std::u16string& type, const float priority); + [[maybe_unused]] Effect& AddEffect(const int32_t effectId, const std::string& name, const std::u16string& type, const float priority); /** * Removes an effect for this entity @@ -99,12 +103,6 @@ public: */ void StopEffect(const std::string& name, bool killImmediate = true); - /** - * Returns the list of currently active effects - * @return - */ - std::vector& GetEffects(); - /** * Verifies that an animation can be played on this entity by checking * if it has the animation assigned to its group. If it does, the animation is echo'd @@ -125,10 +123,10 @@ public: static float PlayAnimation(Entity* self, const std::u16string& animation, float priority = 0.0f, float scale = 1.0f); static float PlayAnimation(Entity* self, const std::string& animation, float priority = 0.0f, float scale = 1.0f); - static float GetAnimationTime(Entity* self, const std::string& animation); - static float GetAnimationTime(Entity* self, const std::u16string& animation); + [[nodiscard]] static float GetAnimationTime(Entity* self, const std::string& animation); + [[nodiscard]] static float GetAnimationTime(Entity* self, const std::u16string& animation); - const std::string& GetLastAnimationName() const { return m_LastAnimationName; }; + [[nodiscard]] const std::string& GetLastAnimationName() const { return m_LastAnimationName; }; void SetLastAnimationName(const std::string& name) { m_LastAnimationName = name; }; private: @@ -136,7 +134,7 @@ private: /** * List of currently active effects */ - std::vector m_Effects; + std::vector m_Effects; std::vector m_animationGroupIds; diff --git a/dGame/dComponents/RigidbodyPhantomPhysicsComponent.h b/dGame/dComponents/RigidbodyPhantomPhysicsComponent.h index bc60c38e..082dd1e7 100644 --- a/dGame/dComponents/RigidbodyPhantomPhysicsComponent.h +++ b/dGame/dComponents/RigidbodyPhantomPhysicsComponent.h @@ -19,7 +19,7 @@ */ class RigidbodyPhantomPhysicsComponent : public PhysicsComponent { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::RIGID_BODY_PHANTOM_PHYSICS; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::RIGID_BODY_PHANTOM_PHYSICS; RigidbodyPhantomPhysicsComponent(Entity* parent); diff --git a/dGame/dComponents/RocketLaunchpadControlComponent.h b/dGame/dComponents/RocketLaunchpadControlComponent.h index 06d97cd3..03d2f141 100644 --- a/dGame/dComponents/RocketLaunchpadControlComponent.h +++ b/dGame/dComponents/RocketLaunchpadControlComponent.h @@ -16,9 +16,9 @@ class PreconditionExpression; /** * Component that handles rocket launchpads that can be interacted with to travel to other worlds. */ -class RocketLaunchpadControlComponent : public Component { +class RocketLaunchpadControlComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::ROCKET_LAUNCH; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::ROCKET_LAUNCH; RocketLaunchpadControlComponent(Entity* parent, int rocketId); ~RocketLaunchpadControlComponent() override; diff --git a/dGame/dComponents/ScriptedActivityComponent.h b/dGame/dComponents/ScriptedActivityComponent.h index 79a9593d..308a0a86 100644 --- a/dGame/dComponents/ScriptedActivityComponent.h +++ b/dGame/dComponents/ScriptedActivityComponent.h @@ -6,9 +6,9 @@ class Entity; -class ScriptedActivityComponent : public ActivityComponent { +class ScriptedActivityComponent final : public ActivityComponent { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::SCRIPTED_ACTIVITY; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::SCRIPTED_ACTIVITY; ScriptedActivityComponent(Entity* parent, int activityID) : ActivityComponent(parent, activityID){}; }; diff --git a/dGame/dComponents/ShootingGalleryComponent.h b/dGame/dComponents/ShootingGalleryComponent.h index 4024e99a..c4b8fea2 100644 --- a/dGame/dComponents/ShootingGalleryComponent.h +++ b/dGame/dComponents/ShootingGalleryComponent.h @@ -71,9 +71,9 @@ struct StaticShootingGalleryParams { * A very ancient component that was used to guide shooting galleries, it's still kind of used but a lot of logic is * also in the related scripts. */ -class ShootingGalleryComponent : public Component { +class ShootingGalleryComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::SHOOTING_GALLERY; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::SHOOTING_GALLERY; explicit ShootingGalleryComponent(Entity* parent); ~ShootingGalleryComponent(); diff --git a/dGame/dComponents/SimplePhysicsComponent.h b/dGame/dComponents/SimplePhysicsComponent.h index 752fef0b..8f92dd95 100644 --- a/dGame/dComponents/SimplePhysicsComponent.h +++ b/dGame/dComponents/SimplePhysicsComponent.h @@ -28,7 +28,7 @@ enum class eClimbableType : int32_t { */ class SimplePhysicsComponent : public PhysicsComponent { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::SIMPLE_PHYSICS; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::SIMPLE_PHYSICS; SimplePhysicsComponent(Entity* parent, uint32_t componentID); ~SimplePhysicsComponent() override; @@ -87,12 +87,12 @@ private: /** * The current velocity of the entity */ - NiPoint3 m_Velocity = NiPoint3::ZERO; + NiPoint3 m_Velocity = NiPoint3Constant::ZERO; /** * The current angular velocity of the entity */ - NiPoint3 m_AngularVelocity = NiPoint3::ZERO; + NiPoint3 m_AngularVelocity = NiPoint3Constant::ZERO; /** * Whether or not the velocity has changed diff --git a/dGame/dComponents/SkillComponent.cpp b/dGame/dComponents/SkillComponent.cpp index b65c7d21..c43813c1 100644 --- a/dGame/dComponents/SkillComponent.cpp +++ b/dGame/dComponents/SkillComponent.cpp @@ -234,7 +234,7 @@ bool SkillComponent::CastSkill(const uint32_t skillId, LWOOBJID target, const LW // if it's not in the cache look it up and cache it if (pair == m_skillBehaviorCache.end()) { - auto skillTable = CDClientManager::Instance().GetTable(); + auto skillTable = CDClientManager::GetTable(); behaviorId = skillTable->GetSkillByID(skillId).behaviorID; m_skillBehaviorCache.insert_or_assign(skillId, behaviorId); } else { diff --git a/dGame/dComponents/SkillComponent.h b/dGame/dComponents/SkillComponent.h index 2d8628ef..530c2a25 100644 --- a/dGame/dComponents/SkillComponent.h +++ b/dGame/dComponents/SkillComponent.h @@ -1,6 +1,6 @@ /* * Darkflame Universe - * Copyright 2018 + * Copyright 2024 */ #ifndef SKILLCOMPONENT_H @@ -55,11 +55,11 @@ struct SkillExecutionResult { * * Skills are a built up by a tree of behaviors. See dGame/dBehaviors/ for a list of behaviors. * - * This system is very conveluted and still has a lot of unknowns. + * This system is very convoluted and still has a lot of unknowns. */ -class SkillComponent : public Component { +class SkillComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::SKILL; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::SKILL; explicit SkillComponent(Entity* parent); ~SkillComponent() override; diff --git a/dGame/dComponents/SoundTriggerComponent.h b/dGame/dComponents/SoundTriggerComponent.h index 56c71770..48366017 100644 --- a/dGame/dComponents/SoundTriggerComponent.h +++ b/dGame/dComponents/SoundTriggerComponent.h @@ -1,4 +1,5 @@ #pragma once + #include "dCommonVars.h" #include "Entity.h" #include "GUID.h" @@ -43,7 +44,7 @@ struct GUIDResults{ void Serialize(RakNet::BitStream* outBitStream); }; -struct MixerProgram{ +struct MixerProgram { std::string name; uint32_t result; @@ -58,7 +59,7 @@ struct MixerProgram{ class SoundTriggerComponent : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::SOUND_TRIGGER; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::SOUND_TRIGGER; explicit SoundTriggerComponent(Entity* parent); void Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate) override; void ActivateMusicCue(const std::string& name, float bordemTime = -1.0); @@ -66,11 +67,11 @@ public: private: - std::vector m_MusicCues = {}; - std::vector m_MusicParameters = {}; - std::vector m_2DAmbientSounds = {}; - std::vector m_3DAmbientSounds = {}; - std::vector m_MixerPrograms = {}; + std::vector m_MusicCues; + std::vector m_MusicParameters; + std::vector m_2DAmbientSounds; + std::vector m_3DAmbientSounds; + std::vector m_MixerPrograms; bool m_Dirty = false; }; diff --git a/dGame/dComponents/SwitchComponent.h b/dGame/dComponents/SwitchComponent.h index 7f2c3498..f732a8c1 100644 --- a/dGame/dComponents/SwitchComponent.h +++ b/dGame/dComponents/SwitchComponent.h @@ -14,9 +14,9 @@ /** * A component for switches in game, including pet triggered switches. */ -class SwitchComponent : public Component { +class SwitchComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::SWITCH; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::SWITCH; SwitchComponent(Entity* parent); ~SwitchComponent() override; diff --git a/dGame/dComponents/TriggerComponent.cpp b/dGame/dComponents/TriggerComponent.cpp index d30353ab..5d4415f8 100644 --- a/dGame/dComponents/TriggerComponent.cpp +++ b/dGame/dComponents/TriggerComponent.cpp @@ -9,10 +9,10 @@ #include "ControllablePhysicsComponent.h" #include "MissionComponent.h" #include "PhantomPhysicsComponent.h" -#include "Player.h" #include "QuickBuildComponent.h" #include "SkillComponent.h" #include "eEndBehavior.h" +#include "PlayerManager.h" TriggerComponent::TriggerComponent(Entity* parent, const std::string triggerInfo): Component(parent) { @@ -21,10 +21,8 @@ TriggerComponent::TriggerComponent(Entity* parent, const std::string triggerInfo std::vector tokens = GeneralUtils::SplitString(triggerInfo, ':'); - uint32_t sceneID; - GeneralUtils::TryParse(tokens.at(0), sceneID); - uint32_t triggerID; - GeneralUtils::TryParse(tokens.at(1), triggerID); + const auto sceneID = GeneralUtils::TryParse(tokens.at(0)).value_or(0); + const auto triggerID = GeneralUtils::TryParse(tokens.at(1)).value_or(0); m_Trigger = Game::zoneManager->GetZone()->GetTrigger(sceneID, triggerID); @@ -175,7 +173,7 @@ std::vector TriggerComponent::GatherTargets(LUTriggers::Command* comman } } else if (command->target == "objGroup") entities = Game::entityManager->GetEntitiesInGroup(command->targetName); else if (command->target == "allPlayers") { - for (auto* player : Player::GetAllPlayers()) { + for (auto* player : PlayerManager::GetAllPlayers()) { entities.push_back(player); } } else if (command->target == "allNPCs") { /*UNUSED*/ } @@ -190,9 +188,8 @@ void TriggerComponent::HandleFireEvent(Entity* targetEntity, std::string args) { } void TriggerComponent::HandleDestroyObject(Entity* targetEntity, std::string args){ - uint32_t killType; - GeneralUtils::TryParse(args, killType); - targetEntity->Smash(m_Parent->GetObjectID(), static_cast(killType)); + const eKillType killType = GeneralUtils::TryParse(args).value_or(eKillType::VIOLENT); + targetEntity->Smash(m_Parent->GetObjectID(), killType); } void TriggerComponent::HandleToggleTrigger(Entity* targetEntity, std::string args){ @@ -216,9 +213,8 @@ void TriggerComponent::HandleResetRebuild(Entity* targetEntity, std::string args void TriggerComponent::HandleMoveObject(Entity* targetEntity, std::vector argArray){ if (argArray.size() <= 2) return; - auto position = targetEntity->GetPosition(); - NiPoint3 offset = NiPoint3::ZERO; - GeneralUtils::TryParse(argArray.at(0), argArray.at(1), argArray.at(2), offset); + NiPoint3 position = targetEntity->GetPosition(); + const NiPoint3 offset = GeneralUtils::TryParse(argArray).value_or(NiPoint3Constant::ZERO); position += offset; targetEntity->SetPosition(position); @@ -227,8 +223,7 @@ void TriggerComponent::HandleMoveObject(Entity* targetEntity, std::vector argArray){ if (argArray.size() <= 2) return; - NiPoint3 vector = NiPoint3::ZERO; - GeneralUtils::TryParse(argArray.at(0), argArray.at(1), argArray.at(2), vector); + const NiPoint3 vector = GeneralUtils::TryParse(argArray).value_or(NiPoint3Constant::ZERO); NiQuaternion rotation = NiQuaternion::FromEulerAngles(vector); targetEntity->SetRotation(rotation); @@ -245,8 +240,7 @@ void TriggerComponent::HandlePushObject(Entity* targetEntity, std::vectorSetPhysicsEffectActive(true); phantomPhysicsComponent->SetEffectType(ePhysicsEffectType::PUSH); phantomPhysicsComponent->SetDirectionalMultiplier(1); - NiPoint3 direction = NiPoint3::ZERO; - GeneralUtils::TryParse(argArray.at(0), argArray.at(1), argArray.at(2), direction); + const NiPoint3 direction = GeneralUtils::TryParse(argArray).value_or(NiPoint3Constant::ZERO); phantomPhysicsComponent->SetDirection(direction); Game::entityManager->SerializeEntity(m_Parent); @@ -259,8 +253,8 @@ void TriggerComponent::HandleRepelObject(Entity* targetEntity, std::string args) LOG_DEBUG("Phantom Physics component not found!"); return; } - float forceMultiplier; - GeneralUtils::TryParse(args, forceMultiplier); + const float forceMultiplier = GeneralUtils::TryParse(args).value_or(1.0f); + phantomPhysicsComponent->SetPhysicsEffectActive(true); phantomPhysicsComponent->SetEffectType(ePhysicsEffectType::REPULSE); phantomPhysicsComponent->SetDirectionalMultiplier(forceMultiplier); @@ -279,11 +273,10 @@ void TriggerComponent::HandleRepelObject(Entity* targetEntity, std::string args) void TriggerComponent::HandleSetTimer(Entity* targetEntity, std::vector argArray){ if (argArray.size() != 2) { - LOG_DEBUG("Not ehought variables!"); + LOG_DEBUG("Not enough variables!"); return; } - float time = 0.0; - GeneralUtils::TryParse(argArray.at(1), time); + const float time = GeneralUtils::TryParse(argArray.at(1)).value_or(0.0f); m_Parent->AddTimer(argArray.at(0), time); } @@ -299,7 +292,7 @@ void TriggerComponent::HandlePlayCinematic(Entity* targetEntity, std::vector= 2) { - GeneralUtils::TryParse(argArray.at(1), leadIn); + leadIn = GeneralUtils::TryParse(argArray.at(1)).value_or(leadIn); if (argArray.size() >= 3 && argArray.at(2) == "wait") { wait = eEndBehavior::WAIT; if (argArray.size() >= 4 && argArray.at(3) == "unlock") { @@ -344,12 +337,16 @@ void TriggerComponent::HandleUpdateMission(Entity* targetEntity, std::vector argArray) { if (argArray.size() < 3) return; - int32_t effectID = 0; - if (!GeneralUtils::TryParse(argArray.at(1), effectID)) return; + const auto effectID = GeneralUtils::TryParse(argArray.at(1)); + if (!effectID) return; std::u16string effectType = GeneralUtils::UTF8ToUTF16(argArray.at(2)); + float priority = 1; - if (argArray.size() == 4) GeneralUtils::TryParse(argArray.at(3), priority); - GameMessages::SendPlayFXEffect(targetEntity, effectID, effectType, argArray.at(0), LWOOBJID_EMPTY, priority); + if (argArray.size() == 4) { + priority = GeneralUtils::TryParse(argArray.at(3)).value_or(priority); + } + + GameMessages::SendPlayFXEffect(targetEntity, effectID.value(), effectType, argArray.at(0), LWOOBJID_EMPTY, priority); } void TriggerComponent::HandleCastSkill(Entity* targetEntity, std::string args){ @@ -358,8 +355,7 @@ void TriggerComponent::HandleCastSkill(Entity* targetEntity, std::string args){ LOG_DEBUG("Skill component not found!"); return; } - uint32_t skillId; - GeneralUtils::TryParse(args, skillId); + const uint32_t skillId = GeneralUtils::TryParse(args).value_or(0); skillComponent->CastSkill(skillId, targetEntity->GetObjectID()); } @@ -381,17 +377,16 @@ void TriggerComponent::HandleSetPhysicsVolumeEffect(Entity* targetEntity, std::v phantomPhysicsComponent->SetEffectType(effectType); phantomPhysicsComponent->SetDirectionalMultiplier(std::stof(argArray.at(1))); if (argArray.size() > 4) { - NiPoint3 direction = NiPoint3::ZERO; - GeneralUtils::TryParse(argArray.at(2), argArray.at(3), argArray.at(4), direction); + const NiPoint3 direction = + GeneralUtils::TryParse(argArray.at(2), argArray.at(3), argArray.at(4)).value_or(NiPoint3Constant::ZERO); + phantomPhysicsComponent->SetDirection(direction); } if (argArray.size() > 5) { - uint32_t min; - GeneralUtils::TryParse(argArray.at(6), min); + const uint32_t min = GeneralUtils::TryParse(argArray.at(6)).value_or(0); phantomPhysicsComponent->SetMin(min); - uint32_t max; - GeneralUtils::TryParse(argArray.at(7), max); + const uint32_t max = GeneralUtils::TryParse(argArray.at(7)).value_or(0); phantomPhysicsComponent->SetMax(max); } diff --git a/dGame/dComponents/TriggerComponent.h b/dGame/dComponents/TriggerComponent.h index 90ecc52c..5610f24a 100644 --- a/dGame/dComponents/TriggerComponent.h +++ b/dGame/dComponents/TriggerComponent.h @@ -5,9 +5,9 @@ #include "LUTriggers.h" #include "eReplicaComponentType.h" -class TriggerComponent : public Component { +class TriggerComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::TRIGGER; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::TRIGGER; explicit TriggerComponent(Entity* parent, const std::string triggerInfo); diff --git a/dGame/dComponents/VendorComponent.cpp b/dGame/dComponents/VendorComponent.cpp index dfea33df..afa3d013 100644 --- a/dGame/dComponents/VendorComponent.cpp +++ b/dGame/dComponents/VendorComponent.cpp @@ -41,14 +41,14 @@ void VendorComponent::RefreshInventory(bool isCreation) { return; } - auto* lootMatrixTable = CDClientManager::Instance().GetTable(); + auto* lootMatrixTable = CDClientManager::GetTable(); const auto& lootMatrices = lootMatrixTable->GetMatrix(m_LootMatrixID); if (lootMatrices.empty()) return; - auto* lootTableTable = CDClientManager::Instance().GetTable(); - auto* itemComponentTable = CDClientManager::Instance().GetTable(); - auto* compRegistryTable = CDClientManager::Instance().GetTable(); + auto* lootTableTable = CDClientManager::GetTable(); + auto* itemComponentTable = CDClientManager::GetTable(); + auto* compRegistryTable = CDClientManager::GetTable(); for (const auto& lootMatrix : lootMatrices) { auto vendorItems = lootTableTable->GetTable(lootMatrix.LootTableIndex); @@ -101,10 +101,10 @@ void VendorComponent::RefreshInventory(bool isCreation) { } void VendorComponent::SetupConstants() { - auto* compRegistryTable = CDClientManager::Instance().GetTable(); + auto* compRegistryTable = CDClientManager::GetTable(); int componentID = compRegistryTable->GetByIDAndType(m_Parent->GetLOT(), eReplicaComponentType::VENDOR); - auto* vendorComponentTable = CDClientManager::Instance().GetTable(); + auto* vendorComponentTable = CDClientManager::GetTable(); std::vector vendorComps = vendorComponentTable->Query([=](CDVendorComponent entry) { return (entry.id == componentID); }); if (vendorComps.empty()) return; auto vendorData = vendorComps.at(0); diff --git a/dGame/dComponents/VendorComponent.h b/dGame/dComponents/VendorComponent.h index 7924a928..48b766d2 100644 --- a/dGame/dComponents/VendorComponent.h +++ b/dGame/dComponents/VendorComponent.h @@ -20,7 +20,7 @@ struct SoldItem { class VendorComponent : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::VENDOR; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::VENDOR; VendorComponent(Entity* parent); void Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate) override; diff --git a/dGame/dGameMessages/EchoStartSkill.h b/dGame/dGameMessages/EchoStartSkill.h index f5dee816..389a81e0 100644 --- a/dGame/dGameMessages/EchoStartSkill.h +++ b/dGame/dGameMessages/EchoStartSkill.h @@ -13,13 +13,13 @@ public: bUsedMouse = false; fCasterLatency = 0.0f; iCastType = 0; - lastClickedPosit = NiPoint3::ZERO; + lastClickedPosit = NiPoint3Constant::ZERO; optionalTargetID = LWOOBJID_EMPTY; - originatorRot = NiQuaternion::IDENTITY; + originatorRot = NiQuaternionConstant::IDENTITY; uiSkillHandle = 0; } - EchoStartSkill(LWOOBJID _optionalOriginatorID, std::string _sBitStream, TSkillID _skillID, bool _bUsedMouse = false, float _fCasterLatency = 0.0f, int32_t _iCastType = 0, NiPoint3 _lastClickedPosit = NiPoint3::ZERO, LWOOBJID _optionalTargetID = LWOOBJID_EMPTY, NiQuaternion _originatorRot = NiQuaternion::IDENTITY, uint32_t _uiSkillHandle = 0) { + EchoStartSkill(LWOOBJID _optionalOriginatorID, std::string _sBitStream, TSkillID _skillID, bool _bUsedMouse = false, float _fCasterLatency = 0.0f, int32_t _iCastType = 0, NiPoint3 _lastClickedPosit = NiPoint3Constant::ZERO, LWOOBJID _optionalTargetID = LWOOBJID_EMPTY, NiQuaternion _originatorRot = NiQuaternionConstant::IDENTITY, uint32_t _uiSkillHandle = 0) { bUsedMouse = _bUsedMouse; fCasterLatency = _fCasterLatency; iCastType = _iCastType; @@ -50,16 +50,16 @@ public: stream->Write(iCastType != 0); if (iCastType != 0) stream->Write(iCastType); - stream->Write(lastClickedPosit != NiPoint3::ZERO); - if (lastClickedPosit != NiPoint3::ZERO) stream->Write(lastClickedPosit); + stream->Write(lastClickedPosit != NiPoint3Constant::ZERO); + if (lastClickedPosit != NiPoint3Constant::ZERO) stream->Write(lastClickedPosit); stream->Write(optionalOriginatorID); stream->Write(optionalTargetID != LWOOBJID_EMPTY); if (optionalTargetID != LWOOBJID_EMPTY) stream->Write(optionalTargetID); - stream->Write(originatorRot != NiQuaternion::IDENTITY); - if (originatorRot != NiQuaternion::IDENTITY) stream->Write(originatorRot); + stream->Write(originatorRot != NiQuaternionConstant::IDENTITY); + if (originatorRot != NiQuaternionConstant::IDENTITY) stream->Write(originatorRot); uint32_t sBitStreamLength = sBitStream.length(); stream->Write(sBitStreamLength); diff --git a/dGame/dGameMessages/GameMessageHandler.cpp b/dGame/dGameMessages/GameMessageHandler.cpp index 6e313ab1..64790e31 100644 --- a/dGame/dGameMessages/GameMessageHandler.cpp +++ b/dGame/dGameMessages/GameMessageHandler.cpp @@ -18,7 +18,6 @@ #include "Character.h" #include "ControllablePhysicsComponent.h" #include "dZoneManager.h" -#include "Player.h" #include "CppScripts.h" #include "CDClientDatabase.h" @@ -37,6 +36,7 @@ #include "eGameMessageType.h" #include "ePlayerFlag.h" #include "dConfig.h" +#include "GhostComponent.h" #include "StringifiedEnum.h" void GameMessageHandler::HandleMessage(RakNet::BitStream* inStream, const SystemAddress& sysAddr, LWOOBJID objectID, eGameMessageType messageID) { @@ -108,9 +108,9 @@ void GameMessageHandler::HandleMessage(RakNet::BitStream* inStream, const System GameMessages::SendRestoreToPostLoadStats(entity, sysAddr); entity->SetPlayerReadyForUpdates(); - auto* player = dynamic_cast(entity); - if (player != nullptr) { - player->ConstructLimboEntities(); + auto* ghostComponent = entity->GetComponent(); + if (ghostComponent != nullptr) { + ghostComponent->ConstructLimboEntities(); } InventoryComponent* inv = entity->GetComponent(); @@ -137,14 +137,14 @@ void GameMessageHandler::HandleMessage(RakNet::BitStream* inStream, const System Entity* zoneControl = Game::entityManager->GetZoneControlEntity(); for (CppScripts::Script* script : CppScripts::GetEntityScripts(zoneControl)) { - script->OnPlayerLoaded(zoneControl, player); + script->OnPlayerLoaded(zoneControl, entity); } std::vector scriptedActs = Game::entityManager->GetEntitiesByComponent(eReplicaComponentType::SCRIPT); for (Entity* scriptEntity : scriptedActs) { if (scriptEntity->GetObjectID() != zoneControl->GetObjectID()) { // Don't want to trigger twice on instance worlds for (CppScripts::Script* script : CppScripts::GetEntityScripts(scriptEntity)) { - script->OnPlayerLoaded(scriptEntity, player); + script->OnPlayerLoaded(scriptEntity, entity); } } } @@ -196,8 +196,8 @@ void GameMessageHandler::HandleMessage(RakNet::BitStream* inStream, const System } case eGameMessageType::MISSION_DIALOGUE_CANCELLED: { - //This message is pointless for our implementation, as the client just carries on after - //rejecting a mission offer. We dont need to do anything. This is just here to remove a warning in our logs :) + // This message is pointless for our implementation, as the client just carries on after + // rejecting a mission offer. We dont need to do anything. This is just here to remove a warning in our logs :) break; } @@ -290,7 +290,7 @@ void GameMessageHandler::HandleMessage(RakNet::BitStream* inStream, const System comp->Progress(eMissionTaskType::USE_SKILL, startSkill.skillID); } - CDSkillBehaviorTable* skillTable = CDClientManager::Instance().GetTable(); + CDSkillBehaviorTable* skillTable = CDClientManager::GetTable(); unsigned int behaviorId = skillTable->GetSkillByID(startSkill.skillID).behaviorID; bool success = false; diff --git a/dGame/dGameMessages/GameMessages.cpp b/dGame/dGameMessages/GameMessages.cpp index 0ea53f0f..ebd05f13 100644 --- a/dGame/dGameMessages/GameMessages.cpp +++ b/dGame/dGameMessages/GameMessages.cpp @@ -1,7 +1,6 @@ #include "GameMessages.h" #include "User.h" #include "Entity.h" -#include "PacketUtils.h" #include "BitStreamUtils.h" #include "BitStream.h" #include "Game.h" @@ -21,7 +20,6 @@ #include "WorldPackets.h" #include "Item.h" #include "ZCompression.h" -#include "Player.h" #include "dConfig.h" #include "TeamManager.h" #include "ChatPackets.h" @@ -81,6 +79,7 @@ #include "RailActivatorComponent.h" #include "LevelProgressionComponent.h" #include "DonationVendorComponent.h" +#include "GhostComponent.h" // Message includes: #include "dZoneManager.h" @@ -97,7 +96,9 @@ #include "eReplicaComponentType.h" #include "eClientMessageType.h" #include "eGameMessageType.h" +#include "ePetAbilityType.h" #include "ActivityManager.h" +#include "PlayerManager.h" #include "CDComponentsRegistryTable.h" #include "CDObjectsTable.h" @@ -385,8 +386,8 @@ void GameMessages::SendPlatformResync(Entity* entity, const SystemAddress& sysAd float fIdleTimeElapsed = 0.0f; float fMoveTimeElapsed = 0.0f; float fPercentBetweenPoints = 0.0f; - NiPoint3 ptUnexpectedLocation = NiPoint3::ZERO; - NiQuaternion qUnexpectedRotation = NiQuaternion::IDENTITY; + NiPoint3 ptUnexpectedLocation = NiPoint3Constant::ZERO; + NiQuaternion qUnexpectedRotation = NiQuaternionConstant::IDENTITY; bitStream.Write(bReverse); bitStream.Write(bStopAtDesiredWaypoint); @@ -403,8 +404,8 @@ void GameMessages::SendPlatformResync(Entity* entity, const SystemAddress& sysAd bitStream.Write(ptUnexpectedLocation.y); bitStream.Write(ptUnexpectedLocation.z); - bitStream.Write(qUnexpectedRotation != NiQuaternion::IDENTITY); - if (qUnexpectedRotation != NiQuaternion::IDENTITY) { + bitStream.Write(qUnexpectedRotation != NiQuaternionConstant::IDENTITY); + if (qUnexpectedRotation != NiQuaternionConstant::IDENTITY) { bitStream.Write(qUnexpectedRotation.x); bitStream.Write(qUnexpectedRotation.y); bitStream.Write(qUnexpectedRotation.z); @@ -636,6 +637,25 @@ void GameMessages::SendUIMessageServerToSingleClient(Entity* entity, const Syste SEND_PACKET; } +void GameMessages::SendUIMessageServerToSingleClient(const std::string& message, AMFBaseValue& args, const SystemAddress& sysAddr) { + CBITSTREAM; + CMSGHEADER; + + LWOOBJID empty = 0; + bitStream.Write(empty); + bitStream.Write(eGameMessageType::UI_MESSAGE_SERVER_TO_ALL_CLIENTS); // This is intentional to allow the server to send a ui message to a client via their system address. + + bitStream.Write(args); + uint32_t strMessageNameLength = message.size(); + bitStream.Write(strMessageNameLength); + + for (uint32_t k = 0; k < strMessageNameLength; k++) { + bitStream.Write(message[k]); + } + + SEND_PACKET; +} + void GameMessages::SendUIMessageServerToAllClients(const std::string& message, AMFBaseValue& args) { CBITSTREAM; CMSGHEADER; @@ -762,7 +782,7 @@ void GameMessages::SendSetCurrency(Entity* entity, int64_t currency, int lootTyp bitStream.Write(lootType != LOOTTYPE_NONE); if (lootType != LOOTTYPE_NONE) bitStream.Write(lootType); - bitStream.Write(NiPoint3::ZERO); + bitStream.Write(NiPoint3Constant::ZERO); bitStream.Write(sourceLOT != LOT_NULL); if (sourceLOT != LOT_NULL) bitStream.Write(sourceLOT); @@ -1071,7 +1091,7 @@ void GameMessages::SendDropClientLoot(Entity* entity, const LWOOBJID& sourceID, entity->RegisterCoinDrop(currency); } - if (spawnPos != NiPoint3::ZERO) { + if (spawnPos != NiPoint3Constant::ZERO) { bUsePosition = true; //Calculate where the loot will go: @@ -1093,8 +1113,8 @@ void GameMessages::SendDropClientLoot(Entity* entity, const LWOOBJID& sourceID, bitStream.Write(bUsePosition); - bitStream.Write(finalPosition != NiPoint3::ZERO); - if (finalPosition != NiPoint3::ZERO) bitStream.Write(finalPosition); + bitStream.Write(finalPosition != NiPoint3Constant::ZERO); + if (finalPosition != NiPoint3Constant::ZERO) bitStream.Write(finalPosition); bitStream.Write(currency); bitStream.Write(item); @@ -1102,14 +1122,14 @@ void GameMessages::SendDropClientLoot(Entity* entity, const LWOOBJID& sourceID, bitStream.Write(owner); bitStream.Write(sourceID); - bitStream.Write(spawnPos != NiPoint3::ZERO); - if (spawnPos != NiPoint3::ZERO) bitStream.Write(spawnPos); + bitStream.Write(spawnPos != NiPoint3Constant::ZERO); + if (spawnPos != NiPoint3Constant::ZERO) bitStream.Write(spawnPos); auto* team = TeamManager::Instance()->GetTeam(owner); // Currency and powerups should not sync if (team != nullptr && currency == 0) { - CDObjectsTable* objectsTable = CDClientManager::Instance().GetTable(); + CDObjectsTable* objectsTable = CDClientManager::GetTable(); const CDObjects& object = objectsTable->GetByID(item); @@ -1162,7 +1182,7 @@ void GameMessages::SendPlayerReachedRespawnCheckpoint(Entity* entity, const NiPo bitStream.Write(position.y); bitStream.Write(position.z); - const bool bIsNotIdentity = rotation != NiQuaternion::IDENTITY; + const bool bIsNotIdentity = rotation != NiQuaternionConstant::IDENTITY; bitStream.Write(bIsNotIdentity); if (bIsNotIdentity) { @@ -1638,8 +1658,8 @@ void GameMessages::SendNotifyClientShootingGalleryScore(LWOOBJID objectId, const void GameMessages::HandleUpdateShootingGalleryRotation(RakNet::BitStream* inStream, Entity* entity, const SystemAddress& sysAddr) { float angle = 0.0f; - NiPoint3 facing = NiPoint3::ZERO; - NiPoint3 muzzlePos = NiPoint3::ZERO; + NiPoint3 facing = NiPoint3Constant::ZERO; + NiPoint3 muzzlePos = NiPoint3Constant::ZERO; inStream->Read(angle); inStream->Read(facing); inStream->Read(muzzlePos); @@ -1739,8 +1759,6 @@ void GameMessages::SendStartCelebrationEffect(Entity* entity, const SystemAddres bitStream.Write(0); //subtext SEND_PACKET; - - //PacketUtils::SavePacket("StartCelebrationEffect.bin", (char*)bitStream.GetData(), bitStream.GetNumberOfBytesUsed()); } @@ -1963,7 +1981,6 @@ void GameMessages::SendBBBSaveResponse(const LWOOBJID& objectId, const LWOOBJID& bitStream.Write(buffer[i]); SEND_PACKET; - //PacketUtils::SavePacket("eGameMessageType::BBB_SAVE_RESPONSE.bin", reinterpret_cast(bitStream.GetData()), bitStream.GetNumberOfBytesUsed()); } // Property @@ -2098,8 +2115,8 @@ void GameMessages::SendPlaceModelResponse(LWOOBJID objectId, const SystemAddress bitStream.Write(objectId); bitStream.Write(eGameMessageType::PLACE_MODEL_RESPONSE); - bitStream.Write(position != NiPoint3::ZERO); - if (position != NiPoint3::ZERO) { + bitStream.Write(position != NiPoint3Constant::ZERO); + if (position != NiPoint3Constant::ZERO) { bitStream.Write(position); } @@ -2113,8 +2130,8 @@ void GameMessages::SendPlaceModelResponse(LWOOBJID objectId, const SystemAddress bitStream.Write(response); } - bitStream.Write(rotation != NiQuaternion::IDENTITY); - if (rotation != NiQuaternion::IDENTITY) { + bitStream.Write(rotation != NiQuaternionConstant::IDENTITY); + if (rotation != NiQuaternionConstant::IDENTITY) { bitStream.Write(response); } @@ -2266,7 +2283,7 @@ void GameMessages::HandleSetBuildMode(RakNet::BitStream* inStream, Entity* entit bool modePaused{}; int modeValue = 1; LWOOBJID playerId{}; - NiPoint3 startPosition = NiPoint3::ZERO; + NiPoint3 startPosition = NiPoint3Constant::ZERO; inStream->Read(start); @@ -2285,7 +2302,7 @@ void GameMessages::HandleSetBuildMode(RakNet::BitStream* inStream, Entity* entit auto* player = Game::entityManager->GetEntity(playerId); - if (startPosition == NiPoint3::ZERO) { + if (startPosition == NiPoint3Constant::ZERO) { startPosition = player->GetPosition(); } @@ -2379,13 +2396,13 @@ void GameMessages::HandlePlacePropertyModel(RakNet::BitStream* inStream, Entity* inStream->Read(model); - PropertyManagementComponent::Instance()->UpdateModelPosition(model, NiPoint3::ZERO, NiQuaternion::IDENTITY); + PropertyManagementComponent::Instance()->UpdateModelPosition(model, NiPoint3Constant::ZERO, NiQuaternionConstant::IDENTITY); } void GameMessages::HandleUpdatePropertyModel(RakNet::BitStream* inStream, Entity* entity, const SystemAddress& sysAddr) { LWOOBJID model; NiPoint3 position; - NiQuaternion rotation = NiQuaternion::IDENTITY; + NiQuaternion rotation = NiQuaternionConstant::IDENTITY; inStream->Read(model); inStream->Read(position); @@ -2579,6 +2596,7 @@ void GameMessages::HandleBBBSaveRequest(RakNet::BitStream* inStream, Entity* ent //We need to get a new ID for our model first: ObjectIDManager::RequestPersistentID([=](uint32_t newID) { + if (!entity || !entity->GetCharacter() || !entity->GetCharacter()->GetParentUser()) return; LWOOBJID newIDL = newID; GeneralUtils::SetBit(newIDL, eObjectBits::CHARACTER); GeneralUtils::SetBit(newIDL, eObjectBits::PERSISTENT); @@ -2601,13 +2619,13 @@ void GameMessages::HandleBBBSaveRequest(RakNet::BitStream* inStream, Entity* ent //Insert into ugc: std::string str(sd0Data.get(), sd0Size); std::istringstream sd0DataStream(str); - Database::Get()->InsertNewUgcModel(sd0DataStream, blueprintIDSmall, entity->GetParentUser()->GetAccountID(), entity->GetCharacter()->GetID()); + Database::Get()->InsertNewUgcModel(sd0DataStream, blueprintIDSmall, entity->GetCharacter()->GetParentUser()->GetAccountID(), entity->GetCharacter()->GetID()); //Insert into the db as a BBB model: IPropertyContents::Model model; model.id = newIDL; model.ugcId = blueprintIDSmall; - model.position = NiPoint3::ZERO; + model.position = NiPoint3Constant::ZERO; model.rotation = NiQuaternion(0.0f, 0.0f, 0.0f, 0.0f); model.lot = 14; Database::Get()->InsertNewPropertyModel(propertyId, model, "Objects_14_name"); @@ -2709,7 +2727,7 @@ void GameMessages::HandlePropertyEntranceSync(RakNet::BitStream* inStream, Entit filterText.push_back(c); } - auto* player = Player::GetPlayer(sysAddr); + auto* player = PlayerManager::GetPlayer(sysAddr); auto* entranceComponent = entity->GetComponent(); @@ -2736,7 +2754,7 @@ void GameMessages::HandleEnterProperty(RakNet::BitStream* inStream, Entity* enti inStream->Read(index); inStream->Read(returnToZone); - auto* player = Player::GetPlayer(sysAddr); + auto* player = PlayerManager::GetPlayer(sysAddr); auto* entranceComponent = entity->GetComponent(); if (entranceComponent != nullptr) { @@ -3388,7 +3406,7 @@ void GameMessages::SendNotifyPetTamingMinigame(LWOOBJID objectId, LWOOBJID petId bitStream.Write(petsDestPos); bitStream.Write(telePos); - const bool hasDefault = teleRot != NiQuaternion::IDENTITY; + const bool hasDefault = teleRot != NiQuaternionConstant::IDENTITY; bitStream.Write(hasDefault); if (hasDefault) bitStream.Write(teleRot); @@ -3533,7 +3551,7 @@ void GameMessages::SendClientExitTamingMinigame(LWOOBJID objectId, bool bVolunta SEND_PACKET; } -void GameMessages::SendShowPetActionButton(const LWOOBJID& objectId, const ePetAbilityType petAbility, const bool bShow, const SystemAddress& sysAddr) { +void GameMessages::SendShowPetActionButton(const LWOOBJID objectId, const ePetAbilityType petAbility, const bool bShow, const SystemAddress& sysAddr) { CBITSTREAM; CMSGHEADER; @@ -4240,7 +4258,7 @@ void GameMessages::HandleVehicleNotifyHitImaginationServer(RakNet::BitStream* in LWOOBJID pickupObjID = LWOOBJID_EMPTY; LWOOBJID pickupSpawnerID = LWOOBJID_EMPTY; int32_t pickupSpawnerIndex = -1; - NiPoint3 vehiclePosition = NiPoint3::ZERO; + NiPoint3 vehiclePosition = NiPoint3Constant::ZERO; if (inStream->ReadBit()) inStream->Read(pickupObjID); if (inStream->ReadBit()) inStream->Read(pickupSpawnerID); @@ -4644,10 +4662,11 @@ void GameMessages::HandleToggleGhostReferenceOverride(RakNet::BitStream* inStrea inStream->Read(bOverride); - auto* player = Player::GetPlayer(sysAddr); + auto* player = PlayerManager::GetPlayer(sysAddr); if (player != nullptr) { - player->SetGhostOverride(bOverride); + auto* ghostComponent = entity->GetComponent(); + if (ghostComponent) ghostComponent->SetGhostOverride(bOverride); Game::entityManager->UpdateGhosting(player); } @@ -4659,10 +4678,11 @@ void GameMessages::HandleSetGhostReferencePosition(RakNet::BitStream* inStream, inStream->Read(position); - auto* player = Player::GetPlayer(sysAddr); + auto* player = PlayerManager::GetPlayer(sysAddr); if (player != nullptr) { - player->SetGhostOverridePoint(position); + auto* ghostComponent = entity->GetComponent(); + if (ghostComponent) ghostComponent->SetGhostOverridePoint(position); Game::entityManager->UpdateGhosting(player); } @@ -4670,7 +4690,7 @@ void GameMessages::HandleSetGhostReferencePosition(RakNet::BitStream* inStream, void GameMessages::HandleBuyFromVendor(RakNet::BitStream* inStream, Entity* entity, const SystemAddress& sysAddr) { - bool bConfirmed{}; // this doesnt even do anything, thanks ND! + bool bConfirmed{}; // This doesn't appear to do anything. Further research is needed. bool countIsDefault{}; int count = 1; LOT item; @@ -4706,8 +4726,8 @@ void GameMessages::HandleBuyFromVendor(RakNet::BitStream* inStream, Entity* enti return; } - CDComponentsRegistryTable* compRegistryTable = CDClientManager::Instance().GetTable(); - CDItemComponentTable* itemComponentTable = CDClientManager::Instance().GetTable(); + CDComponentsRegistryTable* compRegistryTable = CDClientManager::GetTable(); + CDItemComponentTable* itemComponentTable = CDClientManager::GetTable(); int itemCompID = compRegistryTable->GetByIDAndType(item, eReplicaComponentType::ITEM); CDItemComponent itemComp = itemComponentTable->GetItemComponentByID(itemCompID); @@ -4798,8 +4818,8 @@ void GameMessages::HandleSellToVendor(RakNet::BitStream* inStream, Entity* entit Item* item = inv->FindItemById(iObjID); if (!item) return; - CDComponentsRegistryTable* compRegistryTable = CDClientManager::Instance().GetTable(); - CDItemComponentTable* itemComponentTable = CDClientManager::Instance().GetTable(); + CDComponentsRegistryTable* compRegistryTable = CDClientManager::GetTable(); + CDItemComponentTable* itemComponentTable = CDClientManager::GetTable(); int itemCompID = compRegistryTable->GetByIDAndType(item->GetLot(), eReplicaComponentType::ITEM); CDItemComponent itemComp = itemComponentTable->GetItemComponentByID(itemCompID); @@ -4848,8 +4868,8 @@ void GameMessages::HandleBuybackFromVendor(RakNet::BitStream* inStream, Entity* Item* item = inv->FindItemById(iObjID); if (!item) return; - CDComponentsRegistryTable* compRegistryTable = CDClientManager::Instance().GetTable(); - CDItemComponentTable* itemComponentTable = CDClientManager::Instance().GetTable(); + CDComponentsRegistryTable* compRegistryTable = CDClientManager::GetTable(); + CDItemComponentTable* itemComponentTable = CDClientManager::GetTable(); int itemCompID = compRegistryTable->GetByIDAndType(item->GetLot(), eReplicaComponentType::ITEM); CDItemComponent itemComp = itemComponentTable->GetItemComponentByID(itemCompID); @@ -4923,7 +4943,7 @@ void GameMessages::HandleFireEventServerSide(RakNet::BitStream* inStream, Entity inStream->Read(senderID); auto* sender = Game::entityManager->GetEntity(senderID); - auto* player = Player::GetPlayer(sysAddr); + auto* player = PlayerManager::GetPlayer(sysAddr); if (!player) { return; @@ -5061,7 +5081,7 @@ void GameMessages::HandlePlayEmote(RakNet::BitStream* inStream, Entity* entity) if (emoteID == 0) return; std::string sAnimationName = "deaded"; //Default name in case we fail to get the emote - CDEmoteTableTable* emotes = CDClientManager::Instance().GetTable(); + CDEmoteTableTable* emotes = CDClientManager::GetTable(); if (emotes) { CDEmoteTable* emote = emotes->GetEmote(emoteID); if (emote) sAnimationName = emote->animationName; @@ -5130,13 +5150,8 @@ void GameMessages::HandleSetFlag(RakNet::BitStream* inStream, Entity* entity) { inStream->Read(bFlag); inStream->Read(iFlagID); - auto user = entity->GetParentUser(); - if (user) { - auto character = user->GetLastUsedChar(); - if (!character) return; - - character->SetPlayerFlag(iFlagID, bFlag); - } + auto character = entity->GetCharacter(); + if (character) character->SetPlayerFlag(iFlagID, bFlag); } void GameMessages::HandleRespondToMission(RakNet::BitStream* inStream, Entity* entity) { diff --git a/dGame/dGameMessages/GameMessages.h b/dGame/dGameMessages/GameMessages.h index ca40b943..5f284b5f 100644 --- a/dGame/dGameMessages/GameMessages.h +++ b/dGame/dGameMessages/GameMessages.h @@ -34,6 +34,7 @@ enum class eObjectWorldState : uint32_t; enum class eTerminateType : uint32_t; enum class eControlScheme : uint32_t; enum class eStateChangeType : uint32_t; +enum class ePetAbilityType : uint32_t; enum class ePetTamingNotifyType : uint32_t; enum class eUseItemResponse : uint32_t; enum class eQuickBuildFailReason : uint32_t; @@ -56,14 +57,14 @@ namespace GameMessages { const SystemAddress& sysAddr, bool bFirstTime = true, const LWOOBJID& buildAreaID = LWOOBJID_EMPTY, - NiPoint3 buildStartPOS = NiPoint3::ZERO, + NiPoint3 buildStartPOS = NiPoint3Constant::ZERO, int sourceBAG = 0, const LWOOBJID& sourceID = LWOOBJID_EMPTY, LOT sourceLOT = 0, int sourceTYPE = 8, const LWOOBJID& targetID = 0, LOT targetLOT = 0, - NiPoint3 targetPOS = NiPoint3::ZERO, + NiPoint3 targetPOS = NiPoint3Constant::ZERO, int targetTYPE = 0 ); @@ -94,6 +95,9 @@ namespace GameMessages { void SendModifyLEGOScore(Entity* entity, const SystemAddress& sysAddr, int64_t score, eLootSourceType sourceType); void SendUIMessageServerToSingleClient(Entity* entity, const SystemAddress& sysAddr, const std::string& message, AMFBaseValue& args); + + // Specify sysAddr if you need to send a flash message to a client who you dont know the objectID of. + void SendUIMessageServerToSingleClient(const std::string& message, AMFBaseValue& args, const SystemAddress& sysAddr); void SendUIMessageServerToAllClients(const std::string& message, AMFBaseValue& args); void SendPlayEmbeddedEffectOnAllClientsNearObject(Entity* entity, std::u16string effectName, const LWOOBJID& fromObjectID, float radius); @@ -121,7 +125,7 @@ namespace GameMessages { void SendStop2DAmbientSound(Entity* entity, bool force, std::string audioGUID, bool result = false); void SendPlay2DAmbientSound(Entity* entity, std::string audioGUID, bool result = false); void SendSetNetworkScriptVar(Entity* entity, const SystemAddress& sysAddr, std::string data); - void SendDropClientLoot(Entity* entity, const LWOOBJID& sourceID, LOT item, int currency, NiPoint3 spawnPos = NiPoint3::ZERO, int count = 1); + void SendDropClientLoot(Entity* entity, const LWOOBJID& sourceID, LOT item, int currency, NiPoint3 spawnPos = NiPoint3Constant::ZERO, int count = 1); void SendSetPlayerControlScheme(Entity* entity, eControlScheme controlScheme); void SendPlayerReachedRespawnCheckpoint(Entity* entity, const NiPoint3& position, const NiQuaternion& rotation); @@ -238,7 +242,7 @@ namespace GameMessages { void SendLockNodeRotation(Entity* entity, std::string nodeName); - void SendSetBuildModeConfirmed(LWOOBJID objectId, const SystemAddress& sysAddr, bool start, bool warnVisitors, bool modePaused, int32_t modeValue, LWOOBJID playerId, NiPoint3 startPos = NiPoint3::ZERO); + void SendSetBuildModeConfirmed(LWOOBJID objectId, const SystemAddress& sysAddr, bool start, bool warnVisitors, bool modePaused, int32_t modeValue, LWOOBJID playerId, NiPoint3 startPos = NiPoint3Constant::ZERO); void SendGetModelsOnProperty(LWOOBJID objectId, std::map models, const SystemAddress& sysAddr); @@ -389,7 +393,7 @@ namespace GameMessages { void SendClientExitTamingMinigame(LWOOBJID objectId, bool bVoluntaryExit, const SystemAddress& sysAddr); - void SendShowPetActionButton(const LWOOBJID& objectId, const ePetAbilityType petAbility, const bool bShow, const SystemAddress& sysAddr); + void SendShowPetActionButton(const LWOOBJID objectId, const ePetAbilityType petAbility, const bool bShow, const SystemAddress& sysAddr); void SendPlayEmote(LWOOBJID objectId, int32_t emoteID, LWOOBJID target, const SystemAddress& sysAddr); diff --git a/dGame/dGameMessages/PropertyDataMessage.cpp b/dGame/dGameMessages/PropertyDataMessage.cpp index 29bc8ea9..ab7fe9c3 100644 --- a/dGame/dGameMessages/PropertyDataMessage.cpp +++ b/dGame/dGameMessages/PropertyDataMessage.cpp @@ -103,7 +103,7 @@ void GameMessages::PropertyDataMessage::Serialize(RakNet::BitStream& stream) con } GameMessages::PropertyDataMessage::PropertyDataMessage(uint32_t mapID) { - const auto propertyTemplate = CDClientManager::Instance().GetTable()->GetByMapID(mapID); + const auto propertyTemplate = CDClientManager::GetTable()->GetByMapID(mapID); TemplateID = propertyTemplate.id; ZoneId = propertyTemplate.mapID; diff --git a/dGame/dGameMessages/StartSkill.h b/dGame/dGameMessages/StartSkill.h index 40bc210f..6b0d267a 100644 --- a/dGame/dGameMessages/StartSkill.h +++ b/dGame/dGameMessages/StartSkill.h @@ -16,13 +16,13 @@ public: consumableItemID = LWOOBJID_EMPTY; fCasterLatency = 0.0f; iCastType = 0; - lastClickedPosit = NiPoint3::ZERO; + lastClickedPosit = NiPoint3Constant::ZERO; optionalTargetID = LWOOBJID_EMPTY; - originatorRot = NiQuaternion::IDENTITY; + originatorRot = NiQuaternionConstant::IDENTITY; uiSkillHandle = 0; } - StartSkill(LWOOBJID _optionalOriginatorID, std::string _sBitStream, TSkillID _skillID, bool _bUsedMouse = false, LWOOBJID _consumableItemID = LWOOBJID_EMPTY, float _fCasterLatency = 0.0f, int32_t _iCastType = 0, NiPoint3 _lastClickedPosit = NiPoint3::ZERO, LWOOBJID _optionalTargetID = LWOOBJID_EMPTY, NiQuaternion _originatorRot = NiQuaternion::IDENTITY, uint32_t _uiSkillHandle = 0) { + StartSkill(LWOOBJID _optionalOriginatorID, std::string _sBitStream, TSkillID _skillID, bool _bUsedMouse = false, LWOOBJID _consumableItemID = LWOOBJID_EMPTY, float _fCasterLatency = 0.0f, int32_t _iCastType = 0, NiPoint3 _lastClickedPosit = NiPoint3Constant::ZERO, LWOOBJID _optionalTargetID = LWOOBJID_EMPTY, NiQuaternion _originatorRot = NiQuaternionConstant::IDENTITY, uint32_t _uiSkillHandle = 0) { bUsedMouse = _bUsedMouse; consumableItemID = _consumableItemID; fCasterLatency = _fCasterLatency; @@ -57,16 +57,16 @@ public: stream->Write(iCastType != 0); if (iCastType != 0) stream->Write(iCastType); - stream->Write(lastClickedPosit != NiPoint3::ZERO); - if (lastClickedPosit != NiPoint3::ZERO) stream->Write(lastClickedPosit); + stream->Write(lastClickedPosit != NiPoint3Constant::ZERO); + if (lastClickedPosit != NiPoint3Constant::ZERO) stream->Write(lastClickedPosit); stream->Write(optionalOriginatorID); stream->Write(optionalTargetID != LWOOBJID_EMPTY); if (optionalTargetID != LWOOBJID_EMPTY) stream->Write(optionalTargetID); - stream->Write(originatorRot != NiQuaternion::IDENTITY); - if (originatorRot != NiQuaternion::IDENTITY) stream->Write(originatorRot); + stream->Write(originatorRot != NiQuaternionConstant::IDENTITY); + if (originatorRot != NiQuaternionConstant::IDENTITY) stream->Write(originatorRot); uint32_t sBitStreamLength = sBitStream.length(); stream->Write(sBitStreamLength); diff --git a/dGame/dInventory/CMakeLists.txt b/dGame/dInventory/CMakeLists.txt index a663a97d..bc741efe 100644 --- a/dGame/dInventory/CMakeLists.txt +++ b/dGame/dInventory/CMakeLists.txt @@ -5,5 +5,11 @@ set(DGAME_DINVENTORY_SOURCES "ItemSet.cpp" "ItemSetPassiveAbility.cpp") +# Workaround for compiler bug where the optimized code could result in a memcpy of 0 bytes, even though that isnt possible. +# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97185 +if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + set_source_files_properties("Item.cpp" PROPERTIES COMPILE_FLAGS "-Wno-stringop-overflow") +endif() + add_library(dInventory STATIC ${DGAME_DINVENTORY_SOURCES}) target_precompile_headers(dInventory REUSE_FROM dGameBase) diff --git a/dGame/dInventory/Inventory.cpp b/dGame/dInventory/Inventory.cpp index 4475f77b..35222bea 100644 --- a/dGame/dInventory/Inventory.cpp +++ b/dGame/dInventory/Inventory.cpp @@ -275,9 +275,9 @@ eInventoryType Inventory::FindInventoryTypeForLot(const LOT lot) { } const CDItemComponent& Inventory::FindItemComponent(const LOT lot) { - auto* registry = CDClientManager::Instance().GetTable(); + auto* registry = CDClientManager::GetTable(); - auto* itemComponents = CDClientManager::Instance().GetTable(); + auto* itemComponents = CDClientManager::GetTable(); const auto componentId = registry->GetByIDAndType(lot, eReplicaComponentType::ITEM); @@ -293,7 +293,7 @@ const CDItemComponent& Inventory::FindItemComponent(const LOT lot) { } bool Inventory::IsValidItem(const LOT lot) { - auto* registry = CDClientManager::Instance().GetTable(); + auto* registry = CDClientManager::GetTable(); const auto componentId = registry->GetByIDAndType(lot, eReplicaComponentType::ITEM); diff --git a/dGame/dInventory/Item.cpp b/dGame/dInventory/Item.cpp index 4a13cd92..d3f15315 100644 --- a/dGame/dInventory/Item.cpp +++ b/dGame/dInventory/Item.cpp @@ -247,7 +247,7 @@ bool Item::IsEquipped() const { } bool Item::Consume() { - auto* skillsTable = CDClientManager::Instance().GetTable(); + auto* skillsTable = CDClientManager::GetTable(); auto skills = skillsTable->Query([this](const CDObjectSkills entry) { return entry.objectTemplate == static_cast(lot); @@ -313,12 +313,12 @@ void Item::UseNonEquip(Item* item) { bool success = false; auto inventory = item->GetInventory(); if (inventory && inventory->GetType() == eInventoryType::ITEMS) { - auto* compRegistryTable = CDClientManager::Instance().GetTable(); + auto* compRegistryTable = CDClientManager::GetTable(); const auto packageComponentId = compRegistryTable->GetByIDAndType(lot, eReplicaComponentType::PACKAGE); if (packageComponentId == 0) return; - auto* packCompTable = CDClientManager::Instance().GetTable(); + auto* packCompTable = CDClientManager::GetTable(); auto packages = packCompTable->Query([=](const CDPackageComponent entry) {return entry.id == static_cast(packageComponentId); }); auto success = !packages.empty(); @@ -396,7 +396,7 @@ void Item::Disassemble(const eInventoryType inventoryType) { } void Item::DisassembleModel(uint32_t numToDismantle) { - auto* table = CDClientManager::Instance().GetTable(); + auto* table = CDClientManager::GetTable(); const auto componentId = table->GetByIDAndType(GetLot(), eReplicaComponentType::RENDER); @@ -468,20 +468,20 @@ void Item::DisassembleModel(uint32_t numToDismantle) { // First iteration gets the count std::map parts; while (currentBrick) { - auto* designID = currentBrick->Attribute("designID"); + const char* const designID = currentBrick->Attribute("designID"); if (designID) { - uint32_t designId; - if (!GeneralUtils::TryParse(designID, designId)) { + const auto designId = GeneralUtils::TryParse(designID); + if (!designId) { LOG("Failed to parse designID %s", designID); continue; } - parts[designId]++; + parts[designId.value()]++; } currentBrick = currentBrick->NextSiblingElement(searchTerm.c_str()); } - auto* brickIDTable = CDClientManager::Instance().GetTable(); + auto* brickIDTable = CDClientManager::GetTable(); // Second iteration actually distributes the bricks for (const auto& [part, count] : parts) { diff --git a/dGame/dInventory/ItemSet.cpp b/dGame/dInventory/ItemSet.cpp index 3364b63b..1d086786 100644 --- a/dGame/dInventory/ItemSet.cpp +++ b/dGame/dInventory/ItemSet.cpp @@ -87,10 +87,8 @@ ItemSet::ItemSet(const uint32_t id, InventoryComponent* inventoryComponent) { m_Items = {}; while (std::getline(stream, token, ',')) { - int32_t value; - if (GeneralUtils::TryParse(token, value)) { - m_Items.push_back(value); - } + const auto validToken = GeneralUtils::TryParse(token); + if (validToken) m_Items.push_back(validToken.value()); } m_Equipped = {}; @@ -129,7 +127,7 @@ void ItemSet::OnEquip(const LOT lot) { auto* missionComponent = m_InventoryComponent->GetParent()->GetComponent(); for (const auto skill : skillSet) { - auto* skillTable = CDClientManager::Instance().GetTable(); + auto* skillTable = CDClientManager::GetTable(); const auto behaviorId = skillTable->GetSkillByID(skill).behaviorID; @@ -161,7 +159,7 @@ void ItemSet::OnUnEquip(const LOT lot) { const auto& skillComponent = m_InventoryComponent->GetParent()->GetComponent(); for (const auto skill : skillSet) { - auto* skillTable = CDClientManager::Instance().GetTable(); + auto* skillTable = CDClientManager::GetTable(); const auto behaviorId = skillTable->GetSkillByID(skill).behaviorID; diff --git a/dGame/dMission/Mission.cpp b/dGame/dMission/Mission.cpp index 6799b834..4ed80bf3 100644 --- a/dGame/dMission/Mission.cpp +++ b/dGame/dMission/Mission.cpp @@ -24,6 +24,7 @@ #include "eMissionTaskType.h" #include "eMissionLockState.h" #include "eReplicaComponentType.h" +#include "Character.h" #include "CDMissionEmailTable.h" @@ -40,7 +41,7 @@ Mission::Mission(MissionComponent* missionComponent, const uint32_t missionId) { m_State = eMissionState::UNKNOWN; - auto* missionsTable = CDClientManager::Instance().GetTable(); + auto* missionsTable = CDClientManager::GetTable(); auto* mis = missionsTable->GetPtrByMissionID(missionId); info = *mis; @@ -51,7 +52,7 @@ Mission::Mission(MissionComponent* missionComponent, const uint32_t missionId) { return; } - auto* tasksTable = CDClientManager::Instance().GetTable(); + auto* tasksTable = CDClientManager::GetTable(); auto tasks = tasksTable->GetByMissionID(missionId); @@ -179,7 +180,7 @@ void Mission::UpdateXml(tinyxml2::XMLElement* element) { } bool Mission::IsValidMission(const uint32_t missionId) { - auto* table = CDClientManager::Instance().GetTable(); + auto* table = CDClientManager::GetTable(); const auto missions = table->Query([=](const CDMissions& entry) { return entry.id == static_cast(missionId); @@ -189,7 +190,7 @@ bool Mission::IsValidMission(const uint32_t missionId) { } bool Mission::IsValidMission(const uint32_t missionId, CDMissions& info) { - auto* table = CDClientManager::Instance().GetTable(); + auto* table = CDClientManager::GetTable(); const auto missions = table->Query([=](const CDMissions& entry) { return entry.id == static_cast(missionId); @@ -208,8 +209,8 @@ Entity* Mission::GetAssociate() const { return m_MissionComponent->GetParent(); } -User* Mission::GetUser() const { - return GetAssociate()->GetParentUser(); +Character* Mission::GetCharacter() const { + return GetAssociate()->GetCharacter(); } uint32_t Mission::GetMissionId() const { @@ -333,7 +334,7 @@ void Mission::Complete(const bool yieldRewards) { missionComponent->Progress(eMissionTaskType::RACING, info.id, static_cast(eRacingTaskParam::COMPLETE_TRACK_TASKS)); - auto* missionEmailTable = CDClientManager::Instance().GetTable(); + auto* missionEmailTable = CDClientManager::GetTable(); const auto missionId = GetMissionId(); @@ -390,7 +391,7 @@ void Mission::Catchup() { if (type == eMissionTaskType::PLAYER_FLAG) { for (int32_t target : task->GetAllTargets()) { - const auto flag = GetUser()->GetLastUsedChar()->GetPlayerFlag(target); + const auto flag = GetCharacter()->GetPlayerFlag(target); if (!flag) { continue; @@ -413,7 +414,7 @@ void Mission::YieldRewards() { return; } - auto* character = GetUser()->GetLastUsedChar(); + auto* character = GetCharacter(); auto* inventoryComponent = entity->GetComponent(); auto* levelComponent = entity->GetComponent(); @@ -599,8 +600,10 @@ void Mission::SetMissionState(const eMissionState state, const bool sendingRewar if (entity == nullptr) { return; } + auto* characterComponent = entity->GetComponent(); + if (!characterComponent) return; - GameMessages::SendNotifyMission(entity, entity->GetParentUser()->GetSystemAddress(), info.id, static_cast(state), sendingRewards); + GameMessages::SendNotifyMission(entity, characterComponent->GetSystemAddress(), info.id, static_cast(state), sendingRewards); } void Mission::SetMissionTypeState(eMissionLockState state, const std::string& type, const std::string& subType) { diff --git a/dGame/dMission/Mission.h b/dGame/dMission/Mission.h index f7c17003..d8c104e8 100644 --- a/dGame/dMission/Mission.h +++ b/dGame/dMission/Mission.h @@ -17,6 +17,7 @@ namespace tinyxml2 { enum class eMissionState : int; enum class eMissionLockState : int; class MissionComponent; +class Character; /** * A mission (or achievement) that a player may unlock, progress and complete. @@ -46,7 +47,7 @@ public: * Returns the account owns the entity that is currently progressing this mission * @return the account owns the entity that is currently progressing this mission */ - User* GetUser() const; + Character* GetCharacter() const; /** * Returns the current state of this mission diff --git a/dGame/dMission/MissionPrerequisites.cpp b/dGame/dMission/MissionPrerequisites.cpp index 89d547fd..b5b81160 100644 --- a/dGame/dMission/MissionPrerequisites.cpp +++ b/dGame/dMission/MissionPrerequisites.cpp @@ -161,7 +161,7 @@ bool MissionPrerequisites::CheckPrerequisites(uint32_t missionId, const std::uno return index->second->Execute(missions); } - auto* missionsTable = CDClientManager::Instance().GetTable(); + auto* missionsTable = CDClientManager::GetTable(); const auto missionEntries = missionsTable->Query([=](const CDMissions& entry) { return entry.id == static_cast(missionId); }); diff --git a/dGame/dMission/MissionTask.cpp b/dGame/dMission/MissionTask.cpp index 604276ec..2fe9bc9f 100644 --- a/dGame/dMission/MissionTask.cpp +++ b/dGame/dMission/MissionTask.cpp @@ -27,19 +27,15 @@ MissionTask::MissionTask(Mission* mission, CDMissionTasks* info, uint32_t mask) std::string token; while (std::getline(stream, token, ',')) { - uint32_t parameter; - if (GeneralUtils::TryParse(token, parameter)) { - parameters.push_back(parameter); - } + const auto parameter = GeneralUtils::TryParse(token); + if (parameter) parameters.push_back(parameter.value()); } stream = std::istringstream(info->targetGroup); while (std::getline(stream, token, ',')) { - uint32_t parameter; - if (GeneralUtils::TryParse(token, parameter)) { - targets.push_back(parameter); - } + const auto parameter = GeneralUtils::TryParse(token); + if (parameter) targets.push_back(parameter.value()); } } diff --git a/dGame/dPropertyBehaviors/ControlBehaviorMessages/AddStripMessage.cpp b/dGame/dPropertyBehaviors/ControlBehaviorMessages/AddStripMessage.cpp index 5e75f6c4..ac6e8db7 100644 --- a/dGame/dPropertyBehaviors/ControlBehaviorMessages/AddStripMessage.cpp +++ b/dGame/dPropertyBehaviors/ControlBehaviorMessages/AddStripMessage.cpp @@ -2,7 +2,7 @@ #include "Action.h" -AddStripMessage::AddStripMessage(AMFArrayValue* arguments) : BehaviorMessageBase(arguments) { +AddStripMessage::AddStripMessage(AMFArrayValue* const arguments) : BehaviorMessageBase{ arguments } { actionContext = ActionContext(arguments); position = StripUiPosition(arguments); @@ -22,3 +22,7 @@ AddStripMessage::AddStripMessage(AMFArrayValue* arguments) : BehaviorMessageBase } LOG_DEBUG("number of actions %i", actionsToAdd.size()); } + +std::vector AddStripMessage::GetActionsToAdd() const { + return actionsToAdd; +} diff --git a/dGame/dPropertyBehaviors/ControlBehaviorMessages/AddStripMessage.h b/dGame/dPropertyBehaviors/ControlBehaviorMessages/AddStripMessage.h index 0b9a09e3..2e2bf9a0 100644 --- a/dGame/dPropertyBehaviors/ControlBehaviorMessages/AddStripMessage.h +++ b/dGame/dPropertyBehaviors/ControlBehaviorMessages/AddStripMessage.h @@ -18,10 +18,10 @@ class AMFArrayValue; */ class AddStripMessage : public BehaviorMessageBase { public: - AddStripMessage(AMFArrayValue* arguments); + AddStripMessage(AMFArrayValue* const arguments); StripUiPosition GetPosition() const { return position; }; ActionContext GetActionContext() const { return actionContext; }; - std::vector GetActionsToAdd() const { return actionsToAdd; }; + std::vector GetActionsToAdd() const; private: StripUiPosition position; ActionContext actionContext; diff --git a/dGame/dPropertyBehaviors/ControlBehaviorMessages/BehaviorMessageBase.cpp b/dGame/dPropertyBehaviors/ControlBehaviorMessages/BehaviorMessageBase.cpp index cc817cd7..a49a8aeb 100644 --- a/dGame/dPropertyBehaviors/ControlBehaviorMessages/BehaviorMessageBase.cpp +++ b/dGame/dPropertyBehaviors/ControlBehaviorMessages/BehaviorMessageBase.cpp @@ -5,27 +5,26 @@ #include "dCommonVars.h" BehaviorMessageBase::BehaviorMessageBase(AMFArrayValue* arguments) { - behaviorId = GetBehaviorIdFromArgument(arguments); + this->behaviorId = GetBehaviorIdFromArgument(arguments); } -int32_t BehaviorMessageBase::GetBehaviorIdFromArgument(AMFArrayValue* arguments) { - const auto* key = "BehaviorID"; - auto* behaviorIDValue = arguments->Get(key); +int32_t BehaviorMessageBase::GetBehaviorIdFromArgument(AMFArrayValue* const arguments) { + const char* const key = "BehaviorID"; + const auto* const behaviorIDValue = arguments->Get(key); if (behaviorIDValue && behaviorIDValue->GetValueType() == eAmf::String) { - GeneralUtils::TryParse(behaviorIDValue->GetValue(), behaviorId); + this->behaviorId = + GeneralUtils::TryParse(behaviorIDValue->GetValue()).value_or(this->behaviorId); } else if (arguments->Get(key) && arguments->Get(key)->GetValueType() != eAmf::Undefined) { throw std::invalid_argument("Unable to find behavior ID"); } - return behaviorId; + return this->behaviorId; } -int32_t BehaviorMessageBase::GetActionIndexFromArgument(AMFArrayValue* arguments, const std::string& keyName) { - auto* actionIndexAmf = arguments->Get(keyName); - if (!actionIndexAmf) { - throw std::invalid_argument("Unable to find actionIndex"); - } +int32_t BehaviorMessageBase::GetActionIndexFromArgument(AMFArrayValue* const arguments, const std::string& keyName) const { + const auto* const actionIndexAmf = arguments->Get(keyName); + if (!actionIndexAmf) throw std::invalid_argument("Unable to find actionIndex"); return static_cast(actionIndexAmf->GetValue()); } diff --git a/dGame/dPropertyBehaviors/ControlBehaviorMessages/BehaviorMessageBase.h b/dGame/dPropertyBehaviors/ControlBehaviorMessages/BehaviorMessageBase.h index 8a841d7f..f55fde8e 100644 --- a/dGame/dPropertyBehaviors/ControlBehaviorMessages/BehaviorMessageBase.h +++ b/dGame/dPropertyBehaviors/ControlBehaviorMessages/BehaviorMessageBase.h @@ -15,13 +15,13 @@ enum class BehaviorState : uint32_t; */ class BehaviorMessageBase { public: - static inline int32_t DefaultBehaviorId = -1; - const int32_t GetBehaviorId() const { return behaviorId; }; - bool IsDefaultBehaviorId() { return behaviorId == DefaultBehaviorId; }; - BehaviorMessageBase(AMFArrayValue* arguments); + static constexpr int32_t DefaultBehaviorId = -1; + [[nodiscard]] int32_t GetBehaviorId() const { return behaviorId; }; + [[nodiscard]] bool IsDefaultBehaviorId() { return behaviorId == DefaultBehaviorId; }; + BehaviorMessageBase(AMFArrayValue* const arguments); protected: - int32_t GetBehaviorIdFromArgument(AMFArrayValue* arguments); - int32_t GetActionIndexFromArgument(AMFArrayValue* arguments, const std::string& keyName = "actionIndex"); + [[nodiscard]] int32_t GetBehaviorIdFromArgument(AMFArrayValue* const arguments); + [[nodiscard]] int32_t GetActionIndexFromArgument(AMFArrayValue* const arguments, const std::string& keyName = "actionIndex") const; int32_t behaviorId = DefaultBehaviorId; }; diff --git a/dGame/dPropertyBehaviors/ControlBehaviors.cpp b/dGame/dPropertyBehaviors/ControlBehaviors.cpp index c541257c..5fee358d 100644 --- a/dGame/dPropertyBehaviors/ControlBehaviors.cpp +++ b/dGame/dPropertyBehaviors/ControlBehaviors.cpp @@ -13,6 +13,7 @@ #include "User.h" #include "tinyxml2.h" #include "CDClientDatabase.h" +#include "CharacterComponent.h" // Message includes #include "Action.h" @@ -145,10 +146,12 @@ void ControlBehaviors::ProcessCommand(Entity* modelEntity, const SystemAddress& } else if (command == "moveToInventory") { MoveToInventoryMessage msg(arguments); context.modelComponent->MoveToInventory(msg); + auto* characterComponent = modelOwner->GetComponent(); + if (!characterComponent) return; AMFArrayValue args; args.Insert("BehaviorID", std::to_string(msg.GetBehaviorId())); - GameMessages::SendUIMessageServerToSingleClient(modelOwner, modelOwner->GetParentUser()->GetSystemAddress(), "BehaviorRemoved", args); + GameMessages::SendUIMessageServerToSingleClient(modelOwner, characterComponent->GetSystemAddress(), "BehaviorRemoved", args); SendBehaviorListToClient(context); } else if (command == "updateAction") { diff --git a/dGame/dUtilities/CheatDetection.cpp b/dGame/dUtilities/CheatDetection.cpp index a43cdaeb..a87157a1 100644 --- a/dGame/dUtilities/CheatDetection.cpp +++ b/dGame/dUtilities/CheatDetection.cpp @@ -2,7 +2,6 @@ #include "Database.h" #include "Entity.h" #include "PossessableComponent.h" -#include "Player.h" #include "Game.h" #include "EntityManager.h" #include "Character.h" @@ -10,6 +9,7 @@ #include "UserManager.h" #include "dConfig.h" #include +#include "PlayerManager.h" Entity* GetPossessedEntity(const LWOOBJID& objId) { auto* entity = Game::entityManager->GetEntity(objId); @@ -49,7 +49,7 @@ void LogAndSaveFailedAntiCheatCheck(const LWOOBJID& id, const SystemAddress& sys User* toReport = nullptr; switch (checkType) { case CheckType::Entity: { - auto* player = Player::GetPlayer(sysAddr); + auto* player = PlayerManager::GetPlayer(sysAddr); auto* entity = GetPossessedEntity(id); // If player exists and entity exists in world, use both for logging info. @@ -58,13 +58,13 @@ void LogAndSaveFailedAntiCheatCheck(const LWOOBJID& id, const SystemAddress& sys player->GetCharacter()->GetName().c_str(), player->GetObjectID(), sysAddr.ToString(), entity->GetCharacter()->GetName().c_str(), entity->GetObjectID()); - toReport = player->GetParentUser(); + if (player->GetCharacter()) toReport = player->GetCharacter()->GetParentUser(); // In the case that the target entity id did not exist, just log the player info. } else if (player) { LOG("Player (%s) (%llu) at system address (%s) with sending player (%llu) does not match their own.", player->GetCharacter()->GetName().c_str(), player->GetObjectID(), sysAddr.ToString(), id); - toReport = player->GetParentUser(); + if (player->GetCharacter()) toReport = player->GetCharacter()->GetParentUser(); // In the rare case that the player does not exist, just log the system address and who the target id was. } else { LOG("Player at system address (%s) with sending player (%llu) does not match their own.", diff --git a/dGame/dUtilities/Loot.cpp b/dGame/dUtilities/Loot.cpp index 25c81e74..b2f96ac3 100644 --- a/dGame/dUtilities/Loot.cpp +++ b/dGame/dUtilities/Loot.cpp @@ -28,11 +28,11 @@ void Loot::CacheMatrix(uint32_t matrixIndex) { return; } CachedMatrices.insert(matrixIndex); - CDComponentsRegistryTable* componentsRegistryTable = CDClientManager::Instance().GetTable(); - CDItemComponentTable* itemComponentTable = CDClientManager::Instance().GetTable(); - CDLootMatrixTable* lootMatrixTable = CDClientManager::Instance().GetTable(); - CDLootTableTable* lootTableTable = CDClientManager::Instance().GetTable(); - CDRarityTableTable* rarityTableTable = CDClientManager::Instance().GetTable(); + CDComponentsRegistryTable* componentsRegistryTable = CDClientManager::GetTable(); + CDItemComponentTable* itemComponentTable = CDClientManager::GetTable(); + CDLootMatrixTable* lootMatrixTable = CDClientManager::GetTable(); + CDLootTableTable* lootTableTable = CDClientManager::GetTable(); + CDRarityTableTable* rarityTableTable = CDClientManager::GetTable(); const auto& matrix = lootMatrixTable->GetMatrix(matrixIndex); @@ -47,11 +47,11 @@ void Loot::CacheMatrix(uint32_t matrixIndex) { } std::unordered_map Loot::RollLootMatrix(Entity* player, uint32_t matrixIndex) { - CDComponentsRegistryTable* componentsRegistryTable = CDClientManager::Instance().GetTable(); - CDItemComponentTable* itemComponentTable = CDClientManager::Instance().GetTable(); - CDLootMatrixTable* lootMatrixTable = CDClientManager::Instance().GetTable(); - CDLootTableTable* lootTableTable = CDClientManager::Instance().GetTable(); - CDRarityTableTable* rarityTableTable = CDClientManager::Instance().GetTable(); + CDComponentsRegistryTable* componentsRegistryTable = CDClientManager::GetTable(); + CDItemComponentTable* itemComponentTable = CDClientManager::GetTable(); + CDLootMatrixTable* lootMatrixTable = CDClientManager::GetTable(); + CDLootTableTable* lootTableTable = CDClientManager::GetTable(); + CDRarityTableTable* rarityTableTable = CDClientManager::GetTable(); auto* missionComponent = player->GetComponent(); std::unordered_map drops; @@ -134,11 +134,11 @@ std::unordered_map Loot::RollLootMatrix(Entity* player, uint32_t m } std::unordered_map Loot::RollLootMatrix(uint32_t matrixIndex) { - CDComponentsRegistryTable* componentsRegistryTable = CDClientManager::Instance().GetTable(); - CDItemComponentTable* itemComponentTable = CDClientManager::Instance().GetTable(); - CDLootMatrixTable* lootMatrixTable = CDClientManager::Instance().GetTable(); - CDLootTableTable* lootTableTable = CDClientManager::Instance().GetTable(); - CDRarityTableTable* rarityTableTable = CDClientManager::Instance().GetTable(); + CDComponentsRegistryTable* componentsRegistryTable = CDClientManager::GetTable(); + CDItemComponentTable* itemComponentTable = CDClientManager::GetTable(); + CDLootMatrixTable* lootMatrixTable = CDClientManager::GetTable(); + CDLootTableTable* lootTableTable = CDClientManager::GetTable(); + CDRarityTableTable* rarityTableTable = CDClientManager::GetTable(); std::unordered_map drops; const auto& matrix = lootMatrixTable->GetMatrix(matrixIndex); @@ -216,7 +216,7 @@ void Loot::GiveLoot(Entity* player, std::unordered_map& result, eL } void Loot::GiveActivityLoot(Entity* player, Entity* source, uint32_t activityID, int32_t rating) { - CDActivityRewardsTable* activityRewardsTable = CDClientManager::Instance().GetTable(); + CDActivityRewardsTable* activityRewardsTable = CDClientManager::GetTable(); std::vector activityRewards = activityRewardsTable->Query([activityID](CDActivityRewards entry) { return (entry.objectTemplate == activityID); }); const CDActivityRewards* selectedReward = nullptr; @@ -232,7 +232,7 @@ void Loot::GiveActivityLoot(Entity* player, Entity* source, uint32_t activityID, uint32_t minCoins = 0; uint32_t maxCoins = 0; - CDCurrencyTableTable* currencyTableTable = CDClientManager::Instance().GetTable(); + CDCurrencyTableTable* currencyTableTable = CDClientManager::GetTable(); std::vector currencyTable = currencyTableTable->Query([selectedReward](CDCurrencyTable entry) { return (entry.currencyIndex == selectedReward->CurrencyIndex && entry.npcminlevel == 1); }); if (currencyTable.size() > 0) { @@ -286,7 +286,7 @@ void Loot::DropLoot(Entity* player, Entity* killedObject, std::unordered_map(); + CDActivityRewardsTable* activityRewardsTable = CDClientManager::GetTable(); std::vector activityRewards = activityRewardsTable->Query([activityID](CDActivityRewards entry) { return (entry.objectTemplate == activityID); }); const CDActivityRewards* selectedReward = nullptr; @@ -303,7 +303,7 @@ void Loot::DropActivityLoot(Entity* player, Entity* source, uint32_t activityID, uint32_t minCoins = 0; uint32_t maxCoins = 0; - CDCurrencyTableTable* currencyTableTable = CDClientManager::Instance().GetTable(); + CDCurrencyTableTable* currencyTableTable = CDClientManager::GetTable(); std::vector currencyTable = currencyTableTable->Query([selectedReward](CDCurrencyTable entry) { return (entry.currencyIndex == selectedReward->CurrencyIndex && entry.npcminlevel == 1); }); if (currencyTable.size() > 0) { diff --git a/dGame/dUtilities/Mail.cpp b/dGame/dUtilities/Mail.cpp index 2677c9cc..fc0c833e 100644 --- a/dGame/dUtilities/Mail.cpp +++ b/dGame/dUtilities/Mail.cpp @@ -12,7 +12,6 @@ #include "dServer.h" #include "Entity.h" #include "Character.h" -#include "PacketUtils.h" #include "BitStreamUtils.h" #include "Logger.h" #include "EntityManager.h" @@ -305,7 +304,6 @@ void Mail::HandleDataRequest(RakNet::BitStream* packet, const SystemAddress& sys } Game::server->Send(&bitStream, sysAddr, false); - // PacketUtils::SavePacket("Max_Mail_Data.bin", (const char*)bitStream.GetData(), bitStream.GetNumberOfBytesUsed()); } void Mail::HandleAttachmentCollect(RakNet::BitStream* packet, const SystemAddress& sysAddr, Entity* player) { diff --git a/dGame/dUtilities/Preconditions.cpp b/dGame/dUtilities/Preconditions.cpp index 12059b5b..bd855962 100644 --- a/dGame/dUtilities/Preconditions.cpp +++ b/dGame/dUtilities/Preconditions.cpp @@ -40,10 +40,8 @@ Precondition::Precondition(const uint32_t condition) { std::string token; while (std::getline(stream, token, ',')) { - uint32_t value; - if (GeneralUtils::TryParse(token, value)) { - this->values.push_back(value); - } + const auto validToken = GeneralUtils::TryParse(token); + if (validToken) this->values.push_back(validToken.value()); } } diff --git a/dGame/dUtilities/SlashCommandHandler.cpp b/dGame/dUtilities/SlashCommandHandler.cpp index 4d1ec733..d96a330e 100644 --- a/dGame/dUtilities/SlashCommandHandler.cpp +++ b/dGame/dUtilities/SlashCommandHandler.cpp @@ -54,7 +54,6 @@ #include "Loot.h" #include "EntityInfo.h" #include "LUTriggers.h" -#include "Player.h" #include "PhantomPhysicsComponent.h" #include "ProximityMonitorComponent.h" #include "dpShapeSphere.h" @@ -83,11 +82,13 @@ #include "eConnectionType.h" #include "eChatInternalMessageType.h" #include "eMasterMessageType.h" +#include "PlayerManager.h" #include "CDRewardCodesTable.h" #include "CDObjectsTable.h" #include "CDZoneTableTable.h" #include "ePlayerFlag.h" +#include "dNavMesh.h" void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entity* entity, const SystemAddress& sysAddr) { auto commandCopy = command; @@ -113,13 +114,13 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit if ((chatCommand == "setgmlevel" || chatCommand == "makegm" || chatCommand == "gmlevel") && user->GetMaxGMLevel() > eGameMasterLevel::CIVILIAN) { if (args.size() != 1) return; - uint32_t level_intermed = 0; + const auto level_intermed = GeneralUtils::TryParse(args[0]); - if (!GeneralUtils::TryParse(args[0], level_intermed)) { + if (!level_intermed) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid gm level."); return; } - eGameMasterLevel level = static_cast(level_intermed); + eGameMasterLevel level = static_cast(level_intermed.value()); #ifndef DEVELOPER_SERVER if (user->GetMaxGMLevel() == eGameMasterLevel::JUNIOR_DEVELOPER) { @@ -215,10 +216,10 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit if (chatCommand == "who") { ChatPackets::SendSystemMessage( sysAddr, - u"Players in this instance: (" + GeneralUtils::to_u16string(Player::GetAllPlayers().size()) + u")" + u"Players in this instance: (" + GeneralUtils::to_u16string(PlayerManager::GetAllPlayers().size()) + u")" ); - for (auto* player : Player::GetAllPlayers()) { + for (auto* player : PlayerManager::GetAllPlayers()) { const auto& name = player->GetCharacter()->GetName(); ChatPackets::SendSystemMessage( @@ -378,26 +379,27 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit } if (chatCommand == "resetmission" && args.size() >= 1 && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER) { - uint32_t missionId; - if (!GeneralUtils::TryParse(args[0], missionId)) { + const auto missionId = GeneralUtils::TryParse(args[0]); + if (!missionId) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid mission ID."); return; } auto* missionComponent = entity->GetComponent(); if (!missionComponent) return; - missionComponent->ResetMission(missionId); + missionComponent->ResetMission(missionId.value()); } // Log command to database Database::Get()->InsertSlashCommandUsage(entity->GetObjectID(), chatCommand); if (chatCommand == "setminifig" && args.size() == 2 && entity->GetGMLevel() >= eGameMasterLevel::FORUM_MODERATOR) { // could break characters so only allow if GM > 0 - int32_t minifigItemId; - if (!GeneralUtils::TryParse(args[1], minifigItemId)) { + const auto minifigItemIdExists = GeneralUtils::TryParse(args[1]); + if (!minifigItemIdExists) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid Minifig Item Id ID."); return; } + const int32_t minifigItemId = minifigItemIdExists.value(); Game::entityManager->DestructEntity(entity, sysAddr); auto* charComp = entity->GetComponent(); std::string lowerName = args[0]; @@ -457,14 +459,14 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit } if (chatCommand == "unlock-emote" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER) { - int32_t emoteID; + const auto emoteID = GeneralUtils::TryParse(args.at(0)); - if (!GeneralUtils::TryParse(args[0], emoteID)) { + if (!emoteID) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid emote ID."); return; } - entity->GetCharacter()->UnlockEmote(emoteID); + entity->GetCharacter()->UnlockEmote(emoteID.value()); } if (chatCommand == "force-save" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER) { @@ -474,7 +476,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit if (chatCommand == "kill" && args.size() == 1 && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER) { ChatPackets::SendSystemMessage(sysAddr, u"Brutally murdering that player, if online on this server."); - auto* player = Player::GetPlayer(args[0]); + auto* player = PlayerManager::GetPlayer(args[0]); if (player) { player->Smash(entity->GetObjectID()); ChatPackets::SendSystemMessage(sysAddr, u"It has been done, do you feel good about yourself now?"); @@ -486,19 +488,19 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit } if (chatCommand == "speedboost" && args.size() == 1 && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER) { - float boost; - - if (!GeneralUtils::TryParse(args[0], boost)) { + const auto boostOptional = GeneralUtils::TryParse(args[0]); + if (!boostOptional) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid boost."); return; } + const float boost = boostOptional.value(); auto* controllablePhysicsComponent = entity->GetComponent(); if (!controllablePhysicsComponent) return; controllablePhysicsComponent->SetSpeedMultiplier(boost); - // speedboost possesables + // speedboost possessables auto possessor = entity->GetComponent(); if (possessor) { auto possessedID = possessor->GetPossessable(); @@ -527,14 +529,14 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit } if (chatCommand == "setcontrolscheme" && args.size() == 1 && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER) { - uint32_t scheme; + const auto scheme = GeneralUtils::TryParse(args[0]); - if (!GeneralUtils::TryParse(args[0], scheme)) { + if (!scheme) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid control scheme."); return; } - GameMessages::SendSetPlayerControlScheme(entity, static_cast(scheme)); + GameMessages::SendSetPlayerControlScheme(entity, static_cast(scheme.value())); ChatPackets::SendSystemMessage(sysAddr, u"Switched control scheme."); return; @@ -574,29 +576,28 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit } if ((chatCommand == "setinventorysize" || chatCommand == "setinvsize") && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER && args.size() >= 1) { - uint32_t size; - - if (!GeneralUtils::TryParse(args.at(0), size)) { + const auto sizeOptional = GeneralUtils::TryParse(args[0]); + if (!sizeOptional) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid size."); return; } + const uint32_t size = sizeOptional.value(); eInventoryType selectedInventory = eInventoryType::ITEMS; // a possible inventory was provided if we got more than 1 argument if (args.size() >= 2) { - selectedInventory = eInventoryType::INVALID; - if (!GeneralUtils::TryParse(args.at(1), selectedInventory)) { + selectedInventory = GeneralUtils::TryParse(args.at(1)).value_or(eInventoryType::INVALID); + if (selectedInventory == eInventoryType::INVALID) { + ChatPackets::SendSystemMessage(sysAddr, u"Invalid inventory."); + return; + } else { // In this case, we treat the input as a string and try to find it in the reflection list std::transform(args.at(1).begin(), args.at(1).end(), args.at(1).begin(), ::toupper); for (uint32_t index = 0; index < NUMBER_OF_INVENTORIES; index++) { if (std::string_view(args.at(1)) == std::string_view(InventoryType::InventoryTypeToString(static_cast(index)))) selectedInventory = static_cast(index); } } - if (selectedInventory == eInventoryType::INVALID) { - ChatPackets::SendSystemMessage(sysAddr, u"Invalid inventory."); - return; - } ChatPackets::SendSystemMessage(sysAddr, u"Setting inventory " + GeneralUtils::ASCIIToUTF16(args.at(1)) + @@ -631,6 +632,9 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit if (infile.good()) { std::string line; while (std::getline(infile, line)) { + // Do this in two separate calls to catch both \n and \r\n + line.erase(std::remove(line.begin(), line.end(), '\n'), line.end()); + line.erase(std::remove(line.begin(), line.end(), '\r'), line.end()); SlashCommandHandler::HandleChatCommand(GeneralUtils::ASCIIToUTF16(line), entity, sysAddr); } } else { @@ -643,48 +647,48 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit if (chatCommand == "addmission" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER) { if (args.size() == 0) return; - uint32_t missionID; + const auto missionID = GeneralUtils::TryParse(args.at(0)); - if (!GeneralUtils::TryParse(args[0], missionID)) { + if (!missionID) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid mission id."); return; } auto comp = static_cast(entity->GetComponent(eReplicaComponentType::MISSION)); - if (comp) comp->AcceptMission(missionID, true); + if (comp) comp->AcceptMission(missionID.value(), true); return; } if (chatCommand == "completemission" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER) { if (args.size() == 0) return; - uint32_t missionID; + const auto missionID = GeneralUtils::TryParse(args.at(0)); - if (!GeneralUtils::TryParse(args[0], missionID)) { + if (!missionID) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid mission id."); return; } auto comp = static_cast(entity->GetComponent(eReplicaComponentType::MISSION)); - if (comp) comp->CompleteMission(missionID, true); + if (comp) comp->CompleteMission(missionID.value(), true); return; } if (chatCommand == "setflag" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER && args.size() == 1) { - int32_t flagId; + const auto flagId = GeneralUtils::TryParse(args.at(0)); - if (!GeneralUtils::TryParse(args[0], flagId)) { + if (!flagId) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid flag id."); return; } - entity->GetCharacter()->SetPlayerFlag(flagId, true); + entity->GetCharacter()->SetPlayerFlag(flagId.value(), true); } if (chatCommand == "setflag" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER && args.size() == 2) { - int32_t flagId; - std::string onOffFlag = args[0]; - if (!GeneralUtils::TryParse(args[1], flagId)) { + const auto flagId = GeneralUtils::TryParse(args.at(1)); + std::string onOffFlag = args.at(0); + if (!flagId) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid flag id."); return; } @@ -692,17 +696,17 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit ChatPackets::SendSystemMessage(sysAddr, u"Invalid flag type."); return; } - entity->GetCharacter()->SetPlayerFlag(flagId, onOffFlag == "on"); + entity->GetCharacter()->SetPlayerFlag(flagId.value(), onOffFlag == "on"); } if (chatCommand == "clearflag" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER && args.size() == 1) { - int32_t flagId; + const auto flagId = GeneralUtils::TryParse(args.at(0)); - if (!GeneralUtils::TryParse(args[0], flagId)) { + if (!flagId) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid flag id."); return; } - entity->GetCharacter()->SetPlayerFlag(flagId, false); + entity->GetCharacter()->SetPlayerFlag(flagId.value(), false); } // Pet status utility @@ -872,14 +876,12 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit } if (chatCommand == "playeffect" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER && args.size() >= 3) { - int32_t effectID = 0; + const auto effectID = GeneralUtils::TryParse(args.at(0)); - if (!GeneralUtils::TryParse(args[0], effectID)) { - return; - } + if (!effectID) return; // FIXME: use fallible ASCIIToUTF16 conversion, because non-ascii isn't valid anyway - GameMessages::SendPlayFXEffect(entity->GetObjectID(), effectID, GeneralUtils::ASCIIToUTF16(args[1]), args[2]); + GameMessages::SendPlayFXEffect(entity->GetObjectID(), effectID.value(), GeneralUtils::ASCIIToUTF16(args.at(1)), args.at(2)); } if (chatCommand == "stopeffect" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER && args.size() >= 1) { @@ -934,41 +936,39 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit auto control = static_cast(entity->GetComponent(eReplicaComponentType::CONTROLLABLE_PHYSICS)); if (!control) return; - float y = dpWorld::Instance().GetNavMesh()->GetHeightAtPoint(control->GetPosition()); + float y = dpWorld::GetNavMesh()->GetHeightAtPoint(control->GetPosition()); std::u16string msg = u"Navmesh height: " + (GeneralUtils::to_u16string(y)); ChatPackets::SendSystemMessage(sysAddr, msg); } if (chatCommand == "gmadditem" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER) { if (args.size() == 1) { - uint32_t itemLOT; + const auto itemLOT = GeneralUtils::TryParse(args.at(0)); - if (!GeneralUtils::TryParse(args[0], itemLOT)) { + if (!itemLOT) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid item LOT."); return; } InventoryComponent* inventory = static_cast(entity->GetComponent(eReplicaComponentType::INVENTORY)); - inventory->AddItem(itemLOT, 1, eLootSourceType::MODERATION); + inventory->AddItem(itemLOT.value(), 1, eLootSourceType::MODERATION); } else if (args.size() == 2) { - uint32_t itemLOT; - - if (!GeneralUtils::TryParse(args[0], itemLOT)) { + const auto itemLOT = GeneralUtils::TryParse(args.at(0)); + if (!itemLOT) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid item LOT."); return; } - uint32_t count; - - if (!GeneralUtils::TryParse(args[1], count)) { + const auto count = GeneralUtils::TryParse(args.at(1)); + if (!count) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid item count."); return; } InventoryComponent* inventory = static_cast(entity->GetComponent(eReplicaComponentType::INVENTORY)); - inventory->AddItem(itemLOT, count, eLootSourceType::MODERATION); + inventory->AddItem(itemLOT.value(), count.value(), eLootSourceType::MODERATION); } else { ChatPackets::SendSystemMessage(sysAddr, u"Correct usage: /gmadditem "); } @@ -988,9 +988,9 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit receiverID = playerInfo->id; - LOT lot; + const auto lot = GeneralUtils::TryParse(args.at(1)); - if (!GeneralUtils::TryParse(args[1], lot)) { + if (!lot) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid item lot."); return; } @@ -1003,7 +1003,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit mailInsert.subject = "Lost item"; mailInsert.body = "This is a replacement item for one you lost."; mailInsert.itemID = LWOOBJID_EMPTY; - mailInsert.itemLOT = lot; + mailInsert.itemLOT = lot.value(); mailInsert.itemSubkey = LWOOBJID_EMPTY; mailInsert.itemCount = 1; Database::Get()->InsertNewMail(mailInsert); @@ -1037,46 +1037,47 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit NiPoint3 pos{}; if (args.size() == 3) { - float x, y, z; - - if (!GeneralUtils::TryParse(args[0], x)) { + const auto x = GeneralUtils::TryParse(args.at(0)); + if (!x) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid x."); return; } - if (!GeneralUtils::TryParse(args[1], y)) { + const auto y = GeneralUtils::TryParse(args.at(1)); + if (!y) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid y."); return; } - if (!GeneralUtils::TryParse(args[2], z)) { + const auto z = GeneralUtils::TryParse(args.at(2)); + if (!z) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid z."); return; } - pos.SetX(x); - pos.SetY(y); - pos.SetZ(z); + pos.SetX(x.value()); + pos.SetY(y.value()); + pos.SetZ(z.value()); LOG("Teleporting objectID: %llu to %f, %f, %f", entity->GetObjectID(), pos.x, pos.y, pos.z); GameMessages::SendTeleport(entity->GetObjectID(), pos, NiQuaternion(), sysAddr); } else if (args.size() == 2) { - float x, z; - - if (!GeneralUtils::TryParse(args[0], x)) { + const auto x = GeneralUtils::TryParse(args.at(0)); + if (!x) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid x."); return; } - if (!GeneralUtils::TryParse(args[1], z)) { + const auto z = GeneralUtils::TryParse(args.at(1)); + if (!z) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid z."); return; } - pos.SetX(x); + pos.SetX(x.value()); pos.SetY(0.0f); - pos.SetZ(z); + pos.SetZ(z.value()); LOG("Teleporting objectID: %llu to X: %f, Z: %f", entity->GetObjectID(), pos.x, pos.z); GameMessages::SendTeleport(entity->GetObjectID(), pos, NiQuaternion(), sysAddr); @@ -1136,10 +1137,10 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit float speedScale = 1.0f; if (args.size() >= 1) { - float tempScaleStore; + const auto tempScaleStore = GeneralUtils::TryParse(args.at(0)); - if (GeneralUtils::TryParse(args[0], tempScaleStore)) { - speedScale = tempScaleStore; + if (tempScaleStore) { + speedScale = tempScaleStore.value(); } else { ChatPackets::SendSystemMessage(sysAddr, u"Failed to parse speed scale argument."); } @@ -1160,7 +1161,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit if (chatCommand == "mute" && entity->GetGMLevel() >= eGameMasterLevel::JUNIOR_DEVELOPER) { if (args.size() >= 1) { - auto* player = Player::GetPlayer(args[0]); + auto* player = PlayerManager::GetPlayer(args[0]); uint32_t accountId = 0; LWOOBJID characterId = 0; @@ -1182,23 +1183,26 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit return; } } else { - accountId = player->GetParentUser()->GetAccountID(); + auto* character = player->GetCharacter(); + auto* user = character != nullptr ? character->GetParentUser() : nullptr; + if (user) accountId = user->GetAccountID(); characterId = player->GetObjectID(); } time_t expire = 1; // Default to indefinate mute if (args.size() >= 2) { - uint32_t days = 0; - uint32_t hours = 0; - if (!GeneralUtils::TryParse(args[1], days)) { + const auto days = GeneralUtils::TryParse(args[1]); + if (!days) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid days."); return; } + std::optional hours; if (args.size() >= 3) { - if (!GeneralUtils::TryParse(args[2], hours)) { + hours = GeneralUtils::TryParse(args[2]); + if (!hours) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid hours."); return; @@ -1206,11 +1210,11 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit } expire = time(NULL); - expire += 24 * 60 * 60 * days; - expire += 60 * 60 * hours; + expire += 24 * 60 * 60 * days.value(); + expire += 60 * 60 * hours.value_or(0); } - Database::Get()->UpdateAccountUnmuteTime(accountId, expire); + if (accountId != 0) Database::Get()->UpdateAccountUnmuteTime(accountId, expire); char buffer[32] = "brought up for review.\0"; @@ -1239,7 +1243,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit if (chatCommand == "kick" && entity->GetGMLevel() >= eGameMasterLevel::JUNIOR_MODERATOR) { if (args.size() == 1) { - auto* player = Player::GetPlayer(args[0]); + auto* player = PlayerManager::GetPlayer(args[0]); std::u16string username = GeneralUtils::UTF8ToUTF16(args[0]); if (player == nullptr) { @@ -1257,7 +1261,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit if (chatCommand == "ban" && entity->GetGMLevel() >= eGameMasterLevel::SENIOR_MODERATOR) { if (args.size() == 1) { - auto* player = Player::GetPlayer(args[0]); + auto* player = PlayerManager::GetPlayer(args[0]); uint32_t accountId = 0; @@ -1274,10 +1278,12 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit return; } } else { - accountId = player->GetParentUser()->GetAccountID(); + auto* character = player->GetCharacter(); + auto* user = character != nullptr ? character->GetParentUser() : nullptr; + if (user) accountId = user->GetAccountID(); } - Database::Get()->UpdateAccountBan(accountId, true); + if (accountId != 0) Database::Get()->UpdateAccountBan(accountId, true); if (player != nullptr) { Game::server->Disconnect(player->GetSystemAddress(), eServerDisconnectIdentifiers::FREE_TRIAL_EXPIRED); @@ -1305,14 +1311,14 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit } if (chatCommand == "startcelebration" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER && args.size() == 1) { - int32_t celebration; + const auto celebration = GeneralUtils::TryParse(args.at(0)); - if (!GeneralUtils::TryParse(args[0], celebration)) { + if (!celebration) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid celebration."); return; } - GameMessages::SendStartCelebrationEffect(entity, entity->GetSystemAddress(), celebration); + GameMessages::SendStartCelebrationEffect(entity, entity->GetSystemAddress(), celebration.value()); } if (chatCommand == "buffmed" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER) { @@ -1366,15 +1372,15 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit ControllablePhysicsComponent* comp = static_cast(entity->GetComponent(eReplicaComponentType::CONTROLLABLE_PHYSICS)); if (!comp) return; - uint32_t lot; + const auto lot = GeneralUtils::TryParse(args[0]); - if (!GeneralUtils::TryParse(args[0], lot)) { + if (!lot) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid lot."); return; } EntityInfo info; - info.lot = lot; + info.lot = lot.value(); info.pos = comp->GetPosition(); info.rot = comp->GetRotation(); info.spawner = nullptr; @@ -1395,28 +1401,29 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit auto controllablePhysicsComponent = entity->GetComponent(); if (!controllablePhysicsComponent) return; - LOT lot{}; - uint32_t numberToSpawn{}; - float radiusToSpawnWithin{}; - - if (!GeneralUtils::TryParse(args[0], lot)) { + const auto lot = GeneralUtils::TryParse(args[0]); + if (!lot) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid lot."); return; } - if (!GeneralUtils::TryParse(args[1], numberToSpawn) && numberToSpawn > 0) { + const auto numberToSpawnOptional = GeneralUtils::TryParse(args[1]); + if (!numberToSpawnOptional && numberToSpawnOptional.value() > 0) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid number of enemies to spawn."); return; } + uint32_t numberToSpawn = numberToSpawnOptional.value(); // Must spawn within a radius of at least 0.0f - if (!GeneralUtils::TryParse(args[2], radiusToSpawnWithin) && radiusToSpawnWithin < 0.0f) { + const auto radiusToSpawnWithinOptional = GeneralUtils::TryParse(args[2]); + if (!radiusToSpawnWithinOptional && radiusToSpawnWithinOptional.value() < 0.0f) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid radius to spawn within."); return; } + const float radiusToSpawnWithin = radiusToSpawnWithinOptional.value(); EntityInfo info; - info.lot = lot; + info.lot = lot.value(); info.spawner = nullptr; info.spawnerID = entity->GetObjectID(); info.spawnerNodeID = 0; @@ -1443,12 +1450,12 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit } if ((chatCommand == "giveuscore") && args.size() >= 1 && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER) { - int32_t uscore; - - if (!GeneralUtils::TryParse(args[0], uscore)) { + const auto uscoreOptional = GeneralUtils::TryParse(args[0]); + if (!uscoreOptional) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid uscore."); return; } + const int32_t uscore = uscoreOptional.value(); CharacterComponent* character = entity->GetComponent(); if (character) character->SetUScore(character->GetUScore() + uscore); @@ -1456,9 +1463,9 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit eLootSourceType lootType = eLootSourceType::MODERATION; - int32_t type; - if (args.size() >= 2 && GeneralUtils::TryParse(args[1], type)) { - lootType = static_cast(type); + if (args.size() >= 2) { + const auto type = GeneralUtils::TryParse(args[1]); + lootType = type.value_or(lootType); } GameMessages::SendModifyLEGOScore(entity, entity->GetSystemAddress(), uscore, lootType); @@ -1470,7 +1477,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit if (args.size() > 1) { requestedPlayerToSetLevelOf = args[1]; - auto requestedPlayer = Player::GetPlayer(requestedPlayerToSetLevelOf); + auto requestedPlayer = PlayerManager::GetPlayer(requestedPlayerToSetLevelOf); if (!requestedPlayer) { ChatPackets::SendSystemMessage(sysAddr, u"No player found with username: (" + GeneralUtils::UTF8ToUTF16(requestedPlayerToSetLevelOf) + u")."); @@ -1484,14 +1491,15 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit entity = requestedPlayer->GetOwner(); } - uint32_t requestedLevel; + const auto requestedLevelOptional = GeneralUtils::TryParse(args[0]); uint32_t oldLevel; - // first check the level is valid - if (!GeneralUtils::TryParse(args[0], requestedLevel)) { + // first check the level is valid + if (!requestedLevelOptional) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid level."); return; } + uint32_t requestedLevel = requestedLevelOptional.value(); // query to set our uscore to the correct value for this level auto characterComponent = entity->GetComponent(); @@ -1564,31 +1572,31 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit } if ((chatCommand == "freemoney" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER) && args.size() == 1) { - int64_t money; + const auto money = GeneralUtils::TryParse(args[0]); - if (!GeneralUtils::TryParse(args[0], money)) { + if (!money) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid money."); return; } auto* ch = entity->GetCharacter(); - ch->SetCoins(ch->GetCoins() + money, eLootSourceType::MODERATION); + ch->SetCoins(ch->GetCoins() + money.value(), eLootSourceType::MODERATION); } if ((chatCommand == "setcurrency") && args.size() == 1 && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER) { - int32_t money; + const auto money = GeneralUtils::TryParse(args[0]); - if (!GeneralUtils::TryParse(args[0], money)) { + if (!money) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid money."); return; } auto* ch = entity->GetCharacter(); - ch->SetCoins(money, eLootSourceType::MODERATION); + ch->SetCoins(money.value(), eLootSourceType::MODERATION); } // Allow for this on even while not a GM, as it sometimes toggles incorrrectly. - if (chatCommand == "gminvis" && entity->GetParentUser()->GetMaxGMLevel() >= eGameMasterLevel::DEVELOPER) { + if (chatCommand == "gminvis" && entity->GetCharacter()->GetParentUser()->GetMaxGMLevel() >= eGameMasterLevel::DEVELOPER) { GameMessages::SendToggleGMInvis(entity->GetObjectID(), true, UNASSIGNED_SYSTEM_ADDRESS); return; @@ -1597,17 +1605,14 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit if (chatCommand == "gmimmune" && args.size() >= 1 && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER) { auto* destroyableComponent = entity->GetComponent(); - int32_t state = false; + const auto state = GeneralUtils::TryParse(args[0]); - if (!GeneralUtils::TryParse(args[0], state)) { + if (!state) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid state."); return; } - if (destroyableComponent != nullptr) { - destroyableComponent->SetIsGMImmune(state); - } - + if (destroyableComponent) destroyableComponent->SetIsGMImmune(state.value()); return; } @@ -1615,53 +1620,47 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit if (chatCommand == "attackimmune" && args.size() >= 1 && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER) { auto* destroyableComponent = entity->GetComponent(); - int32_t state = false; + const auto state = GeneralUtils::TryParse(args[0]); - if (!GeneralUtils::TryParse(args[0], state)) { + if (!state) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid state."); return; } - if (destroyableComponent != nullptr) { - destroyableComponent->SetIsImmune(state); - } - + if (destroyableComponent) destroyableComponent->SetIsImmune(state.value()); return; } if (chatCommand == "buff" && args.size() >= 2 && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER) { auto* buffComponent = entity->GetComponent(); - int32_t id = 0; - int32_t duration = 0; - - if (!GeneralUtils::TryParse(args[0], id)) { + const auto id = GeneralUtils::TryParse(args[0]); + if (!id) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid buff id."); return; } - if (!GeneralUtils::TryParse(args[1], duration)) { + const auto duration = GeneralUtils::TryParse(args[1]); + if (!duration) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid buff duration."); return; } - if (buffComponent != nullptr) { - buffComponent->ApplyBuff(id, duration, entity->GetObjectID()); - } - + if (buffComponent) buffComponent->ApplyBuff(id.value(), duration.value(), entity->GetObjectID()); return; } if ((chatCommand == "testmap" && args.size() >= 1) && entity->GetGMLevel() >= eGameMasterLevel::FORUM_MODERATOR) { ChatPackets::SendSystemMessage(sysAddr, u"Requesting map change..."); - uint32_t reqZone; LWOCLONEID cloneId = 0; bool force = false; - if (!GeneralUtils::TryParse(args[0], reqZone)) { + const auto reqZoneOptional = GeneralUtils::TryParse(args[0]); + if (!reqZoneOptional) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid zone."); return; } + const LWOMAPID reqZone = reqZoneOptional.value(); if (args.size() > 1) { auto index = 1; @@ -1672,9 +1671,13 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit force = true; } - if (args.size() > index && !GeneralUtils::TryParse(args[index], cloneId)) { - ChatPackets::SendSystemMessage(sysAddr, u"Invalid clone id."); - return; + if (args.size() > index) { + const auto cloneIdOptional = GeneralUtils::TryParse(args[index]); + if (!cloneIdOptional) { + ChatPackets::SendSystemMessage(sysAddr, u"Invalid clone id."); + return; + } + cloneId = cloneIdOptional.value(); } } @@ -1712,23 +1715,21 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit } if (chatCommand == "createprivate" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER && args.size() >= 3) { - uint32_t zone; - - if (!GeneralUtils::TryParse(args[0], zone)) { + const auto zone = GeneralUtils::TryParse(args[0]); + if (!zone) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid zone."); return; } - uint32_t clone; - - if (!GeneralUtils::TryParse(args[1], clone)) { + const auto clone = GeneralUtils::TryParse(args[1]); + if (!clone) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid clone."); return; } const auto& password = args[2]; - ZoneInstanceManager::Instance()->CreatePrivateZone(Game::server, zone, clone, password); + ZoneInstanceManager::Instance()->CreatePrivateZone(Game::server, zone.value(), clone.value(), password); ChatPackets::SendSystemMessage(sysAddr, GeneralUtils::ASCIIToUTF16("Sent request for private zone with password: " + password)); @@ -1755,14 +1756,14 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit } if (args.size() >= 1) { - float time; + const auto time = GeneralUtils::TryParse(args[0]); - if (!GeneralUtils::TryParse(args[0], time)) { + if (!time) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid boost time."); return; } else { GameMessages::SendVehicleAddPassiveBoostAction(vehicle->GetObjectID(), UNASSIGNED_SYSTEM_ADDRESS); - entity->AddCallbackTimer(time, [vehicle]() { + entity->AddCallbackTimer(time.value(), [vehicle]() { if (!vehicle) return; GameMessages::SendVehicleRemovePassiveBoostAction(vehicle->GetObjectID(), UNASSIGNED_SYSTEM_ADDRESS); }); @@ -1838,20 +1839,19 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit } if (chatCommand == "reforge" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER && args.size() >= 2) { - LOT baseItem; - LOT reforgedItem; + const auto baseItem = GeneralUtils::TryParse(args[0]); + if (!baseItem) return; - if (!GeneralUtils::TryParse(args[0], baseItem)) return; - if (!GeneralUtils::TryParse(args[1], reforgedItem)) return; + const auto reforgedItem = GeneralUtils::TryParse(args[1]); + if (!reforgedItem) return; auto* inventoryComponent = entity->GetComponent(); - - if (inventoryComponent == nullptr) return; + if (!inventoryComponent) return; std::vector data{}; - data.push_back(new LDFData(u"reforgedLOT", reforgedItem)); + data.push_back(new LDFData(u"reforgedLOT", reforgedItem.value())); - inventoryComponent->AddItem(baseItem, 1, eLootSourceType::MODERATION, eInventoryType::INVALID, data); + inventoryComponent->AddItem(baseItem.value(), 1, eLootSourceType::MODERATION, eInventoryType::INVALID, data); } if (chatCommand == "crash" && entity->GetGMLevel() >= eGameMasterLevel::OPERATOR) { @@ -1903,10 +1903,10 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit if (chatCommand == "reloadconfig" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER) { Game::config->ReloadConfig(); VanityUtilities::SpawnVanity(); - dpWorld::Instance().Reload(); + dpWorld::Reload(); auto entities = Game::entityManager->GetEntitiesByComponent(eReplicaComponentType::SCRIPTED_ACTIVITY); - for (auto entity : entities) { - auto* scriptedActivityComponent = entity->GetComponent(); + for (const auto* const entity : entities) { + auto* const scriptedActivityComponent = entity->GetComponent(); if (!scriptedActivityComponent) continue; scriptedActivityComponent->ReloadConfig(); @@ -1917,19 +1917,20 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit } if (chatCommand == "rollloot" && entity->GetGMLevel() >= eGameMasterLevel::OPERATOR && args.size() >= 3) { - uint32_t lootMatrixIndex = 0; - uint32_t targetLot = 0; - uint32_t loops = 1; + const auto lootMatrixIndex = GeneralUtils::TryParse(args[0]); + if (!lootMatrixIndex) return; - if (!GeneralUtils::TryParse(args[0], lootMatrixIndex)) return; - if (!GeneralUtils::TryParse(args[1], targetLot)) return; - if (!GeneralUtils::TryParse(args[2], loops)) return; + const auto targetLot = GeneralUtils::TryParse(args[1]); + if (!targetLot) return; + + const auto loops = GeneralUtils::TryParse(args[2]); + if (!loops) return; uint64_t totalRuns = 0; for (uint32_t i = 0; i < loops; i++) { while (true) { - auto lootRoll = Loot::RollLootMatrix(lootMatrixIndex); + auto lootRoll = Loot::RollLootMatrix(lootMatrixIndex.value()); totalRuns += 1; bool doBreak = false; for (const auto& kv : lootRoll) { @@ -1942,26 +1943,30 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit } std::u16string message = u"Ran loot drops looking for " - + GeneralUtils::to_u16string(targetLot) + + GeneralUtils::to_u16string(targetLot.value()) + u", " - + GeneralUtils::to_u16string(loops) + + GeneralUtils::to_u16string(loops.value()) + u" times. It ran " + GeneralUtils::to_u16string(totalRuns) + u" times. Averaging out at " - + GeneralUtils::to_u16string(static_cast(totalRuns) / loops); + + GeneralUtils::to_u16string(static_cast(totalRuns) / loops.value()); ChatPackets::SendSystemMessage(sysAddr, message); } if (chatCommand == "deleteinven" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER && args.size() >= 1) { eInventoryType inventoryType = eInventoryType::INVALID; - if (!GeneralUtils::TryParse(args[0], inventoryType)) { + + const auto inventoryTypeOptional = GeneralUtils::TryParse(args[0]); + if (!inventoryTypeOptional) { // In this case, we treat the input as a string and try to find it in the reflection list std::transform(args[0].begin(), args[0].end(), args[0].begin(), ::toupper); LOG("looking for inventory %s", args[0].c_str()); for (uint32_t index = 0; index < NUMBER_OF_INVENTORIES; index++) { if (std::string_view(args[0]) == std::string_view(InventoryType::InventoryTypeToString(static_cast(index)))) inventoryType = static_cast(index); } + } else { + inventoryType = inventoryTypeOptional.value(); } if (inventoryType == eInventoryType::INVALID || inventoryType >= NUMBER_OF_INVENTORIES) { @@ -1983,32 +1988,32 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit if (chatCommand == "castskill" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER && args.size() >= 1) { auto* skillComponent = entity->GetComponent(); if (skillComponent) { - uint32_t skillId; + const auto skillId = GeneralUtils::TryParse(args[0]); - if (!GeneralUtils::TryParse(args[0], skillId)) { + if (!skillId) { ChatPackets::SendSystemMessage(sysAddr, u"Error getting skill ID."); return; } else { - skillComponent->CastSkill(skillId, entity->GetObjectID(), entity->GetObjectID()); + skillComponent->CastSkill(skillId.value(), entity->GetObjectID(), entity->GetObjectID()); ChatPackets::SendSystemMessage(sysAddr, u"Cast skill"); } } } if (chatCommand == "setskillslot" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER && args.size() >= 2) { - uint32_t skillId; - int slot; - auto* inventoryComponent = entity->GetComponent(); + auto* const inventoryComponent = entity->GetComponent(); if (inventoryComponent) { - if (!GeneralUtils::TryParse(args[0], slot)) { + const auto slot = GeneralUtils::TryParse(args[0]); + if (!slot) { ChatPackets::SendSystemMessage(sysAddr, u"Error getting slot."); return; } else { - if (!GeneralUtils::TryParse(args[1], skillId)) { + const auto skillId = GeneralUtils::TryParse(args[1]); + if (!skillId) { ChatPackets::SendSystemMessage(sysAddr, u"Error getting skill."); return; } else { - if (inventoryComponent->SetSkill(slot, skillId)) ChatPackets::SendSystemMessage(sysAddr, u"Set skill to slot successfully"); + if (inventoryComponent->SetSkill(slot.value(), skillId.value())) ChatPackets::SendSystemMessage(sysAddr, u"Set skill to slot successfully"); else ChatPackets::SendSystemMessage(sysAddr, u"Set skill to slot failed"); } } @@ -2018,13 +2023,13 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit if (chatCommand == "setfaction" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER && args.size() >= 1) { auto* destroyableComponent = entity->GetComponent(); if (destroyableComponent) { - int32_t faction; + const auto faction = GeneralUtils::TryParse(args[0]); - if (!GeneralUtils::TryParse(args[0], faction)) { + if (!faction) { ChatPackets::SendSystemMessage(sysAddr, u"Error getting faction."); return; } else { - destroyableComponent->SetFaction(faction); + destroyableComponent->SetFaction(faction.value()); ChatPackets::SendSystemMessage(sysAddr, u"Set faction and updated enemies list"); } } @@ -2033,13 +2038,13 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit if (chatCommand == "addfaction" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER && args.size() >= 1) { auto* destroyableComponent = entity->GetComponent(); if (destroyableComponent) { - int32_t faction; + const auto faction = GeneralUtils::TryParse(args[0]); - if (!GeneralUtils::TryParse(args[0], faction)) { + if (!faction) { ChatPackets::SendSystemMessage(sysAddr, u"Error getting faction."); return; } else { - destroyableComponent->AddFaction(faction); + destroyableComponent->AddFaction(faction.value()); ChatPackets::SendSystemMessage(sysAddr, u"Added faction and updated enemies list"); } } @@ -2061,7 +2066,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit } if (chatCommand == "setrewardcode" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER && args.size() == 1) { - auto* cdrewardCodes = CDClientManager::Instance().GetTable(); + auto* cdrewardCodes = CDClientManager::GetTable(); auto id = cdrewardCodes->GetCodeID(args[0]); if (id != -1) Database::Get()->InsertRewardCode(user->GetAccountID(), id); @@ -2070,13 +2075,12 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit if (chatCommand == "inspect" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER && args.size() >= 1) { Entity* closest = nullptr; - eReplicaComponentType component; - std::u16string ldf; bool isLDF = false; - if (!GeneralUtils::TryParse(args[0], component)) { + auto component = GeneralUtils::TryParse(args[0]); + if (!component) { component = eReplicaComponentType::INVALID; ldf = GeneralUtils::UTF8ToUTF16(args[0]); @@ -2088,7 +2092,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit auto closestDistance = 0.0f; - const auto candidates = Game::entityManager->GetEntitiesByComponent(component); + const auto candidates = Game::entityManager->GetEntitiesByComponent(component.value()); for (auto* candidate : candidates) { if (candidate->GetLOT() == 1 || candidate->GetLOT() == 8092) { @@ -2099,7 +2103,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit continue; } - if (closest == nullptr) { + if (!closest) { closest = candidate; closestDistance = NiPoint3::Distance(candidate->GetPosition(), reference); @@ -2116,13 +2120,11 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit } } - if (closest == nullptr) { - return; - } + if (!closest) return; Game::entityManager->SerializeEntity(closest); - auto* table = CDClientManager::Instance().GetTable(); + auto* table = CDClientManager::GetTable(); const auto& info = table->GetByID(closest->GetLOT()); @@ -2144,20 +2146,18 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit if (args.size() >= 2) { if (args[1] == "-m" && args.size() >= 3) { - auto* movingPlatformComponent = closest->GetComponent(); + auto* const movingPlatformComponent = closest->GetComponent(); - int32_t value = 0; + const auto mValue = GeneralUtils::TryParse(args[2]); - if (movingPlatformComponent == nullptr || !GeneralUtils::TryParse(args[2], value)) { - return; - } + if (!movingPlatformComponent || !mValue) return; movingPlatformComponent->SetSerialized(true); - if (value == -1) { + if (mValue == -1) { movingPlatformComponent->StopPathing(); } else { - movingPlatformComponent->GotoWaypoint(value); + movingPlatformComponent->GotoWaypoint(mValue.value()); } Game::entityManager->SerializeEntity(closest); @@ -2198,13 +2198,11 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit } if (args.size() >= 3) { - int32_t faction; - if (!GeneralUtils::TryParse(args[2], faction)) { - return; - } + const auto faction = GeneralUtils::TryParse(args[2]); + if (!faction) return; destuctable->SetFaction(-1); - destuctable->AddFaction(faction, true); + destuctable->AddFaction(faction.value(), true); } } else if (args[1] == "-cf") { auto* destuctable = entity->GetComponent(); diff --git a/dMasterServer/InstanceManager.cpp b/dMasterServer/InstanceManager.cpp index a5a6d8dc..9ae9930c 100644 --- a/dMasterServer/InstanceManager.cpp +++ b/dMasterServer/InstanceManager.cpp @@ -17,7 +17,8 @@ InstanceManager::InstanceManager(Logger* logger, const std::string& externalIP) { mLogger = logger; mExternalIP = externalIP; - GeneralUtils::TryParse(Game::config->GetValue("world_port_start"), m_LastPort); + m_LastPort = + GeneralUtils::TryParse(Game::config->GetValue("world_port_start")).value_or(m_LastPort); m_LastInstanceID = LWOINSTANCEID_INVALID; } @@ -322,7 +323,7 @@ Instance* InstanceManager::FindPrivateInstance(const std::string& password) { } int InstanceManager::GetSoftCap(LWOMAPID mapID) { - CDZoneTableTable* zoneTable = CDClientManager::Instance().GetTable(); + CDZoneTableTable* zoneTable = CDClientManager::GetTable(); if (zoneTable) { const CDZoneTable* zone = zoneTable->Query(mapID); @@ -335,7 +336,7 @@ int InstanceManager::GetSoftCap(LWOMAPID mapID) { } int InstanceManager::GetHardCap(LWOMAPID mapID) { - CDZoneTableTable* zoneTable = CDClientManager::Instance().GetTable(); + CDZoneTableTable* zoneTable = CDClientManager::GetTable(); if (zoneTable) { const CDZoneTable* zone = zoneTable->Query(mapID); diff --git a/dMasterServer/InstanceManager.h b/dMasterServer/InstanceManager.h index b9f1ec5a..1fc4af43 100644 --- a/dMasterServer/InstanceManager.h +++ b/dMasterServer/InstanceManager.h @@ -134,7 +134,7 @@ private: Logger* mLogger; std::string mExternalIP; std::vector m_Instances; - unsigned short m_LastPort = 3000; + uint16_t m_LastPort = 3000; LWOINSTANCEID m_LastInstanceID; /** diff --git a/dMasterServer/MasterServer.cpp b/dMasterServer/MasterServer.cpp index a5691e49..1fade06e 100644 --- a/dMasterServer/MasterServer.cpp +++ b/dMasterServer/MasterServer.cpp @@ -36,7 +36,6 @@ #include "InstanceManager.h" #include "MasterPackets.h" #include "PersistentIDManager.h" -#include "PacketUtils.h" #include "FdbToSqlite.h" #include "BitStreamUtils.h" #include "Start.h" @@ -90,9 +89,8 @@ int main(int argc, char** argv) { if (!dConfig::Exists("worldconfig.ini")) LOG("Could not find worldconfig.ini, using default settings"); - uint32_t clientNetVersion = 171022; const auto clientNetVersionString = Game::config->GetValue("client_net_version"); - if (!clientNetVersionString.empty()) GeneralUtils::TryParse(clientNetVersionString, clientNetVersion); + const uint32_t clientNetVersion = GeneralUtils::TryParse(clientNetVersionString).value_or(171022); LOG("Using net version %i", clientNetVersion); @@ -178,17 +176,6 @@ int main(int argc, char** argv) { // Run migrations should any need to be run. MigrationRunner::RunSQLiteMigrations(); - //Get CDClient initial information - try { - CDClientManager::Instance(); - } catch (CppSQLite3Exception& e) { - LOG("Failed to initialize CDServer SQLite Database"); - LOG("May be caused by corrupted file: %s", (Game::assetManager->GetResPath() / "CDServer.sqlite").string().c_str()); - LOG("Error: %s", e.errorMessage()); - LOG("Error Code: %i", e.errorCode()); - return EXIT_FAILURE; - } - //If the first command line argument is -a or --account then make the user //input a username and password, with the password being hidden. if (argc > 1 && @@ -504,17 +491,17 @@ void HandlePacket(Packet* packet) { uint32_t theirZoneID = 0; uint32_t theirInstanceID = 0; ServerType theirServerType; - std::string theirIP = ""; + LUString theirIP; inStream.Read(theirPort); inStream.Read(theirZoneID); inStream.Read(theirInstanceID); inStream.Read(theirServerType); - theirIP = PacketUtils::ReadString(24, packet, false); //24 is the current offset + inStream.Read(theirIP); if (theirServerType == ServerType::World) { if (!Game::im->IsPortInUse(theirPort)) { - Instance* in = new Instance(theirIP, theirPort, theirZoneID, theirInstanceID, 0, 12, 12); + Instance* in = new Instance(theirIP.string, theirPort, theirZoneID, theirInstanceID, 0, 12, 12); SystemAddress copy; copy.binaryAddress = packet->systemAddress.binaryAddress; @@ -553,47 +540,42 @@ void HandlePacket(Packet* packet) { } case eMasterMessageType::SET_SESSION_KEY: { - RakNet::BitStream inStream(packet->data, packet->length, false); - uint64_t header = inStream.Read(header); + CINSTREAM_SKIP_HEADER; uint32_t sessionKey = 0; - std::string username; - inStream.Read(sessionKey); - username = PacketUtils::ReadString(12, packet, false); - + LUString username; + inStream.Read(username); + for (auto it : activeSessions) { - if (it.second == username) { + if (it.second == username.string) { activeSessions.erase(it.first); CBITSTREAM; BitStreamUtils::WriteHeader(bitStream, eConnectionType::MASTER, eMasterMessageType::NEW_SESSION_ALERT); bitStream.Write(sessionKey); - bitStream.Write(username.size()); - for (auto character : username) { - bitStream.Write(character); - } + bitStream.Write(username); SEND_PACKET_BROADCAST; break; } } - activeSessions.insert(std::make_pair(sessionKey, username)); - LOG("Got sessionKey %i for user %s", sessionKey, username.c_str()); + activeSessions.insert(std::make_pair(sessionKey, username.string)); + LOG("Got sessionKey %i for user %s", sessionKey, username.string.c_str()); break; } case eMasterMessageType::REQUEST_SESSION_KEY: { - RakNet::BitStream inStream(packet->data, packet->length, false); - uint64_t header = inStream.Read(header); - std::string username = PacketUtils::ReadString(8, packet, false); - + CINSTREAM_SKIP_HEADER; + LUWString username; + inStream.Read(username); + LOG("Requesting session key for %s", username.GetAsString().c_str()); for (auto key : activeSessions) { - if (key.second == username) { + if (key.second == username.GetAsString()) { CBITSTREAM; BitStreamUtils::WriteHeader(bitStream, eConnectionType::MASTER, eMasterMessageType::SESSION_KEY_RESPONSE); bitStream.Write(key.first); - bitStream.Write(LUString(key.second, 64)); + bitStream.Write(username); Game::server->Send(&bitStream, packet->systemAddress, false); break; } diff --git a/dNavigation/dNavMesh.cpp b/dNavigation/dNavMesh.cpp index ab9ed2d5..f49dd31e 100644 --- a/dNavigation/dNavMesh.cpp +++ b/dNavigation/dNavMesh.cpp @@ -11,6 +11,7 @@ #include "dZoneManager.h" #include "DluAssert.h" +#include "DetourExtensions.h" dNavMesh::dNavMesh(uint32_t zoneId) { m_ZoneId = zoneId; @@ -30,16 +31,8 @@ dNavMesh::dNavMesh(uint32_t zoneId) { dNavMesh::~dNavMesh() { // Clean up Recast information - if(m_Solid) rcFreeHeightField(m_Solid); - if (m_CHF) rcFreeCompactHeightfield(m_CHF); - if (m_CSet) rcFreeContourSet(m_CSet); - if (m_PMesh) rcFreePolyMesh(m_PMesh); - if (m_PMDMesh) rcFreePolyMeshDetail(m_PMDMesh); if (m_NavMesh) dtFreeNavMesh(m_NavMesh); if (m_NavQuery) dtFreeNavMeshQuery(m_NavQuery); - - if (m_Ctx) delete m_Ctx; - if (m_Triareas) delete[] m_Triareas; } diff --git a/dNavigation/dNavMesh.h b/dNavigation/dNavMesh.h index 600b8b86..8a55c649 100644 --- a/dNavigation/dNavMesh.h +++ b/dNavigation/dNavMesh.h @@ -2,13 +2,17 @@ #include #include -#include -#include -#include - -#include "DetourExtensions.h" class NiPoint3; +class rcHeightfield; +class rcCompactHeightfield; +class rcContourSet; +class rcPolyMesh; +class rcPolyMeshDetail; +class InputGeom; +class dtNavMesh; +class dtNavMeshQuery; +class rcContext; class dNavMesh { public: @@ -26,24 +30,14 @@ public: float GetHeightAtPoint(const NiPoint3& location, const float halfExtentsHeight = 32.0f) const; std::vector GetPath(const NiPoint3& startPos, const NiPoint3& endPos, float speed = 10.0f); - class dtNavMesh* GetdtNavMesh() { return m_NavMesh; } + bool IsNavmeshLoaded() { return m_NavMesh != nullptr; } private: void LoadNavmesh(); uint32_t m_ZoneId; - uint8_t* m_Triareas = nullptr; - rcHeightfield* m_Solid = nullptr; - rcCompactHeightfield* m_CHF = nullptr; - rcContourSet* m_CSet = nullptr; - rcPolyMesh* m_PMesh = nullptr; - rcConfig m_Config; - rcPolyMeshDetail* m_PMDMesh = nullptr; - - class InputGeom* m_Geometry = nullptr; - class dtNavMesh* m_NavMesh = nullptr; - class dtNavMeshQuery* m_NavQuery = nullptr; + dtNavMesh* m_NavMesh = nullptr; + dtNavMeshQuery* m_NavQuery = nullptr; uint8_t m_NavMeshDrawFlags; - rcContext* m_Ctx = nullptr; }; diff --git a/dNavigation/dTerrain/RawFile.cpp b/dNavigation/dTerrain/RawFile.cpp index dfad9ca4..efc2b39d 100644 --- a/dNavigation/dTerrain/RawFile.cpp +++ b/dNavigation/dTerrain/RawFile.cpp @@ -20,7 +20,7 @@ RawFile::RawFile(std::string fileName) { if (m_Version < 0x20) { - return; // Version too crusty to handle + return; // Version is too old to be supported } // Read in chunks diff --git a/dNet/AuthPackets.cpp b/dNet/AuthPackets.cpp index de8f5704..25ccc902 100644 --- a/dNet/AuthPackets.cpp +++ b/dNet/AuthPackets.cpp @@ -1,5 +1,4 @@ #include "AuthPackets.h" -#include "PacketUtils.h" #include "BitStreamUtils.h" #include "dNetCommon.h" @@ -8,8 +7,8 @@ #include "Database.h" #include "ZoneInstanceManager.h" #include "MD5.h" -#include "SHA512.h" #include "GeneralUtils.h" +#include "ClientVersion.h" #include @@ -40,10 +39,9 @@ void AuthPackets::LoadClaimCodes() { auto rcstring = Game::config->GetValue("rewardcodes"); auto codestrings = GeneralUtils::SplitString(rcstring, ','); for(auto const &codestring: codestrings){ - uint32_t code = -1; - if(GeneralUtils::TryParse(codestring, code) && code != -1){ - claimCodes.push_back(code); - } + const auto code = GeneralUtils::TryParse(codestring); + + if (code && code.value() != -1) claimCodes.push_back(code.value()); } } @@ -75,9 +73,8 @@ void AuthPackets::SendHandshake(dServer* server, const SystemAddress& sysAddr, c RakNet::BitStream bitStream; BitStreamUtils::WriteHeader(bitStream, eConnectionType::SERVER, eServerMessageType::VERSION_CONFIRM); - uint32_t clientNetVersion = 171022; const auto clientNetVersionString = Game::config->GetValue("client_net_version"); - if (!clientNetVersionString.empty()) GeneralUtils::TryParse(clientNetVersionString, clientNetVersion); + const uint32_t clientNetVersion = GeneralUtils::TryParse(clientNetVersionString).value_or(171022); bitStream.Write(clientNetVersion); bitStream.Write(861228100); @@ -96,7 +93,7 @@ void AuthPackets::HandleLoginRequest(dServer* server, Packet* packet) { std::vector stamps; stamps.emplace_back(eStamps::PASSPORT_AUTH_START, 0); - LUWString usernameLUString(33); + LUWString usernameLUString; inStream.Read(usernameLUString); const auto username = usernameLUString.GetAsString(); @@ -244,12 +241,12 @@ void AuthPackets::SendLoginResponse(dServer* server, const SystemAddress& sysAdd loginResponse.Write(LUString(Game::config->GetValue("event_7"))); loginResponse.Write(LUString(Game::config->GetValue("event_8"))); - uint16_t version_major = 1; - uint16_t version_current = 10; - uint16_t version_minor = 64; - GeneralUtils::TryParse(Game::config->GetValue("version_major"), version_major); - GeneralUtils::TryParse(Game::config->GetValue("version_current"), version_current); - GeneralUtils::TryParse(Game::config->GetValue("version_minor"), version_minor); + const uint16_t version_major = + GeneralUtils::TryParse(Game::config->GetValue("version_major")).value_or(ClientVersion::major); + const uint16_t version_current = + GeneralUtils::TryParse(Game::config->GetValue("version_current")).value_or(ClientVersion::current); + const uint16_t version_minor = + GeneralUtils::TryParse(Game::config->GetValue("version_minor")).value_or(ClientVersion::minor); loginResponse.Write(version_major); loginResponse.Write(version_current); @@ -297,13 +294,12 @@ void AuthPackets::SendLoginResponse(dServer* server, const SystemAddress& sysAdd for (auto& stamp : stamps) stamp.Serialize(&loginResponse); server->Send(&loginResponse, sysAddr, false); - //Inform the master server that we've created a session for this user: if (responseCode == eLoginResponse::SUCCESS) { CBITSTREAM; BitStreamUtils::WriteHeader(bitStream, eConnectionType::MASTER, eMasterMessageType::SET_SESSION_KEY); bitStream.Write(sessionKey); - bitStream.Write(LUString(username, 66)); + bitStream.Write(LUString(username)); server->SendToMaster(&bitStream); LOG("Set sessionKey: %i for user %s", sessionKey, username.c_str()); diff --git a/dNet/BitStreamUtils.h b/dNet/BitStreamUtils.h index 7403d0e1..1322ec95 100644 --- a/dNet/BitStreamUtils.h +++ b/dNet/BitStreamUtils.h @@ -12,7 +12,7 @@ struct LUString { std::string string; uint32_t size; - LUString(uint32_t size) { + LUString(uint32_t size = 33) { this->size = size; }; LUString(std::string string, uint32_t size = 33) { @@ -28,7 +28,7 @@ struct LUWString { std::u16string string; uint32_t size; - LUWString(uint32_t size) { + LUWString(uint32_t size = 33) { this->size = size; }; LUWString(std::u16string string, uint32_t size = 33) { diff --git a/dNet/CMakeLists.txt b/dNet/CMakeLists.txt index b4b77c82..68de8eb1 100644 --- a/dNet/CMakeLists.txt +++ b/dNet/CMakeLists.txt @@ -8,10 +8,5 @@ set(DNET_SOURCES "AuthPackets.cpp" "ZoneInstanceManager.cpp") add_library(dNet STATIC ${DNET_SOURCES}) -target_include_directories(dNet PRIVATE - ${PROJECT_SOURCE_DIR}/dGame/dComponents - ${PROJECT_SOURCE_DIR}/dScripts # transitive through components -) -target_link_libraries(dNet - PUBLIC dCommon dDatabase - INTERFACE dZoneManager) + +target_link_libraries(dNet PUBLIC dCommon) diff --git a/dNet/ChatPackets.cpp b/dNet/ChatPackets.cpp index 63eff5ef..d0354659 100644 --- a/dNet/ChatPackets.cpp +++ b/dNet/ChatPackets.cpp @@ -7,7 +7,6 @@ #include "RakNetTypes.h" #include "BitStream.h" #include "Game.h" -#include "PacketUtils.h" #include "BitStreamUtils.h" #include "dServer.h" #include "eConnectionType.h" diff --git a/dNet/ClientPackets.cpp b/dNet/ClientPackets.cpp index 47513a66..a6b9f8c6 100644 --- a/dNet/ClientPackets.cpp +++ b/dNet/ClientPackets.cpp @@ -4,422 +4,120 @@ */ #include "ClientPackets.h" -#include "UserManager.h" -#include "User.h" -#include "Character.h" -#include "EntityManager.h" -#include "Entity.h" -#include "ControllablePhysicsComponent.h" -#include "Game.h" -#include "Logger.h" -#include "WorldPackets.h" -#include "NiPoint3.h" -#include "NiQuaternion.h" #include "dCommonVars.h" -#include "BitStream.h" -#include "dChatFilter.h" -#include "WorldPackets.h" -#include "ChatPackets.h" -#include "dServer.h" -#include "GameMessages.h" -#include "dZoneManager.h" -#include "Player.h" -#include "Zone.h" -#include "PossessorComponent.h" -#include "PossessableComponent.h" -#include "HavokVehiclePhysicsComponent.h" -#include "dConfig.h" -#include "CharacterComponent.h" -#include "Database.h" -#include "eGameMasterLevel.h" -#include "eReplicaComponentType.h" -#include "CheatDetection.h" -#include "Amf3.h" - -void ClientPackets::HandleChatMessage(const SystemAddress& sysAddr, Packet* packet) { - User* user = UserManager::Instance()->GetUser(sysAddr); - if (!user) { - LOG("Unable to get user to parse chat message"); - return; - } - - if (user->GetIsMuted()) { - user->GetLastUsedChar()->SendMuteNotice(); - return; - } +#include "PositionUpdate.h" +ChatMessage ClientPackets::HandleChatMessage(Packet* packet) { CINSTREAM_SKIP_HEADER; - char chatChannel; - uint16_t unknown; + ChatMessage message; uint32_t messageLength; - std::u16string message; - inStream.Read(chatChannel); - inStream.Read(unknown); + inStream.Read(message.chatChannel); + inStream.Read(message.unknown); inStream.Read(messageLength); for (uint32_t i = 0; i < (messageLength - 1); ++i) { uint16_t character; inStream.Read(character); - message.push_back(character); + message.message.push_back(character); } - std::string playerName = user->GetLastUsedChar()->GetName(); - bool isMythran = user->GetLastUsedChar()->GetGMLevel() > eGameMasterLevel::CIVILIAN; - bool isOk = Game::chatFilter->IsSentenceOkay(GeneralUtils::UTF16ToWTF8(message), user->GetLastUsedChar()->GetGMLevel()).empty(); - LOG_DEBUG("Msg: %s was approved previously? %i", GeneralUtils::UTF16ToWTF8(message).c_str(), user->GetLastChatMessageApproved()); - if (!isOk) { - // Add a limit to the string converted by general utils because it is a user received string and may be a bad actor. - CheatDetection::ReportCheat( - user, - sysAddr, - "Player %s attempted to bypass chat filter with message: %s", - playerName.c_str(), - GeneralUtils::UTF16ToWTF8(message, 512).c_str()); - } - if (!isOk && !isMythran) return; - - std::string sMessage = GeneralUtils::UTF16ToWTF8(message); - LOG("%s: %s", playerName.c_str(), sMessage.c_str()); - ChatPackets::SendChatMessage(sysAddr, chatChannel, playerName, user->GetLoggedInChar(), isMythran, message); + return message; } -void ClientPackets::HandleClientPositionUpdate(const SystemAddress& sysAddr, Packet* packet) { - User* user = UserManager::Instance()->GetUser(sysAddr); - if (!user) { - LOG("Unable to get user to parse position update"); - return; - } - +PositionUpdate ClientPackets::HandleClientPositionUpdate(Packet* packet) { + PositionUpdate update; CINSTREAM_SKIP_HEADER; - Entity* entity = Game::entityManager->GetEntity(user->GetLastUsedChar()->GetObjectID()); - if (!entity) return; + inStream.Read(update.position.x); + inStream.Read(update.position.y); + inStream.Read(update.position.z); - ControllablePhysicsComponent* comp = static_cast(entity->GetComponent(eReplicaComponentType::CONTROLLABLE_PHYSICS)); - if (!comp) return; + inStream.Read(update.rotation.x); + inStream.Read(update.rotation.y); + inStream.Read(update.rotation.z); + inStream.Read(update.rotation.w); - /* - //If we didn't move, this will match and stop our velocity - if (packet->length == 37) { - NiPoint3 zeroVel(0.0f, 0.0f, 0.0f); - comp->SetVelocity(zeroVel); - comp->SetAngularVelocity(zeroVel); - comp->SetIsOnGround(true); //probably8 - Game::entityManager->SerializeEntity(entity); - return; - } - */ - - auto* possessorComponent = entity->GetComponent(); - - NiPoint3 position; - inStream.Read(position.x); - inStream.Read(position.y); - inStream.Read(position.z); - - NiQuaternion rotation; - inStream.Read(rotation.x); - inStream.Read(rotation.y); - inStream.Read(rotation.z); - inStream.Read(rotation.w); - - bool onGround = false; - bool onRail = false; - inStream.Read(onGround); - inStream.Read(onRail); + inStream.Read(update.onGround); + inStream.Read(update.onRail); bool velocityFlag = false; inStream.Read(velocityFlag); - NiPoint3 velocity{}; if (velocityFlag) { - inStream.Read(velocity.x); - inStream.Read(velocity.y); - inStream.Read(velocity.z); + inStream.Read(update.velocity.x); + inStream.Read(update.velocity.y); + inStream.Read(update.velocity.z); } bool angVelocityFlag = false; inStream.Read(angVelocityFlag); - NiPoint3 angVelocity{}; if (angVelocityFlag) { - inStream.Read(angVelocity.x); - inStream.Read(angVelocity.y); - inStream.Read(angVelocity.z); + inStream.Read(update.angularVelocity.x); + inStream.Read(update.angularVelocity.y); + inStream.Read(update.angularVelocity.z); } // TODO figure out how to use these. Ignoring for now, but reading in if they exist. bool hasLocalSpaceInfo{}; - LWOOBJID objectId{}; - NiPoint3 localSpacePosition{}; - bool hasLinearVelocity{}; - NiPoint3 linearVelocity{}; if (inStream.Read(hasLocalSpaceInfo) && hasLocalSpaceInfo) { - inStream.Read(objectId); - inStream.Read(localSpacePosition.x); - inStream.Read(localSpacePosition.y); - inStream.Read(localSpacePosition.z); + inStream.Read(update.localSpaceInfo.objectId); + inStream.Read(update.localSpaceInfo.position.x); + inStream.Read(update.localSpaceInfo.position.y); + inStream.Read(update.localSpaceInfo.position.z); + bool hasLinearVelocity = false; if (inStream.Read(hasLinearVelocity) && hasLinearVelocity) { - inStream.Read(linearVelocity.x); - inStream.Read(linearVelocity.y); - inStream.Read(linearVelocity.z); + inStream.Read(update.localSpaceInfo.linearVelocity.x); + inStream.Read(update.localSpaceInfo.linearVelocity.y); + inStream.Read(update.localSpaceInfo.linearVelocity.z); } } + bool hasRemoteInputInfo{}; - RemoteInputInfo remoteInput{}; - if (inStream.Read(hasRemoteInputInfo) && hasRemoteInputInfo) { - inStream.Read(remoteInput.m_RemoteInputX); - inStream.Read(remoteInput.m_RemoteInputY); - inStream.Read(remoteInput.m_IsPowersliding); - inStream.Read(remoteInput.m_IsModified); + inStream.Read(update.remoteInputInfo.m_RemoteInputX); + inStream.Read(update.remoteInputInfo.m_RemoteInputY); + inStream.Read(update.remoteInputInfo.m_IsPowersliding); + inStream.Read(update.remoteInputInfo.m_IsModified); } - bool updateChar = true; - - if (possessorComponent != nullptr) { - auto* possassableEntity = Game::entityManager->GetEntity(possessorComponent->GetPossessable()); - - if (possassableEntity != nullptr) { - auto* possessableComponent = possassableEntity->GetComponent(); - if (possessableComponent) { - // While possessing something, only update char if we are attached to the thing we are possessing - if (possessableComponent->GetPossessionType() != ePossessionType::ATTACHED_VISIBLE) updateChar = false; - } - - auto* havokVehiclePhysicsComponent = possassableEntity->GetComponent(); - if (havokVehiclePhysicsComponent != nullptr) { - havokVehiclePhysicsComponent->SetPosition(position); - havokVehiclePhysicsComponent->SetRotation(rotation); - havokVehiclePhysicsComponent->SetIsOnGround(onGround); - havokVehiclePhysicsComponent->SetIsOnRail(onRail); - havokVehiclePhysicsComponent->SetVelocity(velocity); - havokVehiclePhysicsComponent->SetDirtyVelocity(velocityFlag); - havokVehiclePhysicsComponent->SetAngularVelocity(angVelocity); - havokVehiclePhysicsComponent->SetDirtyAngularVelocity(angVelocityFlag); - havokVehiclePhysicsComponent->SetRemoteInputInfo(remoteInput); - } else { - // Need to get the mount's controllable physics - auto* controllablePhysicsComponent = possassableEntity->GetComponent(); - if (!controllablePhysicsComponent) return; - controllablePhysicsComponent->SetPosition(position); - controllablePhysicsComponent->SetRotation(rotation); - controllablePhysicsComponent->SetIsOnGround(onGround); - controllablePhysicsComponent->SetIsOnRail(onRail); - controllablePhysicsComponent->SetVelocity(velocity); - controllablePhysicsComponent->SetDirtyVelocity(velocityFlag); - controllablePhysicsComponent->SetAngularVelocity(angVelocity); - controllablePhysicsComponent->SetDirtyAngularVelocity(angVelocityFlag); - } - Game::entityManager->SerializeEntity(possassableEntity); - } - } - - if (!updateChar) { - velocity = NiPoint3::ZERO; - angVelocity = NiPoint3::ZERO; - } - - - - // Handle statistics - auto* characterComponent = entity->GetComponent(); - if (characterComponent != nullptr) { - characterComponent->TrackPositionUpdate(position); - } - - comp->SetPosition(position); - comp->SetRotation(rotation); - comp->SetIsOnGround(onGround); - comp->SetIsOnRail(onRail); - comp->SetVelocity(velocity); - comp->SetDirtyVelocity(velocityFlag); - comp->SetAngularVelocity(angVelocity); - comp->SetDirtyAngularVelocity(angVelocityFlag); - - auto* player = static_cast(entity); - player->SetGhostReferencePoint(position); - Game::entityManager->QueueGhostUpdate(player->GetObjectID()); - - if (updateChar) Game::entityManager->SerializeEntity(entity); - - //TODO: add moving platform stuffs - /*bool movingPlatformFlag; - inStream.Read(movingPlatformFlag); - if (movingPlatformFlag) { - LWOOBJID objectID; - NiPoint3 niData2; - - inStream.Read(objectID); - inStream.Read(niData2.x); - inStream.Read(niData2.y); - inStream.Read(niData2.z); - - - - bool niData3Flag; - inStream.Read(niData3Flag); - if (niData3Flag) { - NiPoint3 niData3; - inStream.Read(niData3.x); - inStream.Read(niData3.y); - inStream.Read(niData3.z); - - controllablePhysics->GetLocationData()->GetMovingPlatformData()->SetData3(niData3); - } - }*/ - - /* - for (int i = 0; i < Game::server->GetReplicaManager()->GetParticipantCount(); ++i) - { - const auto& player = Game::server->GetReplicaManager()->GetParticipantAtIndex(i); - - if (entity->GetSystemAddress() == player) - { - continue; - } - - Game::entityManager->SerializeEntity(entity, player); - } - */ + return update; } -void ClientPackets::HandleChatModerationRequest(const SystemAddress& sysAddr, Packet* packet) { - User* user = UserManager::Instance()->GetUser(sysAddr); - if (!user) { - LOG("Unable to get user to parse chat moderation request"); - return; - } +ChatModerationRequest ClientPackets::HandleChatModerationRequest(Packet* packet) { + CINSTREAM_SKIP_HEADER; + + ChatModerationRequest request; - auto* entity = Player::GetPlayer(sysAddr); - - if (entity == nullptr) { - LOG("Unable to get player to parse chat moderation request"); - return; - } - - // Check if the player has restricted chat access - auto* character = entity->GetCharacter(); - - if (character->HasPermission(ePermissionMap::RestrictedChatAccess)) { - // Send a message to the player - ChatPackets::SendSystemMessage( - sysAddr, - u"This character has restricted chat access." - ); - - return; - } - - RakNet::BitStream stream(packet->data, packet->length, false); - - uint64_t header; - stream.Read(header); - - // Data - uint8_t chatLevel; - uint8_t requestID; - uint16_t messageLength; - - std::string receiver = ""; - std::string message = ""; - - stream.Read(chatLevel); - stream.Read(requestID); + inStream.Read(request.chatLevel); + inStream.Read(request.requestID); for (uint32_t i = 0; i < 42; ++i) { uint16_t character; - stream.Read(character); - receiver.push_back(static_cast(character)); + inStream.Read(character); + request.receiver.push_back(static_cast(character)); } - if (!receiver.empty()) { - if (std::string(receiver.c_str(), 4) == "[GM]") { // Shift the string forward if we are speaking to a GM as the client appends "[GM]" if they are - receiver = std::string(receiver.c_str() + 4, receiver.size() - 4); + if (!request.receiver.empty()) { + if (std::string(request.receiver.c_str(), 4) == "[GM]") { // Shift the string forward if we are speaking to a GM as the client appends "[GM]" if they are + request.receiver = std::string(request.receiver.c_str() + 4, request.receiver.size() - 4); } } - stream.Read(messageLength); + uint16_t messageLength; + inStream.Read(messageLength); for (uint32_t i = 0; i < messageLength; ++i) { uint16_t character; - stream.Read(character); - message.push_back(static_cast(character)); + inStream.Read(character); + request.message.push_back(static_cast(character)); } - bool isBestFriend = false; - - if (chatLevel == 1) { - // Private chat - LWOOBJID idOfReceiver = LWOOBJID_EMPTY; - - { - auto characterIdFetch = Database::Get()->GetCharacterInfo(receiver); - - if (characterIdFetch) { - idOfReceiver = characterIdFetch->id; - } - } - const auto& bffMap = user->GetIsBestFriendMap(); - if (bffMap.find(receiver) == bffMap.end() && idOfReceiver != LWOOBJID_EMPTY) { - auto bffInfo = Database::Get()->GetBestFriendStatus(entity->GetObjectID(), idOfReceiver); - - if (bffInfo) { - isBestFriend = bffInfo->bestFriendStatus == 3; - } - - if (isBestFriend) { - user->UpdateBestFriendValue(receiver, true); - } - } else if (bffMap.find(receiver) != bffMap.end()) { - isBestFriend = true; - } - } - - std::vector> segments = Game::chatFilter->IsSentenceOkay(message, entity->GetGMLevel(), !(isBestFriend && chatLevel == 1)); - - bool bAllClean = segments.empty(); - - if (user->GetIsMuted()) { - bAllClean = false; - } - - user->SetLastChatMessageApproved(bAllClean); - WorldPackets::SendChatModerationResponse(sysAddr, bAllClean, requestID, receiver, segments); + return request; } -void ClientPackets::SendTop5HelpIssues(Packet* packet) { - auto* user = UserManager::Instance()->GetUser(packet->systemAddress); - if (!user) return; - auto* character = user->GetLastUsedChar(); - if (!character) return; - auto * entity = character->GetEntity(); - if (!entity) return; - +int32_t ClientPackets::SendTop5HelpIssues(Packet* packet) { CINSTREAM_SKIP_HEADER; int32_t language = 0; inStream.Read(language); - - // TODO: Handle different languages in a nice way - // 0: en_US - // 1: pl_US - // 2: de_DE - // 3: en_GB - - AMFArrayValue data; - // Summaries - data.Insert("Summary0", Game::config->GetValue("help_0_summary")); - data.Insert("Summary1", Game::config->GetValue("help_1_summary")); - data.Insert("Summary2", Game::config->GetValue("help_2_summary")); - data.Insert("Summary3", Game::config->GetValue("help_3_summary")); - data.Insert("Summary4", Game::config->GetValue("help_4_summary")); - - // Descriptions - data.Insert("Description0", Game::config->GetValue("help_0_description")); - data.Insert("Description1", Game::config->GetValue("help_1_description")); - data.Insert("Description2", Game::config->GetValue("help_2_description")); - data.Insert("Description3", Game::config->GetValue("help_3_description")); - data.Insert("Description4", Game::config->GetValue("help_4_description")); - - GameMessages::SendUIMessageServerToSingleClient(entity, packet->systemAddress, "UIHelpTop5", data); - + return language; } diff --git a/dNet/ClientPackets.h b/dNet/ClientPackets.h index 5b9fd76d..a7d2941b 100644 --- a/dNet/ClientPackets.h +++ b/dNet/ClientPackets.h @@ -6,13 +6,31 @@ #ifndef CLIENTPACKETS_H #define CLIENTPACKETS_H -#include "RakNetTypes.h" +#include +#include + +class PositionUpdate; + +struct Packet; + +struct ChatMessage { + uint8_t chatChannel = 0; + uint16_t unknown = 0; + std::u16string message; +}; + +struct ChatModerationRequest { + uint8_t chatLevel = 0; + uint8_t requestID = 0; + std::string receiver; + std::string message; +}; namespace ClientPackets { - void HandleChatMessage(const SystemAddress& sysAddr, Packet* packet); - void HandleClientPositionUpdate(const SystemAddress& sysAddr, Packet* packet); - void HandleChatModerationRequest(const SystemAddress& sysAddr, Packet* packet); - void SendTop5HelpIssues(Packet* packet); + ChatMessage HandleChatMessage(Packet* packet); + PositionUpdate HandleClientPositionUpdate(Packet* packet); + ChatModerationRequest HandleChatModerationRequest(Packet* packet); + int32_t SendTop5HelpIssues(Packet* packet); }; #endif // CLIENTPACKETS_H diff --git a/dNet/MasterPackets.cpp b/dNet/MasterPackets.cpp index 624b92ad..6d70fedb 100644 --- a/dNet/MasterPackets.cpp +++ b/dNet/MasterPackets.cpp @@ -1,6 +1,5 @@ #include "MasterPackets.h" #include "BitStream.h" -#include "PacketUtils.h" #include "dCommonVars.h" #include "dServer.h" #include "eConnectionType.h" @@ -88,7 +87,7 @@ void MasterPackets::SendZoneTransferResponse(dServer* server, const SystemAddres bitStream.Write(zoneInstance); bitStream.Write(zoneClone); bitStream.Write(serverPort); - bitStream.Write(LUString(serverIP, static_cast(serverIP.size() + 1))); + bitStream.Write(LUString(serverIP, 255)); server->Send(&bitStream, sysAddr, false); } @@ -100,12 +99,12 @@ void MasterPackets::HandleServerInfo(Packet* packet) { uint32_t theirPort = 0; uint32_t theirZoneID = 0; uint32_t theirInstanceID = 0; - std::string theirIP = ""; + LUString theirIP; inStream.Read(theirPort); inStream.Read(theirZoneID); inStream.Read(theirInstanceID); - theirIP = PacketUtils::ReadString(inStream.GetReadOffset(), packet, false); //20 is the current offset + inStream.Read(theirIP); //TODO: Actually mark this server as an available server in the manager } @@ -118,7 +117,7 @@ void MasterPackets::SendServerInfo(dServer* server, Packet* packet) { bitStream.Write(server->GetZoneID()); bitStream.Write(server->GetInstanceID()); bitStream.Write(server->GetServerType()); - bitStream.Write(LUString(server->GetIP(), server->GetIP().size())); + bitStream.Write(LUString(server->GetIP())); server->SendToMaster(&bitStream); } diff --git a/dNet/PacketUtils.cpp b/dNet/PacketUtils.cpp index 95c3b2b1..5394cff0 100644 --- a/dNet/PacketUtils.cpp +++ b/dNet/PacketUtils.cpp @@ -1,64 +1,8 @@ #include "PacketUtils.h" -#include #include #include "Logger.h" #include "Game.h" -uint16_t PacketUtils::ReadU16(uint32_t startLoc, Packet* packet) { - if (startLoc + 2 > packet->length) return 0; - - std::vector t; - for (uint32_t i = startLoc; i < startLoc + 2; i++) t.push_back(packet->data[i]); - return *(uint16_t*)t.data(); -} - -uint32_t PacketUtils::ReadU32(uint32_t startLoc, Packet* packet) { - if (startLoc + 4 > packet->length) return 0; - - std::vector t; - for (uint32_t i = startLoc; i < startLoc + 4; i++) { - t.push_back(packet->data[i]); - } - return *(uint32_t*)t.data(); -} - -uint64_t PacketUtils::ReadU64(uint32_t startLoc, Packet* packet) { - if (startLoc + 8 > packet->length) return 0; - - std::vector t; - for (uint32_t i = startLoc; i < startLoc + 8; i++) t.push_back(packet->data[i]); - return *(uint64_t*)t.data(); -} - -int64_t PacketUtils::ReadS64(uint32_t startLoc, Packet* packet) { - if (startLoc + 8 > packet->length) return 0; - - std::vector t; - for (size_t i = startLoc; i < startLoc + 8; i++) t.push_back(packet->data[i]); - return *(int64_t*)t.data(); -} - -std::string PacketUtils::ReadString(uint32_t startLoc, Packet* packet, bool wide, uint32_t maxLen) { - std::string readString = ""; - - if (wide) maxLen *= 2; - - if (packet->length > startLoc) { - uint32_t i = 0; - while (packet->data[startLoc + i] != '\0' && packet->length > static_cast(startLoc + i) && maxLen > i) { - readString.push_back(packet->data[startLoc + i]); - - if (wide) { - i += 2; // Wide-char string - } else { - i++; // Regular string - } - } - } - - return readString; -} - //! Saves a packet to the filesystem void PacketUtils::SavePacket(const std::string& filename, const char* data, size_t length) { //If we don't log to the console, don't save the bin files either. This takes up a lot of time. diff --git a/dNet/PacketUtils.h b/dNet/PacketUtils.h index f8558dfd..3cd44b5f 100644 --- a/dNet/PacketUtils.h +++ b/dNet/PacketUtils.h @@ -8,11 +8,6 @@ enum class eConnectionType : uint16_t; namespace PacketUtils { - uint16_t ReadU16(uint32_t startLoc, Packet* packet); - uint32_t ReadU32(uint32_t startLoc, Packet* packet); - uint64_t ReadU64(uint32_t startLoc, Packet* packet); - int64_t ReadS64(uint32_t startLoc, Packet* packet); - std::string ReadString(uint32_t startLoc, Packet* packet, bool wide, uint32_t maxLen = 33); void SavePacket(const std::string& filename, const char* data, size_t length); }; diff --git a/dNet/WorldPackets.cpp b/dNet/WorldPackets.cpp index 1ee98c83..f92a971f 100644 --- a/dNet/WorldPackets.cpp +++ b/dNet/WorldPackets.cpp @@ -1,26 +1,21 @@ -#include "dCommonVars.h" #include "WorldPackets.h" +#include "dCommonVars.h" #include "BitStream.h" -#include "PacketUtils.h" #include "GeneralUtils.h" -#include "User.h" -#include "Character.h" #include "Logger.h" -#include #include "Game.h" #include "LDFFormat.h" #include "dServer.h" -#include "dZoneManager.h" -#include "CharacterComponent.h" #include "ZCompression.h" #include "eConnectionType.h" #include "BitStreamUtils.h" -void WorldPackets::SendLoadStaticZone(const SystemAddress& sysAddr, float x, float y, float z, uint32_t checksum) { +#include + +void WorldPackets::SendLoadStaticZone(const SystemAddress& sysAddr, float x, float y, float z, uint32_t checksum, LWOZONEID zone) { RakNet::BitStream bitStream; BitStreamUtils::WriteHeader(bitStream, eConnectionType::CLIENT, eClientMessageType::LOAD_STATIC_ZONE); - auto zone = Game::zoneManager->GetZone()->GetZoneID(); bitStream.Write(zone.GetMapID()); bitStream.Write(zone.GetInstanceID()); //bitStream.Write(zone.GetCloneID()); @@ -38,57 +33,6 @@ void WorldPackets::SendLoadStaticZone(const SystemAddress& sysAddr, float x, flo SEND_PACKET; } -void WorldPackets::SendCharacterList(const SystemAddress& sysAddr, User* user) { - if (!user) return; - - RakNet::BitStream bitStream; - BitStreamUtils::WriteHeader(bitStream, eConnectionType::CLIENT, eClientMessageType::CHARACTER_LIST_RESPONSE); - - std::vector characters = user->GetCharacters(); - bitStream.Write(characters.size()); - bitStream.Write(0); //character index in front, just picking 0 - - for (uint32_t i = 0; i < characters.size(); ++i) { - bitStream.Write(characters[i]->GetObjectID()); - bitStream.Write(0); - - bitStream.Write(LUWString(characters[i]->GetName())); - bitStream.Write(LUWString(characters[i]->GetUnapprovedName())); - - bitStream.Write(characters[i]->GetNameRejected()); - bitStream.Write(false); - - bitStream.Write(LUString("", 10)); - - bitStream.Write(characters[i]->GetShirtColor()); - bitStream.Write(characters[i]->GetShirtStyle()); - bitStream.Write(characters[i]->GetPantsColor()); - bitStream.Write(characters[i]->GetHairStyle()); - bitStream.Write(characters[i]->GetHairColor()); - bitStream.Write(characters[i]->GetLeftHand()); - bitStream.Write(characters[i]->GetRightHand()); - bitStream.Write(characters[i]->GetEyebrows()); - bitStream.Write(characters[i]->GetEyes()); - bitStream.Write(characters[i]->GetMouth()); - bitStream.Write(0); - - bitStream.Write(characters[i]->GetZoneID()); - bitStream.Write(characters[i]->GetZoneInstance()); - bitStream.Write(characters[i]->GetZoneClone()); - - bitStream.Write(characters[i]->GetLastLogin()); - - const auto& equippedItems = characters[i]->GetEquippedItems(); - bitStream.Write(equippedItems.size()); - - for (uint32_t j = 0; j < equippedItems.size(); ++j) { - bitStream.Write(equippedItems[j]); - } - } - - SEND_PACKET; -} - void WorldPackets::SendCharacterCreationResponse(const SystemAddress& sysAddr, eCharacterCreationResponse response) { RakNet::BitStream bitStream; BitStreamUtils::WriteHeader(bitStream, eConnectionType::CLIENT, eClientMessageType::CHARACTER_CREATE_RESPONSE); @@ -128,26 +72,20 @@ void WorldPackets::SendServerState(const SystemAddress& sysAddr) { SEND_PACKET; } -void WorldPackets::SendCreateCharacter(const SystemAddress& sysAddr, Entity* entity, const std::string& xmlData, const std::u16string& username, eGameMasterLevel gm) { +void WorldPackets::SendCreateCharacter(const SystemAddress& sysAddr, int64_t reputation, LWOOBJID player, const std::string& xmlData, const std::u16string& username, eGameMasterLevel gm) { RakNet::BitStream bitStream; BitStreamUtils::WriteHeader(bitStream, eConnectionType::CLIENT, eClientMessageType::CREATE_CHARACTER); RakNet::BitStream data; data.Write(7); //LDF key count - auto character = entity->GetComponent(); - if (!character) { - LOG("Entity is not a character?? what??"); - return; - } - - LDFData* objid = new LDFData(u"objid", entity->GetObjectID()); - LDFData* lot = new LDFData(u"template", 1); - LDFData* xmlConfigData = new LDFData(u"xmlData", xmlData); - LDFData* name = new LDFData(u"name", username); - LDFData* gmlevel = new LDFData(u"gmlevel", static_cast(gm)); - LDFData* chatmode = new LDFData(u"chatmode", static_cast(gm)); - LDFData* reputation = new LDFData(u"reputation", character->GetReputation()); + std::unique_ptr> objid(new LDFData(u"objid", player)); + std::unique_ptr> lot(new LDFData(u"template", 1)); + std::unique_ptr> xmlConfigData(new LDFData(u"xmlData", xmlData)); + std::unique_ptr> name(new LDFData(u"name", username)); + std::unique_ptr> gmlevel(new LDFData(u"gmlevel", static_cast(gm))); + std::unique_ptr> chatmode(new LDFData(u"chatmode", static_cast(gm))); + std::unique_ptr> reputationLdf(new LDFData(u"reputation", reputation)); objid->WriteToPacket(&data); lot->WriteToPacket(&data); @@ -155,15 +93,7 @@ void WorldPackets::SendCreateCharacter(const SystemAddress& sysAddr, Entity* ent gmlevel->WriteToPacket(&data); chatmode->WriteToPacket(&data); xmlConfigData->WriteToPacket(&data); - reputation->WriteToPacket(&data); - - delete objid; - delete lot; - delete xmlConfigData; - delete gmlevel; - delete chatmode; - delete name; - delete reputation; + reputationLdf->WriteToPacket(&data); //Compress the data before sending: const uint32_t reservedSize = ZCompression::GetMaxCompressedLength(data.GetNumberOfBytesUsed()); @@ -187,14 +117,12 @@ void WorldPackets::SendCreateCharacter(const SystemAddress& sysAddr, Entity* ent * an assertion is done to prevent bad data from being saved or sent. */ #pragma warning(disable:6385) // C6385 Reading invalid data from 'compressedData'. - for (size_t i = 0; i < size; i++) - bitStream.Write(compressedData[i]); + bitStream.WriteAlignedBytes(compressedData, size); #pragma warning(default:6385) - // PacketUtils::SavePacket("chardata.bin", (const char*)bitStream.GetData(), static_cast(bitStream.GetNumberOfBytesUsed())); SEND_PACKET; delete[] compressedData; - LOG("Sent CreateCharacter for ID: %llu", entity->GetObjectID()); + LOG("Sent CreateCharacter for ID: %llu", player); } void WorldPackets::SendChatModerationResponse(const SystemAddress& sysAddr, bool requestAccepted, uint32_t requestID, const std::string& receiver, std::vector> unacceptedItems) { diff --git a/dNet/WorldPackets.h b/dNet/WorldPackets.h index ea8186c7..0d5de079 100644 --- a/dNet/WorldPackets.h +++ b/dNet/WorldPackets.h @@ -2,9 +2,8 @@ #define WORLDPACKETS_H #include "dCommonVars.h" +#include #include -#include -#include "Entity.h" class User; struct SystemAddress; @@ -13,14 +12,13 @@ enum class eCharacterCreationResponse : uint8_t; enum class eRenameResponse : uint8_t; namespace WorldPackets { - void SendLoadStaticZone(const SystemAddress& sysAddr, float x, float y, float z, uint32_t checksum); - void SendCharacterList(const SystemAddress& sysAddr, User* user); + void SendLoadStaticZone(const SystemAddress& sysAddr, float x, float y, float z, uint32_t checksum, LWOZONEID zone); void SendCharacterCreationResponse(const SystemAddress& sysAddr, eCharacterCreationResponse response); void SendCharacterRenameResponse(const SystemAddress& sysAddr, eRenameResponse response); void SendCharacterDeleteResponse(const SystemAddress& sysAddr, bool response); void SendTransferToWorld(const SystemAddress& sysAddr, const std::string& serverIP, uint32_t serverPort, bool mythranShift); void SendServerState(const SystemAddress& sysAddr); - void SendCreateCharacter(const SystemAddress& sysAddr, Entity* entity, const std::string& xmlData, const std::u16string& username, eGameMasterLevel gm); + void SendCreateCharacter(const SystemAddress& sysAddr, int64_t reputation, LWOOBJID player, const std::string& xmlData, const std::u16string& username, eGameMasterLevel gm); void SendChatModerationResponse(const SystemAddress& sysAddr, bool requestAccepted, uint32_t requestID, const std::string& receiver, std::vector> unacceptedItems); void SendGMLevelChange(const SystemAddress& sysAddr, bool success, eGameMasterLevel highestLevel, eGameMasterLevel prevLevel, eGameMasterLevel newLevel); } diff --git a/dNet/ZoneInstanceManager.cpp b/dNet/ZoneInstanceManager.cpp index f9285579..354d3634 100644 --- a/dNet/ZoneInstanceManager.cpp +++ b/dNet/ZoneInstanceManager.cpp @@ -3,7 +3,6 @@ // Custom Classes #include "MasterPackets.h" -#include "PacketUtils.h" #include "dServer.h" // C++ @@ -25,20 +24,30 @@ void ZoneInstanceManager::RequestZoneTransfer(dServer* server, uint32_t zoneID, } //! Handles a zone transfer response -void ZoneInstanceManager::HandleRequestZoneTransferResponse(uint64_t requestID, Packet* packet) { - - bool mythranShift = static_cast(packet->data[16]); - uint32_t zoneID = PacketUtils::ReadU32(17, packet); - uint32_t zoneInstance = PacketUtils::ReadU32(21, packet); - uint32_t zoneClone = PacketUtils::ReadU32(25, packet); - uint16_t serverPort = PacketUtils::ReadU16(29, packet); - std::string serverIP = PacketUtils::ReadString(31, packet, false); +void ZoneInstanceManager::HandleRequestZoneTransferResponse(Packet* packet) { + CINSTREAM_SKIP_HEADER; + uint64_t requestID; + inStream.Read(requestID); + bool mythranShift; + uint8_t tmp; + inStream.Read(tmp); + mythranShift = tmp > 0; + uint32_t zoneID; + inStream.Read(zoneID); + uint32_t zoneInstance; + inStream.Read(zoneInstance); + uint32_t zoneClone; + inStream.Read(zoneClone); + uint16_t serverPort; + inStream.Read(serverPort); + LUString serverIP(255); + inStream.Read(serverIP); for (uint32_t i = 0; i < this->requests.size(); ++i) { if (this->requests[i]->requestID == requestID) { // Call the request callback - this->requests[i]->callback(mythranShift, zoneID, zoneInstance, zoneClone, serverIP, serverPort); + this->requests[i]->callback(mythranShift, zoneID, zoneInstance, zoneClone, serverIP.string, serverPort); delete this->requests[i]; this->requests.erase(this->requests.begin() + i); diff --git a/dNet/ZoneInstanceManager.h b/dNet/ZoneInstanceManager.h index 3f8da1ae..47080cde 100644 --- a/dNet/ZoneInstanceManager.h +++ b/dNet/ZoneInstanceManager.h @@ -1,7 +1,5 @@ #pragma once -// C++ -#define _VARIADIC_MAX 10 #include #include #include @@ -56,7 +54,7 @@ public: \param requestID The request ID \param packet The packet */ - void HandleRequestZoneTransferResponse(uint64_t requestID, Packet* packet); + void HandleRequestZoneTransferResponse(Packet* packet); void CreatePrivateZone(dServer* server, uint32_t zoneID, uint32_t zoneClone, const std::string& password); diff --git a/dNet/dServer.cpp b/dNet/dServer.cpp index e60e04f9..ed66b42c 100644 --- a/dNet/dServer.cpp +++ b/dNet/dServer.cpp @@ -10,7 +10,6 @@ #include "eServerMessageType.h" #include "eMasterMessageType.h" -#include "PacketUtils.h" #include "BitStreamUtils.h" #include "MasterPackets.h" #include "ZoneInstanceManager.h" @@ -127,8 +126,7 @@ Packet* dServer::ReceiveFromMaster() { if (static_cast(packet->data[1]) == eConnectionType::MASTER) { switch (static_cast(packet->data[3])) { case eMasterMessageType::REQUEST_ZONE_TRANSFER_RESPONSE: { - uint64_t requestID = PacketUtils::ReadU64(8, packet); - ZoneInstanceManager::Instance()->HandleRequestZoneTransferResponse(requestID, packet); + ZoneInstanceManager::Instance()->HandleRequestZoneTransferResponse(packet); break; } case eMasterMessageType::SHUTDOWN: diff --git a/dPhysics/dpEntity.cpp b/dPhysics/dpEntity.cpp index 70bddb40..cbe3f5e8 100644 --- a/dPhysics/dpEntity.cpp +++ b/dPhysics/dpEntity.cpp @@ -76,7 +76,7 @@ void dpEntity::CheckCollision(dpEntity* other) { return; } - bool wasFound = (m_CurrentlyCollidingObjects.find(other->GetObjectID()) != m_CurrentlyCollidingObjects.end()); + bool wasFound = m_CurrentlyCollidingObjects.contains(other->GetObjectID()); bool isColliding = m_CollisionShape->IsColliding(other->GetShape()); diff --git a/dPhysics/dpGrid.cpp b/dPhysics/dpGrid.cpp index 1704e068..8ec944fd 100644 --- a/dPhysics/dpGrid.cpp +++ b/dPhysics/dpGrid.cpp @@ -8,24 +8,14 @@ dpGrid::dpGrid(int numCells, int cellSize) { CELL_SIZE = cellSize; m_DeleteGrid = true; - //fill x - for (int i = 0; i < NUM_CELLS; i++) { - m_Cells.push_back(std::vector>()); - } - - //fill z - for (int i = 0; i < NUM_CELLS; i++) { - for (int i = 0; i < NUM_CELLS; i++) { - m_Cells[i].push_back(std::forward_list()); - } - } + m_Cells.resize(NUM_CELLS, std::vector>(NUM_CELLS)); } dpGrid::~dpGrid() { if (!this->m_DeleteGrid) return; for (auto& x : m_Cells) { //x - for (auto& y : x) { //y - for (auto en : y) { + for (auto& z : x) { //y + for (auto en : z) { if (!en) continue; delete en; en = nullptr; @@ -39,13 +29,12 @@ void dpGrid::Add(dpEntity* entity) { int cellX = (int)std::round(entity->m_Position.x) / dpGrid::CELL_SIZE + NUM_CELLS / 2; int cellZ = (int)std::round(entity->m_Position.z) / dpGrid::CELL_SIZE + NUM_CELLS / 2; - if (cellX < 0) cellX = 0; - if (cellZ < 0) cellZ = 0; - if (cellX >= NUM_CELLS) cellX = NUM_CELLS - 1; - if (cellZ >= NUM_CELLS) cellZ = NUM_CELLS - 1; + // Clamp values to the range [0, NUM_CELLS - 1] + cellX = std::clamp(cellX, 0, NUM_CELLS - 1); + cellZ = std::clamp(cellZ, 0, NUM_CELLS - 1); //Add to cell: - m_Cells[cellX][cellZ].push_front(entity); + m_Cells[cellX][cellZ].push_back(entity); //To verify that the object isn't gargantuan: if (entity->GetScale() >= CELL_SIZE * 2 || entity->GetIsGargantuan()) @@ -59,23 +48,27 @@ void dpGrid::Move(dpEntity* entity, float x, float z) { int cellX = (int)std::round(x) / dpGrid::CELL_SIZE + NUM_CELLS / 2; int cellZ = (int)std::round(z) / dpGrid::CELL_SIZE + NUM_CELLS / 2; - if (cellX < 0) cellX = 0; - if (cellZ < 0) cellZ = 0; - if (cellX >= NUM_CELLS) cellX = NUM_CELLS - 1; - if (cellZ >= NUM_CELLS) cellZ = NUM_CELLS - 1; + // Clamp values to the range [0, NUM_CELLS - 1] + cellX = std::clamp(cellX, 0, NUM_CELLS - 1); + cellZ = std::clamp(cellZ, 0, NUM_CELLS - 1); - if (oldCellX < 0) oldCellX = 0; - if (oldCellZ < 0) oldCellZ = 0; - if (oldCellX >= NUM_CELLS) oldCellX = NUM_CELLS - 1; - if (oldCellZ >= NUM_CELLS) oldCellZ = NUM_CELLS - 1; + oldCellX = std::clamp(oldCellX, 0, NUM_CELLS - 1); + oldCellZ = std::clamp(oldCellZ, 0, NUM_CELLS - 1); if (oldCellX == cellX && oldCellZ == cellZ) return; - //Remove from perv cell: - m_Cells[oldCellX][oldCellZ].remove(entity); + //Remove from prev cell: + auto& cell = m_Cells[oldCellX][oldCellZ]; + + // For speed, find the single match and swap it with the last element, then pop_back. + auto toRemove = std::find(cell.begin(), cell.end(), entity); + if (toRemove != cell.end()) { + *toRemove = cell.back(); + cell.pop_back(); + } //Add to the new cell - m_Cells[cellX][cellZ].push_front(entity); + m_Cells[cellX][cellZ].push_back(entity); } void dpGrid::Delete(dpEntity* entity) { @@ -83,15 +76,18 @@ void dpGrid::Delete(dpEntity* entity) { int oldCellX = (int)std::round(entity->m_Position.x) / dpGrid::CELL_SIZE + NUM_CELLS / 2; int oldCellZ = (int)std::round(entity->m_Position.z) / dpGrid::CELL_SIZE + NUM_CELLS / 2; - if (oldCellX < 0) oldCellX = 0; - if (oldCellZ < 0) oldCellZ = 0; - if (oldCellX >= NUM_CELLS) oldCellX = NUM_CELLS - 1; - if (oldCellZ >= NUM_CELLS) oldCellZ = NUM_CELLS - 1; + // Clamp values to the range [0, NUM_CELLS - 1] + oldCellX = std::clamp(oldCellX, 0, NUM_CELLS - 1); + oldCellZ = std::clamp(oldCellZ, 0, NUM_CELLS - 1); - m_Cells[oldCellX][oldCellZ].remove(entity); + auto& cell = m_Cells[oldCellX][oldCellZ]; + auto toRemove = std::find(cell.begin(), cell.end(), entity); + if (toRemove != cell.end()) { + *toRemove = cell.back(); + cell.pop_back(); + } - if (m_GargantuanObjects.find(entity->m_ObjectID) != m_GargantuanObjects.end()) - m_GargantuanObjects.erase(entity->m_ObjectID); + m_GargantuanObjects.erase(entity->m_ObjectID); if (entity) delete entity; entity = nullptr; @@ -100,8 +96,8 @@ void dpGrid::Delete(dpEntity* entity) { void dpGrid::Update(float deltaTime) { //Pre-update: for (auto& x : m_Cells) { //x - for (auto& y : x) { //y - for (auto en : y) { + for (auto& z : x) { //y + for (auto en : z) { if (!en) continue; en->PreUpdate(); } @@ -110,8 +106,8 @@ void dpGrid::Update(float deltaTime) { //Actual collision detection update: for (int x = 0; x < NUM_CELLS; x++) { - for (int y = 0; y < NUM_CELLS; y++) { - HandleCell(x, y, deltaTime); + for (int z = 0; z < NUM_CELLS; z++) { + HandleCell(x, z, deltaTime); } } } @@ -157,7 +153,7 @@ void dpGrid::HandleCell(int x, int z, float deltaTime) { HandleEntity(en, other); } - for (auto other : m_GargantuanObjects) - HandleEntity(en, other.second); + for (auto& [id, entity] : m_GargantuanObjects) + HandleEntity(en, entity); } } diff --git a/dPhysics/dpGrid.h b/dPhysics/dpGrid.h index 229e7449..39a9cfe7 100644 --- a/dPhysics/dpGrid.h +++ b/dPhysics/dpGrid.h @@ -1,8 +1,7 @@ #pragma once -#include -#include -#include #include +#include + #include "dCommonVars.h" class dpEntity; @@ -30,7 +29,8 @@ public: */ void SetDeleteGrid(bool value) { this->m_DeleteGrid = value; }; - std::vector>> GetCells() { return this->m_Cells; }; + // Intentional copy since this is only used when we delete this class to re-create it. + std::vector>> GetCells() { return this->m_Cells; }; private: void HandleEntity(dpEntity* entity, dpEntity* other); @@ -38,7 +38,7 @@ private: private: //cells on X, cells on Y for that X, then another vector that contains the entities within that cell. - std::vector>> m_Cells; + std::vector>> m_Cells; std::map m_GargantuanObjects; bool m_DeleteGrid = true; }; diff --git a/dPhysics/dpWorld.cpp b/dPhysics/dpWorld.cpp index 698ceb63..c9bc742a 100644 --- a/dPhysics/dpWorld.cpp +++ b/dPhysics/dpWorld.cpp @@ -9,11 +9,32 @@ #include "Logger.h" #include "dConfig.h" +#include "dNavMesh.h" + +namespace { + dpGrid* m_Grid = nullptr; + dNavMesh* m_NavMesh = nullptr; + int32_t phys_sp_tilesize = 205; + int32_t phys_sp_tilecount = 12; + + uint32_t m_ZoneID = 0; + + std::vector m_StaticEntities; + std::vector m_DynamicEntites; + bool phys_spatial_partitioning = true; +}; + void dpWorld::Initialize(unsigned int zoneID, bool generateNewNavMesh) { const auto physSpTilecount = Game::config->GetValue("phys_sp_tilecount"); - if (!physSpTilecount.empty()) GeneralUtils::TryParse(physSpTilecount, phys_sp_tilecount); + if (!physSpTilecount.empty()) { + phys_sp_tilecount = GeneralUtils::TryParse(physSpTilecount).value_or(phys_sp_tilecount); + } + const auto physSpTilesize = Game::config->GetValue("phys_sp_tilesize"); - if (!physSpTilesize.empty()) GeneralUtils::TryParse(physSpTilesize, phys_sp_tilesize); + if (!physSpTilesize.empty()) { + phys_sp_tilesize = GeneralUtils::TryParse(physSpTilesize).value_or(phys_sp_tilesize); + } + const auto physSpatialPartitioning = Game::config->GetValue("phys_spatial_partitioning"); if (!physSpatialPartitioning.empty()) phys_spatial_partitioning = physSpatialPartitioning == "1"; @@ -51,7 +72,7 @@ void dpWorld::Reload() { } } -dpWorld::~dpWorld() { +void dpWorld::Shutdown() { if (m_Grid) { // Triple check this is true m_Grid->SetDeleteGrid(true); @@ -65,6 +86,10 @@ dpWorld::~dpWorld() { } } +bool dpWorld::IsLoaded() { + return m_NavMesh->IsNavmeshLoaded(); +} + void dpWorld::StepWorld(float deltaTime) { if (m_Grid) { m_Grid->Update(deltaTime); @@ -91,6 +116,10 @@ void dpWorld::StepWorld(float deltaTime) { } } +dNavMesh* dpWorld::GetNavMesh() { + return m_NavMesh; +} + void dpWorld::AddEntity(dpEntity* entity) { if (m_Grid) entity->SetGrid(m_Grid); //This sorts this entity into the right cell else { //old method, slow @@ -125,7 +154,7 @@ void dpWorld::RemoveEntity(dpEntity* entity) { } } -bool dpWorld::ShouldUseSP(unsigned int zoneID) { +bool dpWorld::ShouldUseSP(uint32_t zoneID) { if (!phys_spatial_partitioning) return false; // TODO: Add to this list as needed. diff --git a/dPhysics/dpWorld.h b/dPhysics/dpWorld.h index bf2af06d..fc23a15c 100644 --- a/dPhysics/dpWorld.h +++ b/dPhysics/dpWorld.h @@ -1,48 +1,22 @@ #pragma once -#include "Singleton.h" +#include -//Navmesh includes: -#include "Recast.h" -#include "DetourNavMesh.h" -#include "DetourNavMeshBuilder.h" -#include "DetourNavMeshQuery.h" - -#include -#include - -#include "dNavMesh.h" - -class NiPoint3; +class dNavMesh; class dpEntity; -class dpGrid; -class dpWorld : public Singleton { -public: - void Initialize(unsigned int zoneID, bool generateNewNavMesh = true); +namespace dpWorld { + void Initialize(uint32_t zoneID, bool generateNewNavMesh = true); + void Shutdown(); void Reload(); - ~dpWorld(); - - bool ShouldUseSP(unsigned int zoneID); - bool IsLoaded() const { return m_NavMesh->GetdtNavMesh() != nullptr; } + bool ShouldUseSP(uint32_t zoneID); + bool IsLoaded(); void StepWorld(float deltaTime); void AddEntity(dpEntity* entity); void RemoveEntity(dpEntity* entity); - dNavMesh* GetNavMesh() { return m_NavMesh; } - -private: - dpGrid* m_Grid; - bool phys_spatial_partitioning = true; - int phys_sp_tilesize = 205; - int phys_sp_tilecount = 12; - - std::vector m_StaticEntities; - std::vector m_DynamicEntites; - - dNavMesh* m_NavMesh = nullptr; - uint32_t m_ZoneID = 0; + dNavMesh* GetNavMesh(); }; diff --git a/dScripts/02_server/Enemy/AG/BossSpiderQueenEnemyServer.cpp b/dScripts/02_server/Enemy/AG/BossSpiderQueenEnemyServer.cpp index f85e0561..40b248f5 100644 --- a/dScripts/02_server/Enemy/AG/BossSpiderQueenEnemyServer.cpp +++ b/dScripts/02_server/Enemy/AG/BossSpiderQueenEnemyServer.cpp @@ -85,7 +85,7 @@ void BossSpiderQueenEnemyServer::WithdrawSpider(Entity* self, const bool withdra GameMessages::SendNotifyClientObject(self->GetObjectID(), u"SetColGroup", 10, 0, 0, "", UNASSIGNED_SYSTEM_ADDRESS); //First rotate for anim - NiQuaternion rot = NiQuaternion::IDENTITY; + NiQuaternion rot = NiQuaternionConstant::IDENTITY; controllable->SetStatic(false); @@ -179,7 +179,7 @@ void BossSpiderQueenEnemyServer::SpiderWaveManager(Entity* self) { std::vector spiderEggs{}; - auto spooders = Game::entityManager->GetEntitiesInGroup("EGG"); + auto spooders = Game::entityManager->GetEntitiesInGroup("SpiderEggs"); for (auto spodder : spooders) { spiderEggs.push_back(spodder->GetObjectID()); } @@ -402,7 +402,7 @@ void BossSpiderQueenEnemyServer::OnTimerDone(Entity* self, const std::string tim const auto withdrawn = self->GetBoolean(u"isWithdrawn"); if (!withdrawn) return; - NiQuaternion rot = NiQuaternion::IDENTITY; + NiQuaternion rot = NiQuaternionConstant::IDENTITY; //First rotate for anim controllable->SetStatic(false); @@ -597,12 +597,12 @@ void BossSpiderQueenEnemyServer::OnUpdate(Entity* self) { if (!isWithdrawn) return; - if (controllable->GetRotation() == NiQuaternion::IDENTITY) { + if (controllable->GetRotation() == NiQuaternionConstant::IDENTITY) { return; } controllable->SetStatic(false); - controllable->SetRotation(NiQuaternion::IDENTITY); + controllable->SetRotation(NiQuaternionConstant::IDENTITY); controllable->SetStatic(true); Game::entityManager->SerializeEntity(self); diff --git a/dScripts/02_server/Enemy/General/BaseEnemyMech.cpp b/dScripts/02_server/Enemy/General/BaseEnemyMech.cpp index c652dab0..8e70d4c3 100644 --- a/dScripts/02_server/Enemy/General/BaseEnemyMech.cpp +++ b/dScripts/02_server/Enemy/General/BaseEnemyMech.cpp @@ -7,6 +7,7 @@ #include "GeneralUtils.h" #include "DestroyableComponent.h" #include "eReplicaComponentType.h" +#include "dNavMesh.h" void BaseEnemyMech::OnStartup(Entity* self) { auto* destroyableComponent = self->GetComponent(); @@ -19,7 +20,7 @@ void BaseEnemyMech::OnDie(Entity* self, Entity* killer) { ControllablePhysicsComponent* controlPhys = static_cast(self->GetComponent(eReplicaComponentType::CONTROLLABLE_PHYSICS)); if (!controlPhys) return; - NiPoint3 newLoc = { controlPhys->GetPosition().x, dpWorld::Instance().GetNavMesh()->GetHeightAtPoint(controlPhys->GetPosition()), controlPhys->GetPosition().z }; + NiPoint3 newLoc = { controlPhys->GetPosition().x, dpWorld::GetNavMesh()->GetHeightAtPoint(controlPhys->GetPosition()), controlPhys->GetPosition().z }; EntityInfo info = EntityInfo(); std::vector cfg; diff --git a/dScripts/02_server/Map/AG/AgLaserSensorServer.cpp b/dScripts/02_server/Map/AG/AgLaserSensorServer.cpp index 7fcea9fa..6e19922a 100644 --- a/dScripts/02_server/Map/AG/AgLaserSensorServer.cpp +++ b/dScripts/02_server/Map/AG/AgLaserSensorServer.cpp @@ -13,7 +13,7 @@ void AgLaserSensorServer::OnStartup(Entity* self) { phantomPhysicsComponent->SetPhysicsEffectActive(true); phantomPhysicsComponent->SetEffectType(ePhysicsEffectType::REPULSE); phantomPhysicsComponent->SetDirectionalMultiplier(repelForce); - phantomPhysicsComponent->SetDirection(NiPoint3::UNIT_Y); + phantomPhysicsComponent->SetDirection(NiPoint3Constant::UNIT_Y); } diff --git a/dScripts/02_server/Map/AG/NpcCowboyServer.cpp b/dScripts/02_server/Map/AG/NpcCowboyServer.cpp index 6dd212a4..996a99c2 100644 --- a/dScripts/02_server/Map/AG/NpcCowboyServer.cpp +++ b/dScripts/02_server/Map/AG/NpcCowboyServer.cpp @@ -1,6 +1,7 @@ #include "NpcCowboyServer.h" #include "eMissionState.h" #include "InventoryComponent.h" +#include "dZoneManager.h" void NpcCowboyServer::OnMissionDialogueOK(Entity* self, Entity* target, int missionID, eMissionState missionState) { if (missionID != 1880) { @@ -23,4 +24,17 @@ void NpcCowboyServer::OnMissionDialogueOK(Entity* self, Entity* target, int miss } else if (missionState == eMissionState::READY_TO_COMPLETE || missionState == eMissionState::COMPLETE_READY_TO_COMPLETE) { inventoryComponent->RemoveItem(14378, 1); } + + // Next up hide or show the samples based on the mission state + int32_t visible = 1; + if (missionState == eMissionState::READY_TO_COMPLETE || missionState == eMissionState::COMPLETE_READY_TO_COMPLETE) { + visible = 0; + } + + auto spawners = Game::zoneManager->GetSpawnersByName("PlungerGunTargets"); + for (auto* spawner : spawners) { + for (const auto entity : spawner->GetSpawnedObjectIDs()) + GameMessages::SendNotifyClientObject(entity, u"SetVisibility", visible, 0, + target->GetObjectID(), "", target->GetSystemAddress()); + } } diff --git a/dScripts/02_server/Map/AG/NpcWispServer.cpp b/dScripts/02_server/Map/AG/NpcWispServer.cpp index e3b5398d..e087df24 100644 --- a/dScripts/02_server/Map/AG/NpcWispServer.cpp +++ b/dScripts/02_server/Map/AG/NpcWispServer.cpp @@ -1,9 +1,10 @@ #include "NpcWispServer.h" #include "InventoryComponent.h" -#include "EntityManager.h" +#include "dZoneManager.h" #include "Entity.h" #include "GameMessages.h" #include "eMissionState.h" +#include "Spawner.h" void NpcWispServer::OnMissionDialogueOK(Entity* self, Entity* target, int missionID, eMissionState missionState) { if (missionID != 1849 && missionID != 1883) @@ -25,7 +26,7 @@ void NpcWispServer::OnMissionDialogueOK(Entity* self, Entity* target, int missio } // Next up hide or show the samples based on the mission state - auto visible = 1; + int32_t visible = 1; if (missionState == eMissionState::READY_TO_COMPLETE || missionState == eMissionState::COMPLETE_READY_TO_COMPLETE) { visible = 0; } @@ -35,9 +36,10 @@ void NpcWispServer::OnMissionDialogueOK(Entity* self, Entity* target, int missio : std::vector{ "MaelstromSamples", "MaelstromSamples2ndary1", "MaelstromSamples2ndary2" }; for (const auto& group : groups) { - auto samples = Game::entityManager->GetEntitiesInGroup(group); - for (auto* sample : samples) { - GameMessages::SendNotifyClientObject(sample->GetObjectID(), u"SetVisibility", visible, 0, + auto spawners = Game::zoneManager->GetSpawnersByName(group); + for (const auto* spawner : spawners) { + for (const auto objId : spawner->GetSpawnedObjectIDs()) + GameMessages::SendNotifyClientObject(objId, u"SetVisibility", visible, 0, target->GetObjectID(), "", target->GetSystemAddress()); } } diff --git a/dScripts/02_server/Map/AM/AmDrawBridge.cpp b/dScripts/02_server/Map/AM/AmDrawBridge.cpp index 11d52cd7..cd42f196 100644 --- a/dScripts/02_server/Map/AM/AmDrawBridge.cpp +++ b/dScripts/02_server/Map/AM/AmDrawBridge.cpp @@ -66,7 +66,7 @@ void AmDrawBridge::OnTimerDone(Entity* self, std::string timerName) { return; } - simplePhysicsComponent->SetAngularVelocity(NiPoint3::ZERO); + simplePhysicsComponent->SetAngularVelocity(NiPoint3Constant::ZERO); Game::entityManager->SerializeEntity(bridge); } diff --git a/dScripts/02_server/Map/AM/AmDropshipComputer.cpp b/dScripts/02_server/Map/AM/AmDropshipComputer.cpp index 60b750cf..e88ec468 100644 --- a/dScripts/02_server/Map/AM/AmDropshipComputer.cpp +++ b/dScripts/02_server/Map/AM/AmDropshipComputer.cpp @@ -12,16 +12,12 @@ void AmDropshipComputer::OnStartup(Entity* self) { void AmDropshipComputer::OnUse(Entity* self, Entity* user) { auto* quickBuildComponent = self->GetComponent(); - if (quickBuildComponent == nullptr || quickBuildComponent->GetState() != eQuickBuildState::COMPLETED) { - return; - } + if (!quickBuildComponent || quickBuildComponent->GetState() != eQuickBuildState::COMPLETED) return; auto* missionComponent = user->GetComponent(); auto* inventoryComponent = user->GetComponent(); - if (missionComponent == nullptr || inventoryComponent == nullptr) { - return; - } + if (!missionComponent || !inventoryComponent) return; if (inventoryComponent->GetLotCount(m_NexusTalonDataCard) != 0 || missionComponent->GetMission(979)->GetMissionState() == eMissionState::COMPLETE) { return; @@ -33,14 +29,12 @@ void AmDropshipComputer::OnUse(Entity* self, Entity* user) { void AmDropshipComputer::OnDie(Entity* self, Entity* killer) { const auto myGroup = GeneralUtils::UTF16ToWTF8(self->GetVar(u"spawner_name")); - int32_t pipeNum = 0; - if (!GeneralUtils::TryParse(myGroup.substr(10, 1), pipeNum)) { - return; - } + const auto pipeNum = GeneralUtils::TryParse(myGroup.substr(10, 1)); + if (!pipeNum) return; const auto pipeGroup = myGroup.substr(0, 10); - const auto nextPipeNum = pipeNum + 1; + const auto nextPipeNum = pipeNum.value() + 1; const auto samePipeSpawners = Game::zoneManager->GetSpawnersByName(myGroup); @@ -70,11 +64,9 @@ void AmDropshipComputer::OnDie(Entity* self, Entity* killer) { } void AmDropshipComputer::OnTimerDone(Entity* self, std::string timerName) { - auto* quickBuildComponent = self->GetComponent(); + const auto* const quickBuildComponent = self->GetComponent(); - if (quickBuildComponent == nullptr) { - return; - } + if (!quickBuildComponent) return; if (timerName == "reset" && quickBuildComponent->GetState() == eQuickBuildState::OPEN) { self->Smash(self->GetObjectID(), eKillType::SILENT); diff --git a/dScripts/02_server/Map/AM/AmSkullkinTower.cpp b/dScripts/02_server/Map/AM/AmSkullkinTower.cpp index 0c0f7515..1eaad3c9 100644 --- a/dScripts/02_server/Map/AM/AmSkullkinTower.cpp +++ b/dScripts/02_server/Map/AM/AmSkullkinTower.cpp @@ -144,13 +144,10 @@ void AmSkullkinTower::OnChildRemoved(Entity* self, Entity* child) { ); for (const auto& mission : missions) { - int32_t missionID = 0; + const auto missionID = GeneralUtils::TryParse(mission); + if (!missionID) continue; - if (!GeneralUtils::TryParse(mission, missionID)) { - continue; - } - - missionIDs.push_back(missionID); + missionIDs.push_back(missionID.value()); } } diff --git a/dScripts/02_server/Map/AM/AmTemplateSkillVolume.cpp b/dScripts/02_server/Map/AM/AmTemplateSkillVolume.cpp index 3acc9063..82f1481a 100644 --- a/dScripts/02_server/Map/AM/AmTemplateSkillVolume.cpp +++ b/dScripts/02_server/Map/AM/AmTemplateSkillVolume.cpp @@ -12,12 +12,9 @@ void AmTemplateSkillVolume::OnSkillEventFired(Entity* self, Entity* caster, cons const auto missionIDs = GeneralUtils::SplitString(missionIDsVariable, '_'); for (const auto& missionIDStr : missionIDs) { - int32_t missionID = 0; + const auto missionID = GeneralUtils::TryParse(missionIDStr); + if (!missionID) continue; - if (!GeneralUtils::TryParse(missionIDStr, missionID)) { - continue; - } - - missionComponent->ForceProgressTaskType(missionID, 1, 1, false); + missionComponent->ForceProgressTaskType(missionID.value(), 1, 1, false); } } diff --git a/dScripts/02_server/Map/GF/MastTeleport.cpp b/dScripts/02_server/Map/GF/MastTeleport.cpp index 5a40507d..e181779e 100644 --- a/dScripts/02_server/Map/GF/MastTeleport.cpp +++ b/dScripts/02_server/Map/GF/MastTeleport.cpp @@ -81,7 +81,7 @@ void MastTeleport::OnTimerDone(Entity* self, std::string timerName) { GameMessages::SendOrientToAngle(playerId, true, rads, player->GetSystemAddress()); - GameMessages::SendTeleport(playerId, position, NiQuaternion::IDENTITY, player->GetSystemAddress()); + GameMessages::SendTeleport(playerId, position, NiQuaternionConstant::IDENTITY, player->GetSystemAddress()); GameMessages::SendSetStunned(playerId, eStateChangeType::POP, player->GetSystemAddress(), LWOOBJID_EMPTY, true, true, true, true, true, true, true diff --git a/dScripts/02_server/Map/General/PetDigServer.cpp b/dScripts/02_server/Map/General/PetDigServer.cpp index 43c00f7f..e8382567 100644 --- a/dScripts/02_server/Map/General/PetDigServer.cpp +++ b/dScripts/02_server/Map/General/PetDigServer.cpp @@ -119,7 +119,7 @@ void PetDigServer::HandleXBuildDig(const Entity* self, Entity* owner, Entity* pe return; auto* playerEntity = Game::entityManager->GetEntity(playerID); - if (!playerEntity || !playerEntity->GetParentUser() || !playerEntity->GetParentUser()->GetLastUsedChar()) + if (!playerEntity || !playerEntity->GetCharacter()) return; auto* player = playerEntity->GetCharacter(); diff --git a/dScripts/02_server/Map/General/QbEnemyStunner.cpp b/dScripts/02_server/Map/General/QbEnemyStunner.cpp index 8291f409..2238c410 100644 --- a/dScripts/02_server/Map/General/QbEnemyStunner.cpp +++ b/dScripts/02_server/Map/General/QbEnemyStunner.cpp @@ -17,12 +17,12 @@ void QbEnemyStunner::OnQuickBuildComplete(Entity* self, Entity* target) { if (!skillComponent) return; // Get the skill IDs of this object. - CDObjectSkillsTable* skillsTable = CDClientManager::Instance().GetTable(); + CDObjectSkillsTable* skillsTable = CDClientManager::GetTable(); auto skills = skillsTable->Query([=](CDObjectSkills entry) {return (entry.objectTemplate == self->GetLOT()); }); std::map skillBehaviorMap; // For each skill, cast it with the associated behavior ID. for (auto skill : skills) { - CDSkillBehaviorTable* skillBehaviorTable = CDClientManager::Instance().GetTable(); + CDSkillBehaviorTable* skillBehaviorTable = CDClientManager::GetTable(); CDSkillBehavior behaviorData = skillBehaviorTable->GetSkillByID(skill.skillID); skillBehaviorMap.insert(std::make_pair(skill.skillID, behaviorData.behaviorID)); diff --git a/dScripts/02_server/Pets/PetFromDigServer.cpp b/dScripts/02_server/Pets/PetFromDigServer.cpp index 525f3e94..33bab32a 100644 --- a/dScripts/02_server/Pets/PetFromDigServer.cpp +++ b/dScripts/02_server/Pets/PetFromDigServer.cpp @@ -40,6 +40,6 @@ void PetFromDigServer::OnNotifyPetTamingMinigame(Entity* self, Entity* tamer, eP return; // TODO: Remove custom group? // Command the pet to the player as it may otherwise go to its spawn point which is non existant - // petComponent->Command(NiPoint3::ZERO, LWOOBJID_EMPTY, 6, 202, true); + // petComponent->Command(NiPoint3Constant::ZERO, LWOOBJID_EMPTY, 6, 202, true); } } diff --git a/dScripts/BaseConsoleTeleportServer.cpp b/dScripts/BaseConsoleTeleportServer.cpp index 27d2bcfc..d4a49299 100644 --- a/dScripts/BaseConsoleTeleportServer.cpp +++ b/dScripts/BaseConsoleTeleportServer.cpp @@ -1,6 +1,6 @@ #include "BaseConsoleTeleportServer.h" #include "GameMessages.h" -#include "Player.h" +#include "CharacterComponent.h" #include "RenderComponent.h" #include "EntityManager.h" #include "eTerminateType.h" @@ -94,7 +94,9 @@ void BaseConsoleTeleportServer::TransferPlayer(Entity* self, Entity* player, int const auto& teleportZone = self->GetVar(u"transferZoneID"); - static_cast(player)->SendToZone(std::stoi(GeneralUtils::UTF16ToWTF8(teleportZone))); + auto* characterComponent = player->GetComponent(); + + if (characterComponent) characterComponent->SendToZone(std::stoi(GeneralUtils::UTF16ToWTF8(teleportZone))); UpdatePlayerTable(self, player, false); } diff --git a/dScripts/BaseSurvivalServer.cpp b/dScripts/BaseSurvivalServer.cpp index 93476d78..ecdcad64 100644 --- a/dScripts/BaseSurvivalServer.cpp +++ b/dScripts/BaseSurvivalServer.cpp @@ -3,7 +3,7 @@ #include "DestroyableComponent.h" #include "EntityManager.h" #include "dZoneManager.h" -#include "Player.h" +#include "CharacterComponent.h" #include "eMissionTaskType.h" #include "eMissionState.h" #include "MissionComponent.h" @@ -23,7 +23,8 @@ void BaseSurvivalServer::BasePlayerLoaded(Entity* self, Entity* player) { const auto& playersIter = std::find(state.players.begin(), state.players.end(), player->GetObjectID()); if (waitingIter != state.waitingPlayers.end() || playersIter != state.players.end()) { - static_cast(player)->SendToZone(player->GetCharacter()->GetLastNonInstanceZoneID()); + auto* characterComponent = player->GetComponent(); + if (characterComponent) characterComponent->SendToZone(player->GetCharacter()->GetLastNonInstanceZoneID()); return; } @@ -161,8 +162,8 @@ void BaseSurvivalServer::BaseMessageBoxResponse(Entity* self, Entity* sender, in if (sender->IsPlayer()) { auto* character = sender->GetCharacter(); if (character != nullptr) { - auto* player = dynamic_cast(sender); - player->SendToZone(character->GetLastNonInstanceZoneID()); + auto* characterComponent = sender->GetComponent(); + if (characterComponent) characterComponent->SendToZone(character->GetLastNonInstanceZoneID()); } } } diff --git a/dScripts/BaseWavesServer.cpp b/dScripts/BaseWavesServer.cpp index 9be19806..e2f646f9 100644 --- a/dScripts/BaseWavesServer.cpp +++ b/dScripts/BaseWavesServer.cpp @@ -3,7 +3,7 @@ #include "DestroyableComponent.h" #include "EntityManager.h" #include "dZoneManager.h" -#include "Player.h" +#include "CharacterComponent.h" #include "eMissionTaskType.h" #include "eMissionState.h" #include "MissionComponent.h" @@ -162,8 +162,8 @@ void BaseWavesServer::BaseMessageBoxResponse(Entity* self, Entity* sender, int32 if (sender->IsPlayer()) { auto* character = sender->GetCharacter(); if (character != nullptr) { - auto* player = dynamic_cast(sender); - player->SendToZone(character->GetLastNonInstanceZoneID()); + auto* characterComponent = sender->GetComponent(); + if (characterComponent) characterComponent->SendToZone(character->GetLastNonInstanceZoneID()); } } } diff --git a/dScripts/ChooseYourDestinationNsToNt.cpp b/dScripts/ChooseYourDestinationNsToNt.cpp index f9ca0a79..b736883c 100644 --- a/dScripts/ChooseYourDestinationNsToNt.cpp +++ b/dScripts/ChooseYourDestinationNsToNt.cpp @@ -41,11 +41,9 @@ void ChooseYourDestinationNsToNt::SetDestination(Entity* self, Entity* player) { void ChooseYourDestinationNsToNt::BaseChoiceBoxRespond(Entity* self, Entity* sender, int32_t button, const std::u16string& buttonIdentifier, const std::u16string& identifier) { if (button != -1) { const auto newMapStr = GeneralUtils::UTF16ToWTF8(buttonIdentifier).substr(7, -1); - - int32_t newMap = 0; - if (!GeneralUtils::TryParse(newMapStr, newMap)) { - return; - } + const auto newMap = GeneralUtils::TryParse(newMapStr); + + if (!newMap) return; std::u16string strText = u""; @@ -56,7 +54,7 @@ void ChooseYourDestinationNsToNt::BaseChoiceBoxRespond(Entity* self, Entity* sen } self->SetVar(u"teleportString", strText); - self->SetVar(u"transferZoneID", GeneralUtils::to_u16string(newMap)); + self->SetVar(u"transferZoneID", GeneralUtils::to_u16string(newMap.value())); GameMessages::SendDisplayMessageBox(sender->GetObjectID(), true, self->GetObjectID(), u"TransferBox", 0, strText, u"", sender->GetSystemAddress()); } else { diff --git a/dScripts/EquipmentScripts/FireFirstSkillonStartup.cpp b/dScripts/EquipmentScripts/FireFirstSkillonStartup.cpp index 389f3621..0220711d 100644 --- a/dScripts/EquipmentScripts/FireFirstSkillonStartup.cpp +++ b/dScripts/EquipmentScripts/FireFirstSkillonStartup.cpp @@ -11,12 +11,12 @@ void FireFirstSkillonStartup::OnStartup(Entity* self) { if (!skillComponent) return; // Get the skill IDs of this object. - CDObjectSkillsTable* skillsTable = CDClientManager::Instance().GetTable(); + CDObjectSkillsTable* skillsTable = CDClientManager::GetTable(); std::vector skills = skillsTable->Query([=](CDObjectSkills entry) {return (entry.objectTemplate == self->GetLOT()); }); // For each skill, cast it with the associated behavior ID. for (auto skill : skills) { - CDSkillBehaviorTable* skillBehaviorTable = CDClientManager::Instance().GetTable(); + CDSkillBehaviorTable* skillBehaviorTable = CDClientManager::GetTable(); CDSkillBehavior behaviorData = skillBehaviorTable->GetSkillByID(skill.skillID); // Should parent entity be null, make the originator self. diff --git a/dScripts/ScriptComponent.cpp b/dScripts/ScriptComponent.cpp index 2079d67a..7c44ded3 100644 --- a/dScripts/ScriptComponent.cpp +++ b/dScripts/ScriptComponent.cpp @@ -46,11 +46,7 @@ CppScripts::Script* ScriptComponent::GetScript() { } void ScriptComponent::SetScript(const std::string& scriptName) { - //we don't need to delete the script because others may be using it :) - /*if (m_Client) { - m_Script = new InvalidScript(); - return; - }*/ - + // Scripts are managed by the CppScripts class and are effecitvely singletons + // and they may also be used by other script components so DON'T delete them. m_Script = CppScripts::GetScript(m_Parent, scriptName); } diff --git a/dScripts/ScriptComponent.h b/dScripts/ScriptComponent.h index cabbf8bf..a28c9419 100644 --- a/dScripts/ScriptComponent.h +++ b/dScripts/ScriptComponent.h @@ -17,9 +17,9 @@ class Entity; * Handles the loading and execution of server side scripts on entities, scripts were originally written in Lua, * here they're written in C++ */ -class ScriptComponent : public Component { +class ScriptComponent final : public Component { public: - inline static const eReplicaComponentType ComponentType = eReplicaComponentType::SCRIPT; + static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::SCRIPT; ScriptComponent(Entity* parent, std::string scriptName, bool serialized, bool client = false); ~ScriptComponent() override; diff --git a/dScripts/ai/FV/FvBrickPuzzleServer.cpp b/dScripts/ai/FV/FvBrickPuzzleServer.cpp index e1f1ac88..f8601e3f 100644 --- a/dScripts/ai/FV/FvBrickPuzzleServer.cpp +++ b/dScripts/ai/FV/FvBrickPuzzleServer.cpp @@ -7,10 +7,8 @@ void FvBrickPuzzleServer::OnStartup(Entity* self) { const auto myGroup = GeneralUtils::UTF16ToWTF8(self->GetVar(u"spawner_name")); - int32_t pipeNum = 0; - if (!GeneralUtils::TryParse(myGroup.substr(10, 1), pipeNum)) { - return; - } + const auto pipeNum = GeneralUtils::TryParse(myGroup.substr(10, 1)); + if (!pipeNum) return; if (pipeNum != 1) { self->AddTimer("reset", 30); @@ -20,14 +18,12 @@ void FvBrickPuzzleServer::OnStartup(Entity* self) { void FvBrickPuzzleServer::OnDie(Entity* self, Entity* killer) { const auto myGroup = GeneralUtils::UTF16ToWTF8(self->GetVar(u"spawner_name")); - int32_t pipeNum = 0; - if (!GeneralUtils::TryParse(myGroup.substr(10, 1), pipeNum)) { - return; - } + const auto pipeNum = GeneralUtils::TryParse(myGroup.substr(10, 1)); + if (!pipeNum) return; const auto pipeGroup = myGroup.substr(0, 10); - const auto nextPipeNum = pipeNum + 1; + const auto nextPipeNum = pipeNum.value() + 1; const auto samePipeSpawners = Game::zoneManager->GetSpawnersByName(myGroup); @@ -37,7 +33,7 @@ void FvBrickPuzzleServer::OnDie(Entity* self, Entity* killer) { samePipeSpawners[0]->Deactivate(); } - if (killer != nullptr && killer->IsPlayer()) { + if (killer && killer->IsPlayer()) { const auto nextPipe = pipeGroup + std::to_string(nextPipeNum); const auto nextPipeSpawners = Game::zoneManager->GetSpawnersByName(nextPipe); diff --git a/dScripts/ai/GENERAL/InstanceExitTransferPlayerToLastNonInstance.cpp b/dScripts/ai/GENERAL/InstanceExitTransferPlayerToLastNonInstance.cpp index de1c62e0..667f41bd 100644 --- a/dScripts/ai/GENERAL/InstanceExitTransferPlayerToLastNonInstance.cpp +++ b/dScripts/ai/GENERAL/InstanceExitTransferPlayerToLastNonInstance.cpp @@ -1,6 +1,6 @@ #include "InstanceExitTransferPlayerToLastNonInstance.h" #include "GameMessages.h" -#include "Player.h" +#include "CharacterComponent.h" #include "Character.h" #include "dServer.h" #include "eTerminateType.h" @@ -23,10 +23,8 @@ void InstanceExitTransferPlayerToLastNonInstance::OnUse(Entity* self, Entity* us } void InstanceExitTransferPlayerToLastNonInstance::OnMessageBoxResponse(Entity* self, Entity* sender, int32_t button, const std::u16string& identifier, const std::u16string& userData) { - auto* player = dynamic_cast(sender); - if (player == nullptr) - return; - + if (!sender->IsPlayer()) return; + auto* character = sender->GetCharacter(); if (character != nullptr) { if (identifier == u"Instance_Exit" && button == 1) { @@ -47,7 +45,8 @@ void InstanceExitTransferPlayerToLastNonInstance::OnMessageBoxResponse(Entity* s } } - player->SendToZone(lastInstance); + auto* characterComponent = sender->GetComponent(); + if (characterComponent) characterComponent->SendToZone(lastInstance); } } diff --git a/dScripts/ai/GF/GfBanana.cpp b/dScripts/ai/GF/GfBanana.cpp index 6bc5c179..b06aae6f 100644 --- a/dScripts/ai/GF/GfBanana.cpp +++ b/dScripts/ai/GF/GfBanana.cpp @@ -56,7 +56,7 @@ void GfBanana::OnHit(Entity* self, Entity* attacker) { return; } - bananaEntity->SetPosition(bananaEntity->GetPosition() - NiPoint3::UNIT_Y * 8); + bananaEntity->SetPosition(bananaEntity->GetPosition() - NiPoint3Constant::UNIT_Y * 8); auto* bananaDestroyable = bananaEntity->GetComponent(); diff --git a/dScripts/ai/MINIGAME/SG_GF/SERVER/SGCannon.cpp b/dScripts/ai/MINIGAME/SG_GF/SERVER/SGCannon.cpp index 1a298843..1952831a 100644 --- a/dScripts/ai/MINIGAME/SG_GF/SERVER/SGCannon.cpp +++ b/dScripts/ai/MINIGAME/SG_GF/SERVER/SGCannon.cpp @@ -2,7 +2,6 @@ #include "EntityManager.h" #include "GameMessages.h" #include "dZoneManager.h" -#include "Player.h" #include "Character.h" #include "ShootingGalleryComponent.h" #include "PossessorComponent.h" @@ -453,16 +452,12 @@ void SGCannon::SpawnNewModel(Entity* self) { void SGCannon::RemovePlayer(LWOOBJID playerID) { auto* player = Game::entityManager->GetEntity(playerID); - if (player == nullptr) - return; + if (!player) return; - auto* playerObject = dynamic_cast(player); - if (playerObject == nullptr) - return; - - auto* character = playerObject->GetCharacter(); - if (character != nullptr) { - playerObject->SendToZone(character->GetLastNonInstanceZoneID()); + auto* character = player->GetCharacter(); + auto* characterComponent = player->GetComponent(); + if (characterComponent && character) { + characterComponent->SendToZone(character->GetLastNonInstanceZoneID()); } } diff --git a/dScripts/client/ai/PR/CrabServer.cpp b/dScripts/client/ai/PR/CrabServer.cpp index f30142ba..6cdc2de8 100644 --- a/dScripts/client/ai/PR/CrabServer.cpp +++ b/dScripts/client/ai/PR/CrabServer.cpp @@ -39,6 +39,6 @@ void CrabServer::OnNotifyPetTamingMinigame(Entity* self, Entity* tamer, ePetTami return; // TODO: Remove custom group? // Command the pet to the player as it may otherwise go to its spawn point which is non existant - // petComponent->Command(NiPoint3::ZERO, LWOOBJID_EMPTY, 6, 202, true); + // petComponent->Command(NiPoint3Constant::ZERO, LWOOBJID_EMPTY, 6, 202, true); } } diff --git a/dScripts/zone/LUPs/WblGenericZone.cpp b/dScripts/zone/LUPs/WblGenericZone.cpp index 5a670d8e..600628fa 100644 --- a/dScripts/zone/LUPs/WblGenericZone.cpp +++ b/dScripts/zone/LUPs/WblGenericZone.cpp @@ -1,10 +1,11 @@ #include "WblGenericZone.h" -#include "Player.h" +#include "CharacterComponent.h" void WblGenericZone::OnFireEventServerSide(Entity* self, Entity* sender, std::string args, int32_t param1, int32_t param2, int32_t param3) { if (args == m_WblAbortMsg) { if (!sender) return; - auto player = dynamic_cast(sender); - if (player) player->SendToZone(m_WblMainZone); + + auto* characterComponent = sender->GetComponent(); + if (characterComponent) characterComponent->SendToZone(m_WblMainZone); } } diff --git a/dWorldServer/PerformanceManager.cpp b/dWorldServer/PerformanceManager.cpp index 248be54a..052d075b 100644 --- a/dWorldServer/PerformanceManager.cpp +++ b/dWorldServer/PerformanceManager.cpp @@ -70,7 +70,7 @@ std::map PerformanceManager::m_Profiles = { void PerformanceManager::SelectProfile(LWOMAPID mapID) { // Try to get it from zoneTable - CDZoneTableTable* zoneTable = CDClientManager::Instance().GetTable(); + CDZoneTableTable* zoneTable = CDClientManager::GetTable(); if (zoneTable) { const CDZoneTable* zone = zoneTable->Query(mapID); if (zone) { diff --git a/dWorldServer/WorldServer.cpp b/dWorldServer/WorldServer.cpp index d65596b6..bb879dbd 100644 --- a/dWorldServer/WorldServer.cpp +++ b/dWorldServer/WorldServer.cpp @@ -29,7 +29,6 @@ #include #include "AuthPackets.h" -#include "PacketUtils.h" #include "BitStreamUtils.h" #include "WorldPackets.h" #include "UserManager.h" @@ -57,7 +56,6 @@ #include "DestroyableComponent.h" #include "Game.h" #include "MasterPackets.h" -#include "Player.h" #include "PropertyManagementComponent.h" #include "AssetManager.h" #include "LevelProgressionComponent.h" @@ -78,11 +76,13 @@ #include "eGameMasterLevel.h" #include "StringifiedEnum.h" #include "Server.h" +#include "PositionUpdate.h" +#include "PlayerManager.h" +#include "eLoginResponse.h" namespace Game { Logger* logger = nullptr; dServer* server = nullptr; - dpWorld* physicsWorld = nullptr; dChatFilter* chatFilter = nullptr; dConfig* config = nullptr; AssetManager* assetManager = nullptr; @@ -180,7 +180,7 @@ int main(int argc, char** argv) { return EXIT_FAILURE; } - CDClientManager::Instance(); + CDClientManager::LoadValuesFromDatabase(); Diagnostics::SetProduceMemoryDump(Game::config->GetValue("generate_dump") == "1"); @@ -208,8 +208,7 @@ int main(int argc, char** argv) { UserManager::Instance()->Initialize(); - bool dontGenerateDCF = false; - GeneralUtils::TryParse(Game::config->GetValue("dont_generate_dcf"), dontGenerateDCF); + const bool dontGenerateDCF = GeneralUtils::TryParse(Game::config->GetValue("dont_generate_dcf")).value_or(false); Game::chatFilter = new dChatFilter(Game::assetManager->GetResPath().string() + "/chatplus_en_us", dontGenerateDCF); Game::server = new dServer(masterIP, ourPort, instanceID, maxClients, false, true, Game::logger, masterIP, masterPort, ServerType::World, Game::config, &Game::lastSignal, zoneID); @@ -255,7 +254,7 @@ int main(int argc, char** argv) { Game::zoneManager = new dZoneManager(); //Load our level: if (zoneID != 0) { - dpWorld::Instance().Initialize(zoneID); + dpWorld::Initialize(zoneID); Game::zoneManager->Initialize(LWOZONEID(zoneID, instanceID, cloneID)); g_CloneID = cloneID; @@ -386,7 +385,7 @@ int main(int argc, char** argv) { if (zoneID != 0 && deltaTime > 0.0f) { Metrics::StartMeasurement(MetricVariable::Physics); - dpWorld::Instance().StepWorld(deltaTime); + dpWorld::StepWorld(deltaTime); Metrics::EndMeasurement(MetricVariable::Physics); Metrics::StartMeasurement(MetricVariable::UpdateEntities); @@ -606,9 +605,10 @@ void HandlePacketChat(Packet* packet) { inStream.Read(expire); auto* entity = Game::entityManager->GetEntity(playerId); - - if (entity != nullptr) { - entity->GetParentUser()->SetMuteExpire(expire); + auto* character = entity != nullptr ? entity->GetCharacter() : nullptr; + auto* user = character != nullptr ? character->GetParentUser() : nullptr; + if (user) { + user->SetMuteExpire(expire); entity->GetCharacter()->SendMuteNotice(); } @@ -663,24 +663,25 @@ void HandleMasterPacket(Packet* packet) { if (static_cast(packet->data[1]) != eConnectionType::MASTER || packet->length < 4) return; switch (static_cast(packet->data[3])) { case eMasterMessageType::REQUEST_PERSISTENT_ID_RESPONSE: { - uint64_t requestID = PacketUtils::ReadU64(8, packet); - uint32_t objectID = PacketUtils::ReadU32(16, packet); + CINSTREAM_SKIP_HEADER; + uint64_t requestID; + inStream.Read(requestID); + uint32_t objectID; + inStream.Read(objectID); ObjectIDManager::HandleRequestPersistentIDResponse(requestID, objectID); break; } case eMasterMessageType::SESSION_KEY_RESPONSE: { //Read our session key and to which user it belongs: - RakNet::BitStream inStream(packet->data, packet->length, false); - uint64_t header = inStream.Read(header); + CINSTREAM_SKIP_HEADER; uint32_t sessionKey = 0; - std::string username; - inStream.Read(sessionKey); - username = PacketUtils::ReadString(12, packet, false); + LUWString username; + inStream.Read(username); //Find them: - auto it = m_PendingUsers.find(username); + auto it = m_PendingUsers.find(username.GetAsString()); if (it == m_PendingUsers.end()) return; //Convert our key: @@ -693,12 +694,12 @@ void HandleMasterPacket(Packet* packet) { Game::server->Disconnect(it->second.sysAddr, eServerDisconnectIdentifiers::INVALID_SESSION_KEY); return; } else { - LOG("User %s authenticated with correct key.", username.c_str()); + LOG("User %s authenticated with correct key.", username.GetAsString().c_str()); UserManager::Instance()->DeleteUser(packet->systemAddress); //Create our user and send them in: - UserManager::Instance()->CreateUser(it->second.sysAddr, username, userHash); + UserManager::Instance()->CreateUser(it->second.sysAddr, username.GetAsString(), userHash); auto zone = Game::zoneManager->GetZone(); if (zone) { @@ -713,7 +714,7 @@ void HandleMasterPacket(Packet* packet) { z = pos.z; } - WorldPackets::SendLoadStaticZone(it->second.sysAddr, x, y, z, zone->GetChecksum()); + WorldPackets::SendLoadStaticZone(it->second.sysAddr, x, y, z, zone->GetChecksum(), Game::zoneManager->GetZoneID()); } if (Game::server->GetZoneID() == 0) { @@ -721,7 +722,7 @@ void HandleMasterPacket(Packet* packet) { UserManager::Instance()->RequestCharacterList(it->second.sysAddr); } - m_PendingUsers.erase(username); + m_PendingUsers.erase(username.GetAsString()); //Notify master: { @@ -736,8 +737,9 @@ void HandleMasterPacket(Packet* packet) { break; } case eMasterMessageType::AFFIRM_TRANSFER_REQUEST: { - const uint64_t requestID = PacketUtils::ReadU64(8, packet); - + CINSTREAM_SKIP_HEADER; + uint64_t requestID; + inStream.Read(requestID); LOG("Got affirmation request of transfer %llu", requestID); CBITSTREAM; @@ -756,30 +758,22 @@ void HandleMasterPacket(Packet* packet) { } case eMasterMessageType::NEW_SESSION_ALERT: { - RakNet::BitStream inStream(packet->data, packet->length, false); - uint64_t header = inStream.Read(header); + CINSTREAM_SKIP_HEADER; uint32_t sessionKey = inStream.Read(sessionKey); - std::string username; - - uint32_t len; - inStream.Read(len); - - for (uint32_t i = 0; i < len; i++) { - char character; inStream.Read(character); - username += character; - } - + LUString username; + inStream.Read(username); + LOG("Got new session alert for user %s", username.string.c_str()); //Find them: - User* user = UserManager::Instance()->GetUser(username.c_str()); + User* user = UserManager::Instance()->GetUser(username.string.c_str()); if (!user) { - LOG("Got new session alert for user %s, but they're not logged in.", username.c_str()); + LOG("But they're not logged in?"); return; } //Check the key: if (sessionKey != std::atoi(user->GetSessionKey().c_str())) { - LOG("Got new session alert for user %s, but the session key is invalid.", username.c_str()); + LOG("But the session key is invalid!", username.string.c_str()); Game::server->Disconnect(user->GetSystemAddress(), eServerDisconnectIdentifiers::INVALID_SESSION_KEY); return; } @@ -804,7 +798,7 @@ void HandlePacket(Packet* packet) { auto* entity = Game::entityManager->GetEntity(c->GetObjectID()); if (!entity) { - entity = Player::GetPlayer(packet->systemAddress); + entity = PlayerManager::GetPlayer(packet->systemAddress); } if (entity) { @@ -852,16 +846,19 @@ void HandlePacket(Packet* packet) { switch (static_cast(packet->data[3])) { case eWorldMessageType::VALIDATION: { - std::string username = PacketUtils::ReadString(0x08, packet, true); - std::string sessionKey = PacketUtils::ReadString(74, packet, true); - std::string clientDatabaseChecksum = PacketUtils::ReadString(packet->length - 33, packet, false); + CINSTREAM_SKIP_HEADER; + LUWString username; + inStream.Read(username); + LUWString sessionKey; // sometimes client puts a null terminator at the end of the checksum and sometimes doesn't, weird - clientDatabaseChecksum = clientDatabaseChecksum.substr(0, 32); + inStream.Read(sessionKey); + LUString clientDatabaseChecksum(32); + inStream.Read(clientDatabaseChecksum); // If the check is turned on, validate the client's database checksum. if (Game::config->GetValue("check_fdb") == "1" && !databaseChecksum.empty()) { - auto accountInfo = Database::Get()->GetAccountInfo(username); + auto accountInfo = Database::Get()->GetAccountInfo(username.GetAsString()); if (!accountInfo) { LOG("Client's account does not exist in the database, aborting connection."); Game::server->Disconnect(packet->systemAddress, eServerDisconnectIdentifiers::CHARACTER_NOT_FOUND); @@ -869,24 +866,42 @@ void HandlePacket(Packet* packet) { } // Developers may skip this check - if (accountInfo->maxGmLevel < eGameMasterLevel::DEVELOPER && clientDatabaseChecksum != databaseChecksum) { - LOG("Client's database checksum does not match the server's, aborting connection."); - Game::server->Disconnect(packet->systemAddress, eServerDisconnectIdentifiers::WRONG_GAME_VERSION); - return; + if (clientDatabaseChecksum.string != databaseChecksum) { + + if (accountInfo->maxGmLevel < eGameMasterLevel::DEVELOPER) { + LOG("Client's database checksum does not match the server's, aborting connection."); + std::vector stamps; + + // Using the LoginResponse here since the UI is still in the login screen state + // and we have a way to send a message about the client mismatch. + AuthPackets::SendLoginResponse( + Game::server, packet->systemAddress, eLoginResponse::PERMISSIONS_NOT_HIGH_ENOUGH, + Game::config->GetValue("cdclient_mismatch_message"), "", 0, "", stamps); + return; + } else { + AMFArrayValue args; + + args.Insert("title", Game::config->GetValue("cdclient_mismatch_title")); + args.Insert("message", Game::config->GetValue("cdclient_mismatch_message")); + + GameMessages::SendUIMessageServerToSingleClient("ToggleAnnounce", args, packet->systemAddress); + LOG("Account (%s) with GmLevel (%s) does not have a matching FDB, but is a developer and will skip this check." + , username.GetAsString().c_str(), StringifiedEnum::ToString(accountInfo->maxGmLevel).data()); + } } } //Request the session info from Master: CBITSTREAM; BitStreamUtils::WriteHeader(bitStream, eConnectionType::MASTER, eMasterMessageType::REQUEST_SESSION_KEY); - bitStream.Write(LUString(username, 64)); + bitStream.Write(username); Game::server->SendToMaster(&bitStream); //Insert info into our pending list tempSessionInfo info; info.sysAddr = SystemAddress(packet->systemAddress); - info.hash = sessionKey; - m_PendingUsers.insert(std::make_pair(username, info)); + info.hash = sessionKey.GetAsString(); + m_PendingUsers.insert(std::make_pair(username.GetAsString(), info)); break; } @@ -1005,21 +1020,22 @@ void HandlePacket(Packet* packet) { info.lot = 1; Entity* player = Game::entityManager->CreateEntity(info, UserManager::Instance()->GetUser(packet->systemAddress)); - WorldPackets::SendCreateCharacter(packet->systemAddress, player, c->GetXMLData(), username, c->GetGMLevel()); + auto* characterComponent = player->GetComponent(); + if (!characterComponent) return; + + WorldPackets::SendCreateCharacter(packet->systemAddress, player->GetComponent()->GetReputation(), player->GetObjectID(), c->GetXMLData(), username, c->GetGMLevel()); WorldPackets::SendServerState(packet->systemAddress); const auto respawnPoint = player->GetCharacter()->GetRespawnPoint(Game::zoneManager->GetZone()->GetWorldID()); Game::entityManager->ConstructEntity(player, UNASSIGNED_SYSTEM_ADDRESS, true); - if (respawnPoint != NiPoint3::ZERO) { - GameMessages::SendPlayerReachedRespawnCheckpoint(player, respawnPoint, NiQuaternion::IDENTITY); + if (respawnPoint != NiPoint3Constant::ZERO) { + GameMessages::SendPlayerReachedRespawnCheckpoint(player, respawnPoint, NiQuaternionConstant::IDENTITY); } Game::entityManager->ConstructAllEntities(packet->systemAddress); - auto* characterComponent = player->GetComponent(); - if (!characterComponent) return; characterComponent->RocketUnEquip(player); // Do charxml fixes here @@ -1096,7 +1112,6 @@ void HandlePacket(Packet* packet) { SystemAddress sysAddr = packet->systemAddress; SEND_PACKET; - // PacketUtils::SavePacket("lxfml packet " + std::to_string(bbbModel.id) + ".bin", (char*)bitStream.GetData(), bitStream.GetNumberOfBytesUsed()); } } @@ -1111,9 +1126,10 @@ void HandlePacket(Packet* packet) { //Mail::HandleNotificationRequest(packet->systemAddress, player->GetObjectID()); //Notify chat that a player has loaded: - { - const auto& playerName = player->GetCharacter()->GetName(); - //RakNet::RakString playerName(player->GetCharacter()->GetName().c_str()); + auto* character = player->GetCharacter(); + auto* user = character != nullptr ? character->GetParentUser() : nullptr; + if (user) { + const auto& playerName = character->GetName(); CBITSTREAM; BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT_INTERNAL, eChatInternalMessageType::PLAYER_ADDED_NOTIFICATION); @@ -1127,7 +1143,8 @@ void HandlePacket(Packet* packet) { bitStream.Write(zone.GetMapID()); bitStream.Write(zone.GetInstanceID()); bitStream.Write(zone.GetCloneID()); - bitStream.Write(player->GetParentUser()->GetMuteExpire()); + bitStream.Write(user->GetMuteExpire()); + bitStream.Write(player->GetGMLevel()); Game::chatServer->Send(&bitStream, SYSTEM_PRIORITY, RELIABLE, 0, Game::chatSysAddr, false); } @@ -1142,7 +1159,16 @@ void HandlePacket(Packet* packet) { } case eWorldMessageType::POSITION_UPDATE: { - ClientPackets::HandleClientPositionUpdate(packet->systemAddress, packet); + auto positionUpdate = ClientPackets::HandleClientPositionUpdate(packet); + + User* user = UserManager::Instance()->GetUser(packet->systemAddress); + if (!user) { + LOG("Unable to get user to parse position update"); + return; + } + + Entity* entity = Game::entityManager->GetEntity(user->GetLastUsedChar()->GetObjectID()); + if (entity) entity->ProcessPositionUpdate(positionUpdate); break; } @@ -1190,7 +1216,74 @@ void HandlePacket(Packet* packet) { } case eWorldMessageType::STRING_CHECK: { - ClientPackets::HandleChatModerationRequest(packet->systemAddress, packet); + auto request = ClientPackets::HandleChatModerationRequest(packet); + + // TODO: Find a good home for the logic in this case. + User* user = UserManager::Instance()->GetUser(packet->systemAddress); + if (!user) { + LOG("Unable to get user to parse chat moderation request"); + return; + } + + auto* entity = PlayerManager::GetPlayer(packet->systemAddress); + + if (entity == nullptr) { + LOG("Unable to get player to parse chat moderation request"); + return; + } + + // Check if the player has restricted chat access + auto* character = entity->GetCharacter(); + + if (character->HasPermission(ePermissionMap::RestrictedChatAccess)) { + // Send a message to the player + ChatPackets::SendSystemMessage( + packet->systemAddress, + u"This character has restricted chat access." + ); + + return; + } + + bool isBestFriend = false; + + if (request.chatLevel == 1) { + // Private chat + LWOOBJID idOfReceiver = LWOOBJID_EMPTY; + + { + auto characterIdFetch = Database::Get()->GetCharacterInfo(request.receiver); + + if (characterIdFetch) { + idOfReceiver = characterIdFetch->id; + } + } + const auto& bffMap = user->GetIsBestFriendMap(); + if (bffMap.find(request.receiver) == bffMap.end() && idOfReceiver != LWOOBJID_EMPTY) { + auto bffInfo = Database::Get()->GetBestFriendStatus(entity->GetObjectID(), idOfReceiver); + + if (bffInfo) { + isBestFriend = bffInfo->bestFriendStatus == 3; + } + + if (isBestFriend) { + user->UpdateBestFriendValue(request.receiver, true); + } + } else if (bffMap.find(request.receiver) != bffMap.end()) { + isBestFriend = true; + } + } + + std::vector> segments = Game::chatFilter->IsSentenceOkay(request.message, entity->GetGMLevel(), !(isBestFriend && request.chatLevel == 1)); + + bool bAllClean = segments.empty(); + + if (user->GetIsMuted()) { + bAllClean = false; + } + + user->SetLastChatMessageApproved(bAllClean); + WorldPackets::SendChatModerationResponse(packet->systemAddress, bAllClean, request.requestID, request.receiver, segments); break; } @@ -1198,7 +1291,29 @@ void HandlePacket(Packet* packet) { if (chatDisabled) { ChatPackets::SendMessageFail(packet->systemAddress); } else { - ClientPackets::HandleChatMessage(packet->systemAddress, packet); + auto chatMessage = ClientPackets::HandleChatMessage(packet); + + // TODO: Find a good home for the logic in this case. + User* user = UserManager::Instance()->GetUser(packet->systemAddress); + if (!user) { + LOG("Unable to get user to parse chat message"); + return; + } + + if (user->GetIsMuted()) { + user->GetLastUsedChar()->SendMuteNotice(); + return; + } + std::string playerName = user->GetLastUsedChar()->GetName(); + bool isMythran = user->GetLastUsedChar()->GetGMLevel() > eGameMasterLevel::CIVILIAN; + bool isOk = Game::chatFilter->IsSentenceOkay(GeneralUtils::UTF16ToWTF8(chatMessage.message), user->GetLastUsedChar()->GetGMLevel()).empty(); + LOG_DEBUG("Msg: %s was approved previously? %i", GeneralUtils::UTF16ToWTF8(chatMessage.message).c_str(), user->GetLastChatMessageApproved()); + if (!isOk) return; + if (!isOk && !isMythran) return; + + std::string sMessage = GeneralUtils::UTF16ToWTF8(chatMessage.message); + LOG("%s: %s", playerName.c_str(), sMessage.c_str()); + ChatPackets::SendChatMessage(packet->systemAddress, chatMessage.chatChannel, playerName, user->GetLoggedInChar(), isMythran, chatMessage.message); } break; @@ -1224,7 +1339,37 @@ void HandlePacket(Packet* packet) { case eWorldMessageType::UI_HELP_TOP_5: { - ClientPackets::SendTop5HelpIssues(packet); + auto language = ClientPackets::SendTop5HelpIssues(packet); + // TODO: Handle different languages in a nice way + // 0: en_US + // 1: pl_US + // 2: de_DE + // 3: en_GB + + // TODO: Find a good home for the logic in this case. + auto* user = UserManager::Instance()->GetUser(packet->systemAddress); + if (!user) return; + auto* character = user->GetLastUsedChar(); + if (!character) return; + auto* entity = character->GetEntity(); + if (!entity) return; + + AMFArrayValue data; + // Summaries + data.Insert("Summary0", Game::config->GetValue("help_0_summary")); + data.Insert("Summary1", Game::config->GetValue("help_1_summary")); + data.Insert("Summary2", Game::config->GetValue("help_2_summary")); + data.Insert("Summary3", Game::config->GetValue("help_3_summary")); + data.Insert("Summary4", Game::config->GetValue("help_4_summary")); + + // Descriptions + data.Insert("Description0", Game::config->GetValue("help_0_description")); + data.Insert("Description1", Game::config->GetValue("help_1_description")); + data.Insert("Description2", Game::config->GetValue("help_2_description")); + data.Insert("Description3", Game::config->GetValue("help_3_description")); + data.Insert("Description4", Game::config->GetValue("help_4_description")); + + GameMessages::SendUIMessageServerToSingleClient(entity, packet->systemAddress, "UIHelpTop5", data); break; } @@ -1240,7 +1385,7 @@ void WorldShutdownProcess(uint32_t zoneId) { for (auto i = 0; i < Game::server->GetReplicaManager()->GetParticipantCount(); ++i) { const auto& player = Game::server->GetReplicaManager()->GetParticipantAtIndex(i); - auto* entity = Player::GetPlayer(player); + auto* entity = PlayerManager::GetPlayer(player); LOG("Saving data!"); if (entity != nullptr && entity->GetCharacter() != nullptr) { auto* skillComponent = entity->GetComponent(); @@ -1293,6 +1438,7 @@ void FinalizeShutdown() { //Delete our objects here: Metrics::Clear(); + dpWorld::Shutdown(); Database::Destroy("WorldServer"); if (Game::chatFilter) delete Game::chatFilter; Game::chatFilter = nullptr; diff --git a/dZoneManager/Level.cpp b/dZoneManager/Level.cpp index 0a46dc89..9524d908 100644 --- a/dZoneManager/Level.cpp +++ b/dZoneManager/Level.cpp @@ -14,6 +14,7 @@ #include "CDFeatureGatingTable.h" #include "CDClientManager.h" #include "AssetManager.h" +#include "ClientVersion.h" #include "dConfig.h" Level::Level(Zone* parentZone, const std::string& filepath) { @@ -208,15 +209,15 @@ void Level::ReadSceneObjectDataChunk(std::istream& file, Header& header) { uint32_t objectsCount = 0; BinaryIO::BinaryRead(file, objectsCount); - CDFeatureGatingTable* featureGatingTable = CDClientManager::Instance().GetTable(); + CDFeatureGatingTable* featureGatingTable = CDClientManager::GetTable(); CDFeatureGating gating; - gating.major = 1; - gating.current = 10; - gating.minor = 64; - GeneralUtils::TryParse(Game::config->GetValue("version_major"), gating.major); - GeneralUtils::TryParse(Game::config->GetValue("version_current"), gating.current); - GeneralUtils::TryParse(Game::config->GetValue("version_minor"), gating.minor); + gating.major = + GeneralUtils::TryParse(Game::config->GetValue("version_major")).value_or(ClientVersion::major); + gating.current = + GeneralUtils::TryParse(Game::config->GetValue("version_current")).value_or(ClientVersion::current); + gating.minor = + GeneralUtils::TryParse(Game::config->GetValue("version_minor")).value_or(ClientVersion::minor); const auto zoneControlObject = Game::zoneManager->GetZoneControlObject(); DluAssert(zoneControlObject != nullptr); diff --git a/dZoneManager/Spawner.cpp b/dZoneManager/Spawner.cpp index 31188907..3baf193f 100644 --- a/dZoneManager/Spawner.cpp +++ b/dZoneManager/Spawner.cpp @@ -25,16 +25,6 @@ Spawner::Spawner(const SpawnerInfo info) { m_Start = m_Info.noTimedSpawn; - //ssssh... - if (m_EntityInfo.lot == 14718) { //AG - MAELSTROM SAMPLE - m_Info.groups.emplace_back("MaelstromSamples"); - } - - if (m_EntityInfo.lot == 14375) //AG - SPIDER BOSS EGG - { - m_Info.groups.emplace_back("EGG"); - } - int timerCount = m_Info.amountMaintained; if (m_Info.amountMaintained > m_Info.nodes.size()) { diff --git a/dZoneManager/Spawner.h b/dZoneManager/Spawner.h index 79ba40d1..686c7588 100644 --- a/dZoneManager/Spawner.h +++ b/dZoneManager/Spawner.h @@ -12,8 +12,8 @@ #include "EntityInfo.h" struct SpawnerNode { - NiPoint3 position = NiPoint3::ZERO; - NiQuaternion rotation = NiQuaternion::IDENTITY; + NiPoint3 position = NiPoint3Constant::ZERO; + NiQuaternion rotation = NiQuaternionConstant::IDENTITY; uint32_t nodeID = 0; uint32_t nodeMax = 1; std::vector entities; diff --git a/dZoneManager/Zone.cpp b/dZoneManager/Zone.cpp index cbaccded..42ce1215 100644 --- a/dZoneManager/Zone.cpp +++ b/dZoneManager/Zone.cpp @@ -17,6 +17,7 @@ #include "eTriggerCommandType.h" #include "eTriggerEventType.h" +#include "dNavMesh.h" Zone::Zone(const LWOMAPID& mapID, const LWOINSTANCEID& instanceID, const LWOCLONEID& cloneID) : m_ZoneID(mapID, instanceID, cloneID) { @@ -153,7 +154,7 @@ void Zone::LoadZoneIntoMemory() { std::string Zone::GetFilePathForZoneID() { //We're gonna go ahead and presume we've got the db loaded already: - CDZoneTableTable* zoneTable = CDClientManager::Instance().GetTable(); + CDZoneTableTable* zoneTable = CDClientManager::GetTable(); const CDZoneTable* zone = zoneTable->Query(this->GetZoneID().GetMapID()); if (zone != nullptr) { std::string toReturn = "maps/" + zone->zoneName; @@ -463,9 +464,9 @@ void Zone::LoadPath(std::istream& file) { // We verify the waypoint heights against the navmesh because in many movement paths, // the waypoint is located near 0 height, if (path.pathType == PathType::Movement) { - if (dpWorld::Instance().IsLoaded()) { + if (dpWorld::IsLoaded()) { // 2000 should be large enough for every world. - waypoint.position.y = dpWorld::Instance().GetNavMesh()->GetHeightAtPoint(waypoint.position, 2000.0f); + waypoint.position.y = dpWorld::GetNavMesh()->GetHeightAtPoint(waypoint.position, 2000.0f); } } path.pathWaypoints.push_back(waypoint); diff --git a/dZoneManager/dZoneManager.cpp b/dZoneManager/dZoneManager.cpp index 1da1bce8..546ce9a4 100644 --- a/dZoneManager/dZoneManager.cpp +++ b/dZoneManager/dZoneManager.cpp @@ -29,7 +29,7 @@ void dZoneManager::Initialize(const LWOZONEID& zoneID) { LOT zoneControlTemplate = 2365; - CDZoneTableTable* zoneTable = CDClientManager::Instance().GetTable(); + CDZoneTableTable* zoneTable = CDClientManager::GetTable(); if (zoneTable != nullptr) { const CDZoneTable* zone = zoneTable->Query(zoneID.GetMapID()); @@ -212,7 +212,7 @@ uint32_t dZoneManager::GetUniqueMissionIdStartingValue() { bool dZoneManager::CheckIfAccessibleZone(LWOMAPID zoneID) { //We're gonna go ahead and presume we've got the db loaded already: - CDZoneTableTable* zoneTable = CDClientManager::Instance().GetTable(); + CDZoneTableTable* zoneTable = CDClientManager::GetTable(); const CDZoneTable* zone = zoneTable->Query(zoneID); if (zone != nullptr) { return Game::assetManager->HasFile(("maps/" + zone->zoneName).c_str()); diff --git a/docker-compose.yml b/docker-compose.yml index 1d83f010..a7954718 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,58 +1,73 @@ version: "3" +name: dlu services: darkflamedb: - image: mariadb:latest - environment: - - MARIADB_USER=${MARIADB_USER:-darkflame} - - MARIADB_PASSWORD=${MARIADB_PASSWORD:-darkflame} - - MARIADB_ROOT_PASSWORD=${MARIADB_ROOT_PASSWORD:-darkflame} - - MARIADB_DATABASE=${MARIADB_DATABASE:-darkflame} networks: - darkflame + image: mariadb:latest + volumes: + - ${DB_DATA_DIR:-./db/data}:/var/lib/mysql + environment: + - MARIADB_RANDOM_ROOT_PASSWORD=1 + - MARIADB_USER=${MARIADB_USER:-darkflame} + - MARIADB_PASSWORD=${MARIADB_PASSWORD:?error} + - MARIADB_DATABASE=${MARIADB_DATABASE:-darkflame} + healthcheck: + test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"] + start_period: 10s + interval: 10s + timeout: 5s + retries: 3 darkflameserver: networks: - darkflame image: ghcr.io/darkflameuniverse/darkflameserver:latest volumes: - - $HOST_CONFIG_DIR:/app/configs/ - - $HOST_CLIENT_LOCATION:$CLIENT_LOCATION:ro - - $HOST_RESSERVER_LOCATION:/app/resServer/ - - $HOST_DUMP_FOLDER:$DUMP_FOLDER - - $HOST_LOGS_DIR:/app/logs/ + - ${HOST_CONFIG_DIR:-./server/config}:/app/configs/ + - ${CLIENT_PATH:-./client}:/app/luclient:ro + - ${HOST_RESSERVER_LOCATION:-./server/res}:/app/resServer/ + - ${HOST_DUMP_FOLDER:-./server/dump}:/app/dump/ + - ${HOST_LOGS_DIR:-./server/logs}:/app/logs/ environment: + - CLIENT_LOCATION=/app/luclient - DLU_CONFIG_DIR=/app/configs + - DUMP_FOLDER=/app/dump - MYSQL_HOST=darkflamedb - MYSQL_DATABASE=${MARIADB_DATABASE:-darkflame} - MYSQL_USERNAME=${MARIADB_USER:-darkflame} - - MYSQL_PASSWORD=${MARIADB_PASSWORD:-darkflame} + - MYSQL_PASSWORD=${MARIADB_PASSWORD:?error} - EXTERNAL_IP=${EXTERNAL_IP:-localhost} - - DUMP_FOLDER=$DUMP_FOLDER - - CLIENT_LOCATION=$CLIENT_LOCATION + - CLIENT_NET_VERSION=${CLIENT_NET_VERSION:-171022} depends_on: - darkflamedb ports: - "1001:1001/udp" - "2005:2005/udp" - "3000-3300:3000-3300/udp" + healthcheck: + test: ["CMD", "ls", "/app/resServer/CDServer.sqlite"] darkflameweb: networks: - darkflame image: ghcr.io/darkflameuniverse/nexusdashboard:latest volumes: - - $HOST_CLIENT_LOCATION:/app/luclient:ro - - $HOST_CACHE_LOCATION:/app/cache - - $HOST_CD_SQLITE_LOCATION:/app/cdclient:ro + - ${CLIENT_PATH:-./client}:/app/luclient:ro + - ${HOST_CACHE_LOCATION:-./web/cache}:/app/cache + - ${HOST_RESSERVER_LOCATION:-./server/res}:/app/cdclient:ro + - ${HOST_ND_LOGS_LOCATION:-./web/logs}:/logs environment: - - APP_SECRET_KEY=$APP_SECRET_KEY - - APP_DATABASE_URI=mysql+pymysql://${MARIADB_USER:-darkflame}:${MARIADB_PASSWORD:-darkflame}@darkflamedb:3306/${MARIADB_DATABASE:-darkflame} - - CLIENT_LOCATION=/app/luclient - - CACHE_LOCATION=/app/cache - - CD_SQLITE_LOCATION=/app/cdclient + - APP_SECRET_KEY=${ACCOUNT_MANAGER_SECRET:?error} + - APP_DATABASE_URI=mysql+pymysql://${MARIADB_USER:-darkflame}:${MARIADB_PASSWORD:?error}@darkflamedb:3306/${MARIADB_DATABASE:-darkflame} + - CLIENT_LOCATION=/app/luclient/ + - CACHE_LOCATION=/app/cache/ + - CD_SQLITE_LOCATION=/app/cdclient/ + - USER_ENABLE_REGISTER=1 # "0" is _not_ false, to disable, remove this line depends_on: - darkflamedb + - darkflameserver ports: - 8000:8000 healthcheck: diff --git a/resources/navmeshes.zip b/resources/navmeshes.zip index c3053520..95948656 100644 Binary files a/resources/navmeshes.zip and b/resources/navmeshes.zip differ diff --git a/resources/worldconfig.ini b/resources/worldconfig.ini index c68b42d2..91028ffe 100644 --- a/resources/worldconfig.ini +++ b/resources/worldconfig.ini @@ -73,3 +73,7 @@ help_4_description=Visit Discussions on the DarkflameServer GitHub page
to a # Toggleable quality of life feature to allow users to skip most cinematics. allow_players_to_skip_cinematics=0 + +# Customizable message for what to say when there is a cdclient fdb mismatch +cdclient_mismatch_title=Version out of date +cdclient_mismatch_message=We detected that your client is out of date. Please update your client to the latest version. diff --git a/tests/dCommonTests/TestLUString.cpp b/tests/dCommonTests/TestLUString.cpp index 3abec985..30cc0f73 100644 --- a/tests/dCommonTests/TestLUString.cpp +++ b/tests/dCommonTests/TestLUString.cpp @@ -101,7 +101,7 @@ TEST(LUString33Test, SerializeReadTestNew) { std::string testString; for (int i = 0; i < 33; i++) testString += "a"; bitStream.Write(LUString(testString, 33)); - LUString result(33); + LUString result; ASSERT_EQ(result.size, 33); ASSERT_TRUE(bitStream.Read(result)); ASSERT_EQ(bitStream.GetNumberOfUnreadBits(), 0); @@ -113,7 +113,7 @@ TEST(LUString33Test, SerializeReadTestNewPartial) { std::string testString; for (int i = 0; i < 15; i++) testString += "a"; bitStream.Write(LUString(testString, 33)); - LUString result(33); + LUString result; ASSERT_EQ(result.size, 33); ASSERT_TRUE(bitStream.Read(result)); ASSERT_EQ(bitStream.GetNumberOfUnreadBits(), 0); diff --git a/tests/dCommonTests/TestLUWString.cpp b/tests/dCommonTests/TestLUWString.cpp index a16dd911..e812ae0c 100644 --- a/tests/dCommonTests/TestLUWString.cpp +++ b/tests/dCommonTests/TestLUWString.cpp @@ -101,7 +101,7 @@ TEST(LUWString33Test, SerializeReadTestNew) { std::u16string testString; for (int i = 0; i < 33; i++) testString += u'ü'; bitStream.Write(LUWString(testString, 33)); - LUWString result(33); + LUWString result; ASSERT_EQ(result.size, 33); ASSERT_TRUE(bitStream.Read(result)); ASSERT_EQ(bitStream.GetNumberOfUnreadBits(), 0); @@ -113,7 +113,7 @@ TEST(LUWString33Test, SerializeReadTestNewPartial) { std::u16string testString; for (int i = 0; i < 15; i++) testString += u'ü'; bitStream.Write(LUWString(testString, 33)); - LUWString result(33); + LUWString result; ASSERT_EQ(result.size, 33); ASSERT_TRUE(bitStream.Read(result)); ASSERT_EQ(bitStream.GetNumberOfUnreadBits(), 0); diff --git a/tests/dCommonTests/TestNiPoint3.cpp b/tests/dCommonTests/TestNiPoint3.cpp index fbc98eb0..49814d15 100644 --- a/tests/dCommonTests/TestNiPoint3.cpp +++ b/tests/dCommonTests/TestNiPoint3.cpp @@ -8,9 +8,9 @@ */ TEST(dCommonTests, NiPoint3Test) { // Check that Unitize works - ASSERT_EQ(NiPoint3(3, 0, 0).Unitize(), NiPoint3::UNIT_X); + ASSERT_EQ(NiPoint3(3, 0, 0).Unitize(), NiPoint3Constant::UNIT_X); // Check what unitize does to a vector of length 0 - ASSERT_EQ(NiPoint3::ZERO.Unitize(), NiPoint3::ZERO); + ASSERT_EQ(NiPoint3Constant::ZERO.Unitize(), NiPoint3Constant::ZERO); } TEST(dCommonTests, NiPoint3OperatorTest) { diff --git a/tests/dGameTests/GameDependencies.h b/tests/dGameTests/GameDependencies.h index 92dafc0c..0352c47a 100644 --- a/tests/dGameTests/GameDependencies.h +++ b/tests/dGameTests/GameDependencies.h @@ -4,6 +4,7 @@ #include "Game.h" #include "Logger.h" #include "dServer.h" +#include "CDClientManager.h" #include "EntityInfo.h" #include "EntityManager.h" #include "dZoneManager.h" @@ -26,8 +27,8 @@ public: class GameDependenciesTest : public ::testing::Test { protected: void SetUpDependencies() { - info.pos = NiPoint3::ZERO; - info.rot = NiQuaternion::IDENTITY; + info.pos = NiPoint3Constant::ZERO; + info.rot = NiQuaternionConstant::IDENTITY; info.scale = 1.0f; info.spawner = nullptr; info.lot = 999; @@ -41,6 +42,9 @@ protected: auto worldConfig = new WorldConfig(); worldConfig->petFollowRadius = 10.0f; Game::zoneManager->SetWorldConfig(worldConfig); + + // Create a CDClientManager instance and load from defaults + CDClientManager::LoadValuesFromDefaults(); } void TearDownDependencies() { diff --git a/thirdparty/CMakeLists.txt b/thirdparty/CMakeLists.txt index 11d278c0..40c81544 100644 --- a/thirdparty/CMakeLists.txt +++ b/thirdparty/CMakeLists.txt @@ -57,3 +57,5 @@ if(UNIX AND NOT APPLE) include_directories(${backtrace_SOURCE_DIR}) endif() endif() + +add_subdirectory(MD5) diff --git a/thirdparty/MD5/CMakeLists.txt b/thirdparty/MD5/CMakeLists.txt new file mode 100644 index 00000000..755b4519 --- /dev/null +++ b/thirdparty/MD5/CMakeLists.txt @@ -0,0 +1,6 @@ +add_library(MD5 "MD5.cpp") + +# Disable deprecation warnings on MD5.cpp for Apple Clang +if (APPLE) + set_source_files_properties("MD5.cpp" PROPERTIES COMPILE_FLAGS "-Wno-deprecated-declarations") +endif() diff --git a/dCommon/MD5.cpp b/thirdparty/MD5/MD5.cpp similarity index 100% rename from dCommon/MD5.cpp rename to thirdparty/MD5/MD5.cpp diff --git a/dCommon/MD5.h b/thirdparty/MD5/MD5.h similarity index 100% rename from dCommon/MD5.h rename to thirdparty/MD5/MD5.h diff --git a/thirdparty/SQLite/CMakeLists.txt b/thirdparty/SQLite/CMakeLists.txt index aa7a6423..3aa066a4 100644 --- a/thirdparty/SQLite/CMakeLists.txt +++ b/thirdparty/SQLite/CMakeLists.txt @@ -10,5 +10,10 @@ if(UNIX) target_link_libraries(sqlite3 pthread dl m) # -Wno-unused-result -Wno-unknown-pragmas -fpermissive - target_compile_options(sqlite3 PRIVATE "-Wno-return-local-addr" "-Wno-maybe-uninitialized") + target_compile_options(sqlite3 PRIVATE) + if(NOT APPLE) + target_compile_options(sqlite3 PRIVATE "-Wno-return-local-addr" "-Wno-maybe-uninitialized") + else() + target_compile_options(sqlite3 PRIVATE "-Wno-return-stack-address" "-Wno-uninitialized" "-Wno-deprecated-declarations") + endif() endif()