2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-09-25 05:13:59 -04:00
|
|
|
#include "ChunkDef.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-10-19 06:46:25 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-09-26 13:13:19 -04:00
|
|
|
class cChunk;
|
2020-09-23 11:06:27 -04:00
|
|
|
class cItems;
|
2012-06-14 09:06:06 -04:00
|
|
|
class cPlayer;
|
2014-09-26 13:13:19 -04:00
|
|
|
class cWorld;
|
2020-04-03 17:23:38 -04:00
|
|
|
class cBlockEntity;
|
|
|
|
|
|
|
|
using OwnedBlockEntity = std::unique_ptr<cBlockEntity>;
|
|
|
|
using cBlockEntities = std::unordered_map<size_t, OwnedBlockEntity>;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-05-25 07:59:13 -04:00
|
|
|
// tolua_begin
|
2012-06-14 09:06:06 -04:00
|
|
|
class cBlockEntity
|
|
|
|
{
|
|
|
|
protected:
|
2021-01-02 08:50:34 -05:00
|
|
|
|
|
|
|
cBlockEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld * a_World);
|
2013-04-06 17:21:57 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
public:
|
2020-10-20 07:28:45 -04:00
|
|
|
|
2013-05-25 07:59:13 -04:00
|
|
|
// tolua_end
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2021-01-02 08:50:34 -05:00
|
|
|
virtual ~cBlockEntity() = default; // force a virtual destructor in all descendants
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2017-06-15 09:32:33 -04:00
|
|
|
/** Makes an exact copy of this block entity, except for its m_World (set to nullptr), and at a new position.
|
|
|
|
Uses CopyFrom() to copy the properties. */
|
2020-04-03 17:23:38 -04:00
|
|
|
OwnedBlockEntity Clone(Vector3i a_Pos);
|
2017-06-15 09:32:33 -04:00
|
|
|
|
2020-09-23 11:06:27 -04:00
|
|
|
/** Returns the contents of this block entity that it would drop if broken.
|
2020-09-24 06:59:02 -04:00
|
|
|
Note that the block handler will usually handle pickups for the block itself, in addition to any items returned here. */
|
2020-09-23 11:06:27 -04:00
|
|
|
virtual cItems ConvertToPickups() const;
|
|
|
|
|
2017-06-15 09:32:33 -04:00
|
|
|
/** Copies all properties of a_Src into this entity, except for its m_World and location.
|
|
|
|
Each non-abstract descendant should override to copy its specific properties, and call
|
|
|
|
Super::CopyFrom(a_Src) to copy the common ones. */
|
|
|
|
virtual void CopyFrom(const cBlockEntity & a_Src);
|
|
|
|
|
2021-01-02 08:50:34 -05:00
|
|
|
/** Creates a new block entity for the specified block type at the specified absolute pos.
|
|
|
|
If a_World is valid, then the entity is created bound to that world
|
|
|
|
Returns nullptr for unknown block types. */
|
|
|
|
static OwnedBlockEntity CreateByBlockType(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld * a_World = nullptr);
|
|
|
|
|
2021-04-30 09:23:46 -04:00
|
|
|
/** Called when this block entity's associated block is destroyed.
|
|
|
|
It is guaranteed that this function is called before OnRemoveFromWorld. */
|
2021-01-02 08:50:34 -05:00
|
|
|
virtual void Destroy();
|
|
|
|
|
|
|
|
/** Returns true if the specified blocktype is supposed to have an associated block entity. */
|
|
|
|
static bool IsBlockEntityBlockType(BLOCKTYPE a_BlockType);
|
|
|
|
|
2021-04-30 09:23:46 -04:00
|
|
|
/** Called when the block entity object is added to a world. */
|
|
|
|
virtual void OnAddToWorld(cWorld & a_World, cChunk & a_Chunk);
|
|
|
|
|
|
|
|
/** Called when the block entity object is removed from a world.
|
|
|
|
This occurs when the chunk it resides in is unloaded, or when the associated block is destroyed.
|
|
|
|
If it is the latter, Destroy() is guaranteed to be called first. */
|
2021-01-02 08:50:34 -05:00
|
|
|
virtual void OnRemoveFromWorld();
|
|
|
|
|
|
|
|
/** Sends the packet defining the block entity to the client specified.
|
|
|
|
To send to all eligible clients, use cWorld::BroadcastBlockEntity() */
|
|
|
|
virtual void SendTo(cClientHandle & a_Client) = 0;
|
|
|
|
|
|
|
|
/** Updates the internally stored position.
|
|
|
|
Note that this should not ever be used for world-contained block entities, it is meant only for when BEs in a cBlockArea are manipulated.
|
|
|
|
Asserts that the block entity is not assigned to a world. */
|
|
|
|
void SetPos(Vector3i a_NewPos);
|
|
|
|
|
|
|
|
void SetWorld(cWorld * a_World);
|
|
|
|
|
|
|
|
/** Ticks the entity; returns true if the chunk should be marked as dirty as a result of this ticking. By default does nothing. */
|
|
|
|
virtual bool Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk);
|
|
|
|
|
|
|
|
/** Called when a player uses this entity; should open the UI window.
|
|
|
|
returns true if the use was successful, return false to use the block as a "normal" block */
|
|
|
|
virtual bool UsedBy(cPlayer * a_Player) = 0;
|
|
|
|
|
2013-11-15 04:15:27 -05:00
|
|
|
// tolua_begin
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// Position, in absolute block coordinates:
|
2019-09-29 08:59:24 -04:00
|
|
|
Vector3i GetPos() const { return m_Pos; }
|
|
|
|
int GetPosX() const { return m_Pos.x; }
|
|
|
|
int GetPosY() const { return m_Pos.y; }
|
|
|
|
int GetPosZ() const { return m_Pos.z; }
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2019-09-29 08:59:24 -04:00
|
|
|
Vector3i GetRelPos() const { return Vector3i(m_RelX, m_Pos.y, m_RelZ); }
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2019-09-29 08:59:24 -04:00
|
|
|
BLOCKTYPE GetBlockType() const { return m_BlockType; }
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2019-09-29 08:59:24 -04:00
|
|
|
cWorld * GetWorld() const { return m_World; }
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2019-09-29 08:59:24 -04:00
|
|
|
int GetChunkX() const { return FAST_FLOOR_DIV(m_Pos.x, cChunkDef::Width); }
|
2020-09-27 13:02:16 -04:00
|
|
|
int GetChunkZ() const { return FAST_FLOOR_DIV(m_Pos.z, cChunkDef::Width); }
|
2019-09-29 08:59:24 -04:00
|
|
|
|
|
|
|
int GetRelX() const { return m_RelX; }
|
|
|
|
int GetRelZ() const { return m_RelZ; }
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-05-25 07:59:13 -04:00
|
|
|
// tolua_end
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2019-09-29 08:59:24 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
protected:
|
2019-09-29 08:59:24 -04:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Position in absolute block coordinates */
|
2019-09-29 08:59:24 -04:00
|
|
|
Vector3i m_Pos;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Position relative to the chunk, used to speed up ticking */
|
2013-05-28 14:50:44 -04:00
|
|
|
int m_RelX, m_RelZ;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2017-06-15 09:32:33 -04:00
|
|
|
/** The blocktype representing this particular instance in the world.
|
|
|
|
Mainly used for multi-block-type entities, such as furnaces / lit furnaces. */
|
2013-04-06 17:21:57 -04:00
|
|
|
BLOCKTYPE m_BlockType;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2017-06-15 09:32:33 -04:00
|
|
|
/** The block meta representing this particular instance in the world
|
|
|
|
Mainly used for directional entities, such as dispensers. */
|
|
|
|
NIBBLETYPE m_BlockMeta;
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
cWorld * m_World;
|
2013-05-25 07:59:13 -04:00
|
|
|
} ; // tolua_export
|