From d2eb58f27780a3c65fedd0d21d152ee8866ebb86 Mon Sep 17 00:00:00 2001 From: mgueydan Date: Sat, 7 Sep 2013 22:19:56 +0200 Subject: Adding mob census (sorry this is a big commit as work was done before git integration i couldn't split it more) --- source/Chunk.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'source/Chunk.cpp') diff --git a/source/Chunk.cpp b/source/Chunk.cpp index db533f642..59a65a537 100644 --- a/source/Chunk.cpp +++ b/source/Chunk.cpp @@ -30,6 +30,8 @@ #include "PluginManager.h" #include "Blocks/BlockHandler.h" #include "Simulator/FluidSimulator.h" +#include "MobCensus.h" + #include @@ -429,6 +431,43 @@ void cChunk::Stay(bool a_Stay) +void cChunk::CollectMobCensus(cMobCensus& toFill) +{ + toFill.CollectSpawnableChunck(*this); + std::list playerPositions; + cPlayer* currentPlayer; + for (cClientHandleList::iterator itr = m_LoadedByClient.begin(), end = m_LoadedByClient.end(); itr != end; ++itr) + { + currentPlayer = (*itr)->GetPlayer(); + playerPositions.push_back(&(currentPlayer->GetPosition())); + } + + Vector3d currentPosition; + for (cEntityList::iterator itr = m_Entities.begin(); itr != m_Entities.end(); ++itr) + { + //LOGD("Counting entity #%i (%s)", (*itr)->GetUniqueID(), (*itr)->GetClass()); + if ((*itr)->IsMob()) + { + try + { + cMonster& Monster = (cMonster&)(**itr); + currentPosition = Monster.GetPosition(); + for (std::list::const_iterator itr2 = playerPositions.begin(); itr2 != playerPositions.end(); itr2 ++) + { + toFill.CollectMob(Monster,*this,(currentPosition-**itr2).SqrLength()); + } + } + catch (std::bad_cast& e) + { + LOGD("Something wrong happend I'm collecting an entity that respond 'true' to IsMob() but are not castable in cMonster - No Action"); + } + } + } // for itr - m_Entitites[] +} + + + + void cChunk::Tick(float a_Dt) { -- cgit v1.2.3 From bf1fb0aa3dbd5b6b793d920c4e301e4782fe0f0f Mon Sep 17 00:00:00 2001 From: mgueydan Date: Sun, 8 Sep 2013 00:11:38 +0200 Subject: Adding an Empty shell that would launch mob spawner - not called yet --- source/Chunk.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'source/Chunk.cpp') diff --git a/source/Chunk.cpp b/source/Chunk.cpp index 59a65a537..35e44363e 100644 --- a/source/Chunk.cpp +++ b/source/Chunk.cpp @@ -31,6 +31,7 @@ #include "Blocks/BlockHandler.h" #include "Simulator/FluidSimulator.h" #include "MobCensus.h" +#include "MobSpawner.h" #include @@ -468,6 +469,42 @@ void cChunk::CollectMobCensus(cMobCensus& toFill) +void cChunk::getThreeRandomNumber(int& a_X, int& a_Y, int& a_Z,int a_MaxX, int a_MaxY, int a_MaxZ) +{ + assert(a_MaxX * a_MaxY * a_MaxZ * 8 < 0x00ffffff); + int Random = m_World->GetTickRandomNumber(0x00ffffff); + a_X = Random % (a_MaxX * 2); + a_Y = (Random / (a_MaxX * 2)) % (a_MaxY * 2); + a_Z = ((Random / (a_MaxX * 2)) / (a_MaxY * 2)) % (a_MaxZ * 2); + a_X /= 2; + a_Y /= 2; + a_Z /= 2; +} + + + + + +void cChunk::getRandomBlock(int& a_X, int& a_Y, int& a_Z) +{ + // MG TODO : check if this kind of optimization (only one random call) is still needed + // MG TODO : if so propagate it + + getThreeRandomNumber(a_X, a_Y, a_Z, Width, Height-2, Width); + a_Y++; +} + + + + + +void cChunk::SpawnMobs(cMobSpawner& a_MobSpawner) +{ +} + + + + void cChunk::Tick(float a_Dt) { -- cgit v1.2.3 From 04151677d57905e35993eac4899e6bd72831ec6f Mon Sep 17 00:00:00 2001 From: mgueydan Date: Sun, 8 Sep 2013 01:43:55 +0200 Subject: Disabeling current mob spawning and tick --- source/Chunk.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'source/Chunk.cpp') diff --git a/source/Chunk.cpp b/source/Chunk.cpp index 35e44363e..1d649f3f6 100644 --- a/source/Chunk.cpp +++ b/source/Chunk.cpp @@ -533,10 +533,14 @@ void cChunk::Tick(float a_Dt) m_IsDirty = (*itr)->Tick(a_Dt, *this) | m_IsDirty; } - // Tick all entities in this chunk: + // Tick all entities in this chunk (except mobs): for (cEntityList::iterator itr = m_Entities.begin(); itr != m_Entities.end(); ++itr) { - (*itr)->Tick(a_Dt, *this); + // Mobs are tickes inside MobTick (as we don't have to tick them if they are far away from players) + if (!((*itr)->IsMob())) + { + (*itr)->Tick(a_Dt, *this); + } } // for itr - m_Entitites[] // Remove all entities that were scheduled for removal: -- cgit v1.2.3 From 7a5e3592ffcc80f8c768ed2391b0ad521c59b67e Mon Sep 17 00:00:00 2001 From: mgueydan Date: Sun, 8 Sep 2013 02:47:02 +0200 Subject: Adding glue to call everything done in last commits - now the mobs are spawning --- source/Chunk.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'source/Chunk.cpp') diff --git a/source/Chunk.cpp b/source/Chunk.cpp index 1d649f3f6..8c4c77250 100644 --- a/source/Chunk.cpp +++ b/source/Chunk.cpp @@ -500,6 +500,67 @@ void cChunk::getRandomBlock(int& a_X, int& a_Y, int& a_Z) void cChunk::SpawnMobs(cMobSpawner& a_MobSpawner) { + int Center_X,Center_Y,Center_Z; + getRandomBlock(Center_X,Center_Y,Center_Z); + + BLOCKTYPE PackCenterBlock = GetBlock(Center_X, Center_Y, Center_Z); + if (a_MobSpawner.CheckPackCenter(PackCenterBlock)) + { + a_MobSpawner.NewPack(); + int NumberOfTries = 0; + int NumberOfSuccess = 0; + int MaxNbOfSuccess = 4; // this can be changed during the process for Wolves and Ghass + while (NumberOfTries < 12 && NumberOfSuccess < MaxNbOfSuccess) + { + const int HorizontalRange = 20; // MG TODO : relocate + const int VerticalRange = 0; // MG TODO : relocate + int Try_X, Try_Y, Try_Z; + getThreeRandomNumber(Try_X, Try_Y, Try_Z, 2*HorizontalRange+1 , 2*VerticalRange+1 , 2*HorizontalRange+1); + Try_X -= HorizontalRange; + Try_Y -= VerticalRange; + Try_Z -= HorizontalRange; + Try_X += Center_X; + Try_Y += Center_Y; + Try_Z += Center_Z; + + assert(Try_Y > 0); + assert(Try_Y < cChunkDef::Height-1); + + BLOCKTYPE BlockType; + NIBBLETYPE BlockMeta; + BLOCKTYPE BlockType_below; + NIBBLETYPE BlockMeta_below; + BLOCKTYPE BlockType_above; + NIBBLETYPE BlockMeta_above; + if (UnboundedRelGetBlock(Try_X, Try_Y , Try_Z, BlockType, BlockMeta) && + UnboundedRelGetBlock(Try_X, Try_Y-1, Try_Z, BlockType_below, BlockMeta_below)&& + UnboundedRelGetBlock(Try_X, Try_Y+1, Try_Z, BlockType_above, BlockMeta_above) + ) + { + EMCSBiome Biome = m_ChunkMap->GetBiomeAt (Try_X, Try_Z); + // MG TODO : + // Moon cycle (for slime) + // check player and playerspawn presence < 24 blocks + // check mobs presence on the block + + // MG TODO: fix the "light" thing, I'm pretty sure that UnboundedRelGetBlock s not returning the right thing + + // MG TODO : check that "Level" really means Y + cEntity* newMob = a_MobSpawner.TryToSpawnHere(BlockType, BlockMeta, BlockType_below, BlockMeta_below, BlockType_above, BlockMeta_above, Biome, Try_Y, MaxNbOfSuccess); + if (newMob) + { + int WorldX, WorldY, WorldZ; + PositionToWorldPosition(Try_X, Try_Y, Try_Z, WorldX, WorldY, WorldZ); + newMob->SetPosition(WorldX, WorldY, WorldZ); + LOGD("Spawning %s #%i at %d,%d,%d",newMob->GetClass(),newMob->GetUniqueID(),WorldX, WorldY, WorldZ); + NumberOfSuccess++; + } + } + + NumberOfTries++; + } + } + } -- cgit v1.2.3 From 668b6edaa7390f87b0b7e7e28ddb49c9d3f66d2a Mon Sep 17 00:00:00 2001 From: mgueydan Date: Sun, 8 Sep 2013 11:48:48 +0200 Subject: renaming the cChunk::getRandomBlock method + removing a buggy working log --- source/Chunk.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source/Chunk.cpp') diff --git a/source/Chunk.cpp b/source/Chunk.cpp index 8c4c77250..a77f10609 100644 --- a/source/Chunk.cpp +++ b/source/Chunk.cpp @@ -485,7 +485,7 @@ void cChunk::getThreeRandomNumber(int& a_X, int& a_Y, int& a_Z,int a_MaxX, int a -void cChunk::getRandomBlock(int& a_X, int& a_Y, int& a_Z) +void cChunk::getRandomBlockCoords(int& a_X, int& a_Y, int& a_Z) { // MG TODO : check if this kind of optimization (only one random call) is still needed // MG TODO : if so propagate it @@ -501,7 +501,7 @@ void cChunk::getRandomBlock(int& a_X, int& a_Y, int& a_Z) void cChunk::SpawnMobs(cMobSpawner& a_MobSpawner) { int Center_X,Center_Y,Center_Z; - getRandomBlock(Center_X,Center_Y,Center_Z); + getRandomBlockCoords(Center_X,Center_Y,Center_Z); BLOCKTYPE PackCenterBlock = GetBlock(Center_X, Center_Y, Center_Z); if (a_MobSpawner.CheckPackCenter(PackCenterBlock)) -- cgit v1.2.3 From 5846be9400d26d198ddc3884a8e02c405748c6ad Mon Sep 17 00:00:00 2001 From: mgueydan Date: Sun, 8 Sep 2013 12:20:19 +0200 Subject: replacing asserts by ASSERTs --- source/Chunk.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'source/Chunk.cpp') diff --git a/source/Chunk.cpp b/source/Chunk.cpp index a77f10609..01cb13c4d 100644 --- a/source/Chunk.cpp +++ b/source/Chunk.cpp @@ -471,7 +471,7 @@ void cChunk::CollectMobCensus(cMobCensus& toFill) void cChunk::getThreeRandomNumber(int& a_X, int& a_Y, int& a_Z,int a_MaxX, int a_MaxY, int a_MaxZ) { - assert(a_MaxX * a_MaxY * a_MaxZ * 8 < 0x00ffffff); + ASSERT(a_MaxX * a_MaxY * a_MaxZ * 8 < 0x00ffffff); int Random = m_World->GetTickRandomNumber(0x00ffffff); a_X = Random % (a_MaxX * 2); a_Y = (Random / (a_MaxX * 2)) % (a_MaxY * 2); @@ -523,8 +523,8 @@ void cChunk::SpawnMobs(cMobSpawner& a_MobSpawner) Try_Y += Center_Y; Try_Z += Center_Z; - assert(Try_Y > 0); - assert(Try_Y < cChunkDef::Height-1); + ASSERT(Try_Y > 0); + ASSERT(Try_Y < cChunkDef::Height-1); BLOCKTYPE BlockType; NIBBLETYPE BlockMeta; -- cgit v1.2.3 From b4bb2553445d44dc8ca03bb33d801cf620f71898 Mon Sep 17 00:00:00 2001 From: mgueydan Date: Sun, 8 Sep 2013 12:25:07 +0200 Subject: Replacing chunCk by chunk --- source/Chunk.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source/Chunk.cpp') diff --git a/source/Chunk.cpp b/source/Chunk.cpp index 01cb13c4d..0d6ce335d 100644 --- a/source/Chunk.cpp +++ b/source/Chunk.cpp @@ -434,7 +434,7 @@ void cChunk::Stay(bool a_Stay) void cChunk::CollectMobCensus(cMobCensus& toFill) { - toFill.CollectSpawnableChunck(*this); + toFill.CollectSpawnableChunk(*this); std::list playerPositions; cPlayer* currentPlayer; for (cClientHandleList::iterator itr = m_LoadedByClient.begin(), end = m_LoadedByClient.end(); itr != end; ++itr) -- cgit v1.2.3 From ead953898d06faa75c6f5cffae6dd284c19db83a Mon Sep 17 00:00:00 2001 From: mgueydan Date: Sun, 8 Sep 2013 12:37:14 +0200 Subject: replacing C-style cast by dynamic_cast --- source/Chunk.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source/Chunk.cpp') diff --git a/source/Chunk.cpp b/source/Chunk.cpp index 0d6ce335d..00010e806 100644 --- a/source/Chunk.cpp +++ b/source/Chunk.cpp @@ -451,7 +451,7 @@ void cChunk::CollectMobCensus(cMobCensus& toFill) { try { - cMonster& Monster = (cMonster&)(**itr); + cMonster& Monster = dynamic_cast(**itr); currentPosition = Monster.GetPosition(); for (std::list::const_iterator itr2 = playerPositions.begin(); itr2 != playerPositions.end(); itr2 ++) { -- cgit v1.2.3 From 59f46353091ef3a453193ea3d74dd712bb0acf38 Mon Sep 17 00:00:00 2001 From: mgueydan Date: Mon, 9 Sep 2013 18:45:39 +0200 Subject: replacing dynamic_cast by c-style cast --- source/Chunk.cpp | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) (limited to 'source/Chunk.cpp') diff --git a/source/Chunk.cpp b/source/Chunk.cpp index 00010e806..21401163b 100644 --- a/source/Chunk.cpp +++ b/source/Chunk.cpp @@ -449,18 +449,11 @@ void cChunk::CollectMobCensus(cMobCensus& toFill) //LOGD("Counting entity #%i (%s)", (*itr)->GetUniqueID(), (*itr)->GetClass()); if ((*itr)->IsMob()) { - try - { - cMonster& Monster = dynamic_cast(**itr); - currentPosition = Monster.GetPosition(); - for (std::list::const_iterator itr2 = playerPositions.begin(); itr2 != playerPositions.end(); itr2 ++) - { - toFill.CollectMob(Monster,*this,(currentPosition-**itr2).SqrLength()); - } - } - catch (std::bad_cast& e) + cMonster& Monster = (cMonster&)(**itr); + currentPosition = Monster.GetPosition(); + for (std::list::const_iterator itr2 = playerPositions.begin(); itr2 != playerPositions.end(); itr2 ++) { - LOGD("Something wrong happend I'm collecting an entity that respond 'true' to IsMob() but are not castable in cMonster - No Action"); + toFill.CollectMob(Monster,*this,(currentPosition-**itr2).SqrLength()); } } } // for itr - m_Entitites[] -- cgit v1.2.3