aystar.c
author celestar
Fri, 29 Dec 2006 10:13:35 +0000
branchcustombridgeheads
changeset 5592 fd60d4ecc921
parent 4434 4175805666a5
permissions -rw-r--r--
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2008
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2008
diff changeset
     2
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
     3
/*
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
     4
 * This file has the core function for AyStar
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
     5
 *  AyStar is a fast pathfinding routine and is used for things like
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
     6
 *  AI_pathfinding and Train_pathfinding.
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
     7
 *  For more information about AyStar (A* Algorithm), you can look at
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
     8
 *    http://en.wikipedia.org/wiki/A-star_search_algorithm
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
     9
 */
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 110
diff changeset
    10
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    11
/*
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    12
 * Friendly reminder:
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    13
 *  Call (AyStar).free() when you are done with Aystar. It reserves a lot of memory
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    14
 *  And when not free'd, it can cause system-crashes.
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    15
 * Also remember that when you stop an algorithm before it is finished, your
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    16
 * should call clear() yourself!
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    17
 */
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    18
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    19
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1777
diff changeset
    20
#include "openttd.h"
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    21
#include "aystar.h"
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 2916
diff changeset
    22
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 2916
diff changeset
    23
int _aystar_stats_open_size;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 2916
diff changeset
    24
int _aystar_stats_closed_size;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 2916
diff changeset
    25
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    26
// This looks in the Hash if a node exists in ClosedList
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    27
//  If so, it returns the PathNode, else NULL
4171
3fadda3afe70 (svn r5609) CodeChange : Apply coding style
belugas
parents: 4000
diff changeset
    28
static PathNode* AyStarMain_ClosedList_IsInList(AyStar *aystar, const AyStarNode *node)
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 959
diff changeset
    29
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    30
	return (PathNode*)Hash_Get(&aystar->ClosedListHash, node->tile, node->direction);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    31
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    32
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    33
// This adds a node to the ClosedList
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    34
//  It makes a copy of the data
4171
3fadda3afe70 (svn r5609) CodeChange : Apply coding style
belugas
parents: 4000
diff changeset
    35
static void AyStarMain_ClosedList_Add(AyStar *aystar, const PathNode *node)
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 959
diff changeset
    36
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    37
	// Add a node to the ClosedList
4171
3fadda3afe70 (svn r5609) CodeChange : Apply coding style
belugas
parents: 4000
diff changeset
    38
	PathNode *new_node = malloc(sizeof(*new_node));
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    39
	*new_node = *node;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    40
	Hash_Set(&aystar->ClosedListHash, node->node.tile, node->node.direction, new_node);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    41
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    42
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    43
// Checks if a node is in the OpenList
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    44
//   If so, it returns the OpenListNode, else NULL
4171
3fadda3afe70 (svn r5609) CodeChange : Apply coding style
belugas
parents: 4000
diff changeset
    45
static OpenListNode *AyStarMain_OpenList_IsInList(AyStar *aystar, const AyStarNode *node)
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 959
diff changeset
    46
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    47
	return (OpenListNode*)Hash_Get(&aystar->OpenListHash, node->tile, node->direction);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    48
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    49
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    50
// Gets the best node from OpenList
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    51
//  returns the best node, or NULL of none is found
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    52
// Also it deletes the node from the OpenList
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 959
diff changeset
    53
static OpenListNode *AyStarMain_OpenList_Pop(AyStar *aystar)
90220990fd7c (svn r1596) Add some more statics
tron
parents: 959
diff changeset
    54
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    55
	// Return the item the Queue returns.. the best next OpenList item.
4171
3fadda3afe70 (svn r5609) CodeChange : Apply coding style
belugas
parents: 4000
diff changeset
    56
	OpenListNode *res = (OpenListNode*)aystar->OpenListQueue.pop(&aystar->OpenListQueue);
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3949
diff changeset
    57
	if (res != NULL) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    58
		Hash_Delete(&aystar->OpenListHash, res->path.node.tile, res->path.node.direction);
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3949
diff changeset
    59
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 110
diff changeset
    60
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    61
	return res;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    62
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    63
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    64
// Adds a node to the OpenList
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    65
//  It makes a copy of node, and puts the pointer of parent in the struct
4171
3fadda3afe70 (svn r5609) CodeChange : Apply coding style
belugas
parents: 4000
diff changeset
    66
static void AyStarMain_OpenList_Add(AyStar *aystar, PathNode *parent, const AyStarNode *node, int f, int g)
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 959
diff changeset
    67
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    68
	// Add a new Node to the OpenList
4171
3fadda3afe70 (svn r5609) CodeChange : Apply coding style
belugas
parents: 4000
diff changeset
    69
	OpenListNode *new_node = malloc(sizeof(*new_node));
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    70
	new_node->g = g;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    71
	new_node->path.parent = parent;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    72
	new_node->path.node = *node;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    73
	Hash_Set(&aystar->OpenListHash, node->tile, node->direction, new_node);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    74
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    75
	// Add it to the queue
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    76
	aystar->OpenListQueue.push(&aystar->OpenListQueue, new_node, f);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    77
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    78
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    79
/*
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    80
 * Checks one tile and calculate his f-value
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    81
 *  return values:
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4171
diff changeset
    82
 * AYSTAR_DONE : indicates we are done
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    83
 */
4171
3fadda3afe70 (svn r5609) CodeChange : Apply coding style
belugas
parents: 4000
diff changeset
    84
int AyStarMain_CheckTile(AyStar *aystar, AyStarNode *current, OpenListNode *parent)
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3949
diff changeset
    85
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    86
	int new_f, new_g, new_h;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    87
	PathNode *closedlist_parent;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    88
	OpenListNode *check;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    89
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    90
	// Check the new node against the ClosedList
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    91
	if (AyStarMain_ClosedList_IsInList(aystar, current) != NULL) return AYSTAR_DONE;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 110
diff changeset
    92
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    93
	// Calculate the G-value for this node
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    94
	new_g = aystar->CalculateG(aystar, current, parent);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    95
	// If the value was INVALID_NODE, we don't do anything with this node
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    96
	if (new_g == AYSTAR_INVALID_NODE) return AYSTAR_DONE;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 110
diff changeset
    97
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    98
	// There should not be given any other error-code..
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    99
	assert(new_g >= 0);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   100
	// Add the parent g-value to the new g-value
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   101
	new_g += parent->g;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   102
	if (aystar->max_path_cost != 0 && (uint)new_g > aystar->max_path_cost) return AYSTAR_DONE;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 110
diff changeset
   103
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   104
	// Calculate the h-value
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   105
	new_h = aystar->CalculateH(aystar, current, parent);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   106
	// There should not be given any error-code..
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   107
	assert(new_h >= 0);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 110
diff changeset
   108
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   109
	// The f-value if g + h
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   110
	new_f = new_g + new_h;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 110
diff changeset
   111
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   112
	// Get the pointer to the parent in the ClosedList (the currentone is to a copy of the one in the OpenList)
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   113
	closedlist_parent = AyStarMain_ClosedList_IsInList(aystar, &parent->path.node);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 110
