truebrain@9844: /* $Id$ */ truebrain@9844: truebrain@9844: /** @file ai_error.hpp Everything to query errors. */ truebrain@9844: truebrain@9844: #ifndef AI_ERROR_HPP truebrain@9844: #define AI_ERROR_HPP truebrain@9844: truebrain@9844: #include "ai_object.hpp" truebrain@9844: #include truebrain@9844: truebrain@9844: /** rubidium@9868: * Helper to write precondition enforcers for the AI API in an abbreviated manner. rubidium@9868: * @param returnval The value to return on failure. rubidium@9868: * @param condition The condition that must be obeyed. rubidium@9868: */ rubidium@9868: #define EnforcePrecondition(returnval, condition) \ rubidium@9868: if (!(condition)) { \ rubidium@9868: AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED); \ rubidium@9868: return returnval; \ rubidium@9868: } rubidium@9868: rubidium@9868: /** truebrain@9844: * Class that handles all error related functions. truebrain@9844: */ truebrain@9844: class AIError : public AIObject { truebrain@9844: public: truebrain@9844: static const char *GetClassName() { return "AIError"; } truebrain@9844: truebrain@9844: /** truebrain@9844: * All categories errors can be divided in. truebrain@9844: */ truebrain@9844: enum ErrorCategories { truebrain@9844: ERR_CAT_NONE = 0, //!< Error messages not related to any category. truebrain@9844: ERR_CAT_GENERAL, //!< Error messages related to general things. truebrain@9844: ERR_CAT_VEHICLE, //!< Error messages related to building / maintaining vehicles. rubidium@9866: ERR_CAT_STATION, //!< Error messages related to building / maintaining stations. rubidium@9867: ERR_CAT_BRIDGE, //!< Error messages related to building / removing bridges. rubidium@10088: ERR_CAT_TUNNEL, //!< Error messages related to building / removing tunnels. rubidium@10089: ERR_CAT_TILE, //!< Error messages related to raising / lowering and demolishing tiles. rubidium@10090: ERR_CAT_SIGN, //!< Error messages related to building / removing signs. rubidium@10091: ERR_CAT_ROAD, //!< Error messages related to building / maintaining roads. rubidium@10093: ERR_CAT_ORDER, //!< Error messages related to managing orders. rubidium@10094: ERR_CAT_MARINE, //!< Error messages related to building / removing ships, docks and channels. truebrain@9844: truebrain@9844: /** truebrain@9844: * DO NOT USE! The error bitsize determines how many errors can be stored in truebrain@9844: * a category and what the offsets are of all categories. truebrain@9844: */ truebrain@9844: ERR_CAT_BIT_SIZE = 8, truebrain@9844: }; truebrain@9844: truebrain@9844: /** truebrain@9873: * All general related error messages. truebrain@9844: */ truebrain@9844: enum ErrorMessages { rubidium@9861: /** Initial error value */ rubidium@9861: ERR_NONE = ERR_CAT_NONE << ERR_CAT_BIT_SIZE, // [] rubidium@9861: /** If an error occured and the error wasn't mapped */ rubidium@9861: ERR_UNKNOWN, // [] rubidium@9863: /** If a precondition is not met */ rubidium@9863: ERR_PRECONDITION_FAILED, // [] rubidium@9871: /** An error returned by a NewGRF. No possibility to get the exact error in an AI readable format */ rubidium@9871: ERR_NEWGRF_SUPPLIED_ERROR, // [] truebrain@9844: truebrain@9844: /** Base for general errors */ truebrain@9844: ERR_GENERAL_BASE = ERR_CAT_GENERAL << ERR_CAT_BIT_SIZE, truebrain@9844: truebrain@9844: /** Not enough cash to perform the previous action */ truebrain@9844: ERR_NOT_ENOUGH_CASH, // [STR_0003_NOT_ENOUGH_CASH_REQUIRES] truebrain@9844: truebrain@9844: /** Local authority won't allow the previous action */ truebrain@9844: ERR_LOCAL_AUTHORITY_REFUSES, // [STR_2009_LOCAL_AUTHORITY_REFUSES] truebrain@9844: truebrain@9844: /** The piece of infrastructure you tried to build is already in place */ rubidium@9867: ERR_ALREADY_BUILT, // [STR_1007_ALREADY_BUILT, STR_5007_MUST_DEMOLISH_BRIDGE_FIRST] truebrain@9844: truebrain@9844: /** Area isn't clear, try to demolish the building on it */ rubidium@10089: ERR_AREA_NOT_CLEAR, // [STR_2004_BUILDING_MUST_BE_DEMOLISHED, STR_5007_MUST_DEMOLISH_BRIDGE_FIRST, STR_300B_MUST_DEMOLISH_RAILROAD, STR_300E_MUST_DEMOLISH_AIRPORT_FIRST, STR_MUST_DEMOLISH_CARGO_TRAM_STATION, STR_3047_MUST_DEMOLISH_TRUCK_STATION, STR_MUST_DEMOLISH_PASSENGER_TRAM_STATION, STR_3046_MUST_DEMOLISH_BUS_STATION, STR_306A_BUOY_IN_THE_WAY, STR_304D_MUST_DEMOLISH_DOCK_FIRST, STR_4800_IN_THE_WAY, STR_5804_COMPANY_HEADQUARTERS_IN, STR_5800_OBJECT_IN_THE_WAY, STR_1801_MUST_REMOVE_ROAD_FIRST, STR_1008_MUST_REMOVE_RAILROAD_TRACK, STR_5007_MUST_DEMOLISH_BRIDGE_FIRST, STR_5006_MUST_DEMOLISH_TUNNEL_FIRST, STR_1002_EXCAVATION_WOULD_DAMAGE] truebrain@9844: rubidium@10091: /** Area / property is owned by another company */ rubidium@10091: ERR_OWNED_BY_ANOTHER_COMPANY, // [STR_1024_AREA_IS_OWNED_BY_ANOTHER, STR_013B_OWNED_BY] rubidium@9865: rubidium@9865: /** The name given is not unique for the object type */ rubidium@9865: ERR_NAME_IS_NOT_UNIQUE, // [STR_NAME_MUST_BE_UNIQUE] rubidium@9865: rubidium@9865: /** The building you want to build requires flat land */ rubidium@9865: ERR_FLAT_LAND_REQUIRED, // [STR_0007_FLAT_LAND_REQUIRED] rubidium@10091: rubidium@10091: /** Land is sloped in the wrong direction for this build action */ rubidium@10091: ERR_LAND_SLOPED_WRONG, // [STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION] rubidium@10091: rubidium@10091: /** A vehicle is in the way */ rubidium@10094: ERR_VEHICLE_IN_THE_WAY, // [STR_8803_TRAIN_IN_THE_WAY, STR_9000_ROAD_VEHICLE_IN_THE_WAY, STR_980E_SHIP_IN_THE_WAY, STR_A015_AIRCRAFT_IN_THE_WAY] rubidium@10094: rubidium@10094: /** Site is unsuitable */ rubidium@10094: ERR_SITE_UNSUITABLE, // [STR_0239_SITE_UNSUITABLE, STR_304B_SITE_UNSUITABLE] rubidium@10094: rubidium@10094: /** Too close to the edge of the map */ rubidium@10094: ERR_TOO_CLOSE_TO_EDGE, // [STR_0002_TOO_CLOSE_TO_EDGE_OF_MAP] truebrain@9844: }; truebrain@9844: truebrain@9844: /** truebrain@9844: * Check the membership of the last thrown error. truebrain@9844: * @return The category the error belongs to. truebrain@9844: * @note The last throw error can be aquired by calling GetLastError(). truebrain@9844: */ truebrain@9844: static ErrorCategories GetErrorCategory(); truebrain@9844: truebrain@9844: /** truebrain@9844: * Get the last error. truebrain@9844: * @return An ErrorMessages enum value. truebrain@9844: */ rubidium@9864: static AIErrorType GetLastError(); truebrain@9844: truebrain@9844: /** truebrain@9844: * Get the last error in string format (for human readability). truebrain@9844: * @return An ErrorMessage enum item, as string. truebrain@9844: */ truebrain@9844: static const char *GetLastErrorString(); truebrain@9844: truebrain@9844: /** rubidium@9863: * Get the error based on the OpenTTD StringID. rubidium@9863: * @note DO NOT INVOKE THIS METHOD YOURSELF! rubidium@9863: * @param internal_string_id The string to convert. rubidium@9863: * @return The NoAI equivalent error message. rubidium@9863: */ rubidium@9864: static AIErrorType StringToError(StringID internal_string_id); rubidium@9863: rubidium@9863: /** truebrain@9844: * Map an internal OpenTTD error message to it's NoAI equivalent. truebrain@9844: * @note DO NOT INVOKE THIS METHOD YOURSELF! The calls are autogenerated. truebrain@9844: * @param internal_string_id The OpenTTD StringID used for an error. truebrain@9844: * @param ai_error_msg The NoAI equivalent error message. truebrain@9844: */ rubidium@9864: static void RegisterErrorMap(StringID internal_string_id, AIErrorType ai_error_msg); truebrain@9844: truebrain@9844: /** truebrain@9844: * Map an internal OpenTTD error message to it's NoAI equivalent. truebrain@9844: * @note DO NOT INVOKE THIS METHOD YOURSELF! The calls are autogenerated. rubidium@9861: * @param ai_error_msg The NoAI error message representation. truebrain@9844: * @param message The string representation of this error message, used for debug purposes. truebrain@9844: */ rubidium@9864: static void RegisterErrorMapString(AIErrorType ai_error_msg, const char *message); truebrain@9844: truebrain@9844: private: rubidium@9864: typedef std::map AIErrorMap; rubidium@9864: typedef std::map AIErrorMapString; truebrain@9844: truebrain@9844: static AIErrorMap error_map; truebrain@9844: static AIErrorMapString error_map_string; truebrain@9844: }; truebrain@9844: truebrain@9844: #endif /* AI_ERROR_HPP */