Converted ShatteredPD to Build from Gradle
Major Changes: - Shattered now builds effortlessly either from gradle CLI or android studio - Much better dependency management through gradle (although it's not really used atm) - Separate PD-classes repo is now SPD-classes module within main repo
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Pixel Dungeon
|
||||
* Copyright (C) 2012-2015 Oleg Dolya
|
||||
*
|
||||
* Shattered Pixel Dungeon
|
||||
* Copyright (C) 2014-2016 Evan Debenham
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
package com.watabou.utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class Graph {
|
||||
|
||||
public static <T extends Node> void setPrice( List<T> nodes, int value ) {
|
||||
for (T node : nodes) {
|
||||
node.price( value );
|
||||
}
|
||||
}
|
||||
|
||||
public static <T extends Node> void buildDistanceMap( Collection<T> nodes, Node focus ) {
|
||||
|
||||
for (T node : nodes) {
|
||||
node.distance( Integer.MAX_VALUE );
|
||||
}
|
||||
|
||||
LinkedList<Node> queue = new LinkedList<Node>();
|
||||
|
||||
focus.distance( 0 );
|
||||
queue.add( focus );
|
||||
|
||||
while (!queue.isEmpty()) {
|
||||
|
||||
Node node = queue.poll();
|
||||
int distance = node.distance();
|
||||
int price = node.price();
|
||||
|
||||
for (Node edge : node.edges()) {
|
||||
if (edge.distance() > distance + price) {
|
||||
queue.add( edge );
|
||||
edge.distance( distance + price );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T extends Node> List<T> buildPath( Collection<T> nodes, T from, T to ) {
|
||||
|
||||
List<T> path = new ArrayList<T>();
|
||||
|
||||
T room = from;
|
||||
while (room != to) {
|
||||
|
||||
int min = room.distance();
|
||||
T next = null;
|
||||
|
||||
Collection<? extends Node> edges = room.edges();
|
||||
|
||||
for (Node edge : edges) {
|
||||
|
||||
int distance = edge.distance();
|
||||
if (distance < min) {
|
||||
min = distance;
|
||||
next = (T)edge;
|
||||
}
|
||||
}
|
||||
|
||||
if (next == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
path.add( next );
|
||||
room = next;
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
public interface Node {
|
||||
|
||||
int distance();
|
||||
void distance( int value );
|
||||
|
||||
int price();
|
||||
void price( int value );
|
||||
|
||||
Collection<? extends Node> edges();
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user