OpenTTD Source 20260129-master-g2bb01bd0e4
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
22enum class FaceVarType : uint8_t {
23 Sprite,
24 Palette,
25 Toggle,
26};
27
29struct FaceVar {
31 uint8_t position;
32 uint8_t offset;
33 uint8_t length;
34 uint8_t valid_values;
35 std::variant<SpriteID, uint64_t, std::pair<uint64_t, uint64_t>> data;
36 StringID name = STR_NULL;
37
43 inline uint GetBits(const CompanyManagerFace &cmf) const
44 {
45 return GB(cmf.bits, this->offset, this->length);
46 }
47
53 inline void SetBits(CompanyManagerFace &cmf, uint val) const
54 {
55 SB(cmf.bits, this->offset, this->length, val);
56 }
57
64 inline void ChangeBits(CompanyManagerFace &cmf, int8_t amount) const
65 {
66 int8_t val = this->GetBits(cmf) + amount; // the new value for the cmfv
67
68 /* scales the new value to the correct scope */
69 while (val < 0) {
70 val += this->valid_values;
71 }
72 val %= this->valid_values;
73
74 this->SetBits(cmf, val); // save the new value
75 }
76
82 inline bool IsValid(const CompanyManagerFace &cmf) const
83 {
84 return GB(cmf.bits, this->offset, this->length) < this->valid_values;
85 }
86
93 inline uint ScaleBits(uint val) const
94 {
95 assert(val < (1U << this->length));
96 return (val * this->valid_values) >> this->length;
97 }
98
105 inline SpriteID GetSprite(const CompanyManagerFace &cmf) const
106 {
107 assert(this->type == FaceVarType::Sprite);
108 return std::get<SpriteID>(this->data) + this->GetBits(cmf);
109 }
110};
111
112using FaceVars = std::span<const FaceVar>;
113
114struct FaceSpec {
115 std::string label;
116 std::variant<FaceVars, std::vector<FaceVar>> face_vars;
117
118 inline FaceVars GetFaceVars() const
119 {
120 struct visitor {
121 FaceVars operator()(FaceVars vars) const { return vars; }
122 FaceVars operator()(const std::vector<FaceVar> &vars) const { return vars; }
123 };
124 return std::visit(visitor{}, this->face_vars);
125 }
126};
127
128void ResetFaces();
130std::optional<uint> FindCompanyManagerFaceLabel(std::string_view label);
131const FaceSpec *GetCompanyManagerFaceSpec(uint style_index);
132FaceVars GetCompanyManagerFaceVars(uint style_index);
133
141inline uint64_t GetActiveFaceVars(const CompanyManagerFace &cmf, FaceVars vars)
142{
143 uint64_t active_vars = (1ULL << std::size(vars)) - 1ULL;
144
145 for (const auto &info : vars) {
146 if (info.type != FaceVarType::Toggle) continue;
147 const auto &[off, on] = std::get<std::pair<uint64_t, uint64_t>>(info.data);
148 active_vars &= ~(HasBit(cmf.bits, info.offset) ? on : off);
149 }
150
151 return active_vars;
152}
153
160{
161 for (auto var : SetBitIterator(GetActiveFaceVars(cmf, vars))) {
162 vars[var].ChangeBits(cmf, 0);
163 }
164}
165
172inline void RandomiseCompanyManagerFaceBits(CompanyManagerFace &cmf, FaceVars vars, Randomizer &randomizer)
173{
174 cmf.bits = randomizer.Next();
176}
177
180uint32_t MaskCompanyManagerFaceBits(const CompanyManagerFace &cmf, FaceVars vars);
182std::optional<CompanyManagerFace> ParseCompanyManagerFaceCode(std::string_view str);
183
184void DrawCompanyManagerFace(const CompanyManagerFace &cmf, Colours colour, const Rect &r);
185
186#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.
@ Palette
FaceVar describes a palette to apply to the given parts bitmask.
@ 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
Pseudo random number generator.
Types related to strings.
uint32_t 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.
Specification of a rectangle with absolute coordinates of all edges.
Iterable ensemble of each set bit in a value.