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
00426 Company *DoStartupNewCompany(bool is_ai, CompanyID company = INVALID_COMPANY)
00427 {
00428 if (ActiveCompanyCount() == MAX_COMPANIES || !Company::CanAllocateItem()) return NULL;
00429
00430
00431 Colours colour = GenerateCompanyColour();
00432
00433 Company *c;
00434 if (company == INVALID_COMPANY) {
00435 c = new Company(STR_SV_UNNAMED, is_ai);
00436 } else {
00437 if (IsValidCompanyID(company)) return NULL;
00438 c = new (company) Company(STR_SV_UNNAMED, is_ai);
00439 }
00440
00441 c->colour = colour;
00442
00443 ResetCompanyLivery(c);
00444 _company_colours[c->index] = (Colours)c->colour;
00445
00446 c->money = c->current_loan = 100000;
00447
00448 c->share_owners[0] = c->share_owners[1] = c->share_owners[2] = c->share_owners[3] = INVALID_OWNER;
00449
00450 c->avail_railtypes = GetCompanyRailtypes(c->index);
00451 c->avail_roadtypes = GetCompanyRoadtypes(c->index);
00452 c->inaugurated_year = _cur_year;
00453 RandomCompanyManagerFaceBits(c->face, (GenderEthnicity)Random(), false);
00454
00455
00456 if (is_ai) {
00457 c->engine_renew_money = 100000;
00458 c->engine_renew_months = 6;
00459 }
00460
00461 GeneratePresidentName(c);
00462
00463 InvalidateWindow(WC_GRAPH_LEGEND, 0);
00464 InvalidateWindow(WC_TOOLBAR_MENU, 0);
00465 InvalidateWindow(WC_CLIENT_LIST, 0);
00466
00467 if (is_ai && (!_networking || _network_server)) AI::StartNew(c->index);
00468
00469 c->num_engines = CallocT<uint16>(GetEnginePoolSize());
00470
00471 return c;
00472 }
00473
00474 void StartupCompanies()
00475 {
00476 _next_competitor_start = 0;
00477 }
00478
00479 static void MaybeStartNewCompany()
00480 {
00481 #ifdef ENABLE_NETWORK
00482 if (_networking && ActiveCompanyCount() >= _settings_client.network.max_companies) return;
00483 #endif
00484
00485 Company *c;
00486
00487
00488 uint n = 0;
00489 FOR_ALL_COMPANIES(c) {
00490 if (c->is_ai) n++;
00491 }
00492
00493 if (n < (uint)_settings_game.difficulty.max_no_competitors) {
00494
00495
00496 DoCommandP(0, 1, INVALID_COMPANY, CMD_COMPANY_CTRL);
00497 }
00498 }
00499
00500 void InitializeCompanies()
00501 {
00502 _Company_pool.CleanPool();
00503 _Company_pool.AddBlockToPool();
00504 _cur_company_tick_index = 0;
00505 }
00506
00507 void OnTick_Companies()
00508 {
00509 if (_game_mode == GM_EDITOR) return;
00510
00511 if (IsValidCompanyID((CompanyID)_cur_company_tick_index)) {
00512 Company *c = GetCompany((CompanyID)_cur_company_tick_index);
00513 if (c->name_1 != 0) GenerateCompanyName(c);
00514 }
00515
00516 if (_next_competitor_start == 0) {
00517 _next_competitor_start = AI::GetStartNextTime() * DAY_TICKS;
00518 }
00519
00520 if (AI::CanStartNew() && _game_mode != GM_MENU && --_next_competitor_start == 0) {
00521 MaybeStartNewCompany();
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, DoCommandFlag 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
00721 CommandCost CmdCompanyCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00722 {
00723 if (flags & DC_EXEC) _current_company = OWNER_NONE;
00724
00725 InvalidateWindowData(WC_COMPANY_LEAGUE, 0, 0);
00726
00727 switch (p1) {
00728 case 0: {
00729
00730 if (!_networking) return CMD_ERROR;
00731
00732 #ifdef ENABLE_NETWORK
00733
00734
00735
00736
00737
00738
00739
00740
00741 ClientID cid = (ClientID)p2;
00742
00743
00744 if (!(flags & DC_EXEC)) return CommandCost();
00745 NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(cid);
00746 if (ci == NULL) return CommandCost();
00747
00748
00749 DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
00750
00751 Company *c = DoStartupNewCompany(false);
00752
00753
00754 if (c == NULL) {
00755 if (_network_server) {
00756 ci->client_playas = COMPANY_SPECTATOR;
00757 NetworkUpdateClientInfo(ci->client_id);
00758 } else if (_local_company == COMPANY_SPECTATOR) {
00759 _network_playas = COMPANY_SPECTATOR;
00760 }
00761 break;
00762 }
00763
00764
00765 if (cid == _network_own_client_id) {
00766
00767 uint32 p1 = (_settings_client.gui.autorenew << 15 ) | (_settings_client.gui.autorenew_months << 16) | 4;
00768 uint32 p2 = _settings_client.gui.autorenew_money;
00769 assert(_local_company == COMPANY_SPECTATOR);
00770 SetLocalCompany(c->index);
00771 if (!StrEmpty(_settings_client.network.default_company_pass)) {
00772 char *password = _settings_client.network.default_company_pass;
00773 NetworkChangeCompanyPassword(1, &password);
00774 }
00775
00776 _current_company = _local_company;
00777
00778
00779
00780 NetworkSend_Command(0, p1, p2, CMD_SET_AUTOREPLACE, NULL, NULL);
00781
00782 MarkWholeScreenDirty();
00783 }
00784
00785 if (_network_server) {
00786
00787
00788
00789 CompanyID old_playas = ci->client_playas;
00790 ci->client_playas = c->index;
00791 NetworkUpdateClientInfo(ci->client_id);
00792
00793 if (IsValidCompanyID(ci->client_playas)) {
00794 CompanyID company_backup = _local_company;
00795 _network_company_states[c->index].months_empty = 0;
00796 _network_company_states[c->index].password[0] = '\0';
00797 NetworkServerUpdateCompanyPassworded(ci->client_playas, false);
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810 _local_company = ci->client_playas;
00811 NetworkSend_Command(0, 0, 0, CMD_RENAME_PRESIDENT, NULL, ci->client_name);
00812 _local_company = company_backup;
00813 }
00814
00815
00816 if (old_playas == COMPANY_SPECTATOR) {
00817 NetworkServerSendChat(NETWORK_ACTION_COMPANY_NEW, DESTTYPE_BROADCAST, 0, "", ci->client_id, ci->client_playas + 1);
00818 }
00819 }
00820 #endif
00821 } break;
00822
00823 case 1:
00824 if (!(flags & DC_EXEC)) return CommandCost();
00825
00826 if (p2 != INVALID_COMPANY && (p2 >= MAX_COMPANIES || IsValidCompanyID((CompanyID)p2))) return CMD_ERROR;
00827 DoStartupNewCompany(true, (CompanyID)p2);
00828 break;
00829
00830 case 2: {
00831 Company *c;
00832
00833 if (!IsValidCompanyID((CompanyID)p2)) return CMD_ERROR;
00834
00835 if (!(flags & DC_EXEC)) return CommandCost();
00836
00837 c = GetCompany((CompanyID)p2);
00838
00839
00840 DeleteCompanyWindows(c->index);
00841 CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
00842 cni->FillData(c);
00843
00844
00845 SetDParam(0, STR_705C_BANKRUPT);
00846 SetDParam(1, STR_705D_HAS_BEEN_CLOSED_DOWN_BY);
00847 SetDParamStr(2, cni->company_name);
00848 AddNewsItem(STR_02B6, NS_COMPANY_BANKRUPT, 0, 0, cni);
00849
00850
00851 ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER);
00852 if (!IsHumanCompany(c->index)) AI::Stop(c->index);
00853
00854 CompanyID c_index = c->index;
00855 delete c;
00856 AI::BroadcastNewEvent(new AIEventCompanyBankrupt(c_index));
00857 } break;
00858
00859 case 3: {
00860 CompanyID cid_old = (CompanyID)GB(p2, 0, 16);
00861 CompanyID cid_new = (CompanyID)GB(p2, 16, 16);
00862
00863 if (!IsValidCompanyID(cid_old) || !IsValidCompanyID(cid_new)) return CMD_ERROR;
00864
00865 if (!(flags & DC_EXEC)) return CMD_ERROR;
00866
00867 ChangeOwnershipOfCompanyItems(cid_old, cid_new);
00868 delete GetCompany(cid_old);
00869 } break;
00870
00871 default: return CMD_ERROR;
00872 }
00873
00874 return CommandCost();
00875 }