tron@2186: /* $Id$ */ tron@2186: rubidium@10207: /** @file company_cmd.cpp Handling of companies. */ rubidium@9111: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" rubidium@8786: #include "engine_func.h" peter1138@9070: #include "engine_base.h" rubidium@10208: #include "company_func.h" rubidium@10208: #include "company_gui.h" truelight@0: #include "town.h" rubidium@8763: #include "news_func.h" truelight@0: #include "saveload.h" rubidium@8116: #include "command_func.h" rubidium@5469: #include "network/network.h" rubidium@9428: #include "network/network_func.h" tron@2159: #include "variables.h" rubidium@8965: #include "cheat_func.h" truelight@2395: #include "ai/ai.h" rubidium@10208: #include "company_manager_face.h" rubidium@6643: #include "group.h" rubidium@8106: #include "window_func.h" rubidium@8108: #include "tile_map.h" rubidium@8114: #include "strings_func.h" rubidium@8123: #include "gfx_func.h" rubidium@8131: #include "functions.h" rubidium@8140: #include "date_func.h" rubidium@8144: #include "vehicle_func.h" rubidium@8157: #include "sound_func.h" peter1138@9070: #include "core/alloc_func.hpp" skidd13@9526: #include "core/sort_func.hpp" rubidium@8212: #include "autoreplace_func.h" rubidium@8212: #include "autoreplace_gui.h" rubidium@8214: #include "string_func.h" rubidium@8229: #include "ai/default/default.h" rubidium@8229: #include "ai/trolly/trolly.h" rubidium@8236: #include "road_func.h" rubidium@8236: #include "rail.h" rubidium@8785: #include "sprite.h" rubidium@9114: #include "debug.h" rubidium@9659: #include "oldpool_func.h" truelight@0: rubidium@8264: #include "table/strings.h" rubidium@8264: #include "table/sprites.h" rubidium@8264: rubidium@10207: CompanyByte _local_company; rubidium@10207: CompanyByte _current_company; rubidium@10207: /* NOSAVE: can be determined from company structs */ rubidium@10207: byte _company_colours[MAX_COMPANIES]; rubidium@10207: CompanyManagerFace _company_manager_face; ///< for company manager face storage in openttd.cfg rubidium@8254: HighScore _highscore_table[5][5]; // 4 difficulty-settings (+ network); top 5 rubidium@8254: rubidium@10207: DEFINE_OLD_POOL_GENERIC(Company, Company) rubidium@9659: rubidium@10207: Company::Company(uint16 name_1, bool is_ai) : name_1(name_1), is_ai(is_ai) rubidium@9659: { rubidium@10207: for (uint j = 0; j < 4; j++) this->share_owners[j] = COMPANY_SPECTATOR; rubidium@9659: } rubidium@9659: rubidium@10207: Company::~Company() rubidium@9659: { rubidium@9659: free(this->name); rubidium@9659: free(this->president_name); rubidium@9659: free(this->num_engines); rubidium@9659: rubidium@9659: if (CleaningPool()) return; rubidium@9659: rubidium@10207: DeleteCompanyWindows(this->index); rubidium@9659: this->name_1 = 0; rubidium@9659: } rubidium@9659: rubidium@5376: /** rubidium@10207: * Sets the local company and updates the patch settings that are set on a rubidium@10207: * per-company basis to reflect the core's state in the GUI. rubidium@10207: * @param new_company the new company rubidium@10207: * @pre IsValidCompanyID(new_company) || new_company == COMPANY_SPECTATOR || new_company == OWNER_NONE rubidium@5376: */ rubidium@10207: void SetLocalCompany(CompanyID new_company) rubidium@5376: { rubidium@10207: /* company could also be COMPANY_SPECTATOR or OWNER_NONE */ rubidium@10207: assert(IsValidCompanyID(new_company) || new_company == COMPANY_SPECTATOR || new_company == OWNER_NONE); rubidium@5376: rubidium@10207: _local_company = new_company; rubidium@5376: rubidium@5376: /* Do not update the patches if we are in the intro GUI */ rubidium@10207: if (IsValidCompanyID(new_company) && _game_mode != GM_MENU) { smatz@10225: const Company *c = GetCompany(new_company); smatz@10225: _settings_client.gui.autorenew = c->engine_renew; smatz@10225: _settings_client.gui.autorenew_months = c->engine_renew_months; smatz@10225: _settings_client.gui.autorenew_money = c->engine_renew_money; rubidium@5376: InvalidateWindow(WC_GAME_OPTIONS, 0); rubidium@5376: } rubidium@5376: } rubidium@5376: rubidium@10207: bool IsHumanCompany(CompanyID company) rubidium@8254: { rubidium@10207: return !GetCompany(company)->is_ai; rubidium@8254: } rubidium@8254: tron@4428: rubidium@10207: uint16 GetDrawStringCompanyColor(CompanyID company) tron@4428: { Darkvater@4944: /* Get the color for DrawString-subroutines which matches the color rubidium@10207: * of the company */ rubidium@10207: if (!IsValidCompanyID(company)) return _colour_gradient[COLOUR_WHITE][4] | IS_PALETTE_COLOR; rubidium@10207: return (_colour_gradient[_company_colours[company]][4]) | IS_PALETTE_COLOR; tron@4428: } tron@4428: smatz@10225: void DrawCompanyIcon(CompanyID c, int x, int y) maedhros@5750: { smatz@10225: DrawSprite(SPR_PLAYER_ICON, COMPANY_SPRITE_COLOR(c), x, y); maedhros@5750: } maedhros@5750: rubidium@6137: /** rubidium@10207: * Converts an old company manager's face format to the new company manager's face format rubidium@6137: * rubidium@6190: * Meaning of the bits in the old face (some bits are used in several times): rubidium@6137: * - 4 and 5: chin rubidium@6149: * - 6 to 9: eyebrows rubidium@6137: * - 10 to 13: nose rubidium@6137: * - 13 to 15: lips (also moustache for males) rubidium@6137: * - 16 to 19: hair rubidium@6149: * - 20 to 22: eye color rubidium@6137: * - 20 to 27: tie, ear rings etc. rubidium@6137: * - 28 to 30: glasses rubidium@6137: * - 19, 26 and 27: race (bit 27 set and bit 19 equal to bit 26 = black, otherwise white) rubidium@6137: * - 31: gender (0 = male, 1 = female) rubidium@6137: * rubidium@6190: * @param face the face in the old format rubidium@6190: * @return the face in the new format rubidium@6137: */ rubidium@10207: CompanyManagerFace ConvertFromOldCompanyManagerFace(uint32 face) truelight@0: { rubidium@10207: CompanyManagerFace cmf = 0; rubidium@6190: GenderEthnicity ge = GE_WM; truelight@0: skidd13@7931: if (HasBit(face, 31)) SetBit(ge, GENDER_FEMALE); skidd13@7931: if (HasBit(face, 27) && (HasBit(face, 26) == HasBit(face, 19))) SetBit(ge, ETHNICITY_BLACK); truelight@201: rubidium@10207: SetCompanyManagerFaceBits(cmf, CMFV_GEN_ETHN, ge, ge); rubidium@10207: SetCompanyManagerFaceBits(cmf, CMFV_HAS_GLASSES, ge, GB(face, 28, 3) <= 1); rubidium@10207: SetCompanyManagerFaceBits(cmf, CMFV_EYE_COLOUR, ge, HasBit(ge, ETHNICITY_BLACK) ? 0 : ClampU(GB(face, 20, 3), 5, 7) - 5); rubidium@10207: SetCompanyManagerFaceBits(cmf, CMFV_CHIN, ge, ScaleCompanyManagerFaceValue(CMFV_CHIN, ge, GB(face, 4, 2))); rubidium@10207: SetCompanyManagerFaceBits(cmf, CMFV_EYEBROWS, ge, ScaleCompanyManagerFaceValue(CMFV_EYEBROWS, ge, GB(face, 6, 4))); rubidium@10207: SetCompanyManagerFaceBits(cmf, CMFV_HAIR, ge, ScaleCompanyManagerFaceValue(CMFV_HAIR, ge, GB(face, 16, 4))); rubidium@10207: SetCompanyManagerFaceBits(cmf, CMFV_JACKET, ge, ScaleCompanyManagerFaceValue(CMFV_JACKET, ge, GB(face, 20, 2))); rubidium@10207: SetCompanyManagerFaceBits(cmf, CMFV_COLLAR, ge, ScaleCompanyManagerFaceValue(CMFV_COLLAR, ge, GB(face, 22, 2))); rubidium@10207: SetCompanyManagerFaceBits(cmf, CMFV_GLASSES, ge, GB(face, 28, 1)); rubidium@6190: rubidium@6190: uint lips = GB(face, 10, 4); skidd13@7928: if (!HasBit(ge, GENDER_FEMALE) && lips < 4) { rubidium@10207: SetCompanyManagerFaceBits(cmf, CMFV_HAS_MOUSTACHE, ge, true); rubidium@10207: SetCompanyManagerFaceBits(cmf, CMFV_MOUSTACHE, ge, max(lips, 1U) - 1); rubidium@6137: } else { skidd13@7928: if (!HasBit(ge, GENDER_FEMALE)) { rubidium@6346: lips = lips * 15 / 16; rubidium@6190: lips -= 3; skidd13@7928: if (HasBit(ge, ETHNICITY_BLACK) && lips > 8) lips = 0; rubidium@6190: } else { rubidium@10207: lips = ScaleCompanyManagerFaceValue(CMFV_LIPS, ge, lips); rubidium@6190: } rubidium@10207: SetCompanyManagerFaceBits(cmf, CMFV_LIPS, ge, lips); truelight@0: rubidium@6190: uint nose = GB(face, 13, 3); rubidium@6190: if (ge == GE_WF) { rubidium@6190: nose = (nose * 3 >> 3) * 3 >> 2; // There is 'hole' in the nose sprites for females rubidium@6190: } else { rubidium@10207: nose = ScaleCompanyManagerFaceValue(CMFV_NOSE, ge, nose); rubidium@6190: } rubidium@10207: SetCompanyManagerFaceBits(cmf, CMFV_NOSE, ge, nose); rubidium@6137: } rubidium@6137: rubidium@6190: uint tie_earring = GB(face, 24, 4); skidd13@7928: if (!HasBit(ge, GENDER_FEMALE) || tie_earring < 3) { // Not all females have an earring rubidium@10207: if (HasBit(ge, GENDER_FEMALE)) SetCompanyManagerFaceBits(cmf, CMFV_HAS_TIE_EARRING, ge, true); rubidium@10207: SetCompanyManagerFaceBits(cmf, CMFV_TIE_EARRING, ge, HasBit(ge, GENDER_FEMALE) ? tie_earring : ScaleCompanyManagerFaceValue(CMFV_TIE_EARRING, ge, tie_earring / 2)); truelight@0: } truelight@0: rubidium@10207: return cmf; rubidium@6190: } rubidium@6190: rubidium@6190: /** rubidium@10207: * Checks whether a company manager's face is a valid encoding. rubidium@6190: * Unused bits are not enforced to be 0. rubidium@10207: * @param cmf the fact to check rubidium@6190: * @return true if and only if the face is valid rubidium@6190: */ rubidium@10207: bool IsValidCompanyManagerFace(CompanyManagerFace cmf) rubidium@6190: { rubidium@10207: if (!AreCompanyManagerFaceBitsValid(cmf, CMFV_GEN_ETHN, GE_WM)) return false; rubidium@6190: rubidium@10207: GenderEthnicity ge = (GenderEthnicity)GetCompanyManagerFaceBits(cmf, CMFV_GEN_ETHN, GE_WM); rubidium@10207: bool has_moustache = !HasBit(ge, GENDER_FEMALE) && GetCompanyManagerFaceBits(cmf, CMFV_HAS_MOUSTACHE, ge) != 0; rubidium@10207: bool has_tie_earring = !HasBit(ge, GENDER_FEMALE) || GetCompanyManagerFaceBits(cmf, CMFV_HAS_TIE_EARRING, ge) != 0; rubidium@10207: bool has_glasses = GetCompanyManagerFaceBits(cmf, CMFV_HAS_GLASSES, ge) != 0; rubidium@6190: rubidium@10207: if (!AreCompanyManagerFaceBitsValid(cmf, CMFV_EYE_COLOUR, ge)) return false; rubidium@10207: for (CompanyManagerFaceVariable cmfv = CMFV_CHEEKS; cmfv < CMFV_END; cmfv++) { rubidium@10207: switch (cmfv) { rubidium@10207: case CMFV_MOUSTACHE: if (!has_moustache) continue; break; rubidium@10207: case CMFV_LIPS: /* FALL THROUGH */ rubidium@10207: case CMFV_NOSE: if (has_moustache) continue; break; rubidium@10207: case CMFV_TIE_EARRING: if (!has_tie_earring) continue; break; rubidium@10207: case CMFV_GLASSES: if (!has_glasses) continue; break; rubidium@6190: default: break; KUDr@6144: } rubidium@10207: if (!AreCompanyManagerFaceBitsValid(cmf, cmfv, ge)) return false; truelight@0: } truelight@0: rubidium@6190: return true; truelight@0: } truelight@0: rubidium@10207: void InvalidateCompanyWindows(const Company *company) truelight@0: { rubidium@10207: CompanyID cid = company->index; truelight@0: rubidium@10207: if (cid == _local_company) InvalidateWindow(WC_STATUS_BAR, 0); rubidium@10207: InvalidateWindow(WC_FINANCES, cid); truelight@0: } truelight@0: rubidium@10207: bool CheckCompanyHasMoney(CommandCost cost) truelight@0: { rubidium@6950: if (cost.GetCost() > 0) { rubidium@10207: CompanyID company = _current_company; rubidium@10207: if (IsValidCompanyID(company) && cost.GetCost() > GetCompany(company)->money) { rubidium@7002: SetDParam(0, cost.GetCost()); truelight@0: _error_message = STR_0003_NOT_ENOUGH_CASH_REQUIRES; truelight@0: return false; truelight@0: } truelight@0: } truelight@0: return true; truelight@0: } truelight@0: rubidium@10207: static void SubtractMoneyFromAnyCompany(Company *c, CommandCost cost) truelight@0: { rubidium@8230: if (cost.GetCost() == 0) return; rubidium@8230: assert(cost.GetExpensesType() != INVALID_EXPENSES); truelight@201: rubidium@10207: c->money -= cost.GetCost(); rubidium@10207: c->yearly_expenses[0][cost.GetExpensesType()] += cost.GetCost(); truelight@0: skidd13@7928: if (HasBit(1 << EXPENSES_TRAIN_INC | bjarni@6841: 1 << EXPENSES_ROADVEH_INC | bjarni@6841: 1 << EXPENSES_AIRCRAFT_INC | rubidium@8230: 1 << EXPENSES_SHIP_INC, cost.GetExpensesType())) { rubidium@10207: c->cur_economy.income -= cost.GetCost(); skidd13@7928: } else if (HasBit(1 << EXPENSES_TRAIN_RUN | bjarni@6841: 1 << EXPENSES_ROADVEH_RUN | bjarni@6841: 1 << EXPENSES_AIRCRAFT_RUN | bjarni@6841: 1 << EXPENSES_SHIP_RUN | bjarni@6841: 1 << EXPENSES_PROPERTY | rubidium@8230: 1 << EXPENSES_LOAN_INT, cost.GetExpensesType())) { rubidium@10207: c->cur_economy.expenses -= cost.GetCost(); tron@4077: } truelight@201: rubidium@10207: InvalidateCompanyWindows(c); truelight@0: } truelight@0: rubidium@10207: void SubtractMoneyFromCompany(CommandCost cost) truelight@0: { rubidium@10207: CompanyID cid = _current_company; tron@4077: rubidium@10207: if (IsValidCompanyID(cid)) SubtractMoneyFromAnyCompany(GetCompany(cid), cost); truelight@0: } truelight@0: rubidium@10207: void SubtractMoneyFromCompanyFract(CompanyID company, CommandCost cst) truelight@0: { rubidium@10207: Company *c = GetCompany(company); rubidium@10207: byte m = c->money_fraction; rubidium@6953: Money cost = cst.GetCost(); tron@4077: rubidium@10207: c->money_fraction = m - (byte)cost; truelight@0: cost >>= 8; rubidium@10207: if (c->money_fraction > m) cost++; rubidium@10207: if (cost != 0) SubtractMoneyFromAnyCompany(c, CommandCost(cst.GetExpensesType(), cost)); truelight@0: } truelight@0: Darkvater@4849: void GetNameOfOwner(Owner owner, TileIndex tile) truelight@0: { tron@534: SetDParam(2, owner); truelight@201: truelight@0: if (owner != OWNER_TOWN) { rubidium@10207: if (!IsValidCompanyID(owner)) { tron@534: SetDParam(0, STR_0150_SOMEONE); tron@4077: } else { peter1138@7058: SetDParam(0, STR_COMPANY_NAME); rubidium@10207: SetDParam(1, owner); truelight@0: } truelight@0: } else { rubidium@10236: const Town *t = ClosestTownFromTile(tile, UINT_MAX); tron@4077: ludde@2070: SetDParam(0, STR_TOWN); ludde@2070: SetDParam(1, t->index); truelight@0: } truelight@0: } truelight@0: truelight@0: rubidium@10207: bool CheckOwnership(Owner owner) truelight@0: { Darkvater@4849: assert(owner < OWNER_END); truelight@0: rubidium@10207: if (owner == _current_company) return true; truelight@0: _error_message = STR_013B_OWNED_BY; truelight@0: GetNameOfOwner(owner, 0); truelight@0: return false; truelight@0: } truelight@0: tron@1977: bool CheckTileOwnership(TileIndex tile) truelight@0: { Darkvater@4849: Owner owner = GetTileOwner(tile); tron@1901: Darkvater@4849: assert(owner < OWNER_END); tron@1901: rubidium@10207: if (owner == _current_company) return true; truelight@0: _error_message = STR_013B_OWNED_BY; truelight@201: rubidium@10207: /* no need to get the name of the owner unless we're the local company (saves some time) */ rubidium@10207: if (IsLocalCompany()) GetNameOfOwner(owner, tile); truelight@0: return false; truelight@0: } truelight@0: rubidium@10207: static void GenerateCompanyName(Company *c) truelight@0: { tron@1977: TileIndex tile; truelight@0: Town *t; truelight@0: StringID str; rubidium@10207: Company *cc; truelight@0: uint32 strp; truelight@0: char buffer[100]; truelight@0: rubidium@10207: if (c->name_1 != STR_SV_UNNAMED) return; truelight@0: rubidium@10207: tile = c->last_build_coordinate; tron@4077: if (tile == 0) return; truelight@0: rubidium@10236: t = ClosestTownFromTile(tile, UINT_MAX); truelight@0: rubidium@10141: if (t->name == NULL && IsInsideMM(t->townnametype, SPECSTR_TOWNNAME_START, SPECSTR_TOWNNAME_LAST + 1)) { truelight@0: str = t->townnametype - SPECSTR_TOWNNAME_START + SPECSTR_PLAYERNAME_START; truelight@0: strp = t->townnameparts; truelight@0: truelight@0: verify_name:; rubidium@10207: /* No companies must have this name already */ rubidium@10207: FOR_ALL_COMPANIES(cc) { rubidium@10207: if (cc->name_1 == str && cc->name_2 == strp) goto bad_town_name; truelight@0: } truelight@0: Darkvater@4912: GetString(buffer, str, lastof(buffer)); rubidium@9914: if (strlen(buffer) >= MAX_LENGTH_COMPANY_NAME_BYTES) goto bad_town_name; truelight@0: truelight@0: set_name:; rubidium@10207: c->name_1 = str; rubidium@10207: c->name_2 = strp; truelight@0: truelight@0: MarkWholeScreenDirty(); truelight@0: rubidium@10207: if (!IsHumanCompany(c->index)) { rubidium@9658: CompanyNewsInformation *cni = MallocT(1); rubidium@10207: cni->FillData(c); rubidium@9199: SetDParam(0, STR_705E_NEW_TRANSPORT_COMPANY_LAUNCHED); rubidium@9199: SetDParam(1, STR_705F_STARTS_CONSTRUCTION_NEAR); rubidium@9658: SetDParamStr(2, cni->company_name); rubidium@9199: SetDParam(3, t->index); rubidium@10207: AddNewsItem(STR_02B6, NS_COMPANY_NEW, c->last_build_coordinate, 0, cni); truelight@0: } truelight@0: return; truelight@0: } truelight@0: bad_town_name:; truelight@201: rubidium@10207: if (c->president_name_1 == SPECSTR_PRESIDENT_NAME) { truelight@0: str = SPECSTR_ANDCO_NAME; rubidium@10207: strp = c->president_name_2; truelight@201: goto set_name; truelight@0: } else { truelight@0: str = SPECSTR_ANDCO_NAME; truelight@0: strp = Random(); truelight@0: goto verify_name; truelight@0: } truelight@0: } truelight@0: rubidium@9044: static const byte _colour_sort[COLOUR_END] = {2, 2, 3, 2, 3, 2, 3, 2, 3, 2, 2, 2, 3, 1, 1, 1}; rubidium@9044: static const Colours _similar_colour[COLOUR_END][2] = { rubidium@9044: { COLOUR_BLUE, COLOUR_LIGHT_BLUE }, // COLOUR_DARK_BLUE rubidium@9044: { COLOUR_GREEN, COLOUR_DARK_GREEN }, // COLOUR_PALE_GREEN rubidium@9044: { INVALID_COLOUR, INVALID_COLOUR }, // COLOUR_PINK rubidium@9044: { COLOUR_ORANGE, INVALID_COLOUR }, // COLOUR_YELLOW rubidium@9044: { INVALID_COLOUR, INVALID_COLOUR }, // COLOUR_RED rubidium@9044: { COLOUR_DARK_BLUE, COLOUR_BLUE }, // COLOUR_LIGHT_BLUE rubidium@9044: { COLOUR_PALE_GREEN, COLOUR_DARK_GREEN }, // COLOUR_GREEN rubidium@9044: { COLOUR_PALE_GREEN, COLOUR_GREEN }, // COLOUR_DARK_GREEN smatz@9981: { COLOUR_DARK_BLUE, COLOUR_LIGHT_BLUE }, // COLOUR_BLUE rubidium@9044: { COLOUR_BROWN, COLOUR_ORANGE }, // COLOUR_CREAM rubidium@9044: { COLOUR_PURPLE, INVALID_COLOUR }, // COLOUR_MAUVE rubidium@9044: { COLOUR_MAUVE, INVALID_COLOUR }, // COLOUR_PURPLE rubidium@9044: { COLOUR_YELLOW, COLOUR_CREAM }, // COLOUR_ORANGE rubidium@9044: { COLOUR_CREAM, INVALID_COLOUR }, // COLOUR_BROWN rubidium@9044: { COLOUR_WHITE, INVALID_COLOUR }, // COLOUR_GREY rubidium@9044: { COLOUR_GREY, INVALID_COLOUR }, // COLOUR_WHITE rubidium@9044: }; truelight@0: rubidium@10207: static byte GenerateCompanyColour() truelight@0: { rubidium@9044: Colours colours[COLOUR_END]; truelight@0: belugas@6352: /* Initialize array */ rubidium@9044: for (uint i = 0; i < COLOUR_END; i++) colours[i] = (Colours)i; truelight@0: belugas@6352: /* And randomize it */ rubidium@9044: for (uint i = 0; i < 100; i++) { rubidium@9044: uint r = Random(); rubidium@9044: Swap(colours[GB(r, 0, 4)], colours[GB(r, 4, 4)]); rubidium@9044: } truelight@0: belugas@6352: /* Bubble sort it according to the values in table 1 */ rubidium@9044: for (uint i = 0; i < COLOUR_END; i++) { rubidium@9044: for (uint j = 1; j < COLOUR_END; j++) { rubidium@9044: if (_colour_sort[colours[j - 1]] < _colour_sort[colours[j]]) { rubidium@9044: Swap(colours[j - 1], colours[j]); truelight@0: } truelight@0: } rubidium@9044: }; truelight@0: rubidium@10207: /* Move the colors that look similar to each company's color to the side */ rubidium@10207: Company *c; rubidium@10207: FOR_ALL_COMPANIES(c) { rubidium@10207: Colours pcolour = (Colours)c->colour; truelight@0: rubidium@9044: for (uint i = 0; i < COLOUR_END; i++) { rubidium@9044: if (colours[i] == pcolour) { rubidium@9044: colours[i] = INVALID_COLOUR; rubidium@9044: break; truelight@0: } rubidium@9044: } rubidium@9044: rubidium@9044: for (uint j = 0; j < 2; j++) { rubidium@9044: Colours similar = _similar_colour[pcolour][j]; rubidium@9044: if (similar == INVALID_COLOUR) break; rubidium@9044: rubidium@9044: for (uint i = 1; i < COLOUR_END; i++) { rubidium@9044: if (colours[i - 1] == similar) Swap(colours[i - 1], colours[i]); rubidium@9044: } truelight@0: } truelight@0: } truelight@0: belugas@6352: /* Return the first available color */ rubidium@9044: for (uint i = 0; i < COLOUR_END; i++) { rubidium@9044: if (colours[i] != INVALID_COLOUR) return colours[i]; truelight@0: } rubidium@9044: rubidium@9044: NOT_REACHED(); truelight@0: } truelight@0: rubidium@10207: static void GeneratePresidentName(Company *c) truelight@0: { rubidium@10207: Company *cc; truelight@0: char buffer[100], buffer2[40]; truelight@0: tron@2952: for (;;) { truelight@0: restart:; truelight@0: rubidium@10207: c->president_name_2 = Random(); rubidium@10207: c->president_name_1 = SPECSTR_PRESIDENT_NAME; truelight@201: rubidium@10207: SetDParam(0, c->index); rubidium@10209: GetString(buffer, STR_PRESIDENT_NAME, lastof(buffer)); Darkvater@4609: if (strlen(buffer) >= 32 || GetStringBoundingBox(buffer).width >= 94) truelight@0: continue; truelight@0: rubidium@10207: FOR_ALL_COMPANIES(cc) { rubidium@10207: if (c != cc) { rubidium@10207: SetDParam(0, cc->index); rubidium@10209: GetString(buffer2, STR_PRESIDENT_NAME, lastof(buffer2)); tron@1304: if (strcmp(buffer2, buffer) == 0) truelight@0: goto restart; truelight@0: } truelight@0: } truelight@0: return; truelight@0: } truelight@0: } truelight@0: rubidium@10207: void ResetCompanyLivery(Company *c) peter1138@4603: { rubidium@5587: for (LiveryScheme scheme = LS_BEGIN; scheme < LS_END; scheme++) { rubidium@10207: c->livery[scheme].in_use = false; rubidium@10207: c->livery[scheme].colour1 = c->colour; rubidium@10207: c->livery[scheme].colour2 = c->colour; peter1138@4603: } peter1138@4603: } peter1138@4603: rubidium@7734: /** rubidium@10207: * Create a new company and sets all company variables default values rubidium@7734: * rubidium@10207: * @param is_ai is a ai company? rubidium@10207: * @return the company struct rubidium@7734: */ rubidium@10207: Company *DoStartupNewCompany(bool is_ai) truelight@0: { rubidium@10207: if (!Company::CanAllocateItem()) return NULL; rubidium@9890: rubidium@10207: Company *c = new Company(STR_SV_UNNAMED, is_ai); truelight@0: rubidium@10207: memset(&_companies_ai[c->index], 0, sizeof(CompanyAI)); rubidium@10207: memset(&_companies_ainew[c->index], 0, sizeof(CompanyAiNew)); rubidium@9659: belugas@6352: /* Make a color */ rubidium@10207: c->colour = GenerateCompanyColour(); rubidium@10207: ResetCompanyLivery(c); rubidium@10207: _company_colours[c->index] = c->colour; truelight@0: rubidium@10207: c->money = c->current_loan = 100000; truelight@0: rubidium@10207: _companies_ai[c->index].state = 5; // AIS_WANT_NEW_ROUTE rubidium@10207: c->share_owners[0] = c->share_owners[1] = c->share_owners[2] = c->share_owners[3] = INVALID_OWNER; rubidium@10207: rubidium@10207: c->avail_railtypes = GetCompanyRailtypes(c->index); rubidium@10207: c->avail_roadtypes = GetCompanyRoadtypes(c->index); rubidium@10207: c->inaugurated_year = _cur_year; rubidium@10207: RandomCompanyManagerFaceBits(c->face, (GenderEthnicity)Random(), false); // create a random company manager face truelight@0: bjarni@2293: /* Engine renewal settings */ rubidium@10207: c->engine_renew_list = NULL; rubidium@10207: c->renew_keep_length = false; rubidium@10207: c->engine_renew = _settings_client.gui.autorenew; rubidium@10207: c->engine_renew_months = _settings_client.gui.autorenew_months; rubidium@10207: c->engine_renew_money = _settings_client.gui.autorenew_money; bjarni@2293: rubidium@10207: GeneratePresidentName(c); truelight@0: truelight@0: InvalidateWindow(WC_GRAPH_LEGEND, 0); truelight@0: InvalidateWindow(WC_TOOLBAR_MENU, 0); truelight@543: InvalidateWindow(WC_CLIENT_LIST, 0); truelight@0: truelight@2395: if (is_ai && (!_networking || _network_server) && _ai.enabled) rubidium@10207: AI_StartNewAI(c->index); truelight@2395: rubidium@10207: c->num_engines = CallocT(GetEnginePoolSize()); bjarni@4574: rubidium@10207: return c; truelight@0: } truelight@0: rubidium@10207: void StartupCompanies() truelight@0: { belugas@6352: /* The AI starts like in the setting with +2 month max */ rubidium@9413: _next_competitor_start = _settings_game.difficulty.competitor_start_time * 90 * DAY_TICKS + RandomRange(60 * DAY_TICKS) + 1; truelight@0: } truelight@0: rubidium@10207: static void MaybeStartNewCompany() truelight@0: { truelight@0: uint n; rubidium@10207: Company *c; truelight@201: belugas@6352: /* count number of competitors */ truelight@0: n = 0; rubidium@10207: FOR_ALL_COMPANIES(c) { rubidium@10207: if (c->is_ai) n++; truelight@2395: } truelight@0: belugas@6352: /* when there's a lot of computers in game, the probability that a new one starts is lower */ rubidium@9413: if (n < (uint)_settings_game.difficulty.max_no_competitors && tron@4000: n < (_network_server ? rubidium@9413: InteractiveRandomRange(_settings_game.difficulty.max_no_competitors + 2) : rubidium@9413: RandomRange(_settings_game.difficulty.max_no_competitors + 2) tron@4000: )) { tron@4000: /* Send a command to all clients to start up a new AI. tron@4000: * Works fine for Multiplayer and Singleplayer */ rubidium@10207: DoCommandP(0, 1, 0, NULL, CMD_COMPANY_CTRL); tron@4000: } truelight@201: belugas@6352: /* The next AI starts like the difficulty setting said, with +2 month max */ rubidium@9413: _next_competitor_start = _settings_game.difficulty.competitor_start_time * 90 * DAY_TICKS + 1; truelight@2682: _next_competitor_start += _network_server ? InteractiveRandomRange(60 * DAY_TICKS) : RandomRange(60 * DAY_TICKS); truelight@0: } truelight@0: rubidium@10207: void InitializeCompanies() truelight@0: { rubidium@10207: _Company_pool.CleanPool(); rubidium@10207: _Company_pool.AddBlockToPool(); rubidium@10207: _cur_company_tick_index = 0; truelight@0: } truelight@0: rubidium@10207: void OnTick_Companies() truelight@0: { tron@4000: if (_game_mode == GM_EDITOR) return; truelight@201: rubidium@10207: if (IsValidCompanyID((CompanyID)_cur_company_tick_index)) { rubidium@10207: Company *c = GetCompany((CompanyID)_cur_company_tick_index); rubidium@10207: if (c->name_1 != 0) GenerateCompanyName(c); rubidium@9659: rubidium@9659: if (AI_AllowNewAI() && _game_mode != GM_MENU && !--_next_competitor_start) { rubidium@10207: MaybeStartNewCompany(); rubidium@9659: } rubidium@9659: } rubidium@9659: rubidium@10207: _cur_company_tick_index = (_cur_company_tick_index + 1) % MAX_COMPANIES; truelight@0: } truelight@0: rubidium@10207: void CompaniesYearlyLoop() truelight@0: { rubidium@10207: Company *c; truelight@0: belugas@6352: /* Copy statistics */ rubidium@10207: FOR_ALL_COMPANIES(c) { rubidium@10207: memmove(&c->yearly_expenses[1], &c->yearly_expenses[0], sizeof(c->yearly_expenses) - sizeof(c->yearly_expenses[0])); rubidium@10207: memset(&c->yearly_expenses[0], 0, sizeof(c->yearly_expenses[0])); rubidium@10207: InvalidateWindow(WC_FINANCES, c->index); truelight@0: } truelight@0: rubidium@10207: if (_settings_client.gui.show_finances && _local_company != COMPANY_SPECTATOR) { rubidium@10207: ShowCompanyFinances(_local_company); rubidium@10207: c = GetCompany(_local_company); rubidium@10207: if (c->num_valid_stat_ent > 5 && c->old_economy[0].performance_history < c->old_economy[4].performance_history) { tron@541: SndPlayFx(SND_01_BAD_YEAR); truelight@0: } else { tron@541: SndPlayFx(SND_00_GOOD_YEAR); truelight@0: } truelight@0: } truelight@0: } truelight@0: bjarni@2293: /** Change engine renewal parameters tron@3491: * @param tile unused belugas@6483: * @param flags operation to perform bjarni@2293: * @param p1 bits 0-3 command bjarni@2293: * - p1 = 0 - change auto renew bool bjarni@2293: * - p1 = 1 - change auto renew months bjarni@2293: * - p1 = 2 - change auto renew money bjarni@2293: * - p1 = 3 - change auto renew array bjarni@2293: * - p1 = 4 - change bool, months & money all together bjarni@2617: * - p1 = 5 - change renew_keep_length bjarni@2293: * @param p2 value to set bjarni@2293: * if p1 = 0, then: bjarni@2293: * - p2 = enable engine renewal bjarni@2293: * if p1 = 1, then: bjarni@2293: * - p2 = months left before engine expires to replace it bjarni@2293: * if p1 = 2, then bjarni@2293: * - p2 = minimum amount of money available bjarni@2293: * if p1 = 3, then: rubidium@9579: * - p1 bits 16-31 = engine group bjarni@2293: * - p2 bits 0-15 = old engine type bjarni@2293: * - p2 bits 16-31 = new engine type bjarni@2293: * if p1 = 4, then: bjarni@2293: * - p1 bit 15 = enable engine renewal bjarni@2293: * - p1 bits 16-31 = months left before engine expires to replace it bjarni@2293: * - p2 bits 0-31 = minimum amount of money available bjarni@2617: * if p1 = 5, then bjarni@2617: * - p2 = enable renew_keep_length bjarni@2293: */ rubidium@6943: CommandCost CmdSetAutoReplace(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) bjarni@2293: { rubidium@10207: if (!IsValidCompanyID(_current_company)) return CMD_ERROR; bjarni@2293: rubidium@10207: Company *c = GetCompany(_current_company); bjarni@2293: switch (GB(p1, 0, 3)) { bjarni@2293: case 0: rubidium@10207: if (c->engine_renew == HasBit(p2, 0)) return CMD_ERROR; bjarni@2293: bjarni@2293: if (flags & DC_EXEC) { rubidium@10207: c->engine_renew = HasBit(p2, 0); rubidium@10207: if (IsLocalCompany()) { rubidium@10207: _settings_client.gui.autorenew = c->engine_renew; bjarni@2293: InvalidateWindow(WC_GAME_OPTIONS, 0); bjarni@2293: } bjarni@2293: } bjarni@2293: break; glx@9013: bjarni@2293: case 1: glx@9011: if (Clamp((int16)p2, -12, 12) != (int16)p2) return CMD_ERROR; rubidium@10207: if (c->engine_renew_months == (int16)p2) return CMD_ERROR; bjarni@2293: bjarni@2293: if (flags & DC_EXEC) { rubidium@10207: c->engine_renew_months = (int16)p2; rubidium@10207: if (IsLocalCompany()) { rubidium@10207: _settings_client.gui.autorenew_months = c->engine_renew_months; bjarni@2293: InvalidateWindow(WC_GAME_OPTIONS, 0); bjarni@2293: } bjarni@2293: } bjarni@2293: break; glx@9013: bjarni@2293: case 2: glx@9011: if (ClampU(p2, 0, 2000000) != p2) return CMD_ERROR; rubidium@10207: if (c->engine_renew_money == p2) return CMD_ERROR; bjarni@2293: bjarni@2293: if (flags & DC_EXEC) { rubidium@10207: c->engine_renew_money = p2; rubidium@10207: if (IsLocalCompany()) { rubidium@10207: _settings_client.gui.autorenew_money = c->engine_renew_money; bjarni@2293: InvalidateWindow(WC_GAME_OPTIONS, 0); bjarni@2293: } bjarni@2293: } bjarni@2293: break; glx@9013: bjarni@2293: case 3: { bjarni@2293: EngineID old_engine_type = GB(p2, 0, 16); bjarni@2293: EngineID new_engine_type = GB(p2, 16, 16); rubidium@6648: GroupID id_g = GB(p1, 16, 16); rubidium@6943: CommandCost cost; bjarni@2293: rubidium@7258: if (!IsValidGroupID(id_g) && !IsAllGroupID(id_g) && !IsDefaultGroupID(id_g)) return CMD_ERROR; bjarni@2293: if (new_engine_type != INVALID_ENGINE) { rubidium@10207: if (!CheckAutoreplaceValidity(old_engine_type, new_engine_type, _current_company)) return CMD_ERROR; bjarni@2293: rubidium@10207: cost = AddEngineReplacementForCompany(c, old_engine_type, new_engine_type, id_g, flags); peter1138@2697: } else { rubidium@10207: cost = RemoveEngineReplacementForCompany(c, old_engine_type, id_g, flags); bjarni@2293: } peter1138@2811: rubidium@10207: if (IsLocalCompany()) InvalidateAutoreplaceWindow(old_engine_type, id_g); peter1138@2811: peter1138@2811: return cost; tron@2755: } tron@2755: bjarni@2293: case 4: glx@9011: if (Clamp((int16)GB(p1, 16, 16), -12, 12) != (int16)GB(p1, 16, 16)) return CMD_ERROR; glx@9011: if (ClampU(p2, 0, 2000000) != p2) return CMD_ERROR; glx@9013: bjarni@2293: if (flags & DC_EXEC) { rubidium@10207: c->engine_renew = HasBit(p1, 15); rubidium@10207: c->engine_renew_months = (int16)GB(p1, 16, 16); rubidium@10207: c->engine_renew_money = p2; bjarni@2293: rubidium@10207: if (IsLocalCompany()) { rubidium@10207: _settings_client.gui.autorenew = c->engine_renew; rubidium@10207: _settings_client.gui.autorenew_months = c->engine_renew_months; rubidium@10207: _settings_client.gui.autorenew_money = c->engine_renew_money; bjarni@2293: InvalidateWindow(WC_GAME_OPTIONS, 0); bjarni@2293: } bjarni@2293: } bjarni@2293: break; glx@9013: bjarni@2617: case 5: rubidium@10207: if (c->renew_keep_length == HasBit(p2, 0)) return CMD_ERROR; bjarni@2617: bjarni@2617: if (flags & DC_EXEC) { rubidium@10207: c->renew_keep_length = HasBit(p2, 0); rubidium@10207: if (IsLocalCompany()) { rubidium@6259: InvalidateWindow(WC_REPLACE_VEHICLE, VEH_TRAIN); bjarni@2617: } bjarni@2617: } bjarni@2617: break; glx@9013: } bjarni@2617: rubidium@6950: return CommandCost(); bjarni@2293: } bjarni@2293: rubidium@9658: /** rubidium@9658: * Fill the CompanyNewsInformation struct with the required data. rubidium@9658: * @param p the current company. rubidium@9658: * @param other the other company. rubidium@9658: */ rubidium@10207: void CompanyNewsInformation::FillData(const Company *c, const Company *other) rubidium@9658: { rubidium@10207: SetDParam(0, c->index); rubidium@9658: GetString(this->company_name, STR_COMPANY_NAME, lastof(this->company_name)); rubidium@9658: rubidium@9658: if (other == NULL) { rubidium@9658: *this->other_company_name = '\0'; rubidium@9658: } else { rubidium@9658: SetDParam(0, other->index); rubidium@9658: GetString(this->other_company_name, STR_COMPANY_NAME, lastof(this->other_company_name)); rubidium@10207: c = other; rubidium@9658: } rubidium@9658: rubidium@10207: SetDParam(0, c->index); rubidium@9658: GetString(this->president_name, STR_7058_PRESIDENT, lastof(this->president_name)); rubidium@9658: rubidium@10207: this->colour = c->colour; rubidium@10207: this->face = c->face; rubidium@9658: rubidium@9658: } rubidium@9658: rubidium@10207: /** Control the companies: add, delete, etc. tron@3491: * @param tile unused belugas@6483: * @param flags operation to perform Darkvater@1796: * @param p1 various functionality rubidium@10207: * - p1 = 0 - create a new company, Which company (network) it will be is in p2 rubidium@10207: * - p1 = 1 - create a new AI company rubidium@10207: * - p1 = 2 - delete a company. Company is identified by p2 rubidium@10207: * - p1 = 3 - merge two companies together. merge #1 with #2. Identified by p2 Darkvater@1796: * @param p2 various functionality, dictated by p1 rubidium@10207: * - p1 = 0 - ClientID of the newly created client rubidium@10207: * - p1 = 2 - CompanyID of the that is getting deleted rubidium@10207: * - p1 = 3 - #1 p2 = (bit 0-15) - company to merge (p2 & 0xFFFF) rubidium@10207: * - #2 p2 = (bit 16-31) - company to be merged into ((p2>>16)&0xFFFF) rubidium@10207: * @todo In the case of p1=0, create new company, the clientID of the new client is in parameter Darkvater@1796: * p2. This parameter is passed in at function DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND) Darkvater@1796: * on the server itself. First of all this is unbelievably ugly; second of all, well, Darkvater@1796: * it IS ugly! Someone fix this up :) So where to fix?@n Darkvater@1796: * @arg - network_server.c:838 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND)@n Darkvater@1796: * @arg - network_client.c:536 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_MAP) from where the map has been received Darkvater@1796: */ rubidium@10207: CommandCost CmdCompanyCtrl(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { rubidium@10207: if (flags & DC_EXEC) _current_company = OWNER_NONE; truelight@0: skidd13@9525: InvalidateWindowData(WC_COMPANY_LEAGUE, 0, 0); skidd13@9525: Darkvater@1796: switch (p1) { rubidium@10207: case 0: { /* Create a new company */ rubidium@10207: /* This command is only executed in a multiplayer game */ rubidium@10207: if (!_networking) return CMD_ERROR; rubidium@9650: rubidium@9650: #ifdef ENABLE_NETWORK rubidium@9650: rubidium@10207: /* Joining Client: rubidium@10207: * _local_company: COMPANY_SPECTATOR rubidium@10207: * _network_playas/cid = requested company/clientid rubidium@10207: * rubidium@10207: * Other client(s)/server: rubidium@10207: * _local_company/_network_playas: what they play as rubidium@10207: * cid = requested company/company of joining client */ rubidium@10207: uint16 cid = p2; // ClientID truelight@670: rubidium@10207: /* Has the network client a correct ClientID? */ rubidium@10207: if (!(flags & DC_EXEC)) return CommandCost(); rubidium@10207: if (cid >= MAX_CLIENT_INFO) return CommandCost(); truelight@690: rubidium@10207: /* Delete multiplayer progress bar */ rubidium@10207: DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0); rubidium@10207: rubidium@10207: Company *c = DoStartupNewCompany(false); rubidium@10207: rubidium@10207: /* A new company could not be created, revert to being a spectator */ rubidium@10207: if (c == NULL) { rubidium@10207: if (_network_server) { rubidium@10207: NetworkClientInfo *ci = &_network_client_info[cid]; rubidium@10207: ci->client_playas = COMPANY_SPECTATOR; rubidium@10207: NetworkUpdateClientInfo(ci->client_index); rubidium@10207: } else if (_local_company == COMPANY_SPECTATOR) { rubidium@10207: _network_playas = COMPANY_SPECTATOR; rubidium@10207: } rubidium@10207: break; rubidium@7998: } rubidium@9651: rubidium@10207: /* This is the joining client who wants a new company */ rubidium@10207: if (_local_company != _network_playas && _network_playas == c->index) { rubidium@10207: assert(_local_company == COMPANY_SPECTATOR); rubidium@10207: SetLocalCompany(c->index); rubidium@10207: if (!StrEmpty(_settings_client.network.default_company_pass)) { rubidium@10207: char *password = _settings_client.network.default_company_pass; rubidium@10207: NetworkChangeCompanyPassword(1, &password); rubidium@10207: } Darkvater@1796: rubidium@10207: _current_company = _local_company; rubidium@9658: rubidium@10207: /* Now that we have a new company, broadcast our autorenew settings to rubidium@10207: * all clients so everything is in sync */ rubidium@10207: NetworkSend_Command(0, rubidium@10207: (_settings_client.gui.autorenew << 15 ) | (_settings_client.gui.autorenew_months << 16) | 4, rubidium@10207: _settings_client.gui.autorenew_money, rubidium@10207: CMD_SET_AUTOREPLACE, rubidium@10207: NULL rubidium@10207: ); truelight@0: rubidium@10207: MarkWholeScreenDirty(); rubidium@10207: } Darkvater@1796: rubidium@10207: if (_network_server) { rubidium@10207: /* XXX - UGLY! p2 (pid) is mis-used to fetch the client-id, done at rubidium@10207: * server-side in network_server.c:838, function rubidium@10207: * DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND) */ rubidium@10207: NetworkClientInfo *ci = &_network_client_info[cid]; rubidium@10207: ci->client_playas = c->index; rubidium@10207: NetworkUpdateClientInfo(ci->client_index); Darkvater@1796: rubidium@10207: if (IsValidCompanyID(ci->client_playas)) { rubidium@10207: CompanyID company_backup = _local_company; rubidium@10207: _network_company_info[c->index].months_empty = 0; Darkvater@5067: rubidium@10207: /* XXX - When a client joins, we automatically set its name to the rubidium@10207: * client's name (for some reason). As it stands now only the server rubidium@10207: * knows the client's name, so it needs to send out a "broadcast" to rubidium@10207: * do this. To achieve this we send a network command. However, it rubidium@10207: * uses _local_company to execute the command as. To prevent abuse rubidium@10207: * (eg. only yourself can change your name/company), we 'cheat' by rubidium@10207: * impersonation _local_company as the server. Not the best solution; rubidium@10207: * but it works. rubidium@10207: * TODO: Perhaps this could be improved by when the client is ready rubidium@10207: * with joining to let it send itself the command, and not the server? rubidium@10207: * For example in network_client.c:534? */ rubidium@10207: _cmd_text = ci->client_name; rubidium@10207: _local_company = ci->client_playas; rubidium@10207: NetworkSend_Command(0, 0, 0, CMD_RENAME_PRESIDENT, NULL); rubidium@10207: _local_company = company_backup; rubidium@10207: } rubidium@10207: } rubidium@10207: #endif /* ENABLE_NETWORK */ rubidium@10207: } break; rubidium@10207: rubidium@10207: case 1: /* Make a new AI company */ rubidium@10207: if (!(flags & DC_EXEC)) return CommandCost(); rubidium@10207: rubidium@10207: DoStartupNewCompany(true); rubidium@10207: break; rubidium@10207: rubidium@10207: case 2: { /* Delete a company */ rubidium@10207: Company *c; rubidium@10207: rubidium@10207: if (!IsValidCompanyID((CompanyID)p2)) return CMD_ERROR; rubidium@10207: rubidium@10207: if (!(flags & DC_EXEC)) return CommandCost(); rubidium@10207: rubidium@10207: c = GetCompany((CompanyID)p2); rubidium@10207: rubidium@10207: /* Only allow removal of HUMAN companies */ rubidium@10207: if (IsHumanCompany(c->index)) { rubidium@10207: /* Delete any open window of the company */ rubidium@10207: DeleteCompanyWindows(c->index); rubidium@10207: rubidium@10207: CompanyNewsInformation *cni = MallocT(1); rubidium@10207: cni->FillData(c); rubidium@10207: rubidium@10207: /* Show the bankrupt news */ rubidium@10207: SetDParam(0, STR_705C_BANKRUPT); rubidium@10207: SetDParam(1, STR_705D_HAS_BEEN_CLOSED_DOWN_BY); rubidium@10207: SetDParamStr(2, cni->company_name); rubidium@10207: AddNewsItem(STR_02B6, NS_COMPANY_BANKRUPT, 0, 0, cni); rubidium@10207: rubidium@10207: /* Remove the company */ rubidium@10207: ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER); rubidium@10207: rubidium@10207: delete c; rubidium@10207: } rubidium@10207: } break; rubidium@10207: rubidium@10207: case 3: { /* Merge a company (#1) into another company (#2), elimination company #1 */ rubidium@10207: CompanyID cid_old = (CompanyID)GB(p2, 0, 16); rubidium@10207: CompanyID cid_new = (CompanyID)GB(p2, 16, 16); rubidium@10207: rubidium@10207: if (!IsValidCompanyID(cid_old) || !IsValidCompanyID(cid_new)) return CMD_ERROR; rubidium@10207: rubidium@10207: if (!(flags & DC_EXEC)) return CMD_ERROR; rubidium@10207: rubidium@10207: ChangeOwnershipOfCompanyItems(cid_old, cid_new); rubidium@10207: delete GetCompany(cid_old); rubidium@10207: } break; rubidium@10207: rubidium@10207: default: return CMD_ERROR; truelight@0: } truelight@0: rubidium@6950: return CommandCost(); truelight@0: } truelight@0: tron@4077: static const StringID _endgame_perf_titles[] = { darkvater@983: STR_0213_BUSINESSMAN, darkvater@983: STR_0213_BUSINESSMAN, darkvater@983: STR_0213_BUSINESSMAN, darkvater@983: STR_0213_BUSINESSMAN, darkvater@983: STR_0213_BUSINESSMAN, darkvater@983: STR_0214_ENTREPRENEUR, darkvater@983: STR_0214_ENTREPRENEUR, darkvater@983: STR_0215_INDUSTRIALIST, darkvater@983: STR_0215_INDUSTRIALIST, darkvater@983: STR_0216_CAPITALIST, darkvater@983: STR_0216_CAPITALIST, darkvater@983: STR_0217_MAGNATE, darkvater@983: STR_0217_MAGNATE, darkvater@983: STR_0218_MOGUL, darkvater@983: STR_0218_MOGUL, tron@4077: STR_0219_TYCOON_OF_THE_CENTURY darkvater@983: }; darkvater@983: darkvater@1016: StringID EndGameGetPerformanceTitleFromValue(uint value) darkvater@983: { tron@4077: value = minu(value / 64, lengthof(_endgame_perf_titles) - 1); Darkvater@2613: Darkvater@2613: return _endgame_perf_titles[value]; darkvater@983: } darkvater@983: rubidium@10207: /** Save the highscore for the company */ rubidium@10207: int8 SaveHighScoreValue(const Company *c) darkvater@983: { rubidium@9413: HighScore *hs = _highscore_table[_settings_game.difficulty.diff_level]; darkvater@983: uint i; rubidium@10207: uint16 score = c->old_economy[0].performance_history; darkvater@983: darkvater@1016: /* Exclude cheaters from the honour of being in the highscore table */ tron@4000: if (CheatHasBeenUsed()) return -1; darkvater@1016: darkvater@983: for (i = 0; i < lengthof(_highscore_table[0]); i++) { darkvater@983: /* You are in the TOP5. Move all values one down and save us there */ darkvater@983: if (hs[i].score <= score) { belugas@6352: /* move all elements one down starting from the replaced one */ darkvater@983: memmove(&hs[i + 1], &hs[i], sizeof(HighScore) * (lengthof(_highscore_table[0]) - i - 1)); rubidium@10207: SetDParam(0, c->index); rubidium@10207: SetDParam(1, c->index); Darkvater@4912: GetString(hs[i].company, STR_HIGHSCORE_NAME, lastof(hs[i].company)); // get manager/company name string darkvater@983: hs[i].score = score; darkvater@983: hs[i].title = EndGameGetPerformanceTitleFromValue(score); darkvater@983: return i; darkvater@983: } darkvater@983: } darkvater@983: darkvater@983: return -1; // too bad; we did not make it into the top5 darkvater@983: } darkvater@983: rubidium@10207: /** Sort all companies given their performance */ rubidium@10207: static int CDECL HighScoreSorter(const Company* const *a, const Company* const *b) darkvater@998: { skidd13@9526: return (*b)->old_economy[0].performance_history - (*a)->old_economy[0].performance_history; darkvater@998: } darkvater@998: darkvater@998: /* Save the highscores in a network game when it has ended */ darkvater@998: #define LAST_HS_ITEM lengthof(_highscore_table) - 1 rubidium@6247: int8 SaveHighScoreValueNetwork() darkvater@998: { rubidium@10207: const Company *c; rubidium@10207: const Company *cl[MAX_COMPANIES]; rubidium@9552: uint count = 0; rubidium@10207: int8 company = -1; darkvater@998: rubidium@10207: /* Sort all active companies with the highest score first */ rubidium@10207: FOR_ALL_COMPANIES(c) cl[count++] = c; skidd13@9526: rubidium@10207: GSortT(cl, count, &HighScoreSorter); darkvater@998: darkvater@998: { tron@2988: uint i; darkvater@998: darkvater@998: memset(_highscore_table[LAST_HS_ITEM], 0, sizeof(_highscore_table[0])); darkvater@998: darkvater@998: /* Copy over Top5 companies */ tron@2484: for (i = 0; i < lengthof(_highscore_table[LAST_HS_ITEM]) && i < count; i++) { tron@2988: HighScore* hs = &_highscore_table[LAST_HS_ITEM][i]; tron@2988: rubidium@10207: SetDParam(0, cl[i]->index); rubidium@10207: SetDParam(1, cl[i]->index); Darkvater@4912: GetString(hs->company, STR_HIGHSCORE_NAME, lastof(hs->company)); // get manager/company name string rubidium@10207: hs->score = cl[i]->old_economy[0].performance_history; darkvater@998: hs->title = EndGameGetPerformanceTitleFromValue(hs->score); darkvater@998: rubidium@10207: /* get the ranking of the local company */ rubidium@10207: if (cl[i]->index == _local_company) company = i; darkvater@998: } darkvater@998: } darkvater@998: rubidium@10207: /* Add top5 companys to highscore table */ rubidium@10207: return company; darkvater@998: } darkvater@998: belugas@6352: /** Save HighScore table to file */ rubidium@6247: void SaveToHighScore() darkvater@983: { Darkvater@2613: FILE *fp = fopen(_highscore_file, "wb"); darkvater@983: darkvater@983: if (fp != NULL) { darkvater@983: uint i; darkvater@983: HighScore *hs; darkvater@983: darkvater@998: for (i = 0; i < LAST_HS_ITEM; i++) { // don't save network highscores darkvater@983: for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) { darkvater@983: /* First character is a command character, so strlen will fail on that */ rubidium@9114: byte length = min(sizeof(hs->company), StrEmpty(hs->company) ? 0 : (int)strlen(&hs->company[1]) + 1); darkvater@983: rubidium@9114: if (fwrite(&length, sizeof(length), 1, fp) != 1 || // write away string length rubidium@9114: fwrite(hs->company, length, 1, fp) > 1 || // Yes... could be 0 bytes too rubidium@9114: fwrite(&hs->score, sizeof(hs->score), 1, fp) != 1 || rubidium@9114: fwrite(" ", 2, 1, fp) != 1) { // XXX - placeholder for hs->title, not saved anymore; compatibility rubidium@9114: DEBUG(misc, 1, "Could not save highscore."); rubidium@9114: i = LAST_HS_ITEM; rubidium@9114: break; rubidium@9114: } darkvater@983: } darkvater@983: } darkvater@983: fclose(fp); darkvater@983: } darkvater@983: } darkvater@983: belugas@6352: /** Initialize the highscore table to 0 and if any file exists, load in values */ rubidium@6247: void LoadFromHighScore() darkvater@983: { Darkvater@2613: FILE *fp = fopen(_highscore_file, "rb"); darkvater@983: darkvater@983: memset(_highscore_table, 0, sizeof(_highscore_table)); darkvater@983: darkvater@983: if (fp != NULL) { darkvater@983: uint i; darkvater@983: HighScore *hs; darkvater@983: darkvater@998: for (i = 0; i < LAST_HS_ITEM; i++) { // don't load network highscores darkvater@983: for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) { darkvater@983: byte length; rubidium@9114: if (fread(&length, sizeof(length), 1, fp) != 1 || rubidium@9114: fread(hs->company, length, 1, fp) > 1 || // Yes... could be 0 bytes too rubidium@9114: fread(&hs->score, sizeof(hs->score), 1, fp) != 1 || rubidium@9114: fseek(fp, 2, SEEK_CUR) == -1) { // XXX - placeholder for hs->title, not saved anymore; compatibility rubidium@9114: DEBUG(misc, 1, "Highscore corrupted"); rubidium@9114: i = LAST_HS_ITEM; rubidium@9114: break; rubidium@9114: } rubidium@9114: *lastof(hs->company) = '\0'; Darkvater@2613: hs->title = EndGameGetPerformanceTitleFromValue(hs->score); darkvater@983: } darkvater@983: } darkvater@983: fclose(fp); darkvater@983: } tron@1019: darkvater@998: /* Initialize end of game variable (when to show highscore chart) */ rubidium@9413: _settings_client.gui.ending_year = 2051; darkvater@983: } darkvater@983: rubidium@10207: /* Save/load of companies */ rubidium@10207: static const SaveLoad _company_desc[] = { rubidium@10207: SLE_VAR(Company, name_2, SLE_UINT32), rubidium@10207: SLE_VAR(Company, name_1, SLE_STRINGID), rubidium@10207: SLE_CONDSTR(Company, name, SLE_STR, 0, 84, SL_MAX_VERSION), truelight@0: rubidium@10207: SLE_VAR(Company, president_name_1, SLE_UINT16), rubidium@10207: SLE_VAR(Company, president_name_2, SLE_UINT32), rubidium@10207: SLE_CONDSTR(Company, president_name, SLE_STR, 0, 84, SL_MAX_VERSION), truelight@0: rubidium@10207: SLE_VAR(Company, face, SLE_UINT32), truelight@201: belugas@6352: /* money was changed to a 64 bit field in savegame version 1. */ rubidium@10207: SLE_CONDVAR(Company, money, SLE_VAR_I64 | SLE_FILE_I32, 0, 0), rubidium@10207: SLE_CONDVAR(Company, money, SLE_INT64, 1, SL_MAX_VERSION), truelight@0: rubidium@10207: SLE_CONDVAR(Company, current_loan, SLE_VAR_I64 | SLE_FILE_I32, 0, 64), rubidium@10207: SLE_CONDVAR(Company, current_loan, SLE_INT64, 65, SL_MAX_VERSION), rubidium@4344: rubidium@10207: SLE_VAR(Company, colour, SLE_UINT8), rubidium@10207: SLE_VAR(Company, money_fraction, SLE_UINT8), rubidium@10207: SLE_CONDVAR(Company, avail_railtypes, SLE_UINT8, 0, 57), rubidium@10207: SLE_VAR(Company, block_preview, SLE_UINT8), rubidium@4344: rubidium@10207: SLE_CONDVAR(Company, cargo_types, SLE_FILE_U16 | SLE_VAR_U32, 0, 93), rubidium@10207: SLE_CONDVAR(Company, cargo_types, SLE_UINT32, 94, SL_MAX_VERSION), rubidium@10207: SLE_CONDVAR(Company, location_of_HQ, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), rubidium@10207: SLE_CONDVAR(Company, location_of_HQ, SLE_UINT32, 6, SL_MAX_VERSION), rubidium@10207: SLE_CONDVAR(Company, last_build_coordinate, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), rubidium@10207: SLE_CONDVAR(Company, last_build_coordinate, SLE_UINT32, 6, SL_MAX_VERSION), rubidium@10207: SLE_CONDVAR(Company, inaugurated_year, SLE_FILE_U8 | SLE_VAR_I32, 0, 30), rubidium@10207: SLE_CONDVAR(Company, inaugurated_year, SLE_INT32, 31, SL_MAX_VERSION), rubidium@10207: rubidium@10207: SLE_ARR(Company, share_owners, SLE_UINT8, 4), rubidium@10207: rubidium@10207: SLE_VAR(Company, num_valid_stat_ent, SLE_UINT8), rubidium@10207: rubidium@10207: SLE_VAR(Company, quarters_of_bankrupcy, SLE_UINT8), rubidium@10207: SLE_VAR(Company, bankrupt_asked, SLE_UINT8), rubidium@10207: SLE_VAR(Company, bankrupt_timeout, SLE_INT16), rubidium@10207: SLE_CONDVAR(Company, bankrupt_value, SLE_VAR_I64 | SLE_FILE_I32, 0, 64), rubidium@10207: SLE_CONDVAR(Company, bankrupt_value, SLE_INT64, 65, SL_MAX_VERSION), truelight@0: belugas@6352: /* yearly expenses was changed to 64-bit in savegame version 2. */ rubidium@10207: SLE_CONDARR(Company, yearly_expenses, SLE_FILE_I32 | SLE_VAR_I64, 3 * 13, 0, 1), rubidium@10207: SLE_CONDARR(Company, yearly_expenses, SLE_INT64, 3 * 13, 2, SL_MAX_VERSION), truelight@0: rubidium@10207: SLE_CONDVAR(Company, is_ai, SLE_BOOL, 2, SL_MAX_VERSION), rubidium@9659: SLE_CONDNULL(1, 4, 99), truelight@0: belugas@6352: /* Engine renewal settings */ Darkvater@3222: SLE_CONDNULL(512, 16, 18), rubidium@10207: SLE_CONDREF(Company, engine_renew_list, REF_ENGINE_RENEWS, 19, SL_MAX_VERSION), rubidium@10207: SLE_CONDVAR(Company, engine_renew, SLE_BOOL, 16, SL_MAX_VERSION), rubidium@10207: SLE_CONDVAR(Company, engine_renew_months, SLE_INT16, 16, SL_MAX_VERSION), rubidium@10207: SLE_CONDVAR(Company, engine_renew_money, SLE_UINT32, 16, SL_MAX_VERSION), rubidium@10207: SLE_CONDVAR(Company, renew_keep_length, SLE_BOOL, 2, SL_MAX_VERSION), // added with 16.1, but was blank since 2 bjarni@2293: belugas@6352: /* reserve extra space in savegame here. (currently 63 bytes) */ Darkvater@3222: SLE_CONDNULL(63, 2, SL_MAX_VERSION), truelight@0: truelight@0: SLE_END() truelight@0: }; truelight@0: rubidium@10207: static const SaveLoad _company_economy_desc[] = { belugas@6352: /* these were changed to 64-bit in savegame format 2 */ rubidium@10207: SLE_CONDVAR(CompanyEconomyEntry, income, SLE_FILE_I32 | SLE_VAR_I64, 0, 1), rubidium@10207: SLE_CONDVAR(CompanyEconomyEntry, income, SLE_INT64, 2, SL_MAX_VERSION), rubidium@10207: SLE_CONDVAR(CompanyEconomyEntry, expenses, SLE_FILE_I32 | SLE_VAR_I64, 0, 1), rubidium@10207: SLE_CONDVAR(CompanyEconomyEntry, expenses, SLE_INT64, 2, SL_MAX_VERSION), rubidium@10207: SLE_CONDVAR(CompanyEconomyEntry, company_value, SLE_FILE_I32 | SLE_VAR_I64, 0, 1), rubidium@10207: SLE_CONDVAR(CompanyEconomyEntry, company_value, SLE_INT64, 2, SL_MAX_VERSION), truelight@0: rubidium@10207: SLE_VAR(CompanyEconomyEntry, delivered_cargo, SLE_INT32), rubidium@10207: SLE_VAR(CompanyEconomyEntry, performance_history, SLE_INT32), truelight@201: truelight@0: SLE_END() truelight@0: }; truelight@0: rubidium@10207: static const SaveLoad _company_livery_desc[] = { peter1138@4603: SLE_CONDVAR(Livery, in_use, SLE_BOOL, 34, SL_MAX_VERSION), peter1138@4603: SLE_CONDVAR(Livery, colour1, SLE_UINT8, 34, SL_MAX_VERSION), peter1138@4603: SLE_CONDVAR(Livery, colour2, SLE_UINT8, 34, SL_MAX_VERSION), peter1138@4603: SLE_END() peter1138@4603: }; peter1138@4603: rubidium@10207: static void SaveLoad_PLYR(Company *c) tron@2952: { truelight@0: int i; truelight@0: rubidium@10207: SlObject(c, _company_desc); truelight@0: belugas@6352: /* Write AI? */ rubidium@10207: if (!IsHumanCompany(c->index)) { rubidium@10207: SaveLoad_AI(c->index); truelight@0: } truelight@0: belugas@6352: /* Write economy */ rubidium@10207: SlObject(&c->cur_economy, _company_economy_desc); truelight@0: belugas@6352: /* Write old economy entries. */ rubidium@10207: for (i = 0; i < c->num_valid_stat_ent; i++) { rubidium@10207: SlObject(&c->old_economy[i], _company_economy_desc); truelight@0: } peter1138@4603: belugas@6352: /* Write each livery entry. */ rubidium@8309: int num_liveries = CheckSavegameVersion(63) ? LS_END - 4 : (CheckSavegameVersion(85) ? LS_END - 2: LS_END); peter1138@6724: for (i = 0; i < num_liveries; i++) { rubidium@10207: SlObject(&c->livery[i], _company_livery_desc); peter1138@4603: } peter1138@6724: rubidium@8309: if (num_liveries < LS_END) { rubidium@8309: /* We want to insert some liveries somewhere in between. This means some have to be moved. */ rubidium@10207: memmove(&c->livery[LS_FREIGHT_WAGON], &c->livery[LS_PASSENGER_WAGON_MONORAIL], (LS_END - LS_FREIGHT_WAGON) * sizeof(c->livery[0])); rubidium@10207: c->livery[LS_PASSENGER_WAGON_MONORAIL] = c->livery[LS_MONORAIL]; rubidium@10207: c->livery[LS_PASSENGER_WAGON_MAGLEV] = c->livery[LS_MAGLEV]; rubidium@8309: } rubidium@8309: rubidium@8309: if (num_liveries == LS_END - 4) { peter1138@6724: /* Copy bus/truck liveries over to trams */ rubidium@10207: c->livery[LS_PASSENGER_TRAM] = c->livery[LS_BUS]; rubidium@10207: c->livery[LS_FREIGHT_TRAM] = c->livery[LS_TRUCK]; peter1138@6724: } truelight@0: } truelight@0: rubidium@6247: static void Save_PLYR() truelight@0: { rubidium@10207: Company *c; rubidium@10207: FOR_ALL_COMPANIES(c) { rubidium@10207: SlSetArrayIndex(c->index); rubidium@10207: SlAutolength((AutolengthProc*)SaveLoad_PLYR, c); truelight@0: } truelight@0: } truelight@0: rubidium@6247: static void Load_PLYR() truelight@0: { truelight@0: int index; truelight@0: while ((index = SlIterateArray()) != -1) { rubidium@10207: Company *c = new (index) Company(); rubidium@10207: SaveLoad_PLYR(c); rubidium@10207: _company_colours[index] = c->colour; truelight@2395: truelight@2395: /* This is needed so an AI is attached to a loaded AI */ rubidium@10207: if (c->is_ai && (!_networking || _network_server) && _ai.enabled) { rubidium@9514: /* Clear the memory of the new AI, otherwise we might be doing wrong things. */ rubidium@10207: memset(&_companies_ainew[index], 0, sizeof(CompanyAiNew)); rubidium@10207: AI_StartNewAI(c->index); rubidium@9514: } truelight@0: } truelight@0: } truelight@0: rubidium@10207: extern const ChunkHandler _company_chunk_handlers[] = { truelight@0: { 'PLYR', Save_PLYR, Load_PLYR, CH_ARRAY | CH_LAST}, truelight@0: };