Changeset - r17850:7726e086e93b
[Not reviewed]
master
0 4 0
michi_cc - 13 years ago 2011-07-11 16:32:19
michi_cc@openttd.org
(svn r22656) -Codechange: Deduplicate the custom error message of the industry shape and location callbacks.
4 files changed with 32 insertions and 26 deletions:
0 comments (0 inline, 0 general)
src/newgrf_commons.cpp
Show inline comments
 
@@ -21,12 +21,15 @@
 
#include "station_map.h"
 
#include "tree_map.h"
 
#include "tunnelbridge_map.h"
 
#include "newgrf_object.h"
 
#include "genworld.h"
 
#include "newgrf_spritegroup.h"
 
#include "newgrf_text.h"
 

	
 
#include "table/strings.h"
 

	
 
/**
 
 * Constructor of generic class
 
 * @param offset end of original data for this entity. i.e: houses = 110
 
 * @param maximum of entities this manager can deal with. i.e: houses = 512
 
 * @param invalid is the ID used to identify an invalid entity id
 
@@ -443,12 +446,36 @@ uint32 GetNearbyTileInformation(TileInde
 
	uint z;
 
	Slope tileh = GetTileSlope(tile, &z);
 
	byte terrain_type = (HasTileWaterClass(tile) ? GetWaterClass(tile) : WATER_CLASS_INVALID) << 5 | GetTerrainType(tile) << 2 | (tile_type == MP_WATER ? 1 : 0) << 1;
 
	return tile_type << 24 | z << 16 | terrain_type << 8 | tileh;
 
}
 

	
 
/**
 
 * Get the error message from a shape/location/slope check callback result.
 
 * @param cb_res Callback result to translate. If bit 10 is set this is a standard error message, otherwise a NewGRF provided string.
 
 * @param grfid grfID to use to resolve a custom error message.
 
 * @param default_error Error message to use for the generic error.
 
 * @return CommandCost indicating success or the error message.
 
 */
 
CommandCost GetErrorMessageFromLocationCallbackResult(uint16 cb_res, uint32 grfid, StringID default_error)
 
{
 
	CommandCost res;
 
	switch (cb_res) {
 
		case 0x400: return res; // No error.
 
		case 0x401: res = CommandCost(default_error); break;
 
		case 0x402: res = CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_IN_RAINFOREST); break;
 
		case 0x403: res = CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_IN_DESERT); break;
 
		default:    res = CommandCost(GetGRFStringID(grfid, 0xD000 + cb_res)); break;
 
	}
 

	
 
	/* Copy some parameters from the registers to the error message text ref. stack */
 
	res.UseTextRefStack(4);
 

	
 
	return res;
 
}
 

	
 
/* static */ SmallVector<DrawTileSeqStruct, 8> NewGRFSpriteLayout::result_seq;
 

	
 
/**
 
 * Clone the building sprites of a spritelayout.
 
 * @param source The building sprites to copy.
 
 */
src/newgrf_commons.h
Show inline comments
 
@@ -16,12 +16,13 @@
 
#define NEWGRF_COMMONS_H
 

	
 
#include "tile_type.h"
 
#include "sprite.h"
 
#include "core/alloc_type.hpp"
 
#include "core/smallvec_type.hpp"
 
#include "command_type.h"
 

	
 
/** Context for tile accesses */
 
enum TileContext {
 
	TCX_NORMAL,         ///< Nothing special.
 
	TCX_UPPER_HALFTILE, ///< Querying information about the upper part of a tile with halftile foundation.
 
	TCX_ON_BRIDGE,      ///< Querying information about stuff on the bridge (via some bridgehead).
 
@@ -264,12 +265,13 @@ extern AirportOverrideManager _airport_m
 
extern AirportTileOverrideManager _airporttile_mngr;
 
extern ObjectOverrideManager _object_mngr;
 

	
 
uint32 GetTerrainType(TileIndex tile, TileContext context = TCX_NORMAL);
 
TileIndex GetNearbyTile(byte parameter, TileIndex tile, bool signed_offsets = true);
 
uint32 GetNearbyTileInformation(TileIndex tile);
 
CommandCost GetErrorMessageFromLocationCallbackResult(uint16 cb_res, uint32 grfid, StringID default_error);
 

	
 
/**
 
 * Data related to the handling of grf files.
 
 * @tparam Tcnt Number of spritegroups
 
 */
 
template <size_t Tcnt>
src/newgrf_industries.cpp
Show inline comments
 
@@ -541,26 +541,15 @@ CommandCost CheckIfCallBackAllowsCreatio
 
	group = SpriteGroup::Resolve(GetIndustrySpec(type)->grf_prop.spritegroup[0], &object);
 

	
 
	/* Unlike the "normal" cases, not having a valid result means we allow
 
	 * the building of the industry, as that's how it's done in TTDP. */
 
	if (group == NULL) return CommandCost();
 
	uint16 result = group->GetCallbackResult();
 
	if (result == 0x400 || result == CALLBACK_FAILED) return CommandCost();
 
	if (result == CALLBACK_FAILED) return CommandCost();
 

	
 
	CommandCost res;
 
	switch (result) {
 
		case 0x401: res = CommandCost(STR_ERROR_SITE_UNSUITABLE); break;
 
		case 0x402: res = CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_IN_RAINFOREST); break;
 
		case 0x403: res = CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_IN_DESERT); break;
 
		default:    res = CommandCost(GetGRFStringID(indspec->grf_prop.grffile->grfid, 0xD000 + result)); break;
 
	}
 

	
 
	/* Copy some parameters from the registers to the error message text ref. stack */
 
	res.UseTextRefStack(4);
 

	
 
	return res;
 
	return GetErrorMessageFromLocationCallbackResult(result, indspec->grf_prop.grffile->grfid, STR_ERROR_SITE_UNSUITABLE);
 
}
 

	
 
/**
 
 * Check with callback #CBM_IND_AVAILABLE whether the industry can be built.
 
 * @param type Industry type to check.
 
 * @param creation_type Reason to construct a new industry.
src/newgrf_industrytiles.cpp
Show inline comments
 
@@ -300,26 +300,14 @@ CommandCost PerformIndustryTileSlopeChec
 
		return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
 
	}
 
	if (its->grf_prop.grffile->grf_version < 7) {
 
		if (callback_res != 0) return CommandCost();
 
		return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
 
	}
 
	if (callback_res == 0x400) return CommandCost();
 

	
 
	CommandCost res;
 
	switch (callback_res) {
 
		case 0x401: res = CommandCost(STR_ERROR_SITE_UNSUITABLE); break;
 
		case 0x402: res = CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_IN_RAINFOREST); break;
 
		case 0x403: res = CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_IN_DESERT); break;
 
		default:    res = CommandCost(GetGRFStringID(its->grf_prop.grffile->grfid, 0xD000 + callback_res)); break;
 
	}
 

	
 
	/* Copy some parameters from the registers to the error message text ref. stack */
 
	res.UseTextRefStack(4);
 

	
 
	return res;
 
	return GetErrorMessageFromLocationCallbackResult(callback_res, its->grf_prop.grffile->grfid, STR_ERROR_SITE_UNSUITABLE);
 
}
 

	
 
/* Simple wrapper for GetHouseCallback to keep the animation unified. */
 
uint16 GetSimpleIndustryCallback(CallbackID callback, uint32 param1, uint32 param2, const IndustryTileSpec *spec, Industry *ind, TileIndex tile)
 
{
 
	return GetIndustryTileCallback(callback, param1, param2, spec - GetIndustryTileSpec(0), ind, tile);
0 comments (0 inline, 0 general)