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