(svn r11139) -Codechange: add support for persistent storage for NewGRFs.
authorrubidium
Sat, 22 Sep 2007 13:56:38 +0000
changeset 7610 13b7d9e247d2
parent 7609 b70ffc13652a
child 7611 886a5a0c7479
(svn r11139) -Codechange: add support for persistent storage for NewGRFs.
src/industry.h
src/industry_cmd.cpp
src/newgrf_industries.cpp
src/newgrf_industrytiles.cpp
src/newgrf_spritegroup.cpp
src/newgrf_spritegroup.h
src/newgrf_storage.h
src/saveload.cpp
src/saveload.h
--- a/src/industry.h	Sat Sep 22 12:59:43 2007 +0000
+++ b/src/industry.h	Sat Sep 22 13:56:38 2007 +0000
@@ -7,6 +7,7 @@
 
 #include "oldpool.h"
 #include "helpers.hpp"
+#include "newgrf_storage.h"
 
 typedef byte IndustryGfx;
 typedef uint8 IndustryType;
@@ -94,6 +95,8 @@
  * Defines the internal data of a functionnal industry
  */
 struct Industry : PoolItem<Industry, IndustryID, &_Industry_pool> {
+	typedef PersistentStorageArray<uint32, 16> PersistentStorage;
+
 	TileIndex xy;                       ///< coordinates of the primary tile the industry is built one
 	byte width;
 	byte height;
@@ -121,6 +124,8 @@
 	Date last_cargo_accepted_at;        ///< Last day cargo was accepted by this industry
 	byte selected_layout;               ///< Which tile layout was used when creating the industry
 
+	PersistentStorage psa;              ///< Persistent storage for NewGRF industries.
+
 	Industry(TileIndex tile = 0) : xy(tile) {}
 	~Industry();
 
--- a/src/industry_cmd.cpp	Sat Sep 22 12:59:43 2007 +0000
+++ b/src/industry_cmd.cpp	Sat Sep 22 13:56:38 2007 +0000
@@ -2074,6 +2074,8 @@
 	SLE_CONDVAR(Industry, last_cargo_accepted_at,     SLE_INT32,                 70, SL_MAX_VERSION),
 	SLE_CONDVAR(Industry, selected_layout,            SLE_UINT8,                 73, SL_MAX_VERSION),
 
+	SLE_CONDARRX(cpp_offsetof(Industry, psa) + cpp_offsetof(Industry::PersistentStorage, storage), SLE_UINT32, 16, 76, SL_MAX_VERSION),
+
 	/* reserve extra space in savegame here. (currently 32 bytes) */
 	SLE_CONDNULL(32, 2, SL_MAX_VERSION),
 
--- a/src/newgrf_industries.cpp	Sat Sep 22 12:59:43 2007 +0000
+++ b/src/newgrf_industries.cpp	Sat Sep 22 13:56:38 2007 +0000
@@ -243,6 +243,9 @@
 		case 0x67:
 		case 0x68: return GetCountAndDistanceOfClosestInstance(parameter, variable == 0x68 ? GB(GetRegister(0x101), 0, 8) : 0, industry);
 
+		/* Get a variable from the persistent storage */
+		case 0x7C: return industry->psa.Get(parameter);
+
 		/* Industry structure access*/
 		case 0x80: return industry->xy;
 		case 0x81: return GB(industry->xy, 8, 8);
@@ -323,6 +326,7 @@
 	res->GetVariable   = IndustryGetVariable;
 	res->ResolveReal   = IndustryResolveReal;
 
+	res->psa             = &indus->psa;
 	res->u.industry.tile = tile;
 	res->u.industry.ind  = indus;
 	res->u.industry.gfx  = INVALID_INDUSTRYTILE;
--- a/src/newgrf_industrytiles.cpp	Sat Sep 22 12:59:43 2007 +0000
+++ b/src/newgrf_industrytiles.cpp	Sat Sep 22 13:56:38 2007 +0000
@@ -140,6 +140,7 @@
 	res->GetVariable   = IndustryTileGetVariable;
 	res->ResolveReal   = IndustryTileResolveReal;
 
+	res->psa             = &indus->psa;
 	res->u.industry.tile = tile;
 	res->u.industry.ind  = indus;
 	res->u.industry.gfx  = gfx;
--- a/src/newgrf_spritegroup.cpp	Sat Sep 22 12:59:43 2007 +0000
+++ b/src/newgrf_spritegroup.cpp	Sat Sep 22 13:56:38 2007 +0000
@@ -109,7 +109,7 @@
 /* Evaluate an adjustment for a variable of the given size.
 * U is the unsigned type and S is the signed type to use. */
 template <typename U, typename S>
-static U EvalAdjustT(const DeterministicSpriteGroupAdjust *adjust, U last_value, uint32 value)
+static U EvalAdjustT(const DeterministicSpriteGroupAdjust *adjust, ResolverObject *object, U last_value, uint32 value)
 {
 	value >>= adjust->shift_num;
 	value  &= adjust->and_mask;
@@ -139,6 +139,7 @@
 		case DSGA_OP_XOR:  return last_value ^ value;
 		case DSGA_OP_STO:  _temp_store.Store(value, last_value); return last_value;
 		case DSGA_OP_RST:  return value;
+		case DSGA_OP_STOP: if (object->psa != NULL) object->psa->Store(value, last_value); return last_value;
 		default:           return value;
 	}
 }
