1
0

Made mob spawning code use the chunk so that it could use varying sizes of areas for different mobs.

This commit is contained in:
Samuel Barney 2013-10-28 09:49:06 -06:00
parent 6c30ce93da
commit 16bac5ace9
3 changed files with 81 additions and 54 deletions

View File

@ -519,42 +519,30 @@ void cChunk::SpawnMobs(cMobSpawner& a_MobSpawner)
ASSERT(Try_Y > 0); ASSERT(Try_Y > 0);
ASSERT(Try_Y < cChunkDef::Height-1); ASSERT(Try_Y < cChunkDef::Height-1);
BLOCKTYPE BlockType; EMCSBiome Biome = m_ChunkMap->GetBiomeAt (Try_X, Try_Z);
NIBBLETYPE BlockMeta; // MG TODO :
BLOCKTYPE BlockType_below; // Moon cycle (for slime)
NIBBLETYPE BlockMeta_below; // check player and playerspawn presence < 24 blocks
BLOCKTYPE BlockType_above; // check mobs presence on the block
NIBBLETYPE BlockMeta_above;
if (UnboundedRelGetBlock(Try_X, Try_Y , Try_Z, BlockType, BlockMeta) && // MG TODO : check that "Level" really means Y
UnboundedRelGetBlock(Try_X, Try_Y-1, Try_Z, BlockType_below, BlockMeta_below)&&
UnboundedRelGetBlock(Try_X, Try_Y+1, Try_Z, BlockType_above, BlockMeta_above) NIBBLETYPE SkyLight = 0;
)
NIBBLETYPE BlockLight = 0;
if (IsLightValid())
{ {
EMCSBiome Biome = m_ChunkMap->GetBiomeAt (Try_X, Try_Z); cEntity* newMob = a_MobSpawner.TryToSpawnHere(this, Try_X, Try_Y, Try_Z, Biome, MaxNbOfSuccess);
// MG TODO : if (newMob)
// Moon cycle (for slime)
// check player and playerspawn presence < 24 blocks
// check mobs presence on the block
// MG TODO : check that "Level" really means Y
NIBBLETYPE SkyLight = 0;
NIBBLETYPE BlockLight = 0;
if (IsLightValid() && (UnboundedRelGetBlockBlockLight(Try_X, Try_Y, Try_Z, BlockLight)) && (UnboundedRelGetBlockSkyLight(Try_X, Try_Y, Try_Z, SkyLight)))
{ {
cEntity* newMob = a_MobSpawner.TryToSpawnHere(BlockType, BlockMeta, BlockType_below, BlockMeta_below, BlockType_above, BlockMeta_above, SkyLight, BlockLight, Biome, Try_Y, MaxNbOfSuccess); int WorldX, WorldY, WorldZ;
if (newMob) PositionToWorldPosition(Try_X, Try_Y, Try_Z, WorldX, WorldY, WorldZ);
{ double ActualX = WorldX + 0.5;
int WorldX, WorldY, WorldZ; double ActualZ = WorldZ + 0.5;
PositionToWorldPosition(Try_X, Try_Y, Try_Z, WorldX, WorldY, WorldZ); newMob->SetPosition(ActualX, WorldY, ActualZ);
double ActualX = WorldX + 0.5; LOGD("Spawning %s #%i at %d,%d,%d",newMob->GetClass(),newMob->GetUniqueID(),WorldX, WorldY, WorldZ);
double ActualZ = WorldZ + 0.5; NumberOfSuccess++;
newMob->SetPosition(ActualX, WorldY, ActualZ);
LOGD("Spawning %s #%i at %d,%d,%d",newMob->GetClass(),newMob->GetUniqueID(),WorldX, WorldY, WorldZ);
NumberOfSuccess++;
}
} }
} }

View File

