00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "company_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 "network/network_admin.h"
00024 #include "ai/ai.hpp"
00025 #include "company_manager_face.h"
00026 #include "window_func.h"
00027 #include "strings_func.h"
00028 #include "date_func.h"
00029 #include "sound_func.h"
00030 #include "rail.h"
00031 #include "core/pool_func.hpp"
00032 #include "settings_func.h"
00033 #include "vehicle_base.h"
00034 #include "vehicle_func.h"
00035 #include "smallmap_gui.h"
00036 #include "game/game.hpp"
00037 #include "goal_base.h"
00038 #include "story_base.h"
00039
00040 #include "table/strings.h"
00041
00042 CompanyByte _local_company;
00043 CompanyByte _current_company;
00044 Colours _company_colours[MAX_COMPANIES];
00045 CompanyManagerFace _company_manager_face;
00046 uint _next_competitor_start;
00047 uint _cur_company_tick_index;
00048
00049 CompanyPool _company_pool("Company");
00050 INSTANTIATE_POOL_METHODS(Company)
00051
00052
00057 Company::Company(uint16 name_1, bool is_ai)
00058 {
00059 this->name_1 = name_1;
00060 this->location_of_HQ = INVALID_TILE;
00061 this->is_ai = is_ai;
00062 this->terraform_limit = _settings_game.construction.terraform_frame_burst << 16;
00063 this->clear_limit = _settings_game.construction.clear_frame_burst << 16;
00064 this->tree_limit = _settings_game.construction.tree_frame_burst << 16;
00065
00066 for (uint j = 0; j < 4; j++) this->share_owners[j] = COMPANY_SPECTATOR;
00067 InvalidateWindowData(WC_PERFORMANCE_DETAIL, 0, INVALID_COMPANY);
00068 }
00069
00071 Company::~Company()
00072 {
00073 if (CleaningPool()) return;
00074
00075 DeleteCompanyWindows(this->index);
00076 }
00077
00082 void Company::PostDestructor(size_t index)
00083 {
00084 InvalidateWindowData(WC_GRAPH_LEGEND, 0, (int)index);
00085 InvalidateWindowData(WC_PERFORMANCE_DETAIL, 0, (int)index);
00086 InvalidateWindowData(WC_COMPANY_LEAGUE, 0, 0);
00087 InvalidateWindowData(WC_LINKGRAPH_LEGEND, 0);
00088
00089 InvalidateWindowData(WC_ERRMSG, 0);
00090 }
00091
00098 void SetLocalCompany(CompanyID new_company)
00099 {
00100
00101 assert(Company::IsValidID(new_company) || new_company == COMPANY_SPECTATOR || new_company == OWNER_NONE);
00102
00103 #ifdef ENABLE_NETWORK
00104
00105 InvalidateWindowData(WC_SEND_NETWORK_MSG, DESTTYPE_TEAM, _local_company);
00106 #endif
00107
00108 assert(IsLocalCompany());
00109
00110 _current_company = _local_company = new_company;
00111
00112
00113 DeleteConstructionWindows();
00114
00115
00116 MarkWholeScreenDirty();
00117 InvalidateWindowClassesData(WC_SIGN_LIST, -1);
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 static bool IsValidCompanyManagerFace(CompanyManagerFace cmf)
00149 {
00150 if (!AreCompanyManagerFaceBitsValid(cmf, CMFV_GEN_ETHN, GE_WM)) return false;
00151
00152 GenderEthnicity ge = (GenderEthnicity)GetCompanyManagerFaceBits(cmf, CMFV_GEN_ETHN, GE_WM);
00153 bool has_moustache = !HasBit(ge, GENDER_FEMALE) && GetCompanyManagerFaceBits(cmf, CMFV_HAS_MOUSTACHE, ge) != 0;
00154 bool has_tie_earring = !HasBit(ge, GENDER_FEMALE) || GetCompanyManagerFaceBits(cmf, CMFV_HAS_TIE_EARRING, ge) != 0;
00155 bool has_glasses = GetCompanyManagerFaceBits(cmf, CMFV_HAS_GLASSES, ge) != 0;
00156
00157 if (!AreCompanyManagerFaceBitsValid(cmf, CMFV_EYE_COLOUR, ge)) return false;
00158 for (CompanyManagerFaceVariable cmfv = CMFV_CHEEKS; cmfv < CMFV_END; cmfv++) {
00159 switch (cmfv) {
00160 case CMFV_MOUSTACHE: if (!has_moustache) continue; break;
00161 case CMFV_LIPS:
00162 case CMFV_NOSE: if (has_moustache) continue; break;
00163 case CMFV_TIE_EARRING: if (!has_tie_earring) continue; break;
00164 case CMFV_GLASSES: if (!has_glasses) continue; break;
00165 default: break;
00166 }
00167 if (!AreCompanyManagerFaceBitsValid(cmf, cmfv, ge)) return false;
00168 }
00169
00170 return true;
00171 }
00172
00177 void InvalidateCompanyWindows(const Company *company)
00178 {
00179 CompanyID cid = company->index;
00180
00181 if (cid == _local_company) SetWindowDirty(WC_STATUS_BAR, 0);
00182 SetWindowDirty(WC_FINANCES, cid);
00183 }
00184
00190 bool CheckCompanyHasMoney(CommandCost &cost)
00191 {
00192 if (cost.GetCost() > 0) {
00193 const Company *c = Company::GetIfValid(_current_company);
00194 if (c != NULL && cost.GetCost() > c->money) {
00195 SetDParam(0, cost.GetCost());
00196 cost.MakeError(STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY);
00197 return false;
00198 }
00199 }
00200 return true;
00201 }
00202
00208 static void SubtractMoneyFromAnyCompany(Company *c, CommandCost cost)
00209 {
00210 if (cost.GetCost() == 0) return;
00211 assert(cost.GetExpensesType() != INVALID_EXPENSES);
00212
00213 c->money -= cost.GetCost();
00214 c->yearly_expenses[0][cost.GetExpensesType()] += cost.GetCost();
00215
00216 if (HasBit(1 << EXPENSES_TRAIN_INC |
00217 1 << EXPENSES_ROADVEH_INC |
00218 1 << EXPENSES_AIRCRAFT_INC |
00219 1 << EXPENSES_SHIP_INC, cost.GetExpensesType())) {
00220 c->cur_economy.income -= cost.GetCost();
00221 } else if (HasBit(1 << EXPENSES_TRAIN_RUN |
00222 1 << EXPENSES_ROADVEH_RUN |
00223 1 << EXPENSES_AIRCRAFT_RUN |
00224 1 << EXPENSES_SHIP_RUN |
00225 1 << EXPENSES_PROPERTY |
00226 1 << EXPENSES_LOAN_INT, cost.GetExpensesType())) {
00227 c->cur_economy.expenses -= cost.GetCost();
00228 }
00229
00230 InvalidateCompanyWindows(c);
00231 }
00232
00237 void SubtractMoneyFromCompany(CommandCost cost)
00238 {
00239 Company *c = Company::GetIfValid(_current_company);
00240 if (c != NULL) SubtractMoneyFromAnyCompany(c, cost);
00241 }
00242
00248 void SubtractMoneyFromCompanyFract(CompanyID company, CommandCost cst)
00249 {
00250 Company *c = Company::Get(company);
00251 byte m = c->money_fraction;
00252 Money cost = cst.GetCost();
00253
00254 c->money_fraction = m - (byte)cost;
00255 cost >>= 8;
00256 if (c->money_fraction > m) cost++;
00257 if (cost != 0) SubtractMoneyFromAnyCompany(c, CommandCost(cst.GetExpensesType(), cost));
00258 }
00259
00261 void UpdateLandscapingLimits()
00262 {
00263 Company *c;
00264 FOR_ALL_COMPANIES(c) {
00265 c->terraform_limit = min(c->terraform_limit + _settings_game.construction.terraform_per_64k_frames, (uint32)_settings_game.construction.terraform_frame_burst << 16);
00266 c->clear_limit = min(c->clear_limit + _settings_game.construction.clear_per_64k_frames, (uint32)_settings_game.construction.clear_frame_burst << 16);
00267 c->tree_limit = min(c->tree_limit + _settings_game.construction.tree_per_64k_frames, (uint32)_settings_game.construction.tree_frame_burst << 16);
00268 }
00269 }
00270
00277 void GetNameOfOwner(Owner owner, TileIndex tile)
00278 {
00279 SetDParam(2, owner);
00280
00281 if (owner != OWNER_TOWN) {
00282 if (!Company::IsValidID(owner)) {
00283 SetDParam(0, STR_COMPANY_SOMEONE);
00284 } else {
00285 SetDParam(0, STR_COMPANY_NAME);
00286 SetDParam(1, owner);
00287 }
00288 } else {
00289 assert(tile != 0);
00290 const Town *t = ClosestTownFromTile(tile, UINT_MAX);
00291
00292 SetDParam(0, STR_TOWN_NAME);
00293 SetDParam(1, t->index);
00294 }
00295 }
00296
00297
00306 CommandCost CheckOwnership(Owner owner, TileIndex tile)
00307 {
00308 assert(owner < OWNER_END);
00309 assert(owner != OWNER_TOWN || tile != 0);
00310
00311 if (owner == _current_company) return CommandCost();
00312
00313 GetNameOfOwner(owner, tile);
00314 return_cmd_error(STR_ERROR_OWNED_BY);
00315 }
00316
00324 CommandCost CheckTileOwnership(TileIndex tile)
00325 {
00326 Owner owner = GetTileOwner(tile);
00327
00328 assert(owner < OWNER_END);
00329
00330 if (owner == _current_company) return CommandCost();
00331
00332
00333 if (IsLocalCompany()) GetNameOfOwner(owner, tile);
00334 return_cmd_error(STR_ERROR_OWNED_BY);
00335 }
00336
00341 static void GenerateCompanyName(Company *c)
00342 {
00343
00344
00345 char buffer[(MAX_LENGTH_COMPANY_NAME_CHARS + 1) * MAX_CHAR_LENGTH];
00346
00347 if (c->name_1 != STR_SV_UNNAMED) return;
00348 if (c->last_build_coordinate == 0) return;
00349
00350 Town *t = ClosestTownFromTile(c->last_build_coordinate, UINT_MAX);
00351
00352 StringID str;
00353 uint32 strp;
00354 if (t->name == NULL && IsInsideMM(t->townnametype, SPECSTR_TOWNNAME_START, SPECSTR_TOWNNAME_LAST + 1)) {
00355 str = t->townnametype - SPECSTR_TOWNNAME_START + SPECSTR_COMPANY_NAME_START;
00356 strp = t->townnameparts;
00357
00358 verify_name:;
00359
00360 Company *cc;
00361 FOR_ALL_COMPANIES(cc) {
00362 if (cc->name_1 == str && cc->name_2 == strp) goto bad_town_name;
00363 }
00364
00365 GetString(buffer, str, lastof(buffer));
00366 if (Utf8StringLength(buffer) >= MAX_LENGTH_COMPANY_NAME_CHARS) goto bad_town_name;
00367
00368 set_name:;
00369 c->name_1 = str;
00370 c->name_2 = strp;
00371
00372 MarkWholeScreenDirty();
00373
00374 if (c->is_ai) {
00375 CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
00376 cni->FillData(c);
00377 SetDParam(0, STR_NEWS_COMPANY_LAUNCH_TITLE);
00378 SetDParam(1, STR_NEWS_COMPANY_LAUNCH_DESCRIPTION);
00379 SetDParamStr(2, cni->company_name);
00380 SetDParam(3, t->index);
00381 AddNewsItem(STR_MESSAGE_NEWS_FORMAT, NT_COMPANY_INFO, NF_COMPANY, NR_TILE, c->last_build_coordinate, NR_NONE, UINT32_MAX, cni);
00382 }
00383 return;
00384 }
00385 bad_town_name:;
00386
00387 if (c->president_name_1 == SPECSTR_PRESIDENT_NAME) {
00388 str = SPECSTR_ANDCO_NAME;
00389 strp = c->president_name_2;
00390 goto set_name;
00391 } else {
00392 str = SPECSTR_ANDCO_NAME;
00393 strp = Random();
00394 goto verify_name;
00395 }
00396 }
00397
00399 static const byte _colour_sort[COLOUR_END] = {2, 2, 3, 2, 3, 2, 3, 2, 3, 2, 2, 2, 3, 1, 1, 1};
00401 static const Colours _similar_colour[COLOUR_END][2] = {
00402 { COLOUR_BLUE, COLOUR_LIGHT_BLUE },
00403 { COLOUR_GREEN, COLOUR_DARK_GREEN },
00404 { INVALID_COLOUR, INVALID_COLOUR },
00405 { COLOUR_ORANGE, INVALID_COLOUR },
00406 { INVALID_COLOUR, INVALID_COLOUR },
00407 { COLOUR_DARK_BLUE, COLOUR_BLUE },
00408 { COLOUR_PALE_GREEN, COLOUR_DARK_GREEN },
00409 { COLOUR_PALE_GREEN, COLOUR_GREEN },
00410 { COLOUR_DARK_BLUE, COLOUR_LIGHT_BLUE },
00411 { COLOUR_BROWN, COLOUR_ORANGE },
00412 { COLOUR_PURPLE, INVALID_COLOUR },
00413 { COLOUR_MAUVE, INVALID_COLOUR },
00414 { COLOUR_YELLOW, COLOUR_CREAM },
00415 { COLOUR_CREAM, INVALID_COLOUR },
00416 { COLOUR_WHITE, INVALID_COLOUR },
00417 { COLOUR_GREY, INVALID_COLOUR },
00418 };
00419
00424 static Colours GenerateCompanyColour()
00425 {
00426 Colours colours[COLOUR_END];
00427
00428
00429 for (uint i = 0; i < COLOUR_END; i++) colours[i] = (Colours)i;
00430
00431
00432 for (uint i = 0; i < 100; i++) {
00433 uint r = Random();
00434 Swap(colours[GB(r, 0, 4)], colours[GB(r, 4, 4)]);
00435 }
00436
00437
00438 for (uint i = 0; i < COLOUR_END; i++) {
00439 for (uint j = 1; j < COLOUR_END; j++) {
00440 if (_colour_sort[colours[j - 1]] < _colour_sort[colours[j]]) {
00441 Swap(colours[j - 1], colours[j]);
00442 }
00443 }
00444 }
00445
00446
00447 Company *c;
00448 FOR_ALL_COMPANIES(c) {
00449 Colours pcolour = (Colours)c->colour;
00450
00451 for (uint i = 0; i < COLOUR_END; i++) {
00452 if (colours[i] == pcolour) {
00453 colours[i] = INVALID_COLOUR;
00454 break;
00455 }
00456 }
00457
00458 for (uint j = 0; j < 2; j++) {
00459 Colours similar = _similar_colour[pcolour][j];
00460 if (similar == INVALID_COLOUR) break;
00461
00462 for (uint i = 1; i < COLOUR_END; i++) {
00463 if (colours[i - 1] == similar) Swap(colours[i - 1], colours[i]);
00464 }
00465 }
00466 }
00467
00468
00469 for (uint i = 0; i < COLOUR_END; i++) {
00470 if (colours[i] != INVALID_COLOUR) return colours[i];
00471 }
00472
00473 NOT_REACHED();
00474 }
00475
00480 static void GeneratePresidentName(Company *c)
00481 {
00482 for (;;) {
00483 restart:;
00484 c->president_name_2 = Random();
00485 c->president_name_1 = SPECSTR_PRESIDENT_NAME;
00486
00487
00488
00489 char buffer[(MAX_LENGTH_PRESIDENT_NAME_CHARS + 1) * MAX_CHAR_LENGTH];
00490 SetDParam(0, c->index);
00491 GetString(buffer, STR_PRESIDENT_NAME, lastof(buffer));
00492 if (Utf8StringLength(buffer) >= MAX_LENGTH_PRESIDENT_NAME_CHARS) continue;
00493
00494 Company *cc;
00495 FOR_ALL_COMPANIES(cc) {
00496 if (c != cc) {
00497
00498 char buffer2[(MAX_LENGTH_PRESIDENT_NAME_CHARS + 1) * MAX_CHAR_LENGTH];
00499 SetDParam(0, cc->index);
00500 GetString(buffer2, STR_PRESIDENT_NAME, lastof(buffer2));
00501 if (strcmp(buffer2, buffer) == 0) goto restart;
00502 }
00503 }
00504 return;
00505 }
00506 }
00507
00513 void ResetCompanyLivery(Company *c)
00514 {
00515 for (LiveryScheme scheme = LS_BEGIN; scheme < LS_END; scheme++) {
00516 c->livery[scheme].in_use = false;
00517 c->livery[scheme].colour1 = c->colour;
00518 c->livery[scheme].colour2 = c->colour;
00519 }
00520 }
00521
00529 Company *DoStartupNewCompany(bool is_ai, CompanyID company = INVALID_COMPANY)
00530 {
00531 if (!Company::CanAllocateItem()) return NULL;
00532
00533
00534 Colours colour = GenerateCompanyColour();
00535
00536 Company *c;
00537 if (company == INVALID_COMPANY) {
00538 c = new Company(STR_SV_UNNAMED, is_ai);
00539 } else {
00540 if (Company::IsValidID(company)) return NULL;
00541 c = new (company) Company(STR_SV_UNNAMED, is_ai);
00542 }
00543
00544 c->colour = colour;
00545
00546 ResetCompanyLivery(c);
00547 _company_colours[c->index] = (Colours)c->colour;
00548
00549 c->money = c->current_loan = (100000ll * _economy.inflation_prices >> 16) / 50000 * 50000;
00550
00551 c->share_owners[0] = c->share_owners[1] = c->share_owners[2] = c->share_owners[3] = INVALID_OWNER;
00552
00553 c->avail_railtypes = GetCompanyRailtypes(c->index);
00554 c->avail_roadtypes = GetCompanyRoadtypes(c->index);
00555 c->inaugurated_year = _cur_year;
00556 RandomCompanyManagerFaceBits(c->face, (GenderEthnicity)Random(), false, false);
00557
00558 SetDefaultCompanySettings(c->index);
00559
00560 GeneratePresidentName(c);
00561
00562 SetWindowDirty(WC_GRAPH_LEGEND, 0);
00563 SetWindowClassesDirty(WC_CLIENT_LIST_POPUP);
00564 SetWindowDirty(WC_CLIENT_LIST, 0);
00565 InvalidateWindowData(WC_LINKGRAPH_LEGEND, 0);
00566 BuildOwnerLegend();
00567 InvalidateWindowData(WC_SMALLMAP, 0, 1);
00568
00569 if (is_ai && (!_networking || _network_server)) AI::StartNew(c->index);
00570
00571 AI::BroadcastNewEvent(new ScriptEventCompanyNew(c->index), c->index);
00572 Game::NewEvent(new ScriptEventCompanyNew(c->index));
00573
00574 return c;
00575 }
00576
00578 void StartupCompanies()
00579 {
00580 _next_competitor_start = 0;
00581 }
00582
00584 static void MaybeStartNewCompany()
00585 {
00586 #ifdef ENABLE_NETWORK
00587 if (_networking && Company::GetNumItems() >= _settings_client.network.max_companies) return;
00588 #endif
00589
00590 Company *c;
00591
00592
00593 uint n = 0;
00594 FOR_ALL_COMPANIES(c) {
00595 if (c->is_ai) n++;
00596 }
00597
00598 if (n < (uint)_settings_game.difficulty.max_no_competitors) {
00599
00600
00601 DoCommandP(0, 1 | INVALID_COMPANY << 16, 0, CMD_COMPANY_CTRL);
00602 }
00603 }
00604
00606 void InitializeCompanies()
00607 {
00608 _cur_company_tick_index = 0;
00609 }
00610
00617 bool MayCompanyTakeOver(CompanyID cbig, CompanyID csmall)
00618 {
00619 const Company *c1 = Company::Get(cbig);
00620 const Company *c2 = Company::Get(csmall);
00621
00622
00623 return c1->group_all[VEH_TRAIN].num_vehicle + c2->group_all[VEH_TRAIN].num_vehicle <= _settings_game.vehicle.max_trains &&
00624 c1->group_all[VEH_ROAD].num_vehicle + c2->group_all[VEH_ROAD].num_vehicle <= _settings_game.vehicle.max_roadveh &&
00625 c1->group_all[VEH_SHIP].num_vehicle + c2->group_all[VEH_SHIP].num_vehicle <= _settings_game.vehicle.max_ships &&
00626 c1->group_all[VEH_AIRCRAFT].num_vehicle + c2->group_all[VEH_AIRCRAFT].num_vehicle <= _settings_game.vehicle.max_aircraft;
00627 }
00628
00638 static void HandleBankruptcyTakeover(Company *c)
00639 {
00640
00641
00642
00643
00644
00645 static const int TAKE_OVER_TIMEOUT = 3 * 30 * DAY_TICKS / (MAX_COMPANIES - 1);
00646
00647 assert(c->bankrupt_asked != 0);
00648
00649
00650 if (c->bankrupt_timeout != 0) {
00651 c->bankrupt_timeout -= MAX_COMPANIES;
00652 if (c->bankrupt_timeout > 0) return;
00653 c->bankrupt_timeout = 0;
00654
00655 return;
00656 }
00657
00658
00659 if (c->bankrupt_asked == MAX_UVALUE(CompanyMask)) return;
00660
00661 Company *c2, *best = NULL;
00662 int32 best_performance = -1;
00663
00664
00665 FOR_ALL_COMPANIES(c2) {
00666 if (c2->bankrupt_asked == 0 &&
00667 !HasBit(c->bankrupt_asked, c2->index) &&
00668 best_performance < c2->old_economy[1].performance_history &&
00669 MayCompanyTakeOver(c2->index, c->index)) {
00670 best_performance = c2->old_economy[1].performance_history;
00671 best = c2;
00672 }
00673 }
00674
00675
00676 if (best_performance == -1) {
00677 c->bankrupt_asked = MAX_UVALUE(CompanyMask);
00678 return;
00679 }
00680
00681 SetBit(c->bankrupt_asked, best->index);
00682
00683 c->bankrupt_timeout = TAKE_OVER_TIMEOUT;
00684 if (best->is_ai) {
00685 AI::NewEvent(best->index, new ScriptEventCompanyAskMerger(c->index, ClampToI32(c->bankrupt_value)));
00686 } else if (IsInteractiveCompany(best->index)) {
00687 ShowBuyCompanyDialog(c->index);
00688 }
00689 }
00690
00692 void OnTick_Companies()
00693 {
00694 if (_game_mode == GM_EDITOR) return;
00695
00696 Company *c = Company::GetIfValid(_cur_company_tick_index);
00697 if (c != NULL) {
00698 if (c->name_1 != 0) GenerateCompanyName(c);
00699 if (c->bankrupt_asked != 0) HandleBankruptcyTakeover(c);
00700 }
00701
00702 if (_next_competitor_start == 0) {
00703 _next_competitor_start = AI::GetStartNextTime() * DAY_TICKS;
00704 }
00705
00706 if (AI::CanStartNew() && _game_mode != GM_MENU && --_next_competitor_start == 0) {
00707 MaybeStartNewCompany();
00708 }
00709
00710 _cur_company_tick_index = (_cur_company_tick_index + 1) % MAX_COMPANIES;
00711 }
00712
00717 void CompaniesYearlyLoop()
00718 {
00719 Company *c;
00720
00721
00722 FOR_ALL_COMPANIES(c) {
00723 memmove(&c->yearly_expenses[1], &c->yearly_expenses[0], sizeof(c->yearly_expenses) - sizeof(c->yearly_expenses[0]));
00724 memset(&c->yearly_expenses[0], 0, sizeof(c->yearly_expenses[0]));
00725 SetWindowDirty(WC_FINANCES, c->index);
00726 }
00727
00728 if (_settings_client.gui.show_finances && _local_company != COMPANY_SPECTATOR) {
00729 ShowCompanyFinances(_local_company);
00730 c = Company::Get(_local_company);
00731 if (c->num_valid_stat_ent > 5 && c->old_economy[0].performance_history < c->old_economy[4].performance_history) {
00732 if (_settings_client.sound.new_year) SndPlayFx(SND_01_BAD_YEAR);
00733 } else {
00734 if (_settings_client.sound.new_year) SndPlayFx(SND_00_GOOD_YEAR);
00735 }
00736 }
00737 }
00738
00744 void CompanyNewsInformation::FillData(const Company *c, const Company *other)
00745 {
00746 SetDParam(0, c->index);
00747 GetString(this->company_name, STR_COMPANY_NAME, lastof(this->company_name));
00748
00749 if (other == NULL) {
00750 *this->other_company_name = '\0';
00751 } else {
00752 SetDParam(0, other->index);
00753 GetString(this->other_company_name, STR_COMPANY_NAME, lastof(this->other_company_name));
00754 c = other;
00755 }
00756
00757 SetDParam(0, c->index);
00758 GetString(this->president_name, STR_PRESIDENT_NAME_MANAGER, lastof(this->president_name));
00759
00760 this->colour = c->colour;
00761 this->face = c->face;
00762
00763 }
00764
00769 void CompanyAdminUpdate(const Company *company)
00770 {
00771 #ifdef ENABLE_NETWORK
00772 if (_network_server) NetworkAdminCompanyUpdate(company);
00773 #endif
00774 }
00775
00781 void CompanyAdminRemove(CompanyID company_id, CompanyRemoveReason reason)
00782 {
00783 #ifdef ENABLE_NETWORK
00784 if (_network_server) NetworkAdminCompanyRemove(company_id, (AdminCompanyRemoveReason)reason);
00785 #endif
00786 }
00787
00802 CommandCost CmdCompanyCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00803 {
00804 InvalidateWindowData(WC_COMPANY_LEAGUE, 0, 0);
00805 CompanyID company_id = (CompanyID)GB(p1, 16, 8);
00806 #ifdef ENABLE_NETWORK
00807 ClientID client_id = (ClientID)p2;
00808 #endif
00809
00810 switch (GB(p1, 0, 16)) {
00811 case 0: {
00812
00813 if (!_networking) return CMD_ERROR;
00814
00815 #ifdef ENABLE_NETWORK
00816
00817 if (!(flags & DC_EXEC)) return CommandCost();
00818 NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id);
00819 #ifndef DEBUG_DUMP_COMMANDS
00820
00821
00822
00823
00824 if (ci == NULL) return CommandCost();
00825 #endif
00826
00827
00828 DeleteWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_JOIN);
00829
00830 Company *c = DoStartupNewCompany(false);
00831
00832
00833 if (c == NULL) {
00834 if (_network_server) {
00835 ci->client_playas = COMPANY_SPECTATOR;
00836 NetworkUpdateClientInfo(ci->client_id);
00837 }
00838 break;
00839 }
00840
00841
00842 if (client_id == _network_own_client_id) {
00843 assert(_local_company == COMPANY_SPECTATOR);
00844 SetLocalCompany(c->index);
00845 if (!StrEmpty(_settings_client.network.default_company_pass)) {
00846 NetworkChangeCompanyPassword(_local_company, _settings_client.network.default_company_pass);
00847 }
00848
00849
00850
00851 SyncCompanySettings();
00852
00853 MarkWholeScreenDirty();
00854 }
00855
00856 NetworkServerNewCompany(c, ci);
00857 #endif
00858 break;
00859 }
00860
00861 case 1: {
00862 if (!(flags & DC_EXEC)) return CommandCost();
00863
00864 if (company_id != INVALID_COMPANY && (company_id >= MAX_COMPANIES || Company::IsValidID(company_id))) return CMD_ERROR;
00865 Company *c = DoStartupNewCompany(true, company_id);
00866 #ifdef ENABLE_NETWORK
00867 if (c != NULL) NetworkServerNewCompany(c, NULL);
00868 #endif
00869 break;
00870 }
00871
00872 case 2: {
00873 CompanyRemoveReason reason = (CompanyRemoveReason)GB(p2, 0, 2);
00874 if (reason >= CRR_END) return CMD_ERROR;
00875
00876 Company *c = Company::GetIfValid(company_id);
00877 if (c == NULL) return CMD_ERROR;
00878
00879 if (!(flags & DC_EXEC)) return CommandCost();
00880
00881
00882 DeleteCompanyWindows(c->index);
00883 CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
00884 cni->FillData(c);
00885
00886
00887 SetDParam(0, STR_NEWS_COMPANY_BANKRUPT_TITLE);
00888 SetDParam(1, STR_NEWS_COMPANY_BANKRUPT_DESCRIPTION);
00889 SetDParamStr(2, cni->company_name);
00890 AddCompanyNewsItem(STR_MESSAGE_NEWS_FORMAT, cni);
00891
00892
00893 ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER);
00894 if (c->is_ai) AI::Stop(c->index);
00895
00896 CompanyID c_index = c->index;
00897 delete c;
00898 AI::BroadcastNewEvent(new ScriptEventCompanyBankrupt(c_index));
00899 Game::NewEvent(new ScriptEventCompanyBankrupt(c_index));
00900 CompanyAdminRemove(c_index, (CompanyRemoveReason)reason);
00901
00902 if (StoryPage::GetNumItems() == 0 || Goal::GetNumItems() == 0) InvalidateWindowData(WC_MAIN_TOOLBAR, 0);
00903 break;
00904 }
00905
00906 default: return CMD_ERROR;
00907 }
00908
00909 InvalidateWindowClassesData(WC_GAME_OPTIONS);
00910 InvalidateWindowClassesData(WC_AI_SETTINGS);
00911 InvalidateWindowClassesData(WC_AI_LIST);
00912
00913 return CommandCost();
00914 }
00915
00925 CommandCost CmdSetCompanyManagerFace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00926 {
00927 CompanyManagerFace cmf = (CompanyManagerFace)p2;
00928
00929 if (!IsValidCompanyManagerFace(cmf)) return CMD_ERROR;
00930
00931 if (flags & DC_EXEC) {
00932 Company::Get(_current_company)->face = cmf;
00933 MarkWholeScreenDirty();
00934 }
00935 return CommandCost();
00936 }
00937
00949 CommandCost CmdSetCompanyColour(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00950 {
00951 Colours colour = Extract<Colours, 0, 4>(p2);
00952 LiveryScheme scheme = Extract<LiveryScheme, 0, 8>(p1);
00953 byte state = GB(p1, 8, 2);
00954
00955 if (scheme >= LS_END || state >= 3 || colour == INVALID_COLOUR) return CMD_ERROR;
00956
00957 Company *c = Company::Get(_current_company);
00958
00959
00960 if (scheme == LS_DEFAULT && state == 0) {
00961 const Company *cc;
00962 FOR_ALL_COMPANIES(cc) {
00963 if (cc != c && cc->colour == colour) return CMD_ERROR;
00964 }
00965 }
00966
00967 if (flags & DC_EXEC) {
00968 switch (state) {
00969 case 0:
00970 c->livery[scheme].colour1 = colour;
00971
00972
00973
00974 if (scheme == LS_DEFAULT) {
00975 _company_colours[_current_company] = colour;
00976 c->colour = colour;
00977 CompanyAdminUpdate(c);
00978 }
00979 break;
00980
00981 case 1:
00982 c->livery[scheme].colour2 = colour;
00983 break;
00984
00985 case 2:
00986 c->livery[scheme].in_use = colour != 0;
00987
00988
00989
00990
00991
00992
00993
00994
00995 if (colour != 0) {
00996 c->livery[LS_DEFAULT].in_use = true;
00997 break;
00998 }
00999
01000
01001
01002 c->livery[LS_DEFAULT].in_use = false;
01003 for (scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
01004 if (c->livery[scheme].in_use) {
01005 c->livery[LS_DEFAULT].in_use = true;
01006 break;
01007 }
01008 }
01009 break;
01010
01011 default:
01012 break;
01013 }
01014 ResetVehicleColourMap();
01015 MarkWholeScreenDirty();
01016
01017
01018 InvalidateWindowData(WC_INCOME_GRAPH, 0);
01019 InvalidateWindowData(WC_OPERATING_PROFIT, 0);
01020 InvalidateWindowData(WC_DELIVERED_CARGO, 0);
01021 InvalidateWindowData(WC_PERFORMANCE_HISTORY, 0);
01022 InvalidateWindowData(WC_COMPANY_VALUE, 0);
01023 InvalidateWindowData(WC_LINKGRAPH_LEGEND, 0);
01024
01025 BuildOwnerLegend();
01026 InvalidateWindowData(WC_SMALLMAP, 0, 1);
01027
01028
01029 Vehicle *v;
01030 FOR_ALL_VEHICLES(v) {
01031 if (v->owner == _current_company) v->InvalidateNewGRFCache();
01032 }
01033
01034 extern void UpdateObjectColours(const Company *c);
01035 UpdateObjectColours(c);
01036 }
01037 return CommandCost();
01038 }
01039
01045 static bool IsUniqueCompanyName(const char *name)
01046 {
01047 const Company *c;
01048
01049 FOR_ALL_COMPANIES(c) {
01050 if (c->name != NULL && strcmp(c->name, name) == 0) return false;
01051 }
01052
01053 return true;
01054 }
01055
01065 CommandCost CmdRenameCompany(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01066 {
01067 bool reset = StrEmpty(text);
01068
01069 if (!reset) {
01070 if (Utf8StringLength(text) >= MAX_LENGTH_COMPANY_NAME_CHARS) return CMD_ERROR;
01071 if (!IsUniqueCompanyName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
01072 }
01073
01074 if (flags & DC_EXEC) {
01075 Company *c = Company::Get(_current_company);
01076 free(c->name);
01077 c->name = reset ? NULL : strdup(text);
01078 MarkWholeScreenDirty();
01079 CompanyAdminUpdate(c);
01080 }
01081
01082 return CommandCost();
01083 }
01084
01090 static bool IsUniquePresidentName(const char *name)
01091 {
01092 const Company *c;
01093
01094 FOR_ALL_COMPANIES(c) {
01095 if (c->president_name != NULL && strcmp(c->president_name, name) == 0) return false;
01096 }
01097
01098 return true;
01099 }
01100
01110 CommandCost CmdRenamePresident(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01111 {
01112 bool reset = StrEmpty(text);
01113
01114 if (!reset) {
01115 if (Utf8StringLength(text) >= MAX_LENGTH_PRESIDENT_NAME_CHARS) return CMD_ERROR;
01116 if (!IsUniquePresidentName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
01117 }
01118
01119 if (flags & DC_EXEC) {
01120 Company *c = Company::Get(_current_company);
01121 free(c->president_name);
01122
01123 if (reset) {
01124 c->president_name = NULL;
01125 } else {
01126 c->president_name = strdup(text);
01127
01128 if (c->name_1 == STR_SV_UNNAMED && c->name == NULL) {
01129 char buf[80];
01130
01131 snprintf(buf, lengthof(buf), "%s Transport", text);
01132 DoCommand(0, 0, 0, DC_EXEC, CMD_RENAME_COMPANY, buf);
01133 }
01134 }
01135
01136 MarkWholeScreenDirty();
01137 CompanyAdminUpdate(c);
01138 }
01139
01140 return CommandCost();
01141 }
01142
01149 int CompanyServiceInterval(const Company *c, VehicleType type)
01150 {
01151 const VehicleDefaultSettings *vds = (c == NULL) ? &_settings_client.company.vehicle : &c->settings.vehicle;
01152 switch (type) {
01153 default: NOT_REACHED();
01154 case VEH_TRAIN: return vds->servint_trains;
01155 case VEH_ROAD: return vds->servint_roadveh;
01156 case VEH_AIRCRAFT: return vds->servint_aircraft;
01157 case VEH_SHIP: return vds->servint_ships;
01158 }
01159 }