Add pet imagination draining

Address an issue where pets did not consume imagination when they were spawned.
This commit is contained in:
EmosewaMC 2022-06-17 23:53:09 -07:00
parent e28b084395
commit 35ea3d35ae
3 changed files with 73 additions and 0 deletions

View File

@ -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<DestroyableComponent>();
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();

View File

@ -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<int>(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<DestroyableComponent>();
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);

View File

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