# economy.cpp File Reference

Handling of the economy. More...

`#include "stdafx.h"`
`#include "openttd.h"`
`#include "tile_cmd.h"`
`#include "company_func.h"`
`#include "command_func.h"`
`#include "industry_map.h"`
`#include "town.h"`
`#include "news_func.h"`
`#include "network/network.h"`
`#include "network/network_func.h"`
`#include "vehicle_gui.h"`
`#include "ai/ai.hpp"`
`#include "aircraft.h"`
`#include "newgrf_engine.h"`
`#include "newgrf_sound.h"`
`#include "newgrf_industries.h"`
`#include "newgrf_industrytiles.h"`
`#include "newgrf_station.h"`
`#include "unmovable.h"`
`#include "group.h"`
`#include "strings_func.h"`
`#include "functions.h"`
`#include "window_func.h"`
`#include "date_func.h"`
`#include "vehicle_func.h"`
`#include "sound_func.h"`
`#include "gfx_func.h"`
`#include "autoreplace_func.h"`
`#include "company_gui.h"`
`#include "signs_base.h"`
`#include "table/strings.h"`
`#include "table/sprites.h"`

Go to the source code of this file.

## Data Structures
struct | FoundRoute
struct | FindIndustryToDeliverData
## Typedefs
typedef SmallVector< Industry *, 16 > | SmallIndustryList
## Functions
static int32 | BigMulS (const int32 a, const int32 b, const uint8 shift)
| Multiply two integer values and shift the results to right.
static uint32 | BigMulSU (const uint32 a, const uint32 b, const uint8 shift)
| Multiply two unsigned integers and shift the results to right.
Money | CalculateCompanyValue (const Company *c)
int | UpdateCompanyRatingAndValue (Company *c, bool update)
| if update is set to true, the economy is updated with this score (also the house is updated, should only be true in the on-tick event)
void | ChangeOwnershipOfCompanyItems (Owner old_owner, Owner new_owner)
static void | ChangeNetworkOwner (Owner current_owner, Owner new_owner)
static void | CompanyCheckBankrupt (Company *c)
static void | CompaniesGenStatistics ()
static void | AddSingleInflation (Money *value, uint16 *frac, int32 amt)
static void | AddInflation (bool check_year=true)
static void | CompaniesPayInterest ()
static void | HandleEconomyFluctuations ()
void | ResetPriceBaseMultipliers ()
| Reset changes to the price base multipliers.
void | SetPriceBaseMultiplier (uint price, byte factor)
| Change a price base by the given factor.
void | StartupIndustryDailyChanges (bool init_counter)
| Initialize the variables that will maintain the daily industry change system.
void | StartupEconomy ()
void | ResetEconomy ()
Money | GetPriceByIndex (uint8 index)
Pair | SetupSubsidyDecodeParam (const Subsidy *s, bool mode)
void | DeleteSubsidyWithTown (TownID index)
void | DeleteSubsidyWithIndustry (IndustryID index)
void | DeleteSubsidyWithStation (StationID index)
static void | FindSubsidyPassengerRoute (FoundRoute *fr)
static void | FindSubsidyCargoRoute (FoundRoute *fr)
static bool | CheckSubsidyDuplicate (Subsidy *s)
static void | SubsidyMonthlyHandler ()
Money | GetTransportedGoodsIncome (uint num_pieces, uint dist, byte transit_days, CargoID cargo_type)
static bool | FindIndustryToDeliver (TileIndex ind_tile, void *user_data)
static void | DeliverGoodsToIndustry (const Station *st, CargoID cargo_type, int num_pieces, SmallIndustryList *industry_set)
| Transfer goods from station to industry.
static bool | CheckSubsidised (Station *from, Station *to, CargoID cargo_type)
static Money | DeliverGoods (int num_pieces, CargoID cargo_type, StationID source, StationID dest, TileIndex source_tile, byte days_in_transit, SmallIndustryList *industry_set)
| Delivers goods to industries/towns and calculates the payment.
static void | TriggerIndustryProduction (Industry *i)
| Inform the industry about just delivered cargo DeliverGoodsToIndustry() silently incremented incoming_cargo_waiting, now it is time to do something with the new cargo.
void | VehiclePayment (Vehicle *front_v)
| Performs the vehicle payment _and_ marks the vehicle to be unloaded.
static void | LoadUnloadVehicle (Vehicle *v, int *cargo_left)
| Loads/unload the vehicle if possible.
void | LoadUnloadStation (Station *st)
| Load/unload the vehicles in this station according to the order they entered.
void | CompaniesMonthlyLoop ()
static void | DoAcquireCompany (Company *c)
int | GetAmountOwnedBy (const Company *c, Owner owner)
CommandCost | CmdBuyShareInCompany (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Acquire shares in an opposing company.
CommandCost | CmdSellShareInCompany (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Sell shares in an opposing company.
CommandCost | CmdBuyCompany (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Buy up another company.
## Variables
const ScoreInfo | _score_info []
int | _score_part [MAX_COMPANIES][SCORE_END]
Economy | _economy
Subsidy | _subsidies [MAX_COMPANIES]
Prices | _price
uint16 | _price_frac [NUM_PRICES]
Money | _cargo_payment_rates [NUM_CARGO]
uint16 | _cargo_payment_rates_frac [NUM_CARGO]
Money | _additional_cash_required
static byte | _price_category [NUM_PRICES]
static const Money | _price_base [NUM_PRICES]
static byte | price_base_multiplier [NUM_PRICES]

## Detailed Description

Handling of the economy.

Definition in file economy.cpp.

## Function Documentation

static int32 BigMulS | ( | const int32 | a,
|  | const int32 | b,
|  | const uint8 | shift |
| ) |  |  | `[inline, static]`

Multiply two integer values and shift the results to right.

This function multiplies two integer values. The result is shifted by the amount of shift to right.

Parameters:

  | a | The first integer
  | b | The second integer
  | shift | The amount to shift the value to right.

Returns:
  The shifted result

Definition at line 50 of file economy.cpp.

static uint32 BigMulSU | ( | const uint32 | a,
|  | const uint32 | b,
|  | const uint8 | shift |
| ) |  |  | `[inline, static]`

Multiply two unsigned integers and shift the results to right.

This function multiplies two unsigned integers. The result is shifted by the amount of shift to right.

Parameters:

  | a | The first unsigned integer
  | b | The second unsigned integer
  | shift | The amount to shift the value to right.

Returns:
  The shifted result

Definition at line 66 of file economy.cpp.

CommandCost CmdBuyCompany | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Buy up another company.

When a competing company is gone bankrupt you get the chance to purchase that company.

Todo:
  currently this only works for AI companies

Parameters:

  | tile | unused
  | flags | type of operation
  | p1 | company to buy up
  | p2 | unused

Definition at line 1966 of file economy.cpp.

References CMD_ERROR, and DC_EXEC.

CommandCost CmdBuyShareInCompany | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Acquire shares in an opposing company.

Parameters:

  | tile | unused
  | flags | type of operation
  | p1 | company to buy the shares from
  | p2 | unused

Definition at line 1888 of file economy.cpp.

References _cur_year, _settings_game, CommandCost::AddCost(), EconomySettings::allow_shares, CMD_ERROR, COMPANY_SPECTATOR, DC_EXEC, GameSettings::economy, InvalidateWindow(), and return_cmd_error.

CommandCost CmdSellShareInCompany | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Sell shares in an opposing company.

Parameters:

  | tile | unused
  | flags | type of operation
  | p1 | company to sell the shares from
  | p2 | unused

Definition at line 1933 of file economy.cpp.

References _settings_game, EconomySettings::allow_shares, CMD_ERROR, COMPANY_SPECTATOR, DC_EXEC, GameSettings::economy, and InvalidateWindow().

static Money DeliverGoods | ( | int | num_pieces,
|  | CargoID | cargo_type,
|  | StationID | source,
|  | StationID | dest,
|  | TileIndex | source_tile,
|  | byte | days_in_transit,
|  | SmallIndustryList * | industry_set |
| ) |  |  | `[static]`

Delivers goods to industries/towns and calculates the payment.

Parameters:

  | num_pieces | amount of cargo delivered
  | source | Originstation of the cargo
  | dest | Station the cargo has been unloaded
  | source_tile | The origin of the cargo for distance calculation
  | days_in_transit | Travel time
  | industry_set | The delivered industry will be inserted into this set, if not yet contained The cargo is just added to the stockpile of the industry. It is due to the caller to trigger the industry's production machinery

Definition at line 1353 of file economy.cpp.

References _settings_game, DeliverGoodsToIndustry(), GameSettings::difficulty, DistanceManhattan(), SetBit(), DifficultySettings::subsidy_multiplier, Station::town, and Station::xy.

Referenced by VehiclePayment().

static void DeliverGoodsToIndustry | ( | const Station * | st,
|  | CargoID | cargo_type,
|  | int | num_pieces,
|  | SmallIndustryList * | industry_set |
| ) |  |  | `[static]`

Transfer goods from station to industry.

All cargo is delivered to the nearest (Manhattan) industry to the station sign, which is inside the acceptance rectangle and actually accepts the cargo.

Parameters:

  | st | The station that accepted the cargo
  | cargo_type | Type of cargo delivered
  | nun_pieces | Amount of cargo delivered
  | industry_set | The destination industry will be inserted into this set

Definition at line 1231 of file economy.cpp.

References _settings_game, Pair::a, AddNewsItem(), Pair::b, Rect::bottom, CircularTileSearch(), GameSettings::difficulty, DistanceManhattan(), DistanceMax(), endof, Station::GetCatchmentRadius(), SmallVector< T, S >::Include(), Industry::incoming_cargo_waiting, PoolItem< T, Tid, Tpool >::index, InvalidateWindow(), StationRect::IsEmpty(), Rect::left, MapMaxX(), MapMaxY(), max(), min(), NS_SUBSIDIES, Station::rect, Rect::right, DifficultySettings::subsidy_multiplier, TileXY(), Rect::top, and Station::xy.

Referenced by DeliverGoods().

void LoadUnloadStation | ( | Station * | st | ) |

Load/unload the vehicles in this station according to the order they entered.

Parameters:

  | st | the station to do the loading/unloading for

Definition at line 1808 of file economy.cpp.

References Station::goods, Station::loading_vehicles, and LoadUnloadVehicle().

static void LoadUnloadVehicle | ( | Vehicle * | v,
|  | int * | cargo_left |
| ) |  |  | `[static]`

Loads/unload the vehicle if possible.

Parameters:

  | v | the vehicle to be (un)loaded
  | cargo_left | the amount of each cargo type that is virtually left on the platform to be picked up by another vehicle when all previous vehicles have loaded.

Definition at line 1548 of file economy.cpp.

References _cur_year, _settings_client, _settings_game, CalcPercentVehicleFilled(), CALLBACK_FAILED, CBID_VEHICLE_LOAD_AMOUNT, CBM_VEHICLE_LOAD_AMOUNT, CC_PASSENGERS, ClrBit(), COMPANY_SPECTATOR, GB(), Station::GetPlatformLength(), GetStationIndex(), GetVehicleCallback(), GetWindowClassForVehicleType(), Station::goods, OrderSettings::gradual_loading, ClientSettings::gui, HasBit(), OrderSettings::improved_load, PoolItem< T, Tid, Tpool >::index, InvalidateWindow(), IsNormalAircraft(), IsTileType(), Station::last_vehicle_type, GUISettings::loading_indicators, Station::MarkTilesDirty(), min(), MP_STATION, CargoList::MTA_CARGO_LOAD, CargoList::MTA_FINAL_DELIVERY, OLF_FULL_LOAD_ANY, OLFB_FULL_LOAD, OLFB_NO_LOAD, GameSettings::order, OUFB_NO_UNLOAD, OUFB_TRANSFER, OUFB_UNLOAD, SB(), SetBit(), TILE_SIZE, Station::time_since_load, Station::time_since_unload, and Station::xy.

Referenced by LoadUnloadStation().

void SetPriceBaseMultiplier | ( | uint | price,
|  | byte | factor |
| ) |  |  |

Change a price base by the given factor.

The price base is altered by factors of two, with an offset of 8. NewBaseCost = OldBaseCost * 2^(n-8)

Parameters:

  | price | Index of price base to change.
  | factor | Amount to change by.

Definition at line 761 of file economy.cpp.

void StartupIndustryDailyChanges | ( | bool | init_counter | ) |

Initialize the variables that will maintain the daily industry change system.

Parameters:

  | init_counter | specifies if the counter is required to be initialized

Definition at line 771 of file economy.cpp.

References MapLogX(), and MapLogY().

Referenced by Load_ECMY().

static void TriggerIndustryProduction | ( | Industry * | i | ) | `[static]`

Inform the industry about just delivered cargo DeliverGoodsToIndustry() silently incremented incoming_cargo_waiting, now it is time to do something with the new cargo.

Parameters:

  | i | The industry to process

Definition at line 1404 of file economy.cpp.

References _date, IndustrySpec::callback_flags, CBM_IND_PRODUCTION_256_TICKS, CBM_IND_PRODUCTION_CARGO_ARRIVAL, GetIndustrySpec(), HasBit(), Industry::incoming_cargo_waiting, PoolItem< T, Tid, Tpool >::index, IndustryProductionCallback(), IndustrySpec::input_cargo_multiplier, InvalidateWindow(), Industry::last_cargo_accepted_at, lengthof, min(), Industry::produced_cargo_waiting, Industry::type, and Industry::was_cargo_delivered.

Referenced by VehiclePayment().

int UpdateCompanyRatingAndValue | ( | Company * | c,
|  | bool | update |
| ) |  |  |

if update is set to true, the economy is updated with this score (also the house is updated, should only be true in the on-tick event)

Parameters:

  | update | the economy with calculated score
  | c | company been evaluated

Returns:
  actual score of this company

Definition at line 135 of file economy.cpp.

References Clamp(), ClampToI32(), CountBits(), Station::facilities, InvalidateWindow(), max(), min(), Station::owner, SCORE_END, SCORE_MAX, and SCORE_TOTAL.

Referenced by CmdBuildCompanyHQ().

void VehiclePayment | ( | Vehicle * | front_v | ) |

Performs the vehicle payment _and_ marks the vehicle to be unloaded.

Parameters:

  | front_v | the vehicle to be unloaded

Definition at line 1438 of file economy.cpp.

References SmallVector< T, S >::Begin(), SmallVector< T, S >::Clear(), ClrBit(), CargoPacket::count, CargoPacket::days_in_transit, DeliverGoods(), DistanceManhattan(), SmallVector< T, S >::End(), CargoPacket::feeder_share, Station::goods, HasBit(), CargoPacket::loaded_at_xy, OUFB_NO_UNLOAD, OUFB_TRANSFER, OUFB_UNLOAD, CargoPacket::paid_for, SetBit(), CargoPacket::source, CargoPacket::source_xy, Station::time_since_unload, and TriggerIndustryProduction().

## Variable Documentation

byte _price_category[NUM_PRICES]`[static]`

Initial value:

     {
      0, 2, 2, 2, 2, 2, 2, 2,
      2, 2, 2, 2, 2, 2, 2, 2,
      2, 2, 2, 2, 2, 2, 2, 2,
      2, 2, 2, 2, 2, 2, 2, 2,
      2, 2, 2, 2, 2, 2, 2, 2,
      2, 2, 1, 1, 1, 1, 1, 1,
      2,
    }

Definition at line 678 of file economy.cpp.

const ScoreInfo _score_info[]

Initial value:

     {
      { SCORE_VEHICLES,        120, 100 },
      { SCORE_STATIONS,         80, 100 },
      { SCORE_MIN_PROFIT,    10000, 100 },
      { SCORE_MIN_INCOME,    50000,  50 },
      { SCORE_MAX_INCOME,   100000, 100 },
      { SCORE_DELIVERED,     40000, 400 },
      { SCORE_CARGO,             8,  50 },
      { SCORE_MONEY,      10000000,  50 },
      { SCORE_LOAN,         250000,  50 },
      { SCORE_TOTAL,             0,   0 }
    }

Definition at line 74 of file economy.cpp.