Cuberite
A lightweight, fast and extensible game server for Minecraft
Minecart.h
Go to the documentation of this file.
1 
2 // Minecart.h
3 
4 // Declares the cMinecart class representing a minecart in the world
5 
6 
7 
8 
9 
10 #pragma once
11 
12 #include "Entity.h"
13 #include "../World.h"
14 #include "../UI/WindowOwner.h"
15 
16 
17 
18 
19 
20 class cMinecart :
21  public cEntity
22 {
23  using Super = cEntity;
24 
25 public:
27 
28 
29  enum ePayload
30  {
31  mpNone = 0, // Empty minecart, ridable by player or mobs
32  mpChest = 1, // Minecart-with-chest, can store a grid of 3 * 8 items
33  mpFurnace = 2, // Minecart-with-furnace, can be powered
34  mpTNT = 3, // Minecart-with-TNT, can be blown up with activator rail
35  mpHopper = 5, // Minecart-with-hopper, can be hopper
36  // TODO: Spawner minecarts, (and possibly any block in a minecart with NBT editing)
37  } ;
38 
39  // cEntity overrides:
40  virtual void SpawnOn(cClientHandle & a_ClientHandle) override;
41  virtual void HandlePhysics(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override;
42  virtual bool DoTakeDamage(TakeDamageInfo & TDI) override;
43  virtual void KilledBy(TakeDamageInfo & a_TDI) override;
44  virtual void OnRemoveFromWorld(cWorld & a_World) override;
45  virtual void HandleSpeedFromAttachee(float a_Forward, float a_Sideways) override;
46  int LastDamage(void) const { return m_LastDamage; }
47  ePayload GetPayload(void) const { return m_Payload; }
48 
49 protected:
50 
55 
57  void ApplyAcceleration(Vector3d a_ForwardDirection, double a_Acceleration);
58 
59  cMinecart(ePayload a_Payload, Vector3d a_Pos);
60 
63  void HandleRailPhysics(NIBBLETYPE a_RailMeta, std::chrono::milliseconds a_Dt);
64 
68  void HandlePoweredRailPhysics(NIBBLETYPE a_RailMeta);
69 
73  void HandleDetectorRailPhysics(NIBBLETYPE a_RailMeta, std::chrono::milliseconds a_Dt);
74 
76  virtual void HandleActivatorRailPhysics(NIBBLETYPE a_RailMeta, std::chrono::milliseconds a_Dt);
77 
80  void SnapToRail(NIBBLETYPE a_RailMeta);
82  bool TestBlockCollision(NIBBLETYPE a_RailMeta);
84  bool IsSolidBlockAtPosition(Vector3i a_Offset);
86  bool IsSolidBlockAtOffset(int a_XOffset, int a_YOffset, int a_ZOffset);
87 
88  bool IsBlockCollisionAtOffset(Vector3i a_Offset);
89 
91  bool TestEntityCollision(NIBBLETYPE a_RailMeta);
92 } ;
93 
94 
95 
96 
97 
98 class cRideableMinecart final :
99  public cMinecart
100 {
101  using Super = cMinecart;
102 
103 public:
104 
106 
107  cRideableMinecart(Vector3d a_Pos, const cItem & a_Content, int a_ContentHeight);
108 
109  const cItem & GetContent(void) const { return m_Content; }
110  int GetBlockHeight(void) const { return m_ContentHeight; }
111 
112  // cEntity overrides:
113  virtual void GetDrops(cItems & a_Drops, cEntity * a_Killer = nullptr) override;
114  virtual void OnRightClicked(cPlayer & a_Player) override;
115 
116 protected:
117 
120 } ;
121 
122 
123 
124 
125 
126 class cMinecartWithChest final :
127  public cMinecart,
128  public cItemGrid::cListener,
129  public cEntityWindowOwner
130 {
131  using Super = cMinecart;
132 
133 public:
134 
136 
138 
139  enum
140  {
143  };
144 
145  const cItem & GetSlot(int a_Idx) const { return m_Contents.GetSlot(a_Idx); }
146  void SetSlot(int a_Idx, const cItem & a_Item) { m_Contents.SetSlot(a_Idx, a_Item); }
147 
148 
149 protected:
150 
152  void OpenNewWindow(void);
153 
154  // cItemGrid::cListener overrides:
155  virtual void OnSlotChanged(cItemGrid * a_Grid, int a_SlotNum) override
156  {
157  UNUSED(a_SlotNum);
158  ASSERT(a_Grid == &m_Contents);
159  if (m_World != nullptr)
160  {
161  if (GetWindow() != nullptr)
162  {
164  }
165 
167  }
168  }
169 
170  // cEntity overrides:
171  virtual void GetDrops(cItems & a_Drops, cEntity * a_Killer = nullptr) override;
172  virtual void OnRemoveFromWorld(cWorld & a_World) override;
173  virtual void OnRightClicked(cPlayer & a_Player) override;
174 } ;
175 
176 
177 
178 
179 
180 class cMinecartWithFurnace final :
181  public cMinecart
182 {
183  using Super = cMinecart;
184 
185 public:
186 
188 
190 
191  // cEntity overrides:
192  virtual void GetDrops(cItems & a_Drops, cEntity * a_Killer = nullptr) override;
193  virtual void OnRightClicked(cPlayer & a_Player) override;
194  virtual void Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override;
195 
196  // Set functions.
197  void SetIsFueled(bool a_IsFueled, int a_FueledTimeLeft = -1) {m_IsFueled = a_IsFueled; m_FueledTimeLeft = a_FueledTimeLeft;}
198 
199  // Get functions.
200  int GetFueledTimeLeft(void) const {return m_FueledTimeLeft; }
201  bool IsFueled (void) const {return m_IsFueled;}
202 
203 private:
204 
207 
208 } ;
209 
210 
211 
212 
213 
214 class cMinecartWithTNT final :
215  public cMinecart
216 {
217  using Super = cMinecart;
218 
219 public:
220 
222 
223  cMinecartWithTNT(Vector3d a_Pos);
224  void Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override;
225 
226 private:
228  bool m_isTNTFused = false;
229 
230  virtual void GetDrops(cItems & a_Drops, cEntity * a_Killer = nullptr) override;
231  void HandleActivatorRailPhysics(NIBBLETYPE a_RailMeta, std::chrono::milliseconds a_Dt) override;
232 } ;
233 
234 
235 
236 
237 
238 class cMinecartWithHopper final :
239  public cMinecart
240 {
241  using Super = cMinecart;
242 
243 public:
244 
246 
248 
249 private:
250 
251  virtual void GetDrops(cItems & a_Drops, cEntity * a_Killer = nullptr) override;
252 } ;
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:44
#define CLASS_PROTODEF(classname)
Definition: Entity.h:13
#define ASSERT(x)
Definition: Globals.h:276
#define UNUSED
Definition: Globals.h:72
Definition: Chunk.h:36
Definition: Entity.h:76
int GetChunkZ(void) const
Definition: Entity.h:208
int GetChunkX(void) const
Definition: Entity.h:207
cWorld * m_World
Definition: Entity.h:624
cEntity(eEntityType a_EntityType, Vector3d a_Pos, float a_Width, float a_Height)
Definition: Entity.cpp:37
bool IsSolidBlockAtOffset(int a_XOffset, int a_YOffset, int a_ZOffset)
Tests if a solid block is at a specific offset of the minecart position.
Definition: Minecart.cpp:836
void SnapToRail(NIBBLETYPE a_RailMeta)
Snaps a mincecart to a rail's axis, resetting its speed For curved rails, it changes the cart's direc...
Definition: Minecart.cpp:697
virtual void HandleActivatorRailPhysics(NIBBLETYPE a_RailMeta, std::chrono::milliseconds a_Dt)
Handles activator rails.
Definition: Minecart.cpp:687
cMinecart(ePayload a_Payload, Vector3d a_Pos)
Definition: Minecart.cpp:132
bool TestBlockCollision(NIBBLETYPE a_RailMeta)
Tests if a solid block is in front of a cart, and stops the cart (and returns true) if so; returns fa...
Definition: Minecart.cpp:865
void ApplyAcceleration(Vector3d a_ForwardDirection, double a_Acceleration)
Applies an acceleration to the minecart parallel to a_ForwardDirection but without allowing backward ...
Definition: Minecart.cpp:1316
Vector3i m_DetectorRailPosition
Definition: Minecart.h:53
int LastDamage(void) const
Definition: Minecart.h:46
void HandleDetectorRailPhysics(NIBBLETYPE a_RailMeta, std::chrono::milliseconds a_Dt)
Handles detector rail activation Activates detector rails when a minecart is on them.
Definition: Minecart.cpp:675
bool IsBlockCollisionAtOffset(Vector3i a_Offset)
Definition: Minecart.cpp:845
virtual bool DoTakeDamage(TakeDamageInfo &TDI) override
Makes this entity take damage specified in the a_TDI.
Definition: Minecart.cpp:1249
ePayload
Minecart payload, values correspond to packet subtype.
Definition: Minecart.h:30
@ mpHopper
Definition: Minecart.h:35
@ mpNone
Definition: Minecart.h:31
@ mpChest
Definition: Minecart.h:32
@ mpFurnace
Definition: Minecart.h:33
bool TestEntityCollision(NIBBLETYPE a_RailMeta)
Tests if this mincecart's bounding box is intersecting another entity's bounding box (collision) and ...
Definition: Minecart.cpp:1082
bool IsSolidBlockAtPosition(Vector3i a_Offset)
Tests if there is a block at the specified position which is impassable to minecarts.
Definition: Minecart.cpp:826
ePayload GetPayload(void) const
Definition: Minecart.h:47
virtual void HandleSpeedFromAttachee(float a_Forward, float a_Sideways) override
Definition: Minecart.cpp:1298
virtual void SpawnOn(cClientHandle &a_ClientHandle) override
Descendants override this function to send a command to the specified client to spawn the entity on t...
Definition: Minecart.cpp:150
int m_LastDamage
Definition: Minecart.h:52
virtual void KilledBy(TakeDamageInfo &a_TDI) override
Called when the health drops below zero.
Definition: Minecart.cpp:1273
void HandlePoweredRailPhysics(NIBBLETYPE a_RailMeta)
Handles powered rail physics Each tick, speed up or slow down cart, depending on metadata of rail (po...
Definition: Minecart.cpp:508
ePayload m_Payload
Definition: Minecart.h:51
bool m_bIsOnDetectorRail
Definition: Minecart.h:54
virtual void HandlePhysics(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
Handles the physics of the entity - updates position based on speed, updates speed based on environme...
Definition: Minecart.cpp:160
void HandleRailPhysics(NIBBLETYPE a_RailMeta, std::chrono::milliseconds a_Dt)
Handles physics on normal rails For each tick, slow down on flat rails, speed up or slow down on asce...
Definition: Minecart.cpp:266
virtual void OnRemoveFromWorld(cWorld &a_World) override
Called when the entity is removed from a world.
Definition: Minecart.cpp:1284
virtual void GetDrops(cItems &a_Drops, cEntity *a_Killer=nullptr) override
Returns the list of drops for this pawn when it is killed.
Definition: Minecart.cpp:1350
virtual void OnRightClicked(cPlayer &a_Player) override
Called when the specified player right-clicks this entity.
Definition: Minecart.cpp:1359
cRideableMinecart(Vector3d a_Pos, const cItem &a_Content, int a_ContentHeight)
Definition: Minecart.cpp:1339
const cItem & GetContent(void) const
Definition: Minecart.h:109
int GetBlockHeight(void) const
Definition: Minecart.h:110
virtual void GetDrops(cItems &a_Drops, cEntity *a_Killer=nullptr) override
Returns the list of drops for this pawn when it is killed.
Definition: Minecart.cpp:1405
cItemGrid m_Contents
Definition: Minecart.h:151
virtual void OnSlotChanged(cItemGrid *a_Grid, int a_SlotNum) override
Called whenever a slot changes.
Definition: Minecart.h:155
virtual void OnRemoveFromWorld(cWorld &a_World) override
Called when the entity is removed from a world.
Definition: Minecart.cpp:1415
virtual void OnRightClicked(cPlayer &a_Player) override
Called when the specified player right-clicks this entity.
Definition: Minecart.cpp:1430
cMinecartWithChest(Vector3d a_Pos)
Definition: Minecart.cpp:1393
void OpenNewWindow(void)
Definition: Minecart.cpp:1454
const cItem & GetSlot(int a_Idx) const
Definition: Minecart.h:145
void SetSlot(int a_Idx, const cItem &a_Item)
Definition: Minecart.h:146
void SetIsFueled(bool a_IsFueled, int a_FueledTimeLeft=-1)
Definition: Minecart.h:197
bool IsFueled(void) const
Definition: Minecart.h:201
virtual void OnRightClicked(cPlayer &a_Player) override
Called when the specified player right-clicks this entity.
Definition: Minecart.cpp:1486
int GetFueledTimeLeft(void) const
Definition: Minecart.h:200
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
Definition: Minecart.cpp:1508
virtual void GetDrops(cItems &a_Drops, cEntity *a_Killer=nullptr) override
Returns the list of drops for this pawn when it is killed.
Definition: Minecart.cpp:1477
cMinecartWithFurnace(Vector3d a_Pos)
Definition: Minecart.cpp:1466
void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
Definition: Minecart.cpp:1577
void HandleActivatorRailPhysics(NIBBLETYPE a_RailMeta, std::chrono::milliseconds a_Dt) override
Handles activator rails.
Definition: Minecart.cpp:1551
cMinecartWithTNT(Vector3d a_Pos)
Definition: Minecart.cpp:1542
virtual void GetDrops(cItems &a_Drops, cEntity *a_Killer=nullptr) override
Returns the list of drops for this pawn when it is killed.
Definition: Minecart.cpp:1568
int m_TNTFuseTicksLeft
Definition: Minecart.h:227
cMinecartWithHopper(Vector3d a_Pos)
Definition: Minecart.cpp:1606
virtual void GetDrops(cItems &a_Drops, cEntity *a_Killer=nullptr) override
Returns the list of drops for this pawn when it is killed.
Definition: Minecart.cpp:1618
Definition: Player.h:29
Definition: Item.h:37
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:215
void SetSlot(int a_X, int a_Y, const cItem &a_Item)
Definition: ItemGrid.cpp:121
const cItem & GetSlot(int a_X, int a_Y) const
Definition: ItemGrid.cpp:96
This class is used as a callback for when a slot changes.
Definition: ItemGrid.h:26
void BroadcastWholeWindow(void)
Sends the contents of the whole window to all clients of this window.
Definition: Window.cpp:393
cWindow * GetWindow(void) const
Definition: WindowOwner.h:40
Window owner that is associated with an entity (chest minecart etc.)
Definition: WindowOwner.h:82
Definition: World.h:53
void MarkChunkDirty(int a_ChunkX, int a_ChunkZ)
Definition: World.cpp:2150