(svn r2598) Small cleanup, especially get rid of a FindLandscapeHeight(), because it was overkill
authortron
Sun, 17 Jul 2005 11:09:03 +0000
changeset 2088 f290b54c97cb
parent 2087 bedb167de57b
child 2089 36f1c7177730
(svn r2598) Small cleanup, especially get rid of a FindLandscapeHeight(), because it was overkill
tree_cmd.c
--- a/tree_cmd.c	Sun Jul 17 10:18:23 2005 +0000
+++ b/tree_cmd.c	Sun Jul 17 11:09:03 2005 +0000
@@ -20,14 +20,9 @@
 
 		case LT_DESERT:
 			switch (GetMapExtraBits(tile)) {
-				case 0:
-					return (seed >> 6) + 28;
-
-				case 1:
-					return (seed > 12) ? -1 : 27;
-
-				default:
-					return (seed * 7 >> 8) + 20;
+				case 0:  return (seed >> 6) + 28;
+				case 1:  return (seed > 12) ? -1 : 27;
+				default: return (seed * 7 >> 8) + 20;
 			}
 
 		default:
@@ -37,12 +32,14 @@
 
 static void PlaceTree(TileIndex tile, uint32 r, byte m5_or)
 {
-	int tree = GetRandomTreeType(tile, (r >> 24));
+	int tree = GetRandomTreeType(tile, GB(r, 24, 8));
 	byte m5;
 
 	if (tree >= 0) {
-		m5 = (byte)(r >> 16);
-		if ((m5 & 0x7) == 7) m5--; // there is no growth state 7
+		SetTileType(tile, MP_TREES);
+
+		m5 = GB(r, 16, 8);
+		if (GB(m5, 0, 3) == 7) m5--; // there is no growth state 7
 
 		_m[tile].m5 = m5 & 0x07;	// growth state;
 		_m[tile].m5 |=  m5 & 0xC0;	// amount of trees
@@ -53,35 +50,23 @@
 		// above snowline?
 		if (_opt.landscape == LT_HILLY && GetTileZ(tile) > _opt.snow_line) {
 			_m[tile].m2 = 0xE0;	// set land type to snow
-			_m[tile].m2 |= (byte)(r >> 24)&0x07; // randomize counter
+			_m[tile].m2 |= GB(r, 24, 3); // randomize counter
+		} else {
+			_m[tile].m2 = GB(r, 24, 5); // randomize counter and ground
 		}
-		else
-		{
-			_m[tile].m2 = (byte)(r >> 24)&0x1F; // randomize counter and ground
-		}
-
-
-		// make it tree class
-		SetTileType(tile, MP_TREES);
 	}
 }
 
 static void DoPlaceMoreTrees(TileIndex tile)
 {
-	int i = 1000;
-	int x,y;
-	int dist;
+	uint i;
 
-	do {
+	for (i = 0; i < 1000; i++) {
 		uint32 r = Random();
-		TileIndex cur_tile;
-
-		x = (r & 0x1F) - 16;
-		y = ((r>>8) & 0x1F) - 16;
-
-		dist = myabs(x) + myabs(y);
-
-		cur_tile = TILE_MASK(tile + TileDiffXY(x, y));
+		int x = GB(r, 0, 5) - 16;
+		int y = GB(r, 8, 5) - 16;
+		uint dist = myabs(x) + myabs(y);
+		TileIndex cur_tile = TILE_MASK(tile + TileDiffXY(x, y));
 
 		/* Only on tiles within 13 squares from tile,
 		    on clear tiles, and NOT on farm-tiles or rocks */
@@ -89,12 +74,12 @@
 			 (_m[cur_tile].m5 & 0x1F) != 0x0F && (_m[cur_tile].m5 & 0x1C) != 8) {
 			PlaceTree(cur_tile, r, dist <= 6 ? 0xC0 : 0);
 		}
-	} while (--i);
+	}
 }
 
 static void PlaceMoreTrees(void)
 {
-	int i = ScaleByMapSize((Random() & 0x1F) + 25);
+	uint i = ScaleByMapSize((Random() & 0x1F) + 25);
 	do {
 		DoPlaceMoreTrees(RandomTile());
 	} while (--i);
@@ -102,14 +87,12 @@
 
 void PlaceTreesRandomly(void)
 {
-	int i;
-	uint32 r;
-	TileIndex tile;
+	uint i;
 
 	i = ScaleByMapSize(1000);
 	do {
-		r = Random();
-		tile = RandomTileSeed(r);
+		uint32 r = Random();
+		TileIndex tile = RandomTileSeed(r);
 		/* Only on clear tiles, and NOT on farm-tiles or rocks */
 		if (IsTileType(tile, MP_CLEAR) && (_m[tile].m5 & 0x1F) != 0x0F && (_m[tile].m5 & 0x1C) != 8) {
 			PlaceTree(tile, r, 0);
@@ -121,8 +104,8 @@
 		i = ScaleByMapSize(15000);
 
 		do {
-			r = Random();
-			tile = RandomTileSeed(r);
+			uint32 r = Random();
+			TileIndex tile = RandomTileSeed(r);
 			if (IsTileType(tile, MP_CLEAR) && GetMapExtraBits(tile) == 2) {
 				PlaceTree(tile, r, 0);
 			}
@@ -132,16 +115,13 @@
 
 void GenerateTrees(void)
 {
-	int i;
+	uint i;
 
-	if (_opt.landscape != LT_CANDY) {
-		PlaceMoreTrees();
+	if (_opt.landscape != LT_CANDY) PlaceMoreTrees();
+
+	for (i = _opt.landscape == LT_HILLY ? 15 : 6; i != 0; i--) {
+		PlaceTreesRandomly();
 	}
-
-	i = _opt.landscape == LT_HILLY ? 15 : 6;
-	do {
-		PlaceTreesRandomly();
-	} while (--i);
 }
 
 /** Plant a tree.
@@ -173,38 +153,36 @@
 
 	for (x = sx; x <= ex; x += 16) {
 		for (y = sy; y <= ey; y += 16) {
-			TileInfo ti;
+			TileIndex tile = TileXY(x, y);
 
-			FindLandscapeHeight(&ti, x, y);
-			if (!EnsureNoVehicle(ti.tile))
-				continue;
+			if (!EnsureNoVehicle(tile)) continue;
 
-			switch (ti.type) {
+			switch (GetTileType(tile)) {
 				case MP_TREES:
 					// no more space for trees?
-					if (_game_mode != GM_EDITOR && (ti.map5 & 0xC0) == 0xC0) {
+					if (_game_mode != GM_EDITOR && (_m[tile].m5 & 0xC0) == 0xC0) {
 						_error_message = STR_2803_TREE_ALREADY_HERE;
 						continue;
 					}
 
 					if (flags & DC_EXEC) {
-						_m[ti.tile].m5 = ti.map5 + 0x40;
-						MarkTileDirtyByTile(ti.tile);
+						_m[tile].m5 += 0x40;
+						MarkTileDirtyByTile(tile);
 					}
 					// 2x as expensive to add more trees to an existing tile
 					cost += _price.build_trees * 2;
 					break;
 
 				case MP_CLEAR:
-					if (!IsTileOwner(ti.tile, OWNER_NONE)) {
+					if (!IsTileOwner(tile, OWNER_NONE)) {
 						_error_message = STR_2804_SITE_UNSUITABLE;
 						continue;
 					}
 
 					// it's expensive to clear farmland
-					if ((ti.map5 & 0x1F) == 0xF)
+					if ((_m[tile].m5 & 0x1F) == 0xF)
 						cost += _price.clear_3;
-					else if ((ti.map5 & 0x1C) == 8)
+					else if ((_m[tile].m5 & 0x1C) == 8)
 						cost += _price.clear_2;
 
 					if (flags & DC_EXEC) {
@@ -212,18 +190,18 @@
 						int m2;
 
 						if (_game_mode != GM_EDITOR && _current_player < MAX_PLAYERS) {
-							Town *t = ClosestTownFromTile(ti.tile, _patches.dist_local_authority);
+							Town *t = ClosestTownFromTile(tile, _patches.dist_local_authority);
 							if (t != NULL)
 								ChangeTownRating(t, RATING_TREE_UP_STEP, RATING_TREE_MAXIMUM);
 						}
 
-						switch (ti.map5 & 0x1C) {
+						switch (_m[tile].m5 & 0x1C) {
 							case 0x04:
 								m2 = 16;
 								break;
 
 							case 0x10:
-								m2 = ((ti.map5 & 3) << 6) | 0x20;
+								m2 = ((_m[tile].m5 & 3) << 6) | 0x20;
 								break;
 
 							default:
@@ -233,11 +211,11 @@
 
 						treetype = p1;
 						if (treetype == -1) {
-							treetype = GetRandomTreeType(ti.tile, Random() >> 24);
+							treetype = GetRandomTreeType(tile, Random() >> 24);
 							if (treetype == -1) treetype = 27;
 						}
 
-						ModifyTile(ti.tile,
+						ModifyTile(tile,
 							MP_SETTYPE(MP_TREES) |
 							MP_MAP2 | MP_MAP3LO | MP_MAP3HI_CLEAR | MP_MAP5,
 							m2, /* map2 */
@@ -246,7 +224,7 @@
 						);
 
 						if (_game_mode == GM_EDITOR && IS_BYTE_INSIDE(treetype, 0x14, 0x1B))
-							SetMapExtraBits(ti.tile, 2);
+							SetMapExtraBits(tile, 2);
 					}
 					cost += _price.build_trees;
 					break;
@@ -272,13 +250,12 @@
 	const uint32 *s;
 	const byte *d;
 	byte z;
-	TreeListEnt te[4];
 
 	m2 = _m[ti->tile].m2;
 
-	if ( (m2&0x30) == 0) {
+	if ((m2 & 0x30) == 0) {
 		DrawClearLandTile(ti, 3);
-	} else if ((m2&0x30) == 0x20) {
+	} else if ((m2 & 0x30) == 0x20) {
 		DrawGroundSprite(_tree_sprites_1[m2 >> 6] + _tileh_to_sprite[ti->tileh]);
 	} else {
 		DrawHillyLandTile(ti);
@@ -297,16 +274,16 @@
 		uint16 tmp = ti->x;
 		uint index;
 
-		tmp = (tmp >> 2) | (tmp << 14);
+		tmp = ROR(tmp, 2);
 		tmp -= ti->y;
-		tmp = (tmp >> 3) | (tmp << 13);
+		tmp = ROR(tmp, 3);
 		tmp -= ti->x;
-		tmp = (tmp >> 1) | (tmp << 15);
+		tmp = ROR(tmp, 1);
 		tmp += ti->y;
 
-		d = _tree_layout_xy[(tmp & 0x30) >> 4];
+		d = _tree_layout_xy[GB(tmp, 4, 2)];
 
-		index = ((tmp>>6)&3) + (_m[ti->tile].m3<<2);
+		index = GB(tmp, 6, 2) + (_m[ti->tile].m3 << 2);
 
 		/* different tree styles above one of the grounds */
 		if ((m2 & 0xB0) == 0xA0 && index >= 48 && index < 80)
@@ -318,21 +295,21 @@
 
 	StartSpriteCombine();
 
-	if (!(_display_opt & DO_TRANS_BUILDINGS) || !_patches.invisible_trees)
-	{
-		int i;
+	if (!(_display_opt & DO_TRANS_BUILDINGS) || !_patches.invisible_trees) {
+		TreeListEnt te[4];
+		uint i;
 
 		/* put the trees to draw in a list */
 		i = (ti->map5 >> 6) + 1;
 		do {
-			uint32 image = s[0] + (--i==0 ? (ti->map5 & 7) : 3);
+			uint32 image = s[0] + (--i == 0 ? (ti->map5 & 7) : 3);
 			if (_display_opt & DO_TRANS_BUILDINGS)
 				image = (image & 0x3FFF) | 0x3224000;
 			te[i].image = image;
 			te[i].x = d[0];
 			te[i].y = d[1];
 			s++;
-			d+=2;
+			d += 2;
 		} while (i);
 
 		/* draw them in a sorted way */
@@ -342,14 +319,13 @@
 
 			i = (ti->map5 >> 6) + 1;
 			do {
-				if (te[--i].image!=0 && (byte)(te[i].x + te[i].y) < min) {
+				if (te[--i].image != 0 && (byte)(te[i].x + te[i].y) < min) {
 					min = te[i].x + te[i].y;
 					tep = &te[i];
 				}
 			} while (i);
 
-			if (tep == NULL)
-				break;
+			if (tep == NULL) break;
 
 			AddSortableSpriteToDraw(tep->image, ti->x + tep->x, ti->y + tep->y, 5, 5, 0x10, z);
 			tep->image = 0;
@@ -423,12 +399,11 @@
 {
 	byte b;
 
-	if ((b=GetMapExtraBits(tile)) == 2) {
-		uint32 r;
+	b = GetMapExtraBits(tile);
+	if (b == 2) {
+		uint32 r = Random();
 
-		if (CHANCE16I(1,200,r=Random())) {
-			SndPlayTileFx(_desert_sounds[(r >> 16) & 3], tile);
-		}
+		if (CHANCE16I(1,200, r)) SndPlayTileFx(_desert_sounds[GB(r, 16, 2)], tile);
 	} else if (b == 1) {
 		if ((_m[tile].m2 & 0x30) != 0x20) {
 			_m[tile].m2 &= 0xF;
@@ -450,29 +425,25 @@
 
 	if (k < -8) {
 		/* snow_m2_down */
-		if ((tmp&0x30) != 0x20)
-			return;
+		if ((tmp & 0x30) != 0x20) return;
 		m2 = 0;
 	} else if (k == -8) {
 		/* snow_m1 */
 		m2 = 0x20;
-		if (tmp == m2)
-			return;
+		if (tmp == m2) return;
 	} else if (k < 8) {
 		/* snow_0 */
 		m2 = 0x60;
-		if (tmp == m2)
-			return;
+		if (tmp == m2) return;
 	} else if (k == 8) {
 		/* snow_p1 */
 		m2 = 0xA0;
-		if (tmp == m2)
-			return;
+		if (tmp == m2) return;
 	} else {
 		/* snow_p2_up */
 		if (tmp == 0xC0) {
-			uint32 r;
-			if (CHANCE16I(1,200,r=Random())) {
+			uint32 r = Random();
+			if (CHANCE16I(1,200,r)) {
 				SndPlayTileFx((r & 0x80000000) ? SND_39_HEAVY_WIND : SND_34_WIND, tile);
 			}
 			return;
@@ -513,15 +484,14 @@
 	/* increase counter */
 	{
 		uint16 m2 = _m[tile].m2;
-		_m[tile].m2 = m2 = (m2 & 0xF0) | ((m2+1)&0xF);
-		if (m2 & 0xF)
-			return;
+		_m[tile].m2 = m2 = (m2 & 0xF0) | ((m2 + 1) & 0xF);
+		if ((m2 & 0xF) != 0) return;
 	}
 
 	m5 = _m[tile].m5;
-	if ((m5&7) == 3) {
+	if (GB(m5, 0, 3) == 3) {
 		/* regular sized tree */
-		if (_opt.landscape == LT_DESERT && _m[tile].m3!=0x1B && GetMapExtraBits(tile)==1) {
+		if (_opt.landscape == LT_DESERT && _m[tile].m3 != 0x1B && GetMapExtraBits(tile) == 1) {
 			m5++; /* start destructing */
 		} else {
 			switch(Random() & 0x7) {
@@ -541,16 +511,14 @@
 
 				tile += ToTileIndexDiff(_tileloop_trees_dir[Random() & 7]);
 
-				if (!IsTileType(tile, MP_CLEAR))
-					return;
+				if (!IsTileType(tile, MP_CLEAR)) return;
 
 				if ( (_m[tile].m5 & 0x1C) == 4) {
 					_m[tile].m2 = 0x10;
 				} else if ((_m[tile].m5 & 0x1C) == 16) {
 					_m[tile].m2 = ((_m[tile].m5 & 3) << 6) | 0x20;
 				} else {
-					if ((_m[tile].m5 & 0x1F) != 3)
-						return;
+					if ((_m[tile].m5 & 0x1F) != 3) return;
 					_m[tile].m2 = 0;
 				}
 
@@ -566,9 +534,9 @@
 				return;
 			}
 		}
-	} else if ((m5&7) == 6) {
+	} else if (GB(m5, 0, 3) == 6) {
 		/* final stage of tree destruction */
-		if (m5 & 0xC0) {
+		if (GB(m5, 6, 2) != 0) {
 			/* more than one tree, delete it? */
 			m5 = ((m5 - 6) - 0x40) + 3;
 		} else {
@@ -577,9 +545,9 @@
 
 			m5 = 3;
 			m2 = _m[tile].m2;
-			if ((m2&0x30) != 0) { // on snow/desert or rough land
+			if ((m2 & 0x30) != 0) { // on snow/desert or rough land
 				m5 = (m2 >> 6) | 0x10;
-				if ((m2&0x30) != 0x20) // if not on snow/desert, then on rough land
+				if ((m2 & 0x30) != 0x20) // if not on snow/desert, then on rough land
 					m5 = 7;
 			}
 			SetTileOwner(tile, OWNER_NONE);
@@ -602,10 +570,10 @@
 
 	/* place a tree at a random rainforest spot */
 	if (_opt.landscape == LT_DESERT &&
-			(r=Random(),tile=RandomTileSeed(r),GetMapExtraBits(tile)==2) &&
+			(r = Random(), tile = RandomTileSeed(r), GetMapExtraBits(tile) == 2) &&
 			IsTileType(tile, MP_CLEAR) &&
-			(m=_m[tile].m5&0x1C, m<=4) &&
-			(tree=GetRandomTreeType(tile, r>>24)) >= 0) {
+			(m = _m[tile].m5 & 0x1C, m <= 4) &&
+			(tree = GetRandomTreeType(tile, r >> 24)) >= 0) {
 
 		ModifyTile(tile,
 			MP_SETTYPE(MP_TREES) |
@@ -618,15 +586,14 @@
 	}
 
 	// byte underflow
-	if (--_trees_tick_ctr)
-		return;
+	if (--_trees_tick_ctr != 0) return;
 
 	/* place a tree at a random spot */
 	r = Random();
 	tile = TILE_MASK(r);
 	if (IsTileType(tile, MP_CLEAR) &&
-			(m=_m[tile].m5&0x1C, m==0 || m==4 || m==0x10) &&
-			(tree=GetRandomTreeType(tile, r>>24)) >= 0) {
+			(m = _m[tile].m5 & 0x1C, m == 0 || m == 4 || m == 0x10) &&
+			(tree = GetRandomTreeType(tile, r >> 24)) >= 0) {
 		int m2;
 
 		if (m == 0) {