Search in sources :

Example 1 with BuildingHospital

use of com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingHospital in project minecolonies by ldtteam.

the class EntityAIWorkHealer method decide.

/**
 * Decide what to do next. Check if all patients are up date, else update their states. Then check if there is any patient we can cure or request things for.
 *
 * @return the next state to go to.
 */
private IAIState decide() {
    if (walkToBuilding()) {
        return DECIDE;
    }
    final BuildingHospital hospital = getOwnBuilding();
    for (final AbstractEntityCitizen citizen : WorldUtil.getEntitiesWithinBuilding(world, AbstractEntityCitizen.class, getOwnBuilding(), cit -> cit.getCitizenDiseaseHandler().isSick())) {
        hospital.checkOrCreatePatientFile(citizen.getCivilianID());
    }
    for (final Patient patient : hospital.getPatients()) {
        final ICitizenData data = hospital.getColony().getCitizenManager().getCivilian(patient.getId());
        if (data == null || !data.getEntity().isPresent() || (data.getEntity().isPresent() && !data.getEntity().get().getCitizenDiseaseHandler().isSick())) {
            hospital.removePatientFile(patient);
            continue;
        }
        final EntityCitizen citizen = (EntityCitizen) data.getEntity().get();
        final String diseaseName = citizen.getCitizenDiseaseHandler().getDisease();
        @Nullable final Disease disease = diseaseName.isEmpty() ? null : IColonyManager.getInstance().getCompatibilityManager().getDisease(diseaseName);
        if (patient.getState() == Patient.PatientState.NEW) {
            this.currentPatient = patient;
            return REQUEST_CURE;
        }
        if (patient.getState() == Patient.PatientState.REQUESTED) {
            if (disease == null) {
                this.currentPatient = patient;
                return CURE;
            }
            if (testRandomCureChance()) {
                this.currentPatient = patient;
                return FREE_CURE;
            }
            if (!InventoryUtils.isItemHandlerFull(citizen.getInventoryCitizen())) {
                if (hasCureInInventory(disease, worker.getInventoryCitizen()) || hasCureInInventory(disease, getOwnBuilding().getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).orElseGet(null))) {
                    this.currentPatient = patient;
                    return CURE;
                }
                final ImmutableList<IRequest<? extends Stack>> list = getOwnBuilding().getOpenRequestsOfType(worker.getCitizenData().getId(), TypeToken.of(Stack.class));
                final ImmutableList<IRequest<? extends Stack>> completed = getOwnBuilding().getCompletedRequestsOfType(worker.getCitizenData(), TypeToken.of(Stack.class));
                for (final ItemStack cure : IColonyManager.getInstance().getCompatibilityManager().getDisease(diseaseName).getCure()) {
                    if (!InventoryUtils.hasItemInItemHandler(worker.getInventoryCitizen(), cure::sameItem)) {
                        if (InventoryUtils.getItemCountInItemHandler(getOwnBuilding().getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).orElseGet(null), stack -> stack.sameItem(cure)) >= cure.getCount()) {
                            needsCurrently = new Tuple<>(stack -> stack.sameItem(cure), cure.getCount());
                            return GATHERING_REQUIRED_MATERIALS;
                        }
                        boolean hasCureRequested = false;
                        for (final IRequest<? extends Stack> request : list) {
                            if (request.getRequest().getStack().sameItem(cure)) {
                                hasCureRequested = true;
                                break;
                            }
                        }
                        for (final IRequest<? extends Stack> request : completed) {
                            if (request.getRequest().getStack().sameItem(cure)) {
                                hasCureRequested = true;
                                break;
                            }
                        }
                        if (!hasCureRequested) {
                            patient.setState(Patient.PatientState.NEW);
                            break;
                        }
                    }
                }
            } else {
                data.triggerInteraction(new StandardInteraction(new TranslationTextComponent(PATIENT_FULL_INVENTORY), ChatPriority.BLOCKING));
            }
        }
        if (patient.getState() == Patient.PatientState.TREATED) {
            if (disease == null) {
                this.currentPatient = patient;
                return CURE;
            }
            if (!hasCureInInventory(disease, citizen.getInventoryCitizen())) {
                patient.setState(Patient.PatientState.NEW);
                return DECIDE;
            }
        }
    }
    for (final PlayerEntity player : WorldUtil.getEntitiesWithinBuilding(world, PlayerEntity.class, getOwnBuilding(), player -> player.getHealth() < player.getMaxHealth() - 10 - (2 * getOwnBuilding().getBuildingLevel()))) {
        playerToHeal = player;
        return CURE_PLAYER;
    }
    final ICitizenData data = getOwnBuilding().getColony().getCitizenManager().getRandomCitizen();
    if (data.getEntity().isPresent() && data.getEntity().get().getHealth() < 10.0 && BlockPosUtil.getDistance2D(data.getEntity().get().blockPosition(), getOwnBuilding().getPosition()) < getOwnBuilding().getBuildingLevel() * 40) {
        remotePatient = data;
        return WANDER;
    }
    return DECIDE;
}
Also used : IItemHandler(net.minecraftforge.items.IItemHandler) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) ICitizenData(com.minecolonies.api.colony.ICitizenData) TypeToken(com.google.common.reflect.TypeToken) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) ItemStack(net.minecraft.item.ItemStack) ImmutableList(com.google.common.collect.ImmutableList) IAIState(com.minecolonies.api.entity.ai.statemachine.states.IAIState) JobHealer(com.minecolonies.coremod.colony.jobs.JobHealer) AITarget(com.minecolonies.api.entity.ai.statemachine.AITarget) com.minecolonies.api.util(com.minecolonies.api.util) Network(com.minecolonies.coremod.Network) PATIENT_FULL_INVENTORY(com.minecolonies.api.util.constant.TranslationConstants.PATIENT_FULL_INVENTORY) StreamParticleEffectMessage(com.minecolonies.coremod.network.messages.client.StreamParticleEffectMessage) Stack(com.minecolonies.api.colony.requestsystem.requestable.Stack) AbstractEntityAIInteract(com.minecolonies.coremod.entity.ai.basic.AbstractEntityAIInteract) PlayerEntity(net.minecraft.entity.player.PlayerEntity) CircleParticleEffectMessage(com.minecolonies.coremod.network.messages.client.CircleParticleEffectMessage) IColonyManager(com.minecolonies.api.colony.IColonyManager) ChatPriority(com.minecolonies.api.colony.interactionhandling.ChatPriority) StandardInteraction(com.minecolonies.coremod.colony.interactionhandling.StandardInteraction) BlockPos(net.minecraft.util.math.BlockPos) AIWorkerState(com.minecolonies.api.entity.ai.statemachine.states.AIWorkerState) ParticleTypes(net.minecraft.particles.ParticleTypes) Nullable(org.jetbrains.annotations.Nullable) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) BuildingHospital(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingHospital) EntityCitizen(com.minecolonies.coremod.entity.citizen.EntityCitizen) CapabilityItemHandler(net.minecraftforge.items.CapabilityItemHandler) NotNull(org.jetbrains.annotations.NotNull) BuildingHospital(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingHospital) StandardInteraction(com.minecolonies.coremod.colony.interactionhandling.StandardInteraction) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) ICitizenData(com.minecolonies.api.colony.ICitizenData) ItemStack(net.minecraft.item.ItemStack) Stack(com.minecolonies.api.colony.requestsystem.requestable.Stack) PlayerEntity(net.minecraft.entity.player.PlayerEntity) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) EntityCitizen(com.minecolonies.coremod.entity.citizen.EntityCitizen) ItemStack(net.minecraft.item.ItemStack) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with BuildingHospital

