Functions | Variables

company_cmd.cpp File Reference

Handling of companies. More...

#include "stdafx.h"
#include "engine_base.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 "sprite.h"
#include "smallmap_gui.h"
#include "game/game.hpp"
#include "table/strings.h"

Go to the source code of this file.

Functions

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.
TextColour GetDrawStringCompanyColour (CompanyID company)
 Get the colour for DrawString-subroutines which matches the colour of the company.
void DrawCompanyIcon (CompanyID c, int x, int y)
 Draw the icon of a company.
static bool IsValidCompanyManagerFace (CompanyManagerFace cmf)
 Checks whether a company manager's face is a valid encoding.
void InvalidateCompanyWindows (const Company *company)
 Refresh all windows owned by a company.
bool CheckCompanyHasMoney (CommandCost &cost)
 Verify whether the company can pay the bill.
static void SubtractMoneyFromAnyCompany (Company *c, CommandCost cost)
 Deduct costs of a command from the money of a company.
void SubtractMoneyFromCompany (CommandCost cost)
 Subtract money from the _current_company, if the company is valid.
void SubtractMoneyFromCompanyFract (CompanyID company, CommandCost cst)
 Subtract money from a company, including the money fraction.
void UpdateLandscapingLimits ()
 Update the landscaping limits per company.
void GetNameOfOwner (Owner owner, TileIndex tile)
 Set the right DParams to get the name of an owner.
CommandCost CheckOwnership (Owner owner, TileIndex tile)
 Check whether the current owner owns something.
CommandCost CheckTileOwnership (TileIndex tile)
 Check whether the current owner owns the stuff on the given tile.
static void GenerateCompanyName (Company *c)
 Generate the name of a company from the last build coordinate.
static Colours GenerateCompanyColour ()
 Generate a company colour.
static void GeneratePresidentName (Company *c)
 Generate a random president name of a company.
void ResetCompanyLivery (Company *c)
 Reset the livery schemes to the company's primary colour.
CompanyDoStartupNewCompany (bool is_ai, CompanyID company=INVALID_COMPANY)
 Create a new company and sets all company variables default values.
void StartupCompanies ()
 Start the next competitor now.
static void MaybeStartNewCompany ()
 Start a new competitor company if possible.
void InitializeCompanies ()
 Initialize the pool of companies.
bool MayCompanyTakeOver (CompanyID cbig, CompanyID csmall)
 May company cbig buy company csmall?
static void HandleBankruptcyTakeover (Company *c)
 Handle the bankruptcy take over of a company.
void OnTick_Companies ()
 Called every tick for updating some company info.
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.
void CompanyAdminRemove (CompanyID company_id, CompanyRemoveReason reason)
 Called whenever a company is removed in order to notify admins.
