OpenTTD Source 20250502-master-ge0a9c7d818
newgrf_badge.cpp File Reference

Functionality for NewGRF badges. More...

#include "stdafx.h"
#include "newgrf.h"
#include "newgrf_badge.h"
#include "newgrf_badge_type.h"
#include "newgrf_spritegroup.h"
#include "stringfilter_type.h"
#include "strings_func.h"
#include "timer/timer_game_calendar.h"
#include "table/strings.h"
#include "safeguards.h"

Go to the source code of this file.

Data Structures

class  Badges
 Global state for badge definitions. More...
 
struct  BadgeScopeResolver
 Resolver for a badge scope. More...
 
struct  BadgeResolverObject
 Resolver of badges. More...
 

Functions

std::span< const BadgeGetBadges ()
 Get a read-only view of badges.
 
static BadgeClassID GetOrCreateBadgeClass (BadgeID index)
 Assign a BadgeClassID to the given badge.
 
void ResetBadges ()
 Reset badges to the default state.
 
BadgeGetOrCreateBadge (std::string_view label)
 Register a badge label and return its global index.
 
BadgeGetBadge (BadgeID index)
 Get a badge if it exists.
 
BadgeGetBadgeByLabel (std::string_view label)
 Get a badge by label if it exists.
 
BadgeGetClassBadge (BadgeClassID class_index)
 Get the badge class of a badge label.
 
uint32_t GetBadgeVariableResult (const GRFFile &grffile, std::span< const BadgeID > badges, uint32_t parameter)
 Test for a matching badge in a list of badges, returning the number of matching bits.
 
void MarkBadgeSeen (BadgeID index, GrfSpecFeature feature)
 Mark a badge a seen (used) by a feature.
 
void AppendCopyableBadgeList (std::vector< BadgeID > &dst, std::span< const BadgeID > src, GrfSpecFeature feature)
 Append copyable badges from a list onto another.
 
void ApplyBadgeFeaturesToClassBadges ()
 Apply features from all badges to their badge classes.
 
PalSpriteID GetBadgeSprite (const Badge &badge, GrfSpecFeature feature, std::optional< TimerGameCalendar::Date > introduction_date, PaletteID remap)
 Get sprite for the given badge.
 

Variables

static constexpr char BADGE_CLASS_SEPARATOR = '/'
 Separator to identify badge classes from a label.
 
static Badges _badges = {}
 Static instance of badge state.
 

Detailed Description

Functionality for NewGRF badges.

Definition in file newgrf_badge.cpp.

Function Documentation

◆ AppendCopyableBadgeList()

void AppendCopyableBadgeList ( std::vector< BadgeID > &  dst,
std::span< const BadgeID src,
GrfSpecFeature  feature 
)

Append copyable badges from a list onto another.

Badges must exist and be marked with the Copy flag.

Parameters
dstDestination badge list.
srcSource badge list.
featureFeature of list.

Definition at line 241 of file newgrf_badge.cpp.

References Copy, Badge::features, Badge::flags, GetBadge(), BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Set(), and BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Test().

Referenced by FinaliseEngineArray().

◆ ApplyBadgeFeaturesToClassBadges()

void ApplyBadgeFeaturesToClassBadges ( )

Apply features from all badges to their badge classes.

Definition at line 258 of file newgrf_badge.cpp.

References Badge::features, GetBadges(), GetClassBadge(), and BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Set().

Referenced by FinaliseBadges().

◆ GetBadge()

Badge * GetBadge ( BadgeID  index)

Get a badge if it exists.

Parameters
indexIndex of badge.
Returns
Badge with specified index, or nullptr if it does not exist.

Definition at line 104 of file newgrf_badge.cpp.

References _badges, and Badges::specs.

Referenced by AppendCopyableBadgeList(), NewGRFInspectWindow::DrawMainPanelWidget(), GetClassBadge(), MarkBadgeSeen(), and UsedBadgeClasses::UsedBadgeClasses().

◆ GetBadgeByLabel()

Badge * GetBadgeByLabel ( std::string_view  label)

Get a badge by label if it exists.

Parameters
labelLabel of badge.
Returns
Badge with specified label, or nullptr if it does not exist.

Definition at line 115 of file newgrf_badge.cpp.