use of com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingHospital in project minecolonies by ldtteam.

the class EntityAISickTask method cure.

/**
 * Cure the citizen.
 */
private void cure() {
    final Disease disease = IColonyManager.getInstance().getCompatibilityManager().getDisease(citizen.getCitizenDiseaseHandler().getDisease());
    if (disease != null) {
        for (final ItemStack cure : disease.getCure()) {
            final int slot = InventoryUtils.findFirstSlotInProviderNotEmptyWith(citizen, stack -> stack.sameItem(cure));
            if (slot != -1) {
                citizenData.getInventory().extractItem(slot, 1, false);
            }
        }
    }
    if (usedBed != null) {
        final BlockPos hospitalPos = citizen.getCitizenColonyHandler().getColony().getBuildingManager().getBestBuilding(citizen, BuildingHospital.class);
        final IColony colony = citizen.getCitizenColonyHandler().getColony();
        final IBuilding hospital = colony.getBuildingManager().getBuilding(hospitalPos);
        ((BuildingHospital) hospital).registerPatient(usedBed, 0);
        usedBed = null;
        citizen.getCitizenData().setBedPos(BlockPos.ZERO);
    }
    citizen.setItemInHand(Hand.MAIN_HAND, ItemStack.EMPTY);
    citizenData.markDirty();
    citizen.getCitizenDiseaseHandler().cure();
    citizen.setHealth(citizen.getMaxHealth());
    reset();
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding) BuildingHospital(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingHospital) IColony(com.minecolonies.api.colony.IColony) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack)

