(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
authortron
Fri, 28 Jan 2005 15:31:04 +0000
changeset 1202 7d8b86bd8ba2
parent 1201 790c46deeba4
child 1203 c95a16972da5
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
These scale a number relative to the map size/circumference.
Use them to scale the amount of map objects.
Of course at the moment they return just the input, because there are no bigger/smaller maps yet.
clear_cmd.c
industry_cmd.c
landscape.c
macros.h
map.c
map.h
town_cmd.c
tree_cmd.c
unmovable_cmd.c
--- a/clear_cmd.c	Fri Jan 28 12:21:04 2005 +0000
+++ b/clear_cmd.c	Fri Jan 28 15:31:04 2005 +0000
@@ -740,7 +740,7 @@
 	uint32 r;
 
 	/* add hills */
-	i = (Random() & 0x3FF) | 0x400;
+	i = ScaleByMapSize((Random() & 0x3FF) + 0x400);
 	do {
 		tile = TILE_MASK(Random());
 		if (IsTileType(tile, MP_CLEAR))
@@ -748,7 +748,7 @@
 	} while (--i);
 
 	/* add grey squares */
-	i = (Random() & 0x7F) | 0x80;
+	i = ScaleByMapSize((Random() & 0x7F) + 0x80);
 	do {
 		r = Random();
 		tile = TILE_MASK(r);
--- a/industry_cmd.c	Fri Jan 28 12:21:04 2005 +0000
+++ b/industry_cmd.c	Fri Jan 28 15:31:04 2005 +0000
@@ -1594,7 +1594,8 @@
 
 static void PlaceInitialIndustry(byte type, int amount)
 {
-	int num = _numof_industry_table[_opt.diff.number_industries][amount];
+	int num =
+		ScaleByMapSize(_numof_industry_table[_opt.diff.number_industries][amount]);
 
 	if (_opt.diff.number_industries != 0)
 	{
--- a/landscape.c	Fri Jan 28 12:21:04 2005 +0000
+++ b/landscape.c	Fri Jan 28 15:31:04 2005 +0000
@@ -656,38 +656,38 @@
 	uint32 r;
 
 	if (_opt.landscape == LT_HILLY) {
-		i = ((Random() & 0x7F) + 950) * LANDSCAPE_SIZE_FACTOR;
+		i = ScaleByMapSize((Random() & 0x7F) + 950);
 		do {
 			GenerateTerrain(2, 0);
 		} while (--i);
 
 		r = Random();
 		flag = (r & 3) | 4;
-		i = (((r >> 16) & 0x7F) + 450) * LANDSCAPE_SIZE_FACTOR;
+		i = ScaleByMapSize(((r >> 16) & 0x7F) + 450);
 		do {
 			GenerateTerrain(4, flag);
 		} while (--i);
 	} else if (_opt.landscape == LT_DESERT) {
-		i = ((Random()&0x7F) + 170) * LANDSCAPE_SIZE_FACTOR;
+		i = ScaleByMapSize((Random()&0x7F) + 170);
 		do {
 			GenerateTerrain(0, 0);
 		} while (--i);
 
 		r = Random();
 		flag = (r & 3) | 4;
-		i = (((r >> 16) & 0xFF) + 1700) * LANDSCAPE_SIZE_FACTOR;
+		i = ScaleByMapSize(((r >> 16) & 0xFF) + 1700);
 		do {
 			GenerateTerrain(0, flag);
 		} while (--i);
 
 		flag ^= 2;
 
-		i = ((Random() & 0x7F) + 410) * LANDSCAPE_SIZE_FACTOR;
+		i = ScaleByMapSize((Random() & 0x7F) + 410);
 		do {
 			GenerateTerrain(3, flag);
 		} while (--i);
 	} else {
-		i = ((Random() & 0x7F) + (3 - _opt.diff.quantity_sea_lakes)*256 + 100) * LANDSCAPE_SIZE_FACTOR;
+		i = ScaleByMapSize((Random() & 0x7F) + (3 - _opt.diff.quantity_sea_lakes) * 256 + 100);
 		do {
 			GenerateTerrain(_opt.diff.terrain_type, 0);
 		} while (--i);
--- a/macros.h	Fri Jan 28 12:21:04 2005 +0000
+++ b/macros.h	Fri Jan 28 15:31:04 2005 +0000
@@ -68,7 +68,6 @@
 //#define IS_INSIDE_1D(x, base, size) ((x) >= (base) && (x) < (base) + (size))
 #define IS_INSIDE_1D(x, base, size) ( (uint)((x) - (base)) < ((uint)(size)) )
 
-#define LANDSCAPE_SIZE_FACTOR 1
 
 enum {
 	CORRECT_Z_BITS = 1 << 1 | 1 << 2 | 1 << 3 | 1 << 4 | 1 << 5 | 1 << 6 | 1 << 7
--- a/map.c	Fri Jan 28 12:21:04 2005 +0000
+++ b/map.c	Fri Jan 28 15:31:04 2005 +0000
@@ -54,6 +54,28 @@
 #endif
 
 
+uint ScaleByMapSize(uint n)
+{
+	int shift = (int)MapLogX() - 8 + (int)MapLogY() - 8;
+
+	if (shift < 0)
+		return (n + (1 << -shift) - 1) >> -shift;
+	else
+		return n << shift;
+}
+
+
+uint ScaleByMapSize1D(uint n)
+{
+	int shift = ((int)MapLogX() - 8 + (int)MapLogY() - 8) / 2;
+
+	if (shift < 0)
+		return (n + (1 << -shift) - 1) >> -shift;
+	else
+		return n << shift;
+}
+
+
 const TileIndexDiffC _tileoffs_by_dir[] = {
 	{-1,  0},
 	{ 0,  1},
--- a/map.h	Fri Jan 28 12:21:04 2005 +0000
+++ b/map.h	Fri Jan 28 15:31:04 2005 +0000
@@ -26,6 +26,10 @@
 /* The number of tiles in the map */
 static inline uint MapSize(void) { return MapSizeX() * MapSizeY(); }
 
+// Scale a number relative to the map size
+uint ScaleByMapSize(uint); // Scale relative to the number of tiles
+uint ScaleByMapSize1D(uint); // Scale relative to the circumference of the map
+
 typedef uint32 TileIndex;
 
 
--- a/town_cmd.c	Fri Jan 28 12:21:04 2005 +0000
+++ b/town_cmd.c	Fri Jan 28 15:31:04 2005 +0000
@@ -1036,8 +1036,9 @@
 
 void GenerateTowns(void)
 {
-	uint n;
-	n = _num_initial_towns[_opt.diff.number_towns] + (Random()&7);
+	uint n =
+		ScaleByMapSize(_num_initial_towns[_opt.diff.number_towns] + (Random() & 7));
+
 	do CreateRandomTown(); while (--n);
 }
 
--- a/tree_cmd.c	Fri Jan 28 12:21:04 2005 +0000
+++ b/tree_cmd.c	Fri Jan 28 15:31:04 2005 +0000
@@ -90,7 +90,7 @@
 
 static void PlaceMoreTrees(void)
 {
-	int i = (Random() & 0x1F) + 25;
+	int i = ScaleByMapSize((Random() & 0x1F) + 25);
 	do {
 		DoPlaceMoreTrees(TILE_MASK(Random()));
 	} while (--i);
@@ -102,7 +102,7 @@
 	uint32 r;
 	uint tile;
 
-	i = 1000;
+	i = ScaleByMapSize(1000);
 	do {
 		r = Random();
 		tile = TILE_MASK(r);
@@ -114,7 +114,7 @@
 
 	/* place extra trees at rainforest area */
 	if (_opt.landscape == LT_DESERT) {
-		i = 15000;
+		i = ScaleByMapSize(15000);
 
 		do {
 			r = Random();
--- a/unmovable_cmd.c	Fri Jan 28 12:21:04 2005 +0000
+++ b/unmovable_cmd.c	Fri Jan 28 15:31:04 2005 +0000
@@ -258,8 +258,8 @@
 		return;
 
 	/* add radio tower */
-	i = 1000;
-	j = 40; // limit of 40 radio towers per world.
+	i = ScaleByMapSize(1000);
+	j = ScaleByMapSize(40); // maximum number of radio towers on the map
 	do {
 		r = Random();
 		tile = r % MapSize();
@@ -280,7 +280,7 @@
 		return;
 
 	/* add lighthouses */
-	i = (Random()&3) + 7;
+	i = ScaleByMapSize1D((Random() & 3) + 7);
 	do {
 restart:
 		r = Random();