OpenTTD Source: src/company_manager_face.h Source File

OpenTTD Source 15.3

company_manager_face.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 COMPANY_MANAGER_FACE_H
11#define COMPANY_MANAGER_FACE_H
12
13#include "core/random_func.hpp"
14#include "core/bitmath_func.hpp"
15#include "strings_type.h"
16#include "company_type.h"
17#include "gfx_type.h"
18
19#include "table/strings.h"
20
21enum class FaceVarType : uint8_t {
22 Sprite,
23 Palette,
24 Toggle,
25};
26
28 struct FaceVar {
29 FaceVarType type;
30 uint8_t position;
31 uint8_t offset;
32 uint8_t length;
33 uint8_t valid_values;
34 std::variant<SpriteID, uint64_t, std::pair<uint64_t, uint64_t>> data;
35 StringID name = STR_NULL;
36
42 inline uint GetBits(const CompanyManagerFace &cmf) const
43 {
44 return GB(cmf.bits, this->offset, this->length);
45 }
46
52 inline void SetBits(CompanyManagerFace &cmf, uint val) const
53 {
54 SB(cmf.bits, this->offset, this->length, val);
55 }
56
63 inline void ChangeBits(CompanyManagerFace &cmf, int8_t amount) const
64 {
65 int8_t val = this->GetBits(cmf) + amount; // the new value for the cmfv
66
67 /* scales the new value to the correct scope */
68 while (val < 0) {
69 val += this->valid_values;
70 }
71 val %= this->valid_values;
72
73 this->SetBits(cmf, val); // save the new value
74 }
75
81 inline bool IsValid(const CompanyManagerFace &cmf) const
82 {
83 return GB(cmf.bits, this->offset, this->length) < this->valid_values;
84 }
85
92 inline uint ScaleBits(uint val) const
93 {
94 assert(val < (1U << this->length));
95 return (val * this->valid_values) >> this->length;
96 }
97
104 inline SpriteID GetSprite(const CompanyManagerFace &cmf) const
105 {
106 assert(this->type == FaceVarType::Sprite);
107 return std::get<SpriteID>(this->data) + this->GetBits(cmf);
108 }
109};
110
111using FaceVars = std::span<const FaceVar>;
112
113 struct FaceSpec {
114 std::string label;
115 std::variant<FaceVars, std::vector<FaceVar>> face_vars;
116
117 inline FaceVars GetFaceVars() const
118 {
119 struct visitor {
120 FaceVars operator()(FaceVars vars) const { return vars; }
121 FaceVars operator()(const std::vector<FaceVar> &vars) const { return vars; }
122 };
123 return std::visit(visitor{}, this->face_vars);
124 }
125};
126
127void ResetFaces();
128uint GetNumCompanyManagerFaceStyles();
129std::optional<uint> FindCompanyManagerFaceLabel(std::string_view label);
130const FaceSpec *GetCompanyManagerFaceSpec(uint style_index);
131FaceVars GetCompanyManagerFaceVars(uint style_index);
132
140 inline uint64_t GetActiveFaceVars(const CompanyManagerFace &cmf, FaceVars vars)
141{
142 uint64_t active_vars = (1ULL << std::size(vars)) - 1ULL;
143
144 for (const auto &info : vars) {
145 if (info.type != FaceVarType::Toggle) continue;
146 const auto &[off, on] = std::get<std::pair<uint64_t, uint64_t>>(info.data);
147 active_vars &= ~(HasBit(cmf.bits, info.offset) ? on : off);
148 }
149
150 return active_vars;
151}
152
158 inline void ScaleAllCompanyManagerFaceBits(CompanyManagerFace &cmf, FaceVars vars)
159{
160 for (auto var : SetBitIterator(GetActiveFaceVars(cmf, vars))) {
161 vars[var].ChangeBits(cmf, 0);
162 }
163}
164
171 inline void RandomiseCompanyManagerFaceBits(CompanyManagerFace &cmf, FaceVars vars, Randomizer &randomizer)
172{
173 cmf.bits = randomizer.Next();
174 ScaleAllCompanyManagerFaceBits(cmf, vars);
175}
176
177void SetCompanyManagerFaceStyle(CompanyManagerFace &cmf, uint style);
178void RandomiseCompanyManagerFace(CompanyManagerFace &cmf, Randomizer &randomizer);
179uint32_t MaskCompanyManagerFaceBits(const CompanyManagerFace &cmf, FaceVars vars);
180std::string FormatCompanyManagerFaceCode(const CompanyManagerFace &cmf);
181std::optional<CompanyManagerFace> ParseCompanyManagerFaceCode(std::string_view str);
182
183void DrawCompanyManagerFace(const CompanyManagerFace &cmf, Colours colour, const Rect &r);
184
185#endif /* COMPANY_MANAGER_FACE_H */
bitmath_func.hpp
Functions related to bit mathematics.
SB
constexpr T SB(T &x, const uint8_t s, const uint8_t n, const U d)
Set n bits in x starting at bit s to d.
Definition bitmath_func.hpp:58
GB
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Definition bitmath_func.hpp:32
HasBit
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition bitmath_func.hpp:103
ParseCompanyManagerFaceCode
std::optional< CompanyManagerFace > ParseCompanyManagerFaceCode(std::string_view str)
Parse a face code into a company manager face.
Definition company_cmd.cpp:1490
GetCompanyManagerFaceSpec
const FaceSpec * GetCompanyManagerFaceSpec(uint style_index)
Get the definition of a company manager face style.
Definition company_cmd.cpp:1398
RandomiseCompanyManagerFace
void RandomiseCompanyManagerFace(CompanyManagerFace &cmf, Randomizer &randomizer)
Completely randomise a company manager face, including style.
Definition company_cmd.cpp:1450
ResetFaces
void ResetFaces()
Reset company manager face styles to default.
Definition company_cmd.cpp:1378
MaskCompanyManagerFaceBits
uint32_t MaskCompanyManagerFaceBits(const CompanyManagerFace &cmf, FaceVars vars)
Mask company manager face bits to ensure they are all within range.
Definition company_cmd.cpp:1463
RandomiseCompanyManagerFaceBits
void RandomiseCompanyManagerFaceBits(CompanyManagerFace &cmf, FaceVars vars, Randomizer &randomizer)
Make a random new face without changing the face style.
Definition company_manager_face.h:171
GetCompanyManagerFaceVars
FaceVars GetCompanyManagerFaceVars(uint style_index)
Get the face variables for a face style.
Definition company_cmd.cpp:1422
SetCompanyManagerFaceStyle
void SetCompanyManagerFaceStyle(CompanyManagerFace &cmf, uint style)
Set a company face style.
Definition company_cmd.cpp:1435
FindCompanyManagerFaceLabel
std::optional< uint > FindCompanyManagerFaceLabel(std::string_view label)
Find a company manager face style by label.
Definition company_cmd.cpp:1409
GetActiveFaceVars
uint64_t GetActiveFaceVars(const CompanyManagerFace &cmf, FaceVars vars)
Get a bitmask of currently active face variables.
Definition company_manager_face.h:140
ScaleAllCompanyManagerFaceBits
void ScaleAllCompanyManagerFaceBits(CompanyManagerFace &cmf, FaceVars vars)
Scales all company manager's face bits to the correct scope.
Definition company_manager_face.h:158
GetNumCompanyManagerFaceStyles
uint GetNumCompanyManagerFaceStyles()
Get the number of company manager face styles.
Definition company_cmd.cpp:1388
FormatCompanyManagerFaceCode
std::string FormatCompanyManagerFaceCode(const CompanyManagerFace &cmf)
Get a face code representation of a company manager face.
Definition company_cmd.cpp:1479
DrawCompanyManagerFace
void DrawCompanyManagerFace(const CompanyManagerFace &cmf, Colours colour, const Rect &r)
Draws the face of a company manager's face.
Definition company_gui.cpp:1120
company_type.h
Types related to companies.
gfx_type.h
Types related to the graphics and/or input devices.
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
random_func.hpp
Pseudo random number generator.
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
CompanyManagerFace
Definition company_type.h:53
CompanyManagerFace::bits
uint32_t bits
Company manager face bits, meaning is dependent on style.
Definition company_type.h:55
FaceSpec
Definition company_manager_face.h:113
FaceVar
Information about the valid values of CompanyManagerFace bitgroups as well as the sprites to draw.
Definition company_manager_face.h:28
FaceVar::position
uint8_t position
Position in UI.
Definition company_manager_face.h:30
FaceVar::GetBits
uint GetBits(const CompanyManagerFace &cmf) const
Gets the company manager's face bits.
Definition company_manager_face.h:42
FaceVar::ChangeBits
void ChangeBits(CompanyManagerFace &cmf, int8_t amount) const
Increase/Decrease the company manager's face variable by the given amount.
Definition company_manager_face.h:63
FaceVar::offset
uint8_t offset
Offset in bits into the CompanyManagerFace.
Definition company_manager_face.h:31
FaceVar::data
std::variant< SpriteID, uint64_t, std::pair< uint64_t, uint64_t > > data
The first sprite.
Definition company_manager_face.h:34
FaceVar::valid_values
uint8_t valid_values
The number of valid values.
Definition company_manager_face.h:33
FaceVar::ScaleBits
uint ScaleBits(uint val) const
Scales a company manager's face bits variable to the correct scope.
Definition company_manager_face.h:92
FaceVar::IsValid
bool IsValid(const CompanyManagerFace &cmf) const
Checks whether the company manager's face bits have a valid range.
Definition company_manager_face.h:81
FaceVar::GetSprite
SpriteID GetSprite(const CompanyManagerFace &cmf) const
Gets the sprite to draw.
Definition company_manager_face.h:104
FaceVar::SetBits
void SetBits(CompanyManagerFace &cmf, uint val) const
Sets the company manager's face bits.
Definition company_manager_face.h:52
FaceVar::length
uint8_t length
Number of bits used in the CompanyManagerFace.
Definition company_manager_face.h:32
Palette
Information about the currently used palette.
Definition gfx_type.h:373
Randomizer
Structure to encapsulate the pseudo random number generators.
Definition random_func.hpp:27
Randomizer::Next
uint32_t Next()
Generate the next pseudo random number.
Definition random_func.cpp:42
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition geometry_type.hpp:101
SetBitIterator
Iterable ensemble of each set bit in a value.
Definition bitmath_func.hpp:277
Sprite
Data structure describing a sprite.
Definition spritecache_type.h:16