Example 3 with BuildingHospital

use of com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingHospital in project minecolonies by ldtteam.

the class EntityAISickTask method waitForCure.

/**
 * Stay in bed while waiting to be cured.
 *
 * @return the next state to go to.
 */
private DiseaseState waitForCure() {
    final IColony colony = citizenData.getColony();
    placeToPath = colony.getBuildingManager().getBestBuilding(citizen, BuildingHospital.class);
    if (placeToPath == null) {
        return SEARCH_HOSPITAL;
    }
    final DiseaseState state = checkForCure();
    if (state == APPLY_CURE) {
        return APPLY_CURE;
    } else if (state == IDLE) {
        reset();
        return IDLE;
    }
    if (citizen.getRandom().nextInt(10000) < CHANCE_FOR_RANDOM_CURE) {
        cure();
        return IDLE;
    }
    if (citizen.getCitizenSleepHandler().isAsleep()) {
        final BlockPos hospital = colony.getBuildingManager().getBestBuilding(citizen, BuildingHospital.class);
        if (hospital != null) {
            final IBuilding building = colony.getBuildingManager().getBuilding(hospital);
            if (building instanceof BuildingHospital && !((BuildingHospital) building).getBedList().contains(citizen.getCitizenSleepHandler().getBedLocation())) {
                citizen.getCitizenSleepHandler().onWakeUp();
            }
        }
    }
    if (!citizen.getCitizenSleepHandler().isAsleep() && BlockPosUtil.getDistance2D(placeToPath, citizen.blockPosition()) > MINIMUM_DISTANCE_TO_HOSPITAL) {
        return GO_TO_HOSPITAL;
    }
    if (!citizen.getCitizenSleepHandler().isAsleep()) {
        return FIND_EMPTY_BED;
    }
    return WAIT_FOR_CURE;
}
Also used : BuildingHospital(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingHospital) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) IColony(com.minecolonies.api.colony.IColony) BlockPos(net.minecraft.util.math.BlockPos) DiseaseState(com.minecolonies.coremod.entity.ai.minimal.EntityAISickTask.DiseaseState)

Example 4 with BuildingHospital

use of com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingHospital in project minecolonies by ldtteam.

the class EntityAISickTask method findEmptyBed.

/**
 * Find an empty bed to ly in.
 *
 * @return the next state to go to.
 */
private DiseaseState findEmptyBed() {
    // Finding bed
    if (usedBed == null && citizen.getCitizenData() != null) {
        this.usedBed = citizen.getCitizenData().getBedPos();
        if (citizen.getCitizenData().getBedPos().equals(BlockPos.ZERO)) {
            this.usedBed = null;
        }
    }
    final BlockPos hospitalPos = citizen.getCitizenColonyHandler().getColony().getBuildingManager().getBestBuilding(citizen, BuildingHospital.class);
    final IColony colony = citizen.getCitizenColonyHandler().getColony();
    final IBuilding hospital = colony.getBuildingManager().getBuilding(hospitalPos);
    if (hospital instanceof BuildingHospital) {
        if (usedBed != null && !((BuildingHospital) hospital).getBedList().contains(usedBed)) {
            usedBed = null;
        }
        if (usedBed == null) {
            for (final BlockPos pos : ((BuildingHospital) hospital).getBedList()) {
                final World world = citizen.level;
                BlockState state = world.getBlockState(pos);
                if (state.getBlock().is(BlockTags.BEDS) && !state.getValue(BedBlock.OCCUPIED) && state.getValue(BedBlock.PART).equals(BedPart.HEAD) && world.isEmptyBlock(pos.above())) {
                    usedBed = pos;
                    ((BuildingHospital) hospital).registerPatient(usedBed, citizen.getCivilianID());
                    return FIND_EMPTY_BED;
                }
            }
            if (usedBed == null) {
                return WAIT_FOR_CURE;
            }
        }
        if (citizen.isWorkerAtSiteWithMove(usedBed, 3)) {
            waitingTicks++;
            if (!citizen.getCitizenSleepHandler().trySleep(usedBed)) {
                ((BuildingHospital) hospital).registerPatient(usedBed, 0);
                citizen.getCitizenData().setBedPos(BlockPos.ZERO);
                usedBed = null;
            }
        }
    }
    if (waitingTicks > GOING_TO_BED_ATTEMPTS) {
        waitingTicks = 0;
        return WAIT_FOR_CURE;
    }
    return FIND_EMPTY_BED;
}
Also used : BlockState(net.minecraft.block.BlockState) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) BuildingHospital(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingHospital) IColony(com.minecolonies.api.colony.IColony) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World)

