2019-04-06 06:46:15 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file newgrf_roadtype.cpp NewGRF handling of road types. */
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
2023-05-18 09:20:35 +00:00
|
|
|
#include "core/container_func.hpp"
|
2019-04-06 06:46:15 +00:00
|
|
|
#include "debug.h"
|
|
|
|
#include "newgrf_roadtype.h"
|
|
|
|
#include "date_func.h"
|
|
|
|
#include "depot_base.h"
|
|
|
|
#include "town.h"
|
|
|
|
|
|
|
|
#include "safeguards.h"
|
|
|
|
|
2024-01-07 16:41:53 +00:00
|
|
|
/* virtual */ uint32_t RoadTypeScopeResolver::GetRandomBits() const
|
2019-04-06 06:46:15 +00:00
|
|
|
{
|
|
|
|
uint tmp = CountBits(this->tile + (TileX(this->tile) + TileY(this->tile)) * TILE_SIZE);
|
|
|
|
return GB(tmp, 0, 2);
|
|
|
|
}
|
|
|
|
|
2024-01-07 16:41:53 +00:00
|
|
|
/* virtual */ uint32_t RoadTypeScopeResolver::GetVariable(uint16_t variable, uint32_t parameter, GetVariableExtra *extra) const
|
2019-04-06 06:46:15 +00:00
|
|
|
{
|
|
|
|
if (this->tile == INVALID_TILE) {
|
|
|
|
switch (variable) {
|
|
|
|
case 0x40: return 0;
|
|
|
|
case 0x41: return 0;
|
|
|
|
case 0x42: return 0;
|
2023-12-19 01:03:18 +00:00
|
|
|
case 0x43: return _date.base();
|
2019-04-06 06:46:15 +00:00
|
|
|
case 0x44: return HZB_TOWN_EDGE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (variable) {
|
|
|
|
case 0x40: return GetTerrainType(this->tile, this->context);
|
|
|
|
case 0x41: return 0;
|
|
|
|
case 0x42: return IsLevelCrossingTile(this->tile) && IsCrossingBarred(this->tile);
|
|
|
|
case 0x43:
|
2023-12-19 01:03:18 +00:00
|
|
|
if (IsRoadDepotTile(this->tile)) return Depot::GetByTile(this->tile)->build_date.base();
|
|
|
|
return _date.base();
|
2019-04-06 06:46:15 +00:00
|
|
|
case 0x44: {
|
|
|
|
const Town *t = nullptr;
|
|
|
|
if (IsRoadDepotTile(this->tile)) {
|
|
|
|
t = Depot::GetByTile(this->tile)->town;
|
2020-12-21 22:46:38 +00:00
|
|
|
} else {
|
2019-04-06 06:46:15 +00:00
|
|
|
t = ClosestTownFromTile(this->tile, UINT_MAX);
|
|
|
|
}
|
|
|
|
return t != nullptr ? GetTownRadiusGroup(t, this->tile) : HZB_TOWN_EDGE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG(grf, 1, "Unhandled road type tile variable 0x%X", variable);
|
|
|
|
|
2020-08-25 00:26:44 +00:00
|
|
|
extra->available = false;
|
2019-04-06 06:46:15 +00:00
|
|
|
return UINT_MAX;
|
|
|
|
}
|
|
|
|
|
2020-01-26 12:45:51 +00:00
|
|
|
GrfSpecFeature RoadTypeResolverObject::GetFeature() const
|
|
|
|
{
|
|
|
|
RoadType rt = GetRoadTypeByLabel(this->roadtype_scope.rti->label, false);
|
|
|
|
switch (GetRoadTramType(rt)) {
|
|
|
|
case RTT_ROAD: return GSF_ROADTYPES;
|
|
|
|
case RTT_TRAM: return GSF_TRAMTYPES;
|
|
|
|
default: return GSF_INVALID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-07 16:41:53 +00:00
|
|
|
uint32_t RoadTypeResolverObject::GetDebugID() const
|
2020-01-26 12:45:51 +00:00
|
|
|
{
|
|
|
|
return this->roadtype_scope.rti->label;
|
|
|
|
}
|
|
|
|
|
2019-04-06 06:46:15 +00:00
|
|
|
/**
|
|
|
|
* Resolver object for road types.
|
|
|
|
* @param rti Roadtype. nullptr in NewGRF Inspect window.
|
|
|
|
* @param tile %Tile containing the track. For track on a bridge this is the southern bridgehead.
|
|
|
|
* @param context Are we resolving sprites for the upper halftile, or on a bridge?
|
|
|
|
* @param rtsg Roadpart of interest
|
|
|
|
* @param param1 Extra parameter (first parameter of the callback, except roadtypes do not have callbacks).
|
|
|
|
* @param param2 Extra parameter (second parameter of the callback, except roadtypes do not have callbacks).
|
|
|
|
*/
|
2024-01-07 16:41:53 +00:00
|
|
|
RoadTypeResolverObject::RoadTypeResolverObject(const RoadTypeInfo *rti, TileIndex tile, TileContext context, RoadTypeSpriteGroup rtsg, uint32_t param1, uint32_t param2)
|
2020-01-26 12:45:51 +00:00
|
|
|
: ResolverObject(rti != nullptr ? rti->grffile[rtsg] : nullptr, CBID_NO_CALLBACK, param1, param2), roadtype_scope(*this, rti, tile, context)
|
2019-04-06 06:46:15 +00:00
|
|
|
{
|
|
|
|
this->root_spritegroup = rti != nullptr ? rti->group[rtsg] : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the sprite to draw for the given tile.
|
|
|
|
* @param rti The road type data (spec).
|
|
|
|
* @param tile The tile to get the sprite for.
|
|
|
|
* @param rtsg The type of sprite to draw.
|
|
|
|
* @param content Where are we drawing the tile?
|
|
|
|
* @param [out] num_results If not nullptr, return the number of sprites in the spriteset.
|
|
|
|
* @return The sprite to draw.
|
|
|
|
*/
|
|
|
|
SpriteID GetCustomRoadSprite(const RoadTypeInfo *rti, TileIndex tile, RoadTypeSpriteGroup rtsg, TileContext context, uint *num_results)
|
|
|
|
{
|
|
|
|
assert(rtsg < ROTSG_END);
|
|
|
|
|
|
|
|
if (rti->group[rtsg] == nullptr) return 0;
|
|
|
|
|
|
|
|
RoadTypeResolverObject object(rti, tile, context, rtsg);
|
|
|
|
const SpriteGroup *group = object.Resolve();
|
|
|
|
if (group == nullptr || group->GetNumResults() == 0) return 0;
|
|
|
|
|
|
|
|
if (num_results) *num_results = group->GetNumResults();
|
|
|
|
|
|
|
|
return group->GetResult();
|
|
|
|
}
|
|
|
|
|
2021-01-10 13:37:40 +00:00
|
|
|
/**
|
|
|
|
* Translate an index to the GRF-local road/tramtype-translation table into a RoadType.
|
|
|
|
* @param rtt Whether to index the road- or tramtype-table.
|
|
|
|
* @param tracktype Index into GRF-local translation table.
|
|
|
|
* @param grffile Originating GRF file.
|
|
|
|
* @return RoadType or INVALID_ROADTYPE if the roadtype is unknown.
|
|
|
|
*/
|
2024-01-07 16:41:53 +00:00
|
|
|
RoadType GetRoadTypeTranslation(RoadTramType rtt, uint8_t tracktype, const GRFFile *grffile)
|
2021-01-10 13:37:40 +00:00
|
|
|
{
|
|
|
|
/* Because OpenTTD mixes RoadTypes and TramTypes into the same type,
|
|
|
|
* the mapping of the original road- and tramtypes does not match the default GRF-local mapping.
|
|
|
|
* So, this function cannot provide any similar behavior to GetCargoTranslation() and GetRailTypeTranslation()
|
|
|
|
* when the GRF defines no translation table.
|
|
|
|
* But since there is only one default road/tram-type, this makes little sense anyway.
|
|
|
|
* So for GRF without translation table, we always return INVALID_ROADTYPE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (grffile == nullptr) return INVALID_ROADTYPE;
|
|
|
|
|
|
|
|
const auto &list = rtt == RTT_TRAM ? grffile->tramtype_list : grffile->roadtype_list;
|
|
|
|
if (tracktype >= list.size()) return INVALID_ROADTYPE;
|
|
|
|
|
|
|
|
/* Look up roadtype including alternate labels. */
|
|
|
|
RoadType result = GetRoadTypeByLabel(list[tracktype]);
|
|
|
|
|
|
|
|
/* Check whether the result is actually the wanted road/tram-type */
|
|
|
|
if (result != INVALID_ROADTYPE && GetRoadTramType(result) != rtt) return INVALID_ROADTYPE;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-04-06 06:46:15 +00:00
|
|
|
/**
|
|
|
|
* Perform a reverse roadtype lookup to get the GRF internal ID.
|
|
|
|
* @param roadtype The global (OpenTTD) roadtype.
|
|
|
|
* @param grffile The GRF to do the lookup for.
|
|
|
|
* @return the GRF internal ID.
|
|
|
|
*/
|
2024-01-07 16:41:53 +00:00
|
|
|
uint8_t GetReverseRoadTypeTranslation(RoadType roadtype, const GRFFile *grffile)
|
2019-04-06 06:46:15 +00:00
|
|
|
{
|
|
|
|
/* No road type table present, return road type as-is */
|
|
|
|
if (grffile == nullptr) return roadtype;
|
|
|
|
|
|
|
|
const std::vector<RoadTypeLabel> *list = RoadTypeIsRoad(roadtype) ? &grffile->roadtype_list : &grffile->tramtype_list;
|
2023-10-20 18:09:58 +00:00
|
|
|
if (list->empty()) return roadtype;
|
2019-04-06 06:46:15 +00:00
|
|
|
|
|
|
|
/* Look for a matching road type label in the table */
|
|
|
|
RoadTypeLabel label = GetRoadTypeInfo(roadtype)->label;
|
|
|
|
|
|
|
|
int index = find_index(*list, label);
|
|
|
|
if (index >= 0) return index;
|
|
|
|
|
|
|
|
/* If not found, return as invalid */
|
|
|
|
return 0xFF;
|
|
|
|
}
|
2023-02-25 16:16:49 +00:00
|
|
|
|
2023-12-27 23:46:45 +00:00
|
|
|
void DumpRoadTypeSpriteGroup(RoadType rt, SpriteGroupDumper &dumper)
|
2023-02-25 16:16:49 +00:00
|
|
|
{
|
|
|
|
char buffer[64];
|
|
|
|
const RoadTypeInfo *rti = GetRoadTypeInfo(rt);
|
|
|
|
|
|
|
|
static const char *sprite_group_names[] = {
|
|
|
|
"ROTSG_CURSORS",
|
|
|
|
"ROTSG_OVERLAY",
|
|
|
|
"ROTSG_GROUND",
|
|
|
|
"ROTSG_TUNNEL",
|
|
|
|
"ROTSG_CATENARY_FRONT",
|
|
|
|
"ROTSG_CATENARY_BACK",
|
|
|
|
"ROTSG_BRIDGE",
|
|
|
|
"ROTSG_reserved2",
|
|
|
|
"ROTSG_DEPOT",
|
|
|
|
"ROTSG_reserved3",
|
|
|
|
"ROTSG_ROADSTOP",
|
|
|
|
"ROTSG_ONEWAY"
|
|
|
|
};
|
|
|
|
static_assert(lengthof(sprite_group_names) == ROTSG_END);
|
|
|
|
|
|
|
|
for (RoadTypeSpriteGroup rtsg = (RoadTypeSpriteGroup)0; rtsg < ROTSG_END; rtsg = (RoadTypeSpriteGroup)(rtsg + 1)) {
|
|
|
|
if (rti->group[rtsg] != nullptr) {
|
|
|
|
char *b = buffer;
|
|
|
|
b += seprintf(b, lastof(buffer), "%s: %s", RoadTypeIsTram(rt) ? "Tram" : "Road", sprite_group_names[rtsg]);
|
|
|
|
if (rti->grffile[rtsg] != nullptr) {
|
|
|
|
b += seprintf(b, lastof(buffer), ", GRF: %08X", BSWAP32(rti->grffile[rtsg]->grfid));
|
|
|
|
}
|
2023-12-27 23:46:45 +00:00
|
|
|
dumper.Print(buffer);
|
2023-02-25 16:16:49 +00:00
|
|
|
dumper.DumpSpriteGroup(rti->group[rtsg], 0);
|
2023-12-27 23:46:45 +00:00
|
|
|
dumper.Print("");
|
2023-02-25 16:16:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|