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

OpenTTD Source 15.3

newgrf_act0_badges.cpp
Go to the documentation of this file.
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 "../newgrf_badge.h"
13#include "../newgrf_badge_type.h"
14#include "newgrf_bytereader.h"
15#include "newgrf_internal.h"
16
17#include "../safeguards.h"
18
19static ChangeInfoResult BadgeChangeInfo(uint first, uint last, int prop, ByteReader &buf)
20{
21 ChangeInfoResult ret = CIR_SUCCESS;
22
23 if (last >= UINT16_MAX) {
24 GrfMsg(1, "BadgeChangeInfo: Tag {} is invalid, max {}, ignoring", last, UINT16_MAX - 1);
25 return CIR_INVALID_ID;
26 }
27
28 for (uint id = first; id < last; ++id) {
29 auto it = _cur_gps.grffile->badge_map.find(id);
30 if (prop != 0x08 && it == std::end(_cur_gps.grffile->badge_map)) {
31 GrfMsg(1, "BadgeChangeInfo: Attempt to modify undefined tag {}, ignoring", id);
32 return CIR_INVALID_ID;
33 }
34
35 Badge *badge = nullptr;
36 if (prop != 0x08) badge = GetBadge(it->second);
37
38 switch (prop) {
39 case 0x08: { // Label
40 std::string_view label = buf.ReadString();
41 _cur_gps.grffile->badge_map[id] = GetOrCreateBadge(label).index;
42 break;
43 }
44
45 case 0x09: // Flags
46 badge->flags = static_cast<BadgeFlags>(buf.ReadDWord());
47 break;
48
49 default:
50 ret = CIR_UNKNOWN;
51 break;
52 }
53 }
54
55 return ret;
56}
57
58template <> ChangeInfoResult GrfChangeInfoHandler<GSF_BADGES>::Reserve(uint, uint, int, ByteReader &) { return CIR_UNHANDLED; }
59template <> ChangeInfoResult GrfChangeInfoHandler<GSF_BADGES>::Activation(uint first, uint last, int prop, ByteReader &buf) { return BadgeChangeInfo(first, last, prop, buf); }
Badge
Definition newgrf_badge.h:20
Badge::index
BadgeID index
Index assigned to badge.
Definition newgrf_badge.h:23
Badge::flags
BadgeFlags flags
Display flags.
Definition newgrf_badge.h:25
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::ReadString
std::string_view ReadString()
Read a NUL-terminated string.
Definition newgrf_bytereader.h:91
EnumBitSet< BadgeFlag, uint8_t >
GetBadge
Badge * GetBadge(BadgeID index)
Get a badge if it exists.
Definition newgrf_badge.cpp:113
GetOrCreateBadge
Badge & GetOrCreateBadge(std::string_view label)
Register a badge label and return its global index.
Definition newgrf_badge.cpp:82
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
GrfChangeInfoHandler
GRF feature handler.
Definition newgrf_internal.h:30
GrfProcessingState::grffile
GRFFile * grffile
Currently processed GRF file.
Definition newgrf_internal.h:67