OpenTTD Source: src/newgrf/newgrf_act0_bridges.cpp Source File

OpenTTD Source 15.0-RC2

newgrf_act0_bridges.cpp
1/*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "../stdafx.h"
11#include "../debug.h"
12#include "../bridge.h"
13#include "newgrf_bytereader.h"
14#include "newgrf_internal.h"
15#include "newgrf_stringmapping.h"
16
17#include "../safeguards.h"
18
27static ChangeInfoResult BridgeChangeInfo(uint first, uint last, int prop, ByteReader &buf)
28{
29 ChangeInfoResult ret = CIR_SUCCESS;
30
31 if (last > MAX_BRIDGES) {
32 GrfMsg(1, "BridgeChangeInfo: Bridge {} is invalid, max {}, ignoring", last, MAX_BRIDGES);
33 return CIR_INVALID_ID;
34 }
35
36 for (uint id = first; id < last; ++id) {
37 BridgeSpec *bridge = &_bridge[id];
38
39 switch (prop) {
40 case 0x08: { // Year of availability
41 /* We treat '0' as always available */
42 uint8_t year = buf.ReadByte();
43 bridge->avail_year = (year > 0 ? CalendarTime::ORIGINAL_BASE_YEAR + year : TimerGameCalendar::Year{});
44 break;
45 }
46
47 case 0x09: // Minimum length
48 bridge->min_length = buf.ReadByte();
49 break;
50
51 case 0x0A: // Maximum length
52 bridge->max_length = buf.ReadByte();
53 if (bridge->max_length > 16) bridge->max_length = UINT16_MAX;
54 break;
55
56 case 0x0B: // Cost factor
57 bridge->price = buf.ReadByte();
58 break;
59
60 case 0x0C: // Maximum speed
61 bridge->speed = buf.ReadWord();
62 if (bridge->speed == 0) bridge->speed = UINT16_MAX;
63 break;
64
65 case 0x0D: { // Bridge sprite tables
66 uint8_t tableid = buf.ReadByte();
67 uint8_t numtables = buf.ReadByte();
68 size_t size = tableid + numtables;
69
70 if (bridge->sprite_table.size() < size) {
71 /* Allocate memory for sprite table pointers and zero out */
72 bridge->sprite_table.resize(std::min<size_t>(size, NUM_BRIDGE_PIECES));
73 }
74
75 for (; numtables-- != 0; tableid++) {
76 if (tableid >= NUM_BRIDGE_PIECES) { // skip invalid data
77 GrfMsg(1, "BridgeChangeInfo: Table {} >= {}, skipping", tableid, NUM_BRIDGE_PIECES);
78 for (uint8_t sprite = 0; sprite < SPRITES_PER_BRIDGE_PIECE; sprite++) buf.ReadDWord();
79 continue;
80 }
81
82 if (bridge->sprite_table[tableid].empty()) {
83 bridge->sprite_table[tableid].resize(SPRITES_PER_BRIDGE_PIECE);
84 }
85
86 for (uint8_t sprite = 0; sprite < SPRITES_PER_BRIDGE_PIECE; sprite++) {
87 SpriteID image = buf.ReadWord();
88 PaletteID pal = buf.ReadWord();
89
90 bridge->sprite_table[tableid][sprite].sprite = image;
91 bridge->sprite_table[tableid][sprite].pal = pal;
92
93 MapSpriteMappingRecolour(&bridge->sprite_table[tableid][sprite]);
94 }
95 }
96 if (!bridge->ctrl_flags.Test(BridgeSpec::ControlFlag::CustomPillarFlags)) bridge->ctrl_flags.Set(BridgeSpec::ControlFlag::InvalidPillarFlags);
97 break;
98 }
99
100 case 0x0E: // Flags; bit 0 - disable far pillars
101 bridge->flags = buf.ReadByte();
102 break;
103
104 case 0x0F: // Long format year of availability (year since year 0)
105 bridge->avail_year = Clamp(TimerGameCalendar::Year(buf.ReadDWord()), CalendarTime::MIN_YEAR, CalendarTime::MAX_YEAR);
106 break;
107
108 case 0x10: // purchase string
109 AddStringForMapping(GRFStringID{buf.ReadWord()}, &bridge->material);
110 break;
111
112 case 0x11: // description of bridge with rails
113 AddStringForMapping(GRFStringID{buf.ReadWord()}, &bridge->transport_name[0]);
114 break;
115
116 case 0x12: // description of bridge with roads
117 AddStringForMapping(GRFStringID{buf.ReadWord()}, &bridge->transport_name[1]);
118 break;
119
120 case 0x13: // 16 bits cost multiplier
121 bridge->price = buf.ReadWord();
122 break;
123
124 case 0x15: { // Pillar information for each bridge piece.
125 uint16_t tiles = buf.ReadExtendedByte();
126 for (uint j = 0; j != tiles; ++j) {
127 if (j < std::size(bridge->pillar_flags)) {
128 bridge->pillar_flags[j][AXIS_X] = BridgePillarFlags{buf.ReadByte()};
129 bridge->pillar_flags[j][AXIS_Y] = BridgePillarFlags{buf.ReadByte()};
130 } else {
131 buf.ReadWord();
132 }
133 }
134 bridge->ctrl_flags.Reset(BridgeSpec::ControlFlag::InvalidPillarFlags);
135 bridge->ctrl_flags.Set(BridgeSpec::ControlFlag::CustomPillarFlags);
136 break;
137 }
138
139 default:
140 ret = CIR_UNKNOWN;
141 break;
142 }
143 }
144
145 return ret;
146}
147
148template <> ChangeInfoResult GrfChangeInfoHandler<GSF_BRIDGES>::Reserve(uint, uint, int, ByteReader &) { return CIR_UNHANDLED; }
149template <> ChangeInfoResult GrfChangeInfoHandler<GSF_BRIDGES>::Activation(uint first, uint last, int prop, ByteReader &buf) { return BridgeChangeInfo(first, last, prop, buf); }
SPRITES_PER_BRIDGE_PIECE
constexpr uint SPRITES_PER_BRIDGE_PIECE
Number of sprites there are per bridge piece.
Definition bridge.h:19
MAX_BRIDGES
static const uint MAX_BRIDGES
Maximal number of available bridge specs.
Definition bridge.h:18
_bridge
BridgeSpec _bridge[MAX_BRIDGES]
The specification of all bridges.
Definition tunnelbridge_cmd.cpp:49
BaseBitSet::Test
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Definition base_bitset_type.hpp:141
BaseBitSet::Reset
constexpr Timpl & Reset()
Reset all bits.
Definition base_bitset_type.hpp:83
BaseBitSet::Set
constexpr Timpl & Set()
Set all bits.
Definition base_bitset_type.hpp:40
ByteReader
Class to read from a NewGRF file.
Definition newgrf_bytereader.h:18
ByteReader::ReadDWord
uint32_t ReadDWord()
Read a single DWord (32 bits).
Definition newgrf_bytereader.h:66
ByteReader::ReadWord
uint16_t ReadWord()
Read a single Word (16 bits).
Definition newgrf_bytereader.h:45
ByteReader::ReadExtendedByte
uint16_t ReadExtendedByte()
Read a single Extended Byte (8 or 16 bits).
Definition newgrf_bytereader.h:56
ByteReader::ReadByte
uint8_t ReadByte()
Read a single byte (8 bits).
Definition newgrf_bytereader.h:34
EnumBitSet< BridgePillarFlag, uint8_t >
TimerGameConst< struct Calendar >::MIN_YEAR
static constexpr TimerGame< struct Calendar >::Year MIN_YEAR
The absolute minimum year in OTTD.
Definition timer_game_common.h:176
TimerGameConst< struct Calendar >::ORIGINAL_BASE_YEAR
static constexpr TimerGame< struct Calendar >::Year ORIGINAL_BASE_YEAR
The minimum starting year/base year of the original TTD.
Definition timer_game_common.h:163
TimerGameConst< struct Calendar >::MAX_YEAR
static constexpr TimerGame< struct Calendar >::Year MAX_YEAR
MAX_YEAR, nicely rounded value of the number of years that can be encoded in a single 32 bits date,...
Definition timer_game_common.h:173
AXIS_X
@ AXIS_X
The X axis.
Definition direction_type.h:124
AXIS_Y
@ AXIS_Y
The y axis.
Definition direction_type.h:125
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
PaletteID
uint32_t PaletteID
The number of the palette.
Definition gfx_type.h:18
Clamp
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
MapSpriteMappingRecolour
void MapSpriteMappingRecolour(PalSpriteID *grf_sprite)
Map the colour modifiers of TTDPatch to those that Open is using.
Definition newgrf_act2.cpp:32
newgrf_bytereader.h
NewGRF buffer reader definition.
newgrf_internal.h
NewGRF internal processing state.
ChangeInfoResult
ChangeInfoResult
Possible return values for the GrfChangeInfoHandler functions.
Definition newgrf_internal.h:20
CIR_INVALID_ID
@ CIR_INVALID_ID
Attempt to modify an invalid ID.
Definition newgrf_internal.h:25
CIR_UNKNOWN
@ CIR_UNKNOWN
Variable is unknown.
Definition newgrf_internal.h:24
CIR_UNHANDLED
@ CIR_UNHANDLED
Variable was parsed but unread.
Definition newgrf_internal.h:23
CIR_SUCCESS
@ CIR_SUCCESS
Variable was parsed and read.
Definition newgrf_internal.h:21
AddStringForMapping
void AddStringForMapping(GRFStringID source, std::function< void(StringID)> &&func)
Record a static StringID for getting translated later.
Definition newgrf_stringmapping.cpp:42
newgrf_stringmapping.h
NewGRF string mapping definition.
BridgeSpec
Struct containing information about a single bridge type.
Definition bridge.h:27
BridgeSpec::price
uint16_t price
the price multiplier
Definition bridge.h:38
BridgeSpec::ctrl_flags
ControlFlags ctrl_flags
control flags
Definition bridge.h:46
BridgeSpec::ControlFlag::CustomPillarFlags
@ CustomPillarFlags
Bridge has set custom pillar flags.
BridgeSpec::ControlFlag::InvalidPillarFlags
@ InvalidPillarFlags
Bridge pillar flags are not valid, i.e. only the tile layout has been modified.
BridgeSpec::min_length
uint8_t min_length
the minimum length (not counting start and end tile)
Definition bridge.h:36
BridgeSpec::pillar_flags
BridgeMiddlePillarFlags pillar_flags
bridge pillar flags.
Definition bridge.h:47
BridgeSpec::material
StringID material
the string that contains the bridge description
Definition bridge.h:42
BridgeSpec::sprite_table
std::vector< std::vector< PalSpriteID > > sprite_table
table of sprites for drawing the bridge
Definition bridge.h:44
BridgeSpec::avail_year
TimerGameCalendar::Year avail_year
the year where it becomes available
Definition bridge.h:35
BridgeSpec::transport_name
StringID transport_name[2]
description of the bridge, when built for road or rail
Definition bridge.h:43
BridgeSpec::flags
uint8_t flags
bit 0 set: disable drawing of far pillars.
Definition bridge.h:45
BridgeSpec::speed
uint16_t speed
maximum travel speed (1 unit = 1/1.6 mph = 1 km-ish/h)
Definition bridge.h:39
BridgeSpec::max_length
uint16_t max_length
the maximum length (not counting start and end tile)
Definition bridge.h:37
GrfChangeInfoHandler
GRF feature handler.
Definition newgrf_internal.h:30
StrongType::Typedef
Templated helper to make a type-safe 'typedef' representing a single POD value.
Definition strong_typedef_type.hpp:130