# company_cmd.cpp

Go to the documentation of this file.

    00001 /* $Id: company_cmd.cpp 16324 2009-05-16 20:29:53Z rubidium $ */
    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 /* NOSAVE: can be determined from company structs */
    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   /* company could also be COMPANY_SPECTATOR or OWNER_NONE */
    00074   assert(IsValidCompanyID(new_company) || new_company == COMPANY_SPECTATOR || new_company == OWNER_NONE);
    00075
    00076   _local_company = new_company;
    00077
    00078   /* Do not update the settings if we are in the intro GUI */
    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   /* Delete any construction windows... */
    00088   DeleteConstructionWindows();
    00089
    00090   /* ... and redraw the whole screen. */
    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   /* Get the colour for DrawString-subroutines which matches the colour
    00103    * of the company */
    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:        // FALL THROUGH
    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   /* no need to get the name of the owner unless we're the local company (saves some time) */
    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     /* No companies must have this name already */
    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 }, // COLOUR_DARK_BLUE
    00316   { COLOUR_GREEN,      COLOUR_DARK_GREEN }, // COLOUR_PALE_GREEN
    00317   { INVALID_COLOUR,    INVALID_COLOUR    }, // COLOUR_PINK
    00318   { COLOUR_ORANGE,     INVALID_COLOUR    }, // COLOUR_YELLOW
    00319   { INVALID_COLOUR,    INVALID_COLOUR    }, // COLOUR_RED
    00320   { COLOUR_DARK_BLUE,  COLOUR_BLUE       }, // COLOUR_LIGHT_BLUE
    00321   { COLOUR_PALE_GREEN, COLOUR_DARK_GREEN }, // COLOUR_GREEN
    00322   { COLOUR_PALE_GREEN, COLOUR_GREEN      }, // COLOUR_DARK_GREEN
    00323   { COLOUR_DARK_BLUE,  COLOUR_LIGHT_BLUE }, // COLOUR_BLUE
    00324   { COLOUR_BROWN,      COLOUR_ORANGE     }, // COLOUR_CREAM
    00325   { COLOUR_PURPLE,     INVALID_COLOUR    }, // COLOUR_MAUVE
    00326   { COLOUR_MAUVE,      INVALID_COLOUR    }, // COLOUR_PURPLE
    00327   { COLOUR_YELLOW,     COLOUR_CREAM      }, // COLOUR_ORANGE
    00328   { COLOUR_CREAM,      INVALID_COLOUR    }, // COLOUR_BROWN
    00329   { COLOUR_WHITE,      INVALID_COLOUR    }, // COLOUR_GREY
    00330   { COLOUR_GREY,       INVALID_COLOUR    }, // COLOUR_WHITE
    00331 };
    00332
    00333 static Colours GenerateCompanyColour()
    00334 {
    00335   Colours colours[COLOUR_END];
    00336
    00337   /* Initialize array */
    00338   for (uint i = 0; i < COLOUR_END; i++) colours[i] = (Colours)i;
    00339
    00340   /* And randomize it */
    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   /* Bubble sort it according to the values in table 1 */
    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   /* Move the colours that look similar to each company's colour to the side */
    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   /* Return the first available colour */
    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   /* we have to generate colour before this company is valid */
    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); // create a random company manager face
    00447
    00448   /* Settings for non-ai companies are copied from the client settings later. */
    00449   if (is_ai) {
    00450     c->engine_renew_money = 100000;
    00451     c->engine_renew_months = 6;
    00452   }
    00453
    00454   GeneratePresidentName(c);
    00455
    00456   InvalidateWindow(WC_GRAPH_LEGEND, 0);
    00457   InvalidateWindow(WC_TOOLBAR_MENU, 0);
    00458   InvalidateWindow(WC_CLIENT_LIST, 0);
    00459
    00460   if (is_ai && (!_networking || _network_server)) AI::StartNew(c->index);
    00461
    00462   c->num_engines = CallocT<uint16>(GetEnginePoolSize());
    00463
    00464   return c;
    00465 }
    00466
    00467 void StartupCompanies()
    00468 {
    00469   _next_competitor_start = 0;
    00470 }
    00471
    00472 static void MaybeStartNewCompany()
    00473 {
    00474 #ifdef ENABLE_NETWORK
    00475   if (_networking && ActiveCompanyCount() >= _settings_client.network.max_companies) return;
    00476 #endif /* ENABLE_NETWORK */
    00477
    00478   Company *c;
    00479
    00480   /* count number of competitors */
    00481   uint n = 0;
    00482   FOR_ALL_COMPANIES(c) {
    00483     if (c->is_ai) n++;
    00484   }
    00485
    00486   if (n < (uint)_settings_game.difficulty.max_no_competitors) {
    00487     /* Send a command to all clients to start up a new AI.
    00488      * Works fine for Multiplayer and Singleplayer */
    00489     DoCommandP(0, 1, 0, CMD_COMPANY_CTRL);
    00490   }
    00491 }
    00492
    00493 void InitializeCompanies()
    00494 {
    00495   _Company_pool.CleanPool();
    00496   _Company_pool.AddBlockToPool();
    00497   _cur_company_tick_index = 0;
    00498 }
    00499
    00500 void OnTick_Companies()
    00501 {
    00502   if (_game_mode == GM_EDITOR) return;
    00503
    00504   if (IsValidCompanyID((CompanyID)_cur_company_tick_index)) {
    00505     Company *c = GetCompany((CompanyID)_cur_company_tick_index);
    00506     if (c->name_1 != 0) GenerateCompanyName(c);
    00507   }
    00508
    00509   if (_next_competitor_start == 0) {
    00510     _next_competitor_start = AI::GetStartNextTime() * DAY_TICKS;
    00511   }
    00512
    00513   if (AI::CanStartNew() && _game_mode != GM_MENU && --_next_competitor_start == 0) {
    00514     MaybeStartNewCompany();
    00515   }
    00516
    00517   _cur_company_tick_index = (_cur_company_tick_index + 1) % MAX_COMPANIES;
    00518 }
    00519
    00520 void CompaniesYearlyLoop()
    00521 {
    00522   Company *c;
    00523
    00524   /* Copy statistics */
    00525   FOR_ALL_COMPANIES(c) {
    00526     memmove(&c->yearly_expenses[1], &c->yearly_expenses[0], sizeof(c->yearly_expenses) - sizeof(c->yearly_expenses[0]));
    00527     memset(&c->yearly_expenses[0], 0, sizeof(c->yearly_expenses[0]));
    00528     InvalidateWindow(WC_FINANCES, c->index);
    00529   }
    00530
    00531   if (_settings_client.gui.show_finances && _local_company != COMPANY_SPECTATOR) {
    00532     ShowCompanyFinances(_local_company);
    00533     c = GetCompany(_local_company);
    00534     if (c->num_valid_stat_ent > 5 && c->old_economy[0].performance_history < c->old_economy[4].performance_history) {
    00535       SndPlayFx(SND_01_BAD_YEAR);
    00536     } else {
    00537       SndPlayFx(SND_00_GOOD_YEAR);
    00538     }
    00539   }
    00540 }
    00541
    00570 CommandCost CmdSetAutoReplace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00571 {
    00572   if (!IsValidCompanyID(_current_company)) return CMD_ERROR;
    00573
    00574   Company *c = GetCompany(_current_company);
    00575   switch (GB(p1, 0, 3)) {
    00576     case 0:
    00577       if (c->engine_renew == HasBit(p2, 0)) return CMD_ERROR;
    00578
    00579       if (flags & DC_EXEC) {
    00580         c->engine_renew = HasBit(p2, 0);
    00581         if (IsLocalCompany()) {
    00582           _settings_client.gui.autorenew = c->engine_renew;
    00583           InvalidateWindow(WC_GAME_OPTIONS, 0);
    00584         }
    00585       }
    00586       break;
    00587
    00588     case 1:
    00589       if (Clamp((int16)p2, -12, 12) != (int16)p2) return CMD_ERROR;
    00590       if (c->engine_renew_months == (int16)p2) return CMD_ERROR;
    00591
    00592       if (flags & DC_EXEC) {
    00593         c->engine_renew_months = (int16)p2;
    00594         if (IsLocalCompany()) {
    00595           _settings_client.gui.autorenew_months = c->engine_renew_months;
    00596           InvalidateWindow(WC_GAME_OPTIONS, 0);
    00597         }
    00598       }
    00599       break;
    00600
    00601     case 2:
    00602       if (ClampU(p2, 0, 2000000) != p2) return CMD_ERROR;
    00603       if (c->engine_renew_money == p2) return CMD_ERROR;
    00604
    00605       if (flags & DC_EXEC) {
    00606         c->engine_renew_money = p2;
    00607         if (IsLocalCompany()) {
    00608           _settings_client.gui.autorenew_money = c->engine_renew_money;
    00609           InvalidateWindow(WC_GAME_OPTIONS, 0);
    00610         }
    00611       }
    00612       break;
    00613
    00614     case 3: {
    00615       EngineID old_engine_type = GB(p2, 0, 16);
    00616       EngineID new_engine_type = GB(p2, 16, 16);
    00617       GroupID id_g = GB(p1, 16, 16);
    00618       CommandCost cost;
    00619
    00620       if (!IsValidGroupID(id_g) && !IsAllGroupID(id_g) && !IsDefaultGroupID(id_g)) return CMD_ERROR;
    00621       if (new_engine_type != INVALID_ENGINE) {
    00622         if (!CheckAutoreplaceValidity(old_engine_type, new_engine_type, _current_company)) return CMD_ERROR;
    00623
    00624         cost = AddEngineReplacementForCompany(c, old_engine_type, new_engine_type, id_g, flags);
    00625       } else {
    00626         cost = RemoveEngineReplacementForCompany(c, old_engine_type, id_g, flags);
    00627       }
    00628
    00629       if (IsLocalCompany()) InvalidateAutoreplaceWindow(old_engine_type, id_g);
    00630
    00631       return cost;
    00632     }
    00633
    00634     case 4:
    00635       if (Clamp((int16)GB(p1, 16, 16), -12, 12) != (int16)GB(p1, 16, 16)) return CMD_ERROR;
    00636       if (ClampU(p2, 0, 2000000) != p2) return CMD_ERROR;
    00637
    00638       if (flags & DC_EXEC) {
    00639         c->engine_renew = HasBit(p1, 15);
    00640         c->engine_renew_months = (int16)GB(p1, 16, 16);
    00641         c->engine_renew_money = p2;
    00642
    00643         if (IsLocalCompany()) {
    00644           _settings_client.gui.autorenew = c->engine_renew;
    00645           _settings_client.gui.autorenew_months = c->engine_renew_months;
    00646           _settings_client.gui.autorenew_money = c->engine_renew_money;
    00647           InvalidateWindow(WC_GAME_OPTIONS, 0);
    00648         }
    00649       }
    00650       break;
    00651
    00652     case 5:
    00653       if (c->renew_keep_length == HasBit(p2, 0)) return CMD_ERROR;
    00654
    00655       if (flags & DC_EXEC) {
    00656         c->renew_keep_length = HasBit(p2, 0);
    00657         if (IsLocalCompany()) {
    00658           InvalidateWindow(WC_REPLACE_VEHICLE, VEH_TRAIN);
    00659         }
    00660       }
    00661     break;
    00662   }
    00663
    00664   return CommandCost();
    00665 }
    00666
    00672 void CompanyNewsInformation::FillData(const Company *c, const Company *other)
    00673 {
    00674   SetDParam(0, c->index);
    00675   GetString(this->company_name, STR_COMPANY_NAME, lastof(this->company_name));
    00676
    00677   if (other == NULL) {
    00678     *this->other_company_name = '\0';
    00679   } else {
    00680     SetDParam(0, other->index);
    00681     GetString(this->other_company_name, STR_COMPANY_NAME, lastof(this->other_company_name));
    00682     c = other;
    00683   }
    00684
    00685   SetDParam(0, c->index);
    00686   GetString(this->president_name, STR_7058_PRESIDENT, lastof(this->president_name));
    00687
    00688   this->colour = c->colour;
    00689   this->face = c->face;
    00690
    00691 }
    00692
    00713 CommandCost CmdCompanyCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00714 {
    00715   if (flags & DC_EXEC) _current_company = OWNER_NONE;
    00716
    00717   InvalidateWindowData(WC_COMPANY_LEAGUE, 0, 0);
    00718
    00719   switch (p1) {
    00720     case 0: { // Create a new company
    00721       /* This command is only executed in a multiplayer game */
    00722       if (!_networking) return CMD_ERROR;
    00723
    00724 #ifdef ENABLE_NETWORK
    00725
    00726       /* Joining Client:
    00727        * _local_company: COMPANY_SPECTATOR
    00728        * _network_playas/cid = requested company/clientid
    00729        *
    00730        * Other client(s)/server:
    00731        * _local_company/_network_playas: what they play as
    00732        * cid = requested company/company of joining client */
    00733       ClientID cid = (ClientID)p2;
    00734
    00735       /* Has the network client a correct ClientIndex? */
    00736       if (!(flags & DC_EXEC)) return CommandCost();
    00737       NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(cid);
    00738       if (ci == NULL) return CommandCost();
    00739
    00740       /* Delete multiplayer progress bar */
    00741       DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
    00742
    00743       Company *c = DoStartupNewCompany(false);
    00744
    00745       /* A new company could not be created, revert to being a spectator */
    00746       if (c == NULL) {
    00747         if (_network_server) {
    00748           ci->client_playas = COMPANY_SPECTATOR;
    00749           NetworkUpdateClientInfo(ci->client_id);
    00750         } else if (_local_company == COMPANY_SPECTATOR) {
    00751           _network_playas = COMPANY_SPECTATOR;
    00752         }
    00753         break;
    00754       }
    00755
    00756       /* This is the client (or non-dedicated server) who wants a new company */
    00757       if (cid == _network_own_client_id) {
    00758         /* Create p1 and p2 here because SetLocalCompany resets the gui.autorenew* settings. */
    00759         uint32 p1 = (_settings_client.gui.autorenew << 15 ) | (_settings_client.gui.autorenew_months << 16) | 4;
    00760         uint32 p2 = _settings_client.gui.autorenew_money;
    00761         assert(_local_company == COMPANY_SPECTATOR);
    00762         SetLocalCompany(c->index);
    00763         if (!StrEmpty(_settings_client.network.default_company_pass)) {
    00764           char *password = _settings_client.network.default_company_pass;
    00765           NetworkChangeCompanyPassword(1, &password);
    00766         }
    00767
    00768         _current_company = _local_company;
    00769
    00770         /* Now that we have a new company, broadcast our autorenew settings to
    00771          * all clients so everything is in sync */
    00772         NetworkSend_Command(0, p1, p2, CMD_SET_AUTOREPLACE, NULL, NULL);
    00773
    00774         MarkWholeScreenDirty();
    00775       }
    00776
    00777       if (_network_server) {
    00778         /* XXX - UGLY! p2 (pid) is mis-used to fetch the client-id, done at
    00779          * server side in network_server.c:838, function
    00780          * DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND) */
    00781         CompanyID old_playas = ci->client_playas;
    00782         ci->client_playas = c->index;
    00783         NetworkUpdateClientInfo(ci->client_id);
    00784
    00785         if (IsValidCompanyID(ci->client_playas)) {
    00786           CompanyID company_backup = _local_company;
    00787           _network_company_states[c->index].months_empty = 0;
    00788           _network_company_states[c->index].password[0] = '\0';
    00789           NetworkServerUpdateCompanyPassworded(ci->client_playas, false);
    00790
    00791           /* XXX - When a client joins, we automatically set its name to the
    00792            * client's name (for some reason). As it stands now only the server
    00793            * knows the client's name, so it needs to send out a "broadcast" to
    00794            * do this. To achieve this we send a network command. However, it
    00795            * uses _local_company to execute the command as.  To prevent abuse
    00796            * (eg. only yourself can change your name/company), we 'cheat' by
    00797            * impersonation _local_company as the server. Not the best solution;
    00798            * but it works.
    00799            * TODO: Perhaps this could be improved by when the client is ready
    00800            * with joining to let it send itself the command, and not the server?
    00801            * For example in network_client.c:534? */
    00802           _local_company = ci->client_playas;
    00803           NetworkSend_Command(0, 0, 0, CMD_RENAME_PRESIDENT, NULL, ci->client_name);
    00804           _local_company = company_backup;
    00805         }
    00806
    00807         /* Announce new company on network, if the client was a SPECTATOR before */
    00808         if (old_playas == COMPANY_SPECTATOR) {
    00809           NetworkServerSendChat(NETWORK_ACTION_COMPANY_NEW, DESTTYPE_BROADCAST, 0, "", ci->client_id, ci->client_playas + 1);
    00810         }
    00811       }
    00812 #endif /* ENABLE_NETWORK */
    00813     } break;
    00814
    00815     case 1: // Make a new AI company
    00816       if (!(flags & DC_EXEC)) return CommandCost();
    00817
    00818       DoStartupNewCompany(true);
    00819       break;
    00820
    00821     case 2: { // Delete a company
    00822       Company *c;
    00823
    00824       if (!IsValidCompanyID((CompanyID)p2)) return CMD_ERROR;
    00825
    00826       if (!(flags & DC_EXEC)) return CommandCost();
    00827
    00828       c = GetCompany((CompanyID)p2);
    00829
    00830       /* Delete any open window of the company */
    00831       DeleteCompanyWindows(c->index);
    00832       CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
    00833       cni->FillData(c);
    00834
    00835       /* Show the bankrupt news */
    00836       SetDParam(0, STR_705C_BANKRUPT);
    00837       SetDParam(1, STR_705D_HAS_BEEN_CLOSED_DOWN_BY);
    00838       SetDParamStr(2, cni->company_name);
    00839       AddNewsItem(STR_02B6, NS_COMPANY_BANKRUPT, 0, 0, cni);
    00840
    00841       /* Remove the company */
    00842       ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER);
    00843       if (!IsHumanCompany(c->index)) AI::Stop(c->index);
    00844
    00845       CompanyID c_index = c->index;
    00846       delete c;
    00847       AI::BroadcastNewEvent(new AIEventCompanyBankrupt(c_index));
    00848     } break;
    00849
    00850     case 3: { // Merge a company (#1) into another company (#2), elimination company #1
    00851       CompanyID cid_old = (CompanyID)GB(p2,  0, 16);
    00852       CompanyID cid_new = (CompanyID)GB(p2, 16, 16);
    00853
    00854       if (!IsValidCompanyID(cid_old) || !IsValidCompanyID(cid_new)) return CMD_ERROR;
    00855
    00856       if (!(flags & DC_EXEC)) return CMD_ERROR;
    00857
    00858       ChangeOwnershipOfCompanyItems(cid_old, cid_new);
    00859       delete GetCompany(cid_old);
    00860     } break;
    00861
    00862     default: return CMD_ERROR;
    00863   }
    00864
    00865   return CommandCost();
    00866 }