Add spell script functions to tes3mp server

coverity_scan^2
David Cernat 8 years ago
parent 8aeb3a6cc6
commit c81b58e1be

@ -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}

@ -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;

@ -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;
}

@ -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;

@ -0,0 +1,80 @@
#include "Spells.hpp"
#include <apps/openmw-mp/Script/ScriptFunctions.hpp>
#include <components/openmw-mp/NetworkMessages.hpp>
#include <apps/openmw-mp/Networking.hpp>
#include <components/misc/stringops.hpp>
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();
}

@ -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

@ -10,6 +10,7 @@
#include <Script/Functions/GUI.hpp>
#include <Script/Functions/Stats.hpp>
#include <Script/Functions/Items.hpp>
#include <Script/Functions/Spells.hpp>
#include <Script/Functions/World.hpp>
#include <RakNetTypes.h>
//#include <amx/amx.h>
@ -68,7 +69,6 @@ public:
{"MakePublic", ScriptFunctions::MakePublic},
{"CallPublic", reinterpret_cast<Function<void>>(ScriptFunctions::CallPublic)},
{"StartTimer", ScriptFunctions::StartTimer},
{"StopTimer", ScriptFunctions::StopTimer},
{"RestartTimer", ScriptFunctions::RestartTimer},
@ -77,7 +77,6 @@ public:
{"StopServer", ScriptFunctions::StopServer},
// {"Cast", ScriptFunctions::Cast},
{"SendMessage", ScriptFunctions::SendMessage},
{"Kick", ScriptFunctions::Kick},
{"GetServerVersion", ScriptFunctions::GetServerVersion},
@ -87,11 +86,10 @@ public:
TRANSLOCATIONFUNCTIONS,
STATSFUNCTIONS,
ITEMAPI,
SPELLAPI,
GUIFUNCTIONS,
CHARCLASSFUNCTIONS,
WORLDFUNCTIONS,
};
static constexpr ScriptCallbackData callbacks[]{

Loading…
Cancel
Save