From c81b58e1be2515f5d52eee886d8bb381b2ac8d5a Mon Sep 17 00:00:00 2001 From: David Cernat Date: Mon, 21 Nov 2016 20:37:04 +0200 Subject: [PATCH] Add spell script functions to tes3mp server --- apps/openmw-mp/CMakeLists.txt | 4 +- apps/openmw-mp/Player.hpp | 3 + apps/openmw-mp/Script/Functions/Items.cpp | 16 ++--- apps/openmw-mp/Script/Functions/Items.hpp | 8 +-- apps/openmw-mp/Script/Functions/Spells.cpp | 80 ++++++++++++++++++++++ apps/openmw-mp/Script/Functions/Spells.hpp | 34 +++++++++ apps/openmw-mp/Script/ScriptFunctions.hpp | 6 +- 7 files changed, 133 insertions(+), 18 deletions(-) create mode 100644 apps/openmw-mp/Script/Functions/Spells.cpp create mode 100644 apps/openmw-mp/Script/Functions/Spells.hpp diff --git a/apps/openmw-mp/CMakeLists.txt b/apps/openmw-mp/CMakeLists.txt index 9e7a7e8ff..ca5028a53 100644 --- a/apps/openmw-mp/CMakeLists.txt +++ b/apps/openmw-mp/CMakeLists.txt @@ -66,8 +66,8 @@ set(SERVER Script/ScriptFunctions.cpp Script/Functions/CharClass.cpp Script/Functions/Chat.cpp Script/Functions/GUI.cpp - Script/Functions/Items.cpp Script/Functions/Stats.cpp Script/Functions/Timer.cpp - Script/Functions/Translocations.cpp Script/Functions/World.cpp + Script/Functions/Items.cpp Script/Functions/Stats.cpp Script/Functions/Spells.cpp + Script/Functions/Timer.cpp Script/Functions/Translocations.cpp Script/Functions/World.cpp Script/API/TimerAPI.cpp Script/API/PublicFnAPI.cpp ${PawnScript_Sources} diff --git a/apps/openmw-mp/Player.hpp b/apps/openmw-mp/Player.hpp index 51b61600e..4e4298d5f 100644 --- a/apps/openmw-mp/Player.hpp +++ b/apps/openmw-mp/Player.hpp @@ -66,8 +66,11 @@ public: std::chrono::steady_clock::time_point getLastAttackerTime(); virtual ~Player(); + public: mwmp::Inventory inventorySendBuffer; + mwmp::Spellbook spellbookSendBuffer; + private: bool handshakeState; int loadState; diff --git a/apps/openmw-mp/Script/Functions/Items.cpp b/apps/openmw-mp/Script/Functions/Items.cpp index a97106137..892de79cc 100644 --- a/apps/openmw-mp/Script/Functions/Items.cpp +++ b/apps/openmw-mp/Script/Functions/Items.cpp @@ -24,12 +24,12 @@ unsigned int ItemFunctions::GetInventorySize(unsigned short pid) noexcept return player->inventory.count; } -void ItemFunctions::EquipItem(unsigned short pid, unsigned short slot, const char *itemName, unsigned int count, int health) noexcept +void ItemFunctions::EquipItem(unsigned short pid, unsigned short slot, const char *itemId, unsigned int count, int health) noexcept { Player *player; GET_PLAYER(pid, player,); - player->EquipedItem(slot)->refid = itemName; + player->EquipedItem(slot)->refid = itemId; player->EquipedItem(slot)->count = count; player->EquipedItem(slot)->health = health; } @@ -42,13 +42,13 @@ void ItemFunctions::UnequipItem(unsigned short pid, unsigned short slot) noexcep ItemFunctions::EquipItem(pid, slot, "", 0, -1); } -void ItemFunctions::AddItem(unsigned short pid, const char* itemName, unsigned int count, int health) noexcept +void ItemFunctions::AddItem(unsigned short pid, const char* itemId, unsigned int count, int health) noexcept { Player *player; GET_PLAYER(pid, player, ); Item item; - item.refid = itemName; + item.refid = itemId; item.count = count; item.health = health; @@ -56,13 +56,13 @@ void ItemFunctions::AddItem(unsigned short pid, const char* itemName, unsigned i player->inventorySendBuffer.action = Inventory::ADDITEM; } -void ItemFunctions::RemoveItem(unsigned short pid, const char* itemName, unsigned short count) noexcept +void ItemFunctions::RemoveItem(unsigned short pid, const char* itemId, unsigned short count) noexcept { Player *player; GET_PLAYER(pid, player, ); Item item; - item.refid = itemName; + item.refid = itemId; item.count = count; player->inventorySendBuffer.items.push_back(item); @@ -78,13 +78,13 @@ void ItemFunctions::ClearInventory(unsigned short pid) noexcept player->inventorySendBuffer.action = Inventory::UPDATE; } -bool ItemFunctions::HasItemEquipped(unsigned short pid, const char* itemName) +bool ItemFunctions::HasItemEquipped(unsigned short pid, const char* itemId) { Player *player; GET_PLAYER(pid, player, false); for (int slot = 0; slot < MWWorld::InventoryStore::Slots; slot++) - if (Misc::StringUtils::ciEqual(player->EquipedItem(slot)->refid, itemName)) + if (Misc::StringUtils::ciEqual(player->EquipedItem(slot)->refid, itemId)) return true; return false; } diff --git a/apps/openmw-mp/Script/Functions/Items.hpp b/apps/openmw-mp/Script/Functions/Items.hpp index af1f4c1f5..e26d00833 100644 --- a/apps/openmw-mp/Script/Functions/Items.hpp +++ b/apps/openmw-mp/Script/Functions/Items.hpp @@ -36,14 +36,14 @@ public: static int GetEquipmentSize() noexcept; static unsigned int GetInventorySize(unsigned short pid) noexcept; - static void EquipItem(unsigned short pid, unsigned short slot, const char* itemName, unsigned int count, int health) noexcept; + static void EquipItem(unsigned short pid, unsigned short slot, const char* itemId, unsigned int count, int health) noexcept; static void UnequipItem(unsigned short pid, unsigned short slot) noexcept; - static void AddItem(unsigned short pid, const char* itemName, unsigned int count, int health) noexcept; - static void RemoveItem(unsigned short pid, const char* itemName, unsigned short count) noexcept; + static void AddItem(unsigned short pid, const char* itemId, unsigned int count, int health) noexcept; + static void RemoveItem(unsigned short pid, const char* itemId, unsigned short count) noexcept; static void ClearInventory(unsigned short pid) noexcept; - static bool HasItemEquipped(unsigned short pid, const char* itemName); + static bool HasItemEquipped(unsigned short pid, const char* itemId); static const char *GetEquipmentItemId(unsigned short pid, unsigned short slot) noexcept; static int GetEquipmentItemCount(unsigned short pid, unsigned short slot) noexcept; diff --git a/apps/openmw-mp/Script/Functions/Spells.cpp b/apps/openmw-mp/Script/Functions/Spells.cpp new file mode 100644 index 000000000..59866cc42 --- /dev/null +++ b/apps/openmw-mp/Script/Functions/Spells.cpp @@ -0,0 +1,80 @@ +#include "Spells.hpp" +#include +#include +#include +#include + +using namespace mwmp; + +unsigned int SpellFunctions::GetSpellbookSize(unsigned short pid) noexcept +{ + Player *player; + GET_PLAYER(pid, player, 0); + + return player->spellbook.count; +} + +void SpellFunctions::AddSpell(unsigned short pid, const char* spellId) noexcept +{ + Player *player; + GET_PLAYER(pid, player, ); + + Spell spell; + spell.id = spellId; + + player->spellbookSendBuffer.spells.push_back(spell); + player->spellbookSendBuffer.action = Spellbook::ADD; +} + +void SpellFunctions::RemoveSpell(unsigned short pid, const char* spellId) noexcept +{ + Player *player; + GET_PLAYER(pid, player, ); + + Spell spell; + spell.id = spellId; + + player->spellbookSendBuffer.spells.push_back(spell); + player->spellbookSendBuffer.action = Spellbook::REMOVE; +} + +void SpellFunctions::ClearSpellbook(unsigned short pid) noexcept +{ + Player *player; + GET_PLAYER(pid, player, ); + + player->spellbookSendBuffer.spells.clear(); + player->spellbookSendBuffer.action = Spellbook::UPDATE; +} + +bool SpellFunctions::HasSpell(unsigned short pid, const char* spellId) +{ + Player *player; + GET_PLAYER(pid, player, false); + + for (int i = 0; i < player->spellbook.count; i++) + if (Misc::StringUtils::ciEqual(player->spellbook.spells.at(i).id, spellId)) + return true; + return false; +} + +const char *SpellFunctions::GetSpellId(unsigned short pid, unsigned int i) noexcept +{ + Player *player; + GET_PLAYER(pid, player, ""); + + if (i >= player->spellbook.count) + return "invalid"; + + return player->spellbook.spells.at(i).id.c_str(); +} + +void SpellFunctions::SendSpellbook(unsigned short pid) noexcept +{ + Player *player; + GET_PLAYER(pid, player, ); + std::swap(player->spellbook, player->spellbookSendBuffer); + mwmp::Networking::get().getPlayerController()->GetPacket(ID_GAME_SPELLBOOK)->Send(player, false); + player->spellbook = std::move(player->spellbookSendBuffer); + player->spellbookSendBuffer.spells.clear(); +} diff --git a/apps/openmw-mp/Script/Functions/Spells.hpp b/apps/openmw-mp/Script/Functions/Spells.hpp new file mode 100644 index 000000000..da882cb8c --- /dev/null +++ b/apps/openmw-mp/Script/Functions/Spells.hpp @@ -0,0 +1,34 @@ +#ifndef OPENMW_SPELLS_HPP +#define OPENMW_SPELLS_HPP + +#define SPELLAPI \ + {"GetSpellbookSize", SpellFunctions::GetSpellbookSize},\ + \ + {"AddSpell", SpellFunctions::AddSpell},\ + {"RemoveSpell", SpellFunctions::RemoveSpell},\ + {"ClearSpellbook", SpellFunctions::ClearInventory},\ + \ + {"HasSpell", SpellFunctions::HasSpell},\ + {"GetSpellId", SpellFunctions::GetSpellId},\ + \ + {"SendSpellbook", SpellFunctions::SendSpellbook} + +class SpellFunctions +{ +public: + + static unsigned int GetSpellbookSize(unsigned short pid) noexcept; + + static void AddSpell(unsigned short pid, const char* spellId) noexcept; + static void RemoveSpell(unsigned short pid, const char* spellId) noexcept; + static void ClearSpellbook(unsigned short pid) noexcept; + + static bool HasSpell(unsigned short pid, const char* itemName); + static const char *GetSpellId(unsigned short pid, unsigned int i) noexcept; + + static void SendSpellbook(unsigned short pid) noexcept; +private: + +}; + +#endif //OPENMW_SPELLS_HPP diff --git a/apps/openmw-mp/Script/ScriptFunctions.hpp b/apps/openmw-mp/Script/ScriptFunctions.hpp index 1354b04d5..95d7e185f 100644 --- a/apps/openmw-mp/Script/ScriptFunctions.hpp +++ b/apps/openmw-mp/Script/ScriptFunctions.hpp @@ -10,6 +10,7 @@ #include