company_cmd.cpp

Go to the documentation of this file.
00001 /* $Id: company_cmd.cpp 17629 2009-09-24 19:22:32Z 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 
00063   /* Zero the memory of the company; we might 'reuse' it later on.
00064    * This is more a hack than a proper fix, but... it's already
00065    * fixed in trunk by the new pool system and this is the only
00066    * troublesome case in 0.7, so we'll leave it at this fix. */
00067   CompanyID id = this->index;
00068   memset(this, 0, sizeof(*this));
00069   this->index = id;
00070 }
00071 
00078 void SetLocalCompany(CompanyID new_company)
00079 {
00080   /* company could also be COMPANY_SPECTATOR or OWNER_NONE */
00081   assert(IsValidCompanyID(new_company) || new_company == COMPANY_SPECTATOR || new_company == OWNER_NONE);
00082 
00083   _local_company = new_company;
00084 
00085   /* Do not update the settings if we are in the intro GUI */
00086   if (IsValidCompanyID(new_company) && _game_mode != GM_MENU) {
00087     const Company *c = GetCompany(new_company);
00088     _settings_client.gui.autorenew        = c->engine_renew;
00089     _settings_client.gui.autorenew_months = c->engine_renew_months;
00090     _settings_client.gui.autorenew_money  = c->engine_renew_money;
00091     InvalidateWindow(WC_GAME_OPTIONS, 0);
00092   }
00093 
00094   /* Delete any construction windows... */
00095   DeleteConstructionWindows();
00096 
00097   /* ... and redraw the whole screen. */
00098   MarkWholeScreenDirty();
00099 }
00100 
00101 bool IsHumanCompany(CompanyID company)
00102 {
00103   return !GetCompany(company)->is_ai;
00104 }
00105 
00106 
00107 uint16 GetDrawStringCompanyColour(CompanyID company)
00108 {
00109   /* Get the colour for DrawString-subroutines which matches the colour
00110    * of the company */
00111   if (!IsValidCompanyID(company)) return _colour_gradient[COLOUR_WHITE][4] | IS_PALETTE_COLOUR;
00112   return (_colour_gradient[_company_colours[company]][4]) | IS_PALETTE_COLOUR;
00113 }
00114 
00115 void DrawCompanyIcon(CompanyID c, int x, int y)
00116 {
00117   DrawSprite(SPR_PLAYER_ICON, COMPANY_SPRITE_COLOUR(c), x, y);
00118 }
00119 
00126 bool IsValidCompanyManagerFace(CompanyManagerFace cmf)
00127 {
00128   if (!AreCompanyManagerFaceBitsValid(cmf, CMFV_GEN_ETHN, GE_WM)) return false;
00129 
00130   GenderEthnicity ge   = (GenderEthnicity)GetCompanyManagerFaceBits(cmf, CMFV_GEN_ETHN, GE_WM);
00131   bool has_moustache   = !HasBit(ge, GENDER_FEMALE) && GetCompanyManagerFaceBits(cmf, CMFV_HAS_MOUSTACHE,   ge) != 0;
00132   bool has_tie_earring = !HasBit(ge, GENDER_FEMALE) || GetCompanyManagerFaceBits(cmf, CMFV_HAS_TIE_EARRING, ge) != 0;
00133   bool has_glasses     = GetCompanyManagerFaceBits(cmf, CMFV_HAS_GLASSES, ge) != 0;
00134 
00135   if (!AreCompanyManagerFaceBitsValid(cmf, CMFV_EYE_COLOUR, ge)) return false;
00136   for (CompanyManagerFaceVariable cmfv = CMFV_CHEEKS; cmfv < CMFV_END; cmfv++) {
00137     switch (cmfv) {
00138       case CMFV_MOUSTACHE:   if (!has_moustache)   continue; break;
00139       case CMFV_LIPS:        // FALL THROUGH
00140       case CMFV_NOSE:        if (has_moustache)    continue; break;
00141       case CMFV_TIE_EARRING: if (!has_tie_earring) continue; break;
00142       case CMFV_GLASSES:     if (!has_glasses)     continue; break;
00143       default: break;
00144     }
00145     if (!AreCompanyManagerFaceBitsValid(cmf, cmfv, ge)) return false;
00146   }
00147 
00148   return true;
00149 }
00150 
00151 void InvalidateCompanyWindows(const Company *company)
00152 {
00153   CompanyID cid = company->index;
00154 
00155   if (cid == _local_company) InvalidateWindow(WC_STATUS_BAR, 0);
00156   InvalidateWindow(WC_FINANCES, cid);
00157 }
00158 
00159 bool CheckCompanyHasMoney(CommandCost cost)
00160 {
00161   if (cost.GetCost() > 0) {
00162     CompanyID company = _current_company;
00163     if (IsValidCompanyID(company) && cost.GetCost() > GetCompany(company)->money) {
00164       SetDParam(0, cost.GetCost());
00165       _error_message = STR_0003_NOT_ENOUGH_CASH_REQUIRES;
00166       return false;
00167     }
00168   }
00169   return true;
00170 }
00171 
00172 static void SubtractMoneyFromAnyCompany(Company *c, CommandCost cost)
00173 {
00174   if (cost.GetCost() == 0) return;
00175   assert(cost.GetExpensesType() != INVALID_EXPENSES);
00176 
00177   c->money -= cost.GetCost();
00178   c->yearly_expenses[0][cost.GetExpensesType()] += cost.GetCost();
00179 
00180   if (HasBit(1 << EXPENSES_TRAIN_INC    |
00181              1 << EXPENSES_ROADVEH_INC  |
00182              1 << EXPENSES_AIRCRAFT_INC |
00183              1 << EXPENSES_SHIP_INC, cost.GetExpensesType())) {
00184     c->cur_economy.income -= cost.GetCost();
00185   } else if (HasBit(1 << EXPENSES_TRAIN_RUN    |
00186                     1 << EXPENSES_ROADVEH_RUN  |
00187                     1 << EXPENSES_AIRCRAFT_RUN |
00188                     1 << EXPENSES_SHIP_RUN     |
00189                     1 << EXPENSES_PROPERTY     |
00190                     1 << EXPENSES_LOAN_INT, cost.GetExpensesType())) {
00191     c->cur_economy.expenses -= cost.GetCost();
00192   }
00193 
00194   InvalidateCompanyWindows(c);
00195 }
00196 
00197 void SubtractMoneyFromCompany(CommandCost cost)
00198 {
00199   CompanyID cid = _current_company;
00200 
00201   if (IsValidCompanyID(cid)) SubtractMoneyFromAnyCompany(GetCompany(cid), cost);
00202 }
00203 
00204 void SubtractMoneyFromCompanyFract(CompanyID company, CommandCost cst)
00205 {
00206   Company *c = GetCompany(company);
00207   byte m = c->money_fraction;
00208   Money cost = cst.GetCost();
00209 
00210   c->money_fraction = m - (byte)cost;
00211   cost >>= 8;
00212   if (c->money_fraction > m) cost++;
00213   if (cost != 0) SubtractMoneyFromAnyCompany(c, CommandCost(cst.GetExpensesType(), cost));
00214 }
00215 
00216 void GetNameOfOwner(Owner owner, TileIndex tile)
00217 {
00218   SetDParam(2, owner);
00219 
00220   if (owner != OWNER_TOWN) {
00221     if (!IsValidCompanyID(owner)) {
00222       SetDParam(0, STR_0150_SOMEONE);
00223     } else {
00224       SetDParam(0, STR_COMPANY_NAME);
00225       SetDParam(1, owner);
00226     }
00227   } else {
00228     const Town *t = ClosestTownFromTile(tile, UINT_MAX);
00229 
00230     SetDParam(0, STR_TOWN);
00231     SetDParam(1, t->index);
00232   }
00233 }
00234 
00235 
00236 bool CheckOwnership(Owner owner)
00237 {
00238   assert(owner < OWNER_END);
00239 
00240   if (owner == _current_company) return true;
00241   _error_message = STR_013B_OWNED_BY;
00242   GetNameOfOwner(owner, 0);
00243   return false;
00244 }
00245 
00246 bool CheckTileOwnership(TileIndex tile)
00247 {
00248   Owner owner = GetTileOwner(tile);
00249 
00250   assert(owner < OWNER_END);
00251 
00252   if (owner == _current_company) return true;
00253   _error_message = STR_013B_OWNED_BY;
00254 
00255   /* no need to get the name of the owner unless we're the local company (saves some time) */
00256   if (IsLocalCompany()) GetNameOfOwner(owner, tile);
00257   return false;
00258 }
00259 
00260 static void GenerateCompanyName(Company *c)
00261 {
00262   TileIndex tile;
00263   Town *t;
00264   StringID str;
00265   Company *cc;
00266   uint32 strp;
00267   /* Reserve space for extra unicode character. We need to do this to be able
00268    * to detect too long company name. */
00269   char buffer[MAX_LENGTH_COMPANY_NAME_BYTES + MAX_CHAR_LENGTH];
00270 
00271   if (c->name_1 != STR_SV_UNNAMED) return;
00272 
00273   tile = c->last_build_coordinate;
00274   if (tile == 0) return;
00275 
00276   t = ClosestTownFromTile(tile, UINT_MAX);
00277 
00278   if (t->name == NULL && IsInsideMM(t->townnametype, SPECSTR_TOWNNAME_START, SPECSTR_TOWNNAME_LAST + 1)) {
00279     str = t->townnametype - SPECSTR_TOWNNAME_START + SPECSTR_PLAYERNAME_START;
00280     strp = t->townnameparts;
00281 
00282 verify_name:;
00283     /* No companies must have this name already */
00284     FOR_ALL_COMPANIES(cc) {
00285       if (cc->name_1 == str && cc->name_2 == strp) goto bad_town_name;
00286     }
00287 
00288     GetString(buffer, str, lastof(buffer));
00289     if (strlen(buffer) >= MAX_LENGTH_COMPANY_NAME_BYTES) goto bad_town_name;
00290 
00291 set_name:;
00292     c->name_1 = str;
00293     c->name_2 = strp;
00294 
00295     MarkWholeScreenDirty();
00296 
00297     if (!IsHumanCompany(c->index)) {
00298       CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
00299       cni->FillData(c);
00300       SetDParam(0, STR_705E_NEW_TRANSPORT_COMPANY_LAUNCHED);
00301       SetDParam(1, STR_705F_STARTS_CONSTRUCTION_NEAR);
00302       SetDParamStr(2, cni->company_name);
00303       SetDParam(3, t->index);
00304       AddNewsItem(STR_02B6, NS_COMPANY_NEW, c->last_build_coordinate, 0, cni);
00305     }
00306     AI::BroadcastNewEvent(new AIEventCompanyNew(c->index), c->index);
00307     return;
00308   }
00309 bad_town_name:;
00310 
00311   if (c->president_name_1 == SPECSTR_PRESIDENT_NAME) {
00312     str = SPECSTR_ANDCO_NAME;
00313     strp = c->president_name_2;
00314     goto set_name;
00315   } else {
00316     str = SPECSTR_ANDCO_NAME;
00317     strp = Random();
00318     goto verify_name;
00319   }
00320 }
00321 
00322 static const byte _colour_sort[COLOUR_END] = {2, 2, 3, 2, 3, 2, 3, 2, 3, 2, 2, 2, 3, 1, 1, 1};
00323 static const Colours _similar_colour[COLOUR_END][2] = {
00324   { COLOUR_BLUE,       COLOUR_LIGHT_BLUE }, // COLOUR_DARK_BLUE
00325   { COLOUR_GREEN,      COLOUR_DARK_GREEN }, // COLOUR_PALE_GREEN
00326   { INVALID_COLOUR,    INVALID_COLOUR    }, // COLOUR_PINK
00327   { COLOUR_ORANGE,     INVALID_COLOUR    }, // COLOUR_YELLOW
00328   { INVALID_COLOUR,    INVALID_COLOUR    }, // COLOUR_RED
00329   { COLOUR_DARK_BLUE,  COLOUR_BLUE       }, // COLOUR_LIGHT_BLUE
00330   { COLOUR_PALE_GREEN, COLOUR_DARK_GREEN }, // COLOUR_GREEN
00331   { COLOUR_PALE_GREEN, COLOUR_GREEN      }, // COLOUR_DARK_GREEN
00332   { COLOUR_DARK_BLUE,  COLOUR_LIGHT_BLUE }, // COLOUR_BLUE
00333   { COLOUR_BROWN,      COLOUR_ORANGE     }, // COLOUR_CREAM
00334   { COLOUR_PURPLE,     INVALID_COLOUR    }, // COLOUR_MAUVE
00335   { COLOUR_MAUVE,      INVALID_COLOUR    }, // COLOUR_PURPLE
00336   { COLOUR_YELLOW,     COLOUR_CREAM      }, // COLOUR_ORANGE
00337   { COLOUR_CREAM,      INVALID_COLOUR    }, // COLOUR_BROWN
00338   { COLOUR_WHITE,      INVALID_COLOUR    }, // COLOUR_GREY
00339   { COLOUR_GREY,       INVALID_COLOUR    }, // COLOUR_WHITE
00340 };
00341 
00342 static Colours GenerateCompanyColour()
00343 {
00344   Colours colours[COLOUR_END];
00345 
00346   /* Initialize array */
00347   for (uint i = 0; i < COLOUR_END; i++) colours[i] = (Colours)i;
00348 
00349   /* And randomize it */
00350   for (uint i = 0; i < 100; i++) {
00351     uint r = Random();
00352     Swap(colours[GB(r, 0, 4)], colours[GB(r, 4, 4)]);
00353   }
00354 
00355   /* Bubble sort it according to the values in table 1 */
00356   for (uint i = 0; i < COLOUR_END; i++) {
00357     for (uint j = 1; j < COLOUR_END; j++) {
00358       if (_colour_sort[colours[j - 1]] < _colour_sort[colours[j]]) {
00359         Swap(colours[j - 1], colours[j]);
00360       }
00361     }
00362   };
00363 
00364   /* Move the colours that look similar to each company's colour to the side */
00365   Company *c;
00366   FOR_ALL_COMPANIES(c) {
00367     Colours pcolour = (Colours)c->colour;
00368 
00369     for (uint i = 0; i < COLOUR_END; i++) {
00370       if (colours[i] == pcolour) {
00371         colours[i] = INVALID_COLOUR;
00372         break;
00373       }
00374     }
00375 
00376     for (uint j = 0; j < 2; j++) {
00377       Colours similar = _similar_colour[pcolour][j];
00378       if (similar == INVALID_COLOUR) break;
00379 
00380       for (uint i = 1; i < COLOUR_END; i++) {
00381         if (colours[i - 1] == similar) Swap(colours[i - 1], colours[i]);
00382       }
00383     }
00384   }
00385 
00386   /* Return the first available colour */
00387   for (uint i = 0; i < COLOUR_END; i++) {
00388     if (colours[i] != INVALID_COLOUR) return colours[i];
00389   }
00390 
00391   NOT_REACHED();
00392 }
00393 
00394 static void GeneratePresidentName(Company *c)
00395 {
00396   for (;;) {
00397 restart:;
00398     c->president_name_2 = Random();
00399     c->president_name_1 = SPECSTR_PRESIDENT_NAME;
00400 
00401     /* Reserve space for extra unicode character. We need to do this to be able
00402      * to detect too long president name. */
00403     char buffer[MAX_LENGTH_PRESIDENT_NAME_BYTES + MAX_CHAR_LENGTH];
00404     SetDParam(0, c->index);
00405     GetString(buffer, STR_PRESIDENT_NAME, lastof(buffer));
00406     if (strlen(buffer) >= MAX_LENGTH_PRESIDENT_NAME_BYTES) continue;
00407 
00408     Company *cc;
00409     FOR_ALL_COMPANIES(cc) {
00410       if (c != cc) {
00411         /* Reserve extra space so even overlength president names can be compared. */
00412         char buffer2[MAX_LENGTH_PRESIDENT_NAME_BYTES + MAX_CHAR_LENGTH];
00413         SetDParam(0, cc->index);
00414         GetString(buffer2, STR_PRESIDENT_NAME, lastof(buffer2));
00415         if (strcmp(buffer2, buffer) == 0) goto restart;
00416       }
00417     }
00418     return;
00419   }
00420 }
00421 
00422 void ResetCompanyLivery(Company *c)
00423 {
00424   for (LiveryScheme scheme = LS_BEGIN; scheme < LS_END; scheme++) {
00425     c->livery[scheme].in_use  = false;
00426     c->livery[scheme].colour1 = c->colour;
00427     c->livery[scheme].colour2 = c->colour;
00428   }
00429 }
00430 
00438 Company *DoStartupNewCompany(bool is_ai, CompanyID company = INVALID_COMPANY)
00439 {
00440   if (ActiveCompanyCount() == MAX_COMPANIES || !Company::CanAllocateItem()) return NULL;
00441 
00442   /* we have to generate colour before this company is valid */
00443   Colours colour = GenerateCompanyColour();
00444 
00445   Company *c;
00446   if (company == INVALID_COMPANY) {
00447     c = new Company(STR_SV_UNNAMED, is_ai);
00448   } else {
00449     if (IsValidCompanyID(company)) return NULL;
00450     c = new (company) Company(STR_SV_UNNAMED, is_ai);
00451   }
00452 
00453   c->colour = colour;
00454 
00455   ResetCompanyLivery(c);
00456   _company_colours[c->index] = (Colours)c->colour;
00457 
00458   c->money = c->current_loan = 100000;
00459 
00460   c->share_owners[0] = c->share_owners[1] = c->share_owners[2] = c->share_owners[3] = INVALID_OWNER;
00461 
00462   c->avail_railtypes = GetCompanyRailtypes(c->index);
00463   c->avail_roadtypes = GetCompanyRoadtypes(c->index);
00464   c->inaugurated_year = _cur_year;
00465   RandomCompanyManagerFaceBits(c->face, (GenderEthnicity)Random(), false); // create a random company manager face
00466 
00467   /* Settings for non-ai companies are copied from the client settings later. */
00468   if (is_ai) {
00469     c->engine_renew_money = 100000;
00470     c->engine_renew_months = 6;
00471   }
00472 
00473   GeneratePresidentName(c);
00474 
00475   InvalidateWindow(WC_GRAPH_LEGEND, 0);
00476   InvalidateWindow(WC_TOOLBAR_MENU, 0);
00477   InvalidateWindow(WC_CLIENT_LIST, 0);
00478 
00479   if (is_ai && (!_networking || _network_server)) AI::StartNew(c->index);
00480 
00481   c->num_engines = CallocT<uint16>(GetEnginePoolSize());
00482 
00483   return c;
00484 }
00485 
00486 void StartupCompanies()
00487 {
00488   _next_competitor_start = 0;
00489 }
00490 
00491 static void MaybeStartNewCompany()
00492 {
00493 #ifdef ENABLE_NETWORK
00494   if (_networking && ActiveCompanyCount() >= _settings_client.network.max_companies) return;
00495 #endif /* ENABLE_NETWORK */
00496 
00497   Company *c;
00498 
00499   /* count number of competitors */
00500   uint n = 0;
00501   FOR_ALL_COMPANIES(c) {
00502     if (c->is_ai) n++;
00503   }
00504 
00505   if (n < (uint)_settings_game.difficulty.max_no_competitors) {
00506     /* Send a command to all clients to start up a new AI.
00507      * Works fine for Multiplayer and Singleplayer */
00508     DoCommandP(0, 1, INVALID_COMPANY, CMD_COMPANY_CTRL);
00509   }
00510 }
00511 
00512 void InitializeCompanies()
00513 {
00514   _Company_pool.CleanPool();
00515   _Company_pool.AddBlockToPool();
00516   _cur_company_tick_index = 0;
00517 }
00518 
00519 void OnTick_Companies()
00520 {
00521   if (_game_mode == GM_EDITOR) return;
00522 
00523   if (IsValidCompanyID((CompanyID)_cur_company_tick_index)) {
00524     Company *c = GetCompany((CompanyID)_cur_company_tick_index);
00525     if (c->name_1 != 0) GenerateCompanyName(c);
00526   }
00527 
00528   if (_next_competitor_start == 0) {
00529     _next_competitor_start = AI::GetStartNextTime() * DAY_TICKS;
00530   }
00531 
00532   if (AI::CanStartNew() && _game_mode != GM_MENU && --_next_competitor_start == 0) {
00533     MaybeStartNewCompany();
00534   }
00535 
00536   _cur_company_tick_index = (_cur_company_tick_index + 1) % MAX_COMPANIES;
00537 }
00538 
00539 void CompaniesYearlyLoop()
00540 {
00541   Company *c;
00542 
00543   /* Copy statistics */
00544   FOR_ALL_COMPANIES(c) {
00545     memmove(&c->yearly_expenses[1], &c->yearly_expenses[0], sizeof(c->yearly_expenses) - sizeof(c->yearly_expenses[0]));
00546     memset(&c->yearly_expenses[0], 0, sizeof(c->yearly_expenses[0]));
00547     InvalidateWindow(WC_FINANCES, c->index);
00548   }
00549 
00550   if (_settings_client.gui.show_finances && _local_company != COMPANY_SPECTATOR) {
00551     ShowCompanyFinances(_local_company);
00552     c = GetCompany(_local_company);
00553     if (c->num_valid_stat_ent > 5 && c->old_economy[0].performance_history < c->old_economy[4].performance_history) {
00554       SndPlayFx(SND_01_BAD_YEAR);
00555     } else {
00556       SndPlayFx(SND_00_GOOD_YEAR);
00557     }
00558   }
00559 }
00560 
00589 CommandCost CmdSetAutoReplace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00590 {
00591   if (!IsValidCompanyID(_current_company)) return CMD_ERROR;
00592 
00593   Company *c = GetCompany(_current_company);
00594   switch (GB(p1, 0, 3)) {
00595     case 0:
00596       if (c->engine_renew == HasBit(p2, 0)) return CMD_ERROR;
00597 
00598       if (flags & DC_EXEC) {
00599         c->engine_renew = HasBit(p2, 0);
00600         if (IsLocalCompany()) {
00601           _settings_client.gui.autorenew = c->engine_renew;
00602           InvalidateWindow(WC_GAME_OPTIONS, 0);
00603         }
00604       }
00605       break;
00606 
00607     case 1:
00608       if (Clamp((int16)p2, -12, 12) != (int16)p2) return CMD_ERROR;
00609       if (c->engine_renew_months == (int16)p2) return CMD_ERROR;
00610 
00611       if (flags & DC_EXEC) {
00612         c->engine_renew_months = (int16)p2;
00613         if (IsLocalCompany()) {
00614           _settings_client.gui.autorenew_months = c->engine_renew_months;
00615           InvalidateWindow(WC_GAME_OPTIONS, 0);
00616         }
00617       }
00618       break;
00619 
00620     case 2:
00621       if (ClampU(p2, 0, 2000000) != p2) return CMD_ERROR;
00622       if (c->engine_renew_money == p2) return CMD_ERROR;
00623 
00624       if (flags & DC_EXEC) {
00625         c->engine_renew_money = p2;
00626         if (IsLocalCompany()) {
00627           _settings_client.gui.autorenew_money = c->engine_renew_money;
00628           InvalidateWindow(WC_GAME_OPTIONS, 0);
00629         }
00630       }
00631       break;
00632 
00633     case 3: {
00634       EngineID old_engine_type = GB(p2, 0, 16);
00635       EngineID new_engine_type = GB(p2, 16, 16);
00636       GroupID id_g = GB(p1, 16, 16);
00637       CommandCost cost;
00638 
00639       if (!IsValidGroupID(id_g) && !IsAllGroupID(id_g) && !IsDefaultGroupID(id_g)) return CMD_ERROR;
00640       if (new_engine_type != INVALID_ENGINE) {
00641         if (!CheckAutoreplaceValidity(old_engine_type, new_engine_type, _current_company)) return CMD_ERROR;
00642 
00643         cost = AddEngineReplacementForCompany(c, old_engine_type, new_engine_type, id_g, flags);
00644       } else {
00645         cost = RemoveEngineReplacementForCompany(c, old_engine_type, id_g, flags);
00646       }
00647 
00648       if (IsLocalCompany()) InvalidateAutoreplaceWindow(old_engine_type, id_g);
00649 
00650       return cost;
00651     }
00652 
00653     case 4:
00654       if (Clamp((int16)GB(p1, 16, 16), -12, 12) != (int16)GB(p1, 16, 16)) return CMD_ERROR;
00655       if (ClampU(p2, 0, 2000000) != p2) return CMD_ERROR;
00656 
00657       if (flags & DC_EXEC) {
00658         c->engine_renew = HasBit(p1, 15);
00659         c->engine_renew_months = (int16)GB(p1, 16, 16);
00660         c->engine_renew_money = p2;
00661 
00662         if (IsLocalCompany()) {
00663           _settings_client.gui.autorenew = c->engine_renew;
00664           _settings_client.gui.autorenew_months = c->engine_renew_months;
00665           _settings_client.gui.autorenew_money = c->engine_renew_money;
00666           InvalidateWindow(WC_GAME_OPTIONS, 0);
00667         }
00668       }
00669       break;
00670 
00671     case 5:
00672       if (c->renew_keep_length == HasBit(p2, 0)) return CMD_ERROR;
00673 
00674       if (flags & DC_EXEC) {
00675         c->renew_keep_length = HasBit(p2, 0);
00676         if (IsLocalCompany()) {
00677           InvalidateWindow(WC_REPLACE_VEHICLE, VEH_TRAIN);
00678         }
00679       }
00680     break;
00681   }
00682 
00683   return CommandCost();
00684 }
00685 
00691 void CompanyNewsInformation::FillData(const Company *c, const Company *other)
00692 {
00693   SetDParam(0, c->index);
00694   GetString(this->company_name, STR_COMPANY_NAME, lastof(this->company_name));
00695 
00696   if (other == NULL) {
00697     *this->other_company_name = '\0';
00698   } else {
00699     SetDParam(0, other->index);
00700     GetString(this->other_company_name, STR_COMPANY_NAME, lastof(this->other_company_name));
00701     c = other;
00702   }
00703 
00704   SetDParam(0, c->index);
00705   GetString(this->president_name, STR_7058_PRESIDENT, lastof(this->president_name));
00706 
00707   this->colour = c->colour;
00708   this->face = c->face;
00709 
00710 }
00711 
00733 CommandCost CmdCompanyCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00734 {
00735   if (flags & DC_EXEC) _current_company = OWNER_NONE;
00736 
00737   InvalidateWindowData(WC_COMPANY_LEAGUE, 0, 0);
00738 
00739   switch (p1) {
00740     case 0: { // Create a new company
00741       /* This command is only executed in a multiplayer game */
00742       if (!_networking) return CMD_ERROR;
00743 
00744 #ifdef ENABLE_NETWORK
00745 
00746       /* Joining Client:
00747        * _local_company: COMPANY_SPECTATOR
00748        * _network_playas/cid = requested company/clientid
00749        *
00750        * Other client(s)/server:
00751        * _local_company/_network_playas: what they play as
00752        * cid = requested company/company of joining client */
00753       ClientID cid = (ClientID)p2;
00754 
00755       /* Has the network client a correct ClientIndex? */
00756       if (!(flags & DC_EXEC)) return CommandCost();
00757       NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(cid);
00758       if (ci == NULL) return CommandCost();
00759 
00760       /* Delete multiplayer progress bar */
00761       DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
00762 
00763       Company *c = DoStartupNewCompany(false);
00764 
00765       /* A new company could not be created, revert to being a spectator */
00766       if (c == NULL) {
00767         if (_network_server) {
00768           ci->client_playas = COMPANY_SPECTATOR;
00769           NetworkUpdateClientInfo(ci->client_id);
00770         } else if (_local_company == COMPANY_SPECTATOR) {
00771           _network_playas = COMPANY_SPECTATOR;
00772         }
00773         break;
00774       }
00775 
00776       /* This is the client (or non-dedicated server) who wants a new company */
00777       if (cid == _network_own_client_id) {
00778         /* Create p1 and p2 here because SetLocalCompany resets the gui.autorenew* settings. */
00779         uint32 p1 = (_settings_client.gui.autorenew << 15 ) | (_settings_client.gui.autorenew_months << 16) | 4;
00780         uint32 p2 = _settings_client.gui.autorenew_money;
00781         assert(_local_company == COMPANY_SPECTATOR);
00782         SetLocalCompany(c->index);
00783         if (!StrEmpty(_settings_client.network.default_company_pass)) {
00784           char *password = _settings_client.network.default_company_pass;
00785           NetworkChangeCompanyPassword(1, &password);
00786         }
00787 
00788         _current_company = _local_company;
00789 
00790         /* Now that we have a new company, broadcast our autorenew settings to
00791          * all clients so everything is in sync */
00792         NetworkSend_Command(0, p1, p2, CMD_SET_AUTOREPLACE, NULL, NULL);
00793 
00794         MarkWholeScreenDirty();
00795       }
00796 
00797       if (_network_server) {
00798         /* XXX - UGLY! p2 (pid) is mis-used to fetch the client-id, done at
00799          * server side in network_server.c:838, function
00800          * DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND) */
00801         CompanyID old_playas = ci->client_playas;
00802         ci->client_playas = c->index;
00803         NetworkUpdateClientInfo(ci->client_id);
00804 
00805         if (IsValidCompanyID(ci->client_playas)) {
00806           CompanyID company_backup = _local_company;
00807           _network_company_states[c->index].months_empty = 0;
00808           _network_company_states[c->index].password[0] = '\0';
00809           NetworkServerUpdateCompanyPassworded(ci->client_playas, false);
00810 
00811           /* XXX - When a client joins, we automatically set its name to the
00812            * client's name (for some reason). As it stands now only the server
00813            * knows the client's name, so it needs to send out a "broadcast" to
00814            * do this. To achieve this we send a network command. However, it
00815            * uses _local_company to execute the command as.  To prevent abuse
00816            * (eg. only yourself can change your name/company), we 'cheat' by
00817            * impersonation _local_company as the server. Not the best solution;
00818            * but it works.
00819            * TODO: Perhaps this could be improved by when the client is ready
00820            * with joining to let it send itself the command, and not the server?
00821            * For example in network_client.c:534? */
00822           _local_company = ci->client_playas;
00823           NetworkSend_Command(0, 0, 0, CMD_RENAME_PRESIDENT, NULL, ci->client_name);
00824           _local_company = company_backup;
00825         }
00826 
00827         /* Announce new company on network, if the client was a SPECTATOR before */
00828         if (old_playas == COMPANY_SPECTATOR) {
00829           NetworkServerSendChat(NETWORK_ACTION_COMPANY_NEW, DESTTYPE_BROADCAST, 0, "", ci->client_id, ci->client_playas + 1);
00830         }
00831       }
00832 #endif /* ENABLE_NETWORK */
00833     } break;
00834 
00835     case 1: // Make a new AI company
00836       if (!(flags & DC_EXEC)) return CommandCost();
00837 
00838       if (p2 != INVALID_COMPANY && (p2 >= MAX_COMPANIES || IsValidCompanyID((CompanyID)p2))) return CMD_ERROR;
00839       DoStartupNewCompany(true, (CompanyID)p2);
00840       break;
00841 
00842     case 2: { // Delete a company
00843       Company *c;
00844 
00845       if (!IsValidCompanyID((CompanyID)p2)) return CMD_ERROR;
00846 
00847       if (!(flags & DC_EXEC)) return CommandCost();
00848 
00849       c = GetCompany((CompanyID)p2);
00850 
00851       /* Delete any open window of the company */
00852       DeleteCompanyWindows(c->index);
00853       CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
00854       cni->FillData(c);
00855 
00856       /* Show the bankrupt news */
00857       SetDParam(0, STR_705C_BANKRUPT);
00858       SetDParam(1, STR_705D_HAS_BEEN_CLOSED_DOWN_BY);
00859       SetDParamStr(2, cni->company_name);
00860       AddNewsItem(STR_02B6, NS_COMPANY_BANKRUPT, 0, 0, cni);
00861 
00862       /* Remove the company */
00863       ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER);
00864       if (!IsHumanCompany(c->index)) AI::Stop(c->index);
00865 
00866       CompanyID c_index = c->index;
00867       delete c;
00868       AI::BroadcastNewEvent(new AIEventCompanyBankrupt(c_index));
00869     } break;
00870 
00871     case 3: { // Merge a company (#1) into another company (#2), elimination company #1
00872       CompanyID cid_old = (CompanyID)GB(p2,  0, 16);
00873       CompanyID cid_new = (CompanyID)GB(p2, 16, 16);
00874 
00875       if (!IsValidCompanyID(cid_old) || !IsValidCompanyID(cid_new)) return CMD_ERROR;
00876 
00877       if (!(flags & DC_EXEC)) return CMD_ERROR;
00878 
00879       ChangeOwnershipOfCompanyItems(cid_old, cid_new);
00880       delete GetCompany(cid_old);
00881     } break;
00882 
00883     default: return CMD_ERROR;
00884   }
00885 
00886   return CommandCost();
00887 }

Generated on Mon Dec 14 20:59:58 2009 for OpenTTD by  doxygen 1.5.6