summaryrefslogtreecommitdiff
path: root/src/pathfinder/npf
diff options
context:
space:
mode:
authorHenry Wilson <m3henry@googlemail.com>2019-04-10 22:07:06 +0100
committerMichael Lutz <michi@icosahedron.de>2019-04-10 23:22:20 +0200
commit7c8e7c6b6e16d4a26259a676db32d8776b99817e (patch)
tree99f134b7e66367cf11e10bc5061896eab4a3264f /src/pathfinder/npf
parent3b4f224c0bc50e7248050d4bcbb6d83fd510c1cc (diff)
downloadopenttd-7c8e7c6b6e16d4a26259a676db32d8776b99817e.tar.xz
Codechange: Use null pointer literal instead of the NULL macro
Diffstat (limited to 'src/pathfinder/npf')
-rw-r--r--src/pathfinder/npf/aystar.cpp18
-rw-r--r--src/pathfinder/npf/npf.cpp32
-rw-r--r--src/pathfinder/npf/queue.cpp62
3 files changed, 56 insertions, 56 deletions
diff --git a/src/pathfinder/npf/aystar.cpp b/src/pathfinder/npf/aystar.cpp
index beac06291..addfc4930 100644
--- a/src/pathfinder/npf/aystar.cpp
+++ b/src/pathfinder/npf/aystar.cpp
@@ -32,7 +32,7 @@
/**
* This looks in the hash whether a node exists in the closed list.
* @param node Node to search.
- * @return The #PathNode if it is available, else \c NULL
+ * @return The #PathNode if it is available, else \c nullptr
*/
PathNode *AyStar::ClosedListIsInList(const AyStarNode *node)
{
@@ -55,7 +55,7 @@ void AyStar::ClosedListAdd(const PathNode *node)
/**
* Check whether a node is in the open list.
* @param node Node to search.
- * @return If the node is available, it is returned, else \c NULL is returned.
+ * @return If the node is available, it is returned, else \c nullptr is returned.
*/
OpenListNode *AyStar::OpenListIsInList(const AyStarNode *node)
{
@@ -65,13 +65,13 @@ OpenListNode *AyStar::OpenListIsInList(const AyStarNode *node)
/**
* Gets the best node from the open list.
* It deletes the returned node from the open list.
- * @returns the best node available, or \c NULL of none is found.
+ * @returns the best node available, or \c nullptr of none is found.
*/
OpenListNode *AyStar::OpenListPop()
{
/* Return the item the Queue returns.. the best next OpenList item. */
OpenListNode *res = (OpenListNode*)this->openlist_queue.Pop();
- if (res != NULL) {
+ if (res != nullptr) {
this->openlist_hash.DeleteValue(res->path.node.tile, res->path.node.direction);
}
@@ -105,7 +105,7 @@ void AyStar::CheckTile(AyStarNode *current, OpenListNode *parent)
OpenListNode *check;
/* Check the new node against the ClosedList */
- if (this->ClosedListIsInList(current) != NULL) return;
+ if (this->ClosedListIsInList(current) != nullptr) return;
/* Calculate the G-value for this node */
new_g = this->CalculateG(this, current, parent);
@@ -131,7 +131,7 @@ void AyStar::CheckTile(AyStarNode *current, OpenListNode *parent)
/* Check if this item is already in the OpenList */
check = this->OpenListIsInList(current);
- if (check != NULL) {
+ if (check != nullptr) {
uint i;
/* Yes, check if this g value is lower.. */
if (new_g > check->g) return;
@@ -167,11 +167,11 @@ int AyStar::Loop()
/* Get the best node from OpenList */
OpenListNode *current = this->OpenListPop();
/* If empty, drop an error */
- if (current == NULL) return AYSTAR_EMPTY_OPENLIST;
+ if (current == nullptr) return AYSTAR_EMPTY_OPENLIST;
/* Check for end node and if found, return that code */
if (this->EndNodeCheck(this, current) == AYSTAR_FOUND_END_NODE && !CheckIgnoreFirstTile(&current->path)) {
- if (this->FoundEndNode != NULL) {
+ if (this->FoundEndNode != nullptr) {
this->FoundEndNode(this, current);
}
free(current);
@@ -285,7 +285,7 @@ void AyStar::AddStartNode(AyStarNode *start_node, uint g)
printf("[AyStar] Starting A* Algorithm from node (%d, %d, %d)\n",
TileX(start_node->tile), TileY(start_node->tile), start_node->direction);
#endif
- this->OpenListAdd(NULL, start_node, 0, g);
+ this->OpenListAdd(nullptr, start_node, 0, g);
}
/**
diff --git a/src/pathfinder/npf/npf.cpp b/src/pathfinder/npf/npf.cpp
index 32c6e982d..2863e7881 100644
--- a/src/pathfinder/npf/npf.cpp
+++ b/src/pathfinder/npf/npf.cpp
@@ -103,7 +103,7 @@ static inline void NPFSetFlag(AyStarNode *node, NPFNodeFlag flag, bool value)
bool CheckIgnoreFirstTile(const PathNode *node)
{
- return (node->parent == NULL && HasBit(node->node.user_data[NPF_NODE_FLAGS], NPF_FLAG_IGNORE_START_TILE));
+ return (node->parent == nullptr && HasBit(node->node.user_data[NPF_NODE_FLAGS], NPF_FLAG_IGNORE_START_TILE));
}
/**
@@ -192,7 +192,7 @@ static int32 NPFCalcStationOrTileHeuristic(AyStar *as, AyStarNode *current, Open
* choice */
static void NPFFillTrackdirChoice(AyStarNode *current, OpenListNode *parent)
{
- if (parent->path.parent == NULL) {
+ if (parent->path.parent == nullptr) {
Trackdir trackdir = current->direction;
/* This is a first order decision, so we'd better save the
* direction we chose */
@@ -584,7 +584,7 @@ static const PathNode *FindSafePosition(PathNode *path, const Train *v)
/* If there is no signal, reserve the whole path. */
PathNode *sig = path;
- for (; path->parent != NULL; path = path->parent) {
+ for (; path->parent != nullptr; path = path->parent) {
if (IsSafeWaitingPosition(v, path->node.tile, path->node.direction, true, _settings_game.pf.forbid_90_deg)) {
sig = path;
}
@@ -625,7 +625,7 @@ static void NPFSaveTargetData(AyStar *as, OpenListNode *current)
ftd->node = current->path.node;
ftd->res_okay = false;
- if (as->user_target != NULL && ((NPFFindStationOrTileData*)as->user_target)->reserve_path && user->type == TRANSPORT_RAIL) {
+ if (as->user_target != nullptr && ((NPFFindStationOrTileData*)as->user_target)->reserve_path && user->type == TRANSPORT_RAIL) {
/* Path reservation is requested. */
const Train *v = Train::From(((NPFFindStationOrTileData *)as->user_target)->v);
@@ -647,7 +647,7 @@ static void NPFSaveTargetData(AyStar *as, OpenListNode *current)
if (!IsWaitingPositionFree(v, target->node.tile, target->node.direction, _settings_game.pf.forbid_90_deg)) return;
}
- for (const PathNode *cur = target; cur->parent != NULL; cur = cur->parent) {
+ for (const PathNode *cur = target; cur->parent != nullptr; cur = cur->parent) {
if (!TryReserveRailTrack(cur->node.tile, TrackdirToTrack(cur->node.direction))) {
/* Reservation failed, undo. */
ClearPathReservation(target, cur);
@@ -957,7 +957,7 @@ static void NPFFollowTrack(AyStar *aystar, OpenListNode *current)
/*
* Plan a route to the specified target (which is checked by target_proc),
- * from start1 and if not NULL, from start2 as well. The type of transport we
+ * from start1 and if not nullptr, from start2 as well. The type of transport we
* are checking is in type. reverse_penalty is applied to all routes that
* originate from the second start node.
* When we are looking for one specific target (optionally multiple tiles), we
@@ -989,7 +989,7 @@ static NPFFoundTargetData NPFRouteInternal(AyStarNode *start1, bool ignore_start
NPFSetFlag(start1, NPF_FLAG_IGNORE_START_TILE, ignore_start_tile1);
NPFSetFlag(start1, NPF_FLAG_IGNORE_RESERVED, ignore_reserved);
_npf_aystar.AddStartNode(start1, 0);
- if (start2 != NULL) {
+ if (start2 != nullptr) {
start2->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
start2->user_data[NPF_NODE_FLAGS] = 0;
NPFSetFlag(start2, NPF_FLAG_IGNORE_START_TILE, ignore_start_tile2);
@@ -1017,10 +1017,10 @@ static NPFFoundTargetData NPFRouteInternal(AyStarNode *start1, bool ignore_start
assert(r != AYSTAR_STILL_BUSY);
if (result.best_bird_dist != 0) {
- if (target != NULL) {
+ if (target != nullptr) {
DEBUG(npf, 1, "Could not find route to tile 0x%X from 0x%X.", target->dest_coords, start1->tile);
} else {
- /* Assumption: target == NULL, so we are looking for a depot */
+ /* Assumption: target == nullptr, so we are looking for a depot */
DEBUG(npf, 1, "Could not find route to a depot from tile 0x%X.", start1->tile);
}
@@ -1041,7 +1041,7 @@ static NPFFoundTargetData NPFRouteToStationOrTileTwoWay(TileIndex tile1, Trackdi
start1.direction = trackdir1;
start2.direction = trackdir2;
- return NPFRouteInternal(&start1, ignore_start_tile1, (IsValidTile(tile2) ? &start2 : NULL), ignore_start_tile2, target, NPFFindStationOrTile, NPFCalcStationOrTileHeuristic, user, 0);
+ return NPFRouteInternal(&start1, ignore_start_tile1, (IsValidTile(tile2) ? &start2 : nullptr), ignore_start_tile2, target, NPFFindStationOrTile, NPFCalcStationOrTileHeuristic, user, 0);
}
/* Will search from the given tile and direction, for a route to the given
@@ -1069,9 +1069,9 @@ static NPFFoundTargetData NPFRouteToDepotBreadthFirstTwoWay(TileIndex tile1, Tra
start1.direction = trackdir1;
start2.direction = trackdir2;
- /* perform a breadth first search. Target is NULL,
+ /* perform a breadth first search. Target is nullptr,
* since we are just looking for any depot...*/
- return NPFRouteInternal(&start1, ignore_start_tile1, (IsValidTile(tile2) ? &start2 : NULL), ignore_start_tile2, target, NPFFindDepot, NPFCalcZero, user, reverse_penalty, false, max_penalty);
+ return NPFRouteInternal(&start1, ignore_start_tile1, (IsValidTile(tile2) ? &start2 : nullptr), ignore_start_tile2, target, NPFFindDepot, NPFCalcZero, user, reverse_penalty, false, max_penalty);
}
void InitializeNPF()
@@ -1121,7 +1121,7 @@ FindDepotData NPFRoadVehicleFindNearestDepot(const RoadVehicle *v, int max_penal
Trackdir trackdir = v->GetVehicleTrackdir();
AyStarUserData user = { v->owner, TRANSPORT_ROAD, INVALID_RAILTYPES, v->compatible_roadtypes };
- NPFFoundTargetData ftd = NPFRouteToDepotBreadthFirstTwoWay(v->tile, trackdir, false, INVALID_TILE, INVALID_TRACKDIR, false, NULL, &user, 0, max_penalty);
+ NPFFoundTargetData ftd = NPFRouteToDepotBreadthFirstTwoWay(v->tile, trackdir, false, INVALID_TILE, INVALID_TRACKDIR, false, nullptr, &user, 0, max_penalty);
if (ftd.best_bird_dist != 0) return FindDepotData();
@@ -1233,10 +1233,10 @@ bool NPFTrainFindNearestSafeTile(const Train *v, TileIndex tile, Trackdir trackd
RailTypes railtypes = v->compatible_railtypes;
if (override_railtype) railtypes |= GetRailTypeInfo(v->railtype)->compatible_railtypes;
- /* perform a breadth first search. Target is NULL,
+ /* perform a breadth first search. Target is nullptr,
* since we are just looking for any safe tile...*/
AyStarUserData user = { v->owner, TRANSPORT_RAIL, railtypes, ROADTYPES_NONE };
- return NPFRouteInternal(&start1, true, NULL, false, &fstd, NPFFindSafeTile, NPFCalcZero, &user, 0, true).res_okay;
+ return NPFRouteInternal(&start1, true, nullptr, false, &fstd, NPFFindSafeTile, NPFCalcZero, &user, 0, true).res_okay;
}
bool NPFTrainCheckReverse(const Train *v)
@@ -1269,7 +1269,7 @@ Track NPFTrainChooseTrack(const Train *v, bool &path_found, bool reserve_track,
AyStarUserData user = { v->owner, TRANSPORT_RAIL, v->compatible_railtypes, ROADTYPES_NONE };
NPFFoundTargetData ftd = NPFRouteToStationOrTile(origin.tile, origin.trackdir, true, &fstd, &user);
- if (target != NULL) {
+ if (target != nullptr) {
target->tile = ftd.node.tile;
target->trackdir = (Trackdir)ftd.node.direction;
target->okay = ftd.res_okay;
diff --git a/src/pathfinder/npf/queue.cpp b/src/pathfinder/npf/queue.cpp
index 2afb41391..02a195c5d 100644
--- a/src/pathfinder/npf/queue.cpp
+++ b/src/pathfinder/npf/queue.cpp
@@ -37,7 +37,7 @@ void BinaryHeap::Clear(bool free_values)
uint j;
for (i = 0; i < this->blocks; i++) {
- if (this->elements[i] == NULL) {
+ if (this->elements[i] == nullptr) {
/* No more allocated blocks */
break;
}
@@ -55,7 +55,7 @@ void BinaryHeap::Clear(bool free_values)
if (i != 0) {
/* Leave the first block of memory alone */
free(this->elements[i]);
- this->elements[i] = NULL;
+ this->elements[i] = nullptr;
}
}
this->size = 0;
@@ -73,7 +73,7 @@ void BinaryHeap::Free(bool free_values)
this->Clear(free_values);
for (i = 0; i < this->blocks; i++) {
- if (this->elements[i] == NULL) break;
+ if (this->elements[i] == nullptr) break;
free(this->elements[i]);
}
free(this->elements);
@@ -88,7 +88,7 @@ bool BinaryHeap::Push(void *item, int priority)
if (this->size == this->max_size) return false;
assert(this->size < this->max_size);
- if (this->elements[this->size >> BINARY_HEAP_BLOCKSIZE_BITS] == NULL) {
+ if (this->elements[this->size >> BINARY_HEAP_BLOCKSIZE_BITS] == nullptr) {
/* The currently allocated blocks are full, allocate a new one */
assert((this->size & BINARY_HEAP_BLOCKSIZE_MASK) == 0);
this->elements[this->size >> BINARY_HEAP_BLOCKSIZE_BITS] = MallocT<BinaryHeapNode>(BINARY_HEAP_BLOCKSIZE);
@@ -195,7 +195,7 @@ void *BinaryHeap::Pop()
{
void *result;
- if (this->size == 0) return NULL;
+ if (this->size == 0) return nullptr;
/* The best item is always on top, so give that as result */
result = this->GetElement(1).item;
@@ -264,7 +264,7 @@ void Hash::Delete(bool free_values)
/* Free the first value */
if (free_values) free(this->buckets[i].value);
node = this->buckets[i].next;
- while (node != NULL) {
+ while (node != nullptr) {
HashNode *prev = node;
node = node->next;
@@ -296,7 +296,7 @@ void Hash::PrintStatistics() const
const HashNode *node;
used_buckets++;
- for (node = &this->buckets[i]; node != NULL; node = node->next) collision++;
+ for (node = &this->buckets[i]; node != nullptr; node = node->next) collision++;
if (collision > max_collision) max_collision = collision;
}
if (collision >= lengthof(usage)) collision = lengthof(usage) - 1;
@@ -351,7 +351,7 @@ void Hash::Clear(bool free_values)
/* Free the first value */
if (free_values) free(this->buckets[i].value);
node = this->buckets[i].next;
- while (node != NULL) {
+ while (node != nullptr) {
HashNode *prev = node;
node = node->next;
@@ -365,32 +365,32 @@ void Hash::Clear(bool free_values)
/**
* 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
+ * found, returns nullptr. 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
+ * to nullptr. If it is not found, *prev is set to the last HashNode in the
+ * bucket, or nullptr if it is empty. prev can also be nullptr, in which case it is
* not used for output.
*/
HashNode *Hash::FindNode(uint key1, uint key2, HashNode** prev_out) const
{
uint hash = this->hash(key1, key2);
- HashNode *result = NULL;
+ HashNode *result = nullptr;
/* Check if the bucket is empty */
if (!this->buckets_in_use[hash]) {
- if (prev_out != NULL) *prev_out = NULL;
- result = NULL;
+ if (prev_out != nullptr) *prev_out = nullptr;
+ result = nullptr;
/* Check the first node specially */
} else if (this->buckets[hash].key1 == key1 && this->buckets[hash].key2 == key2) {
/* Save the value */
result = this->buckets + hash;
- if (prev_out != NULL) *prev_out = NULL;
+ if (prev_out != nullptr) *prev_out = nullptr;
/* Check all other nodes */
} else {
HashNode *prev = this->buckets + hash;
HashNode *node;
- for (node = prev->next; node != NULL; node = node->next) {
+ for (node = prev->next; node != nullptr; node = node->next) {
if (node->key1 == key1 && node->key2 == key2) {
/* Found it */
result = node;
@@ -398,14 +398,14 @@ HashNode *Hash::FindNode(uint key1, uint key2, HashNode** prev_out) const
}
prev = node;
}
- if (prev_out != NULL) *prev_out = prev;
+ if (prev_out != nullptr) *prev_out = prev;
}
return result;
}
/**
* 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
+ * that value. Returns nullptr when the value was not present. The value returned
* is _not_ free()'d!
*/
void *Hash::DeleteValue(uint key1, uint key2)
@@ -414,15 +414,15 @@ void *Hash::DeleteValue(uint key1, uint key2)
HashNode *prev; // Used as output var for below function call
HashNode *node = this->FindNode(key1, key2, &prev);
- if (node == NULL) {
+ if (node == nullptr) {
/* not found */
- result = NULL;
- } else if (prev == NULL) {
+ result = nullptr;
+ } else if (prev == nullptr) {
/* 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) {
+ if (node->next != nullptr) {
HashNode *next = node->next;
/* Copy the second to the first */
*node = *next;
@@ -443,20 +443,20 @@ void *Hash::DeleteValue(uint key1, uint key2)
/* Free the node */
free(node);
}
- if (result != NULL) this->size--;
+ if (result != nullptr) this->size--;
return result;
}
/**
* 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.
+ * Returns the old value if the value was replaced, nullptr when it was not yet present.
*/
void *Hash::Set(uint key1, uint key2, void *value)
{
HashNode *prev;
HashNode *node = this->FindNode(key1, key2, &prev);
- if (node != NULL) {
+ if (node != nullptr) {
/* Found it */
void *result = node->value;
@@ -464,7 +464,7 @@ void *Hash::Set(uint key1, uint key2, void *value)
return result;
}
/* It is not yet present, let's add it */
- if (prev == NULL) {
+ if (prev == nullptr) {
/* The bucket is still empty */
uint hash = this->hash(key1, key2);
this->buckets_in_use[hash] = true;
@@ -474,21 +474,21 @@ void *Hash::Set(uint key1, uint key2, void *value)
node = MallocT<HashNode>(1);
prev->next = node;
}
- node->next = NULL;
+ node->next = nullptr;
node->key1 = key1;
node->key2 = key2;
node->value = value;
this->size++;
- return NULL;
+ return nullptr;
}
/**
- * Gets the value associated with the given key pair, or NULL when it is not
+ * Gets the value associated with the given key pair, or nullptr when it is not
* present.
*/
void *Hash::Get(uint key1, uint key2) const
{
- HashNode *node = this->FindNode(key1, key2, NULL);
+ HashNode *node = this->FindNode(key1, key2, nullptr);
- return (node != NULL) ? node->value : NULL;
+ return (node != nullptr) ? node->value : nullptr;
}