# company_cmd.cpp File Reference

Handling of companies. More...

`#include "stdafx.h"`
`#include "engine_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 "ai/ai.hpp"`
`#include "company_manager_face.h"`
`#include "group.h"`
`#include "window_func.h"`
`#include "strings_func.h"`
`#include "gfx_func.h"`
`#include "date_func.h"`
`#include "sound_func.h"`
`#include "autoreplace_func.h"`
`#include "autoreplace_gui.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 "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.
uint16 | GetDrawStringCompanyColour (CompanyID company)
void | DrawCompanyIcon (CompanyID c, int x, int y)
bool | IsValidCompanyManagerFace (CompanyManagerFace cmf)
| Checks whether a company manager's face is a valid encoding.
void | InvalidateCompanyWindows (const Company *company)
bool | CheckCompanyHasMoney (CommandCost &cost)
static void | SubtractMoneyFromAnyCompany (Company *c, CommandCost cost)
void | SubtractMoneyFromCompany (CommandCost cost)
void | SubtractMoneyFromCompanyFract (CompanyID company, CommandCost cst)
void | GetNameOfOwner (Owner owner, TileIndex tile)
| Set the right DParams to get the name of an owner.
bool | CheckOwnership (Owner owner, TileIndex tile)
| Check whether the current owner owns something.
bool | CheckTileOwnership (TileIndex tile)
| Check whether the current owner owns the stuff on the given tile.
static void | GenerateCompanyName (Company *c)
static Colours | GenerateCompanyColour ()
static void | GeneratePresidentName (Company *c)
void | ResetCompanyLivery (Company *c)
| Reset the livery schemes to the company's primary colour.
Company * | DoStartupNewCompany (bool is_ai, CompanyID company=INVALID_COMPANY)
| Create a new company and sets all company variables default values.
void | StartupCompanies ()
static void | MaybeStartNewCompany ()
void | InitializeCompanies ()
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 ()
void | CompaniesYearlyLoop ()
CommandCost | CmdSetAutoReplace (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Change engine renewal parameters.
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)
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)
CommandCost | CmdRenamePresident (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Change the name of the president.
## Variables
CompanyByte | _local_company
CompanyByte | _current_company
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")
static const byte | _colour_sort [COLOUR_END] = {2, 2, 3, 2, 3, 2, 3, 2, 3, 2, 2, 2, 3, 1, 1, 1}
static const Colours | _similar_colour [COLOUR_END][2]

## Detailed Description

Handling of companies.

Definition in file company_cmd.cpp.

## Function Documentation

bool 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:

  | owner | the owner of the thing to check.
  | tile | optional tile to get the right town.

Precondition:
  if tile == 0 then the owner can't be OWNER_TOWN.

Returns:
  true iff it's owned by the current company.

Definition at line 254 of file company_cmd.cpp.

References GetNameOfOwner(), OWNER_END, and OWNER_TOWN.

Referenced by CanRemoveRoadWithStop(), CheckAllowRemoveRoad(), CmdAutofillTimetable(), CmdAutoreplaceVehicle(), CmdBuildRoad(), CmdBuildRoadStop(), CmdChangeServiceInt(), CmdChangeTimetable(), CmdCloneOrder(), CmdCloneVehicle(), CmdDeleteOrder(), CmdForceTrainProceed(), CmdInsertOrder(), CmdModifyOrder(), CmdMoveOrder(), CmdMoveRailVehicle(), CmdOrderRefit(), CmdRefitAircraft(), CmdRefitRailVehicle(), CmdRefitRoadVeh(), CmdRefitShip(), CmdRenameStation(), CmdRenameVehicle(), CmdRenameWaypoint(), CmdRestoreOrderIndex(), CmdReverseTrainDirection(), CmdSellAircraft(), CmdSellRailWagon(), CmdSellRoadVeh(), CmdSellShip(), CmdSetTimetableStart(), CmdSetVehicleOnTime(), CmdSkipToOrder(), CmdStartStopVehicle(), CmdTurnRoadVeh(), IsValidTileForWaypoint(), RemoveAirport(), RemoveDock(), RemoveFromRailBaseStation(), RemoveRailStation(), RemoveRoadStop(), and Vehicle::SendToDepot().

bool 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:
  true iff it's owned by the current company.

Definition at line 272 of file company_cmd.cpp.

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

Referenced by CmdBuildSingleRail(), CmdBuildSingleSignal(), CmdConvertRail(), CmdRemoveSingleRail(), CmdRemoveSingleSignal(), and CmdSellLandArea().

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 p1 = 0 - create a new company, Which company (network) it will be is in p2 p1 = 1 - create a new AI company p1 = 2 - delete a company. Company is identified by p2
  | p2 | various functionality, dictated by p1 p1 = 0 - ClientID of the newly created client p1 = 1 - CompanyID to start AI (INVALID_COMPANY for first available) p1 = 2 - CompanyID of the that is getting deleted
  | text | unused

Returns:
  the cost of this operation or an error

Todo:
  In the case of p1=0, create new company, the clientID of the new client is in parameter p2. This parameter is passed in at function DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND) on the server itself. First of all this is unbelievably ugly; second of all, well, it IS ugly! Someone fix this up :) So where to fix?

- - network_server.c:838 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND)
- - network_client.c:536 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_MAP) from where the map has been received

Definition at line 753 of file company_cmd.cpp.

References _network_server, _networking, _settings_client, AI::BroadcastNewEvent(), NetworkClientInfo::client_id, NetworkClientInfo::client_name, NetworkClientInfo::client_playas, CMD_ERROR, CMD_RENAME_PRESIDENT, CompanyNewsInformation::company_name, COMPANY_SPECTATOR, DC_EXEC, NetworkSettings::default_company_pass, DeleteCompanyWindows(), DeleteWindowById(), DESTTYPE_BROADCAST, DoStartupNewCompany(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_company_pool >::GetIfValid(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, INVALID_COMPANY, INVALID_OWNER, InvalidateWindowData(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID(), MarkWholeScreenDirty(), MAX_COMPANIES, NetworkCompanyState::months_empty, ClientSettings::network, NetworkChangeCompanyPassword(), NetworkFindClientInfoFromClientID(), NetworkSend_Command(), NS_COMPANY_BANKRUPT, OWNER_NONE, NetworkCompanyState::password, SetDParam(), SetDParamStr(), SetLocalCompany(), AI::Stop(), StrEmpty(), and SyncCompanySettings().

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

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

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

References CMD_ERROR, CMD_RENAME_COMPANY, DC_EXEC, DoCommand(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_company_pool >::Get(), lengthof, MarkWholeScreenDirty(), MAX_LENGTH_PRESIDENT_NAME_BYTES, return_cmd_error, and StrEmpty().

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

Change engine renewal parameters.

Parameters:

  | tile | unused
  | flags | operation to perform
  | p1 | packed data bits 16-31 = engine group
  | p2 | packed data bits 0-15 = old engine type bits 16-31 = new engine type
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 679 of file company_cmd.cpp.

References AddEngineReplacementForCompany(), CheckAutoreplaceValidity(), CMD_ERROR, DC_EXEC, GB(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_company_pool >::GetIfValid(), InvalidateAutoreplaceWindow(), IsAllGroupID(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_engine_pool >::IsValidID(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_group_pool >::IsValidID(), and RemoveEngineReplacementForCompany().

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

References _company_colours, CMD_ERROR, Livery::colour1, Livery::colour2, DC_EXEC, GB(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_company_pool >::Get(), Livery::in_use, Vehicle::InvalidateNewGRFCache(), InvalidateWindowData(), and MarkWholeScreenDirty().

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

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

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

Create a new company and sets all company variables default values.

Parameters:

  | is_ai | is a ai company?
  | company | CompanyID to use for the new company

Returns:
  the company struct

Definition at line 464 of file company_cmd.cpp.

References _company_colours, _cur_year, _network_server, _networking, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_company_pool >::CanAllocateItem(), GetCompanyRailtypes(), GetCompanyRoadtypes(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_engine_pool >::GetPoolSize(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, INVALID_COMPANY, INVALID_OWNER, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID(), Company::num_engines, RandomCompanyManagerFaceBits(), ResetCompanyLivery(), SetDefaultCompanySettings(), SetWindowDirty(), and AI::StartNew().

Referenced by CmdCompanyCtrl().

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

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

Referenced by CheckOwnership(), and CheckTileOwnership().

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

References Company::bankrupt_asked, ClampToI32(), DAY_TICKS, HasBit(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, MAX_COMPANIES, MAX_UVALUE, MayCompanyTakeOver(), AI::NewEvent(), CompanyEconomyEntry::performance_history, and SetBit().

bool IsValidCompanyManagerFace | ( | CompanyManagerFace | cmf | ) |

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

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

Referenced by CmdSetCompanyManagerFace().

bool MayCompanyTakeOver | ( | CompanyID | cbig,
|  | CompanyID | csmall |
| ) |  |  |

May company cbig buy company csmall?

Parameters:

  | cbig | Company buying csmall.
  | csmall | Company getting bought.

Returns:
  Return `true` if it is allowed.

Definition at line 548 of file company_cmd.cpp.

References _settings_game, CountCompanyVehicles(), VehicleSettings::max_aircraft, VehicleSettings::max_roadveh, VehicleSettings::max_ships, VehicleSettings::max_trains, VEH_AIRCRAFT, VEH_ROAD, VEH_SHIP, VEH_TRAIN, and GameSettings::vehicle.

Referenced by CmdBuyCompany(), CmdBuyShareInCompany(), and HandleBankruptcyTakeover().

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

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

Referenced by DoStartupNewCompany().

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_company | the new company

Precondition:
  Company::IsValidID(new_company) || new_company == COMPANY_SPECTATOR || new_company == OWNER_NONE

Definition at line 92 of file company_cmd.cpp.

References COMPANY_SPECTATOR, DeleteConstructionWindows(), DESTTYPE_TEAM, InvalidateWindowData(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID(), MarkWholeScreenDirty(), and OWNER_NONE.

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

## Variable Documentation

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    },
    }

Definition at line 349 of file company_cmd.cpp.