(svn r13632) -Codechange: Use 'void *' for user-data of CircularTileSearch().

This commit is contained in:
frosch 2008-06-25 18:46:05 +00:00
parent 644fa40339
commit 4e6d827ea6
5 changed files with 32 additions and 28 deletions

View File

@ -962,10 +962,10 @@ void PlantRandomFarmField(const Industry *i)
/** /**
* Search callback function for ChopLumberMillTrees * Search callback function for ChopLumberMillTrees
* @param tile to test * @param tile to test
* @param data that is passed by the caller. In this case, nothing * @param user_data that is passed by the caller. In this case, nothing
* @return the result of the test * @return the result of the test
*/ */
static bool SearchLumberMillTrees(TileIndex tile, uint32 data) static bool SearchLumberMillTrees(TileIndex tile, void *user_data)
{ {
if (IsTileType(tile, MP_TREES) && GetTreeGrowth(tile) > 2) { ///< 3 and up means all fully grown trees if (IsTileType(tile, MP_TREES) && GetTreeGrowth(tile) > 2) { ///< 3 and up means all fully grown trees
PlayerID old_player = _current_player; PlayerID old_player = _current_player;
@ -994,7 +994,7 @@ static void ChopLumberMillTrees(Industry *i)
if (!IsIndustryCompleted(tile)) return; ///< Can't proceed if not completed if (!IsIndustryCompleted(tile)) return; ///< Can't proceed if not completed
if (CircularTileSearch(&tile, 40, SearchLumberMillTrees, 0)) ///< 40x40 tiles to search if (CircularTileSearch(&tile, 40, SearchLumberMillTrees, NULL)) ///< 40x40 tiles to search
i->produced_cargo_waiting[0] = min(0xffff, i->produced_cargo_waiting[0] + 45); ///< Found a tree, add according value to waiting cargo i->produced_cargo_waiting[0] = min(0xffff, i->produced_cargo_waiting[0] + 45); ///< Found a tree, add according value to waiting cargo
} }

View File

@ -261,12 +261,12 @@ uint DistanceFromEdge(TileIndex tile)
* @param tile to start the search from. Upon completion, it will return the tile matching the search * @param tile to start the search from. Upon completion, it will return the tile matching the search
* @param size: number of tiles per side of the desired search area * @param size: number of tiles per side of the desired search area
* @param proc: callback testing function pointer. * @param proc: callback testing function pointer.
* @param data to be passed to the callback function. Depends on the implementation * @param user_data to be passed to the callback function. Depends on the implementation
* @return result of the search * @return result of the search
* @pre proc != NULL * @pre proc != NULL
* @pre size > 0 * @pre size > 0
*/ */
bool CircularTileSearch(TileIndex *tile, uint size, TestTileOnSearchProc proc, uint32 data) bool CircularTileSearch(TileIndex *tile, uint size, TestTileOnSearchProc proc, void *user_data)
{ {
uint n, x, y; uint n, x, y;
DiagDirection dir; DiagDirection dir;
@ -281,7 +281,7 @@ bool CircularTileSearch(TileIndex *tile, uint size, TestTileOnSearchProc proc, u
/* If the length of the side is uneven, the center has to be checked /* If the length of the side is uneven, the center has to be checked
* separately, as the pattern of uneven sides requires to go around the center */ * separately, as the pattern of uneven sides requires to go around the center */
n = 2; n = 2;
if (proc(TileXY(x, y), data)) { if (proc(TileXY(x, y), user_data)) {
*tile = TileXY(x, y); *tile = TileXY(x, y);
return true; return true;
} }
@ -304,7 +304,7 @@ bool CircularTileSearch(TileIndex *tile, uint size, TestTileOnSearchProc proc, u
uint j; uint j;
for (j = n; j != 0; j--) { for (j = n; j != 0; j--) {
if (x <= MapMaxX() && y <= MapMaxY() && ///< Is the tile within the map? if (x <= MapMaxX() && y <= MapMaxY() && ///< Is the tile within the map?
proc(TileXY(x, y), data)) { ///< Is the callback successful? proc(TileXY(x, y), user_data)) { ///< Is the callback successful?
*tile = TileXY(x, y); *tile = TileXY(x, y);
return true; ///< then stop the search return true; ///< then stop the search
} }

View File

@ -383,15 +383,15 @@ static inline TileIndex TileAddByDiagDir(TileIndex tile, DiagDirection dir)
* A callback function type for searching tiles. * A callback function type for searching tiles.
* *
* @param tile The tile to test * @param tile The tile to test
* @param data additional data for the callback function to use * @param user_data additional data for the callback function to use
* @return A boolean value, depend on the definition of the function. * @return A boolean value, depend on the definition of the function.
*/ */
typedef bool TestTileOnSearchProc(TileIndex tile, uint32 data); typedef bool TestTileOnSearchProc(TileIndex tile, void *user_data);
/** /**
* Searches for some cirumstances of a tile around a given tile with a helper function. * Searches for some cirumstances of a tile around a given tile with a helper function.
*/ */
bool CircularTileSearch(TileIndex *tile, uint size, TestTileOnSearchProc proc, uint32 data); bool CircularTileSearch(TileIndex *tile, uint size, TestTileOnSearchProc proc, void *user_data);
/** /**
* Get a random tile out of a given seed. * Get a random tile out of a given seed.

View File

@ -203,15 +203,16 @@ uint32 GetNearbyTileInformation(byte parameter, TileIndex tile)
/** Callback function to search a house by its HouseID /** Callback function to search a house by its HouseID
* @param tile TileIndex to be examined * @param tile TileIndex to be examined
* @param data house id, in order to get the specs * @param user_data house id, in order to get the specs
* @return true or false, if found or not * @return true or false, if found or not
*/ */
static bool SearchNearbyHouseID(TileIndex tile, uint32 data) static bool SearchNearbyHouseID(TileIndex tile, void *user_data)
{ {
if (IsTileType(tile, MP_HOUSE)) { if (IsTileType(tile, MP_HOUSE)) {
const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile)); // tile been examined const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile)); // tile been examined
if (hs->grffile != NULL) { // must be one from a grf file if (hs->grffile != NULL) { // must be one from a grf file
const HouseSpec *test_hs = GetHouseSpecs((HouseID)GB(data, 0, 16)); HouseID *test_house = (HouseID *)user_data;
const HouseSpec *test_hs = GetHouseSpecs(*test_house);
return hs->local_id == test_hs->local_id && // same local id as the one requested return hs->local_id == test_hs->local_id && // same local id as the one requested
hs->grffile->grfid == test_hs->grffile->grfid; // from the same grf hs->grffile->grfid == test_hs->grffile->grfid; // from the same grf
} }
@ -221,15 +222,16 @@ static bool SearchNearbyHouseID(TileIndex tile, uint32 data)
/** Callback function to search a house by its classID /** Callback function to search a house by its classID
* @param tile TileIndex to be examined * @param tile TileIndex to be examined
* @param data house id, in order to get the specs * @param user_data house id, in order to get the specs
* @return true or false, if found or not * @return true or false, if found or not
*/ */
static bool SearchNearbyHouseClass(TileIndex tile, uint32 data) static bool SearchNearbyHouseClass(TileIndex tile, void *user_data)
{ {
if (IsTileType(tile, MP_HOUSE)) { if (IsTileType(tile, MP_HOUSE)) {
const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile)); // tile been examined const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile)); // tile been examined
if (hs->grffile != NULL) { // must be one from a grf file if (hs->grffile != NULL) { // must be one from a grf file
const HouseSpec *test_hs = GetHouseSpecs((HouseID)GB(data, 0, 16)); HouseID *test_house = (HouseID *)user_data;
const HouseSpec *test_hs = GetHouseSpecs(*test_house);
return hs->class_id == test_hs->class_id && // same classid as the one requested return hs->class_id == test_hs->class_id && // same classid as the one requested
hs->grffile->grfid == test_hs->grffile->grfid; // from the same grf hs->grffile->grfid == test_hs->grffile->grfid; // from the same grf
} }
@ -239,15 +241,16 @@ static bool SearchNearbyHouseClass(TileIndex tile, uint32 data)
/** Callback function to search a house by its grfID /** Callback function to search a house by its grfID
* @param tile TileIndex to be examined * @param tile TileIndex to be examined
* @param data house id, in order to get the specs * @param user_data house id, in order to get the specs
* @return true or false, if found or not * @return true or false, if found or not
*/ */
static bool SearchNearbyHouseGRFID(TileIndex tile, uint32 data) static bool SearchNearbyHouseGRFID(TileIndex tile, void *user_data)
{ {
if (IsTileType(tile, MP_HOUSE)) { if (IsTileType(tile, MP_HOUSE)) {
const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile)); // tile been examined const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile)); // tile been examined
if (hs->grffile != NULL) { // must be one from a grf file if (hs->grffile != NULL) { // must be one from a grf file
const HouseSpec *test_hs = GetHouseSpecs((HouseID)GB(data, 0, 16)); HouseID *test_house = (HouseID *)user_data;
const HouseSpec *test_hs = GetHouseSpecs(*test_house);
return hs->grffile->grfid == test_hs->grffile->grfid; // from the same grf return hs->grffile->grfid == test_hs->grffile->grfid; // from the same grf
} }
} }
@ -260,10 +263,10 @@ static bool SearchNearbyHouseGRFID(TileIndex tile, uint32 data)
* bits 0..6 radius of the search * bits 0..6 radius of the search
* bits 7..8 search type i.e.: 0 = houseID/ 1 = classID/ 2 = grfID * bits 7..8 search type i.e.: 0 = houseID/ 1 = classID/ 2 = grfID
* @param tile TileIndex from which to start the search * @param tile TileIndex from which to start the search
* @param data the HouseID that is associated to the house work who started the callback * @param house the HouseID that is associated to the house, the callback is called for
* @result the Manhattan distance from the center tile, if any, and 0 if failure * @return the Manhattan distance from the center tile, if any, and 0 if failure
*/ */
static uint32 GetDistanceFromNearbyHouse(uint8 parameter, TileIndex tile, uint32 data) static uint32 GetDistanceFromNearbyHouse(uint8 parameter, TileIndex tile, HouseID house)
{ {
static TestTileOnSearchProc * const search_procs[3] = { static TestTileOnSearchProc * const search_procs[3] = {
SearchNearbyHouseID, SearchNearbyHouseID,
@ -277,7 +280,7 @@ static uint32 GetDistanceFromNearbyHouse(uint8 parameter, TileIndex tile, uint32
if (searchradius < 1) return 0; // do not use a too low radius if (searchradius < 1) return 0; // do not use a too low radius
/* Use a pointer for the tile to start the search. Will be required for calculating the distance*/ /* Use a pointer for the tile to start the search. Will be required for calculating the distance*/
if (CircularTileSearch(&found_tile, 2 * searchradius + 1, search_procs[searchtype], data)) { if (CircularTileSearch(&found_tile, 2 * searchradius + 1, search_procs[searchtype], &house)) {
return DistanceManhattan(found_tile, tile); return DistanceManhattan(found_tile, tile);
} }
return 0; return 0;

View File

@ -2182,12 +2182,13 @@ static bool DoBuildStatueOfCompany(TileIndex tile, TownID town_id)
/** /**
* Search callback function for TownActionBuildStatue * Search callback function for TownActionBuildStatue
* @param tile on which to perform the search * @param tile on which to perform the search
* @param town_id The town_id for which we want a statue * @param user_data The town_id for which we want a statue
* @return the result of the test * @return the result of the test
*/ */
static bool SearchTileForStatue(TileIndex tile, uint32 town_id) static bool SearchTileForStatue(TileIndex tile, void *user_data)
{ {
return DoBuildStatueOfCompany(tile, town_id); TownID *town_id = (TownID *)user_data;
return DoBuildStatueOfCompany(tile, *town_id);
} }
/** /**
@ -2199,7 +2200,7 @@ static void TownActionBuildStatue(Town *t)
{ {
TileIndex tile = t->xy; TileIndex tile = t->xy;
if (CircularTileSearch(&tile, 9, SearchTileForStatue, t->index)) { if (CircularTileSearch(&tile, 9, SearchTileForStatue, &t->index)) {
SetBit(t->statues, _current_player); // Once found and built, "inform" the Town SetBit(t->statues, _current_player); // Once found and built, "inform" the Town
} }
} }