2009-01-12 17:11:45 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2011-11-29 23:07:38 +00:00
|
|
|
/** @file script_error.hpp Everything to query errors. */
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2011-11-29 23:07:38 +00:00
|
|
|
#ifndef SCRIPT_ERROR_HPP
|
|
|
|
#define SCRIPT_ERROR_HPP
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2011-11-29 23:07:38 +00:00
|
|
|
#include "script_object.hpp"
|
2009-01-12 17:11:45 +00:00
|
|
|
#include <map>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper to write precondition enforcers for the AI API in an abbreviated manner.
|
|
|
|
* @param returnval The value to return on failure.
|
|
|
|
* @param condition The condition that must be obeyed.
|
|
|
|
*/
|
|
|
|
#define EnforcePrecondition(returnval, condition) \
|
|
|
|
if (!(condition)) { \
|
2011-11-29 23:15:35 +00:00
|
|
|
ScriptObject::SetLastError(ScriptError::ERR_PRECONDITION_FAILED); \
|
2009-01-12 17:11:45 +00:00
|
|
|
return returnval; \
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper to write precondition enforcers for the AI API in an abbreviated manner.
|
|
|
|
* @param returnval The value to return on failure.
|
|
|
|
* @param condition The condition that must be obeyed.
|
2011-11-29 23:15:35 +00:00
|
|
|
* @param error_code The error code passed to ScriptObject::SetLastError.
|
2009-01-12 17:11:45 +00:00
|
|
|
*/
|
|
|
|
#define EnforcePreconditionCustomError(returnval, condition, error_code) \
|
|
|
|
if (!(condition)) { \
|
2011-11-29 23:15:35 +00:00
|
|
|
ScriptObject::SetLastError(error_code); \
|
2009-01-12 17:11:45 +00:00
|
|
|
return returnval; \
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class that handles all error related functions.
|
2011-11-29 23:27:26 +00:00
|
|
|
* @api ai
|
2009-01-12 17:11:45 +00:00
|
|
|
*/
|
2011-11-29 23:15:35 +00:00
|
|
|
class ScriptError : public ScriptObject {
|
2009-01-12 17:11:45 +00:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* All categories errors can be divided in.
|
|
|
|
*/
|
|
|
|
enum ErrorCategories {
|
2010-08-01 19:36:36 +00:00
|
|
|
ERR_CAT_NONE = 0, ///< Error messages not related to any category.
|
|
|
|
ERR_CAT_GENERAL, ///< Error messages related to general things.
|
|
|
|
ERR_CAT_VEHICLE, ///< Error messages related to building / maintaining vehicles.
|
|
|
|
ERR_CAT_STATION, ///< Error messages related to building / maintaining stations.
|
|
|
|
ERR_CAT_BRIDGE, ///< Error messages related to building / removing bridges.
|
|
|
|
ERR_CAT_TUNNEL, ///< Error messages related to building / removing tunnels.
|
|
|
|
ERR_CAT_TILE, ///< Error messages related to raising / lowering and demolishing tiles.
|
|
|
|
ERR_CAT_SIGN, ///< Error messages related to building / removing signs.
|
|
|
|
ERR_CAT_RAIL, ///< Error messages related to building / maintaining rails.
|
|
|
|
ERR_CAT_ROAD, ///< Error messages related to building / maintaining roads.
|
|
|
|
ERR_CAT_ORDER, ///< Error messages related to managing orders.
|
|
|
|
ERR_CAT_MARINE, ///< Error messages related to building / removing ships, docks and channels.
|
|
|
|
ERR_CAT_WAYPOINT, ///< Error messages related to building / maintaining waypoints.
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* DO NOT USE! The error bitsize determines how many errors can be stored in
|
|
|
|
* a category and what the offsets are of all categories.
|
|
|
|
*/
|
|
|
|
ERR_CAT_BIT_SIZE = 8,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* All general related error messages.
|
|
|
|
*/
|
|
|
|
enum ErrorMessages {
|
|
|
|
/** Initial error value */
|
|
|
|
ERR_NONE = ERR_CAT_NONE << ERR_CAT_BIT_SIZE, // []
|
2010-07-31 21:02:56 +00:00
|
|
|
/** If an error occurred and the error wasn't mapped */
|
2009-01-12 17:11:45 +00:00
|
|
|
ERR_UNKNOWN, // []
|
|
|
|
/** If a precondition is not met */
|
|
|
|
ERR_PRECONDITION_FAILED, // []
|
|
|
|
/** A string supplied was too long */
|
|
|
|
ERR_PRECONDITION_STRING_TOO_LONG, // []
|
|
|
|
/** An error returned by a NewGRF. No possibility to get the exact error in an AI readable format */
|
|
|
|
ERR_NEWGRF_SUPPLIED_ERROR, // []
|
|
|
|
|
|
|
|
/** Base for general errors */
|
|
|
|
ERR_GENERAL_BASE = ERR_CAT_GENERAL << ERR_CAT_BIT_SIZE,
|
|
|
|
|
|
|
|
/** Not enough cash to perform the previous action */
|
2009-04-21 23:40:56 +00:00
|
|
|
ERR_NOT_ENOUGH_CASH, // [STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY]
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
/** Local authority won't allow the previous action */
|
2009-04-21 23:40:56 +00:00
|
|
|
ERR_LOCAL_AUTHORITY_REFUSES, // [STR_ERROR_LOCAL_AUTHORITY_REFUSES_TO_ALLOW_THIS]
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
/** The piece of infrastructure you tried to build is already in place */
|
2009-04-21 23:40:56 +00:00
|
|
|
ERR_ALREADY_BUILT, // [STR_ERROR_ALREADY_BUILT, STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST]
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
/** Area isn't clear, try to demolish the building on it */
|
2010-08-08 10:59:30 +00:00
|
|
|
ERR_AREA_NOT_CLEAR, // [STR_ERROR_BUILDING_MUST_BE_DEMOLISHED, STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST, STR_ERROR_MUST_DEMOLISH_RAILROAD, STR_ERROR_MUST_DEMOLISH_AIRPORT_FIRST, STR_ERROR_MUST_DEMOLISH_CARGO_TRAM_STATION_FIRST, STR_ERROR_MUST_DEMOLISH_TRUCK_STATION_FIRST, STR_ERROR_MUST_DEMOLISH_PASSENGER_TRAM_STATION_FIRST, STR_ERROR_MUST_DEMOLISH_BUS_STATION_FIRST, STR_ERROR_BUOY_IN_THE_WAY, STR_ERROR_MUST_DEMOLISH_DOCK_FIRST, STR_ERROR_GENERIC_OBJECT_IN_THE_WAY, STR_ERROR_COMPANY_HEADQUARTERS_IN, STR_ERROR_OBJECT_IN_THE_WAY, STR_ERROR_MUST_REMOVE_ROAD_FIRST, STR_ERROR_MUST_REMOVE_RAILROAD_TRACK, STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST, STR_ERROR_MUST_DEMOLISH_TUNNEL_FIRST, STR_ERROR_EXCAVATION_WOULD_DAMAGE]
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
/** Area / property is owned by another company */
|
2009-04-21 23:40:56 +00:00
|
|
|
ERR_OWNED_BY_ANOTHER_COMPANY, // [STR_ERROR_AREA_IS_OWNED_BY_ANOTHER, STR_ERROR_OWNED_BY]
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
/** The name given is not unique for the object type */
|
2009-08-05 17:59:21 +00:00
|
|
|
ERR_NAME_IS_NOT_UNIQUE, // [STR_ERROR_NAME_MUST_BE_UNIQUE]
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
/** The building you want to build requires flat land */
|
2009-04-21 23:40:56 +00:00
|
|
|
ERR_FLAT_LAND_REQUIRED, // [STR_ERROR_FLAT_LAND_REQUIRED]
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
/** Land is sloped in the wrong direction for this build action */
|
2009-04-21 23:40:56 +00:00
|
|
|
ERR_LAND_SLOPED_WRONG, // [STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION]
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
/** A vehicle is in the way */
|
2009-04-21 23:40:56 +00:00
|
|
|
ERR_VEHICLE_IN_THE_WAY, // [STR_ERROR_TRAIN_IN_THE_WAY, STR_ERROR_ROAD_VEHICLE_IN_THE_WAY, STR_ERROR_SHIP_IN_THE_WAY, STR_ERROR_AIRCRAFT_IN_THE_WAY]
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
/** Site is unsuitable */
|
2009-04-21 23:40:56 +00:00
|
|
|
ERR_SITE_UNSUITABLE, // [STR_ERROR_SITE_UNSUITABLE]
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
/** Too close to the edge of the map */
|
2009-04-21 23:40:56 +00:00
|
|
|
ERR_TOO_CLOSE_TO_EDGE, // [STR_ERROR_TOO_CLOSE_TO_EDGE_OF_MAP]
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
/** Station is too spread out */
|
2009-04-21 23:40:56 +00:00
|
|
|
ERR_STATION_TOO_SPREAD_OUT, // [STR_ERROR_STATION_TOO_SPREAD_OUT]
|
2009-01-12 17:11:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check the membership of the last thrown error.
|
|
|
|
* @return The category the error belongs to.
|
|
|
|
* @note The last throw error can be aquired by calling GetLastError().
|
|
|
|
*/
|
|
|
|
static ErrorCategories GetErrorCategory();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the last error.
|
|
|
|
* @return An ErrorMessages enum value.
|
|
|
|
*/
|
2011-11-29 23:15:35 +00:00
|
|
|
static ScriptErrorType GetLastError();
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the last error in string format (for human readability).
|
|
|
|
* @return An ErrorMessage enum item, as string.
|
|
|
|
*/
|
2009-01-17 15:31:30 +00:00
|
|
|
static char *GetLastErrorString();
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the error based on the OpenTTD StringID.
|
2011-11-29 23:27:26 +00:00
|
|
|
* @api -all
|
2009-01-12 17:11:45 +00:00
|
|
|
* @param internal_string_id The string to convert.
|
|
|
|
* @return The NoAI equivalent error message.
|
|
|
|
*/
|
2011-11-29 23:15:35 +00:00
|
|
|
static ScriptErrorType StringToError(StringID internal_string_id);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
/**
|
2010-04-12 14:12:47 +00:00
|
|
|
* Map an internal OpenTTD error message to its NoAI equivalent.
|
2011-11-29 23:27:26 +00:00
|
|
|
* @api -all
|
2009-01-12 17:11:45 +00:00
|
|
|
* @param internal_string_id The OpenTTD StringID used for an error.
|
|
|
|
* @param ai_error_msg The NoAI equivalent error message.
|
|
|
|
*/
|
2011-11-29 23:15:35 +00:00
|
|
|
static void RegisterErrorMap(StringID internal_string_id, ScriptErrorType ai_error_msg);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
/**
|
2010-04-12 14:12:47 +00:00
|
|
|
* Map an internal OpenTTD error message to its NoAI equivalent.
|
2011-11-29 23:27:26 +00:00
|
|
|
* @api -all
|
2009-01-12 17:11:45 +00:00
|
|
|
* @param ai_error_msg The NoAI error message representation.
|
|
|
|
* @param message The string representation of this error message, used for debug purposes.
|
|
|
|
*/
|
2011-11-29 23:15:35 +00:00
|
|
|
static void RegisterErrorMapString(ScriptErrorType ai_error_msg, const char *message);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
private:
|
2011-11-29 23:15:35 +00:00
|
|
|
typedef std::map<StringID, ScriptErrorType> ScriptErrorMap; ///< The type for mapping between error (internal OpenTTD) StringID to the AI error type.
|
|
|
|
typedef std::map<ScriptErrorType, const char *> ScriptErrorMapString; ///< The type for mapping between error type and textual representation.
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
static ScriptErrorMap error_map; ///< The mapping between error (internal OpenTTD) StringID to the AI error type.
|
|
|
|
static ScriptErrorMapString error_map_string; ///< The mapping between error type and textual representation.
|
2009-01-12 17:11:45 +00:00
|
|
|
};
|
|
|
|
|
2011-11-29 23:07:38 +00:00
|
|
|
#endif /* SCRIPT_ERROR_HPP */
|