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