-
Notifications
You must be signed in to change notification settings - Fork 497
/
PathFindingSearch.java
171 lines (150 loc) · 4.61 KB
/
PathFindingSearch.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
/**
* Copyright (c) 2011-2015, 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.utils.concurrency;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import net.minecraft.world.World;
import buildcraft.api.core.BlockIndex;
import buildcraft.api.core.BuildCraftAPI;
import buildcraft.api.core.IZone;
import buildcraft.core.utils.IBlockFilter;
public class PathFindingSearch implements IIterableAlgorithm {
public static int PATH_ITERATIONS = 1000;
private static HashMap<Integer, HashSet<BlockIndex>> reservations = new HashMap<Integer, HashSet<BlockIndex>>();
private World world;
private BlockIndex start;
private List<PathFinding> pathFinders;
private IBlockFilter pathFound;
private IZone zone;
private float maxDistance;
private Iterator<BlockIndex> blockIter;
public PathFindingSearch(World iWorld, BlockIndex iStart,
Iterator<BlockIndex> iBlockIter, IBlockFilter iPathFound,
float iMaxDistance, IZone iZone) {
world = iWorld;
start = iStart;
pathFound = iPathFound;
maxDistance = iMaxDistance;
zone = iZone;
blockIter = iBlockIter;
pathFinders = new LinkedList<PathFinding>();
}
@Override
public void iterate() {
if (pathFinders.size() < 5 && blockIter.hasNext()) {
iterateSearch(PATH_ITERATIONS * 50);
}
iteratePathFind(PATH_ITERATIONS);
}
private void iterateSearch(int itNumber) {
for (int i = 0; i < itNumber; ++i) {
if (!blockIter.hasNext()) {
return;
}
BlockIndex delta = blockIter.next();
BlockIndex block = new BlockIndex(start.x + delta.x,
((start.y + delta.y) > 0) ? start.y + delta.y : 0,
start.z + delta.z);
if (isLoadedChunk(block.x, block.z)) {
if (isTarget(block)) {
pathFinders.add(new PathFinding(world, start, block, 0, maxDistance));
}
}
if (pathFinders.size() >= 5) {
return;
}
}
}
private boolean isTarget(BlockIndex block) {
if (zone != null && !zone.contains(block.x, block.y, block.z)) {
return false;
}
if (!pathFound.matches(world, block.x, block.y, block.z)) {
return false;
}
synchronized (reservations) {
if (reservations.containsKey(world.provider.dimensionId)) {
HashSet<BlockIndex> dimReservations = reservations
.get(world.provider.dimensionId);
if (dimReservations.contains(block)) {
return false;
}
}
}
if (!BuildCraftAPI.isSoftBlock(world, block.x - 1, block.y, block.z)
&& !BuildCraftAPI.isSoftBlock(world, block.x + 1, block.y, block.z)
&& !BuildCraftAPI.isSoftBlock(world, block.x, block.y, block.z - 1)
&& !BuildCraftAPI.isSoftBlock(world, block.x, block.y, block.z + 1)
&& !BuildCraftAPI.isSoftBlock(world, block.x, block.y - 1, block.z)
&& !BuildCraftAPI.isSoftBlock(world, block.x, block.y + 1, block.z)) {
return false;
}
return true;
}
private boolean isLoadedChunk(int x, int z) {
return world.getChunkProvider().chunkExists(x >> 4, z >> 4);
}
public void iteratePathFind(int itNumber) {
for (PathFinding pathFinding : new ArrayList<PathFinding>(pathFinders)) {
pathFinding.iterate(itNumber / pathFinders.size());
if (pathFinding.isDone()) {
LinkedList<BlockIndex> path = pathFinding.getResult();
if (path != null && path.size() > 0) {
if (reserve(path.getLast())) {
return;
}
}
pathFinders.remove(pathFinding);
}
}
}
@Override
public boolean isDone() {
for (PathFinding pathFinding : pathFinders) {
if (pathFinding.isDone()) {
return true;
}
}
return !blockIter.hasNext();
}
public LinkedList<BlockIndex> getResult() {
for (PathFinding pathFinding : pathFinders) {
if (pathFinding.isDone()) {
return pathFinding.getResult();
}
}
return new LinkedList<BlockIndex>();
}
private boolean reserve(BlockIndex block) {
synchronized (reservations) {
if (!reservations.containsKey(world.provider.dimensionId)) {
reservations.put(world.provider.dimensionId,
new HashSet<BlockIndex>());
}
HashSet<BlockIndex> dimReservations = reservations
.get(world.provider.dimensionId);
if (dimReservations.contains(block)) {
return false;
}
dimReservations.add(block);
return true;
}
}
public void unreserve(BlockIndex block) {
synchronized (reservations) {
if (reservations.containsKey(world.provider.dimensionId)) {
reservations.get(world.provider.dimensionId).remove(block);
}
}
}
}