Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions source/world/entity/EntityCategories.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
#include "EntityCategories.hpp"

const EntityCategories::CategoriesMask EntityCategories::maskEnums[] =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call this all

{
ENTITY,
MOB,
PATHFINDER_MOB,
UNKNOWN,
MONSTER,
ANIMAL,
WATER_ANIMAL,
TAMABLE_ANIMAL,
AMBIENT,
UNDEAD_MOB,
ZOMBIE_MONSTER,
ARTHROPOD,
MINECART,
SKELETON_MONSTER,
EQUINE_ANIMAL,
PROJECTILE,
ABSTRACT_ARROW,
VILLAGER_BASE
};
const int EntityCategories::maskEnumCount = sizeof(EntityCategories::maskEnums) / sizeof(const EntityCategories::CategoriesMask);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sizeof(const EntityCategories::CategoriesMask) doesn't need the const.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this an unsigned int.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call this allCount


EntityCategories::EntityCategories(CategoriesMask categoriesMask)
{
m_categoriesMask = categoriesMask;
Expand Down
2 changes: 2 additions & 0 deletions source/world/entity/EntityCategories.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class EntityCategories
ABSTRACT_ARROW = 1<<15 | PROJECTILE,
VILLAGER_BASE = 1<<16 | PATHFINDER_MOB
};
static const CategoriesMask maskEnums[];
static const int maskEnumCount;

public:
EntityCategories(CategoriesMask = ENTITY);
Expand Down
22 changes: 6 additions & 16 deletions source/world/entity/MobCategory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
MobCategory MobCategory::monster = MobCategory(EntityCategories(EntityCategories::MONSTER), 10, 20, nullptr, false);
MobCategory MobCategory::creature = MobCategory(EntityCategories(EntityCategories::ANIMAL), 10, 15, nullptr, true);
MobCategory MobCategory::waterCreature = MobCategory(EntityCategories(EntityCategories::WATER_ANIMAL), 5, 10, nullptr, true);
const MobCategory MobCategory::values[] = {
MobCategory::monster,
MobCategory::creature,

const MobCategory* MobCategory::all[] = {
&MobCategory::monster,
&MobCategory::creature,
//MobCategory::waterCreature
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add ampersand within comment.

};
const int MobCategory::numValues = sizeof(MobCategory::values) / sizeof(MobCategory);

const int MobCategory::allCount = sizeof(MobCategory::all) / sizeof(const MobCategory*);

MobCategory::MobCategory(const EntityCategories& baseType, int unknown, int max, const Material* material, bool friendly)
: m_baseType(baseType)
Expand All @@ -27,15 +29,3 @@ void MobCategory::initMobCategories()
MobCategory::creature.m_pSpawnPositionMaterial = Material::air;
MobCategory::waterCreature.m_pSpawnPositionMaterial = Material::water;
}

MobCategory& MobCategory::GetCategoryByIndex(MobCategory::ID i)
{
switch (i)
{
case MobCategory::CREATURE:
return MobCategory::creature;
case MobCategory::MONSTER:
default:
return MobCategory::monster;
}
}
13 changes: 2 additions & 11 deletions source/world/entity/MobCategory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,11 @@ class Material;
class MobCategory
{
public:
enum ID {
MONSTER,
CREATURE,
// WATER_CREATURE
};


static MobCategory monster;
static MobCategory creature;
static MobCategory waterCreature;
static const MobCategory values[];
static const int numValues;
static const MobCategory* all[];
static const int allCount;

private:
MobCategory(const EntityCategories&, int, int, const Material*, bool);
Expand All @@ -31,8 +24,6 @@ class MobCategory
const Material* getSpawnPositionMaterial() const { return m_pSpawnPositionMaterial; }
bool isFriendly() const { return m_bIsFriendly; }

// custom addition
static MobCategory& GetCategoryByIndex(MobCategory::ID i);

private:
const EntityCategories m_baseType;
Expand Down
11 changes: 7 additions & 4 deletions source/world/entity/MobFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ Mob* MobFactory::CreateMob(EntityType::ID entityType, Level *level)
}
}

