package space.m0e.quga.oop.lab56maven.handlers; import javafx.application.Platform; import javafx.event.ActionEvent; import javafx.event.EventHandler; import space.m0e.quga.oop.lab56maven.Main; import space.m0e.quga.oop.lab56maven.decisions.*; import space.m0e.quga.oop.lab56maven.entities.common.Ability; import space.m0e.quga.oop.lab56maven.entities.common.Coordinates; import space.m0e.quga.oop.lab56maven.entities.common.Item; import space.m0e.quga.oop.lab56maven.entities.macro.Fortress; import space.m0e.quga.oop.lab56maven.entities.micro.Dwarf; import space.m0e.quga.oop.lab56maven.entities.micro.Nobel; import java.util.Random; import java.util.concurrent.atomic.AtomicBoolean; public class DwarfHandler implements EventHandler { public Dwarf getDwarf() { return dwarf; } public int getTimer() { return timer; } private int timer = 0; public void setDecision(Decision decision) { this.decision = decision; } private Decision decision; private static final String[] availableDecisions = new String[]{ "GoToWorkstation", "GoToDwarvesContainer", "GoToThroneRoom", "GoTrading" }; private static String lastDecision; public void setDwarf(Dwarf dwarf) { this.dwarf = dwarf; } private Dwarf dwarf; public DwarfHandler(Dwarf dwarf) { super(); // this.setName(String.format("DwarfThread_%s", dwarf.getFullName())); this.dwarf = dwarf; } @Override public void handle(ActionEvent e) { if (dwarf != null && !dwarf.isActive()) { // try { // TimeUnit.MILLISECONDS.sleep(10); // } catch (InterruptedException e) { // break; // } timer++; if (dwarf.getFortress() == null) { Fortress fortress = dwarf.findNearestFortress(); if (fortress != null) { Coordinates moveTo = dwarf.getCoordinatesToFortress(fortress); dwarf.move(moveTo); if (moveTo.isThere(dwarf)) { Platform.runLater(() -> { dwarf.setFortress(fortress); fortress.add(dwarf); Main.root.getChildren().remove(dwarf.getGroup()); }); } } } else { if (decision == null) { Fortress fortress = dwarf.getFortress(); String decisionName = fortress.getAvailableDecisions().get(new Random().nextInt(fortress.getAvailableDecisions().size())); // while (Objects.equals(lastDecision, decisionName)) { // decisionName = availableDecisions[new Random().nextInt(availableDecisions.length)]; // } // lastDecision = decisionName; switch (decisionName) { case "GoToWorkstation" -> { if (dwarf.getAge() > 12 && dwarf.getAge() < 70 && dwarf.getAbility() != Ability.COMMUNICATION ) { decision = new GoToWorkstationDecision(new Random().nextInt(timer + 5, timer + 50), dwarf); } } case "GoToDwarvesContainer" -> { if (dwarf.getFortress().contains(dwarf)) break; decision = new GoToDwarvesContainerDecision(new Random().nextInt(timer + 5, timer + 50), dwarf); } case "GoToThroneRoom" -> { if (dwarf.getAge() > 35 && dwarf.getAge() < 70) { // if (!dwarf.getFortress().getDwarves().stream().anyMatch(dwarf1 -> { // DwarfHandler dwarfHandler = (DwarfHandler) dwarf1.getHandler(); // return dwarfHandler.decision.getClass().getSimpleName().equals("GoToThroneRoomDecision"); // })) AtomicBoolean isThroneEmpty = new AtomicBoolean(false); dwarf.getFortress().getDwarves().stream().forEach(dwarf1 -> { if (dwarf1 instanceof Nobel) { return; } DwarfHandler dwarfHandler = (DwarfHandler) dwarf1.getHandler(); if (dwarfHandler.decision != null && dwarfHandler.decision.getClass().getSimpleName().equals("GoToThroneRoomDecision")) { isThroneEmpty.set(true); } }); if (!isThroneEmpty.get()) { decision = new GoToThroneRoomDecision(new Random().nextInt(timer + 5, timer + 50), dwarf); } } } case "GoTrading" -> { if (dwarf.getAbility() == Ability.COMMUNICATION) { Item itemToTrade; int amountToTrade; if (fortress.getWoodCount() > 0 && fortress.getWoodCount() >= fortress.getStoneCount()) { itemToTrade = Item.WOOD; amountToTrade = new Random().nextInt(1, fortress.getWoodCount() / 2); fortress.setWoodCount(fortress.getWoodCount() - amountToTrade); } else if (fortress.getStoneCount() > 0 && fortress.getStoneCount() >= fortress.getWoodCount()) { itemToTrade = Item.STONE; amountToTrade = new Random().nextInt(1, fortress.getStoneCount() / 2); fortress.setStoneCount(fortress.getStoneCount() - amountToTrade); } else { break; } if (dwarf.getItems().containsKey(itemToTrade)) { dwarf.getItems().put(itemToTrade, dwarf.getItems().get(itemToTrade) + amountToTrade); } else { dwarf.getItems().put(itemToTrade, amountToTrade); } Platform.runLater(dwarf::updateItemLabel); decision = new GoTradingDecision(new Random().nextInt(timer + 200, timer + 500), dwarf, itemToTrade, amountToTrade); } } } } else { if (decision.getExecuteWhen() < timer) { decision.cycle(); } if (decision.isComplete()) decision = null; } } } } }