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