OpenTTD Source 20260208-master-g43af8e94d0
newgrf_industries.cpp File Reference

Handling of NewGRF industries. More...

#include "stdafx.h"
#include "debug.h"
#include "industry.h"
#include "newgrf_badge.h"
#include "newgrf_industries.h"
#include "newgrf_town.h"
#include "newgrf_cargo.h"
#include "window_func.h"
#include "town.h"
#include "company_base.h"
#include "error.h"
#include "strings_func.h"
#include "core/random_func.hpp"
#include "timer/timer_game_calendar.h"
#include "table/strings.h"
#include "safeguards.h"

Go to the source code of this file.

Functions

IndustryType MapNewGRFIndustryType (IndustryType grf_type, uint32_t grf_id)
 Map the GRF local type to an industry type.
uint32_t GetIndustryIDAtOffset (TileIndex tile, const Industry *i, uint32_t cur_grfid)
 Make an analysis of a tile and check for its belonging to the same industry, and/or the same grf file.
static uint32_t GetClosestIndustry (TileIndex tile, IndustryType type, const Industry *current)
static uint32_t GetCountAndDistanceOfClosestInstance (const ResolverObject &object, uint8_t param_set_id, uint8_t layout_filter, bool town_filter, const Industry *current)
 Implementation of both var 67 and 68 since the mechanism is almost the same, it is easier to regroup them on the same function.
static const GRFFileGetGrffile (IndustryType type)
 Get the grf file associated with the given industry type.
uint16_t GetIndustryCallback (CallbackID callback, uint32_t param1, uint32_t param2, Industry *industry, IndustryType type, TileIndex tile, std::span< int32_t > regs100)
 Perform an industry callback.
CommandCost CheckIfCallBackAllowsCreation (TileIndex tile, IndustryType type, size_t layout, uint32_t seed, uint16_t initial_random_bits, Owner founder, IndustryAvailabilityCallType creation_type)
 Check that the industry callback allows creation of the industry.
uint32_t GetIndustryProbabilityCallback (IndustryType type, IndustryAvailabilityCallType creation_type, uint32_t default_prob)
 Check with callback CBID_INDUSTRY_PROBABILITY whether the industry can be built.
void IndustryProductionCallback (Industry *ind, int reason)
 Get the industry production callback and apply it to the industry.
bool IndustryTemporarilyRefusesCargo (Industry *ind, CargoType cargo_type)
 Check whether an industry temporarily refuses to accept a certain cargo.

Variables

IndustryOverrideManager _industry_mngr (NEW_INDUSTRYOFFSET, NUM_INDUSTRYTYPES, IT_INVALID)
IndustryTileOverrideManager _industile_mngr (NEW_INDUSTRYTILEOFFSET, NUM_INDUSTRYTILES, INVALID_INDUSTRYTILE)

Detailed Description

Handling of NewGRF industries.

Definition in file newgrf_industries.cpp.

Function Documentation

◆ CheckIfCallBackAllowsCreation()

CommandCost CheckIfCallBackAllowsCreation ( TileIndex tile,
IndustryType type,
size_t layout,
uint32_t seed,
uint16_t initial_random_bits,
Owner founder,
IndustryAvailabilityCallType creation_type )

Check that the industry callback allows creation of the industry.

Parameters
tileTile to build the industry.
typeType of industry to build.
layoutLayout number.
seedSeed for the random generator.
initial_random_bitsThe random bits the industry is going to have after construction.
founderIndustry founder
creation_typeThe circumstances the industry is created under.
Returns
Succeeded or failed command.

Definition at line 545 of file newgrf_industries.cpp.

References CALLBACK_FAILED, CBID_INDUSTRY_LOCATION, ClosestTownFromTile(), Industry::founder, GetErrorMessageFromLocationCallbackResult(), GetIndustrySpec(), IndustrySpec::grf_prop, GRFFilePropsBase::grffile, Industry::location, Industry::psa, Industry::random, Industry::selected_layout, OrthogonalTileArea::tile, Industry::town, Industry::type, and OrthogonalTileArea::w.

Referenced by CreateNewIndustryHelper().

◆ GetClosestIndustry()

uint32_t GetClosestIndustry ( TileIndex tile,
IndustryType type,
const Industry * current )
static

Definition at line 93 of file newgrf_industries.cpp.

◆ GetCountAndDistanceOfClosestInstance()

uint32_t GetCountAndDistanceOfClosestInstance ( const ResolverObject & object,
uint8_t param_set_id,
uint8_t layout_filter,
bool town_filter,
const Industry * current )
static

Implementation of both var 67 and 68 since the mechanism is almost the same, it is easier to regroup them on the same function.

Parameters
objectResolverObject owning the temporary storage.
param_set_idparameter given to the callback, which is the set id, or the local id, in our terminology
layout_filteron what layout do we filter?
town_filterDo we filter on the same town as the current industry?
currentIndustry for which the inquiry is made
Returns
the formatted answer to the callback : rr(reserved) cc(count) dddd(manhattan distance of closest sister)

< Get the GRFID of the definition to look for in register 100h

Definition at line 117 of file newgrf_industries.cpp.

References ClampTo(), DistanceManhattan(), GB(), GetIndustrySpec(), Industry::GetIndustryTypeCount(), IndustrySpec::grf_prop, GRFFilePropsBase::grfid, Industry::industries, Industry::location, MapNewGRFIndustryType(), NUM_INDUSTRYTYPES, Industry::selected_layout, SetBit(), OrthogonalTileArea::tile, Industry::town, and Industry::type.

