summaryrefslogtreecommitdiff
path: root/src/openttd.h
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2007-06-18 19:53:50 +0000
committerrubidium <rubidium@openttd.org>2007-06-18 19:53:50 +0000
commit49220cc6f1e3570dc1b9001c40af2a8a4e35b649 (patch)
tree62843984493ffedb69f91d7b85cb631ecb61ff3e /src/openttd.h
parent3771d666c0e6bb48980b7548a2b38b3594efb3ff (diff)
downloadopenttd-49220cc6f1e3570dc1b9001c40af2a8a4e35b649.tar.xz
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
Diffstat (limited to 'src/openttd.h')
-rw-r--r--src/openttd.h81
1 files changed, 77 insertions, 4 deletions
diff --git a/src/openttd.h b/src/openttd.h
index 6969f1c23..290ca968b 100644
--- a/src/openttd.h
+++ b/src/openttd.h
@@ -52,6 +52,8 @@ struct PalSpriteID {
typedef uint16 EngineID;
typedef uint16 UnitID;
typedef uint16 StringID;
+#define INVALID_STRING_ID 0xFFFF
+
typedef EngineID *EngineList; ///< engine list type placeholder acceptable for C code (see helpers.cpp)
/* IDs used in Pools */
@@ -67,7 +69,6 @@ typedef uint16 SignID;
typedef uint16 GroupID;
typedef uint16 EngineRenewID;
typedef uint16 DestinationID;
-typedef int32 CommandCost;
/* DestinationID must be at least as large as every these below, because it can
* be any of them
@@ -359,10 +360,84 @@ struct ViewportSign {
byte width_1, width_2;
};
+/**
+ * Common return value for all commands. Wraps the cost and
+ * a possible error message/state together.
+ */
+class CommandCost {
+ int32 cost; ///< The cost of this action
+ StringID message; ///< Warning message for when success is unset
+ bool success; ///< Whether the comment went fine up to this moment
+
+public:
+ /**
+ * Creates a command cost return with no cost and no error
+ */
+ CommandCost() : cost(0), message(INVALID_STRING_ID), success(true) {}
+
+ /**
+ * Creates a command return value the is failed with the given message
+ */
+ CommandCost(StringID msg) : cost(0), message(msg), success(false) {}
+
+ /**
+ * Creates a command return value with the given start cost
+ * @param cst the initial cost of this command
+ */
+ CommandCost(int32 cst) : cost(cst), message(INVALID_STRING_ID), success(true) {}
+ /** "Hack" to make everything compile nicely, not needed when cost is int64 */
+ CommandCost(uint cst) : cost(cst), message(INVALID_STRING_ID), success(true) {}
+
+ /**
+ * Adds the cost of the given command return value to this cost.
+ * Also takes a possible error message when it is set.
+ * @param ret the command to add the cost of.
+ * @return this class.
+ */
+ CommandCost AddCost(CommandCost ret);
+
+ /**
+ * Adds the given cost to the cost of the command.
+ * @param cost the cost to add
+ * @return this class.
+ */
+ CommandCost AddCost(int32 cost);
+
+ /**
+ * Multiplies the cost of the command by the given factor.
+ * @param cost factor to multiply the costs with
+ * @return this class
+ */
+ CommandCost MultiplyCost(int factor);
+
+ /**
+ * The costs as made up to this moment
+ * @return the costs
+ */
+ int32 GetCost() const;
+
+ /**
+ * Sets the global error message *if* this class has one.
+ */
+ void SetGlobalErrorMessage() const;
+
+ /**
+ * Did this command succeed?
+ * @return true if and only if it succeeded
+ */
+ bool Succeeded() const;
+
+ /**
+ * Did this command fail?
+ * @return true if and only if it failed
+ */
+ bool Failed() const;
+};
+
typedef void DrawTileProc(TileInfo *ti);
typedef uint GetSlopeZProc(TileIndex tile, uint x, uint y);
-typedef int32 ClearTileProc(TileIndex tile, byte flags);
+typedef CommandCost ClearTileProc(TileIndex tile, byte flags);
typedef void GetAcceptedCargoProc(TileIndex tile, AcceptedCargo res);
typedef void GetTileDescProc(TileIndex tile, TileDesc *td);
/**
@@ -589,8 +664,6 @@ enum {
VARDEF byte _savegame_sort_order;
-#define INVALID_STRING_ID 0xFFFF
-
enum {
MAX_SCREEN_WIDTH = 2048,
MAX_SCREEN_HEIGHT = 1200,