00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "engine_base.h"
00014 #include "company_base.h"
00015 #include "company_func.h"
00016 #include "company_gui.h"
00017 #include "town.h"
00018 #include "news_func.h"
00019 #include "cmd_helper.h"
00020 #include "command_func.h"
00021 #include "network/network.h"
00022 #include "network/network_func.h"
00023 #include "network/network_base.h"
00024 #include "network/network_admin.h"
00025 #include "ai/ai.hpp"
00026 #include "company_manager_face.h"
00027 #include "window_func.h"
00028 #include "strings_func.h"
00029 #include "date_func.h"
00030 #include "sound_func.h"
00031 #include "rail.h"
00032 #include "core/pool_func.hpp"
00033 #include "settings_func.h"
00034 #include "vehicle_base.h"
00035 #include "vehicle_func.h"
00036 #include "sprite.h"
00037 #include "smallmap_gui.h"
00038
00039 #include "table/strings.h"
00040
00041 CompanyByte _local_company;
00042 CompanyByte _current_company;
00043 Colours _company_colours[MAX_COMPANIES];
00044 CompanyManagerFace _company_manager_face;
00045 uint _next_competitor_start;
00046 uint _cur_company_tick_index;
00047
00048 CompanyPool _company_pool("Company");
00049 INSTANTIATE_POOL_METHODS(Company)
00050
00051
00056 Company::Company(uint16 name_1, bool is_ai)
00057 {
00058 this->name_1 = name_1;
00059 this->location_of_HQ = INVALID_TILE;
00060 this->is_ai = is_ai;
00061 this->terraform_limit = _settings_game.construction.terraform_frame_burst << 16;
00062 this->clear_limit = _settings_game.construction.clear_frame_burst << 16;
00063
00064 for (uint j = 0; j < 4; j++) this->share_owners[j] = COMPANY_SPECTATOR;
00065 InvalidateWindowData(WC_PERFORMANCE_DETAIL, 0, INVALID_COMPANY);
00066 InvalidateWindowClassesData(WC_WATCH_COMPANY, 0);
00067 }
00068
00070 Company::~Company()
00071 {
00072 free(this->num_engines);
00073
00074 if (CleaningPool()) return;
00075
00076 DeleteCompanyWindows(this->index);
00077 }
00078
00083 void Company::PostDestructor(size_t index)
00084 {
00085 InvalidateWindowData(WC_GRAPH_LEGEND, 0, (int)index);
00086 InvalidateWindowData(WC_PERFORMANCE_DETAIL, 0, (int)index);
00087 InvalidateWindowData(WC_COMPANY_LEAGUE, 0, 0);
00088 InvalidateWindowClassesData(WC_WATCH_COMPANY, 0);
00089
00090 InvalidateWindowData(WC_ERRMSG, 0);
00091 }
00092
00099 void SetLocalCompany(CompanyID new_company)
00100 {
00101
00102 assert(Company::IsValidID(new_company) || new_company == COMPANY_SPECTATOR || new_company == OWNER_NONE);
00103
00104 #ifdef ENABLE_NETWORK
00105
00106 InvalidateWindowData(WC_SEND_NETWORK_MSG, DESTTYPE_TEAM, _local_company);
00107 #endif
00108
00109 assert(IsLocalCompany());
00110
00111 _current_company = _local_company = new_company;
00112
00113
00114 DeleteConstructionWindows();
00115
00116
00117 MarkWholeScreenDirty();
00118 }
00119
00125 TextColour GetDrawStringCompanyColour(CompanyID company)
00126 {
00127 if (!Company::IsValidID(company)) return (TextColour)_colour_gradient[COLOUR_WHITE][4] | TC_IS_PALETTE_COLOUR;
00128 return (TextColour)_colour_gradient[_company_colours[company]][4] | TC_IS_PALETTE_COLOUR;
00129 }
00130
00137 void DrawCompanyIcon(CompanyID c, int x, int y)
00138 {
00139 DrawSprite(SPR_COMPANY_ICON, COMPANY_SPRITE_COLOUR(c), x, y);
00140 }
00141
00148
00149 bool IsValidCompanyManagerFace(CompanyManagerFace cmf)
00150 {
00151 if (!AreCompanyManagerFaceBitsValid(cmf, CMFV_GEN_ETHN, GE_WM)) return false;
00152
00153 GenderEthnicity ge = (GenderEthnicity)GetCompanyManagerFaceBits(cmf, CMFV_GEN_ETHN, GE_WM);
00154 bool has_moustache = !HasBit(ge, GENDER_FEMALE) && GetCompanyManagerFaceBits(cmf, CMFV_HAS_MOUSTACHE, ge) != 0;
00155 bool has_tie_earring = !HasBit(ge, GENDER_FEMALE) || GetCompanyManagerFaceBits(cmf, CMFV_HAS_TIE_EARRING, ge) != 0;
00156 bool has_glasses = GetCompanyManagerFaceBits(cmf, CMFV_HAS_GLASSES, ge) != 0;
00157
00158 if (!AreCompanyManagerFaceBitsValid(cmf, CMFV_EYE_COLOUR, ge)) return false;
00159 for (CompanyManagerFaceVariable cmfv = CMFV_CHEEKS; cmfv < CMFV_END; cmfv++) {
00160 switch (cmfv) {
00161 case CMFV_MOUSTACHE: if (!has_moustache) continue; break;
00162 case CMFV_LIPS:
00163 case CMFV_NOSE: if (has_moustache) continue; break;
00164 case CMFV_TIE_EARRING: if (!has_tie_earring) continue; break;
00165 case CMFV_GLASSES: if (!has_glasses) continue; break;
00166 default: break;
00167 }
00168 if (!AreCompanyManagerFaceBitsValid(cmf, cmfv, ge)) return false;
00169 }
00170
00171 return true;
00172 }
00173
00178 void InvalidateCompanyWindows(const Company *company)
00179 {
00180 CompanyID cid = company->index;
00181
00182 if (cid == _local_company) SetWindowDirty(WC_STATUS_BAR, 0);
00183 SetWindowDirty(WC_FINANCES, cid);
00184 }
00185
00191 bool CheckCompanyHasMoney(CommandCost &cost)
00192 {
00193 if (cost.GetCost() > 0) {
00194 const Company *c = Company::GetIfValid(_current_company);
00195 if (c != NULL && cost.GetCost() > c->money) {
00196 SetDParam(0, cost.GetCost());
00197 cost.MakeError(STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY);
00198 return false;
00199 }
00200 }
00201 return true;
00202 }
00203
00209 static void SubtractMoneyFromAnyCompany(Company *c, CommandCost cost)
00210 {
00211 if (cost.GetCost() == 0) return;
00212 assert(cost.GetExpensesType() != INVALID_EXPENSES);
00213
00214 c->money -= cost.GetCost();
00215 c->yearly_expenses[0][cost.GetExpensesType()] += cost.GetCost();
00216
00217 if (HasBit(1 << EXPENSES_TRAIN_INC |
00218 1 << EXPENSES_ROADVEH_INC |
00219 1 << EXPENSES_AIRCRAFT_INC |
00220 1 << EXPENSES_SHIP_INC |
00221 1 << EXPENSES_SHARING_INC, cost.GetExpensesType())) {
00222 c->cur_economy.income -= cost.GetCost();
00223 } else if (HasBit(1 << EXPENSES_TRAIN_RUN |
00224 1 << EXPENSES_ROADVEH_RUN |
00225 1 << EXPENSES_AIRCRAFT_RUN |
00226 1 << EXPENSES_SHIP_RUN |
00227 1 << EXPENSES_PROPERTY |
00228 1 << EXPENSES_LOAN_INT |
00229 1 << EXPENSES_SHARING_COST, cost.GetExpensesType())) {
00230 c->cur_economy.expenses -= cost.GetCost();
00231 }
00232
00233 InvalidateCompanyWindows(c);
00234 }
00235
00240 void SubtractMoneyFromCompany(CommandCost cost)
00241 {
00242 Company *c = Company::GetIfValid(_current_company);
00243 if (c != NULL) SubtractMoneyFromAnyCompany(c, cost);
00244 }
00245
00251 void SubtractMoneyFromCompanyFract(CompanyID company, CommandCost cst)
00252 {
00253 Company *c = Company::Get(company);
00254 byte m = c->money_fraction;
00255 Money cost = cst.GetCost();
00256
00257 c->money_fraction = m - (byte)cost;
00258 cost >>= 8;
00259 if (c->money_fraction > m) cost++;
00260 if (cost != 0) SubtractMoneyFromAnyCompany(c, CommandCost(cst.GetExpensesType(), cost));
00261 }
00262
00264 void UpdateLandscapingLimits()
00265 {
00266 Company *c;
00267 FOR_ALL_COMPANIES(c) {
00268 c->terraform_limit = min(c->terraform_limit + _settings_game.construction.terraform_per_64k_frames, (uint32)_settings_game.construction.terraform_frame_burst << 16);
00269 c->clear_limit = min(c->clear_limit + _settings_game.construction.clear_per_64k_frames, (uint32)_settings_game.construction.clear_frame_burst << 16);
00270 }
00271 }
00272
00279 void GetNameOfOwner(Owner owner, TileIndex tile)
00280 {
00281 SetDParam(2, owner);
00282
00283 if (owner != OWNER_TOWN) {
00284 if (!Company::IsValidID(owner)) {
00285 SetDParam(0, STR_COMPANY_SOMEONE);
00286 } else {
00287 SetDParam(0, STR_COMPANY_NAME);
00288 SetDParam(1, owner);
00289 }
00290 } else {
00291 assert(tile != 0);
00292 const Town *t = ClosestTownFromTile(tile, UINT_MAX);
00293
00294 SetDParam(0, STR_TOWN_NAME);
00295 SetDParam(1, t->index);
00296 }
00297 }
00298
00299
00308 CommandCost CheckOwnership(Owner owner, TileIndex tile)
00309 {
00310 assert(owner < OWNER_END);
00311 assert(owner != OWNER_TOWN || tile != 0);
00312
00313 if (owner == _current_company) return CommandCost();
00314
00315 GetNameOfOwner(owner, tile);
00316 return_cmd_error(STR_ERROR_OWNED_BY);
00317 }
00318
00326 CommandCost CheckTileOwnership(TileIndex tile)
00327 {
00328 Owner owner = GetTileOwner(tile);
00329
00330 assert(owner < OWNER_END);
00331
00332 if (owner == _current_company) return CommandCost();
00333
00334
00335 if (IsLocalCompany()) GetNameOfOwner(owner, tile);
00336 return_cmd_error(STR_ERROR_OWNED_BY);
00337 }
00338
00343 static void GenerateCompanyName(Company *c)
00344 {
00345
00346
00347 char buffer[(MAX_LENGTH_COMPANY_NAME_CHARS + 1) * MAX_CHAR_LENGTH];
00348
00349 if (c->name_1 != STR_SV_UNNAMED) return;
00350 if (c->last_build_coordinate == 0) return;
00351
00352 Town *t = ClosestTownFromTile(c->last_build_coordinate, UINT_MAX);
00353
00354 StringID str;
00355 uint32 strp;
00356 if (t->name == NULL && IsInsideMM(t->townnametype, SPECSTR_TOWNNAME_START, SPECSTR_TOWNNAME_LAST + 1)) {
00357 str = t->townnametype - SPECSTR_TOWNNAME_START + SPECSTR_PLAYERNAME_START;
00358 strp = t->townnameparts;
00359
00360 verify_name:;
00361
00362 Company *cc;
00363 FOR_ALL_COMPANIES(cc) {
00364 if (cc->name_1 == str && cc->name_2 == strp) goto bad_town_name;
00365 }
00366
00367 GetString(buffer, str, lastof(buffer));
00368 if (Utf8StringLength(buffer) >= MAX_LENGTH_COMPANY_NAME_CHARS) goto bad_town_name;
00369
00370 set_name:;
00371 c->name_1 = str;
00372 c->name_2 = strp;
00373
00374 MarkWholeScreenDirty();
00375
00376 if (c->is_ai) {
00377 CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
00378 cni->FillData(c);
00379 SetDParam(0, STR_NEWS_COMPANY_LAUNCH_TITLE);
00380 SetDParam(1, STR_NEWS_COMPANY_LAUNCH_DESCRIPTION);
00381 SetDParamStr(2, cni->company_name);
00382 SetDParam(3, t->index);
00383 AddNewsItem(STR_MESSAGE_NEWS_FORMAT, NS_COMPANY_NEW, NR_TILE, c->last_build_coordinate, NR_NONE, UINT32_MAX, cni);
00384 }
00385 AI::BroadcastNewEvent(new AIEventCompanyNew(c->index), c->index);
00386 return;
00387 }
00388 bad_town_name:;
00389
00390 if (c->president_name_1 == SPECSTR_PRESIDENT_NAME) {
00391 str = SPECSTR_ANDCO_NAME;
00392 strp = c->president_name_2;
00393 goto set_name;
00394 } else {
00395 str = SPECSTR_ANDCO_NAME;
00396 strp = Random();
00397 goto verify_name;
00398 }
00399 }
00400
00402 static const byte _colour_sort[COLOUR_END] = {2, 2, 3, 2, 3, 2, 3, 2, 3, 2, 2, 2, 3, 1, 1, 1};
00404 static const Colours _similar_colour[COLOUR_END][2] = {
00405 { COLOUR_BLUE, COLOUR_LIGHT_BLUE },
00406 { COLOUR_GREEN, COLOUR_DARK_GREEN },
00407 { INVALID_COLOUR, INVALID_COLOUR },
00408 { COLOUR_ORANGE, INVALID_COLOUR },
00409 { INVALID_COLOUR, INVALID_COLOUR },
00410 { COLOUR_DARK_BLUE, COLOUR_BLUE },
00411 { COLOUR_PALE_GREEN, COLOUR_DARK_GREEN },
00412 { COLOUR_PALE_GREEN, COLOUR_GREEN },
00413 { COLOUR_DARK_BLUE, COLOUR_LIGHT_BLUE },
00414 { COLOUR_BROWN, COLOUR_ORANGE },
00415 { COLOUR_PURPLE, INVALID_COLOUR },
00416 { COLOUR_MAUVE, INVALID_COLOUR },
00417 { COLOUR_YELLOW, COLOUR_CREAM },
00418 { COLOUR_CREAM, INVALID_COLOUR },
00419 { COLOUR_WHITE, INVALID_COLOUR },
00420 { COLOUR_GREY, INVALID_COLOUR },
00421 };
00422
00427 static Colours GenerateCompanyColour()
00428 {
00429 Colours colours[COLOUR_END];
00430
00431
00432 for (uint i = 0; i < COLOUR_END; i++) colours[i] = (Colours)i;
00433
00434
00435 for (uint i = 0; i < 100; i++) {
00436 uint r = Random();
00437 Swap(colours[GB(r, 0, 4)], colours[GB(r, 4, 4)]);
00438 }
00439
00440
00441 for (uint i = 0; i < COLOUR_END; i++) {
00442 for (uint j = 1; j < COLOUR_END; j++) {
00443 if (_colour_sort[colours[j - 1]] < _colour_sort[colours[j]]) {
00444 Swap(colours[j - 1], colours[j]);
00445 }
00446 }
00447 }
00448
00449
00450 Company *c;
00451 FOR_ALL_COMPANIES(c) {
00452 Colours pcolour = (Colours)c->colour;
00453
00454 for (uint i = 0; i < COLOUR_END; i++) {
00455 if (colours[i] == pcolour) {
00456 colours[i] = INVALID_COLOUR;
00457 break;
00458 }
00459 }
00460
00461 for (uint j = 0; j < 2; j++) {
00462 Colours similar = _similar_colour[pcolour][j];
00463 if (similar == INVALID_COLOUR) break;
00464
00465 for (uint i = 1; i < COLOUR_END; i++) {
00466 if (colours[i - 1] == similar) Swap(colours[i - 1], colours[i]);
00467 }
00468 }
00469 }
00470
00471
00472 for (uint i = 0; i < COLOUR_END; i++) {
00473 if (colours[i] != INVALID_COLOUR) return colours[i];
00474 }
00475
00476 NOT_REACHED();
00477 }
00478
00483 static void GeneratePresidentName(Company *c)
00484 {
00485 for (;;) {
00486 restart:;
00487 c->president_name_2 = Random();
00488 c->president_name_1 = SPECSTR_PRESIDENT_NAME;
00489
00490
00491
00492 char buffer[(MAX_LENGTH_PRESIDENT_NAME_CHARS + 1) * MAX_CHAR_LENGTH];
00493 SetDParam(0, c->index);
00494 GetString(buffer, STR_PRESIDENT_NAME, lastof(buffer));
00495 if (Utf8StringLength(buffer) >= MAX_LENGTH_PRESIDENT_NAME_CHARS) continue;
00496
00497 Company *cc;
00498 FOR_ALL_COMPANIES(cc) {
00499 if (c != cc) {
00500
00501 char buffer2[(MAX_LENGTH_PRESIDENT_NAME_CHARS + 1) * MAX_CHAR_LENGTH];
00502 SetDParam(0, cc->index);
00503 GetString(buffer2, STR_PRESIDENT_NAME, lastof(buffer2));
00504 if (strcmp(buffer2, buffer) == 0) goto restart;
00505 }
00506 }
00507 return;
00508 }
00509 }
00510
00516 void ResetCompanyLivery(Company *c)
00517 {
00518 for (LiveryScheme scheme = LS_BEGIN; scheme < LS_END; scheme++) {
00519 c->livery[scheme].in_use = false;
00520 c->livery[scheme].colour1 = c->colour;
00521 c->livery[scheme].colour2 = c->colour;
00522 }
00523 }
00524
00532 Company *DoStartupNewCompany(bool is_ai, CompanyID company = INVALID_COMPANY)
00533 {
00534 if (!Company::CanAllocateItem()) return NULL;
00535
00536
00537 Colours colour = GenerateCompanyColour();
00538
00539 Company *c;
00540 if (company == INVALID_COMPANY) {
00541 c = new Company(STR_SV_UNNAMED, is_ai);
00542 } else {
00543 if (Company::IsValidID(company)) return NULL;
00544 c = new (company) Company(STR_SV_UNNAMED, is_ai);
00545 }
00546
00547 c->colour = colour;
00548
00549 ResetCompanyLivery(c);
00550 _company_colours[c->index] = (Colours)c->colour;
00551
00552 c->money = c->current_loan = (100000ll * _economy.inflation_prices >> 16) / 50000 * 50000;
00553
00554 c->share_owners[0] = c->share_owners[1] = c->share_owners[2] = c->share_owners[3] = INVALID_OWNER;
00555
00556 c->avail_railtypes = GetCompanyRailtypes(c->index);
00557 c->avail_roadtypes = GetCompanyRoadtypes(c->index);
00558 c->inaugurated_year = _cur_year;
00559 RandomCompanyManagerFaceBits(c->face, (GenderEthnicity)Random(), false);
00560
00561 SetDefaultCompanySettings(c->index);
00562
00563 GeneratePresidentName(c);
00564
00565 SetWindowDirty(WC_GRAPH_LEGEND, 0);
00566 SetWindowClassesDirty(WC_CLIENT_LIST_POPUP);
00567 SetWindowDirty(WC_CLIENT_LIST, 0);
00568 BuildOwnerLegend();
00569 InvalidateWindowData(WC_SMALLMAP, 0, 1);
00570
00571 if (is_ai && (!_networking || _network_server)) AI::StartNew(c->index);
00572
00573 c->num_engines = CallocT<uint16>(Engine::GetPoolSize());
00574
00575 if (!is_ai) UpdateAllTownVirtCoords();
00576
00577 return c;
00578 }
00579
00581 void StartupCompanies()
00582 {
00583 _next_competitor_start = 0;
00584 }
00585
00586 #ifdef ENABLE_AI
00587
00588 static void MaybeStartNewCompany()
00589 {
00590 #ifdef ENABLE_NETWORK
00591 if (_networking && Company::GetNumItems() >= _settings_client.network.max_companies) return;
00592 #endif
00593
00594 Company *c;
00595
00596
00597 uint n = 0;
00598 FOR_ALL_COMPANIES(c) {
00599 if (c->is_ai) n++;
00600 }
00601
00602 if (n < (uint)_settings_game.difficulty.max_no_competitors) {
00603
00604
00605 DoCommandP(0, 1 | INVALID_COMPANY << 16, 0, CMD_COMPANY_CTRL);
00606 }
00607 }
00608 #endif
00609
00611 void InitializeCompanies()
00612 {
00613 _cur_company_tick_index = 0;
00614 }
00615
00622 bool MayCompanyTakeOver(CompanyID cbig, CompanyID csmall)
00623 {
00624 uint big_counts[4], small_counts[4];
00625 CountCompanyVehicles(cbig, big_counts);
00626 CountCompanyVehicles(csmall, small_counts);
00627
00628
00629 return big_counts[VEH_TRAIN] + small_counts[VEH_TRAIN] <= _settings_game.vehicle.max_trains &&
00630 big_counts[VEH_ROAD] + small_counts[VEH_ROAD] <= _settings_game.vehicle.max_roadveh &&
00631 big_counts[VEH_SHIP] + small_counts[VEH_SHIP] <= _settings_game.vehicle.max_ships &&
00632 big_counts[VEH_AIRCRAFT] + small_counts[VEH_AIRCRAFT] <= _settings_game.vehicle.max_aircraft;
00633 }
00634
00644 static void HandleBankruptcyTakeover(Company *c)
00645 {
00646
00647
00648
00649
00650
00651 static const int TAKE_OVER_TIMEOUT = 3 * 30 * DAY_TICKS / (MAX_COMPANIES - 1);
00652
00653 assert(c->bankrupt_asked != 0);
00654
00655
00656 if (c->bankrupt_timeout != 0) {
00657 c->bankrupt_timeout -= MAX_COMPANIES;
00658 if (c->bankrupt_timeout > 0) return;
00659 c->bankrupt_timeout = 0;
00660
00661 return;
00662 }
00663
00664
00665 if (c->bankrupt_asked == MAX_UVALUE(CompanyMask)) return;
00666
00667 Company *c2, *best = NULL;
00668 int32 best_performance = -1;
00669
00670
00671 FOR_ALL_COMPANIES(c2) {
00672 if (c2->bankrupt_asked == 0 &&
00673 !HasBit(c->bankrupt_asked, c2->index) &&
00674 best_performance < c2->old_economy[1].performance_history &&
00675 MayCompanyTakeOver(c2->index, c->index)) {
00676 best_performance = c2->old_economy[1].performance_history;
00677 best = c2;
00678 }
00679 }
00680
00681
00682 if (best_performance == -1) {
00683 c->bankrupt_asked = MAX_UVALUE(CompanyMask);
00684 return;
00685 }
00686
00687 SetBit(c->bankrupt_asked, best->index);
00688
00689 c->bankrupt_timeout = TAKE_OVER_TIMEOUT;
00690 if (best->is_ai) {
00691 AI::NewEvent(best->index, new AIEventCompanyAskMerger(c->index, ClampToI32(c->bankrupt_value)));
00692 } else if (IsInteractiveCompany(best->index)) {
00693 ShowBuyCompanyDialog(c->index);
00694 }
00695 }
00696
00698 void OnTick_Companies()
00699 {
00700 if (_game_mode == GM_EDITOR) return;
00701
00702 Company *c = Company::GetIfValid(_cur_company_tick_index);
00703 if (c != NULL) {
00704 if (c->name_1 != 0) GenerateCompanyName(c);
00705 if (c->bankrupt_asked != 0) HandleBankruptcyTakeover(c);
00706 }
00707
00708 #ifdef ENABLE_AI
00709 if (_next_competitor_start == 0) {
00710 _next_competitor_start = AI::GetStartNextTime() * DAY_TICKS;
00711 }
00712
00713 if (AI::CanStartNew() && _game_mode != GM_MENU && --_next_competitor_start == 0) {
00714 MaybeStartNewCompany();
00715 }
00716 #endif
00717
00718 _cur_company_tick_index = (_cur_company_tick_index + 1) % MAX_COMPANIES;
00719 }
00720
00725 void CompaniesYearlyLoop()
00726 {
00727 Company *c;
00728
00729
00730 FOR_ALL_COMPANIES(c) {
00731 memmove(&c->yearly_expenses[1], &c->yearly_expenses[0], sizeof(c->yearly_expenses) - sizeof(c->yearly_expenses[0]));
00732 memset(&c->yearly_expenses[0], 0, sizeof(c->yearly_expenses[0]));
00733 SetWindowDirty(WC_FINANCES, c->index);
00734 }
00735
00736 if (_settings_client.gui.show_finances && _local_company != COMPANY_SPECTATOR) {
00737 ShowCompanyFinances(_local_company);
00738 c = Company::Get(_local_company);
00739 if (c->num_valid_stat_ent > 5 && c->old_economy[0].performance_history < c->old_economy[4].performance_history) {
00740 SndPlayFx(SND_01_BAD_YEAR);
00741 } else {
00742 SndPlayFx(SND_00_GOOD_YEAR);
00743 }
00744 }
00745 }
00746
00752 void CompanyNewsInformation::FillData(const Company *c, const Company *other)
00753 {
00754 SetDParam(0, c->index);
00755 GetString(this->company_name, STR_COMPANY_NAME, lastof(this->company_name));
00756
00757 if (other == NULL) {
00758 *this->other_company_name = '\0';
00759 } else {
00760 SetDParam(0, other->index);
00761 GetString(this->other_company_name, STR_COMPANY_NAME, lastof(this->other_company_name));
00762 c = other;
00763 }
00764
00765 SetDParam(0, c->index);
00766 GetString(this->president_name, STR_PRESIDENT_NAME_MANAGER, lastof(this->president_name));
00767
00768 this->colour = c->colour;
00769 this->face = c->face;
00770
00771 }
00772
00777 void CompanyAdminUpdate(const Company *company)
00778 {
00779 #ifdef ENABLE_NETWORK
00780 if (_network_server) NetworkAdminCompanyUpdate(company);
00781 #endif
00782 }
00783
00788 void CompanyAdminBankrupt(CompanyID company_id)
00789 {
00790 #ifdef ENABLE_NETWORK
00791 if (_network_server) NetworkAdminCompanyRemove(company_id, ADMIN_CRR_BANKRUPT);
00792 #endif
00793 }
00794
00809 CommandCost CmdCompanyCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00810 {
00811 InvalidateWindowData(WC_COMPANY_LEAGUE, 0, 0);
00812 CompanyID company_id = (CompanyID)GB(p1, 16, 8);
00813 #ifdef ENABLE_NETWORK
00814 ClientID client_id = (ClientID)p2;
00815 #endif
00816
00817 switch (GB(p1, 0, 16)) {
00818 case 0: {
00819
00820 if (!_networking) return CMD_ERROR;
00821
00822 #ifdef ENABLE_NETWORK
00823
00824 if (!(flags & DC_EXEC)) return CommandCost();
00825 NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id);
00826 if (ci == NULL) return CommandCost();
00827
00828
00829 DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
00830
00831 Company *c = DoStartupNewCompany(false);
00832
00833
00834 if (c == NULL) {
00835 if (_network_server) {
00836 ci->client_playas = COMPANY_SPECTATOR;
00837 NetworkUpdateClientInfo(ci->client_id);
00838 }
00839 break;
00840 }
00841
00842
00843 if (client_id == _network_own_client_id) {
00844 assert(_local_company == COMPANY_SPECTATOR);
00845 SetLocalCompany(c->index);
00846 if (!StrEmpty(_settings_client.network.default_company_pass)) {
00847 NetworkChangeCompanyPassword(_local_company, _settings_client.network.default_company_pass);
00848 }
00849
00850
00851
00852 SyncCompanySettings();
00853
00854 MarkWholeScreenDirty();
00855 }
00856
00857 if (_network_server) {
00858 CompanyID old_playas = ci->client_playas;
00859 ci->client_playas = c->index;
00860 NetworkUpdateClientInfo(ci->client_id);
00861
00862 if (Company::IsValidID(ci->client_playas)) {
00863 _network_company_states[c->index].months_empty = 0;
00864 _network_company_states[c->index].password[0] = '\0';
00865 NetworkServerUpdateCompanyPassworded(ci->client_playas, false);
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878 NetworkSendCommand(0, 0, 0, CMD_RENAME_PRESIDENT, NULL, ci->client_name, ci->client_playas);
00879 }
00880
00881
00882 if (old_playas == COMPANY_SPECTATOR) {
00883 NetworkAdminCompanyInfo(c, true);
00884 NetworkServerSendChat(NETWORK_ACTION_COMPANY_NEW, DESTTYPE_BROADCAST, 0, "", ci->client_id, ci->client_playas + 1);
00885 }
00886 }
00887 #endif
00888 break;
00889 }
00890
00891 case 1:
00892 if (!(flags & DC_EXEC)) return CommandCost();
00893
00894 if (company_id != INVALID_COMPANY && (company_id >= MAX_COMPANIES || Company::IsValidID(company_id))) return CMD_ERROR;
00895 DoStartupNewCompany(true, company_id);
00896 break;
00897
00898 case 2: {
00899 Company *c = Company::GetIfValid(company_id);
00900 if (c == NULL) return CMD_ERROR;
00901
00902 if (!(flags & DC_EXEC)) return CommandCost();
00903
00904
00905 DeleteCompanyWindows(c->index);
00906 CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
00907 cni->FillData(c);
00908
00909
00910 SetDParam(0, STR_NEWS_COMPANY_BANKRUPT_TITLE);
00911 SetDParam(1, STR_NEWS_COMPANY_BANKRUPT_DESCRIPTION);
00912 SetDParamStr(2, cni->company_name);
00913 AddCompanyNewsItem(STR_MESSAGE_NEWS_FORMAT, NS_COMPANY_BANKRUPT, cni);
00914
00915
00916 ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER);
00917 if (c->is_ai) AI::Stop(c->index);
00918
00919 CompanyID c_index = c->index;
00920 delete c;
00921 AI::BroadcastNewEvent(new AIEventCompanyBankrupt(c_index));
00922 CompanyAdminBankrupt(c_index);
00923 break;
00924 }
00925
00926 default: return CMD_ERROR;
00927 }
00928
00929 InvalidateWindowClassesData(WC_GAME_OPTIONS);
00930 InvalidateWindowClassesData(WC_AI_SETTINGS);
00931 InvalidateWindowClassesData(WC_AI_LIST);
00932
00933 return CommandCost();
00934 }
00935
00945 CommandCost CmdSetCompanyManagerFace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00946 {
00947 CompanyManagerFace cmf = (CompanyManagerFace)p2;
00948
00949 if (!IsValidCompanyManagerFace(cmf)) return CMD_ERROR;
00950
00951 if (flags & DC_EXEC) {
00952 Company::Get(_current_company)->face = cmf;
00953 MarkWholeScreenDirty();
00954 }
00955 return CommandCost();
00956 }
00957
00969 CommandCost CmdSetCompanyColour(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00970 {
00971 Colours colour = Extract<Colours, 0, 4>(p2);
00972 LiveryScheme scheme = Extract<LiveryScheme, 0, 8>(p1);
00973 byte state = GB(p1, 8, 2);
00974
00975 if (scheme >= LS_END || state >= 3 || colour == INVALID_COLOUR) return CMD_ERROR;
00976
00977 Company *c = Company::Get(_current_company);
00978
00979
00980 if (scheme == LS_DEFAULT && state == 0) {
00981 const Company *cc;
00982 FOR_ALL_COMPANIES(cc) {
00983 if (cc != c && cc->colour == colour) return CMD_ERROR;
00984 }
00985 }
00986
00987 if (flags & DC_EXEC) {
00988 switch (state) {
00989 case 0:
00990 c->livery[scheme].colour1 = colour;
00991
00992
00993
00994 if (scheme == LS_DEFAULT) {
00995 _company_colours[_current_company] = colour;
00996 c->colour = colour;
00997 CompanyAdminUpdate(c);
00998 }
00999 break;
01000
01001 case 1:
01002 c->livery[scheme].colour2 = colour;
01003 break;
01004
01005 case 2:
01006 c->livery[scheme].in_use = colour != 0;
01007
01008
01009
01010
01011
01012
01013
01014
01015 if (colour != 0) {
01016 c->livery[LS_DEFAULT].in_use = true;
01017 break;
01018 }
01019
01020
01021
01022 c->livery[LS_DEFAULT].in_use = false;
01023 for (scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
01024 if (c->livery[scheme].in_use) {
01025 c->livery[LS_DEFAULT].in_use = true;
01026 break;
01027 }
01028 }
01029 break;
01030
01031 default:
01032 break;
01033 }
01034 ResetVehicleColourMap();
01035 MarkWholeScreenDirty();
01036
01037
01038 InvalidateWindowData(WC_INCOME_GRAPH, 0);
01039 InvalidateWindowData(WC_OPERATING_PROFIT, 0);
01040 InvalidateWindowData(WC_DELIVERED_CARGO, 0);
01041 InvalidateWindowData(WC_PERFORMANCE_HISTORY, 0);
01042 InvalidateWindowData(WC_COMPANY_VALUE, 0);
01043
01044 BuildOwnerLegend();
01045 InvalidateWindowData(WC_SMALLMAP, 0, 1);
01046
01047
01048 Vehicle *v;
01049 FOR_ALL_VEHICLES(v) {
01050 if (v->owner == _current_company) v->InvalidateNewGRFCache();
01051 }
01052
01053 extern void UpdateObjectColours(const Company *c);
01054 UpdateObjectColours(c);
01055 }
01056 return CommandCost();
01057 }
01058
01064 static bool IsUniqueCompanyName(const char *name)
01065 {
01066 const Company *c;
01067
01068 FOR_ALL_COMPANIES(c) {
01069 if (c->name != NULL && strcmp(c->name, name) == 0) return false;
01070 }
01071
01072 return true;
01073 }
01074
01084 CommandCost CmdRenameCompany(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01085 {
01086 bool reset = StrEmpty(text);
01087
01088 if (!reset) {
01089 if (Utf8StringLength(text) >= MAX_LENGTH_COMPANY_NAME_CHARS) return CMD_ERROR;
01090 if (!IsUniqueCompanyName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
01091 }
01092
01093 if (flags & DC_EXEC) {
01094 Company *c = Company::Get(_current_company);
01095 free(c->name);
01096 c->name = reset ? NULL : strdup(text);
01097 MarkWholeScreenDirty();
01098 InvalidateWindowClassesData(WC_WATCH_COMPANY, 0);
01099 CompanyAdminUpdate(c);
01100 }
01101
01102 return CommandCost();
01103 }
01104
01110 static bool IsUniquePresidentName(const char *name)
01111 {
01112 const Company *c;
01113
01114 FOR_ALL_COMPANIES(c) {
01115 if (c->president_name != NULL && strcmp(c->president_name, name) == 0) return false;
01116 }
01117
01118 return true;
01119 }
01120
01130 CommandCost CmdRenamePresident(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01131 {
01132 bool reset = StrEmpty(text);
01133
01134 if (!reset) {
01135 if (Utf8StringLength(text) >= MAX_LENGTH_PRESIDENT_NAME_CHARS) return CMD_ERROR;
01136 if (!IsUniquePresidentName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
01137 }
01138
01139 if (flags & DC_EXEC) {
01140 Company *c = Company::Get(_current_company);
01141 free(c->president_name);
01142
01143 if (reset) {
01144 c->president_name = NULL;
01145 } else {
01146 c->president_name = strdup(text);
01147
01148 if (c->name_1 == STR_SV_UNNAMED && c->name == NULL) {
01149 char buf[80];
01150
01151 snprintf(buf, lengthof(buf), "%s Transport", text);
01152 DoCommand(0, 0, 0, DC_EXEC, CMD_RENAME_COMPANY, buf);
01153 }
01154 }
01155
01156 MarkWholeScreenDirty();
01157 CompanyAdminUpdate(c);
01158 }
01159
01160 return CommandCost();
01161 }