-
Notifications
You must be signed in to change notification settings - Fork 497
/
RobotRegistry.java
executable file
·321 lines (238 loc) · 8.24 KB
/
RobotRegistry.java
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.core.robots;
import java.security.InvalidParameterException;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;
import net.minecraft.world.WorldSavedData;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.robots.EntityRobotBase;
public class RobotRegistry extends WorldSavedData {
public static RobotRegistry[] registries = new RobotRegistry[256];
protected World world;
private long nextRobotID = Long.MIN_VALUE;
private HashMap<Long, EntityRobot> robotsLoaded = new HashMap<Long, EntityRobot>();
private HashMap<ResourceId, Long> resourcesTaken = new HashMap<ResourceId, Long>();
private HashMap<Long, HashSet<ResourceId>> resourcesTakenByRobot = new HashMap<Long, HashSet<ResourceId>>();
private HashMap<StationIndex, DockingStation> stations = new HashMap<StationIndex, DockingStation>();
private HashMap<Long, HashSet<StationIndex>> stationsTakenByRobot = new HashMap<Long, HashSet<StationIndex>>();
public RobotRegistry(String id) {
super(id);
}
public long getNextRobotId() {
long result = nextRobotID;
nextRobotID = nextRobotID + 1;
return result;
}
public void registerRobot(EntityRobot robot) {
markDirty();
if (robot.getRobotId() == EntityRobotBase.NULL_ROBOT_ID) {
robot.setUniqueRobotId(getNextRobotId());
}
robotsLoaded.put(robot.getRobotId(), robot);
}
public void killRobot(EntityRobot robot) {
markDirty();
releaseResources(robot, true);
robotsLoaded.remove(robot.getRobotId());
}
public EntityRobot getLoadedRobot(long id) {
if (robotsLoaded.containsKey(id)) {
return robotsLoaded.get(id);
} else {
return null;
}
}
public boolean isTaken(ResourceId resourceId) {
return robotTaking(resourceId) != EntityRobotBase.NULL_ROBOT_ID;
}
public long robotTaking(ResourceId resourceId) {
if (!resourcesTaken.containsKey(resourceId)) {
return EntityRobotBase.NULL_ROBOT_ID;
}
long robotId = resourcesTaken.get(resourceId);
if (robotsLoaded.containsKey(robotId) && !robotsLoaded.get(robotId).isDead) {
return robotId;
} else {
// If the robot is either not loaded or dead, the resource is not
// actively used anymore. Release it.
release(resourceId);
return EntityRobotBase.NULL_ROBOT_ID;
}
}
public boolean take(ResourceId resourceId, EntityRobotBase robot) {
markDirty();
return take(resourceId, robot.getRobotId());
}
public boolean take(ResourceId resourceId, long robotId) {
if (resourceId == null) {
return false;
}
markDirty();
if (!resourcesTaken.containsKey(resourceId)) {
resourcesTaken.put(resourceId, robotId);
if (!resourcesTakenByRobot.containsKey(robotId)) {
resourcesTakenByRobot.put(robotId, new HashSet<ResourceId>());
}
resourcesTakenByRobot.get(robotId).add(resourceId);
resourceId.taken(robotId);
return true;
} else {
return false;
}
}
public void release(ResourceId resourceId) {
if (resourceId == null) {
return;
}
markDirty();
if (resourcesTaken.containsKey(resourceId)) {
long robotId = resourcesTaken.get(resourceId);
resourcesTakenByRobot.get(resourcesTaken.get(resourceId)).remove(resourceId);
resourcesTaken.remove(resourceId);
resourceId.released(robotId);
}
}
public void releaseResources(EntityRobotBase robot) {
releaseResources(robot, false);
}
private void releaseResources(EntityRobotBase robot, boolean forceAll) {
markDirty();
if (resourcesTakenByRobot.containsKey(robot.getRobotId())) {
HashSet<ResourceId> resourceSet = resourcesTakenByRobot.get(robot.getRobotId());
ResourceId mainId = null;
for (ResourceId id : resourceSet) {
release(id);
}
resourcesTakenByRobot.remove(robot.getRobotId());
}
if (stationsTakenByRobot.containsKey(robot.getRobotId())) {
HashSet<StationIndex> stationSet = (HashSet<StationIndex>) stationsTakenByRobot.get(robot.getRobotId())
.clone();
for (StationIndex s : stationSet) {
DockingStation d = stations.get(s);
if (!d.canRelease()) {
if (forceAll) {
d.unsafeRelease(robot);
}
} else {
d.unsafeRelease(robot);
}
}
if (forceAll) {
stationsTakenByRobot.remove(robot.getRobotId());
}
}
}
public DockingStation getStation(int x, int y, int z, ForgeDirection side) {
StationIndex index = new StationIndex(side, x, y, z);
if (stations.containsKey(index)) {
return stations.get(index);
} else {
return null;
}
}
public Collection<DockingStation> getStations() {
return stations.values();
}
public void registerStation(DockingStation station) {
markDirty();
StationIndex index = new StationIndex(station);
if (stations.containsKey(index)) {
throw new InvalidParameterException("Station " + index + " already registerd");
} else {
stations.put(index, station);
}
}
public void removeStation(DockingStation station) {
markDirty();
StationIndex index = new StationIndex(station);
if (stations.containsKey(index)) {
if (station.robotTaking() != null) {
station.robotTaking().setDead();
}
stations.remove(index);
}
}
public void take(DockingStation station, long robotId) {
if (!stationsTakenByRobot.containsKey(robotId)) {
stationsTakenByRobot.put(robotId, new HashSet<StationIndex>());
}
stationsTakenByRobot.get(robotId).add(new StationIndex(station));
}
public void release(DockingStation station, long robotId) {
if (stationsTakenByRobot.containsKey(robotId)) {
stationsTakenByRobot.get(robotId).remove(new StationIndex(station));
}
}
public static RobotRegistry getRegistry (World world) {
if (registries[world.provider.dimensionId] == null
|| registries[world.provider.dimensionId].world != world) {
RobotRegistry newRegistry = (RobotRegistry) world.perWorldStorage.loadData(RobotRegistry.class, "robotRegistry");
if (newRegistry == null) {
newRegistry = new RobotRegistry("robotRegistry");
world.perWorldStorage.setData("robotRegistry", newRegistry);
}
newRegistry.world = world;
for (DockingStation d : newRegistry.stations.values()) {
d.world = world;
}
registries[world.provider.dimensionId] = newRegistry;
}
return registries[world.provider.dimensionId];
}
@Override
public void writeToNBT(NBTTagCompound nbt) {
nbt.setLong("nextRobotID", nextRobotID);
NBTTagList resourceList = new NBTTagList();
for (Map.Entry<ResourceId, Long> e : resourcesTaken.entrySet()) {
NBTTagCompound cpt = new NBTTagCompound();
NBTTagCompound resourceId = new NBTTagCompound();
e.getKey().writeToNBT(resourceId);
cpt.setTag("resourceId", resourceId);
cpt.setLong("robotId", e.getValue());
resourceList.appendTag(cpt);
}
nbt.setTag("resourceList", resourceList);
NBTTagList stationList = new NBTTagList();
for (Map.Entry<StationIndex, DockingStation> e : stations.entrySet()) {
NBTTagCompound cpt = new NBTTagCompound();
e.getValue().writeToNBT(cpt);
stationList.appendTag(cpt);
}
nbt.setTag("stationList", stationList);
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
nextRobotID = nbt.getLong("nextRobotID");
NBTTagList resourceList = nbt.getTagList("resourceList", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < resourceList.tagCount(); ++i) {
NBTTagCompound cpt = resourceList.getCompoundTagAt(i);
ResourceId resourceId = ResourceId.load(cpt.getCompoundTag("resourceId"));
long robotId = cpt.getLong("robotId");
take(resourceId, robotId);
}
NBTTagList stationList = nbt.getTagList("stationList", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < stationList.tagCount(); ++i) {
NBTTagCompound cpt = stationList.getCompoundTagAt(i);
DockingStation station = new DockingStation();
station.readFromNBT(cpt);
registerStation(station);
if (station.linkedId() != EntityRobotBase.NULL_ROBOT_ID) {
take(station, station.linkedId());
}
}
}
}