summaryrefslogtreecommitdiff
path: root/src/ch/epfl/maze/physical/pacman/Ghost.java
blob: f8f511ec294f06bb995f6e0cb8cbb7a6cb863376 (plain)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package ch.epfl.maze.physical.pacman;

import ch.epfl.maze.physical.Daedalus;
import ch.epfl.maze.physical.Predator;
import ch.epfl.maze.physical.Prey;
import ch.epfl.maze.physical.stragegies.picker.RandomPicker;
import ch.epfl.maze.physical.stragegies.reducer.BackwardReducer;
import ch.epfl.maze.physical.stragegies.reducer.CostReducer;
import ch.epfl.maze.util.Direction;
import ch.epfl.maze.util.Vector2D;

import java.util.EnumSet;
import java.util.Set;

/**
 * Predator ghost that have two different modes and a home position in the labyrinth.
 *
 * @author Pacien TRAN-GIRARD
 */
abstract public class Ghost extends Predator implements BackwardReducer, CostReducer, RandomPicker {

    public enum Mode {
        CHASE(40),
        SCATTER(14);

        public static final Mode DEFAULT = CHASE;
        public final int duration;

        /**
         * Constructs a new Mode with the given duration.
         *
         * @param duration The duration in cycles
         */
        Mode(int duration) {
            this.duration = duration;
        }

        /**
         * Returns the next Mode.
         *
         * @return The next Mode
         */
        public Mode getNext() {
            switch (this) {
                case CHASE:
                    return SCATTER;
                case SCATTER:
                    return CHASE;
                default:
                    return DEFAULT;
            }
        }
    }

    private static Prey commonPrey;

    private final Vector2D homePosition;

    private Mode mode;
    private int modeCycle;

    /**
     * Constructs a predator with a specified position.
     *
     * @param position Position of the predator in the labyrinth
     */
    public Ghost(Vector2D position) {
        super(position);

        this.homePosition = position;

        this.mode = Mode.DEFAULT;
        this.modeCycle = 0;
    }

    /**
     * Returns the cost to reach the target by choosing the given Direction by calculating the Euclidean distance.
     *
     * @param choice   The Direction choice
     * @param daedalus The Daedalus
     * @return The Euclidean distance cost
     */
    @Override
    public int getChoiceCost(Direction choice, Daedalus daedalus) {
        Vector2D target = this.getTargetPosition(daedalus);
        return (int) (this.getDistanceTo(choice, target) * 100);
    }

    @Override
    public Set<Direction> reduce(Set<Direction> choices) {
        return EnumSet.noneOf(Direction.class);
    }

    /**
     * Selects the best Direction in the given choices by minimizing the Euclidean distance to the targeted position
     * after excluding the provenance if possible.
     *
     * @param choices  A set of Direction choices
     * @param daedalus The Daedalus
     * @return A set of optimal Direction choices
     */
    @Override
    public Set<Direction> reduce(Set<Direction> choices, Daedalus daedalus) {
        Set<Direction> forwardChoices = choices.size() > 1 ? BackwardReducer.super.reduce(choices) : choices;
        return CostReducer.super.reduce(forwardChoices, daedalus);
    }

    @Override
    public Direction move(Set<Direction> choices, Daedalus daedalus) {
        this.countCycle();

        Set<Direction> bestChoices = this.reduce(choices, daedalus);
        return this.pick(bestChoices);
    }

    /**
     * Returns the Prey's projected targeted position.
     *
     * @param daedalus The Daedalus
     * @return The projected position
     */
    abstract protected Vector2D getPreyTargetPosition(Daedalus daedalus);

    /**
     * Returns the current Mode.
     *
     * @param daedalus The Daedalus
     * @return The current Mode
     */
    protected Mode getMode(Daedalus daedalus) {
        return this.mode;
    }

    /**
     * Returns the position to target according to the current Mode.
     *
     * @param daedalus The Daedalus
     * @return The position to target
     */
    protected Vector2D getTargetPosition(Daedalus daedalus) {
        switch (this.getMode(daedalus)) {
            case CHASE:
                return this.getPreyTargetPosition(daedalus);
            case SCATTER:
                return this.homePosition;
            default:
                return this.homePosition;
        }
    }

    /**
     * Selects a new random Prey to chase in the Daedalus.
     *
     * @param daedalus The Daedalus
     * @return The Chosen One
     */
    private static Prey selectAnyPrey(Daedalus daedalus) {
        if (daedalus.getPreySet().isEmpty()) return null;
        return daedalus.getPreySet().stream().findAny().get();
    }

    /**
     * Sets a random Prey as the common Pre.
     *
     * @param daedalus The Daedalus
     */
    private static synchronized void setAnyPrey(Daedalus daedalus) {
        Ghost.commonPrey = Ghost.selectAnyPrey(daedalus);
    }

    /**
     * Returns the commonly targeted Prey.
     *
     * @param daedalus The Daedalus
     * @return The common Prey
     */
    private static Prey getPrey(Daedalus daedalus) {
        if (Ghost.commonPrey == null || !daedalus.hasPrey(Ghost.commonPrey))
            Ghost.setAnyPrey(daedalus);

        return Ghost.commonPrey;
    }

    /**
     * Returns the commonly targeted Prey's position.
     *
     * @param daedalus The Daedalus
     * @return The position of the Prey
     */
    protected final Vector2D getPreyPosition(Daedalus daedalus) {
        Prey prey = Ghost.getPrey(daedalus);

        if (prey == null) return this.homePosition;
        return prey.getPosition();
    }

    /**
     * Returns the commonly targeted Prey's Direction.
     *
     * @param daedalus The Daedalus
     * @return The Direction the Prey is facing
     */
    protected final Direction getPreyDirection(Daedalus daedalus) {
        Prey prey = Ghost.getPrey(daedalus);

        if (prey == null) return Direction.NONE;
        return prey.getDirection();
    }

    /**
     * Calculates the Euclidean distance from the adjacent position at the given Direction to the target position.
     *
     * @param dir            The adjacent Direction
     * @param targetPosition The targeted position
     * @return The Euclidean distance between the two positions
     */
    private double getDistanceTo(Direction dir, Vector2D targetPosition) {
        return this
                .getPosition()
                .addDirectionTo(dir)
                .sub(targetPosition)
                .dist();
    }

    /**
     * Rotates to the next Mode.
     */
    protected void rotateMode() {
        this.mode = this.mode.getNext();
        this.modeCycle = 0;
    }

    /**
     * Increments the cycle counter and rotates to the next Mode if the Mode's duration has been reached.
     */
    private void countCycle() {
        this.modeCycle += 1;

        if (this.modeCycle >= this.mode.duration)
            this.rotateMode();
    }

}