@@ -177,9 +178,9 @@
 		}
 
 		switch (group->g.determ.size) {
-			case DSG_SIZE_BYTE:  value = EvalAdjustT<uint8, int8>(adjust, last_value, value); break;
-			case DSG_SIZE_WORD:  value = EvalAdjustT<uint16, int16>(adjust, last_value, value); break;
-			case DSG_SIZE_DWORD: value = EvalAdjustT<uint32, int32>(adjust, last_value, value); break;
+			case DSG_SIZE_BYTE:  value = EvalAdjustT<uint8,  int8> (adjust, object, last_value, value); break;
+			case DSG_SIZE_WORD:  value = EvalAdjustT<uint16, int16>(adjust, object, last_value, value); break;
+			case DSG_SIZE_DWORD: value = EvalAdjustT<uint32, int32>(adjust, object, last_value, value); break;
 			default: NOT_REACHED(); break;
 		}
 		last_value = value;
--- a/src/newgrf_spritegroup.h	Sat Sep 22 12:59:43 2007 +0000
+++ b/src/newgrf_spritegroup.h	Sat Sep 22 13:56:38 2007 +0000
@@ -76,6 +76,7 @@
 	DSGA_OP_XOR,  ///< a ^ b
 	DSGA_OP_STO,  ///< store a into temporary storage, indexed by b. return a
 	DSGA_OP_RST,  ///< return b
+	DSGA_OP_STOP, ///< store a into persistent storage, indexed by b, return a
 };
 
 
@@ -199,6 +200,8 @@
 
 	bool info_view; ///< Indicates if the item is being drawn in an info window
 
+	BaseStorageArray *psa; ///< The persistent storage array of this resolved object.
+
 	union {
 		struct {
 			const struct Vehicle *self;
--- a/src/newgrf_storage.h	Sat Sep 22 12:59:43 2007 +0000
+++ b/src/newgrf_storage.h	Sat Sep 22 13:56:38 2007 +0000
@@ -21,7 +21,14 @@
 	 *  - reverting to the previous version
 	 * @param keep_changes do we save or revert the changes since the last ClearChanges?
 	 */
-	virtual void ClearChanges(bool keep_changes) {}
+	virtual void ClearChanges(bool keep_changes) = 0;
+
+	/**
+	 * Stores some value at a given position.
+	 * @param pos   the position to write at
+	 * @param value the value to write
+	 */
+	virtual void Store(uint pos, uint32 value) = 0;
 };
 
 /**
@@ -54,7 +61,7 @@
 	 * @param pos   the position to write at
 	 * @param value the value to write
 	 */
-	void Store(uint pos, TYPE value)
+	void Store(uint pos, uint32 value)
 	{
 		/* Out of the scope of the array */
 		if (pos >= SIZE) return;
@@ -83,7 +90,7 @@
 	 * @param pos the position to get the data from
 	 * @return the data from that position
 	 */
-	TYPE Get(uint pos)
+	TYPE Get(uint pos) const
 	{
 		/* Out of the scope of the array */
 		if (pos >= SIZE) return 0;
@@ -124,7 +131,7 @@
 	 * @param pos   the position to write at
 	 * @param value the value to write
 	 */
-	void Store(uint pos, TYPE value)
+	void Store(uint pos, uint32 value)
 	{
 		/* Out of the scope of the array */
 		if (pos >= SIZE) return;
@@ -138,7 +145,7 @@
 	 * @param pos the position to get the data from
 	 * @return the data from that position
 	 */
-	TYPE Get(uint pos)
+	TYPE Get(uint pos) const
 	{
 		/* Out of the scope of the array */
 		if (pos >= SIZE) return 0;
--- a/src/saveload.cpp	Sat Sep 22 12:59:43 2007 +0000
+++ b/src/saveload.cpp	Sat Sep 22 13:56:38 2007 +0000
@@ -29,7 +29,7 @@
 #include "strings.h"
 #include <list>
 
-extern const uint16 SAVEGAME_VERSION = 75;
+extern const uint16 SAVEGAME_VERSION = 76;
 uint16 _sl_version;       ///< the major savegame version identifier
 byte   _sl_minor_version; ///< the minor savegame version, DO NOT USE!
 
--- a/src/saveload.h	Sat Sep 22 12:59:43 2007 +0000
+++ b/src/saveload.h	Sat Sep 22 13:56:38 2007 +0000
@@ -211,15 +211,16 @@
 #define SLE_WRITEBYTE(base, variable, value) SLE_GENERAL(SL_WRITEBYTE, base, variable, 0, 0, value, value)
 
 /* The same as the ones at the top, only the offset is given directly; used for unions */
-#define SLE_GENERALX(cmd, offset, type, param1, param2) {false, cmd, type, 0, param1, param2, (void*)(offset)}
-#define SLE_CONDVARX(offset, type, from, to) SLE_GENERALX(SL_VAR, offset, type, from, to)
-#define SLE_CONDREFX(offset, type, from, to) SLE_GENERALX(SL_REF, offset, type, from, to)
+#define SLE_GENERALX(cmd, offset, type, length, param1, param2) {false, cmd, type, length, param1, param2, (void*)(offset)}
+#define SLE_CONDVARX(offset, type, from, to) SLE_GENERALX(SL_VAR, offset, type, 0, from, to)
+#define SLE_CONDARRX(offset, type, length, from, to) SLE_GENERALX(SL_ARR, offset, type, length, from, to)
+#define SLE_CONDREFX(offset, type, from, to) SLE_GENERALX(SL_REF, offset, type, 0, from, to)
 
 #define SLE_VARX(offset, type) SLE_CONDVARX(offset, type, 0, SL_MAX_VERSION)
 #define SLE_REFX(offset, type) SLE_CONDREFX(offset, type, 0, SL_MAX_VERSION)
 
-#define SLE_WRITEBYTEX(offset, something) SLE_GENERALX(SL_WRITEBYTE, offset, 0, something, 0)
-#define SLE_VEH_INCLUDEX() SLE_GENERALX(SL_VEH_INCLUDE, 0, 0, 0, SL_MAX_VERSION)
+#define SLE_WRITEBYTEX(offset, something) SLE_GENERALX(SL_WRITEBYTE, offset, 0, 0, something, 0)
+#define SLE_VEH_INCLUDEX() SLE_GENERALX(SL_VEH_INCLUDE, 0, 0, 0, 0, SL_MAX_VERSION)
 
 /* End marker */
 #define SLE_END() {false, SL_END, 0, 0, 0, 0, NULL}