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
00036 #include "table/strings.h"
00037
00038 CompanyByte _local_company;
00039 CompanyByte _current_company;
00040
00041 Colours _company_colours[MAX_COMPANIES];
00042 CompanyManagerFace _company_manager_face;
00043 uint _next_competitor_start;
00044 uint _cur_company_tick_index;
00045
00046 CompanyPool _company_pool("Company");
00047 INSTANTIATE_POOL_METHODS(Company)
00048
00049 Company::Company(uint16 name_1, bool is_ai) :
00050 name_1(name_1),
00051 location_of_HQ(INVALID_TILE),
00052 is_ai(is_ai)
00053 {
00054 for (uint j = 0; j < 4; j++) this->share_owners[j] = COMPANY_SPECTATOR;
00055 InvalidateWindowData(WC_PERFORMANCE_DETAIL, 0, INVALID_COMPANY);
00056 }
00057
00058 Company::~Company()
00059 {
00060 free(this->name);
00061 free(this->president_name);
00062 free(this->num_engines);
00063
00064 if (CleaningPool()) return;
00065
00066 DeleteCompanyWindows(this->index);
00067 }
00068
00073 void Company::PostDestructor(size_t index)
00074 {
00075 InvalidateWindowData(WC_GRAPH_LEGEND, 0, (int)index);
00076 InvalidateWindowData(WC_PERFORMANCE_DETAIL, 0, (int)index);
00077 InvalidateWindowData(WC_COMPANY_LEAGUE, 0, 0);
00078 }
00079
00086 void SetLocalCompany(CompanyID new_company)
00087 {
00088
00089 assert(Company::IsValidID(new_company) || new_company == COMPANY_SPECTATOR || new_company == OWNER_NONE);
00090
00091 #ifdef ENABLE_NETWORK
00092
00093 InvalidateWindowData(WC_SEND_NETWORK_MSG, DESTTYPE_TEAM, _local_company);
00094 #endif
00095
00096 _local_company = new_company;
00097
00098
00099 DeleteConstructionWindows();
00100
00101
00102 MarkWholeScreenDirty();
00103 }
00104
00105 uint16 GetDrawStringCompanyColour(CompanyID company)
00106 {
00107
00108
00109 if (!Company::IsValidID(company)) return _colour_gradient[COLOUR_WHITE][4] | IS_PALETTE_COLOUR;
00110 return (_colour_gradient[_company_colours[company]][4]) | IS_PALETTE_COLOUR;
00111 }
00112
00113 void DrawCompanyIcon(CompanyID c, int x, int y)
00114 {
00115 DrawSprite(SPR_COMPANY_ICON, COMPANY_SPRITE_COLOUR(c), x, y);
00116 }
00117
00124 bool IsValidCompanyManagerFace(CompanyManagerFace cmf)
00125 {
00126 if (!AreCompanyManagerFaceBitsValid(cmf, CMFV_GEN_ETHN, GE_WM)) return false;
00127
00128 GenderEthnicity ge = (GenderEthnicity)GetCompanyManagerFaceBits(cmf, CMFV_GEN_ETHN, GE_WM);
00129 bool has_moustache = !HasBit(ge, GENDER_FEMALE) && GetCompanyManagerFaceBits(cmf, CMFV_HAS_MOUSTACHE, ge) != 0;
00130 bool has_tie_earring = !HasBit(ge, GENDER_FEMALE) || GetCompanyManagerFaceBits(cmf, CMFV_HAS_TIE_EARRING, ge) != 0;
00131 bool has_glasses = GetCompanyManagerFaceBits(cmf, CMFV_HAS_GLASSES, ge) != 0;
00132
00133 if (!AreCompanyManagerFaceBitsValid(cmf, CMFV_EYE_COLOUR, ge)) return false;
00134 for (CompanyManagerFaceVariable cmfv = CMFV_CHEEKS; cmfv < CMFV_END; cmfv++) {
00135 switch (cmfv) {
00136 case CMFV_MOUSTACHE: if (!has_moustache) continue; break;
00137 case CMFV_LIPS:
00138 case CMFV_NOSE: if (has_moustache) continue; break;
00139 case CMFV_TIE_EARRING: if (!has_tie_earring) continue; break;
00140 case CMFV_GLASSES: if (!has_glasses) continue; break;
00141 default: break;
00142 }
00143 if (!AreCompanyManagerFaceBitsValid(cmf, cmfv, ge)) return false;
00144 }
00145
00146 return true;
00147 }
00148
00149 void InvalidateCompanyWindows(const Company *company)
00150 {
00151 CompanyID cid = company->index;
00152
00153 if (cid == _local_company) SetWindowDirty(WC_STATUS_BAR, 0);
00154 SetWindowDirty(WC_FINANCES, cid);
00155 }
00156
00157 bool CheckCompanyHasMoney(CommandCost cost)
00158 {
00159 if (cost.GetCost() > 0) {
00160 const Company *c = Company::GetIfValid(_current_company);
00161 if (c != NULL && cost.GetCost() > c->money) {
00162 SetDParam(0, cost.GetCost());
00163 _error_message = STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY;
00164 return false;
00165 }
00166 }
00167 return true;
00168 }
00169
00170 static void SubtractMoneyFromAnyCompany(Company *c, CommandCost cost)
00171 {
00172 if (cost.GetCost() == 0) return;
00173 assert(cost.GetExpensesType() != INVALID_EXPENSES);
00174
00175 c->money -= cost.GetCost();
00176 c->yearly_expenses[0][cost.GetExpensesType()] += cost.GetCost();
00177
00178 if (HasBit(1 << EXPENSES_TRAIN_INC |
00179 1 << EXPENSES_ROADVEH_INC |
00180 1 << EXPENSES_AIRCRAFT_INC |
00181 1 << EXPENSES_SHIP_INC, cost.GetExpensesType())) {
00182 c->cur_economy.income -= cost.GetCost();
00183 } else if (HasBit(1 << EXPENSES_TRAIN_RUN |
00184 1 << EXPENSES_ROADVEH_RUN |
00185 1 << EXPENSES_AIRCRAFT_RUN |
00186 1 << EXPENSES_SHIP_RUN |
00187 1 << EXPENSES_PROPERTY |
00188 1 << EXPENSES_LOAN_INT, cost.GetExpensesType())) {
00189 c->cur_economy.expenses -= cost.GetCost();
00190 }
00191
00192 InvalidateCompanyWindows(c);
00193 }
00194
00195 void SubtractMoneyFromCompany(CommandCost cost)
00196 {
00197 Company *c = Company::GetIfValid(_current_company);
00198 if (c != NULL) SubtractMoneyFromAnyCompany(c, cost);
00199 }
00200
00201 void SubtractMoneyFromCompanyFract(CompanyID company, CommandCost cst)
00202 {
00203 Company *c = Company::Get(company);
00204 byte m = c->money_fraction;
00205 Money cost = cst.GetCost();
00206
00207 c->money_fraction = m - (byte)cost;
00208 cost >>= 8;
00209 if (c->money_fraction > m) cost++;
00210 if (cost != 0) SubtractMoneyFromAnyCompany(c, CommandCost(cst.GetExpensesType(), cost));
00211 }
00212
00213 void GetNameOfOwner(Owner owner, TileIndex tile)
00214 {
00215 SetDParam(2, owner);
00216
00217 if (owner != OWNER_TOWN) {
00218 if (!Company::IsValidID(owner)) {
00219 SetDParam(0, STR_COMPANY_SOMEONE);
00220 } else {
00221 SetDParam(0, STR_COMPANY_NAME);
00222 SetDParam(1, owner);
00223 }
00224 } else {
00225 const Town *t = ClosestTownFromTile(tile, UINT_MAX);
00226
00227 SetDParam(0, STR_TOWN_NAME);
00228 SetDParam(1, t->index);
00229 }
00230 }
00231
00232
00233 bool CheckOwnership(Owner owner)
00234 {
00235 assert(owner < OWNER_END);
00236
00237 if (owner == _current_company) return true;
00238 _error_message = STR_ERROR_OWNED_BY;
00239 GetNameOfOwner(owner, 0);
00240 return false;
00241 }
00242
00243 bool CheckTileOwnership(TileIndex tile)
00244 {
00245 Owner owner = GetTileOwner(tile);
00246
00247 assert(owner < OWNER_END);
00248
00249 if (owner == _current_company) return true;
00250 _error_message = STR_ERROR_OWNED_BY;
00251
00252
00253 if (IsLocalCompany()) GetNameOfOwner(owner, tile);
00254 return false;
00255 }
00256
00257 static void GenerateCompanyName(Company *c)
00258 {
00259 TileIndex tile;
00260 Town *t;
00261 StringID str;
00262 Company *cc;
00263 uint32 strp;
00264
00265
00266 char buffer[MAX_LENGTH_COMPANY_NAME_BYTES + MAX_CHAR_LENGTH];
00267
00268 if (c->name_1 != STR_SV_UNNAMED) return;
00269
00270 tile = c->last_build_coordinate;
00271 if (tile == 0) return;
00272
00273 t = ClosestTownFromTile(tile, UINT_MAX);
00274
00275 if (t->name == NULL && IsInsideMM(t->townnametype, SPECSTR_TOWNNAME_START, SPECSTR_TOWNNAME_LAST + 1)) {
00276 str = t->townnametype - SPECSTR_TOWNNAME_START + SPECSTR_PLAYERNAME_START;
00277 strp = t->townnameparts;
00278
00279 verify_name:;
00280
00281 FOR_ALL_COMPANIES(cc) {
00282 if (cc->name_1 == str && cc->name_2 == strp) goto bad_town_name;
00283 }
00284
00285 GetString(buffer, str, lastof(buffer));
00286 if (strlen(buffer) >= MAX_LENGTH_COMPANY_NAME_BYTES) goto bad_town_name;
00287
00288 set_name:;
00289 c->name_1 = str;
00290 c->name_2 = strp;
00291
00292 MarkWholeScreenDirty();
00293
00294 if (c->is_ai) {
00295 CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
00296 cni->FillData(c);
00297 SetDParam(0, STR_NEWS_COMPANY_LAUNCH_TITLE);
00298 SetDParam(1, STR_NEWS_COMPANY_LAUNCH_DESCRIPTION);
00299 SetDParamStr(2, cni->company_name);
00300 SetDParam(3, t->index);
00301 AddNewsItem(STR_MESSAGE_NEWS_FORMAT, NS_COMPANY_NEW, NR_TILE, c->last_build_coordinate, NR_NONE, UINT32_MAX, cni);
00302 }
00303 AI::BroadcastNewEvent(new AIEventCompanyNew(c->index), c->index);
00304 return;
00305 }
00306 bad_town_name:;
00307
00308 if (c->president_name_1 == SPECSTR_PRESIDENT_NAME) {
00309 str = SPECSTR_ANDCO_NAME;
00310 strp = c->president_name_2;
00311 goto set_name;
00312 } else {
00313 str = SPECSTR_ANDCO_NAME;
00314 strp = Random();
00315 goto verify_name;
00316 }
00317 }
00318
00319 static const byte _colour_sort[COLOUR_END] = {2, 2, 3, 2, 3, 2, 3, 2, 3, 2, 2, 2, 3, 1, 1, 1};
00320 static const Colours _similar_colour[COLOUR_END][2] = {
00321 { COLOUR_BLUE, COLOUR_LIGHT_BLUE },
00322 { COLOUR_GREEN, COLOUR_DARK_GREEN },
00323 { INVALID_COLOUR, INVALID_COLOUR },
00324 { COLOUR_ORANGE, INVALID_COLOUR },
00325 { INVALID_COLOUR, INVALID_COLOUR },
00326 { COLOUR_DARK_BLUE, COLOUR_BLUE },
00327 { COLOUR_PALE_GREEN, COLOUR_DARK_GREEN },
00328 { COLOUR_PALE_GREEN, COLOUR_GREEN },
00329 { COLOUR_DARK_BLUE, COLOUR_LIGHT_BLUE },
00330 { COLOUR_BROWN, COLOUR_ORANGE },
00331 { COLOUR_PURPLE, INVALID_COLOUR },
00332 { COLOUR_MAUVE, INVALID_COLOUR },
00333 { COLOUR_YELLOW, COLOUR_CREAM },
00334 { COLOUR_CREAM, INVALID_COLOUR },
00335 { COLOUR_WHITE, INVALID_COLOUR },
00336 { COLOUR_GREY, INVALID_COLOUR },
00337 };
00338
00339 static Colours GenerateCompanyColour()
00340 {
00341 Colours colours[COLOUR_END];
00342
00343
00344 for (uint i = 0; i < COLOUR_END; i++) colours[i] = (Colours)i;
00345
00346
00347 for (uint i = 0; i < 100; i++) {
00348 uint r = Random();
00349 Swap(colours[GB(r, 0, 4)], colours[GB(r, 4, 4)]);
00350 }
00351
00352
00353 for (uint i = 0; i < COLOUR_END; i++) {
00354 for (uint j = 1; j < COLOUR_END; j++) {
00355 if (_colour_sort[colours[j - 1]] < _colour_sort[colours[j]]) {
00356 Swap(colours[j - 1], colours[j]);
00357 }
00358 }
00359 };
00360
00361
00362 Company *c;
00363 FOR_ALL_COMPANIES(c) {
00364 Colours pcolour = (Colours)c->colour;
00365
00366 for (uint i = 0; i < COLOUR_END; i++) {
00367 if (colours[i] == pcolour) {
00368 colours[i] = INVALID_COLOUR;
00369 break;
00370 }
00371 }
00372
00373 for (uint j = 0; j < 2; j++) {
00374 Colours similar = _similar_colour[pcolour][j];
00375 if (similar == INVALID_COLOUR) break;
00376
00377 for (uint i = 1; i < COLOUR_END; i++) {
00378 if (colours[i - 1] == similar) Swap(colours[i - 1], colours[i]);
00379 }
00380 }
00381 }
00382
00383
00384 for (uint i = 0; i < COLOUR_END; i++) {
00385 if (colours[i] != INVALID_COLOUR) return colours[i];
00386 }
00387
00388 NOT_REACHED();
00389 }
00390
00391 static void GeneratePresidentName(Company *c)
00392 {
00393 for (;;) {
00394 restart:;
00395 c->president_name_2 = Random();
00396 c->president_name_1 = SPECSTR_PRESIDENT_NAME;
00397
00398
00399
00400 char buffer[MAX_LENGTH_PRESIDENT_NAME_BYTES + MAX_CHAR_LENGTH];
00401 SetDParam(0, c->index);
00402 GetString(buffer, STR_PRESIDENT_NAME, lastof(buffer));
00403 if (strlen(buffer) >= MAX_LENGTH_PRESIDENT_NAME_BYTES) continue;
00404
00405 Company *cc;
00406 FOR_ALL_COMPANIES(cc) {
00407 if (c != cc) {
00408
00409 char buffer2[MAX_LENGTH_PRESIDENT_NAME_BYTES + MAX_CHAR_LENGTH];
00410 SetDParam(0, cc->index);
00411 GetString(buffer2, STR_PRESIDENT_NAME, lastof(buffer2));
00412 if (strcmp(buffer2, buffer) == 0) goto restart;
00413 }
00414 }
00415 return;
00416 }
00417 }
00418
00419 void ResetCompanyLivery(Company *c)
00420 {
00421 for (LiveryScheme scheme = LS_BEGIN; scheme < LS_END; scheme++) {
00422 c->livery[scheme].in_use = false;
00423 c->livery[scheme].colour1 = c->colour;
00424 c->livery[scheme].colour2 = c->colour;
00425 }
00426 }
00427
00435 Company *DoStartupNewCompany(bool is_ai, CompanyID company = INVALID_COMPANY)
00436 {
00437 if (!Company::CanAllocateItem()) return NULL;
00438
00439
00440 Colours colour = GenerateCompanyColour();
00441
00442 Company *c;
00443 if (company == INVALID_COMPANY) {
00444 c = new Company(STR_SV_UNNAMED, is_ai);
00445 } else {
00446 if (Company::IsValidID(company)) return NULL;
00447 c = new (company) Company(STR_SV_UNNAMED, is_ai);
00448 }
00449
00450 c->colour = colour;
00451
00452 ResetCompanyLivery(c);
00453 _company_colours[c->index] = (Colours)c->colour;
00454
00455 c->money = c->current_loan = 100000;
00456
00457 c->share_owners[0] = c->share_owners[1] = c->share_owners[2] = c->share_owners[3] = INVALID_OWNER;
00458
00459 c->avail_railtypes = GetCompanyRailtypes(c->index);
00460 c->avail_roadtypes = GetCompanyRoadtypes(c->index);
00461 c->inaugurated_year = _cur_year;
00462 RandomCompanyManagerFaceBits(c->face, (GenderEthnicity)Random(), false);
00463
00464 SetDefaultCompanySettings(c->index);
00465
00466 GeneratePresidentName(c);
00467
00468 SetWindowDirty(WC_GRAPH_LEGEND, 0);
00469 SetWindowDirty(WC_TOOLBAR_MENU, 0);
00470 SetWindowDirty(WC_CLIENT_LIST, 0);
00471
00472 if (is_ai && (!_networking || _network_server)) AI::StartNew(c->index);
00473
00474 c->num_engines = CallocT<uint16>(Engine::GetPoolSize());
00475
00476 return c;
00477 }
00478
00479 void StartupCompanies()
00480 {
00481 _next_competitor_start = 0;
00482 }
00483
00484 static void MaybeStartNewCompany()
00485 {
00486 #ifdef ENABLE_NETWORK
00487 if (_networking && Company::GetNumItems() >= _settings_client.network.max_companies) return;
00488 #endif
00489
00490 Company *c;
00491
00492
00493 uint n = 0;
00494 FOR_ALL_COMPANIES(c) {
00495 if (c->is_ai) n++;
00496 }
00497
00498 if (n < (uint)_settings_game.difficulty.max_no_competitors) {
00499
00500
00501 DoCommandP(0, 1, INVALID_COMPANY, CMD_COMPANY_CTRL);
00502 }
00503 }
00504
00505 void InitializeCompanies()
00506 {
00507 _company_pool.CleanPool();
00508 _cur_company_tick_index = 0;
00509 }
00510
00520 static void HandleBankruptcyTakeover(Company *c)
00521 {
00522
00523
00524
00525
00526
00527 static const int TAKE_OVER_TIMEOUT = 3 * 30 * DAY_TICKS / (MAX_COMPANIES - 1);
00528
00529 assert(c->bankrupt_asked != 0);
00530
00531
00532 if (c->bankrupt_timeout != 0) {
00533 c->bankrupt_timeout -= MAX_COMPANIES;
00534 if (c->bankrupt_timeout > 0) return;
00535 c->bankrupt_timeout = 0;
00536
00537 return;
00538 }
00539
00540
00541 if (c->bankrupt_asked == MAX_UVALUE(CompanyMask)) return;
00542
00543 Company *c2, *best = NULL;
00544 int32 best_performance = -1;
00545
00546
00547 FOR_ALL_COMPANIES(c2) {
00548 if (c2->bankrupt_asked == 0 &&
00549 !HasBit(c->bankrupt_asked, c2->index) &&
00550 best_performance < c2->old_economy[1].performance_history) {
00551 best_performance = c2->old_economy[1].performance_history;
00552 best = c2;
00553 }
00554 }
00555
00556
00557 if (best_performance == -1) {
00558 c->bankrupt_asked = MAX_UVALUE(CompanyMask);
00559 return;
00560 }
00561
00562 SetBit(c->bankrupt_asked, best->index);
00563
00564 if (IsInteractiveCompany(best->index)) {
00565 c->bankrupt_timeout = TAKE_OVER_TIMEOUT;
00566 ShowBuyCompanyDialog(c->index);
00567 return;
00568 }
00569
00570 if (best->is_ai) {
00571 AI::NewEvent(best->index, new AIEventCompanyAskMerger(c->index, ClampToI32(c->bankrupt_value)));
00572 }
00573 }
00574
00575 void OnTick_Companies()
00576 {
00577 if (_game_mode == GM_EDITOR) return;
00578
00579 Company *c = Company::GetIfValid(_cur_company_tick_index);
00580 if (c != NULL) {
00581 if (c->name_1 != 0) GenerateCompanyName(c);
00582 if (c->bankrupt_asked != 0) HandleBankruptcyTakeover(c);
00583 }
00584
00585 if (_next_competitor_start == 0) {
00586 _next_competitor_start = AI::GetStartNextTime() * DAY_TICKS;
00587 }
00588
00589 if (AI::CanStartNew() && _game_mode != GM_MENU && --_next_competitor_start == 0) {
00590 MaybeStartNewCompany();
00591 }
00592
00593 _cur_company_tick_index = (_cur_company_tick_index + 1) % MAX_COMPANIES;
00594 }
00595
00596 void CompaniesYearlyLoop()
00597 {
00598 Company *c;
00599
00600
00601 FOR_ALL_COMPANIES(c) {
00602 memmove(&c->yearly_expenses[1], &c->yearly_expenses[0], sizeof(c->yearly_expenses) - sizeof(c->yearly_expenses[0]));
00603 memset(&c->yearly_expenses[0], 0, sizeof(c->yearly_expenses[0]));
00604 SetWindowDirty(WC_FINANCES, c->index);
00605 }
00606
00607 if (_settings_client.gui.show_finances && _local_company != COMPANY_SPECTATOR) {
00608 ShowCompanyFinances(_local_company);
00609 c = Company::Get(_local_company);
00610 if (c->num_valid_stat_ent > 5 && c->old_economy[0].performance_history < c->old_economy[4].performance_history) {
00611 SndPlayFx(SND_01_BAD_YEAR);
00612 } else {
00613 SndPlayFx(SND_00_GOOD_YEAR);
00614 }
00615 }
00616 }
00617
00629 CommandCost CmdSetAutoReplace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00630 {
00631 Company *c = Company::GetIfValid(_current_company);
00632 if (c == NULL) return CMD_ERROR;
00633
00634 EngineID old_engine_type = GB(p2, 0, 16);
00635 EngineID new_engine_type = GB(p2, 16, 16);
00636 GroupID id_g = GB(p1, 16, 16);
00637 CommandCost cost;
00638
00639 if (!Group::IsValidID(id_g) && !IsAllGroupID(id_g) && !IsDefaultGroupID(id_g)) return CMD_ERROR;
00640 if (new_engine_type != INVALID_ENGINE) {
00641 if (!CheckAutoreplaceValidity(old_engine_type, new_engine_type, _current_company)) return CMD_ERROR;
00642
00643 cost = AddEngineReplacementForCompany(c, old_engine_type, new_engine_type, id_g, flags);
00644 } else {
00645 cost = RemoveEngineReplacementForCompany(c, old_engine_type, id_g, flags);
00646 }
00647
00648 if (IsLocalCompany()) InvalidateAutoreplaceWindow(old_engine_type, id_g);
00649
00650 return cost;
00651 }
00652
00658 void CompanyNewsInformation::FillData(const Company *c, const Company *other)
00659 {
00660 SetDParam(0, c->index);
00661 GetString(this->company_name, STR_COMPANY_NAME, lastof(this->company_name));
00662
00663 if (other == NULL) {
00664 *this->other_company_name = '\0';
00665 } else {
00666 SetDParam(0, other->index);
00667 GetString(this->other_company_name, STR_COMPANY_NAME, lastof(this->other_company_name));
00668 c = other;
00669 }
00670
00671 SetDParam(0, c->index);
00672 GetString(this->president_name, STR_PRESIDENT_NAME_MANAGER, lastof(this->president_name));
00673
00674 this->colour = c->colour;
00675 this->face = c->face;
00676
00677 }
00678
00700 CommandCost CmdCompanyCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00701 {
00702 if (flags & DC_EXEC) _current_company = OWNER_NONE;
00703
00704 InvalidateWindowData(WC_COMPANY_LEAGUE, 0, 0);
00705
00706 switch (p1) {
00707 case 0: {
00708
00709 if (!_networking) return CMD_ERROR;
00710
00711 #ifdef ENABLE_NETWORK
00712
00713
00714
00715
00716
00717
00718
00719
00720 ClientID cid = (ClientID)p2;
00721
00722
00723 if (!(flags & DC_EXEC)) return CommandCost();
00724 NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(cid);
00725 if (ci == NULL) return CommandCost();
00726
00727
00728 DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
00729
00730 Company *c = DoStartupNewCompany(false);
00731
00732
00733 if (c == NULL) {
00734 if (_network_server) {
00735 ci->client_playas = COMPANY_SPECTATOR;
00736 NetworkUpdateClientInfo(ci->client_id);
00737 }
00738 break;
00739 }
00740
00741
00742 if (cid == _network_own_client_id) {
00743 assert(_local_company == COMPANY_SPECTATOR);
00744 SetLocalCompany(c->index);
00745 if (!StrEmpty(_settings_client.network.default_company_pass)) {
00746 char *password = _settings_client.network.default_company_pass;
00747 NetworkChangeCompanyPassword(1, &password);
00748 }
00749
00750 _current_company = _local_company;
00751
00752
00753
00754 SyncCompanySettings();
00755
00756 MarkWholeScreenDirty();
00757 }
00758
00759 if (_network_server) {
00760
00761
00762
00763 CompanyID old_playas = ci->client_playas;
00764 ci->client_playas = c->index;
00765 NetworkUpdateClientInfo(ci->client_id);
00766
00767 if (Company::IsValidID(ci->client_playas)) {
00768 CompanyID company_backup = _local_company;
00769 _network_company_states[c->index].months_empty = 0;
00770 _network_company_states[c->index].password[0] = '\0';
00771 NetworkServerUpdateCompanyPassworded(ci->client_playas, false);
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784 _local_company = ci->client_playas;
00785 NetworkSend_Command(0, 0, 0, CMD_RENAME_PRESIDENT, NULL, ci->client_name);
00786 _local_company = company_backup;
00787 }
00788
00789
00790 if (old_playas == COMPANY_SPECTATOR) {
00791 NetworkServerSendChat(NETWORK_ACTION_COMPANY_NEW, DESTTYPE_BROADCAST, 0, "", ci->client_id, ci->client_playas + 1);
00792 }
00793 }
00794 #endif
00795 } break;
00796
00797 case 1:
00798 if (!(flags & DC_EXEC)) return CommandCost();
00799
00800 if (p2 != INVALID_COMPANY && (p2 >= MAX_COMPANIES || Company::IsValidID(p2))) return CMD_ERROR;
00801 DoStartupNewCompany(true, (CompanyID)p2);
00802 break;
00803
00804 case 2: {
00805 Company *c = Company::GetIfValid(p2);
00806 if (c == NULL) return CMD_ERROR;
00807
00808 if (!(flags & DC_EXEC)) return CommandCost();
00809
00810
00811 DeleteCompanyWindows(c->index);
00812 CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
00813 cni->FillData(c);
00814
00815
00816 SetDParam(0, STR_NEWS_COMPANY_BANKRUPT_TITLE);
00817 SetDParam(1, STR_NEWS_COMPANY_BANKRUPT_DESCRIPTION);
00818 SetDParamStr(2, cni->company_name);
00819 AddCompanyNewsItem(STR_MESSAGE_NEWS_FORMAT, NS_COMPANY_BANKRUPT, cni);
00820
00821
00822 ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER);
00823 if (c->is_ai) AI::Stop(c->index);
00824
00825 CompanyID c_index = c->index;
00826 delete c;
00827 AI::BroadcastNewEvent(new AIEventCompanyBankrupt(c_index));
00828 } break;
00829
00830 default: return CMD_ERROR;
00831 }
00832
00833 return CommandCost();
00834 }