mirror of
https://github.com/4jcraft/4jcraft.git
synced 2026-04-25 06:53:37 +00:00
# Conflicts: # Minecraft.Client/Network/PlayerChunkMap.cpp # Minecraft.Client/Network/PlayerList.cpp # Minecraft.Client/Network/ServerChunkCache.cpp # Minecraft.Client/Platform/Common/Consoles_App.cpp # Minecraft.Client/Platform/Common/DLC/DLCManager.cpp # Minecraft.Client/Platform/Common/GameRules/LevelGenerationOptions.cpp # Minecraft.Client/Platform/Common/GameRules/LevelRuleset.cpp # Minecraft.Client/Platform/Common/Tutorial/Tutorial.cpp # Minecraft.Client/Platform/Common/Tutorial/TutorialTask.cpp # Minecraft.Client/Platform/Common/UI/IUIScene_CreativeMenu.cpp # Minecraft.Client/Platform/Common/UI/UIComponent_Panorama.cpp # Minecraft.Client/Platform/Common/UI/UIController.cpp # Minecraft.Client/Platform/Common/UI/UIController.h # Minecraft.Client/Platform/Extrax64Stubs.cpp # Minecraft.Client/Platform/Windows64/4JLibs/inc/4J_Input.h # Minecraft.Client/Platform/Windows64/4JLibs/inc/4J_Storage.h # Minecraft.Client/Player/EntityTracker.cpp # Minecraft.Client/Player/ServerPlayer.cpp # Minecraft.Client/Rendering/EntityRenderers/PlayerRenderer.cpp # Minecraft.Client/Textures/Packs/DLCTexturePack.cpp # Minecraft.Client/Textures/Stitching/StitchedTexture.cpp # Minecraft.Client/Textures/Stitching/TextureMap.cpp # Minecraft.Client/Textures/Textures.cpp # Minecraft.World/Blocks/NotGateTile.cpp # Minecraft.World/Blocks/PressurePlateTile.cpp # Minecraft.World/Blocks/TileEntities/PotionBrewing.cpp # Minecraft.World/Enchantments/EnchantmentHelper.cpp # Minecraft.World/Entities/HangingEntity.cpp # Minecraft.World/Entities/LeashFenceKnotEntity.cpp # Minecraft.World/Entities/LivingEntity.cpp # Minecraft.World/Entities/Mobs/Boat.cpp # Minecraft.World/Entities/Mobs/Minecart.cpp # Minecraft.World/Entities/Mobs/Witch.cpp # Minecraft.World/Entities/SyncedEntityData.cpp # Minecraft.World/Items/LeashItem.cpp # Minecraft.World/Items/PotionItem.cpp # Minecraft.World/Level/BaseMobSpawner.cpp # Minecraft.World/Level/CustomLevelSource.cpp # Minecraft.World/Level/Level.cpp # Minecraft.World/Level/Storage/DirectoryLevelStorage.cpp # Minecraft.World/Level/Storage/McRegionLevelStorage.cpp # Minecraft.World/Level/Storage/RegionFileCache.cpp # Minecraft.World/Player/Player.cpp # Minecraft.World/WorldGen/Biomes/BiomeCache.cpp # Minecraft.World/WorldGen/Features/RandomScatteredLargeFeature.cpp # Minecraft.World/WorldGen/Layers/BiomeOverrideLayer.cpp
125 lines
4.3 KiB
C++
125 lines
4.3 KiB
C++
#include "../../Platform/stdafx.h"
|
|
#include "../../Headers/net.minecraft.world.entity.ai.control.h"
|
|
#include "../../Headers/net.minecraft.world.entity.ai.navigation.h"
|
|
#include "../../Headers/net.minecraft.world.entity.ai.util.h"
|
|
#include "../../Headers/net.minecraft.world.entity.ai.village.h"
|
|
#include "../../Headers/net.minecraft.world.entity.h"
|
|
#include "../../Headers/net.minecraft.world.level.h"
|
|
#include "MoveThroughVillageGoal.h"
|
|
#include <limits>
|
|
#include "../Navigation/Path.h"
|
|
|
|
MoveThroughVillageGoal::MoveThroughVillageGoal(PathfinderMob* mob,
|
|
double speedModifier,
|
|
bool onlyAtNight) {
|
|
path = nullptr;
|
|
doorInfo = std::weak_ptr<DoorInfo>();
|
|
|
|
this->mob = mob;
|
|
this->speedModifier = speedModifier;
|
|
this->onlyAtNight = onlyAtNight;
|
|
setRequiredControlFlags(Control::MoveControlFlag);
|
|
}
|
|
|
|
MoveThroughVillageGoal::~MoveThroughVillageGoal() {
|
|
if (path != nullptr) delete path;
|
|
}
|
|
|
|
bool MoveThroughVillageGoal::canUse() {
|
|
updateVisited();
|
|
|
|
if (onlyAtNight && mob->level->isDay()) return false;
|
|
|
|
std::shared_ptr<Village> village = mob->level->villages->getClosestVillage(
|
|
Mth::floor(mob->x), Mth::floor(mob->y), Mth::floor(mob->z), 0);
|
|
if (village == nullptr) return false;
|
|
|
|
std::shared_ptr<DoorInfo> _doorInfo = getNextDoorInfo(village);
|
|
if (_doorInfo == nullptr) return false;
|
|
doorInfo = _doorInfo;
|
|
|
|
bool oldCanOpenDoors = mob->getNavigation()->canOpenDoors();
|
|
mob->getNavigation()->setCanOpenDoors(false);
|
|
delete path;
|
|
|
|
path = mob->getNavigation()->createPath(_doorInfo->x, _doorInfo->y,
|
|
_doorInfo->z);
|
|
mob->getNavigation()->setCanOpenDoors(oldCanOpenDoors);
|
|
if (path != nullptr) return true;
|
|
|
|
Vec3 towards(_doorInfo->x, _doorInfo->y, _doorInfo->z);
|
|
auto pos = RandomPos::getPosTowards(
|
|
std::dynamic_pointer_cast<PathfinderMob>(mob->shared_from_this()), 10,
|
|
7, &towards);
|
|
if (!pos.has_value()) return false;
|
|
mob->getNavigation()->setCanOpenDoors(false);
|
|
delete path;
|
|
path = mob->getNavigation()->createPath(pos->x, pos->y, pos->z);
|
|
mob->getNavigation()->setCanOpenDoors(oldCanOpenDoors);
|
|
return path != nullptr;
|
|
}
|
|
|
|
bool MoveThroughVillageGoal::canContinueToUse() {
|
|
if (mob->getNavigation()->isDone()) return false;
|
|
float dist = mob->bbWidth + 4.f;
|
|
std::shared_ptr<DoorInfo> _doorInfo = doorInfo.lock();
|
|
if (_doorInfo == nullptr) return false;
|
|
|
|
return mob->distanceToSqr(_doorInfo->x, _doorInfo->y, _doorInfo->z) >
|
|
dist * dist;
|
|
}
|
|
|
|
void MoveThroughVillageGoal::start() {
|
|
mob->getNavigation()->moveTo(path, speedModifier);
|
|
path = nullptr;
|
|
}
|
|
|
|
void MoveThroughVillageGoal::stop() {
|
|
std::shared_ptr<DoorInfo> _doorInfo = doorInfo.lock();
|
|
if (_doorInfo == nullptr) return;
|
|
|
|
if (mob->getNavigation()->isDone() ||
|
|
mob->distanceToSqr(_doorInfo->x, _doorInfo->y, _doorInfo->z) < 4 * 4) {
|
|
visited.push_back(doorInfo);
|
|
}
|
|
}
|
|
|
|
std::shared_ptr<DoorInfo> MoveThroughVillageGoal::getNextDoorInfo(
|
|
std::shared_ptr<Village> village) {
|
|
std::shared_ptr<DoorInfo> closest = nullptr;
|
|
int closestDistSqr = std::numeric_limits<int>::max();
|
|
std::vector<std::shared_ptr<DoorInfo> >* doorInfos =
|
|
village->getDoorInfos();
|
|
// for (DoorInfo di : doorInfos)
|
|
for (auto it = doorInfos->begin(); it != doorInfos->end(); ++it) {
|
|
std::shared_ptr<DoorInfo> di = *it;
|
|
int distSqr = di->distanceToSqr(Mth::floor(mob->x), Mth::floor(mob->y),
|
|
Mth::floor(mob->z));
|
|
if (distSqr < closestDistSqr) {
|
|
if (hasVisited(di)) continue;
|
|
closest = di;
|
|
closestDistSqr = distSqr;
|
|
}
|
|
}
|
|
return closest;
|
|
}
|
|
|
|
bool MoveThroughVillageGoal::hasVisited(std::shared_ptr<DoorInfo> di) {
|
|
// for (DoorInfo di2 : visited)
|
|
for (auto it = visited.begin(); it != visited.end();) {
|
|
std::shared_ptr<DoorInfo> di2 = (*it).lock();
|
|
if (di2 == nullptr) {
|
|
it = visited.erase(it);
|
|
} else {
|
|
if (di->x == di2->x && di->y == di2->y && di->z == di2->z)
|
|
return true;
|
|
++it;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void MoveThroughVillageGoal::updateVisited() {
|
|
if (visited.size() > 15) visited.erase(visited.begin());
|
|
}
|