summaryrefslogtreecommitdiff
path: root/src/ch/epfl/maze/physical/GhostPredator.java
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2015-11-24 23:43:00 +0100
committerPacien TRAN-GIRARD2015-11-24 23:43:00 +0100
commit4da9bdc4fa2f44eedba3dff29af7b0ce9180e442 (patch)
treea7bb129adc714d4c06e2fdeb0008343aa816bd97 /src/ch/epfl/maze/physical/GhostPredator.java
parent5c943f7c7b53d850d7ad3413183fef7f001c6cc4 (diff)
downloadmaze-solver-4da9bdc4fa2f44eedba3dff29af7b0ce9180e442.tar.gz
Refactor Ghosts
Diffstat (limited to 'src/ch/epfl/maze/physical/GhostPredator.java')
-rw-r--r--src/ch/epfl/maze/physical/GhostPredator.java224
1 files changed, 0 insertions, 224 deletions
diff --git a/src/ch/epfl/maze/physical/GhostPredator.java b/src/ch/epfl/maze/physical/GhostPredator.java
deleted file mode 100644
index c1cfca8..0000000
--- a/src/ch/epfl/maze/physical/GhostPredator.java
+++ /dev/null
@@ -1,224 +0,0 @@
1package ch.epfl.maze.physical;
2
3import ch.epfl.maze.util.Direction;
4import ch.epfl.maze.util.Vector2D;
5
6import java.util.EnumSet;
7import java.util.Set;
8
9/**
10 * Predator ghost that have two different modes and a home position in the labyrinth.
11 *
12 * @author Pacien TRAN-GIRARD
13 */
14abstract public class GhostPredator extends Predator {
15
16 public enum Mode {
17 CHASE(40),
18 SCATTER(14);
19
20 public static final Mode DEFAULT = CHASE;
21 public final int duration;
22
23 /**
24 * Constructs a new Mode with the given duration.
25 *
26 * @param duration The duration in cycles
27 */
28 Mode(int duration) {
29 this.duration = duration;
30 }
31
32 /**
33 * Returns the next Mode.
34 *
35 * @return The next Mode
36 */
37 public Mode getNext() {
38 switch (this) {
39 case CHASE:
40 return SCATTER;
41 case SCATTER:
42 return CHASE;
43 default:
44 return DEFAULT;
45 }
46 }
47 }
48
49 private static Prey commonPrey;
50
51 private final Vector2D homePosition;
52
53 private Mode mode;
54 private int modeCycle;
55
56 /**
57 * Constructs a predator with a specified position.
58 *
59 * @param position Position of the predator in the labyrinth
60 */
61 public GhostPredator(Vector2D position) {
62 super(position);
63
64 this.homePosition = position;
65
66 this.mode = Mode.DEFAULT;
67 this.modeCycle = 0;
68 }
69
70 /**
71 * Selects a new random Prey to chase in the Daedalus.
72 *
73 * @param daedalus The Daedalus
74 * @return The Chosen One
75 */
76 private static Prey selectRandomPrey(Daedalus daedalus) {
77 if (daedalus.getPreys().isEmpty()) return null;
78
79 int randomPreyIndex = GhostPredator.RANDOM_SOURCE.nextInt(daedalus.getPreys().size());
80 return daedalus.getPreys().get(randomPreyIndex);
81 }
82
83 /**
84 * Returns the commonly targeted Prey.
85 *
86 * @param daedalus The Daedalus
87 * @return The common Prey
88 */
89 private static Prey getPrey(Daedalus daedalus) {
90 if (GhostPredator.commonPrey == null || !daedalus.hasPrey(GhostPredator.commonPrey))
91 GhostPredator.commonPrey = GhostPredator.selectRandomPrey(daedalus);
92
93 return GhostPredator.commonPrey;
94 }
95
96 /**
97 * Returns the commonly targeted Prey's position.
98 *
99 * @param daedalus The Daedalus
100 * @return The position of the Prey
101 */
102 protected final Vector2D getPreyPosition(Daedalus daedalus) {
103 Prey prey = GhostPredator.getPrey(daedalus);
104
105 if (prey == null) return this.homePosition;
106 return prey.getPosition();
107 }
108
109 /**
110 * Returns the commonly targeted Prey's Direction.
111 *
112 * @param daedalus The Daedalus
113 * @return The Direction the Prey is facing
114 */
115 protected final Direction getPreyDirection(Daedalus daedalus) {
116 Prey prey = GhostPredator.getPrey(daedalus);
117
118 if (prey == null) return Direction.NONE;
119 return prey.getDirection();
120 }
121
122 /**
123 * Calculates the Euclidean distance from the adjacent position at the given Direction to the target position.
124 *
125 * @param dir The adjacent Direction
126 * @param targetPosition The targeted position
127 * @return The Euclidean distance between the two positions
128 */
129 private double calcDistanceFromAdjacentDirectionTo(Direction dir, Vector2D targetPosition) {
130 return this
131 .getPosition()
132 .addDirectionTo(dir)
133 .sub(targetPosition)
134 .dist();
135 }
136
137 /**
138 * Selects the best Direction in the given choices by minimizing the Euclidean distance to the targeted position.
139 *
140 * @param targetPosition The targeted position
141 * @param choices A set of Direction choices
142 * @return A set of optimal Direction choices
143 */
144 private Set<Direction> selectBestPaths(Vector2D targetPosition, Set<Direction> choices) {
145 Set<Direction> bestPaths = EnumSet.noneOf(Direction.class);
146 double minDist = Double.MAX_VALUE;
147
148 for (Direction dir : choices) {
149 double dist = this.calcDistanceFromAdjacentDirectionTo(dir, targetPosition);
150
151 if (dist < minDist) {
152 minDist = dist;
153 bestPaths.clear();
154 }
155
156 if (dist <= minDist)
157 bestPaths.add(dir);
158 }
159
160 return bestPaths;
161 }
162
163 /**
164 * Rotates to the next Mode.
165 */
166 protected void rotateMode() {
167 this.mode = this.mode.getNext();
168 this.modeCycle = 0;
169 }
170
171 /**
172 * Increments the cycle counter and rotates to the next Mode if the Mode's duration has been reached.
173 */
174 private void countCycle() {
175 this.modeCycle += 1;
176
177 if (this.modeCycle >= this.mode.duration)
178 this.rotateMode();
179 }
180
181 @Override
182 public Direction move(Set<Direction> choices, Daedalus daedalus) {
183 this.countCycle();
184
185 Set<Direction> smartChoices = choices.size() > 1 ? this.excludeOrigin(choices) : choices;
186 Set<Direction> bestPaths = this.selectBestPaths(this.getTargetPosition(daedalus), smartChoices);
187 return this.move(bestPaths);
188 }
189
190 /**
191 * Returns the current Mode.
192 *
193 * @return The current Mode
194 */
195 protected Mode getMode(Daedalus daedalus) {
196 return this.mode;
197 }
198
199 /**
200 * Returns the position to target according to the current Mode.
201 *
202 * @param daedalus The Daedalus
203 * @return The position to target
204 */
205 protected Vector2D getTargetPosition(Daedalus daedalus) {
206 switch (this.getMode(daedalus)) {
207 case CHASE:
208 return this.getPreyTargetPosition(daedalus);
209 case SCATTER:
210 return this.homePosition;
211 default:
212 return this.homePosition;
213 }
214 }
215
216 /**
217 * Returns the Prey's projected targeted position.
218 *
219 * @param daedalus The Daedalus
220 * @return The projected position
221 */
222 protected abstract Vector2D getPreyTargetPosition(Daedalus daedalus);