src/newgrf_house.cpp
branchnoai
changeset 11126 72d4c9314c72
parent 11044 097ea3e7ec56
equal deleted inserted replaced
11111:1b984dab8cec 11126:72d4c9314c72
   199 {
   199 {
   200 	tile = GetNearbyTile(parameter, tile);
   200 	tile = GetNearbyTile(parameter, tile);
   201 	return GetNearbyTileInformation(tile);
   201 	return GetNearbyTileInformation(tile);
   202 }
   202 }
   203 
   203 
       
   204 /** Structure with user-data for SearchNearbyHouseXXX - functions */
       
   205 typedef struct {
       
   206 	const HouseSpec *hs;  ///< Specs of the house, that startet the search
       
   207 	TileIndex north_tile; ///< Northern tile of the house.
       
   208 } SearchNearbyHouseData;
       
   209 
       
   210 /** Callback function to search a house by its HouseID
       
   211  * @param tile TileIndex to be examined
       
   212  * @param user_data SearchNearbyHouseData
       
   213  * @return true or false, if found or not
       
   214  */
       
   215 static bool SearchNearbyHouseID(TileIndex tile, void *user_data)
       
   216 {
       
   217 	if (IsTileType(tile, MP_HOUSE)) {
       
   218 		HouseID house = GetHouseType(tile); // tile been examined
       
   219 		const HouseSpec *hs = GetHouseSpecs(house);
       
   220 		if (hs->grffile != NULL) { // must be one from a grf file
       
   221 			SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data;
       
   222 
       
   223 			TileIndex north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
       
   224 			if (north_tile == nbhd->north_tile) return false; // Always ignore origin house
       
   225 
       
   226 			return hs->local_id == nbhd->hs->local_id &&  // same local id as the one requested
       
   227 				hs->grffile->grfid == nbhd->hs->grffile->grfid;  // from the same grf
       
   228 		}
       
   229 	}
       
   230 	return false;
       
   231 }
       
   232 
       
   233 /** Callback function to search a house by its classID
       
   234  * @param tile TileIndex to be examined
       
   235  * @param user_data SearchNearbyHouseData
       
   236  * @return true or false, if found or not
       
   237  */
       
   238 static bool SearchNearbyHouseClass(TileIndex tile, void *user_data)
       
   239 {
       
   240 	if (IsTileType(tile, MP_HOUSE)) {
       
   241 		HouseID house = GetHouseType(tile); // tile been examined
       
   242 		const HouseSpec *hs = GetHouseSpecs(house);
       
   243 		if (hs->grffile != NULL) { // must be one from a grf file
       
   244 			SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data;
       
   245 
       
   246 			TileIndex north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
       
   247 			if (north_tile == nbhd->north_tile) return false; // Always ignore origin house
       
   248 
       
   249 			return hs->class_id == nbhd->hs->class_id &&  // same classid as the one requested
       
   250 				hs->grffile->grfid == nbhd->hs->grffile->grfid;  // from the same grf
       
   251 		}
       
   252 	}
       
   253 	return false;
       
   254 }
       
   255 
       
   256 /** Callback function to search a house by its grfID
       
   257  * @param tile TileIndex to be examined
       
   258  * @param user_data SearchNearbyHouseData
       
   259  * @return true or false, if found or not
       
   260  */
       
   261 static bool SearchNearbyHouseGRFID(TileIndex tile, void *user_data)
       
   262 {
       
   263 	if (IsTileType(tile, MP_HOUSE)) {
       
   264 		HouseID house = GetHouseType(tile); // tile been examined
       
   265 		const HouseSpec *hs = GetHouseSpecs(house);
       
   266 		if (hs->grffile != NULL) { // must be one from a grf file
       
   267 			SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data;
       
   268 
       
   269 			TileIndex north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
       
   270 			if (north_tile == nbhd->north_tile) return false; // Always ignore origin house
       
   271 
       
   272 			return hs->grffile->grfid == nbhd->hs->grffile->grfid;  // from the same grf
       
   273 		}
       
   274 	}
       
   275 	return false;
       
   276 }
       
   277 
       
   278 /** This function will activate a search around a central tile, looking for some houses
       
   279  * that fit the requested characteristics
       
   280  * @param parameter that is given by the callback.
       
   281  *                  bits 0..6 radius of the search
       
   282  *                  bits 7..8 search type i.e.: 0 = houseID/ 1 = classID/ 2 = grfID
       
   283  * @param tile TileIndex from which to start the search
       
   284  * @param house the HouseID that is associated to the house, the callback is called for
       
   285  * @return the Manhattan distance from the center tile, if any, and 0 if failure
       
   286  */
       
   287 static uint32 GetDistanceFromNearbyHouse(uint8 parameter, TileIndex tile, HouseID house)
       
   288 {
       
   289 	static TestTileOnSearchProc * const search_procs[3] = {
       
   290 		SearchNearbyHouseID,
       
   291 		SearchNearbyHouseClass,
       
   292 		SearchNearbyHouseGRFID,
       
   293 	};
       
   294 	TileIndex found_tile = tile;
       
   295 	uint8 searchtype = GB(parameter, 6, 2);
       
   296 	uint8 searchradius = GB(parameter, 0, 6);
       
   297 	if (searchtype >= lengthof(search_procs)) return 0;  // do not run on ill-defined code
       
   298 	if (searchradius < 1) return 0; // do not use a too low radius
       
   299 
       
   300 	SearchNearbyHouseData nbhd;
       
   301 	nbhd.hs = GetHouseSpecs(house);
       
   302 	nbhd.north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
       
   303 
       
   304 	/* Use a pointer for the tile to start the search. Will be required for calculating the distance*/
       
   305 	if (CircularTileSearch(&found_tile, 2 * searchradius + 1, search_procs[searchtype], &nbhd)) {
       
   306 		return DistanceManhattan(found_tile, tile);
       
   307 	}
       
   308 	return 0;
       
   309 }
       
   310 
   204 /**
   311 /**
   205  * HouseGetVariable():
   312  * HouseGetVariable():
   206  *
   313  *
   207  * Used by the resolver to get values for feature 07 deterministic spritegroups.
   314  * Used by the resolver to get values for feature 07 deterministic spritegroups.
   208  */
   315  */
   257 		/* Current animation frame of nearby house tiles */
   364 		/* Current animation frame of nearby house tiles */
   258 		case 0x63: {
   365 		case 0x63: {
   259 			TileIndex testtile = GetNearbyTile(parameter, tile);
   366 			TileIndex testtile = GetNearbyTile(parameter, tile);
   260 			return IsTileType(testtile, MP_HOUSE) ? GetHouseAnimationFrame(testtile) : 0;
   367 			return IsTileType(testtile, MP_HOUSE) ? GetHouseAnimationFrame(testtile) : 0;
   261 		}
   368 		}
       
   369 
       
   370 		/* Cargo acceptance history of nearby stations */
       
   371 		/*case 0x64: not implemented yet */
       
   372 
       
   373 		/* Distance test for some house types */
       
   374 		case 0x65: return GetDistanceFromNearbyHouse(parameter, tile, object->u.house.house_id);
   262 
   375 
   263 		/* Read GRF parameter */
   376 		/* Read GRF parameter */
   264 		case 0x7F: return GetGRFParameter(object->u.house.house_id, parameter);
   377 		case 0x7F: return GetGRFParameter(object->u.house.house_id, parameter);
   265 	}
   378 	}
   266 
   379