@ -124,57 +124,95 @@ cMonster::eType cMobSpawner::ChooseMobType(EMCSBiome a_Biome)
bool cMobSpawner::CanSpawnHere(cMonster::eType a_MobType, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, BLOCKTYPE a_BlockType_below, NIBBLETYPE a_BlockMeta_below, BLOCKTYPE a_BlockType_above, NIBBLETYPE a_BlockMeta_above, NIBBLETYPE a_Skylight, NIBBLETYPE a_Blocklight, EMCSBiome a_Biome, int a_Level) bool cMobSpawner::CanSpawnHere(const cChunk * a_Chunk, int a_RelX, int a_RelY, int a_RelZ, cMonster::eType a_MobType, EMCSBiome a_Biome)
{ {
if (m_AllowedTypes.find(a_MobType) != m_AllowedTypes.end()) BLOCKTYPE TargetBlock;
BLOCKTYPE BlockAbove;
BLOCKTYPE BlockBelow;
NIBBLETYPE BlockLight;
NIBBLETYPE SkyLight;
if (m_AllowedTypes.find(a_MobType) != m_AllowedTypes.end() && a_Chunk->UnboundedRelGetBlockType(a_RelX, a_RelY, a_RelZ, TargetBlock))
{ {
a_Chunk->UnboundedRelGetBlockBlockLight(a_RelX, a_RelY, a_RelZ, BlockLight);
a_Chunk->UnboundedRelGetBlockSkyLight(a_RelX, a_RelY, a_RelZ, SkyLight);
a_Chunk->UnboundedRelGetBlockType(a_RelX, a_RelY + 1, a_RelZ, BlockAbove);
a_Chunk->UnboundedRelGetBlockType(a_RelX, a_RelY - 1, a_RelZ, BlockBelow);
switch(a_MobType) switch(a_MobType)
{ {
case cMonster::mtSquid: case cMonster::mtSquid:
return IsBlockWater(a_BlockType) && (a_Level >= 45) && (a_Level <= 62); return IsBlockWater(TargetBlock) && (a_RelY >= 45) && (a_RelY <= 62);
case cMonster::mtBat: case cMonster::mtBat:
return a_Level <= 63 && (a_Skylight == 0) && (a_Blocklight <= 4) && (a_BlockType == E_BLOCK_AIR) && (!g_BlockTransparent[a_BlockType_above]); return (a_RelY <= 63) && (BlockLight <= 4) && (SkyLight <= 4) && (TargetBlock == E_BLOCK_AIR) && (!g_BlockTransparent[BlockAbove]);
case cMonster::mtChicken: case cMonster::mtChicken:
case cMonster::mtCow: case cMonster::mtCow:
case cMonster::mtPig: case cMonster::mtPig:
case cMonster::mtHorse: case cMonster::mtHorse:
case cMonster::mtSheep: case cMonster::mtSheep:
return (a_BlockType == E_BLOCK_AIR) && (a_BlockType_above == E_BLOCK_AIR) && (!g_BlockTransparent[a_BlockType_below]) && {
(a_BlockType_below == E_BLOCK_GRASS) && (a_Skylight >= 9); return (TargetBlock == E_BLOCK_AIR) && (BlockAbove == E_BLOCK_AIR) && (!g_BlockTransparent[BlockBelow]) &&
(BlockBelow == E_BLOCK_GRASS) && (SkyLight >= 9);
}
case cMonster::mtOcelot: case cMonster::mtOcelot:
return (a_BlockType == E_BLOCK_AIR) && (a_BlockType_above == E_BLOCK_AIR) && {
((a_BlockType_below == E_BLOCK_GRASS) || (a_BlockType_below == E_BLOCK_LEAVES)) && (a_Level >= 62) && (m_Random.NextInt(3,a_Biome) != 0); return (TargetBlock == E_BLOCK_AIR) && (BlockAbove == E_BLOCK_AIR) &&
((BlockBelow == E_BLOCK_GRASS) || (BlockBelow == E_BLOCK_LEAVES)) && (a_RelY >= 62) && (m_Random.NextInt(3,a_Biome) != 0);
}
case cMonster::mtEnderman: case cMonster::mtEnderman:
{
if (a_RelY < 250)
{
BLOCKTYPE BlockTop;
a_Chunk->UnboundedRelGetBlockType(a_RelX, a_RelY + 2, a_RelZ, BlockTop);
return (TargetBlock == E_BLOCK_AIR) && (BlockAbove == E_BLOCK_AIR) && (BlockTop == E_BLOCK_AIR) && (!g_BlockTransparent[BlockBelow]) &&
(SkyLight <= 7) && (BlockLight <= 7);
}
break;
}
case cMonster::mtSpider: case cMonster::mtSpider:
return false; {
bool CanSpawn = true;
for (int x = -1; x < 2; ++x)
{
for(int z = -1; z < 2; ++x)
{
CanSpawn = a_Chunk->UnboundedRelGetBlockType(a_RelX + x, a_RelY, a_RelZ + z, TargetBlock);
CanSpawn = CanSpawn && (TargetBlock == E_BLOCK_AIR);
if (!CanSpawn)
return false;
}
}
return CanSpawn && (!g_BlockTransparent[BlockBelow]) && (SkyLight <= 7) && (BlockLight <= 7);
}
case cMonster::mtCreeper: case cMonster::mtCreeper:
case cMonster::mtZombie: case cMonster::mtZombie:
return (a_BlockType == E_BLOCK_AIR) && (a_BlockType_above == E_BLOCK_AIR) && (!g_BlockTransparent[a_BlockType_below]) && return (TargetBlock == E_BLOCK_AIR) && (BlockAbove == E_BLOCK_AIR) && (!g_BlockTransparent[BlockBelow]) &&
(a_Skylight <= 7) && (a_Blocklight <= 7) && (m_Random.NextInt(2,a_Biome) == 0); (SkyLight <= 7) && (BlockLight <= 7) && (m_Random.NextInt(2,a_Biome) == 0);
case cMonster::mtSlime: case cMonster::mtSlime:
return (a_BlockType == E_BLOCK_AIR) && (a_BlockType_above == E_BLOCK_AIR) && (!g_BlockTransparent[a_BlockType_below]) && return (TargetBlock == E_BLOCK_AIR) && (BlockAbove == E_BLOCK_AIR) && (!g_BlockTransparent[BlockBelow]) &&
(a_Level <= 40); (a_RelY <= 40);
case cMonster::mtGhast: case cMonster::mtGhast:
case cMonster::mtZombiePigman: case cMonster::mtZombiePigman:
return (a_BlockType == E_BLOCK_AIR) && (a_BlockType_above == E_BLOCK_AIR) && (!g_BlockTransparent[a_BlockType_below]) && return (TargetBlock == E_BLOCK_AIR) && (BlockAbove == E_BLOCK_AIR) && (!g_BlockTransparent[BlockBelow]) &&
(m_Random.NextInt(20,a_Biome) == 0); (m_Random.NextInt(20,a_Biome) == 0);
default: default:
LOGD("MG TODO : check I've got a Rule to write for type %d",a_MobType); LOGD("MG TODO : check I've got a Rule to write for type %d",a_MobType);
return false; return false;
} }
} }
return false;
} }
cMonster* cMobSpawner::TryToSpawnHere(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, BLOCKTYPE a_BlockType_below, NIBBLETYPE a_BlockMeta_below, BLOCKTYPE a_BlockType_above, NIBBLETYPE a_BlockMeta_above, NIBBLETYPE a_Skylight, NIBBLETYPE a_Blocklight, EMCSBiome a_Biome, int a_Level, int& a_MaxPackSize) cMonster* cMobSpawner::TryToSpawnHere(const cChunk * a_Chunk, int a_RelX, int a_RelY, int a_RelZ, EMCSBiome a_Biome, int& a_MaxPackSize)
{ {
cMonster* toReturn = NULL; cMonster* toReturn = NULL;
if (m_NewPack) if (m_NewPack)
@ -196,7 +234,7 @@ cMonster* cMobSpawner::TryToSpawnHere(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockM
} }
if (CanSpawnHere(m_MobType, a_BlockType, a_BlockMeta, a_BlockType_below, a_BlockMeta_below, a_BlockType_above, a_BlockMeta_above, a_Skylight, a_Blocklight, a_Biome, a_Level)) if (CanSpawnHere(a_Chunk, a_RelX, a_RelY, a_RelZ, m_MobType, a_Biome))
{ {
cMonster * newMob = cMonster::NewMonsterFromType(m_MobType); cMonster * newMob = cMonster::NewMonsterFromType(m_MobType);
if (newMob) if (newMob)

View File

@ -4,6 +4,7 @@
#include <set> #include <set>
#include "BlockID.h" #include "BlockID.h"
#include "ChunkDef.h" #include "ChunkDef.h"
#include "Chunk.h"
#include "FastRandom.h" #include "FastRandom.h"
#include "Mobs/Monster.h" //this is a side-effect of keeping Mobfamily inside Monster class. I'd prefer to keep both (Mobfamily and Monster) inside a "Monster" namespace MG TODO : do it #include "Mobs/Monster.h" //this is a side-effect of keeping Mobfamily inside Monster class. I'd prefer to keep both (Mobfamily and Monster) inside a "Monster" namespace MG TODO : do it
@ -38,7 +39,7 @@ public :
// if this is the first of a Pack : determine the type of monster // if this is the first of a Pack : determine the type of monster
// BlockType & BlockMeta are used to decide what kind of Mob can Spawn here // BlockType & BlockMeta are used to decide what kind of Mob can Spawn here
// MaxPackSize is set to the maximal size for a pack this type of mob // MaxPackSize is set to the maximal size for a pack this type of mob
cMonster * TryToSpawnHere(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, BLOCKTYPE a_BlockType_below, NIBBLETYPE a_BlockMeta_below, BLOCKTYPE a_BlockType_above, NIBBLETYPE a_BlockMeta_above, NIBBLETYPE a_Skylight, NIBBLETYPE a_Blocklight, EMCSBiome a_Biome, int a_Level, int& a_MaxPackSize); cMonster * TryToSpawnHere(const cChunk * a_Chunk, int A_RelX, int a_RelY, int a_RelZ, EMCSBiome a_Biome, int& a_MaxPackSize);
// mark the beginning of a new Pack // mark the beginning of a new Pack
// all mobs of the same Pack are the same type // all mobs of the same Pack are the same type
@ -52,7 +53,7 @@ public :
protected : protected :
// return true if specified type of mob can spawn on specified block // return true if specified type of mob can spawn on specified block
bool CanSpawnHere(cMonster::eType a_MobType, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, BLOCKTYPE a_BlockType_below, NIBBLETYPE a_BlockMeta_below, BLOCKTYPE a_BlockType_above, NIBBLETYPE a_BlockMeta_above, NIBBLETYPE a_Skylight, NIBBLETYPE a_Blocklight, EMCSBiome a_Biome, int a_Level); bool CanSpawnHere(const cChunk * a_Chunk, int a_RelX, int a_RelY, int a_RelZ, cMonster::eType a_MobType, EMCSBiome a_Biome);
// return a random type that can spawn on specified biome. // return a random type that can spawn on specified biome.
// returns E_ENTITY_TYPE_DONOTUSE if none is possible // returns E_ENTITY_TYPE_DONOTUSE if none is possible