00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "engine_base.h"
00014 #include "company_func.h"
00015 #include "company_gui.h"
00016 #include "town.h"
00017 #include "news_func.h"
00018 #include "cmd_helper.h"
00019 #include "command_func.h"
00020 #include "network/network.h"
00021 #include "network/network_func.h"
00022 #include "network/network_base.h"
00023 #include "ai/ai.hpp"
00024 #include "company_manager_face.h"
00025 #include "group.h"
00026 #include "window_func.h"
00027 #include "strings_func.h"
00028 #include "gfx_func.h"
00029 #include "date_func.h"
00030 #include "sound_func.h"
00031 #include "autoreplace_func.h"
00032 #include "autoreplace_gui.h"
00033 #include "rail.h"
00034 #include "core/pool_func.hpp"
00035 #include "settings_func.h"
00036 #include "vehicle_base.h"
00037 #include "vehicle_func.h"
00038 #include "sprite.h"
00039
00040 #include "table/strings.h"
00041
00042 CompanyByte _local_company;
00043 CompanyByte _current_company;
00044
00045 Colours _company_colours[MAX_COMPANIES];
00046 CompanyManagerFace _company_manager_face;
00047 uint _next_competitor_start;
00048 uint _cur_company_tick_index;
00049
00050 CompanyPool _company_pool("Company");
00051 INSTANTIATE_POOL_METHODS(Company)
00052
00053 Company::Company(uint16 name_1, bool is_ai) :
00054 name_1(name_1),
00055 location_of_HQ(INVALID_TILE),
00056 is_ai(is_ai)
00057 {
00058 for (uint j = 0; j < 4; j++) this->share_owners[j] = COMPANY_SPECTATOR;
00059 InvalidateWindowData(WC_PERFORMANCE_DETAIL, 0, INVALID_COMPANY);
00060 }
00061
00062 Company::~Company()
00063 {
00064 free(this->name);
00065 free(this->president_name);
00066 free(this->num_engines);
00067
00068 if (CleaningPool()) return;
00069
00070 DeleteCompanyWindows(this->index);
00071 }
00072
00077 void Company::PostDestructor(size_t index)
00078 {
00079 InvalidateWindowData(WC_GRAPH_LEGEND, 0, (int)index);
00080 InvalidateWindowData(WC_PERFORMANCE_DETAIL, 0, (int)index);
00081 InvalidateWindowData(WC_COMPANY_LEAGUE, 0, 0);
00082
00083 InvalidateWindowData(WC_ERRMSG, 0);
00084 }
00085
00092 void SetLocalCompany(CompanyID new_company)
00093 {
00094
00095 assert(Company::IsValidID(new_company) || new_company == COMPANY_SPECTATOR || new_company == OWNER_NONE);
00096
00097 #ifdef ENABLE_NETWORK
00098
00099 InvalidateWindowData(WC_SEND_NETWORK_MSG, DESTTYPE_TEAM, _local_company);
00100 #endif
00101
00102 _local_company = new_company;
00103
00104
00105 DeleteConstructionWindows();
00106
00107
00108 MarkWholeScreenDirty();
00109 }
00110
00111 uint16 GetDrawStringCompanyColour(CompanyID company)
00112 {
00113
00114
00115 if (!Company::IsValidID(company)) return _colour_gradient[COLOUR_WHITE][4] | IS_PALETTE_COLOUR;
00116 return (_colour_gradient[_company_colours[company]][4]) | IS_PALETTE_COLOUR;
00117 }
00118
00119 void DrawCompanyIcon(CompanyID c, int x, int y)
00120 {
00121 DrawSprite(SPR_COMPANY_ICON, COMPANY_SPRITE_COLOUR(c), x, y);
00122 }
00123
00130 bool IsValidCompanyManagerFace(CompanyManagerFace cmf)
00131 {
00132 if (!AreCompanyManagerFaceBitsValid(cmf, CMFV_GEN_ETHN, GE_WM)) return false;
00133
00134 GenderEthnicity ge = (GenderEthnicity)GetCompanyManagerFaceBits(cmf, CMFV_GEN_ETHN, GE_WM);
00135 bool has_moustache = !HasBit(ge, GENDER_FEMALE) && GetCompanyManagerFaceBits(cmf, CMFV_HAS_MOUSTACHE, ge) != 0;
00136 bool has_tie_earring = !HasBit(ge, GENDER_FEMALE) || GetCompanyManagerFaceBits(cmf, CMFV_HAS_TIE_EARRING, ge) != 0;
00137 bool has_glasses = GetCompanyManagerFaceBits(cmf, CMFV_HAS_GLASSES, ge) != 0;
00138
00139 if (!AreCompanyManagerFaceBitsValid(cmf, CMFV_EYE_COLOUR, ge)) return false;
00140 for (CompanyManagerFaceVariable cmfv = CMFV_CHEEKS; cmfv < CMFV_END; cmfv++) {
00141 switch (cmfv) {
00142 case CMFV_MOUSTACHE: if (!has_moustache) continue; break;
00143 case CMFV_LIPS:
00144 case CMFV_NOSE: if (has_moustache) continue; break;
00145 case CMFV_TIE_EARRING: if (!has_tie_earring) continue; break;
00146 case CMFV_GLASSES: if (!has_glasses) continue; break;
00147 default: break;
00148 }
00149 if (!AreCompanyManagerFaceBitsValid(cmf, cmfv, ge)) return false;
00150 }
00151
00152 return true;
00153 }
00154
00155 void InvalidateCompanyWindows(const Company *company)
00156 {
00157 CompanyID cid = company->index;
00158
00159 if (cid == _local_company) SetWindowDirty(WC_STATUS_BAR, 0);
00160 SetWindowDirty(WC_FINANCES, cid);
00161 }
00162
00163 bool CheckCompanyHasMoney(CommandCost &cost)
00164 {
00165 if (cost.GetCost() > 0) {
00166 const Company *c = Company::GetIfValid(_current_company);
00167 if (c != NULL && cost.GetCost() > c->money) {
00168 SetDParam(0, cost.GetCost());
00169 cost.MakeError(STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY);
00170 return false;
00171 }
00172 }
00173 return true;
00174 }
00175
00176 static void SubtractMoneyFromAnyCompany(Company *c, CommandCost cost)
00177 {
00178 if (cost.GetCost() == 0) return;
00179 assert(cost.GetExpensesType() != INVALID_EXPENSES);
00180
00181 c->money -= cost.GetCost();
00182 c->yearly_expenses[0][cost.GetExpensesType()] += cost.GetCost();
00183
00184 if (HasBit(1 << EXPENSES_TRAIN_INC |
00185 1 << EXPENSES_ROADVEH_INC |
00186 1 << EXPENSES_AIRCRAFT_INC |
00187 1 << EXPENSES_SHIP_INC, cost.GetExpensesType())) {
00188 c->cur_economy.income -= cost.GetCost();
00189 } else if (HasBit(1 << EXPENSES_TRAIN_RUN |
00190 1 << EXPENSES_ROADVEH_RUN |
00191 1 << EXPENSES_AIRCRAFT_RUN |
00192 1 << EXPENSES_SHIP_RUN |
00193 1 << EXPENSES_PROPERTY |
00194 1 << EXPENSES_LOAN_INT, cost.GetExpensesType())) {
00195 c->cur_economy.expenses -= cost.GetCost();
00196 }
00197
00198 InvalidateCompanyWindows(c);
00199 }
00200
00201 void SubtractMoneyFromCompany(CommandCost cost)
00202 {
00203 Company *c = Company::GetIfValid(_current_company);
00204 if (c != NULL) SubtractMoneyFromAnyCompany(c, cost);
00205 }
00206
00207 void SubtractMoneyFromCompanyFract(CompanyID company, CommandCost cst)
00208 {
00209 Company *c = Company::Get(company);
00210 byte m = c->money_fraction;
00211 Money cost = cst.GetCost();
00212
00213 c->money_fraction = m - (byte)cost;
00214 cost >>= 8;
00215 if (c->money_fraction > m) cost++;
00216 if (cost != 0) SubtractMoneyFromAnyCompany(c, CommandCost(cst.GetExpensesType(), cost));
00217 }
00218
00225 void GetNameOfOwner(Owner owner, TileIndex tile)
00226 {
00227 SetDParam(2, owner);
00228
00229 if (owner != OWNER_TOWN) {
00230 if (!Company::IsValidID(owner)) {
00231 SetDParam(0, STR_COMPANY_SOMEONE);
00232 } else {
00233 SetDParam(0, STR_COMPANY_NAME);
00234 SetDParam(1, owner);
00235 }
00236 } else {
00237 assert(tile != 0);
00238 const Town *t = ClosestTownFromTile(tile, UINT_MAX);
00239
00240 SetDParam(0, STR_TOWN_NAME);
00241 SetDParam(1, t->index);
00242 }
00243 }
00244
00245
00254 bool CheckOwnership(Owner owner, TileIndex tile)
00255 {
00256 assert(owner < OWNER_END);
00257 assert(owner != OWNER_TOWN || tile != 0);
00258
00259 if (owner == _current_company) return true;
00260 _error_message = STR_ERROR_OWNED_BY;
00261 GetNameOfOwner(owner, tile);
00262 return false;
00263 }
00264
00272 bool CheckTileOwnership(TileIndex tile)
00273 {
00274 Owner owner = GetTileOwner(tile);
00275
00276 assert(owner < OWNER_END);
00277
00278 if (owner == _current_company) return true;
00279 _error_message = STR_ERROR_OWNED_BY;
00280
00281
00282 if (IsLocalCompany()) GetNameOfOwner(owner, tile);
00283 return false;
00284 }
00285
00286 static void GenerateCompanyName(Company *c)
00287 {
00288 TileIndex tile;
00289 Town *t;
00290 StringID str;
00291 Company *cc;
00292 uint32 strp;
00293
00294
00295 char buffer[MAX_LENGTH_COMPANY_NAME_BYTES + MAX_CHAR_LENGTH];
00296
00297 if (c->name_1 != STR_SV_UNNAMED) return;
00298
00299 tile = c->last_build_coordinate;
00300 if (tile == 0) return;
00301
00302 t = ClosestTownFromTile(tile, UINT_MAX);
00303
00304 if (t->name == NULL && IsInsideMM(t->townnametype, SPECSTR_TOWNNAME_START, SPECSTR_TOWNNAME_LAST + 1)) {
00305 str = t->townnametype - SPECSTR_TOWNNAME_START + SPECSTR_PLAYERNAME_START;
00306 strp = t->townnameparts;
00307
00308 verify_name:;
00309
00310 FOR_ALL_COMPANIES(cc) {
00311 if (cc->name_1 == str && cc->name_2 == strp) goto bad_town_name;
00312 }
00313
00314 GetString(buffer, str, lastof(buffer));
00315 if (strlen(buffer) >= MAX_LENGTH_COMPANY_NAME_BYTES) goto bad_town_name;
00316
00317 set_name:;
00318 c->name_1 = str;
00319 c->name_2 = strp;
00320
00321 MarkWholeScreenDirty();
00322
00323 if (c->is_ai) {
00324 CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
00325 cni->FillData(c);
00326 SetDParam(0, STR_NEWS_COMPANY_LAUNCH_TITLE);
00327 SetDParam(1, STR_NEWS_COMPANY_LAUNCH_DESCRIPTION);
00328 SetDParamStr(2, cni->company_name);
00329 SetDParam(3, t->index);
00330 AddNewsItem(STR_MESSAGE_NEWS_FORMAT, NS_COMPANY_NEW, NR_TILE, c->last_build_coordinate, NR_NONE, UINT32_MAX, cni);
00331 }
00332 AI::BroadcastNewEvent(new AIEventCompanyNew(c->index), c->index);
00333 return;
00334 }
00335 bad_town_name:;
00336
00337 if (c->president_name_1 == SPECSTR_PRESIDENT_NAME) {
00338 str = SPECSTR_ANDCO_NAME;
00339 strp = c->president_name_2;
00340 goto set_name;
00341 } else {
00342 str = SPECSTR_ANDCO_NAME;
00343 strp = Random();
00344 goto verify_name;
00345 }
00346 }
00347
00348 static const byte _colour_sort[COLOUR_END] = {2, 2, 3, 2, 3, 2, 3, 2, 3, 2, 2, 2, 3, 1, 1, 1};
00349 static const Colours _similar_colour[COLOUR_END][2] = {
00350 { COLOUR_BLUE, COLOUR_LIGHT_BLUE },
00351 { COLOUR_GREEN, COLOUR_DARK_GREEN },
00352 { INVALID_COLOUR, INVALID_COLOUR },
00353 { COLOUR_ORANGE, INVALID_COLOUR },
00354 { INVALID_COLOUR, INVALID_COLOUR },
00355 { COLOUR_DARK_BLUE, COLOUR_BLUE },
00356 { COLOUR_PALE_GREEN, COLOUR_DARK_GREEN },
00357 { COLOUR_PALE_GREEN, COLOUR_GREEN },
00358 { COLOUR_DARK_BLUE, COLOUR_LIGHT_BLUE },
00359 { COLOUR_BROWN, COLOUR_ORANGE },
00360 { COLOUR_PURPLE, INVALID_COLOUR },
00361 { COLOUR_MAUVE, INVALID_COLOUR },
00362 { COLOUR_YELLOW, COLOUR_CREAM },
00363 { COLOUR_CREAM, INVALID_COLOUR },
00364 { COLOUR_WHITE, INVALID_COLOUR },
00365 { COLOUR_GREY, INVALID_COLOUR },
00366 };
00367
00368 static Colours GenerateCompanyColour()
00369 {
00370 Colours colours[COLOUR_END];
00371
00372
00373 for (uint i = 0; i < COLOUR_END; i++) colours[i] = (Colours)i;
00374
00375
00376 for (uint i = 0; i < 100; i++) {
00377 uint r = Random();
00378 Swap(colours[GB(r, 0, 4)], colours[GB(r, 4, 4)]);
00379 }
00380
00381
00382 for (uint i = 0; i < COLOUR_END; i++) {
00383 for (uint j = 1; j < COLOUR_END; j++) {
00384 if (_colour_sort[colours[j - 1]] < _colour_sort[colours[j]]) {
00385 Swap(colours[j - 1], colours[j]);
00386 }
00387 }
00388 };
00389
00390
00391 Company *c;
00392 FOR_ALL_COMPANIES(c) {
00393 Colours pcolour = (Colours)c->colour;
00394
00395 for (uint i = 0; i < COLOUR_END; i++) {
00396 if (colours[i] == pcolour) {
00397 colours[i] = INVALID_COLOUR;
00398 break;
00399 }
00400 }
00401
00402 for (uint j = 0; j < 2; j++) {
00403 Colours similar = _similar_colour[pcolour][j];
00404 if (similar == INVALID_COLOUR) break;
00405
00406 for (uint i = 1; i < COLOUR_END; i++) {
00407 if (colours[i - 1] == similar) Swap(colours[i - 1], colours[i]);
00408 }
00409 }
00410 }
00411
00412
00413 for (uint i = 0; i < COLOUR_END; i++) {
00414 if (colours[i] != INVALID_COLOUR) return colours[i];
00415 }
00416
00417 NOT_REACHED();
00418 }
00419
00420 static void GeneratePresidentName(Company *c)
00421 {
00422 for (;;) {
00423 restart:;
00424 c->president_name_2 = Random();
00425 c->president_name_1 = SPECSTR_PRESIDENT_NAME;
00426
00427
00428
00429 char buffer[MAX_LENGTH_PRESIDENT_NAME_BYTES + MAX_CHAR_LENGTH];
00430 SetDParam(0, c->index);
00431 GetString(buffer, STR_PRESIDENT_NAME, lastof(buffer));
00432 if (strlen(buffer) >= MAX_LENGTH_PRESIDENT_NAME_BYTES) continue;
00433
00434 Company *cc;
00435 FOR_ALL_COMPANIES(cc) {
00436 if (c != cc) {
00437
00438 char buffer2[MAX_LENGTH_PRESIDENT_NAME_BYTES + MAX_CHAR_LENGTH];
00439 SetDParam(0, cc->index);
00440 GetString(buffer2, STR_PRESIDENT_NAME, lastof(buffer2));
00441 if (strcmp(buffer2, buffer) == 0) goto restart;
00442 }
00443 }
00444 return;
00445 }
00446 }
00447
00448 void ResetCompanyLivery(Company *c)
00449 {
00450 for (LiveryScheme scheme = LS_BEGIN; scheme < LS_END; scheme++) {
00451 c->livery[scheme].in_use = false;
00452 c->livery[scheme].colour1 = c->colour;
00453 c->livery[scheme].colour2 = c->colour;
00454 }
00455 }
00456
00464 Company *DoStartupNewCompany(bool is_ai, CompanyID company = INVALID_COMPANY)
00465 {
00466 if (!Company::CanAllocateItem()) return NULL;
00467
00468
00469 Colours colour = GenerateCompanyColour();
00470
00471 Company *c;
00472 if (company == INVALID_COMPANY) {
00473 c = new Company(STR_SV_UNNAMED, is_ai);
00474 } else {
00475 if (Company::IsValidID(company)) return NULL;
00476 c = new (company) Company(STR_SV_UNNAMED, is_ai);
00477 }
00478
00479 c->colour = colour;
00480
00481 ResetCompanyLivery(c);
00482 _company_colours[c->index] = (Colours)c->colour;
00483
00484 c->money = c->current_loan = 100000;
00485
00486 c->share_owners[0] = c->share_owners[1] = c->share_owners[2] = c->share_owners[3] = INVALID_OWNER;
00487
00488 c->avail_railtypes = GetCompanyRailtypes(c->index);
00489 c->avail_roadtypes = GetCompanyRoadtypes(c->index);
00490 c->inaugurated_year = _cur_year;
00491 RandomCompanyManagerFaceBits(c->face, (GenderEthnicity)Random(), false);
00492
00493 SetDefaultCompanySettings(c->index);
00494
00495 GeneratePresidentName(c);
00496
00497 SetWindowDirty(WC_GRAPH_LEGEND, 0);
00498 SetWindowDirty(WC_TOOLBAR_MENU, 0);
00499 SetWindowDirty(WC_CLIENT_LIST, 0);
00500
00501 if (is_ai && (!_networking || _network_server)) AI::StartNew(c->index);
00502
00503 c->num_engines = CallocT<uint16>(Engine::GetPoolSize());
00504
00505 return c;
00506 }
00507
00508 void StartupCompanies()
00509 {
00510 _next_competitor_start = 0;
00511 }
00512
00513 #ifdef ENABLE_AI
00514 static void MaybeStartNewCompany()
00515 {
00516 #ifdef ENABLE_NETWORK
00517 if (_networking && Company::GetNumItems() >= _settings_client.network.max_companies) return;
00518 #endif
00519
00520 Company *c;
00521
00522
00523 uint n = 0;
00524 FOR_ALL_COMPANIES(c) {
00525 if (c->is_ai) n++;
00526 }
00527
00528 if (n < (uint)_settings_game.difficulty.max_no_competitors) {
00529
00530
00531 DoCommandP(0, 1, INVALID_COMPANY, CMD_COMPANY_CTRL);
00532 }
00533 }
00534 #endif
00535
00536 void InitializeCompanies()
00537 {
00538 _company_pool.CleanPool();
00539 _cur_company_tick_index = 0;
00540 }
00541
00548 bool MayCompanyTakeOver(CompanyID cbig, CompanyID csmall)
00549 {
00550 uint big_counts[4], small_counts[4];
00551 CountCompanyVehicles(cbig, big_counts);
00552 CountCompanyVehicles(csmall, small_counts);
00553
00554
00555 return big_counts[VEH_TRAIN] + small_counts[VEH_TRAIN] <= _settings_game.vehicle.max_trains &&
00556 big_counts[VEH_ROAD] + small_counts[VEH_ROAD] <= _settings_game.vehicle.max_roadveh &&
00557 big_counts[VEH_SHIP] + small_counts[VEH_SHIP] <= _settings_game.vehicle.max_ships &&
00558 big_counts[VEH_AIRCRAFT] + small_counts[VEH_AIRCRAFT] <= _settings_game.vehicle.max_aircraft;
00559 }
00560
00570 static void HandleBankruptcyTakeover(Company *c)
00571 {
00572
00573
00574
00575
00576
00577 static const int TAKE_OVER_TIMEOUT = 3 * 30 * DAY_TICKS / (MAX_COMPANIES - 1);
00578
00579 assert(c->bankrupt_asked != 0);
00580
00581
00582 if (c->bankrupt_timeout != 0) {
00583 c->bankrupt_timeout -= MAX_COMPANIES;
00584 if (c->bankrupt_timeout > 0) return;
00585 c->bankrupt_timeout = 0;
00586
00587 return;
00588 }
00589
00590
00591 if (c->bankrupt_asked == MAX_UVALUE(CompanyMask)) return;
00592
00593 Company *c2, *best = NULL;
00594 int32 best_performance = -1;
00595
00596
00597 FOR_ALL_COMPANIES(c2) {
00598 if (c2->bankrupt_asked == 0 &&
00599 !HasBit(c->bankrupt_asked, c2->index) &&
00600 best_performance < c2->old_economy[1].performance_history &&
00601 MayCompanyTakeOver(c2->index, c->index)) {
00602 best_performance = c2->old_economy[1].performance_history;
00603 best = c2;
00604 }
00605 }
00606
00607
00608 if (best_performance == -1) {
00609 c->bankrupt_asked = MAX_UVALUE(CompanyMask);
00610 return;
00611 }
00612
00613 SetBit(c->bankrupt_asked, best->index);
00614
00615 c->bankrupt_timeout = TAKE_OVER_TIMEOUT;
00616 if (best->is_ai) {
00617 AI::NewEvent(best->index, new AIEventCompanyAskMerger(c->index, ClampToI32(c->bankrupt_value)));
00618 } else if (IsInteractiveCompany(best->index)) {
00619 ShowBuyCompanyDialog(c->index);
00620 }
00621 }
00622
00623 void OnTick_Companies()
00624 {
00625 if (_game_mode == GM_EDITOR) return;
00626
00627 Company *c = Company::GetIfValid(_cur_company_tick_index);
00628 if (c != NULL) {
00629 if (c->name_1 != 0) GenerateCompanyName(c);
00630 if (c->bankrupt_asked != 0) HandleBankruptcyTakeover(c);
00631 }
00632
00633 #ifdef ENABLE_AI
00634 if (_next_competitor_start == 0) {
00635 _next_competitor_start = AI::GetStartNextTime() * DAY_TICKS;
00636 }
00637
00638 if (AI::CanStartNew() && _game_mode != GM_MENU && --_next_competitor_start == 0) {
00639 MaybeStartNewCompany();
00640 }
00641 #endif
00642
00643 _cur_company_tick_index = (_cur_company_tick_index + 1) % MAX_COMPANIES;
00644 }
00645
00646 void CompaniesYearlyLoop()
00647 {
00648 Company *c;
00649
00650
00651 FOR_ALL_COMPANIES(c) {
00652 memmove(&c->yearly_expenses[1], &c->yearly_expenses[0], sizeof(c->yearly_expenses) - sizeof(c->yearly_expenses[0]));
00653 memset(&c->yearly_expenses[0], 0, sizeof(c->yearly_expenses[0]));
00654 SetWindowDirty(WC_FINANCES, c->index);
00655 }
00656
00657 if (_settings_client.gui.show_finances && _local_company != COMPANY_SPECTATOR) {
00658 ShowCompanyFinances(_local_company);
00659 c = Company::Get(_local_company);
00660 if (c->num_valid_stat_ent > 5 && c->old_economy[0].performance_history < c->old_economy[4].performance_history) {
00661 SndPlayFx(SND_01_BAD_YEAR);
00662 } else {
00663 SndPlayFx(SND_00_GOOD_YEAR);
00664 }
00665 }
00666 }
00667
00679 CommandCost CmdSetAutoReplace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00680 {
00681 Company *c = Company::GetIfValid(_current_company);
00682 if (c == NULL) return CMD_ERROR;
00683
00684 EngineID old_engine_type = GB(p2, 0, 16);
00685 EngineID new_engine_type = GB(p2, 16, 16);
00686 GroupID id_g = GB(p1, 16, 16);
00687 CommandCost cost;
00688
00689 if (!Group::IsValidID(id_g) && !IsAllGroupID(id_g) && !IsDefaultGroupID(id_g)) return CMD_ERROR;
00690 if (!Engine::IsValidID(old_engine_type)) return CMD_ERROR;
00691
00692 if (new_engine_type != INVALID_ENGINE) {
00693 if (!Engine::IsValidID(new_engine_type)) return CMD_ERROR;
00694 if (!CheckAutoreplaceValidity(old_engine_type, new_engine_type, _current_company)) return CMD_ERROR;
00695
00696 cost = AddEngineReplacementForCompany(c, old_engine_type, new_engine_type, id_g, flags);
00697 } else {
00698 cost = RemoveEngineReplacementForCompany(c, old_engine_type, id_g, flags);
00699 }
00700
00701 if ((flags & DC_EXEC) && IsLocalCompany()) InvalidateAutoreplaceWindow(old_engine_type, id_g);
00702
00703 return cost;
00704 }
00705
00711 void CompanyNewsInformation::FillData(const Company *c, const Company *other)
00712 {
00713 SetDParam(0, c->index);
00714 GetString(this->company_name, STR_COMPANY_NAME, lastof(this->company_name));
00715
00716 if (other == NULL) {
00717 *this->other_company_name = '\0';
00718 } else {
00719 SetDParam(0, other->index);
00720 GetString(this->other_company_name, STR_COMPANY_NAME, lastof(this->other_company_name));
00721 c = other;
00722 }
00723
00724 SetDParam(0, c->index);
00725 GetString(this->president_name, STR_PRESIDENT_NAME_MANAGER, lastof(this->president_name));
00726
00727 this->colour = c->colour;
00728 this->face = c->face;
00729
00730 }
00731
00753 CommandCost CmdCompanyCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00754 {
00755 if (flags & DC_EXEC) _current_company = OWNER_NONE;
00756
00757 InvalidateWindowData(WC_COMPANY_LEAGUE, 0, 0);
00758
00759 switch (p1) {
00760 case 0: {
00761
00762 if (!_networking) return CMD_ERROR;
00763
00764 #ifdef ENABLE_NETWORK
00765
00766
00767
00768
00769
00770
00771
00772
00773 ClientID cid = (ClientID)p2;
00774
00775
00776 if (!(flags & DC_EXEC)) return CommandCost();
00777 NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(cid);
00778 if (ci == NULL) return CommandCost();
00779
00780
00781 DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
00782
00783 Company *c = DoStartupNewCompany(false);
00784
00785
00786 if (c == NULL) {
00787 if (_network_server) {
00788 ci->client_playas = COMPANY_SPECTATOR;
00789 NetworkUpdateClientInfo(ci->client_id);
00790 }
00791 break;
00792 }
00793
00794
00795 if (cid == _network_own_client_id) {
00796 assert(_local_company == COMPANY_SPECTATOR);
00797 SetLocalCompany(c->index);
00798 if (!StrEmpty(_settings_client.network.default_company_pass)) {
00799 NetworkChangeCompanyPassword(_settings_client.network.default_company_pass);
00800 }
00801
00802 _current_company = _local_company;
00803
00804
00805
00806 SyncCompanySettings();
00807
00808 MarkWholeScreenDirty();
00809 }
00810
00811 if (_network_server) {
00812
00813
00814
00815 CompanyID old_playas = ci->client_playas;
00816 ci->client_playas = c->index;
00817 NetworkUpdateClientInfo(ci->client_id);
00818
00819 if (Company::IsValidID(ci->client_playas)) {
00820 _network_company_states[c->index].months_empty = 0;
00821 _network_company_states[c->index].password[0] = '\0';
00822 NetworkServerUpdateCompanyPassworded(ci->client_playas, false);
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835 NetworkSend_Command(0, 0, 0, CMD_RENAME_PRESIDENT, NULL, ci->client_name, ci->client_playas);
00836 }
00837
00838
00839 if (old_playas == COMPANY_SPECTATOR) {
00840 NetworkServerSendChat(NETWORK_ACTION_COMPANY_NEW, DESTTYPE_BROADCAST, 0, "", ci->client_id, ci->client_playas + 1);
00841 }
00842 }
00843 #endif
00844 } break;
00845
00846 case 1:
00847 if (!(flags & DC_EXEC)) return CommandCost();
00848
00849 if (p2 != INVALID_COMPANY && (p2 >= MAX_COMPANIES || Company::IsValidID(p2))) return CMD_ERROR;
00850 DoStartupNewCompany(true, (CompanyID)p2);
00851 break;
00852
00853 case 2: {
00854 Company *c = Company::GetIfValid(p2);
00855 if (c == NULL) return CMD_ERROR;
00856
00857 if (!(flags & DC_EXEC)) return CommandCost();
00858
00859
00860 DeleteCompanyWindows(c->index);
00861 CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
00862 cni->FillData(c);
00863
00864
00865 SetDParam(0, STR_NEWS_COMPANY_BANKRUPT_TITLE);
00866 SetDParam(1, STR_NEWS_COMPANY_BANKRUPT_DESCRIPTION);
00867 SetDParamStr(2, cni->company_name);
00868 AddCompanyNewsItem(STR_MESSAGE_NEWS_FORMAT, NS_COMPANY_BANKRUPT, cni);
00869
00870
00871 ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER);
00872 if (c->is_ai) AI::Stop(c->index);
00873
00874 CompanyID c_index = c->index;
00875 delete c;
00876 AI::BroadcastNewEvent(new AIEventCompanyBankrupt(c_index));
00877 } break;
00878
00879 default: return CMD_ERROR;
00880 }
00881
00882 return CommandCost();
00883 }
00884
00893 CommandCost CmdSetCompanyManagerFace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00894 {
00895 CompanyManagerFace cmf = (CompanyManagerFace)p2;
00896
00897 if (!IsValidCompanyManagerFace(cmf)) return CMD_ERROR;
00898
00899 if (flags & DC_EXEC) {
00900 Company::Get(_current_company)->face = cmf;
00901 MarkWholeScreenDirty();
00902 }
00903 return CommandCost();
00904 }
00905
00916 CommandCost CmdSetCompanyColour(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00917 {
00918 Colours colour = Extract<Colours, 0, 4>(p2);
00919 LiveryScheme scheme = Extract<LiveryScheme, 0, 8>(p1);
00920 byte state = GB(p1, 8, 2);
00921
00922 if (scheme >= LS_END || state >= 3 || colour == INVALID_COLOUR) return CMD_ERROR;
00923
00924 Company *c = Company::Get(_current_company);
00925
00926
00927 if (scheme == LS_DEFAULT && state == 0) {
00928 const Company *cc;
00929 FOR_ALL_COMPANIES(cc) {
00930 if (cc != c && cc->colour == colour) return CMD_ERROR;
00931 }
00932 }
00933
00934 if (flags & DC_EXEC) {
00935 switch (state) {
00936 case 0:
00937 c->livery[scheme].colour1 = colour;
00938
00939
00940
00941 if (scheme == LS_DEFAULT) {
00942 _company_colours[_current_company] = colour;
00943 c->colour = colour;
00944 }
00945 break;
00946
00947 case 1:
00948 c->livery[scheme].colour2 = colour;
00949 break;
00950
00951 case 2:
00952 c->livery[scheme].in_use = colour != 0;
00953
00954
00955
00956
00957
00958
00959
00960
00961 if (colour != 0) {
00962 c->livery[LS_DEFAULT].in_use = true;
00963 break;
00964 }
00965
00966
00967
00968 c->livery[LS_DEFAULT].in_use = false;
00969 for (scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
00970 if (c->livery[scheme].in_use) {
00971 c->livery[LS_DEFAULT].in_use = true;
00972 break;
00973 }
00974 }
00975 break;
00976
00977 default:
00978 break;
00979 }
00980 ResetVehicleColourMap();
00981 MarkWholeScreenDirty();
00982
00983
00984 InvalidateWindowData(WC_INCOME_GRAPH, 0);
00985 InvalidateWindowData(WC_OPERATING_PROFIT, 0);
00986 InvalidateWindowData(WC_DELIVERED_CARGO, 0);
00987 InvalidateWindowData(WC_PERFORMANCE_HISTORY, 0);
00988 InvalidateWindowData(WC_COMPANY_VALUE, 0);
00989
00990
00991 Vehicle *v;
00992 FOR_ALL_VEHICLES(v) {
00993 if (v->owner == _current_company) v->InvalidateNewGRFCache();
00994 }
00995 }
00996 return CommandCost();
00997 }
00998
00999 static bool IsUniqueCompanyName(const char *name)
01000 {
01001 const Company *c;
01002
01003 FOR_ALL_COMPANIES(c) {
01004 if (c->name != NULL && strcmp(c->name, name) == 0) return false;
01005 }
01006
01007 return true;
01008 }
01009
01018 CommandCost CmdRenameCompany(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01019 {
01020 bool reset = StrEmpty(text);
01021
01022 if (!reset) {
01023 if (strlen(text) >= MAX_LENGTH_COMPANY_NAME_BYTES) return CMD_ERROR;
01024 if (!IsUniqueCompanyName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
01025 }
01026
01027 if (flags & DC_EXEC) {
01028 Company *c = Company::Get(_current_company);
01029 free(c->name);
01030 c->name = reset ? NULL : strdup(text);
01031 MarkWholeScreenDirty();
01032 }
01033
01034 return CommandCost();
01035 }
01036
01037 static bool IsUniquePresidentName(const char *name)
01038 {
01039 const Company *c;
01040
01041 FOR_ALL_COMPANIES(c) {
01042 if (c->president_name != NULL && strcmp(c->president_name, name) == 0) return false;
01043 }
01044
01045 return true;
01046 }
01047
01056 CommandCost CmdRenamePresident(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01057 {
01058 bool reset = StrEmpty(text);
01059
01060 if (!reset) {
01061 if (strlen(text) >= MAX_LENGTH_PRESIDENT_NAME_BYTES) return CMD_ERROR;
01062 if (!IsUniquePresidentName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
01063 }
01064
01065 if (flags & DC_EXEC) {
01066 Company *c = Company::Get(_current_company);
01067 free(c->president_name);
01068
01069 if (reset) {
01070 c->president_name = NULL;
01071 } else {
01072 c->president_name = strdup(text);
01073
01074 if (c->name_1 == STR_SV_UNNAMED && c->name == NULL) {
01075 char buf[80];
01076
01077 snprintf(buf, lengthof(buf), "%s Transport", text);
01078 DoCommand(0, 0, 0, DC_EXEC, CMD_RENAME_COMPANY, buf);
01079 }
01080 }
01081
01082 MarkWholeScreenDirty();
01083 }
01084
01085 return CommandCost();
01086 }