OpenTTD Source: src/source_type.h Source File

OpenTTD Source 15.3

source_type.h
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#ifndef SOURCE_TYPE_H
11#define SOURCE_TYPE_H
12
13#include "company_type.h"
14#include "industry_type.h"
15#include "news_type.h"
16#include "strings_type.h"
17#include "town_type.h"
18
20 enum class SourceType : uint8_t {
21 Industry = 0,
22 Town = 1,
23 Headquarters = 2,
24};
25
26 using SourceID = uint16_t;
27static_assert(sizeof(SourceID) >= sizeof(CompanyID));
28static_assert(sizeof(SourceID) >= sizeof(IndustryID));
29static_assert(sizeof(SourceID) >= sizeof(TownID));
30
32 struct Source {
33public:
34 static constexpr SourceID Invalid = 0xFFFF;
35
36 SourceID id;
37 SourceType type;
38
39 Source() = default;
40 Source(ConvertibleThroughBase auto id, SourceType type) : id(id.base()), type(type) {}
41 Source(SourceID id, SourceType type) : id(id), type(type) {}
42
43 constexpr CompanyID ToCompanyID() const { assert(this->type == SourceType::Headquarters); return static_cast<CompanyID>(this->id); }
44 constexpr IndustryID ToIndustryID() const { assert(this->type == SourceType::Industry); return static_cast<IndustryID>(this->id); }
45 constexpr TownID ToTownID() const { assert(this->type == SourceType::Town); return static_cast<TownID>(this->id); }
46
47 constexpr void MakeInvalid() { this->id = Source::Invalid; }
48 constexpr void SetIndex(SourceID index) { this->id = index; }
49 constexpr void SetIndex(ConvertibleThroughBase auto index) { this->id = index.base(); }
50
51 constexpr bool IsValid() const noexcept { return this->id != Source::Invalid; }
52 auto operator<=>(const Source &source) const = default;
53
54 NewsReference GetNewsReference() const;
55 StringID GetFormat() const;
56};
57
58#endif /* SOURCE_TYPE_H */
company_type.h
Types related to companies.
ConvertibleThroughBase
A type is considered 'convertible through base()' when it has a 'base()' function that returns someth...
Definition convertible_through_base.hpp:19
industry_type.h
Types related to the industry.
news_type.h
Types related to news.
NewsReference
std::variant< std::monostate, TileIndex, VehicleID, StationID, IndustryID, TownID, EngineID > NewsReference
References to objects in news.
Definition news_type.h:74
SourceID
uint16_t SourceID
Contains either industry ID, town ID or company ID (or Source::Invalid)
Definition source_type.h:26
SourceType
SourceType
Types of cargo source and destination.
Definition source_type.h:20
SourceType::Industry
@ Industry
Source/destination is an industry.
SourceType::Headquarters
@ Headquarters
Source/destination are company headquarters.
SourceType::Town
@ Town
Source/destination is a town.
strings_type.h
Types related to strings.
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:19
Industry
Defines the internal data of a functional industry.
Definition industry.h:62
PoolID< uint8_t, struct CompanyIDTag, 0xF, 0xFF >
Source
A location from where cargo can come from (or go to).
Definition source_type.h:32
Source::Invalid
static constexpr SourceID Invalid
Invalid/unknown index of source.
Definition source_type.h:34
Source::id
SourceID id
Index of industry/town/HQ, Source::Invalid if unknown/invalid.
Definition source_type.h:36
Source::GetFormat
StringID GetFormat() const
Get the format string for a subsidy Source.
Definition subsidy.cpp:56
Source::GetNewsReference
NewsReference GetNewsReference() const
Get the NewsReference for a subsidy Source.
Definition subsidy.cpp:43
Source::type
SourceType type
Type of source_id.
Definition source_type.h:37
Town
Town data structure.
Definition town.h:63
town_type.h
Types related to towns.