Skip to content

Commit

Permalink
Added ItemInventory to enemies and containers.
Browse files Browse the repository at this point in the history
  • Loading branch information
afritz1 committed Jan 5, 2025
1 parent 4aefabd commit 67c03a8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
21 changes: 20 additions & 1 deletion OpenTESArena/src/Entities/EntityChunkManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,15 @@ void EntityChunkManager::populateChunkEntities(EntityChunk &entityChunk, const V
DebugLogError("Couldn't allocate Jolt physics body for entity.");
}

bool hasInventory = (entityDefType == EntityDefinitionType::Enemy) || (entityDefType == EntityDefinitionType::Container);
if (hasInventory)
{
if (!this->itemInventories.tryAlloc(&entityInst.itemInventoryInstID))
{
DebugCrash("Couldn't allocate EntityItemInventoryInstanceID.");
}
}

entityChunk.entityIDs.emplace_back(entityInstID);
}
}
Expand Down Expand Up @@ -697,7 +706,7 @@ const EntityAnimationInstance &EntityChunkManager::getEntityAnimationInstance(En
return this->animInsts.get(id);
}

const int8_t &EntityChunkManager::getEntityCitizenDirectionIndex(EntityCitizenDirectionIndexID id) const
int8_t EntityChunkManager::getEntityCitizenDirectionIndex(EntityCitizenDirectionIndexID id) const
{
return this->citizenDirectionIndices.get(id);
}
Expand All @@ -707,6 +716,11 @@ const PaletteIndices &EntityChunkManager::getEntityPaletteIndices(EntityPaletteI
return this->paletteIndices.get(id);
}

ItemInventory &EntityChunkManager::getEntityItemInventory(EntityItemInventoryInstanceID id)
{
return this->itemInventories.get(id);
}

int EntityChunkManager::getCountInChunkWithDirection(const ChunkInt2 &chunkPos) const
{
int count = 0;
Expand Down Expand Up @@ -1068,6 +1082,11 @@ void EntityChunkManager::cleanUp(JPH::PhysicsSystem &physicsSystem)
this->paletteIndices.free(entityInst.paletteIndicesInstID);
}

if (entityInst.itemInventoryInstID >= 0)
{
this->itemInventories.free(entityInst.itemInventoryInstID);
}

const JPH::BodyID physicsBodyID = entityInst.physicsBodyID;
if (!physicsBodyID.IsInvalid())
{
Expand Down
7 changes: 6 additions & 1 deletion OpenTESArena/src/Entities/EntityChunkManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "EntityGeneration.h"
#include "EntityInstance.h"
#include "EntityUtils.h"
#include "../Items/ItemInventory.h"
#include "../Math/BoundingBox.h"
#include "../World/SpecializedChunkManager.h"

Expand Down Expand Up @@ -43,6 +44,7 @@ class EntityChunkManager final : public SpecializedChunkManager<EntityChunk>
using EntityCreatureSoundPool = RecyclablePool<double, EntityCreatureSoundInstanceID>;
using EntityCitizenDirectionIndexPool = RecyclablePool<int8_t, EntityCitizenDirectionIndexID>;
using EntityPaletteIndicesInstancePool = RecyclablePool<PaletteIndices, EntityPaletteIndicesInstanceID>;
using EntityItemInventoryInstancePool = RecyclablePool<ItemInventory, EntityItemInventoryInstanceID>;

EntityPool entities;
EntityPositionPool positions;
Expand All @@ -57,6 +59,8 @@ class EntityChunkManager final : public SpecializedChunkManager<EntityChunk>
// citizen textures will need to be 8-bit.
EntityPaletteIndicesInstancePool paletteIndices;

EntityItemInventoryInstancePool itemInventories;

// Entity definitions for this currently-active level. Their definition IDs CANNOT be assumed
// to be zero-based because these are in addition to ones in the entity definition library.
// @todo: separate EntityAnimationDefinition from EntityDefinition?
Expand Down Expand Up @@ -96,8 +100,9 @@ class EntityChunkManager final : public SpecializedChunkManager<EntityChunk>
const VoxelDouble2 &getEntityDirection(EntityDirectionID id) const;
EntityAnimationInstance &getEntityAnimationInstance(EntityAnimationInstanceID id);
const EntityAnimationInstance &getEntityAnimationInstance(EntityAnimationInstanceID id) const;
const int8_t &getEntityCitizenDirectionIndex(EntityCitizenDirectionIndexID id) const;
int8_t getEntityCitizenDirectionIndex(EntityCitizenDirectionIndexID id) const;
const PaletteIndices &getEntityPaletteIndices(EntityPaletteIndicesInstanceID id) const;
ItemInventory &getEntityItemInventory(EntityItemInventoryInstanceID id);

// Gets the entities scheduled for destruction this frame. If they're in this list, they should no longer be
// simulated or rendered.
Expand Down
6 changes: 6 additions & 0 deletions OpenTESArena/src/Entities/EntityInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ bool EntityInstance::isCitizen() const
return this->citizenDirectionIndexID >= 0;
}

bool EntityInstance::hasInventory() const
{
return this->itemInventoryInstID >= 0;
}

void EntityInstance::clear()
{
this->instanceID = -1;
Expand All @@ -40,5 +45,6 @@ void EntityInstance::clear()
this->creatureSoundInstID = -1;
this->citizenDirectionIndexID = -1;
this->paletteIndicesInstID = -1;
this->itemInventoryInstID = -1;
this->physicsBodyID = JPH::BodyID();
}
4 changes: 4 additions & 0 deletions OpenTESArena/src/Entities/EntityInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ using EntityAnimationInstanceID = int;
using EntityCreatureSoundInstanceID = int;
using EntityCitizenDirectionIndexID = int;
using EntityPaletteIndicesInstanceID = int;
using EntityItemInventoryInstanceID = int;

struct EntityInstance
{
Expand All @@ -26,6 +27,7 @@ struct EntityInstance
EntityCreatureSoundInstanceID creatureSoundInstID;
EntityCitizenDirectionIndexID citizenDirectionIndexID;
EntityPaletteIndicesInstanceID paletteIndicesInstID;
EntityItemInventoryInstanceID itemInventoryInstID;
JPH::BodyID physicsBodyID;

EntityInstance();
Expand All @@ -38,6 +40,8 @@ struct EntityInstance

bool isCitizen() const;

bool hasInventory() const;

void clear();
};

Expand Down

0 comments on commit 67c03a8

Please sign in to comment.