2017-09-02 03:45:06 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <cstdlib>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "Chunk.h"
|
2020-04-03 02:57:01 -04:00
|
|
|
#include "BlockInfo.h"
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "World.h"
|
|
|
|
#include "ClientHandle.h"
|
|
|
|
#include "Server.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
#include "Defines.h"
|
2013-08-19 05:39:13 -04:00
|
|
|
#include "Entities/Pickup.h"
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "Item.h"
|
2014-11-18 06:07:08 -05:00
|
|
|
#include "Noise/Noise.h"
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "Root.h"
|
2013-08-19 05:39:13 -04:00
|
|
|
#include "Entities/Player.h"
|
2012-07-09 03:00:28 -04:00
|
|
|
#include "BlockArea.h"
|
2013-12-08 06:17:54 -05:00
|
|
|
#include "Bindings/PluginManager.h"
|
2012-09-29 09:59:32 -04:00
|
|
|
#include "Blocks/BlockHandler.h"
|
2013-03-02 10:44:31 -05:00
|
|
|
#include "Simulator/FluidSimulator.h"
|
2013-09-07 16:19:56 -04:00
|
|
|
#include "MobCensus.h"
|
2013-09-07 18:11:38 -04:00
|
|
|
#include "MobSpawner.h"
|
2014-02-02 09:49:37 -05:00
|
|
|
#include "BlockInServerPluginInterface.h"
|
2014-07-24 12:32:05 -04:00
|
|
|
#include "SetChunkData.h"
|
2014-09-03 11:00:26 -04:00
|
|
|
#include "BoundingBox.h"
|
2014-09-26 13:13:19 -04:00
|
|
|
#include "Blocks/ChunkInterface.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-11-27 03:17:25 -05:00
|
|
|
#include "json/json.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-07-02 15:22:05 -04:00
|
|
|
|
2014-07-17 16:15:34 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2012-06-14 09:06:06 -04:00
|
|
|
// cChunk:
|
|
|
|
|
2012-12-14 17:38:30 -05:00
|
|
|
cChunk::cChunk(
|
2014-08-28 05:36:35 -04:00
|
|
|
int a_ChunkX, int a_ChunkZ,
|
2021-03-05 08:03:55 -05:00
|
|
|
cChunkMap * a_ChunkMap, cWorld * a_World
|
2019-10-11 05:02:53 -04:00
|
|
|
):
|
2014-09-05 16:16:48 -04:00
|
|
|
m_Presence(cpInvalid),
|
2013-12-20 13:10:07 -05:00
|
|
|
m_IsLightValid(false),
|
|
|
|
m_IsDirty(false),
|
|
|
|
m_IsSaving(false),
|
|
|
|
m_StayCount(0),
|
|
|
|
m_PosX(a_ChunkX),
|
|
|
|
m_PosZ(a_ChunkZ),
|
|
|
|
m_World(a_World),
|
|
|
|
m_ChunkMap(a_ChunkMap),
|
|
|
|
m_WaterSimulatorData(a_World->GetWaterSimulator()->CreateChunkData()),
|
2014-07-10 12:18:32 -04:00
|
|
|
m_LavaSimulatorData (a_World->GetLavaSimulator ()->CreateChunkData()),
|
2014-09-28 12:57:01 -04:00
|
|
|
m_RedstoneSimulatorData(a_World->GetRedstoneSimulator()->CreateChunkData()),
|
2014-07-10 12:18:32 -04:00
|
|
|
m_AlwaysTicked(0)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2020-08-20 07:50:22 -04:00
|
|
|
m_NeighborXM = a_ChunkMap->FindChunk(a_ChunkX - 1, a_ChunkZ);
|
|
|
|
m_NeighborXP = a_ChunkMap->FindChunk(a_ChunkX + 1, a_ChunkZ);
|
|
|
|
m_NeighborZM = a_ChunkMap->FindChunk(a_ChunkX, a_ChunkZ - 1);
|
|
|
|
m_NeighborZP = a_ChunkMap->FindChunk(a_ChunkX, a_ChunkZ + 1);
|
|
|
|
|
|
|
|
if (m_NeighborXM != nullptr)
|
2012-12-14 17:38:30 -05:00
|
|
|
{
|
2020-08-20 07:50:22 -04:00
|
|
|
m_NeighborXM->m_NeighborXP = this;
|
2012-12-14 17:38:30 -05:00
|
|
|
}
|
2020-08-20 07:50:22 -04:00
|
|
|
if (m_NeighborXP != nullptr)
|
2012-12-14 17:38:30 -05:00
|
|
|
{
|
2020-08-20 07:50:22 -04:00
|
|
|
m_NeighborXP->m_NeighborXM = this;
|
2012-12-14 17:38:30 -05:00
|
|
|
}
|
2020-08-20 07:50:22 -04:00
|
|
|
if (m_NeighborZM != nullptr)
|
2012-12-14 17:38:30 -05:00
|
|
|
{
|
2020-08-20 07:50:22 -04:00
|
|
|
m_NeighborZM->m_NeighborZP = this;
|
2012-12-14 17:38:30 -05:00
|
|
|
}
|
2020-08-20 07:50:22 -04:00
|
|
|
if (m_NeighborZP != nullptr)
|
2012-12-14 17:38:30 -05:00
|
|
|
{
|
2020-08-20 07:50:22 -04:00
|
|
|
m_NeighborZP->m_NeighborZM = this;
|
2012-12-14 17:38:30 -05:00
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cChunk::~cChunk()
|
|
|
|
{
|
2014-07-21 09:19:48 -04:00
|
|
|
// LOGINFO("### delete cChunk() (%i, %i) from %p, thread 0x%x ###", m_PosX, m_PosZ, this, GetCurrentThreadId());
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2020-09-22 16:21:47 -04:00
|
|
|
// Inform our neighbours that we're no longer valid:
|
2014-10-20 16:55:07 -04:00
|
|
|
if (m_NeighborXM != nullptr)
|
2012-12-14 17:38:30 -05:00
|
|
|
{
|
2014-10-20 16:55:07 -04:00
|
|
|
m_NeighborXM->m_NeighborXP = nullptr;
|
2012-12-14 17:38:30 -05:00
|
|
|
}
|
2014-10-20 16:55:07 -04:00
|
|
|
if (m_NeighborXP != nullptr)
|
2012-12-14 17:38:30 -05:00
|
|
|
{
|
2014-10-20 16:55:07 -04:00
|
|
|
m_NeighborXP->m_NeighborXM = nullptr;
|
2012-12-14 17:38:30 -05:00
|
|
|
}
|
2014-10-20 16:55:07 -04:00
|
|
|
if (m_NeighborZM != nullptr)
|
2012-12-14 17:38:30 -05:00
|
|
|
{
|
2014-10-20 16:55:07 -04:00
|
|
|
m_NeighborZM->m_NeighborZP = nullptr;
|
2012-12-14 17:38:30 -05:00
|
|
|
}
|
2014-10-20 16:55:07 -04:00
|
|
|
if (m_NeighborZP != nullptr)
|
2012-12-14 17:38:30 -05:00
|
|
|
{
|
2014-10-20 16:55:07 -04:00
|
|
|
m_NeighborZP->m_NeighborZM = nullptr;
|
2012-12-14 17:38:30 -05:00
|
|
|
}
|
2020-09-22 16:21:47 -04:00
|
|
|
|
2013-03-02 10:44:31 -05:00
|
|
|
delete m_WaterSimulatorData;
|
2014-10-20 16:55:07 -04:00
|
|
|
m_WaterSimulatorData = nullptr;
|
2013-03-02 10:44:31 -05:00
|
|
|
delete m_LavaSimulatorData;
|
2014-10-20 16:55:07 -04:00
|
|
|
m_LavaSimulatorData = nullptr;
|
2014-09-26 12:00:34 -04:00
|
|
|
delete m_RedstoneSimulatorData;
|
2014-10-20 16:55:07 -04:00
|
|
|
m_RedstoneSimulatorData = nullptr;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-05 16:16:48 -04:00
|
|
|
void cChunk::SetPresence(cChunk::ePresence a_Presence)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-09-05 16:16:48 -04:00
|
|
|
m_Presence = a_Presence;
|
|
|
|
if (a_Presence == cpPresent)
|
|
|
|
{
|
|
|
|
m_World->GetChunkMap()->ChunkValidated();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
void cChunk::MarkRegenerating(void)
|
|
|
|
{
|
2014-09-05 16:16:48 -04:00
|
|
|
// Set as queued again:
|
|
|
|
SetPresence(cpQueued);
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// Tell all clients attached to this chunk that they want this chunk:
|
2015-09-16 12:04:05 -04:00
|
|
|
for (auto ClientHandle : m_LoadedByClient)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2015-09-16 12:04:05 -04:00
|
|
|
ClientHandle->AddWantedChunk(m_PosX, m_PosZ);
|
2012-06-14 09:06:06 -04:00
|
|
|
} // for itr - m_LoadedByClient[]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-08-20 07:50:22 -04:00
|
|
|
bool cChunk::HasPlayerEntities() const
|
2020-03-18 13:17:59 -04:00
|
|
|
{
|
2020-08-20 07:50:22 -04:00
|
|
|
return std::any_of(
|
|
|
|
m_Entities.begin(), m_Entities.end(),
|
|
|
|
[](const auto & Entity)
|
2020-03-18 13:17:59 -04:00
|
|
|
{
|
|
|
|
return Entity->IsPlayer();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-08-20 07:50:22 -04:00
|
|
|
bool cChunk::CanUnload(void) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-09-05 16:16:48 -04:00
|
|
|
return
|
|
|
|
m_LoadedByClient.empty() && // The chunk is not used by any client
|
2020-03-18 13:17:59 -04:00
|
|
|
!HasPlayerEntities() && // Ensure not only the absence of ClientHandlers, but also of cPlayer objects
|
2014-09-05 16:16:48 -04:00
|
|
|
!m_IsDirty && // The chunk has been saved properly or hasn't been touched since the load / gen
|
|
|
|
(m_StayCount == 0) && // The chunk is not in a ChunkStay
|
|
|
|
(m_Presence != cpQueued) ; // The chunk is not queued for loading / generating (otherwise multi-load / multi-gen could occur)
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-08-20 07:50:22 -04:00
|
|
|
bool cChunk::CanUnloadAfterSaving(void) const
|
2016-09-03 11:38:29 -04:00
|
|
|
{
|
|
|
|
return
|
|
|
|
m_LoadedByClient.empty() && // The chunk is not used by any client
|
2020-03-18 13:17:59 -04:00
|
|
|
!HasPlayerEntities() && // Ensure not only the absence of ClientHandlers, but also of cPlayer objects
|
2016-09-03 11:38:29 -04:00
|
|
|
m_IsDirty && // The chunk is dirty
|
|
|
|
(m_StayCount == 0) && // The chunk is not in a ChunkStay
|
|
|
|
(m_Presence != cpQueued) ; // The chunk is not queued for loading / generating (otherwise multi-load / multi-gen could occur)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-09-22 16:21:47 -04:00
|
|
|
void cChunk::OnUnload()
|
|
|
|
{
|
|
|
|
// Note: this is only called during normal operation, not during shutdown
|
|
|
|
|
|
|
|
// Notify all entities of imminent unload:
|
|
|
|
for (auto & Entity : m_Entities)
|
|
|
|
{
|
|
|
|
// Chunks cannot be unloaded when they still contain players:
|
|
|
|
ASSERT(!Entity->IsPlayer());
|
|
|
|
|
|
|
|
// Notify the entity:
|
|
|
|
Entity->OnRemoveFromWorld(*Entity->GetWorld());
|
|
|
|
}
|
2021-01-02 08:50:34 -05:00
|
|
|
|
|
|
|
// Notify all block entities of imminent unload:
|
|
|
|
for (auto & BlockEntity : m_BlockEntities)
|
|
|
|
{
|
|
|
|
BlockEntity.second->OnRemoveFromWorld();
|
|
|
|
}
|
2020-09-22 16:21:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
void cChunk::MarkSaving(void)
|
|
|
|
{
|
|
|
|
m_IsSaving = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cChunk::MarkSaved(void)
|
|
|
|
{
|
|
|
|
if (!m_IsSaving)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_IsDirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cChunk::MarkLoaded(void)
|
|
|
|
{
|
|
|
|
m_IsDirty = false;
|
2014-09-05 16:16:48 -04:00
|
|
|
SetPresence(cpPresent);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cChunk::MarkLoadFailed(void)
|
|
|
|
{
|
2014-09-05 16:16:48 -04:00
|
|
|
ASSERT(m_Presence == cpQueued);
|
|
|
|
|
2020-12-24 10:27:44 -05:00
|
|
|
// Mark dirty before generating, so that we get saved and don't have to later generate again:
|
|
|
|
MarkDirty();
|
|
|
|
|
2020-08-28 16:26:04 -04:00
|
|
|
// The chunk is always needed, generate it:
|
2021-03-05 08:03:55 -05:00
|
|
|
m_World->GetGenerator().QueueGenerateChunk({ m_PosX, m_PosZ }, false);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-12-21 09:31:44 -05:00
|
|
|
void cChunk::GetAllData(cChunkDataCallback & a_Callback) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-09-05 16:16:48 -04:00
|
|
|
ASSERT(m_Presence == cpPresent);
|
|
|
|
|
2014-05-21 14:58:48 -04:00
|
|
|
a_Callback.LightIsValid(m_IsLightValid);
|
2021-03-05 08:03:55 -05:00
|
|
|
a_Callback.ChunkData(m_BlockData, m_LightData);
|
|
|
|
a_Callback.HeightMap(m_HeightMap);
|
|
|
|
a_Callback.BiomeMap(m_BiomeMap);
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2016-12-19 15:12:23 -05:00
|
|
|
for (const auto & Entity : m_Entities)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2016-12-19 15:12:23 -05:00
|
|
|
a_Callback.Entity(Entity.get());
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2017-05-22 16:27:55 -04:00
|
|
|
for (auto & KeyPair : m_BlockEntities)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2020-04-03 17:23:38 -04:00
|
|
|
a_Callback.BlockEntity(KeyPair.second.get());
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-05 08:03:55 -05:00
|
|
|
void cChunk::SetAllData(SetChunkData && a_SetChunkData)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2021-03-05 08:03:55 -05:00
|
|
|
std::copy(a_SetChunkData.HeightMap, a_SetChunkData.HeightMap + std::size(a_SetChunkData.HeightMap), m_HeightMap);
|
|
|
|
std::copy(a_SetChunkData.BiomeMap, a_SetChunkData.BiomeMap + std::size(a_SetChunkData.BiomeMap), m_BiomeMap);
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2021-03-05 08:03:55 -05:00
|
|
|
m_BlockData = std::move(a_SetChunkData.BlockData);
|
|
|
|
m_LightData = std::move(a_SetChunkData.LightData);
|
|
|
|
m_IsLightValid = a_SetChunkData.IsLightValid;
|
2014-04-06 18:30:21 -04:00
|
|
|
|
2020-12-24 10:27:44 -05:00
|
|
|
// Entities need some extra steps to destroy, so here we're keeping the old ones.
|
|
|
|
// Move the entities already in the chunk, including player entities, so that we don't lose any:
|
2021-03-05 08:03:55 -05:00
|
|
|
a_SetChunkData.Entities.insert(
|
|
|
|
a_SetChunkData.Entities.end(),
|
2020-12-24 10:27:44 -05:00
|
|
|
std::make_move_iterator(m_Entities.begin()),
|
|
|
|
std::make_move_iterator(m_Entities.end())
|
|
|
|
);
|
|
|
|
|
|
|
|
// Store the augmented result:
|
2021-03-05 08:03:55 -05:00
|
|
|
m_Entities = std::move(a_SetChunkData.Entities);
|
2020-12-24 10:27:44 -05:00
|
|
|
|
|
|
|
// Set all the entity variables again:
|
|
|
|
for (const auto & Entity : m_Entities)
|
|
|
|
{
|
|
|
|
Entity->SetWorld(m_World);
|
|
|
|
Entity->SetParentChunk(this);
|
|
|
|
Entity->SetIsTicking(true);
|
|
|
|
}
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// Clear the block entities present - either the loader / saver has better, or we'll create empty ones:
|
2021-03-05 08:03:55 -05:00
|
|
|
m_BlockEntities = std::move(a_SetChunkData.BlockEntities);
|
2014-08-29 12:19:27 -04:00
|
|
|
|
|
|
|
// Check that all block entities have a valid blocktype at their respective coords (DEBUG-mode only):
|
2021-01-26 04:41:55 -05:00
|
|
|
#ifndef NDEBUG
|
2017-05-22 16:27:55 -04:00
|
|
|
for (auto & KeyPair : m_BlockEntities)
|
2014-08-29 12:19:27 -04:00
|
|
|
{
|
2020-04-03 17:23:38 -04:00
|
|
|
cBlockEntity * Block = KeyPair.second.get();
|
2017-05-22 16:27:55 -04:00
|
|
|
BLOCKTYPE EntityBlockType = Block->GetBlockType();
|
|
|
|
BLOCKTYPE WorldBlockType = GetBlock(Block->GetRelX(), Block->GetPosY(), Block->GetRelZ());
|
2014-10-18 14:54:34 -04:00
|
|
|
ASSERT(WorldBlockType == EntityBlockType);
|
2017-05-22 16:27:55 -04:00
|
|
|
} // for KeyPair - m_BlockEntities
|
2021-01-26 04:41:55 -05:00
|
|
|
#endif // !NDEBUG
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2013-04-06 17:21:57 -04:00
|
|
|
// Set all block entities' World variable:
|
2017-05-22 16:27:55 -04:00
|
|
|
for (auto & KeyPair : m_BlockEntities)
|
2013-04-06 17:21:57 -04:00
|
|
|
{
|
2017-05-22 16:27:55 -04:00
|
|
|
KeyPair.second->SetWorld(m_World);
|
2013-04-06 17:21:57 -04:00
|
|
|
}
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2013-03-14 16:02:52 -04:00
|
|
|
// Set the chunk data as valid. This may be needed for some simulators that perform actions upon block adding (Vaporize)
|
2014-09-05 16:16:48 -04:00
|
|
|
SetPresence(cpPresent);
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2013-03-03 10:33:55 -05:00
|
|
|
// Wake up all simulators for their respective blocks:
|
|
|
|
WakeUpSimulators();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cChunk::SetLight(
|
|
|
|
const cChunkDef::BlockNibbles & a_BlockLight,
|
|
|
|
const cChunkDef::BlockNibbles & a_SkyLight
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// TODO: We might get cases of wrong lighting when a chunk changes in the middle of a lighting calculation.
|
|
|
|
// Postponing until we see how bad it is :)
|
2014-04-06 18:30:21 -04:00
|
|
|
|
2021-03-05 08:03:55 -05:00
|
|
|
m_LightData.SetAll(a_BlockLight, a_SkyLight);
|
2014-04-06 18:30:21 -04:00
|
|
|
|
2020-12-24 10:28:22 -05:00
|
|
|
MarkDirty();
|
2012-06-14 09:06:06 -04:00
|
|
|
m_IsLightValid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-06 12:58:31 -04:00
|
|
|
void cChunk::WriteBlockArea(cBlockArea & a_Area, int a_MinBlockX, int a_MinBlockY, int a_MinBlockZ, int a_DataTypes)
|
|
|
|
{
|
|
|
|
if ((a_DataTypes & (cBlockArea::baTypes | cBlockArea::baMetas)) != (cBlockArea::baTypes | cBlockArea::baMetas))
|
|
|
|
{
|
2017-06-24 05:58:06 -04:00
|
|
|
LOGWARNING("cChunk::WriteBlockArea(): unsupported datatype request, can write only types + metas together (0x%x), requested 0x%x. Ignoring.",
|
2012-10-06 12:58:31 -04:00
|
|
|
(cBlockArea::baTypes | cBlockArea::baMetas), a_DataTypes & (cBlockArea::baTypes | cBlockArea::baMetas)
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2012-10-06 12:58:31 -04:00
|
|
|
// SizeX, SizeZ are the dimensions of the block data to copy to the chunk (size of the geometric union)
|
|
|
|
|
|
|
|
int BlockStartX = std::max(a_MinBlockX, m_PosX * cChunkDef::Width);
|
|
|
|
int BlockEndX = std::min(a_MinBlockX + a_Area.GetSizeX(), (m_PosX + 1) * cChunkDef::Width);
|
|
|
|
int BlockStartZ = std::max(a_MinBlockZ, m_PosZ * cChunkDef::Width);
|
|
|
|
int BlockEndZ = std::min(a_MinBlockZ + a_Area.GetSizeZ(), (m_PosZ + 1) * cChunkDef::Width);
|
2017-06-24 05:58:06 -04:00
|
|
|
int SizeX = BlockEndX - BlockStartX; // Size of the union
|
2012-10-06 12:58:31 -04:00
|
|
|
int SizeZ = BlockEndZ - BlockStartZ;
|
2017-06-24 05:58:06 -04:00
|
|
|
int SizeY = std::min(a_Area.GetSizeY(), cChunkDef::Height - a_MinBlockY);
|
|
|
|
int OffX = BlockStartX - m_PosX * cChunkDef::Width; // Offset within the chunk where the union starts
|
2012-10-06 12:58:31 -04:00
|
|
|
int OffZ = BlockStartZ - m_PosZ * cChunkDef::Width;
|
2017-06-24 05:58:06 -04:00
|
|
|
int BaseX = BlockStartX - a_MinBlockX; // Offset within the area where the union starts
|
2012-10-06 12:58:31 -04:00
|
|
|
int BaseZ = BlockStartZ - a_MinBlockZ;
|
|
|
|
|
2017-06-24 05:58:06 -04:00
|
|
|
// Copy blocktype and blockmeta:
|
2012-10-06 12:58:31 -04:00
|
|
|
BLOCKTYPE * AreaBlockTypes = a_Area.GetBlockTypes();
|
|
|
|
NIBBLETYPE * AreaBlockMetas = a_Area.GetBlockMetas();
|
|
|
|
for (int y = 0; y < SizeY; y++)
|
|
|
|
{
|
|
|
|
int ChunkY = a_MinBlockY + y;
|
|
|
|
int AreaY = y;
|
|
|
|
for (int z = 0; z < SizeZ; z++)
|
|
|
|
{
|
|
|
|
int ChunkZ = OffZ + z;
|
|
|
|
int AreaZ = BaseZ + z;
|
|
|
|
for (int x = 0; x < SizeX; x++)
|
|
|
|
{
|
|
|
|
int ChunkX = OffX + x;
|
|
|
|
int AreaX = BaseX + x;
|
2017-09-07 04:40:58 -04:00
|
|
|
auto idx = a_Area.MakeIndex(AreaX, AreaY, AreaZ);
|
2012-10-06 12:58:31 -04:00
|
|
|
BLOCKTYPE BlockType = AreaBlockTypes[idx];
|
|
|
|
NIBBLETYPE BlockMeta = AreaBlockMetas[idx];
|
|
|
|
FastSetBlock(ChunkX, ChunkY, ChunkZ, BlockType, BlockMeta);
|
|
|
|
} // for x
|
|
|
|
} // for z
|
|
|
|
} // for y
|
2017-06-24 05:58:06 -04:00
|
|
|
|
|
|
|
// Erase all affected block entities:
|
2017-09-27 17:22:15 -04:00
|
|
|
cCuboid affectedArea( // In world coordinates
|
|
|
|
{BlockStartX, a_MinBlockY, BlockStartZ},
|
|
|
|
{BlockEndX, a_MinBlockY + SizeY - 1, BlockEndZ}
|
2017-08-17 09:48:38 -04:00
|
|
|
);
|
2017-06-24 05:58:06 -04:00
|
|
|
for (auto itr = m_BlockEntities.begin(); itr != m_BlockEntities.end();)
|
|
|
|
{
|
|
|
|
if (affectedArea.IsInside(itr->second->GetPos()))
|
|
|
|
{
|
2020-09-12 14:57:44 -04:00
|
|
|
itr->second->Destroy();
|
2021-01-02 08:50:34 -05:00
|
|
|
itr->second->OnRemoveFromWorld();
|
2017-06-24 05:58:06 -04:00
|
|
|
itr = m_BlockEntities.erase(itr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clone block entities from a_Area into this chunk:
|
|
|
|
if ((a_DataTypes & cBlockArea::baBlockEntities) != 0)
|
|
|
|
{
|
|
|
|
for (const auto & keyPair: a_Area.GetBlockEntities())
|
|
|
|
{
|
|
|
|
auto & be = keyPair.second;
|
|
|
|
auto posX = be->GetPosX() + a_MinBlockX;
|
|
|
|
auto posY = be->GetPosY() + a_MinBlockY;
|
|
|
|
auto posZ = be->GetPosZ() + a_MinBlockZ;
|
|
|
|
if (
|
|
|
|
(posX < m_PosX * cChunkDef::Width) || (posX >= m_PosX * cChunkDef::Width + cChunkDef::Width) ||
|
|
|
|
(posZ < m_PosZ * cChunkDef::Width) || (posZ >= m_PosZ * cChunkDef::Width + cChunkDef::Width)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// This block entity is inside the chunk, clone it (and remove any that is there currently):
|
2021-01-18 11:09:10 -05:00
|
|
|
auto idx = static_cast<size_t>(cChunkDef::MakeIndex(posX - m_PosX * cChunkDef::Width, posY, posZ - m_PosZ * cChunkDef::Width));
|
2017-06-24 05:58:06 -04:00
|
|
|
auto itr = m_BlockEntities.find(idx);
|
|
|
|
if (itr != m_BlockEntities.end())
|
|
|
|
{
|
|
|
|
m_BlockEntities.erase(itr);
|
|
|
|
}
|
2019-09-29 08:59:24 -04:00
|
|
|
auto clone = be->Clone({posX, posY, posZ});
|
2017-06-24 05:58:06 -04:00
|
|
|
clone->SetWorld(m_World);
|
2020-08-28 16:40:40 -04:00
|
|
|
AddBlockEntity(std::move(clone));
|
2017-06-24 05:58:06 -04:00
|
|
|
}
|
|
|
|
}
|
2012-10-06 12:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-07-23 14:12:51 -04:00
|
|
|
bool cChunk::HasBlockEntityAt(Vector3i a_BlockPos)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-07-23 14:12:51 -04:00
|
|
|
return (GetBlockEntity(a_BlockPos) != nullptr);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cChunk::Stay(bool a_Stay)
|
|
|
|
{
|
2020-10-05 08:09:42 -04:00
|
|
|
if (a_Stay)
|
|
|
|
{
|
|
|
|
m_StayCount++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ASSERT(m_StayCount != 0);
|
|
|
|
m_StayCount--;
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-07-26 17:24:36 -04:00
|
|
|
|
2015-05-09 05:16:56 -04:00
|
|
|
void cChunk::CollectMobCensus(cMobCensus & toFill)
|
2013-09-07 16:19:56 -04:00
|
|
|
{
|
2013-09-08 06:25:07 -04:00
|
|
|
toFill.CollectSpawnableChunk(*this);
|
2015-09-16 12:04:05 -04:00
|
|
|
std::vector<Vector3d> PlayerPositions;
|
|
|
|
PlayerPositions.reserve(m_LoadedByClient.size());
|
|
|
|
for (auto ClientHandle : m_LoadedByClient)
|
2013-09-07 16:19:56 -04:00
|
|
|
{
|
2015-09-16 12:04:05 -04:00
|
|
|
const cPlayer * currentPlayer = ClientHandle->GetPlayer();
|
|
|
|
PlayerPositions.push_back(currentPlayer->GetPosition());
|
2014-07-17 10:33:09 -04:00
|
|
|
}
|
2013-09-07 16:19:56 -04:00
|
|
|
|
|
|
|
Vector3d currentPosition;
|
2016-12-19 15:12:23 -05:00
|
|
|
for (auto & entity : m_Entities)
|
2013-09-07 16:19:56 -04:00
|
|
|
{
|
2014-07-17 13:13:23 -04:00
|
|
|
// LOGD("Counting entity #%i (%s)", (*itr)->GetUniqueID(), (*itr)->GetClass());
|
2015-09-16 12:04:05 -04:00
|
|
|
if (entity->IsMob())
|
2013-09-07 16:19:56 -04:00
|
|
|
{
|
2018-05-02 03:50:36 -04:00
|
|
|
auto & Monster = static_cast<cMonster &>(*entity);
|
2013-09-09 12:45:39 -04:00
|
|
|
currentPosition = Monster.GetPosition();
|
2015-09-24 10:04:44 -04:00
|
|
|
for (const auto & PlayerPos : PlayerPositions)
|
2013-09-07 16:19:56 -04:00
|
|
|
{
|
2015-09-16 12:04:05 -04:00
|
|
|
toFill.CollectMob(Monster, *this, (currentPosition - PlayerPos).SqrLength());
|
2013-09-07 16:19:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // for itr - m_Entitites[]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-07-26 17:24:36 -04:00
|
|
|
|
2014-07-19 08:53:41 -04:00
|
|
|
void cChunk::GetThreeRandomNumbers(int & a_X, int & a_Y, int & a_Z, int a_MaxX, int a_MaxY, int a_MaxZ)
|
2013-09-07 18:11:38 -04:00
|
|
|
{
|
2017-06-13 15:35:30 -04:00
|
|
|
ASSERT(
|
|
|
|
(a_MaxX > 0) && (a_MaxY > 0) && (a_MaxZ > 0) &&
|
|
|
|
(a_MaxX <= std::numeric_limits<int>::max() / a_MaxY) && // a_MaxX * a_MaxY doesn't overflow
|
|
|
|
(a_MaxX * a_MaxY <= std::numeric_limits<int>::max() / a_MaxZ) // a_MaxX * a_MaxY * a_MaxZ doesn't overflow
|
|
|
|
);
|
|
|
|
|
|
|
|
// MTRand gives an inclusive range [0, Max] but this gives the exclusive range [0, Max)
|
2017-06-14 11:35:34 -04:00
|
|
|
int OverallMax = (a_MaxX * a_MaxY * a_MaxZ) - 1;
|
2017-06-13 15:35:30 -04:00
|
|
|
int Random = m_World->GetTickRandomNumber(OverallMax);
|
|
|
|
|
|
|
|
a_X = Random % a_MaxX;
|
|
|
|
a_Y = (Random / a_MaxX) % a_MaxY;
|
|
|
|
a_Z = ((Random / a_MaxX) / a_MaxY) % a_MaxZ;
|
2013-09-07 18:11:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-10 17:04:33 -04:00
|
|
|
void cChunk::GetRandomBlockCoords(int & a_X, int & a_Y, int & a_Z)
|
2013-09-07 18:11:38 -04:00
|
|
|
{
|
|
|
|
// MG TODO : check if this kind of optimization (only one random call) is still needed
|
|
|
|
// MG TODO : if so propagate it
|
|
|
|
|
2021-01-18 11:09:10 -05:00
|
|
|
GetThreeRandomNumbers(a_X, a_Y, a_Z, cChunkDef::Width, cChunkDef::Height - 2, cChunkDef::Width);
|
2013-09-07 18:11:38 -04:00
|
|
|
a_Y++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-05-09 05:16:56 -04:00
|
|
|
void cChunk::SpawnMobs(cMobSpawner & a_MobSpawner)
|
2013-09-07 18:11:38 -04:00
|
|
|
{
|
2014-07-10 17:04:33 -04:00
|
|
|
int CenterX, CenterY, CenterZ;
|
|
|
|
GetRandomBlockCoords(CenterX, CenterY, CenterZ);
|
2013-10-21 11:41:48 -04:00
|
|
|
|
2014-07-10 17:04:33 -04:00
|
|
|
BLOCKTYPE PackCenterBlock = GetBlock(CenterX, CenterY, CenterZ);
|
|
|
|
if (!a_MobSpawner.CheckPackCenter(PackCenterBlock))
|
2013-09-07 20:47:02 -04:00
|
|
|
{
|
2014-07-10 17:04:33 -04:00
|
|
|
return;
|
2013-09-07 20:47:02 -04:00
|
|
|
}
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2014-07-10 17:04:33 -04:00
|
|
|
a_MobSpawner.NewPack();
|
|
|
|
int NumberOfTries = 0;
|
|
|
|
int NumberOfSuccess = 0;
|
|
|
|
int MaxNbOfSuccess = 4; // This can be changed during the process for Wolves and Ghasts
|
|
|
|
while ((NumberOfTries < 12) && (NumberOfSuccess < MaxNbOfSuccess))
|
|
|
|
{
|
|
|
|
const int HorizontalRange = 20; // MG TODO : relocate
|
|
|
|
const int VerticalRange = 0; // MG TODO : relocate
|
|
|
|
int TryX, TryY, TryZ;
|
|
|
|
GetThreeRandomNumbers(TryX, TryY, TryZ, 2 * HorizontalRange + 1, 2 * VerticalRange + 1, 2 * HorizontalRange + 1);
|
|
|
|
TryX -= HorizontalRange;
|
|
|
|
TryY -= VerticalRange;
|
|
|
|
TryZ -= HorizontalRange;
|
|
|
|
TryX += CenterX;
|
|
|
|
TryY += CenterY;
|
|
|
|
TryZ += CenterZ;
|
|
|
|
|
|
|
|
ASSERT(TryY > 0);
|
|
|
|
ASSERT(TryY < cChunkDef::Height - 1);
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2020-04-24 18:00:27 -04:00
|
|
|
int WorldX, WorldY, WorldZ;
|
|
|
|
PositionToWorldPosition(TryX, TryY, TryZ, WorldX, WorldY, WorldZ);
|
|
|
|
|
2014-07-10 17:04:33 -04:00
|
|
|
// MG TODO :
|
|
|
|
// 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
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2014-07-10 17:04:33 -04:00
|
|
|
/*
|
|
|
|
NIBBLETYPE SkyLight = 0;
|
|
|
|
|
|
|
|
NIBBLETYPE BlockLight = 0;
|
|
|
|
*/
|
2013-09-07 20:47:02 -04:00
|
|
|
|
2014-07-10 17:04:33 -04:00
|
|
|
NumberOfTries++;
|
2021-01-04 21:13:02 -05:00
|
|
|
|
|
|
|
Vector3i Try(TryX, TryY, TryZ);
|
|
|
|
const auto Chunk = GetRelNeighborChunkAdjustCoords(Try);
|
|
|
|
|
|
|
|
if ((Chunk == nullptr) || !Chunk->IsValid() || !Chunk->IsLightValid())
|
2013-09-07 20:47:02 -04:00
|
|
|
{
|
2014-07-10 17:04:33 -04:00
|
|
|
continue;
|
2013-09-07 20:47:02 -04:00
|
|
|
}
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2021-01-04 21:13:02 -05:00
|
|
|
auto newMob = a_MobSpawner.TryToSpawnHere(this, Try, GetBiomeAt(Try.x, Try.z), MaxNbOfSuccess);
|
2014-10-20 16:55:07 -04:00
|
|
|
if (newMob == nullptr)
|
2014-07-10 17:04:33 -04:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
double ActualX = WorldX + 0.5;
|
|
|
|
double ActualZ = WorldZ + 0.5;
|
|
|
|
newMob->SetPosition(ActualX, WorldY, ActualZ);
|
2018-09-24 16:33:39 -04:00
|
|
|
FLOGD("Spawning {0} #{1} at {2}", newMob->GetClass(), newMob->GetUniqueID(), Vector3i{WorldX, WorldY, WorldZ});
|
2014-07-10 17:04:33 -04:00
|
|
|
NumberOfSuccess++;
|
|
|
|
} // while (retry)
|
2013-09-07 18:11:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2015-01-11 16:12:26 -05:00
|
|
|
void cChunk::Tick(std::chrono::milliseconds a_Dt)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2021-01-05 19:35:42 -05:00
|
|
|
const auto ShouldTick = ShouldBeTicked();
|
|
|
|
|
2016-04-05 04:45:09 -04:00
|
|
|
// If we are not valid, tick players and bailout
|
2021-01-05 19:35:42 -05:00
|
|
|
if (!ShouldTick)
|
2016-04-05 04:45:09 -04:00
|
|
|
{
|
2016-12-19 15:12:23 -05:00
|
|
|
for (const auto & Entity : m_Entities)
|
2016-04-05 04:45:09 -04:00
|
|
|
{
|
|
|
|
if (Entity->IsPlayer())
|
|
|
|
{
|
|
|
|
Entity->Tick(a_Dt, *this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-13 17:02:10 -04:00
|
|
|
TickBlocks();
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-04-01 16:56:25 -04:00
|
|
|
// Tick all block entities in this chunk:
|
2017-05-22 16:27:55 -04:00
|
|
|
for (auto & KeyPair : m_BlockEntities)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2017-05-22 16:27:55 -04:00
|
|
|
m_IsDirty = KeyPair.second->Tick(a_Dt, *this) | m_IsDirty;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2016-12-19 15:12:23 -05:00
|
|
|
for (auto itr = m_Entities.begin(); itr != m_Entities.end();)
|
2014-08-13 19:03:30 -04:00
|
|
|
{
|
2016-02-07 12:07:14 -05:00
|
|
|
// Do not tick mobs that are detached from the world. They're either scheduled for teleportation or for removal.
|
|
|
|
if (!(*itr)->IsTicking())
|
|
|
|
{
|
|
|
|
++itr;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-04-06 05:16:14 -04:00
|
|
|
if (!((*itr)->IsMob())) // Mobs are ticked inside cWorld::TickMobs() (as we don't have to tick them if they are far away from players)
|
|
|
|
{
|
|
|
|
// Tick all entities in this chunk (except mobs):
|
|
|
|
ASSERT((*itr)->GetParentChunk() == this);
|
|
|
|
(*itr)->Tick(a_Dt, *this);
|
|
|
|
ASSERT((*itr)->GetParentChunk() == this);
|
|
|
|
}
|
2014-07-22 12:26:48 -04:00
|
|
|
|
2016-02-07 12:07:14 -05:00
|
|
|
// Do not move mobs that are detached from the world to neighbors. They're either scheduled for teleportation or for removal.
|
2016-03-27 13:43:30 -04:00
|
|
|
// Because the schedulded destruction is going to look for them in this chunk. See cEntity::destroy.
|
2016-02-07 12:07:14 -05:00
|
|
|
if (!(*itr)->IsTicking())
|
2014-05-31 17:28:51 -04:00
|
|
|
{
|
2016-02-07 12:07:14 -05:00
|
|
|
++itr;
|
|
|
|
continue;
|
2014-06-21 17:07:38 -04:00
|
|
|
}
|
2016-02-07 12:07:14 -05:00
|
|
|
|
2016-12-19 15:12:23 -05:00
|
|
|
if (
|
|
|
|
((*itr)->GetChunkX() != m_PosX) ||
|
|
|
|
((*itr)->GetChunkZ() != m_PosZ)
|
2013-04-13 17:02:10 -04:00
|
|
|
)
|
|
|
|
{
|
2016-02-07 12:07:14 -05:00
|
|
|
// Mark as dirty if it was a server-generated entity:
|
|
|
|
if (!(*itr)->IsPlayer())
|
|
|
|
{
|
|
|
|
MarkDirty();
|
|
|
|
}
|
2016-12-19 15:12:23 -05:00
|
|
|
|
|
|
|
// This block is very similar to RemoveEntity, except it uses an iterator to avoid scanning the whole m_Entities
|
|
|
|
// The entity moved out of the chunk, move it to the neighbor
|
|
|
|
(*itr)->SetParentChunk(nullptr);
|
|
|
|
MoveEntityToNewChunk(std::move(*itr));
|
|
|
|
|
2016-02-20 13:39:01 -05:00
|
|
|
itr = m_Entities.erase(itr);
|
2013-04-13 17:02:10 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-12 10:21:07 -04:00
|
|
|
++itr;
|
2013-04-13 17:02:10 -04:00
|
|
|
}
|
2014-05-31 17:28:51 -04:00
|
|
|
} // for itr - m_Entitites[]
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2013-04-01 16:56:25 -04:00
|
|
|
ApplyWeatherToTop();
|
2020-08-01 06:25:06 -04:00
|
|
|
|
2021-01-05 19:35:42 -05:00
|
|
|
// Tick simulators:
|
|
|
|
m_World->GetSimulatorManager()->SimulateChunk(a_Dt, m_PosX, m_PosZ, this);
|
|
|
|
|
|
|
|
// Check blocks after everything else to apply at least one round of queued ticks (i.e. cBlockHandler::Check) this tick:
|
|
|
|
CheckBlocks();
|
|
|
|
|
|
|
|
// Finally, tell the client about all block changes:
|
2020-08-01 06:25:06 -04:00
|
|
|
BroadcastPendingBlockChanges();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-04-17 05:36:37 -04:00
|
|
|
void cChunk::TickBlock(const Vector3i a_RelPos)
|
2013-11-30 09:58:27 -05:00
|
|
|
{
|
2014-02-02 09:49:37 -05:00
|
|
|
cChunkInterface ChunkInterface(this->GetWorld()->GetChunkMap());
|
|
|
|
cBlockInServerPluginInterface PluginInterface(*this->GetWorld());
|
2020-09-20 09:50:52 -04:00
|
|
|
cBlockHandler::For(GetBlock(a_RelPos)).OnUpdate(ChunkInterface, *this->GetWorld(), PluginInterface, *this, a_RelPos);
|
2013-11-30 09:58:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-12-19 15:12:23 -05:00
|
|
|
void cChunk::MoveEntityToNewChunk(OwnedEntity a_Entity)
|
2013-04-13 17:02:10 -04:00
|
|
|
{
|
2013-05-19 07:49:01 -04:00
|
|
|
cChunk * Neighbor = GetNeighborChunk(a_Entity->GetChunkX() * cChunkDef::Width, a_Entity->GetChunkZ() * cChunkDef::Width);
|
2014-10-20 16:55:07 -04:00
|
|
|
if (Neighbor == nullptr)
|
2013-04-13 17:02:10 -04:00
|
|
|
{
|
2021-01-05 19:35:42 -05:00
|
|
|
LOGWARNING("%s: Entity at %p (%s, ID %d) moving to a non-existent chunk.",
|
|
|
|
__FUNCTION__, static_cast<void *>(a_Entity.get()), a_Entity->GetClass(), a_Entity->GetUniqueID()
|
|
|
|
);
|
|
|
|
|
|
|
|
Neighbor = &m_ChunkMap->ConstructChunk(a_Entity->GetChunkX(), a_Entity->GetChunkZ());
|
2013-04-13 17:02:10 -04:00
|
|
|
}
|
|
|
|
|
2013-05-19 07:49:01 -04:00
|
|
|
ASSERT(Neighbor != this); // Moving into the same chunk? wtf?
|
2016-12-19 15:12:23 -05:00
|
|
|
auto & Entity = *a_Entity;
|
|
|
|
Neighbor->AddEntity(std::move(a_Entity));
|
2013-04-13 17:02:10 -04:00
|
|
|
|
|
|
|
class cMover :
|
|
|
|
public cClientDiffCallback
|
|
|
|
{
|
|
|
|
virtual void Removed(cClientHandle * a_Client) override
|
|
|
|
{
|
2016-12-19 15:12:23 -05:00
|
|
|
a_Client->SendDestroyEntity(m_Entity);
|
2013-04-13 17:02:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Added(cClientHandle * a_Client) override
|
|
|
|
{
|
2016-12-19 15:12:23 -05:00
|
|
|
m_Entity.SpawnOn(*a_Client);
|
2013-04-13 17:02:10 -04:00
|
|
|
}
|
|
|
|
|
2016-12-19 15:12:23 -05:00
|
|
|
cEntity & m_Entity;
|
2013-04-13 17:02:10 -04:00
|
|
|
|
|
|
|
public:
|
2016-12-19 15:12:23 -05:00
|
|
|
cMover(cEntity & a_CallbackEntity) :
|
2015-05-19 14:32:10 -04:00
|
|
|
m_Entity(a_CallbackEntity)
|
2013-04-13 17:02:10 -04:00
|
|
|
{}
|
2016-12-19 15:12:23 -05:00
|
|
|
} Mover(Entity);
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2013-04-13 17:02:10 -04:00
|
|
|
m_ChunkMap->CompareChunkClients(this, Neighbor, Mover);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-25 13:52:08 -04:00
|
|
|
void cChunk::BroadcastPendingBlockChanges(void)
|
|
|
|
{
|
2021-03-28 09:40:57 -04:00
|
|
|
if (const auto PendingBlocksCount = m_PendingSendBlocks.size(); PendingBlocksCount >= 10240)
|
2013-08-19 15:55:03 -04:00
|
|
|
{
|
2021-03-28 09:40:57 -04:00
|
|
|
// Resend the full chunk:
|
|
|
|
for (const auto ClientHandle : m_LoadedByClient)
|
2015-03-09 17:32:12 -04:00
|
|
|
{
|
2020-09-12 15:43:18 -04:00
|
|
|
m_World->ForceSendChunkTo(m_PosX, m_PosZ, cChunkSender::Priority::Medium, ClientHandle);
|
2015-03-09 17:32:12 -04:00
|
|
|
}
|
|
|
|
}
|
2021-03-28 09:40:57 -04:00
|
|
|
else if (PendingBlocksCount != 0)
|
2012-08-25 13:52:08 -04:00
|
|
|
{
|
2021-03-28 09:40:57 -04:00
|
|
|
// Send block changes:
|
|
|
|
for (const auto ClientHandle : m_LoadedByClient)
|
2015-03-09 17:32:12 -04:00
|
|
|
{
|
2015-09-16 12:04:05 -04:00
|
|
|
ClientHandle->SendBlockChanges(m_PosX, m_PosZ, m_PendingSendBlocks);
|
2015-03-09 17:32:12 -04:00
|
|
|
}
|
2012-08-25 13:52:08 -04:00
|
|
|
}
|
2021-03-28 09:40:57 -04:00
|
|
|
|
|
|
|
// Send block entity changes:
|
|
|
|
for (const auto Entity : m_PendingSendBlockEntities)
|
|
|
|
{
|
|
|
|
for (const auto ClientHandle : m_LoadedByClient)
|
|
|
|
{
|
|
|
|
Entity->SendTo(*ClientHandle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-19 03:13:19 -04:00
|
|
|
m_PendingSendBlocks.clear();
|
2021-03-28 09:40:57 -04:00
|
|
|
m_PendingSendBlockEntities.clear();
|
2012-08-25 13:52:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-01-31 18:17:41 -05:00
|
|
|
void cChunk::CheckBlocks()
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-01-31 18:17:41 -05:00
|
|
|
cChunkInterface ChunkInterface(m_World->GetChunkMap());
|
2014-02-02 09:49:37 -05:00
|
|
|
cBlockInServerPluginInterface PluginInterface(*m_World);
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2020-07-29 13:51:39 -04:00
|
|
|
// Process a limited number of blocks since cBlockHandler::Check may queue more to tick
|
2021-03-28 09:40:57 -04:00
|
|
|
auto Count = m_BlocksToCheck.size();
|
2020-07-29 13:51:39 -04:00
|
|
|
|
|
|
|
while (Count != 0)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2021-03-28 09:40:57 -04:00
|
|
|
const auto Pos = m_BlocksToCheck.front();
|
|
|
|
m_BlocksToCheck.pop();
|
2020-07-29 13:51:39 -04:00
|
|
|
Count--;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2020-09-20 09:50:52 -04:00
|
|
|
cBlockHandler::For(GetBlock(Pos)).Check(ChunkInterface, PluginInterface, Pos, *this);
|
2020-07-29 13:51:39 -04:00
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-04-13 17:02:10 -04:00
|
|
|
void cChunk::TickBlocks(void)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-02-02 09:49:37 -05:00
|
|
|
cChunkInterface ChunkInterface(this->GetWorld()->GetChunkMap());
|
|
|
|
cBlockInServerPluginInterface PluginInterface(*this->GetWorld());
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2020-04-17 05:36:37 -04:00
|
|
|
// Tick random blocks, but the first one should be m_BlockToTick (so that SetNextBlockToTick() works)
|
|
|
|
auto Idx = cChunkDef::MakeIndexNoCheck(m_BlockToTick);
|
2020-11-06 11:54:01 -05:00
|
|
|
auto & Random = GetRandomProvider();
|
|
|
|
|
2020-04-17 05:36:37 -04:00
|
|
|
for (int i = 0; i < 50; ++i)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2020-04-17 05:36:37 -04:00
|
|
|
auto Pos = cChunkDef::IndexToCoordinate(static_cast<size_t>(Idx));
|
2020-11-06 11:54:01 -05:00
|
|
|
Idx = Random.RandInt(cChunkDef::NumBlocks - 1);
|
2020-04-17 05:36:37 -04:00
|
|
|
if (Pos.y > cChunkDef::GetHeight(m_HeightMap, Pos.x, Pos.z))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-07-17 13:13:23 -04:00
|
|
|
continue; // It's all air up here
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2020-09-20 09:50:52 -04:00
|
|
|
cBlockHandler::For(GetBlock(Pos)).OnUpdate(ChunkInterface, *this->GetWorld(), PluginInterface, *this, Pos);
|
2020-04-17 05:36:37 -04:00
|
|
|
} // for i
|
|
|
|
|
|
|
|
// Set a new random coord for the next tick:
|
|
|
|
m_BlockToTick = cChunkDef::IndexToCoordinate(static_cast<size_t>(Idx));
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2012-09-04 11:07:08 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-04-01 16:56:25 -04:00
|
|
|
void cChunk::ApplyWeatherToTop()
|
2012-10-21 17:15:57 -04:00
|
|
|
{
|
|
|
|
if (
|
2017-06-13 15:35:30 -04:00
|
|
|
(GetRandomProvider().RandBool(0.99)) ||
|
2012-10-21 17:15:57 -04:00
|
|
|
(
|
|
|
|
(m_World->GetWeather() != eWeather_Rain) &&
|
|
|
|
(m_World->GetWeather() != eWeather_ThunderStorm)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// Not the right weather, or not at this tick; bail out
|
|
|
|
return;
|
|
|
|
}
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2013-04-01 16:56:25 -04:00
|
|
|
int X = m_World->GetTickRandomNumber(15);
|
|
|
|
int Z = m_World->GetTickRandomNumber(15);
|
2015-02-25 13:02:08 -05:00
|
|
|
|
|
|
|
int Height = GetHeight(X, Z);
|
|
|
|
|
|
|
|
if (GetSnowStartHeight(GetBiomeAt(X, Z)) > Height)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-21 09:13:18 -04:00
|
|
|
if (GetBlockLight(X, Height, Z) > 10)
|
|
|
|
{
|
|
|
|
// Snow only generates on blocks with a block light level of 10 or less.
|
|
|
|
// Ref: https://minecraft.gamepedia.com/Snow_(layer)#Snowfall
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-25 13:02:08 -05:00
|
|
|
BLOCKTYPE TopBlock = GetBlock(X, Height, Z);
|
|
|
|
NIBBLETYPE TopMeta = GetMeta (X, Height, Z);
|
|
|
|
if (m_World->IsDeepSnowEnabled() && (TopBlock == E_BLOCK_SNOW))
|
|
|
|
{
|
|
|
|
int MaxSize = 7;
|
|
|
|
BLOCKTYPE BlockType[4];
|
|
|
|
NIBBLETYPE BlockMeta[4];
|
|
|
|
UnboundedRelGetBlock(X - 1, Height, Z, BlockType[0], BlockMeta[0]);
|
|
|
|
UnboundedRelGetBlock(X + 1, Height, Z, BlockType[1], BlockMeta[1]);
|
|
|
|
UnboundedRelGetBlock(X, Height, Z - 1, BlockType[2], BlockMeta[2]);
|
|
|
|
UnboundedRelGetBlock(X, Height, Z + 1, BlockType[3], BlockMeta[3]);
|
|
|
|
for (int i = 0; i < 4; i++)
|
2012-10-21 17:15:57 -04:00
|
|
|
{
|
2015-02-25 13:02:08 -05:00
|
|
|
switch (BlockType[i])
|
2012-10-21 17:15:57 -04:00
|
|
|
{
|
2015-02-25 13:02:08 -05:00
|
|
|
case E_BLOCK_AIR:
|
2012-10-21 17:15:57 -04:00
|
|
|
{
|
2015-02-25 13:02:08 -05:00
|
|
|
MaxSize = 0;
|
|
|
|
break;
|
2012-10-21 17:15:57 -04:00
|
|
|
}
|
2015-02-25 13:02:08 -05:00
|
|
|
case E_BLOCK_SNOW:
|
2012-10-21 17:15:57 -04:00
|
|
|
{
|
2015-02-25 13:02:08 -05:00
|
|
|
MaxSize = std::min(BlockMeta[i] + 1, MaxSize);
|
|
|
|
break;
|
2012-10-21 17:15:57 -04:00
|
|
|
}
|
|
|
|
}
|
2015-02-25 13:02:08 -05:00
|
|
|
}
|
|
|
|
if (TopMeta < MaxSize)
|
|
|
|
{
|
|
|
|
FastSetBlock(X, Height, Z, E_BLOCK_SNOW, TopMeta + 1);
|
|
|
|
}
|
|
|
|
else if (TopMeta > MaxSize)
|
2013-12-09 09:05:32 -05:00
|
|
|
{
|
2015-02-25 13:02:08 -05:00
|
|
|
FastSetBlock(X, Height, Z, E_BLOCK_SNOW, TopMeta - 1);
|
2013-12-09 09:05:32 -05:00
|
|
|
}
|
2015-02-25 13:02:08 -05:00
|
|
|
}
|
2015-02-14 17:11:38 -05:00
|
|
|
else if (cBlockInfo::IsSnowable(TopBlock) && (Height < cChunkDef::Height - 1))
|
2015-02-25 13:02:08 -05:00
|
|
|
{
|
2019-10-16 04:06:34 -04:00
|
|
|
SetBlock({X, Height + 1, Z}, E_BLOCK_SNOW, 0);
|
2015-02-25 13:02:08 -05:00
|
|
|
}
|
|
|
|
else if (IsBlockWater(TopBlock) && (TopMeta == 0))
|
|
|
|
{
|
2019-10-16 04:06:34 -04:00
|
|
|
SetBlock({X, Height, Z}, E_BLOCK_ICE, 0);
|
2015-02-25 13:02:08 -05:00
|
|
|
}
|
|
|
|
else if (
|
|
|
|
(m_World->IsDeepSnowEnabled()) &&
|
|
|
|
(
|
|
|
|
(TopBlock == E_BLOCK_RED_ROSE) ||
|
|
|
|
(TopBlock == E_BLOCK_YELLOW_FLOWER) ||
|
|
|
|
(TopBlock == E_BLOCK_RED_MUSHROOM) ||
|
|
|
|
(TopBlock == E_BLOCK_BROWN_MUSHROOM)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
{
|
2019-10-16 04:06:34 -04:00
|
|
|
SetBlock({X, Height, Z}, E_BLOCK_SNOW, 0);
|
2015-02-25 13:02:08 -05:00
|
|
|
}
|
2012-10-21 17:15:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-10-16 04:06:34 -04:00
|
|
|
cItems cChunk::PickupsFromBlock(Vector3i a_RelPos, const cEntity * a_Digger, const cItem * a_Tool)
|
|
|
|
{
|
2020-09-23 11:06:27 -04:00
|
|
|
BLOCKTYPE BlockType;
|
|
|
|
NIBBLETYPE BlockMeta;
|
|
|
|
GetBlockTypeMeta(a_RelPos, BlockType, BlockMeta);
|
|
|
|
|
|
|
|
cItems Pickups;
|
|
|
|
const auto BlockEntity = GetBlockEntityRel(a_RelPos);
|
|
|
|
|
2021-03-28 09:41:34 -04:00
|
|
|
if ((a_Tool == nullptr) || a_Tool->GetHandler()->CanHarvestBlock(BlockType))
|
2020-09-23 11:06:27 -04:00
|
|
|
{
|
2021-03-28 09:41:34 -04:00
|
|
|
Pickups = cBlockHandler::For(BlockType).ConvertToPickups(BlockMeta, a_Tool);
|
2020-09-23 11:06:27 -04:00
|
|
|
|
|
|
|
if (BlockEntity != nullptr)
|
|
|
|
{
|
|
|
|
auto BlockEntityPickups = BlockEntity->ConvertToPickups();
|
|
|
|
Pickups.insert(Pickups.end(), std::make_move_iterator(BlockEntityPickups.begin()), std::make_move_iterator(BlockEntityPickups.end()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: this should be in cWorld::DropBlockAsPickups. When it's here we can't check the return value and cancel spawning:
|
|
|
|
cRoot::Get()->GetPluginManager()->CallHookBlockToPickups(
|
|
|
|
*m_World,
|
|
|
|
cChunkDef::RelativeToAbsolute(a_RelPos, GetPos()),
|
|
|
|
BlockType, BlockMeta, BlockEntity,
|
|
|
|
a_Digger, a_Tool, Pickups
|
|
|
|
);
|
|
|
|
|
|
|
|
return Pickups;
|
2019-10-16 04:06:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-10-11 05:02:53 -04:00
|
|
|
int cChunk::GrowPlantAt(Vector3i a_RelPos, int a_NumStages)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2020-09-20 09:50:52 -04:00
|
|
|
return cBlockHandler::For(GetBlock(a_RelPos)).Grow(*this, a_RelPos, a_NumStages);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-10-11 05:02:53 -04:00
|
|
|
bool cChunk::UnboundedRelGetBlock(Vector3i a_RelPos, BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2019-10-11 05:02:53 -04:00
|
|
|
if (!cChunkDef::IsValidHeight(a_RelPos.y))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2019-10-11 05:02:53 -04:00
|
|
|
LOGWARNING("%s: requesting a block with a_RelY out of range: %d", __FUNCTION__, a_RelPos.y);
|
2012-06-14 09:06:06 -04:00
|
|
|
return false;
|
|
|
|
}
|
2019-10-11 05:02:53 -04:00
|
|
|
auto chunk = GetRelNeighborChunkAdjustCoords(a_RelPos);
|
|
|
|
if ((chunk == nullptr) || !chunk->IsValid())
|
2012-12-14 17:38:30 -05:00
|
|
|
{
|
2013-10-22 11:54:09 -04:00
|
|
|
// The chunk is not available, bail out
|
|
|
|
return false;
|
2012-12-14 17:38:30 -05:00
|
|
|
}
|
2019-10-11 05:02:53 -04:00
|
|
|
chunk->GetBlockTypeMeta(a_RelPos, a_BlockType, a_BlockMeta);
|
2013-10-22 11:54:09 -04:00
|
|
|
return true;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-10-11 05:02:53 -04:00
|
|
|
bool cChunk::UnboundedRelGetBlockType(Vector3i a_RelPos, BLOCKTYPE & a_BlockType) const
|
2013-05-28 08:05:23 -04:00
|
|
|
{
|
2019-10-11 05:02:53 -04:00
|
|
|
if (!cChunkDef::IsValidHeight(a_RelPos.y))
|
2013-05-28 08:05:23 -04:00
|
|
|
{
|
2019-10-11 05:02:53 -04:00
|
|
|
LOGWARNING("%s: requesting a block with a_RelY out of range: %d", __FUNCTION__, a_RelPos.y);
|
2013-05-28 08:05:23 -04:00
|
|
|
return false;
|
|
|
|
}
|
2019-10-11 05:02:53 -04:00
|
|
|
auto chunk = GetRelNeighborChunkAdjustCoords(a_RelPos);
|
|
|
|
if ((chunk == nullptr) || !chunk->IsValid())
|
2013-05-28 08:05:23 -04:00
|
|
|
{
|
2013-10-22 11:54:09 -04:00
|
|
|
// The chunk is not available, bail out
|
|
|
|
return false;
|
2013-05-28 08:05:23 -04:00
|
|
|
}
|
2019-10-11 05:02:53 -04:00
|
|
|
a_BlockType = chunk->GetBlock(a_RelPos);
|
2013-10-22 11:54:09 -04:00
|
|
|
return true;
|
2013-05-28 08:05:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-10-11 05:02:53 -04:00
|
|
|
bool cChunk::UnboundedRelGetBlockMeta(Vector3i a_RelPos, NIBBLETYPE & a_BlockMeta) const
|
2013-05-28 08:05:23 -04:00
|
|
|
{
|
2019-10-11 05:02:53 -04:00
|
|
|
if (!cChunkDef::IsValidHeight(a_RelPos.y))
|
2013-05-28 08:05:23 -04:00
|
|
|
{
|
2019-10-11 05:02:53 -04:00
|
|
|
LOGWARNING("%s: requesting a block with a_RelY out of range: %d", __FUNCTION__, a_RelPos.y);
|
2013-05-28 08:05:23 -04:00
|
|
|
return false;
|
|
|
|
}
|
2019-10-11 05:02:53 -04:00
|
|
|
auto chunk = GetRelNeighborChunkAdjustCoords(a_RelPos);
|
|
|
|
if ((chunk == nullptr) || !chunk->IsValid())
|
2013-05-28 08:05:23 -04:00
|
|
|
{
|
2013-10-22 11:54:09 -04:00
|
|
|
// The chunk is not available, bail out
|
|
|
|
return false;
|
2013-05-28 08:05:23 -04:00
|
|
|
}
|
2019-10-11 05:02:53 -04:00
|
|
|
a_BlockMeta = chunk->GetMeta(a_RelPos);
|
2013-10-22 11:54:09 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-05-28 08:05:23 -04:00
|
|
|
|
2013-10-22 11:54:09 -04:00
|
|
|
|
2019-10-11 05:02:53 -04:00
|
|
|
bool cChunk::UnboundedRelGetBlockBlockLight(Vector3i a_RelPos, NIBBLETYPE & a_BlockBlockLight) const
|
2013-10-22 11:54:09 -04:00
|
|
|
{
|
2019-10-11 05:02:53 -04:00
|
|
|
if (!cChunkDef::IsValidHeight(a_RelPos.y))
|
2013-05-28 08:05:23 -04:00
|
|
|
{
|
2019-10-11 05:02:53 -04:00
|
|
|
LOGWARNING("%s: requesting a block with a_RelY out of range: %d", __FUNCTION__, a_RelPos.y);
|
2013-10-22 11:54:09 -04:00
|
|
|
return false;
|
2013-05-28 08:05:23 -04:00
|
|
|
}
|
2019-10-11 05:02:53 -04:00
|
|
|
auto chunk = GetRelNeighborChunkAdjustCoords(a_RelPos);
|
|
|
|
if ((chunk == nullptr) || !chunk->IsValid())
|
2013-05-28 08:05:23 -04:00
|
|
|
{
|
2013-10-22 11:54:09 -04:00
|
|
|
// The chunk is not available, bail out
|
|
|
|
return false;
|
2013-05-28 08:05:23 -04:00
|
|
|
}
|
2019-10-11 05:02:53 -04:00
|
|
|
a_BlockBlockLight = chunk->GetBlockLight(a_RelPos);
|
2013-10-22 11:54:09 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-10-11 05:02:53 -04:00
|
|
|
bool cChunk::UnboundedRelGetBlockSkyLight(Vector3i a_RelPos, NIBBLETYPE & a_BlockSkyLight) const
|
2013-10-22 11:54:09 -04:00
|
|
|
{
|
2019-10-11 05:02:53 -04:00
|
|
|
if (!cChunkDef::IsValidHeight(a_RelPos.y))
|
2013-05-28 08:05:23 -04:00
|
|
|
{
|
2019-10-11 05:02:53 -04:00
|
|
|
LOGWARNING("%s: requesting a block with a_RelY out of range: %d", __FUNCTION__, a_RelPos.y);
|
2013-10-22 11:54:09 -04:00
|
|
|
return false;
|
2013-05-28 08:05:23 -04:00
|
|
|
}
|
2019-10-11 05:02:53 -04:00
|
|
|
auto chunk = GetRelNeighborChunkAdjustCoords(a_RelPos);
|
|
|
|
if ((chunk == nullptr) || !chunk->IsValid())
|
2013-05-28 08:05:23 -04:00
|
|
|
{
|
2013-10-22 11:54:09 -04:00
|
|
|
// The chunk is not available, bail out
|
|
|
|
return false;
|
2013-05-28 08:05:23 -04:00
|
|
|
}
|
2019-10-11 05:02:53 -04:00
|
|
|
a_BlockSkyLight = chunk->GetSkyLight(a_RelPos);
|
2013-10-22 11:54:09 -04:00
|
|
|
return true;
|
2013-05-28 08:05:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-10-11 05:02:53 -04:00
|
|
|
bool cChunk::UnboundedRelGetBlockLights(Vector3i a_RelPos, NIBBLETYPE & a_BlockLight, NIBBLETYPE & a_SkyLight) const
|
2013-10-27 04:19:13 -04:00
|
|
|
{
|
2019-10-11 05:02:53 -04:00
|
|
|
if (!cChunkDef::IsValidHeight(a_RelPos.y))
|
2013-10-27 04:19:13 -04:00
|
|
|
{
|
2019-10-11 05:02:53 -04:00
|
|
|
LOGWARNING("%s: requesting a block with a_RelY out of range: %d", __FUNCTION__, a_RelPos.y);
|
2013-10-27 04:19:13 -04:00
|
|
|
return false;
|
|
|
|
}
|
2019-10-11 05:02:53 -04:00
|
|
|
auto chunk = GetRelNeighborChunkAdjustCoords(a_RelPos);
|
|
|
|
if ((chunk == nullptr) || !chunk->IsValid())
|
2013-10-27 04:19:13 -04:00
|
|
|
{
|
|
|
|
// The chunk is not available, bail out
|
|
|
|
return false;
|
|
|
|
}
|
2019-10-11 05:02:53 -04:00
|
|
|
a_BlockLight = chunk->GetBlockLight(a_RelPos);
|
|
|
|
a_SkyLight = chunk->GetSkyLight (a_RelPos);
|
2013-10-27 04:19:13 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-10-11 05:02:53 -04:00
|
|
|
bool cChunk::UnboundedRelSetBlock(Vector3i a_RelPos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2019-10-11 05:02:53 -04:00
|
|
|
if (!cChunkDef::IsValidHeight(a_RelPos.y))
|
2012-12-14 17:38:30 -05:00
|
|
|
{
|
2019-10-11 05:02:53 -04:00
|
|
|
LOGWARNING("%s: requesting a block with a_RelY out of range: %d", __FUNCTION__, a_RelPos.y);
|
2012-12-14 17:38:30 -05:00
|
|
|
return false;
|
|
|
|
}
|
2019-10-11 05:02:53 -04:00
|
|
|
auto chunk = GetRelNeighborChunkAdjustCoords(a_RelPos);
|
|
|
|
if ((chunk == nullptr) || !chunk->IsValid())
|
2012-12-14 17:38:30 -05:00
|
|
|
{
|
2013-10-22 11:54:09 -04:00
|
|
|
// The chunk is not available, bail out
|
|
|
|
return false;
|
2012-12-14 17:38:30 -05:00
|
|
|
}
|
2019-10-11 05:02:53 -04:00
|
|
|
chunk->SetBlock(a_RelPos, a_BlockType, a_BlockMeta);
|
2013-10-22 11:54:09 -04:00
|
|
|
return true;
|
2014-07-17 10:33:09 -04:00
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-10-11 05:02:53 -04:00
|
|
|
bool cChunk::UnboundedRelFastSetBlock(Vector3i a_RelPos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2019-10-11 05:02:53 -04:00
|
|
|
if (!cChunkDef::IsValidHeight(a_RelPos.y))
|
2012-12-14 17:38:30 -05:00
|
|
|
{
|
2019-10-11 05:02:53 -04:00
|
|
|
LOGWARNING("%s: requesting a block with a_RelY out of range: %d", __FUNCTION__, a_RelPos.y);
|
2012-12-14 17:38:30 -05:00
|
|
|
return false;
|
|
|
|
}
|
2019-10-11 05:02:53 -04:00
|
|
|
auto chunk = GetRelNeighborChunkAdjustCoords(a_RelPos);
|
|
|
|
if ((chunk == nullptr) || !chunk->IsValid())
|
2012-12-14 17:38:30 -05:00
|
|
|
{
|
2013-10-22 11:54:09 -04:00
|
|
|
// The chunk is not available, bail out
|
|
|
|
return false;
|
2012-12-14 17:38:30 -05:00
|
|
|
}
|
2019-10-11 05:02:53 -04:00
|
|
|
chunk->FastSetBlock(a_RelPos, a_BlockType, a_BlockMeta);
|
2013-10-22 11:54:09 -04:00
|
|
|
return true;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-04 21:13:02 -05:00
|
|
|
int cChunk::GetHeight(int a_X, int a_Z) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2021-01-18 11:09:10 -05:00
|
|
|
ASSERT((a_X >= 0) && (a_X < cChunkDef::Width) && (a_Z >= 0) && (a_Z < cChunkDef::Width));
|
|
|
|
return m_HeightMap[a_X + a_Z * cChunkDef::Width];
|
2021-01-04 21:13:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cChunk::IsWeatherSunnyAt(int a_RelX, int a_RelZ) const
|
|
|
|
{
|
|
|
|
return m_World->IsWeatherSunny() || IsBiomeNoDownfall(GetBiomeAt(a_RelX, a_RelZ));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cChunk::IsWeatherWetAt(const int a_RelX, const int a_RelZ) const
|
|
|
|
{
|
|
|
|
const auto Biome = GetBiomeAt(a_RelX, a_RelZ);
|
|
|
|
return m_World->IsWeatherWet() && !IsBiomeNoDownfall(Biome) && !IsBiomeCold(Biome);
|
|
|
|
}
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2021-01-04 21:13:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cChunk::IsWeatherWetAt(const Vector3i a_Position) const
|
|
|
|
{
|
|
|
|
if ((a_Position.y < 0) || !IsWeatherWetAt(a_Position.x, a_Position.z))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a_Position.y >= cChunkDef::Height)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int y = GetHeight(a_Position.x, a_Position.z); y >= a_Position.y; y--)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2021-01-04 21:13:02 -05:00
|
|
|
if (cBlockInfo::IsRainBlocker(GetBlock({ a_Position.x, y, a_Position.z })))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2021-01-04 21:13:02 -05:00
|
|
|
|
|
|
|
return true;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-03-03 10:33:55 -05:00
|
|
|
void cChunk::WakeUpSimulators(void)
|
|
|
|
{
|
2018-07-23 14:12:51 -04:00
|
|
|
auto * WaterSimulator = m_World->GetWaterSimulator();
|
|
|
|
auto * LavaSimulator = m_World->GetLavaSimulator();
|
|
|
|
auto * RedstoneSimulator = m_World->GetRedstoneSimulator();
|
|
|
|
|
2021-03-05 08:03:55 -05:00
|
|
|
for (size_t SectionIdx = 0; SectionIdx != cChunkDef::NumSections; ++SectionIdx)
|
2013-03-03 10:33:55 -05:00
|
|
|
{
|
2021-03-05 08:03:55 -05:00
|
|
|
const auto * Section = m_BlockData.GetSection(SectionIdx);
|
2018-07-23 14:12:51 -04:00
|
|
|
if (Section == nullptr)
|
2013-03-03 10:33:55 -05:00
|
|
|
{
|
2018-07-23 14:12:51 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-03-05 08:03:55 -05:00
|
|
|
for (size_t BlockIdx = 0; BlockIdx != ChunkBlockData::SectionBlockCount; ++BlockIdx)
|
2018-07-23 14:12:51 -04:00
|
|
|
{
|
2021-03-05 08:03:55 -05:00
|
|
|
const auto BlockType = (*Section)[BlockIdx];
|
|
|
|
const auto Position = cChunkDef::IndexToCoordinate(BlockIdx + SectionIdx * ChunkBlockData::SectionBlockCount);
|
2013-12-13 14:01:15 -05:00
|
|
|
|
2020-07-28 20:18:59 -04:00
|
|
|
RedstoneSimulator->AddBlock(*this, Position, BlockType);
|
|
|
|
WaterSimulator->AddBlock(*this, Position, BlockType);
|
|
|
|
LavaSimulator->AddBlock(*this, Position, BlockType);
|
2018-07-23 14:12:51 -04:00
|
|
|
}
|
|
|
|
}
|
2013-03-03 10:33:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-10-16 04:06:34 -04:00
|
|
|
void cChunk::SetBlock(Vector3i a_RelPos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2019-10-16 04:06:34 -04:00
|
|
|
FastSetBlock(a_RelPos, a_BlockType, a_BlockMeta);
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2021-03-28 09:40:57 -04:00
|
|
|
// Queue a check of this block's neighbors:
|
|
|
|
m_BlocksToCheck.push(a_RelPos);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2020-07-28 18:39:55 -04:00
|
|
|
// Wake up the simulators for this block:
|
2020-07-28 20:18:59 -04:00
|
|
|
GetWorld()->GetSimulatorManager()->WakeUp(*this, a_RelPos);
|
2020-07-28 18:39:55 -04:00
|
|
|
|
2013-08-19 03:19:45 -04:00
|
|
|
// If there was a block entity, remove it:
|
2019-10-16 04:06:34 -04:00
|
|
|
cBlockEntity * BlockEntity = GetBlockEntityRel(a_RelPos);
|
2014-10-20 16:55:07 -04:00
|
|
|
if (BlockEntity != nullptr)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
BlockEntity->Destroy();
|
2021-01-02 08:50:34 -05:00
|
|
|
BlockEntity->OnRemoveFromWorld();
|
2013-02-28 02:42:45 -05:00
|
|
|
RemoveBlockEntity(BlockEntity);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2013-08-19 03:19:45 -04:00
|
|
|
// If the new block is a block entity, create the entity object:
|
2020-08-04 14:01:36 -04:00
|
|
|
if (cBlockEntity::IsBlockEntityBlockType(a_BlockType))
|
|
|
|
{
|
2020-08-28 16:40:40 -04:00
|
|
|
AddBlockEntity(cBlockEntity::CreateByBlockType(a_BlockType, a_BlockMeta, RelativeToAbsolute(a_RelPos), m_World));
|
2020-08-04 14:01:36 -04:00
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-08-26 16:45:13 -04:00
|
|
|
void cChunk::FastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockType, BLOCKTYPE a_BlockMeta)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2021-03-14 22:28:18 -04:00
|
|
|
ASSERT(cChunkDef::IsValidRelPos({ a_RelX, a_RelY, a_RelZ }));
|
2012-06-14 09:06:06 -04:00
|
|
|
ASSERT(IsValid());
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2014-04-26 13:50:23 -04:00
|
|
|
const BLOCKTYPE OldBlockType = GetBlock(a_RelX, a_RelY, a_RelZ);
|
2021-03-05 08:03:55 -05:00
|
|
|
const BLOCKTYPE OldBlockMeta = m_BlockData.GetMeta({ a_RelX, a_RelY, a_RelZ });
|
2013-03-14 17:56:01 -04:00
|
|
|
if ((OldBlockType == a_BlockType) && (OldBlockMeta == a_BlockMeta))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-18 06:30:23 -04:00
|
|
|
bool ReplacingLiquids = (
|
|
|
|
((OldBlockType == E_BLOCK_STATIONARY_WATER) && (a_BlockType == E_BLOCK_WATER)) || // Replacing stationary water with water
|
|
|
|
((OldBlockType == E_BLOCK_WATER) && (a_BlockType == E_BLOCK_STATIONARY_WATER)) || // Replacing water with stationary water
|
|
|
|
((OldBlockType == E_BLOCK_STATIONARY_LAVA) && (a_BlockType == E_BLOCK_LAVA)) || // Replacing stationary lava with lava
|
|
|
|
((OldBlockType == E_BLOCK_LAVA) && (a_BlockType == E_BLOCK_STATIONARY_LAVA)) // Replacing lava with stationary lava
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!ReplacingLiquids)
|
|
|
|
{
|
|
|
|
MarkDirty();
|
|
|
|
}
|
2014-03-23 10:34:19 -04:00
|
|
|
|
2021-03-05 08:03:55 -05:00
|
|
|
m_BlockData.SetBlock({ a_RelX, a_RelY, a_RelZ }, a_BlockType);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-07-20 17:10:31 -04:00
|
|
|
// Queue block to be sent only if ...
|
|
|
|
if (
|
2020-08-26 16:45:13 -04:00
|
|
|
!( // ... the old and new blocktypes AREN'T leaves (because the client doesn't need meta updates)
|
|
|
|
((OldBlockType == E_BLOCK_LEAVES) && (a_BlockType == E_BLOCK_LEAVES)) ||
|
|
|
|
((OldBlockType == E_BLOCK_NEW_LEAVES) && (a_BlockType == E_BLOCK_NEW_LEAVES))
|
|
|
|
) && // ... AND ...
|
2014-06-01 15:00:11 -04:00
|
|
|
(
|
2020-08-26 16:45:13 -04:00
|
|
|
(OldBlockMeta != a_BlockMeta) || (!ReplacingLiquids)
|
2013-03-22 12:48:45 -04:00
|
|
|
)
|
|
|
|
)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2021-03-07 11:31:43 -05:00
|
|
|
m_PendingSendBlocks.emplace_back(m_PosX, m_PosZ, a_RelX, a_RelY, a_RelZ, a_BlockType, a_BlockMeta);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2021-03-05 08:03:55 -05:00
|
|
|
m_BlockData.SetMeta({ a_RelX, a_RelY, a_RelZ }, a_BlockMeta);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// ONLY recalculate lighting if it's necessary!
|
2014-03-01 14:34:19 -05:00
|
|
|
if (
|
|
|
|
(cBlockInfo::GetLightValue (OldBlockType) != cBlockInfo::GetLightValue (a_BlockType)) ||
|
|
|
|
(cBlockInfo::GetSpreadLightFalloff(OldBlockType) != cBlockInfo::GetSpreadLightFalloff(a_BlockType)) ||
|
|
|
|
(cBlockInfo::IsTransparent (OldBlockType) != cBlockInfo::IsTransparent (a_BlockType))
|
2012-06-14 09:06:06 -04:00
|
|
|
)
|
|
|
|
{
|
|
|
|
m_IsLightValid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update heightmap, if needed:
|
2021-01-18 11:09:10 -05:00
|
|
|
if (a_RelY >= m_HeightMap[a_RelX + a_RelZ * cChunkDef::Width])
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
if (a_BlockType != E_BLOCK_AIR)
|
|
|
|
{
|
2021-01-18 11:09:10 -05:00
|
|
|
m_HeightMap[a_RelX + a_RelZ * cChunkDef::Width] = static_cast<HEIGHTTYPE>(a_RelY);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-03-14 17:56:01 -04:00
|
|
|
for (int y = a_RelY - 1; y > 0; --y)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-04-27 09:45:33 -04:00
|
|
|
if (GetBlock(a_RelX, y, a_RelZ) != E_BLOCK_AIR)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2021-01-18 11:09:10 -05:00
|
|
|
m_HeightMap[a_RelX + a_RelZ * cChunkDef::Width] = static_cast<HEIGHTTYPE>(y);
|
2012-06-14 09:06:06 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} // for y - column in m_BlockData
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-25 13:52:08 -04:00
|
|
|
void cChunk::SendBlockTo(int a_RelX, int a_RelY, int a_RelZ, cClientHandle * a_Client)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2021-03-28 09:40:57 -04:00
|
|
|
const auto BlockEntity = GetBlockEntityRel({ a_RelX, a_RelY, a_RelZ });
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-10-20 16:55:07 -04:00
|
|
|
if (a_Client == nullptr)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2021-03-28 09:40:57 -04:00
|
|
|
// Queue the block (entity) for all clients in the chunk (will be sent in BroadcastPendingBlockChanges()):
|
2021-03-07 11:31:43 -05:00
|
|
|
m_PendingSendBlocks.emplace_back(m_PosX, m_PosZ, a_RelX, a_RelY, a_RelZ, GetBlock(a_RelX, a_RelY, a_RelZ), GetMeta(a_RelX, a_RelY, a_RelZ));
|
2021-03-28 09:40:57 -04:00
|
|
|
if (BlockEntity != nullptr)
|
|
|
|
{
|
|
|
|
m_PendingSendBlockEntities.push_back(BlockEntity);
|
|
|
|
}
|
2012-08-25 13:52:08 -04:00
|
|
|
return;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2012-08-25 13:52:08 -04:00
|
|
|
|
2021-03-28 09:40:57 -04:00
|
|
|
const auto Position = PositionToWorldPosition(a_RelX, a_RelY, a_RelZ);
|
|
|
|
a_Client->SendBlockChange(Position.x, Position.y, Position.z, GetBlock(a_RelX, a_RelY, a_RelZ), GetMeta(a_RelX, a_RelY, a_RelZ));
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2012-11-11 03:48:38 -05:00
|
|
|
// FS #268 - if a BlockEntity digging is cancelled by a plugin, the entire block entity must be re-sent to the client:
|
2021-03-28 09:40:57 -04:00
|
|
|
if (BlockEntity != nullptr)
|
2012-11-11 03:48:38 -05:00
|
|
|
{
|
2021-03-28 09:40:57 -04:00
|
|
|
BlockEntity->SendTo(*a_Client);
|
2017-05-22 16:27:55 -04:00
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-04-03 17:23:38 -04:00
|
|
|
void cChunk::AddBlockEntity(OwnedBlockEntity a_BlockEntity)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2020-08-28 16:40:40 -04:00
|
|
|
[[maybe_unused]] const auto Result = m_BlockEntities.emplace(
|
2021-01-18 11:09:10 -05:00
|
|
|
cChunkDef::MakeIndex(a_BlockEntity->GetRelX(), a_BlockEntity->GetPosY(), a_BlockEntity->GetRelZ()),
|
2020-08-28 16:40:40 -04:00
|
|
|
std::move(a_BlockEntity)
|
|
|
|
);
|
2017-05-22 16:27:55 -04:00
|
|
|
ASSERT(Result.second); // No block entity already at this position
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-10-16 04:06:34 -04:00
|
|
|
cBlockEntity * cChunk::GetBlockEntity(Vector3i a_AbsPos)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2021-01-18 11:09:10 -05:00
|
|
|
const auto relPos = cChunkDef::AbsoluteToRelative(a_AbsPos);
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2021-01-18 11:09:10 -05:00
|
|
|
if (!cChunkDef::IsValidRelPos(relPos))
|
2017-08-10 13:24:50 -04:00
|
|
|
{
|
2019-10-16 04:06:34 -04:00
|
|
|
// Coordinates are outside outside this chunk, no block entities here
|
2017-08-10 13:24:50 -04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-01-18 11:09:10 -05:00
|
|
|
auto itr = m_BlockEntities.find(static_cast<size_t>(cChunkDef::MakeIndexNoCheck(relPos)));
|
2020-04-03 17:23:38 -04:00
|
|
|
return (itr == m_BlockEntities.end()) ? nullptr : itr->second.get();
|
2019-10-16 04:06:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cBlockEntity * cChunk::GetBlockEntityRel(Vector3i a_RelPos)
|
|
|
|
{
|
2021-01-18 11:09:10 -05:00
|
|
|
ASSERT(cChunkDef::IsValidRelPos(a_RelPos));
|
|
|
|
auto itr = m_BlockEntities.find(static_cast<size_t>(cChunkDef::MakeIndexNoCheck(a_RelPos)));
|
2020-04-03 17:23:38 -04:00
|
|
|
return (itr == m_BlockEntities.end()) ? nullptr : itr->second.get();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-10 12:18:32 -04:00
|
|
|
bool cChunk::ShouldBeTicked(void) const
|
|
|
|
{
|
2021-01-05 19:35:42 -05:00
|
|
|
return IsValid() && (HasAnyClients() || (m_AlwaysTicked > 0));
|
2014-07-10 12:18:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cChunk::SetAlwaysTicked(bool a_AlwaysTicked)
|
|
|
|
{
|
|
|
|
if (a_AlwaysTicked)
|
|
|
|
{
|
|
|
|
m_AlwaysTicked += 1;
|
2016-10-09 08:46:09 -04:00
|
|
|
Stay(a_AlwaysTicked);
|
2014-07-10 12:18:32 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_AlwaysTicked -= 1;
|
2016-10-09 08:46:09 -04:00
|
|
|
Stay(a_AlwaysTicked);
|
2014-07-10 12:18:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-01 17:12:44 -05:00
|
|
|
bool cChunk::UseBlockEntity(cPlayer * a_Player, int a_X, int a_Y, int a_Z)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
cBlockEntity * be = GetBlockEntity(a_X, a_Y, a_Z);
|
2014-10-20 16:55:07 -04:00
|
|
|
if (be != nullptr)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2015-12-01 17:12:44 -05:00
|
|
|
return be->UsedBy(a_Player);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2015-12-01 17:12:44 -05:00
|
|
|
return false;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-18 07:06:18 -05:00
|
|
|
void cChunk::SetBiomeAt(int a_RelX, int a_RelZ, EMCSBiome a_Biome)
|
|
|
|
{
|
|
|
|
cChunkDef::SetBiome(m_BiomeMap, a_RelX, a_RelZ, a_Biome);
|
|
|
|
MarkDirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cChunk::SetAreaBiome(int a_MinRelX, int a_MaxRelX, int a_MinRelZ, int a_MaxRelZ, EMCSBiome a_Biome)
|
|
|
|
{
|
|
|
|
for (int z = a_MinRelZ; z <= a_MaxRelZ; z++)
|
|
|
|
{
|
|
|
|
for (int x = a_MinRelX; x <= a_MaxRelX; x++)
|
|
|
|
{
|
|
|
|
cChunkDef::SetBiome(m_BiomeMap, x, z, a_Biome);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MarkDirty();
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2014-02-18 07:06:18 -05:00
|
|
|
// Re-send the chunk to all clients:
|
2015-09-16 12:04:05 -04:00
|
|
|
for (auto ClientHandle : m_LoadedByClient)
|
2014-02-18 07:06:18 -05:00
|
|
|
{
|
2020-09-12 15:43:18 -04:00
|
|
|
m_World->ForceSendChunkTo(m_PosX, m_PosZ, cChunkSender::Priority::Medium, ClientHandle);
|
2014-02-18 07:06:18 -05:00
|
|
|
} // for itr - m_LoadedByClient[]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-05-09 05:16:56 -04:00
|
|
|
void cChunk::RemoveBlockEntity(cBlockEntity * a_BlockEntity)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
MarkDirty();
|
2017-05-22 16:27:55 -04:00
|
|
|
ASSERT(a_BlockEntity != nullptr);
|
2021-03-28 09:40:57 -04:00
|
|
|
m_BlockEntities.erase(static_cast<size_t>(cChunkDef::MakeIndex(a_BlockEntity->GetRelX(), a_BlockEntity->GetPosY(), a_BlockEntity->GetRelZ())));
|
|
|
|
m_PendingSendBlockEntities.erase(std::remove(m_PendingSendBlockEntities.begin(), m_PendingSendBlockEntities.end(), a_BlockEntity), m_PendingSendBlockEntities.end());
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-31 17:28:51 -04:00
|
|
|
bool cChunk::AddClient(cClientHandle * a_Client)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2015-09-16 12:04:05 -04:00
|
|
|
if (std::find(m_LoadedByClient.begin(), m_LoadedByClient.end(), a_Client) != m_LoadedByClient.end())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2015-09-16 12:04:05 -04:00
|
|
|
// Already there, nothing needed
|
|
|
|
return false;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2015-09-16 12:04:05 -04:00
|
|
|
|
|
|
|
m_LoadedByClient.push_back(a_Client);
|
2012-06-14 09:06:06 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-31 17:28:51 -04:00
|
|
|
void cChunk::RemoveClient(cClientHandle * a_Client)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2016-04-06 05:16:14 -04:00
|
|
|
auto itr = std::remove(m_LoadedByClient.begin(), m_LoadedByClient.end(), a_Client);
|
2015-09-19 12:22:57 -04:00
|
|
|
// We should always remove at most one client.
|
2016-04-06 05:16:14 -04:00
|
|
|
ASSERT(std::distance(itr, m_LoadedByClient.end()) <= 1);
|
2015-09-19 12:22:57 -04:00
|
|
|
// Note: itr can equal m_LoadedByClient.end()
|
2016-04-06 05:16:14 -04:00
|
|
|
m_LoadedByClient.erase(itr, m_LoadedByClient.end());
|
2015-09-16 12:04:05 -04:00
|
|
|
|
|
|
|
if (!a_Client->IsDestroyed())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2016-12-19 15:12:23 -05:00
|
|
|
for (auto & Entity : m_Entities)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2015-09-16 12:04:05 -04:00
|
|
|
/*
|
|
|
|
// DEBUG:
|
|
|
|
LOGD("chunk [%i, %i] destroying entity #%i for player \"%s\"",
|
|
|
|
m_PosX, m_PosZ,
|
|
|
|
(*itr)->GetUniqueID(), a_Client->GetUsername().c_str()
|
|
|
|
);
|
|
|
|
*/
|
|
|
|
a_Client->SendDestroyEntity(*Entity);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2015-09-16 12:04:05 -04:00
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-22 05:24:28 -04:00
|
|
|
bool cChunk::HasClient(cClientHandle * a_Client)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2015-09-16 12:04:05 -04:00
|
|
|
return std::find(m_LoadedByClient.begin(), m_LoadedByClient.end(), a_Client) != m_LoadedByClient.end();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-10 12:18:32 -04:00
|
|
|
bool cChunk::HasAnyClients(void) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
return !m_LoadedByClient.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-12-19 15:12:23 -05:00
|
|
|
void cChunk::AddEntity(OwnedEntity a_Entity)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-12-21 07:52:14 -05:00
|
|
|
if (!a_Entity->IsPlayer())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
MarkDirty();
|
|
|
|
}
|
2014-05-31 17:28:51 -04:00
|
|
|
|
2016-12-19 15:12:23 -05:00
|
|
|
auto EntityPtr = a_Entity.get();
|
|
|
|
|
2014-07-22 12:26:48 -04:00
|
|
|
ASSERT(std::find(m_Entities.begin(), m_Entities.end(), a_Entity) == m_Entities.end()); // Not there already
|
2016-12-19 15:12:23 -05:00
|
|
|
m_Entities.emplace_back(std::move(a_Entity));
|
2014-05-31 17:28:51 -04:00
|
|
|
|
2016-12-19 15:12:23 -05:00
|
|
|
ASSERT(EntityPtr->GetParentChunk() == nullptr);
|
|
|
|
EntityPtr->SetParentChunk(this);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-12-19 15:12:23 -05:00
|
|
|
OwnedEntity cChunk::RemoveEntity(cEntity & a_Entity)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2016-12-19 15:12:23 -05:00
|
|
|
ASSERT(a_Entity.GetParentChunk() == this);
|
|
|
|
ASSERT(!a_Entity.IsTicking());
|
|
|
|
a_Entity.SetParentChunk(nullptr);
|
|
|
|
|
2016-02-02 10:37:21 -05:00
|
|
|
// Mark as dirty if it was a server-generated entity:
|
2016-12-19 15:12:23 -05:00
|
|
|
if (!a_Entity.IsPlayer())
|
2016-02-02 10:37:21 -05:00
|
|
|
{
|
|
|
|
MarkDirty();
|
|
|
|
}
|
2016-12-19 15:12:23 -05:00
|
|
|
|
|
|
|
OwnedEntity Removed;
|
|
|
|
m_Entities.erase(
|
|
|
|
std::remove_if(
|
|
|
|
m_Entities.begin(),
|
|
|
|
m_Entities.end(),
|
|
|
|
[&a_Entity, &Removed](decltype(m_Entities)::value_type & a_Value)
|
|
|
|
{
|
|
|
|
if (a_Value.get() == &a_Entity)
|
|
|
|
{
|
|
|
|
ASSERT(!Removed);
|
|
|
|
Removed = std::move(a_Value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
),
|
|
|
|
m_Entities.end()
|
|
|
|
);
|
|
|
|
|
|
|
|
return Removed;
|
2016-02-02 10:37:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-08-08 07:09:40 -04:00
|
|
|
bool cChunk::HasEntity(UInt32 a_EntityID) const
|
2013-04-13 17:02:10 -04:00
|
|
|
{
|
2016-12-19 15:12:23 -05:00
|
|
|
for (const auto & Entity : m_Entities)
|
2013-04-13 17:02:10 -04:00
|
|
|
{
|
2016-12-19 15:12:23 -05:00
|
|
|
if (Entity->GetUniqueID() == a_EntityID)
|
2013-04-13 17:02:10 -04:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2016-12-19 15:12:23 -05:00
|
|
|
}
|
2013-04-13 17:02:10 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-08-08 07:09:40 -04:00
|
|
|
bool cChunk::ForEachEntity(cEntityCallback a_Callback) const
|
2012-06-16 04:35:07 -04:00
|
|
|
{
|
|
|
|
// The entity list is locked by the parent chunkmap's CS
|
2017-09-11 17:20:49 -04:00
|
|
|
for (const auto & Entity : m_Entities)
|
2012-06-16 04:35:07 -04:00
|
|
|
{
|
2017-09-11 17:20:49 -04:00
|
|
|
if (Entity->IsTicking() && a_Callback(*Entity))
|
2012-06-16 04:35:07 -04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} // for itr - m_Entitites[]
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-08-08 07:09:40 -04:00
|
|
|
bool cChunk::ForEachEntityInBox(const cBoundingBox & a_Box, cEntityCallback a_Callback) const
|
2014-09-03 11:00:26 -04:00
|
|
|
{
|
|
|
|
// The entity list is locked by the parent chunkmap's CS
|
2017-09-11 17:20:49 -04:00
|
|
|
for (const auto & Entity : m_Entities)
|
2014-09-03 11:00:26 -04:00
|
|
|
{
|
2017-09-11 17:20:49 -04:00
|
|
|
if (!Entity->IsTicking())
|
2016-02-01 15:49:34 -05:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2020-05-03 16:04:33 -04:00
|
|
|
if (!Entity->GetBoundingBox().DoesIntersect(a_Box))
|
2014-09-03 11:00:26 -04:00
|
|
|
{
|
|
|
|
// The entity is not in the specified box
|
|
|
|
continue;
|
|
|
|
}
|
2017-09-11 17:20:49 -04:00
|
|
|
if (a_Callback(*Entity))
|
2014-09-03 11:00:26 -04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} // for itr - m_Entitites[]
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-08-08 07:09:40 -04:00
|
|
|
bool cChunk::DoWithEntityByID(UInt32 a_EntityID, cEntityCallback a_Callback, bool & a_CallbackResult) const
|
2013-04-13 17:02:10 -04:00
|
|
|
{
|
|
|
|
// The entity list is locked by the parent chunkmap's CS
|
2016-12-19 15:12:23 -05:00
|
|
|
for (const auto & Entity : m_Entities)
|
2013-04-13 17:02:10 -04:00
|
|
|
{
|
2016-12-19 15:12:23 -05:00
|
|
|
if ((Entity->GetUniqueID() == a_EntityID) && (Entity->IsTicking()))
|
2013-04-13 17:02:10 -04:00
|
|
|
{
|
2017-09-11 17:20:49 -04:00
|
|
|
a_CallbackResult = a_Callback(*Entity);
|
2013-04-13 17:02:10 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} // for itr - m_Entitites[]
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-28 09:40:57 -04:00
|
|
|
bool cChunk::ForEachBlockEntity(cBlockEntityCallback a_Callback)
|
2013-11-20 15:53:29 -05:00
|
|
|
{
|
|
|
|
// The blockentity list is locked by the parent chunkmap's CS
|
2021-03-28 09:40:57 -04:00
|
|
|
|
2017-05-22 16:27:55 -04:00
|
|
|
for (auto & KeyPair : m_BlockEntities)
|
2013-11-20 15:53:29 -05:00
|
|
|
{
|
2021-03-28 09:40:57 -04:00
|
|
|
if (a_Callback(*KeyPair.second))
|
2013-11-20 15:53:29 -05:00
|
|
|
{
|
2021-03-28 09:40:57 -04:00
|
|
|
return false;
|
2013-11-20 15:53:29 -05:00
|
|
|
}
|
2017-05-22 16:27:55 -04:00
|
|
|
}
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2021-03-28 09:40:57 -04:00
|
|
|
return true;
|
2014-03-06 19:30:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-28 09:40:57 -04:00
|
|
|
bool cChunk::DoWithBlockEntityAt(Vector3i a_Position, cBlockEntityCallback a_Callback)
|
2012-06-17 15:58:39 -04:00
|
|
|
{
|
|
|
|
// The blockentity list is locked by the parent chunkmap's CS
|
2021-03-28 09:40:57 -04:00
|
|
|
|
|
|
|
const auto BlockEntity = GetBlockEntityRel(a_Position);
|
|
|
|
if (BlockEntity == nullptr)
|
2017-05-22 16:27:55 -04:00
|
|
|
{
|
2021-03-28 09:40:57 -04:00
|
|
|
return false; // No block entity here
|
2017-05-22 16:27:55 -04:00
|
|
|
}
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2021-03-28 09:40:57 -04:00
|
|
|
const bool Result = a_Callback(*BlockEntity);
|
|
|
|
m_PendingSendBlockEntities.push_back(BlockEntity);
|
|
|
|
MarkDirty();
|
|
|
|
return Result;
|
2012-06-17 15:58:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-02-04 17:15:31 -05:00
|
|
|
void cChunk::GetBlockTypeMeta(Vector3i a_RelPos, BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-02-04 17:15:31 -05:00
|
|
|
a_BlockType = GetBlock(a_RelPos);
|
|
|
|
a_BlockMeta = GetMeta(a_RelPos);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-12-21 09:31:44 -05:00
|
|
|
void cChunk::GetBlockInfo(Vector3i a_RelPos, BLOCKTYPE & a_BlockType, NIBBLETYPE & a_Meta, NIBBLETYPE & a_SkyLight, NIBBLETYPE & a_BlockLight) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-02-04 17:15:31 -05:00
|
|
|
a_BlockType = GetBlock(a_RelPos);
|
2021-03-05 08:03:55 -05:00
|
|
|
a_Meta = m_BlockData.GetMeta(a_RelPos);
|
|
|
|
a_SkyLight = m_LightData.GetSkyLight(a_RelPos);
|
|
|
|
a_BlockLight = m_LightData.GetBlockLight(a_RelPos);
|
2012-10-20 07:40:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-01-17 09:09:25 -05:00
|
|
|
bool cChunk::GetChunkAndRelByAbsolute(const Vector3d & a_Position, cChunk ** a_Chunk, Vector3i & a_Rel)
|
|
|
|
{
|
|
|
|
return GetChunkAndRelByAbsolute(Vector3i(FloorC(a_Position.x), FloorC(a_Position.y), FloorC(a_Position.z)), a_Chunk, a_Rel);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cChunk::GetChunkAndRelByAbsolute(const Vector3i & a_Position, cChunk ** a_Chunk, Vector3i & a_Rel)
|
|
|
|
{
|
|
|
|
*a_Chunk = this->GetNeighborChunk(a_Position.x, a_Position.z);
|
|
|
|
if ((*a_Chunk == nullptr) || !(*a_Chunk)->IsValid())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
a_Rel.x = a_Position.x - (*a_Chunk)->GetPosX() * cChunkDef::Width;
|
|
|
|
a_Rel.y = a_Position.y;
|
|
|
|
a_Rel.z = a_Position.z - (*a_Chunk)->GetPosZ() * cChunkDef::Width;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-26 17:24:36 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-03-01 14:35:29 -05:00
|
|
|
cChunk * cChunk::GetNeighborChunk(int a_BlockX, int a_BlockZ)
|
2013-02-28 02:42:45 -05:00
|
|
|
{
|
|
|
|
// Convert coords to relative, then call the relative version:
|
|
|
|
a_BlockX -= m_PosX * cChunkDef::Width;
|
|
|
|
a_BlockZ -= m_PosZ * cChunkDef::Width;
|
2013-03-01 14:35:29 -05:00
|
|
|
return GetRelNeighborChunk(a_BlockX, a_BlockZ);
|
2013-02-28 02:42:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-03-01 14:35:29 -05:00
|
|
|
cChunk * cChunk::GetRelNeighborChunk(int a_RelX, int a_RelZ)
|
2013-02-28 02:42:45 -05:00
|
|
|
{
|
2014-03-27 13:13:52 -04:00
|
|
|
// If the relative coords are too far away, use the parent's chunk lookup instead:
|
2014-04-08 11:57:35 -04:00
|
|
|
if ((a_RelX < -128) || (a_RelX > 128) || (a_RelZ < -128) || (a_RelZ > 128))
|
2014-03-27 13:13:52 -04:00
|
|
|
{
|
|
|
|
int BlockX = m_PosX * cChunkDef::Width + a_RelX;
|
|
|
|
int BlockZ = m_PosZ * cChunkDef::Width + a_RelZ;
|
2014-07-06 18:50:22 -04:00
|
|
|
int ChunkX, ChunkZ;
|
2021-01-18 11:09:10 -05:00
|
|
|
cChunkDef::BlockToChunk(BlockX, BlockZ, ChunkX, ChunkZ);
|
2020-08-28 16:32:46 -04:00
|
|
|
return m_ChunkMap->FindChunk(ChunkX, ChunkZ);
|
2014-03-27 13:13:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Walk the neighbors:
|
2013-02-28 02:42:45 -05:00
|
|
|
bool ReturnThis = true;
|
|
|
|
if (a_RelX < 0)
|
|
|
|
{
|
2014-10-20 16:55:07 -04:00
|
|
|
if (m_NeighborXM != nullptr)
|
2013-02-28 02:42:45 -05:00
|
|
|
{
|
2013-03-01 14:35:29 -05:00
|
|
|
cChunk * Candidate = m_NeighborXM->GetRelNeighborChunk(a_RelX + cChunkDef::Width, a_RelZ);
|
2014-10-20 16:55:07 -04:00
|
|
|
if (Candidate != nullptr)
|
2013-02-28 02:42:45 -05:00
|
|
|
{
|
|
|
|
return Candidate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Going X first failed, but if the request is crossing Z as well, let's try the Z first later on.
|
|
|
|
ReturnThis = false;
|
|
|
|
}
|
|
|
|
else if (a_RelX >= cChunkDef::Width)
|
|
|
|
{
|
2014-10-20 16:55:07 -04:00
|
|
|
if (m_NeighborXP != nullptr)
|
2013-02-28 02:42:45 -05:00
|
|
|
{
|
2013-03-01 14:35:29 -05:00
|
|
|
cChunk * Candidate = m_NeighborXP->GetRelNeighborChunk(a_RelX - cChunkDef::Width, a_RelZ);
|
2014-10-20 16:55:07 -04:00
|
|
|
if (Candidate != nullptr)
|
2013-02-28 02:42:45 -05:00
|
|
|
{
|
|
|
|
return Candidate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Going X first failed, but if the request is crossing Z as well, let's try the Z first later on.
|
|
|
|
ReturnThis = false;
|
|
|
|
}
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2013-02-28 02:42:45 -05:00
|
|
|
if (a_RelZ < 0)
|
|
|
|
{
|
2014-10-20 16:55:07 -04:00
|
|
|
if (m_NeighborZM != nullptr)
|
2013-02-28 02:42:45 -05:00
|
|
|
{
|
2013-03-01 14:35:29 -05:00
|
|
|
return m_NeighborZM->GetRelNeighborChunk(a_RelX, a_RelZ + cChunkDef::Width);
|
2013-02-28 02:42:45 -05:00
|
|
|
// For requests crossing both X and Z, the X-first way has been already tried
|
|
|
|
}
|
2014-10-20 16:55:07 -04:00
|
|
|
return nullptr;
|
2013-03-03 14:05:11 -05:00
|
|
|
}
|
|
|
|
else if (a_RelZ >= cChunkDef::Width)
|
|
|
|
{
|
2014-10-20 16:55:07 -04:00
|
|
|
if (m_NeighborZP != nullptr)
|
2013-02-28 02:42:45 -05:00
|
|
|
{
|
2013-03-01 14:35:29 -05:00
|
|
|
return m_NeighborZP->GetRelNeighborChunk(a_RelX, a_RelZ - cChunkDef::Width);
|
2013-02-28 02:42:45 -05:00
|
|
|
// For requests crossing both X and Z, the X-first way has been already tried
|
|
|
|
}
|
2014-10-20 16:55:07 -04:00
|
|
|
return nullptr;
|
2013-02-28 02:42:45 -05:00
|
|
|
}
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2014-10-20 16:55:07 -04:00
|
|
|
return (ReturnThis ? this : nullptr);
|
2013-02-28 02:42:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-10-11 05:02:53 -04:00
|
|
|
cChunk * cChunk::GetRelNeighborChunkAdjustCoords(Vector3i & a_RelPos) const
|
2013-05-28 08:18:03 -04:00
|
|
|
{
|
2013-10-22 11:54:09 -04:00
|
|
|
cChunk * ToReturn = const_cast<cChunk *>(this);
|
|
|
|
|
|
|
|
// The most common case: inside this chunk:
|
|
|
|
if (
|
2021-01-18 11:09:10 -05:00
|
|
|
(a_RelPos.x >= 0) && (a_RelPos.x < cChunkDef::Width) &&
|
|
|
|
(a_RelPos.z >= 0) && (a_RelPos.z < cChunkDef::Width)
|
2013-10-22 11:54:09 -04:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return ToReturn;
|
|
|
|
}
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2013-10-22 11:54:09 -04:00
|
|
|
// Request for a different chunk, calculate chunk offset:
|
2019-10-11 05:02:53 -04:00
|
|
|
int RelX = a_RelPos.x; // Make a local copy of the coords (faster access)
|
|
|
|
int RelZ = a_RelPos.z;
|
2021-01-18 11:09:10 -05:00
|
|
|
while ((RelX >= cChunkDef::Width) && (ToReturn != nullptr))
|
2013-05-28 08:18:03 -04:00
|
|
|
{
|
2021-01-18 11:09:10 -05:00
|
|
|
RelX -= cChunkDef::Width;
|
2013-10-22 11:54:09 -04:00
|
|
|
ToReturn = ToReturn->m_NeighborXP;
|
2013-05-28 08:18:03 -04:00
|
|
|
}
|
2014-10-20 16:55:07 -04:00
|
|
|
while ((RelX < 0) && (ToReturn != nullptr))
|
2013-05-28 08:18:03 -04:00
|
|
|
{
|
2021-01-18 11:09:10 -05:00
|
|
|
RelX += cChunkDef::Width;
|
2013-10-22 11:54:09 -04:00
|
|
|
ToReturn = ToReturn->m_NeighborXM;
|
2013-05-28 08:18:03 -04:00
|
|
|
}
|
2021-01-18 11:09:10 -05:00
|
|
|
while ((RelZ >= cChunkDef::Width) && (ToReturn != nullptr))
|
2013-05-28 08:18:03 -04:00
|
|
|
{
|
2021-01-18 11:09:10 -05:00
|
|
|
RelZ -= cChunkDef::Width;
|
2013-10-22 11:54:09 -04:00
|
|
|
ToReturn = ToReturn->m_NeighborZP;
|
2013-05-28 08:18:03 -04:00
|
|
|
}
|
2014-10-20 16:55:07 -04:00
|
|
|
while ((RelZ < 0) && (ToReturn != nullptr))
|
2013-05-28 08:18:03 -04:00
|
|
|
{
|
2021-01-18 11:09:10 -05:00
|
|
|
RelZ += cChunkDef::Width;
|
2013-10-22 11:54:09 -04:00
|
|
|
ToReturn = ToReturn->m_NeighborZM;
|
|
|
|
}
|
2014-10-20 16:55:07 -04:00
|
|
|
if (ToReturn != nullptr)
|
2013-10-22 11:54:09 -04:00
|
|
|
{
|
2019-10-11 05:02:53 -04:00
|
|
|
a_RelPos.x = RelX;
|
|
|
|
a_RelPos.z = RelZ;
|
2013-10-22 11:54:09 -04:00
|
|
|
return ToReturn;
|
2013-05-28 08:18:03 -04:00
|
|
|
}
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2013-10-22 11:54:09 -04:00
|
|
|
// The chunk cannot be walked through neighbors, find it through the chunkmap:
|
2021-01-18 11:09:10 -05:00
|
|
|
int AbsX = a_RelPos.x + m_PosX * cChunkDef::Width;
|
|
|
|
int AbsZ = a_RelPos.z + m_PosZ * cChunkDef::Width;
|
2013-10-22 11:54:09 -04:00
|
|
|
int DstChunkX, DstChunkZ;
|
2021-01-18 11:09:10 -05:00
|
|
|
cChunkDef::BlockToChunk(AbsX, AbsZ, DstChunkX, DstChunkZ);
|
2013-10-22 11:54:09 -04:00
|
|
|
ToReturn = m_ChunkMap->FindChunk(DstChunkX, DstChunkZ);
|
2021-01-18 11:09:10 -05:00
|
|
|
a_RelPos.x = AbsX - DstChunkX * cChunkDef::Width;
|
|
|
|
a_RelPos.z = AbsZ - DstChunkZ * cChunkDef::Width;
|
2013-10-22 11:54:09 -04:00
|
|
|
return ToReturn;
|
2013-05-28 08:18:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-24 03:58:26 -04:00
|
|
|
void cChunk::SendBlockEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cClientHandle & a_Client)
|
|
|
|
{
|
|
|
|
cBlockEntity * Entity = GetBlockEntity(a_BlockX, a_BlockY, a_BlockZ);
|
2014-10-20 16:55:07 -04:00
|
|
|
if (Entity == nullptr)
|
2012-08-24 03:58:26 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Entity->SendTo(a_Client);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-25 13:52:08 -04:00
|
|
|
void cChunk::PositionToWorldPosition(int a_RelX, int a_RelY, int a_RelZ, int & a_BlockX, int & a_BlockY, int & a_BlockZ)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-25 13:52:08 -04:00
|
|
|
a_BlockY = a_RelY;
|
2021-01-18 11:09:10 -05:00
|
|
|
a_BlockX = m_PosX * cChunkDef::Width + a_RelX;
|
|
|
|
a_BlockZ = m_PosZ * cChunkDef::Width + a_RelZ;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-25 13:52:08 -04:00
|
|
|
Vector3i cChunk::PositionToWorldPosition(int a_RelX, int a_RelY, int a_RelZ)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2021-01-18 11:09:10 -05:00
|
|
|
return Vector3i(m_PosX * cChunkDef::Width + a_RelX, a_RelY, m_PosZ * cChunkDef::Width + a_RelZ);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-29 12:44:51 -04:00
|
|
|
NIBBLETYPE cChunk::GetTimeAlteredLight(NIBBLETYPE a_Skylight) const
|
|
|
|
{
|
|
|
|
a_Skylight -= m_World->GetSkyDarkness();
|
2013-10-30 18:14:42 -04:00
|
|
|
// Because NIBBLETYPE is unsigned, we clamp it to 0 .. 15 by checking for values above 15
|
2013-10-29 12:44:51 -04:00
|
|
|
return (a_Skylight < 16)? a_Skylight : 0;
|
|
|
|
}
|