Cuberite
A lightweight, fast and extensible game server for Minecraft
SnowGolem.cpp
Go to the documentation of this file.
1 
2 #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
3 
4 #include "Chunk.h"
5 #include "SnowGolem.h"
6 #include "../BlockInfo.h"
7 #include "../World.h"
8 #include "../Entities/ThrownSnowballEntity.h"
9 
10 
11 
12 
13 
15  Super("SnowGolem", mtSnowGolem, "entity.snowman.hurt", "entity.snowman.death", "entity.snowman.ambient", 0.7f, 1.9f)
16 {
17 }
18 
19 
20 
21 
22 
23 void cSnowGolem::GetDrops(cItems & a_Drops, cEntity * a_Killer)
24 {
25  UNUSED(a_Killer);
26  AddRandomDropItem(a_Drops, 0, 15, E_ITEM_SNOWBALL);
27 }
28 
29 
30 
31 
32 
33 void cSnowGolem::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
34 {
35  Super::Tick(a_Dt, a_Chunk);
36  if (!IsTicking())
37  {
38  // The base class tick destroyed us
39  return;
40  }
41 
42  PREPARE_REL_AND_CHUNK(GetPosition().Floor(), a_Chunk);
43  if (!RelSuccess)
44  {
45  return;
46  }
47 
48  if (IsBiomeNoDownfall(Chunk->GetBiomeAt(Rel.x, Rel.z)))
49  {
51  }
52  else if (const auto Below = Rel.addedY(-1); Below.y >= 0)
53  {
54  if ((Chunk->GetBlock(Rel) == E_BLOCK_AIR) && cBlockInfo::IsSolid(Chunk->GetBlock(Below)))
55  {
56  Chunk->SetBlock(Rel, E_BLOCK_SNOW, 0);
57  }
58  }
59 }
60 
61 
62 
63 
64 
65 bool cSnowGolem::Attack(std::chrono::milliseconds a_Dt)
66 {
67  UNUSED(a_Dt);
68 
69  // Comment inherited from skeletons
70  StopMovingToPosition(); // Todo handle this in a better way, the snowman does some uneeded recalcs due to inStateChasing
71 
72  if ((GetTarget() != nullptr) && (m_AttackCoolDownTicksLeft == 0))
73  {
74  auto & Random = GetRandomProvider();
75  Vector3d Inaccuracy = Vector3d(Random.RandReal<double>(-0.75, 0.75), Random.RandReal<double>(-0.75, 0.75), Random.RandReal<double>(-0.75, 0.75));
76 
77  // The projectile is launched from the head
78  const auto HeadPos = GetPosition().addedY(1.5);
79  // It aims around the head / chest
80  const auto TargetPos = GetTarget()->GetPosition().addedY(GetTarget()->GetHeight() * 0.75);
81  // With this data, we can calculate the speed
82  const auto Speed = (TargetPos + Inaccuracy - HeadPos) * 5;
83 
84  auto Snowball = std::make_unique<cThrownSnowballEntity>(this, HeadPos, Speed);
85  auto SnowballPtr = Snowball.get();
86  if (!SnowballPtr->Initialize(std::move(Snowball), *GetWorld()))
87  {
88  return false;
89  }
90 
92  return true;
93  }
94  return false;
95 }
bool IsBiomeNoDownfall(EMCSBiome a_Biome)
Returns true if the biome has no downfall - deserts and savannas.
Definition: BiomeDef.cpp:142
@ E_BLOCK_AIR
Definition: BlockType.h:10
@ E_BLOCK_SNOW
Definition: BlockType.h:92
@ E_ITEM_SNOWBALL
Definition: BlockType.h:376
#define PREPARE_REL_AND_CHUNK(Position, OriginalChunk)
Definition: Chunk.h:32
@ dtEnvironment
Definition: Defines.h:265
MTRand & GetRandomProvider()
Returns the current thread's random number source.
Definition: FastRandom.cpp:12
#define UNUSED
Definition: Globals.h:72
@ mtSnowGolem
Definition: MonsterTypes.h:62
@ Snowball
Vector3< double > Vector3d
Definition: Vector3.h:485
static bool IsSolid(BLOCKTYPE Block)
Is this block solid (player cannot walk through)?
Definition: BlockInfo.cpp:892
Definition: Chunk.h:36
Definition: Entity.h:76
virtual double GetKnockbackAmountAgainst(const cEntity &a_Receiver)
Returns the knockback amount that the currently equipped items would cause to a_Receiver on a hit.
Definition: Entity.cpp:827
bool IsTicking(void) const
Returns true if the entity is valid and ticking.
Definition: Entity.cpp:2259
void TakeDamage(cEntity &a_Attacker)
Makes this pawn take damage from an attack by a_Attacker.
Definition: Entity.cpp:272
float GetHeight(void) const
Definition: Entity.h:193
const Vector3d & GetPosition(void) const
Exported in ManualBindings.
Definition: Entity.h:297
virtual int GetRawDamageAgainst(const cEntity &a_Receiver)
Returns the hitpoints that this pawn can deal to a_Receiver using its equipped items.
Definition: Entity.cpp:614
cWorld * GetWorld(void) const
Definition: Entity.h:190
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:215
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
void ResetAttackCooldown()
Definition: Monster.cpp:937
void AddRandomDropItem(cItems &a_Drops, unsigned int a_Min, unsigned int a_Max, short a_Item, short a_ItemHealth=0)
Adds a random number of a_Item between a_Min and a_Max to itemdrops a_Drops.
Definition: Monster.cpp:1518
cPawn * GetTarget()
Returns the current target.
Definition: Monster.cpp:1243
int m_AttackCoolDownTicksLeft
Definition: Monster.h:313
void StopMovingToPosition()
Stops pathfinding.
Definition: Monster.cpp:255
virtual bool Attack(std::chrono::milliseconds a_Dt) override
Try to perform attack returns true if attack was deemed successful (hit player, fired projectile,...
Definition: SnowGolem.cpp:65
virtual void GetDrops(cItems &a_Drops, cEntity *a_Killer=nullptr) override
Returns the list of drops for this pawn when it is killed.
Definition: SnowGolem.cpp:23
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
Definition: SnowGolem.cpp:33
Vector3< T > addedY(T a_AddY) const
Returns a copy of this vector moved by the specified amount on the y axis.
Definition: Vector3.h:314