Cuberite
A lightweight, fast and extensible game server for Minecraft
Simulator.cpp
Go to the documentation of this file.
1 
2 #include "Globals.h"
3 
4 #include "../World.h"
5 #include "../Defines.h"
6 #include "../Chunk.h"
7 #include "../Cuboid.h"
8 
9 #ifdef __clang__
10  #pragma clang diagnostic ignored "-Wweak-template-vtables"
11 #endif // __clang__
12 
13 
14 
15 #include "Simulator.h"
16 
17 
18 
19 
20 
21 void cSimulator::WakeUp(Vector3i a_Block, cChunk * a_Chunk)
22 {
23  AddBlock(a_Block, a_Chunk);
24  AddBlock(a_Block + Vector3i(-1, 0, 0), a_Chunk->GetNeighborChunk(a_Block.x - 1, a_Block.z));
25  AddBlock(a_Block + Vector3i( 1, 0, 0), a_Chunk->GetNeighborChunk(a_Block.x + 1, a_Block.z));
26  AddBlock(a_Block + Vector3i(0, 0, -1), a_Chunk->GetNeighborChunk(a_Block.x, a_Block.z - 1));
27  AddBlock(a_Block + Vector3i(0, 0, 1), a_Chunk->GetNeighborChunk(a_Block.x, a_Block.z + 1));
28  if (a_Block.y > 0)
29  {
30  AddBlock(a_Block + Vector3i(0, -1, 0), a_Chunk);
31  }
32  if (a_Block.y < cChunkDef::Height - 1)
33  {
34  AddBlock(a_Block + Vector3i(0, 1, 0), a_Chunk);
35  }
36 }
37 
38 
39 
40 
41 
42 void cSimulator::WakeUpArea(const cCuboid & a_Area)
43 {
44  cCuboid area(a_Area);
45  area.Sort();
46  area.Expand(1, 1, 1, 1, 1, 1); // Expand the area to contain the neighbors, too.
47  area.ClampY(0, cChunkDef::Height - 1);
48 
49  // Add all blocks, in a per-chunk manner:
50  cChunkCoords ChunkStart = cChunkDef::BlockToChunk(area.p1);
51  cChunkCoords ChunkEnd = cChunkDef::BlockToChunk(area.p2);
52  for (int cz = ChunkStart.m_ChunkZ; cz <= ChunkEnd.m_ChunkZ; ++cz)
53  {
54  for (int cx = ChunkStart.m_ChunkX; cx <= ChunkEnd.m_ChunkX; ++cx)
55  {
56  m_World.DoWithChunk(cx, cz, [this, &area](cChunk & a_CBChunk) -> bool
57  {
58  if (!a_CBChunk.IsValid())
59  {
60  LOGWARNING("%s: Trying to wake up inside a non-valid chunk [%d, %d]. Ignoring.",
61  __FUNCTION__, a_CBChunk.GetPosX(), a_CBChunk.GetPosZ()
62  );
63  return true;
64  }
65  int startX = std::max(area.p1.x, a_CBChunk.GetPosX() * cChunkDef::Width);
66  int startZ = std::max(area.p1.z, a_CBChunk.GetPosZ() * cChunkDef::Width);
67  int endX = std::min(area.p2.x, a_CBChunk.GetPosX() * cChunkDef::Width + cChunkDef::Width - 1);
68  int endZ = std::min(area.p2.z, a_CBChunk.GetPosZ() * cChunkDef::Width + cChunkDef::Width - 1);
69  for (int y = area.p1.y; y <= area.p2.y; ++y)
70  {
71  for (int z = startZ; z <= endZ; ++z)
72  {
73  for (int x = startX; x <= endX; ++x)
74  {
75  AddBlock({x, y, z}, &a_CBChunk);
76  } // for x
77  } // for z
78  } // for y
79  return true;
80  } // lambda
81  ); // DoWithChunk
82  } // for cx
83  } // for cz
84 }
85 
86 
87 
88 
void Sort(void)
Definition: Cuboid.cpp:23
bool IsValid(void) const
Returns true iff the chunk block data is valid (loaded / generated)
Definition: Chunk.h:72
T x
Definition: Vector3.h:17
static const int Width
Definition: ChunkDef.h:134
virtual void AddBlock(Vector3i a_Block, cChunk *a_Chunk)=0
Called to simulate a new block.
Vector3i p1
Definition: Cuboid.h:13
bool DoWithChunk(int a_ChunkX, int a_ChunkZ, cChunkCallback a_Callback)
Calls the callback for the chunk specified, with ChunkMapCS locked.
Definition: World.cpp:1558
void WakeUpArea(const cCuboid &a_Area)
Does the same processing as WakeUp, but for all blocks within the specified area. ...
Definition: Simulator.cpp:42
Definition: Chunk.h:49
cWorld & m_World
Definition: Simulator.h:60
T y
Definition: Vector3.h:17
T z
Definition: Vector3.h:17
static const int Height
Definition: ChunkDef.h:135
static void BlockToChunk(int a_X, int a_Z, int &a_ChunkX, int &a_ChunkZ)
Converts absolute block coords to chunk coords:
Definition: ChunkDef.h:234
int GetPosX(void) const
Definition: Chunk.h:150
void WakeUp(Vector3i a_Block, cChunk *a_Chunk)
Called when a block changes.
Definition: Simulator.cpp:21
void LOGWARNING(const char *a_Format, fmt::ArgList a_ArgList)
Definition: Logger.cpp:174
Definition: Cuboid.h:9
int m_ChunkZ
Definition: ChunkDef.h:60
int m_ChunkX
Definition: ChunkDef.h:59
void Expand(int a_SubMinX, int a_AddMaxX, int a_SubMinY, int a_AddMaxY, int a_SubMinZ, int a_AddMaxZ)
Expands the cuboid by the specified amount in each direction.
Definition: Cuboid.cpp:84
int GetPosZ(void) const
Definition: Chunk.h:151
cChunk * GetNeighborChunk(int a_BlockX, int a_BlockZ)
Returns the chunk into which the specified block belongs, by walking the neighbors.
Definition: Chunk.cpp:2279
void ClampY(int a_MinY, int a_MaxY)
Clamps both Y coords to the specified range.
Definition: Cuboid.cpp:173
Vector3< int > Vector3i
Definition: Vector3.h:447
Vector3i p2
Definition: Cuboid.h:13