CommandCost CmdCompanyCtrl (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 Control the companies: add, delete, etc.
CommandCost CmdSetCompanyManagerFace (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 Change the company manager's face.
CommandCost CmdSetCompanyColour (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 Change the company's company-colour.
static bool IsUniqueCompanyName (const char *name)
 Is the given name in use as name of a company?
CommandCost CmdRenameCompany (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 Change the name of the company.
static bool IsUniquePresidentName (const char *name)
 Is the given name in use as president name of a company?
CommandCost CmdRenamePresident (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 Change the name of the president.

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.
static const Colours _similar_colour [COLOUR_END][2]
 Similar colours, so we can try to prevent same coloured companies.

Detailed Description

Handling of companies.

Definition in file company_cmd.cpp.


Function Documentation

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 189 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().

Referenced by CmdCloneVehicle(), DoCommand(), and DoCommandPInternal().

CommandCost CheckOwnership ( Owner  owner,
TileIndex  tile 
)
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:
tile the tile to check.
Returns:
A succeeded command iff it's owned by the current company, else a failed command.

Definition at line 322 of file company_cmd.cpp.

References GetNameOfOwner(), GetTileOwner(), IsLocalCompany(), OWNER_END, and return_cmd_error.

Referenced by CheckAllowRemoveTunnelBridge(), CmdBuildSingleRail(), CmdBuildSingleSignal(), CmdConvertRail(), CmdRemoveSingleRail(), CmdRemoveSingleSignal(), CmdRenameDepot(), and RemoveLock().

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

Control the companies: add, delete, etc.

Parameters:
tile unused
flags operation to perform
p1 various functionality

  • bits 0..15: = 0 - create a new company = 1 - create a new AI company = 2 - delete a company
  • bits 16..24: CompanyID
p2 ClientID
text unused
Returns:
the cost of this operation or an error

Definition at line 800 of file company_cmd.cpp.

References _network_company_states, _network_own_client_id, _network_server, _networking, _settings_client, AI::BroadcastNewEvent(), ChangeOwnershipOfCompanyItems(), NetworkClientInfo::client_id, NetworkClientInfo::client_name, NetworkClientInfo::client_playas, CMD_ERROR, CMD_RENAME_PRESIDENT, CompanyNewsInformation::company_name, COMPANY_SPECTATOR, CompanyAdminRemove(), CRR_END, DC_EXEC, NetworkSettings::default_company_pass, DeleteCompanyWindows(), DeleteWindowById(), DESTTYPE_BROADCAST, DoStartupNewCompany(), GB(), NetworkClientInfo::GetByClientID(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::GetIfValid(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, INVALID_COMPANY, INVALID_OWNER, InvalidateWindowClassesData(), InvalidateWindowData(), CompanyProperties::is_ai, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID(), MarkWholeScreenDirty(), MAX_COMPANIES, NetworkCompanyState::months_empty, ClientSettings::network, NetworkAdminCompanyInfo(), NetworkChangeCompanyPassword(), NetworkSendCommand(), NetworkServerSendChat(), NetworkServerUpdateCompanyPassworded(), NetworkUpdateClientInfo(), Game::NewEvent(), NS_COMPANY_BANKRUPT, NetworkCompanyState::password, SetDParam(), SetDParamStr(), SetLocalCompany(), AI::Stop(), StrEmpty(), SyncCompanySettings(), WC_AI_LIST, WC_AI_SETTINGS, WC_COMPANY_LEAGUE, WC_GAME_OPTIONS, WC_NETWORK_STATUS_WINDOW, and WN_NETWORK_STATUS_WINDOW_JOIN.

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

Change the name of the company.

Parameters:
tile unused
flags operation to perform
p1 unused
p2 unused
text the new name or an empty string when resetting to the default
Returns:
the cost of this operation or an error

Definition at line 1077 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, StrEmpty(), and Utf8StringLength().

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

Change the name of the president.

Parameters:
tile unused
flags operation to perform
p1 unused
p2 unused
text the new name or an empty string when resetting to the default
Returns:
the cost of this operation or an error

Definition at line 1122 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(), lengthof, MarkWholeScreenDirty(), MAX_LENGTH_PRESIDENT_NAME_CHARS, CompanyProperties::name, CompanyProperties::name_1, CompanyProperties::president_name, return_cmd_error, StrEmpty(), and Utf8StringLength().

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

Change the company's company-colour.

Parameters:
tile unused
flags operation to perform
p1 bitstuffed: p1 bits 0-7 scheme to set p1 bits 8-9 set in use state or first/second colour
p2 new colour for vehicles, property, etc.
text unused
Returns:
the cost of this operation or an error

Definition at line 961 of file company_cmd.cpp.

References _company_colours, BuildOwnerLegend(), CMD_ERROR, CompanyProperties::colour, Livery::colour1, Livery::colour2, CompanyAdminUpdate(), DC_EXEC, FOR_ALL_VEHICLES, GB(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::Get(), Livery::in_use, Vehicle::InvalidateNewGRFCache(), InvalidateWindowData(), MarkWholeScreenDirty(), Vehicle::owner, UpdateObjectColours(), WC_COMPANY_VALUE, WC_DELIVERED_CARGO, WC_INCOME_GRAPH, WC_LINKGRAPH_LEGEND, WC_OPERATING_PROFIT, WC_PERFORMANCE_HISTORY, and WC_SMALLMAP.

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

Change the company manager's face.

Parameters:
tile unused
flags operation to perform
p1 unused
p2 face bitmasked
text unused
Returns:
the cost of this operation or an error

Definition at line 937 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().

void CompanyAdminRemove ( CompanyID  company_id,
CompanyRemoveReason  reason 
)

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

Parameters:
company_id The company that was removed.
reason The reason the company was removed.

Definition at line 779 of file company_cmd.cpp.

References _network_server, and NetworkAdminCompanyRemove().

Referenced by CmdCompanyCtrl().

void CompanyAdminUpdate ( const Company company  ) 

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

Parameters:
company The company data changed of.

Definition at line 767 of file company_cmd.cpp.

References _network_server, and NetworkAdminCompanyUpdate().

Referenced by CmdBuyShareInCompany(), CmdRenameCompany(), CmdRenamePresident(), CmdSellShareInCompany(), and CmdSetCompanyColour().

Company* DoStartupNewCompany ( bool  is_ai,
CompanyID  company = INVALID_COMPANY 
)
void DrawCompanyIcon ( CompanyID  c,
int  x,
int  y 
)

Draw the icon of a company.

Parameters:
c Company that needs its icon drawn.
x Horizontal coordinate of the icon.
y Vertical coordinate of the icon.

Definition at line 136 of file company_cmd.cpp.

References DrawSprite().

Referenced by TownAuthorityWindow::DrawRatings(), NetworkClientListWindow::DrawWidget(), LinkGraphLegendWindow::DrawWidget(), PerformanceRatingDetailWindow::DrawWidget(), CompanyLeagueWindow::DrawWidget(), GraphLegendWindow::DrawWidget(), and CheatWindow::DrawWidget().

static Colours GenerateCompanyColour (  )  [static]

Generate a company colour.

Returns:
Generated company colour.

Definition at line 422 of file company_cmd.cpp.

References _colour_sort, _similar_colour, CompanyProperties::colour, GB(), and Swap().

Referenced by DoStartupNewCompany().

static void GenerateCompanyName ( Company c  )  [static]
static void GeneratePresidentName ( Company c  )  [static]
TextColour GetDrawStringCompanyColour ( CompanyID  company  ) 

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

Parameters:
company Company to get the colour of.
Returns:
Colour of company.

Definition at line 124 of file company_cmd.cpp.

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

Referenced by NetworkServerSendChat(), and ClientNetworkGameSocketHandler::Receive_SERVER_CHAT().

void GetNameOfOwner ( Owner  owner,
TileIndex  tile 
)

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

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

Definition at line 275 of file company_cmd.cpp.

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

Referenced by CheckOwnership(), CheckTileOwnership(), and LandInfoWindow::OnInit().

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:
c the company that is going bankrupt.

Definition at line 636 of file company_cmd.cpp.

References CompanyProperties::bankrupt_asked, CompanyProperties::bankrupt_timeout, ClampToI32(), DAY_TICKS, HasBit(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, CompanyProperties::is_ai, IsInteractiveCompany(), MAX_COMPANIES, MAX_UVALUE, MayCompanyTakeOver(), AI::NewEvent(), CompanyProperties::old_economy, CompanyEconomyEntry::performance_history, SetBit(), and ShowBuyCompanyDialog().

Referenced by OnTick_Companies().

void InitializeCompanies (  ) 

Initialize the pool of companies.

Definition at line 604 of file company_cmd.cpp.

References _cur_company_tick_index.

void InvalidateCompanyWindows ( const Company company  ) 

Refresh all windows owned by a company.

Parameters:
company Company that changed, and needs its windows refreshed.

Definition at line 176 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(), CmdIncreaseLoan(), and SubtractMoneyFromAnyCompany().

static bool IsUniqueCompanyName ( const char *  name  )  [static]

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

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

Definition at line 1057 of file company_cmd.cpp.

References CompanyProperties::name.

Referenced by CmdRenameCompany().

static bool IsUniquePresidentName ( const char *  name  )  [static]

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

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

Definition at line 1102 of file company_cmd.cpp.

References CompanyProperties::president_name.

Referenced by CmdRenamePresident().

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:
cmf the fact to check
Returns:
true if and only if the face is valid

Definition at line 147 of file company_cmd.cpp.

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

Referenced by CmdSetCompanyManagerFace().

static void MaybeStartNewCompany (  )  [static]
bool MayCompanyTakeOver ( CompanyID  cbig,
CompanyID  csmall 
)
void OnTick_Companies (  ) 
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:
c Company to reset.

Definition at line 511 of file company_cmd.cpp.

References CompanyProperties::colour, Livery::colour1, Livery::colour2, and Livery::in_use.

Referenced by AfterLoadGame(), and DoStartupNewCompany().

void SetLocalCompany ( CompanyID  new_company  ) 
void StartupCompanies (  ) 

Start the next competitor now.

Definition at line 576 of file company_cmd.cpp.

References _next_competitor_start.

Referenced by _GenerateWorld().

static void SubtractMoneyFromAnyCompany ( Company c,
CommandCost  cost 
) [static]
void SubtractMoneyFromCompany ( CommandCost  cost  ) 
void SubtractMoneyFromCompanyFract ( CompanyID  company,
CommandCost  cst 
)

Subtract money from a company, including the money fraction.

Parameters:
company Company paying the bill.
cst Cost of a command.

Definition at line 247 of file company_cmd.cpp.

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

void UpdateLandscapingLimits (  ) 

Variable Documentation

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 397 of file company_cmd.cpp.

Referenced by GenerateCompanyColour().

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 399 of file company_cmd.cpp.

Referenced by GenerateCompanyColour().