void MobFactory::initMobLists() {
void MobFactory::initMobLists()
{
// format: ID, spawnrate

monsterList.insert(std::make_pair(EntityType::SPIDER, 10));
Expand All @@ -59,9 +60,11 @@ void MobFactory::initMobLists() {
waterCreatureList.insert(std::make_pair(EntityType::SQUID, 10));
}

const std::map<EntityType::ID, int>& MobFactory::GetMobListOfCategory(EntityCategories::CategoriesMask category) {
return category == EntityCategories::MONSTER ? monsterList :
category == EntityCategories::ANIMAL ? creatureList :
const std::map<EntityType::ID, int>& MobFactory::GetMobListOfCategory(const EntityCategories& category)
{
EntityCategories::CategoriesMask mask = category.getCategoryMask();
return mask == EntityCategories::MONSTER ? monsterList :
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be better as a map or a switch statement, just a suggestion.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to be fetching the mask manually, you can just compare the EntitityCategories to a mask directly.

mask == EntityCategories::ANIMAL ? creatureList :
nullCreatureList;
}

Expand Down
2 changes: 1 addition & 1 deletion source/world/entity/MobFactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ class MobFactory
public:
static void initMobLists();
static Mob* CreateMob(EntityType::ID entityType, Level *level);
static const std::map<EntityType::ID, int>& GetMobListOfCategory(EntityCategories::CategoriesMask category);
static const std::map<EntityType::ID, int>& GetMobListOfCategory(const EntityCategories& category);
};
20 changes: 10 additions & 10 deletions source/world/entity/MobSpawner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void MobSpawner::tick(Level& level, bool allowHostile, bool allowFriendly)

for (std::vector<Player*>::const_iterator it = level.m_players.begin(); it != level.m_players.end(); ++it)
{
Player* player = *it;
const Player* player = *it;
int cx = Mth::floor(player->m_pos.x / 16.0f);
int cz = Mth::floor(player->m_pos.z / 16.0f);

Expand All @@ -29,10 +29,10 @@ void MobSpawner::tick(Level& level, bool allowHostile, bool allowFriendly)

int totalSpawned = 0;

for (int i = 0; i < MobCategory::numValues; i++)
for (int i = 0; i < MobCategory::allCount; i++)
{
MobCategory& category = MobCategory::GetCategoryByIndex((MobCategory::ID)i);
const EntityCategories::CategoriesMask& mask = category.getBaseType().getCategoryMask();
const MobCategory& category = *MobCategory::all[i];
const EntityCategories& baseType = category.getBaseType();
bool isFriendly = category.isFriendly();

// good mobs don't spawn after dark, otherwise they will crowd around torches like beta
Expand All @@ -42,18 +42,18 @@ void MobSpawner::tick(Level& level, bool allowHostile, bool allowFriendly)
if ((isFriendly && !allowFriendly) || (!isFriendly && !allowHostile))
continue;

if (level.countInstanceOfType(mask) <= (category.getMaxInstancesPerChunk() * (int)chunksToPoll.size() / 256))
if (level.getEntityCount(baseType) <= (category.getMaxInstancesPerChunk() * (int)chunksToPoll.size() / 256))
{
for (std::set<ChunkPos>::iterator it = chunksToPoll.begin(); it != chunksToPoll.end(); ++it)
{
const ChunkPos& pos = *it;

const std::map<EntityType::ID, int>& spawnList = MobFactory::GetMobListOfCategory(mask);
const std::map<EntityType::ID, int>& spawnList = MobFactory::GetMobListOfCategory(baseType);

if (spawnList.empty())
continue;

EntityType::ID type = spawnList.begin()->first;
EntityType::ID entityID = spawnList.begin()->first;

int spawnWeight = 1; // make sure it starts with 1 so arithmetic exception doesn't occur

Expand All @@ -69,7 +69,7 @@ void MobSpawner::tick(Level& level, bool allowHostile, bool allowFriendly)
randomRate -= it->second;
if (randomRate < 0)
{
type = it->first;
entityID = it->first;
break;
}
}
Expand Down Expand Up @@ -104,7 +104,7 @@ void MobSpawner::tick(Level& level, bool allowHostile, bool allowFriendly)

if (dPos.lengthSqr() >= 576.0f)
{
Mob* entity = MobFactory::CreateMob(type, &level);
Mob* entity = MobFactory::CreateMob(entityID, &level);
if (!entity)
break;

Expand Down Expand Up @@ -160,7 +160,7 @@ int MobSpawner::AddMob(Level& level, Mob *mob, const Vec3& pos, const Vec2& rot)
return 1;
}

bool MobSpawner::IsSpawnPositionOk(MobCategory& category, Level& level, const TilePos& pos)
bool MobSpawner::IsSpawnPositionOk(const MobCategory& category, Level& level, const TilePos& pos)
{
if (category.getSpawnPositionMaterial() == Material::water)
return level.getMaterial(pos)->isLiquid() && !level.isSolidTile(pos.above());
Expand Down
2 changes: 1 addition & 1 deletion source/world/entity/MobSpawner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class MobSpawner {
public:
static bool IsSpawnPositionOk(MobCategory& category, Level& level, const TilePos& pos);
static bool IsSpawnPositionOk(const MobCategory& category, Level& level, const TilePos& pos);
static void FinalizeMobSettings(Mob* mob, Level& level, const Vec3& pos);
static void MakeBabyMob(Mob* mob, Level& level);
static void PostProcessSpawnMobs(Level& level, Biome& biome, const Vec3& pos);
Expand Down
19 changes: 15 additions & 4 deletions source/world/level/Level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -772,8 +772,14 @@ void Level::setTilesDirty(const TilePos& min, const TilePos& max)

void Level::entityAdded(Entity* pEnt)
{
// save for a bit
EntityCategories::CategoriesMask mask = pEnt->getDescriptor().getCategories().getCategoryMask();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't bother fetching the CategoriesMask directly, just compare the EntityCategories to a CategoriesMask.

m_entityTypeCounts[mask]++;
for (int i = 0; i < EntityCategories::maskEnumCount; i++ )
{
EntityCategories::CategoriesMask category = EntityCategories::maskEnums[i];
if ((mask & category) == category)
m_entityCountsByCategory[category]++;
}

for (std::vector<LevelListener*>::iterator it = m_levelListeners.begin(); it != m_levelListeners.end(); it++)
{
Expand All @@ -786,7 +792,12 @@ void Level::entityAdded(Entity* pEnt)
void Level::entityRemoved(Entity* pEnt)
{
EntityCategories::CategoriesMask mask = pEnt->getDescriptor().getCategories().getCategoryMask();
m_entityTypeCounts[mask]++;
for (int i = 0; i < EntityCategories::maskEnumCount; i++ )
{
EntityCategories::CategoriesMask category = EntityCategories::maskEnums[i];
if ((mask & category) == category)
m_entityCountsByCategory[category]--;
}

for (std::vector<LevelListener*>::iterator it = m_levelListeners.begin(); it != m_levelListeners.end(); it++)
{
Expand Down Expand Up @@ -1950,7 +1961,7 @@ float Level::getSunAngle(float f) const
return (float(M_PI) * getTimeOfDay(f)) * 2;
}

int Level::countInstanceOfType(EntityCategories::CategoriesMask category)
int Level::getEntityCount(const EntityCategories& category)
{
return m_entityTypeCounts[category];
return m_entityCountsByCategory[category.getCategoryMask()];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still not sure how indexing by a bitmask would allow this to work correctly.

}
4 changes: 2 additions & 2 deletions source/world/level/Level.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class Level : public LevelSource
bool hasNeighborSignal(const TilePos& pos) const;


int countInstanceOfType(EntityCategories::CategoriesMask);
int getEntityCount(const EntityCategories&);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this return an unsigned int.


#ifdef ENH_IMPROVED_SAVING
void saveUnsavedChunks();
Expand Down Expand Up @@ -236,6 +236,6 @@ class Level : public LevelSource
PathFinder* m_pPathFinder;
MobSpawner* m_pMobSpawner;

std::map<EntityCategories::CategoriesMask, int> m_entityTypeCounts;
std::map<EntityCategories::CategoriesMask, int> m_entityCountsByCategory;
};

Loading