1
0
Fork 0

(svn r3773) Shove some semantics down ottd's throat by replacing ints and magic numbers by enums and some related changes

release/0.5
tron 2006-03-06 13:11:08 +00:00
parent d793292778
commit f3fb21c96c
6 changed files with 79 additions and 62 deletions

View File

@ -215,7 +215,6 @@ bool CheckIfAuthorityAllows(TileIndex tile);
Town *ClosestTownFromTile(TileIndex tile, uint threshold); Town *ClosestTownFromTile(TileIndex tile, uint threshold);
void ChangeTownRating(Town *t, int add, int max); void ChangeTownRating(Town *t, int add, int max);
uint GetRoadBitsByTile(TileIndex tile);
int GetTownRadiusGroup(const Town *t, TileIndex tile); int GetTownRadiusGroup(const Town *t, TileIndex tile);
void ShowNetworkChatQueryWindow(byte desttype, byte dest); void ShowNetworkChatQueryWindow(byte desttype, byte dest);
void ShowNetworkGiveMoneyWindow(byte player); void ShowNetworkGiveMoneyWindow(byte player);

View File

@ -73,11 +73,6 @@ static bool CheckAllowRemoveRoad(TileIndex tile, RoadBits remove, bool* edge_roa
return true; return true;
} }
uint GetRoadBitsByTile(TileIndex tile)
{
uint32 r = GetTileTrackStatus(tile, TRANSPORT_ROAD);
return (byte)(r | (r >> 8));
}
/** Delete a piece of road. /** Delete a piece of road.
* @param x,y tile coordinates for road construction * @param x,y tile coordinates for road construction
@ -158,30 +153,33 @@ int32 CmdRemoveRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
switch (GetRoadType(ti.tile)) { switch (GetRoadType(ti.tile)) {
case ROAD_NORMAL: { case ROAD_NORMAL: {
byte c = pieces, t2; RoadBits present = GetRoadBits(ti.tile);
RoadBits c = pieces;
if (ti.tileh != 0 && (ti.map5 == ROAD_Y || ti.map5 == ROAD_X)) { if (ti.tileh != 0 && (present == ROAD_Y || present == ROAD_X)) {
c |= (c & 0xC) >> 2; c |= (c & 0xC) >> 2;
c |= (c & 0x3) << 2; c |= (c & 0x3) << 2;
} }
// limit the bits to delete to the existing bits. // limit the bits to delete to the existing bits.
if ((c &= ti.map5) == 0) goto return_error; c &= present;
if (c == 0) goto return_error;
// calculate the cost // calculate the cost
t2 = c;
cost = 0; cost = 0;
do { if (c & ROAD_NW) cost += _price.remove_road;
if (t2 & 1) cost += _price.remove_road; if (c & ROAD_SW) cost += _price.remove_road;
} while (t2 >>= 1); if (c & ROAD_SE) cost += _price.remove_road;
if (c & ROAD_NE) cost += _price.remove_road;
if (flags & DC_EXEC) { if (flags & DC_EXEC) {
ChangeTownRating(t, -road_remove_cost[(byte)edge_road], RATING_ROAD_MINIMUM); ChangeTownRating(t, -road_remove_cost[(byte)edge_road], RATING_ROAD_MINIMUM);
_m[tile].m5 ^= c; present ^= c;
if (GetRoadBits(tile) == 0) { if (present == 0) {
DoClearSquare(tile); DoClearSquare(tile);
} else { } else {
SetRoadBits(tile, present);
MarkTileDirtyByTile(tile); MarkTileDirtyByTile(tile);
} }
} }
@ -248,10 +246,10 @@ static const RoadBits _valid_tileh_slopes_road[][15] = {
}; };
static uint32 CheckRoadSlope(int tileh, byte *pieces, byte existing) static uint32 CheckRoadSlope(int tileh, RoadBits* pieces, RoadBits existing)
{ {
if (!IsSteepTileh(tileh)) { if (!IsSteepTileh(tileh)) {
byte road_bits = *pieces | existing; RoadBits road_bits = *pieces | existing;
// no special foundation // no special foundation
if ((~_valid_tileh_slopes_road[0][tileh] & road_bits) == 0) { if ((~_valid_tileh_slopes_road[0][tileh] & road_bits) == 0) {
@ -266,11 +264,11 @@ static uint32 CheckRoadSlope(int tileh, byte *pieces, byte existing)
} }
// partly leveled up tile, only if there's no road on that tile // partly leveled up tile, only if there's no road on that tile
if (!existing && (tileh == 1 || tileh == 2 || tileh == 4 || tileh == 8)) { if (existing == 0 && (tileh == 1 || tileh == 2 || tileh == 4 || tileh == 8)) {
// force full pieces. // force full pieces.
*pieces |= (*pieces & 0xC) >> 2; *pieces |= (*pieces & 0xC) >> 2;
*pieces |= (*pieces & 0x3) << 2; *pieces |= (*pieces & 0x3) << 2;
return (*pieces == (ROAD_NE|ROAD_SW) || *pieces == (ROAD_SE|ROAD_NW)) ? _price.terraform : CMD_ERROR; return (*pieces == ROAD_X || *pieces == ROAD_Y) ? _price.terraform : CMD_ERROR;
} }
} }
return CMD_ERROR; return CMD_ERROR;
@ -285,14 +283,16 @@ int32 CmdBuildRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
{ {
TileInfo ti; TileInfo ti;
int32 cost; int32 cost;
byte pieces = (byte)p1, existing = 0; RoadBits existing = 0;
RoadBits pieces;
TileIndex tile; TileIndex tile;
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
/* Road pieces are max 4 bitset values (NE, NW, SE, SW) and town can only be non-zero /* Road pieces are max 4 bitset values (NE, NW, SE, SW) and town can only be non-zero
* if a non-player is building the road */ * if a non-player is building the road */
if ((pieces >> 4) || (_current_player < MAX_PLAYERS && p2 != 0) || !IsTownIndex(p2)) return CMD_ERROR; if ((p1 >> 4) || (_current_player < MAX_PLAYERS && p2 != 0) || !IsTownIndex(p2)) return CMD_ERROR;
pieces = p1;
FindLandscapeHeight(&ti, x, y); FindLandscapeHeight(&ti, x, y);
tile = ti.tile; tile = ti.tile;
@ -302,16 +302,16 @@ int32 CmdBuildRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
switch (ti.type) { switch (ti.type) {
case MP_STREET: case MP_STREET:
switch (GetRoadType(ti.tile)) { switch (GetRoadType(tile)) {
case ROAD_NORMAL: case ROAD_NORMAL:
if ((GetRoadBits(ti.tile) & pieces) == pieces) { existing = GetRoadBits(tile);
if ((existing & pieces) == pieces) {
return_cmd_error(STR_1007_ALREADY_BUILT); return_cmd_error(STR_1007_ALREADY_BUILT);
} }
existing = ti.map5;
break; break;
case ROAD_CROSSING: case ROAD_CROSSING:
if (pieces != GetCrossingRoadBits(ti.tile)) { // XXX is this correct? if (pieces != GetCrossingRoadBits(tile)) { // XXX is this correct?
return_cmd_error(STR_1007_ALREADY_BUILT); return_cmd_error(STR_1007_ALREADY_BUILT);
} }
goto do_clear; goto do_clear;
@ -363,7 +363,7 @@ int32 CmdBuildRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
if ((ti.map5 & 0xC0) != 0xC0) goto do_clear; if ((ti.map5 & 0xC0) != 0xC0) goto do_clear;
/* only allow roads pertendicular to bridge */ /* only allow roads pertendicular to bridge */
if (((pieces & 5U) != 0) == ((ti.map5 & 0x01U) != 0)) goto do_clear; if (((pieces & ROAD_Y) != 0) == ((ti.map5 & 0x01U) != 0)) goto do_clear;
/* check if clear land under bridge */ /* check if clear land under bridge */
if ((ti.map5 & 0xF8) == 0xE8) { /* road under bridge */ if ((ti.map5 & 0xF8) == 0xE8) { /* road under bridge */
@ -396,26 +396,23 @@ do_clear:;
if (cost && (!_patches.build_on_slopes || _is_old_ai_player)) if (cost && (!_patches.build_on_slopes || _is_old_ai_player))
return CMD_ERROR; return CMD_ERROR;
if (ti.type != MP_STREET || GetRoadType(ti.tile) != ROAD_NORMAL) { if (ti.type == MP_STREET && GetRoadType(tile) == ROAD_NORMAL) {
cost += DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
} else {
// Don't put the pieces that already exist // Don't put the pieces that already exist
pieces &= ~ti.map5; pieces &= ComplementRoadBits(existing);
} else {
cost += DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
} }
{ if (pieces & ROAD_NW) cost += _price.build_road;
byte t = pieces; if (pieces & ROAD_SW) cost += _price.build_road;
while (t) { if (pieces & ROAD_SE) cost += _price.build_road;
if (t & 1) cost += _price.build_road; if (pieces & ROAD_NE) cost += _price.build_road;
t >>= 1;
}
}
if (flags & DC_EXEC) { if (flags & DC_EXEC) {
if (ti.type != MP_STREET) { if (ti.type == MP_STREET) {
MakeRoadNormal(tile, _current_player, pieces, p2); SetRoadBits(tile, existing | pieces);
} else { } else {
_m[tile].m5 |= pieces; MakeRoadNormal(tile, _current_player, pieces, p2);
} }
MarkTileDirtyByTile(tile); MarkTileDirtyByTile(tile);

View File

@ -2,6 +2,7 @@
#include "stdafx.h" #include "stdafx.h"
#include "openttd.h" #include "openttd.h"
#include "road_map.h"
#include "table/sprites.h" #include "table/sprites.h"
#include "table/strings.h" #include "table/strings.h"
#include "functions.h" #include "functions.h"
@ -72,7 +73,7 @@ static void BuildRoadOutsideStation(TileIndex tile, int direction)
static const byte _roadbits_by_dir[4] = {2,1,8,4}; static const byte _roadbits_by_dir[4] = {2,1,8,4};
tile += TileOffsByDir(direction); tile += TileOffsByDir(direction);
// if there is a roadpiece just outside of the station entrance, build a connecting route // if there is a roadpiece just outside of the station entrance, build a connecting route
if (IsTileType(tile, MP_STREET) && !(_m[tile].m5 & 0x20)) { if (IsTileType(tile, MP_STREET) && GetRoadType(tile) == ROAD_NORMAL) {
DoCommandP(tile, _roadbits_by_dir[direction], 0, NULL, CMD_BUILD_ROAD); DoCommandP(tile, _roadbits_by_dir[direction], 0, NULL, CMD_BUILD_ROAD);
} }
} }

View File

@ -2,6 +2,7 @@
#include "stdafx.h" #include "stdafx.h"
#include "openttd.h" #include "openttd.h"
#include "functions.h"
#include "road_map.h" #include "road_map.h"
#include "station.h" #include "station.h"
@ -42,3 +43,10 @@ RoadBits GetAnyRoadBits(TileIndex tile)
default: return 0; default: return 0;
} }
} }
TrackBits GetAnyRoadTrackBits(TileIndex tile)
{
uint32 r = GetTileTrackStatus(tile, TRANSPORT_ROAD);
return (byte)(r | (r >> 8));
}

View File

@ -33,6 +33,12 @@ static inline RoadBits GetRoadBits(TileIndex tile)
return GB(_m[tile].m5, 0, 4); return GB(_m[tile].m5, 0, 4);
} }
static inline void SetRoadBits(TileIndex tile, RoadBits r)
{
SB(_m[tile].m5, 0, 4, r);
}
static inline RoadBits GetCrossingRoadBits(TileIndex tile) static inline RoadBits GetCrossingRoadBits(TileIndex tile)
{ {
return _m[tile].m5 & 8 ? ROAD_Y : ROAD_X; return _m[tile].m5 & 8 ? ROAD_Y : ROAD_X;
@ -67,6 +73,9 @@ static inline RoadType GetRoadType(TileIndex tile)
RoadBits GetAnyRoadBits(TileIndex); RoadBits GetAnyRoadBits(TileIndex);
TrackBits GetAnyRoadTrackBits(TileIndex tile);
static inline void MakeRoadNormal(TileIndex t, Owner owner, RoadBits bits, uint town) static inline void MakeRoadNormal(TileIndex t, Owner owner, RoadBits bits, uint town)
{ {
SetTileType(t, MP_STREET); SetTileType(t, MP_STREET);

View File

@ -461,17 +461,17 @@ void OnTick_Town(void)
} }
} }
static byte GetTownRoadMask(TileIndex tile) static RoadBits GetTownRoadMask(TileIndex tile)
{ {
byte b = GetRoadBitsByTile(tile); TrackBits b = GetAnyRoadTrackBits(tile);
byte r = 0; RoadBits r = 0;
if (b & 0x01) r |= 10; if (b & TRACK_BIT_X) r |= ROAD_X;
if (b & 0x02) r |= 5; if (b & TRACK_BIT_Y) r |= ROAD_Y;
if (b & 0x04) r |= 9; if (b & TRACK_BIT_UPPER) r |= ROAD_NE | ROAD_NW;
if (b & 0x08) r |= 6; if (b & TRACK_BIT_LOWER) r |= ROAD_SE | ROAD_SW;
if (b & 0x10) r |= 3; if (b & TRACK_BIT_LEFT) r |= ROAD_NW | ROAD_SW;
if (b & 0x20) r |= 12; if (b & TRACK_BIT_RIGHT) r |= ROAD_NE | ROAD_SE;
return r; return r;
} }
@ -486,7 +486,7 @@ static bool IsRoadAllowedHere(TileIndex tile, int dir)
for (;;) { for (;;) {
// Check if there already is a road at this point? // Check if there already is a road at this point?
if (GetRoadBitsByTile(tile) == 0) { if (GetAnyRoadTrackBits(tile) == 0) {
// No, try to build one in the direction. // No, try to build one in the direction.
// if that fails clear the land, and if that fails exit. // if that fails clear the land, and if that fails exit.
// This is to make sure that we can build a road here later. // This is to make sure that we can build a road here later.
@ -567,17 +567,20 @@ static void LevelTownLand(TileIndex tile)
#define IS_WATER_TILE(t) (IsTileType((t), MP_WATER) && _m[(t)].m5 == 0) #define IS_WATER_TILE(t) (IsTileType((t), MP_WATER) && _m[(t)].m5 == 0)
static void GrowTownInTile(TileIndex *tile_ptr, uint mask, int block, Town *t1) static void GrowTownInTile(TileIndex* tile_ptr, RoadBits mask, int block, Town* t1)
{ {
int a,b,rcmd; RoadBits rcmd;
TileIndex tmptile; TileIndex tmptile;
uint i; DiagDirection i;
int j; int j;
TileIndex tile = *tile_ptr; TileIndex tile = *tile_ptr;
TILE_ASSERT(tile); TILE_ASSERT(tile);
if (mask == 0) { if (mask == 0) {
int a;
int b;
// Tile has no road. First reset the status counter // Tile has no road. First reset the status counter
// to say that this is the last iteration. // to say that this is the last iteration.
_grow_town_result = 0; _grow_town_result = 0;
@ -618,6 +621,7 @@ static void GrowTownInTile(TileIndex *tile_ptr, uint mask, int block, Town *t1)
_grow_town_result = 0; _grow_town_result = 0;
rcmd = 1 << (block ^ 2); rcmd = 1 << (block ^ 2);
} else { } else {
int i;
// Reached a tunnel? Then continue at the other side of it. // Reached a tunnel? Then continue at the other side of it.
if (IsTileType(tile, MP_TUNNELBRIDGE) && (_m[tile].m5& ~3) == 4) { if (IsTileType(tile, MP_TUNNELBRIDGE) && (_m[tile].m5& ~3) == 4) {
@ -665,10 +669,10 @@ static void GrowTownInTile(TileIndex *tile_ptr, uint mask, int block, Town *t1)
// Determine direction of slope, // Determine direction of slope,
// and build a road if not a special slope. // and build a road if not a special slope.
switch (GetTileSlope(tile, NULL)) { switch (GetTileSlope(tile, NULL)) {
case 3: i = 0; break; case 3: i = DIAGDIR_NE; break;
case 6: i = 3; break; case 6: i = DIAGDIR_NW; break;
case 9: i = 1; break; case 9: i = DIAGDIR_SE; break;
case 12: i = 2; break; case 12: i = DIAGDIR_SW; break;
default: default:
build_road_and_exit: build_road_and_exit:
@ -714,7 +718,6 @@ build_road_and_exit:
// Returns true if a house was built, or no if the build failed. // Returns true if a house was built, or no if the build failed.
static int GrowTownAtRoad(Town *t, TileIndex tile) static int GrowTownAtRoad(Town *t, TileIndex tile)
{ {
uint mask;
int block = 5; // special case int block = 5; // special case
TILE_ASSERT(tile); TILE_ASSERT(tile);
@ -724,7 +727,7 @@ static int GrowTownAtRoad(Town *t, TileIndex tile)
do { do {
// Get a bitmask of the road blocks on a tile // Get a bitmask of the road blocks on a tile
mask = GetTownRoadMask(tile); RoadBits mask = GetTownRoadMask(tile);
// Try to grow the town from this point // Try to grow the town from this point
GrowTownInTile(&tile,mask,block,t); GrowTownInTile(&tile,mask,block,t);
@ -761,7 +764,7 @@ static int GrowTownAtRoad(Town *t, TileIndex tile)
// Generate a random road block // Generate a random road block
// The probability of a straight road // The probability of a straight road
// is somewhat higher than a curved. // is somewhat higher than a curved.
static int GenRandomRoadBits(void) static RoadBits GenRandomRoadBits(void)
{ {
uint32 r = Random(); uint32 r = Random();
uint a = GB(r, 0, 2); uint a = GB(r, 0, 2);
@ -801,7 +804,7 @@ static bool GrowTown(Town *t)
// Find a road that we can base the construction on. // Find a road that we can base the construction on.
tile = t->xy; tile = t->xy;
for (ptr = _town_coord_mod; ptr != endof(_town_coord_mod); ++ptr) { for (ptr = _town_coord_mod; ptr != endof(_town_coord_mod); ++ptr) {
if (GetRoadBitsByTile(tile) != 0) { if (GetAnyRoadTrackBits(tile) != 0) {
int r = GrowTownAtRoad(t, tile); int r = GrowTownAtRoad(t, tile);
_current_player = old_player; _current_player = old_player;
return r; return r;