Changeset - r7451:e5b11cf98a73
[Not reviewed]
master
0 1 0
belugas - 17 years ago 2007-08-17 00:23:33
belugas@openttd.org
(svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
-Fix: a very few codestyle/comments-typo changes
1 file changed with 55 insertions and 7 deletions:
0 comments (0 inline, 0 general)
src/newgrf_industries.cpp
Show inline comments
 
@@ -69,25 +69,25 @@ static uint GetClosestWaterDistance(Tile
 
				t += max(best_dist - dist, 0);
 
				continue;
 
			}
 
		}
 
	}
 

	
 
	return best_dist;
 
}
 

	
 
/** Make an analysis of a tile and check for its belonging to the same
 
 * industry, and/or the same grf file
 
 * @param new_tile TileIndex of the tile to query
 
 * @param old_tile TileINdex of teh reference tile
 
 * @param old_tile TileIndex of the reference tile
 
 * @param i Industry to which old_tile belongs to
 
 * @return value encoded as per NFO specs */
 
uint32 GetIndustryIDAtOffset(TileIndex new_tile, TileIndex old_tile, const Industry *i)
 
{
 
	if (IsTileType(new_tile, MP_INDUSTRY)) {  // Is this an industry tile?
 

	
 
		if (GetIndustryIndex(new_tile) == i->index) {  // Does it belong to the same industry?
 
			IndustryGfx gfx = GetIndustryGfx(new_tile);
 
			const IndustryTileSpec *indtsp = GetIndustryTileSpec(gfx, false);
 
			const IndustryTileSpec *indold = GetIndustryTileSpec(GetIndustryGfx(old_tile), false);
 

	
 
			if (gfx < NEW_INDUSTRYOFFSET) {  // Does it belongs to an old type?
 
@@ -124,24 +124,74 @@ static uint32 GetClosestIndustry(TileInd
 
{
 
	uint32 best_dist = MAX_UVALUE(uint32);
 
	const Industry *i;
 
	FOR_ALL_INDUSTRIES(i) {
 
		if (i->type != type || i == current) continue;
 

	
 
		best_dist = min(best_dist, DistanceManhattan(tile, i->xy));
 
	}
 

	
 
	return best_dist;
 
}
 

	
 
/** Implementation of both var 67 and 68
 
 * since the mechanism is almost the same, it is easier to regroup them on the same
 
 * function.
 
 * @param param_setID parameter given to the callback, which is the set id, or the local id, in our terminology
 
 * @param layout_filter on what layout do we filter?
 
 * @param current Industry for which the inquiry is made
 
 * @return the formatted answer to the callback : rr(reserved) cc(count) dddd(manhattan distance of closest sister)
 
 */
 
static uint32 GetCountAndDistanceOfClosestInstance(byte param_setID, byte layout_filter, const Industry *current)
 
{
 
	uint32 GrfID = GetRegister(0x100);  ///< Get the GRFID of the definition to look for in register 100h
 
	IndustryType ind_index;
 
	uint32 closest_dist = MAX_UVALUE(uint32);
 
	byte count = 0;
 

	
 
	/* Determine what will be the industry type to look for */
 
	switch (GrfID) {
 
		case 0:  // this is a default industry type
 
			ind_index = param_setID;
 
			break;
 

	
 
		case 0xFFFFFFFF: // current grf
 
			ind_index = GetIndustrySpec(current->type)->grf_prop.grffile->grfid;
 
			/*Fall through*/
 

	
 
		default: //use the grfid specified in register 100h
 
			ind_index = MapNewGRFIndustryType(param_setID, GrfID);
 
			break;
 
	}
 

	
 
	if (layout_filter == 0) {
 
		/* If the filter is 0, it could be because none was specified as well as being really a 0.
 
		 * In either case, just do the regular var67 */
 
		closest_dist = GetClosestIndustry(current->xy, ind_index, current);
 
		count = GetIndustryTypeCount(ind_index);
 
	} else {
 
		/* Count only those who match the same industry type and layout filter
 
		 * Unfortunately, we have to do it manually */
 
		const Industry *i;
 
		FOR_ALL_INDUSTRIES(i) {
 
			if (i->type == ind_index && i != current && i->selected_layout == layout_filter) {
 
				closest_dist = min(closest_dist, DistanceManhattan(current->xy, i->xy));
 
				count++;
 
			}
 
		}
 
	}
 

	
 
	return count << 16 | GB(closest_dist, 0, 16);
 
}
 

	
 
/** This function implements the industries variables that newGRF defines.
 
 * @param variable that is queried
 
 * @param parameter unused
 
 * @param available will return false if ever the variable asked for does not exist
 
 * @param ind is of course the industry we are inquiring
 
 * @return the value stored in the corresponding variable*/
 
uint32 IndustryGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
 
{
 
	const Industry *industry = object->u.industry.ind;
 
	TileIndex tile = object->u.industry.tile;
 
	const IndustrySpec *indspec = GetIndustrySpec(industry->type);
 

	
 
@@ -163,45 +213,43 @@ uint32 IndustryGetVariable(const Resolve
 
		/* Layout number */
 
		case 0x44: return industry->selected_layout;
 

	
 
		/* Get industry ID at offset param */
 
		case 0x60: return GetIndustryIDAtOffset(GetNearbyTile(parameter, industry->xy), tile, industry);
 

	
 
		case 0x61: return 0; // Get random tile bits at offset param
 

	
 
		/* Land info of nearby tiles */
 
		case 0x62: return GetNearbyIndustryTileInformation(parameter, tile, INVALID_INDUSTRY);
 

	
 
		/* Animation stage of nearby tiles */
 
		case 0x63 : {
 
		case 0x63: {
 
			tile = GetNearbyTile(parameter, tile);
 
			if (IsTileType(tile, MP_INDUSTRY) && GetIndustryByTile(tile) == industry) {
 
				return GetIndustryAnimationState(tile);
 
			}
 
			return 0xFFFFFFFF;
 
		}
 

	
 
		/* Distance of nearest industry of given type */
 
		case 0x64: return GetClosestIndustry(tile, MapNewGRFIndustryType(parameter, indspec->grf_prop.grffile->grfid), industry);
 
		/* Get town zone and Manhattan distance of closest town */
 
 		case 0x65: return GetTownRadiusGroup(industry->town, tile) << 16 | min(DistanceManhattan(tile, industry->town->xy), 0xFFFF);
 
		/* Get square of Euclidian distance of closes town */
 
		case 0x66: return GetTownRadiusGroup(industry->town, tile) << 16 | min(DistanceSquare(tile, industry->town->xy), 0xFFFF);
 

	
 
		/* Count of industry, distance of closest instance
 
		 * format is rr(reserved) cc(count)  dddd(manhattan distance of closest sister)
 
		 * A lot more should be done, since it has to check for local id, grf id etc...
 
		 * let's just say it is a beginning ;) */
 
		case 0x67: return GetIndustryTypeCount(industry->type) << 16 | 0;
 
		case 0x68: break;
 
		 * 68 is the same as 67, but with a filtering on selected layout */
 
		case 0x67:
 
		case 0x68: return GetCountAndDistanceOfClosestInstance(parameter, variable == 0x68 ? GB(GetRegister(0x101), 0, 8) : 0, industry);
 

	
 
		/* Industry structure access*/
 
		case 0x80: return industry->xy;
 
		case 0x81: return GB(industry->xy, 8, 8);
 
		/* Pointer to the town the industry is associated with */
 
		case 0x82: return industry->town->index;
 
		case 0x83:
 
		case 0x84:
 
		case 0x85: DEBUG(grf, 0, "NewGRFs shouldn't be doing pointer magic"); break; // not supported
 
		case 0x86: return industry->width;
 
		case 0x87: return industry->height;// xy dimensions
 
		/*  */
0 comments (0 inline, 0 general)