(svn r1722) -Feature: Bigger maps - anyone?
authortron
Sat, 29 Jan 2005 19:45:14 +0000
changeset 1218 353a7773bc3c
parent 1217 ab9f02a224ab
child 1219 6a1f8d818d58
(svn r1722) -Feature: Bigger maps - anyone?
landscape.c
lang/english.txt
main_gui.c
map.c
map.h
misc.c
saveload.c
settings.c
settings_gui.c
ttd.c
variables.h
--- a/landscape.c	Sat Jan 29 19:41:44 2005 +0000
+++ b/landscape.c	Sat Jan 29 19:45:14 2005 +0000
@@ -462,25 +462,27 @@
 	_cur_tileloop_tile = tile;
 }
 
-void InitializeLandscape(void)
+void InitializeLandscape(uint log_x, uint log_y)
 {
-	uint map_size = MapSize();
+	uint map_size;
 	uint i;
 
-	memset(_map_owner, OWNER_NONE, map_size);
-	memset(_map2, 0, map_size * sizeof(uint16));
-	memset(_map3_lo, 0, map_size);
-	memset(_map3_hi, 0, map_size);
-	memset(_map_extra_bits, 0, map_size / 4);
+	InitMap(log_x, log_y);
+	map_size = MapSize();
+
 	memset(_map_type_and_height, MP_CLEAR << 4, map_size);
+	memset(_map_owner,              OWNER_NONE, map_size);
+	memset(_map2,                            0, map_size * sizeof(_map2[0]));
+	memset(_map3_lo,                         0, map_size);
+	memset(_map3_hi,                         0, map_size);
+	memset(_map5,                            3, map_size);
+	memset(_map_extra_bits,                  0, map_size / 4);
 
 	// create void tiles at the border
 	for (i = 0; i < MapMaxY(); ++i)
 		SetTileType(i * MapSizeX() + MapMaxX(), MP_VOID);
 	for (i = 0; i < MapSizeX(); ++i)
 		SetTileType(MapSizeX() * MapMaxY() + i, MP_VOID);
-
-	memset(_map5, 3, map_size);
 }
 
 void ConvertGroundTilesIntoWaterTiles(void)
--- a/lang/english.txt	Sat Jan 29 19:41:44 2005 +0000
+++ b/lang/english.txt	Sat Jan 29 19:45:14 2005 +0000
@@ -1397,6 +1397,9 @@
 ############ end network gui strings
 
 
+STR_CONFIG_PATCHES_MAP_X				:{LTBLUE}X-size of map: {ORANGE}{STRING}
+STR_CONFIG_PATCHES_MAP_Y				:{LTBLUE}Y-size of map: {ORANGE}{STRING}
+
 ##id 0x0800
 STR_0800_COST							:{TINYFONT}{RED}Cost: {CURRENCY}
 STR_0801_COST							:{RED}Cost: {CURRENCY}
--- a/main_gui.c	Sat Jan 29 19:41:44 2005 +0000
+++ b/main_gui.c	Sat Jan 29 19:45:14 2005 +0000
@@ -40,7 +40,7 @@
 
 static byte _terraform_size = 1;
 static byte _last_built_railtype;
-extern void GenerateWorld(int mode);
+extern void GenerateWorld(int mode, uint log_x, uint log_y);
 
 extern void GenerateIndustries(void);
 extern void GenerateTowns(void);
@@ -1081,7 +1081,7 @@
 	_random_seeds[0][0] = InteractiveRandom();
 	_random_seeds[0][1] = InteractiveRandom();
 
-	GenerateWorld(1);
+	GenerateWorld(1, _patches.map_x, _patches.map_y);
 	MarkWholeScreenDirty();
 }
 
