1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
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<ActionEvent> {
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;
}
}
}
}
}
|