From 1b97875dd6b9e99afa7a3219aa1120288905b70c Mon Sep 17 00:00:00 2001 From: iProgramInCpp Date: Mon, 7 Aug 2023 23:05:30 +0300 Subject: [PATCH] * Improve the entity ID system, and add priority system. We should no longer have clashes. --- .../renderer/entity/HumanoidMobRenderer.cpp | 9 +++++ .../renderer/entity/HumanoidMobRenderer.hpp | 1 + source/client/renderer/entity/MobRenderer.cpp | 2 +- source/world/entity/Entity.cpp | 33 +++++++++++++++++-- source/world/entity/Entity.hpp | 15 +++++++-- source/world/level/Level.cpp | 15 ++++++++- source/world/particle/Particle.cpp | 5 +++ source/world/particle/Particle.hpp | 1 + source/world/tile/SandTile.cpp | 3 ++ 9 files changed, 78 insertions(+), 6 deletions(-) diff --git a/source/client/renderer/entity/HumanoidMobRenderer.cpp b/source/client/renderer/entity/HumanoidMobRenderer.cpp index 85e76bf..c4f6c7a 100644 --- a/source/client/renderer/entity/HumanoidMobRenderer.cpp +++ b/source/client/renderer/entity/HumanoidMobRenderer.cpp @@ -30,6 +30,7 @@ void HumanoidMobRenderer::additionalRendering(Mob* mob, float f) ItemInstance inst(itemID, 1, 0); glPushMatrix(); + m_pHumanoidModel->m_armL.translateTo(0.0625f); glTranslatef(-0.0625f, 0.4375f, 0.0625f); if (itemID <= C_MAX_TILES && TileRenderer::canRender(Tile::tiles[itemID]->getRenderShape())) @@ -64,6 +65,14 @@ void HumanoidMobRenderer::onGraphicsReset() m_pHumanoidModel->onGraphicsReset(); } +void HumanoidMobRenderer::renderName(Mob* mob, float x, float y, float z) +{ + if (!mob->isPlayer()) return; + Player* player = (Player*)mob; + + renderNameTag(player, player->m_name, x, y, z, player->isSneaking() ? 32 : 64); +} + void HumanoidMobRenderer::renderHand() { m_pHumanoidModel->field_4 = 0; diff --git a/source/client/renderer/entity/HumanoidMobRenderer.hpp b/source/client/renderer/entity/HumanoidMobRenderer.hpp index 9d68f2a..678ddf7 100644 --- a/source/client/renderer/entity/HumanoidMobRenderer.hpp +++ b/source/client/renderer/entity/HumanoidMobRenderer.hpp @@ -16,6 +16,7 @@ public: HumanoidMobRenderer(HumanoidModel*, float); virtual void additionalRendering(Mob*, float) override; virtual void onGraphicsReset() override; + virtual void renderName(Mob*, float, float, float) override; void renderHand(); diff --git a/source/client/renderer/entity/MobRenderer.cpp b/source/client/renderer/entity/MobRenderer.cpp index 408ec2c..8a14245 100644 --- a/source/client/renderer/entity/MobRenderer.cpp +++ b/source/client/renderer/entity/MobRenderer.cpp @@ -156,7 +156,7 @@ void MobRenderer::renderNameTag(Mob* mob, const std::string& str, float x, float glNormal3f(0.0f, 1.0f, 0.0f); // billboard the name towards the camera glRotatef(-m_pDispatcher->m_yaw, 0.0f, 1.0f, 0.0f); - glRotatef(-m_pDispatcher->m_pitch, 1.0f, 0.0f, 0.0f); + glRotatef(m_pDispatcher->m_pitch, 1.0f, 0.0f, 0.0f); glScalef(-0.026667f, -0.026667f, 0.026667f); glDepthMask(false); glDisable(GL_DEPTH_TEST); diff --git a/source/world/entity/Entity.cpp b/source/world/entity/Entity.cpp index 606aff2..36b31de 100644 --- a/source/world/entity/Entity.cpp +++ b/source/world/entity/Entity.cpp @@ -10,7 +10,7 @@ #include "Player.hpp" #include "world/level/Level.hpp" -int Entity::entityCounter; +EntityIDSet Entity::s_EntityIDs; Random Entity::sharedRandom; void Entity::_init() @@ -56,6 +56,7 @@ void Entity::_init() field_D5 = false; field_D6 = true; field_D8 = 1; + m_EntityID = 0; } Entity::Entity(Level* pLevel) @@ -63,7 +64,7 @@ Entity::Entity(Level* pLevel) _init(); m_pLevel = pLevel; - m_EntityID = ++entityCounter; + m_EntityID = allocateEntityId(); setPos(0, 0, 0); } @@ -1064,3 +1065,31 @@ bool Entity::operator==(const Entity& other) const { return m_EntityID == other.m_EntityID; } + +bool Entity::isUnimportant() +{ + return false; +} + +int Entity::allocateEntityId() +{ + int id = 1; + + // TODO: could be improved? While a static monotonically increasing integer + // is okay, as long as entities with non-standard entity IDs start to get + // added you run into issues with clashing IDs, because Level::addEntity + // will actually remove the old entity with the same ID. + while (s_EntityIDs.find(id) != s_EntityIDs.end()) + id++; + + return id; +} + +void Entity::releaseEntityId(int id) +{ + EntityIDSet::iterator it = s_EntityIDs.find(id); + if (it == s_EntityIDs.end()) + return; + + s_EntityIDs.erase(it); +} diff --git a/source/world/entity/Entity.hpp b/source/world/entity/Entity.hpp index bf3bb27..b96e351 100644 --- a/source/world/entity/Entity.hpp +++ b/source/world/entity/Entity.hpp @@ -8,6 +8,7 @@ #pragma once +#include #include "client/common/AABB.hpp" #include "client/common/Vec3.hpp" #include "world/level/Material.hpp" @@ -39,19 +40,22 @@ struct EntityPos EntityPos() { m_yaw = 0, m_pitch = 0; - m_bHasRot, m_bHasPos; + m_bHasRot = false, m_bHasPos = false; }; EntityPos(const Vec3& pos) { m_pos = pos; + m_yaw = m_pitch = 0; m_bHasPos = true; + m_bHasRot = false; } EntityPos(float yaw, float pitch) { m_yaw = yaw; m_pitch = pitch; + m_bHasPos = false; m_bHasRot = true; } @@ -65,6 +69,8 @@ struct EntityPos } }; +typedef std::unordered_set EntityIDSet; + class Entity { private: @@ -134,6 +140,7 @@ public: virtual void markHurt(); virtual void burn(int); virtual void lavaHurt(); + virtual bool isUnimportant(); int hashCode(); @@ -147,8 +154,12 @@ public: (m_pos.z - z) * (m_pos.z - z); } +private: + int allocateEntityId(); + void releaseEntityId(int); + static EntityIDSet s_EntityIDs; + //static int entityCounter; public: - static int entityCounter; static Random sharedRandom; Vec3 m_pos; diff --git a/source/world/level/Level.cpp b/source/world/level/Level.cpp index c81c5ab..3bdd252 100644 --- a/source/world/level/Level.cpp +++ b/source/world/level/Level.cpp @@ -279,6 +279,14 @@ Material* Level::getMaterial(int x, int y, int z) Entity* Level::getEntity(int id) { + // always prioritize players!! + for (auto it = m_entities.begin(); it != m_entities.end(); it++) + { + Entity* pEnt = *it; + if (pEnt->isPlayer() && pEnt->m_EntityID == id) + return pEnt; + } + for (auto it = m_entities.begin(); it != m_entities.end(); it++) { Entity* pEnt = *it; @@ -1062,7 +1070,12 @@ bool Level::addEntity(Entity* pEnt) { Entity* pOldEnt = getEntity(pEnt->hashCode()); if (pOldEnt) - removeEntity(pOldEnt); + { + if (pOldEnt->isUnimportant()) + removeEntity(pOldEnt); + else + LogMsg("Clash of entity IDs! (%d) This could go bad", pEnt->hashCode()); + } //@NOTE: useless Mth::floor() calls Mth::floor(pEnt->m_pos.x / 16); diff --git a/source/world/particle/Particle.cpp b/source/world/particle/Particle.cpp index 4445cb6..bcd88c4 100644 --- a/source/world/particle/Particle.cpp +++ b/source/world/particle/Particle.cpp @@ -52,6 +52,11 @@ int Particle::getParticleTexture() return PT_PARTICLES; } +bool Particle::isUnimportant() +{ + return true; +} + Particle* Particle::scale(float f) { setSize(0.2f * f, 0.2f * f); diff --git a/source/world/particle/Particle.hpp b/source/world/particle/Particle.hpp index 5c32bca..d24ec4d 100644 --- a/source/world/particle/Particle.hpp +++ b/source/world/particle/Particle.hpp @@ -36,6 +36,7 @@ public: virtual void render(Tesselator&, float, float, float, float, float, float); virtual int getParticleTexture(); + virtual bool isUnimportant(); //TODO: addAdditonalSaveData //TODO: readAdditionalSaveData //TODO: defineSynchedData diff --git a/source/world/tile/SandTile.cpp b/source/world/tile/SandTile.cpp index ddd9ab3..356d4bd 100644 --- a/source/world/tile/SandTile.cpp +++ b/source/world/tile/SandTile.cpp @@ -81,6 +81,9 @@ bool SandTile::isFree(Level* level, int x, int y, int z) void SandTile::tick(Level* level, int x, int y, int z, Random* random) { + if (level->field_11) + return; + checkSlide(level, x, y, z); }