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 }
00067
00069 Company::~Company()
00070 {
00071 free(this->num_engines);
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 }
00118
00124 TextColour GetDrawStringCompanyColour(CompanyID company)
00125 {
00126 if (!Company::IsValidID(company)) return (TextColour)_colour_gradient[COLOUR_WHITE][4] | TC_IS_PALETTE_COLOUR;
00127 return (TextColour)_colour_gradient[_company_colours[company]][4] | TC_IS_PALETTE_COLOUR;
00128 }
00129
00136 void DrawCompanyIcon(CompanyID c, int x, int y)
00137 {
00138 DrawSprite(SPR_COMPANY_ICON, COMPANY_SPRITE_COLOUR(c), x, y);
00139 }
00140
00147 static bool IsValidCompanyManagerFace(CompanyManagerFace cmf)
00148 {
00149 if (!AreCompanyManagerFaceBitsValid(cmf, CMFV_GEN_ETHN, GE_WM)) return false;
00150
00151 GenderEthnicity ge = (GenderEthnicity)GetCompanyManagerFaceBits(cmf, CMFV_GEN_ETHN, GE_WM);
00152 bool has_moustache = !HasBit(ge, GENDER_FEMALE) && GetCompanyManagerFaceBits(cmf, CMFV_HAS_MOUSTACHE, ge) != 0;
00153 bool has_tie_earring = !HasBit(ge, GENDER_FEMALE) || GetCompanyManagerFaceBits(cmf, CMFV_HAS_TIE_EARRING, ge) != 0;
00154 bool has_glasses = GetCompanyManagerFaceBits(cmf, CMFV_HAS_GLASSES, ge) != 0;
00155
00156 if (!AreCompanyManagerFaceBitsValid(cmf, CMFV_EYE_COLOUR, ge)) return false;
00157 for (CompanyManagerFaceVariable cmfv = CMFV_CHEEKS; cmfv < CMFV_END; cmfv++) {
00158 switch (cmfv) {
00159 case CMFV_MOUSTACHE: if (!has_moustache) continue; break;
00160 case CMFV_LIPS:
00161 case CMFV_NOSE: if (has_moustache) continue; break;
00162 case CMFV_TIE_EARRING: if (!has_tie_earring) continue; break;
00163 case CMFV_GLASSES: if (!has_glasses) continue; break;
00164 default: break;
00165 }
00166 if (!AreCompanyManagerFaceBitsValid(cmf, cmfv, ge)) return false;
00167 }
00168
00169 return true;
00170 }
00171
00176 void InvalidateCompanyWindows(const Company *company)
00177 {
00178 CompanyID cid = company->index;
00179
00180 if (cid == _local_company) SetWindowDirty(WC_STATUS_BAR, 0);
00181 SetWindowDirty(WC_FINANCES, cid);
00182 }
00183
00189 bool CheckCompanyHasMoney(CommandCost &cost)
00190 {
00191 if (cost.GetCost() > 0) {
00192 const Company *c = Company::GetIfValid(_current_company);
00193 if (c != NULL && cost.GetCost() > c->money) {
00194 SetDParam(0, cost.GetCost());
00195 cost.MakeError(STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY);
00196 return false;
00197 }
00198 }
00199 return true;
00200 }
00201
00207 static void SubtractMoneyFromAnyCompany(Company *c, CommandCost cost)
00208 {
00209 if (cost.GetCost() == 0) return;
00210 assert(cost.GetExpensesType() != INVALID_EXPENSES);
00211
00212 c->money -= cost.GetCost();
00213 c->yearly_expenses[0][cost.GetExpensesType()] += cost.GetCost();
00214
00215 if (HasBit(1 << EXPENSES_TRAIN_INC |
00216 1 << EXPENSES_ROADVEH_INC |
00217 1 << EXPENSES_AIRCRAFT_INC |
00218 1 << EXPENSES_SHIP_INC, cost.GetExpensesType())) {
00219 c->cur_economy.income -= cost.GetCost();
00220 } else if (HasBit(1 << EXPENSES_TRAIN_RUN |
00221 1 << EXPENSES_ROADVEH_RUN |
00222 1 << EXPENSES_AIRCRAFT_RUN |
00223 1 << EXPENSES_SHIP_RUN |
00224 1 << EXPENSES_PROPERTY |
00225 1 << EXPENSES_LOAN_INT, cost.GetExpensesType())) {
00226 c->cur_economy.expenses -= cost.GetCost();
00227 }
00228
00229 InvalidateCompanyWindows(c);
00230 }
00231
00236 void SubtractMoneyFromCompany(CommandCost cost)
00237 {
00238 Company *c = Company::GetIfValid(_current_company);
00239 if (c != NULL) SubtractMoneyFromAnyCompany(c, cost);
00240 }
00241
00247 void SubtractMoneyFromCompanyFract(CompanyID company, CommandCost cst)
00248 {
00249 Company *c = Company::Get(company);
00250 byte m = c->money_fraction;
00251 Money cost = cst.GetCost();
00252
00253 c->money_fraction = m - (byte)cost;
00254 cost >>= 8;
00255 if (c->money_fraction > m) cost++;
00256 if (cost != 0) SubtractMoneyFromAnyCompany(c, CommandCost(cst.GetExpensesType(), cost));
00257 }
00258
00260 void UpdateLandscapingLimits()
00261 {
00262 Company *c;
00263 FOR_ALL_COMPANIES(c) {
00264 c->terraform_limit = min(c->terraform_limit + _settings_game.construction.terraform_per_64k_frames, (uint32)_settings_game.construction.terraform_frame_burst << 16);
00265 c->clear_limit = min(c->clear_limit + _settings_game.construction.clear_per_64k_frames, (uint32)_settings_game.construction.clear_frame_burst << 16);
00266 }
00267 }
00268
00275 void GetNameOfOwner(Owner owner, TileIndex tile)
00276 {
00277 SetDParam(2, owner);
00278
00279 if (owner != OWNER_TOWN) {
00280 if (!Company::IsValidID(owner)) {
00281 SetDParam(0, STR_COMPANY_SOMEONE);
00282 } else {
00283 SetDParam(0, STR_COMPANY_NAME);
00284 SetDParam(1, owner);
00285 }
00286 } else {
00287 assert(tile != 0);
00288 const Town *t = ClosestTownFromTile(tile, UINT_MAX);
00289
00290 SetDParam(0, STR_TOWN_NAME);
00291 SetDParam(1, t->index);
00292 }
00293 }
00294
00295
00304 CommandCost CheckOwnership(Owner owner, TileIndex tile)
00305 {
00306 assert(owner < OWNER_END);
00307 assert(owner != OWNER_TOWN || tile != 0);
00308
00309 if (owner == _current_company) return CommandCost();
00310
00311 GetNameOfOwner(owner, tile);
00312 return_cmd_error(STR_ERROR_OWNED_BY);
00313 }
00314
00322 CommandCost CheckTileOwnership(TileIndex tile)
00323 {
00324 Owner owner = GetTileOwner(tile);
00325
00326 assert(owner < OWNER_END);
00327
00328 if (owner == _current_company) return CommandCost();
00329
00330
00331 if (IsLocalCompany()) GetNameOfOwner(owner, tile);
00332 return_cmd_error(STR_ERROR_OWNED_BY);
00333 }
00334
00339 static void GenerateCompanyName(Company *c)
00340 {
00341
00342
00343 char buffer[(MAX_LENGTH_COMPANY_NAME_CHARS + 1) * MAX_CHAR_LENGTH];
00344
00345 if (c->name_1 != STR_SV_UNNAMED) return;
00346 if (c->last_build_coordinate == 0) return;
00347
00348 Town *t = ClosestTownFromTile(c->last_build_coordinate, UINT_MAX);
00349
00350 StringID str;
00351 uint32 strp;
00352 if (t->name == NULL && IsInsideMM(t->townnametype, SPECSTR_TOWNNAME_START, SPECSTR_TOWNNAME_LAST + 1)) {
00353 str = t->townnametype - SPECSTR_TOWNNAME_START + SPECSTR_PLAYERNAME_START;
00354 strp = t->townnameparts;
00355
00356 verify_name:;
00357
00358 Company *cc;
00359 FOR_ALL_COMPANIES(cc) {
00360 if (cc->name_1 == str && cc->name_2 == strp) goto bad_town_name;
00361 }
00362
00363 GetString(buffer, str, lastof(buffer));
00364 if (Utf8StringLength(buffer) >= MAX_LENGTH_COMPANY_NAME_CHARS) goto bad_town_name;
00365
00366 set_name:;
00367 c->name_1 = str;
00368 c->name_2 = strp;
00369
00370 MarkWholeScreenDirty();
00371
00372 if (c->is_ai) {
00373 CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
00374 cni->FillData(c);
00375 SetDParam(0, STR_NEWS_COMPANY_LAUNCH_TITLE);
00376 SetDParam(1, STR_NEWS_COMPANY_LAUNCH_DESCRIPTION);
00377 SetDParamStr(2, cni->company_name);
00378 SetDParam(3, t->index);
00379 AddNewsItem(STR_MESSAGE_NEWS_FORMAT, NS_COMPANY_NEW, NR_TILE, c->last_build_coordinate, NR_NONE, UINT32_MAX, cni);
00380 }
00381 AI::BroadcastNewEvent(new AIEventCompanyNew(c->index), c->index);
00382 return;
00383 }
00384 bad_town_name:;
00385
00386 if (c->president_name_1 == SPECSTR_PRESIDENT_NAME) {
00387 str = SPECSTR_ANDCO_NAME;
00388 strp = c->president_name_2;
00389 goto set_name;
00390 } else {
00391 str = SPECSTR_ANDCO_NAME;
00392 strp = Random();
00393 goto verify_name;
00394 }
00395 }
00396
00398 static const byte _colour_sort[COLOUR_END] = {2, 2, 3, 2, 3, 2, 3, 2, 3, 2, 2, 2, 3, 1, 1, 1};
00400 static const Colours _similar_colour[COLOUR_END][2] = {
00401 { COLOUR_BLUE, COLOUR_LIGHT_BLUE },
00402 { COLOUR_GREEN, COLOUR_DARK_GREEN },
00403 { INVALID_COLOUR, INVALID_COLOUR },
00404 { COLOUR_ORANGE, INVALID_COLOUR },
00405 { INVALID_COLOUR, INVALID_COLOUR },
00406 { COLOUR_DARK_BLUE, COLOUR_BLUE },
00407 { COLOUR_PALE_GREEN, COLOUR_DARK_GREEN },
00408 { COLOUR_PALE_GREEN, COLOUR_GREEN },
00409 { COLOUR_DARK_BLUE, COLOUR_LIGHT_BLUE },
00410 { COLOUR_BROWN, COLOUR_ORANGE },
00411 { COLOUR_PURPLE, INVALID_COLOUR },
00412 { COLOUR_MAUVE, INVALID_COLOUR },
00413 { COLOUR_YELLOW, COLOUR_CREAM },
00414 { COLOUR_CREAM, INVALID_COLOUR },
00415 { COLOUR_WHITE, INVALID_COLOUR },
00416 { COLOUR_GREY, INVALID_COLOUR },
00417 };
00418
00423 static Colours GenerateCompanyColour()
00424 {
00425 Colours colours[COLOUR_END];
00426
00427
00428 for (uint i = 0; i < COLOUR_END; i++) colours[i] = (Colours)i;
00429
00430
00431 for (uint i = 0; i < 100; i++) {
00432 uint r = Random();
00433 Swap(colours[GB(r, 0, 4)], colours[GB(r, 4, 4)]);
00434 }
00435
00436
00437 for (uint i = 0; i < COLOUR_END; i++) {
00438 for (uint j = 1; j < COLOUR_END; j++) {
00439 if (_colour_sort[colours[j - 1]] < _colour_sort[colours[j]]) {
00440 Swap(colours[j - 1], colours[j]);
00441 }
00442 }
00443 }
00444
00445
00446 Company *c;
00447 FOR_ALL_COMPANIES(c) {
00448 Colours pcolour = (Colours)c->colour;
00449
00450 for (uint i = 0; i < COLOUR_END; i++) {
00451 if (colours[i] == pcolour) {
00452 colours[i] = INVALID_COLOUR;
00453 break;
00454 }
00455 }
00456
00457 for (uint j = 0; j < 2; j++) {
00458 Colours similar = _similar_colour[pcolour][j];
00459 if (similar == INVALID_COLOUR) break;
00460
00461 for (uint i = 1; i < COLOUR_END; i++) {
00462 if (colours[i - 1] == similar) Swap(colours[i - 1], colours[i]);
00463 }
00464 }
00465 }
00466
00467
00468 for (uint i = 0; i < COLOUR_END; i++) {
00469 if (colours[i] != INVALID_COLOUR) return colours[i];
00470 }
00471
00472 NOT_REACHED();
00473 }
00474
00479 static void GeneratePresidentName(Company *c)
00480 {
00481 for (;;) {
00482 restart:;
00483 c->president_name_2 = Random();
00484 c->president_name_1 = SPECSTR_PRESIDENT_NAME;
00485
00486
00487
00488 char buffer[(MAX_LENGTH_PRESIDENT_NAME_CHARS + 1) * MAX_CHAR_LENGTH];
00489 SetDParam(0, c->index);
00490 GetString(buffer, STR_PRESIDENT_NAME, lastof(buffer));
00491 if (Utf8StringLength(buffer) >= MAX_LENGTH_PRESIDENT_NAME_CHARS) continue;
00492
00493 Company *cc;
00494 FOR_ALL_COMPANIES(cc) {
00495 if (c != cc) {
00496
00497 char buffer2[(MAX_LENGTH_PRESIDENT_NAME_CHARS + 1) * MAX_CHAR_LENGTH];
00498 SetDParam(0, cc->index);
00499 GetString(buffer2, STR_PRESIDENT_NAME, lastof(buffer2));
00500 if (strcmp(buffer2, buffer) == 0) goto restart;
00501 }
00502 }
00503 return;
00504 }
00505 }
00506
00512 void ResetCompanyLivery(Company *c)
00513 {
00514 for (LiveryScheme scheme = LS_BEGIN; scheme < LS_END; scheme++) {
00515 c->livery[scheme].in_use = false;
00516 c->livery[scheme].colour1 = c->colour;
00517 c->livery[scheme].colour2 = c->colour;
00518 }
00519 }
00520
00528 Company *DoStartupNewCompany(bool is_ai, CompanyID company = INVALID_COMPANY)
00529 {
00530 if (!Company::CanAllocateItem()) return NULL;
00531
00532
00533 Colours colour = GenerateCompanyColour();
00534
00535 Company *c;
00536 if (company == INVALID_COMPANY) {
00537 c = new Company(STR_SV_UNNAMED, is_ai);
00538 } else {
00539 if (Company::IsValidID(company)) return NULL;
00540 c = new (company) Company(STR_SV_UNNAMED, is_ai);
00541 }
00542
00543 c->colour = colour;
00544
00545 ResetCompanyLivery(c);
00546 _company_colours[c->index] = (Colours)c->colour;
00547
00548 c->money = c->current_loan = (100000ll * _economy.inflation_prices >> 16) / 50000 * 50000;
00549
00550 c->share_owners[0] = c->share_owners[1] = c->share_owners[2] = c->share_owners[3] = INVALID_OWNER;
00551
00552 c->avail_railtypes = GetCompanyRailtypes(c->index);
00553 c->avail_roadtypes = GetCompanyRoadtypes(c->index);
00554 c->inaugurated_year = _cur_year;
00555 RandomCompanyManagerFaceBits(c->face, (GenderEthnicity)Random(), false);
00556
00557 SetDefaultCompanySettings(c->index);
00558
00559 GeneratePresidentName(c);
00560
00561 SetWindowDirty(WC_GRAPH_LEGEND, 0);
00562 SetWindowClassesDirty(WC_CLIENT_LIST_POPUP);
00563 SetWindowDirty(WC_CLIENT_LIST, 0);
00564 InvalidateWindowData(WC_LINKGRAPH_LEGEND, 0);
00565 BuildOwnerLegend();
00566 InvalidateWindowData(WC_SMALLMAP, 0, 1);
00567
00568 if (is_ai && (!_networking || _network_server)) AI::StartNew(c->index);
00569
00570 c->num_engines = CallocT<uint16>(Engine::GetPoolSize());
00571
00572 return c;
00573 }
00574
00576 void StartupCompanies()
00577 {
00578 _next_competitor_start = 0;
00579 }
00580
00581 #ifdef ENABLE_AI
00582
00583 static void MaybeStartNewCompany()
00584 {
00585 #ifdef ENABLE_NETWORK
00586 if (_networking && Company::GetNumItems() >= _settings_client.network.max_companies) return;
00587 #endif
00588
00589 Company *c;
00590
00591
00592 uint n = 0;
00593 FOR_ALL_COMPANIES(c) {
00594 if (c->is_ai) n++;
00595 }
00596
00597 if (n < (uint)_settings_game.difficulty.max_no_competitors) {
00598
00599
00600 DoCommandP(0, 1 | INVALID_COMPANY << 16, 0, CMD_COMPANY_CTRL);
00601 }
00602 }
00603 #endif
00604
00606 void InitializeCompanies()
00607 {
00608 _cur_company_tick_index = 0;
00609 }
00610
00617 bool MayCompanyTakeOver(CompanyID cbig, CompanyID csmall)
00618 {
00619 uint big_counts[4], small_counts[4];
00620 CountCompanyVehicles(cbig, big_counts);
00621 CountCompanyVehicles(csmall, small_counts);
00622
00623
00624 return big_counts[VEH_TRAIN] + small_counts[VEH_TRAIN] <= _settings_game.vehicle.max_trains &&
00625 big_counts[VEH_ROAD] + small_counts[VEH_ROAD] <= _settings_game.vehicle.max_roadveh &&
00626 big_counts[VEH_SHIP] + small_counts[VEH_SHIP] <= _settings_game.vehicle.max_ships &&
00627 big_counts[VEH_AIRCRAFT] + small_counts[VEH_AIRCRAFT] <= _settings_game.vehicle.max_aircraft;
00628 }
00629
00639 static void HandleBankruptcyTakeover(Company *c)
00640 {
00641
00642
00643
00644
00645
00646 static const int TAKE_OVER_TIMEOUT = 3 * 30 * DAY_TICKS / (MAX_COMPANIES - 1);
00647
00648 assert(c->bankrupt_asked != 0);
00649
00650
00651 if (c->bankrupt_timeout != 0) {
00652 c->bankrupt_timeout -= MAX_COMPANIES;
00653 if (c->bankrupt_timeout > 0) return;
00654 c->bankrupt_timeout = 0;
00655
00656 return;
00657 }
00658
00659
00660 if (c->bankrupt_asked == MAX_UVALUE(CompanyMask)) return;
00661
00662 Company *c2, *best = NULL;
00663 int32 best_performance = -1;
00664
00665
00666 FOR_ALL_COMPANIES(c2) {
00667 if (c2->bankrupt_asked == 0 &&
00668 !HasBit(c->bankrupt_asked, c2->index) &&
00669 best_performance < c2->old_economy[1].performance_history &&
00670 MayCompanyTakeOver(c2->index, c->index)) {
00671 best_performance = c2->old_economy[1].performance_history;
00672 best = c2;
00673 }
00674 }
00675
00676
00677 if (best_performance == -1) {
00678 c->bankrupt_asked = MAX_UVALUE(CompanyMask);
00679 return;
00680 }
00681
00682 SetBit(c->bankrupt_asked, best->index);
00683
00684 c->bankrupt_timeout = TAKE_OVER_TIMEOUT;
00685 if (best->is_ai) {
00686 AI::NewEvent(best->index, new AIEventCompanyAskMerger(c->index, ClampToI32(c->bankrupt_value)));
00687 } else if (IsInteractiveCompany(best->index)) {
00688 ShowBuyCompanyDialog(c->index);
00689 }
00690 }
00691
00693 void OnTick_Companies()
00694 {
00695 if (_game_mode == GM_EDITOR) return;
00696
00697 Company *c = Company::GetIfValid(_cur_company_tick_index);
00698 if (c != NULL) {
00699 if (c->name_1 != 0) GenerateCompanyName(c);
00700 if (c->bankrupt_asked != 0) HandleBankruptcyTakeover(c);
00701 }
00702
00703 #ifdef ENABLE_AI
00704 if (_next_competitor_start == 0) {
00705 _next_competitor_start = AI::GetStartNextTime() * DAY_TICKS;
00706 }
00707
00708 if (AI::CanStartNew() && _game_mode != GM_MENU && --_next_competitor_start == 0) {
00709 MaybeStartNewCompany();
00710 }
00711 #endif
00712
00713 _cur_company_tick_index = (_cur_company_tick_index + 1) % MAX_COMPANIES;
00714 }
00715
00720 void CompaniesYearlyLoop()
00721 {
00722 Company *c;
00723
00724
00725 FOR_ALL_COMPANIES(c) {
00726 memmove(&c->yearly_expenses[1], &c->yearly_expenses[0], sizeof(c->yearly_expenses) - sizeof(c->yearly_expenses[0]));
00727 memset(&c->yearly_expenses[0], 0, sizeof(c->yearly_expenses[0]));
00728 SetWindowDirty(WC_FINANCES, c->index);
00729 }
00730
00731 if (_settings_client.gui.show_finances && _local_company != COMPANY_SPECTATOR) {
00732 ShowCompanyFinances(_local_company);
00733 c = Company::Get(_local_company);
00734 if (c->num_valid_stat_ent > 5 && c->old_economy[0].performance_history < c->old_economy[4].performance_history) {
00735 SndPlayFx(SND_01_BAD_YEAR);
00736 } else {
00737 SndPlayFx(SND_00_GOOD_YEAR);
00738 }
00739 }
00740 }
00741
00747 void CompanyNewsInformation::FillData(const Company *c, const Company *other)
00748 {
00749 SetDParam(0, c->index);
00750 GetString(this->company_name, STR_COMPANY_NAME, lastof(this->company_name));
00751
00752 if (other == NULL) {
00753 *this->other_company_name = '\0';
00754 } else {
00755 SetDParam(0, other->index);
00756 GetString(this->other_company_name, STR_COMPANY_NAME, lastof(this->other_company_name));
00757 c = other;
00758 }
00759
00760 SetDParam(0, c->index);
00761 GetString(this->president_name, STR_PRESIDENT_NAME_MANAGER, lastof(this->president_name));
00762
00763 this->colour = c->colour;
00764 this->face = c->face;
00765
00766 }
00767
00772 void CompanyAdminUpdate(const Company *company)
00773 {
00774 #ifdef ENABLE_NETWORK
00775 if (_network_server) NetworkAdminCompanyUpdate(company);
00776 #endif
00777 }
00778
00783 void CompanyAdminBankrupt(CompanyID company_id)
00784 {
00785 #ifdef ENABLE_NETWORK
00786 if (_network_server) NetworkAdminCompanyRemove(company_id, ADMIN_CRR_BANKRUPT);
00787 #endif
00788 }
00789
00804 CommandCost CmdCompanyCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00805 {
00806 InvalidateWindowData(WC_COMPANY_LEAGUE, 0, 0);
00807 CompanyID company_id = (CompanyID)GB(p1, 16, 8);
00808 #ifdef ENABLE_NETWORK
00809 ClientID client_id = (ClientID)p2;
00810 #endif
00811
00812 switch (GB(p1, 0, 16)) {
00813 case 0: {
00814
00815 if (!_networking) return CMD_ERROR;
00816
00817 #ifdef ENABLE_NETWORK
00818
00819 if (!(flags & DC_EXEC)) return CommandCost();
00820 NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id);
00821 if (ci == NULL) return CommandCost();
00822
00823
00824 DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
00825
00826 Company *c = DoStartupNewCompany(false);
00827
00828
00829 if (c == NULL) {
00830 if (_network_server) {
00831 ci->client_playas = COMPANY_SPECTATOR;
00832 NetworkUpdateClientInfo(ci->client_id);
00833 }
00834 break;
00835 }
00836
00837
00838 if (client_id == _network_own_client_id) {
00839 assert(_local_company == COMPANY_SPECTATOR);
00840 SetLocalCompany(c->index);
00841 if (!StrEmpty(_settings_client.network.default_company_pass)) {
00842 NetworkChangeCompanyPassword(_local_company, _settings_client.network.default_company_pass);
00843 }
00844
00845
00846
00847 SyncCompanySettings();
00848
00849 MarkWholeScreenDirty();
00850 }
00851
00852 if (_network_server) {
00853 CompanyID old_playas = ci->client_playas;
00854 ci->client_playas = c->index;
00855 NetworkUpdateClientInfo(ci->client_id);
00856
00857 if (Company::IsValidID(ci->client_playas)) {
00858 _network_company_states[c->index].months_empty = 0;
00859 _network_company_states[c->index].password[0] = '\0';
00860 NetworkServerUpdateCompanyPassworded(ci->client_playas, false);
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873 NetworkSendCommand(0, 0, 0, CMD_RENAME_PRESIDENT, NULL, ci->client_name, ci->client_playas);
00874 }
00875
00876
00877 if (old_playas == COMPANY_SPECTATOR) {
00878 NetworkAdminCompanyInfo(c, true);
00879 NetworkServerSendChat(NETWORK_ACTION_COMPANY_NEW, DESTTYPE_BROADCAST, 0, "", ci->client_id, ci->client_playas + 1);
00880 }
00881 }
00882 #endif
00883 break;
00884 }
00885
00886 case 1:
00887 if (!(flags & DC_EXEC)) return CommandCost();
00888
00889 if (company_id != INVALID_COMPANY && (company_id >= MAX_COMPANIES || Company::IsValidID(company_id))) return CMD_ERROR;
00890 DoStartupNewCompany(true, company_id);
00891 break;
00892
00893 case 2: {
00894 Company *c = Company::GetIfValid(company_id);
00895 if (c == NULL) return CMD_ERROR;
00896
00897 if (!(flags & DC_EXEC)) return CommandCost();
00898
00899
00900 DeleteCompanyWindows(c->index);
00901 CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
00902 cni->FillData(c);
00903
00904
00905 SetDParam(0, STR_NEWS_COMPANY_BANKRUPT_TITLE);
00906 SetDParam(1, STR_NEWS_COMPANY_BANKRUPT_DESCRIPTION);
00907 SetDParamStr(2, cni->company_name);
00908 AddCompanyNewsItem(STR_MESSAGE_NEWS_FORMAT, NS_COMPANY_BANKRUPT, cni);
00909
00910
00911 ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER);
00912 if (c->is_ai) AI::Stop(c->index);
00913
00914 CompanyID c_index = c->index;
00915 delete c;
00916 AI::BroadcastNewEvent(new AIEventCompanyBankrupt(c_index));
00917 CompanyAdminBankrupt(c_index);
00918 break;
00919 }
00920
00921 default: return CMD_ERROR;
00922 }
00923
00924 InvalidateWindowClassesData(WC_GAME_OPTIONS);
00925 InvalidateWindowClassesData(WC_AI_SETTINGS);
00926 InvalidateWindowClassesData(WC_AI_LIST);
00927
00928 return CommandCost();
00929 }
00930
00940 CommandCost CmdSetCompanyManagerFace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00941 {
00942 CompanyManagerFace cmf = (CompanyManagerFace)p2;
00943
00944 if (!IsValidCompanyManagerFace(cmf)) return CMD_ERROR;
00945
00946 if (flags & DC_EXEC) {
00947 Company::Get(_current_company)->face = cmf;
00948 MarkWholeScreenDirty();
00949 }
00950 return CommandCost();
00951 }
00952
00964 CommandCost CmdSetCompanyColour(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00965 {
00966 Colours colour = Extract<Colours, 0, 4>(p2);
00967 LiveryScheme scheme = Extract<LiveryScheme, 0, 8>(p1);
00968 byte state = GB(p1, 8, 2);
00969
00970 if (scheme >= LS_END || state >= 3 || colour == INVALID_COLOUR) return CMD_ERROR;
00971
00972 Company *c = Company::Get(_current_company);
00973
00974
00975 if (scheme == LS_DEFAULT && state == 0) {
00976 const Company *cc;
00977 FOR_ALL_COMPANIES(cc) {
00978 if (cc != c && cc->colour == colour) return CMD_ERROR;
00979 }
00980 }
00981
00982 if (flags & DC_EXEC) {
00983 switch (state) {
00984 case 0:
00985 c->livery[scheme].colour1 = colour;
00986
00987
00988
00989 if (scheme == LS_DEFAULT) {
00990 _company_colours[_current_company] = colour;
00991 c->colour = colour;
00992 CompanyAdminUpdate(c);
00993 }
00994 break;
00995
00996 case 1:
00997 c->livery[scheme].colour2 = colour;
00998 break;
00999
01000 case 2:
01001 c->livery[scheme].in_use = colour != 0;
01002
01003
01004
01005
01006
01007
01008
01009
01010 if (colour != 0) {
01011 c->livery[LS_DEFAULT].in_use = true;
01012 break;
01013 }
01014
01015
01016
01017 c->livery[LS_DEFAULT].in_use = false;
01018 for (scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
01019 if (c->livery[scheme].in_use) {
01020 c->livery[LS_DEFAULT].in_use = true;
01021 break;
01022 }
01023 }
01024 break;
01025
01026 default:
01027 break;
01028 }
01029 ResetVehicleColourMap();
01030 MarkWholeScreenDirty();
01031
01032
01033 InvalidateWindowData(WC_INCOME_GRAPH, 0);
01034 InvalidateWindowData(WC_OPERATING_PROFIT, 0);
01035 InvalidateWindowData(WC_DELIVERED_CARGO, 0);
01036 InvalidateWindowData(WC_PERFORMANCE_HISTORY, 0);
01037 InvalidateWindowData(WC_COMPANY_VALUE, 0);
01038 InvalidateWindowData(WC_LINKGRAPH_LEGEND, 0);
01039
01040 BuildOwnerLegend();
01041 InvalidateWindowData(WC_SMALLMAP, 0, 1);
01042
01043
01044 Vehicle *v;
01045 FOR_ALL_VEHICLES(v) {
01046 if (v->owner == _current_company) v->InvalidateNewGRFCache();
01047 }
01048
01049 extern void UpdateObjectColours(const Company *c);
01050 UpdateObjectColours(c);
01051 }
01052 return CommandCost();
01053 }
01054
01060 static bool IsUniqueCompanyName(const char *name)
01061 {
01062 const Company *c;
01063
01064 FOR_ALL_COMPANIES(c) {
01065 if (c->name != NULL && strcmp(c->name, name) == 0) return false;
01066 }
01067
01068 return true;
01069 }
01070
01080 CommandCost CmdRenameCompany(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01081 {
01082 bool reset = StrEmpty(text);
01083
01084 if (!reset) {
01085 if (Utf8StringLength(text) >= MAX_LENGTH_COMPANY_NAME_CHARS) return CMD_ERROR;
01086 if (!IsUniqueCompanyName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
01087 }
01088
01089 if (flags & DC_EXEC) {
01090 Company *c = Company::Get(_current_company);
01091 free(c->name);
01092 c->name = reset ? NULL : strdup(text);
01093 MarkWholeScreenDirty();
01094 CompanyAdminUpdate(c);
01095 }
01096
01097 return CommandCost();
01098 }
01099
01105 static bool IsUniquePresidentName(const char *name)
01106 {
01107 const Company *c;
01108
01109 FOR_ALL_COMPANIES(c) {
01110 if (c->president_name != NULL && strcmp(c->president_name, name) == 0) return false;
01111 }
01112
01113 return true;
01114 }
01115
01125 CommandCost CmdRenamePresident(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01126 {
01127 bool reset = StrEmpty(text);
01128
01129 if (!reset) {
01130 if (Utf8StringLength(text) >= MAX_LENGTH_PRESIDENT_NAME_CHARS) return CMD_ERROR;
01131 if (!IsUniquePresidentName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
01132 }
01133
01134 if (flags & DC_EXEC) {
01135 Company *c = Company::Get(_current_company);
01136 free(c->president_name);
01137
01138 if (reset) {
01139 c->president_name = NULL;
01140 } else {
01141 c->president_name = strdup(text);
01142
01143 if (c->name_1 == STR_SV_UNNAMED && c->name == NULL) {
01144 char buf[80];
01145
01146 snprintf(buf, lengthof(buf), "%s Transport", text);
01147 DoCommand(0, 0, 0, DC_EXEC, CMD_RENAME_COMPANY, buf);
01148 }
01149 }
01150
01151 MarkWholeScreenDirty();
01152 CompanyAdminUpdate(c);
01153 }
01154
01155 return CommandCost();
01156 }