From 35ea3d35aed8a02c0f82740de71872bbca3f5575 Mon Sep 17 00:00:00 2001 From: EmosewaMC <39972741+EmosewaMC@users.noreply.github.com> Date: Fri, 17 Jun 2022 23:53:09 -0700 Subject: [PATCH] Add pet imagination draining Address an issue where pets did not consume imagination when they were spawned. --- dGame/dComponents/InventoryComponent.cpp | 8 ++++ dGame/dComponents/PetComponent.cpp | 52 ++++++++++++++++++++++++ dGame/dComponents/PetComponent.h | 13 ++++++ 3 files changed, 73 insertions(+) diff --git a/dGame/dComponents/InventoryComponent.cpp b/dGame/dComponents/InventoryComponent.cpp index cf17b0ac..bc589e7b 100644 --- a/dGame/dComponents/InventoryComponent.cpp +++ b/dGame/dComponents/InventoryComponent.cpp @@ -1351,6 +1351,14 @@ void InventoryComponent::SpawnPet(Item* item) } } + // First check if we can summon the pet. You need 1 imagination to do so. + auto destroyableComponent = m_Parent->GetComponent(); + + if (destroyableComponent && destroyableComponent->GetImagination() <= 0) { + GameMessages::SendUseItemRequirementsResponse(m_Parent->GetObjectID(), m_Parent->GetSystemAddress(), UseItemResponse::NoImaginationForPet); + return; + } + EntityInfo info {}; info.lot = item->GetLot(); info.pos = m_Parent->GetPosition(); diff --git a/dGame/dComponents/PetComponent.cpp b/dGame/dComponents/PetComponent.cpp index a36d59e3..caefbf68 100644 --- a/dGame/dComponents/PetComponent.cpp +++ b/dGame/dComponents/PetComponent.cpp @@ -81,6 +81,20 @@ PetComponent::PetComponent(Entity* parent, uint32_t componentId) : Component(par if (!checkPreconditions.empty()) { SetPreconditions(checkPreconditions); } + // Get the imagination drain rate from the CDClient + auto query = CDClientDatabase::CreatePreppedStmt("SELECT imaginationDrainRate FROM PetComponent WHERE id = ?;"); + + query.bind(1, static_cast(componentId)); + + auto result = query.execQuery(); + + // Should a result not exist for this pet default to 60 seconds. + if (!result.eof() && !result.fieldIsNull(0)) { + imaginationDrainRate = result.getFloatField(0, 60.0f); + } else { + imaginationDrainRate = 60.0f; + } + result.finalize(); } void PetComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate, unsigned int& flags) @@ -899,6 +913,8 @@ void PetComponent::Wander() void PetComponent::Activate(Item* item, bool registerPet) { + AddDrainImaginationTimer(item); + m_ItemId = item->GetId(); m_DatabaseId = item->GetSubKey(); @@ -968,6 +984,42 @@ void PetComponent::Activate(Item* item, bool registerPet) GameMessages::SendShowPetActionButton(m_Owner, 3, true, owner->GetSystemAddress()); } +void PetComponent::AddDrainImaginationTimer(Item* item) { + auto playerInventory = item->GetInventory(); + if (!playerInventory) return; + + auto playerInventoryComponent = playerInventory->GetComponent(); + if (!playerInventoryComponent) return; + + auto playerEntity = playerInventoryComponent->GetParent(); + if (!playerEntity) return; + + auto playerDestroyableComponent = playerEntity->GetComponent(); + if (!playerDestroyableComponent) return; + + // Drain by 1 when you summon pet or when this method is called + 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(imaginationDrainRate, [playerDestroyableComponent, this, item](){ + if (!playerDestroyableComponent) { + Game::logger->Log("PetComponent", "No petComponent and/or no playerDestroyableComponent\n"); + return; + } + + // If we are out of imagination despawn the pet. + if (playerDestroyableComponent->GetImagination() == 0) { + this->Deactivate(); + auto playerEntity = playerDestroyableComponent->GetParent(); + if (!playerEntity) return; + + GameMessages::SendUseItemRequirementsResponse(playerEntity->GetObjectID(), playerEntity->GetSystemAddress(), UseItemResponse::NoImaginationForPet); + } + + this->AddDrainImaginationTimer(item); + }); +} + void PetComponent::Deactivate() { GameMessages::SendPlayFXEffect(m_Parent->GetObjectID(), -1, u"despawn", "", LWOOBJID_EMPTY, 1, 1, true); diff --git a/dGame/dComponents/PetComponent.h b/dGame/dComponents/PetComponent.h index 845cfe31..9139575a 100644 --- a/dGame/dComponents/PetComponent.h +++ b/dGame/dComponents/PetComponent.h @@ -203,6 +203,14 @@ public: */ static PetComponent* GetActivePet(LWOOBJID owner); + /** + * Adds the timer to the owner of this pet to drain imagination at the rate + * specified by the parameter imaginationDrainRate + * + * @param item The item that represents this pet in the inventory. + */ + void AddDrainImaginationTimer(Item* item); + private: /** @@ -346,4 +354,9 @@ private: * Preconditions that need to be met before an entity can tame this pet */ PreconditionExpression* m_Preconditions; + + /** + * The rate at which imagination is drained from the user for having the pet out. + */ + float imaginationDrainRate; }; \ No newline at end of file