diff options
author | rubidium <rubidium@openttd.org> | 2008-01-09 21:05:03 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2008-01-09 21:05:03 +0000 |
commit | 2d2e1e386315db0c83f508c19387e9f2e0c9cee1 (patch) | |
tree | 6b9fc11a0135e1ccdb74151dd8962e4fd5264843 | |
parent | a5101fb403bc7220884236426c38bdafc2cd17cf (diff) | |
download | openttd-2d2e1e386315db0c83f508c19387e9f2e0c9cee1.tar.xz |
(svn r11800) -Codechange: move some functions to a more logical location + some type safety.
-rw-r--r-- | src/ai/default/default.cpp | 4 | ||||
-rw-r--r-- | src/ai/default/default.h | 2 | ||||
-rw-r--r-- | src/elrail.cpp | 2 | ||||
-rw-r--r-- | src/gfxinit.cpp | 1 | ||||
-rw-r--r-- | src/newgrf.cpp | 1 | ||||
-rw-r--r-- | src/newgrf_engine.cpp | 1 | ||||
-rw-r--r-- | src/npf.cpp | 12 | ||||
-rw-r--r-- | src/npf.h | 11 | ||||
-rw-r--r-- | src/pathfind.cpp | 4 | ||||
-rw-r--r-- | src/pathfind.h | 2 | ||||
-rw-r--r-- | src/player.h | 59 | ||||
-rw-r--r-- | src/player_face.h | 1 | ||||
-rw-r--r-- | src/players.cpp | 43 | ||||
-rw-r--r-- | src/rail.cpp | 42 | ||||
-rw-r--r-- | src/rail.h | 35 | ||||
-rw-r--r-- | src/rail_cmd.cpp | 15 | ||||
-rw-r--r-- | src/rail_gui.cpp | 4 | ||||
-rw-r--r-- | src/rail_type.h | 19 | ||||
-rw-r--r-- | src/road.cpp | 40 | ||||
-rw-r--r-- | src/road_func.h | 22 | ||||
-rw-r--r-- | src/road_type.h | 4 | ||||
-rw-r--r-- | src/roadveh_cmd.cpp | 8 | ||||
-rw-r--r-- | src/ship_cmd.cpp | 6 | ||||
-rw-r--r-- | src/station_cmd.cpp | 2 | ||||
-rw-r--r-- | src/table/railtypes.h | 16 | ||||
-rw-r--r-- | src/terraform_gui.cpp | 1 | ||||
-rw-r--r-- | src/train_cmd.cpp | 4 | ||||
-rw-r--r-- | src/tunnelbridge_cmd.cpp | 4 | ||||
-rw-r--r-- | src/vehicle_base.h | 2 | ||||
-rw-r--r-- | src/yapf/yapf_destrail.hpp | 2 |
30 files changed, 219 insertions, 150 deletions
diff --git a/src/ai/default/default.cpp b/src/ai/default/default.cpp index e3e3e8ba5..6d72f2043 100644 --- a/src/ai/default/default.cpp +++ b/src/ai/default/default.cpp @@ -1103,7 +1103,7 @@ static void AiWantTrainRoute(Player *p) { uint16 r = GB(Random(), 0, 16); - _players_ai[p->index].railtype_to_use = GetBestRailtype(p); + _players_ai[p->index].railtype_to_use = GetBestRailtype(p->index); if (r > 0xD000) { AiWantLongIndustryRoute(p); @@ -4034,4 +4034,4 @@ void SaveLoad_AI(PlayerID id) for (int i = 0; i != pai->num_build_rec; i++) { SlObject(&pai->src + i, _player_ai_build_rec_desc); } -}
\ No newline at end of file +} diff --git a/src/ai/default/default.h b/src/ai/default/default.h index 9c45d0945..934f96f22 100644 --- a/src/ai/default/default.h +++ b/src/ai/default/default.h @@ -3,6 +3,8 @@ #ifndef DEFAULT_H #define DEFAULT_H +#include "../../direction_type.h" + void AiDoGameLoop(Player*); void SaveLoad_AI(PlayerID id); diff --git a/src/elrail.cpp b/src/elrail.cpp index 6b1197998..c074b9b02 100644 --- a/src/elrail.cpp +++ b/src/elrail.cpp @@ -496,7 +496,7 @@ int32 SettingsDisableElrail(int32 p1) if (v->type == VEH_TRAIN && v->u.rail.railtype == RAILTYPE_ELECTRIC) { /* this railroad vehicle is now compatible only with elrail, * so add there also normal rail compatibility */ - v->u.rail.compatible_railtypes |= (1 << RAILTYPE_RAIL); + v->u.rail.compatible_railtypes |= RAILTYPES_RAIL; v->u.rail.railtype = RAILTYPE_RAIL; SetBit(v->u.rail.flags, VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL); } diff --git a/src/gfxinit.cpp b/src/gfxinit.cpp index 781de27e7..01f82d34a 100644 --- a/src/gfxinit.cpp +++ b/src/gfxinit.cpp @@ -16,6 +16,7 @@ #include "fontcache.h" #include "gfx_func.h" #include "core/alloc_func.hpp" +#include "core/bitmath_func.hpp" #include <string.h> struct MD5File { diff --git a/src/newgrf.cpp b/src/newgrf.cpp index aec41b4f6..3d3973cd2 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -46,6 +46,7 @@ #include "vehicle_func.h" #include "sound_func.h" #include "string_func.h" +#include "road_func.h" /* TTDPatch extended GRF format codec * (c) Petr Baudis 2004 (GPL'd) diff --git a/src/newgrf_engine.cpp b/src/newgrf_engine.cpp index 8dea0c7ea..5a3de90e2 100644 --- a/src/newgrf_engine.cpp +++ b/src/newgrf_engine.cpp @@ -21,6 +21,7 @@ #include "date_func.h" #include "vehicle_func.h" #include "core/random_func.hpp" +#include "direction_func.h" int _traininfo_vehicle_pitch = 0; diff --git a/src/npf.cpp b/src/npf.cpp index 2031035b5..31e791674 100644 --- a/src/npf.cpp +++ b/src/npf.cpp @@ -668,7 +668,7 @@ static void NPFFollowTrack(AyStar* aystar, OpenListNode* current) * multiple targets that are spread around, we should perform a breadth first * search by specifiying CalcZero as our heuristic. */ -static NPFFoundTargetData NPFRouteInternal(AyStarNode* start1, AyStarNode* start2, NPFFindStationOrTileData* target, AyStar_EndNodeCheck target_proc, AyStar_CalculateH heuristic_proc, TransportType type, uint sub_type, Owner owner, RailTypeMask railtypes, uint reverse_penalty) +static NPFFoundTargetData NPFRouteInternal(AyStarNode* start1, AyStarNode* start2, NPFFindStationOrTileData* target, AyStar_EndNodeCheck target_proc, AyStar_CalculateH heuristic_proc, TransportType type, uint sub_type, Owner owner, RailTypes railtypes, uint reverse_penalty) { int r; NPFFoundTargetData result; @@ -727,7 +727,7 @@ static NPFFoundTargetData NPFRouteInternal(AyStarNode* start1, AyStarNode* start return result; } -NPFFoundTargetData NPFRouteToStationOrTileTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypeMask railtypes) +NPFFoundTargetData NPFRouteToStationOrTileTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypes railtypes) { AyStarNode start1; AyStarNode start2; @@ -744,12 +744,12 @@ NPFFoundTargetData NPFRouteToStationOrTileTwoWay(TileIndex tile1, Trackdir track return NPFRouteInternal(&start1, (IsValidTile(tile2) ? &start2 : NULL), target, NPFFindStationOrTile, NPFCalcStationOrTileHeuristic, type, sub_type, owner, railtypes, 0); } -NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypeMask railtypes) +NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypes railtypes) { return NPFRouteToStationOrTileTwoWay(tile, trackdir, INVALID_TILE, INVALID_TRACKDIR, target, type, sub_type, owner, railtypes); } -NPFFoundTargetData NPFRouteToDepotBreadthFirstTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, TransportType type, uint sub_type, Owner owner, RailTypeMask railtypes, uint reverse_penalty) +NPFFoundTargetData NPFRouteToDepotBreadthFirstTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, TransportType type, uint sub_type, Owner owner, RailTypes railtypes, uint reverse_penalty) { AyStarNode start1; AyStarNode start2; @@ -768,12 +768,12 @@ NPFFoundTargetData NPFRouteToDepotBreadthFirstTwoWay(TileIndex tile1, Trackdir t return NPFRouteInternal(&start1, (IsValidTile(tile2) ? &start2 : NULL), NULL, NPFFindDepot, NPFCalcZero, type, sub_type, owner, railtypes, reverse_penalty); } -NPFFoundTargetData NPFRouteToDepotBreadthFirst(TileIndex tile, Trackdir trackdir, TransportType type, uint sub_type, Owner owner, RailTypeMask railtypes) +NPFFoundTargetData NPFRouteToDepotBreadthFirst(TileIndex tile, Trackdir trackdir, TransportType type, uint sub_type, Owner owner, RailTypes railtypes) { return NPFRouteToDepotBreadthFirstTwoWay(tile, trackdir, INVALID_TILE, INVALID_TRACKDIR, type, sub_type, owner, railtypes, 0); } -NPFFoundTargetData NPFRouteToDepotTrialError(TileIndex tile, Trackdir trackdir, TransportType type, uint sub_type, Owner owner, RailTypeMask railtypes) +NPFFoundTargetData NPFRouteToDepotTrialError(TileIndex tile, Trackdir trackdir, TransportType type, uint sub_type, Owner owner, RailTypes railtypes) { /* Okay, what we're gonna do. First, we look at all depots, calculate * the manhatten distance to get to each depot. We then sort them by @@ -10,6 +10,7 @@ #include "vehicle_type.h" #include "tile_type.h" #include "track_type.h" +#include "core/bitmath_func.hpp" /* mowing grass */ enum { @@ -77,28 +78,28 @@ struct NPFFoundTargetData { /* Will search from the given tile and direction, for a route to the given * station for the given transport type. See the declaration of * NPFFoundTargetData above for the meaning of the result. */ -NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypeMask railtypes); +NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypes railtypes); /* Will search as above, but with two start nodes, the second being the * reverse. Look at the NPF_FLAG_REVERSE flag in the result node to see which * direction was taken (NPFGetBit(result.node, NPF_FLAG_REVERSE)) */ -NPFFoundTargetData NPFRouteToStationOrTileTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypeMask railtypes); +NPFFoundTargetData NPFRouteToStationOrTileTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypes railtypes); /* Will search a route to the closest depot. */ /* Search using breadth first. Good for little track choice and inaccurate * heuristic, such as railway/road.*/ -NPFFoundTargetData NPFRouteToDepotBreadthFirst(TileIndex tile, Trackdir trackdir, TransportType type, uint sub_type, Owner owner, RailTypeMask railtypes); +NPFFoundTargetData NPFRouteToDepotBreadthFirst(TileIndex tile, Trackdir trackdir, TransportType type, uint sub_type, Owner owner, RailTypes railtypes); /* Same as above but with two start nodes, the second being the reverse. Call * NPFGetBit(result.node, NPF_FLAG_REVERSE) to see from which node the path * orginated. All pathfs from the second node will have the given * reverse_penalty applied (NPF_TILE_LENGTH is the equivalent of one full * tile). */ -NPFFoundTargetData NPFRouteToDepotBreadthFirstTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, TransportType type, uint sub_type, Owner owner, RailTypeMask railtypes, uint reverse_penalty); +NPFFoundTargetData NPFRouteToDepotBreadthFirstTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, TransportType type, uint sub_type, Owner owner, RailTypes railtypes, uint reverse_penalty); /* Search by trying each depot in order of Manhattan Distance. Good for lots * of choices and accurate heuristics, such as water. */ -NPFFoundTargetData NPFRouteToDepotTrialError(TileIndex tile, Trackdir trackdir, TransportType type, uint sub_type, Owner owner, RailTypeMask railtypes); +NPFFoundTargetData NPFRouteToDepotTrialError(TileIndex tile, Trackdir trackdir, TransportType type, uint sub_type, Owner owner, RailTypes railtypes); void NPFFillWithOrderData(NPFFindStationOrTileData* fstd, Vehicle* v); diff --git a/src/pathfind.cpp b/src/pathfind.cpp index 616f4e0eb..eafee883a 100644 --- a/src/pathfind.cpp +++ b/src/pathfind.cpp @@ -442,7 +442,7 @@ struct NewTrackPathFinder { TileIndex dest; TransportType tracktype; - RailTypeMask railtypes; + RailTypes railtypes; uint maxlength; HashLink *new_link; @@ -938,7 +938,7 @@ start_at: /** new pathfinder for trains. better and faster. */ -void NewTrainPathfind(TileIndex tile, TileIndex dest, RailTypeMask railtypes, DiagDirection direction, NTPEnumProc* enum_proc, void* data) +void NewTrainPathfind(TileIndex tile, TileIndex dest, RailTypes railtypes, DiagDirection direction, NTPEnumProc* enum_proc, void* data) { NewTrackPathFinder tpf; diff --git a/src/pathfind.h b/src/pathfind.h index f089e8e16..d01dcc564 100644 --- a/src/pathfind.h +++ b/src/pathfind.h @@ -76,6 +76,6 @@ struct FindLengthOfTunnelResult { }; FindLengthOfTunnelResult FindLengthOfTunnel(TileIndex tile, DiagDirection direction); -void NewTrainPathfind(TileIndex tile, TileIndex dest, RailTypeMask railtypes, DiagDirection direction, NTPEnumProc* enum_proc, void* data); +void NewTrainPathfind(TileIndex tile, TileIndex dest, RailTypes railtypes, DiagDirection direction, NTPEnumProc* enum_proc, void* data); #endif /* PATHFIND_H */ diff --git a/src/player.h b/src/player.h index 1d17c7f26..5ebe5d37d 100644 --- a/src/player.h +++ b/src/player.h @@ -5,11 +5,11 @@ #ifndef PLAYER_H #define PLAYER_H -#include "road_func.h" +#include "road_type.h" +#include "rail_type.h" #include "date_type.h" #include "engine.h" #include "livery.h" -#include "genworld.h" #include "autoreplace_type.h" struct PlayerEconomyEntry { @@ -41,8 +41,8 @@ struct Player { byte player_color; Livery livery[LS_END]; byte player_money_fraction; - byte avail_railtypes; - byte avail_roadtypes; + RailTypes avail_railtypes; + RoadTypes avail_roadtypes; byte block_preview; PlayerByte index; @@ -119,37 +119,6 @@ static inline bool IsValidPlayer(PlayerID pi) return IsInsideBS(pi, PLAYER_FIRST, MAX_PLAYERS); } -byte GetPlayerRailtypes(PlayerID p); -byte GetPlayerRoadtypes(PlayerID p); - -/** Finds out if a Player has a certain railtype available - * @param p Player in question - * @param Railtype requested RailType - * @return true if player has requested RailType available - */ -static inline bool HasRailtypeAvail(const Player *p, const RailType Railtype) -{ - return HasBit(p->avail_railtypes, Railtype); -} - -/** Finds out, whether given player has all given RoadTypes available - * @param PlayerID ID of player - * @param rts RoadTypes to test - * @return true if player has all requested RoadTypes available - */ -static inline bool HasRoadTypesAvail(const PlayerID p, const RoadTypes rts) -{ - RoadTypes avail_roadtypes; - - if (p == OWNER_TOWN || _game_mode == GM_EDITOR || IsGeneratingWorld()) { - avail_roadtypes = ROADTYPES_ROAD; - } else { - if (!IsValidPlayer(p)) return false; - avail_roadtypes = (RoadTypes)GetPlayer(p)->avail_roadtypes | ROADTYPES_ROAD; // road is available for always for everybody - } - return (rts & ~avail_roadtypes) == 0; -} - static inline bool IsHumanPlayer(PlayerID pi) { return !GetPlayer(pi)->is_ai; @@ -162,26 +131,6 @@ static inline bool IsInteractivePlayer(PlayerID pi) void DrawPlayerIcon(PlayerID p, int x, int y); -/* Validate functions for rail building */ -static inline bool ValParamRailtype(const uint32 rail) { return HasBit(GetPlayer(_current_player)->avail_railtypes, rail);} - -/* Validate functions for road building */ -static inline bool ValParamRoadType(const RoadType rt) { return HasRoadTypesAvail(_current_player, RoadTypeToRoadTypes(rt));} - -/** Returns the "best" railtype a player can build. - * As the AI doesn't know what the BEST one is, we have our own priority list - * here. When adding new railtypes, modify this function - * @param p the player "in action" - * @return The "best" railtype a player has available - */ -static inline RailType GetBestRailtype(const Player *p) -{ - if (HasRailtypeAvail(p, RAILTYPE_MAGLEV)) return RAILTYPE_MAGLEV; - if (HasRailtypeAvail(p, RAILTYPE_MONO)) return RAILTYPE_MONO; - if (HasRailtypeAvail(p, RAILTYPE_ELECTRIC)) return RAILTYPE_ELECTRIC; - return RAILTYPE_RAIL; -} - struct HighScore { char company[100]; StringID title; ///< NO_SAVE, has troubles with changing string-numbers. diff --git a/src/player_face.h b/src/player_face.h index 7f77dcaa6..eb75e47bc 100644 --- a/src/player_face.h +++ b/src/player_face.h @@ -6,6 +6,7 @@ #define PLAYER_FACE_H #include "core/random_func.hpp" +#include "core/bitmath_func.hpp" /** The gender/race combinations that we have faces for */ enum GenderEthnicity { diff --git a/src/players.cpp b/src/players.cpp index b0314f685..9c0ee2e67 100644 --- a/src/players.cpp +++ b/src/players.cpp @@ -32,6 +32,8 @@ #include "string_func.h" #include "ai/default/default.h" #include "ai/trolly/trolly.h" +#include "road_func.h" +#include "rail.h" /** * Sets the local player and updates the patch settings that are set on a @@ -590,47 +592,6 @@ void PlayersYearlyLoop() } } -byte GetPlayerRailtypes(PlayerID p) -{ - byte rt = 0; - EngineID i; - - for (i = 0; i != TOTAL_NUM_ENGINES; i++) { - const Engine* e = GetEngine(i); - const EngineInfo *ei = EngInfo(i); - - if (e->type == VEH_TRAIN && HasBit(ei->climates, _opt.landscape) && - (HasBit(e->player_avail, p) || _date >= e->intro_date + 365)) { - const RailVehicleInfo *rvi = RailVehInfo(i); - - if (rvi->railveh_type != RAILVEH_WAGON) { - assert(rvi->railtype < RAILTYPE_END); - SetBit(rt, rvi->railtype); - } - } - } - - return rt; -} - -byte GetPlayerRoadtypes(PlayerID p) -{ - byte rt = 0; - EngineID i; - - for (i = 0; i != TOTAL_NUM_ENGINES; i++) { - const Engine* e = GetEngine(i); - const EngineInfo *ei = EngInfo(i); - - if (e->type == VEH_ROAD && HasBit(ei->climates, _opt.landscape) && - (HasBit(e->player_avail, p) || _date >= e->intro_date + 365)) { - SetBit(rt, HasBit(ei->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD); - } - } - - return rt; -} - static void DeletePlayerStuff(PlayerID pi) { Player *p; diff --git a/src/rail.cpp b/src/rail.cpp index ea8a78de3..b722fd5d8 100644 --- a/src/rail.cpp +++ b/src/rail.cpp @@ -9,6 +9,8 @@ #include "station_map.h" #include "tunnel_map.h" #include "tunnelbridge_map.h" +#include "settings_type.h" +#include "date_func.h" /* XXX: Below 3 tables store duplicate data. Maybe remove some? */ @@ -140,3 +142,43 @@ RailType GetTileRailType(TileIndex tile) } return INVALID_RAILTYPE; } + +bool HasRailtypeAvail(const PlayerID p, const RailType railtype) +{ + return HasBit(GetPlayer(p)->avail_railtypes, railtype); +} + +bool ValParamRailtype(const RailType rail) +{ + return HasRailtypeAvail(_current_player, rail); +} + +RailType GetBestRailtype(const PlayerID p) +{ + if (HasRailtypeAvail(p, RAILTYPE_MAGLEV)) return RAILTYPE_MAGLEV; + if (HasRailtypeAvail(p, RAILTYPE_MONO)) return RAILTYPE_MONO; + if (HasRailtypeAvail(p, RAILTYPE_ELECTRIC)) return RAILTYPE_ELECTRIC; + return RAILTYPE_RAIL; +} + +RailTypes GetPlayerRailtypes(PlayerID p) +{ + RailTypes rt = RAILTYPES_NONE; + + for (EngineID i = 0; i != TOTAL_NUM_ENGINES; i++) { + const Engine* e = GetEngine(i); + const EngineInfo *ei = EngInfo(i); + + if (e->type == VEH_TRAIN && HasBit(ei->climates, _opt.landscape) && + (HasBit(e->player_avail, p) || _date >= e->intro_date + 365)) { + const RailVehicleInfo *rvi = RailVehInfo(i); + + if (rvi->railveh_type != RAILVEH_WAGON) { + assert(rvi->railtype < RAILTYPE_END); + SetBit(rt, rvi->railtype); + } + } + } + + return rt; +} diff --git a/src/rail.h b/src/rail.h index 8ba804c6e..a789b57eb 100644 --- a/src/rail.h +++ b/src/rail.h @@ -64,10 +64,10 @@ struct RailtypeInfo { SpriteID snow_offset; /** bitmask to the OTHER railtypes on which an engine of THIS railtype generates power */ - RailTypeMask powered_railtypes; + RailTypes powered_railtypes; /** bitmask to the OTHER railtypes on which an engine of THIS railtype can physically travel */ - RailTypeMask compatible_railtypes; + RailTypes compatible_railtypes; /** * Offset between the current railtype and normal rail. This means that:<p> @@ -201,4 +201,35 @@ Foundation GetRailFoundation(Slope tileh, TrackBits bits); int32 SettingsDisableElrail(int32 p1); ///< _patches.disable_elrail callback +/** + * Finds out if a Player has a certain railtype available + * @param p Player in question + * @param railtype requested RailType + * @return true if player has requested RailType available + */ +bool HasRailtypeAvail(const PlayerID p, const RailType railtype); + +/** + * Validate functions for rail building. + * @param rail the railtype to check. + * @return true if the current player may build the rail. + */ +bool ValParamRailtype(const RailType rail); + +/** + * Returns the "best" railtype a player can build. + * As the AI doesn't know what the BEST one is, we have our own priority list + * here. When adding new railtypes, modify this function + * @param p the player "in action" + * @return The "best" railtype a player has available + */ +RailType GetBestRailtype(const PlayerID p); + +/** + * Get the rail types the given player can build. + * @param p the player to get the rail types for. + * @return the rail types. + */ +RailTypes GetPlayerRailtypes(const PlayerID p); + #endif /* RAIL_H */ diff --git a/src/rail_cmd.cpp b/src/rail_cmd.cpp index b8d8e6e54..b86616ba5 100644 --- a/src/rail_cmd.cpp +++ b/src/rail_cmd.cpp @@ -303,15 +303,13 @@ static inline bool ValParamTrackOrientation(Track track) {return IsValidTrack(tr CommandCost CmdBuildSingleRail(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) { Slope tileh; - RailType railtype; - Track track; + RailType railtype = (RailType)p1; + Track track = (Track)p2; TrackBits trackbit; CommandCost cost(EXPENSES_CONSTRUCTION); CommandCost ret; - if (!ValParamRailtype(p1) || !ValParamTrackOrientation((Track)p2)) return CMD_ERROR; - railtype = (RailType)p1; - track = (Track)p2; + if (!ValParamRailtype(railtype) || !ValParamTrackOrientation(track)) return CMD_ERROR; tileh = GetTileSlope(tile, NULL); trackbit = TrackToTrackBits(track); @@ -703,7 +701,7 @@ CommandCost CmdBuildTrainDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p Slope tileh; /* check railtype and valid direction for depot (0 through 3), 4 in total */ - if (!ValParamRailtype(p1)) return CMD_ERROR; + if (!ValParamRailtype((RailType)p1)) return CMD_ERROR; tileh = GetTileSlope(tile, NULL); @@ -1150,12 +1148,11 @@ void *UpdateTrainPowerProc(Vehicle *v, void *data) CommandCost CmdConvertRail(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) { CommandCost cost(EXPENSES_CONSTRUCTION); + RailType totype = (RailType)p2; - if (!ValParamRailtype(p2)) return CMD_ERROR; + if (!ValParamRailtype(totype)) return CMD_ERROR; if (p1 >= MapSize()) return CMD_ERROR; - RailType totype = (RailType)p2; - uint ex = TileX(tile); uint ey = TileY(tile); uint sx = TileX(p1); diff --git a/src/rail_gui.cpp b/src/rail_gui.cpp index 53a823ed1..b83a4efe6 100644 --- a/src/rail_gui.cpp +++ b/src/rail_gui.cpp @@ -1540,11 +1540,11 @@ void SetDefaultRailGui() switch (rt) { case RAILTYPE_END + 0: rt = RAILTYPE_RAIL; - while (rt < RAILTYPE_END && !HasRailtypeAvail(GetPlayer(_local_player), rt)) rt++; + while (rt < RAILTYPE_END && !HasRailtypeAvail(_local_player, rt)) rt++; break; case RAILTYPE_END + 1: - rt = GetBestRailtype(GetPlayer(_local_player)); + rt = GetBestRailtype(_local_player); break; default: diff --git a/src/rail_type.h b/src/rail_type.h index 05d28d2b0..c52e0251b 100644 --- a/src/rail_type.h +++ b/src/rail_type.h @@ -20,12 +20,27 @@ enum RailType { INVALID_RAILTYPE = 0xFF ///< Flag for invalid railtype }; -typedef byte RailTypeMask; - /** Allow incrementing of Track variables */ DECLARE_POSTFIX_INCREMENT(RailType); /** Define basic enum properties */ template <> struct EnumPropsT<RailType> : MakeEnumPropsT<RailType, byte, RAILTYPE_BEGIN, RAILTYPE_END, INVALID_RAILTYPE> {}; typedef TinyEnumT<RailType> RailTypeByte; +/** + * The different roadtypes we support, but then a bitmask of them + */ +enum RailTypes { + RAILTYPES_NONE = 0, ///< No rail types + RAILTYPES_RAIL = 1 << RAILTYPE_RAIL, ///< Non-electrified rails + RAILTYPES_ELECTRIC = 1 << RAILTYPE_ELECTRIC, ///< Electrified rails + RAILTYPES_MONO = 1 << RAILTYPE_MONO, ///< Monorail! + RAILTYPES_MAGLEV = 1 << RAILTYPE_MAGLEV, ///< Ever fast maglev + RAILTYPES_ALL = RAILTYPES_RAIL | RAILTYPES_ELECTRIC | RAILTYPES_MONO | RAILTYPES_MAGLEV, ///< All of them + RAILTYPES_END, ///< Used for iterations? + INVALID_RAILTYPES = 0xFF ///< Invalid railtypes +}; +DECLARE_ENUM_AS_BIT_SET(RailTypes); +template <> struct EnumPropsT<RailTypes> : MakeEnumPropsT<RailTypes, byte, RAILTYPES_NONE, RAILTYPES_END, INVALID_RAILTYPES> {}; +typedef TinyEnumT<RailTypes> RailTypesByte; + #endif /* RAIL_TYPE_H */ diff --git a/src/road.cpp b/src/road.cpp index 2107ec0e3..698bb37fe 100644 --- a/src/road.cpp +++ b/src/road.cpp @@ -6,6 +6,11 @@ #include "road_map.h" #include "road_internal.h" #include "water_map.h" +#include "genworld.h" +#include "player.h" +#include "engine.h" +#include "settings_type.h" +#include "date_func.h" bool IsPossibleCrossing(const TileIndex tile, Axis ax) { @@ -67,3 +72,38 @@ RoadBits CleanUpRoadBits(const TileIndex tile, RoadBits org_rb) return org_rb; } + +bool HasRoadTypesAvail(const PlayerID p, const RoadTypes rts) +{ + RoadTypes avail_roadtypes; + + if (p == OWNER_TOWN || _game_mode == GM_EDITOR || IsGeneratingWorld()) { + avail_roadtypes = ROADTYPES_ROAD; + } else { + if (!IsValidPlayer(p)) return false; + avail_roadtypes = (RoadTypes)GetPlayer(p)->avail_roadtypes | ROADTYPES_ROAD; // road is available for always for everybody + } + return (rts & ~avail_roadtypes) == 0; +} + +bool ValParamRoadType(const RoadType rt) +{ + return HasRoadTypesAvail(_current_player, RoadTypeToRoadTypes(rt)); +} + +RoadTypes GetPlayerRoadtypes(PlayerID p) +{ + RoadTypes rt = ROADTYPES_NONE; + + for (EngineID i = 0; i != TOTAL_NUM_ENGINES; i++) { + const Engine* e = GetEngine(i); + const EngineInfo *ei = EngInfo(i); + + if (e->type == VEH_ROAD && HasBit(ei->climates, _opt.landscape) && + (HasBit(e->player_avail, p) || _date >= e->intro_date + 365)) { + SetBit(rt, HasBit(ei->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD); + } + } + + return rt; +} diff --git a/src/road_func.h b/src/road_func.h index 4bec22415..59de2d0a5 100644 --- a/src/road_func.h +++ b/src/road_func.h @@ -113,4 +113,26 @@ static inline RoadBits DiagDirToRoadBits(DiagDirection d) return (RoadBits)(ROAD_NW << (3 ^ d)); } +/** + * Finds out, whether given player has all given RoadTypes available + * @param PlayerID ID of player + * @param rts RoadTypes to test + * @return true if player has all requested RoadTypes available + */ +bool HasRoadTypesAvail(const PlayerID p, const RoadTypes rts); + +/** + * Validate functions for rail building. + * @param rt road type to check. + * @return true if the current player may build the road. + */ +bool ValParamRoadType(const RoadType rt); + +/** + * Get the road types the given player can build. + * @param p the player to get the roadtypes for. + * @return the road types. + */ +RoadTypes GetPlayerRoadtypes(const PlayerID p); + #endif /* ROAD_FUNC_H */ diff --git a/src/road_type.h b/src/road_type.h index 36c01eec6..407ab894b 100644 --- a/src/road_type.h +++ b/src/road_type.h @@ -34,8 +34,12 @@ enum RoadTypes { ROADTYPES_ROADHWAY = ROADTYPES_ROAD | ROADTYPES_HWAY, ///< Road + highway (or whatever substitute) ROADTYPES_TRAMHWAY = ROADTYPES_TRAM | ROADTYPES_HWAY, ///< Trams + highway (or whatever substitute) ROADTYPES_ALL = ROADTYPES_ROAD | ROADTYPES_TRAM | ROADTYPES_HWAY, ///< Road + trams + highway (or whatever substitute) + ROADTYPES_END, ///< Used for iterations? + INVALID_ROADTYPES = 0xFF ///< Invalid roadtypes }; DECLARE_ENUM_AS_BIT_SET(RoadTypes); +template <> struct EnumPropsT<RoadTypes> : MakeEnumPropsT<RoadTypes, byte, ROADTYPES_NONE, ROADTYPES_END, INVALID_ROADTYPES> {}; +typedef TinyEnumT<RoadTypes> RoadTypesByte; /** diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp index b3340c0a2..b445cdd03 100644 --- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -422,7 +422,7 @@ static const Depot* FindClosestRoadDepot(const Vehicle* v) /* See where we are now */ Trackdir trackdir = GetVehicleTrackdir(v); - ftd = NPFRouteToDepotBreadthFirstTwoWay(v->tile, trackdir, v->tile, ReverseTrackdir(trackdir), TRANSPORT_ROAD, v->u.road.compatible_roadtypes, v->owner, INVALID_RAILTYPE, 0); + ftd = NPFRouteToDepotBreadthFirstTwoWay(v->tile, trackdir, v->tile, ReverseTrackdir(trackdir), TRANSPORT_ROAD, v->u.road.compatible_roadtypes, v->owner, INVALID_RAILTYPES, 0); if (ftd.best_bird_dist == 0) { return GetDepotByTile(ftd.node.tile); /* Target found */ } else { @@ -1126,7 +1126,7 @@ static bool EnumRoadTrackFindDist(TileIndex tile, void* data, Trackdir trackdir, return false; } -static inline NPFFoundTargetData PerfNPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypeMask railtypes) +static inline NPFFoundTargetData PerfNPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypes railtypes) { void* perf = NpfBeginInterval(); @@ -1228,7 +1228,7 @@ static Trackdir RoadFindPathToDest(Vehicle* v, TileIndex tile, DiagDirection ent trackdir = DiagdirToDiagTrackdir(enterdir); //debug("Finding path. Enterdir: %d, Trackdir: %d", enterdir, trackdir); - ftd = PerfNPFRouteToStationOrTile(tile - TileOffsByDiagDir(enterdir), trackdir, &fstd, TRANSPORT_ROAD, v->u.road.compatible_roadtypes, v->owner, INVALID_RAILTYPE); + ftd = PerfNPFRouteToStationOrTile(tile - TileOffsByDiagDir(enterdir), trackdir, &fstd, TRANSPORT_ROAD, v->u.road.compatible_roadtypes, v->owner, INVALID_RAILTYPES); if (ftd.best_trackdir == INVALID_TRACKDIR) { /* We are already at our target. Just do something * @todo: maybe display error? @@ -1310,7 +1310,7 @@ static uint RoadFindPathToStop(const Vehicle *v, TileIndex tile) fstd.dest_coords = tile; fstd.station_index = INVALID_STATION; // indicates that the destination is a tile, not a station - dist = NPFRouteToStationOrTile(v->tile, trackdir, &fstd, TRANSPORT_ROAD, v->u.road.compatible_roadtypes, v->owner, INVALID_RAILTYPE).best_path_dist; + dist = NPFRouteToStationOrTile(v->tile, trackdir, &fstd, TRANSPORT_ROAD, v->u.road.compatible_roadtypes, v->owner, INVALID_RAILTYPES).best_path_dist; /* change units from NPF_TILE_LENGTH to # of tiles */ if (dist != UINT_MAX) dist = (dist + NPF_TILE_LENGTH - 1) / NPF_TILE_LENGTH; diff --git a/src/ship_cmd.cpp b/src/ship_cmd.cpp index 1e3aeed06..0d4dff71e 100644 --- a/src/ship_cmd.cpp +++ b/src/ship_cmd.cpp @@ -121,7 +121,7 @@ static const Depot* FindClosestShipDepot(const Vehicle* v) if (_patches.new_pathfinding_all) { NPFFoundTargetData ftd; Trackdir trackdir = GetVehicleTrackdir(v); - ftd = NPFRouteToDepotTrialError(v->tile, trackdir, TRANSPORT_WATER, 0, v->owner, INVALID_RAILTYPE); + ftd = NPFRouteToDepotTrialError(v->tile, trackdir, TRANSPORT_WATER, 0, v->owner, INVALID_RAILTYPES); if (ftd.best_bird_dist == 0) { best_depot = GetDepotByTile(ftd.node.tile); /* Found target */ } else { @@ -508,7 +508,7 @@ bad:; return best_bird_dist; } -static inline NPFFoundTargetData PerfNPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, NPFFindStationOrTileData* target, TransportType type, Owner owner, RailTypeMask railtypes) +static inline NPFFoundTargetData PerfNPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, NPFFindStationOrTileData* target, TransportType type, Owner owner, RailTypes railtypes) { void* perf = NpfBeginInterval(); @@ -537,7 +537,7 @@ static Track ChooseShipTrack(Vehicle *v, TileIndex tile, DiagDirection enterdir, NPFFillWithOrderData(&fstd, v); - ftd = PerfNPFRouteToStationOrTile(src_tile, trackdir, &fstd, TRANSPORT_WATER, v->owner, INVALID_RAILTYPE); + ftd = PerfNPFRouteToStationOrTile(src_tile, trackdir, &fstd, TRANSPORT_WATER, v->owner, INVALID_RAILTYPES); if (ftd.best_trackdir != 0xff) { /* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index cd52c5d51..dbe6808a4 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -905,7 +905,7 @@ CommandCost CmdBuildRailroadStation(TileIndex tile_org, uint32 flags, uint32 p1, /* Does the authority allow this? */ if (!(flags & DC_NO_TOWN_RATING) && !CheckIfAuthorityAllows(tile_org)) return CMD_ERROR; - if (!ValParamRailtype(p2 & 0xF)) return CMD_ERROR; + if (!ValParamRailtype((RailType)(p2 & 0xF))) return CMD_ERROR; /* unpack parameters */ Axis axis = Extract<Axis, 0>(p1); diff --git a/src/table/railtypes.h b/src/table/railtypes.h index db5e4a94d..42738b80b 100644 --- a/src/table/railtypes.h +++ b/src/table/railtypes.h @@ -44,10 +44,10 @@ RailtypeInfo _railtypes[] = { SPR_RAIL_SNOW_OFFSET, /* Powered railtypes */ - 1 << RAILTYPE_RAIL | 1 << RAILTYPE_ELECTRIC, + RAILTYPES_RAIL | RAILTYPES_ELECTRIC, /* Compatible railtypes */ - 1 << RAILTYPE_RAIL | 1 << RAILTYPE_ELECTRIC, + RAILTYPES_RAIL | RAILTYPES_ELECTRIC, /* main offset */ 0, @@ -100,10 +100,10 @@ RailtypeInfo _railtypes[] = { SPR_RAIL_SNOW_OFFSET, /* Powered railtypes */ - 1 << RAILTYPE_ELECTRIC, + RAILTYPES_ELECTRIC, /* Compatible railtypes */ - 1 << RAILTYPE_ELECTRIC | 1 << RAILTYPE_RAIL, + RAILTYPES_ELECTRIC | RAILTYPES_RAIL, /* main offset */ 0, @@ -152,10 +152,10 @@ RailtypeInfo _railtypes[] = { SPR_MONO_SNOW_OFFSET, /* Powered railtypes */ - 1 << RAILTYPE_MONO, + RAILTYPES_MONO, /* Compatible Railtypes */ - 1 << RAILTYPE_MONO, + RAILTYPES_MONO, /* main offset */ 82, @@ -204,10 +204,10 @@ RailtypeInfo _railtypes[] = { SPR_MGLV_SNOW_OFFSET, /* Powered railtypes */ - 1 << RAILTYPE_MAGLEV, + RAILTYPES_MAGLEV, /* Compatible Railtypes */ - 1 << RAILTYPE_MAGLEV, + RAILTYPES_MAGLEV, /* main offset */ 164, diff --git a/src/terraform_gui.cpp b/src/terraform_gui.cpp index 24106090f..79d7bc124 100644 --- a/src/terraform_gui.cpp +++ b/src/terraform_gui.cpp @@ -21,6 +21,7 @@ #include "station.h" #include "unmovable_map.h" #include "textbuf_gui.h" +#include "genworld.h" void CcTerraform(bool success, TileIndex tile, uint32 p1, uint32 p2) { diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index da0477c5a..190bf5ae2 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -165,7 +165,7 @@ void TrainConsistChanged(Vehicle* v) const RailVehicleInfo *rvi_v = RailVehInfo(v->engine_type); EngineID first_engine = IsFrontEngine(v) ? v->engine_type : INVALID_ENGINE; v->u.rail.cached_total_length = 0; - v->u.rail.compatible_railtypes = 0; + v->u.rail.compatible_railtypes = RAILTYPES_NONE; bool train_can_tilt = true; @@ -230,7 +230,7 @@ void TrainConsistChanged(Vehicle* v) * existing electric engines when elrails are disabled and then re-enabled */ if (HasBit(u->u.rail.flags, VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL)) { u->u.rail.railtype = RAILTYPE_RAIL; - u->u.rail.compatible_railtypes |= (1 << RAILTYPE_RAIL); + u->u.rail.compatible_railtypes |= RAILTYPES_RAIL; } /* max speed is the minimum of the speed limits of all vehicles in the consist */ diff --git a/src/tunnelbridge_cmd.cpp b/src/tunnelbridge_cmd.cpp index 9148c9255..2b8964e78 100644 --- a/src/tunnelbridge_cmd.cpp +++ b/src/tunnelbridge_cmd.cpp @@ -214,7 +214,7 @@ CommandCost CmdBuildBridge(TileIndex end_tile, uint32 flags, uint32 p1, uint32 p roadtypes = (RoadTypes)GB(p2, 8, 3); if (!AreValidRoadTypes(roadtypes) || !HasRoadTypesAvail(_current_player, roadtypes)) return CMD_ERROR; } else { - if (!ValParamRailtype(GB(p2, 8, 8))) return CMD_ERROR; + if (!ValParamRailtype((RailType)GB(p2, 8, 8))) return CMD_ERROR; railtype = (RailType)GB(p2, 8, 8); roadtypes = ROADTYPES_NONE; } @@ -462,7 +462,7 @@ CommandCost CmdBuildTunnel(TileIndex start_tile, uint32 flags, uint32 p1, uint32 _build_tunnel_endtile = 0; if (!HasBit(p1, 9)) { - if (!ValParamRailtype(p1)) return CMD_ERROR; + if (!ValParamRailtype((RailType)p1)) return CMD_ERROR; } else { const RoadTypes rts = (RoadTypes)GB(p1, 0, 3); if (!AreValidRoadTypes(rts) || !HasRoadTypesAvail(_current_player, rts)) return CMD_ERROR; diff --git a/src/vehicle_base.h b/src/vehicle_base.h index ad8bffd4b..734137667 100644 --- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -106,7 +106,7 @@ struct VehicleRail { TrackBitsByte track; byte force_proceed; RailTypeByte railtype; - RailTypeMask compatible_railtypes; + RailTypes compatible_railtypes; byte flags; diff --git a/src/yapf/yapf_destrail.hpp b/src/yapf/yapf_destrail.hpp index 157c3b6d2..59fa71d0c 100644 --- a/src/yapf/yapf_destrail.hpp +++ b/src/yapf/yapf_destrail.hpp @@ -8,7 +8,7 @@ class CYapfDestinationRailBase { protected: - RailTypeMask m_compatible_railtypes; + RailTypes m_compatible_railtypes; public: void SetDestination(Vehicle* v) |