@@ -2389,14 +2389,14 @@
 	switch(_game_mode) {
 	case GM_MENU:
 		w = AllocateWindow(0, 0, width, height, MainWindowWndProc, 0, NULL);
-		AssignWindowViewport(w, 0, 0, width, height, 0x8080, 0);
+		AssignWindowViewport(w, 0, 0, width, height, TILE_XY(32, 32), 0);
 //		w = AllocateWindowDesc(&_toolb_intro_desc);
 //		w->flags4 &= ~WF_WHITE_BORDER_MASK;
 		ShowSelectGameWindow();
 		break;
 	case GM_NORMAL:
 		w = AllocateWindow(0, 0, width, height, MainWindowWndProc, 0, NULL);
-		AssignWindowViewport(w, 0, 0, width, height, 0x8080, 0);
+		AssignWindowViewport(w, 0, 0, width, height, TILE_XY(32, 32), 0);
 
 		ShowVitalWindows();
 
--- a/map.c	Sat Jan 29 19:41:44 2005 +0000
+++ b/map.c	Sat Jan 29 19:45:14 2005 +0000
@@ -1,22 +1,51 @@
 #include "stdafx.h"
 #include "ttd.h"
+#include "functions.h"
 #include "map.h"
 
-#define TILE_X_BITS 8
-#define TILE_Y_BITS 8
-
-uint _map_log_x = TILE_X_BITS;
-uint _map_log_y = TILE_Y_BITS;
+uint _map_log_x;
+uint _map_log_y;
 
-#define MAP_SIZE ((1 << TILE_X_BITS) * (1 << TILE_Y_BITS))
+byte   *_map_type_and_height = NULL;
+byte   *_map_owner           = NULL;
+uint16 *_map2                = NULL;
+byte   *_map3_lo             = NULL;
+byte   *_map3_hi             = NULL;
+byte   *_map5                = NULL;
+byte   *_map_extra_bits      = NULL;
 
-byte   _map_type_and_height [MAP_SIZE];
-byte   _map5                [MAP_SIZE];
-byte   _map3_lo             [MAP_SIZE];
-byte   _map3_hi             [MAP_SIZE];
-byte   _map_owner           [MAP_SIZE];
-uint16 _map2                [MAP_SIZE];
-byte   _map_extra_bits      [MAP_SIZE / 4];
+
+void InitMap(uint log_x, uint log_y)
+{
+	uint map_size;
+
+	assert(log_x <= 15 && log_y <= 15);
+
+	_map_log_x = log_x;
+	_map_log_y = log_y;
+
+	map_size = MapSize();
+
+	_map_type_and_height =
+		realloc(_map_type_and_height, map_size * sizeof(_map_type_and_height[0]));
+	_map_owner = realloc(_map_owner, map_size * sizeof(_map_owner[0]));
+	_map2      = realloc(_map2,      map_size * sizeof(_map2[0]));
+	_map3_lo   = realloc(_map3_lo,   map_size * sizeof(_map3_lo[0]));
+	_map3_hi   = realloc(_map3_hi,   map_size * sizeof(_map3_hi[0]));
+	_map5      = realloc(_map5,      map_size * sizeof(_map5[0]));
+	_map_extra_bits =
+		realloc(_map_extra_bits, map_size * sizeof(_map_extra_bits[0] / 4));
+
+	// XXX TODO handle memory shortage more gracefully
+	if (_map_type_and_height == NULL ||
+			_map_owner           == NULL ||
+			_map2                == NULL ||
+			_map3_lo             == NULL ||
+			_map3_hi             == NULL ||
+			_map5                == NULL ||
+			_map_extra_bits      == NULL)
+		error("Failed to allocate memory for the map");
+}
 
 
 #ifdef _DEBUG
--- a/map.h	Sat Jan 29 19:41:44 2005 +0000
+++ b/map.h	Sat Jan 29 19:45:14 2005 +0000
@@ -8,13 +8,15 @@
 
 #define TILE_MASK(x) ((x) & ((1 << (MapLogX() + MapLogY())) - 1))
 
-extern byte   _map_type_and_height[];
-extern byte   _map5[];
-extern byte   _map3_lo[];
-extern byte   _map3_hi[];
-extern byte   _map_owner[];
-extern uint16 _map2[];
-extern byte   _map_extra_bits[];
+extern byte   *_map_type_and_height;
+extern byte   *_map_owner;
+extern uint16 *_map2;
+extern byte   *_map3_lo;
+extern byte   *_map3_hi;
+extern byte   *_map5;
+extern byte   *_map_extra_bits;
+
+void InitMap(uint log_x, uint log_y);
 
 // binary logarithm of the map size, try to avoid using this one
 static inline uint MapLogX(void)  { extern uint _map_log_x; return _map_log_x; }
--- a/misc.c	Sat Jan 29 19:41:44 2005 +0000
+++ b/misc.c	Sat Jan 29 19:45:14 2005 +0000
@@ -165,7 +165,7 @@
 void InitializeDock(void);
 void InitializeDockGui(void);
 void InitializeIndustries(void);
-void InitializeLandscape(void);
+void InitializeLandscape(uint log_x, uint log_y);
 void InitializeTowns(void);
 void InitializeTrees(void);
 void InitializeSigns(void);
@@ -187,7 +187,7 @@
 
 void ConvertGroundTilesIntoWaterTiles(void);
 
-void InitializeGame(void)
+void InitializeGame(uint log_x, uint log_y)
 {
 	// Initialize the autoreplace array. Needs to be cleared between each game
 	uint i;
@@ -217,7 +217,7 @@
 	InitializeOrders();
 
 	InitNewsItemStructs();
-	InitializeLandscape();
+	InitializeLandscape(log_x, log_y);
 	InitializeClearLand();
 	InitializeRail();
 	InitializeRailGui();
@@ -248,7 +248,7 @@
 	ResetObjectToPlace();
 }
 
-void GenerateWorld(int mode)
+void GenerateWorld(int mode, uint log_x, uint log_y)
 {
 	int i;
 
@@ -256,7 +256,7 @@
 	_current_player = OWNER_NONE;
 
 	_generating_world = true;
-	InitializeGame();
+	InitializeGame(log_x, log_y);
 	SetObjectToPlace(1, 0, 0, 0);
 
 	// Must start economy early because of the costs.
@@ -860,6 +860,28 @@
 	SlGlobList(_view_desc);
 }
 
+static uint32 _map_dim_x;
+static uint32 _map_dim_y;
+
+static const SaveLoadGlobVarList _map_dimensions[] = {
+	{&_map_dim_x, SLE_UINT32, 6, 255},
+	{&_map_dim_y, SLE_UINT32, 6, 255},
+	{NULL, 0, 0, 0}
+};
+
+static void Save_MAPSIZE(void)
+{
+	_map_dim_x = MapLogX();
+	_map_dim_y = MapLogY();
+	SlGlobList(_map_dimensions);
+}
+
+static void Load_MAPSIZE(void)
+{
+	SlGlobList(_map_dimensions);
+	InitMap(_map_dim_x, _map_dim_y);
+}
+
 static void SaveLoad_MAPT(void)
 {
   SlArray(_map_type_and_height, MapSize(), SLE_UINT8);
@@ -928,6 +950,7 @@
 
 
 const ChunkHandler _misc_chunk_handlers[] = {
+	{ 'MAPS', Save_MAPSIZE,  Load_MAPSIZE,  CH_RIFF },
 	{ 'MAPT', SaveLoad_MAPT, SaveLoad_MAPT, CH_RIFF },
 	{ 'MAP2', SaveLoad_MAP2, SaveLoad_MAP2, CH_RIFF },
 	{ 'M3LO', SaveLoad_M3LO, SaveLoad_M3LO, CH_RIFF },
--- a/saveload.c	Sat Jan 29 19:41:44 2005 +0000
+++ b/saveload.c	Sat Jan 29 19:45:14 2005 +0000
@@ -1007,7 +1007,7 @@
 }
 
 // actual loader/saver function
-extern void InitializeGame(void);
+void InitializeGame(uint log_x, uint log_y);
 extern bool AfterLoadGame(uint version);
 extern void BeforeSaveGame(void);
 extern bool LoadOldSaveGame(const char *file);
@@ -1021,7 +1021,7 @@
 
 	// old style load
 	if (mode == SL_OLD_LOAD) {
-		InitializeGame();
+		InitializeGame(8, 8);
 		if (!LoadOldSaveGame(filename)) return SL_REINIT;
 		AfterLoadGame(0);
 		return SL_OK;
@@ -1126,7 +1126,10 @@
 
 		if (!fmt->init_read()) goto init_err;
 		// Clear everything
-		InitializeGame();
+		/* Set the current map to 256x256, in case of an old map.
+		 * Else MAPS will read the right information */
+		InitializeGame(8, 8);
+
 		SlLoadChunks();
 		fmt->uninit_read();
 	}
--- a/settings.c	Sat Jan 29 19:41:44 2005 +0000
+++ b/settings.c	Sat Jan 29 19:45:14 2005 +0000
@@ -909,6 +909,9 @@
 
 	{"ainew_active",				SDT_BOOL,		(void*)false, &_patches.ainew_active,					NULL},
 
+	{"map_x", SDT_UINT32, (void*)8, &_patches.map_x, NULL},
+	{"map_y", SDT_UINT32, (void*)8, &_patches.map_y, NULL},
+
 	{NULL,									0,					NULL,					NULL,																						NULL}
 };
 
--- a/settings_gui.c	Sat Jan 29 19:41:44 2005 +0000
+++ b/settings_gui.c	Sat Jan 29 19:45:14 2005 +0000
@@ -622,6 +622,9 @@
 	{PE_UINT8,	PF_0ISDIS | PF_PLAYERBASED, STR_CONFIG_PATCHES_SNAP_RADIUS, "window_snap_radius", &_patches.window_snap_radius,     1, 32,  1, NULL},
 	{PE_BOOL,		PF_PLAYERBASED, STR_CONFIG_PATCHES_INVISIBLE_TREES,	"invisible_trees", &_patches.invisible_trees,					0,  1,  1, &InvisibleTreesActive},
 	{PE_BOOL,		PF_PLAYERBASED, STR_CONFIG_PATCHES_POPULATION_IN_LABEL, "population_in_label", &_patches.population_in_label, 0, 1, 1, &PopulationInLabelActive},
+
+	{PE_INT32, 0, STR_CONFIG_PATCHES_MAP_X, "map_x", &_patches.map_x, 6, 11, 1, NULL},
+	{PE_INT32, 0, STR_CONFIG_PATCHES_MAP_Y, "map_y", &_patches.map_y, 6, 11, 1, NULL},
 };
 
 static const PatchEntry _patches_construction[] = {
--- a/ttd.c	Sat Jan 29 19:41:44 2005 +0000
+++ b/ttd.c	Sat Jan 29 19:45:14 2005 +0000
@@ -32,7 +32,7 @@
 #include <stdarg.h>
 
 void IncreaseSpriteLRU(void);
-void GenerateWorld(int mode);
+void GenerateWorld(int mode, uint log_x, uint log_y);
 void CallLandscapeTick(void);
 void IncreaseDate(void);
 void RunOtherPlayersLoop(void);
@@ -534,6 +534,8 @@
 	GfxLoadSprites();
 	LoadStringWidthTable();
 
+	GenerateWorld(1, 6, 6); // Make the viewport initialization happy
+
 	// Setup main window
 	InitWindowSystem();
 	SetupColorsAndInitialWindow();
@@ -545,7 +547,7 @@
 		sprintf(filename, "%sopntitle.dat",  _path.second_data_dir);
 		if (SaveOrLoad(filename, SL_LOAD) != SL_OK)
 #endif
-			GenerateWorld(1); // if failed loading, make empty world.
+			GenerateWorld(1, 6, 6); // if failed loading, make empty world.
 	}
 	_opt.currency = _new_opt.currency;
 