diff changeset
   114
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   115
	// Check if this item is already in the OpenList
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3949
diff changeset
   116
	check = AyStarMain_OpenList_IsInList(aystar, current);
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3949
diff changeset
   117
	if (check != NULL) {
959
b031d88c76f3 (svn r1451) Fix some of the signed/unsigned comparison warnings
tron
parents: 926
diff changeset
   118
		uint i;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   119
		// Yes, check if this g value is lower..
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   120
		if (new_g > check->g) return AYSTAR_DONE;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   121
		aystar->OpenListQueue.del(&aystar->OpenListQueue, check, 0);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   122
		// It is lower, so change it to this item
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   123
		check->g = new_g;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   124
		check->path.parent = closedlist_parent;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   125
		/* Copy user data, will probably have changed */
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3949
diff changeset
   126
		for (i = 0; i < lengthof(current->user_data); i++) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   127
			check->path.node.user_data[i] = current->user_data[i];
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3949
diff changeset
   128
		}
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   129
		// Readd him in the OpenListQueue
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   130
		aystar->OpenListQueue.push(&aystar->OpenListQueue, check, new_f);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   131
	} else {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   132
		// A new node, add him to the OpenList
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1617
diff changeset
   133
		AyStarMain_OpenList_Add(aystar, closedlist_parent, current, new_f, new_g);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   134
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 110
diff changeset
   135
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   136
	return AYSTAR_DONE;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   137
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   138
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   139
/*
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   140
 * This function is the core of AyStar. It handles one item and checks
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   141
 *  his neighbour items. If they are valid, they are added to be checked too.
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   142
 *  return values:
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4171
diff changeset
   143
 *   AYSTAR_EMPTY_OPENLIST : indicates all items are tested, and no path
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4171
diff changeset
   144
 *    has been found.
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4171
diff changeset
   145
 *   AYSTAR_LIMIT_REACHED : Indicates that the max_nodes limit has been
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4171
diff changeset
   146
 *    reached.
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4171
diff changeset
   147
 *   AYSTAR_FOUND_END_NODE : indicates we found the end. Path_found now is true, and in path is the path found.
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4171
diff changeset
   148
 *   AYSTAR_STILL_BUSY : indicates we have done this tile, did not found the path yet, and have items left to try.
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   149
 */
4171
3fadda3afe70 (svn r5609) CodeChange : Apply coding style
belugas
parents: 4000
diff changeset
   150
int AyStarMain_Loop(AyStar *aystar)
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3949
diff changeset
   151
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   152
	int i, r;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 110
diff changeset
   153
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   154
	// Get the best node from OpenList
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   155
	OpenListNode *current = AyStarMain_OpenList_Pop(aystar);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   156
	// If empty, drop an error
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3949
diff changeset
   157
	if (current == NULL) return AYSTAR_EMPTY_OPENLIST;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 110
diff changeset
   158
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   159
	// Check for end node and if found, return that code
1617
55878ca5ada9 (svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents: 1459
diff changeset
   160
	if (aystar->EndNodeCheck(aystar, current) == AYSTAR_FOUND_END_NODE) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   161
		if (aystar->FoundEndNode != NULL)
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   162
			aystar->FoundEndNode(aystar, current);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   163
		free(current);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   164
		return AYSTAR_FOUND_END_NODE;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   165
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 110
diff changeset
   166
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   167
	// Add the node to the ClosedList
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   168
	AyStarMain_ClosedList_Add(aystar, &current->path);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   169
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   170
	// Load the neighbours
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   171
	aystar->GetNeighbours(aystar, current);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 110
diff changeset
   172
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   173
	// Go through all neighbours
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3949
diff changeset
   174
	for (i = 0; i < aystar->num_neighbours; i++) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   175
		// Check and add them to the OpenList if needed
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   176
		r = aystar->checktile(aystar, &aystar->neighbours[i], current);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   177
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 110
diff changeset
   178
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   179
	// Free the node
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   180
	free(current);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 110
diff changeset
   181
3949
3f495d15621d (svn r5095) -CodeChange: {} added around returns
KUDr
parents: 3944
diff changeset
   182
	if (aystar->max_search_nodes != 0 && Hash_Size(&aystar->ClosedListHash) >= aystar->max_search_nodes) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   183
		/* We've expanded enough nodes */
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   184
		return AYSTAR_LIMIT_REACHED;
3949
3f495d15621d (svn r5095) -CodeChange: {} added around returns
KUDr
parents: 3944
diff changeset
   185
	} else {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   186
		// Return that we are still busy
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   187
		return AYSTAR_STILL_BUSY;
3949
3f495d15621d (svn r5095) -CodeChange: {} added around returns
KUDr
parents: 3944
diff changeset
   188
	}
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   189
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   190
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   191
/*
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   192
 * This function frees the memory it allocated
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   193
 */
4171
3fadda3afe70 (svn r5609) CodeChange : Apply coding style
belugas
parents: 4000
diff changeset
   194
void AyStarMain_Free(AyStar *aystar)
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3949
diff changeset
   195
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   196
	aystar->OpenListQueue.free(&aystar->OpenListQueue, false);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   197
	/* 2nd argument above is false, below is true, to free the values only
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   198
	 * once */
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   199
	delete_Hash(&aystar->OpenListHash, true);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   200
	delete_Hash(&aystar->ClosedListHash, true);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   201
#ifdef AYSTAR_DEBUG
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   202
	printf("[AyStar] Memory free'd\n");
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   203
#endif
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   204
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   205
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   206
/*
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   207
 * This function make the memory go back to zero
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   208
 *  This function should be called when you are using the same instance again.
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   209
 */
4171
3fadda3afe70 (svn r5609) CodeChange : Apply coding style
belugas
parents: 4000
diff changeset
   210
void AyStarMain_Clear(AyStar *aystar)
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3949
diff changeset
   211
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   212
	// Clean the Queue, but not the elements within. That will be done by
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   213
	// the hash.
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   214
	aystar->OpenListQueue.clear(&aystar->OpenListQueue, false);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   215
	// Clean the hashes
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   216
	clear_Hash(&aystar->OpenListHash, true);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   217
	clear_Hash(&aystar->ClosedListHash, true);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   218
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   219
#ifdef AYSTAR_DEBUG
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   220
	printf("[AyStar] Cleared AyStar\n");
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   221
#endif
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   222
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   223
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   224
/*
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   225
 * This is the function you call to run AyStar.
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   226
 *  return values:
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4171
diff changeset
   227
 *   AYSTAR_FOUND_END_NODE : indicates we found an end node.
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4171
diff changeset
   228
 *   AYSTAR_NO_PATH : indicates that there was no path found.
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4171
diff changeset
   229
 *   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.
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   230
 * When the algorithm is done (when the return value is not AYSTAR_STILL_BUSY)
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   231
 * aystar->clear() is called. Note that when you stop the algorithm halfway,
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   232
 * you should still call clear() yourself!
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   233
 */
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   234
int AyStarMain_Main(AyStar *aystar) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   235
	int r, i = 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   236
	// Loop through the OpenList
826
0e2b569b737b (svn r1297) Language fixes in the source.. (ln-)
miham
parents: 193
diff changeset
   237
	//  Quit if result is no AYSTAR_STILL_BUSY or is more than loops_per_tick
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   238
	while ((r = aystar->loop(aystar)) == AYSTAR_STILL_BUSY && (aystar->loops_per_tick == 0 || ++i < aystar->loops_per_tick)) { }
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   239
#ifdef AYSTAR_DEBUG
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3949
diff changeset
   240
	switch (r) {
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3949
diff changeset
   241
		case AYSTAR_FOUND_END_NODE: printf("[AyStar] Found path!\n"); break;
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3949
diff changeset
   242
		case AYSTAR_EMPTY_OPENLIST: printf("[AyStar] OpenList run dry, no path found\n"); break;
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3949
diff changeset
   243
		case AYSTAR_LIMIT_REACHED:  printf("[AyStar] Exceeded search_nodes, no path found\n"); break;
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3949
diff changeset
   244
		default: break;
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3949
diff changeset
   245
	}
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   246
#endif
3944
edd36e1c6487 (svn r5090) -Fix: [NPF] broken by me - r4366 (thanks Tron)
KUDr
parents: 3900
diff changeset
   247
	if (r != AYSTAR_STILL_BUSY) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   248
		/* We're done, clean up */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 2916
diff changeset
   249
		_aystar_stats_open_size = aystar->OpenListHash.size;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 2916
diff changeset
   250
		_aystar_stats_closed_size = aystar->ClosedListHash.size;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   251
		aystar->clear(aystar);
3944
edd36e1c6487 (svn r5090) -Fix: [NPF] broken by me - r4366 (thanks Tron)
KUDr
parents: 3900
diff changeset
   252
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 110
diff changeset
   253
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3949
diff changeset
   254
	switch (r) {
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3949
diff changeset
   255
		case AYSTAR_FOUND_END_NODE: return AYSTAR_FOUND_END_NODE;
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3949
diff changeset
   256
		case AYSTAR_EMPTY_OPENLIST:
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3949
diff changeset
   257
		case AYSTAR_LIMIT_REACHED:  return AYSTAR_NO_PATH;
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3949
diff changeset
   258
		default:                    return AYSTAR_STILL_BUSY;
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3949
diff changeset
   259
	}
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   260
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   261
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   262
/*
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   263
 * Adds a node from where to start an algorithm. Multiple nodes can be added
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   264
 * if wanted. You should make sure that clear() is called before adding nodes
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   265
 * if the AyStar has been used before (though the normal main loop calls
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 110
diff changeset
   266
 * clear() automatically when the algorithm finishes
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1617
diff changeset
   267
 * g is the cost for starting with this node.
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   268
 */
4171
3fadda3afe70 (svn r5609) CodeChange : Apply coding style
belugas
parents: 4000
diff changeset
   269
void AyStarMain_AddStartNode(AyStar *aystar, AyStarNode *start_node, uint g)
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3949
diff changeset
   270
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   271
#ifdef AYSTAR_DEBUG
926
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 826
diff changeset
   272
	printf("[AyStar] Starting A* Algorithm from node (%d, %d, %d)\n",
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 826
diff changeset
   273
		TileX(start_node->tile), TileY(start_node->tile), start_node->direction);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   274
#endif
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1617
diff changeset
   275
	AyStarMain_OpenList_Add(aystar, NULL, start_node, 0, g);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   276
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   277
4171
3fadda3afe70 (svn r5609) CodeChange : Apply coding style
belugas
parents: 4000
diff changeset
   278
void init_AyStar(AyStar *aystar, Hash_HashProc hash, uint num_buckets)
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3949
diff changeset
   279
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   280
	// Allocated the Hash for the OpenList and ClosedList
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   281
	init_Hash(&aystar->OpenListHash, hash, num_buckets);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   282
	init_Hash(&aystar->ClosedListHash, hash, num_buckets);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   283
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   284
	// Set up our sorting queue
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   285
	//  BinaryHeap allocates a block of 1024 nodes
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   286
	//  When thatone gets full it reserves an otherone, till this number
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   287
	//  That is why it can stay this high
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   288
	init_BinaryHeap(&aystar->OpenListQueue, 102400);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   289
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4171
diff changeset
   290
	aystar->addstart  = AyStarMain_AddStartNode;
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4171
diff changeset
   291
	aystar->main      = AyStarMain_Main;
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4171
diff changeset
   292
	aystar->loop      = AyStarMain_Loop;
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4171
diff changeset
   293
	aystar->free      = AyStarMain_Free;
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4171
diff changeset
   294
	aystar->clear     = AyStarMain_Clear;
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4171
diff changeset
   295
	aystar->checktile = AyStarMain_CheckTile;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   296
}