00001
00002
00005 #include "stdafx.h"
00006 #include "openttd.h"
00007 #include "engine_base.h"
00008 #include "company_func.h"
00009 #include "company_gui.h"
00010 #include "town.h"
00011 #include "news_func.h"
00012 #include "command_func.h"
00013 #include "network/network.h"
00014 #include "network/network_func.h"
00015 #include "network/network_base.h"
00016 #include "ai/ai.hpp"
00017 #include "company_manager_face.h"
00018 #include "group.h"
00019 #include "window_func.h"
00020 #include "tile_map.h"
00021 #include "strings_func.h"
00022 #include "gfx_func.h"
00023 #include "date_func.h"
00024 #include "sound_func.h"
00025 #include "autoreplace_func.h"
00026 #include "autoreplace_gui.h"
00027 #include "string_func.h"
00028 #include "road_func.h"
00029 #include "rail.h"
00030 #include "sprite.h"
00031 #include "oldpool_func.h"
00032
00033 #include "table/strings.h"
00034
00035 CompanyByte _local_company;
00036 CompanyByte _current_company;
00037
00038 Colours _company_colours[MAX_COMPANIES];
00039 CompanyManagerFace _company_manager_face;
00040 uint _next_competitor_start;
00041 uint _cur_company_tick_index;
00042
00043 DEFINE_OLD_POOL_GENERIC(Company, Company)
00044
00045 Company::Company(uint16 name_1, bool is_ai) :
00046 name_1(name_1),
00047 location_of_HQ(INVALID_TILE),
00048 is_ai(is_ai)
00049 {
00050 for (uint j = 0; j < 4; j++) this->share_owners[j] = COMPANY_SPECTATOR;
00051 }
00052
00053 Company::~Company()
00054 {
00055 free(this->name);
00056 free(this->president_name);
00057 free(this->num_engines);
00058
00059 if (CleaningPool()) return;
00060
00061 DeleteCompanyWindows(this->index);
00062 this->name_1 = 0;
00063 }
00064
00071 void SetLocalCompany(CompanyID new_company)
00072 {
00073
00074 assert(IsValidCompanyID(new_company) || new_company == COMPANY_SPECTATOR || new_company == OWNER_NONE);
00075
00076 _local_company = new_company;
00077
00078
00079 if (IsValidCompanyID(new_company) && _game_mode != GM_MENU) {
00080 const Company *c = GetCompany(new_company);
00081 _settings_client.gui.autorenew = c->engine_renew;
00082 _settings_client.gui.autorenew_months = c->engine_renew_months;
00083 _settings_client.gui.autorenew_money = c->engine_renew_money;
00084 InvalidateWindow(WC_GAME_OPTIONS, 0);
00085 }
00086
00087
00088 DeleteConstructionWindows();
00089
00090
00091 MarkWholeScreenDirty();
00092 }
00093
00094 bool IsHumanCompany(CompanyID company)
00095 {
00096 return !GetCompany(company)->is_ai;
00097 }
00098
00099
00100 uint16 GetDrawStringCompanyColour(CompanyID company)
00101 {
00102
00103
00104 if (!IsValidCompanyID(company)) return _colour_gradient[COLOUR_WHITE][4] | IS_PALETTE_COLOUR;
00105 return (_colour_gradient[_company_colours[company]][4]) | IS_PALETTE_COLOUR;
00106 }
00107
00108 void DrawCompanyIcon(CompanyID c, int x, int y)
00109 {
00110 DrawSprite(SPR_PLAYER_ICON, COMPANY_SPRITE_COLOUR(c), x, y);
00111 }
00112
00119 bool IsValidCompanyManagerFace(CompanyManagerFace cmf)
00120 {
00121 if (!AreCompanyManagerFaceBitsValid(cmf, CMFV_GEN_ETHN, GE_WM)) return false;
00122
00123 GenderEthnicity ge = (GenderEthnicity)GetCompanyManagerFaceBits(cmf, CMFV_GEN_ETHN, GE_WM);
00124 bool has_moustache = !HasBit(ge, GENDER_FEMALE) && GetCompanyManagerFaceBits(cmf, CMFV_HAS_MOUSTACHE, ge) != 0;
00125 bool has_tie_earring = !HasBit(ge, GENDER_FEMALE) || GetCompanyManagerFaceBits(cmf, CMFV_HAS_TIE_EARRING, ge) != 0;
00126 bool has_glasses = GetCompanyManagerFaceBits(cmf, CMFV_HAS_GLASSES, ge) != 0;
00127
00128 if (!AreCompanyManagerFaceBitsValid(cmf, CMFV_EYE_COLOUR, ge)) return false;
00129 for (CompanyManagerFaceVariable cmfv = CMFV_CHEEKS; cmfv < CMFV_END; cmfv++) {
00130 switch (cmfv) {
00131 case CMFV_MOUSTACHE: if (!has_moustache) continue; break;
00132 case CMFV_LIPS:
00133 case CMFV_NOSE: if (has_moustache) continue; break;
00134 case CMFV_TIE_EARRING: if (!has_tie_earring) continue; break;
00135 case CMFV_GLASSES: if (!has_glasses) continue; break;
00136 default: break;
00137 }
00138 if (!AreCompanyManagerFaceBitsValid(cmf, cmfv, ge)) return false;
00139 }
00140
00141 return true;
00142 }
00143
00144 void InvalidateCompanyWindows(const Company *company)
00145 {
00146 CompanyID cid = company->index;
00147
00148 if (cid == _local_company) InvalidateWindow(WC_STATUS_BAR, 0);
00149 InvalidateWindow(WC_FINANCES, cid);
00150 }
00151
00152 bool CheckCompanyHasMoney(CommandCost cost)
00153 {
00154 if (cost.GetCost() > 0) {
00155 CompanyID company = _current_company;
00156 if (IsValidCompanyID(company) && cost.GetCost() > GetCompany(company)->money) {
00157 SetDParam(0, cost.GetCost());
00158 _error_message = STR_0003_NOT_ENOUGH_CASH_REQUIRES;
00159 return false;
00160 }
00161 }
00162 return true;
00163 }
00164
00165 static void SubtractMoneyFromAnyCompany(Company *c, CommandCost cost)
00166 {
00167 if (cost.GetCost() == 0) return;
00168 assert(cost.GetExpensesType() != INVALID_EXPENSES);
00169
00170 c->money -= cost.GetCost();
00171 c->yearly_expenses[0][cost.GetExpensesType()] += cost.GetCost();
00172
00173 if (HasBit(1 << EXPENSES_TRAIN_INC |
00174 1 << EXPENSES_ROADVEH_INC |
00175 1 << EXPENSES_AIRCRAFT_INC |
00176 1 << EXPENSES_SHIP_INC, cost.GetExpensesType())) {
00177 c->cur_economy.income -= cost.GetCost();
00178 } else if (HasBit(1 << EXPENSES_TRAIN_RUN |
00179 1 << EXPENSES_ROADVEH_RUN |
00180 1 << EXPENSES_AIRCRAFT_RUN |
00181 1 << EXPENSES_SHIP_RUN |
00182 1 << EXPENSES_PROPERTY |
00183 1 << EXPENSES_LOAN_INT, cost.GetExpensesType())) {
00184 c->cur_economy.expenses -= cost.GetCost();
00185 }
00186
00187 InvalidateCompanyWindows(c);
00188 }
00189
00190 void SubtractMoneyFromCompany(CommandCost cost)
00191 {
00192 CompanyID cid = _current_company;
00193
00194 if (IsValidCompanyID(cid)) SubtractMoneyFromAnyCompany(GetCompany(cid), cost);
00195 }
00196
00197 void SubtractMoneyFromCompanyFract(CompanyID company, CommandCost cst)
00198 {
00199 Company *c = GetCompany(company);
00200 byte m = c->money_fraction;
00201 Money cost = cst.GetCost();
00202
00203 c->money_fraction = m - (byte)cost;
00204 cost >>= 8;
00205 if (c->money_fraction > m) cost++;
00206 if (cost != 0) SubtractMoneyFromAnyCompany(c, CommandCost(cst.GetExpensesType(), cost));
00207 }
00208
00209 void GetNameOfOwner(Owner owner, TileIndex tile)
00210 {
00211 SetDParam(2, owner);
00212
00213 if (owner != OWNER_TOWN) {
00214 if (!IsValidCompanyID(owner)) {
00215 SetDParam(0, STR_0150_SOMEONE);
00216 } else {
00217 SetDParam(0, STR_COMPANY_NAME);
00218 SetDParam(1, owner);
00219 }
00220 } else {
00221 const Town *t = ClosestTownFromTile(tile, UINT_MAX);
00222
00223 SetDParam(0, STR_TOWN);
00224 SetDParam(1, t->index);
00225 }
00226 }
00227
00228
00229 bool CheckOwnership(Owner owner)
00230 {
00231 assert(owner < OWNER_END);
00232
00233 if (owner == _current_company) return true;
00234 _error_message = STR_013B_OWNED_BY;
00235 GetNameOfOwner(owner, 0);
00236 return false;
00237 }
00238
00239 bool CheckTileOwnership(TileIndex tile)
00240 {
00241 Owner owner = GetTileOwner(tile);
00242
00243 assert(owner < OWNER_END);
00244
00245 if (owner == _current_company) return true;
00246 _error_message = STR_013B_OWNED_BY;
00247
00248
00249 if (IsLocalCompany()) GetNameOfOwner(owner, tile);
00250 return false;
00251 }
00252
00253 static void GenerateCompanyName(Company *c)
00254 {
00255 TileIndex tile;
00256 Town *t;
00257 StringID str;
00258 Company *cc;
00259 uint32 strp;
00260 char buffer[100];
00261
00262 if (c->name_1 != STR_SV_UNNAMED) return;
00263
00264 tile = c->last_build_coordinate;
00265 if (tile == 0) return;
00266
00267 t = ClosestTownFromTile(tile, UINT_MAX);
00268
00269 if (t->name == NULL && IsInsideMM(t->townnametype, SPECSTR_TOWNNAME_START, SPECSTR_TOWNNAME_LAST + 1)) {
00270 str = t->townnametype - SPECSTR_TOWNNAME_START + SPECSTR_PLAYERNAME_START;
00271 strp = t->townnameparts;
00272
00273 verify_name:;
00274
00275 FOR_ALL_COMPANIES(cc) {
00276 if (cc->name_1 == str && cc->name_2 == strp) goto bad_town_name;
00277 }
00278
00279 GetString(buffer, str, lastof(buffer));
00280 if (strlen(buffer) >= MAX_LENGTH_COMPANY_NAME_BYTES) goto bad_town_name;
00281
00282 set_name:;
00283 c->name_1 = str;
00284 c->name_2 = strp;
00285
00286 MarkWholeScreenDirty();
00287
00288 if (!IsHumanCompany(c->index)) {
00289 CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
00290 cni->FillData(c);
00291 SetDParam(0, STR_705E_NEW_TRANSPORT_COMPANY_LAUNCHED);
00292 SetDParam(1, STR_705F_STARTS_CONSTRUCTION_NEAR);
00293 SetDParamStr(2, cni->company_name);
00294 SetDParam(3, t->index);
00295 AddNewsItem(STR_02B6, NS_COMPANY_NEW, c->last_build_coordinate, 0, cni);
00296 }
00297 AI::BroadcastNewEvent(new AIEventCompanyNew(c->index), c->index);
00298 return;
00299 }
00300 bad_town_name:;
00301
00302 if (c->president_name_1 == SPECSTR_PRESIDENT_NAME) {
00303 str = SPECSTR_ANDCO_NAME;
00304 strp = c->president_name_2;
00305 goto set_name;
00306 } else {
00307 str = SPECSTR_ANDCO_NAME;
00308 strp = Random();
00309 goto verify_name;
00310 }
00311 }
00312
00313 static const byte _colour_sort[COLOUR_END] = {2, 2, 3, 2, 3, 2, 3, 2, 3, 2, 2, 2, 3, 1, 1, 1};
00314 static const Colours _similar_colour[COLOUR_END][2] = {
00315 { COLOUR_BLUE, COLOUR_LIGHT_BLUE },
00316 { COLOUR_GREEN, COLOUR_DARK_GREEN },
00317 { INVALID_COLOUR, INVALID_COLOUR },
00318 { COLOUR_ORANGE, INVALID_COLOUR },
00319 { INVALID_COLOUR, INVALID_COLOUR },
00320 { COLOUR_DARK_BLUE, COLOUR_BLUE },
00321 { COLOUR_PALE_GREEN, COLOUR_DARK_GREEN },
00322 { COLOUR_PALE_GREEN, COLOUR_GREEN },
00323 { COLOUR_DARK_BLUE, COLOUR_LIGHT_BLUE },
00324 { COLOUR_BROWN, COLOUR_ORANGE },
00325 { COLOUR_PURPLE, INVALID_COLOUR },
00326 { COLOUR_MAUVE, INVALID_COLOUR },
00327 { COLOUR_YELLOW, COLOUR_CREAM },
00328 { COLOUR_CREAM, INVALID_COLOUR },
00329 { COLOUR_WHITE, INVALID_COLOUR },
00330 { COLOUR_GREY, INVALID_COLOUR },
00331 };
00332
00333 static Colours GenerateCompanyColour()
00334 {
00335 Colours colours[COLOUR_END];
00336
00337
00338 for (uint i = 0; i < COLOUR_END; i++) colours[i] = (Colours)i;
00339
00340
00341 for (uint i = 0; i < 100; i++) {
00342 uint r = Random();
00343 Swap(colours[GB(r, 0, 4)], colours[GB(r, 4, 4)]);
00344 }
00345
00346
00347 for (uint i = 0; i < COLOUR_END; i++) {
00348 for (uint j = 1; j < COLOUR_END; j++) {
00349 if (_colour_sort[colours[j - 1]] < _colour_sort[colours[j]]) {
00350 Swap(colours[j - 1], colours[j]);
00351 }
00352 }
00353 };
00354
00355
00356 Company *c;
00357 FOR_ALL_COMPANIES(c) {
00358 Colours pcolour = (Colours)c->colour;
00359
00360 for (uint i = 0; i < COLOUR_END; i++) {
00361 if (colours[i] == pcolour) {
00362 colours[i] = INVALID_COLOUR;
00363 break;
00364 }
00365 }
00366
00367 for (uint j = 0; j < 2; j++) {
00368 Colours similar = _similar_colour[pcolour][j];
00369 if (similar == INVALID_COLOUR) break;
00370
00371 for (uint i = 1; i < COLOUR_END; i++) {
00372 if (colours[i - 1] == similar) Swap(colours[i - 1], colours[i]);
00373 }
00374 }
00375 }
00376
00377
00378 for (uint i = 0; i < COLOUR_END; i++) {
00379 if (colours[i] != INVALID_COLOUR) return colours[i];
00380 }
00381
00382 NOT_REACHED();
00383 }
00384
00385 static void GeneratePresidentName(Company *c)
00386 {
00387 for (;;) {
00388 restart:;
00389 c->president_name_2 = Random();
00390 c->president_name_1 = SPECSTR_PRESIDENT_NAME;
00391
00392 char buffer[MAX_LENGTH_PRESIDENT_NAME_BYTES + 1];
00393 SetDParam(0, c->index);
00394 GetString(buffer, STR_PRESIDENT_NAME, lastof(buffer));
00395 if (strlen(buffer) >= MAX_LENGTH_PRESIDENT_NAME_BYTES) continue;
00396
00397 Company *cc;
00398 FOR_ALL_COMPANIES(cc) {
00399 if (c != cc) {
00400 char buffer2[MAX_LENGTH_PRESIDENT_NAME_BYTES + 2];
00401 SetDParam(0, cc->index);
00402 GetString(buffer2, STR_PRESIDENT_NAME, lastof(buffer2));
00403 if (strcmp(buffer2, buffer) == 0) goto restart;
00404 }
00405 }
00406 return;
00407 }
00408 }
00409
00410 void ResetCompanyLivery(Company *c)
00411 {
00412 for (LiveryScheme scheme = LS_BEGIN; scheme < LS_END; scheme++) {
00413 c->livery[scheme].in_use = false;
00414 c->livery[scheme].colour1 = c->colour;
00415 c->livery[scheme].colour2 = c->colour;
00416 }
00417 }
00418
00425 Company *DoStartupNewCompany(bool is_ai)
00426 {
00427 if (ActiveCompanyCount() == MAX_COMPANIES || !Company::CanAllocateItem()) return NULL;
00428
00429
00430 Colours colour = GenerateCompanyColour();
00431
00432 Company *c = new Company(STR_SV_UNNAMED, is_ai);
00433
00434 c->colour = colour;
00435
00436 ResetCompanyLivery(c);
00437 _company_colours[c->index] = (Colours)c->colour;
00438
00439 c->money = c->current_loan = 100000;
00440
00441 c->share_owners[0] = c->share_owners[1] = c->share_owners[2] = c->share_owners[3] = INVALID_OWNER;
00442
00443 c->avail_railtypes = GetCompanyRailtypes(c->index);
00444 c->avail_roadtypes = GetCompanyRoadtypes(c->index);
00445 c->inaugurated_year = _cur_year;
00446 RandomCompanyManagerFaceBits(c->face, (GenderEthnicity)Random(), false);
00447
00448
00449 c->engine_renew_list = NULL;
00450 c->renew_keep_length = false;
00451 c->engine_renew = _settings_client.gui.autorenew;
00452 c->engine_renew_months = _settings_client.gui.autorenew_months;
00453 c->engine_renew_money = _settings_client.gui.autorenew_money;
00454
00455 GeneratePresidentName(c);
00456
00457 InvalidateWindow(WC_GRAPH_LEGEND, 0);
00458 InvalidateWindow(WC_TOOLBAR_MENU, 0);
00459 InvalidateWindow(WC_CLIENT_LIST, 0);
00460
00461 if (is_ai && (!_networking || _network_server)) AI::StartNew(c->index);
00462
00463 c->num_engines = CallocT<uint16>(GetEnginePoolSize());
00464
00465 return c;
00466 }
00467
00468 void StartupCompanies()
00469 {
00470 _next_competitor_start = 0;
00471 }
00472
00473 static void MaybeStartNewCompany()
00474 {
00475 #ifdef ENABLE_NETWORK
00476 if (_networking && ActiveCompanyCount() >= _settings_client.network.max_companies) return;
00477 #endif
00478
00479 Company *c;
00480
00481
00482 uint n = 0;
00483 FOR_ALL_COMPANIES(c) {
00484 if (c->is_ai) n++;
00485 }
00486
00487 if (n < (uint)_settings_game.difficulty.max_no_competitors) {
00488
00489
00490 DoCommandP(0, 1, 0, CMD_COMPANY_CTRL);
00491 }
00492 }
00493
00494 void InitializeCompanies()
00495 {
00496 _Company_pool.CleanPool();
00497 _Company_pool.AddBlockToPool();
00498 _cur_company_tick_index = 0;
00499 }
00500
00501 void OnTick_Companies()
00502 {
00503 if (_game_mode == GM_EDITOR) return;
00504
00505 if (IsValidCompanyID((CompanyID)_cur_company_tick_index)) {
00506 Company *c = GetCompany((CompanyID)_cur_company_tick_index);
00507 if (c->name_1 != 0) GenerateCompanyName(c);
00508 }
00509
00510 if (_next_competitor_start == 0) {
00511 _next_competitor_start = AI::GetStartNextTime() * DAY_TICKS;
00512 }
00513
00514 if (AI::CanStartNew() && _game_mode != GM_MENU && --_next_competitor_start == 0) {
00515 MaybeStartNewCompany();
00516 }
00517
00518 _cur_company_tick_index = (_cur_company_tick_index + 1) % MAX_COMPANIES;
00519 }
00520
00521 void CompaniesYearlyLoop()
00522 {
00523 Company *c;
00524
00525
00526 FOR_ALL_COMPANIES(c) {
00527 memmove(&c->yearly_expenses[1], &c->yearly_expenses[0], sizeof(c->yearly_expenses) - sizeof(c->yearly_expenses[0]));
00528 memset(&c->yearly_expenses[0], 0, sizeof(c->yearly_expenses[0]));
00529 InvalidateWindow(WC_FINANCES, c->index);
00530 }
00531
00532 if (_settings_client.gui.show_finances && _local_company != COMPANY_SPECTATOR) {
00533 ShowCompanyFinances(_local_company);
00534 c = GetCompany(_local_company);
00535 if (c->num_valid_stat_ent > 5 && c->old_economy[0].performance_history < c->old_economy[4].performance_history) {
00536 SndPlayFx(SND_01_BAD_YEAR);
00537 } else {
00538 SndPlayFx(SND_00_GOOD_YEAR);
00539 }
00540 }
00541 }
00542
00571 CommandCost CmdSetAutoReplace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00572 {
00573 if (!IsValidCompanyID(_current_company)) return CMD_ERROR;
00574
00575 Company *c = GetCompany(_current_company);
00576 switch (GB(p1, 0, 3)) {
00577 case 0:
00578 if (c->engine_renew == HasBit(p2, 0)) return CMD_ERROR;
00579
00580 if (flags & DC_EXEC) {
00581 c->engine_renew = HasBit(p2, 0);
00582 if (IsLocalCompany()) {
00583 _settings_client.gui.autorenew = c->engine_renew;
00584 InvalidateWindow(WC_GAME_OPTIONS, 0);
00585 }
00586 }
00587 break;
00588
00589 case 1:
00590 if (Clamp((int16)p2, -12, 12) != (int16)p2) return CMD_ERROR;
00591 if (c->engine_renew_months == (int16)p2) return CMD_ERROR;
00592
00593 if (flags & DC_EXEC) {
00594 c->engine_renew_months = (int16)p2;
00595 if (IsLocalCompany()) {
00596 _settings_client.gui.autorenew_months = c->engine_renew_months;
00597 InvalidateWindow(WC_GAME_OPTIONS, 0);
00598 }
00599 }
00600 break;
00601
00602 case 2:
00603 if (ClampU(p2, 0, 2000000) != p2) return CMD_ERROR;
00604 if (c->engine_renew_money == p2) return CMD_ERROR;
00605
00606 if (flags & DC_EXEC) {
00607 c->engine_renew_money = p2;
00608 if (IsLocalCompany()) {
00609 _settings_client.gui.autorenew_money = c->engine_renew_money;
00610 InvalidateWindow(WC_GAME_OPTIONS, 0);
00611 }
00612 }
00613 break;
00614
00615 case 3: {
00616 EngineID old_engine_type = GB(p2, 0, 16);
00617 EngineID new_engine_type = GB(p2, 16, 16);
00618 GroupID id_g = GB(p1, 16, 16);
00619 CommandCost cost;
00620
00621 if (!IsValidGroupID(id_g) && !IsAllGroupID(id_g) && !IsDefaultGroupID(id_g)) return CMD_ERROR;
00622 if (new_engine_type != INVALID_ENGINE) {
00623 if (!CheckAutoreplaceValidity(old_engine_type, new_engine_type, _current_company)) return CMD_ERROR;
00624
00625 cost = AddEngineReplacementForCompany(c, old_engine_type, new_engine_type, id_g, flags);
00626 } else {
00627 cost = RemoveEngineReplacementForCompany(c, old_engine_type, id_g, flags);
00628 }
00629
00630 if (IsLocalCompany()) InvalidateAutoreplaceWindow(old_engine_type, id_g);
00631
00632 return cost;
00633 }
00634
00635 case 4:
00636 if (Clamp((int16)GB(p1, 16, 16), -12, 12) != (int16)GB(p1, 16, 16)) return CMD_ERROR;
00637 if (ClampU(p2, 0, 2000000) != p2) return CMD_ERROR;
00638
00639 if (flags & DC_EXEC) {
00640 c->engine_renew = HasBit(p1, 15);
00641 c->engine_renew_months = (int16)GB(p1, 16, 16);
00642 c->engine_renew_money = p2;
00643
00644 if (IsLocalCompany()) {
00645 _settings_client.gui.autorenew = c->engine_renew;
00646 _settings_client.gui.autorenew_months = c->engine_renew_months;
00647 _settings_client.gui.autorenew_money = c->engine_renew_money;
00648 InvalidateWindow(WC_GAME_OPTIONS, 0);
00649 }
00650 }
00651 break;
00652
00653 case 5:
00654 if (c->renew_keep_length == HasBit(p2, 0)) return CMD_ERROR;
00655
00656 if (flags & DC_EXEC) {
00657 c->renew_keep_length = HasBit(p2, 0);
00658 if (IsLocalCompany()) {
00659 InvalidateWindow(WC_REPLACE_VEHICLE, VEH_TRAIN);
00660 }
00661 }
00662 break;
00663 }
00664
00665 return CommandCost();
00666 }
00667
00673 void CompanyNewsInformation::FillData(const Company *c, const Company *other)
00674 {
00675 SetDParam(0, c->index);
00676 GetString(this->company_name, STR_COMPANY_NAME, lastof(this->company_name));
00677
00678 if (other == NULL) {
00679 *this->other_company_name = '\0';
00680 } else {
00681 SetDParam(0, other->index);
00682 GetString(this->other_company_name, STR_COMPANY_NAME, lastof(this->other_company_name));
00683 c = other;
00684 }
00685
00686 SetDParam(0, c->index);
00687 GetString(this->president_name, STR_7058_PRESIDENT, lastof(this->president_name));
00688
00689 this->colour = c->colour;
00690 this->face = c->face;
00691
00692 }
00693
00714 CommandCost CmdCompanyCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00715 {
00716 if (flags & DC_EXEC) _current_company = OWNER_NONE;
00717
00718 InvalidateWindowData(WC_COMPANY_LEAGUE, 0, 0);
00719
00720 switch (p1) {
00721 case 0: {
00722
00723 if (!_networking) return CMD_ERROR;
00724
00725 #ifdef ENABLE_NETWORK
00726
00727
00728
00729
00730
00731
00732
00733
00734 ClientID cid = (ClientID)p2;
00735
00736
00737 if (!(flags & DC_EXEC)) return CommandCost();
00738 NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(cid);
00739 if (ci == NULL) return CommandCost();
00740
00741
00742 DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
00743
00744 Company *c = DoStartupNewCompany(false);
00745
00746
00747 if (c == NULL) {
00748 if (_network_server) {
00749 ci->client_playas = COMPANY_SPECTATOR;
00750 NetworkUpdateClientInfo(ci->client_id);
00751 } else if (_local_company == COMPANY_SPECTATOR) {
00752 _network_playas = COMPANY_SPECTATOR;
00753 }
00754 break;
00755 }
00756
00757
00758 if (cid == _network_own_client_id) {
00759 assert(_local_company == COMPANY_SPECTATOR);
00760 SetLocalCompany(c->index);
00761 if (!StrEmpty(_settings_client.network.default_company_pass)) {
00762 char *password = _settings_client.network.default_company_pass;
00763 NetworkChangeCompanyPassword(1, &password);
00764 }
00765
00766 _current_company = _local_company;
00767
00768
00769
00770 NetworkSend_Command(0,
00771 (_settings_client.gui.autorenew << 15 ) | (_settings_client.gui.autorenew_months << 16) | 4,
00772 _settings_client.gui.autorenew_money,
00773 CMD_SET_AUTOREPLACE,
00774 NULL,
00775 NULL
00776 );
00777
00778 MarkWholeScreenDirty();
00779 }
00780
00781 if (_network_server) {
00782
00783
00784
00785 CompanyID old_playas = ci->client_playas;
00786 ci->client_playas = c->index;
00787 NetworkUpdateClientInfo(ci->client_id);
00788
00789 if (IsValidCompanyID(ci->client_playas)) {
00790 CompanyID company_backup = _local_company;
00791 _network_company_states[c->index].months_empty = 0;
00792 _network_company_states[c->index].password[0] = '\0';
00793 NetworkServerUpdateCompanyPassworded(ci->client_playas, false);
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806 _local_company = ci->client_playas;
00807 NetworkSend_Command(0, 0, 0, CMD_RENAME_PRESIDENT, NULL, ci->client_name);
00808 _local_company = company_backup;
00809 }
00810
00811
00812 if (old_playas == COMPANY_SPECTATOR) {
00813 NetworkServerSendChat(NETWORK_ACTION_COMPANY_NEW, DESTTYPE_BROADCAST, 0, "", ci->client_id, ci->client_playas + 1);
00814 }
00815 }
00816 #endif
00817 } break;
00818
00819 case 1:
00820 if (!(flags & DC_EXEC)) return CommandCost();
00821
00822 DoStartupNewCompany(true);
00823 break;
00824
00825 case 2: {
00826 Company *c;
00827
00828 if (!IsValidCompanyID((CompanyID)p2)) return CMD_ERROR;
00829
00830 if (!(flags & DC_EXEC)) return CommandCost();
00831
00832 c = GetCompany((CompanyID)p2);
00833
00834
00835 DeleteCompanyWindows(c->index);
00836 CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
00837 cni->FillData(c);
00838
00839
00840 SetDParam(0, STR_705C_BANKRUPT);
00841 SetDParam(1, STR_705D_HAS_BEEN_CLOSED_DOWN_BY);
00842 SetDParamStr(2, cni->company_name);
00843 AddNewsItem(STR_02B6, NS_COMPANY_BANKRUPT, 0, 0, cni);
00844
00845
00846 ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER);
00847 if (!IsHumanCompany(c->index)) AI::Stop(c->index);
00848
00849 CompanyID c_index = c->index;
00850 delete c;
00851 AI::BroadcastNewEvent(new AIEventCompanyBankrupt(c_index));
00852 } break;
00853
00854 case 3: {
00855 CompanyID cid_old = (CompanyID)GB(p2, 0, 16);
00856 CompanyID cid_new = (CompanyID)GB(p2, 16, 16);
00857
00858 if (!IsValidCompanyID(cid_old) || !IsValidCompanyID(cid_new)) return CMD_ERROR;
00859
00860 if (!(flags & DC_EXEC)) return CMD_ERROR;
00861
00862 ChangeOwnershipOfCompanyItems(cid_old, cid_new);
00863 delete GetCompany(cid_old);
00864 } break;
00865
00866 default: return CMD_ERROR;
00867 }
00868
00869 return CommandCost();
00870 }