@@ -804,7 +806,7 @@
 	SetupColorsAndInitialWindow();
 
 	// Randomize world
-	GenerateWorld(0);
+	GenerateWorld(0, _patches.map_x, _patches.map_y);
 
 	// In a dedicated server, the server does not play
 	if (_network_dedicated) {
@@ -836,7 +838,7 @@
 	SetupColorsAndInitialWindow();
 
 	// Startup the game system
-	GenerateWorld(1);
+	GenerateWorld(1, _patches.map_x, _patches.map_y);
 
 	_local_player = OWNER_NONE;
 	MarkWholeScreenDirty();
@@ -1022,7 +1024,7 @@
 		break;
 
 	case SM_GENRANDLAND:
-		GenerateWorld(2);
+		GenerateWorld(2, _patches.map_x, _patches.map_y);
 		// XXX: set date
 		_local_player = OWNER_NONE;
 		MarkWholeScreenDirty();
--- a/variables.h	Sat Jan 29 19:41:44 2005 +0000
+++ b/variables.h	Sat Jan 29 19:45:14 2005 +0000
@@ -180,6 +180,9 @@
 	byte wait_oneway_signal;	//waitingtime in days before a oneway signal
 	byte wait_twoway_signal;	//waitingtime in days before a twoway signal
 
+	uint map_x; // Size of map
+	uint map_y;
+
 	byte drag_signals_density; // many signals density
 	bool ainew_active;  // Is the new AI active?