/* * Pixel Dungeon * Copyright (C) 2012-2015 Oleg Dolya * * Shattered Pixel Dungeon * Copyright (C) 2014-2021 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 */ 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 void setPrice( List nodes, int value ) { for (T node : nodes) { node.price( value ); } } public static void buildDistanceMap( Collection nodes, Node focus ) { for (T node : nodes) { node.distance( Integer.MAX_VALUE ); } LinkedList queue = new LinkedList<>(); 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 List buildPath( Collection nodes, T from, T to ) { List path = new ArrayList<>(); T room = from; while (room != to) { int min = room.distance(); T next = null; Collection 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 edges(); } }