OpenTTD Source 20250331-master-g3c15e0c889
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 <http://www.gnu.org/licenses/>.
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
20enum class SourceType : uint8_t {
21 Industry = 0,
22 Town = 1,
23 Headquarters = 2,
24};
25
26using SourceID = uint16_t;
27static_assert(sizeof(SourceID) >= sizeof(CompanyID));
28static_assert(sizeof(SourceID) >= sizeof(IndustryID));
29static_assert(sizeof(SourceID) >= sizeof(TownID));
30
32struct Source {
33public:
34 static constexpr SourceID Invalid = 0xFFFF;
35
38
39 Source() = default;
40 Source(ConvertibleThroughBase auto id, SourceType type) : id(id.base()), 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
55 StringID GetFormat() const;
56};
57
58#endif /* SOURCE_TYPE_H */
Types related to companies.
A type is considered 'convertible through base()' when it has a 'base()' function that returns someth...
Types related to the industry.
Types related to news.
std::variant< std::monostate, TileIndex, VehicleID, StationID, IndustryID, TownID, EngineID > NewsReference
References to objects in news.
Definition news_type.h:73
uint16_t SourceID
Contains either industry ID, town ID or company ID (or Source::Invalid)
Definition source_type.h:26
SourceType
Types of cargo source and destination.
Definition source_type.h:20
@ Industry
Source/destination is an industry.
@ Headquarters
Source/destination are company headquarters.
@ Town
Source/destination is a town.
Types related to strings.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Defines the internal data of a functional industry.
Definition industry.h:63
A location from where cargo can come from (or go to).
Definition source_type.h:32
static constexpr SourceID Invalid
Invalid/unknown index of source.
Definition source_type.h:34
SourceID id
Index of industry/town/HQ, Source::Invalid if unknown/invalid.
Definition source_type.h:36
StringID GetFormat() const
Get the format string for a subsidy Source.
Definition subsidy.cpp:56
NewsReference GetNewsReference() const
Get the NewsReference for a subsidy Source.
Definition subsidy.cpp:43
SourceType type
Type of source_id.
Definition source_type.h:37
Town data structure.
Definition town.h:52
Types related to towns.