OpenTTD
Functions | Variables
company_cmd.cpp File Reference

Handling of companies. More...

#include "stdafx.h"
#include "company_base.h"
#include "company_func.h"
#include "company_gui.h"
#include "town.h"
#include "news_func.h"
#include "cmd_helper.h"
#include "command_func.h"
#include "network/network.h"
#include "network/network_func.h"
#include "network/network_base.h"
#include "network/network_admin.h"
#include "ai/ai.hpp"
#include "company_manager_face.h"
#include "window_func.h"
#include "strings_func.h"
#include "date_func.h"
#include "sound_func.h"
#include "rail.h"
#include "core/pool_func.hpp"
#include "settings_func.h"
#include "vehicle_base.h"
#include "vehicle_func.h"
#include "smallmap_gui.h"
#include "game/game.hpp"
#include "goal_base.h"
#include "story_base.h"
#include "table/strings.h"
#include "safeguards.h"

Go to the source code of this file.

Functions

void ClearEnginesHiddenFlagOfCompany (CompanyID cid)
 Clear the 'hidden' flag for all engines of a new company. More...
 
void SetLocalCompany (CompanyID new_company)
 Sets the local company and updates the settings that are set on a per-company basis to reflect the core's state in the GUI. More...
 
TextColour GetDrawStringCompanyColour (CompanyID company)
 Get the colour for DrawString-subroutines which matches the colour of the company. More...
 
void DrawCompanyIcon (CompanyID c, int x, int y)
 Draw the icon of a company. More...
 
static bool IsValidCompanyManagerFace (CompanyManagerFace cmf)
 Checks whether a company manager's face is a valid encoding. More...
 
void InvalidateCompanyWindows (const Company *company)
 Refresh all windows owned by a company. More...
 
bool CheckCompanyHasMoney (CommandCost &cost)
 Verify whether the company can pay the bill. More...
 
static void SubtractMoneyFromAnyCompany (Company *c, CommandCost cost)
 Deduct costs of a command from the money of a company. More...
 
void SubtractMoneyFromCompany (CommandCost cost)
 Subtract money from the _current_company, if the company is valid. More...
 
void SubtractMoneyFromCompanyFract (CompanyID company, CommandCost cst)
 Subtract money from a company, including the money fraction. More...
 
void UpdateLandscapingLimits ()
 Update the landscaping limits per company. More...
 
void GetNameOfOwner (Owner owner, TileIndex tile)
 Set the right DParams to get the name of an owner. More...
 
CommandCost CheckOwnership (Owner owner, TileIndex tile)
 Check whether the current owner owns something. More...
 
CommandCost CheckTileOwnership (TileIndex tile)
 Check whether the current owner owns the stuff on the given tile. More...
 
static void GenerateCompanyName (Company *c)
 Generate the name of a company from the last build coordinate. More...
 
static Colours GenerateCompanyColour ()
 Generate a company colour. More...
 
static void GeneratePresidentName (Company *c)
 Generate a random president name of a company. More...
 
void ResetCompanyLivery (Company *c)
 Reset the livery schemes to the company's primary colour. More...
 
CompanyDoStartupNewCompany (bool is_ai, CompanyID company=INVALID_COMPANY)
 Create a new company and sets all company variables default values. More...
 
void StartupCompanies ()
 Start the next competitor now. More...
 
static void MaybeStartNewCompany ()
 Start a new competitor company if possible. More...
 
void InitializeCompanies ()
 Initialize the pool of companies. More...
 
bool MayCompanyTakeOver (CompanyID cbig, CompanyID csmall)
 May company cbig buy company csmall? More...
 
static void HandleBankruptcyTakeover (Company *c)
 Handle the bankruptcy take over of a company. More...
 
void OnTick_Companies ()
 Called every tick for updating some company info. More...
 
void CompaniesYearlyLoop ()
 A year has passed, update the economic data of all companies, and perhaps show the financial overview window of the local company.
 
void CompanyAdminUpdate (const Company *company)
 Called whenever company related information changes in order to notify admins. More...
 
void CompanyAdminRemove (CompanyID company_id, CompanyRemoveReason reason)
 Called whenever a company is removed in order to notify admins. More...
 
