Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions CachedLevels.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Record getRecord() {
Scanner s = new Scanner(new InputStreamReader(lu.getInputStream()));
if(s.hasNext()) c = new Record(s.nextLong(), s.next(), s.nextLong());
else System.out.println("none found");
wr.close();

s.close();
} catch (IOException e) {
e.printStackTrace();
Expand All @@ -46,14 +46,21 @@ public boolean setRecord(Record r, long time) {
connect();
wr.write(URLEncoder.encode("?player=" + player + "&seed=" + r.seed + "&time=" + time, "UTF-8"));
wr.flush();

Scanner s = new Scanner(new InputStreamReader(lu.getInputStream()));
if(s.hasNextInt() && s.nextInt() == 1) recordHolder = true;
wr.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
return recordHolder;
}

public void close() {
try {
wr.flush();
wr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
37 changes: 26 additions & 11 deletions CachedSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public int hashCode() {
* @param obsticals constant locations search must avoid
*/
public CachedSearch(Vector2 start, Collection<Vector2> obsticals) {
//also keep search in bounding region
this.start = start;
this.obsticals = obsticals;

Expand Down Expand Up @@ -102,6 +103,18 @@ public boolean addMinimumPath(Vector2 end, Collection<Vector2> floor, int newFlo
private boolean search(Vector2 end, Collection<Vector2> floor, int newFloorCost) {
if(obsticals.contains(end)) return false;

int minX = Math.min(start.x, end.x);
int maxX = Math.max(start.x, end.x);
int minY = Math.min(start.y, end.y);
int maxY = Math.max(start.y, end.y);

for(Vector2 v : floor != null ? floor : obsticals) {
if(v.x < minX) minX = v.x;
if(v.x > maxX) maxX = v.x;
if(v.y < minY) minY = v.y;
if(v.y > maxY) maxY = v.y;
}

Queue<GridState> pathQueue = new PriorityQueue<GridState>((a, b)-> a.getCost() + Vector2.manDistance(a.getPosition(), start) - b.getCost() - Vector2.manDistance(b.getPosition(), start));
pathQueue.add(new GridState(end));
List<GridState> closed = new ArrayList<GridState>();
Expand All @@ -116,20 +129,22 @@ private boolean search(Vector2 end, Collection<Vector2> floor, int newFloorCost)
return true;
}

for(Vector2 childDir : Generator.directions) {
for(Vector2 childDir : Vector2.directions) {
Vector2 childPos = Vector2.add(parent.getPosition(), childDir);
GridState child = new GridState(parent, childPos, (floor != null && floor.contains(childPos)) ? 1 : newFloorCost);
if(!obsticals.contains(childPos)) {
int prevIndex = 0;
if((prevIndex = closed.indexOf(child)) != -1) {
if(child.getCost() < closed.get(prevIndex).getCost()) {
closed.remove(prevIndex);
if(childPos.x >= minX-1 && childPos.x <= maxX+1 && childPos.y >= minY-1 && childPos.y <= maxY+1) {
GridState child = new GridState(parent, childPos, (floor != null && floor.contains(childPos)) ? 1 : newFloorCost);
if(!obsticals.contains(childPos)) {
int prevIndex = 0;
if((prevIndex = closed.indexOf(child)) != -1) {
if(child.getCost() < closed.get(prevIndex).getCost()) {
closed.remove(prevIndex);
pathQueue.add(child);
}
} else if(pathQueue.removeIf(a -> child.equals(a) && child.getCost() < a.getCost())) {
pathQueue.add(child);
} else {
pathQueue.add(child);
}
} else if(pathQueue.removeIf(a -> child.equals(a) && child.getCost() < a.getCost())) {
pathQueue.add(child);
} else {
pathQueue.add(child);
}
}
}
Expand Down
Loading