-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateRepresentation.java
More file actions
146 lines (128 loc) · 4.09 KB
/
StateRepresentation.java
File metadata and controls
146 lines (128 loc) · 4.09 KB
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
package grah8384;
import java.util.HashMap;
import java.util.HashSet;
import java.util.UUID;
import spacesettlers.objects.AbstractObject;
import spacesettlers.objects.Asteroid;
import spacesettlers.objects.Base;
import spacesettlers.objects.Beacon;
import spacesettlers.objects.Ship;
import spacesettlers.objects.resources.ResourcePile;
import spacesettlers.simulator.Toroidal2DPhysics;
import spacesettlers.utilities.Position;
public class StateRepresentation {
HashSet<Ship> ships;
HashSet<Beacon> beacons;
HashSet<Asteroid> asteroids;
HashSet<Base> bases;
HashMap<UUID, Position> at;
HashMap<UUID, ResourcePile> resources;
HashMap<UUID, Double> energy;
Toroidal2DPhysics space;
/**
* Constructor to create initial state from space object
* @param space
* @param team
*/
public StateRepresentation(Toroidal2DPhysics space, String teamName) {
this.space = space.deepClone();
asteroids = new HashSet<Asteroid>();
ships = new HashSet<Ship>();
beacons = new HashSet<Beacon>();
bases = new HashSet<Base>();
at = new HashMap<UUID, Position>();
resources = new HashMap<UUID, ResourcePile>();
energy = new HashMap<UUID, Double>();
for (Asteroid a : space.getAsteroids()) {
if (!a.isMineable()) continue;
asteroids.add(a);
at.put(a.getId(), a.getPosition());
}
for (Beacon b : space.getBeacons()) {
beacons.add(b);
at.put(b.getId(), b.getPosition());
}
for (Ship s : space.getShips()) {
if (s.getTeamName() != teamName) continue;
ships.add(s);
at.put(s.getId(), s.getPosition());
resources.put(s.getId(), s.getResources());
energy.put(s.getId(), s.getEnergy());
}
for (Base b : space.getBases()) {
if (b.getTeamName() == teamName) {
bases.add(b);
at.put(b.getId(), b.getPosition());
}
}
}
public StateRepresentation(StateRepresentation state) {
this.asteroids = new HashSet<Asteroid>(state.asteroids);
this.at = new HashMap<UUID, Position>(state.at);
this.bases = new HashSet<Base>(state.bases);
this.beacons = new HashSet<Beacon>(state.beacons);
this.energy = new HashMap<UUID, Double>(state.energy);
this.resources = new HashMap<UUID, ResourcePile>(state.resources);
this.ships = new HashSet<Ship>(state.ships);
this.space = state.space;
}
/**
* Checks if the UUID is a ship by comparing to the set of ships in the state
* @param object
* @return true if the UUID is in the ship list
* false if the UUID is not in the ship list
*/
public boolean isShip(UUID object) {
return ships.contains(object);
}
/**
* Checks if the UUID is a beacon by comparing to the set of beacons in the state
* @param object
* @return true if the UUID is in the beacon list
* false if the UUID is not in the beacon list
*/
public boolean isBeacon(UUID object) {
return beacons.contains(object);
}
/**
* Checks if the UUID is a asteroid by comparing to the set of asteroids in the state
* @param object
* @return true if the UUID is in the asteroid list
* false if the UUID is not in the asteroid list
*/
public boolean isAsteroids(UUID object) {
return asteroids.contains(object);
}
/**
* Checks if the given object has the given locations stored in the state
* @param object
* @param pos
* @return
*/
public boolean at(UUID object, Position pos) {
return at.containsKey(object) && pos.equalsLocationOnly(at.get(object));
}
/**
* Changes the position of the given object in the state representation
* @param object
* @param pos
*/
public void setAt(UUID object, Position pos) {
at.put(object, pos);
}
public void addResources(UUID ship, ResourcePile resource) {
resources.get(ship).add(resource);
}
public void addEnergy(UUID ship, Double energy) {
this.energy.put(ship, this.energy.get(ship) + energy);
}
/**
* Removes the given object from the list (asteroid, ship, or beacon) that it is currently in
* @param object
* @return true if the object was in some state list
* false if the object was not in a state list
*/
public boolean removeObject(AbstractObject object) {
return asteroids.remove(object) || beacons.remove(object) || ships.remove(object);
}
}