summaryrefslogtreecommitdiff
path: root/src/newgrf_commons.cpp
diff options
context:
space:
mode:
authorfrosch <frosch@openttd.org>2011-11-08 17:26:49 +0000
committerfrosch <frosch@openttd.org>2011-11-08 17:26:49 +0000
commit30874b5e81af0fff2f42d34b9b105ee86666b8ac (patch)
tree6fd8200f08019910de826cf6f1b9b6e92f7d2d69 /src/newgrf_commons.cpp
parentb98c7763de42eda4b3d19604bc3f33452b9b05e4 (diff)
downloadopenttd-30874b5e81af0fff2f42d34b9b105ee86666b8ac.tar.xz
(svn r23147) -Change: [NewGRF v8] Unify the return values of boolean callbacks, and check the results for validity.
Diffstat (limited to 'src/newgrf_commons.cpp')
-rw-r--r--src/newgrf_commons.cpp39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/newgrf_commons.cpp b/src/newgrf_commons.cpp
index 2f49c51c7..5f2ada887 100644
--- a/src/newgrf_commons.cpp
+++ b/src/newgrf_commons.cpp
@@ -538,6 +538,45 @@ void ErrorUnknownCallbackResult(uint32 grfid, uint16 cbid, uint16 cb_res)
DEBUG(grf, 0, "%s", buffer + 3);
}
+/**
+ * Converts a callback result into a boolean.
+ * For grf version < 8 the result is checked for zero or non-zero.
+ * For grf version >= 8 the callback result must be 0 or 1.
+ * @param grffile NewGRF returning the value.
+ * @param cbid Callback returning the value.
+ * @param cb_res Callback result.
+ * @return Boolean value. True if cb_res != 0.
+ */
+bool ConvertBooleanCallback(const GRFFile *grffile, uint16 cbid, uint16 cb_res)
+{
+ assert(cb_res != CALLBACK_FAILED); // We do not know what to return
+
+ if (grffile->grf_version < 8) return cb_res != 0;
+
+ if (cb_res > 1) ErrorUnknownCallbackResult(grffile->grfid, cbid, cb_res);
+ return cb_res != 0;
+}
+
+/**
+ * Converts a callback result into a boolean.
+ * For grf version < 8 the first 8 bit of the result are checked for zero or non-zero.
+ * For grf version >= 8 the callback result must be 0 or 1.
+ * @param grffile NewGRF returning the value.
+ * @param cbid Callback returning the value.
+ * @param cb_res Callback result.
+ * @return Boolean value. True if cb_res != 0.
+ */
+bool Convert8bitBooleanCallback(const GRFFile *grffile, uint16 cbid, uint16 cb_res)
+{
+ assert(cb_res != CALLBACK_FAILED); // We do not know what to return
+
+ if (grffile->grf_version < 8) return GB(cb_res, 0, 8) != 0;
+
+ if (cb_res > 1) ErrorUnknownCallbackResult(grffile->grfid, cbid, cb_res);
+ return cb_res != 0;
+}
+
+
/* static */ SmallVector<DrawTileSeqStruct, 8> NewGRFSpriteLayout::result_seq;
/**