CommandCost CmdCompanyCtrl (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 Control the companies: add, delete, etc. More...
 
CommandCost CmdSetCompanyManagerFace (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 Change the company manager's face. More...
 
CommandCost CmdSetCompanyColour (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 Change the company's company-colour. More...
 
static bool IsUniqueCompanyName (const char *name)
 Is the given name in use as name of a company? More...
 
CommandCost CmdRenameCompany (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 Change the name of the company. More...
 
static bool IsUniquePresidentName (const char *name)
 Is the given name in use as president name of a company? More...
 
CommandCost CmdRenamePresident (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 Change the name of the president. More...
 
int CompanyServiceInterval (const Company *c, VehicleType type)
 Get the service interval for the given company and vehicle type. More...
 

Variables

CompanyByte _local_company
 Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
 
CompanyByte _current_company
 Company currently doing an action.
 
Colours _company_colours [MAX_COMPANIES]
 NOSAVE: can be determined from company structs.
 
CompanyManagerFace _company_manager_face
 for company manager face storage in openttd.cfg
 
uint _next_competitor_start
 the number of ticks before the next AI is started
 
uint _cur_company_tick_index
 used to generate a name for one company that doesn't have a name yet per tick
 
CompanyPool _company_pool ("Company")
 Pool of companies.
 
static const byte _colour_sort [COLOUR_END] = {2, 2, 3, 2, 3, 2, 3, 2, 3, 2, 2, 2, 3, 1, 1, 1}
 Sorting weights for the company colours. More...
 
static const Colours _similar_colour [COLOUR_END][2]
 Similar colours, so we can try to prevent same coloured companies. More...
 

Detailed Description

Handling of companies.

Definition in file company_cmd.cpp.

Function Documentation

◆ CheckCompanyHasMoney()

bool CheckCompanyHasMoney ( CommandCost cost)

Verify whether the company can pay the bill.

Parameters
cost[inout] Money to pay, is changed to an error if the company does not have enough money.
Returns
Function returns true if the company has enough money, else it returns false.

Definition at line 194 of file company_cmd.cpp.

References CommandCost::GetCost(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::GetIfValid(), CommandCost::MakeError(), CompanyProperties::money, and SetDParam().

◆ CheckOwnership()

CommandCost CheckOwnership ( Owner  owner,
TileIndex  tile 
)

Check whether the current owner owns something.

If that isn't the case an appropriate error will be given.

Parameters
ownerthe owner of the thing to check.
tileoptional tile to get the right town.
Precondition
if tile == 0 then the owner can't be OWNER_TOWN.
Returns
A succeeded command iff it's owned by the current company, else a failed command.

Definition at line 310 of file company_cmd.cpp.

Referenced by CanRemoveRoadWithStop(), CmdAutofillTimetable(), CmdAutoreplaceVehicle(), CmdChangeServiceInt(), CmdChangeTimetable(), CmdCloneOrder(), CmdCloneVehicle(), CmdDeleteOrder(), CmdForceTrainProceed(), CmdInsertOrder(), CmdMoveOrder(), CmdMoveRailVehicle(), CmdOpenCloseAirport(), CmdOrderRefit(), CmdRefitVehicle(), CmdRenameStation(), CmdRenameVehicle(), CmdRenameWaypoint(), CmdReverseTrainDirection(), CmdSellVehicle(), CmdSetTimetableStart(), CmdSetVehicleOnTime(), CmdSkipToOrder(), CmdStartStopVehicle(), CmdTurnRoadVeh(), RemoveAirport(), RemoveDock(), RemoveRoadStop(), and Vehicle::SendToDepot().

◆ CheckTileOwnership()

CommandCost CheckTileOwnership ( TileIndex  tile)

Check whether the current owner owns the stuff on the given tile.

If that isn't the case an appropriate error will be given.

Parameters
tilethe tile to check.
Returns
A succeeded command iff it's owned by the current company, else a failed command.

Definition at line 328 of file company_cmd.cpp.

References GetTileOwner().

Referenced by CmdConvertRail(), CmdRenameDepot(), and RemoveLock().

◆ ClearEnginesHiddenFlagOfCompany()

void ClearEnginesHiddenFlagOfCompany ( CompanyID  cid)

Clear the 'hidden' flag for all engines of a new company.

Parameters
cidCompany being created.

Definition at line 843 of file engine.cpp.

◆ CmdCompanyCtrl()

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

Control the companies: add, delete, etc.

Parameters
tileunused
flagsoperation to perform
p1various functionality
  • bits 0..15: = 0 - create a new company = 1 - create a new AI company = 2 - delete a company
  • bits 16..24: CompanyID
p2ClientID
textunused
Returns
the cost of this operation or an error

Definition at line 807 of file company_cmd.cpp.

References InvalidateWindowData(), and WC_COMPANY_LEAGUE.

◆ CmdRenameCompany()

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

Change the name of the company.

Parameters
tileunused
flagsoperation to perform
p1unused
p2unused
textthe new name or an empty string when resetting to the default
Returns
the cost of this operation or an error

Definition at line 1070 of file company_cmd.cpp.

References CMD_ERROR, CompanyAdminUpdate(), DC_EXEC, free(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::Get(), IsUniqueCompanyName(), MarkWholeScreenDirty(), MAX_LENGTH_COMPANY_NAME_CHARS, CompanyProperties::name, return_cmd_error, stredup(), StrEmpty(), and Utf8StringLength().

◆ CmdRenamePresident()

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

Change the name of the president.

Parameters
tileunused
flagsoperation to perform
p1unused
p2unused
textthe new name or an empty string when resetting to the default
Returns
the cost of this operation or an error

Definition at line 1115 of file company_cmd.cpp.

References CMD_ERROR, CMD_RENAME_COMPANY, CompanyAdminUpdate(), DC_EXEC, DoCommand(), free(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::Get(), IsUniquePresidentName(), lastof, MarkWholeScreenDirty(), MAX_LENGTH_PRESIDENT_NAME_CHARS, CompanyProperties::name, CompanyProperties::name_1, CompanyProperties::president_name, return_cmd_error, seprintf(), stredup(), StrEmpty(), and Utf8StringLength().

◆ CmdSetCompanyColour()

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

Change the company's company-colour.

Parameters
tileunused
flagsoperation to perform
p1bitstuffed: p1 bits 0-7 scheme to set p1 bits 8-9 set in use state or first/second colour
p2new colour for vehicles, property, etc.
textunused
Returns
the cost of this operation or an error

Definition at line 954 of file company_cmd.cpp.

References CompanyProperties::colour, and GB().

◆ CmdSetCompanyManagerFace()

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

Change the company manager's face.

Parameters
tileunused
flagsoperation to perform
p1unused
p2face bitmasked
textunused
Returns
the cost of this operation or an error

Definition at line 930 of file company_cmd.cpp.

References CMD_ERROR, DC_EXEC, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::Get(), IsValidCompanyManagerFace(), and MarkWholeScreenDirty().

◆ CompanyAdminRemove()

void CompanyAdminRemove ( CompanyID  company_id,
CompanyRemoveReason  reason 
)

Called whenever a company is removed in order to notify admins.

Parameters
company_idThe company that was removed.
reasonThe reason the company was removed.

Definition at line 786 of file company_cmd.cpp.

References _network_server, and NetworkAdminCompanyRemove().

◆ CompanyAdminUpdate()

void CompanyAdminUpdate ( const Company company)

Called whenever company related information changes in order to notify admins.

Parameters
companyThe company data changed of.

Definition at line 774 of file company_cmd.cpp.

References _network_server, and NetworkAdminCompanyUpdate().

Referenced by CmdRenameCompany(), and CmdRenamePresident().

◆ CompanyServiceInterval()

int CompanyServiceInterval ( const Company c,
VehicleType  type 
)

Get the service interval for the given company and vehicle type.

Parameters
cThe company, or NULL for client-default settings.
typeThe vehicle type to get the interval for.
Returns
The service interval.

Definition at line 1154 of file company_cmd.cpp.

References _settings_client, ClientSettings::company, Company::settings, and CompanySettings::vehicle.

Referenced by CmdChangeServiceInt(), and IsInteractiveCompany().

◆ DoStartupNewCompany()

Company* DoStartupNewCompany ( bool  is_ai,
CompanyID  company = INVALID_COMPANY 
)

◆ DrawCompanyIcon()

void DrawCompanyIcon ( CompanyID  c,
int  x,
int  y 
)

Draw the icon of a company.

Parameters
cCompany that needs its icon drawn.
xHorizontal coordinate of the icon.
yVertical coordinate of the icon.

Definition at line 141 of file company_cmd.cpp.

Referenced by GetLobbyCompanyInfo(), and AIDebugWindow::OnPaint().

◆ GenerateCompanyColour()

static Colours GenerateCompanyColour ( )
static

Generate a company colour.

Returns
Generated company colour.

Definition at line 428 of file company_cmd.cpp.

Referenced by DoStartupNewCompany().

◆ GenerateCompanyName()

static void GenerateCompanyName ( Company c)
static

Generate the name of a company from the last build coordinate.

Parameters
cCompany to give a name.

Definition at line 345 of file company_cmd.cpp.

References ClosestTownFromTile(), IsInsideMM(), CompanyProperties::last_build_coordinate, MAX_CHAR_LENGTH, MAX_LENGTH_COMPANY_NAME_CHARS, Town::name, and CompanyProperties::name_1.

◆ GeneratePresidentName()

static void GeneratePresidentName ( Company c)
static

Generate a random president name of a company.

Parameters
cCompany that needs a new president name.

Definition at line 484 of file company_cmd.cpp.

References CompanyProperties::president_name_2.

◆ GetDrawStringCompanyColour()

TextColour GetDrawStringCompanyColour ( CompanyID  company)

Get the colour for DrawString-subroutines which matches the colour of the company.

Parameters
companyCompany to get the colour of.
Returns
Colour of company.

Definition at line 129 of file company_cmd.cpp.

References _colour_gradient, and Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID().

◆ GetNameOfOwner()

void GetNameOfOwner ( Owner  owner,
TileIndex  tile 
)

Set the right DParams to get the name of an owner.

Parameters
ownerthe owner to get the name of.
tileoptional tile to get the right town.
Precondition
if tile == 0, then owner can't be OWNER_TOWN.

Definition at line 281 of file company_cmd.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID(), OWNER_TOWN, and SetDParam().

◆ HandleBankruptcyTakeover()

static void HandleBankruptcyTakeover ( Company c)
static

Handle the bankruptcy take over of a company.

Companies going bankrupt will ask the other companies in order of their performance rating, so better performing companies get the 'do you want to merge with Y' question earlier. The question will then stay till either the company has gone bankrupt or got merged with a company.

Parameters
cthe company that is going bankrupt.

Definition at line 643 of file company_cmd.cpp.

References DAY_TICKS, and MAX_COMPANIES.

◆ InitializeCompanies()

void InitializeCompanies ( )

Initialize the pool of companies.

Definition at line 611 of file company_cmd.cpp.

References _cur_company_tick_index.

◆ InvalidateCompanyWindows()

void InvalidateCompanyWindows ( const Company company)

Refresh all windows owned by a company.

Parameters
companyCompany that changed, and needs its windows refreshed.

Definition at line 181 of file company_cmd.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, SetWindowDirty(), WC_FINANCES, and WC_STATUS_BAR.

Referenced by CmdDecreaseLoan().

◆ IsUniqueCompanyName()

static bool IsUniqueCompanyName ( const char *  name)
static

Is the given name in use as name of a company?

Parameters
nameName to search.
Returns
true if the name us unique (that is, not in use), else false.

Definition at line 1050 of file company_cmd.cpp.

Referenced by CmdRenameCompany().

◆ IsUniquePresidentName()

static bool IsUniquePresidentName ( const char *  name)
static

Is the given name in use as president name of a company?

Parameters
nameName to search.
Returns
true if the name us unique (that is, not in use), else false.

Definition at line 1095 of file company_cmd.cpp.

Referenced by CmdRenamePresident().

◆ IsValidCompanyManagerFace()

static bool IsValidCompanyManagerFace ( CompanyManagerFace  cmf)
static

Checks whether a company manager's face is a valid encoding.

Unused bits are not enforced to be 0.

Parameters
cmfthe fact to check
Returns
true if and only if the face is valid

Definition at line 152 of file company_cmd.cpp.

References AreCompanyManagerFaceBitsValid(), GE_WM, GENDER_FEMALE, GetCompanyManagerFaceBits(), and HasBit().

Referenced by CmdSetCompanyManagerFace().

◆ MaybeStartNewCompany()

static void MaybeStartNewCompany ( )
static

◆ MayCompanyTakeOver()

bool MayCompanyTakeOver ( CompanyID  cbig,
CompanyID  csmall 
)

◆ OnTick_Companies()

void OnTick_Companies ( )

Called every tick for updating some company info.

Definition at line 697 of file company_cmd.cpp.

◆ ResetCompanyLivery()

void ResetCompanyLivery ( Company c)

Reset the livery schemes to the company's primary colour.

This is used on loading games without livery information and on new company start up.

Parameters
cCompany to reset.

Definition at line 517 of file company_cmd.cpp.

Referenced by DoStartupNewCompany().

◆ SetLocalCompany()

void SetLocalCompany ( CompanyID  new_company)

Sets the local company and updates the settings that are set on a per-company basis to reflect the core's state in the GUI.

Parameters
new_companythe new company
Precondition
Company::IsValidID(new_company) || new_company == COMPANY_SPECTATOR || new_company == OWNER_NONE

Definition at line 102 of file company_cmd.cpp.

Referenced by ClickChangeCompanyCheat(), GenerateWorld(), and NetworkServerDoMove().

◆ StartupCompanies()

void StartupCompanies ( )

Start the next competitor now.

Definition at line 583 of file company_cmd.cpp.

References _next_competitor_start.

◆ SubtractMoneyFromAnyCompany()

static void SubtractMoneyFromAnyCompany ( Company c,
CommandCost  cost 
)
static

Deduct costs of a command from the money of a company.

Parameters
cCompany to pay the bill.
costMoney to pay.

Definition at line 212 of file company_cmd.cpp.

References CommandCost::GetCost().

Referenced by SubtractMoneyFromCompany().

◆ SubtractMoneyFromCompany()

void SubtractMoneyFromCompany ( CommandCost  cost)

Subtract money from the _current_company, if the company is valid.

Parameters
costMoney to pay.

Definition at line 241 of file company_cmd.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::GetIfValid(), and SubtractMoneyFromAnyCompany().

◆ SubtractMoneyFromCompanyFract()

void SubtractMoneyFromCompanyFract ( CompanyID  company,
CommandCost  cst 
)

Subtract money from a company, including the money fraction.

Parameters
companyCompany paying the bill.
cstCost of a command.

Definition at line 252 of file company_cmd.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::Get(), CommandCost::GetCost(), and CompanyProperties::money_fraction.

◆ UpdateLandscapingLimits()

void UpdateLandscapingLimits ( )

Update the landscaping limits per company.

Definition at line 265 of file company_cmd.cpp.

Referenced by StateGameLoop().

Variable Documentation

◆ _colour_sort

const byte _colour_sort[COLOUR_END] = {2, 2, 3, 2, 3, 2, 3, 2, 3, 2, 2, 2, 3, 1, 1, 1}
static

Sorting weights for the company colours.

Definition at line 403 of file company_cmd.cpp.

◆ _similar_colour

const Colours _similar_colour[COLOUR_END][2]
static
Initial value:
= {
{ COLOUR_BLUE, COLOUR_LIGHT_BLUE },
{ COLOUR_GREEN, COLOUR_DARK_GREEN },
{ INVALID_COLOUR, INVALID_COLOUR },
{ COLOUR_ORANGE, INVALID_COLOUR },
{ INVALID_COLOUR, INVALID_COLOUR },
{ COLOUR_DARK_BLUE, COLOUR_BLUE },
{ COLOUR_PALE_GREEN, COLOUR_DARK_GREEN },
{ COLOUR_PALE_GREEN, COLOUR_GREEN },
{ COLOUR_DARK_BLUE, COLOUR_LIGHT_BLUE },
{ COLOUR_BROWN, COLOUR_ORANGE },
{ COLOUR_PURPLE, INVALID_COLOUR },
{ COLOUR_MAUVE, INVALID_COLOUR },
{ COLOUR_YELLOW, COLOUR_CREAM },
{ COLOUR_CREAM, INVALID_COLOUR },
{ COLOUR_WHITE, INVALID_COLOUR },
{ COLOUR_GREY, INVALID_COLOUR },
}

Similar colours, so we can try to prevent same coloured companies.

Definition at line 405 of file company_cmd.cpp.