Example 5 with BuildingHospital

use of com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingHospital in project minecolonies by Minecolonies.

the class EntityAISickTask method waitForCure.

/**
 * Stay in bed while waiting to be cured.
 *
 * @return the next state to go to.
 */
private DiseaseState waitForCure() {
    final IColony colony = citizenData.getColony();
    placeToPath = colony.getBuildingManager().getBestBuilding(citizen, BuildingHospital.class);
    if (placeToPath == null) {
        return SEARCH_HOSPITAL;
    }
    final DiseaseState state = checkForCure();
    if (state == APPLY_CURE) {
        return APPLY_CURE;
    } else if (state == IDLE) {
        reset();
        return IDLE;
    }
    if (citizen.getRandom().nextInt(10000) < CHANCE_FOR_RANDOM_CURE) {
        cure();
        return IDLE;
    }
    if (citizen.getCitizenSleepHandler().isAsleep()) {
        final BlockPos hospital = colony.getBuildingManager().getBestBuilding(citizen, BuildingHospital.class);
        if (hospital != null) {
            final IBuilding building = colony.getBuildingManager().getBuilding(hospital);
            if (building instanceof BuildingHospital && !((BuildingHospital) building).getBedList().contains(citizen.getCitizenSleepHandler().getBedLocation())) {
                citizen.getCitizenSleepHandler().onWakeUp();
            }
        }
    }
    if (!citizen.getCitizenSleepHandler().isAsleep() && BlockPosUtil.getDistance2D(placeToPath, citizen.blockPosition()) > MINIMUM_DISTANCE_TO_HOSPITAL) {
        return GO_TO_HOSPITAL;
    }
    if (!citizen.getCitizenSleepHandler().isAsleep()) {
        return FIND_EMPTY_BED;
    }
    return WAIT_FOR_CURE;
}
Also used : BuildingHospital(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingHospital) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) IColony(com.minecolonies.api.colony.IColony) BlockPos(net.minecraft.util.math.BlockPos) DiseaseState(com.minecolonies.coremod.entity.ai.minimal.EntityAISickTask.DiseaseState)

Aggregations

BuildingHospital (com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingHospital)8 BlockPos (net.minecraft.util.math.BlockPos)8 IColony (com.minecolonies.api.colony.IColony)6 IBuilding (com.minecolonies.api.colony.buildings.IBuilding)6 ItemStack (net.minecraft.item.ItemStack)4 ImmutableList (com.google.common.collect.ImmutableList)2 TypeToken (com.google.common.reflect.TypeToken)2 ICitizenData (com.minecolonies.api.colony.ICitizenData)2 IColonyManager (com.minecolonies.api.colony.IColonyManager)2 ChatPriority (com.minecolonies.api.colony.interactionhandling.ChatPriority)2 IRequest (com.minecolonies.api.colony.requestsystem.request.IRequest)2 Stack (com.minecolonies.api.colony.requestsystem.requestable.Stack)2 AITarget (com.minecolonies.api.entity.ai.statemachine.AITarget)2 AIWorkerState (com.minecolonies.api.entity.ai.statemachine.states.AIWorkerState)2 IAIState (com.minecolonies.api.entity.ai.statemachine.states.IAIState)2 AbstractEntityCitizen (com.minecolonies.api.entity.citizen.AbstractEntityCitizen)2 com.minecolonies.api.util (com.minecolonies.api.util)2 PATIENT_FULL_INVENTORY (com.minecolonies.api.util.constant.TranslationConstants.PATIENT_FULL_INVENTORY)2 Network (com.minecolonies.coremod.Network)2 StandardInteraction (com.minecolonies.coremod.colony.interactionhandling.StandardInteraction)2