-
Notifications
You must be signed in to change notification settings - Fork 74
Natural mob spawn #237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Natural mob spawn #237
Changes from 7 commits
60fecbc
621dfca
433f76d
427122e
abff8ea
3707404
dd60485
ea5091b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,28 @@ | ||
| #include "EntityCategories.hpp" | ||
|
|
||
| const EntityCategories::CategoriesMask EntityCategories::maskEnums[] = | ||
| { | ||
| 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); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make this an unsigned int.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Call this |
||
|
|
||
| EntityCategories::EntityCategories(CategoriesMask categoriesMask) | ||
| { | ||
| m_categoriesMask = categoriesMask; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, | ||
| MobCategory::waterCreature | ||
|
|
||
| const MobCategory* MobCategory::all[] = { | ||
| &MobCategory::monster, | ||
| &MobCategory::creature, | ||
| //MobCategory::waterCreature | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
|
@@ -26,4 +28,4 @@ void MobCategory::initMobCategories() | |
| MobCategory::monster.m_pSpawnPositionMaterial = Material::air; | ||
| MobCategory::creature.m_pSpawnPositionMaterial = Material::air; | ||
| MobCategory::waterCreature.m_pSpawnPositionMaterial = Material::water; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,13 @@ | |
|
|
||
| #define ENT(enumType, classType) case EntityType::enumType: return new classType(level); | ||
|
|
||
|
|
||
| // format: ID, spawnrate | ||
| std::map<EntityType::ID, int> monsterList; | ||
| std::map<EntityType::ID, int> creatureList; | ||
| std::map<EntityType::ID, int> waterCreatureList; | ||
| std::map<EntityType::ID, int> nullCreatureList; | ||
|
|
||
| Mob* MobFactory::CreateMob(EntityType::ID entityType, Level *level) | ||
| { | ||
| switch (entityType) | ||
|
|
@@ -35,4 +42,30 @@ Mob* MobFactory::CreateMob(EntityType::ID entityType, Level *level) | |
| } | ||
| } | ||
|
|
||
| void MobFactory::initMobLists() | ||
| { | ||
| // format: ID, spawnrate | ||
|
|
||
| monsterList.insert(std::make_pair(EntityType::SPIDER, 10)); | ||
rep-stosd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| monsterList.insert(std::make_pair(EntityType::ZOMBIE, 10)); | ||
| monsterList.insert(std::make_pair(EntityType::SKELETON, 10)); | ||
| monsterList.insert(std::make_pair(EntityType::CREEPER, 10)); | ||
| //monsterList.insert(std::make_pair(EntityType::SLIME, 10)); | ||
|
|
||
| creatureList.insert(std::make_pair(EntityType::SHEEP, 12)); | ||
| creatureList.insert(std::make_pair(EntityType::PIG, 10)); | ||
| creatureList.insert(std::make_pair(EntityType::CHICKEN, 10)); | ||
| creatureList.insert(std::make_pair(EntityType::COW, 8)); | ||
|
|
||
| waterCreatureList.insert(std::make_pair(EntityType::SQUID, 10)); | ||
| } | ||
|
|
||
| const std::map<EntityType::ID, int>& MobFactory::GetMobListOfCategory(const EntityCategories& category) | ||
| { | ||
| EntityCategories::CategoriesMask mask = category.getCategoryMask(); | ||
| return mask == EntityCategories::MONSTER ? monsterList : | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| mask == EntityCategories::ANIMAL ? creatureList : | ||
| nullCreatureList; | ||
| } | ||
|
|
||
| #undef ENT | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
|
|
||
| #include "world/entity/MobSpawner.hpp" | ||
| #include "world/entity/MobFactory.hpp" | ||
|
|
||
| #define MOB_SPAWNER_HOSTILE_BRIGHTNESS 7 | ||
| #define MOB_SPAWNER_FRIENDLY_BRIGHTNESS 9 | ||
|
|
||
| void MobSpawner::tick(Level& level, bool allowHostile, bool allowFriendly) | ||
| { | ||
| if (!allowHostile && !allowFriendly) | ||
| return; | ||
|
|
||
| chunksToPoll.clear(); | ||
|
|
||
| for (std::vector<Player*>::const_iterator it = level.m_players.begin(); it != level.m_players.end(); ++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); | ||
|
|
||
| for (int dx = -8; dx <= 8; ++dx) | ||
| { | ||
| for (int dz = -8; dz <= 8; ++dz) | ||
| { | ||
| chunksToPoll.insert(ChunkPos(cx + dx, cz + dz)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| int totalSpawned = 0; | ||
|
|
||
| for (int i = 0; i < MobCategory::allCount; i++) | ||
| { | ||
| 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 | ||
| if (!level.isDay() && isFriendly) | ||
| continue; | ||
|
|
||
| if ((isFriendly && !allowFriendly) || (!isFriendly && !allowHostile)) | ||
| continue; | ||
|
|
||
| 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(baseType); | ||
|
|
||
| if (spawnList.empty()) | ||
| continue; | ||
|
|
||
| EntityType::ID entityID = spawnList.begin()->first; | ||
|
|
||
| int spawnWeight = 1; // make sure it starts with 1 so arithmetic exception doesn't occur | ||
|
|
||
| for (std::map<EntityType::ID, int>::const_iterator it = spawnList.begin(); it != spawnList.end(); ++it) | ||
| { | ||
| spawnWeight += it->second; | ||
| } | ||
|
|
||
| int randomRate = level.m_random.nextInt(spawnWeight); | ||
|
|
||
| for (std::map<EntityType::ID, int>::const_iterator it = spawnList.begin(); it != spawnList.end(); ++it) | ||
| { | ||
| randomRate -= it->second; | ||
| if (randomRate < 0) | ||
| { | ||
| entityID = it->first; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| int idx = level.m_random.nextInt((int)spawnList.size()); | ||
| TilePos tpos = getRandomPosWithin(level, pos.x * 16, pos.z * 16); | ||
|
|
||
| if (level.isSolidTile(tpos) || level.getMaterial(tpos) != category.getSpawnPositionMaterial()) | ||
| continue; | ||
|
|
||
| int spawned = 0; | ||
| for (int i = 0; i < 3; ++i) | ||
| { | ||
| TilePos tp(tpos); | ||
|
|
||
| if (spawned == -1) | ||
| break; | ||
|
|
||
| for (int j = 0; j < 4; ++j) | ||
| { | ||
| tp.x += level.m_random.nextInt(6) - level.m_random.nextInt(6); | ||
| tp.y += level.m_random.nextInt(1) - level.m_random.nextInt(1); | ||
| tp.z += level.m_random.nextInt(6) - level.m_random.nextInt(6); | ||
|
|
||
| if (IsSpawnPositionOk(category, level, tp)) | ||
| { | ||
| Vec3 pPos(tp.x + 0.5, tp.y, tp.z + 0.5); | ||
|
|
||
| if (!level.getNearestPlayer(pPos, 24.0f, false)) | ||
| { | ||
| Vec3 dPos = pPos - level.getSharedSpawnPos(); | ||
|
|
||
| if (dPos.lengthSqr() >= 576.0f) | ||
| { | ||
| Mob* entity = MobFactory::CreateMob(entityID, &level); | ||
| if (!entity) | ||
| break; | ||
|
|
||
| entity->moveTo(pPos, Vec2(level.m_random.nextFloat() * 360.0f, 0.0f)); | ||
|
|
||
| if (entity->canSpawn()) | ||
| { | ||
| ++spawned; | ||
| level.addEntity(entity); | ||
| FinalizeMobSettings(entity, level, pPos); | ||
| if (spawned >= entity->getMaxSpawnClusterSize()) | ||
| { | ||
| totalSpawned += spawned; | ||
| spawned = -1; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (spawned != -1) | ||
| totalSpawned += spawned; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| TilePos MobSpawner::getRandomPosWithin(Level& level, int chunkX, int chunkZ) | ||
| { | ||
| int px = level.m_random.nextInt(16) + chunkX; | ||
| int py = level.m_random.nextInt(128); | ||
| int pz = level.m_random.nextInt(16) + chunkZ; | ||
| return TilePos(px, py, pz); | ||
| } | ||
|
|
||
| //todo: bool? | ||
| int MobSpawner::AddMob(Level& level, Mob *mob, const Vec3& pos, const Vec2& rot) | ||
| { | ||
| if (!mob) | ||
| return 0; | ||
|
|
||
| if (!mob->canSpawn() || !mob->isAlive()) | ||
| return 0; | ||
|
|
||
| mob->moveTo(pos, rot); | ||
| level.addEntity(mob); | ||
| FinalizeMobSettings(mob, level, pos); | ||
|
|
||
| return 1; | ||
| } | ||
|
|
||
| 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()); | ||
|
|
||
| return level.isSolidTile(pos.below()) && !level.isSolidTile(pos) && !level.getMaterial(pos)->isLiquid() && !level.isSolidTile(pos.above()); | ||
| } | ||
|
|
||
| void MobSpawner::FinalizeMobSettings(Mob *mob, Level& level, const Vec3& pos) | ||
| { | ||
| if (!mob) | ||
| return; | ||
|
|
||
| //mob->finalizeMobSpawn(); | ||
| MakeBabyMob(mob, level); | ||
| } | ||
|
|
||
|
|
||
| void MobSpawner::MakeBabyMob(Mob *mob, Level& level) | ||
| { | ||
| level.m_random.setSeed(0x5deea8f); | ||
|
|
||
| if (mob->isBaby()) | ||
| return; | ||
|
|
||
| // todo | ||
| } | ||
|
|
||
|
|
||
| void MobSpawner::PostProcessSpawnMobs(Level& level, Biome& biome, const Vec3& pos) | ||
| { | ||
| // empty (0.7.1) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| #pragma once | ||
|
|
||
| #include <set> | ||
|
|
||
| #include "world/tile/Tile.hpp" | ||
| #include "world/entity/Entity.hpp" | ||
| #include "world/entity/MobCategory.hpp" | ||
| #include "world/level/Level.hpp" | ||
| #include "world/level/levelgen/chunk/LevelChunk.hpp" | ||
| #include "world/level/levelgen/chunk/ChunkSource.hpp" | ||
| #include "world/level/storage/LevelStorageSource.hpp" | ||
| #include "world/level/storage/LevelSource.hpp" | ||
| #include "world/level/storage/LevelData.hpp" | ||
| #include "world/level/path/PathFinder.hpp" | ||
|
|
||
| class MobSpawner { | ||
| public: | ||
| 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); | ||
| static int AddMob(Level& level, Mob* mob, const Vec3& pos, const Vec2& rot = Vec2::ZERO); | ||
|
|
||
| TilePos getRandomPosWithin(Level& level, int chunkX, int chunkZ); | ||
| void tick(Level& level, bool allowHostile, bool allowFriendly); | ||
| private: | ||
|
|
||
| std::set<ChunkPos> chunksToPoll; | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Call this
all