Add: NewGRF custom road waypoint support

This commit is contained in:
Jonathan G Rennison 2024-04-24 00:09:15 +01:00
parent cdba46255d
commit 2e20275f99
6 changed files with 292 additions and 15 deletions

View File

@ -4888,7 +4888,7 @@ static ChangeInfoResult RoadStopChangeInfo(uint id, int numinfo, int prop, ByteR
break;
case 0x12: // General flags
rs->flags = (uint8_t)buf->ReadDWord(); // Future-proofing, size this as 4 bytes, but we only need one byte's worth of flags at present
rs->flags = (uint16_t)buf->ReadDWord(); // Future-proofing, size this as 4 bytes, but we only need two byte's worth of flags at present
break;
case 0x15: // Cost multipliers

View File

@ -119,6 +119,15 @@ uint32_t RoadStopScopeResolver::GetVariable(uint8_t variable, [[maybe_unused]] u
/* Animation frame */
case 0x49: return this->tile == INVALID_TILE ? 0 : this->st->GetRoadStopAnimationFrame(this->tile);
/* Misc info */
case 0x50: {
uint32_t result = 0;
if (this->tile == INVALID_TILE) {
SetBit(result, 4);
}
return result;
}
/* Variables which use the parameter */
/* Variables 0x60 to 0x65 and 0x69 are handled separately below */
@ -284,7 +293,19 @@ void DrawRoadStopTile(int x, int y, RoadType roadtype, const RoadStopSpec *spec,
SpriteID image = dts->ground.sprite;
PaletteID pal = dts->ground.pal;
if (GB(image, 0, SPRITE_WIDTH) != 0) {
RoadStopDrawMode draw_mode;
if (HasBit(spec->flags, RSF_DRAW_MODE_REGISTER)) {
draw_mode = (RoadStopDrawMode)GetRegister(0x100);
} else {
draw_mode = spec->draw_mode;
}
if (type == STATION_ROADWAYPOINT) {
DrawSprite(SPR_ROAD_PAVED_STRAIGHT_X, PAL_NONE, x, y);
if ((draw_mode & ROADSTOP_DRAW_MODE_WAYP_GROUND) && GB(image, 0, SPRITE_WIDTH) != 0) {
DrawSprite(image, GroundSpritePaletteTransform(image, pal, palette), x, y);
}
} else if (GB(image, 0, SPRITE_WIDTH) != 0) {
DrawSprite(image, GroundSpritePaletteTransform(image, pal, palette), x, y);
}
@ -293,7 +314,7 @@ void DrawRoadStopTile(int x, int y, RoadType roadtype, const RoadStopSpec *spec,
uint sprite_offset = 5 - view;
/* Road underlay takes precedence over tram */
if (spec->draw_mode & ROADSTOP_DRAW_MODE_OVERLAY) {
if (type == STATION_ROADWAYPOINT || draw_mode & ROADSTOP_DRAW_MODE_OVERLAY) {
if (rti->UsesOverlay()) {
SpriteID ground = GetCustomRoadSprite(rti, INVALID_TILE, ROTSG_GROUND);
DrawSprite(ground + sprite_offset, PAL_NONE, x, y);
@ -306,7 +327,7 @@ void DrawRoadStopTile(int x, int y, RoadType roadtype, const RoadStopSpec *spec,
}
} else {
/* Bay stop */
if ((spec->draw_mode & ROADSTOP_DRAW_MODE_ROAD) && rti->UsesOverlay()) {
if ((draw_mode & ROADSTOP_DRAW_MODE_ROAD) && rti->UsesOverlay()) {
SpriteID ground = GetCustomRoadSprite(rti, INVALID_TILE, ROTSG_ROADSTOP);
DrawSprite(ground + view, PAL_NONE, x, y);
}

View File

@ -59,6 +59,7 @@ enum RoadStopDrawMode : uint8_t {
ROADSTOP_DRAW_MODE_NONE = 0,
ROADSTOP_DRAW_MODE_ROAD = 1 << 0, ///< Bay stops: Draw the road itself
ROADSTOP_DRAW_MODE_OVERLAY = 1 << 1, ///< Drive-through stops: Draw the road overlay, e.g. pavement
ROADSTOP_DRAW_MODE_WAYP_GROUND = 1 << 2, ///< Waypoints: Draw the sprite layout ground tile (on top of the road)
};
DECLARE_ENUM_AS_BIT_SET(RoadStopDrawMode)
@ -69,6 +70,7 @@ enum RoadStopSpecFlags {
RSF_NO_AUTO_ROAD_CONNECTION = 4, ///< No auto road connection.
RSF_BUILD_MENU_ROAD_ONLY = 5, ///< Only show in the road build menu (not tram).
RSF_BUILD_MENU_TRAM_ONLY = 6, ///< Only show in the tram build menu (not road).
RSF_DRAW_MODE_REGISTER = 8, ///< Read draw mode from register 0x100.
};
/** Scope resolver for road stops. */
@ -133,7 +135,7 @@ struct RoadStopSpec {
RoadStopAvailabilityType stop_type = ROADSTOPTYPE_ALL;
RoadStopDrawMode draw_mode = ROADSTOP_DRAW_MODE_ROAD | ROADSTOP_DRAW_MODE_OVERLAY;
uint8_t callback_mask = 0;
uint8_t flags = 0;
uint16_t flags = 0;
CargoTypes cargo_triggers = 0; ///< Bitmask of cargo types which cause trigger re-randomizing

View File

@ -53,6 +53,7 @@
static void ShowRVStationPicker(Window *parent, RoadStopType rs);
static void ShowRoadDepotPicker(Window *parent);
static void ShowBuildRoadWaypointPicker(Window *parent);
static bool _remove_button_clicked;
static bool _one_way_button_clicked;
@ -66,6 +67,8 @@ static RoadType _cur_roadtype;
static DiagDirection _road_depot_orientation;
static uint16_t _cur_waypoint_type; ///< Currently selected waypoint type
struct RoadStopGUISettings {
DiagDirection orientation;
@ -535,8 +538,9 @@ struct BuildRoadToolbarWindow : Window {
break;
case WID_ROT_BUILD_WAYPOINT:
if (HandlePlacePushButton(this, WID_ROT_BUILD_WAYPOINT, SPR_CURSOR_WAYPOINT, HT_RECT)) {
this->last_started_action = widget;
this->last_started_action = widget;
if (HandlePlacePushButton(this, WID_ROT_BUILD_WAYPOINT, SPR_CURSOR_WAYPOINT, HT_RECT) && RoadStopClass::Get(ROADSTOP_CLASS_WAYP)->GetSpecCount() > 1) {
ShowBuildRoadWaypointPicker(this);
}
break;
@ -756,7 +760,7 @@ struct BuildRoadToolbarWindow : Window {
TileArea ta(start_tile, end_tile);
Axis axis = select_method == VPM_X_LIMITED ? AXIS_X : AXIS_Y;
bool adjacent = _ctrl_pressed;
uint16_t waypoint_type = 0;
uint16_t waypoint_type = _cur_waypoint_type;
auto proc = [=](bool test, StationID to_join) -> bool {
if (test) {
@ -1866,6 +1870,238 @@ static void ShowRVStationPicker(Window *parent, RoadStopType rs)
new BuildRoadStationWindow(RoadTypeIsRoad(_cur_roadtype) ? &_road_station_picker_desc : &_tram_station_picker_desc, parent, rs);
}
struct BuildRoadWaypointWindow : PickerWindowBase {
using WaypointList = GUIList<uint>;
static const uint FILTER_LENGTH = 20;
const RoadStopClass *waypoints;
WaypointList list;
StringFilter string_filter; ///< Filter for waypoint name
static QueryString editbox; ///< Filter editbox
BuildRoadWaypointWindow(WindowDesc *desc, Window *parent) : PickerWindowBase(desc, parent)
{
this->waypoints = RoadStopClass::Get(ROADSTOP_CLASS_WAYP);
this->CreateNestedTree();
NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BROW_WAYPOINT_MATRIX);
matrix->SetScrollbar(this->GetScrollbar(WID_BROW_SCROLL));
this->FinishInitNested(TRANSPORT_ROAD);
this->querystrings[WID_BROW_FILTER] = &this->editbox;
this->editbox.cancel_button = QueryString::ACTION_CLEAR;
this->string_filter.SetFilterTerm(this->editbox.text.buf);
this->list.ForceRebuild();
this->BuildPickerList();
}
void Close([[maybe_unused]] int data = 0) override
{
CloseWindowById(WC_SELECT_STATION, 0);
this->PickerWindowBase::Close();
}
bool FilterByText(const RoadStopSpec *roadstopspec)
{
if (this->string_filter.IsEmpty()) return true;
this->string_filter.ResetState();
if (roadstopspec == nullptr) {
this->string_filter.AddLine(GetString(STR_STATION_CLASS_WAYP_WAYPOINT));
} else {
this->string_filter.AddLine(GetString(roadstopspec->name));
if (roadstopspec->grf_prop.grffile != nullptr) {
const GRFConfig *gc = GetGRFConfig(roadstopspec->grf_prop.grffile->grfid);
this->string_filter.AddLine(gc->GetName());
}
}
return this->string_filter.GetState();
}
void BuildPickerList()
{
if (!this->list.NeedRebuild()) return;
this->list.clear();
this->list.reserve(this->waypoints->GetSpecCount());
for (uint i = 0; i < this->waypoints->GetSpecCount(); i++) {
const RoadStopSpec *roadstopspec = this->waypoints->GetSpec(i);
if (!FilterByText(roadstopspec)) continue;
this->list.push_back(i);
}
this->list.RebuildDone();
NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BROW_WAYPOINT_MATRIX);
matrix->SetCount((int)this->list.size());
matrix->SetClicked(this->UpdateSelection(_cur_waypoint_type));
}
uint UpdateSelection(uint type)
{
auto found = std::find(std::begin(this->list), std::end(this->list), type);
if (found != std::end(this->list)) return found - std::begin(this->list);
/* Selection isn't in the list, default to first */
if (this->list.empty()) {
_cur_waypoint_type = 0;
return -1;
} else {
_cur_waypoint_type = this->list.front();
return 0;
}
}
void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override
{
switch (widget) {
case WID_BROW_WAYPOINT_MATRIX:
/* Two blobs high and three wide. */
size.width += resize.width * 2;
size.height += resize.height * 1;
/* Resizing in X direction only at blob size, but at pixel level in Y. */
resize.height = 1;
break;
case WID_BROW_WAYPOINT:
size.width = ScaleGUITrad(64) + WidgetDimensions::scaled.fullbevel.Horizontal();
size.height = ScaleGUITrad(58) + WidgetDimensions::scaled.fullbevel.Vertical();
break;
}
}
void SetStringParameters(WidgetID widget) const override
{
if (widget == WID_BROW_NAME) {
if (!this->list.empty() && IsInsideBS(_cur_waypoint_type, 0, this->waypoints->GetSpecCount())) {
const RoadStopSpec *roadstopspec = this->waypoints->GetSpec(_cur_waypoint_type);
if (roadstopspec == nullptr) {
SetDParam(0, STR_STATION_CLASS_WAYP_WAYPOINT);
} else {
SetDParam(0, roadstopspec->name);
}
} else {
SetDParam(0, STR_EMPTY);
}
}
}
void OnPaint() override
{
this->BuildPickerList();
this->DrawWidgets();
}
void DrawWidget(const Rect &r, WidgetID widget) const override
{
switch (widget) {
case WID_BROW_WAYPOINT: {
uint16_t type = this->list.at(this->GetWidget<NWidgetBase>(widget)->GetParentWidget<NWidgetMatrix>()->GetCurrentElement());
const RoadStopSpec *roadstopspec = this->waypoints->GetSpec(type);
DrawPixelInfo tmp_dpi;
Rect ir = r.Shrink(WidgetDimensions::scaled.bevel);
if (FillDrawPixelInfo(&tmp_dpi, ir)) {
AutoRestoreBackup dpi_backup(_cur_dpi, &tmp_dpi);
int x = (ir.Width() - ScaleSpriteTrad(64)) / 2 + ScaleSpriteTrad(31);
int y = (ir.Height() + ScaleSpriteTrad(48)) / 2 - ScaleSpriteTrad(31);
if (roadstopspec == nullptr) {
StationPickerDrawSprite(x, y, STATION_ROADWAYPOINT, INVALID_RAILTYPE, _cur_roadtype, 4);
} else {
DrawRoadStopTile(x, y, _cur_roadtype, roadstopspec, STATION_ROADWAYPOINT, 4);
}
}
if (!IsRoadStopAvailable(roadstopspec, STATION_ROADWAYPOINT)) {
GfxFillRect(ir, PC_BLACK, FILLRECT_CHECKER);
}
}
}
}
void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
{
switch (widget) {
case WID_BROW_WAYPOINT: {
uint16_t sel = this->GetWidget<NWidgetBase>(widget)->GetParentWidget<NWidgetMatrix>()->GetCurrentElement();
assert(sel < this->list.size());
uint16_t type = this->list.at(sel);
const RoadStopSpec *roadstopspec = this->waypoints->GetSpec(type);
if (!IsRoadStopAvailable(roadstopspec, STATION_ROADWAYPOINT)) return;
_cur_waypoint_type = type;
this->GetWidget<NWidgetBase>(widget)->GetParentWidget<NWidgetMatrix>()->SetClicked(sel);
if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
this->SetDirty();
break;
}
}
}
void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
{
if (!gui_scope) return;
this->list.ForceRebuild();
}
void OnEditboxChanged(WidgetID wid) override
{
if (wid == WID_BROW_FILTER) {
this->string_filter.SetFilterTerm(this->editbox.text.buf);
this->InvalidateData();
}
}
void OnRealtimeTick([[maybe_unused]] uint delta_ms) override
{
CheckRedrawRoadWaypointCoverage(this);
}
};
/* static */ QueryString BuildRoadWaypointWindow::editbox(BuildRoadWaypointWindow::FILTER_LENGTH * MAX_CHAR_LENGTH, BuildRoadWaypointWindow::FILTER_LENGTH);
/** Nested widget definition for the build NewGRF road waypoint window */
static constexpr NWidgetPart _nested_build_road_waypoint_widgets[] = {
NWidget(NWID_HORIZONTAL),
NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_WAYPOINT_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
NWidget(WWT_DEFSIZEBOX, COLOUR_DARK_GREEN),
EndContainer(),
NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
NWidget(WWT_EDITBOX, COLOUR_DARK_GREEN, WID_BROW_FILTER), SetPadding(2), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
EndContainer(),
NWidget(NWID_HORIZONTAL),
NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetScrollbar(WID_BROW_SCROLL),
NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BROW_WAYPOINT_MATRIX), SetPIP(0, 2, 0), SetPadding(WidgetDimensions::unscaled.picker),
NWidget(WWT_PANEL, COLOUR_GREY, WID_BROW_WAYPOINT), SetDataTip(0x0, STR_WAYPOINT_GRAPHICS_TOOLTIP), SetScrollbar(WID_BROW_SCROLL), EndContainer(),
EndContainer(),
EndContainer(),
NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_BROW_SCROLL),
EndContainer(),
NWidget(NWID_HORIZONTAL),
NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
NWidget(WWT_TEXT, COLOUR_DARK_GREEN, WID_BROW_NAME), SetPadding(2), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_JUST_STRING, STR_NULL), SetTextStyle(TC_ORANGE), SetAlignment(SA_CENTER),
EndContainer(),
NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
EndContainer(),
};
static WindowDesc _build_road_waypoint_desc(
WDP_AUTO, "build_road_waypoint", 0, 0,
WC_BUILD_WAYPOINT, WC_BUILD_TOOLBAR,
WDF_CONSTRUCTION,
std::begin(_nested_build_road_waypoint_widgets), std::end(_nested_build_road_waypoint_widgets)
);
static void ShowBuildRoadWaypointPicker(Window *parent)
{
new BuildRoadWaypointWindow(&_build_road_waypoint_desc, parent);
}
void InitializeRoadGui()
{
_road_depot_orientation = DIAGDIR_NW;

View File

@ -3290,7 +3290,7 @@ draw_default_foundation:
draw_ground = true;
}
if (draw_ground && !IsStationRoadStop(ti->tile)) {
if (draw_ground && !IsAnyRoadStop(ti->tile)) {
SpriteID image = t->ground.sprite;
PaletteID pal = t->ground.pal;
RailTrackOffset overlay_offset;
@ -3317,7 +3317,7 @@ draw_default_foundation:
if (HasStationRail(ti->tile) && HasRailCatenaryDrawn(GetRailType(ti->tile))) DrawRailCatenary(ti);
if (IsStationRoadStop(ti->tile)) {
if (IsAnyRoadStop(ti->tile)) {
RoadType road_rt = GetRoadTypeRoad(ti->tile);
RoadType tram_rt = GetRoadTypeTram(ti->tile);
const RoadTypeInfo *road_rti = road_rt == INVALID_ROADTYPE ? nullptr : GetRoadTypeInfo(road_rt);
@ -3328,13 +3328,21 @@ draw_default_foundation:
StationType type = GetStationType(ti->tile);
const RoadStopSpec *stopspec = GetRoadStopSpec(ti->tile);
RoadStopDrawMode stop_draw_mode{};
if (stopspec != nullptr) {
stop_draw_mode = stopspec->draw_mode;
int view = dir;
if (IsDriveThroughStopTile(ti->tile)) view += 4;
st = BaseStation::GetByTile(ti->tile);
RoadStopResolverObject object(stopspec, st, ti->tile, INVALID_ROADTYPE, type, view);
const SpriteGroup *group = object.Resolve();
if (group != nullptr && group->type == SGT_TILELAYOUT) {
if (HasBit(stopspec->flags, RSF_DRAW_MODE_REGISTER)) {
stop_draw_mode = static_cast<RoadStopDrawMode>(GetRegister(0x100));
}
if (type == STATION_ROADWAYPOINT && (stop_draw_mode & ROADSTOP_DRAW_MODE_WAYP_GROUND)) {
draw_ground = true;
}
t = ((const TileLayoutSpriteGroup *)group)->ProcessRegisters(nullptr);
}
}
@ -3351,14 +3359,15 @@ draw_default_foundation:
}
if (IsDriveThroughStopTile(ti->tile)) {
uint sprite_offset = axis == AXIS_X ? 1 : 0;
DrawRoadOverlays(ti, PAL_NONE, road_rti, tram_rti, sprite_offset, sprite_offset);
if (type != STATION_ROADWAYPOINT && (stopspec == nullptr || (stop_draw_mode & ROADSTOP_DRAW_MODE_OVERLAY) != 0)) {
uint sprite_offset = axis == AXIS_X ? 1 : 0;
DrawRoadOverlays(ti, PAL_NONE, road_rti, tram_rti, sprite_offset, sprite_offset);
}
} else {
/* Non-drivethrough road stops are only valid for roads. */
assert(road_rt != INVALID_ROADTYPE && tram_rt == INVALID_ROADTYPE);
if ((stopspec == nullptr || (stopspec->draw_mode & ROADSTOP_DRAW_MODE_ROAD) != 0) && road_rti->UsesOverlay()) {
if ((stopspec == nullptr || (stop_draw_mode & ROADSTOP_DRAW_MODE_ROAD) != 0) && road_rti->UsesOverlay()) {
SpriteID ground = GetCustomRoadSprite(road_rti, ti->tile, ROTSG_ROADSTOP);
DrawGroundSprite(ground + dir, PAL_NONE);
}
@ -3426,7 +3435,7 @@ void StationPickerDrawSprite(int x, int y, StationType st, RailType railtype, Ro
}
/* Default waypoint has no railtype specific sprites */
DrawRailTileSeqInGUI(x, y, t, st == STATION_WAYPOINT ? 0 : total_offset, 0, pal);
DrawRailTileSeqInGUI(x, y, t, (st == STATION_WAYPOINT || st == STATION_ROADWAYPOINT) ? 0 : total_offset, 0, pal);
}
static int GetSlopePixelZ_Station(TileIndex tile, uint, uint, bool)

View File

@ -71,4 +71,13 @@ enum BuildRoadStationWidgets : WidgetID {
WID_BROS_NEWST_SCROLL, ///< Scrollbar of the #WID_BROS_NEWST_LIST.
};
/** Widgets of the #BuildRoadWaypointWindow class. */
enum BuildRoadWaypointWidgets : WidgetID {
WID_BROW_FILTER, ///< Text filter.
WID_BROW_WAYPOINT_MATRIX, ///< Matrix with waypoints.
WID_BROW_WAYPOINT, ///< A single waypoint.
WID_BROW_SCROLL, ///< Scrollbar for the matrix.
WID_BROW_NAME, ///< Name of selected waypoint.
};
#endif /* WIDGETS_ROAD_WIDGET_H */