OpenTTD Source 20260917-master-gec444f7315
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
9
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
20enum class FaceVarType : uint8_t {
22 Palette,
24};
25
27struct FaceVar {
29 uint8_t position;
30 uint8_t offset;
31 uint8_t length;
32 uint8_t valid_values;
33 std::variant<SpriteID, uint64_t, std::pair<uint64_t, uint64_t>> data;
35
41 inline uint GetBits(const CompanyManagerFace &cmf) const
42 {
43 return GB(cmf.bits, this->offset, this->length);
44 }
45
51 inline void SetBits(CompanyManagerFace &cmf, uint val) const
52 {
53 SB(cmf.bits, this->offset, this->length, val);
54 }
55
62 inline void ChangeBits(CompanyManagerFace &cmf, int8_t amount) const
63 {
64 int8_t val = this->GetBits(cmf) + amount; // the new value for the cmfv
65
66 /* scales the new value to the correct scope */
67 while (val < 0) {
68 val += this->valid_values;
69 }
70 val %= this->valid_values;
71
72 this->SetBits(cmf, val); // save the new value
73 }
74
80 inline bool IsValid(const CompanyManagerFace &cmf) const
81 {
82 return GB(cmf.bits, this->offset, this->length) < this->valid_values;
83 }
84
91 inline uint ScaleBits(uint val) const
92 {
93 assert(val < (1U << this->length));
94 return (val * this->valid_values) >> this->length;
95 }
96
103 inline SpriteID GetSprite(const CompanyManagerFace &cmf) const
104 {
105 assert(this->type == FaceVarType::Sprite);
106 return std::get<SpriteID>(this->data) + this->GetBits(cmf);
107 }
108};
109
110using FaceVars = std::span<const FaceVar>;
111
112struct FaceSpec {
113 std::string label;
114 std::variant<FaceVars, std::vector<FaceVar>> face_vars;
115
116 inline FaceVars GetFaceVars() const
117 {
118 struct visitor {
119 FaceVars operator()(FaceVars vars) const { return vars; }
120 FaceVars operator()(const std::vector<FaceVar> &vars) const { return vars; }
121 };
122 return std::visit(visitor{}, this->face_vars);
123 }
124};
125
126void ResetFaces();
128std::optional<uint> FindCompanyManagerFaceLabel(std::string_view label);
129const FaceSpec *GetCompanyManagerFaceSpec(uint style_index);
130FaceVars GetCompanyManagerFaceVars(uint style_index);
131
139inline uint64_t GetActiveFaceVars(const CompanyManagerFace &cmf, FaceVars vars)
140{
141 uint64_t active_vars = (1ULL << std::size(vars)) - 1ULL;
142
143 for (const auto &info : vars) {
144 if (info.type != FaceVarType::Toggle) continue;
145 const auto &[off, on] = std::get<std::pair<uint64_t, uint64_t>>(info.data);
146 active_vars &= ~(HasBit(cmf.bits, info.offset) ? on : off);
147 }
148
149 return active_vars;
150}
151
159{
160 for (auto var : SetBitIterator(GetActiveFaceVars(cmf, vars))) {
161 vars[var].ChangeBits(cmf, 0);
162 }
163}
164
171inline void RandomiseCompanyManagerFaceBits(CompanyManagerFace &cmf, FaceVars vars, Randomizer &randomizer)
172{
173 cmf.bits = randomizer.Next();
175}
176
179uint32_t MaskCompanyManagerFaceBits(const CompanyManagerFace &cmf, FaceVars vars);
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 */
Functions related to bit mathematics.
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.
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
std::optional< CompanyManagerFace > ParseCompanyManagerFaceCode(std::string_view str)
Parse a face code into a company manager face.
const FaceSpec * GetCompanyManagerFaceSpec(uint style_index)
Get the definition of a company manager face style.
void RandomiseCompanyManagerFace(CompanyManagerFace &cmf, Randomizer &randomizer)
Completely randomise a company manager face, including style.
void ResetFaces()
Reset company manager face styles to default.
uint32_t MaskCompanyManagerFaceBits(const CompanyManagerFace &cmf, FaceVars vars)
Mask company manager face bits to ensure they are all within range.
FaceVarType
Ways a FaceVar should be interpreted.
@ Sprite
FaceVar describes an offset to the given SpriteID.
@ Toggle
FaceVar describes which other FaceVars should be turned off.
void RandomiseCompanyManagerFaceBits(CompanyManagerFace &cmf, FaceVars vars, Randomizer &randomizer)
Make a random new face without changing the face style.
FaceVars GetCompanyManagerFaceVars(uint style_index)
Get the face variables for a face style.
void SetCompanyManagerFaceStyle(CompanyManagerFace &cmf, uint style)
Set a company face style.
std::optional< uint > FindCompanyManagerFaceLabel(std::string_view label)
Find a company manager face style by label.
uint64_t GetActiveFaceVars(const CompanyManagerFace &cmf, FaceVars vars)
Get a bitmask of currently active face variables.
void ScaleAllCompanyManagerFaceBits(CompanyManagerFace &cmf, FaceVars vars)
Scales all company manager's face bits to the correct scope.
uint GetNumCompanyManagerFaceStyles()
Get the number of company manager face styles.
std::string FormatCompanyManagerFaceCode(const CompanyManagerFace &cmf)
Get a face code representation of a company manager face.
void DrawCompanyManagerFace(const CompanyManagerFace &cmf, Colours colour, const Rect &r)
Draws the face of a company manager's face.
Types related to companies.
Types related to the graphics and/or input devices.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
Colours
One of 16 base colours used for companies and windows/widgets.
Definition gfx_type.h:283
#define Rect
Macro that prevents name conflicts between included headers.
Pseudo random number generator.
Types related to strings.
StrongType::Typedef< uint32_t, struct StringIDTag, StrongType::Compare, StrongType::Integer > StringID
Numeric value that represents a string, independent of the selected language.
uint32_t bits
Company manager face bits, meaning is dependent on style.
Information about the valid values of CompanyManagerFace bitgroups as well as the sprites to draw.
uint8_t position
Position in UI.
uint GetBits(const CompanyManagerFace &cmf) const
Gets the company manager's face bits.
void ChangeBits(CompanyManagerFace &cmf, int8_t amount) const
Increase/Decrease the company manager's face variable by the given amount.
uint8_t offset
Offset in bits into the CompanyManagerFace.
FaceVarType type
Type of variable.
StringID name
Name of the configurable component of the face.
std::variant< SpriteID, uint64_t, std::pair< uint64_t, uint64_t > > data
The first sprite.
uint8_t valid_values
The number of valid values.
uint ScaleBits(uint val) const
Scales a company manager's face bits variable to the correct scope.
bool IsValid(const CompanyManagerFace &cmf) const
Checks whether the company manager's face bits have a valid range.
SpriteID GetSprite(const CompanyManagerFace &cmf) const
Gets the sprite to draw.
void SetBits(CompanyManagerFace &cmf, uint val) const
Sets the company manager's face bits.
uint8_t length
Number of bits used in the CompanyManagerFace.
Structure to encapsulate the pseudo random number generators.
uint32_t Next()
Generate the next pseudo random number.
Iterable ensemble of each set bit in a value.
Data structure describing a sprite.