OpenTTD Source 20250331-master-g3c15e0c889
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 <http://www.gnu.org/licenses/>.
6 */
7
10#include "../stdafx.h"
11#include "../debug.h"
12#include "../bridge.h"
13#include "newgrf_bytereader.h"
14#include "newgrf_internal.h"
16
17#include "../safeguards.h"
18
27static ChangeInfoResult BridgeChangeInfo(uint first, uint last, int prop, ByteReader &buf)
28{
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();
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 break;
97 }
98
99 case 0x0E: // Flags; bit 0 - disable far pillars
100 bridge->flags = buf.ReadByte();
101 break;
102
103 case 0x0F: // Long format year of availability (year since year 0)
105 break;
106
107 case 0x10: // purchase string
109 break;
110
111 case 0x11: // description of bridge with rails
113 break;
114
115 case 0x12: // description of bridge with roads
117 break;
118
119 case 0x13: // 16 bits cost multiplier
120 bridge->price = buf.ReadWord();
121 break;
122
123 default:
124 ret = CIR_UNKNOWN;
125 break;
126 }
127 }
128
129 return ret;
130}
131
133template <> ChangeInfoResult GrfChangeInfoHandler<GSF_BRIDGES>::Activation(uint first, uint last, int prop, ByteReader &buf) { return BridgeChangeInfo(first, last, prop, buf); }
constexpr uint SPRITES_PER_BRIDGE_PIECE
Number of sprites there are per bridge piece.
Definition bridge.h:36
static const uint MAX_BRIDGES
Maximal number of available bridge specs.
Definition bridge.h:35
BridgeSpec _bridge[MAX_BRIDGES]
The specification of all bridges.
Class to read from a NewGRF file.
uint32_t ReadDWord()
Read a single DWord (32 bits).
uint16_t ReadWord()
Read a single Word (16 bits).
uint8_t ReadByte()
Read a single byte (8 bits).
static constexpr TimerGame< struct Calendar >::Year MIN_YEAR
The absolute minimum year in OTTD.
static constexpr TimerGame< struct Calendar >::Year ORIGINAL_BASE_YEAR
The minimum starting year/base year of the original TTD.
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,...
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
uint32_t PaletteID
The number of the palette.
Definition gfx_type.h:18
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
void MapSpriteMappingRecolour(PalSpriteID *grf_sprite)
Map the colour modifiers of TTDPatch to those that Open is using.
NewGRF buffer reader definition.
NewGRF internal processing state.
ChangeInfoResult
Possible return values for the GrfChangeInfoHandler functions.
@ CIR_INVALID_ID
Attempt to modify an invalid ID.
@ CIR_UNKNOWN
Variable is unknown.
@ CIR_UNHANDLED
Variable was parsed but unread.
@ CIR_SUCCESS
Variable was parsed and read.
void AddStringForMapping(GRFStringID source, std::function< void(StringID)> &&func)
Record a static StringID for getting translated later.
NewGRF string mapping definition.
Struct containing information about a single bridge type.
Definition bridge.h:43
uint16_t price
the price multiplier
Definition bridge.h:47
uint8_t min_length
the minimum length (not counting start and end tile)
Definition bridge.h:45
StringID material
the string that contains the bridge description
Definition bridge.h:51
std::vector< std::vector< PalSpriteID > > sprite_table
table of sprites for drawing the bridge
Definition bridge.h:53
TimerGameCalendar::Year avail_year
the year where it becomes available
Definition bridge.h:44
StringID transport_name[2]
description of the bridge, when built for road or rail
Definition bridge.h:52
uint8_t flags
bit 0 set: disable drawing of far pillars.
Definition bridge.h:54
uint16_t speed
maximum travel speed (1 unit = 1/1.6 mph = 1 km-ish/h)
Definition bridge.h:48
uint16_t max_length
the maximum length (not counting start and end tile)
Definition bridge.h:46
GRF feature handler.
Templated helper to make a type-safe 'typedef' representing a single POD value.