From 79217ca8e380a931f352b03242b5d68f97d3ddbf Mon Sep 17 00:00:00 2001 From: orng Date: Fri, 27 Mar 2026 21:11:11 -0500 Subject: [PATCH] refactor: modernize `AABB` class --- Minecraft.Client/Network/PlayerConnection.cpp | 11 +- .../ApplySchematicRuleDefinition.cpp | 4 +- .../Common/GameRules/ConsoleSchematicFile.cpp | 4 +- .../Common/Tutorial/AreaConstraint.cpp | 6 +- .../Platform/Common/Tutorial/AreaHint.cpp | 2 +- .../Common/Tutorial/ChangeStateConstraint.cpp | 6 +- Minecraft.Client/Rendering/Chunk.cpp | 4 +- Minecraft.Client/Rendering/GameRenderer.cpp | 17 +- Minecraft.Client/Rendering/LevelRenderer.cpp | 9 +- Minecraft.World/AI/Goals/AvoidPlayerGoal.cpp | 3 +- Minecraft.World/AI/Goals/BreedGoal.cpp | 3 +- Minecraft.World/AI/Goals/FollowParentGoal.cpp | 4 +- Minecraft.World/AI/Goals/HurtByTargetGoal.cpp | 7 +- Minecraft.World/AI/Goals/LookAtPlayerGoal.cpp | 3 +- Minecraft.World/AI/Goals/MakeLoveGoal.cpp | 3 +- .../AI/Goals/NearestAttackableTargetGoal.cpp | 3 +- Minecraft.World/AI/Goals/OfferFlowerGoal.cpp | 5 +- Minecraft.World/AI/Goals/PlayGoal.cpp | 3 +- Minecraft.World/AI/Goals/TakeFlowerGoal.cpp | 3 +- Minecraft.World/Blocks/Tile.cpp | 2 +- .../Blocks/TileEntities/BeaconTileEntity.cpp | 9 +- Minecraft.World/Entities/Entity.cpp | 96 +++---- Minecraft.World/Entities/HangingEntity.cpp | 6 +- Minecraft.World/Entities/ItemEntity.cpp | 3 +- Minecraft.World/Entities/LargeFireball.cpp | 2 +- Minecraft.World/Entities/LivingEntity.cpp | 15 +- Minecraft.World/Entities/MinecartHopper.cpp | 5 +- Minecraft.World/Entities/Mob.cpp | 6 +- Minecraft.World/Entities/Mobs/Animal.cpp | 31 ++- Minecraft.World/Entities/Mobs/Arrow.cpp | 11 +- Minecraft.World/Entities/Mobs/Boat.cpp | 3 +- .../Entities/Mobs/DragonFireball.cpp | 4 +- Minecraft.World/Entities/Mobs/EnderDragon.cpp | 24 +- Minecraft.World/Entities/Mobs/EntityHorse.cpp | 6 +- Minecraft.World/Entities/Mobs/Fireball.cpp | 9 +- Minecraft.World/Entities/Mobs/FishingHook.cpp | 9 +- Minecraft.World/Entities/Mobs/Ghast.cpp | 6 +- Minecraft.World/Entities/Mobs/Minecart.cpp | 3 +- Minecraft.World/Entities/Mobs/PigZombie.cpp | 5 +- Minecraft.World/Entities/Mobs/Squid.cpp | 3 +- .../Entities/Mobs/ThrownPotion.cpp | 4 +- Minecraft.World/Entities/Mobs/WitherBoss.cpp | 4 +- Minecraft.World/Entities/Throwable.cpp | 9 +- Minecraft.World/Entities/WitherSkull.cpp | 2 +- Minecraft.World/Items/BoatItem.cpp | 15 +- Minecraft.World/Level/BaseMobSpawner.cpp | 17 +- Minecraft.World/Level/Level.cpp | 7 +- Minecraft.World/Level/LevelChunk.cpp | 6 +- Minecraft.World/Player/Player.cpp | 6 +- Minecraft.World/Util/AABB.cpp | 242 ++++++++---------- Minecraft.World/Util/AABB.h | 44 ++-- Minecraft.World/Util/Vec3.cpp | 3 +- .../WorldGen/Structures/StructureStart.cpp | 2 +- 53 files changed, 362 insertions(+), 357 deletions(-) diff --git a/Minecraft.Client/Network/PlayerConnection.cpp b/Minecraft.Client/Network/PlayerConnection.cpp index 9f5e861e8..5a4d5d3d7 100644 --- a/Minecraft.Client/Network/PlayerConnection.cpp +++ b/Minecraft.Client/Network/PlayerConnection.cpp @@ -272,8 +272,9 @@ void PlayerConnection::handleMovePlayer( */ float r = 1 / 16.0f; + AABB shrunk = player->bb->shrink(r, r, r); bool oldOk = - level->getCubes(player, player->bb->copy()->shrink(r, r, r)) + level->getCubes(player, &shrunk) ->empty(); if (player->onGround && !packet->onGround && yDist > 0) { @@ -324,17 +325,19 @@ void PlayerConnection::handleMovePlayer( } player->absMoveTo(xt, yt, zt, yRotT, xRotT); + // TODO: check if this can be elided + shrunk = player->bb->shrink(r, r, r); bool newOk = - level->getCubes(player, player->bb->copy()->shrink(r, r, r)) + level->getCubes(player, &shrunk) ->empty(); if (oldOk && (fail || !newOk) && !player->isSleeping()) { teleport(xLastOk, yLastOk, zLastOk, yRotT, xRotT); return; } - AABB* testBox = player->bb->copy()->grow(r, r, r)->expand(0, -0.55, 0); + AABB testBox = (*player->bb).grow(r, r, r).expand(0, -0.55, 0); // && server.level.getCubes(player, testBox).size() == 0 if (!server->isFlightAllowed() && !player->gameMode->isCreative() && - !level->containsAnyBlocks(testBox) && !player->isAllowedToFly()) { + !level->containsAnyBlocks(&testBox) && !player->isAllowedToFly()) { if (oyDist >= (-0.5f / 16.0f)) { aboveGroundTickCount++; if (aboveGroundTickCount > 80) { diff --git a/Minecraft.Client/Platform/Common/GameRules/ApplySchematicRuleDefinition.cpp b/Minecraft.Client/Platform/Common/GameRules/ApplySchematicRuleDefinition.cpp index b2304fbdd..dd91c027c 100644 --- a/Minecraft.Client/Platform/Common/GameRules/ApplySchematicRuleDefinition.cpp +++ b/Minecraft.Client/Platform/Common/GameRules/ApplySchematicRuleDefinition.cpp @@ -163,7 +163,7 @@ void ApplySchematicRuleDefinition::processSchematic(AABB* chunkBox, m_schematic = m_levelGenOptions->getSchematicFile(m_schematicName); if (m_locationBox == NULL) updateLocationBox(); - if (chunkBox->intersects(m_locationBox)) { + if (chunkBox->intersects(*m_locationBox)) { m_locationBox->y1 = std::min((double)Level::maxBuildHeight, m_locationBox->y1); @@ -207,7 +207,7 @@ void ApplySchematicRuleDefinition::processSchematicLighting(AABB* chunkBox, m_schematic = m_levelGenOptions->getSchematicFile(m_schematicName); if (m_locationBox == NULL) updateLocationBox(); - if (chunkBox->intersects(m_locationBox)) { + if (chunkBox->intersects(*m_locationBox)) { m_locationBox->y1 = std::min((double)Level::maxBuildHeight, m_locationBox->y1); diff --git a/Minecraft.Client/Platform/Common/GameRules/ConsoleSchematicFile.cpp b/Minecraft.Client/Platform/Common/GameRules/ConsoleSchematicFile.cpp index 217a7ffb8..2409b12c9 100644 --- a/Minecraft.Client/Platform/Common/GameRules/ConsoleSchematicFile.cpp +++ b/Minecraft.Client/Platform/Common/GameRules/ConsoleSchematicFile.cpp @@ -464,7 +464,7 @@ void ConsoleSchematicFile::applyTileEntities(LevelChunk* chunk, AABB* chunkBox, targetZ); Vec3 pos(targetX, targetY, targetZ); - if (chunkBox->containsIncludingLowerBound(&pos)) { + if (chunkBox->containsIncludingLowerBound(pos)) { std::shared_ptr teCopy = chunk->getTileEntity( (int)targetX & 15, (int)targetY & 15, (int)targetZ & 15); @@ -511,7 +511,7 @@ void ConsoleSchematicFile::applyTileEntities(LevelChunk* chunk, AABB* chunkBox, // Add 0.01 as the AABB::contains function returns false if a value is // <= the lower bound Vec3 pos(targetX + 0.01, targetY + 0.01, targetZ + 0.01); - if (!chunkBox->containsIncludingLowerBound(&pos)) { + if (!chunkBox->containsIncludingLowerBound(pos)) { ++it; continue; } diff --git a/Minecraft.Client/Platform/Common/Tutorial/AreaConstraint.cpp b/Minecraft.Client/Platform/Common/Tutorial/AreaConstraint.cpp index 016cc71ff..271275948 100644 --- a/Minecraft.Client/Platform/Common/Tutorial/AreaConstraint.cpp +++ b/Minecraft.Client/Platform/Common/Tutorial/AreaConstraint.cpp @@ -25,8 +25,10 @@ AreaConstraint::~AreaConstraint() { bool AreaConstraint::isConstraintSatisfied(int iPad) { Minecraft* minecraft = Minecraft::GetInstance(); + + // TODO: check if this can be elided Vec3 ipad_player = minecraft->localplayers[iPad]->getPos(1); - return messageArea->contains(&ipad_player) == contains; + return messageArea->contains(ipad_player) == contains; } bool AreaConstraint::isConstraintRestrictive(int iPad) { @@ -40,7 +42,7 @@ bool AreaConstraint::canMoveToPosition(double xo, double yo, double zo, Vec3 targetPos(xt, yt, zt); Minecraft* minecraft = Minecraft::GetInstance(); - if (movementArea->contains(&targetPos) == contains) { + if (movementArea->contains(targetPos) == contains) { return true; } Vec3 origPos(xo, yo, zo); diff --git a/Minecraft.Client/Platform/Common/Tutorial/AreaHint.cpp b/Minecraft.Client/Platform/Common/Tutorial/AreaHint.cpp index 87dc976bf..1eb6c1017 100644 --- a/Minecraft.Client/Platform/Common/Tutorial/AreaHint.cpp +++ b/Minecraft.Client/Platform/Common/Tutorial/AreaHint.cpp @@ -28,7 +28,7 @@ int AreaHint::tick() { if ((m_displayState == e_Tutorial_State_Any || m_tutorial->getCurrentState() == m_displayState) && - m_hintNeeded && area->contains(&player_pos) == contains) { + m_hintNeeded && area->contains(player_pos) == contains) { if (m_completeState == e_Tutorial_State_None) { m_hintNeeded = false; } else if (m_tutorial->isStateCompleted(m_completeState)) { diff --git a/Minecraft.Client/Platform/Common/Tutorial/ChangeStateConstraint.cpp b/Minecraft.Client/Platform/Common/Tutorial/ChangeStateConstraint.cpp index e1f93eecb..9efbd42c9 100644 --- a/Minecraft.Client/Platform/Common/Tutorial/ChangeStateConstraint.cpp +++ b/Minecraft.Client/Platform/Common/Tutorial/ChangeStateConstraint.cpp @@ -85,9 +85,11 @@ void ChangeStateConstraint::tick(int iPad) { break; } } + + // TODO: check if this can be elided Vec3 ipad_player = minecraft->localplayers[iPad]->getPos(1); if (!m_bHasChanged && inASourceState && - movementArea->contains(&ipad_player) == contains) { + movementArea->contains(ipad_player) == contains) { m_bHasChanged = true; m_changedFromState = m_tutorial->getCurrentState(); m_tutorial->changeTutorialState(m_targetState); @@ -125,7 +127,7 @@ void ChangeStateConstraint::tick(int iPad) { } } } else if (m_bHasChanged && - movementArea->contains(&ipad_player) != contains) { + movementArea->contains(ipad_player) != contains) { m_bHasChanged = false; m_tutorial->changeTutorialState(m_changedFromState); diff --git a/Minecraft.Client/Rendering/Chunk.cpp b/Minecraft.Client/Rendering/Chunk.cpp index fa311aed4..03dc40b8f 100644 --- a/Minecraft.Client/Rendering/Chunk.cpp +++ b/Minecraft.Client/Rendering/Chunk.cpp @@ -509,9 +509,9 @@ void Chunk::rebuild() { // 4J MGH - added this to take the bound from the value calc'd in the // tesselator if (bb) { - bb->set(bounds.boundingBox[0], bounds.boundingBox[1], + *bb = {bounds.boundingBox[0], bounds.boundingBox[1], bounds.boundingBox[2], bounds.boundingBox[3], - bounds.boundingBox[4], bounds.boundingBox[5]); + bounds.boundingBox[4], bounds.boundingBox[5]}; } delete tileRenderer; diff --git a/Minecraft.Client/Rendering/GameRenderer.cpp b/Minecraft.Client/Rendering/GameRenderer.cpp index d228b1030..b2f589b88 100644 --- a/Minecraft.Client/Rendering/GameRenderer.cpp +++ b/Minecraft.Client/Rendering/GameRenderer.cpp @@ -309,11 +309,12 @@ void GameRenderer::pick(float a) { to = to.add(from.x, from.y, from.z); hovered = nullptr; float overlap = 1; - std::vector >* objects = mc->level->getEntities( - mc->cameraTargetPlayer, - mc->cameraTargetPlayer->bb - ->expand(b.x * (range), b.y * (range), b.z * (range)) - ->grow(overlap, overlap, overlap)); + AABB grown = mc->cameraTargetPlayer->bb + ->expand(b.x * (range), b.y * (range), b.z * (range)) + .grow(overlap, overlap, overlap); + + std::vector >* objects = + mc->level->getEntities(mc->cameraTargetPlayer, &grown); double nearest = dist; AUTO_VAR(itEnd, objects->end()); @@ -322,9 +323,9 @@ void GameRenderer::pick(float a) { if (!e->isPickable()) continue; float rr = e->getPickRadius(); - AABB* bb = e->bb->grow(rr, rr, rr); - HitResult* p = bb->clip(&from, &to); - if (bb->contains(&from)) { + AABB bb = e->bb->grow(rr, rr, rr); + HitResult* p = bb.clip(from, to); + if (bb.contains(from)) { if (0 < nearest || nearest == 0) { hovered = e; nearest = 0; diff --git a/Minecraft.Client/Rendering/LevelRenderer.cpp b/Minecraft.Client/Rendering/LevelRenderer.cpp index 722fcfbef..d412a7ede 100644 --- a/Minecraft.Client/Rendering/LevelRenderer.cpp +++ b/Minecraft.Client/Rendering/LevelRenderer.cpp @@ -2479,10 +2479,13 @@ void LevelRenderer::renderHitOutline(std::shared_ptr player, double xo = player->xOld + (player->x - player->xOld) * a; double yo = player->yOld + (player->y - player->yOld) * a; double zo = player->zOld + (player->z - player->zOld) * a; - render(Tile::tiles[tileId] + + AABB bb = Tile::tiles[tileId] ->getTileAABB(level[iPad], h->x, h->y, h->z) ->grow(ss, ss, ss) - ->cloneMove(-xo, -yo, -zo)); + .move(-xo, -yo, -zo); + + render(&bb); } glDepthMask(true); glEnable(GL_TEXTURE_2D); @@ -3974,7 +3977,7 @@ void LevelRenderer::DestroyedTileManager::addAABBs(Level* level, AABB* box, // interested in, add them to the output list, making a temp // AABB copy so that we can destroy our own copy without // worrying about the lifespan of the copy we've passed out - if (m_destroyedTiles[i]->boxes[j]->intersects(box)) { + if (m_destroyedTiles[i]->boxes[j]->intersects(*box)) { boxes->push_back( AABB::newTemp(m_destroyedTiles[i]->boxes[j]->x0, m_destroyedTiles[i]->boxes[j]->y0, diff --git a/Minecraft.World/AI/Goals/AvoidPlayerGoal.cpp b/Minecraft.World/AI/Goals/AvoidPlayerGoal.cpp index 59045d029..cf595ea05 100644 --- a/Minecraft.World/AI/Goals/AvoidPlayerGoal.cpp +++ b/Minecraft.World/AI/Goals/AvoidPlayerGoal.cpp @@ -53,9 +53,10 @@ bool AvoidPlayerGoal::canUse() { mob->level->getNearestPlayer(mob->shared_from_this(), maxDist)); if (toAvoid.lock() == NULL) return false; } else { + AABB grown_bb = mob->bb->grow(maxDist, 3, maxDist); std::vector >* entities = mob->level->getEntitiesOfClass( - avoidType, mob->bb->grow(maxDist, 3, maxDist), entitySelector); + avoidType, &grown_bb, entitySelector); if (entities->empty()) { delete entities; return false; diff --git a/Minecraft.World/AI/Goals/BreedGoal.cpp b/Minecraft.World/AI/Goals/BreedGoal.cpp index 25281c61d..1ec4f59fc 100644 --- a/Minecraft.World/AI/Goals/BreedGoal.cpp +++ b/Minecraft.World/AI/Goals/BreedGoal.cpp @@ -48,8 +48,9 @@ void BreedGoal::tick() { std::shared_ptr BreedGoal::getFreePartner() { float r = 8; + AABB grown_bb = animal->bb->grow(r, r, r); std::vector >* others = - level->getEntitiesOfClass(typeid(*animal), animal->bb->grow(r, r, r)); + level->getEntitiesOfClass(typeid(*animal), &grown_bb); double dist = std::numeric_limits::max(); std::shared_ptr partner = nullptr; for (AUTO_VAR(it, others->begin()); it != others->end(); ++it) { diff --git a/Minecraft.World/AI/Goals/FollowParentGoal.cpp b/Minecraft.World/AI/Goals/FollowParentGoal.cpp index d60dd34a9..0a740a35c 100644 --- a/Minecraft.World/AI/Goals/FollowParentGoal.cpp +++ b/Minecraft.World/AI/Goals/FollowParentGoal.cpp @@ -16,9 +16,9 @@ FollowParentGoal::FollowParentGoal(Animal* animal, double speedModifier) { bool FollowParentGoal::canUse() { if (animal->getAge() >= 0) return false; + AABB grown_bb = animal->bb->grow(8, 4, 8); std::vector >* parents = - animal->level->getEntitiesOfClass(typeid(*animal), - animal->bb->grow(8, 4, 8)); + animal->level->getEntitiesOfClass(typeid(*animal), &grown_bb); std::shared_ptr closest = nullptr; double closestDistSqr = std::numeric_limits::max(); diff --git a/Minecraft.World/AI/Goals/HurtByTargetGoal.cpp b/Minecraft.World/AI/Goals/HurtByTargetGoal.cpp index 7bee32298..00a3e838d 100644 --- a/Minecraft.World/AI/Goals/HurtByTargetGoal.cpp +++ b/Minecraft.World/AI/Goals/HurtByTargetGoal.cpp @@ -22,11 +22,10 @@ void HurtByTargetGoal::start() { if (alertSameType) { double within = getFollowDistance(); + AABB mob_bb = AABB(mob->x, mob->y, mob->z, mob->x + 1, mob->y + 1, mob->z + 1).grow(within, 4, within); std::vector >* nearby = mob->level->getEntitiesOfClass( - typeid(*mob), AABB::newTemp(mob->x, mob->y, mob->z, mob->x + 1, - mob->y + 1, mob->z + 1) - ->grow(within, 4, within)); + typeid(*mob), &mob_bb); for (AUTO_VAR(it, nearby->begin()); it != nearby->end(); ++it) { std::shared_ptr other = std::dynamic_pointer_cast(*it); @@ -40,4 +39,4 @@ void HurtByTargetGoal::start() { } TargetGoal::start(); -} \ No newline at end of file +} diff --git a/Minecraft.World/AI/Goals/LookAtPlayerGoal.cpp b/Minecraft.World/AI/Goals/LookAtPlayerGoal.cpp index 32cd4d1d6..b073212c2 100644 --- a/Minecraft.World/AI/Goals/LookAtPlayerGoal.cpp +++ b/Minecraft.World/AI/Goals/LookAtPlayerGoal.cpp @@ -37,8 +37,9 @@ bool LookAtPlayerGoal::canUse() { lookAt = mob->level->getNearestPlayer(mob->shared_from_this(), lookDistance); } else { + AABB mob_bb = mob->bb->grow(lookDistance, 3, lookDistance); lookAt = std::weak_ptr(mob->level->getClosestEntityOfClass( - lookAtType, mob->bb->grow(lookDistance, 3, lookDistance), + lookAtType, &mob_bb, mob->shared_from_this())); } return lookAt.lock() != NULL; diff --git a/Minecraft.World/AI/Goals/MakeLoveGoal.cpp b/Minecraft.World/AI/Goals/MakeLoveGoal.cpp index 733036801..0357f7113 100644 --- a/Minecraft.World/AI/Goals/MakeLoveGoal.cpp +++ b/Minecraft.World/AI/Goals/MakeLoveGoal.cpp @@ -29,8 +29,9 @@ bool MakeLoveGoal::canUse() { if (village.lock() == NULL) return false; if (!villageNeedsMoreVillagers()) return false; + AABB villager_bb = villager->bb->grow(8, 3, 8); std::shared_ptr mate = level->getClosestEntityOfClass( - typeid(Villager), villager->bb->grow(8, 3, 8), + typeid(Villager), &villager_bb, villager->shared_from_this()); if (mate == NULL) return false; diff --git a/Minecraft.World/AI/Goals/NearestAttackableTargetGoal.cpp b/Minecraft.World/AI/Goals/NearestAttackableTargetGoal.cpp index a67f91cf9..938102d3e 100644 --- a/Minecraft.World/AI/Goals/NearestAttackableTargetGoal.cpp +++ b/Minecraft.World/AI/Goals/NearestAttackableTargetGoal.cpp @@ -55,9 +55,10 @@ bool NearestAttackableTargetGoal::canUse() { return false; double within = getFollowDistance(); + AABB mob_bb = mob->bb->grow(within, 4, within); std::vector >* entities = mob->level->getEntitiesOfClass( - targetType, mob->bb->grow(within, 4, within), selector); + targetType, &mob_bb, selector); bool result = false; if (entities != NULL && !entities->empty()) { diff --git a/Minecraft.World/AI/Goals/OfferFlowerGoal.cpp b/Minecraft.World/AI/Goals/OfferFlowerGoal.cpp index 01c1b3dbf..902b221ee 100644 --- a/Minecraft.World/AI/Goals/OfferFlowerGoal.cpp +++ b/Minecraft.World/AI/Goals/OfferFlowerGoal.cpp @@ -15,9 +15,10 @@ OfferFlowerGoal::OfferFlowerGoal(VillagerGolem* golem) { bool OfferFlowerGoal::canUse() { if (!golem->level->isDay()) return false; if (golem->getRandom()->nextInt(8000) != 0) return false; + AABB golem_bb = golem->bb->grow(6, 2, 6); villager = std::weak_ptr(std::dynamic_pointer_cast( golem->level->getClosestEntityOfClass(typeid(Villager), - golem->bb->grow(6, 2, 6), + &golem_bb, golem->shared_from_this()))); return villager.lock() != NULL; } @@ -39,4 +40,4 @@ void OfferFlowerGoal::stop() { void OfferFlowerGoal::tick() { golem->getLookControl()->setLookAt(villager.lock(), 30, 30); --_tick; -} \ No newline at end of file +} diff --git a/Minecraft.World/AI/Goals/PlayGoal.cpp b/Minecraft.World/AI/Goals/PlayGoal.cpp index 0259d5c93..550f3bb6e 100644 --- a/Minecraft.World/AI/Goals/PlayGoal.cpp +++ b/Minecraft.World/AI/Goals/PlayGoal.cpp @@ -23,9 +23,10 @@ bool PlayGoal::canUse() { if (mob->getAge() >= 0) return false; if (mob->getRandom()->nextInt(400) != 0) return false; + AABB mob_bb = mob->bb->grow(6, 3, 6); std::vector >* children = mob->level->getEntitiesOfClass(typeid(Villager), - mob->bb->grow(6, 3, 6)); + &mob_bb); double closestDistSqr = std::numeric_limits::max(); // for (Entity c : children) for (AUTO_VAR(it, children->begin()); it != children->end(); ++it) { diff --git a/Minecraft.World/AI/Goals/TakeFlowerGoal.cpp b/Minecraft.World/AI/Goals/TakeFlowerGoal.cpp index e7c5410c4..e4e9ed042 100644 --- a/Minecraft.World/AI/Goals/TakeFlowerGoal.cpp +++ b/Minecraft.World/AI/Goals/TakeFlowerGoal.cpp @@ -22,9 +22,10 @@ bool TakeFlowerGoal::canUse() { if (villager->getAge() >= 0) return false; if (!villager->level->isDay()) return false; + AABB villager_bb = villager->bb->grow(6, 2, 6); std::vector >* golems = villager->level->getEntitiesOfClass(typeid(VillagerGolem), - villager->bb->grow(6, 2, 6)); + &villager_bb); if (golems->size() == 0) { delete golems; return false; diff --git a/Minecraft.World/Blocks/Tile.cpp b/Minecraft.World/Blocks/Tile.cpp index d110d7e8a..a895238ef 100644 --- a/Minecraft.World/Blocks/Tile.cpp +++ b/Minecraft.World/Blocks/Tile.cpp @@ -2016,7 +2016,7 @@ AABB* Tile::getTileAABB(Level* level, int x, int y, int z) { void Tile::addAABBs(Level* level, int x, int y, int z, AABB* box, AABBList* boxes, std::shared_ptr source) { AABB* aabb = getAABB(level, x, y, z); - if (aabb != NULL && box->intersects(aabb)) boxes->push_back(aabb); + if (aabb != NULL && box->intersects(*aabb)) boxes->push_back(aabb); } AABB* Tile::getAABB(Level* level, int x, int y, int z) { diff --git a/Minecraft.World/Blocks/TileEntities/BeaconTileEntity.cpp b/Minecraft.World/Blocks/TileEntities/BeaconTileEntity.cpp index 5b160f153..738303f3c 100644 --- a/Minecraft.World/Blocks/TileEntities/BeaconTileEntity.cpp +++ b/Minecraft.World/Blocks/TileEntities/BeaconTileEntity.cpp @@ -70,11 +70,10 @@ void BeaconTileEntity::applyEffects() { baseAmp = 1; } - AABB* bb = AABB::newTemp(x, y, z, x + 1, y + 1, z + 1) - ->grow(range, range, range); - bb->y1 = level->getMaxBuildHeight(); + AABB bb = AABB(x, y, z, x + 1, y + 1, z + 1).grow(range, range, range); + bb.y1 = level->getMaxBuildHeight(); std::vector >* players = - level->getEntitiesOfClass(typeid(Player), bb); + level->getEntitiesOfClass(typeid(Player), &bb); for (AUTO_VAR(it, players->begin()); it != players->end(); ++it) { std::shared_ptr player = std::dynamic_pointer_cast(*it); @@ -299,4 +298,4 @@ bool BeaconTileEntity::canPlaceItem(int slot, std::shared_ptr item) { return (item->id == Item::emerald_Id || item->id == Item::diamond_Id || item->id == Item::goldIngot_Id || item->id == Item::ironIngot_Id); -} \ No newline at end of file +} diff --git a/Minecraft.World/Entities/Entity.cpp b/Minecraft.World/Entities/Entity.cpp index 51ed60e8f..191789eb8 100644 --- a/Minecraft.World/Entities/Entity.cpp +++ b/Minecraft.World/Entities/Entity.cpp @@ -463,8 +463,8 @@ void Entity::setPos(double x, double y, double z) { this->z = z; float w = bbWidth / 2; float h = bbHeight; - bb->set(x - w, y - heightOffset + ySlideOffset, z - w, x + w, - y - heightOffset + ySlideOffset + h, z + w); + *bb = {x - w, y - heightOffset + ySlideOffset, z - w, x + w, + y - heightOffset + ySlideOffset + h, z + w}; } void Entity::turn(float xo, float yo) { @@ -618,18 +618,18 @@ void Entity::clearFire() { onFire = 0; } void Entity::outOfWorld() { remove(); } bool Entity::isFree(float xa, float ya, float za, float grow) { - AABB* box = bb->grow(grow, grow, grow)->cloneMove(xa, ya, za); - AABBList* aABBs = level->getCubes(shared_from_this(), box); + AABB box = bb->grow(grow, grow, grow).move(xa, ya, za); + AABBList* aABBs = level->getCubes(shared_from_this(), &box); if (!aABBs->empty()) return false; - if (level->containsAnyLiquid(box)) return false; + if (level->containsAnyLiquid(&box)) return false; return true; } bool Entity::isFree(double xa, double ya, double za) { - AABB* box = bb->cloneMove(xa, ya, za); - AABBList* aABBs = level->getCubes(shared_from_this(), box); + AABB box = bb->move(xa, ya, za); + AABBList* aABBs = level->getCubes(shared_from_this(), &box); if (!aABBs->empty()) return false; - if (level->containsAnyLiquid(box)) return false; + if (level->containsAnyLiquid(&box)) return false; return true; } @@ -637,7 +637,7 @@ void Entity::move(double xa, double ya, double za, bool noEntityCubes) // 4J - added noEntityCubes parameter { if (noPhysics) { - bb->move(xa, ya, za); + *bb = bb->move(xa, ya, za); x = (bb->x0 + bb->x1) / 2.0f; y = bb->y0 + heightOffset - ySlideOffset; z = (bb->z0 + bb->z1) / 2.0f; @@ -665,16 +665,17 @@ void Entity::move(double xa, double ya, double za, double yaOrg = ya; double zaOrg = za; - AABB* bbOrg = bb->copy(); + AABB bbOrg = *bb; bool isPlayerSneaking = onGround && isSneaking() && instanceof(eTYPE_PLAYER); if (isPlayerSneaking) { double d = 0.05; + + AABB translated_bb = bb->move(xa, -1.0, 0.0); while (xa != 0 && - level->getCubes(shared_from_this(), bb->cloneMove(xa, -1.0, 0)) - ->empty()) { + level->getCubes(shared_from_this(), &translated_bb)->empty()) { if (xa < d && xa >= -d) xa = 0; else if (xa > 0) @@ -683,9 +684,10 @@ void Entity::move(double xa, double ya, double za, xa += d; xaOrg = xa; } + + translated_bb = bb->move(0, -1.0, za); while (za != 0 && - level->getCubes(shared_from_this(), bb->cloneMove(0, -1.0, za)) - ->empty()) { + level->getCubes(shared_from_this(), &translated_bb)->empty()) { if (za < d && za >= -d) za = 0; else if (za > 0) @@ -694,9 +696,10 @@ void Entity::move(double xa, double ya, double za, za += d; zaOrg = za; } + + translated_bb = bb->move(xa, -1.0, za); while (xa != 0 && za != 0 && - level->getCubes(shared_from_this(), bb->cloneMove(xa, -1.0, za)) - ->empty()) { + level->getCubes(shared_from_this(), &translated_bb)->empty()) { if (xa < d && xa >= -d) xa = 0; else if (xa > 0) @@ -714,8 +717,9 @@ void Entity::move(double xa, double ya, double za, } } - AABBList* aABBs = level->getCubes( - shared_from_this(), bb->expand(xa, ya, za), noEntityCubes, true); + AABB expanded = bb->expand(xa, ya, za); + AABBList* aABBs = + level->getCubes(shared_from_this(), &expanded, noEntityCubes, true); // LAND FIRST, then x and z AUTO_VAR(itEndAABB, aABBs->end()); @@ -729,8 +733,8 @@ void Entity::move(double xa, double ya, double za, // But if we don't have the chunk data then all the collision info will // be incorrect as well for (AUTO_VAR(it, aABBs->begin()); it != itEndAABB; it++) - ya = (*it)->clipYCollide(bb, ya); - bb->move(0, ya, 0); + ya = (*it)->clipYCollide(*bb, ya); + *bb = bb->move(0, ya, 0); } if (!slide && yaOrg != ya) { @@ -741,9 +745,9 @@ void Entity::move(double xa, double ya, double za, itEndAABB = aABBs->end(); for (AUTO_VAR(it, aABBs->begin()); it != itEndAABB; it++) - xa = (*it)->clipXCollide(bb, xa); + xa = (*it)->clipXCollide(*bb, xa); - bb->move(xa, 0, 0); + *bb = bb->move(xa, 0, 0); if (!slide && xaOrg != xa) { xa = ya = za = 0; @@ -751,8 +755,8 @@ void Entity::move(double xa, double ya, double za, itEndAABB = aABBs->end(); for (AUTO_VAR(it, aABBs->begin()); it != itEndAABB; it++) - za = (*it)->clipZCollide(bb, za); - bb->move(0, 0, za); + za = (*it)->clipZCollide(*bb, za); + *bb = bb->move(0, 0, za); if (!slide && zaOrg != za) { xa = ya = za = 0; @@ -768,15 +772,14 @@ void Entity::move(double xa, double ya, double za, ya = footSize; za = zaOrg; - AABB* normal = bb->copy(); - bb->set(bbOrg); + AABB normal = *bb; + *bb = bbOrg; // 4J - added extra expand, as if we don't move up by footSize by // hitting a block above us, then overall we could be trying to move as // much as footSize downwards, so we'd better include cubes under our // feet in this list of things we might possibly collide with - aABBs = level->getCubes(shared_from_this(), - bb->expand(xa, ya, za)->expand(0, -ya, 0), - false, true); + AABB expanded = bb->expand(xa, ya, za).expand(0, -ya, 0); + aABBs = level->getCubes(shared_from_this(), &expanded, false, true); // LAND FIRST, then x and z itEndAABB = aABBs->end(); @@ -786,8 +789,8 @@ void Entity::move(double xa, double ya, double za, // all! But if we don't have the chunk data then all the collision // info will be incorrect as well for (AUTO_VAR(it, aABBs->begin()); it != itEndAABB; it++) - ya = (*it)->clipYCollide(bb, ya); - bb->move(0, ya, 0); + ya = (*it)->clipYCollide(*bb, ya); + *bb = bb->move(0, ya, 0); } if (!slide && yaOrg != ya) { @@ -796,8 +799,8 @@ void Entity::move(double xa, double ya, double za, itEndAABB = aABBs->end(); for (AUTO_VAR(it, aABBs->begin()); it != itEndAABB; it++) - xa = (*it)->clipXCollide(bb, xa); - bb->move(xa, 0, 0); + xa = (*it)->clipXCollide(*bb, xa); + *bb = bb->move(xa, 0, 0); if (!slide && xaOrg != xa) { xa = ya = za = 0; @@ -805,8 +808,8 @@ void Entity::move(double xa, double ya, double za, itEndAABB = aABBs->end(); for (AUTO_VAR(it, aABBs->begin()); it != itEndAABB; it++) - za = (*it)->clipZCollide(bb, za); - bb->move(0, 0, za); + za = (*it)->clipZCollide(*bb, za); + *bb = bb->move(0, 0, za); if (!slide && zaOrg != za) { xa = ya = za = 0; @@ -819,15 +822,15 @@ void Entity::move(double xa, double ya, double za, // LAND FIRST, then x and z itEndAABB = aABBs->end(); for (AUTO_VAR(it, aABBs->begin()); it != itEndAABB; it++) - ya = (*it)->clipYCollide(bb, ya); - bb->move(0, ya, 0); + ya = (*it)->clipYCollide(*bb, ya); + *bb = bb->move(0, ya, 0); } if (xaN * xaN + zaN * zaN >= xa * xa + za * za) { xa = xaN; ya = yaN; za = zaN; - bb->set(normal); + *bb = normal; } } @@ -888,7 +891,8 @@ void Entity::move(double xa, double ya, double za, checkInsideTiles(); bool water = isInWaterOrRain(); - if (level->containsFireTile(bb->shrink(0.001, 0.001, 0.001))) { + const AABB& shrunk = bb->shrink(0.001, 0.001, 0.001); + if (level->containsFireTile(bb)) { burn(1); if (!water) { onFire++; @@ -996,9 +1000,9 @@ bool Entity::isInWaterOrRain() { bool Entity::isInWater() { return wasInWater; } bool Entity::updateInWaterState() { - if (level->checkAndHandleWater( - bb->grow(0, -0.4f, 0)->shrink(0.001, 0.001, 0.001), Material::water, - shared_from_this())) { + AABB shrunk = bb->grow(0, -0.4, 0).shrink(0.001, 0.001, 0.001); + if (level->checkAndHandleWater(&shrunk, Material::water, + shared_from_this())) { if (!wasInWater && !firstTick && canCreateParticles()) { float speed = Mth::sqrt(xd * xd * 0.2f + yd * yd + zd * zd * 0.2f) * 0.2f; @@ -1048,8 +1052,8 @@ bool Entity::isUnderLiquid(Material* material) { float Entity::getHeadHeight() { return 0; } bool Entity::isInLava() { - return level->containsMaterial(bb->grow(-0.1f, -0.4f, -0.1f), - Material::lava); + AABB mat_bounds = bb->grow(-0.1, -0.4, -0.1); + return level->containsMaterial(&mat_bounds, Material::lava); } void Entity::moveRelative(float xa, float za, float speed) { @@ -1516,8 +1520,8 @@ void Entity::lerpTo(double x, double y, double z, float yRot, float xRot, // its definitely bad news for arrows as they are actually Meant to // intersect the geometry they land in slightly. if (GetType() != eTYPE_ARROW) { - AABBList* collisions = level->getCubes( - shared_from_this(), bb->shrink(1 / 32.0, 0, 1 / 32.0)); + AABB shrunk = bb->shrink(1 / 32.0, 0.0, 1 / 32.0); + AABBList* collisions = level->getCubes(shared_from_this(), &shrunk); if (!collisions->empty()) { double yTop = 0; AUTO_VAR(itEnd, collisions->end()); diff --git a/Minecraft.World/Entities/HangingEntity.cpp b/Minecraft.World/Entities/HangingEntity.cpp index d67329209..b94d8fdf1 100644 --- a/Minecraft.World/Entities/HangingEntity.cpp +++ b/Minecraft.World/Entities/HangingEntity.cpp @@ -74,8 +74,8 @@ void HangingEntity::setDir(int dir) { float y1 = y + h + ss; float z0 = z - d - ss; float z1 = z + d + ss; - bb->set(std::min(x0, x1), std::min(y0, y1), std::min(z0, z1), - std::max(x0, x1), std::max(y0, y1), std::max(z0, z1)); + *bb = {std::min(x0, x1), std::min(y0, y1), std::min(z0, z1), + std::max(x0, x1), std::max(y0, y1), std::max(z0, z1)}; } float HangingEntity::offs(int w) { @@ -253,4 +253,4 @@ void HangingEntity::readAdditionalSaveData(CompoundTag* tag) { setDir(dir); } -bool HangingEntity::repositionEntityAfterLoad() { return false; } \ No newline at end of file +bool HangingEntity::repositionEntityAfterLoad() { return false; } diff --git a/Minecraft.World/Entities/ItemEntity.cpp b/Minecraft.World/Entities/ItemEntity.cpp index 8725cbe5f..880971f67 100644 --- a/Minecraft.World/Entities/ItemEntity.cpp +++ b/Minecraft.World/Entities/ItemEntity.cpp @@ -119,8 +119,9 @@ void ItemEntity::tick() { } void ItemEntity::mergeWithNeighbours() { + AABB grown = bb->grow(0.5, 0, 0.5); std::vector >* neighbours = - level->getEntitiesOfClass(typeid(*this), bb->grow(0.5, 0, 0.5)); + level->getEntitiesOfClass(typeid(*this), &grown); for (AUTO_VAR(it, neighbours->begin()); it != neighbours->end(); ++it) { std::shared_ptr entity = std::dynamic_pointer_cast(*it); diff --git a/Minecraft.World/Entities/LargeFireball.cpp b/Minecraft.World/Entities/LargeFireball.cpp index 81677498d..5c2eb50a6 100644 --- a/Minecraft.World/Entities/LargeFireball.cpp +++ b/Minecraft.World/Entities/LargeFireball.cpp @@ -44,4 +44,4 @@ void LargeFireball::readAdditionalSaveData(CompoundTag* tag) { Fireball::readAdditionalSaveData(tag); if (tag->contains(L"ExplosionPower")) explosionPower = tag->getInt(L"ExplosionPower"); -} \ No newline at end of file +} diff --git a/Minecraft.World/Entities/LivingEntity.cpp b/Minecraft.World/Entities/LivingEntity.cpp index b4b870baf..8725643cf 100644 --- a/Minecraft.World/Entities/LivingEntity.cpp +++ b/Minecraft.World/Entities/LivingEntity.cpp @@ -1164,7 +1164,7 @@ void LivingEntity::teleportTo(double x, double y, double z) { } void LivingEntity::findStandUpPosition(std::shared_ptr vehicle) { - AABB* boundingBox; + AABB boundingBox; double fallbackX = vehicle->x; double fallbackY = vehicle->bb->y0 + vehicle->bbHeight; double fallbackZ = vehicle->z; @@ -1177,9 +1177,9 @@ void LivingEntity::findStandUpPosition(std::shared_ptr vehicle) { int xToInt = (int)(x + xDiff); int zToInt = (int)(z + zDiff); - boundingBox = bb->cloneMove(xDiff, 1, zDiff); + boundingBox = bb->move(xDiff, 1, zDiff); - if (level->getTileCubes(boundingBox, true)->empty()) { + if (level->getTileCubes(&boundingBox, true)->empty()) { if (level->isTopSolidBlocking(xToInt, (int)y, zToInt)) { teleportTo(x + xDiff, y + 1, z + zDiff); return; @@ -1511,9 +1511,9 @@ void LivingEntity::aiStep() { // collision used to be calculated as: bb->shrink(1 / 32.0, 0, 1 / 32.0) // now using a reduced BB to try and get rid of some issues where mobs // pop up the sides of walls, undersides of trees etc. - AABB* shrinkbb = bb->shrink(0.1, 0, 0.1); - shrinkbb->y1 = shrinkbb->y0 + 0.1; - AABBList* collisions = level->getCubes(shared_from_this(), shrinkbb); + AABB shrinkbb = bb->shrink(0.1, 0, 0.1); + shrinkbb.y1 = shrinkbb.y0 + 0.1; + AABBList* collisions = level->getCubes(shared_from_this(), &shrinkbb); if (collisions->size() > 0) { double yTop = 0; AUTO_VAR(itEnd, collisions->end()); @@ -1582,8 +1582,9 @@ void LivingEntity::aiStep() { void LivingEntity::newServerAiStep() {} void LivingEntity::pushEntities() { + AABB grown = bb->grow(0.2, 0, 0.2); std::vector >* entities = - level->getEntities(shared_from_this(), this->bb->grow(0.2f, 0, 0.2f)); + level->getEntities(shared_from_this(), &grown); if (entities != NULL && !entities->empty()) { AUTO_VAR(itEnd, entities->end()); for (AUTO_VAR(it, entities->begin()); it != itEnd; it++) { diff --git a/Minecraft.World/Entities/MinecartHopper.cpp b/Minecraft.World/Entities/MinecartHopper.cpp index 55b7ed3aa..43707b810 100644 --- a/Minecraft.World/Entities/MinecartHopper.cpp +++ b/Minecraft.World/Entities/MinecartHopper.cpp @@ -85,8 +85,9 @@ void MinecartHopper::tick() { bool MinecartHopper::suckInItems() { if (HopperTileEntity::suckInItems(this)) return true; + AABB grown = bb->grow(0.25, 0, 0.25); std::vector >* items = - level->getEntitiesOfClass(typeid(ItemEntity), bb->grow(0.25f, 0, 0.25f), + level->getEntitiesOfClass(typeid(ItemEntity), &grown, EntitySelector::ENTITY_STILL_ALIVE); if (items->size() > 0) { @@ -116,4 +117,4 @@ void MinecartHopper::readAdditionalSaveData(CompoundTag* base) { void MinecartHopper::setCooldown(int time) { cooldownTime = time; } -bool MinecartHopper::isOnCooldown() { return cooldownTime > 0; } \ No newline at end of file +bool MinecartHopper::isOnCooldown() { return cooldownTime > 0; } diff --git a/Minecraft.World/Entities/Mob.cpp b/Minecraft.World/Entities/Mob.cpp index 6b744faf8..b7fe61e1a 100644 --- a/Minecraft.World/Entities/Mob.cpp +++ b/Minecraft.World/Entities/Mob.cpp @@ -301,8 +301,9 @@ void Mob::aiStep() { if (!level->isClientSide && canPickUpLoot() && !dead && level->getGameRules()->getBoolean(GameRules::RULE_MOBGRIEFING)) { + AABB grown = bb->grow(1, 0, 1); std::vector >* entities = - level->getEntitiesOfClass(typeid(ItemEntity), bb->grow(1, 0, 1)); + level->getEntitiesOfClass(typeid(ItemEntity), &grown); for (AUTO_VAR(it, entities->begin()); it != entities->end(); ++it) { std::shared_ptr entity = std::dynamic_pointer_cast(*it); @@ -847,9 +848,10 @@ void Mob::restoreLeashFromSave() { if (_isLeashed && leashInfoTag != NULL) { if (leashInfoTag->contains(L"UUID")) { std::wstring leashUuid = leashInfoTag->getString(L"UUID"); + AABB grown = bb->grow(10, 10, 10); std::vector >* livingEnts = level->getEntitiesOfClass(typeid(LivingEntity), - bb->grow(10, 10, 10)); + &grown); for (AUTO_VAR(it, livingEnts->begin()); it != livingEnts->end(); ++it) { std::shared_ptr le = diff --git a/Minecraft.World/Entities/Mobs/Animal.cpp b/Minecraft.World/Entities/Mobs/Animal.cpp index b404a0102..8df5245d2 100644 --- a/Minecraft.World/Entities/Mobs/Animal.cpp +++ b/Minecraft.World/Entities/Mobs/Animal.cpp @@ -61,7 +61,7 @@ void Animal::aiStep() { void Animal::checkHurtTarget(std::shared_ptr target, float d) { // 4J-JEV: Changed from dynamic cast to use eINSTANCEOF - if (target->instanceof (eTYPE_PLAYER)) { + if (target->instanceof(eTYPE_PLAYER)) { if (d < 3) { double xd = target->x - x; double zd = target->z - z; @@ -77,7 +77,7 @@ void Animal::checkHurtTarget(std::shared_ptr target, float d) { } // 4J-JEV: Changed from dynamic cast to use eINSTANCEOF - else if (target->instanceof (eTYPE_ANIMAL)) { + else if (target->instanceof(eTYPE_ANIMAL)) { std::shared_ptr a = std::dynamic_pointer_cast(target); if (getAge() > 0 && a->getAge() < 0) { if (d < 2.5) { @@ -162,22 +162,22 @@ bool Animal::hurt(DamageSource* dmgSource, float dmg) { std::shared_ptr source = dmgSource->getDirectEntity(); // 4J-JEV: Changed from dynamic cast to use eINSTANCEOF - if (source->instanceof - (eTYPE_PLAYER) && !std::dynamic_pointer_cast(source) - ->isAllowedToAttackAnimals()) { + if (source->instanceof(eTYPE_PLAYER) && + !std::dynamic_pointer_cast(source) + ->isAllowedToAttackAnimals()) { return false; } - if ((source != NULL) && source->instanceof (eTYPE_ARROW)) { + if ((source != NULL) && source->instanceof(eTYPE_ARROW)) { std::shared_ptr arrow = std::dynamic_pointer_cast(source); // 4J: Check that the arrow's owner can attack animals (dispenser // arrows are not owned) - if (arrow->owner != NULL && arrow->owner->instanceof - (eTYPE_PLAYER) && - !std::dynamic_pointer_cast(arrow->owner) - ->isAllowedToAttackAnimals()) { + if (arrow->owner != NULL && + arrow->owner->instanceof(eTYPE_PLAYER) && + !std::dynamic_pointer_cast(arrow->owner) + ->isAllowedToAttackAnimals()) { return false; } } @@ -216,8 +216,9 @@ std::shared_ptr Animal::findAttackTarget() { float r = 8; if (getInLoveValue() > 0) { + AABB grown = bb->grow(r, r, r); std::vector >* others = - level->getEntitiesOfClass(typeid(*this), bb->grow(r, r, r)); + level->getEntitiesOfClass(typeid(*this), &grown); // for (int i = 0; i < others->size(); i++) for (AUTO_VAR(it, others->begin()); it != others->end(); ++it) { std::shared_ptr p = std::dynamic_pointer_cast(*it); @@ -229,8 +230,9 @@ std::shared_ptr Animal::findAttackTarget() { delete others; } else { if (getAge() == 0) { + AABB grown = bb->grow(r, r, r); std::vector >* players = - level->getEntitiesOfClass(typeid(Player), bb->grow(r, r, r)); + level->getEntitiesOfClass(typeid(Player), &grown); // for (int i = 0; i < players.size(); i++) for (AUTO_VAR(it, players->begin()); it != players->end(); ++it) { setDespawnProtected(); @@ -245,8 +247,9 @@ std::shared_ptr Animal::findAttackTarget() { } delete players; } else if (getAge() > 0) { + AABB grown = bb->grow(r, r, r); std::vector >* others = - level->getEntitiesOfClass(typeid(*this), bb->grow(r, r, r)); + level->getEntitiesOfClass(typeid(*this), &grown); // for (int i = 0; i < others.size(); i++) for (AUTO_VAR(it, others->begin()); it != others->end(); ++it) { std::shared_ptr p = @@ -332,7 +335,7 @@ bool Animal::mobInteract(std::shared_ptr player) { return false; } - } else if (instanceof (eTYPE_MONSTER)) { + } else if (instanceof(eTYPE_MONSTER)) { } break; } diff --git a/Minecraft.World/Entities/Mobs/Arrow.cpp b/Minecraft.World/Entities/Mobs/Arrow.cpp index 5de30c9a4..6c02a0eea 100644 --- a/Minecraft.World/Entities/Mobs/Arrow.cpp +++ b/Minecraft.World/Entities/Mobs/Arrow.cpp @@ -185,7 +185,7 @@ void Arrow::tick() { Tile::tiles[t]->updateShape(level, xTile, yTile, zTile); AABB* aabb = Tile::tiles[t]->getAABB(level, xTile, yTile, zTile); Vec3 pos{x, y, z}; - if (aabb != NULL && aabb->contains(&pos)) { + if (aabb != NULL && aabb->contains(pos)) { inGround = true; } } @@ -230,8 +230,9 @@ void Arrow::tick() { } std::shared_ptr hitEntity = nullptr; - std::vector >* objects = level->getEntities( - shared_from_this(), this->bb->expand(xd, yd, zd)->grow(1, 1, 1)); + AABB grown = bb->expand(xd, yd, zd).grow(1, 1, 1); + std::vector >* objects = + level->getEntities(shared_from_this(), &grown); double nearest = 0; AUTO_VAR(itEnd, objects->end()); for (AUTO_VAR(it, objects->begin()); it != itEnd; it++) { @@ -239,8 +240,8 @@ void Arrow::tick() { if (!e->isPickable() || (e == owner && flightTime < 5)) continue; float rr = 0.3f; - AABB* bb = e->bb->grow(rr, rr, rr); - HitResult* p = bb->clip(&from, &to); + AABB bb = e->bb->grow(rr, rr, rr); + HitResult* p = bb.clip(from, to); if (p != NULL) { double dd = from.distanceTo(p->pos); if (dd < nearest || nearest == 0) { diff --git a/Minecraft.World/Entities/Mobs/Boat.cpp b/Minecraft.World/Entities/Mobs/Boat.cpp index 356dacf57..f379b068e 100644 --- a/Minecraft.World/Entities/Mobs/Boat.cpp +++ b/Minecraft.World/Entities/Mobs/Boat.cpp @@ -345,8 +345,9 @@ void Boat::tick() { if (level->isClientSide) return; + AABB grown = bb->grow(0.2, 0, 0.2); std::vector >* entities = - level->getEntities(shared_from_this(), bb->grow(0.2f, 0, 0.2f)); + level->getEntities(shared_from_this(), &grown); if (entities != NULL && !entities->empty()) { AUTO_VAR(itEnd, entities->end()); for (AUTO_VAR(it, entities->begin()); it != itEnd; it++) { diff --git a/Minecraft.World/Entities/Mobs/DragonFireball.cpp b/Minecraft.World/Entities/Mobs/DragonFireball.cpp index 62f4a067c..11607882a 100644 --- a/Minecraft.World/Entities/Mobs/DragonFireball.cpp +++ b/Minecraft.World/Entities/Mobs/DragonFireball.cpp @@ -29,9 +29,9 @@ DragonFireball::DragonFireball(Level* level, double x, double y, double z, void DragonFireball::onHit(HitResult* res) { if (!level->isClientSide) { - AABB* aoe = bb->grow(SPLASH_RANGE, SPLASH_RANGE / 2, SPLASH_RANGE); + AABB aoe = bb->grow(SPLASH_RANGE, SPLASH_RANGE / 2, SPLASH_RANGE); std::vector >* entitiesOfClass = - level->getEntitiesOfClass(typeid(LivingEntity), aoe); + level->getEntitiesOfClass(typeid(LivingEntity), &aoe); if (entitiesOfClass != NULL && !entitiesOfClass->empty()) { // for (Entity e : entitiesOfClass) diff --git a/Minecraft.World/Entities/Mobs/EnderDragon.cpp b/Minecraft.World/Entities/Mobs/EnderDragon.cpp index 4625557e2..7f9697bc3 100644 --- a/Minecraft.World/Entities/Mobs/EnderDragon.cpp +++ b/Minecraft.World/Entities/Mobs/EnderDragon.cpp @@ -646,12 +646,17 @@ void EnderDragon::aiStep() { if (!level->isClientSide) checkAttack(); if (!level->isClientSide && hurtDuration == 0) { + AABB wing_mov = wing1->bb->grow(4, 2, 4).move(0, -2, 0); knockBack(level->getEntities(shared_from_this(), - wing1->bb->grow(4, 2, 4)->move(0, -2, 0))); + &wing_mov)); + wing_mov = wing2->bb->grow(4, 2, 4).move(0, -2, 0); knockBack(level->getEntities(shared_from_this(), - wing2->bb->grow(4, 2, 4)->move(0, -2, 0))); - hurt(level->getEntities(shared_from_this(), neck->bb->grow(1, 1, 1))); - hurt(level->getEntities(shared_from_this(), head->bb->grow(1, 1, 1))); + &wing_mov)); + + AABB neck_bb = neck->bb->grow(1, 1, 1); + AABB head_bb = head->bb->grow(1, 1, 1); + hurt(level->getEntities(shared_from_this(), &neck_bb)); + hurt(level->getEntities(shared_from_this(), &head_bb)); } double p1components[3]; @@ -684,8 +689,8 @@ void EnderDragon::aiStep() { double acidX = x + ss * 9.5f * ccTilt; double acidY = y + yOffset + ssTilt * 10.5f; double acidZ = z - cc * 9.5f * ccTilt; - m_acidArea->set(acidX - 5, acidY - 17, acidZ - 5, acidX + 5, acidY + 4, - acidZ + 5); + *m_acidArea = {acidX - 5, acidY - 17, acidZ - 5, acidX + 5, acidY + 4, + acidZ + 5}; // app.DebugPrintf("\nDragon is %s, yRot = %f, yRotA = %f, ss = %f, cc = // %f, ccTilt = %f\n",level->isClientSide?"client":"server", yRot, @@ -811,9 +816,11 @@ void EnderDragon::checkCrystals() { if (random->nextInt(10) == 0) { float maxDist = 32; + AABB grown = +bb->grow(maxDist, maxDist, maxDist); std::vector >* crystals = level->getEntitiesOfClass(typeid(EnderCrystal), - bb->grow(maxDist, maxDist, maxDist)); + &grown); std::shared_ptr crystal = nullptr; double nearest = std::numeric_limits::max(); @@ -1422,8 +1429,9 @@ EnderDragon::EEnderdragonAction EnderDragon::getSynchedAction() { void EnderDragon::handleCrystalDestroyed(DamageSource* source) { AABB* tempBB = AABB::newTemp(PODIUM_X_POS, 84.0, PODIUM_Z_POS, PODIUM_X_POS + 1.0, 85.0, PODIUM_Z_POS + 1.0); + AABB grown = tempBB->grow(48, 40, 48); std::vector >* crystals = level->getEntitiesOfClass( - typeid(EnderCrystal), tempBB->grow(48, 40, 48)); + typeid(EnderCrystal), &grown); m_remainingCrystalsCount = (int)crystals->size() - 1; if (m_remainingCrystalsCount < 0) m_remainingCrystalsCount = 0; delete crystals; diff --git a/Minecraft.World/Entities/Mobs/EntityHorse.cpp b/Minecraft.World/Entities/Mobs/EntityHorse.cpp index 65502c329..cd2563466 100644 --- a/Minecraft.World/Entities/Mobs/EntityHorse.cpp +++ b/Minecraft.World/Entities/Mobs/EntityHorse.cpp @@ -420,9 +420,9 @@ std::shared_ptr EntityHorse::getClosestMommy( double closestDistance = std::numeric_limits::max(); std::shared_ptr mommy = nullptr; - std::vector >* list = level->getEntities( - baby, baby->bb->expand(searchRadius, searchRadius, searchRadius), - PARENT_HORSE_SELECTOR); + AABB expanded = baby->bb->expand(searchRadius, searchRadius, searchRadius); + std::vector >* list = + level->getEntities(baby, &expanded, PARENT_HORSE_SELECTOR); for (AUTO_VAR(it, list->begin()); it != list->end(); ++it) { std::shared_ptr horse = *it; diff --git a/Minecraft.World/Entities/Mobs/Fireball.cpp b/Minecraft.World/Entities/Mobs/Fireball.cpp index 91bfe34f7..b6613549c 100644 --- a/Minecraft.World/Entities/Mobs/Fireball.cpp +++ b/Minecraft.World/Entities/Mobs/Fireball.cpp @@ -176,8 +176,9 @@ void Fireball::tick() { to = Vec3{res->pos.x, res->pos.y, res->pos.z}; } std::shared_ptr hitEntity = nullptr; - std::vector >* objects = level->getEntities( - shared_from_this(), bb->expand(xd, yd, zd)->grow(1, 1, 1)); + AABB grown = bb->expand(xd, yd, zd).grow(1, 1, 1); + std::vector >* objects = + level->getEntities(shared_from_this(), &grown); double nearest = 0; AUTO_VAR(itEnd, objects->end()); for (AUTO_VAR(it, objects->begin()); it != itEnd; it++) { @@ -187,8 +188,8 @@ void Fireball::tick() { // && flightTime < 25)) continue; float rr = 0.3f; - AABB* bb = e->bb->grow(rr, rr, rr); - HitResult* p = bb->clip(&from, &to); + AABB bb = e->bb->grow(rr, rr, rr); + HitResult* p = bb.clip(from, to); if (p != NULL) { double dd = from.distanceTo(p->pos); if (dd < nearest || nearest == 0) { diff --git a/Minecraft.World/Entities/Mobs/FishingHook.cpp b/Minecraft.World/Entities/Mobs/FishingHook.cpp index b8cc293bf..76a4d5b07 100644 --- a/Minecraft.World/Entities/Mobs/FishingHook.cpp +++ b/Minecraft.World/Entities/Mobs/FishingHook.cpp @@ -213,8 +213,9 @@ void FishingHook::tick() { to = Vec3(res->pos.x, res->pos.y, res->pos.z); } std::shared_ptr hitEntity = nullptr; - std::vector >* objects = level->getEntities( - shared_from_this(), bb->expand(xd, yd, zd)->grow(1, 1, 1)); + AABB grown = bb->expand(xd, yd, zd).grow(1, 1, 1); + std::vector >* objects = + level->getEntities(shared_from_this(), &grown); double nearest = 0; AUTO_VAR(itEnd, objects->end()); for (AUTO_VAR(it, objects->begin()); it != itEnd; it++) { @@ -222,8 +223,8 @@ void FishingHook::tick() { if (!e->isPickable() || (e == owner && flightTime < 5)) continue; float rr = 0.3f; - AABB* bb = e->bb->grow(rr, rr, rr); - HitResult* p = bb->clip(&from, &to); + AABB bb = e->bb->grow(rr, rr, rr); + HitResult* p = bb.clip(from, to); if (p != NULL) { double dd = from.distanceTo(p->pos); if (dd < nearest || nearest == 0) { diff --git a/Minecraft.World/Entities/Mobs/Ghast.cpp b/Minecraft.World/Entities/Mobs/Ghast.cpp index 6dedd39ef..40c65a43c 100644 --- a/Minecraft.World/Entities/Mobs/Ghast.cpp +++ b/Minecraft.World/Entities/Mobs/Ghast.cpp @@ -170,10 +170,10 @@ bool Ghast::canReach(double xt, double yt, double zt, double dist) { double yd = (yTarget - y) / dist; double zd = (zTarget - z) / dist; - AABB* bb = this->bb->copy(); + AABB bb = *this->bb; for (int d = 1; d < dist; d++) { - bb->move(xd, yd, zd); - if (!level->getCubes(shared_from_this(), bb)->empty()) return false; + bb.move(xd, yd, zd); + if (!level->getCubes(shared_from_this(), &bb)->empty()) return false; } return true; diff --git a/Minecraft.World/Entities/Mobs/Minecart.cpp b/Minecraft.World/Entities/Mobs/Minecart.cpp index 8f0350096..16dcbb183 100644 --- a/Minecraft.World/Entities/Mobs/Minecart.cpp +++ b/Minecraft.World/Entities/Mobs/Minecart.cpp @@ -305,8 +305,9 @@ void Minecart::tick() { } setRot(yRot, xRot); + AABB grown = bb->grow(0.2, 0, 0.2); std::vector >* entities = - level->getEntities(shared_from_this(), bb->grow(0.2f, 0, 0.2f)); + level->getEntities(shared_from_this(), &grown); if (entities != NULL && !entities->empty()) { AUTO_VAR(itEnd, entities->end()); for (AUTO_VAR(it, entities->begin()); it != itEnd; it++) { diff --git a/Minecraft.World/Entities/Mobs/PigZombie.cpp b/Minecraft.World/Entities/Mobs/PigZombie.cpp index e619286cb..0c016c83f 100644 --- a/Minecraft.World/Entities/Mobs/PigZombie.cpp +++ b/Minecraft.World/Entities/Mobs/PigZombie.cpp @@ -100,8 +100,9 @@ std::shared_ptr PigZombie::findAttackTarget() { bool PigZombie::hurt(DamageSource* source, float dmg) { std::shared_ptr sourceEntity = source->getEntity(); if (sourceEntity != NULL && sourceEntity->instanceof(eTYPE_PLAYER)) { + AABB grown = bb->grow(32, 32, 32); std::vector >* nearby = - level->getEntities(shared_from_this(), bb->grow(32, 32, 32)); + level->getEntities(shared_from_this(), &grown); AUTO_VAR(itEnd, nearby->end()); for (AUTO_VAR(it, nearby->begin()); it != itEnd; it++) { std::shared_ptr e = *it; // nearby->at(i); @@ -158,4 +159,4 @@ MobGroupData* PigZombie::finalizeMobSpawn( Zombie::finalizeMobSpawn(groupData); setVillager(false); return groupData; -} \ No newline at end of file +} diff --git a/Minecraft.World/Entities/Mobs/Squid.cpp b/Minecraft.World/Entities/Mobs/Squid.cpp index 951d7b511..7cb96c08d 100644 --- a/Minecraft.World/Entities/Mobs/Squid.cpp +++ b/Minecraft.World/Entities/Mobs/Squid.cpp @@ -65,7 +65,8 @@ void Squid::dropDeathLoot(bool wasKilledByPlayer, int playerBonusLevel) { } bool Squid::isInWater() { - return level->checkAndHandleWater(bb->grow(0, -0.6f, 0), Material::water, + AABB grown = bb->grow(0, -0.6, 0); + return level->checkAndHandleWater(&grown, Material::water, shared_from_this()); } diff --git a/Minecraft.World/Entities/Mobs/ThrownPotion.cpp b/Minecraft.World/Entities/Mobs/ThrownPotion.cpp index 7bfc9cf6a..da329189c 100644 --- a/Minecraft.World/Entities/Mobs/ThrownPotion.cpp +++ b/Minecraft.World/Entities/Mobs/ThrownPotion.cpp @@ -82,9 +82,9 @@ void ThrownPotion::onHit(HitResult* res) { Item::potion->getMobEffects(potionItem); if (mobEffects != NULL && !mobEffects->empty()) { - AABB* aoe = bb->grow(SPLASH_RANGE, SPLASH_RANGE / 2, SPLASH_RANGE); + AABB aoe = bb->grow(SPLASH_RANGE, SPLASH_RANGE / 2, SPLASH_RANGE); std::vector >* entitiesOfClass = - level->getEntitiesOfClass(typeid(LivingEntity), aoe); + level->getEntitiesOfClass(typeid(LivingEntity), &aoe); if (entitiesOfClass != NULL && !entitiesOfClass->empty()) { // for (Entity e : entitiesOfClass) diff --git a/Minecraft.World/Entities/Mobs/WitherBoss.cpp b/Minecraft.World/Entities/Mobs/WitherBoss.cpp index 092e810ea..80316042e 100644 --- a/Minecraft.World/Entities/Mobs/WitherBoss.cpp +++ b/Minecraft.World/Entities/Mobs/WitherBoss.cpp @@ -252,9 +252,9 @@ void WitherBoss::newServerAiStep() { idleHeadUpdates[i - 1] = 0; } } else { + AABB grown = bb->grow(20, 8, 20); std::vector >* entities = - level->getEntitiesOfClass(typeid(LivingEntity), - bb->grow(20, 8, 20), + level->getEntitiesOfClass(typeid(LivingEntity), &grown, livingEntitySelector); // randomly try to find a target 10 times for (int attempt = 0; attempt < 10 && !entities->empty(); diff --git a/Minecraft.World/Entities/Throwable.cpp b/Minecraft.World/Entities/Throwable.cpp index 93e9a9701..056129287 100644 --- a/Minecraft.World/Entities/Throwable.cpp +++ b/Minecraft.World/Entities/Throwable.cpp @@ -147,8 +147,9 @@ void Throwable::tick() { if (!level->isClientSide) { std::shared_ptr hitEntity = nullptr; - std::vector >* objects = level->getEntities( - shared_from_this(), bb->expand(xd, yd, zd)->grow(1, 1, 1)); + AABB grown = bb->expand(xd, yd, zd).grow(1, 1, 1); + std::vector >* objects = + level->getEntities(shared_from_this(), &grown); double nearest = 0; std::shared_ptr owner = getOwner(); for (int i = 0; i < objects->size(); i++) { @@ -156,8 +157,8 @@ void Throwable::tick() { if (!e->isPickable() || (e == owner && flightTime < 5)) continue; float rr = 0.3f; - AABB* bb = e->bb->grow(rr, rr, rr); - HitResult* p = bb->clip(&from, &to); + AABB bb = e->bb->grow(rr, rr, rr); + HitResult* p = bb.clip(from, to); if (p != NULL) { double dd = from.distanceTo(p->pos); delete p; diff --git a/Minecraft.World/Entities/WitherSkull.cpp b/Minecraft.World/Entities/WitherSkull.cpp index 81a6a4cca..049309c04 100644 --- a/Minecraft.World/Entities/WitherSkull.cpp +++ b/Minecraft.World/Entities/WitherSkull.cpp @@ -105,4 +105,4 @@ void WitherSkull::setDangerous(bool value) { entityData->set(DATA_DANGEROUS, value ? (uint8_t)1 : (uint8_t)0); } -bool WitherSkull::shouldBurn() { return false; } \ No newline at end of file +bool WitherSkull::shouldBurn() { return false; } diff --git a/Minecraft.World/Items/BoatItem.cpp b/Minecraft.World/Items/BoatItem.cpp index d84290919..0c4d6a1b9 100644 --- a/Minecraft.World/Items/BoatItem.cpp +++ b/Minecraft.World/Items/BoatItem.cpp @@ -83,17 +83,18 @@ std::shared_ptr BoatItem::use( Vec3 b = player->getViewVector(a); bool hitEntity = false; float overlap = 1; - std::vector >* objects = level->getEntities( - player, player->bb->expand(b.x * (range), b.y * (range), b.z * (range)) - ->grow(overlap, overlap, overlap)); + AABB grown = player->bb->expand(b.x * (range), b.y * (range), b.z * (range)) + .grow(overlap, overlap, overlap); + std::vector >* objects = + level->getEntities(player, &grown); // for (int i = 0; i < objects.size(); i++) { for (AUTO_VAR(it, objects->begin()); it != objects->end(); ++it) { std::shared_ptr e = *it; // objects.get(i); if (!e->isPickable()) continue; float rr = e->getPickRadius(); - AABB* bb = e->bb->grow(rr, rr, rr); - if (bb->contains(&from)) { + AABB bb = e->bb->grow(rr, rr, rr); + if (bb.contains(from)) { hitEntity = true; } } @@ -115,8 +116,8 @@ std::shared_ptr BoatItem::use( boat->yRot = ((Mth::floor(player->yRot * 4.0F / 360.0F + 0.5) & 0x3) - 1) * 90; - if (!level->getCubes(boat, boat->bb->grow(-.1, -.1, -.1)) - ->empty()) { + AABB grown = boat->bb->grow(-0.1, -0.1, -0.1); + if (!level->getCubes(boat, &grown)->empty()) { return itemInstance; } if (!level->isClientSide) { diff --git a/Minecraft.World/Level/BaseMobSpawner.cpp b/Minecraft.World/Level/BaseMobSpawner.cpp index 522290eec..bf6fcb51c 100644 --- a/Minecraft.World/Level/BaseMobSpawner.cpp +++ b/Minecraft.World/Level/BaseMobSpawner.cpp @@ -83,14 +83,13 @@ void BaseMobSpawner::tick() { EntityIO::newEntity(getEntityId(), getLevel()); if (entity == NULL) return; - int nearBy = - getLevel() - ->getEntitiesOfClass( - typeid(entity.get()), - AABB::newTemp(getX(), getY(), getZ(), getX() + 1, - getY() + 1, getZ() + 1) - ->grow(spawnRange * 2, 4, spawnRange * 2)) - ->size(); + AABB grown = + AABB(getX(), getY(), getZ(), getX() + 1, getY() + 1, getZ() + 1) + .grow(spawnRange * 2, 4, spawnRange * 2); + + int nearBy = getLevel() + ->getEntitiesOfClass(typeid(entity.get()), &grown) + ->size(); if (nearBy >= maxNearbyEntities) { delay(); return; @@ -359,4 +358,4 @@ CompoundTag* BaseMobSpawner::SpawnData::save() { result->putInt(L"Weight", randomWeight); return result; -} \ No newline at end of file +} diff --git a/Minecraft.World/Level/Level.cpp b/Minecraft.World/Level/Level.cpp index f61f88c8b..cd572cbf7 100644 --- a/Minecraft.World/Level/Level.cpp +++ b/Minecraft.World/Level/Level.cpp @@ -1798,17 +1798,18 @@ AABBList* Level::getCubes(std::shared_ptr source, AABB* box, if (noEntities) return &boxes; double r = 0.25; + AABB grown = box->grow(r, r, r); std::vector >* ee = - getEntities(source, box->grow(r, r, r)); + getEntities(source, &grown); std::vector >::iterator itEnd = ee->end(); for (AUTO_VAR(it, ee->begin()); it != itEnd; it++) { AABB* collideBox = (*it)->getCollideBox(); - if (collideBox != NULL && collideBox->intersects(box)) { + if (collideBox != NULL && collideBox->intersects(*box)) { boxes.push_back(collideBox); } collideBox = source->getCollideAgainstBox(*it); - if (collideBox != NULL && collideBox->intersects(box)) { + if (collideBox != NULL && collideBox->intersects(*box)) { boxes.push_back(collideBox); } } diff --git a/Minecraft.World/Level/LevelChunk.cpp b/Minecraft.World/Level/LevelChunk.cpp index 71c285e5c..c5c0632ef 100644 --- a/Minecraft.World/Level/LevelChunk.cpp +++ b/Minecraft.World/Level/LevelChunk.cpp @@ -1691,7 +1691,7 @@ void LevelChunk::getEntities(std::shared_ptr except, AABB* bb, AUTO_VAR(itEnd, entities->end()); for (AUTO_VAR(it, entities->begin()); it != itEnd; it++) { std::shared_ptr e = *it; // entities->at(i); - if (e != except && e->bb->intersects(bb) && + if (e != except && e->bb->intersects(*bb) && (selector == NULL || selector->matches(e))) { es.push_back(e); std::vector >* subs = @@ -1699,7 +1699,7 @@ void LevelChunk::getEntities(std::shared_ptr except, AABB* bb, if (subs != NULL) { for (int j = 0; j < subs->size(); j++) { e = subs->at(j); - if (e != except && e->bb->intersects(bb) && + if (e != except && e->bb->intersects(*bb) && (selector == NULL || selector->matches(e))) { es.push_back(e); } @@ -1765,7 +1765,7 @@ void LevelChunk::getEntitiesOfClass(const std::type_info& ec, AABB* bb, else if (Entity* entity = e.get(); entity != NULL && ec == typeid(*entity)) isAssignableFrom = true; - if (isAssignableFrom && e->bb->intersects(bb)) { + if (isAssignableFrom && e->bb->intersects(*bb)) { if (selector == NULL || selector->matches(e)) { es.push_back(e); } diff --git a/Minecraft.World/Player/Player.cpp b/Minecraft.World/Player/Player.cpp index a345d2087..fff23c15e 100644 --- a/Minecraft.World/Player/Player.cpp +++ b/Minecraft.World/Player/Player.cpp @@ -961,17 +961,17 @@ void Player::aiStep() { tilt += (tTilt - tilt) * 0.8f; if (getHealth() > 0) { - AABB* pickupArea = NULL; + AABB pickupArea; if (riding != NULL && !riding->removed) { // if the player is riding, also touch entities under the // pig/horse - pickupArea = bb->minmax(riding->bb)->grow(1, 0, 1); + pickupArea = bb->minmax(*riding->bb).grow(1, 0, 1); } else { pickupArea = bb->grow(1, .5, 1); } std::vector >* entities = - level->getEntities(shared_from_this(), pickupArea); + level->getEntities(shared_from_this(), &pickupArea); if (entities != NULL) { AUTO_VAR(itEnd, entities->end()); for (AUTO_VAR(it, entities->begin()); it != itEnd; it++) { diff --git a/Minecraft.World/Util/AABB.cpp b/Minecraft.World/Util/AABB.cpp index 2cb797e38..53b4412f3 100644 --- a/Minecraft.World/Util/AABB.cpp +++ b/Minecraft.World/Util/AABB.cpp @@ -3,8 +3,11 @@ // import java->util.ArrayList; // import java->util.List; +// TODO: use brace initialization everywhere + #include "../Platform/stdafx.h" #include "AABB.h" +#include #include #include "HitResult.h" #include "Util/Vec3.h" @@ -50,7 +53,7 @@ AABB* AABB::newTemp(double x0, double y0, double z0, double x1, double y1, double z1) { ThreadStorage* tls = m_tlsPool; AABB* thisAABB = &tls->pool[tls->poolPointer]; - thisAABB->set(x0, y0, z0, x1, y1, z1); + *thisAABB = {x0, y0, z0, x1, y1, z1}; tls->poolPointer = (tls->poolPointer + 1) % ThreadStorage::POOL_SIZE; return thisAABB; } @@ -64,18 +67,7 @@ AABB::AABB(double x0, double y0, double z0, double x1, double y1, double z1) { this->z1 = z1; } -AABB* AABB::set(double x0, double y0, double z0, double x1, double y1, - double z1) { - this->x0 = x0; - this->y0 = y0; - this->z0 = z0; - this->x1 = x1; - this->y1 = y1; - this->z1 = z1; - return this; -} - -AABB* AABB::expand(double xa, double ya, double za) { +AABB AABB::expand(double xa, double ya, double za) const { double _x0 = x0; double _y0 = y0; double _z0 = z0; @@ -92,10 +84,10 @@ AABB* AABB::expand(double xa, double ya, double za) { if (za < 0) _z0 += za; if (za > 0) _z1 += za; - return AABB::newTemp(_x0, _y0, _z0, _x1, _y1, _z1); + return {_x0, _y0, _z0, _x1, _y1, _z1}; } -AABB* AABB::grow(double xa, double ya, double za) { +AABB AABB::grow(const double xa, const double ya, const double za) const { double _x0 = x0 - xa; double _y0 = y0 - ya; double _z0 = z0 - za; @@ -103,127 +95,121 @@ AABB* AABB::grow(double xa, double ya, double za) { double _y1 = y1 + ya; double _z1 = z1 + za; - return AABB::newTemp(_x0, _y0, _z0, _x1, _y1, _z1); + return {_x0, _y0, _z0, _x1, _y1, _z1}; } -AABB* AABB::minmax(AABB* other) { - double _x0 = std::min(x0, other->x0); - double _y0 = std::min(y0, other->y0); - double _z0 = std::min(z0, other->z0); - double _x1 = std::max(x1, other->x1); - double _y1 = std::max(y1, other->y1); - double _z1 = std::max(z1, other->z1); +AABB AABB::minmax(const AABB& other) const { + double _x0 = std::min(x0, other.x0); + double _y0 = std::min(y0, other.y0); + double _z0 = std::min(z0, other.z0); + double _x1 = std::max(x1, other.x1); + double _y1 = std::max(y1, other.y1); + double _z1 = std::max(z1, other.z1); - return newTemp(_x0, _y0, _z0, _x1, _y1, _z1); + return {_x0, _y0, _z0, _x1, _y1, _z1}; } -AABB* AABB::cloneMove(double xa, double ya, double za) { - return AABB::newTemp(x0 + xa, y0 + ya, z0 + za, x1 + xa, y1 + ya, z1 + za); -} +double AABB::clipXCollide(const AABB& c, double xa) const { + if (c.y1 <= y0 || c.y0 >= y1) return xa; + if (c.z1 <= z0 || c.z0 >= z1) return xa; -double AABB::clipXCollide(AABB* c, double xa) { - if (c->y1 <= y0 || c->y0 >= y1) return xa; - if (c->z1 <= z0 || c->z0 >= z1) return xa; - - if (xa > 0 && c->x1 <= x0) { - double max = x0 - c->x1; + if (xa > 0 && c.x1 <= x0) { + double max = x0 - c.x1; if (max < xa) xa = max; } - if (xa < 0 && c->x0 >= x1) { - double max = x1 - c->x0; + + if (xa < 0 && c.x0 >= x1) { + double max = x1 - c.x0; if (max > xa) xa = max; } return xa; } -double AABB::clipYCollide(AABB* c, double ya) { - if (c->x1 <= x0 || c->x0 >= x1) return ya; - if (c->z1 <= z0 || c->z0 >= z1) return ya; +double AABB::clipYCollide(const AABB& c, double ya) const { + if (c.x1 <= x0 || c.x0 >= x1) return ya; + if (c.z1 <= z0 || c.z0 >= z1) return ya; - if (ya > 0 && c->y1 <= y0) { - double max = y0 - c->y1; + if (ya > 0 && c.y1 <= y0) { + double max = y0 - c.y1; if (max < ya) ya = max; } - if (ya < 0 && c->y0 >= y1) { - double max = y1 - c->y0; + + if (ya < 0 && c.y0 >= y1) { + double max = y1 - c.y0; if (max > ya) ya = max; } return ya; } -double AABB::clipZCollide(AABB* c, double za) { - if (c->x1 <= x0 || c->x0 >= x1) return za; - if (c->y1 <= y0 || c->y0 >= y1) return za; +double AABB::clipZCollide(const AABB& c, double za) const { + if (c.x1 <= x0 || c.x0 >= x1) return za; + if (c.y1 <= y0 || c.y0 >= y1) return za; - if (za > 0 && c->z1 <= z0) { - double max = z0 - c->z1; + if (za > 0 && c.z1 <= z0) { + double max = z0 - c.z1; if (max < za) za = max; } - if (za < 0 && c->z0 >= z1) { - double max = z1 - c->z0; + + if (za < 0 && c.z0 >= z1) { + double max = z1 - c.z0; if (max > za) za = max; } return za; } -bool AABB::intersects(AABB* c) { - if (c->x1 <= x0 || c->x0 >= x1) return false; - if (c->y1 <= y0 || c->y0 >= y1) return false; - if (c->z1 <= z0 || c->z0 >= z1) return false; +bool AABB::intersects(const AABB& c) const { + if (c.x1 <= x0 || c.x0 >= x1) return false; + if (c.y1 <= y0 || c.y0 >= y1) return false; + if (c.z1 <= z0 || c.z0 >= z1) return false; return true; } -bool AABB::intersectsInner(AABB* c) { - if (c->x1 < x0 || c->x0 > x1) return false; - if (c->y1 < y0 || c->y0 > y1) return false; - if (c->z1 < z0 || c->z0 > z1) return false; - return true; +AABB AABB::move(const double xa, const double ya, const double za) const { + return { + x0 + xa, y0 + ya, z0 + za, + + x1 + xa, y1 + ya, z1 + za, + }; } -AABB* AABB::move(double xa, double ya, double za) { - x0 += xa; - y0 += ya; - z0 += za; - x1 += xa; - y1 += ya; - z1 += za; - return this; -} - -bool AABB::intersects(double x02, double y02, double z02, double x12, - double y12, double z12) { +bool AABB::intersects(const double x02, const double y02, const double z02, + const double x12, const double y12, + const double z12) const { if (x12 <= x0 || x02 >= x1) return false; if (y12 <= y0 || y02 >= y1) return false; if (z12 <= z0 || z02 >= z1) return false; + return true; } -bool AABB::contains(Vec3* p) { - if (p->x <= x0 || p->x >= x1) return false; - if (p->y <= y0 || p->y >= y1) return false; - if (p->z <= z0 || p->z >= z1) return false; +bool AABB::contains(const Vec3& p) const { + if (p.x <= x0 || p.x >= x1) return false; + if (p.y <= y0 || p.y >= y1) return false; + if (p.z <= z0 || p.z >= z1) return false; + return true; } // 4J Added -bool AABB::containsIncludingLowerBound(Vec3* p) { - if (p->x < x0 || p->x >= x1) return false; - if (p->y < y0 || p->y >= y1) return false; - if (p->z < z0 || p->z >= z1) return false; +bool AABB::containsIncludingLowerBound(const Vec3& p) const { + if (p.x < x0 || p.x >= x1) return false; + if (p.y < y0 || p.y >= y1) return false; + if (p.z < z0 || p.z >= z1) return false; return true; } -double AABB::getSize() { - double xs = x1 - x0; - double ys = y1 - y0; - double zs = z1 - z0; +double AABB::getSize() const { + const double xs = x1 - x0; + const double ys = y1 - y0; + const double zs = z1 - z0; + return (xs + ys + zs) / 3.0f; } -AABB* AABB::shrink(double xa, double ya, double za) { +AABB AABB::shrink(const double xa, const double ya, const double za) const { double _x0 = x0 + xa; double _y0 = y0 + ya; double _z0 = z0 + za; @@ -231,58 +217,50 @@ AABB* AABB::shrink(double xa, double ya, double za) { double _y1 = y1 - ya; double _z1 = z1 - za; - return AABB::newTemp(_x0, _y0, _z0, _x1, _y1, _z1); + return {_x0, _y0, _z0, _x1, _y1, _z1}; } -AABB* AABB::copy() { return AABB::newTemp(x0, y0, z0, x1, y1, z1); } +HitResult* AABB::clip(const Vec3& a, const Vec3& b) const { + auto xh0 = a.clipX(b, x0); + auto xh1 = a.clipX(b, x1); -HitResult* AABB::clip(Vec3* a, Vec3* b) { - auto xh0 = a->clipX(*b, x0); - auto xh1 = a->clipX(*b, x1); + auto yh0 = a.clipY(b, y0); + auto yh1 = a.clipY(b, y1); - auto yh0 = a->clipY(*b, y0); - auto yh1 = a->clipY(*b, y1); + auto zh0 = a.clipZ(b, z0); + auto zh1 = a.clipZ(b, z1); - auto zh0 = a->clipZ(*b, z0); - auto zh1 = a->clipZ(*b, z1); - - if (!(xh0.has_value() and containsX(&*xh0))) xh0 = std::nullopt; - if (!(xh1.has_value() and containsX(&*xh1))) xh1 = std::nullopt; - if (!(yh0.has_value() and containsY(&*yh0))) yh0 = std::nullopt; - if (!(yh1.has_value() and containsY(&*yh1))) yh1 = std::nullopt; - if (!(zh0.has_value() and containsZ(&*zh0))) zh0 = std::nullopt; - if (!(zh1.has_value() and containsZ(&*zh1))) zh1 = std::nullopt; + if (!(xh0.has_value() and containsX(*xh0))) xh0 = std::nullopt; + if (!(xh1.has_value() and containsX(*xh1))) xh1 = std::nullopt; + if (!(yh0.has_value() and containsY(*yh0))) yh0 = std::nullopt; + if (!(yh1.has_value() and containsY(*yh1))) yh1 = std::nullopt; + if (!(zh0.has_value() and containsZ(*zh0))) zh0 = std::nullopt; + if (!(zh1.has_value() and containsZ(*zh1))) zh1 = std::nullopt; std::optional closest = std::nullopt; - if (xh0.has_value() and - (!closest.has_value() or - a->distanceToSqr(*xh0) < a->distanceToSqr(*closest))) + if (xh0.has_value() and (!closest.has_value() or + a.distanceToSqr(*xh0) < a.distanceToSqr(*closest))) closest = xh0; - if (xh1.has_value() and - (!closest.has_value() or - a->distanceToSqr(*xh1) < a->distanceToSqr(*closest))) + if (xh1.has_value() and (!closest.has_value() or + a.distanceToSqr(*xh1) < a.distanceToSqr(*closest))) closest = xh1; - if (yh0.has_value() and - (!closest.has_value() or - a->distanceToSqr(*yh0) < a->distanceToSqr(*closest))) + if (yh0.has_value() and (!closest.has_value() or + a.distanceToSqr(*yh0) < a.distanceToSqr(*closest))) closest = yh0; - if (yh1.has_value() and - (!closest.has_value() or - a->distanceToSqr(*yh1) < a->distanceToSqr(*closest))) + if (yh1.has_value() and (!closest.has_value() or + a.distanceToSqr(*yh1) < a.distanceToSqr(*closest))) closest = yh1; - if (zh0.has_value() and - (!closest.has_value() or - a->distanceToSqr(*zh0) < a->distanceToSqr(*closest))) + if (zh0.has_value() and (!closest.has_value() or + a.distanceToSqr(*zh0) < a.distanceToSqr(*closest))) closest = zh0; - if (zh1.has_value() and - (!closest.has_value() or - a->distanceToSqr(*zh1) < a->distanceToSqr(*closest))) + if (zh1.has_value() and (!closest.has_value() or + a.distanceToSqr(*zh1) < a.distanceToSqr(*closest))) closest = zh1; if (!closest.has_value()) return nullptr; @@ -299,32 +277,18 @@ HitResult* AABB::clip(Vec3* a, Vec3* b) { return new HitResult(0, 0, 0, face, *closest); } -bool AABB::containsX(Vec3* v) { - if (v == NULL) return false; - return v->y >= y0 && v->y <= y1 && v->z >= z0 && v->z <= z1; +bool AABB::containsX(const Vec3& v) const { + return v.y >= y0 && v.y <= y1 && v.z >= z0 && v.z <= z1; } -bool AABB::containsY(Vec3* v) { - if (v == NULL) return false; - return v->x >= x0 && v->x <= x1 && v->z >= z0 && v->z <= z1; +bool AABB::containsY(const Vec3& v) const { + return v.x >= x0 && v.x <= x1 && v.z >= z0 && v.z <= z1; } -bool AABB::containsZ(Vec3* v) { - if (v == NULL) return false; - return v->x >= x0 && v->x <= x1 && v->y >= y0 && v->y <= y1; +bool AABB::containsZ(const Vec3& v) const { + return v.x >= x0 && v.x <= x1 && v.y >= y0 && v.y <= y1; } -void AABB::set(AABB* b) { - x0 = b->x0; - y0 = b->y0; - z0 = b->z0; - x1 = b->x1; - y1 = b->y1; - z1 = b->z1; -} - -std::wstring AABB::toString() { - return L"box[" + _toString(x0) + L", " + _toString(y0) + - L", " + _toString(z0) + L" -> " + _toString(x1) + - L", " + _toString(y1) + L", " + _toString(z1) + L"]"; +std::wstring AABB::toString() const { + return std::format(L"box[{}, {}, {}, {}, {}, {}]", x0, y0, z0, x1, y1, z1); } diff --git a/Minecraft.World/Util/AABB.h b/Minecraft.World/Util/AABB.h index 205d5ac98..975f6ebcd 100644 --- a/Minecraft.World/Util/AABB.h +++ b/Minecraft.World/Util/AABB.h @@ -36,33 +36,27 @@ public: double x0, y0, z0; double x1, y1, z1; -private: - AABB(double x0, double y0, double z0, double x1, double y1, double z1); AABB() {} + AABB(double x0, double y0, double z0, double x1, double y1, double z1); public: - AABB* set(double x0, double y0, double z0, double x1, double y1, double z1); - AABB* expand(double xa, double ya, double za); - AABB* grow(double xa, double ya, double za); - AABB* minmax(AABB* other); - AABB* cloneMove(double xa, double ya, double za); - double clipXCollide(AABB* c, double xa); - double clipYCollide(AABB* c, double ya); - double clipZCollide(AABB* c, double za); - bool intersects(AABB* c); - bool intersectsInner(AABB* c); - AABB* move(double xa, double ya, double za); + AABB expand(double xa, double ya, double za) const; + AABB grow(double xa, double ya, double za) const; + AABB minmax(const AABB& other) const; + double clipXCollide(const AABB& c, double xa) const; + double clipYCollide(const AABB& c, double ya) const; + double clipZCollide(const AABB& c, double za) const; + bool intersects(const AABB& c) const; + AABB move(double xa, double ya, double za) const; bool intersects(double x02, double y02, double z02, double x12, double y12, - double z12); - bool contains(Vec3* p); - bool containsIncludingLowerBound(Vec3* p); // 4J Added - double getSize(); - AABB* shrink(double xa, double ya, double za); - AABB* copy(); - HitResult* clip(Vec3* a, Vec3* b); - bool containsX(Vec3* v); - bool containsY(Vec3* v); - bool containsZ(Vec3* v); - void set(AABB* b); - std::wstring toString(); + double z12) const; + bool contains(const Vec3& p) const; + bool containsIncludingLowerBound(const Vec3& p) const; // 4J Added + double getSize() const; + AABB shrink(double xa, double ya, double za) const; + HitResult* clip(const Vec3& a, const Vec3& b) const; + bool containsX(const Vec3& v) const; + bool containsY(const Vec3& v) const; + bool containsZ(const Vec3& v) const; + std::wstring toString() const; }; diff --git a/Minecraft.World/Util/Vec3.cpp b/Minecraft.World/Util/Vec3.cpp index 810456f27..00718fabe 100644 --- a/Minecraft.World/Util/Vec3.cpp +++ b/Minecraft.World/Util/Vec3.cpp @@ -150,8 +150,9 @@ void Vec3::zRot(const float degs) { // Returns 0 if this point is within the box // Otherwise returns the distance to the box +// TODO: rewrite this function double Vec3::distanceTo(AABB* box) { - if (box->contains(this)) return 0; + if (box->contains(*this)) return 0; double xd = 0, yd = 0, zd = 0; diff --git a/Minecraft.World/WorldGen/Structures/StructureStart.cpp b/Minecraft.World/WorldGen/Structures/StructureStart.cpp index 9bcb12777..d378e9c33 100644 --- a/Minecraft.World/WorldGen/Structures/StructureStart.cpp +++ b/Minecraft.World/WorldGen/Structures/StructureStart.cpp @@ -138,4 +138,4 @@ bool StructureStart::isValid() { return true; } int StructureStart::getChunkX() { return chunkX; } -int StructureStart::getChunkZ() { return chunkZ; } \ No newline at end of file +int StructureStart::getChunkZ() { return chunkZ; }