Skip to content

Commit

Permalink
feat: implement consume item behavior (#1098)
Browse files Browse the repository at this point in the history
* feature: implement consume item behavior

* Cleanup

* tested with skill 456 and fixed some things

* remove logs
  • Loading branch information
aronwk-aaron authored Nov 15, 2023
1 parent 8a9883c commit 8cd5bf7
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 22 deletions.
5 changes: 4 additions & 1 deletion dGame/dBehaviors/Behavior.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
#include "FallSpeedBehavior.h"
#include "ChangeIdleFlagsBehavior.h"
#include "DarkInspirationBehavior.h"
#include "ConsumeItemBehavior.h"

//CDClient includes
#include "CDBehaviorParameterTable.h"
Expand Down Expand Up @@ -200,7 +201,9 @@ Behavior* Behavior::CreateBehavior(const uint32_t behaviorId) {
case BehaviorTemplates::BEHAVIOR_SKILL_EVENT:
behavior = new SkillEventBehavior(behaviorId);
break;
case BehaviorTemplates::BEHAVIOR_CONSUME_ITEM: break;
case BehaviorTemplates::BEHAVIOR_CONSUME_ITEM:
behavior = new ConsumeItemBehavior(behaviorId);
break;
case BehaviorTemplates::BEHAVIOR_SKILL_CAST_FAILED:
behavior = new SkillCastFailedBehavior(behaviorId);
break;
Expand Down
1 change: 1 addition & 0 deletions dGame/dBehaviors/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ set(DGAME_DBEHAVIORS_SOURCES "AirMovementBehavior.cpp"
"ChangeOrientationBehavior.cpp"
"ChargeUpBehavior.cpp"
"ClearTargetBehavior.cpp"
"ConsumeItemBehavior.cpp"
"DamageAbsorptionBehavior.cpp"
"DamageReductionBehavior.cpp"
"DarkInspirationBehavior.cpp"
Expand Down
31 changes: 31 additions & 0 deletions dGame/dBehaviors/ConsumeItemBehavior.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "ConsumeItemBehavior.h"
#include "BehaviorContext.h"
#include "BehaviorBranchContext.h"
#include "InventoryComponent.h"

void ConsumeItemBehavior::Handle(BehaviorContext* context, RakNet::BitStream* bitStream, BehaviorBranchContext branch) {
auto action_to_cast = m_ActionNotConsumed;
if (this->m_ConsumeLOT != -1) {
auto caster = Game::entityManager->GetEntity(context->caster);
if (!caster) return;

auto inventoryComponent = caster->GetComponent<InventoryComponent>();
if (!inventoryComponent) return;

if (inventoryComponent->RemoveItem(this->m_ConsumeLOT, this->m_NumToConsume, eInventoryType::INVALID, false, true)){
action_to_cast = m_ActionConsumed;
}
}
if(action_to_cast) action_to_cast->Handle(context, bitStream, branch);
}

void ConsumeItemBehavior::Sync(BehaviorContext* context, RakNet::BitStream* bitStream, BehaviorBranchContext branch) {
Handle(context, bitStream, branch);
}

void ConsumeItemBehavior::Load() {
this->m_ConsumeLOT = GetInt("consume_lot", -1);
this->m_NumToConsume = GetInt("num_to_consume", 1);
this->m_ActionNotConsumed = GetAction("action_not_consumed");
this->m_ActionConsumed = GetAction("action_consumed");
}
17 changes: 17 additions & 0 deletions dGame/dBehaviors/ConsumeItemBehavior.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once
#include "Behavior.h"

class ConsumeItemBehavior final : public Behavior
{
public:
explicit ConsumeItemBehavior(const uint32_t behaviorId) : Behavior(behaviorId) {}
void Handle(BehaviorContext* context, RakNet::BitStream* bitStream, BehaviorBranchContext branch) override;
void Sync(BehaviorContext* context, RakNet::BitStream* bitStream, BehaviorBranchContext branch) override;
void Load() override;

private:
LOT m_ConsumeLOT;
uint32_t m_NumToConsume;
Behavior* m_ActionNotConsumed;
Behavior* m_ActionConsumed;
};
28 changes: 8 additions & 20 deletions dGame/dComponents/InventoryComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,38 +300,26 @@ void InventoryComponent::AddItem(
}
}

void InventoryComponent::RemoveItem(const LOT lot, const uint32_t count, eInventoryType inventoryType, const bool ignoreBound) {
bool InventoryComponent::RemoveItem(const LOT lot, const uint32_t count, eInventoryType inventoryType, const bool ignoreBound, const bool silent) {
if (count == 0) {
LOG("Attempted to remove 0 of item (%i) from the inventory!", lot);

return;
}

if (inventoryType == INVALID) {
inventoryType = Inventory::FindInventoryTypeForLot(lot);
return false;
}

if (inventoryType == INVALID) inventoryType = Inventory::FindInventoryTypeForLot(lot);
auto* inventory = GetInventory(inventoryType);

if (inventory == nullptr) {
return;
}
if (!inventory) return false;

auto left = std::min<uint32_t>(count, inventory->GetLotCount(lot));
if (left != count) return false;

while (left > 0) {
auto* item = FindItemByLot(lot, inventoryType, false, ignoreBound);

if (item == nullptr) {
break;
}

if (!item) break;
const auto delta = std::min<uint32_t>(left, item->GetCount());

item->SetCount(item->GetCount() - delta);

item->SetCount(item->GetCount() - delta, silent);
left -= delta;
}
return true;
}

void InventoryComponent::MoveItemToInventory(Item* item, const eInventoryType inventory, const uint32_t count, const bool showFlyingLot, bool isModMoveAndEquip, const bool ignoreEquipped, const int32_t preferredSlot) {
Expand Down
3 changes: 2 additions & 1 deletion dGame/dComponents/InventoryComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ class InventoryComponent : public Component
* @param count the number of items to remove
* @param inventoryType optional inventory type to remove the item from
* @param ignoreBound ignores bound items
* @param silent silently remove the item
*/
void RemoveItem(LOT lot, uint32_t count, eInventoryType inventoryType = INVALID, bool ignoreBound = false);
bool RemoveItem(LOT lot, uint32_t count, eInventoryType inventoryType = INVALID, bool ignoreBound = false, bool silent = false);

/**
* Moves an existing item to an inventory of the entity
Expand Down

0 comments on commit 8cd5bf7

Please sign in to comment.