summary refs log tree commit diff stats
path: root/src/com/fourisland/fourpuzzle/gamestate/mapview/event/movement/FollowMovementType.java
blob: bbe9d8268a3e5bb350dd4d0decc153c5ae25f256 (plain) (blame)
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
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.fourisland.fourpuzzle.gamestate.mapview.event.movement;

import com.fourisland.fourpuzzle.Direction;
import com.fourisland.fourpuzzle.Game;
import com.fourisland.fourpuzzle.gamestate.mapview.event.Event;
import com.fourisland.fourpuzzle.gamestate.mapview.event.ImmutableEvent;
import com.fourisland.fourpuzzle.gamestate.mapview.event.LayerEvent;
import java.awt.Point;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.List;

/**
 * FollowMovementType allows an Event to continually walk toward a specified
 * Event.
 *
 * @author hatkirby
 */
public class FollowMovementType implements MovementType {
    
    String name = null;
    Event event = null;
    Point lastLoc = new Point();
    Deque<Direction> moves = new ArrayDeque<Direction>();
    List<Point> attempts = new ArrayList<Point>();
    
    public FollowMovementType() {}
    
    public FollowMovementType(String name)
    {
        this.name = name;
    }
    
    private boolean search(ImmutableEvent ev)
    {
        /* Iterate over all of the directions and check if moving in that
         * direction would place the event on the destination event. If so, the
         * correct path has been aquired and thus we can return */
        for (Direction d : Direction.values())
        {
            Point loc = d.to(ev.getLocation());
            if (lastLoc.equals(loc))
            {
                return true;
            }
        }
        
        /* Calculate the directions to attempt and the order in which to do so
         * based on proximity to the destination event */
        
        List<Direction> ds = ev.getLegalMoves();
        List<Direction> tempd = new ArrayList<Direction>();
        
        if (lastLoc.x < ev.getLocation().x)
        {
            tempd.add(Direction.West);
        } else if (lastLoc.x > ev.getLocation().x)
        {
            tempd.add(Direction.East);
        } else {
            if (!ds.contains(Direction.North) || !ds.contains(Direction.South))
            {
                tempd.add(Direction.West);
                tempd.add(Direction.East);
            }
        }
        
        if (lastLoc.y < ev.getLocation().y)
        {
            tempd.add(Direction.North);
        } else if (lastLoc.y > ev.getLocation().y)
        {
            tempd.add(Direction.South);
        } else {
            if (!ds.contains(Direction.West) || !ds.contains(Direction.East))
            {
                tempd.add(Direction.North);
                tempd.add(Direction.South);
            }
        }
        
        // Remove calculated directions that aren't legal movements
        tempd.retainAll(ds);
        
        // Randomize directions so movement is more fluid
        Collections.shuffle(tempd);
        
        // Iterate over the suggested directions 
        for (Direction d : tempd)
        {
            /* If the position in the suggested direction has already been
             * covered, try the next direction */
            Point loc = d.to(ev.getLocation());
            if (attempts.contains(loc))
            {
                continue;
            }
            
            /* Create a dummy event and use it to search from the position in
             * the suggested direction */
            Event temp = new LayerEvent(loc.x, loc.y);
            temp.setParentMap(ev.getParentMap());
            attempts.add(loc);
            
            if (search(new ImmutableEvent(temp)))
            {
                moves.push(d);
                return true;
            }
        }
        
        return false;
    }
    
    public Direction nextMovement(ImmutableEvent ev)
    {
        if (event == null)
        {
            if (name == null)
            {
                event = Game.getHeroEvent();
            } else {
                event = ev.getParentMap().getEvent(name);
            }
        } else if ((name != null) && !(event.getParentMap().equals(ev.getParentMap())))
        {
            event = ev.getParentMap().getEvent(name);
        }
        
        if (!event.getLocation().equals(lastLoc))
        {
            lastLoc.setLocation(event.getLocation());
        
            moves.clear();
            attempts.clear();
        
            search(ev);
        }
        
        if (!moves.isEmpty())
        {
            return moves.pop();
        }
        
        return null;
    }

}