Cuberite
A lightweight, fast and extensible game server for Minecraft
Boat.cpp
Go to the documentation of this file.
1 
2 // Boat.cpp
3 
4 // Implements the cBoat class representing a boat in the world
5 
6 #include "Globals.h"
7 #include "Boat.h"
8 #include "../World.h"
9 #include "../ClientHandle.h"
10 #include "Player.h"
11 
12 
13 
14 
15 
16 cBoat::cBoat(Vector3d a_Pos, eMaterial a_Material) :
17  super(etBoat, a_Pos, 0.98, 0.7),
18  m_LastDamage(0), m_ForwardDirection(0),
19  m_DamageTaken(0.0f), m_Material(a_Material),
20  m_RightPaddleUsed(false), m_LeftPaddleUsed(false)
21 {
22  SetMass(20.0f);
23  SetGravity(-16.0f);
24  SetAirDrag(0.05f);
25  SetMaxHealth(6);
26  SetHealth(6);
27 }
28 
29 
30 
31 
32 
33 void cBoat::SpawnOn(cClientHandle & a_ClientHandle)
34 {
35  a_ClientHandle.SendSpawnVehicle(*this, 1);
36 }
37 
38 
39 
40 
41 
43 {
44  m_LastDamage = 10;
45  if (!super::DoTakeDamage(TDI))
46  {
47  return false;
48  }
49 
51 
52  if (GetHealth() <= 0)
53  {
54  if (TDI.Attacker != nullptr)
55  {
56  if (TDI.Attacker->IsPlayer())
57  {
58  cItems Pickups;
59  Pickups.Add(MaterialToItem(m_Material));
60  m_World->SpawnItemPickups(Pickups, GetPosX(), GetPosY(), GetPosZ(), 0, 0, 0, true);
61  }
62  }
63  Destroy(true);
64  }
65  return true;
66 }
67 
68 
69 
70 
71 
73 {
74  super::OnRightClicked(a_Player);
75 
76  if (m_Attachee != nullptr)
77  {
78  if (m_Attachee->GetUniqueID() == a_Player.GetUniqueID())
79  {
80  // This player is already sitting in, they want out.
81  a_Player.Detach();
82  return;
83  }
84 
85  if (m_Attachee->IsPlayer())
86  {
87  // Another player is already sitting in here, cannot attach
88  return;
89  }
90 
91  // Detach whatever is sitting in this boat now:
92  m_Attachee->Detach();
93  }
94 
95  // Attach the player to this boat
96  a_Player.AttachTo(this);
97 }
98 
99 
100 
101 
102 
103 void cBoat::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
104 {
105  super::Tick(a_Dt, a_Chunk);
106  if (!IsTicking())
107  {
108  // The base class tick destroyed us
109  return;
110  }
112 
113  SetSpeed(GetSpeed() * 0.97); // Slowly decrease the speed
114 
115  if ((POSY_TOINT < 0) || (POSY_TOINT >= cChunkDef::Height))
116  {
117  return;
118  }
119 
121  {
122  if (GetSpeedY() < 2)
123  {
124  AddSpeedY(0.2);
125  }
126  }
127 
128  if (GetLastDamage() > 0)
129  {
131  }
132 
133  // Broadcast any changes in position
135 }
136 
137 
138 
139 
140 
141 void cBoat::HandleSpeedFromAttachee(float a_Forward, float a_Sideways)
142 {
143  if (GetSpeed().Length() > 7.5)
144  {
145  return;
146  }
147 
148  Vector3d ToAddSpeed = m_Attachee->GetLookVector() * (a_Sideways * 0.4) ;
149  ToAddSpeed.y = 0;
150 
151  AddSpeed(ToAddSpeed);
152 }
153 
154 
155 
156 
157 
158 void cBoat::SetLastDamage(int TimeSinceLastHit)
159 {
160  m_LastDamage = TimeSinceLastHit;
161 }
162 
163 
164 
165 
166 
167 void cBoat::UpdatePaddles(bool a_RightPaddleUsed, bool a_LeftPaddleUsed)
168 {
169  m_RightPaddleUsed = a_RightPaddleUsed;
170  m_LeftPaddleUsed = a_LeftPaddleUsed;
171 
173 }
174 
175 
176 
177 
178 
180 {
181  switch (a_Item.m_ItemType)
182  {
183  case E_ITEM_BOAT: return bmOak;
184  case E_ITEM_SPRUCE_BOAT: return bmSpruce;
185  case E_ITEM_BIRCH_BOAT: return bmBirch;
186  case E_ITEM_JUNGLE_BOAT: return bmJungle;
187  case E_ITEM_ACACIA_BOAT: return bmAcacia;
188  case E_ITEM_DARK_OAK_BOAT: return bmDarkOak;
189  default:
190  {
191  LOGWARNING("%s: Item type not handled %d.", __FUNCTION__, a_Item.m_ItemType);
192  return cBoat::bmOak;
193  }
194  }
195 }
196 
197 
198 
199 
200 
202 {
203  switch (a_Material)
204  {
205  case bmOak: return "oak";
206  case bmSpruce: return "spruce";
207  case bmBirch: return "birch";
208  case bmJungle: return "jungle";
209  case bmAcacia: return "acacia";
210  case bmDarkOak: return "dark_oak";
211  }
212  UNREACHABLE("Unsupported boat material");
213 }
214 
215 
216 
217 
218 
220 {
221  if (a_Material == "oak")
222  {
223  return bmOak;
224  }
225  else if (a_Material == "spruce")
226  {
227  return bmSpruce;
228  }
229  else if (a_Material == "birch")
230  {
231  return bmBirch;
232  }
233  else if (a_Material == "jungle")
234  {
235  return bmJungle;
236  }
237  else if (a_Material == "acacia")
238  {
239  return bmAcacia;
240  }
241  else if (a_Material == "dark_oak")
242  {
243  return bmDarkOak;
244  }
245  else
246  {
247  return bmOak;
248  }
249 }
250 
251 
252 
253 
254 
256 {
257  switch (a_Material)
258  {
259  case bmOak: return cItem(E_ITEM_BOAT);
260  case bmSpruce: return cItem(E_ITEM_SPRUCE_BOAT);
261  case bmBirch: return cItem(E_ITEM_BIRCH_BOAT);
262  case bmJungle: return cItem(E_ITEM_JUNGLE_BOAT);
263  case bmAcacia: return cItem(E_ITEM_ACACIA_BOAT);
264  case bmDarkOak: return cItem(E_ITEM_DARK_OAK_BOAT);
265  }
266  UNREACHABLE("Unsupported boat material");
267 }
268 
269 
270 
271 
static eMaterial StringToMaterial(const AString &a_Material)
Returns the boat material for the passed string.
Definition: Boat.cpp:219
double GetPosY(void) const
Definition: Entity.h:207
double GetPosX(void) const
Definition: Entity.h:206
BLOCKTYPE GetBlock(Vector3i a_BlockPos)
Returns the block type at the specified position.
Definition: World.h:416
bool m_LeftPaddleUsed
Definition: Boat.h:89
void SetLastDamage(int TimeSinceLastHit)
Definition: Boat.cpp:158
bool IsBlockWater(BLOCKTYPE a_BlockType)
Definition: Defines.h:436
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
Definition: Boat.cpp:103
eMaterial m_Material
Definition: Boat.h:86
virtual bool DoTakeDamage(TakeDamageInfo &a_TDI)
Makes this entity take damage specified in the a_TDI.
Definition: Entity.cpp:384
void UpdatePaddles(bool rightPaddleUsed, bool leftPaddleUsed)
Definition: Boat.cpp:167
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: Boat.cpp:33
virtual void BroadcastEntityMetadata(const cEntity &a_Entity, const cClientHandle *a_Exclude=nullptr) override
int GetLastDamage(void) const
Definition: Boat.h:47
cWorld * m_World
Definition: Entity.h:620
virtual void Detach(void)
Detaches from the currently attached entity, if any.
Definition: Entity.cpp:1988
Definition: Player.h:27
virtual void Detach(void) override
Detaches from the currently attached entity, if any.
Definition: Player.cpp:2875
const Vector3d & GetSpeed(void) const
Exported in ManualBindings.
Definition: Entity.h:310
void SetMass(double a_Mass)
Definition: Entity.cpp:2047
bool m_RightPaddleUsed
Definition: Boat.h:88
Definition: Chunk.h:49
void SpawnItemPickups(const cItems &a_Pickups, Vector3i a_BlockPos, double a_FlyAwaySpeed=1.0, bool a_IsPlayerCreated=false)
Spawns item pickups for each item in the list.
Definition: World.cpp:1936
T y
Definition: Vector3.h:17
virtual void Destroy(bool a_ShouldBroadcast=true)
Destroys the entity and schedules it for memory freeing; if a_ShouldBroadcast is set to true...
Definition: Entity.cpp:219
virtual bool DoTakeDamage(TakeDamageInfo &TDI) override
Makes this entity take damage specified in the a_TDI.
Definition: Boat.cpp:42
static const int Height
Definition: ChunkDef.h:135
virtual void OnRightClicked(cPlayer &a_Player) override
Called when the specified player right-clicks this entity.
Definition: Boat.cpp:72
void SetMaxHealth(float a_MaxHealth)
Sets the maximum value for the health.
Definition: Entity.cpp:1798
static cItem MaterialToItem(eMaterial a_Material)
Returns the boat item of the boat material.
Definition: Boat.cpp:255
Vector3d GetLookVector(void) const
Definition: Entity.cpp:2209
static eMaterial ItemToMaterial(const cItem &a_Item)
Returns the eMaterial that should be used for a boat created from the specified item.
Definition: Boat.cpp:179
void Add(const cItem &a_Item)
Definition: Item.h:254
bool IsTicking(void) const
Returns true if the entity is valid and ticking.
Definition: Entity.cpp:2201
eMaterial
Definition: Boat.h:24
void AddSpeed(double a_AddSpeedX, double a_AddSpeedY, double a_AddSpeedZ)
Definition: Entity.cpp:2136
#define POSX_TOINT
Definition: Entity.h:29
#define POSY_TOINT
Definition: Entity.h:30
float GetHealth(void) const
Returns the health of this entity.
Definition: Entity.h:380
void LOGWARNING(const char *a_Format, fmt::ArgList a_ArgList)
Definition: Logger.cpp:174
void SetGravity(float a_Gravity)
Definition: Entity.h:288
void SetSpeed(double a_SpeedX, double a_SpeedY, double a_SpeedZ)
Sets the speed of the entity, measured in m / sec.
Definition: Entity.cpp:2091
virtual void OnRightClicked(cPlayer &a_Player)
Called when the specified player right-clicks this entity.
Definition: Entity.h:523
void AddSpeedY(double a_AddSpeedY)
Definition: Entity.cpp:2154
std::string AString
Definition: StringUtils.h:13
bool IsPlayer(void) const
Definition: Entity.h:171
double GetSpeedY(void) const
Definition: Entity.h:214
int m_LastDamage
Definition: Boat.h:81
virtual void AttachTo(cEntity *a_AttachTo) override
Attaches to the specified entity; detaches from any previous one first.
Definition: Player.cpp:2858
virtual void HandleSpeedFromAttachee(float a_Forward, float a_Sideways) override
Definition: Boat.cpp:141
void SendSpawnVehicle(const cEntity &a_Vehicle, char a_VehicleType, char a_VehicleSubType=0)
Definition: Entity.h:73
void SetAirDrag(float a_AirDrag)
Definition: Entity.h:292
cEntity * m_Attachee
The entity which is attached to this entity (rider), nullptr if none.
Definition: Entity.h:591
static AString MaterialToString(const eMaterial a_Material)
Returns the eMaterial as string.
Definition: Boat.cpp:201
double GetPosZ(void) const
Definition: Entity.h:208
#define POSZ_TOINT
Definition: Entity.h:31
short m_ItemType
Definition: Item.h:209
void SetHealth(float a_Health)
Sets the health of this entity; doesn&#39;t broadcast any hurt animation.
Definition: Entity.cpp:832
Definition: Item.h:36
#define UNREACHABLE(x)
Use to mark code that should be impossible to reach.
Definition: Globals.h:344
cBoat(Vector3d a_Pos, eMaterial a_Material)
Definition: Boat.cpp:16
cEntity * Attacker
Definition: Entity.h:60
UInt32 GetUniqueID(void) const
Definition: Entity.h:261
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:234
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk)
Definition: Entity.cpp:841
virtual void BroadcastMovementUpdate(const cClientHandle *a_Exclude=nullptr)
Updates clients of changes in the entity.
Definition: Entity.cpp:1878