Skip to content

Commit

Permalink
Add auto-hire from training centers (ldtteam#5669)
Browse files Browse the repository at this point in the history
Guard towers will now attempt to hire the best from the respective training centers before hiring unemployed
There is a new button in the Guard towers to turn off trainee hiring, it defaults to on.
Training centers are now 'homes' for their workers
Added bed registration to training centers, though not all schematics have beds yet.
  • Loading branch information
Mekle001 authored Aug 29, 2020
1 parent 00859ab commit 3f363c3
Show file tree
Hide file tree
Showing 14 changed files with 284 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.minecolonies.api.colony.buildings;

import java.util.List;
import org.jetbrains.annotations.NotNull;
import net.minecraft.util.math.BlockPos;

public interface IBuildingBedProvider
{
/**
* Gets a list of all beds in this building.
*
* @return a list of all beds in this building.
*/
@NotNull
public List<BlockPos> getBedList();
}
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ public final class WindowConstants
public static final String GUI_BUTTON_ASSIGNMENT_MODE = "assign";
public static final String GUI_BUTTON_PATROL_MODE = "patrol";
public static final String GUI_BUTTON_RETRIEVAL_MODE = "retrieve";
public static final String GUI_BUTTON_TRAINEE_MODE = "trainees";
public static final String GUI_BUTTON_SET_TARGET = "setTarget";
public static final String GUI_BUTTON_RECALCULATE = "recalculate";
//GUI Switches
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public class WindowGuardControl extends AbstractWindowSkeleton
*/
private boolean tightGrouping = true;

/**
* Whether to hire from training facility
*/
private boolean hireTrainees = true;

/**
* The GuardTask of the guard.
*/
Expand Down Expand Up @@ -90,6 +95,7 @@ public WindowGuardControl(final AbstractBuildingGuards.View building)

registerButton(GUI_BUTTON_PATROL_MODE, this::switchPatrolMode);
registerButton(GUI_BUTTON_RETRIEVAL_MODE, this::switchRetrievalMode);
registerButton(GUI_BUTTON_TRAINEE_MODE, this::switchTraineeMode);
registerButton(GUI_BUTTON_RECALCULATE, this::recalculate);
registerButton(GUI_BUTTON_SET_TARGET, this::setTarget);

Expand Down Expand Up @@ -205,6 +211,7 @@ private void pullInfoFromHut()
{
this.patrolManually = building.isPatrolManually();
this.retrieveOnLowHealth = building.isRetrieveOnLowHealth();
this.hireTrainees = building.isHireTrainees();
this.tightGrouping = building.isTightGrouping();
this.task = building.getTask();
this.patrolTargets = building.getPatrolTargets();
Expand All @@ -217,7 +224,7 @@ private void sendChangesToServer()
{
final ResourceLocation resourceName = building.getGuardType() == null ? new ResourceLocation("") : building.getGuardType().getRegistryName();
Network.getNetwork()
.sendToServer(new GuardTaskMessage(building, resourceName, building.isAssignManually(), patrolManually, retrieveOnLowHealth, task.ordinal(), tightGrouping));
.sendToServer(new GuardTaskMessage(building, resourceName, building.isAssignManually(), patrolManually, retrieveOnLowHealth, task.ordinal(), tightGrouping, hireTrainees));
}

/**
Expand Down Expand Up @@ -367,6 +374,17 @@ private void switchRetrievalMode()
this.findPaneOfTypeByID(GUI_BUTTON_RETRIEVAL_MODE, Button.class).setLabel(retrieveOnLowHealth ? GUI_SWITCH_ON : GUI_SWITCH_OFF);
}

/**
* Switch the trainee mode.
*/
private void switchTraineeMode()
{
building.setHireTrainees(!building.isHireTrainees());
pullInfoFromHut();
sendChangesToServer();
this.findPaneOfTypeByID(GUI_BUTTON_TRAINEE_MODE, Button.class).setLabel(hireTrainees ? GUI_SWITCH_ON : GUI_SWITCH_OFF);
}

/**
* Switch the patrol mode.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public class WindowHutGuardTower extends AbstractWindowWorkerBuilding<AbstractBu
*/
private boolean tightGrouping = true;

/**
* Whether to hire from training facilities
*/
private boolean hireTrainees = true;

/**
* The GuardTask of the guard.
*/
Expand Down Expand Up @@ -103,6 +108,7 @@ public WindowHutGuardTower(final AbstractBuildingGuards.View building)
registerButton(GUI_BUTTON_ASSIGNMENT_MODE, this::switchAssignmentMode);
registerButton(GUI_BUTTON_PATROL_MODE, this::switchPatrolMode);
registerButton(GUI_BUTTON_RETRIEVAL_MODE, this::switchRetrievalMode);
registerButton(GUI_BUTTON_TRAINEE_MODE, this::switchTraineeMode);
registerButton(GUI_BUTTON_SET_TARGET, this::setTarget);
registerButton(GUI_BUTTON_RECALCULATE, this::recalculate);
registerButton(BUTTON_GET_TOOL, this::getTool);
Expand Down Expand Up @@ -411,6 +417,7 @@ private void pullInfoFromHut()
this.patrolManually = building.isPatrolManually();
this.retrieveOnLowHealth = building.isRetrieveOnLowHealth();
this.tightGrouping = building.isTightGrouping();
this.hireTrainees = building.isHireTrainees();
this.task = building.getTask();
this.job = building.getGuardType();
this.patrolTargets = building.getPatrolTargets();
Expand Down Expand Up @@ -523,6 +530,17 @@ private void switchRetrievalMode()
this.findPaneOfTypeByID(GUI_BUTTON_RETRIEVAL_MODE, Button.class).setLabel(retrieveOnLowHealth ? GUI_SWITCH_ON : GUI_SWITCH_OFF);
}

/**
* Switch the trainee mode.
*/
private void switchTraineeMode()
{
building.setHireTrainees(!building.isHireTrainees());
pullInfoFromHut();
sendChangesToServer();
this.findPaneOfTypeByID(GUI_BUTTON_TRAINEE_MODE, Button.class).setLabel(hireTrainees ? GUI_SWITCH_ON : GUI_SWITCH_OFF);
}

/**
* Switch the patrol mode.
*/
Expand All @@ -549,6 +567,6 @@ private void switchAssignmentMode()
private void sendChangesToServer()
{
final ResourceLocation resourceName = building.getGuardType() == null ? new ResourceLocation("") : building.getGuardType().getRegistryName();
Network.getNetwork().sendToServer(new GuardTaskMessage(building, resourceName, assignManually, patrolManually, retrieveOnLowHealth, task.ordinal(), tightGrouping));
Network.getNetwork().sendToServer(new GuardTaskMessage(building, resourceName, assignManually, patrolManually, retrieveOnLowHealth, task.ordinal(), tightGrouping, hireTrainees));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.minecolonies.api.colony.IColony;
import com.minecolonies.api.colony.IColonyManager;
import com.minecolonies.api.colony.IColonyView;
import com.minecolonies.api.colony.buildings.HiringMode;
import com.minecolonies.api.colony.buildings.IBuilding;
import com.minecolonies.api.colony.buildings.IGuardBuilding;
import com.minecolonies.api.colony.buildings.views.MobEntryView;
Expand All @@ -27,6 +28,8 @@
import com.minecolonies.coremod.Network;
import com.minecolonies.coremod.client.gui.WindowHutGuardTower;
import com.minecolonies.coremod.colony.jobs.AbstractJobGuard;
import com.minecolonies.coremod.colony.jobs.JobArcherTraining;
import com.minecolonies.coremod.colony.jobs.JobCombatTraining;
import com.minecolonies.coremod.colony.requestsystem.locations.EntityLocation;
import com.minecolonies.coremod.entity.ai.citizen.guard.AbstractEntityAIGuard;
import com.minecolonies.coremod.items.ItemBannerRallyGuards;
Expand Down Expand Up @@ -54,6 +57,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.stream.Collectors;

import static com.minecolonies.api.research.util.ResearchConstants.ARROW_ITEMS;
import static com.minecolonies.api.util.constant.CitizenConstants.*;
Expand All @@ -77,6 +81,7 @@ public abstract class AbstractBuildingGuards extends AbstractBuildingWorker impl
private static final String NBT_GUARD = "guard";
private static final String NBT_MOBS = "mobs";
private static final String NBT_MOB_VIEW = "mobview";
private static final String NBT_RECRUIT = "recruitTrainees";

////// --------------------------- NBTConstants --------------------------- \\\\\\

Expand Down Expand Up @@ -168,6 +173,11 @@ public abstract class AbstractBuildingGuards extends AbstractBuildingWorker impl
*/
private BlockPos tempNextPatrolPoint = null;

/**
* Whether or not to hire from the trainee facilities
*/
private boolean hireTrainees = true;

/**
* The abstract constructor of the building.
*
Expand Down Expand Up @@ -281,6 +291,10 @@ public void deserializeNBT(final CompoundNBT compound)
assignManually = compound.getBoolean(NBT_ASSIGN);
retrieveOnLowHealth = compound.getBoolean(NBT_RETRIEVE);
patrolManually = compound.getBoolean(NBT_PATROL);
if(compound.contains(NBT_RECRUIT))
{
hireTrainees = compound.getBoolean(NBT_RECRUIT);
}
if (compound.keySet().contains(NBT_TIGHT_GROUPING))
{
tightGrouping = compound.getBoolean(NBT_TIGHT_GROUPING);
Expand Down Expand Up @@ -323,6 +337,7 @@ public CompoundNBT serializeNBT()
compound.putBoolean(NBT_RETRIEVE, retrieveOnLowHealth);
compound.putBoolean(NBT_PATROL, patrolManually);
compound.putBoolean(NBT_TIGHT_GROUPING, tightGrouping);
compound.putBoolean(NBT_RECRUIT, hireTrainees);

@NotNull final ListNBT wayPointTagList = new ListNBT();
for (@NotNull final BlockPos pos : patrolTargets)
Expand Down Expand Up @@ -364,8 +379,8 @@ public void removeCitizen(final ICitizenData citizen)
optCitizen.get().setItemStackToSlot(EquipmentSlotType.MAINHAND, ItemStackUtils.EMPTY);
optCitizen.get().setItemStackToSlot(EquipmentSlotType.OFFHAND, ItemStackUtils.EMPTY);
}
citizen.setHomeBuilding(null);
}
citizen.setHomeBuilding(null);
super.removeCitizen(citizen);
}

Expand All @@ -377,6 +392,7 @@ public void serializeToView(@NotNull final PacketBuffer buf)
buf.writeBoolean(retrieveOnLowHealth);
buf.writeBoolean(patrolManually);
buf.writeBoolean(tightGrouping);
buf.writeBoolean(hireTrainees);
buf.writeInt(task.ordinal());
buf.writeString(job == null ? "" : job.getRegistryName().toString());
buf.writeInt(patrolTargets.size());
Expand Down Expand Up @@ -454,7 +470,32 @@ public PlayerEntity getPlayerToFollowOrRally()
@Override
public void onColonyTick(@NotNull final IColony colony)
{
super.onColonyTick(colony);

// If we have no active worker, attempt to grab one from the appropriate trainer
if (hireTrainees && !isFull() && ((getBuildingLevel() > 0 && isBuilt()))
&& (this.getHiringMode() == HiringMode.DEFAULT && !this.getColony().isManualHiring() || this.getHiringMode() == HiringMode.AUTO))
{
ICitizenData trainingCitizen = null;
int maxSkill = 0;

for(ICitizenData trainee:colony.getCitizenManager().getCitizens())
{
if((this.getGuardType() == ModGuardTypes.ranger && trainee instanceof JobArcherTraining) || (this.getGuardType() == ModGuardTypes.knight && trainee instanceof JobCombatTraining)
&& trainee.getCitizenSkillHandler().getLevel(job.getPrimarySkill()) > maxSkill)
{
maxSkill = trainee.getCitizenSkillHandler().getLevel(job.getPrimarySkill());
trainingCitizen = trainee;
}
}

if(trainingCitizen != null )
{
assignCitizen(trainingCitizen);
}
}

super.onColonyTick(colony);

if (patrolTimer > 0 && task == GuardTask.PATROL)
{
patrolTimer--;
Expand Down Expand Up @@ -621,6 +662,11 @@ public static class View extends AbstractBuildingWorker.View
*/
private boolean tightGrouping = true;

/**
* Indicates whether to hire from trainee facilities first
*/
private boolean hireTrainees = true;

/**
* The list of manual patrol targets.
*/
Expand Down Expand Up @@ -676,6 +722,7 @@ public void deserialize(@NotNull final PacketBuffer buf)
retrieveOnLowHealth = buf.readBoolean();
patrolManually = buf.readBoolean();
tightGrouping = buf.readBoolean();
hireTrainees = buf.readBoolean();


task = GuardTask.values()[buf.readInt()];
Expand Down Expand Up @@ -762,6 +809,23 @@ public boolean isTightGrouping()
return tightGrouping;
}

/**
* Is hiring from training facilities enabled
* @return
*/
public boolean isHireTrainees()
{
return hireTrainees;
}

/**
* Set whether or not to hire from training facilities
*/
public void setHireTrainees(final boolean hireTrainees)
{
this.hireTrainees = hireTrainees;
}

public void setPatrolManually(final boolean patrolManually)
{
this.patrolManually = patrolManually;
Expand Down Expand Up @@ -1140,4 +1204,21 @@ public boolean canWorkDuringTheRain()
{
return true;
}

/**
* is hiring from training facilities enabled
* @return
*/
public boolean isHireTrainees()
{
return hireTrainees;
}

/**
* set hiring from training facilities
*/
public void setHireTrainees(boolean hireTrainees)
{
this.hireTrainees = hireTrainees;
}
}
Loading

0 comments on commit 3f363c3

Please sign in to comment.