References _badges, Badge::label, and Badges::specs.

Referenced by FinaliseBadges().

◆ GetBadges()

std::span< const Badge > GetBadges ( )

Get a read-only view of badges.

Returns
Span of badges.

Definition at line 40 of file newgrf_badge.cpp.

References _badges, and Badges::specs.

Referenced by ApplyBadgeFeaturesToClassBadges(), and BadgeTextFilter::BadgeTextFilter().

◆ GetBadgeSprite()

PalSpriteID GetBadgeSprite ( const Badge badge,
GrfSpecFeature  feature,
std::optional< TimerGameCalendar::Date introduction_date,
PaletteID  remap 
)

Get sprite for the given badge.

Parameters
badgeBadge being queried.
featureGRF feature being used.
introduction_dateIntroduction date of the item, if it has one.
remapPalette remap to use if the flag is company-coloured.
Returns
Custom sprite to draw, or 0 if not available.

Definition at line 275 of file newgrf_badge.cpp.

References Badge::flags, BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Test(), and UseCompanyColour.

◆ GetBadgeVariableResult()

uint32_t GetBadgeVariableResult ( const GRFFile grffile,
std::span< const BadgeID badges,
uint32_t  parameter 
)

Test for a matching badge in a list of badges, returning the number of matching bits.

Parameters
grffileGRF file of the current varaction.
badgesList of badges to test.
parameterGRF-local badge index.
Returns
true iff the badge is present.

Definition at line 216 of file newgrf_badge.cpp.

References GRFFile::badge_list.

Referenced by AirportScopeResolver::GetVariable(), AirportTileScopeResolver::GetVariable(), VehicleScopeResolver::GetVariable(), HouseScopeResolver::GetVariable(), IndustriesScopeResolver::GetVariable(), IndustryTileScopeResolver::GetVariable(), ObjectScopeResolver::GetVariable(), RoadStopScopeResolver::GetVariable(), and StationScopeResolver::GetVariable().

◆ GetClassBadge()

Badge * GetClassBadge ( BadgeClassID  class_index)

Get the badge class of a badge label.

Parameters
labelLabel to get class of.
Returns
Badge class index of label.

Definition at line 128 of file newgrf_badge.cpp.

References _badges, Badges::classes, and GetBadge().

Referenced by ApplyBadgeFeaturesToClassBadges(), GUIBadgeClasses::GUIBadgeClasses(), and UsedBadgeClasses::UsedBadgeClasses().

◆ GetOrCreateBadge()

Badge & GetOrCreateBadge ( std::string_view  label)

Register a badge label and return its global index.

Parameters
labelBadge label to register.
Returns
Global index of the badge.

Definition at line 73 of file newgrf_badge.cpp.

References _badges, BADGE_CLASS_SEPARATOR, Badge::class_index, GetOrCreateBadge(), GetOrCreateBadgeClass(), Badge::label, and Badges::specs.

Referenced by GetOrCreateBadge().

◆ GetOrCreateBadgeClass()

static BadgeClassID GetOrCreateBadgeClass ( BadgeID  index)
static

Assign a BadgeClassID to the given badge.

Parameters
indexBadge ID of badge that should be assigned.
Returns
new or existing BadgeClassID.

Definition at line 50 of file newgrf_badge.cpp.

References _badges, and Badges::classes.

Referenced by GetOrCreateBadge().

◆ MarkBadgeSeen()

void MarkBadgeSeen ( BadgeID  index,
GrfSpecFeature  feature 
)

Mark a badge a seen (used) by a feature.

Definition at line 227 of file newgrf_badge.cpp.

References Badge::features, GetBadge(), and BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Set().

Referenced by ReadBadgeList().

◆ ResetBadges()

void ResetBadges ( )

Reset badges to the default state.

Definition at line 63 of file newgrf_badge.cpp.

References _badges.

Referenced by ResetNewGRFData().

Variable Documentation

◆ _badges

Badges _badges = {}
static

◆ BADGE_CLASS_SEPARATOR

constexpr char BADGE_CLASS_SEPARATOR = '/'
staticconstexpr

Separator to identify badge classes from a label.

Definition at line 24 of file newgrf_badge.cpp.

Referenced by GetOrCreateBadge().