(svn r2598) Small cleanup, especially get rid of a FindLandscapeHeight(), because it was overkill

This commit is contained in:
tron 2005-07-17 11:09:03 +00:00
parent b72e1fb67d
commit 2469cedd15

View File

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