# economy.cpp File Reference

`#include "stdafx.h"`
`#include "openttd.h"`
`#include "currency.h"`
`#include "landscape.h"`
`#include "news.h"`
`#include "player_base.h"`
`#include "player_func.h"`
`#include "station.h"`
`#include "command_func.h"`
`#include "saveload.h"`
`#include "industry.h"`
`#include "town.h"`
`#include "network/network.h"`
`#include "engine.h"`
`#include "network/network_data.h"`
`#include "variables.h"`
`#include "vehicle_gui.h"`
`#include "ai/ai.h"`
`#include "train.h"`
`#include "roadveh.h"`
`#include "aircraft.h"`
`#include "newgrf_engine.h"`
`#include "newgrf_sound.h"`
`#include "newgrf_callbacks.h"`
`#include "newgrf_industries.h"`
`#include "newgrf_industrytiles.h"`
`#include "unmovable.h"`
`#include "cargotype.h"`
`#include "player_face.h"`
`#include "group.h"`
`#include "strings_func.h"`
`#include "tile_cmd.h"`
`#include "functions.h"`
`#include "window_func.h"`
`#include "date_func.h"`
`#include "vehicle_func.h"`
`#include "sound_func.h"`
`#include "track_type.h"`
`#include "track_func.h"`
`#include "road_func.h"`
`#include "rail_map.h"`
`#include "signal_func.h"`
`#include "gfx_func.h"`
`#include "autoreplace_func.h"`
`#include "signs.h"`
`#include "table/strings.h"`
`#include "table/sprites.h"`

Go to the source code of this file.

