2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/** @file company_cmd.cpp Handling of companies. */
|
2008-05-06 15:11:33 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2008-04-29 21:31:29 +00:00
|
|
|
#include "engine_base.h"
|
2008-09-30 20:51:04 +00:00
|
|
|
#include "company_func.h"
|
|
|
|
#include "company_gui.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "town.h"
|
2008-03-28 08:53:36 +00:00
|
|
|
#include "news_func.h"
|
2007-12-21 21:50:46 +00:00
|
|
|
#include "command_func.h"
|
2007-01-02 17:34:03 +00:00
|
|
|
#include "network/network.h"
|
2008-05-30 18:20:26 +00:00
|
|
|
#include "network/network_func.h"
|
2008-12-23 11:06:52 +00:00
|
|
|
#include "network/network_base.h"
|
2009-01-12 17:11:45 +00:00
|
|
|
#include "ai/ai.hpp"
|
2008-09-30 20:51:04 +00:00
|
|
|
#include "company_manager_face.h"
|
2007-05-19 09:40:18 +00:00
|
|
|
#include "group.h"
|
2007-12-19 19:44:29 +00:00
|
|
|
#include "window_func.h"
|
2007-12-19 23:26:02 +00:00
|
|
|
#include "tile_map.h"
|
2007-12-21 19:49:27 +00:00
|
|
|
#include "strings_func.h"
|
2007-12-23 10:56:02 +00:00
|
|
|
#include "gfx_func.h"
|
2007-12-26 13:50:40 +00:00
|
|
|
#include "date_func.h"
|
2007-12-29 09:24:26 +00:00
|
|
|
#include "sound_func.h"
|
2008-01-07 09:19:53 +00:00
|
|
|
#include "autoreplace_func.h"
|
|
|
|
#include "autoreplace_gui.h"
|
2008-01-07 14:23:25 +00:00
|
|
|
#include "string_func.h"
|
2008-01-09 21:05:03 +00:00
|
|
|
#include "road_func.h"
|
|
|
|
#include "rail.h"
|
2008-03-31 00:06:17 +00:00
|
|
|
#include "sprite.h"
|
2009-05-22 15:13:50 +00:00
|
|
|
#include "core/pool_func.hpp"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
CompanyByte _local_company;
|
|
|
|
CompanyByte _current_company;
|
|
|
|
/* NOSAVE: can be determined from company structs */
|
2009-02-09 01:06:23 +00:00
|
|
|
Colours _company_colours[MAX_COMPANIES];
|
2008-09-30 20:39:50 +00:00
|
|
|
CompanyManagerFace _company_manager_face; ///< for company manager face storage in openttd.cfg
|
2009-02-25 21:45:14 +00:00
|
|
|
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
|
2008-01-12 14:10:35 +00:00
|
|
|
|
2009-05-22 15:13:50 +00:00
|
|
|
CompanyPool _company_pool("Company");
|
|
|
|
INSTANTIATE_POOL_METHODS(Company)
|
2008-07-18 16:40:29 +00:00
|
|
|
|
2009-01-12 21:39:45 +00:00
|
|
|
Company::Company(uint16 name_1, bool is_ai) :
|
|
|
|
name_1(name_1),
|
|
|
|
location_of_HQ(INVALID_TILE),
|
2009-01-24 15:31:48 +00:00
|
|
|
is_ai(is_ai)
|
2008-07-18 16:40:29 +00:00
|
|
|
{
|
2008-09-30 20:39:50 +00:00
|
|
|
for (uint j = 0; j < 4; j++) this->share_owners[j] = COMPANY_SPECTATOR;
|
2008-07-18 16:40:29 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
Company::~Company()
|
2008-07-18 16:40:29 +00:00
|
|
|
{
|
|
|
|
free(this->name);
|
|
|
|
free(this->president_name);
|
|
|
|
free(this->num_engines);
|
|
|
|
|
|
|
|
if (CleaningPool()) return;
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
DeleteCompanyWindows(this->index);
|
2008-07-18 16:40:29 +00:00
|
|
|
}
|
|
|
|
|
2006-12-26 12:56:48 +00:00
|
|
|
/**
|
2009-02-08 12:25:13 +00:00
|
|
|
* Sets the local company and updates the settings that are set on a
|
2008-09-30 20:39:50 +00:00
|
|
|
* per-company basis to reflect the core's state in the GUI.
|
|
|
|
* @param new_company the new company
|
2009-05-17 01:00:56 +00:00
|
|
|
* @pre Company::IsValidID(new_company) || new_company == COMPANY_SPECTATOR || new_company == OWNER_NONE
|
2006-12-26 12:56:48 +00:00
|
|
|
*/
|
2008-09-30 20:39:50 +00:00
|
|
|
void SetLocalCompany(CompanyID new_company)
|
2006-12-26 12:56:48 +00:00
|
|
|
{
|
2008-09-30 20:39:50 +00:00
|
|
|
/* company could also be COMPANY_SPECTATOR or OWNER_NONE */
|
2009-05-17 01:00:56 +00:00
|
|
|
assert(Company::IsValidID(new_company) || new_company == COMPANY_SPECTATOR || new_company == OWNER_NONE);
|
2006-12-26 12:56:48 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
_local_company = new_company;
|
2006-12-26 12:56:48 +00:00
|
|
|
|
2009-02-08 12:25:13 +00:00
|
|
|
/* Do not update the settings if we are in the intro GUI */
|
2009-05-18 16:21:28 +00:00
|
|
|
const Company *c = Company::GetIfValid(new_company);
|
|
|
|
if (_game_mode != GM_MENU && c != NULL) {
|
2009-05-15 23:55:06 +00:00
|
|
|
_settings_client.company = c->settings;
|
2006-12-26 12:56:48 +00:00
|
|
|
InvalidateWindow(WC_GAME_OPTIONS, 0);
|
|
|
|
}
|
2009-02-04 16:59:41 +00:00
|
|
|
|
|
|
|
/* Delete any construction windows... */
|
|
|
|
DeleteConstructionWindows();
|
|
|
|
|
|
|
|
/* ... and redraw the whole screen. */
|
|
|
|
MarkWholeScreenDirty();
|
2006-12-26 12:56:48 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
bool IsHumanCompany(CompanyID company)
|
2008-01-12 14:10:35 +00:00
|
|
|
{
|
2009-05-16 23:34:14 +00:00
|
|
|
return !Company::Get(company)->is_ai;
|
2008-01-12 14:10:35 +00:00
|
|
|
}
|
|
|
|
|
2006-08-28 06:21:48 +00:00
|
|
|
|
2009-02-09 02:57:15 +00:00
|
|
|
uint16 GetDrawStringCompanyColour(CompanyID company)
|
2006-08-28 06:21:48 +00:00
|
|
|
{
|
2009-02-09 02:57:15 +00:00
|
|
|
/* Get the colour for DrawString-subroutines which matches the colour
|
2008-09-30 20:39:50 +00:00
|
|
|
* of the company */
|
2009-05-17 01:00:56 +00:00
|
|
|
if (!Company::IsValidID(company)) return _colour_gradient[COLOUR_WHITE][4] | IS_PALETTE_COLOUR;
|
2009-02-09 02:57:15 +00:00
|
|
|
return (_colour_gradient[_company_colours[company]][4]) | IS_PALETTE_COLOUR;
|
2006-08-28 06:21:48 +00:00
|
|
|
}
|
|
|
|
|
2008-10-07 20:57:41 +00:00
|
|
|
void DrawCompanyIcon(CompanyID c, int x, int y)
|
2007-01-21 11:49:18 +00:00
|
|
|
{
|
2009-02-09 02:57:15 +00:00
|
|
|
DrawSprite(SPR_PLAYER_ICON, COMPANY_SPRITE_COLOUR(c), x, y);
|
2007-01-21 11:49:18 +00:00
|
|
|
}
|
|
|
|
|
2007-03-02 01:17:11 +00:00
|
|
|
/**
|
2008-09-30 20:39:50 +00:00
|
|
|
* Checks whether a company manager's face is a valid encoding.
|
2007-03-02 01:17:11 +00:00
|
|
|
* Unused bits are not enforced to be 0.
|
2008-09-30 20:39:50 +00:00
|
|
|
* @param cmf the fact to check
|
2007-03-02 01:17:11 +00:00
|
|
|
* @return true if and only if the face is valid
|
|
|
|
*/
|
2008-09-30 20:39:50 +00:00
|
|
|
bool IsValidCompanyManagerFace(CompanyManagerFace cmf)
|
2007-03-02 01:17:11 +00:00
|
|
|
{
|
2008-09-30 20:39:50 +00:00
|
|
|
if (!AreCompanyManagerFaceBitsValid(cmf, CMFV_GEN_ETHN, GE_WM)) return false;
|
|
|
|
|
|
|
|
GenderEthnicity ge = (GenderEthnicity)GetCompanyManagerFaceBits(cmf, CMFV_GEN_ETHN, GE_WM);
|
|
|
|
bool has_moustache = !HasBit(ge, GENDER_FEMALE) && GetCompanyManagerFaceBits(cmf, CMFV_HAS_MOUSTACHE, ge) != 0;
|
|
|
|
bool has_tie_earring = !HasBit(ge, GENDER_FEMALE) || GetCompanyManagerFaceBits(cmf, CMFV_HAS_TIE_EARRING, ge) != 0;
|
|
|
|
bool has_glasses = GetCompanyManagerFaceBits(cmf, CMFV_HAS_GLASSES, ge) != 0;
|
|
|
|
|
|
|
|
if (!AreCompanyManagerFaceBitsValid(cmf, CMFV_EYE_COLOUR, ge)) return false;
|
|
|
|
for (CompanyManagerFaceVariable cmfv = CMFV_CHEEKS; cmfv < CMFV_END; cmfv++) {
|
|
|
|
switch (cmfv) {
|
|
|
|
case CMFV_MOUSTACHE: if (!has_moustache) continue; break;
|
2009-03-15 00:32:18 +00:00
|
|
|
case CMFV_LIPS: // FALL THROUGH
|
2008-09-30 20:39:50 +00:00
|
|
|
case CMFV_NOSE: if (has_moustache) continue; break;
|
|
|
|
case CMFV_TIE_EARRING: if (!has_tie_earring) continue; break;
|
|
|
|
case CMFV_GLASSES: if (!has_glasses) continue; break;
|
2007-03-02 01:17:11 +00:00
|
|
|
default: break;
|
|
|
|
}
|
2008-09-30 20:39:50 +00:00
|
|
|
if (!AreCompanyManagerFaceBitsValid(cmf, cmfv, ge)) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2007-02-24 14:53:32 +00:00
|
|
|
|
2007-03-02 01:17:11 +00:00
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
void InvalidateCompanyWindows(const Company *company)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-09-30 20:39:50 +00:00
|
|
|
CompanyID cid = company->index;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
if (cid == _local_company) InvalidateWindow(WC_STATUS_BAR, 0);
|
|
|
|
InvalidateWindow(WC_FINANCES, cid);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
bool CheckCompanyHasMoney(CommandCost cost)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-06-18 19:53:50 +00:00
|
|
|
if (cost.GetCost() > 0) {
|
2009-05-18 16:21:28 +00:00
|
|
|
const Company *c = Company::GetIfValid(_current_company);
|
|
|
|
if (c != NULL && cost.GetCost() > c->money) {
|
2007-06-21 17:25:17 +00:00
|
|
|
SetDParam(0, cost.GetCost());
|
2009-04-21 23:40:56 +00:00
|
|
|
_error_message = STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY;
|
2004-08-09 17:04:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
static void SubtractMoneyFromAnyCompany(Company *c, CommandCost cost)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-01-09 16:55:48 +00:00
|
|
|
if (cost.GetCost() == 0) return;
|
|
|
|
assert(cost.GetExpensesType() != INVALID_EXPENSES);
|
2004-09-11 09:55:19 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
c->money -= cost.GetCost();
|
|
|
|
c->yearly_expenses[0][cost.GetExpensesType()] += cost.GetCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(1 << EXPENSES_TRAIN_INC |
|
2007-06-10 09:37:09 +00:00
|
|
|
1 << EXPENSES_ROADVEH_INC |
|
|
|
|
1 << EXPENSES_AIRCRAFT_INC |
|
2008-01-09 16:55:48 +00:00
|
|
|
1 << EXPENSES_SHIP_INC, cost.GetExpensesType())) {
|
2008-09-30 20:39:50 +00:00
|
|
|
c->cur_economy.income -= cost.GetCost();
|
2007-11-19 21:02:30 +00:00
|
|
|
} else if (HasBit(1 << EXPENSES_TRAIN_RUN |
|
2007-06-10 09:37:09 +00:00
|
|
|
1 << EXPENSES_ROADVEH_RUN |
|
|
|
|
1 << EXPENSES_AIRCRAFT_RUN |
|
|
|
|
1 << EXPENSES_SHIP_RUN |
|
|
|
|
1 << EXPENSES_PROPERTY |
|
2008-01-09 16:55:48 +00:00
|
|
|
1 << EXPENSES_LOAN_INT, cost.GetExpensesType())) {
|
2008-09-30 20:39:50 +00:00
|
|
|
c->cur_economy.expenses -= cost.GetCost();
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2004-09-11 09:55:19 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
InvalidateCompanyWindows(c);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
void SubtractMoneyFromCompany(CommandCost cost)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-18 16:21:28 +00:00
|
|
|
Company *c = Company::GetIfValid(_current_company);
|
|
|
|
if (c != NULL) SubtractMoneyFromAnyCompany(c, cost);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
void SubtractMoneyFromCompanyFract(CompanyID company, CommandCost cst)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-16 23:34:14 +00:00
|
|
|
Company *c = Company::Get(company);
|
2008-09-30 20:39:50 +00:00
|
|
|
byte m = c->money_fraction;
|
2007-06-18 21:44:47 +00:00
|
|
|
Money cost = cst.GetCost();
|
2006-06-27 21:25:53 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
c->money_fraction = m - (byte)cost;
|
2004-08-09 17:04:08 +00:00
|
|
|
cost >>= 8;
|
2008-09-30 20:39:50 +00:00
|
|
|
if (c->money_fraction > m) cost++;
|
|
|
|
if (cost != 0) SubtractMoneyFromAnyCompany(c, CommandCost(cst.GetExpensesType(), cost));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-10-14 22:22:48 +00:00
|
|
|
void GetNameOfOwner(Owner owner, TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(2, owner);
|
2004-09-11 09:55:19 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (owner != OWNER_TOWN) {
|
2009-05-17 01:00:56 +00:00
|
|
|
if (!Company::IsValidID(owner)) {
|
2009-04-21 23:40:56 +00:00
|
|
|
SetDParam(0, STR_COMPANY_SOMEONE);
|
2006-06-27 21:25:53 +00:00
|
|
|
} else {
|
2007-06-25 13:30:38 +00:00
|
|
|
SetDParam(0, STR_COMPANY_NAME);
|
2008-09-30 20:39:50 +00:00
|
|
|
SetDParam(1, owner);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2008-10-14 18:38:51 +00:00
|
|
|
const Town *t = ClosestTownFromTile(tile, UINT_MAX);
|
2006-06-27 21:25:53 +00:00
|
|
|
|
2005-07-15 18:30:13 +00:00
|
|
|
SetDParam(0, STR_TOWN);
|
|
|
|
SetDParam(1, t->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
bool CheckOwnership(Owner owner)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-10-14 22:22:48 +00:00
|
|
|
assert(owner < OWNER_END);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
if (owner == _current_company) return true;
|
2009-04-21 23:40:56 +00:00
|
|
|
_error_message = STR_ERROR_OWNED_BY;
|
2004-08-09 17:04:08 +00:00
|
|
|
GetNameOfOwner(owner, 0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
bool CheckTileOwnership(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-10-14 22:22:48 +00:00
|
|
|
Owner owner = GetTileOwner(tile);
|
2005-06-04 11:56:32 +00:00
|
|
|
|
2006-10-14 22:22:48 +00:00
|
|
|
assert(owner < OWNER_END);
|
2005-06-04 11:56:32 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
if (owner == _current_company) return true;
|
2009-04-21 23:40:56 +00:00
|
|
|
_error_message = STR_ERROR_OWNED_BY;
|
2004-09-11 09:55:19 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/* no need to get the name of the owner unless we're the local company (saves some time) */
|
|
|
|
if (IsLocalCompany()) GetNameOfOwner(owner, tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
static void GenerateCompanyName(Company *c)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile;
|
2004-08-09 17:04:08 +00:00
|
|
|
Town *t;
|
|
|
|
StringID str;
|
2008-09-30 20:39:50 +00:00
|
|
|
Company *cc;
|
2004-08-09 17:04:08 +00:00
|
|
|
uint32 strp;
|
|
|
|
char buffer[100];
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
if (c->name_1 != STR_SV_UNNAMED) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
tile = c->last_build_coordinate;
|
2006-06-27 21:25:53 +00:00
|
|
|
if (tile == 0) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-10-14 18:38:51 +00:00
|
|
|
t = ClosestTownFromTile(tile, UINT_MAX);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-14 20:11:34 +00:00
|
|
|
if (t->name == NULL && IsInsideMM(t->townnametype, SPECSTR_TOWNNAME_START, SPECSTR_TOWNNAME_LAST + 1)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
str = t->townnametype - SPECSTR_TOWNNAME_START + SPECSTR_PLAYERNAME_START;
|
|
|
|
strp = t->townnameparts;
|
|
|
|
|
|
|
|
verify_name:;
|
2008-09-30 20:39:50 +00:00
|
|
|
/* No companies must have this name already */
|
|
|
|
FOR_ALL_COMPANIES(cc) {
|
|
|
|
if (cc->name_1 == str && cc->name_2 == strp) goto bad_town_name;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-10-21 23:31:34 +00:00
|
|
|
GetString(buffer, str, lastof(buffer));
|
2008-08-13 06:22:04 +00:00
|
|
|
if (strlen(buffer) >= MAX_LENGTH_COMPANY_NAME_BYTES) goto bad_town_name;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
set_name:;
|
2008-09-30 20:39:50 +00:00
|
|
|
c->name_1 = str;
|
|
|
|
c->name_2 = strp;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
MarkWholeScreenDirty();
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
if (!IsHumanCompany(c->index)) {
|
2008-07-18 16:26:51 +00:00
|
|
|
CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
|
2008-09-30 20:39:50 +00:00
|
|
|
cni->FillData(c);
|
2009-04-21 23:40:56 +00:00
|
|
|
SetDParam(0, STR_NEWS_COMPANY_LAUNCH_TITLE);
|
|
|
|
SetDParam(1, STR_NEWS_COMPANY_LAUNCH_DESCRIPTION);
|
2008-07-18 16:26:51 +00:00
|
|
|
SetDParamStr(2, cni->company_name);
|
2008-05-13 10:17:04 +00:00
|
|
|
SetDParam(3, t->index);
|
2009-04-21 23:40:56 +00:00
|
|
|
AddNewsItem(STR_NEWS_MESSAGE, NS_COMPANY_NEW, c->last_build_coordinate, 0, cni);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2009-01-12 17:11:45 +00:00
|
|
|
AI::BroadcastNewEvent(new AIEventCompanyNew(c->index), c->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
bad_town_name:;
|
2004-09-11 09:55:19 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
if (c->president_name_1 == SPECSTR_PRESIDENT_NAME) {
|
2004-08-09 17:04:08 +00:00
|
|
|
str = SPECSTR_ANDCO_NAME;
|
2008-09-30 20:39:50 +00:00
|
|
|
strp = c->president_name_2;
|
2004-09-11 09:55:19 +00:00
|
|
|
goto set_name;
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
|
|
|
str = SPECSTR_ANDCO_NAME;
|
|
|
|
strp = Random();
|
|
|
|
goto verify_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-24 11:11:50 +00:00
|
|
|
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] = {
|
|
|
|
{ COLOUR_BLUE, COLOUR_LIGHT_BLUE }, // COLOUR_DARK_BLUE
|
|
|
|
{ COLOUR_GREEN, COLOUR_DARK_GREEN }, // COLOUR_PALE_GREEN
|
|
|
|
{ INVALID_COLOUR, INVALID_COLOUR }, // COLOUR_PINK
|
|
|
|
{ COLOUR_ORANGE, INVALID_COLOUR }, // COLOUR_YELLOW
|
|
|
|
{ INVALID_COLOUR, INVALID_COLOUR }, // COLOUR_RED
|
|
|
|
{ COLOUR_DARK_BLUE, COLOUR_BLUE }, // COLOUR_LIGHT_BLUE
|
|
|
|
{ COLOUR_PALE_GREEN, COLOUR_DARK_GREEN }, // COLOUR_GREEN
|
|
|
|
{ COLOUR_PALE_GREEN, COLOUR_GREEN }, // COLOUR_DARK_GREEN
|
2008-08-23 12:29:41 +00:00
|
|
|
{ COLOUR_DARK_BLUE, COLOUR_LIGHT_BLUE }, // COLOUR_BLUE
|
2008-04-24 11:11:50 +00:00
|
|
|
{ COLOUR_BROWN, COLOUR_ORANGE }, // COLOUR_CREAM
|
|
|
|
{ COLOUR_PURPLE, INVALID_COLOUR }, // COLOUR_MAUVE
|
|
|
|
{ COLOUR_MAUVE, INVALID_COLOUR }, // COLOUR_PURPLE
|
|
|
|
{ COLOUR_YELLOW, COLOUR_CREAM }, // COLOUR_ORANGE
|
|
|
|
{ COLOUR_CREAM, INVALID_COLOUR }, // COLOUR_BROWN
|
|
|
|
{ COLOUR_WHITE, INVALID_COLOUR }, // COLOUR_GREY
|
|
|
|
{ COLOUR_GREY, INVALID_COLOUR }, // COLOUR_WHITE
|
|
|
|
};
|
|
|
|
|
2009-01-27 01:15:29 +00:00
|
|
|
static Colours GenerateCompanyColour()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-04-24 11:11:50 +00:00
|
|
|
Colours colours[COLOUR_END];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* Initialize array */
|
2008-04-24 11:11:50 +00:00
|
|
|
for (uint i = 0; i < COLOUR_END; i++) colours[i] = (Colours)i;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* And randomize it */
|
2008-04-24 11:11:50 +00:00
|
|
|
for (uint i = 0; i < 100; i++) {
|
|
|
|
uint r = Random();
|
|
|
|
Swap(colours[GB(r, 0, 4)], colours[GB(r, 4, 4)]);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* Bubble sort it according to the values in table 1 */
|
2008-04-24 11:11:50 +00:00
|
|
|
for (uint i = 0; i < COLOUR_END; i++) {
|
|
|
|
for (uint j = 1; j < COLOUR_END; j++) {
|
|
|
|
if (_colour_sort[colours[j - 1]] < _colour_sort[colours[j]]) {
|
|
|
|
Swap(colours[j - 1], colours[j]);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2008-04-24 11:11:50 +00:00
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-02-09 02:57:15 +00:00
|
|
|
/* Move the colours that look similar to each company's colour to the side */
|
2008-09-30 20:39:50 +00:00
|
|
|
Company *c;
|
|
|
|
FOR_ALL_COMPANIES(c) {
|
|
|
|
Colours pcolour = (Colours)c->colour;
|
2008-04-24 11:11:50 +00:00
|
|
|
|
|
|
|
for (uint i = 0; i < COLOUR_END; i++) {
|
|
|
|
if (colours[i] == pcolour) {
|
|
|
|
colours[i] = INVALID_COLOUR;
|
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2008-04-24 11:11:50 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-24 11:11:50 +00:00
|
|
|
for (uint j = 0; j < 2; j++) {
|
|
|
|
Colours similar = _similar_colour[pcolour][j];
|
|
|
|
if (similar == INVALID_COLOUR) break;
|
|
|
|
|
|
|
|
for (uint i = 1; i < COLOUR_END; i++) {
|
|
|
|
if (colours[i - 1] == similar) Swap(colours[i - 1], colours[i]);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-09 02:57:15 +00:00
|
|
|
/* Return the first available colour */
|
2008-04-24 11:11:50 +00:00
|
|
|
for (uint i = 0; i < COLOUR_END; i++) {
|
|
|
|
if (colours[i] != INVALID_COLOUR) return colours[i];
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2008-04-24 11:11:50 +00:00
|
|
|
|
|
|
|
NOT_REACHED();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
static void GeneratePresidentName(Company *c)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-02-01 07:36:15 +00:00
|
|
|
for (;;) {
|
2004-08-09 17:04:08 +00:00
|
|
|
restart:;
|
2008-09-30 20:39:50 +00:00
|
|
|
c->president_name_2 = Random();
|
|
|
|
c->president_name_1 = SPECSTR_PRESIDENT_NAME;
|
2004-09-11 09:55:19 +00:00
|
|
|
|
2009-01-26 22:27:11 +00:00
|
|
|
char buffer[MAX_LENGTH_PRESIDENT_NAME_BYTES + 1];
|
2008-09-30 20:39:50 +00:00
|
|
|
SetDParam(0, c->index);
|
2008-09-30 21:18:28 +00:00
|
|
|
GetString(buffer, STR_PRESIDENT_NAME, lastof(buffer));
|
2009-01-26 22:27:11 +00:00
|
|
|
if (strlen(buffer) >= MAX_LENGTH_PRESIDENT_NAME_BYTES) continue;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-26 22:27:11 +00:00
|
|
|
Company *cc;
|
2008-09-30 20:39:50 +00:00
|
|
|
FOR_ALL_COMPANIES(cc) {
|
|
|
|
if (c != cc) {
|
2009-01-26 22:27:11 +00:00
|
|
|
char buffer2[MAX_LENGTH_PRESIDENT_NAME_BYTES + 2];
|
2008-09-30 20:39:50 +00:00
|
|
|
SetDParam(0, cc->index);
|
2008-09-30 21:18:28 +00:00
|
|
|
GetString(buffer2, STR_PRESIDENT_NAME, lastof(buffer2));
|
2009-01-26 22:27:11 +00:00
|
|
|
if (strcmp(buffer2, buffer) == 0) goto restart;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
void ResetCompanyLivery(Company *c)
|
2006-09-15 12:27:00 +00:00
|
|
|
{
|
2007-01-10 18:56:51 +00:00
|
|
|
for (LiveryScheme scheme = LS_BEGIN; scheme < LS_END; scheme++) {
|
2008-09-30 20:39:50 +00:00
|
|
|
c->livery[scheme].in_use = false;
|
|
|
|
c->livery[scheme].colour1 = c->colour;
|
|
|
|
c->livery[scheme].colour2 = c->colour;
|
2006-09-15 12:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-15 19:59:27 +00:00
|
|
|
/**
|
2008-09-30 20:39:50 +00:00
|
|
|
* Create a new company and sets all company variables default values
|
2007-10-15 19:59:27 +00:00
|
|
|
*
|
2008-09-30 20:39:50 +00:00
|
|
|
* @param is_ai is a ai company?
|
|
|
|
* @return the company struct
|
2007-10-15 19:59:27 +00:00
|
|
|
*/
|
2008-09-30 20:39:50 +00:00
|
|
|
Company *DoStartupNewCompany(bool is_ai)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-22 15:13:50 +00:00
|
|
|
if (!Company::CanAllocateItem()) return NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-27 01:15:29 +00:00
|
|
|
/* we have to generate colour before this company is valid */
|
|
|
|
Colours colour = GenerateCompanyColour();
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
Company *c = new Company(STR_SV_UNNAMED, is_ai);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-27 01:15:29 +00:00
|
|
|
c->colour = colour;
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
ResetCompanyLivery(c);
|
2009-02-09 01:06:23 +00:00
|
|
|
_company_colours[c->index] = (Colours)c->colour;
|
2004-09-11 09:55:19 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
c->money = c->current_loan = 100000;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
c->share_owners[0] = c->share_owners[1] = c->share_owners[2] = c->share_owners[3] = INVALID_OWNER;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
c->avail_railtypes = GetCompanyRailtypes(c->index);
|
|
|
|
c->avail_roadtypes = GetCompanyRoadtypes(c->index);
|
|
|
|
c->inaugurated_year = _cur_year;
|
|
|
|
RandomCompanyManagerFaceBits(c->face, (GenderEthnicity)Random(), false); // create a random company manager face
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-16 00:16:43 +00:00
|
|
|
/* Settings for non-ai companies are copied from the client settings later. */
|
|
|
|
if (is_ai) {
|
|
|
|
c->settings.engine_renew_money = 100000;
|
|
|
|
c->settings.engine_renew_months = 6;
|
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
GeneratePresidentName(c);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
InvalidateWindow(WC_GRAPH_LEGEND, 0);
|
|
|
|
InvalidateWindow(WC_TOOLBAR_MENU, 0);
|
2004-12-04 17:54:56 +00:00
|
|
|
InvalidateWindow(WC_CLIENT_LIST, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
if (is_ai && (!_networking || _network_server)) AI::StartNew(c->index);
|
2005-09-07 15:10:11 +00:00
|
|
|
|
2009-05-16 23:44:36 +00:00
|
|
|
c->num_engines = CallocT<uint16>(Engine::GetPoolSize());
|
2006-09-08 10:47:39 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
return c;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
void StartupCompanies()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-01-13 16:53:03 +00:00
|
|
|
_next_competitor_start = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
static void MaybeStartNewCompany()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-01-31 17:56:06 +00:00
|
|
|
#ifdef ENABLE_NETWORK
|
|
|
|
if (_networking && ActiveCompanyCount() >= _settings_client.network.max_companies) return;
|
|
|
|
#endif /* ENABLE_NETWORK */
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
Company *c;
|
2004-09-11 09:55:19 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* count number of competitors */
|
2009-01-31 17:56:06 +00:00
|
|
|
uint n = 0;
|
2008-09-30 20:39:50 +00:00
|
|
|
FOR_ALL_COMPANIES(c) {
|
|
|
|
if (c->is_ai) n++;
|
2005-09-07 15:10:11 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-13 16:53:03 +00:00
|
|
|
if (n < (uint)_settings_game.difficulty.max_no_competitors) {
|
2006-06-10 08:37:41 +00:00
|
|
|
/* Send a command to all clients to start up a new AI.
|
|
|
|
* Works fine for Multiplayer and Singleplayer */
|
2008-12-28 14:37:19 +00:00
|
|
|
DoCommandP(0, 1, 0, CMD_COMPANY_CTRL);
|
2006-06-10 08:37:41 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
void InitializeCompanies()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-22 15:13:50 +00:00
|
|
|
_company_pool.CleanPool();
|
2008-09-30 20:39:50 +00:00
|
|
|
_cur_company_tick_index = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
void OnTick_Companies()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-06-10 08:37:41 +00:00
|
|
|
if (_game_mode == GM_EDITOR) return;
|
2004-09-11 09:55:19 +00:00
|
|
|
|
2009-05-18 16:21:28 +00:00
|
|
|
Company *c = Company::GetIfValid(_cur_company_tick_index);
|
|
|
|
if (c != NULL && c->name_1 != 0) {
|
|
|
|
GenerateCompanyName(c);
|
2009-01-13 16:53:03 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-13 16:53:03 +00:00
|
|
|
if (_next_competitor_start == 0) {
|
2009-01-13 18:04:12 +00:00
|
|
|
_next_competitor_start = AI::GetStartNextTime() * DAY_TICKS;
|
2009-01-13 16:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (AI::CanStartNew() && _game_mode != GM_MENU && --_next_competitor_start == 0) {
|
|
|
|
MaybeStartNewCompany();
|
2008-07-18 16:40:29 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
_cur_company_tick_index = (_cur_company_tick_index + 1) % MAX_COMPANIES;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
void CompaniesYearlyLoop()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-09-30 20:39:50 +00:00
|
|
|
Company *c;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* Copy statistics */
|
2008-09-30 20:39:50 +00:00
|
|
|
FOR_ALL_COMPANIES(c) {
|
|
|
|
memmove(&c->yearly_expenses[1], &c->yearly_expenses[0], sizeof(c->yearly_expenses) - sizeof(c->yearly_expenses[0]));
|
|
|
|
memset(&c->yearly_expenses[0], 0, sizeof(c->yearly_expenses[0]));
|
|
|
|
InvalidateWindow(WC_FINANCES, c->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
if (_settings_client.gui.show_finances && _local_company != COMPANY_SPECTATOR) {
|
|
|
|
ShowCompanyFinances(_local_company);
|
2009-05-16 23:34:14 +00:00
|
|
|
c = Company::Get(_local_company);
|
2008-09-30 20:39:50 +00:00
|
|
|
if (c->num_valid_stat_ent > 5 && c->old_economy[0].performance_history < c->old_economy[4].performance_history) {
|
2004-12-04 09:26:39 +00:00
|
|
|
SndPlayFx(SND_01_BAD_YEAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2004-12-04 09:26:39 +00:00
|
|
|
SndPlayFx(SND_00_GOOD_YEAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-06 16:07:22 +00:00
|
|
|
/** Change engine renewal parameters
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-17 21:09:38 +00:00
|
|
|
* @param flags operation to perform
|
2005-08-06 16:07:22 +00:00
|
|
|
* @param p1 bits 0-3 command
|
|
|
|
* - p1 = 0 - change auto renew bool
|
|
|
|
* - p1 = 1 - change auto renew months
|
|
|
|
* - p1 = 2 - change auto renew money
|
|
|
|
* - p1 = 3 - change auto renew array
|
|
|
|
* - p1 = 4 - change bool, months & money all together
|
2005-11-07 23:20:47 +00:00
|
|
|
* - p1 = 5 - change renew_keep_length
|
2005-08-06 16:07:22 +00:00
|
|
|
* @param p2 value to set
|
|
|
|
* if p1 = 0, then:
|
|
|
|
* - p2 = enable engine renewal
|
|
|
|
* if p1 = 1, then:
|
|
|
|
* - p2 = months left before engine expires to replace it
|
|
|
|
* if p1 = 2, then
|
|
|
|
* - p2 = minimum amount of money available
|
|
|
|
* if p1 = 3, then:
|
2008-06-23 08:49:09 +00:00
|
|
|
* - p1 bits 16-31 = engine group
|
2005-08-06 16:07:22 +00:00
|
|
|
* - p2 bits 0-15 = old engine type
|
|
|
|
* - p2 bits 16-31 = new engine type
|
|
|
|
* if p1 = 4, then:
|
|
|
|
* - p1 bit 15 = enable engine renewal
|
|
|
|
* - p1 bits 16-31 = months left before engine expires to replace it
|
|
|
|
* - p2 bits 0-31 = minimum amount of money available
|
2005-11-07 23:20:47 +00:00
|
|
|
* if p1 = 5, then
|
|
|
|
* - p2 = enable renew_keep_length
|
2005-08-06 16:07:22 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdSetAutoReplace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2005-08-06 16:07:22 +00:00
|
|
|
{
|
2009-05-18 16:21:28 +00:00
|
|
|
Company *c = Company::GetIfValid(_current_company);
|
|
|
|
if (c == NULL) return CMD_ERROR;
|
2005-08-06 16:07:22 +00:00
|
|
|
|
|
|
|
switch (GB(p1, 0, 3)) {
|
|
|
|
case 0:
|
2009-05-15 23:55:06 +00:00
|
|
|
if (c->settings.engine_renew == HasBit(p2, 0)) return CMD_ERROR;
|
2005-08-06 16:07:22 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2009-05-15 23:55:06 +00:00
|
|
|
c->settings.engine_renew = HasBit(p2, 0);
|
2008-09-30 20:39:50 +00:00
|
|
|
if (IsLocalCompany()) {
|
2009-05-15 23:55:06 +00:00
|
|
|
_settings_client.company.engine_renew = c->settings.engine_renew;
|
2005-08-06 16:07:22 +00:00
|
|
|
InvalidateWindow(WC_GAME_OPTIONS, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2008-04-20 21:49:57 +00:00
|
|
|
|
2005-08-06 16:07:22 +00:00
|
|
|
case 1:
|
2008-04-20 21:36:41 +00:00
|
|
|
if (Clamp((int16)p2, -12, 12) != (int16)p2) return CMD_ERROR;
|
2009-05-15 23:55:06 +00:00
|
|
|
if (c->settings.engine_renew_months == (int16)p2) return CMD_ERROR;
|
2005-08-06 16:07:22 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2009-05-15 23:55:06 +00:00
|
|
|
c->settings.engine_renew_months = (int16)p2;
|
2008-09-30 20:39:50 +00:00
|
|
|
if (IsLocalCompany()) {
|
2009-05-15 23:55:06 +00:00
|
|
|
_settings_client.company.engine_renew_months = c->settings.engine_renew_months;
|
2005-08-06 16:07:22 +00:00
|
|
|
InvalidateWindow(WC_GAME_OPTIONS, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2008-04-20 21:49:57 +00:00
|
|
|
|
2005-08-06 16:07:22 +00:00
|
|
|
case 2:
|
2008-04-20 21:36:41 +00:00
|
|
|
if (ClampU(p2, 0, 2000000) != p2) return CMD_ERROR;
|
2009-05-15 23:55:06 +00:00
|
|
|
if (c->settings.engine_renew_money == p2) return CMD_ERROR;
|
2005-08-06 16:07:22 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2009-05-15 23:55:06 +00:00
|
|
|
c->settings.engine_renew_money = p2;
|
2008-09-30 20:39:50 +00:00
|
|
|
if (IsLocalCompany()) {
|
2009-05-15 23:55:06 +00:00
|
|
|
_settings_client.company.engine_renew_money = c->settings.engine_renew_money;
|
2005-08-06 16:07:22 +00:00
|
|
|
InvalidateWindow(WC_GAME_OPTIONS, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2008-04-20 21:49:57 +00:00
|
|
|
|
2005-08-06 16:07:22 +00:00
|
|
|
case 3: {
|
|
|
|
EngineID old_engine_type = GB(p2, 0, 16);
|
|
|
|
EngineID new_engine_type = GB(p2, 16, 16);
|
2007-05-19 12:46:48 +00:00
|
|
|
GroupID id_g = GB(p1, 16, 16);
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost cost;
|
2005-08-06 16:07:22 +00:00
|
|
|
|
2009-05-17 01:00:56 +00:00
|
|
|
if (!Group::IsValidID(id_g) && !IsAllGroupID(id_g) && !IsDefaultGroupID(id_g)) return CMD_ERROR;
|
2005-08-06 16:07:22 +00:00
|
|
|
if (new_engine_type != INVALID_ENGINE) {
|
2008-09-30 20:39:50 +00:00
|
|
|
if (!CheckAutoreplaceValidity(old_engine_type, new_engine_type, _current_company)) return CMD_ERROR;
|
2005-08-06 16:07:22 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
cost = AddEngineReplacementForCompany(c, old_engine_type, new_engine_type, id_g, flags);
|
2005-11-26 16:41:14 +00:00
|
|
|
} else {
|
2008-09-30 20:39:50 +00:00
|
|
|
cost = RemoveEngineReplacementForCompany(c, old_engine_type, id_g, flags);
|
2005-08-06 16:07:22 +00:00
|
|
|
}
|
2006-01-02 19:55:09 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
if (IsLocalCompany()) InvalidateAutoreplaceWindow(old_engine_type, id_g);
|
2006-01-02 19:55:09 +00:00
|
|
|
|
|
|
|
return cost;
|
2005-12-14 06:49:54 +00:00
|
|
|
}
|
|
|
|
|
2005-08-06 16:07:22 +00:00
|
|
|
case 4:
|
2008-04-20 21:36:41 +00:00
|
|
|
if (Clamp((int16)GB(p1, 16, 16), -12, 12) != (int16)GB(p1, 16, 16)) return CMD_ERROR;
|
|
|
|
if (ClampU(p2, 0, 2000000) != p2) return CMD_ERROR;
|
2008-04-20 21:49:57 +00:00
|
|
|
|
2005-08-06 16:07:22 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2009-05-15 23:55:06 +00:00
|
|
|
c->settings.engine_renew = HasBit(p1, 15);
|
|
|
|
c->settings.engine_renew_months = (int16)GB(p1, 16, 16);
|
|
|
|
c->settings.engine_renew_money = p2;
|
2008-09-30 20:39:50 +00:00
|
|
|
|
|
|
|
if (IsLocalCompany()) {
|
2009-05-15 23:55:06 +00:00
|
|
|
_settings_client.company.engine_renew = c->settings.engine_renew;
|
|
|
|
_settings_client.company.engine_renew_months = c->settings.engine_renew_months;
|
|
|
|
_settings_client.company.engine_renew_money = c->settings.engine_renew_money;
|
2005-08-06 16:07:22 +00:00
|
|
|
InvalidateWindow(WC_GAME_OPTIONS, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2008-04-20 21:49:57 +00:00
|
|
|
|
2005-11-07 23:20:47 +00:00
|
|
|
case 5:
|
2009-05-15 23:55:06 +00:00
|
|
|
if (c->settings.renew_keep_length == HasBit(p2, 0)) return CMD_ERROR;
|
2005-08-06 16:07:22 +00:00
|
|
|
|
2005-11-07 23:20:47 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2009-05-15 23:55:06 +00:00
|
|
|
c->settings.renew_keep_length = HasBit(p2, 0);
|
2008-09-30 20:39:50 +00:00
|
|
|
if (IsLocalCompany()) {
|
2007-03-08 16:27:54 +00:00
|
|
|
InvalidateWindow(WC_REPLACE_VEHICLE, VEH_TRAIN);
|
2005-11-07 23:20:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2008-04-20 21:49:57 +00:00
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2005-08-06 16:07:22 +00:00
|
|
|
}
|
|
|
|
|
2008-07-18 16:26:51 +00:00
|
|
|
/**
|
|
|
|
* Fill the CompanyNewsInformation struct with the required data.
|
|
|
|
* @param p the current company.
|
|
|
|
* @param other the other company.
|
|
|
|
*/
|
2008-09-30 20:39:50 +00:00
|
|
|
void CompanyNewsInformation::FillData(const Company *c, const Company *other)
|
2008-07-18 16:26:51 +00:00
|
|
|
{
|
2008-09-30 20:39:50 +00:00
|
|
|
SetDParam(0, c->index);
|
2008-07-18 16:26:51 +00:00
|
|
|
GetString(this->company_name, STR_COMPANY_NAME, lastof(this->company_name));
|
|
|
|
|
|
|
|
if (other == NULL) {
|
|
|
|
*this->other_company_name = '\0';
|
|
|
|
} else {
|
|
|
|
SetDParam(0, other->index);
|
|
|
|
GetString(this->other_company_name, STR_COMPANY_NAME, lastof(this->other_company_name));
|
2008-09-30 20:39:50 +00:00
|
|
|
c = other;
|
2008-07-18 16:26:51 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
SetDParam(0, c->index);
|
2009-04-21 23:40:56 +00:00
|
|
|
GetString(this->president_name, STR_PRESIDENT_NAME_MANAGER, lastof(this->president_name));
|
2008-07-18 16:26:51 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
this->colour = c->colour;
|
|
|
|
this->face = c->face;
|
2008-07-18 16:26:51 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/** Control the companies: add, delete, etc.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-17 21:09:38 +00:00
|
|
|
* @param flags operation to perform
|
2005-05-12 23:46:01 +00:00
|
|
|
* @param p1 various functionality
|
2008-09-30 20:39:50 +00:00
|
|
|
* - 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
|
|
|
|
* - p1 = 3 - merge two companies together. merge #1 with #2. Identified by p2
|
2005-05-12 23:46:01 +00:00
|
|
|
* @param p2 various functionality, dictated by p1
|
2008-09-30 20:39:50 +00:00
|
|
|
* - p1 = 0 - ClientID of the newly created client
|
|
|
|
* - p1 = 2 - CompanyID of the that is getting deleted
|
|
|
|
* - p1 = 3 - #1 p2 = (bit 0-15) - company to merge (p2 & 0xFFFF)
|
|
|
|
* - #2 p2 = (bit 16-31) - company to be merged into ((p2>>16)&0xFFFF)
|
|
|
|
* @todo In the case of p1=0, create new company, the clientID of the new client is in parameter
|
2005-05-12 23:46:01 +00:00
|
|
|
* 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! <b>Someone fix this up :)</b> So where to fix?@n
|
|
|
|
* @arg - network_server.c:838 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND)@n
|
|
|
|
* @arg - network_client.c:536 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_MAP) from where the map has been received
|
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdCompanyCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-09-30 20:39:50 +00:00
|
|
|
if (flags & DC_EXEC) _current_company = OWNER_NONE;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-06-15 15:48:33 +00:00
|
|
|
InvalidateWindowData(WC_COMPANY_LEAGUE, 0, 0);
|
|
|
|
|
2005-05-12 23:46:01 +00:00
|
|
|
switch (p1) {
|
2009-03-15 00:32:18 +00:00
|
|
|
case 0: { // Create a new company
|
2008-09-30 20:39:50 +00:00
|
|
|
/* This command is only executed in a multiplayer game */
|
|
|
|
if (!_networking) return CMD_ERROR;
|
2008-07-17 15:16:11 +00:00
|
|
|
|
|
|
|
#ifdef ENABLE_NETWORK
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Joining Client:
|
2009-03-14 18:16:29 +00:00
|
|
|
* _local_company: COMPANY_SPECTATOR
|
2009-05-19 21:20:14 +00:00
|
|
|
* cid = clientid
|
2009-03-14 18:16:29 +00:00
|
|
|
*
|
|
|
|
* Other client(s)/server:
|
2009-05-19 21:20:14 +00:00
|
|
|
* _local_company: what they play as
|
2009-03-14 18:16:29 +00:00
|
|
|
* cid = requested company/company of joining client */
|
2008-12-24 00:03:47 +00:00
|
|
|
ClientID cid = (ClientID)p2;
|
2008-09-30 20:39:50 +00:00
|
|
|
|
2008-12-22 12:59:31 +00:00
|
|
|
/* Has the network client a correct ClientIndex? */
|
2008-09-30 20:39:50 +00:00
|
|
|
if (!(flags & DC_EXEC)) return CommandCost();
|
2008-12-24 00:03:47 +00:00
|
|
|
NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(cid);
|
2008-12-23 12:26:19 +00:00
|
|
|
if (ci == NULL) return CommandCost();
|
2008-09-30 20:39:50 +00:00
|
|
|
|
|
|
|
/* Delete multiplayer progress bar */
|
|
|
|
DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
|
|
|
|
|
|
|
|
Company *c = DoStartupNewCompany(false);
|
|
|
|
|
|
|
|
/* A new company could not be created, revert to being a spectator */
|
|
|
|
if (c == NULL) {
|
|
|
|
if (_network_server) {
|
|
|
|
ci->client_playas = COMPANY_SPECTATOR;
|
2008-12-22 12:59:31 +00:00
|
|
|
NetworkUpdateClientInfo(ci->client_id);
|
2008-09-30 20:39:50 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-01-23 22:18:06 +00:00
|
|
|
/* This is the client (or non-dedicated server) who wants a new company */
|
|
|
|
if (cid == _network_own_client_id) {
|
2009-05-13 12:07:36 +00:00
|
|
|
/* Create p1 and p2 here because SetLocalCompany resets the gui.autorenew* settings. */
|
2009-05-15 23:55:06 +00:00
|
|
|
uint32 p1 = (_settings_client.company.engine_renew << 15 ) | (_settings_client.company.engine_renew_months << 16) | 4;
|
|
|
|
uint32 p2 = _settings_client.company.engine_renew_money;
|
2008-09-30 20:39:50 +00:00
|
|
|
assert(_local_company == COMPANY_SPECTATOR);
|
|
|
|
SetLocalCompany(c->index);
|
|
|
|
if (!StrEmpty(_settings_client.network.default_company_pass)) {
|
|
|
|
char *password = _settings_client.network.default_company_pass;
|
|
|
|
NetworkChangeCompanyPassword(1, &password);
|
|
|
|
}
|
2006-11-10 17:52:51 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
_current_company = _local_company;
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Now that we have a new company, broadcast our autorenew settings to
|
2009-03-14 18:16:29 +00:00
|
|
|
* all clients so everything is in sync */
|
2009-05-13 12:07:36 +00:00
|
|
|
NetworkSend_Command(0, p1, p2, CMD_SET_AUTOREPLACE, NULL, NULL);
|
2006-11-10 17:52:51 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
MarkWholeScreenDirty();
|
|
|
|
}
|
2004-12-15 21:13:52 +00:00
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
if (_network_server) {
|
2008-09-30 20:39:50 +00:00
|
|
|
/* XXX - UGLY! p2 (pid) is mis-used to fetch the client-id, done at
|
2009-03-14 18:16:29 +00:00
|
|
|
* server side in network_server.c:838, function
|
|
|
|
* DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND) */
|
2009-01-23 22:18:06 +00:00
|
|
|
CompanyID old_playas = ci->client_playas;
|
2008-09-30 20:39:50 +00:00
|
|
|
ci->client_playas = c->index;
|
2008-12-22 12:59:31 +00:00
|
|
|
NetworkUpdateClientInfo(ci->client_id);
|
2008-07-17 16:38:26 +00:00
|
|
|
|
2009-05-17 01:00:56 +00:00
|
|
|
if (Company::IsValidID(ci->client_playas)) {
|
2008-09-30 20:39:50 +00:00
|
|
|
CompanyID company_backup = _local_company;
|
2008-12-22 18:40:57 +00:00
|
|
|
_network_company_states[c->index].months_empty = 0;
|
2009-01-23 15:53:19 +00:00
|
|
|
_network_company_states[c->index].password[0] = '\0';
|
2009-01-23 22:18:06 +00:00
|
|
|
NetworkServerUpdateCompanyPassworded(ci->client_playas, false);
|
2008-09-30 20:39:50 +00:00
|
|
|
|
|
|
|
/* XXX - When a client joins, we automatically set its name to the
|
2009-03-14 18:16:29 +00:00
|
|
|
* client's name (for some reason). As it stands now only the server
|
|
|
|
* knows the client's name, so it needs to send out a "broadcast" to
|
|
|
|
* do this. To achieve this we send a network command. However, it
|
|
|
|
* uses _local_company to execute the command as. To prevent abuse
|
|
|
|
* (eg. only yourself can change your name/company), we 'cheat' by
|
|
|
|
* impersonation _local_company as the server. Not the best solution;
|
|
|
|
* but it works.
|
|
|
|
* TODO: Perhaps this could be improved by when the client is ready
|
|
|
|
* with joining to let it send itself the command, and not the server?
|
|
|
|
* For example in network_client.c:534? */
|
2008-09-30 20:39:50 +00:00
|
|
|
_local_company = ci->client_playas;
|
2008-12-28 14:37:19 +00:00
|
|
|
NetworkSend_Command(0, 0, 0, CMD_RENAME_PRESIDENT, NULL, ci->client_name);
|
2008-09-30 20:39:50 +00:00
|
|
|
_local_company = company_backup;
|
|
|
|
}
|
2009-01-23 22:18:06 +00:00
|
|
|
|
|
|
|
/* Announce new company on network, if the client was a SPECTATOR before */
|
|
|
|
if (old_playas == COMPANY_SPECTATOR) {
|
|
|
|
NetworkServerSendChat(NETWORK_ACTION_COMPANY_NEW, DESTTYPE_BROADCAST, 0, "", ci->client_id, ci->client_playas + 1);
|
|
|
|
}
|
2006-11-10 17:52:51 +00:00
|
|
|
}
|
|
|
|
#endif /* ENABLE_NETWORK */
|
2008-09-30 20:39:50 +00:00
|
|
|
} break;
|
2005-05-12 23:46:01 +00:00
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
case 1: // Make a new AI company
|
2008-09-30 20:39:50 +00:00
|
|
|
if (!(flags & DC_EXEC)) return CommandCost();
|
2005-05-12 23:46:01 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
DoStartupNewCompany(true);
|
|
|
|
break;
|
2005-05-12 23:46:01 +00:00
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
case 2: { // Delete a company
|
2009-05-18 16:21:28 +00:00
|
|
|
Company *c = Company::GetIfValid(p2);
|
|
|
|
if (c == NULL) return CMD_ERROR;
|
2005-05-12 23:46:01 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
if (!(flags & DC_EXEC)) return CommandCost();
|
2005-05-12 23:46:01 +00:00
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/* Delete any open window of the company */
|
|
|
|
DeleteCompanyWindows(c->index);
|
|
|
|
CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
|
|
|
|
cni->FillData(c);
|
2008-07-18 16:26:51 +00:00
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/* Show the bankrupt news */
|
2009-04-21 23:40:56 +00:00
|
|
|
SetDParam(0, STR_NEWS_COMPANY_BANKRUPT_TITLE);
|
|
|
|
SetDParam(1, STR_NEWS_COMPANY_BANKRUPT_DESCRIPTION);
|
2009-01-12 17:11:45 +00:00
|
|
|
SetDParamStr(2, cni->company_name);
|
2009-04-21 23:40:56 +00:00
|
|
|
AddNewsItem(STR_NEWS_MESSAGE, NS_COMPANY_BANKRUPT, 0, 0, cni);
|
2004-12-16 11:36:57 +00:00
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/* Remove the company */
|
|
|
|
ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER);
|
|
|
|
if (!IsHumanCompany(c->index)) AI::Stop(c->index);
|
2008-07-18 16:40:29 +00:00
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
CompanyID c_index = c->index;
|
|
|
|
delete c;
|
|
|
|
AI::BroadcastNewEvent(new AIEventCompanyBankrupt(c_index));
|
2008-09-30 20:39:50 +00:00
|
|
|
} break;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
case 3: { // Merge a company (#1) into another company (#2), elimination company #1
|
2008-09-30 20:39:50 +00:00
|
|
|
CompanyID cid_old = (CompanyID)GB(p2, 0, 16);
|
|
|
|
CompanyID cid_new = (CompanyID)GB(p2, 16, 16);
|
2005-05-12 23:46:01 +00:00
|
|
|
|
2009-05-17 01:00:56 +00:00
|
|
|
if (!Company::IsValidID(cid_old) || !Company::IsValidID(cid_new)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
if (!(flags & DC_EXEC)) return CMD_ERROR;
|
2005-05-12 23:46:01 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
ChangeOwnershipOfCompanyItems(cid_old, cid_new);
|
2009-05-16 23:34:14 +00:00
|
|
|
delete Company::Get(cid_old);
|
2008-09-30 20:39:50 +00:00
|
|
|
} break;
|
2006-11-10 17:52:51 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
default: return CMD_ERROR;
|
2005-05-12 23:46:01 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|