OpenTTD Source 20250716-master-g6b6caa6fa8
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 <http://www.gnu.org/licenses/>.
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
28struct 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
113struct 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();
129std::optional<uint> FindCompanyManagerFaceLabel(std::string_view label);
130const FaceSpec *GetCompanyManagerFaceSpec(uint style_index);
131FaceVars GetCompanyManagerFaceVars(uint style_index);
132
140inline 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
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.
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
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.
debug_inline static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
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.
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.
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.
Information about the currently used palette.
Definition gfx_type.h:368
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.
Data structure describing a sprite.
Definition spritecache.h:17