2010-07-22 16:50:58 +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/>.
|
|
|
|
*/
|
|
|
|
|
2010-02-07 22:22:54 +00:00
|
|
|
/** @file newgrf_railtype.cpp NewGRF handling of rail types. */
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "debug.h"
|
2012-11-10 20:41:45 +00:00
|
|
|
#include "newgrf_railtype.h"
|
2021-08-19 23:49:48 +00:00
|
|
|
#include "newgrf_newsignals.h"
|
2022-06-12 08:00:31 +00:00
|
|
|
#include "newgrf_extension.h"
|
2010-06-20 19:13:02 +00:00
|
|
|
#include "date_func.h"
|
|
|
|
#include "depot_base.h"
|
2012-01-28 19:36:22 +00:00
|
|
|
#include "town.h"
|
2022-06-27 17:51:26 +00:00
|
|
|
#include "signal_func.h"
|
2010-02-07 22:22:54 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2012-11-10 20:41:45 +00:00
|
|
|
/* virtual */ uint32 RailTypeScopeResolver::GetRandomBits() const
|
2010-02-07 22:22:54 +00:00
|
|
|
{
|
2012-11-10 20:41:45 +00:00
|
|
|
uint tmp = CountBits(this->tile + (TileX(this->tile) + TileY(this->tile)) * TILE_SIZE);
|
2010-02-25 09:43:00 +00:00
|
|
|
return GB(tmp, 0, 2);
|
2010-02-07 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 01:17:35 +00:00
|
|
|
/* virtual */ uint32 RailTypeScopeResolver::GetVariable(uint16 variable, uint32 parameter, GetVariableExtra *extra) const
|
2010-02-07 22:22:54 +00:00
|
|
|
{
|
2012-11-10 20:41:45 +00:00
|
|
|
if (this->tile == INVALID_TILE) {
|
2010-02-07 22:22:54 +00:00
|
|
|
switch (variable) {
|
|
|
|
case 0x40: return 0;
|
|
|
|
case 0x41: return 0;
|
|
|
|
case 0x42: return 0;
|
2010-06-20 19:13:02 +00:00
|
|
|
case 0x43: return _date;
|
2012-01-28 19:36:22 +00:00
|
|
|
case 0x44: return HZB_TOWN_EDGE;
|
2022-06-12 08:00:31 +00:00
|
|
|
case A2VRI_RAILTYPE_SIGNAL_RESTRICTION_INFO: return 0;
|
2022-06-15 18:03:13 +00:00
|
|
|
case A2VRI_RAILTYPE_SIGNAL_CONTEXT: return this->signal_context;
|
2023-02-19 20:22:40 +00:00
|
|
|
case A2VRI_RAILTYPE_SIGNAL_SIDE: return GetNewSignalsSideVariable();
|
2023-04-04 19:29:22 +00:00
|
|
|
case A2VRI_RAILTYPE_SIGNAL_VERTICAL_CLEARANCE: return 0xFF;
|
2010-02-07 22:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (variable) {
|
2012-11-10 20:41:45 +00:00
|
|
|
case 0x40: return GetTerrainType(this->tile, this->context);
|
2010-02-07 22:22:54 +00:00
|
|
|
case 0x41: return 0;
|
2012-11-10 20:41:45 +00:00
|
|
|
case 0x42: return IsLevelCrossingTile(this->tile) && IsCrossingBarred(this->tile);
|
2010-06-20 19:13:02 +00:00
|
|
|
case 0x43:
|
2012-11-10 20:41:45 +00:00
|
|
|
if (IsRailDepotTile(this->tile)) return Depot::GetByTile(this->tile)->build_date;
|
2010-06-20 19:13:02 +00:00
|
|
|
return _date;
|
2012-01-28 19:36:22 +00:00
|
|
|
case 0x44: {
|
2019-04-10 21:07:06 +00:00
|
|
|
const Town *t = nullptr;
|
2012-11-10 20:41:45 +00:00
|
|
|
if (IsRailDepotTile(this->tile)) {
|
|
|
|
t = Depot::GetByTile(this->tile)->town;
|
|
|
|
} else if (IsLevelCrossingTile(this->tile)) {
|
|
|
|
t = ClosestTownFromTile(this->tile, UINT_MAX);
|
2012-01-28 19:36:22 +00:00
|
|
|
}
|
2019-04-10 21:07:06 +00:00
|
|
|
return t != nullptr ? GetTownRadiusGroup(t, this->tile) : HZB_TOWN_EDGE;
|
2012-01-28 19:36:22 +00:00
|
|
|
}
|
2022-06-12 08:00:31 +00:00
|
|
|
case A2VRI_RAILTYPE_SIGNAL_RESTRICTION_INFO:
|
2022-06-19 03:15:27 +00:00
|
|
|
return GetNewSignalsRestrictedSignalsInfo(this->prog, this->tile, 0);
|
2022-06-15 18:03:13 +00:00
|
|
|
case A2VRI_RAILTYPE_SIGNAL_CONTEXT:
|
|
|
|
return GetNewSignalsSignalContext(this->signal_context, this->tile);
|
2023-02-19 20:22:40 +00:00
|
|
|
case A2VRI_RAILTYPE_SIGNAL_SIDE:
|
|
|
|
return GetNewSignalsSideVariable();
|
2023-04-04 19:29:22 +00:00
|
|
|
case A2VRI_RAILTYPE_SIGNAL_VERTICAL_CLEARANCE:
|
|
|
|
return GetNewSignalsVerticalClearanceInfo(this->tile, this->z);
|
2010-02-07 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
2010-11-15 16:43:46 +00:00
|
|
|
DEBUG(grf, 1, "Unhandled rail type tile variable 0x%X", variable);
|
2010-02-07 22:22:54 +00:00
|
|
|
|
2020-08-25 00:26:44 +00:00
|
|
|
extra->available = false;
|
2010-02-07 22:22:54 +00:00
|
|
|
return UINT_MAX;
|
|
|
|
}
|
|
|
|
|
2020-01-26 12:45:51 +00:00
|
|
|
GrfSpecFeature RailTypeResolverObject::GetFeature() const
|
|
|
|
{
|
|
|
|
return GSF_RAILTYPES;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 RailTypeResolverObject::GetDebugID() const
|
|
|
|
{
|
|
|
|
return this->railtype_scope.rti->label;
|
|
|
|
}
|
|
|
|
|
2012-11-10 20:46:39 +00:00
|
|
|
/**
|
|
|
|
* Resolver object for rail types.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @param rti Railtype. nullptr in NewGRF Inspect window.
|
2012-11-10 20:46:39 +00:00
|
|
|
* @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?
|
2014-03-03 20:02:31 +00:00
|
|
|
* @param rtsg Railpart of interest
|
2012-11-10 20:46:39 +00:00
|
|
|
* @param param1 Extra parameter (first parameter of the callback, except railtypes do not have callbacks).
|
|
|
|
* @param param2 Extra parameter (second parameter of the callback, except railtypes do not have callbacks).
|
2022-06-15 18:03:13 +00:00
|
|
|
* @param signal_context Signal context.
|
2023-04-04 19:29:22 +00:00
|
|
|
* @param z Signal pixel z.
|
2022-06-12 08:00:31 +00:00
|
|
|
* @param prog Routing restriction program.
|
2012-11-10 20:46:39 +00:00
|
|
|
*/
|
2023-04-04 19:29:22 +00:00
|
|
|
RailTypeResolverObject::RailTypeResolverObject(const RailtypeInfo *rti, TileIndex tile, TileContext context, RailTypeSpriteGroup rtsg, uint32 param1, uint32 param2,
|
|
|
|
CustomSignalSpriteContext signal_context, const TraceRestrictProgram *prog, uint z)
|
|
|
|
: ResolverObject(rti != nullptr ? rti->grffile[rtsg] : nullptr, CBID_NO_CALLBACK, param1, param2), railtype_scope(*this, rti, tile, context, signal_context, prog, z)
|
2012-11-10 20:41:45 +00:00
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
this->root_spritegroup = rti != nullptr ? rti->group[rtsg] : nullptr;
|
2010-02-07 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
2011-05-02 16:14:23 +00:00
|
|
|
/**
|
|
|
|
* Get the sprite to draw for the given tile.
|
|
|
|
* @param rti The rail type data (spec).
|
|
|
|
* @param tile The tile to get the sprite for.
|
|
|
|
* @param rtsg The type of sprite to draw.
|
2018-10-28 02:17:36 +00:00
|
|
|
* @param context Where are we drawing the tile?
|
2019-04-10 21:07:06 +00:00
|
|
|
* @param[out] num_results If not nullptr, return the number of sprites in the spriteset.
|
2011-05-02 16:14:23 +00:00
|
|
|
* @return The sprite to draw.
|
|
|
|
*/
|
2015-07-26 12:25:37 +00:00
|
|
|
SpriteID GetCustomRailSprite(const RailtypeInfo *rti, TileIndex tile, RailTypeSpriteGroup rtsg, TileContext context, uint *num_results)
|
2010-02-07 22:22:54 +00:00
|
|
|
{
|
|
|
|
assert(rtsg < RTSG_END);
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (rti->group[rtsg] == nullptr) return 0;
|
2010-02-07 22:22:54 +00:00
|
|
|
|
2014-03-03 20:02:31 +00:00
|
|
|
RailTypeResolverObject object(rti, tile, context, rtsg);
|
|
|
|
const SpriteGroup *group = object.Resolve();
|
2019-04-10 21:07:06 +00:00
|
|
|
if (group == nullptr || group->GetNumResults() == 0) return 0;
|
2010-02-07 22:22:54 +00:00
|
|
|
|
2015-07-26 12:25:37 +00:00
|
|
|
if (num_results) *num_results = group->GetNumResults();
|
|
|
|
|
2010-02-07 22:22:54 +00:00
|
|
|
return group->GetResult();
|
|
|
|
}
|
|
|
|
|
2022-06-27 17:51:26 +00:00
|
|
|
inline uint8 RemapAspect(uint8 aspect, uint8 extra_aspects, uint8 style)
|
2021-08-28 11:51:06 +00:00
|
|
|
{
|
|
|
|
if (likely(extra_aspects == 0 || _extra_aspects == 0)) return std::min<uint8>(aspect, 1);
|
|
|
|
if (aspect == 0) return 0;
|
2022-06-27 17:51:26 +00:00
|
|
|
if (style != 0 && HasBit(_signal_style_masks.combined_normal_shunt, style)) {
|
|
|
|
if (aspect == 1) {
|
|
|
|
return 0xFF;
|
|
|
|
}
|
|
|
|
aspect--;
|
|
|
|
}
|
2021-08-28 11:51:06 +00:00
|
|
|
if (aspect >= extra_aspects + 1) return 1;
|
|
|
|
return aspect + 1;
|
|
|
|
}
|
|
|
|
|
2023-04-04 19:29:22 +00:00
|
|
|
static PalSpriteID GetRailTypeCustomSignalSprite(const RailtypeInfo *rti, TileIndex tile, SignalType type, SignalVariant var, uint8 aspect,
|
|
|
|
CustomSignalSpriteContext context, const TraceRestrictProgram *prog, uint z)
|
2021-08-19 23:49:48 +00:00
|
|
|
{
|
2021-08-21 00:07:41 +00:00
|
|
|
if (rti->group[RTSG_SIGNALS] == nullptr) return { 0, PAL_NONE };
|
|
|
|
if (type == SIGTYPE_PROG && !HasBit(rti->ctrl_flags, RTCF_PROGSIG)) return { 0, PAL_NONE };
|
2021-09-04 02:14:09 +00:00
|
|
|
if (type == SIGTYPE_NO_ENTRY && !HasBit(rti->ctrl_flags, RTCF_NOENTRYSIG)) return { 0, PAL_NONE };
|
2021-08-19 23:49:48 +00:00
|
|
|
|
2022-06-15 18:03:13 +00:00
|
|
|
uint32 param1 = (context == CSSC_GUI) ? 0x10 : 0x00;
|
2022-06-27 17:51:26 +00:00
|
|
|
uint32 param2 = (type << 16) | (var << 8) | RemapAspect(aspect, rti->signal_extra_aspects, 0);
|
2022-06-12 08:00:31 +00:00
|
|
|
if ((prog != nullptr) && HasBit(rti->ctrl_flags, RTCF_RESTRICTEDSIG)) SetBit(param2, 24);
|
2023-04-04 19:29:22 +00:00
|
|
|
RailTypeResolverObject object(rti, tile, TCX_NORMAL, RTSG_SIGNALS, param1, param2, context, prog, z);
|
2021-08-19 23:49:48 +00:00
|
|
|
|
|
|
|
const SpriteGroup *group = object.Resolve();
|
2021-08-21 00:07:41 +00:00
|
|
|
if (group == nullptr || group->GetNumResults() == 0) return { 0, PAL_NONE };
|
2021-08-19 23:49:48 +00:00
|
|
|
|
2021-08-21 00:07:41 +00:00
|
|
|
PaletteID pal = HasBit(rti->ctrl_flags, RTCF_RECOLOUR_ENABLED) ? GB(GetRegister(0x100), 0, 24) : PAL_NONE;
|
|
|
|
return { group->GetResult(), pal };
|
2021-08-19 23:49:48 +00:00
|
|
|
}
|
|
|
|
|
2012-07-01 23:12:50 +00:00
|
|
|
/**
|
|
|
|
* Get the sprite to draw for a given signal.
|
|
|
|
* @param rti The rail type data (spec).
|
|
|
|
* @param tile The tile to get the sprite for.
|
|
|
|
* @param type Signal type.
|
|
|
|
* @param var Signal variant.
|
|
|
|
* @param state Signal state.
|
|
|
|
* @param gui Is the sprite being used on the map or in the GUI?
|
|
|
|
* @return The sprite to draw.
|
|
|
|
*/
|
2023-04-04 19:29:22 +00:00
|
|
|
CustomSignalSpriteResult GetCustomSignalSprite(const RailtypeInfo *rti, TileIndex tile, SignalType type, SignalVariant var, uint8 aspect,
|
|
|
|
CustomSignalSpriteContext context, uint8 style, const TraceRestrictProgram *prog, uint z)
|
2012-07-01 23:12:50 +00:00
|
|
|
{
|
2022-06-18 22:37:35 +00:00
|
|
|
if (_settings_client.gui.show_all_signal_default && style == 0) return { { 0, PAL_NONE }, false };
|
2021-12-27 18:31:27 +00:00
|
|
|
|
2022-06-18 22:37:35 +00:00
|
|
|
if (style == 0) {
|
2023-04-04 19:29:22 +00:00
|
|
|
PalSpriteID spr = GetRailTypeCustomSignalSprite(rti, tile, type, var, aspect, context, prog, z);
|
2022-06-18 22:37:35 +00:00
|
|
|
if (spr.sprite != 0) return { spr, HasBit(rti->ctrl_flags, RTCF_RESTRICTEDSIG) };
|
|
|
|
}
|
2012-07-01 23:12:50 +00:00
|
|
|
|
2021-08-19 23:49:48 +00:00
|
|
|
for (const GRFFile *grf : _new_signals_grfs) {
|
2022-06-19 16:43:46 +00:00
|
|
|
if (style == 0) {
|
|
|
|
if (type == SIGTYPE_PROG && !HasBit(grf->new_signal_ctrl_flags, NSCF_PROGSIG)) continue;
|
|
|
|
if (type == SIGTYPE_NO_ENTRY && !HasBit(grf->new_signal_ctrl_flags, NSCF_NOENTRYSIG)) continue;
|
|
|
|
}
|
2022-06-18 22:37:35 +00:00
|
|
|
if (!HasBit(grf->new_signal_style_mask, style)) continue;
|
2012-07-01 23:12:50 +00:00
|
|
|
|
2022-06-15 18:03:13 +00:00
|
|
|
uint32 param1 = (context == CSSC_GUI) ? 0x10 : 0x00;
|
2022-06-27 17:51:26 +00:00
|
|
|
uint32 param2 = (type << 16) | (var << 8) | RemapAspect(aspect, grf->new_signal_extra_aspects, style);
|
2022-06-12 08:00:31 +00:00
|
|
|
if ((prog != nullptr) && HasBit(grf->new_signal_ctrl_flags, NSCF_RESTRICTEDSIG)) SetBit(param2, 24);
|
2023-04-04 19:29:22 +00:00
|
|
|
NewSignalsResolverObject object(grf, tile, TCX_NORMAL, param1, param2, context, style, prog, z);
|
2012-07-01 23:12:50 +00:00
|
|
|
|
2021-08-19 23:49:48 +00:00
|
|
|
const SpriteGroup *group = object.Resolve();
|
2021-08-21 00:07:41 +00:00
|
|
|
if (group != nullptr && group->GetNumResults() != 0) {
|
|
|
|
PaletteID pal = HasBit(grf->new_signal_ctrl_flags, NSCF_RECOLOUR_ENABLED) ? GB(GetRegister(0x100), 0, 24) : PAL_NONE;
|
|
|
|
return { { group->GetResult(), pal }, HasBit(grf->new_signal_ctrl_flags, NSCF_RESTRICTEDSIG) };
|
|
|
|
}
|
2021-08-19 23:49:48 +00:00
|
|
|
}
|
|
|
|
|
2021-08-21 00:07:41 +00:00
|
|
|
return { { 0, PAL_NONE }, false };
|
2012-07-01 23:12:50 +00:00
|
|
|
}
|
|
|
|
|
2021-01-10 13:37:40 +00:00
|
|
|
/**
|
|
|
|
* Translate an index to the GRF-local railtype-translation table into a RailType.
|
|
|
|
* @param railtype Index into GRF-local translation table.
|
|
|
|
* @param grffile Originating GRF file.
|
|
|
|
* @return RailType or INVALID_RAILTYPE if the railtype is unknown.
|
|
|
|
*/
|
|
|
|
RailType GetRailTypeTranslation(uint8 railtype, const GRFFile *grffile)
|
|
|
|
{
|
|
|
|
if (grffile == nullptr || grffile->railtype_list.size() == 0) {
|
|
|
|
/* No railtype table present. Return railtype as-is (if valid), so it works for original railtypes. */
|
|
|
|
if (railtype >= RAILTYPE_END || GetRailTypeInfo(static_cast<RailType>(railtype))->label == 0) return INVALID_RAILTYPE;
|
|
|
|
|
|
|
|
return static_cast<RailType>(railtype);
|
|
|
|
} else {
|
|
|
|
/* Railtype table present, but invalid index, return invalid type. */
|
|
|
|
if (railtype >= grffile->railtype_list.size()) return INVALID_RAILTYPE;
|
|
|
|
|
|
|
|
/* Look up railtype including alternate labels. */
|
|
|
|
return GetRailTypeByLabel(grffile->railtype_list[railtype]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-02 16:14:23 +00:00
|
|
|
/**
|
|
|
|
* Perform a reverse railtype lookup to get the GRF internal ID.
|
|
|
|
* @param railtype The global (OpenTTD) railtype.
|
|
|
|
* @param grffile The GRF to do the lookup for.
|
|
|
|
* @return the GRF internal ID.
|
|
|
|
*/
|
2010-02-07 22:22:54 +00:00
|
|
|
uint8 GetReverseRailTypeTranslation(RailType railtype, const GRFFile *grffile)
|
|
|
|
{
|
|
|
|
/* No rail type table present, return rail type as-is */
|
2019-04-10 21:07:06 +00:00
|
|
|
if (grffile == nullptr || grffile->railtype_list.size() == 0) return railtype;
|
2010-02-07 22:22:54 +00:00
|
|
|
|
|
|
|
/* Look for a matching rail type label in the table */
|
|
|
|
RailTypeLabel label = GetRailTypeInfo(railtype)->label;
|
2019-02-20 19:27:10 +00:00
|
|
|
|
|
|
|
int idx = find_index(grffile->railtype_list, label);
|
|
|
|
if (idx >= 0) return idx;
|
2010-02-07 22:22:54 +00:00
|
|
|
|
|
|
|
/* If not found, return as invalid */
|
|
|
|
return 0xFF;
|
|
|
|
}
|
2023-02-25 16:16:49 +00:00
|
|
|
|
|
|
|
void DumpRailTypeSpriteGroup(RailType rt, DumpSpriteGroupPrinter print)
|
|
|
|
{
|
|
|
|
char buffer[64];
|
|
|
|
const RailtypeInfo *rti = GetRailTypeInfo(rt);
|
|
|
|
|
|
|
|
static const char *sprite_group_names[] = {
|
|
|
|
"RTSG_CURSORS",
|
|
|
|
"RTSG_OVERLAY",
|
|
|
|
"RTSG_GROUND",
|
|
|
|
"RTSG_TUNNEL",
|
|
|
|
"RTSG_WIRES",
|
|
|
|
"RTSG_PYLONS",
|
|
|
|
"RTSG_BRIDGE",
|
|
|
|
"RTSG_CROSSING",
|
|
|
|
"RTSG_DEPOT",
|
|
|
|
"RTSG_FENCES",
|
|
|
|
"RTSG_TUNNEL_PORTAL",
|
|
|
|
"RTSG_SIGNALS",
|
|
|
|
"RTSG_GROUND_COMPLETE"
|
|
|
|
};
|
|
|
|
static_assert(lengthof(sprite_group_names) == RTSG_END);
|
|
|
|
|
|
|
|
SpriteGroupDumper dumper(print);
|
|
|
|
|
|
|
|
bool non_first_group = false;
|
|
|
|
for (RailTypeSpriteGroup rtsg = (RailTypeSpriteGroup)0; rtsg < RTSG_END; rtsg = (RailTypeSpriteGroup)(rtsg + 1)) {
|
|
|
|
if (rti->group[rtsg] != nullptr) {
|
|
|
|
if (non_first_group) {
|
|
|
|
print(nullptr, DSGPO_PRINT, 0, "");
|
|
|
|
} else {
|
|
|
|
non_first_group = true;
|
|
|
|
}
|
|
|
|
char *b = buffer;
|
|
|
|
b = strecpy(b, sprite_group_names[rtsg], lastof(buffer));
|
|
|
|
if (rti->grffile[rtsg] != nullptr) {
|
|
|
|
b += seprintf(b, lastof(buffer), ", GRF: %08X", BSWAP32(rti->grffile[rtsg]->grfid));
|
|
|
|
}
|
|
|
|
print(nullptr, DSGPO_PRINT, 0, buffer);
|
|
|
|
dumper.DumpSpriteGroup(rti->group[rtsg], 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|