forked from iyakshyn/geoEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PathFindBuffers.java
303 lines (264 loc) · 6.5 KB
/
PathFindBuffers.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
package org.mmocore.gameserver.geodata;
import gnu.trove.TIntIntHashMap;
import gnu.trove.TIntIntIterator;
import gnu.trove.TIntObjectHashMap;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.mmocore.commons.text.StrTable;
import org.mmocore.gameserver.Config;
import org.mmocore.gameserver.utils.Location;
public class PathFindBuffers
{
public final static int MIN_MAP_SIZE = 1 << 6;
public final static int STEP_MAP_SIZE = 1 << 5;
public final static int MAX_MAP_SIZE = 1 << 9;
private static TIntObjectHashMap<PathFindBuffer[]> buffers = new TIntObjectHashMap<PathFindBuffer[]>();
private static int[] sizes = new int[0];
private static Lock lock = new ReentrantLock();
static
{
TIntIntHashMap config = new TIntIntHashMap();
String[] k;
for(String e : Config.PATHFIND_BUFFERS.split(";"))
if(!e.isEmpty() && (k = e.split("x")).length == 2)
config.put(Integer.valueOf(k[1]), Integer.valueOf(k[0]));
TIntIntIterator itr = config.iterator();
while(itr.hasNext())
{
itr.advance();
int size = itr.key();
int count = itr.value();
PathFindBuffer[] buff = new PathFindBuffer[count];
for(int i = 0; i < count; i++)
buff[i] = new PathFindBuffer(size);
buffers.put(size, buff);
}
sizes = config.keys();
Arrays.sort(sizes);
}
private static PathFindBuffer create(int mapSize)
{
lock.lock();
try
{
PathFindBuffer buffer;
PathFindBuffer[] buff = buffers.get(mapSize);
if(buff != null)
buff = org.mmocore.commons.lang.ArrayUtils.add(buff, buffer = new PathFindBuffer(mapSize));
else
{
buff = new PathFindBuffer[] { buffer = new PathFindBuffer(mapSize) };
sizes = org.apache.commons.lang3.ArrayUtils.add(sizes, mapSize);
Arrays.sort(sizes);
}
buffers.put(mapSize, buff);
buffer.inUse = true;
return buffer;
}
finally
{
lock.unlock();
}
}
private static PathFindBuffer get(int mapSize)
{
lock.lock();
try
{
PathFindBuffer[] buff = buffers.get(mapSize);
for(PathFindBuffer buffer : buff)
if(!buffer.inUse)
{
buffer.inUse = true;
return buffer;
}
return null;
}
finally
{
lock.unlock();
}
}
public static PathFindBuffer alloc(int mapSize)
{
if(mapSize > MAX_MAP_SIZE)
return null;
mapSize += STEP_MAP_SIZE;
if(mapSize < MIN_MAP_SIZE)
mapSize = MIN_MAP_SIZE;
PathFindBuffer buffer = null;
for(int i = 0; i < sizes.length; i++)
if(sizes[i] >= mapSize)
{
mapSize = sizes[i];
buffer = get(mapSize);
break;
}
//Не найден свободный буффер, или буфферов под такой размер нет
if(buffer == null)
{
for(int size = MIN_MAP_SIZE; size < MAX_MAP_SIZE; size += STEP_MAP_SIZE)
if(size >= mapSize)
{
mapSize = size;
buffer = create(mapSize);
break;
}
}
return buffer;
}
public static void recycle(PathFindBuffer buffer)
{
lock.lock();
try
{
buffer.inUse = false;
}
finally
{
lock.unlock();
}
}
public static StrTable getStats()
{
StrTable table = new StrTable("PathFind Buffers Stats");
lock.lock();
try
{
long totalUses = 0, totalPlayable = 0, totalTime = 0;
int index = 0;
int count;
long uses;
long playable;
long itrs;
long success;
long overtime;
long time;
for(int size : sizes)
{
index++;
count = 0;
uses = 0;
playable = 0;
itrs = 0;
success = 0;
overtime = 0;
time = 0;
for(PathFindBuffer buff : buffers.get(size))
{
count++;
uses += buff.totalUses;
playable += buff.playableUses;
success += buff.successUses;
overtime += buff.overtimeUses;
time += buff.totalTime / 1000000;
itrs += buff.totalItr;
}
totalUses += uses;
totalPlayable += playable;
totalTime += time;
table.set(index, "Size", size);
table.set(index, "Count", count);
table.set(index, "Uses (success%)", uses + "(" + String.format("%2.2f", (uses > 0) ? success * 100. / uses : 0) + "%)");
table.set(index, "Uses, playble", playable + "(" + String.format("%2.2f", (uses > 0) ? playable * 100. / uses : 0) +"%)");
table.set(index, "Uses, overtime", overtime + "(" + String.format("%2.2f", (uses > 0) ? overtime * 100. / uses : 0) +"%)");
table.set(index, "Iter., avg", (uses > 0) ? itrs / uses : 0);
table.set(index, "Time, avg (ms)", String.format("%1.3f", (uses > 0) ? (double)time / uses : 0.));
}
table.addTitle("Uses, total / playable : " + totalUses + " / " + totalPlayable);
table.addTitle("Uses, total time / avg (ms) : " + totalTime + " / " + String.format("%1.3f", totalUses > 0 ? (double)totalTime / totalUses : 0));
}
finally
{
lock.unlock();
}
return table;
}
public static class PathFindBuffer
{
final int mapSize;
final GeoNode[][] nodes;
final Queue<GeoNode> open;
int offsetX, offsetY;
boolean inUse;
//статистика
long totalUses;
long successUses;
long overtimeUses;
long playableUses;
long totalTime;
long totalItr;
public PathFindBuffer(int mapSize)
{
open = new PriorityQueue<GeoNode>(mapSize);
this.mapSize = mapSize;
nodes = new GeoNode[mapSize][mapSize];
for(int i = 0; i < nodes.length; i++)
for(int j = 0; j < nodes[i].length; j++)
nodes[i][j] = new GeoNode();
}
public void free()
{
open.clear();
for(int i = 0; i < nodes.length; i++)
for(int j = 0; j < nodes[i].length; j++)
nodes[i][j].free();
}
}
public static class GeoNode implements Comparable<GeoNode>
{
public final static int NONE = 0;
public final static int OPENED = 1;
public final static int CLOSED = -1;
public int x, y;
public short z, nswe;
public float totalCost, costFromStart, costToEnd;
public int state;
public GeoNode parent;
public GeoNode()
{
nswe = -1;
}
public GeoNode set(int x, int y, short z)
{
this.x = x;
this.y = y;
this.z = z;
return this;
}
public boolean isSet()
{
return nswe != -1;
}
public void free()
{
nswe = -1;
costFromStart = 0f;
totalCost = 0f;
costToEnd = 0f;
parent = null;
state = NONE;
}
public Location getLoc()
{
return new Location(x, y, z);
}
@Override
public String toString()
{
return "[" + x + "," + y + "," + z + "] f: " + totalCost;
}
@Override
public int compareTo(GeoNode o)
{
if(totalCost > o.totalCost)
return 1;
if(totalCost < o.totalCost)
return -1;
return 0;
}
}
}