## Data Structures
struct | FoundRoute
## 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 Player *p)
int | UpdateCompanyRatingAndValue (Player *p, 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 | ChangeOwnershipOfPlayerItems (PlayerID old_player, PlayerID new_player)
static void | ChangeNetworkOwner (PlayerID current_player, PlayerID new_player)
static void | PlayersCheckBankrupt (Player *p)
void | DrawNewsBankrupcy (Window *w)
StringID | GetNewsStringBankrupcy (const NewsItem *ni)
static void | PlayersGenStatistics ()
static void | AddSingleInflation (Money *value, uint16 *frac, int32 amt)
static void | AddInflation ()
static void | PlayersPayInterest ()
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 | StartupEconomy ()
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 ()
static void | Save_SUBS ()
static void | Load_SUBS ()
Money | GetTransportedGoodsIncome (uint num_pieces, uint dist, byte transit_days, CargoID cargo_type)
static void | DeliverGoodsToIndustry (TileIndex xy, CargoID cargo_type, int num_pieces)
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)
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 | PlayersMonthlyLoop ()
static void | DoAcquireCompany (Player *p)
int | GetAmountOwnedBy (const Player *p, PlayerID owner)
CommandCost | CmdBuyShareInCompany (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Acquire shares in an opposing company.
CommandCost | CmdSellShareInCompany (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Sell shares in an opposing company.
CommandCost | CmdBuyCompany (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Buy up another company.
static void | SaveLoad_PRIC ()
| Prices.
static void | SaveLoad_CAPR ()
| Cargo payment rates.
static void | SaveLoad_ECMY ()
| Economy variables.
## Variables
const ScoreInfo | _score_info []
int | _score_part [MAX_PLAYERS][SCORE_END]
Economy | _economy
Subsidy | _subsidies [MAX_PLAYERS]
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]
static const SaveLoad | _subsidies_desc []
static const SaveLoad | _economy_desc []
const ChunkHandler | _economy_chunk_handlers []

## Detailed Description

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 65 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 81 of file economy.cpp.

CommandCost CmdBuyCompany | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

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 players

Parameters:

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

Definition at line 1976 of file economy.cpp.

References _networking, CMD_ERROR, and DC_EXEC.

CommandCost CmdBuyShareInCompany | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Acquire shares in an opposing company.

Parameters:

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

Definition at line 1888 of file economy.cpp.

References CommandCost::AddCost(), CMD_ERROR, DC_EXEC, PLAYER_SPECTATOR, and return_cmd_error.

CommandCost CmdSellShareInCompany | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Sell shares in an opposing company.

Parameters:

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

Definition at line 1937 of file economy.cpp.

References CMD_ERROR, DC_EXEC, and PLAYER_SPECTATOR.

static void DeliverGoodsToIndustry | ( | TileIndex | xy,
|  | CargoID | cargo_type,
|  | int | num_pieces |
| ) |  |  | `[static]`

< unlikely value, just for warning removing

Definition at line 1297 of file economy.cpp.

References Industry::accepts_cargo, IndustrySpec::callback_flags, CBID_INDUSTRY_REFUSE_CARGO, CBM_IND_PRODUCTION_256_TICKS, CBM_IND_PRODUCTION_CARGO_ARRIVAL, CBM_IND_REFUSE_CARGO, DistanceManhattan(), GetIndustrySpec(), IndustrySpec::grf_prop, GRFFileProps::grffile, HasBit(), Industry::incoming_cargo_waiting, PoolItem< T, Tid, Tpool >::index, IndustryProductionCallback(), IndustrySpec::input_cargo_multiplier, Industry::last_cargo_accepted_at, min(), Industry::produced_cargo_waiting, Industry::type, Industry::was_cargo_delivered, and Industry::xy.

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 1819 of file economy.cpp.

References 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 1576 of file economy.cpp.

References CalcPercentVehicleFilled(), CALLBACK_FAILED, CBID_VEHICLE_LOAD_AMOUNT, CBM_VEHICLE_LOAD_AMOUNT, CC_PASSENGERS, ClrBit(), GB(), GetVehicleCallback(), HasBit(), IsTileType(), min(), MP_STATION, CargoList::MTA_CARGO_LOAD, CargoList::MTA_FINAL_DELIVERY, OFB_TRANSFER, OFB_UNLOAD, PLAYER_SPECTATOR, SB(), SetBit(), and TILE_SIZE.

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 899 of file economy.cpp.

int UpdateCompanyRatingAndValue | ( | Player * | p,
|  | 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
  | p | player been evaluated

Returns:
  actual score of this player

Definition at line 148 of file economy.cpp.

References Clamp(), ClampToI32(), CountBits(), max(), min(), 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 1476 of file economy.cpp.

References ClrBit(), CargoPacket::count, CargoPacket::days_in_transit, DistanceManhattan(), CargoPacket::feeder_share, HasBit(), CargoPacket::loaded_at_xy, OFB_TRANSFER, OFB_UNLOAD, CargoPacket::paid_for, SetBit(), CargoPacket::source, and CargoPacket::source_xy.

## Variable Documentation

const ChunkHandler _economy_chunk_handlers[]

Initial value:

     {
      { 'PRIC', SaveLoad_PRIC, SaveLoad_PRIC, CH_RIFF | CH_AUTO_LENGTH},
      { 'CAPR', SaveLoad_CAPR, SaveLoad_CAPR, CH_RIFF | CH_AUTO_LENGTH},
      { 'SUBS', Save_SUBS,     Load_SUBS,     CH_ARRAY},
      { 'ECMY', SaveLoad_ECMY, SaveLoad_ECMY, CH_RIFF | CH_LAST},
    }

const SaveLoad _economy_desc[]`[static]`

Initial value:

     {
      SLE_CONDVAR(Economy, max_loan,         SLE_FILE_I32 | SLE_VAR_I64,  0, 64),
      SLE_CONDVAR(Economy, max_loan,         SLE_INT64,                  65, SL_MAX_VERSION),
      SLE_CONDVAR(Economy, max_loan_unround, SLE_FILE_I32 | SLE_VAR_I64,  0, 64),
      SLE_CONDVAR(Economy, max_loan_unround, SLE_INT64,                  65, SL_MAX_VERSION),
      SLE_CONDVAR(Economy, max_loan_unround_fract, SLE_UINT16,           70, SL_MAX_VERSION),
          SLE_VAR(Economy, fluct,            SLE_INT16),
          SLE_VAR(Economy, interest_rate,    SLE_UINT8),
          SLE_VAR(Economy, infl_amount,      SLE_UINT8),
          SLE_VAR(Economy, infl_amount_pr,   SLE_UINT8),

    }

Definition at line 2014 of file economy.cpp.

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 816 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 87 of file economy.cpp.

const SaveLoad _subsidies_desc[]`[static]`

Initial value:

     {
          SLE_VAR(Subsidy, cargo_type, SLE_UINT8),
          SLE_VAR(Subsidy, age,        SLE_UINT8),
      SLE_CONDVAR(Subsidy, from,       SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
      SLE_CONDVAR(Subsidy, from,       SLE_UINT16,                5, SL_MAX_VERSION),
      SLE_CONDVAR(Subsidy, to,         SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
      SLE_CONDVAR(Subsidy, to,         SLE_UINT16,                5, SL_MAX_VERSION),

    }

Definition at line 1210 of file economy.cpp.