-
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
Open
rep-stosd
wants to merge
8
commits into
ReMinecraftPE:master
Choose a base branch
from
rep-stosd:branch_mobspawn
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Natural mob spawn #237
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
60fecbc
c++03 compliant natural mob spawner
621dfca
mob spawner patch (without crafting)
433f76d
make brent happy
427122e
oops
abff8ea
make brent happy
3707404
seems ok
dd60485
rename
ea5091b
Merge branch 'master' into branch_mobspawn
BrentDaMage File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| { | ||
| Player* player = *it; | ||
rep-stosd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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::numValues; i++) | ||
rep-stosd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| MobCategory& category = MobCategory::GetCategoryByIndex((MobCategory::ID)i); | ||
| const EntityCategories::CategoriesMask& mask = category.getBaseType().getCategoryMask(); | ||
| 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.countInstanceOfType(mask) <= (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); | ||
|
|
||
| if (spawnList.empty()) | ||
| continue; | ||
|
|
||
| EntityType::ID type = 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) | ||
| { | ||
| type = 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(type, &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(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) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(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; | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Add ampersand within comment.