Mercurial > hg > openttd
changeset 3150:20534dd0fae9 draft
(svn r3773) Shove some semantics down ottd's throat by replacing ints and magic numbers by enums and some related changes
author | tron <tron@openttd.org> |
---|---|
date | Mon, 06 Mar 2006 13:11:08 +0000 |
parents | 93d5db93d000 |
children | 113d5ac81142 |
files | functions.h road_cmd.c road_gui.c road_map.c road_map.h town_cmd.c |
diffstat | 6 files changed, 79 insertions(+), 62 deletions(-) [+] |
line wrap: on
line diff
--- a/functions.h +++ b/functions.h @@ -215,7 +215,6 @@ Town *ClosestTownFromTile(TileIndex tile, uint threshold); void ChangeTownRating(Town *t, int add, int max); -uint GetRoadBitsByTile(TileIndex tile); int GetTownRadiusGroup(const Town *t, TileIndex tile); void ShowNetworkChatQueryWindow(byte desttype, byte dest); void ShowNetworkGiveMoneyWindow(byte player);
--- a/road_cmd.c +++ b/road_cmd.c @@ -73,11 +73,6 @@ return true; } -uint GetRoadBitsByTile(TileIndex tile) -{ - uint32 r = GetTileTrackStatus(tile, TRANSPORT_ROAD); - return (byte)(r | (r >> 8)); -} /** Delete a piece of road. * @param x,y tile coordinates for road construction @@ -158,30 +153,33 @@ switch (GetRoadType(ti.tile)) { 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 & 0x3) << 2; } // 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 - t2 = c; cost = 0; - do { - if (t2 & 1) cost += _price.remove_road; - } while (t2 >>= 1); + if (c & ROAD_NW) cost += _price.remove_road; + if (c & ROAD_SW) cost += _price.remove_road; + if (c & ROAD_SE) cost += _price.remove_road; + if (c & ROAD_NE) cost += _price.remove_road; if (flags & DC_EXEC) { ChangeTownRating(t, -road_remove_cost[(byte)edge_road], RATING_ROAD_MINIMUM); - _m[tile].m5 ^= c; - if (GetRoadBits(tile) == 0) { + present ^= c; + if (present == 0) { DoClearSquare(tile); } else { + SetRoadBits(tile, present); MarkTileDirtyByTile(tile); } } @@ -248,10 +246,10 @@ }; -static uint32 CheckRoadSlope(int tileh, byte *pieces, byte existing) +static uint32 CheckRoadSlope(int tileh, RoadBits* pieces, RoadBits existing) { if (!IsSteepTileh(tileh)) { - byte road_bits = *pieces | existing; + RoadBits road_bits = *pieces | existing; // no special foundation if ((~_valid_tileh_slopes_road[0][tileh] & road_bits) == 0) { @@ -266,11 +264,11 @@ } // 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. *pieces |= (*pieces & 0xC) >> 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; @@ -285,14 +283,16 @@ { TileInfo ti; int32 cost; - byte pieces = (byte)p1, existing = 0; + RoadBits existing = 0; + RoadBits pieces; TileIndex tile; SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); /* 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 ((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); tile = ti.tile; @@ -302,16 +302,16 @@ switch (ti.type) { case MP_STREET: - switch (GetRoadType(ti.tile)) { + switch (GetRoadType(tile)) { case ROAD_NORMAL: - if ((GetRoadBits(ti.tile) & pieces) == pieces) { + existing = GetRoadBits(tile); + if ((existing & pieces) == pieces) { return_cmd_error(STR_1007_ALREADY_BUILT); } - existing = ti.map5; break; 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); } goto do_clear; @@ -363,7 +363,7 @@ if ((ti.map5 & 0xC0) != 0xC0) goto do_clear; /* 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 */ if ((ti.map5 & 0xF8) == 0xE8) { /* road under bridge */ @@ -396,26 +396,23 @@ if (cost && (!_patches.build_on_slopes || _is_old_ai_player)) return CMD_ERROR; - if (ti.type != MP_STREET || GetRoadType(ti.tile) != ROAD_NORMAL) { - cost += DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); + if (ti.type == MP_STREET && GetRoadType(tile) == ROAD_NORMAL) { + // Don't put the pieces that already exist + pieces &= ComplementRoadBits(existing); } else { - // Don't put the pieces that already exist - pieces &= ~ti.map5; + cost += DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); } - { - byte t = pieces; - while (t) { - if (t & 1) cost += _price.build_road; - t >>= 1; - } - } + if (pieces & ROAD_NW) cost += _price.build_road; + if (pieces & ROAD_SW) cost += _price.build_road; + if (pieces & ROAD_SE) cost += _price.build_road; + if (pieces & ROAD_NE) cost += _price.build_road; if (flags & DC_EXEC) { - if (ti.type != MP_STREET) { + if (ti.type == MP_STREET) { + SetRoadBits(tile, existing | pieces); + } else { MakeRoadNormal(tile, _current_player, pieces, p2); - } else { - _m[tile].m5 |= pieces; } MarkTileDirtyByTile(tile);
--- a/road_gui.c +++ b/road_gui.c @@ -2,6 +2,7 @@ #include "stdafx.h" #include "openttd.h" +#include "road_map.h" #include "table/sprites.h" #include "table/strings.h" #include "functions.h" @@ -72,7 +73,7 @@ static const byte _roadbits_by_dir[4] = {2,1,8,4}; tile += TileOffsByDir(direction); // 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); } }
--- a/road_map.c +++ b/road_map.c @@ -2,6 +2,7 @@ #include "stdafx.h" #include "openttd.h" +#include "functions.h" #include "road_map.h" #include "station.h" @@ -42,3 +43,10 @@ default: return 0; } } + + +TrackBits GetAnyRoadTrackBits(TileIndex tile) +{ + uint32 r = GetTileTrackStatus(tile, TRANSPORT_ROAD); + return (byte)(r | (r >> 8)); +}
--- a/road_map.h +++ b/road_map.h @@ -33,6 +33,12 @@ 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) { return _m[tile].m5 & 8 ? ROAD_Y : ROAD_X; @@ -67,6 +73,9 @@ RoadBits GetAnyRoadBits(TileIndex); +TrackBits GetAnyRoadTrackBits(TileIndex tile); + + static inline void MakeRoadNormal(TileIndex t, Owner owner, RoadBits bits, uint town) { SetTileType(t, MP_STREET);
--- a/town_cmd.c +++ b/town_cmd.c @@ -461,17 +461,17 @@ } } -static byte GetTownRoadMask(TileIndex tile) +static RoadBits GetTownRoadMask(TileIndex tile) { - byte b = GetRoadBitsByTile(tile); - byte r = 0; + TrackBits b = GetAnyRoadTrackBits(tile); + RoadBits r = 0; - if (b & 0x01) r |= 10; - if (b & 0x02) r |= 5; - if (b & 0x04) r |= 9; - if (b & 0x08) r |= 6; - if (b & 0x10) r |= 3; - if (b & 0x20) r |= 12; + if (b & TRACK_BIT_X) r |= ROAD_X; + if (b & TRACK_BIT_Y) r |= ROAD_Y; + if (b & TRACK_BIT_UPPER) r |= ROAD_NE | ROAD_NW; + if (b & TRACK_BIT_LOWER) r |= ROAD_SE | ROAD_SW; + if (b & TRACK_BIT_LEFT) r |= ROAD_NW | ROAD_SW; + if (b & TRACK_BIT_RIGHT) r |= ROAD_NE | ROAD_SE; return r; } @@ -486,7 +486,7 @@ for (;;) { // 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. // if that fails clear the land, and if that fails exit. // This is to make sure that we can build a road here later. @@ -567,17 +567,20 @@ #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; - uint i; + DiagDirection i; int j; TileIndex tile = *tile_ptr; TILE_ASSERT(tile); if (mask == 0) { + int a; + int b; + // Tile has no road. First reset the status counter // to say that this is the last iteration. _grow_town_result = 0; @@ -618,6 +621,7 @@ _grow_town_result = 0; rcmd = 1 << (block ^ 2); } else { + int i; // Reached a tunnel? Then continue at the other side of it. if (IsTileType(tile, MP_TUNNELBRIDGE) && (_m[tile].m5& ~3) == 4) { @@ -665,10 +669,10 @@ // Determine direction of slope, // and build a road if not a special slope. switch (GetTileSlope(tile, NULL)) { - case 3: i = 0; break; - case 6: i = 3; break; - case 9: i = 1; break; - case 12: i = 2; break; + case 3: i = DIAGDIR_NE; break; + case 6: i = DIAGDIR_NW; break; + case 9: i = DIAGDIR_SE; break; + case 12: i = DIAGDIR_SW; break; default: build_road_and_exit: @@ -714,7 +718,6 @@ // Returns true if a house was built, or no if the build failed. static int GrowTownAtRoad(Town *t, TileIndex tile) { - uint mask; int block = 5; // special case TILE_ASSERT(tile); @@ -724,7 +727,7 @@ do { // 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 GrowTownInTile(&tile,mask,block,t); @@ -761,7 +764,7 @@ // Generate a random road block // The probability of a straight road // is somewhat higher than a curved. -static int GenRandomRoadBits(void) +static RoadBits GenRandomRoadBits(void) { uint32 r = Random(); uint a = GB(r, 0, 2); @@ -801,7 +804,7 @@ // Find a road that we can base the construction on. tile = t->xy; for (ptr = _town_coord_mod; ptr != endof(_town_coord_mod); ++ptr) { - if (GetRoadBitsByTile(tile) != 0) { + if (GetAnyRoadTrackBits(tile) != 0) { int r = GrowTownAtRoad(t, tile); _current_player = old_player; return r;