(svn r18364) -Codechange: move the pathfinders and their related files into a separate directory
This commit is contained in:
306
src/pathfinder/npf/aystar.cpp
Normal file
306
src/pathfinder/npf/aystar.cpp
Normal file
@@ -0,0 +1,306 @@
|
||||
/* $Id$ */
|
||||
|
||||
/*
|
||||
* This file is part of OpenTTD.
|
||||
* OpenTTD 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, version 2.
|
||||
* OpenTTD 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 OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/** @file aystar.cpp Implementation of A*. */
|
||||
|
||||
/*
|
||||
* This file has the core function for AyStar
|
||||
* AyStar is a fast pathfinding routine and is used for things like
|
||||
* AI_pathfinding and Train_pathfinding.
|
||||
* For more information about AyStar (A* Algorithm), you can look at
|
||||
* http://en.wikipedia.org/wiki/A-star_search_algorithm
|
||||
*/
|
||||
|
||||
/*
|
||||
* Friendly reminder:
|
||||
* Call (AyStar).free() when you are done with Aystar. It reserves a lot of memory
|
||||
* And when not free'd, it can cause system-crashes.
|
||||
* Also remember that when you stop an algorithm before it is finished, your
|
||||
* should call clear() yourself!
|
||||
*/
|
||||
|
||||
#include "../../stdafx.h"
|
||||
#include "../../core/alloc_func.hpp"
|
||||
#include "aystar.h"
|
||||
|
||||
int _aystar_stats_open_size;
|
||||
int _aystar_stats_closed_size;
|
||||
|
||||
/* This looks in the Hash if a node exists in ClosedList
|
||||
* If so, it returns the PathNode, else NULL */
|
||||
static PathNode *AyStarMain_ClosedList_IsInList(AyStar *aystar, const AyStarNode *node)
|
||||
{
|
||||
return (PathNode*)Hash_Get(&aystar->ClosedListHash, node->tile, node->direction);
|
||||
}
|
||||
|
||||
/* This adds a node to the ClosedList
|
||||
* It makes a copy of the data */
|
||||
static void AyStarMain_ClosedList_Add(AyStar *aystar, const PathNode *node)
|
||||
{
|
||||
/* Add a node to the ClosedList */
|
||||
PathNode *new_node = MallocT<PathNode>(1);
|
||||
*new_node = *node;
|
||||
Hash_Set(&aystar->ClosedListHash, node->node.tile, node->node.direction, new_node);
|
||||
}
|
||||
|
||||
/* Checks if a node is in the OpenList
|
||||
* If so, it returns the OpenListNode, else NULL */
|
||||
static OpenListNode *AyStarMain_OpenList_IsInList(AyStar *aystar, const AyStarNode *node)
|
||||
{
|
||||
return (OpenListNode*)Hash_Get(&aystar->OpenListHash, node->tile, node->direction);
|
||||
}
|
||||
|
||||
/* Gets the best node from OpenList
|
||||
* returns the best node, or NULL of none is found
|
||||
* Also it deletes the node from the OpenList */
|
||||
static OpenListNode *AyStarMain_OpenList_Pop(AyStar *aystar)
|
||||
{
|
||||
/* Return the item the Queue returns.. the best next OpenList item. */
|
||||
OpenListNode *res = (OpenListNode*)aystar->OpenListQueue.pop(&aystar->OpenListQueue);
|
||||
if (res != NULL) {
|
||||
Hash_Delete(&aystar->OpenListHash, res->path.node.tile, res->path.node.direction);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Adds a node to the OpenList
|
||||
* It makes a copy of node, and puts the pointer of parent in the struct */
|
||||
static void AyStarMain_OpenList_Add(AyStar *aystar, PathNode *parent, const AyStarNode *node, int f, int g)
|
||||
{
|
||||
/* Add a new Node to the OpenList */
|
||||
OpenListNode *new_node = MallocT<OpenListNode>(1);
|
||||
new_node->g = g;
|
||||
new_node->path.parent = parent;
|
||||
new_node->path.node = *node;
|
||||
Hash_Set(&aystar->OpenListHash, node->tile, node->direction, new_node);
|
||||
|
||||
/* Add it to the queue */
|
||||
aystar->OpenListQueue.push(&aystar->OpenListQueue, new_node, f);
|
||||
}
|
||||
|
||||
/*
|
||||
* Checks one tile and calculate his f-value
|
||||
* return values:
|
||||
* AYSTAR_DONE : indicates we are done
|
||||
*/
|
||||
static int AyStarMain_CheckTile(AyStar *aystar, AyStarNode *current, OpenListNode *parent)
|
||||
{
|
||||
int new_f, new_g, new_h;
|
||||
PathNode *closedlist_parent;
|
||||
OpenListNode *check;
|
||||
|
||||
/* Check the new node against the ClosedList */
|
||||
if (AyStarMain_ClosedList_IsInList(aystar, current) != NULL) return AYSTAR_DONE;
|
||||
|
||||
/* Calculate the G-value for this node */
|
||||
new_g = aystar->CalculateG(aystar, current, parent);
|
||||
/* If the value was INVALID_NODE, we don't do anything with this node */
|
||||
if (new_g == AYSTAR_INVALID_NODE) return AYSTAR_DONE;
|
||||
|
||||
/* There should not be given any other error-code.. */
|
||||
assert(new_g >= 0);
|
||||
/* Add the parent g-value to the new g-value */
|
||||
new_g += parent->g;
|
||||
if (aystar->max_path_cost != 0 && (uint)new_g > aystar->max_path_cost) return AYSTAR_DONE;
|
||||
|
||||
/* Calculate the h-value */
|
||||
new_h = aystar->CalculateH(aystar, current, parent);
|
||||
/* There should not be given any error-code.. */
|
||||
assert(new_h >= 0);
|
||||
|
||||
/* The f-value if g + h */
|
||||
new_f = new_g + new_h;
|
||||
|
||||
/* Get the pointer to the parent in the ClosedList (the currentone is to a copy of the one in the OpenList) */
|
||||
closedlist_parent = AyStarMain_ClosedList_IsInList(aystar, &parent->path.node);
|
||||
|
||||
/* Check if this item is already in the OpenList */
|
||||
check = AyStarMain_OpenList_IsInList(aystar, current);
|
||||
if (check != NULL) {
|
||||
uint i;
|
||||
/* Yes, check if this g value is lower.. */
|
||||
if (new_g > check->g) return AYSTAR_DONE;
|
||||
aystar->OpenListQueue.del(&aystar->OpenListQueue, check, 0);
|
||||
/* It is lower, so change it to this item */
|
||||
check->g = new_g;
|
||||
check->path.parent = closedlist_parent;
|
||||
/* Copy user data, will probably have changed */
|
||||
for (i = 0; i < lengthof(current->user_data); i++) {
|
||||
check->path.node.user_data[i] = current->user_data[i];
|
||||
}
|
||||
/* Readd him in the OpenListQueue */
|
||||
aystar->OpenListQueue.push(&aystar->OpenListQueue, check, new_f);
|
||||
} else {
|
||||
/* A new node, add him to the OpenList */
|
||||
AyStarMain_OpenList_Add(aystar, closedlist_parent, current, new_f, new_g);
|
||||
}
|
||||
|
||||
return AYSTAR_DONE;
|
||||
}
|
||||
|
||||
/*
|
||||
* This function is the core of AyStar. It handles one item and checks
|
||||
* his neighbour items. If they are valid, they are added to be checked too.
|
||||
* return values:
|
||||
* AYSTAR_EMPTY_OPENLIST : indicates all items are tested, and no path
|
||||
* has been found.
|
||||
* AYSTAR_LIMIT_REACHED : Indicates that the max_nodes limit has been
|
||||
* reached.
|
||||
* AYSTAR_FOUND_END_NODE : indicates we found the end. Path_found now is true, and in path is the path found.
|
||||
* AYSTAR_STILL_BUSY : indicates we have done this tile, did not found the path yet, and have items left to try.
|
||||
*/
|
||||
static int AyStarMain_Loop(AyStar *aystar)
|
||||
{
|
||||
int i, r;
|
||||
|
||||
/* Get the best node from OpenList */
|
||||
OpenListNode *current = AyStarMain_OpenList_Pop(aystar);
|
||||
/* If empty, drop an error */
|
||||
if (current == NULL) return AYSTAR_EMPTY_OPENLIST;
|
||||
|
||||
/* Check for end node and if found, return that code */
|
||||
if (aystar->EndNodeCheck(aystar, current) == AYSTAR_FOUND_END_NODE) {
|
||||
if (aystar->FoundEndNode != NULL)
|
||||
aystar->FoundEndNode(aystar, current);
|
||||
free(current);
|
||||
return AYSTAR_FOUND_END_NODE;
|
||||
}
|
||||
|
||||
/* Add the node to the ClosedList */
|
||||
AyStarMain_ClosedList_Add(aystar, ¤t->path);
|
||||
|
||||
/* Load the neighbours */
|
||||
aystar->GetNeighbours(aystar, current);
|
||||
|
||||
/* Go through all neighbours */
|
||||
for (i = 0; i < aystar->num_neighbours; i++) {
|
||||
/* Check and add them to the OpenList if needed */
|
||||
r = aystar->checktile(aystar, &aystar->neighbours[i], current);
|
||||
}
|
||||
|
||||
/* Free the node */
|
||||
free(current);
|
||||
|
||||
if (aystar->max_search_nodes != 0 && Hash_Size(&aystar->ClosedListHash) >= aystar->max_search_nodes) {
|
||||
/* We've expanded enough nodes */
|
||||
return AYSTAR_LIMIT_REACHED;
|
||||
} else {
|
||||
/* Return that we are still busy */
|
||||
return AYSTAR_STILL_BUSY;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* This function frees the memory it allocated
|
||||
*/
|
||||
static void AyStarMain_Free(AyStar *aystar)
|
||||
{
|
||||
aystar->OpenListQueue.free(&aystar->OpenListQueue, false);
|
||||
/* 2nd argument above is false, below is true, to free the values only
|
||||
* once */
|
||||
delete_Hash(&aystar->OpenListHash, true);
|
||||
delete_Hash(&aystar->ClosedListHash, true);
|
||||
#ifdef AYSTAR_DEBUG
|
||||
printf("[AyStar] Memory free'd\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
* This function make the memory go back to zero
|
||||
* This function should be called when you are using the same instance again.
|
||||
*/
|
||||
void AyStarMain_Clear(AyStar *aystar)
|
||||
{
|
||||
/* Clean the Queue, but not the elements within. That will be done by
|
||||
* the hash. */
|
||||
aystar->OpenListQueue.clear(&aystar->OpenListQueue, false);
|
||||
/* Clean the hashes */
|
||||
clear_Hash(&aystar->OpenListHash, true);
|
||||
clear_Hash(&aystar->ClosedListHash, true);
|
||||
|
||||
#ifdef AYSTAR_DEBUG
|
||||
printf("[AyStar] Cleared AyStar\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
* This is the function you call to run AyStar.
|
||||
* return values:
|
||||
* AYSTAR_FOUND_END_NODE : indicates we found an end node.
|
||||
* AYSTAR_NO_PATH : indicates that there was no path found.
|
||||
* AYSTAR_STILL_BUSY : indicates we have done some checked, that we did not found the path yet, and that we still have items left to try.
|
||||
* When the algorithm is done (when the return value is not AYSTAR_STILL_BUSY)
|
||||
* aystar->clear() is called. Note that when you stop the algorithm halfway,
|
||||
* you should still call clear() yourself!
|
||||
*/
|
||||
int AyStarMain_Main(AyStar *aystar)
|
||||
{
|
||||
int r, i = 0;
|
||||
/* Loop through the OpenList
|
||||
* Quit if result is no AYSTAR_STILL_BUSY or is more than loops_per_tick */
|
||||
while ((r = aystar->loop(aystar)) == AYSTAR_STILL_BUSY && (aystar->loops_per_tick == 0 || ++i < aystar->loops_per_tick)) { }
|
||||
#ifdef AYSTAR_DEBUG
|
||||
switch (r) {
|
||||
case AYSTAR_FOUND_END_NODE: printf("[AyStar] Found path!\n"); break;
|
||||
case AYSTAR_EMPTY_OPENLIST: printf("[AyStar] OpenList run dry, no path found\n"); break;
|
||||
case AYSTAR_LIMIT_REACHED: printf("[AyStar] Exceeded search_nodes, no path found\n"); break;
|
||||
default: break;
|
||||
}
|
||||
#endif
|
||||
if (r != AYSTAR_STILL_BUSY) {
|
||||
/* We're done, clean up */
|
||||
_aystar_stats_open_size = aystar->OpenListHash.size;
|
||||
_aystar_stats_closed_size = aystar->ClosedListHash.size;
|
||||
aystar->clear(aystar);
|
||||
}
|
||||
|
||||
switch (r) {
|
||||
case AYSTAR_FOUND_END_NODE: return AYSTAR_FOUND_END_NODE;
|
||||
case AYSTAR_EMPTY_OPENLIST:
|
||||
case AYSTAR_LIMIT_REACHED: return AYSTAR_NO_PATH;
|
||||
default: return AYSTAR_STILL_BUSY;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Adds a node from where to start an algorithm. Multiple nodes can be added
|
||||
* if wanted. You should make sure that clear() is called before adding nodes
|
||||
* if the AyStar has been used before (though the normal main loop calls
|
||||
* clear() automatically when the algorithm finishes
|
||||
* g is the cost for starting with this node.
|
||||
*/
|
||||
static void AyStarMain_AddStartNode(AyStar *aystar, AyStarNode *start_node, uint g)
|
||||
{
|
||||
#ifdef AYSTAR_DEBUG
|
||||
printf("[AyStar] Starting A* Algorithm from node (%d, %d, %d)\n",
|
||||
TileX(start_node->tile), TileY(start_node->tile), start_node->direction);
|
||||
#endif
|
||||
AyStarMain_OpenList_Add(aystar, NULL, start_node, 0, g);
|
||||
}
|
||||
|
||||
void init_AyStar(AyStar *aystar, Hash_HashProc hash, uint num_buckets)
|
||||
{
|
||||
/* Allocated the Hash for the OpenList and ClosedList */
|
||||
init_Hash(&aystar->OpenListHash, hash, num_buckets);
|
||||
init_Hash(&aystar->ClosedListHash, hash, num_buckets);
|
||||
|
||||
/* Set up our sorting queue
|
||||
* BinaryHeap allocates a block of 1024 nodes
|
||||
* When thatone gets full it reserves an otherone, till this number
|
||||
* That is why it can stay this high */
|
||||
init_BinaryHeap(&aystar->OpenListQueue, 102400);
|
||||
|
||||
aystar->addstart = AyStarMain_AddStartNode;
|
||||
aystar->main = AyStarMain_Main;
|
||||
aystar->loop = AyStarMain_Loop;
|
||||
aystar->free = AyStarMain_Free;
|
||||
aystar->clear = AyStarMain_Clear;
|
||||
aystar->checktile = AyStarMain_CheckTile;
|
||||
}
|
||||
181
src/pathfinder/npf/aystar.h
Normal file
181
src/pathfinder/npf/aystar.h
Normal file
@@ -0,0 +1,181 @@
|
||||
/* $Id$ */
|
||||
|
||||
/*
|
||||
* This file is part of OpenTTD.
|
||||
* OpenTTD 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, version 2.
|
||||
* OpenTTD 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 OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/** @file aystar.h
|
||||
* This file has the header for AyStar
|
||||
* AyStar is a fast pathfinding routine and is used for things like
|
||||
* AI_pathfinding and Train_pathfinding.
|
||||
* For more information about AyStar (A* Algorithm), you can look at
|
||||
* http://en.wikipedia.org/wiki/A-star_search_algorithm
|
||||
*/
|
||||
|
||||
#ifndef AYSTAR_H
|
||||
#define AYSTAR_H
|
||||
|
||||
#include "queue.h"
|
||||
#include "../../tile_type.h"
|
||||
#include "../../track_type.h"
|
||||
|
||||
//#define AYSTAR_DEBUG
|
||||
enum {
|
||||
AYSTAR_FOUND_END_NODE,
|
||||
AYSTAR_EMPTY_OPENLIST,
|
||||
AYSTAR_STILL_BUSY,
|
||||
AYSTAR_NO_PATH,
|
||||
AYSTAR_LIMIT_REACHED,
|
||||
AYSTAR_DONE
|
||||
};
|
||||
|
||||
enum{
|
||||
AYSTAR_INVALID_NODE = -1,
|
||||
};
|
||||
|
||||
struct AyStarNode {
|
||||
TileIndex tile;
|
||||
Trackdir direction;
|
||||
uint user_data[2];
|
||||
};
|
||||
|
||||
/* The resulting path has nodes looking like this. */
|
||||
struct PathNode {
|
||||
AyStarNode node;
|
||||
/* The parent of this item */
|
||||
PathNode *parent;
|
||||
};
|
||||
|
||||
/* For internal use only
|
||||
* We do not save the h-value, because it is only needed to calculate the f-value.
|
||||
* h-value should _always_ be the distance left to the end-tile. */
|
||||
struct OpenListNode {
|
||||
int g;
|
||||
PathNode path;
|
||||
};
|
||||
|
||||
struct AyStar;
|
||||
/*
|
||||
* This function is called to check if the end-tile is found
|
||||
* return values can be:
|
||||
* AYSTAR_FOUND_END_NODE : indicates this is the end tile
|
||||
* AYSTAR_DONE : indicates this is not the end tile (or direction was wrong)
|
||||
*/
|
||||
/*
|
||||
* The 2nd parameter should be OpenListNode, and NOT AyStarNode. AyStarNode is
|
||||
* part of OpenListNode and so it could be accessed without any problems.
|
||||
* The good part about OpenListNode is, and how AIs use it, that you can
|
||||
* access the parent of the current node, and so check if you, for example
|
||||
* don't try to enter the file tile with a 90-degree curve. So please, leave
|
||||
* this an OpenListNode, it works just fine -- TrueLight
|
||||
*/
|
||||
typedef int32 AyStar_EndNodeCheck(AyStar *aystar, OpenListNode *current);
|
||||
|
||||
/*
|
||||
* This function is called to calculate the G-value for AyStar Algorithm.
|
||||
* return values can be:
|
||||
* AYSTAR_INVALID_NODE : indicates an item is not valid (e.g.: unwalkable)
|
||||
* Any value >= 0 : the g-value for this tile
|
||||
*/
|
||||
typedef int32 AyStar_CalculateG(AyStar *aystar, AyStarNode *current, OpenListNode *parent);
|
||||
|
||||
/*
|
||||
* This function is called to calculate the H-value for AyStar Algorithm.
|
||||
* Mostly, this must result the distance (Manhattan way) between the
|
||||
* current point and the end point
|
||||
* return values can be:
|
||||
* Any value >= 0 : the h-value for this tile
|
||||
*/
|
||||
typedef int32 AyStar_CalculateH(AyStar *aystar, AyStarNode *current, OpenListNode *parent);
|
||||
|
||||
/*
|
||||
* This function request the tiles around the current tile and put them in tiles_around
|
||||
* tiles_around is never resetted, so if you are not using directions, just leave it alone.
|
||||
* Warning: never add more tiles_around than memory allocated for it.
|
||||
*/
|
||||
typedef void AyStar_GetNeighbours(AyStar *aystar, OpenListNode *current);
|
||||
|
||||
/*
|
||||
* If the End Node is found, this function is called.
|
||||
* It can do, for example, calculate the route and put that in an array
|
||||
*/
|
||||
typedef void AyStar_FoundEndNode(AyStar *aystar, OpenListNode *current);
|
||||
|
||||
/* For internal use, see aystar.cpp */
|
||||
typedef void AyStar_AddStartNode(AyStar *aystar, AyStarNode *start_node, uint g);
|
||||
typedef int AyStar_Main(AyStar *aystar);
|
||||
typedef int AyStar_Loop(AyStar *aystar);
|
||||
typedef int AyStar_CheckTile(AyStar *aystar, AyStarNode *current, OpenListNode *parent);
|
||||
typedef void AyStar_Free(AyStar *aystar);
|
||||
typedef void AyStar_Clear(AyStar *aystar);
|
||||
|
||||
struct AyStar {
|
||||
/* These fields should be filled before initting the AyStar, but not changed
|
||||
* afterwards (except for user_data and user_path)! (free and init again to change them) */
|
||||
|
||||
/* These should point to the application specific routines that do the
|
||||
* actual work */
|
||||
AyStar_CalculateG *CalculateG;
|
||||
AyStar_CalculateH *CalculateH;
|
||||
AyStar_GetNeighbours *GetNeighbours;
|
||||
AyStar_EndNodeCheck *EndNodeCheck;
|
||||
AyStar_FoundEndNode *FoundEndNode;
|
||||
|
||||
/* These are completely untouched by AyStar, they can be accesed by
|
||||
* the application specific routines to input and output data.
|
||||
* user_path should typically contain data about the resulting path
|
||||
* afterwards, user_target should typically contain information about
|
||||
* what where looking for, and user_data can contain just about
|
||||
* everything */
|
||||
void *user_path;
|
||||
void *user_target;
|
||||
uint user_data[10];
|
||||
|
||||
/* How many loops are there called before AyStarMain_Main gives
|
||||
* control back to the caller. 0 = until done */
|
||||
byte loops_per_tick;
|
||||
/* If the g-value goes over this number, it stops searching
|
||||
* 0 = infinite */
|
||||
uint max_path_cost;
|
||||
/* The maximum amount of nodes that will be expanded, 0 = infinite */
|
||||
uint max_search_nodes;
|
||||
|
||||
/* These should be filled with the neighbours of a tile by
|
||||
* GetNeighbours */
|
||||
AyStarNode neighbours[12];
|
||||
byte num_neighbours;
|
||||
|
||||
/* These will contain the methods for manipulating the AyStar. Only
|
||||
* main() should be called externally */
|
||||
AyStar_AddStartNode *addstart;
|
||||
AyStar_Main *main;
|
||||
AyStar_Loop *loop;
|
||||
AyStar_Free *free;
|
||||
AyStar_Clear *clear;
|
||||
AyStar_CheckTile *checktile;
|
||||
|
||||
/* These will contain the open and closed lists */
|
||||
|
||||
/* The actual closed list */
|
||||
Hash ClosedListHash;
|
||||
/* The open queue */
|
||||
Queue OpenListQueue;
|
||||
/* An extra hash to speed up the process of looking up an element in
|
||||
* the open list */
|
||||
Hash OpenListHash;
|
||||
};
|
||||
|
||||
|
||||
int AyStarMain_Main(AyStar *aystar);
|
||||
void AyStarMain_Clear(AyStar *aystar);
|
||||
|
||||
/* Initialize an AyStar. You should fill all appropriate fields before
|
||||
* callling init_AyStar (see the declaration of AyStar for which fields are
|
||||
* internal */
|
||||
void init_AyStar(AyStar *aystar, Hash_HashProc hash, uint num_buckets);
|
||||
|
||||
|
||||
#endif /* AYSTAR_H */
|
||||
1120
src/pathfinder/npf/npf.cpp
Normal file
1120
src/pathfinder/npf/npf.cpp
Normal file
File diff suppressed because it is too large
Load Diff
151
src/pathfinder/npf/npf.h
Normal file
151
src/pathfinder/npf/npf.h
Normal file
@@ -0,0 +1,151 @@
|
||||
/* $Id$ */
|
||||
|
||||
/*
|
||||
* This file is part of OpenTTD.
|
||||
* OpenTTD 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, version 2.
|
||||
* OpenTTD 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 OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/** @file npf.h New A* pathfinder. */
|
||||
|
||||
#ifndef NPF_H
|
||||
#define NPF_H
|
||||
|
||||
#include "aystar.h"
|
||||
#include "../../station_type.h"
|
||||
#include "../../rail_type.h"
|
||||
#include "../../company_type.h"
|
||||
#include "../../vehicle_type.h"
|
||||
#include "../../tile_type.h"
|
||||
#include "../../track_type.h"
|
||||
#include "../../core/bitmath_func.hpp"
|
||||
#include "../../transport_type.h"
|
||||
|
||||
/* mowing grass */
|
||||
enum {
|
||||
NPF_HASH_BITS = 12, ///< The size of the hash used in pathfinding. Just changing this value should be sufficient to change the hash size. Should be an even value.
|
||||
/* Do no change below values */
|
||||
NPF_HASH_SIZE = 1 << NPF_HASH_BITS,
|
||||
NPF_HASH_HALFBITS = NPF_HASH_BITS / 2,
|
||||
NPF_HASH_HALFMASK = (1 << NPF_HASH_HALFBITS) - 1
|
||||
};
|
||||
|
||||
/* For new pathfinding. Define here so it is globally available without having
|
||||
* to include npf.h */
|
||||
enum {
|
||||
NPF_TILE_LENGTH = 100
|
||||
};
|
||||
|
||||
enum {
|
||||
/** This penalty is the equivalent of "inifite", which means that paths that
|
||||
* get this penalty will be chosen, but only if there is no other route
|
||||
* without it. Be careful with not applying this penalty to often, or the
|
||||
* total path cost might overflow..
|
||||
* For now, this is just a Very Big Penalty, we might actually implement
|
||||
* this in a nicer way :-)
|
||||
*/
|
||||
NPF_INFINITE_PENALTY = 1000 * NPF_TILE_LENGTH
|
||||
};
|
||||
|
||||
/* Meant to be stored in AyStar.targetdata */
|
||||
struct NPFFindStationOrTileData {
|
||||
TileIndex dest_coords; ///< An indication of where the station is, for heuristic purposes, or the target tile
|
||||
StationID station_index; ///< station index we're heading for, or INVALID_STATION when we're heading for a tile
|
||||
bool reserve_path; ///< Indicates whether the found path should be reserved
|
||||
const Vehicle *v; ///< The vehicle we are pathfinding for
|
||||
};
|
||||
|
||||
/* Indices into AyStar.userdata[] */
|
||||
enum {
|
||||
NPF_TYPE = 0, ///< Contains a TransportTypes value
|
||||
NPF_SUB_TYPE, ///< Contains the sub transport type
|
||||
NPF_OWNER, ///< Contains an Owner value
|
||||
NPF_RAILTYPES, ///< Contains a bitmask the compatible RailTypes of the engine when NPF_TYPE == TRANSPORT_RAIL. Unused otherwise.
|
||||
};
|
||||
|
||||
/* Indices into AyStarNode.userdata[] */
|
||||
enum {
|
||||
NPF_TRACKDIR_CHOICE = 0, ///< The trackdir chosen to get here
|
||||
NPF_NODE_FLAGS,
|
||||
};
|
||||
|
||||
/* Flags for AyStarNode.userdata[NPF_NODE_FLAGS]. Use NPFGetBit() and NPFGetBit() to use them. */
|
||||
enum NPFNodeFlag {
|
||||
NPF_FLAG_SEEN_SIGNAL, ///< Used to mark that a signal was seen on the way, for rail only
|
||||
NPF_FLAG_2ND_SIGNAL, ///< Used to mark that two signals were seen, rail only
|
||||
NPF_FLAG_3RD_SIGNAL, ///< Used to mark that three signals were seen, rail only
|
||||
NPF_FLAG_REVERSE, ///< Used to mark that this node was reached from the second start node, if applicable
|
||||
NPF_FLAG_LAST_SIGNAL_RED, ///< Used to mark that the last signal on this path was red
|
||||
NPF_FLAG_IGNORE_START_TILE, ///< Used to mark that the start tile is invalid, and searching should start from the second tile on
|
||||
NPF_FLAG_TARGET_RESERVED, ///< Used to mark that the possible reservation target is already reserved
|
||||
NPF_FLAG_IGNORE_RESERVED, ///< Used to mark that reserved tiles should be considered impassable
|
||||
};
|
||||
|
||||
/* Meant to be stored in AyStar.userpath */
|
||||
struct NPFFoundTargetData {
|
||||
uint best_bird_dist; ///< The best heuristic found. Is 0 if the target was found
|
||||
uint best_path_dist; ///< The shortest path. Is UINT_MAX if no path is found
|
||||
Trackdir best_trackdir; ///< The trackdir that leads to the shortest path/closest birds dist
|
||||
AyStarNode node; ///< The node within the target the search led us to
|
||||
bool res_okay; ///< True if a path reservation could be made
|
||||
};
|
||||
|
||||
/* These functions below are _not_ re-entrant, in favor of speed! */
|
||||
|
||||
/* Will search from the given tile and direction, for a route to the given
|
||||
* station for the given transport type. See the declaration of
|
||||
* NPFFoundTargetData above for the meaning of the result. */
|
||||
NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, NPFFindStationOrTileData *target, TransportType type, uint sub_type, Owner owner, RailTypes railtypes);
|
||||
|
||||
/* Will search as above, but with two start nodes, the second being the
|
||||
* reverse. Look at the NPF_FLAG_REVERSE flag in the result node to see which
|
||||
* direction was taken (NPFGetBit(result.node, NPF_FLAG_REVERSE)) */
|
||||
NPFFoundTargetData NPFRouteToStationOrTileTwoWay(TileIndex tile1, Trackdir trackdir1, bool ignore_start_tile1, TileIndex tile2, Trackdir trackdir2, bool ignore_start_tile2, NPFFindStationOrTileData *target, TransportType type, uint sub_type, Owner owner, RailTypes railtypes);
|
||||
|
||||
/* Will search a route to the closest depot. */
|
||||
|
||||
/* Search using breadth first. Good for little track choice and inaccurate
|
||||
* heuristic, such as railway/road.*/
|
||||
NPFFoundTargetData NPFRouteToDepotBreadthFirst(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, TransportType type, uint sub_type, Owner owner, RailTypes railtypes);
|
||||
/* Same as above but with two start nodes, the second being the reverse. Call
|
||||
* NPFGetBit(result.node, NPF_FLAG_REVERSE) to see from which node the path
|
||||
* orginated. All pathfs from the second node will have the given
|
||||
* reverse_penalty applied (NPF_TILE_LENGTH is the equivalent of one full
|
||||
* tile).
|
||||
*/
|
||||
NPFFoundTargetData NPFRouteToDepotBreadthFirstTwoWay(TileIndex tile1, Trackdir trackdir1, bool ignore_start_tile1, TileIndex tile2, Trackdir trackdir2, bool ignore_start_tile2, TransportType type, uint sub_type, Owner owner, RailTypes railtypes, uint reverse_penalty);
|
||||
/* Search by trying each depot in order of Manhattan Distance. Good for lots
|
||||
* of choices and accurate heuristics, such as water. */
|
||||
NPFFoundTargetData NPFRouteToDepotTrialError(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, TransportType type, uint sub_type, Owner owner, RailTypes railtypes);
|
||||
|
||||
/**
|
||||
* Search for any safe tile using a breadth first search and try to reserve a path.
|
||||
*/
|
||||
NPFFoundTargetData NPFRouteToSafeTile(const struct Train *v, TileIndex tile, Trackdir trackdir, bool override_railtype);
|
||||
|
||||
|
||||
void NPFFillWithOrderData(NPFFindStationOrTileData *fstd, Vehicle *v, bool reserve_path = false);
|
||||
|
||||
|
||||
/*
|
||||
* Functions to manipulate the various NPF related flags on an AyStarNode.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Returns the current value of the given flag on the given AyStarNode.
|
||||
*/
|
||||
static inline bool NPFGetFlag(const AyStarNode *node, NPFNodeFlag flag)
|
||||
{
|
||||
return HasBit(node->user_data[NPF_NODE_FLAGS], flag);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the given flag on the given AyStarNode to the given value.
|
||||
*/
|
||||
static inline void NPFSetFlag(AyStarNode *node, NPFNodeFlag flag, bool value)
|
||||
{
|
||||
SB(node->user_data[NPF_NODE_FLAGS], flag, 1, value);
|
||||
}
|
||||
|
||||
#endif /* NPF_H */
|
||||
577
src/pathfinder/npf/queue.cpp
Normal file
577
src/pathfinder/npf/queue.cpp
Normal file
@@ -0,0 +1,577 @@
|
||||
/* $Id$ */
|
||||
|
||||
/*
|
||||
* This file is part of OpenTTD.
|
||||
* OpenTTD 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, version 2.
|
||||
* OpenTTD 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 OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/** @file queue.cpp Implementation of the Queue/Hash. */
|
||||
|
||||
#include "../../stdafx.h"
|
||||
#include "../../core/alloc_func.hpp"
|
||||
#include "queue.h"
|
||||
|
||||
|
||||
/*
|
||||
* Insertion Sorter
|
||||
*/
|
||||
|
||||
static void InsSort_Clear(Queue *q, bool free_values)
|
||||
{
|
||||
InsSortNode *node = q->data.inssort.first;
|
||||
InsSortNode *prev;
|
||||
|
||||
while (node != NULL) {
|
||||
if (free_values) free(node->item);
|
||||
prev = node;
|
||||
node = node->next;
|
||||
free(prev);
|
||||
}
|
||||
q->data.inssort.first = NULL;
|
||||
}
|
||||
|
||||
static void InsSort_Free(Queue *q, bool free_values)
|
||||
{
|
||||
q->clear(q, free_values);
|
||||
}
|
||||
|
||||
static bool InsSort_Push(Queue *q, void *item, int priority)
|
||||
{
|
||||
InsSortNode *newnode = MallocT<InsSortNode>(1);
|
||||
|
||||
newnode->item = item;
|
||||
newnode->priority = priority;
|
||||
if (q->data.inssort.first == NULL ||
|
||||
q->data.inssort.first->priority >= priority) {
|
||||
newnode->next = q->data.inssort.first;
|
||||
q->data.inssort.first = newnode;
|
||||
} else {
|
||||
InsSortNode *node = q->data.inssort.first;
|
||||
while (node != NULL) {
|
||||
if (node->next == NULL || node->next->priority >= priority) {
|
||||
newnode->next = node->next;
|
||||
node->next = newnode;
|
||||
break;
|
||||
}
|
||||
node = node->next;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static void *InsSort_Pop(Queue *q)
|
||||
{
|
||||
InsSortNode *node = q->data.inssort.first;
|
||||
void *result;
|
||||
|
||||
if (node == NULL) return NULL;
|
||||
result = node->item;
|
||||
q->data.inssort.first = q->data.inssort.first->next;
|
||||
assert(q->data.inssort.first == NULL || q->data.inssort.first->priority >= node->priority);
|
||||
free(node);
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool InsSort_Delete(Queue *q, void *item, int priority)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void init_InsSort(Queue *q)
|
||||
{
|
||||
q->push = InsSort_Push;
|
||||
q->pop = InsSort_Pop;
|
||||
q->del = InsSort_Delete;
|
||||
q->clear = InsSort_Clear;
|
||||
q->free = InsSort_Free;
|
||||
q->data.inssort.first = NULL;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Binary Heap
|
||||
* For information, see: http://www.policyalmanac.org/games/binaryHeaps.htm
|
||||
*/
|
||||
|
||||
#define BINARY_HEAP_BLOCKSIZE (1 << BINARY_HEAP_BLOCKSIZE_BITS)
|
||||
#define BINARY_HEAP_BLOCKSIZE_MASK (BINARY_HEAP_BLOCKSIZE - 1)
|
||||
|
||||
/* To make our life easy, we make the next define
|
||||
* Because Binary Heaps works with array from 1 to n,
|
||||
* and C with array from 0 to n-1, and we don't like typing
|
||||
* q->data.binaryheap.elements[i - 1] every time, we use this define. */
|
||||
#define BIN_HEAP_ARR(i) q->data.binaryheap.elements[((i) - 1) >> BINARY_HEAP_BLOCKSIZE_BITS][((i) - 1) & BINARY_HEAP_BLOCKSIZE_MASK]
|
||||
|
||||
static void BinaryHeap_Clear(Queue *q, bool free_values)
|
||||
{
|
||||
/* Free all items if needed and free all but the first blocks of memory */
|
||||
uint i;
|
||||
uint j;
|
||||
|
||||
for (i = 0; i < q->data.binaryheap.blocks; i++) {
|
||||
if (q->data.binaryheap.elements[i] == NULL) {
|
||||
/* No more allocated blocks */
|
||||
break;
|
||||
}
|
||||
/* For every allocated block */
|
||||
if (free_values) {
|
||||
for (j = 0; j < (1 << BINARY_HEAP_BLOCKSIZE_BITS); j++) {
|
||||
/* For every element in the block */
|
||||
if ((q->data.binaryheap.size >> BINARY_HEAP_BLOCKSIZE_BITS) == i &&
|
||||
(q->data.binaryheap.size & BINARY_HEAP_BLOCKSIZE_MASK) == j) {
|
||||
break; // We're past the last element
|
||||
}
|
||||
free(q->data.binaryheap.elements[i][j].item);
|
||||
}
|
||||
}
|
||||
if (i != 0) {
|
||||
/* Leave the first block of memory alone */
|
||||
free(q->data.binaryheap.elements[i]);
|
||||
q->data.binaryheap.elements[i] = NULL;
|
||||
}
|
||||
}
|
||||
q->data.binaryheap.size = 0;
|
||||
q->data.binaryheap.blocks = 1;
|
||||
}
|
||||
|
||||
static void BinaryHeap_Free(Queue *q, bool free_values)
|
||||
{
|
||||
uint i;
|
||||
|
||||
q->clear(q, free_values);
|
||||
for (i = 0; i < q->data.binaryheap.blocks; i++) {
|
||||
if (q->data.binaryheap.elements[i] == NULL) break;
|
||||
free(q->data.binaryheap.elements[i]);
|
||||
}
|
||||
free(q->data.binaryheap.elements);
|
||||
}
|
||||
|
||||
static bool BinaryHeap_Push(Queue *q, void *item, int priority)
|
||||
{
|
||||
#ifdef QUEUE_DEBUG
|
||||
printf("[BinaryHeap] Pushing an element. There are %d elements left\n", q->data.binaryheap.size);
|
||||
#endif
|
||||
|
||||
if (q->data.binaryheap.size == q->data.binaryheap.max_size) return false;
|
||||
assert(q->data.binaryheap.size < q->data.binaryheap.max_size);
|
||||
|
||||
if (q->data.binaryheap.elements[q->data.binaryheap.size >> BINARY_HEAP_BLOCKSIZE_BITS] == NULL) {
|
||||
/* The currently allocated blocks are full, allocate a new one */
|
||||
assert((q->data.binaryheap.size & BINARY_HEAP_BLOCKSIZE_MASK) == 0);
|
||||
q->data.binaryheap.elements[q->data.binaryheap.size >> BINARY_HEAP_BLOCKSIZE_BITS] = MallocT<BinaryHeapNode>(BINARY_HEAP_BLOCKSIZE);
|
||||
q->data.binaryheap.blocks++;
|
||||
#ifdef QUEUE_DEBUG
|
||||
printf("[BinaryHeap] Increasing size of elements to %d nodes\n", q->data.binaryheap.blocks * BINARY_HEAP_BLOCKSIZE);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Add the item at the end of the array */
|
||||
BIN_HEAP_ARR(q->data.binaryheap.size + 1).priority = priority;
|
||||
BIN_HEAP_ARR(q->data.binaryheap.size + 1).item = item;
|
||||
q->data.binaryheap.size++;
|
||||
|
||||
/* Now we are going to check where it belongs. As long as the parent is
|
||||
* bigger, we switch with the parent */
|
||||
{
|
||||
BinaryHeapNode temp;
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = q->data.binaryheap.size;
|
||||
while (i > 1) {
|
||||
/* Get the parent of this object (divide by 2) */
|
||||
j = i / 2;
|
||||
/* Is the parent bigger then the current, switch them */
|
||||
if (BIN_HEAP_ARR(i).priority <= BIN_HEAP_ARR(j).priority) {
|
||||
temp = BIN_HEAP_ARR(j);
|
||||
BIN_HEAP_ARR(j) = BIN_HEAP_ARR(i);
|
||||
BIN_HEAP_ARR(i) = temp;
|
||||
i = j;
|
||||
} else {
|
||||
/* It is not, we're done! */
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool BinaryHeap_Delete(Queue *q, void *item, int priority)
|
||||
{
|
||||
uint i = 0;
|
||||
|
||||
#ifdef QUEUE_DEBUG
|
||||
printf("[BinaryHeap] Deleting an element. There are %d elements left\n", q->data.binaryheap.size);
|
||||
#endif
|
||||
|
||||
/* First, we try to find the item.. */
|
||||
do {
|
||||
if (BIN_HEAP_ARR(i + 1).item == item) break;
|
||||
i++;
|
||||
} while (i < q->data.binaryheap.size);
|
||||
/* We did not find the item, so we return false */
|
||||
if (i == q->data.binaryheap.size) return false;
|
||||
|
||||
/* Now we put the last item over the current item while decreasing the size of the elements */
|
||||
q->data.binaryheap.size--;
|
||||
BIN_HEAP_ARR(i + 1) = BIN_HEAP_ARR(q->data.binaryheap.size + 1);
|
||||
|
||||
/* Now the only thing we have to do, is resort it..
|
||||
* On place i there is the item to be sorted.. let's start there */
|
||||
{
|
||||
uint j;
|
||||
BinaryHeapNode temp;
|
||||
/* Because of the fact that Binary Heap uses array from 1 to n, we need to
|
||||
* increase i by 1
|
||||
*/
|
||||
i++;
|
||||
|
||||
for (;;) {
|
||||
j = i;
|
||||
/* Check if we have 2 childs */
|
||||
if (2 * j + 1 <= q->data.binaryheap.size) {
|
||||
/* Is this child smaller than the parent? */
|
||||
if (BIN_HEAP_ARR(j).priority >= BIN_HEAP_ARR(2 * j).priority) i = 2 * j;
|
||||
/* Yes, we _need_ to use i here, not j, because we want to have the smallest child
|
||||
* This way we get that straight away! */
|
||||
if (BIN_HEAP_ARR(i).priority >= BIN_HEAP_ARR(2 * j + 1).priority) i = 2 * j + 1;
|
||||
/* Do we have one child? */
|
||||
} else if (2 * j <= q->data.binaryheap.size) {
|
||||
if (BIN_HEAP_ARR(j).priority >= BIN_HEAP_ARR(2 * j).priority) i = 2 * j;
|
||||
}
|
||||
|
||||
/* One of our childs is smaller than we are, switch */
|
||||
if (i != j) {
|
||||
temp = BIN_HEAP_ARR(j);
|
||||
BIN_HEAP_ARR(j) = BIN_HEAP_ARR(i);
|
||||
BIN_HEAP_ARR(i) = temp;
|
||||
} else {
|
||||
/* None of our childs is smaller, so we stay here.. stop :) */
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void *BinaryHeap_Pop(Queue *q)
|
||||
{
|
||||
void *result;
|
||||
|
||||
#ifdef QUEUE_DEBUG
|
||||
printf("[BinaryHeap] Popping an element. There are %d elements left\n", q->data.binaryheap.size);
|
||||
#endif
|
||||
|
||||
if (q->data.binaryheap.size == 0) return NULL;
|
||||
|
||||
/* The best item is always on top, so give that as result */
|
||||
result = BIN_HEAP_ARR(1).item;
|
||||
/* And now we should get rid of this item... */
|
||||
BinaryHeap_Delete(q, BIN_HEAP_ARR(1).item, BIN_HEAP_ARR(1).priority);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void init_BinaryHeap(Queue *q, uint max_size)
|
||||
{
|
||||
assert(q != NULL);
|
||||
q->push = BinaryHeap_Push;
|
||||
q->pop = BinaryHeap_Pop;
|
||||
q->del = BinaryHeap_Delete;
|
||||
q->clear = BinaryHeap_Clear;
|
||||
q->free = BinaryHeap_Free;
|
||||
q->data.binaryheap.max_size = max_size;
|
||||
q->data.binaryheap.size = 0;
|
||||
/* We malloc memory in block of BINARY_HEAP_BLOCKSIZE
|
||||
* It autosizes when it runs out of memory */
|
||||
q->data.binaryheap.elements = CallocT<BinaryHeapNode*>((max_size - 1) / BINARY_HEAP_BLOCKSIZE + 1);
|
||||
q->data.binaryheap.elements[0] = MallocT<BinaryHeapNode>(BINARY_HEAP_BLOCKSIZE);
|
||||
q->data.binaryheap.blocks = 1;
|
||||
#ifdef QUEUE_DEBUG
|
||||
printf("[BinaryHeap] Initial size of elements is %d nodes\n", BINARY_HEAP_BLOCKSIZE);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Because we don't want anyone else to bother with our defines */
|
||||
#undef BIN_HEAP_ARR
|
||||
|
||||
/*
|
||||
* Hash
|
||||
*/
|
||||
|
||||
void init_Hash(Hash *h, Hash_HashProc *hash, uint num_buckets)
|
||||
{
|
||||
/* Allocate space for the Hash, the buckets and the bucket flags */
|
||||
uint i;
|
||||
|
||||
assert(h != NULL);
|
||||
#ifdef HASH_DEBUG
|
||||
debug("Allocated hash: %p", h);
|
||||
#endif
|
||||
h->hash = hash;
|
||||
h->size = 0;
|
||||
h->num_buckets = num_buckets;
|
||||
h->buckets = (HashNode*)MallocT<byte>(num_buckets * (sizeof(*h->buckets) + sizeof(*h->buckets_in_use)));
|
||||
#ifdef HASH_DEBUG
|
||||
debug("Buckets = %p", h->buckets);
|
||||
#endif
|
||||
h->buckets_in_use = (bool*)(h->buckets + num_buckets);
|
||||
for (i = 0; i < num_buckets; i++) h->buckets_in_use[i] = false;
|
||||
}
|
||||
|
||||
|
||||
void delete_Hash(Hash *h, bool free_values)
|
||||
{
|
||||
uint i;
|
||||
|
||||
/* Iterate all buckets */
|
||||
for (i = 0; i < h->num_buckets; i++) {
|
||||
if (h->buckets_in_use[i]) {
|
||||
HashNode *node;
|
||||
|
||||
/* Free the first value */
|
||||
if (free_values) free(h->buckets[i].value);
|
||||
node = h->buckets[i].next;
|
||||
while (node != NULL) {
|
||||
HashNode *prev = node;
|
||||
|
||||
node = node->next;
|
||||
/* Free the value */
|
||||
if (free_values) free(prev->value);
|
||||
/* Free the node */
|
||||
free(prev);
|
||||
}
|
||||
}
|
||||
}
|
||||
free(h->buckets);
|
||||
/* No need to free buckets_in_use, it is always allocated in one
|
||||
* malloc with buckets */
|
||||
#ifdef HASH_DEBUG
|
||||
debug("Freeing Hash: %p", h);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef HASH_STATS
|
||||
static void stat_Hash(const Hash *h)
|
||||
{
|
||||
uint used_buckets = 0;
|
||||
uint max_collision = 0;
|
||||
uint max_usage = 0;
|
||||
uint usage[200];
|
||||
uint i;
|
||||
|
||||
for (i = 0; i < lengthof(usage); i++) usage[i] = 0;
|
||||
for (i = 0; i < h->num_buckets; i++) {
|
||||
uint collision = 0;
|
||||
if (h->buckets_in_use[i]) {
|
||||
const HashNode *node;
|
||||
|
||||
used_buckets++;
|
||||
for (node = &h->buckets[i]; node != NULL; node = node->next) collision++;
|
||||
if (collision > max_collision) max_collision = collision;
|
||||
}
|
||||
if (collision >= lengthof(usage)) collision = lengthof(usage) - 1;
|
||||
usage[collision]++;
|
||||
if (collision > 0 && usage[collision] >= max_usage) {
|
||||
max_usage = usage[collision];
|
||||
}
|
||||
}
|
||||
printf(
|
||||
"---\n"
|
||||
"Hash size: %d\n"
|
||||
"Nodes used: %d\n"
|
||||
"Non empty buckets: %d\n"
|
||||
"Max collision: %d\n",
|
||||
h->num_buckets, h->size, used_buckets, max_collision
|
||||
);
|
||||
printf("{ ");
|
||||
for (i = 0; i <= max_collision; i++) {
|
||||
if (usage[i] > 0) {
|
||||
printf("%d:%d ", i, usage[i]);
|
||||
#if 0
|
||||
if (i > 0) {
|
||||
uint j;
|
||||
|
||||
for (j = 0; j < usage[i] * 160 / 800; j++) putchar('#');
|
||||
}
|
||||
printf("\n");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
printf ("}\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
void clear_Hash(Hash *h, bool free_values)
|
||||
{
|
||||
uint i;
|
||||
|
||||
#ifdef HASH_STATS
|
||||
if (h->size > 2000) stat_Hash(h);
|
||||
#endif
|
||||
|
||||
/* Iterate all buckets */
|
||||
for (i = 0; i < h->num_buckets; i++) {
|
||||
if (h->buckets_in_use[i]) {
|
||||
HashNode *node;
|
||||
|
||||
h->buckets_in_use[i] = false;
|
||||
/* Free the first value */
|
||||
if (free_values) free(h->buckets[i].value);
|
||||
node = h->buckets[i].next;
|
||||
while (node != NULL) {
|
||||
HashNode *prev = node;
|
||||
|
||||
node = node->next;
|
||||
if (free_values) free(prev->value);
|
||||
free(prev);
|
||||
}
|
||||
}
|
||||
}
|
||||
h->size = 0;
|
||||
}
|
||||
|
||||
/** Finds the node that that saves this key pair. If it is not
|
||||
* found, returns NULL. If it is found, *prev is set to the
|
||||
* node before the one found, or if the node found was the first in the bucket
|
||||
* to NULL. If it is not found, *prev is set to the last HashNode in the
|
||||
* bucket, or NULL if it is empty. prev can also be NULL, in which case it is
|
||||
* not used for output.
|
||||
*/
|
||||
static HashNode *Hash_FindNode(const Hash *h, uint key1, uint key2, HashNode** prev_out)
|
||||
{
|
||||
uint hash = h->hash(key1, key2);
|
||||
HashNode *result = NULL;
|
||||
|
||||
#ifdef HASH_DEBUG
|
||||
debug("Looking for %u, %u", key1, key2);
|
||||
#endif
|
||||
/* Check if the bucket is empty */
|
||||
if (!h->buckets_in_use[hash]) {
|
||||
if (prev_out != NULL) *prev_out = NULL;
|
||||
result = NULL;
|
||||
/* Check the first node specially */
|
||||
} else if (h->buckets[hash].key1 == key1 && h->buckets[hash].key2 == key2) {
|
||||
/* Save the value */
|
||||
result = h->buckets + hash;
|
||||
if (prev_out != NULL) *prev_out = NULL;
|
||||
#ifdef HASH_DEBUG
|
||||
debug("Found in first node: %p", result);
|
||||
#endif
|
||||
/* Check all other nodes */
|
||||
} else {
|
||||
HashNode *prev = h->buckets + hash;
|
||||
HashNode *node;
|
||||
|
||||
for (node = prev->next; node != NULL; node = node->next) {
|
||||
if (node->key1 == key1 && node->key2 == key2) {
|
||||
/* Found it */
|
||||
result = node;
|
||||
#ifdef HASH_DEBUG
|
||||
debug("Found in other node: %p", result);
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
prev = node;
|
||||
}
|
||||
if (prev_out != NULL) *prev_out = prev;
|
||||
}
|
||||
#ifdef HASH_DEBUG
|
||||
if (result == NULL) debug("Not found");
|
||||
#endif
|
||||
return result;
|
||||
}
|
||||
|
||||
void *Hash_Delete(Hash *h, uint key1, uint key2)
|
||||
{
|
||||
void *result;
|
||||
HashNode *prev; // Used as output var for below function call
|
||||
HashNode *node = Hash_FindNode(h, key1, key2, &prev);
|
||||
|
||||
if (node == NULL) {
|
||||
/* not found */
|
||||
result = NULL;
|
||||
} else if (prev == NULL) {
|
||||
/* It is in the first node, we can't free that one, so we free
|
||||
* the next one instead (if there is any)*/
|
||||
/* Save the value */
|
||||
result = node->value;
|
||||
if (node->next != NULL) {
|
||||
HashNode *next = node->next;
|
||||
/* Copy the second to the first */
|
||||
*node = *next;
|
||||
/* Free the second */
|
||||
#ifndef NOFREE
|
||||
free(next);
|
||||
#endif
|
||||
} else {
|
||||
/* This was the last in this bucket
|
||||
* Mark it as empty */
|
||||
uint hash = h->hash(key1, key2);
|
||||
h->buckets_in_use[hash] = false;
|
||||
}
|
||||
} else {
|
||||
/* It is in another node
|
||||
* Save the value */
|
||||
result = node->value;
|
||||
/* Link previous and next nodes */
|
||||
prev->next = node->next;
|
||||
/* Free the node */
|
||||
#ifndef NOFREE
|
||||
free(node);
|
||||
#endif
|
||||
}
|
||||
if (result != NULL) h->size--;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
void *Hash_Set(Hash *h, uint key1, uint key2, void *value)
|
||||
{
|
||||
HashNode *prev;
|
||||
HashNode *node = Hash_FindNode(h, key1, key2, &prev);
|
||||
|
||||
if (node != NULL) {
|
||||
/* Found it */
|
||||
void *result = node->value;
|
||||
|
||||
node->value = value;
|
||||
return result;
|
||||
}
|
||||
/* It is not yet present, let's add it */
|
||||
if (prev == NULL) {
|
||||
/* The bucket is still empty */
|
||||
uint hash = h->hash(key1, key2);
|
||||
h->buckets_in_use[hash] = true;
|
||||
node = h->buckets + hash;
|
||||
} else {
|
||||
/* Add it after prev */
|
||||
node = MallocT<HashNode>(1);
|
||||
prev->next = node;
|
||||
}
|
||||
node->next = NULL;
|
||||
node->key1 = key1;
|
||||
node->key2 = key2;
|
||||
node->value = value;
|
||||
h->size++;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *Hash_Get(const Hash *h, uint key1, uint key2)
|
||||
{
|
||||
HashNode *node = Hash_FindNode(h, key1, key2, NULL);
|
||||
|
||||
#ifdef HASH_DEBUG
|
||||
debug("Found node: %p", node);
|
||||
#endif
|
||||
return (node != NULL) ? node->value : NULL;
|
||||
}
|
||||
|
||||
uint Hash_Size(const Hash *h)
|
||||
{
|
||||
return h->size;
|
||||
}
|
||||
167
src/pathfinder/npf/queue.h
Normal file
167
src/pathfinder/npf/queue.h
Normal file
@@ -0,0 +1,167 @@
|
||||
/* $Id$ */
|
||||
|
||||
/*
|
||||
* This file is part of OpenTTD.
|
||||
* OpenTTD 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, version 2.
|
||||
* OpenTTD 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 OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/** @file queue.h Simple Queue/Hash implementations. */
|
||||
|
||||
#ifndef QUEUE_H
|
||||
#define QUEUE_H
|
||||
|
||||
//#define NOFREE
|
||||
//#define QUEUE_DEBUG
|
||||
//#define HASH_DEBUG
|
||||
//#define HASH_STATS
|
||||
|
||||
|
||||
struct Queue;
|
||||
typedef bool Queue_PushProc(Queue *q, void *item, int priority);
|
||||
typedef void *Queue_PopProc(Queue *q);
|
||||
typedef bool Queue_DeleteProc(Queue *q, void *item, int priority);
|
||||
typedef void Queue_ClearProc(Queue *q, bool free_values);
|
||||
typedef void Queue_FreeProc(Queue *q, bool free_values);
|
||||
|
||||
struct InsSortNode {
|
||||
void *item;
|
||||
int priority;
|
||||
InsSortNode *next;
|
||||
};
|
||||
|
||||
struct BinaryHeapNode {
|
||||
void *item;
|
||||
int priority;
|
||||
};
|
||||
|
||||
|
||||
struct Queue{
|
||||
/*
|
||||
* Pushes an element into the queue, at the appropriate place for the queue.
|
||||
* Requires the queue pointer to be of an appropriate type, of course.
|
||||
*/
|
||||
Queue_PushProc *push;
|
||||
/*
|
||||
* Pops the first element from the queue. What exactly is the first element,
|
||||
* is defined by the exact type of queue.
|
||||
*/
|
||||
Queue_PopProc *pop;
|
||||
/*
|
||||
* Deletes the item from the queue. priority should be specified if
|
||||
* known, which speeds up the deleting for some queue's. Should be -1
|
||||
* if not known.
|
||||
*/
|
||||
Queue_DeleteProc *del;
|
||||
|
||||
/* Clears the queue, by removing all values from it. It's state is
|
||||
* effectively reset. If free_items is true, each of the items cleared
|
||||
* in this way are free()'d.
|
||||
*/
|
||||
Queue_ClearProc *clear;
|
||||
/* Frees the queue, by reclaiming all memory allocated by it. After
|
||||
* this it is no longer usable. If free_items is true, any remaining
|
||||
* items are free()'d too.
|
||||
*/
|
||||
Queue_FreeProc *free;
|
||||
|
||||
union {
|
||||
struct {
|
||||
InsSortNode *first;
|
||||
} inssort;
|
||||
struct {
|
||||
uint max_size;
|
||||
uint size;
|
||||
uint blocks; ///< The amount of blocks for which space is reserved in elements
|
||||
BinaryHeapNode **elements;
|
||||
} binaryheap;
|
||||
} data;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Insertion Sorter
|
||||
*/
|
||||
|
||||
/* Initializes a inssort and allocates internal memory. There is no maximum
|
||||
* size */
|
||||
void init_InsSort(Queue *q);
|
||||
|
||||
|
||||
/*
|
||||
* Binary Heap
|
||||
* For information, see:
|
||||
* http://www.policyalmanac.org/games/binaryHeaps.htm
|
||||
*/
|
||||
|
||||
/* The amount of elements that will be malloc'd at a time */
|
||||
#define BINARY_HEAP_BLOCKSIZE_BITS 10
|
||||
|
||||
/** Initializes a binary heap and allocates internal memory for maximum of
|
||||
* max_size elements */
|
||||
void init_BinaryHeap(Queue *q, uint max_size);
|
||||
|
||||
|
||||
/*
|
||||
* Hash
|
||||
*/
|
||||
struct HashNode {
|
||||
uint key1;
|
||||
uint key2;
|
||||
void *value;
|
||||
HashNode *next;
|
||||
};
|
||||
/**
|
||||
* Generates a hash code from the given key pair. You should make sure that
|
||||
* the resulting range is clearly defined.
|
||||
*/
|
||||
typedef uint Hash_HashProc(uint key1, uint key2);
|
||||
struct Hash {
|
||||
/* The hash function used */
|
||||
Hash_HashProc *hash;
|
||||
/* The amount of items in the hash */
|
||||
uint size;
|
||||
/* The number of buckets allocated */
|
||||
uint num_buckets;
|
||||
/* A pointer to an array of num_buckets buckets. */
|
||||
HashNode *buckets;
|
||||
/* A pointer to an array of numbuckets booleans, which will be true if
|
||||
* there are any Nodes in the bucket */
|
||||
bool *buckets_in_use;
|
||||
};
|
||||
|
||||
/* Call these function to manipulate a hash */
|
||||
|
||||
/** Deletes the value with the specified key pair from the hash and returns
|
||||
* that value. Returns NULL when the value was not present. The value returned
|
||||
* is _not_ free()'d! */
|
||||
void *Hash_Delete(Hash *h, uint key1, uint key2);
|
||||
/** Sets the value associated with the given key pair to the given value.
|
||||
* Returns the old value if the value was replaced, NULL when it was not yet present. */
|
||||
void *Hash_Set(Hash *h, uint key1, uint key2, void *value);
|
||||
/** Gets the value associated with the given key pair, or NULL when it is not
|
||||
* present. */
|
||||
void *Hash_Get(const Hash *h, uint key1, uint key2);
|
||||
|
||||
/* Call these function to create/destroy a hash */
|
||||
|
||||
/** Builds a new hash in an existing struct. Make sure that hash() always
|
||||
* returns a hash less than num_buckets! Call delete_hash after use */
|
||||
void init_Hash(Hash *h, Hash_HashProc *hash, uint num_buckets);
|
||||
/**
|
||||
* Deletes the hash and cleans up. Only cleans up memory allocated by new_Hash
|
||||
* & friends. If free is true, it will call free() on all the values that
|
||||
* are left in the hash.
|
||||
*/
|
||||
void delete_Hash(Hash *h, bool free_values);
|
||||
/**
|
||||
* Cleans the hash, but keeps the memory allocated
|
||||
*/
|
||||
void clear_Hash(Hash *h, bool free_values);
|
||||
/**
|
||||
* Gets the current size of the Hash
|
||||
*/
|
||||
uint Hash_Size(const Hash *h);
|
||||
|
||||
#endif /* QUEUE_H */
|
||||
Reference in New Issue
Block a user