From 48b8d43ff891216735720b9844dba31583a4245b Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Wed, 25 Oct 2023 15:30:31 +1100 Subject: [PATCH] Add player Lua functions from SE --- source/game/StarArmorWearer.cpp | 14 ++++- source/game/StarArmorWearer.hpp | 2 + source/game/StarPlayer.cpp | 9 ++- source/game/StarPlayer.hpp | 3 +- source/game/StarUniverseClient.cpp | 2 +- .../game/scripting/StarPlayerLuaBindings.cpp | 61 +++++++++++++++++++ 6 files changed, 86 insertions(+), 5 deletions(-) diff --git a/source/game/StarArmorWearer.cpp b/source/game/StarArmorWearer.cpp index fe61bad..acc3de2 100644 --- a/source/game/StarArmorWearer.cpp +++ b/source/game/StarArmorWearer.cpp @@ -26,7 +26,7 @@ ArmorWearer::ArmorWearer() : m_lastNude(true) { addNetElement(&m_legsCosmeticItemDataNetState); addNetElement(&m_backCosmeticItemDataNetState); - m_headNeedsSync = m_chestNeedsSync = m_legsNeedsSync = m_backNeedsSync = true; + reset(); } void ArmorWearer::setupHumanoidClothingDrawables(Humanoid& humanoid, bool forceNude) { @@ -140,6 +140,18 @@ void ArmorWearer::effects(EffectEmitter& effectEmitter) { effectEmitter.addEffectSources("backArmor", item->effectSources()); } +void ArmorWearer::reset() { + m_headNeedsSync = m_chestNeedsSync = m_legsNeedsSync = m_backNeedsSync = true; + m_headItem .reset(); + m_chestItem.reset(); + m_legsItem .reset(); + m_backItem .reset(); + m_headCosmeticItem .reset(); + m_chestCosmeticItem.reset(); + m_legsCosmeticItem .reset(); + m_backCosmeticItem .reset(); +} + Json ArmorWearer::diskStore() const { JsonObject res; if (m_headItem) diff --git a/source/game/StarArmorWearer.hpp b/source/game/StarArmorWearer.hpp index 3b02ab2..84df8c7 100644 --- a/source/game/StarArmorWearer.hpp +++ b/source/game/StarArmorWearer.hpp @@ -30,6 +30,8 @@ public: void effects(EffectEmitter& effectEmitter); List statusEffects() const; + void reset(); + Json diskStore() const; void diskLoad(Json const& diskStore); diff --git a/source/game/StarPlayer.cpp b/source/game/StarPlayer.cpp index b3e48a3..3969646 100644 --- a/source/game/StarPlayer.cpp +++ b/source/game/StarPlayer.cpp @@ -245,6 +245,7 @@ void Player::diskLoad(Json const& diskStore) { m_genericProperties = diskStore.getObject("genericProperties"); + m_armor->reset(); refreshArmor(); m_codexes->learnInitialCodexes(species()); @@ -2162,10 +2163,14 @@ void Player::addChatMessage(String const& message) { m_pendingChatActions.append(SayChatAction{entityId(), message, mouthPosition()}); } -void Player::addEmote(HumanoidEmote const& emote) { +void Player::addEmote(HumanoidEmote const& emote, Maybe emoteCooldown) { starAssert(!isSlave()); m_emoteState = emote; - m_emoteCooldownTimer = m_emoteCooldown; + m_emoteCooldownTimer = emoteCooldown.value(m_emoteCooldown); +} + +pair Player::currentEmote() const { + return make_pair(m_emoteState, m_emoteCooldownTimer); } List Player::pullPendingChatActions() { diff --git a/source/game/StarPlayer.hpp b/source/game/StarPlayer.hpp index 21fe2d6..1ac97a2 100644 --- a/source/game/StarPlayer.hpp +++ b/source/game/StarPlayer.hpp @@ -383,7 +383,8 @@ public: void queueItemPickupMessage(ItemPtr const& item); void addChatMessage(String const& message); - void addEmote(HumanoidEmote const& emote); + void addEmote(HumanoidEmote const& emote, Maybe emoteCooldown = {}); + pair currentEmote() const; List pullPendingChatActions() override; diff --git a/source/game/StarUniverseClient.cpp b/source/game/StarUniverseClient.cpp index 3423f2d..c492d89 100644 --- a/source/game/StarUniverseClient.cpp +++ b/source/game/StarUniverseClient.cpp @@ -574,7 +574,7 @@ bool UniverseClient::switchPlayer(String const& name) { if (auto uuid = m_playerStorage->playerUuidByName(name, mainPlayer()->uuid())) return switchPlayer(*uuid); else - return false; + return switchPlayer(Uuid(name)); } UniverseClient::ReloadPlayerCallback& UniverseClient::playerReloadPreCallback() { diff --git a/source/game/scripting/StarPlayerLuaBindings.cpp b/source/game/scripting/StarPlayerLuaBindings.cpp index 6944e59..abd6d72 100644 --- a/source/game/scripting/StarPlayerLuaBindings.cpp +++ b/source/game/scripting/StarPlayerLuaBindings.cpp @@ -17,6 +17,16 @@ namespace Star { LuaCallbacks LuaBindings::makePlayerCallbacks(Player* player) { LuaCallbacks callbacks; + callbacks.registerCallback("save", [player]() { return player->diskStore(); }); + callbacks.registerCallback("load", [player](Json const& data) { + auto saved = player->diskStore(); + try { player->diskLoad(data); } + catch (StarException const&) { + player->diskLoad(saved); + throw; + } + }); + callbacks.registerCallback( "humanoidIdentity", [player]() { return player->humanoid()->identity().toJson(); }); callbacks.registerCallback("setHumanoidIdentity", [player](Json const& id) { player->setIdentity(HumanoidIdentity(id)); }); @@ -85,6 +95,9 @@ LuaCallbacks LuaBindings::makePlayerCallbacks(Player* player) { } }); + callbacks.registerCallback(" description", [player]() { return player->description(); }); + callbacks.registerCallback("setDescription", [player](String const& description) { player->setDescription(description); }); + callbacks.registerCallback( "name", [player]() { return player->name(); }); callbacks.registerCallback("setName", [player](String const& name) { player->setName(name); }); @@ -107,6 +120,54 @@ LuaCallbacks LuaBindings::makePlayerCallbacks(Player* player) { callbacks.registerCallback( "interactRadius", [player]() { return player->interactRadius(); }); callbacks.registerCallback("setInteractRadius", [player](float radius) { player->setInteractRadius(radius); }); + callbacks.registerCallback("actionBarGroup", [player]() { + return luaTupleReturn(player->inventory()->customBarGroup() + 1, player->inventory()->customBarGroups()); + }); + + callbacks.registerCallback("setActionBarGroup", [player](int group) { + player->inventory()->setCustomBarGroup((group - 1) % (unsigned)player->inventory()->customBarGroups()); + }); + + callbacks.registerCallback("selectedActionBarSlot", [player](LuaEngine& engine) -> Maybe { + if (auto barLocation = player->inventory()->selectedActionBarLocation()) { + if (auto index = barLocation.ptr()) + return engine.luaFrom(*index + 1); + else + return engine.luaFrom(EssentialItemNames.getRight(barLocation.get())); + } + else { + return {}; + } + }); + + callbacks.registerCallback("setSelectedActionBarSlot", [player](MVariant const& slot) { + auto inventory = player->inventory(); + if (!slot) + inventory->selectActionBarLocation(SelectedActionBarLocation()); + else if (auto index = slot.ptr()) { + CustomBarIndex wrapped = (*index - 1) % (unsigned)inventory->customBarIndexes(); + inventory->selectActionBarLocation(SelectedActionBarLocation(wrapped)); + } else { + EssentialItem const& item = EssentialItemNames.getLeft(slot.get()); + inventory->selectActionBarLocation(SelectedActionBarLocation(item)); + } + }); + + callbacks.registerCallback("setDamageTeam", [player](String const& typeName, Maybe teamNumber) { + player->setTeam(EntityDamageTeam(TeamTypeNames.getLeft(typeName), teamNumber.value(0))); + }); + + callbacks.registerCallback("say", [player](String const& message) { player->addChatMessage(message); }); + + callbacks.registerCallback("emote", [player](String const& emote, Maybe cooldown) { + player->addEmote(HumanoidEmoteNames.getLeft(emote), cooldown); + }); + + callbacks.registerCallback("currentEmote", [player]() { + auto currentEmote = player->currentEmote(); + return luaTupleReturn(HumanoidEmoteNames.getRight(currentEmote.first), currentEmote.second); + }); + callbacks.registerCallback("aimPosition", [player]() { return player->aimPosition(); }); callbacks.registerCallback("id", [player]() { return player->entityId(); });