Referenced by IndustriesScopeResolver::GetVariable(), and ObjectScopeResolver::GetVariable().

◆ GetGrffile()

const GRFFile * GetGrffile ( IndustryType type)
static

Get the grf file associated with the given industry type.

Parameters
typeIndustry type to query.
Returns
The associated GRF file, if any.

Definition at line 462 of file newgrf_industries.cpp.

References GetIndustrySpec(), IndustrySpec::grf_prop, and GRFFilePropsBase::grffile.

Referenced by IndustriesResolverObject::IndustriesResolverObject().

◆ GetIndustryCallback()

uint16_t GetIndustryCallback ( CallbackID callback,
uint32_t param1,
uint32_t param2,
Industry * industry,
IndustryType type,
TileIndex tile,
std::span< int32_t > regs100 )

Perform an industry callback.

Parameters
callbackThe callback to perform.
param1The first parameter.
param2The second parameter.
industryThe industry to do the callback for.
typeThe type of industry to do the callback for.
tileThe tile associated with the callback.
[out]regs100Additional result values from registers 100+
Returns
The callback result.

Definition at line 528 of file newgrf_industries.cpp.

Referenced by ChangeIndustryProduction(), DoCreateNewIndustry(), IndustryViewWindow::DrawInfo(), BuildIndustryWindow::DrawWidget(), GetCargoSuffix(), GetIndustryProbabilityCallback(), and IndustryTemporarilyRefusesCargo().

◆ GetIndustryIDAtOffset()

uint32_t GetIndustryIDAtOffset ( TileIndex tile,
const Industry * i,
uint32_t cur_grfid )

Make an analysis of a tile and check for its belonging to the same industry, and/or the same grf file.

Parameters
tileTileIndex of the tile to query
iIndustry to which to compare the tile to
cur_grfidGRFID of the current callback chain
Returns
value encoded as per NFO specs

Definition at line 57 of file newgrf_industries.cpp.

References GetCleanIndustryGfx(), GetIndustryTileSpec(), IndustryTileSpec::grf_prop, GRFFilePropsBase::grfid, StandardGRFFileProps::HasSpriteGroups(), INVALID_INDUSTRYTILE, GRFFilePropsBase::local_id, NEW_INDUSTRYTILEOFFSET, SubstituteGRFFileProps::override_id, and Industry::TileBelongsToIndustry().

Referenced by IndustriesScopeResolver::GetVariable(), and IndustryTileScopeResolver::GetVariable().

◆ GetIndustryProbabilityCallback()

uint32_t GetIndustryProbabilityCallback ( IndustryType type,
IndustryAvailabilityCallType creation_type,
uint32_t default_prob )

Check with callback CBID_INDUSTRY_PROBABILITY whether the industry can be built.

Parameters
typeIndustry type to check.
creation_typeReason to construct a new industry.
default_probThe default probability if the NewGRF doesn't override it.
Returns
If the industry has no callback or allows building, true is returned. Otherwise, false is returned.

Definition at line 577 of file newgrf_industries.cpp.

References CALLBACK_FAILED, IndustrySpec::callback_mask, CBID_INDUSTRY_PROBABILITY, ErrorUnknownCallbackResult(), GetIndustryCallback(), GetIndustrySpec(), IndustrySpec::grf_prop, GRFFilePropsBase::grffile, GRFFilePropsBase::grfid, INVALID_TILE, Probability, and BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Test().

Referenced by CmdBuildIndustry(), GetIndustryGamePlayProbability(), and GetScaledIndustryGenerationProbability().

◆ IndustryProductionCallback()

void IndustryProductionCallback ( Industry * ind,
int reason )

◆ IndustryTemporarilyRefusesCargo()

bool IndustryTemporarilyRefusesCargo ( Industry * ind,
CargoType cargo_type )

Check whether an industry temporarily refuses to accept a certain cargo.

Parameters
indThe industry to query.
cargo_typeThe cargo to get information about.
Precondition
cargo_type is in ind->accepts_cargo.
Returns
Whether the given industry refuses to accept this cargo type.

Definition at line 687 of file newgrf_industries.cpp.

References CALLBACK_FAILED, IndustrySpec::callback_mask, GRFFile::cargo_map, CBID_INDUSTRY_REFUSE_CARGO, ConvertBooleanCallback(), GetIndustryCallback(), GetIndustrySpec(), IndustrySpec::grf_prop, GRFFilePropsBase::grffile, Industry::IsCargoAccepted(), Industry::location, RefuseCargo, BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Test(), OrthogonalTileArea::tile, and Industry::type.

Referenced by CanCargoServiceIndustry(), DeliverGoodsToIndustry(), and IndustryDirectoryWindow::DrawWidget().

◆ MapNewGRFIndustryType()

IndustryType MapNewGRFIndustryType ( IndustryType grf_type,
uint32_t grf_id )

Map the GRF local type to an industry type.

Parameters
grf_typeThe GRF local type.
grf_idThe GRF of the local type.
Returns
The industry type in the global scope.

Definition at line 41 of file newgrf_industries.cpp.

References GB(), and HasBit().

Referenced by FinaliseIndustriesArray(), GetCountAndDistanceOfClosestInstance(), and IndustriesScopeResolver::GetVariable().