00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "engine_base.h"
00014 #include "company_base.h"
00015 #include "company_func.h"
00016 #include "company_gui.h"
00017 #include "town.h"
00018 #include "news_func.h"
00019 #include "cmd_helper.h"
00020 #include "command_func.h"
00021 #include "network/network.h"
00022 #include "network/network_func.h"
00023 #include "network/network_base.h"
00024 #include "network/network_admin.h"
00025 #include "ai/ai.hpp"
00026 #include "company_manager_face.h"
00027 #include "window_func.h"
00028 #include "strings_func.h"
00029 #include "date_func.h"
00030 #include "sound_func.h"
00031 #include "rail.h"
00032 #include "core/pool_func.hpp"
00033 #include "settings_func.h"
00034 #include "vehicle_base.h"
00035 #include "vehicle_func.h"
00036 #include "sprite.h"
00037 #include "smallmap_gui.h"
00038
00039 #include "table/strings.h"
00040
00041 CompanyByte _local_company;
00042 CompanyByte _current_company;
00043 Colours _company_colours[MAX_COMPANIES];
00044 CompanyManagerFace _company_manager_face;
00045 uint _next_competitor_start;
00046 uint _cur_company_tick_index;
00047
00048 CompanyPool _company_pool("Company");
00049 INSTANTIATE_POOL_METHODS(Company)
00050
00051
00056 Company::Company(uint16 name_1, bool is_ai)
00057 {
00058 this->name_1 = name_1;
00059 this->location_of_HQ = INVALID_TILE;
00060 this->is_ai = is_ai;
00061 this->terraform_limit = _settings_game.construction.terraform_frame_burst << 16;
00062 this->clear_limit = _settings_game.construction.clear_frame_burst << 16;
00063
00064 for (uint j = 0; j < 4; j++) this->share_owners[j] = COMPANY_SPECTATOR;
00065 InvalidateWindowData(WC_PERFORMANCE_DETAIL, 0, INVALID_COMPANY);
00066 }
00067
00069 Company::~Company()
00070 {
00071 free(this->num_engines);
00072
00073 if (CleaningPool()) return;
00074
00075 DeleteCompanyWindows(this->index);
00076 }
00077
00082 void Company::PostDestructor(size_t index)
00083 {
00084 InvalidateWindowData(WC_GRAPH_LEGEND, 0, (int)index);
00085 InvalidateWindowData(WC_PERFORMANCE_DETAIL, 0, (int)index);
00086 InvalidateWindowData(WC_COMPANY_LEAGUE, 0, 0);
00087
00088 InvalidateWindowData(WC_ERRMSG, 0);
00089 }
00090
00097 void SetLocalCompany(CompanyID new_company)
00098 {
00099
00100 assert(Company::IsValidID(new_company) || new_company == COMPANY_SPECTATOR || new_company == OWNER_NONE);
00101
00102 #ifdef ENABLE_NETWORK
00103
00104 InvalidateWindowData(WC_SEND_NETWORK_MSG, DESTTYPE_TEAM, _local_company);
00105 #endif
00106
00107 assert(IsLocalCompany());
00108
00109 _current_company = _local_company = new_company;
00110
00111
00112 DeleteConstructionWindows();
00113
00114
00115 MarkWholeScreenDirty();
00116 }
00117
00123 TextColour GetDrawStringCompanyColour(CompanyID company)
00124 {
00125 if (!Company::IsValidID(company)) return (TextColour)_colour_gradient[COLOUR_WHITE][4] | TC_IS_PALETTE_COLOUR;
00126 return (TextColour)_colour_gradient[_company_colours[company]][4] | TC_IS_PALETTE_COLOUR;
00127 }
00128
00135 void DrawCompanyIcon(CompanyID c, int x, int y)
00136 {
00137 DrawSprite(SPR_COMPANY_ICON, COMPANY_SPRITE_COLOUR(c), x, y);
00138 }
00139
00146 static bool IsValidCompanyManagerFace(CompanyManagerFace cmf)
00147 {
00148 if (!AreCompanyManagerFaceBitsValid(cmf, CMFV_GEN_ETHN, GE_WM)) return false;
00149
00150 GenderEthnicity ge = (GenderEthnicity)GetCompanyManagerFaceBits(cmf, CMFV_GEN_ETHN, GE_WM);
00151 bool has_moustache = !HasBit(ge, GENDER_FEMALE) && GetCompanyManagerFaceBits(cmf, CMFV_HAS_MOUSTACHE, ge) != 0;
00152 bool has_tie_earring = !HasBit(ge, GENDER_FEMALE) || GetCompanyManagerFaceBits(cmf, CMFV_HAS_TIE_EARRING, ge) != 0;
00153 bool has_glasses = GetCompanyManagerFaceBits(cmf, CMFV_HAS_GLASSES, ge) != 0;
00154
00155 if (!AreCompanyManagerFaceBitsValid(cmf, CMFV_EYE_COLOUR, ge)) return false;
00156 for (CompanyManagerFaceVariable cmfv = CMFV_CHEEKS; cmfv < CMFV_END; cmfv++) {
00157 switch (cmfv) {
00158 case CMFV_MOUSTACHE: if (!has_moustache) continue; break;
00159 case CMFV_LIPS:
00160 case CMFV_NOSE: if (has_moustache) continue; break;
00161 case CMFV_TIE_EARRING: if (!has_tie_earring) continue; break;
00162 case CMFV_GLASSES: if (!has_glasses) continue; break;
00163 default: break;
00164 }
00165 if (!AreCompanyManagerFaceBitsValid(cmf, cmfv, ge)) return false;
00166 }
00167
00168 return true;
00169 }
00170
00175 void InvalidateCompanyWindows(const Company *company)
00176 {
00177 CompanyID cid = company->index;
00178
00179 if (cid == _local_company) SetWindowDirty(WC_STATUS_BAR, 0);
00180 SetWindowDirty(WC_FINANCES, cid);
00181 }
00182
00188 bool CheckCompanyHasMoney(CommandCost &cost)
00189 {
00190 if (cost.GetCost() > 0) {
00191 const Company *c = Company::GetIfValid(_current_company);
00192 if (c != NULL && cost.GetCost() > c->money) {
00193 SetDParam(0, cost.GetCost());
00194 cost.MakeError(STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY);
00195 return false;
00196 }
00197 }
00198 return true;
00199 }
00200
00206 static void SubtractMoneyFromAnyCompany(Company *c, CommandCost cost)
00207 {
00208 if (cost.GetCost() == 0) return;
00209 assert(cost.GetExpensesType() != INVALID_EXPENSES);
00210
00211 c->money -= cost.GetCost();
00212 c->yearly_expenses[0][cost.GetExpensesType()] += cost.GetCost();
00213
00214 if (HasBit(1 << EXPENSES_TRAIN_INC |
00215 1 << EXPENSES_ROADVEH_INC |
00216 1 << EXPENSES_AIRCRAFT_INC |
00217 1 << EXPENSES_SHIP_INC, cost.GetExpensesType())) {
00218 c->cur_economy.income -= cost.GetCost();
00219 } else if (HasBit(1 << EXPENSES_TRAIN_RUN |
00220 1 << EXPENSES_ROADVEH_RUN |
00221 1 << EXPENSES_AIRCRAFT_RUN |
00222 1 << EXPENSES_SHIP_RUN |
00223 1 << EXPENSES_PROPERTY |
00224 1 << EXPENSES_LOAN_INT, cost.GetExpensesType())) {
00225 c->cur_economy.expenses -= cost.GetCost();
00226 }
00227
00228 InvalidateCompanyWindows(c);
00229 }
00230
00235 void SubtractMoneyFromCompany(CommandCost cost)
00236 {
00237 Company *c = Company::GetIfValid(_current_company);
00238 if (c != NULL) SubtractMoneyFromAnyCompany(c, cost);
00239 }
00240
00246 void SubtractMoneyFromCompanyFract(CompanyID company, CommandCost cst)
00247 {
00248 Company *c = Company::Get(company);
00249 byte m = c->money_fraction;
00250 Money cost = cst.GetCost();
00251
00252 c->money_fraction = m - (byte)cost;
00253 cost >>= 8;
00254 if (c->money_fraction > m) cost++;
00255 if (cost != 0) SubtractMoneyFromAnyCompany(c, CommandCost(cst.GetExpensesType(), cost));
00256 }
00257
00259 void UpdateLandscapingLimits()
00260 {
00261 Company *c;
00262 FOR_ALL_COMPANIES(c) {
00263 c->terraform_limit = min(c->terraform_limit + _settings_game.construction.terraform_per_64k_frames, (uint32)_settings_game.construction.terraform_frame_burst << 16);
00264 c->clear_limit = min(c->clear_limit + _settings_game.construction.clear_per_64k_frames, (uint32)_settings_game.construction.clear_frame_burst << 16);
00265 }
00266 }
00267
00274 void GetNameOfOwner(Owner owner, TileIndex tile)
00275 {
00276 SetDParam(2, owner);
00277
00278 if (owner != OWNER_TOWN) {
00279 if (!Company::IsValidID(owner)) {
00280 SetDParam(0, STR_COMPANY_SOMEONE);
00281 } else {
00282 SetDParam(0, STR_COMPANY_NAME);
00283 SetDParam(1, owner);
00284 }
00285 } else {
00286 assert(tile != 0);
00287 const Town *t = ClosestTownFromTile(tile, UINT_MAX);
00288
00289 SetDParam(0, STR_TOWN_NAME);
00290 SetDParam(1, t->index);
00291 }
00292 }
00293
00294
00303 CommandCost CheckOwnership(Owner owner, TileIndex tile)
00304 {
00305 assert(owner < OWNER_END);
00306 assert(owner != OWNER_TOWN || tile != 0);
00307
00308 if (owner == _current_company) return CommandCost();
00309
00310 GetNameOfOwner(owner, tile);
00311 return_cmd_error(STR_ERROR_OWNED_BY);
00312 }
00313
00321 CommandCost CheckTileOwnership(TileIndex tile)
00322 {
00323 Owner owner = GetTileOwner(tile);
00324
00325 assert(owner < OWNER_END);
00326
00327 if (owner == _current_company) return CommandCost();
00328
00329
00330 if (IsLocalCompany()) GetNameOfOwner(owner, tile);
00331 return_cmd_error(STR_ERROR_OWNED_BY);
00332 }
00333
00338 static void GenerateCompanyName(Company *c)
00339 {
00340
00341
00342 char buffer[(MAX_LENGTH_COMPANY_NAME_CHARS + 1) * MAX_CHAR_LENGTH];
00343
00344 if (c->name_1 != STR_SV_UNNAMED) return;
00345 if (c->last_build_coordinate == 0) return;
00346
00347 Town *t = ClosestTownFromTile(c->last_build_coordinate, UINT_MAX);
00348
00349 StringID str;
00350 uint32 strp;
00351 if (t->name == NULL && IsInsideMM(t->townnametype, SPECSTR_TOWNNAME_START, SPECSTR_TOWNNAME_LAST + 1)) {
00352 str = t->townnametype - SPECSTR_TOWNNAME_START + SPECSTR_PLAYERNAME_START;
00353 strp = t->townnameparts;
00354
00355 verify_name:;
00356
00357 Company *cc;
00358 FOR_ALL_COMPANIES(cc) {
00359 if (cc->name_1 == str && cc->name_2 == strp) goto bad_town_name;
00360 }
00361
00362 GetString(buffer, str, lastof(buffer));
00363 if (Utf8StringLength(buffer) >= MAX_LENGTH_COMPANY_NAME_CHARS) goto bad_town_name;
00364
00365 set_name:;
00366 c->name_1 = str;
00367 c->name_2 = strp;
00368
00369 MarkWholeScreenDirty();
00370
00371 if (c->is_ai) {
00372 CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
00373 cni->FillData(c);
00374 SetDParam(0, STR_NEWS_COMPANY_LAUNCH_TITLE);
00375 SetDParam(1, STR_NEWS_COMPANY_LAUNCH_DESCRIPTION);
00376 SetDParamStr(2, cni->company_name);
00377 SetDParam(3, t->index);
00378 AddNewsItem(STR_MESSAGE_NEWS_FORMAT, NS_COMPANY_NEW, NR_TILE, c->last_build_coordinate, NR_NONE, UINT32_MAX, cni);
00379 }
00380 AI::BroadcastNewEvent(new AIEventCompanyNew(c->index), c->index);
00381 return;
00382 }
00383 bad_town_name:;
00384
00385 if (c->president_name_1 == SPECSTR_PRESIDENT_NAME) {
00386 str = SPECSTR_ANDCO_NAME;
00387 strp = c->president_name_2;
00388 goto set_name;
00389 } else {
00390 str = SPECSTR_ANDCO_NAME;
00391 strp = Random();
00392 goto verify_name;
00393 }
00394 }
00395
00396 static const byte _colour_sort[COLOUR_END] = {2, 2, 3, 2, 3, 2, 3, 2, 3, 2, 2, 2, 3, 1, 1, 1};
00397 static const Colours _similar_colour[COLOUR_END][2] = {
00398 { COLOUR_BLUE, COLOUR_LIGHT_BLUE },
00399 { COLOUR_GREEN, COLOUR_DARK_GREEN },
00400 { INVALID_COLOUR, INVALID_COLOUR },
00401 { COLOUR_ORANGE, INVALID_COLOUR },
00402 { INVALID_COLOUR, INVALID_COLOUR },
00403 { COLOUR_DARK_BLUE, COLOUR_BLUE },
00404 { COLOUR_PALE_GREEN, COLOUR_DARK_GREEN },
00405 { COLOUR_PALE_GREEN, COLOUR_GREEN },
00406 { COLOUR_DARK_BLUE, COLOUR_LIGHT_BLUE },
00407 { COLOUR_BROWN, COLOUR_ORANGE },
00408 { COLOUR_PURPLE, INVALID_COLOUR },
00409 { COLOUR_MAUVE, INVALID_COLOUR },
00410 { COLOUR_YELLOW, COLOUR_CREAM },
00411 { COLOUR_CREAM, INVALID_COLOUR },
00412 { COLOUR_WHITE, INVALID_COLOUR },
00413 { COLOUR_GREY, INVALID_COLOUR },
00414 };
00415
00420 static Colours GenerateCompanyColour()
00421 {
00422 Colours colours[COLOUR_END];
00423
00424
00425 for (uint i = 0; i < COLOUR_END; i++) colours[i] = (Colours)i;
00426
00427
00428 for (uint i = 0; i < 100; i++) {
00429 uint r = Random();
00430 Swap(colours[GB(r, 0, 4)], colours[GB(r, 4, 4)]);
00431 }
00432
00433
00434 for (uint i = 0; i < COLOUR_END; i++) {
00435 for (uint j = 1; j < COLOUR_END; j++) {
00436 if (_colour_sort[colours[j - 1]] < _colour_sort[colours[j]]) {
00437 Swap(colours[j - 1], colours[j]);
00438 }
00439 }
00440 };
00441
00442
00443 Company *c;
00444 FOR_ALL_COMPANIES(c) {
00445 Colours pcolour = (Colours)c->colour;
00446
00447 for (uint i = 0; i < COLOUR_END; i++) {
00448 if (colours[i] == pcolour) {
00449 colours[i] = INVALID_COLOUR;
00450 break;
00451 }
00452 }
00453
00454 for (uint j = 0; j < 2; j++) {
00455 Colours similar = _similar_colour[pcolour][j];
00456 if (similar == INVALID_COLOUR) break;
00457
00458 for (uint i = 1; i < COLOUR_END; i++) {
00459 if (colours[i - 1] == similar) Swap(colours[i - 1], colours[i]);
00460 }
00461 }
00462 }
00463
00464
00465 for (uint i = 0; i < COLOUR_END; i++) {
00466 if (colours[i] != INVALID_COLOUR) return colours[i];
00467 }
00468
00469 NOT_REACHED();
00470 }
00471
00476 static void GeneratePresidentName(Company *c)
00477 {
00478 for (;;) {
00479 restart:;
00480 c->president_name_2 = Random();
00481 c->president_name_1 = SPECSTR_PRESIDENT_NAME;
00482
00483
00484
00485 char buffer[(MAX_LENGTH_PRESIDENT_NAME_CHARS + 1) * MAX_CHAR_LENGTH];
00486 SetDParam(0, c->index);
00487 GetString(buffer, STR_PRESIDENT_NAME, lastof(buffer));
00488 if (Utf8StringLength(buffer) >= MAX_LENGTH_PRESIDENT_NAME_CHARS) continue;
00489
00490 Company *cc;
00491 FOR_ALL_COMPANIES(cc) {
00492 if (c != cc) {
00493
00494 char buffer2[(MAX_LENGTH_PRESIDENT_NAME_CHARS + 1) * MAX_CHAR_LENGTH];
00495 SetDParam(0, cc->index);
00496 GetString(buffer2, STR_PRESIDENT_NAME, lastof(buffer2));
00497 if (strcmp(buffer2, buffer) == 0) goto restart;
00498 }
00499 }
00500 return;
00501 }
00502 }
00503
00504 void ResetCompanyLivery(Company *c)
00505 {
00506 for (LiveryScheme scheme = LS_BEGIN; scheme < LS_END; scheme++) {
00507 c->livery[scheme].in_use = false;
00508 c->livery[scheme].colour1 = c->colour;
00509 c->livery[scheme].colour2 = c->colour;
00510 }
00511 }
00512
00520 Company *DoStartupNewCompany(bool is_ai, CompanyID company = INVALID_COMPANY)
00521 {
00522 if (!Company::CanAllocateItem()) return NULL;
00523
00524
00525 Colours colour = GenerateCompanyColour();
00526
00527 Company *c;
00528 if (company == INVALID_COMPANY) {
00529 c = new Company(STR_SV_UNNAMED, is_ai);
00530 } else {
00531 if (Company::IsValidID(company)) return NULL;
00532 c = new (company) Company(STR_SV_UNNAMED, is_ai);
00533 }
00534
00535 c->colour = colour;
00536
00537 ResetCompanyLivery(c);
00538 _company_colours[c->index] = (Colours)c->colour;
00539
00540 c->money = c->current_loan = 100000;
00541
00542 c->share_owners[0] = c->share_owners[1] = c->share_owners[2] = c->share_owners[3] = INVALID_OWNER;
00543
00544 c->avail_railtypes = GetCompanyRailtypes(c->index);
00545 c->avail_roadtypes = GetCompanyRoadtypes(c->index);
00546 c->inaugurated_year = _cur_year;
00547 RandomCompanyManagerFaceBits(c->face, (GenderEthnicity)Random(), false);
00548
00549 SetDefaultCompanySettings(c->index);
00550
00551 GeneratePresidentName(c);
00552
00553 SetWindowDirty(WC_GRAPH_LEGEND, 0);
00554 SetWindowDirty(WC_TOOLBAR_MENU, 0);
00555 SetWindowDirty(WC_CLIENT_LIST, 0);
00556 BuildOwnerLegend();
00557 InvalidateWindowData(WC_SMALLMAP, 0, 1);
00558
00559 if (is_ai && (!_networking || _network_server)) AI::StartNew(c->index);
00560
00561 c->num_engines = CallocT<uint16>(Engine::GetPoolSize());
00562
00563 return c;
00564 }
00565
00567 void StartupCompanies()
00568 {
00569 _next_competitor_start = 0;
00570 }
00571
00572 #ifdef ENABLE_AI
00573
00574 static void MaybeStartNewCompany()
00575 {
00576 #ifdef ENABLE_NETWORK
00577 if (_networking && Company::GetNumItems() >= _settings_client.network.max_companies) return;
00578 #endif
00579
00580 Company *c;
00581
00582
00583 uint n = 0;
00584 FOR_ALL_COMPANIES(c) {
00585 if (c->is_ai) n++;
00586 }
00587
00588 if (n < (uint)_settings_game.difficulty.max_no_competitors) {
00589
00590
00591 DoCommandP(0, 1 | INVALID_COMPANY << 16, 0, CMD_COMPANY_CTRL);
00592 }
00593 }
00594 #endif
00595
00597 void InitializeCompanies()
00598 {
00599 _company_pool.CleanPool();
00600 _cur_company_tick_index = 0;
00601 }
00602
00609 bool MayCompanyTakeOver(CompanyID cbig, CompanyID csmall)
00610 {
00611 uint big_counts[4], small_counts[4];
00612 CountCompanyVehicles(cbig, big_counts);
00613 CountCompanyVehicles(csmall, small_counts);
00614
00615
00616 return big_counts[VEH_TRAIN] + small_counts[VEH_TRAIN] <= _settings_game.vehicle.max_trains &&
00617 big_counts[VEH_ROAD] + small_counts[VEH_ROAD] <= _settings_game.vehicle.max_roadveh &&
00618 big_counts[VEH_SHIP] + small_counts[VEH_SHIP] <= _settings_game.vehicle.max_ships &&
00619 big_counts[VEH_AIRCRAFT] + small_counts[VEH_AIRCRAFT] <= _settings_game.vehicle.max_aircraft;
00620 }
00621
00631 static void HandleBankruptcyTakeover(Company *c)
00632 {
00633
00634
00635
00636
00637
00638 static const int TAKE_OVER_TIMEOUT = 3 * 30 * DAY_TICKS / (MAX_COMPANIES - 1);
00639
00640 assert(c->bankrupt_asked != 0);
00641
00642
00643 if (c->bankrupt_timeout != 0) {
00644 c->bankrupt_timeout -= MAX_COMPANIES;
00645 if (c->bankrupt_timeout > 0) return;
00646 c->bankrupt_timeout = 0;
00647
00648 return;
00649 }
00650
00651
00652 if (c->bankrupt_asked == MAX_UVALUE(CompanyMask)) return;
00653
00654 Company *c2, *best = NULL;
00655 int32 best_performance = -1;
00656
00657
00658 FOR_ALL_COMPANIES(c2) {
00659 if (c2->bankrupt_asked == 0 &&
00660 !HasBit(c->bankrupt_asked, c2->index) &&
00661 best_performance < c2->old_economy[1].performance_history &&
00662 MayCompanyTakeOver(c2->index, c->index)) {
00663 best_performance = c2->old_economy[1].performance_history;
00664 best = c2;
00665 }
00666 }
00667
00668
00669 if (best_performance == -1) {
00670 c->bankrupt_asked = MAX_UVALUE(CompanyMask);
00671 return;
00672 }
00673
00674 SetBit(c->bankrupt_asked, best->index);
00675
00676 c->bankrupt_timeout = TAKE_OVER_TIMEOUT;
00677 if (best->is_ai) {
00678 AI::NewEvent(best->index, new AIEventCompanyAskMerger(c->index, ClampToI32(c->bankrupt_value)));
00679 } else if (IsInteractiveCompany(best->index)) {
00680 ShowBuyCompanyDialog(c->index);
00681 }
00682 }
00683
00684 void OnTick_Companies()
00685 {
00686 if (_game_mode == GM_EDITOR) return;
00687
00688 Company *c = Company::GetIfValid(_cur_company_tick_index);
00689 if (c != NULL) {
00690 if (c->name_1 != 0) GenerateCompanyName(c);
00691 if (c->bankrupt_asked != 0) HandleBankruptcyTakeover(c);
00692 }
00693
00694 #ifdef ENABLE_AI
00695 if (_next_competitor_start == 0) {
00696 _next_competitor_start = AI::GetStartNextTime() * DAY_TICKS;
00697 }
00698
00699 if (AI::CanStartNew() && _game_mode != GM_MENU && --_next_competitor_start == 0) {
00700 MaybeStartNewCompany();
00701 }
00702 #endif
00703
00704 _cur_company_tick_index = (_cur_company_tick_index + 1) % MAX_COMPANIES;
00705 }
00706
00711 void CompaniesYearlyLoop()
00712 {
00713 Company *c;
00714
00715
00716 FOR_ALL_COMPANIES(c) {
00717 memmove(&c->yearly_expenses[1], &c->yearly_expenses[0], sizeof(c->yearly_expenses) - sizeof(c->yearly_expenses[0]));
00718 memset(&c->yearly_expenses[0], 0, sizeof(c->yearly_expenses[0]));
00719 SetWindowDirty(WC_FINANCES, c->index);
00720 }
00721
00722 if (_settings_client.gui.show_finances && _local_company != COMPANY_SPECTATOR) {
00723 ShowCompanyFinances(_local_company);
00724 c = Company::Get(_local_company);
00725 if (c->num_valid_stat_ent > 5 && c->old_economy[0].performance_history < c->old_economy[4].performance_history) {
00726 SndPlayFx(SND_01_BAD_YEAR);
00727 } else {
00728 SndPlayFx(SND_00_GOOD_YEAR);
00729 }
00730 }
00731 }
00732
00738 void CompanyNewsInformation::FillData(const Company *c, const Company *other)
00739 {
00740 SetDParam(0, c->index);
00741 GetString(this->company_name, STR_COMPANY_NAME, lastof(this->company_name));
00742
00743 if (other == NULL) {
00744 *this->other_company_name = '\0';
00745 } else {
00746 SetDParam(0, other->index);
00747 GetString(this->other_company_name, STR_COMPANY_NAME, lastof(this->other_company_name));
00748 c = other;
00749 }
00750
00751 SetDParam(0, c->index);
00752 GetString(this->president_name, STR_PRESIDENT_NAME_MANAGER, lastof(this->president_name));
00753
00754 this->colour = c->colour;
00755 this->face = c->face;
00756
00757 }
00758
00763 void CompanyAdminUpdate(const Company *company)
00764 {
00765 #ifdef ENABLE_NETWORK
00766 if (_network_server) NetworkAdminCompanyUpdate(company);
00767 #endif
00768 }
00769
00774 void CompanyAdminBankrupt(CompanyID company_id)
00775 {
00776 #ifdef ENABLE_NETWORK
00777 if (_network_server) NetworkAdminCompanyRemove(company_id, ADMIN_CRR_BANKRUPT);
00778 #endif
00779 }
00780
00795 CommandCost CmdCompanyCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00796 {
00797 InvalidateWindowData(WC_COMPANY_LEAGUE, 0, 0);
00798 CompanyID company_id = (CompanyID)GB(p1, 16, 8);
00799 #ifdef ENABLE_NETWORK
00800 ClientID client_id = (ClientID)p2;
00801 #endif
00802
00803 switch (GB(p1, 0, 16)) {
00804 case 0: {
00805
00806 if (!_networking) return CMD_ERROR;
00807
00808 #ifdef ENABLE_NETWORK
00809
00810 if (!(flags & DC_EXEC)) return CommandCost();
00811 NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(client_id);
00812 if (ci == NULL) return CommandCost();
00813
00814
00815 DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
00816
00817 Company *c = DoStartupNewCompany(false);
00818
00819
00820 if (c == NULL) {
00821 if (_network_server) {
00822 ci->client_playas = COMPANY_SPECTATOR;
00823 NetworkUpdateClientInfo(ci->client_id);
00824 }
00825 break;
00826 }
00827
00828
00829 if (client_id == _network_own_client_id) {
00830 assert(_local_company == COMPANY_SPECTATOR);
00831 SetLocalCompany(c->index);
00832 if (!StrEmpty(_settings_client.network.default_company_pass)) {
00833 NetworkChangeCompanyPassword(_settings_client.network.default_company_pass);
00834 }
00835
00836
00837
00838 SyncCompanySettings();
00839
00840 MarkWholeScreenDirty();
00841 }
00842
00843 if (_network_server) {
00844 CompanyID old_playas = ci->client_playas;
00845 ci->client_playas = c->index;
00846 NetworkUpdateClientInfo(ci->client_id);
00847
00848 if (Company::IsValidID(ci->client_playas)) {
00849 _network_company_states[c->index].months_empty = 0;
00850 _network_company_states[c->index].password[0] = '\0';
00851 NetworkServerUpdateCompanyPassworded(ci->client_playas, false);
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864 NetworkSendCommand(0, 0, 0, CMD_RENAME_PRESIDENT, NULL, ci->client_name, ci->client_playas);
00865 }
00866
00867
00868 if (old_playas == COMPANY_SPECTATOR) {
00869 NetworkAdminCompanyInfo(c, true);
00870 NetworkServerSendChat(NETWORK_ACTION_COMPANY_NEW, DESTTYPE_BROADCAST, 0, "", ci->client_id, ci->client_playas + 1);
00871 }
00872 }
00873 #endif
00874 break;
00875 }
00876
00877 case 1:
00878 if (!(flags & DC_EXEC)) return CommandCost();
00879
00880 if (company_id != INVALID_COMPANY && (company_id >= MAX_COMPANIES || Company::IsValidID(company_id))) return CMD_ERROR;
00881 DoStartupNewCompany(true, company_id);
00882 break;
00883
00884 case 2: {
00885 Company *c = Company::GetIfValid(company_id);
00886 if (c == NULL) return CMD_ERROR;
00887
00888 if (!(flags & DC_EXEC)) return CommandCost();
00889
00890
00891 DeleteCompanyWindows(c->index);
00892 CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
00893 cni->FillData(c);
00894
00895
00896 SetDParam(0, STR_NEWS_COMPANY_BANKRUPT_TITLE);
00897 SetDParam(1, STR_NEWS_COMPANY_BANKRUPT_DESCRIPTION);
00898 SetDParamStr(2, cni->company_name);
00899 AddCompanyNewsItem(STR_MESSAGE_NEWS_FORMAT, NS_COMPANY_BANKRUPT, cni);
00900
00901
00902 ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER);
00903 if (c->is_ai) AI::Stop(c->index);
00904
00905 CompanyID c_index = c->index;
00906 delete c;
00907 AI::BroadcastNewEvent(new AIEventCompanyBankrupt(c_index));
00908 CompanyAdminBankrupt(c_index);
00909 break;
00910 }
00911
00912 default: return CMD_ERROR;
00913 }
00914
00915 InvalidateWindowClassesData(WC_GAME_OPTIONS);
00916 InvalidateWindowClassesData(WC_AI_SETTINGS);
00917 InvalidateWindowClassesData(WC_AI_LIST);
00918
00919 return CommandCost();
00920 }
00921
00931 CommandCost CmdSetCompanyManagerFace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00932 {
00933 CompanyManagerFace cmf = (CompanyManagerFace)p2;
00934
00935 if (!IsValidCompanyManagerFace(cmf)) return CMD_ERROR;
00936
00937 if (flags & DC_EXEC) {
00938 Company::Get(_current_company)->face = cmf;
00939 MarkWholeScreenDirty();
00940 }
00941 return CommandCost();
00942 }
00943
00955 CommandCost CmdSetCompanyColour(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00956 {
00957 Colours colour = Extract<Colours, 0, 4>(p2);
00958 LiveryScheme scheme = Extract<LiveryScheme, 0, 8>(p1);
00959 byte state = GB(p1, 8, 2);
00960
00961 if (scheme >= LS_END || state >= 3 || colour == INVALID_COLOUR) return CMD_ERROR;
00962
00963 Company *c = Company::Get(_current_company);
00964
00965
00966 if (scheme == LS_DEFAULT && state == 0) {
00967 const Company *cc;
00968 FOR_ALL_COMPANIES(cc) {
00969 if (cc != c && cc->colour == colour) return CMD_ERROR;
00970 }
00971 }
00972
00973 if (flags & DC_EXEC) {
00974 switch (state) {
00975 case 0:
00976 c->livery[scheme].colour1 = colour;
00977
00978
00979
00980 if (scheme == LS_DEFAULT) {
00981 _company_colours[_current_company] = colour;
00982 c->colour = colour;
00983 CompanyAdminUpdate(c);
00984 }
00985 break;
00986
00987 case 1:
00988 c->livery[scheme].colour2 = colour;
00989 break;
00990
00991 case 2:
00992 c->livery[scheme].in_use = colour != 0;
00993
00994
00995
00996
00997
00998
00999
01000
01001 if (colour != 0) {
01002 c->livery[LS_DEFAULT].in_use = true;
01003 break;
01004 }
01005
01006
01007
01008 c->livery[LS_DEFAULT].in_use = false;
01009 for (scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
01010 if (c->livery[scheme].in_use) {
01011 c->livery[LS_DEFAULT].in_use = true;
01012 break;
01013 }
01014 }
01015 break;
01016
01017 default:
01018 break;
01019 }
01020 ResetVehicleColourMap();
01021 MarkWholeScreenDirty();
01022
01023
01024 InvalidateWindowData(WC_INCOME_GRAPH, 0);
01025 InvalidateWindowData(WC_OPERATING_PROFIT, 0);
01026 InvalidateWindowData(WC_DELIVERED_CARGO, 0);
01027 InvalidateWindowData(WC_PERFORMANCE_HISTORY, 0);
01028 InvalidateWindowData(WC_COMPANY_VALUE, 0);
01029
01030
01031 Vehicle *v;
01032 FOR_ALL_VEHICLES(v) {
01033 if (v->owner == _current_company) v->InvalidateNewGRFCache();
01034 }
01035
01036 extern void UpdateObjectColours(const Company *c);
01037 UpdateObjectColours(c);
01038 }
01039 return CommandCost();
01040 }
01041
01047 static bool IsUniqueCompanyName(const char *name)
01048 {
01049 const Company *c;
01050
01051 FOR_ALL_COMPANIES(c) {
01052 if (c->name != NULL && strcmp(c->name, name) == 0) return false;
01053 }
01054
01055 return true;
01056 }
01057
01067 CommandCost CmdRenameCompany(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01068 {
01069 bool reset = StrEmpty(text);
01070
01071 if (!reset) {
01072 if (Utf8StringLength(text) >= MAX_LENGTH_COMPANY_NAME_CHARS) return CMD_ERROR;
01073 if (!IsUniqueCompanyName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
01074 }
01075
01076 if (flags & DC_EXEC) {
01077 Company *c = Company::Get(_current_company);
01078 free(c->name);
01079 c->name = reset ? NULL : strdup(text);
01080 MarkWholeScreenDirty();
01081 CompanyAdminUpdate(c);
01082 }
01083
01084 return CommandCost();
01085 }
01086
01092 static bool IsUniquePresidentName(const char *name)
01093 {
01094 const Company *c;
01095
01096 FOR_ALL_COMPANIES(c) {
01097 if (c->president_name != NULL && strcmp(c->president_name, name) == 0) return false;
01098 }
01099
01100 return true;
01101 }
01102
01112 CommandCost CmdRenamePresident(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01113 {
01114 bool reset = StrEmpty(text);
01115
01116 if (!reset) {
01117 if (Utf8StringLength(text) >= MAX_LENGTH_PRESIDENT_NAME_CHARS) return CMD_ERROR;
01118 if (!IsUniquePresidentName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
01119 }
01120
01121 if (flags & DC_EXEC) {
01122 Company *c = Company::Get(_current_company);
01123 free(c->president_name);
01124
01125 if (reset) {
01126 c->president_name = NULL;
01127 } else {
01128 c->president_name = strdup(text);
01129
01130 if (c->name_1 == STR_SV_UNNAMED && c->name == NULL) {
01131 char buf[80];
01132
01133 snprintf(buf, lengthof(buf), "%s Transport", text);
01134 DoCommand(0, 0, 0, DC_EXEC, CMD_RENAME_COMPANY, buf);
01135 }
01136 }
01137
01138 MarkWholeScreenDirty();
01139 CompanyAdminUpdate(c);
01140 }
01141
01142 return CommandCost();
01143 }