1
0
Fork 0

(svn r1790) Make CmdPlantTree() and related functions more safe and (hopefully) more readable:

- check if end tile is on the map
- uint -> TileIndex
- if cascades -> switch
release/0.4.5
tron 2005-02-04 16:50:18 +00:00
parent 093ae2b4b4
commit e2dab60ad1
1 changed files with 104 additions and 79 deletions

View File

@ -8,27 +8,29 @@
#include "town.h" #include "town.h"
#include "sound.h" #include "sound.h"
static int GetRandomTreeType(uint tile, uint seed) static int GetRandomTreeType(TileIndex tile, uint seed)
{ {
byte i; switch (_opt.landscape) {
case LT_NORMAL:
return seed * 12 >> 8;
if (_opt.landscape == LT_NORMAL) { case LT_HILLY:
return seed * 12 >> 8; return (seed >> 5) + 12;
} else if (_opt.landscape == LT_HILLY) {
return (seed >> 5) + 12; case LT_DESERT:
} else if (_opt.landscape == LT_DESERT) { switch (GetMapExtraBits(tile)) {
i = GetMapExtraBits(tile); case 0:
if (i == 0) { return (seed >> 6) + 28;
return (seed >> 6) + 28;
} else if (i == 1) { case 1:
if (seed > 12) return (seed > 12) ? -1 : 27;
return -1;
return 27; default:
} else { return (seed * 7 >> 8) + 20;
return (seed * 7 >> 8) + 20; }
}
} else { default:
return (seed * 9 >> 8) + 32; return (seed * 9 >> 8) + 32;
} }
} }
@ -148,10 +150,13 @@ void GenerateTrees(void)
int32 CmdPlantTree(int ex, int ey, uint32 flags, uint32 p1, uint32 p2) int32 CmdPlantTree(int ex, int ey, uint32 flags, uint32 p1, uint32 p2)
{ {
TileInfo ti;
int32 cost; int32 cost;
int sx,sy,x,y; int sx;
int treetype; int sy;
int x;
int y;
if (p2 >= MapSize()) return CMD_ERROR;
SET_EXPENSES_TYPE(EXPENSES_OTHER); SET_EXPENSES_TYPE(EXPENSES_OTHER);
@ -163,69 +168,89 @@ int32 CmdPlantTree(int ex, int ey, uint32 flags, uint32 p1, uint32 p2)
cost = 0; // total cost cost = 0; // total cost
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;
FindLandscapeHeight(&ti, x, y); FindLandscapeHeight(&ti, x, y);
if (!EnsureNoVehicle(ti.tile)) if (!EnsureNoVehicle(ti.tile))
continue; continue;
if (ti.type == MP_TREES) { switch (ti.type) {
// no more space for trees? case MP_TREES:
if (_game_mode != GM_EDITOR && (ti.map5 & 0xC0) == 0xC0) { // no more space for trees?
_error_message = STR_2803_TREE_ALREADY_HERE; if (_game_mode != GM_EDITOR && (ti.map5 & 0xC0) == 0xC0) {
continue; _error_message = STR_2803_TREE_ALREADY_HERE;
} continue;
}
if (flags & DC_EXEC) { if (flags & DC_EXEC) {
_map5[ti.tile] = ti.map5 + 0x40; _map5[ti.tile] = ti.map5 + 0x40;
MarkTileDirtyByTile(ti.tile); MarkTileDirtyByTile(ti.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;
} else { break;
if (ti.type != MP_CLEAR || _map_owner[ti.tile] != OWNER_NONE) {
case MP_CLEAR:
if (_map_owner[ti.tile] != OWNER_NONE) {
_error_message = STR_2804_SITE_UNSUITABLE;
continue;
}
// it's expensive to clear farmland
if ((ti.map5 & 0x1F) == 0xF)
cost += _price.clear_3;
else if ((ti.map5 & 0x1C) == 8)
cost += _price.clear_2;
if (flags & DC_EXEC) {
int treetype;
int m2;
if (_game_mode != GM_EDITOR && _current_player < MAX_PLAYERS) {
Town *t = ClosestTownFromTile(ti.tile, _patches.dist_local_authority);
if (t != NULL)
ChangeTownRating(t, RATING_TREE_UP_STEP, RATING_TREE_MAXIMUM);
}
switch (ti.map5 & 0x1C) {
case 0x04:
m2 = 16;
break;
case 0x10:
m2 = ((ti.map5 & 3) << 6) | 0x20;
break;
default:
m2 = 0;
break;
}
treetype = p1;
if (treetype == -1) {
treetype = GetRandomTreeType(ti.tile, Random() >> 24);
if (treetype == -1) treetype = 27;
}
ModifyTile(ti.tile,
MP_SETTYPE(MP_TREES) |
MP_MAP2 | MP_MAP3LO | MP_MAP3HI_CLEAR | MP_MAP5,
m2, /* map2 */
treetype, /* map3lo */
_game_mode == GM_EDITOR ? 3 : 0 /* map5 */
);
if (_game_mode == GM_EDITOR && IS_BYTE_INSIDE(treetype, 0x14, 0x1B))
SetMapExtraBits(ti.tile, 2);
}
cost += _price.build_trees;
break;
default:
_error_message = STR_2804_SITE_UNSUITABLE; _error_message = STR_2804_SITE_UNSUITABLE;
continue; break;
}
// it's expensive to clear farmland
if ((ti.map5 & 0x1F) == 0xF) cost += _price.clear_3;
else if ((ti.map5 & 0x1C) == 8) cost += _price.clear_2;
if (flags & DC_EXEC) {
int m2;
if (_game_mode != GM_EDITOR && _current_player < MAX_PLAYERS) {
Town *t = ClosestTownFromTile(ti.tile, _patches.dist_local_authority);
if (t != NULL)
ChangeTownRating(t, RATING_TREE_UP_STEP, RATING_TREE_MAXIMUM);
}
m2 = 0;
if ( (ti.map5 & 0x1C) == 4 ) {
m2 = 16;
} else if ( (ti.map5 & 0x1C) == 16 ) {
m2 = ((ti.map5 & 3) << 6) | 0x20;
}
treetype = p1;
if (treetype == -1) {
treetype = GetRandomTreeType(ti.tile, Random()>>24);
if (treetype==-1) treetype=27;
}
ModifyTile(ti.tile,
MP_SETTYPE(MP_TREES) |
MP_MAP2 | MP_MAP3LO | MP_MAP3HI_CLEAR | MP_MAP5,
m2, /* map2 */
treetype, /* map3lo */
_game_mode == GM_EDITOR ? 3 : 0 /* map5 */
);
if (_game_mode == GM_EDITOR && IS_BYTE_INSIDE(treetype, 0x14, 0x1B)) {
SetMapExtraBits(ti.tile, 2);
}
}
cost += _price.build_trees;
} }
} }
} }