From 7db4e20fd7ee7cf1c25d5c2c5e9e1b1cf97d4c97 Mon Sep 17 00:00:00 2001 From: Debucquoy Anthony tonitch Date: Tue, 26 Sep 2023 23:54:37 +0200 Subject: adding endermite (#5460) * First Draft of adding endermite * Update src/Mobs/Endermite.h Co-authored-by: x12xx12x <44411062+12xx12@users.noreply.github.com> * Adding Protocols + SpawnEgg TODO: don't forget to put the endermite in core plugin for the summon command * Adding endermite to monster.ini * Adding 5% change of spawning endermite when throwing enderpearl * Spawn endermite at last position instead of Hit Position + .cache to .gitignore * fixup! Spawn endermite at last position instead of Hit Position + .cache to .gitignore * destroy endermite if 2 min, not if name is set * Syntax * Adding Enderman targeting endermite + fixing syntax * Fixing compile error + return error [but crash on enderman spawn] * Fix crash but enderman doesn't target * Enderman targeting endermite finished * checking style because i'm a noob at git... * fixup! checking style because i'm a noob at git... * Added endermite egg meta to docs * Final touches Removed unnecesary imports fixed callback to run only in sight distance and actually check sigtlines * Fixed error after not pulling branch --------- Co-authored-by: x12xx12x <44411062+12xx12@users.noreply.github.com> Co-authored-by: Debucquoy --- src/Mobs/AggressiveMonster.cpp | 61 +++++++++++++++++++++++++++++++++++++++--- src/Mobs/AggressiveMonster.h | 14 +++++++--- src/Mobs/CMakeLists.txt | 2 ++ src/Mobs/Enderman.cpp | 23 ++++++++++++++++ src/Mobs/Endermite.cpp | 41 ++++++++++++++++++++++++++++ src/Mobs/Endermite.h | 24 +++++++++++++++++ src/Mobs/IncludeAllMonsters.h | 1 + src/Mobs/Monster.cpp | 7 +++++ 8 files changed, 166 insertions(+), 7 deletions(-) create mode 100644 src/Mobs/Endermite.cpp create mode 100644 src/Mobs/Endermite.h (limited to 'src/Mobs') diff --git a/src/Mobs/AggressiveMonster.cpp b/src/Mobs/AggressiveMonster.cpp index f7392d92e..c93985b90 100644 --- a/src/Mobs/AggressiveMonster.cpp +++ b/src/Mobs/AggressiveMonster.cpp @@ -3,9 +3,9 @@ #include "AggressiveMonster.h" -#include "../World.h" -#include "../Entities/Player.h" -#include "../LineBlockTracer.h" +#include "LineBlockTracer.h" +#include "World.h" +#include "Entities/Player.h" @@ -46,6 +46,61 @@ void cAggressiveMonster::EventSeePlayer(cPlayer * a_Player, cChunk & a_Chunk) +cMonster * cAggressiveMonster::GetMonsterOfTypeInSight(eMonsterType a_MobType, unsigned int a_SightDistance) +{ + + cMonster * FoundTarget = nullptr; + auto MinimumDistance = static_cast(a_SightDistance * a_SightDistance); + + class cCallback : public cBlockTracer::cCallbacks + { + public: + bool OnNextBlock(Vector3i a_BlockPos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, eBlockFace a_EntryFace) override + { + return a_BlockType != E_BLOCK_AIR; + } + }; + + auto Callbacks = cCallback(); + auto Tracer = cLineBlockTracer(*GetWorld(), Callbacks); + + cEntityCallback Callback = [&](cEntity & a_Entity) + { + if (!a_Entity.IsMob()) + { + return false; + } + + auto & Other = dynamic_cast(a_Entity); + if (Other.GetMobType() != a_MobType) + { + return false; + } + + Vector3d MyHeadPosition = GetPosition().addedY(GetHeight()); + Vector3d TargetPosition = Other.GetPosition().addedY(Other.GetHeight()); + double TargetDistance = (MyHeadPosition - TargetPosition).SqrLength(); + + if ( + (MinimumDistance > TargetDistance) && + (TargetDistance < (a_SightDistance * a_SightDistance)) + ) + { + FoundTarget = & Other; + return true; + } + return false; + }; + + cBoundingBox CheckZone(GetPosition().addedXZ(-a_SightDistance, -a_SightDistance), GetPosition().addedXZ(a_SightDistance, a_SightDistance)); + m_World->ForEachEntityInBox(CheckZone, Callback); + return FoundTarget; +} + + + + + void cAggressiveMonster::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) { Super::Tick(a_Dt, a_Chunk); diff --git a/src/Mobs/AggressiveMonster.h b/src/Mobs/AggressiveMonster.h index 8f648eab9..48ed7932e 100644 --- a/src/Mobs/AggressiveMonster.h +++ b/src/Mobs/AggressiveMonster.h @@ -30,12 +30,18 @@ public: virtual void EventSeePlayer(cPlayer * a_Player, cChunk & a_Chunk) override; + /** + * Check if a monster of certain type is in sight + * + * @param a_mobtype the mob type to find + * @param SightDistance max distance to check + * + * @return pointer to the mob found + */ + cMonster * GetMonsterOfTypeInSight(eMonsterType a_mobtype, unsigned int SightDistance=16); + /** Try to perform attack returns true if attack was deemed successful (hit player, fired projectile, creeper exploded, etc.) even if it didn't actually do damage return false if e.g. the mob is still in cooldown from a previous attack */ virtual bool Attack(std::chrono::milliseconds a_Dt); } ; - - - - diff --git a/src/Mobs/CMakeLists.txt b/src/Mobs/CMakeLists.txt index 932b90b29..010b39afa 100644 --- a/src/Mobs/CMakeLists.txt +++ b/src/Mobs/CMakeLists.txt @@ -9,6 +9,7 @@ target_sources( Cow.cpp Creeper.cpp EnderDragon.cpp + Endermite.cpp Enderman.cpp Ghast.cpp Giant.cpp @@ -49,6 +50,7 @@ target_sources( Cow.h Creeper.h EnderDragon.h + Endermite.h Enderman.h Ghast.h Giant.h diff --git a/src/Mobs/Enderman.cpp b/src/Mobs/Enderman.cpp index 656668fb3..3711d7f07 100644 --- a/src/Mobs/Enderman.cpp +++ b/src/Mobs/Enderman.cpp @@ -149,6 +149,29 @@ void cEnderman::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) return; } + if (m_EMState != CHASING) + { + cMonster * EndermiteFound = GetMonsterOfTypeInSight(mtEndermite, 64); + if (EndermiteFound != nullptr) + { + SetTarget(EndermiteFound); + m_EMState = CHASING; + m_bIsScreaming = true; + } + } + else + { + const auto Target = GetTarget(); + if (Target != nullptr) + { + if (!Target->IsTicking()) + { + m_EMState = IDLE; + m_bIsScreaming = false; + } + } + } + PREPARE_REL_AND_CHUNK(GetPosition().Floor(), a_Chunk); if (!RelSuccess) { diff --git a/src/Mobs/Endermite.cpp b/src/Mobs/Endermite.cpp new file mode 100644 index 000000000..3a89de98a --- /dev/null +++ b/src/Mobs/Endermite.cpp @@ -0,0 +1,41 @@ + +#include "Globals.h" + +#include "Endermite.h" + +#include "../World.h" +#include "../Chunk.h" +#include "../Blocks/BlockHandler.h" +#include "../Blocks/BlockInfested.h" + + + + + +cEndermite::cEndermite() : + Super("Endermite", mtEndermite, "entity.endermite.hurt", "entity.endermite.death", "entity.endermite.ambient", 0.4f, 0.3f), + m_Timer(0), + m_Lifetime(2 * 1000 * 60) // 2 minutes (2 * 1000 (mili to sec) * 60 (sec to min) * 2 because tick = 0.5 sec) +{ +} + + + + + +void cEndermite::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) +{ + Super::Tick(a_Dt, a_Chunk); + + // Not destroying the endermite if a name is set + if (m_CustomName.empty()) + { + m_Timer += a_Dt; + // Destroy the endermite after 2 minutes + if (m_Timer > m_Lifetime) + { + Destroy(); + } + + } +} diff --git a/src/Mobs/Endermite.h b/src/Mobs/Endermite.h new file mode 100644 index 000000000..9e0102a07 --- /dev/null +++ b/src/Mobs/Endermite.h @@ -0,0 +1,24 @@ + +#pragma once + +#include "AggressiveMonster.h" + + + +class cEndermite: + public cAggressiveMonster +{ + using Super = cAggressiveMonster; + + // Endermite should despawn in two minutes + std::chrono::milliseconds m_Timer; + std::chrono::milliseconds m_Lifetime; + +public: + + cEndermite(); + + CLASS_PROTODEF(cEndermite) + + void Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override; +} ; diff --git a/src/Mobs/IncludeAllMonsters.h b/src/Mobs/IncludeAllMonsters.h index afb79c97c..f31c761fe 100644 --- a/src/Mobs/IncludeAllMonsters.h +++ b/src/Mobs/IncludeAllMonsters.h @@ -5,6 +5,7 @@ #include "Cow.h" #include "Creeper.h" #include "Enderman.h" +#include "Endermite.h" #include "EnderDragon.h" #include "Ghast.h" #include "Giant.h" diff --git a/src/Mobs/Monster.cpp b/src/Mobs/Monster.cpp index e05264f9f..788d1b66f 100644 --- a/src/Mobs/Monster.cpp +++ b/src/Mobs/Monster.cpp @@ -45,6 +45,7 @@ static const struct {mtCow, "cow", "Cow", "cow"}, {mtCreeper, "creeper", "Creeper", "creeper"}, {mtEnderman, "enderman", "Enderman", "enderman"}, + {mtEndermite, "endermite", "Endermite", "endermite"}, {mtEnderDragon, "enderdragon", "EnderDragon", "ender_dragon"}, {mtGhast, "ghast", "Ghast", "ghast"}, {mtGiant, "giant", "Giant", "giant"}, @@ -653,6 +654,11 @@ void cMonster::KilledBy(TakeDamageInfo & a_TDI) Reward = GetRandomProvider().RandInt(6, 8); break; } + case mtEndermite: + { + Reward = 3; + break; + } case mtBlaze: { Reward = 10; @@ -1286,6 +1292,7 @@ std::unique_ptr cMonster::NewMonsterFromType(eMonsterType a_MobType) case mtCow: return std::make_unique(); case mtCreeper: return std::make_unique(); case mtEnderDragon: return std::make_unique(); + case mtEndermite: return std::make_unique(); case mtEnderman: return std::make_unique(); case mtGhast: return std::make_unique(); case mtGiant: return std::make_unique(); -- cgit v1.2.3