Skip to content

Instantly share code, notes, and snippets.

@Wolfolo
Last active November 17, 2016 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Wolfolo/21b9c4f6387c8d8546ddb1d165f90f6b to your computer and use it in GitHub Desktop.
Save Wolfolo/21b9c4f6387c8d8546ddb1d165f90f6b to your computer and use it in GitHub Desktop.
diff --git a/src/rail_cmd.cpp b/src/rail_cmd.cpp
index eda0248..6e8336e 100644
--- a/src/rail_cmd.cpp
+++ b/src/rail_cmd.cpp
@@ -636,7 +636,7 @@ CommandCost CmdRemoveSingleRail(TileIndex tile, DoCommandFlag flags, uint32 p1,
owner = GetTileOwner(tile);
Company::Get(owner)->infrastructure.rail[GetRailType(tile)] -= LEVELCROSSING_TRACKBIT_FACTOR;
DirtyCompanyInfrastructureWindows(owner);
- MakeRoadNormal(tile, GetCrossingRoadBits(tile), GetRoadTypes(tile), GetTownIndex(tile), GetRoadOwner(tile, ROADTYPE_ROAD), GetRoadOwner(tile, ROADTYPE_TRAM));
+ MakeRoadNormal(tile, GetCrossingRoadBits(tile), RoadTypeIdentifiers::FromTile(tile), GetTownIndex(tile), GetRoadOwner(tile, ROADTYPE_ROAD), GetRoadOwner(tile, ROADTYPE_TRAM));
DeleteNewGRFInspectWindow(GSF_RAILTYPES, tile);
}
break;
diff --git a/src/road_gui.cpp b/src/road_gui.cpp
index d9adf8e..6e4c542 100644
--- a/src/road_gui.cpp
+++ b/src/road_gui.cpp
@@ -172,8 +172,8 @@ void CcRoadStop(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
* @param start_tile First tile of the area.
* @param end_tile Last tile of the area.
* @param p2 bit 0: 0 For bus stops, 1 for truck stops.
- * bit 2..3: The roadtypes.
- * bit 5: Allow stations directly adjacent to other stations.
+ * bit 2: Allow stations directly adjacent to other stations.
+ * bit 5..10: The roadtypes.
* @param cmd Command to use.
* @see CcRoadStop()
*/
@@ -186,7 +186,7 @@ static void PlaceRoadStop(TileIndex start_tile, TileIndex end_tile, uint32 p2, u
SetBit(p2, 1); // It's a drive-through stop.
ddir -= DIAGDIR_END; // Adjust picker result to actual direction.
}
- p2 |= ddir << 6; // Set the DiagDirecion into p2 bits 6 and 7.
+ p2 |= ddir << 3; // Set the DiagDirecion into p2 bits 3 and 4.
TileArea ta(start_tile, end_tile);
CommandContainer cmdcont = { ta.tile, (uint32)(ta.w | ta.h << 8), p2, cmd, CcRoadStop, "" };
@@ -637,11 +637,11 @@ struct BuildRoadToolbarWindow : Window {
break;
case DDSP_BUILD_BUSSTOP:
- PlaceRoadStop(start_tile, end_tile, (_ctrl_pressed << 5) | RoadTypeToRoadTypes(_cur_roadtype_identifier.basetype) << 2 | ROADSTOP_BUS, CMD_BUILD_ROAD_STOP | CMD_MSG(rti->strings.err_build_station[ROADSTOP_BUS]));
+ PlaceRoadStop(start_tile, end_tile, _cur_roadtype_identifier.Pack() << 5 | (_ctrl_pressed << 2) | ROADSTOP_BUS, CMD_BUILD_ROAD_STOP | CMD_MSG(rti->strings.err_build_station[ROADSTOP_BUS]));
break;
case DDSP_BUILD_TRUCKSTOP:
- PlaceRoadStop(start_tile, end_tile, (_ctrl_pressed << 5) | RoadTypeToRoadTypes(_cur_roadtype_identifier.basetype) << 2 | ROADSTOP_TRUCK, CMD_BUILD_ROAD_STOP | CMD_MSG(rti->strings.err_build_station[ROADSTOP_TRUCK]));
+ PlaceRoadStop(start_tile, end_tile, _cur_roadtype_identifier.Pack() << 5 | (_ctrl_pressed << 2) | ROADSTOP_TRUCK, CMD_BUILD_ROAD_STOP | CMD_MSG(rti->strings.err_build_station[ROADSTOP_TRUCK]));
break;
case DDSP_REMOVE_BUSSTOP: {
diff --git a/src/road_map.h b/src/road_map.h
index aed7f21..38f8084 100644
--- a/src/road_map.h
+++ b/src/road_map.h
@@ -550,6 +550,9 @@ static inline RoadTypeIdentifier GetRoadTypeTram(TileIndex t)
return RoadTypeIdentifier(ROADTYPE_TRAM, (RoadSubType)GB(_m[t].m4, 4, 4));
}
+static inline bool HasRoadTypeRoad(TileIndex t);
+static inline bool HasRoadTypeTram(TileIndex t);
+
struct RoadTypeIdentifiers {
RoadTypeIdentifier road_identifier;
RoadTypeIdentifier tram_identifier;
@@ -562,10 +565,11 @@ struct RoadTypeIdentifiers {
*/
static RoadTypeIdentifiers FromTile(TileIndex t)
{
- assert(IsTileType(t, MP_ROAD) || IsTileType(t, MP_STATION) || IsTileType(t, MP_TUNNELBRIDGE));
-
RoadTypeIdentifiers rtids;
+ if (!(IsTileType(t, MP_ROAD) || IsTileType(t, MP_STATION) || IsTileType(t, MP_TUNNELBRIDGE)))
+ return rtids;
+
switch (GetTileType(t)) {
default: NOT_REACHED();
case MP_ROAD:
@@ -579,8 +583,13 @@ struct RoadTypeIdentifiers {
break;
case MP_STATION: /* TODO */
case MP_TUNNELBRIDGE:
- rtids.road_identifier = GetRoadTypeRoad(t);
- rtids.tram_identifier = GetRoadTypeTram(t);
+ if (HasRoadTypeRoad(t)) {
+ rtids.road_identifier = GetRoadTypeRoad(t);
+ }
+
+ if (HasRoadTypeTram(t)) {
+ rtids.tram_identifier = GetRoadTypeTram(t);
+ }
break;
}
@@ -727,7 +736,7 @@ static inline void SetRoadTypes(TileIndex t, RoadTypeIdentifiers rtids)
static inline bool HasRoadTypeRoad(TileIndex t)
{
- return RoadTypeIdentifiers::FromTile(t).road_identifier.IsValid();
+ return GB(_m[t].m4, 0, 4) != 0;
}
static inline bool HasRoadTypeRoad(RoadTypeIdentifier rtid)
@@ -742,7 +751,7 @@ static inline bool HasRoadTypeRoad(RoadTypeIdentifiers rtids)
static inline bool HasRoadTypeTram(TileIndex t)
{
- return RoadTypeIdentifiers::FromTile(t).tram_identifier.IsValid();
+ return GB(_m[t].m4, 4, 4) != 0;
}
static inline bool HasRoadTypeTram(RoadTypeIdentifier rtid)
@@ -757,23 +766,23 @@ static inline bool HasRoadTypeTram(RoadTypeIdentifiers rtids)
/**
* Make a normal road tile.
- * @param t Tile to make a normal road.
- * @param bits Road bits to set for all present road types.
- * @param rot New present road types.
- * @param town Town ID if the road is a town-owned road.
- * @param road New owner of road.
- * @param tram New owner of tram tracks.
+ * @param t Tile to make a normal road.
+ * @param bits Road bits to set for all present road types.
+ * @param rtids New present road types.
+ * @param town Town ID if the road is a town-owned road.
+ * @param road New owner of road.
+ * @param tram New owner of tram tracks.
*/
-static inline void MakeRoadNormal(TileIndex t, RoadBits bits, RoadTypes rot, TownID town, Owner road, Owner tram)
+static inline void MakeRoadNormal(TileIndex t, RoadBits bits, RoadTypeIdentifiers rtids, TownID town, Owner road, Owner tram)
{
SetTileType(t, MP_ROAD);
SetTileOwner(t, road);
_m[t].m2 = town;
- _m[t].m3 = (HasBit(rot, ROADTYPE_TRAM) ? bits : 0);
- _m[t].m4 = 0;
- _m[t].m5 = (HasBit(rot, ROADTYPE_ROAD) ? bits : 0) | ROAD_TILE_NORMAL << 6;
+ _m[t].m3 = (HasRoadTypeTram(rtids) ? bits : 0);
+ _m[t].m5 = (HasRoadTypeRoad(rtids) ? bits : 0) | ROAD_TILE_NORMAL << 6;
SB(_me[t].m6, 2, 4, 0);
- _me[t].m7 = rot << 6;
+ _me[t].m7 = rtids.PresentRoadTypes() << 6;
+ SetRoadTypes(t, rtids);
SetRoadOwner(t, ROADTYPE_TRAM, tram);
}
diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp
index cde702c..610b56c 100644
--- a/src/saveload/afterload.cpp
+++ b/src/saveload/afterload.cpp
@@ -462,7 +462,7 @@ static void FixOwnerOfRailTrack(TileIndex t)
if (IsLevelCrossingTile(t)) {
/* else change the crossing to normal road (road vehicles won't care) */
- MakeRoadNormal(t, GetCrossingRoadBits(t), GetRoadTypes(t), GetTownIndex(t),
+ MakeRoadNormal(t, GetCrossingRoadBits(t), RoadTypeIdentifiers::FromTile(t), GetTownIndex(t),
GetRoadOwner(t, ROADTYPE_ROAD), GetRoadOwner(t, ROADTYPE_TRAM));
return;
}
@@ -1155,7 +1155,7 @@ bool AfterLoadGame()
MakeRoadNormal(
t,
axis == AXIS_X ? ROAD_Y : ROAD_X,
- ROADTYPES_ROAD,
+ RoadTypeIdentifiers::FromRoadTypeIdentifier(RoadTypeIdentifier(ROADTYPE_ROAD, ROADSUBTYPE_BEGIN)),
town,
GetTileOwner(t), OWNER_NONE
);
diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp
index 6d1468c..5d78738 100644
--- a/src/station_cmd.cpp
+++ b/src/station_cmd.cpp
@@ -1755,7 +1755,9 @@ CommandCost CmdBuildRoadStop(TileIndex tile, DoCommandFlag flags, uint32 p1, uin
{
bool type = HasBit(p2, 0);
bool is_drive_through = HasBit(p2, 1);
- RoadTypes rts = Extract<RoadTypes, 2, 2>(p2);
+ RoadTypeIdentifier rtid;
+ if (!rtid.UnpackIfValid(GB(p2, 5, 5))) return CMD_ERROR;
+ RoadTypes rts = RoadTypeToRoadTypes(rtid.basetype);
StationID station_to_join = GB(p2, 16, 16);
bool reuse = (station_to_join != NEW_STATION);
if (!reuse) station_to_join = INVALID_STATION;
@@ -1784,11 +1786,11 @@ CommandCost CmdBuildRoadStop(TileIndex tile, DoCommandFlag flags, uint32 p1, uin
Axis axis;
if (is_drive_through) {
/* By definition axis is valid, due to there being 2 axes and reading 1 bit. */
- axis = Extract<Axis, 6, 1>(p2);
+ axis = Extract<Axis, 3, 1>(p2);
ddir = AxisToDiagDir(axis);
} else {
/* By definition ddir is valid, due to there being 4 diagonal directions and reading 2 bits. */
- ddir = Extract<DiagDirection, 6, 2>(p2);
+ ddir = Extract<DiagDirection, 3, 2>(p2);
axis = DiagDirToAxis(ddir);
}
@@ -1803,7 +1805,7 @@ CommandCost CmdBuildRoadStop(TileIndex tile, DoCommandFlag flags, uint32 p1, uin
cost.AddCost(ret);
Station *st = NULL;
- ret = FindJoiningRoadStop(est, station_to_join, HasBit(p2, 5), roadstop_area, &st);
+ ret = FindJoiningRoadStop(est, station_to_join, HasBit(p2, 2), roadstop_area, &st);
if (ret.Failed()) return ret;
/* Check if this number of road stops can be allocated. */
@@ -1815,9 +1817,9 @@ CommandCost CmdBuildRoadStop(TileIndex tile, DoCommandFlag flags, uint32 p1, uin
if (flags & DC_EXEC) {
/* Check every tile in the area. */
TILE_AREA_LOOP(cur_tile, roadstop_area) {
- RoadTypes cur_rts = GetRoadTypes(cur_tile);
- Owner road_owner = HasBit(cur_rts, ROADTYPE_ROAD) ? GetRoadOwner(cur_tile, ROADTYPE_ROAD) : _current_company;
- Owner tram_owner = HasBit(cur_rts, ROADTYPE_TRAM) ? GetRoadOwner(cur_tile, ROADTYPE_TRAM) : _current_company;
+ RoadTypeIdentifiers rtids = RoadTypeIdentifiers::FromTile(cur_tile);
+ Owner road_owner = HasRoadTypeRoad(rtids) ? GetRoadOwner(cur_tile, ROADTYPE_ROAD) : _current_company;
+ Owner tram_owner = HasRoadTypeTram(rtids) ? GetRoadOwner(cur_tile, ROADTYPE_TRAM) : _current_company;
if (IsTileType(cur_tile, MP_STATION) && IsRoadStop(cur_tile)) {
RemoveRoadStop(cur_tile, flags);
@@ -1844,6 +1846,7 @@ CommandCost CmdBuildRoadStop(TileIndex tile, DoCommandFlag flags, uint32 p1, uin
/* Update company infrastructure counts. If the current tile is a normal
* road tile, count only the new road bits needed to get a full diagonal road. */
RoadType rt;
+ RoadTypes cur_rts = rtids.PresentRoadTypes();
FOR_EACH_SET_ROADTYPE(rt, cur_rts | rts) {
Company *c = Company::GetIfValid(rt == ROADTYPE_ROAD ? road_owner : tram_owner);
if (c != NULL) {
@@ -1852,12 +1855,13 @@ CommandCost CmdBuildRoadStop(TileIndex tile, DoCommandFlag flags, uint32 p1, uin
}
}
- MakeDriveThroughRoadStop(cur_tile, st->owner, road_owner, tram_owner, st->index, rs_type, rts | cur_rts, axis);
+ rtids.MergeRoadTypes(rtid);
+ MakeDriveThroughRoadStop(cur_tile, st->owner, road_owner, tram_owner, st->index, rs_type, rtids, axis); // Was rts | cur_rts
road_stop->MakeDriveThrough();
} else {
/* Non-drive-through stop never overbuild and always count as two road bits. */
Company::Get(st->owner)->infrastructure.road[FIND_FIRST_BIT(rts)] += 2;
- MakeRoadStop(cur_tile, st->owner, st->index, rs_type, rts, ddir);
+ MakeRoadStop(cur_tile, st->owner, st->index, rs_type, rtids, ddir);
}
Company::Get(st->owner)->infrastructure.station++;
DirtyCompanyInfrastructureWindows(st->owner);
@@ -2033,6 +2037,7 @@ CommandCost CmdRemoveRoadStop(TileIndex tile, DoCommandFlag flags, uint32 p1, ui
if (!IsTileType(cur_tile, MP_STATION) || !IsRoadStop(cur_tile) || (uint32)GetRoadStopType(cur_tile) != GB(p2, 0, 1)) continue;
/* Save information on to-be-restored roads before the stop is removed. */
+ RoadTypeIdentifiers rtids = RoadTypeIdentifiers::FromTile(cur_tile);
RoadTypes rts = ROADTYPES_NONE;
RoadBits road_bits = ROAD_NONE;
Owner road_owner[] = { OWNER_NONE, OWNER_NONE };
@@ -2057,7 +2062,7 @@ CommandCost CmdRemoveRoadStop(TileIndex tile, DoCommandFlag flags, uint32 p1, ui
/* Restore roads. */
if ((flags & DC_EXEC) && rts != ROADTYPES_NONE) {
- MakeRoadNormal(cur_tile, road_bits, rts, ClosestTownFromTile(cur_tile, UINT_MAX)->index,
+ MakeRoadNormal(cur_tile, road_bits, rtids, ClosestTownFromTile(cur_tile, UINT_MAX)->index,
road_owner[ROADTYPE_ROAD], road_owner[ROADTYPE_TRAM]);
/* Update company infrastructure counts. */
diff --git a/src/station_map.h b/src/station_map.h
index 3bed22d..080ee1a 100644
--- a/src/station_map.h
+++ b/src/station_map.h
@@ -586,10 +586,10 @@ static inline void MakeRailWaypoint(TileIndex t, Owner o, StationID sid, Axis a,
* @param rt the roadtypes on this tile
* @param d the direction of the roadstop
*/
-static inline void MakeRoadStop(TileIndex t, Owner o, StationID sid, RoadStopType rst, RoadTypes rt, DiagDirection d)
+static inline void MakeRoadStop(TileIndex t, Owner o, StationID sid, RoadStopType rst, RoadTypeIdentifiers rtids, DiagDirection d)
{
MakeStation(t, o, sid, (rst == ROADSTOP_BUS ? STATION_BUS : STATION_TRUCK), d);
- SetRoadTypes(t, rt);
+ SetRoadTypes(t, rtids);
SetRoadOwner(t, ROADTYPE_ROAD, o);
SetRoadOwner(t, ROADTYPE_TRAM, o);
}
@@ -605,10 +605,10 @@ static inline void MakeRoadStop(TileIndex t, Owner o, StationID sid, RoadStopTyp
* @param rt the roadtypes on this tile
* @param a the direction of the roadstop
*/
-static inline void MakeDriveThroughRoadStop(TileIndex t, Owner station, Owner road, Owner tram, StationID sid, RoadStopType rst, RoadTypes rt, Axis a)
+static inline void MakeDriveThroughRoadStop(TileIndex t, Owner station, Owner road, Owner tram, StationID sid, RoadStopType rst, RoadTypeIdentifiers rtids, Axis a)
{
MakeStation(t, station, sid, (rst == ROADSTOP_BUS ? STATION_BUS : STATION_TRUCK), GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET + a);
- SetRoadTypes(t, rt);
+ SetRoadTypes(t, rtids);
SetRoadOwner(t, ROADTYPE_ROAD, road);
SetRoadOwner(t, ROADTYPE_TRAM, tram);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment