Add player Lua functions from SE

This commit is contained in:
Kae 2023-10-25 15:30:31 +11:00
parent ced12b62e0
commit 48b8d43ff8
6 changed files with 86 additions and 5 deletions

View File

@ -26,7 +26,7 @@ ArmorWearer::ArmorWearer() : m_lastNude(true) {
addNetElement(&m_legsCosmeticItemDataNetState); addNetElement(&m_legsCosmeticItemDataNetState);
addNetElement(&m_backCosmeticItemDataNetState); addNetElement(&m_backCosmeticItemDataNetState);
m_headNeedsSync = m_chestNeedsSync = m_legsNeedsSync = m_backNeedsSync = true; reset();
} }
void ArmorWearer::setupHumanoidClothingDrawables(Humanoid& humanoid, bool forceNude) { void ArmorWearer::setupHumanoidClothingDrawables(Humanoid& humanoid, bool forceNude) {
@ -140,6 +140,18 @@ void ArmorWearer::effects(EffectEmitter& effectEmitter) {
effectEmitter.addEffectSources("backArmor", item->effectSources()); 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 { Json ArmorWearer::diskStore() const {
JsonObject res; JsonObject res;
if (m_headItem) if (m_headItem)

View File

@ -30,6 +30,8 @@ public:
void effects(EffectEmitter& effectEmitter); void effects(EffectEmitter& effectEmitter);
List<PersistentStatusEffect> statusEffects() const; List<PersistentStatusEffect> statusEffects() const;
void reset();
Json diskStore() const; Json diskStore() const;
void diskLoad(Json const& diskStore); void diskLoad(Json const& diskStore);

View File

@ -245,6 +245,7 @@ void Player::diskLoad(Json const& diskStore) {
m_genericProperties = diskStore.getObject("genericProperties"); m_genericProperties = diskStore.getObject("genericProperties");
m_armor->reset();
refreshArmor(); refreshArmor();
m_codexes->learnInitialCodexes(species()); m_codexes->learnInitialCodexes(species());
@ -2162,10 +2163,14 @@ void Player::addChatMessage(String const& message) {
m_pendingChatActions.append(SayChatAction{entityId(), message, mouthPosition()}); m_pendingChatActions.append(SayChatAction{entityId(), message, mouthPosition()});
} }
void Player::addEmote(HumanoidEmote const& emote) { void Player::addEmote(HumanoidEmote const& emote, Maybe<float> emoteCooldown) {
starAssert(!isSlave()); starAssert(!isSlave());
m_emoteState = emote; m_emoteState = emote;
m_emoteCooldownTimer = m_emoteCooldown; m_emoteCooldownTimer = emoteCooldown.value(m_emoteCooldown);
}
pair<HumanoidEmote, float> Player::currentEmote() const {
return make_pair(m_emoteState, m_emoteCooldownTimer);
} }
List<ChatAction> Player::pullPendingChatActions() { List<ChatAction> Player::pullPendingChatActions() {

View File

@ -383,7 +383,8 @@ public:
void queueItemPickupMessage(ItemPtr const& item); void queueItemPickupMessage(ItemPtr const& item);
void addChatMessage(String const& message); void addChatMessage(String const& message);
void addEmote(HumanoidEmote const& emote); void addEmote(HumanoidEmote const& emote, Maybe<float> emoteCooldown = {});
pair<HumanoidEmote, float> currentEmote() const;
List<ChatAction> pullPendingChatActions() override; List<ChatAction> pullPendingChatActions() override;

View File

@ -574,7 +574,7 @@ bool UniverseClient::switchPlayer(String const& name) {
if (auto uuid = m_playerStorage->playerUuidByName(name, mainPlayer()->uuid())) if (auto uuid = m_playerStorage->playerUuidByName(name, mainPlayer()->uuid()))
return switchPlayer(*uuid); return switchPlayer(*uuid);
else else
return false; return switchPlayer(Uuid(name));
} }
UniverseClient::ReloadPlayerCallback& UniverseClient::playerReloadPreCallback() { UniverseClient::ReloadPlayerCallback& UniverseClient::playerReloadPreCallback() {

View File

@ -17,6 +17,16 @@ namespace Star {
LuaCallbacks LuaBindings::makePlayerCallbacks(Player* player) { LuaCallbacks LuaBindings::makePlayerCallbacks(Player* player) {
LuaCallbacks callbacks; 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( "humanoidIdentity", [player]() { return player->humanoid()->identity().toJson(); });
callbacks.registerCallback("setHumanoidIdentity", [player](Json const& id) { player->setIdentity(HumanoidIdentity(id)); }); 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( "name", [player]() { return player->name(); });
callbacks.registerCallback("setName", [player](String const& name) { player->setName(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( "interactRadius", [player]() { return player->interactRadius(); });
callbacks.registerCallback("setInteractRadius", [player](float radius) { player->setInteractRadius(radius); }); 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<LuaValue> {
if (auto barLocation = player->inventory()->selectedActionBarLocation()) {
if (auto index = barLocation.ptr<CustomBarIndex>())
return engine.luaFrom<CustomBarIndex>(*index + 1);
else
return engine.luaFrom<String>(EssentialItemNames.getRight(barLocation.get<EssentialItem>()));
}
else {
return {};
}
});
callbacks.registerCallback("setSelectedActionBarSlot", [player](MVariant<int, String> const& slot) {
auto inventory = player->inventory();
if (!slot)
inventory->selectActionBarLocation(SelectedActionBarLocation());
else if (auto index = slot.ptr<int>()) {
CustomBarIndex wrapped = (*index - 1) % (unsigned)inventory->customBarIndexes();
inventory->selectActionBarLocation(SelectedActionBarLocation(wrapped));
} else {
EssentialItem const& item = EssentialItemNames.getLeft(slot.get<String>());
inventory->selectActionBarLocation(SelectedActionBarLocation(item));
}
});
callbacks.registerCallback("setDamageTeam", [player](String const& typeName, Maybe<uint16_t> 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<float> 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("aimPosition", [player]() { return player->aimPosition(); });
callbacks.registerCallback("id", [player]() { return player->entityId(); }); callbacks.registerCallback("id", [player]() { return player->entityId(); });