From 2ee73b50b8dae28f53acabcb5b7c0e3f02139db2 Mon Sep 17 00:00:00 2001 From: rubidium Date: Mon, 18 Jun 2007 21:44:47 +0000 Subject: (svn r10208) -Codechange: replace int32 with Money where appropriate. --- src/aircraft_cmd.cpp | 2 +- src/clear_cmd.cpp | 2 +- src/command.cpp | 4 +- src/economy.cpp | 87 ++++++++++++++++++++++-------------------- src/main_gui.cpp | 2 +- src/misc_cmd.cpp | 6 +-- src/misc_gui.cpp | 2 +- src/openttd.h | 103 +++++++++++++++++++++++++------------------------- src/player.h | 10 ++--- src/players.cpp | 18 ++++++--- src/roadveh_cmd.cpp | 2 +- src/ship_cmd.cpp | 2 +- src/station.h | 2 +- src/train_cmd.cpp | 4 +- src/unmovable_cmd.cpp | 2 +- src/vehicle.h | 10 ++--- 16 files changed, 137 insertions(+), 121 deletions(-) diff --git a/src/aircraft_cmd.cpp b/src/aircraft_cmd.cpp index 5215b4a0e..6a190ef08 100644 --- a/src/aircraft_cmd.cpp +++ b/src/aircraft_cmd.cpp @@ -493,7 +493,7 @@ CommandCost CmdSellAircraft(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) DoDeleteAircraft(v); } - return CommandCost(-(int32)v->value); + return CommandCost(-v->value); } /** Start/Stop an aircraft. diff --git a/src/clear_cmd.cpp b/src/clear_cmd.cpp index a1a632d61..6d9d0fca2 100644 --- a/src/clear_cmd.cpp +++ b/src/clear_cmd.cpp @@ -446,7 +446,7 @@ CommandCost CmdPurchaseLandArea(TileIndex tile, uint32 flags, uint32 p1, uint32 static CommandCost ClearTile_Clear(TileIndex tile, byte flags) { - static const int32* clear_price_table[] = { + static const Money* clear_price_table[] = { &_price.clear_1, &_price.purchase_land, &_price.clear_2, diff --git a/src/command.cpp b/src/command.cpp index c55846679..31c0d417e 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -602,7 +602,7 @@ CommandCost CommandCost::AddCost(CommandCost ret) return *this; } -CommandCost CommandCost::AddCost(int32 cost) +CommandCost CommandCost::AddCost(Money cost) { this->cost += cost; return *this; @@ -614,7 +614,7 @@ CommandCost CommandCost::MultiplyCost(int factor) return *this; } -int32 CommandCost::GetCost() const +Money CommandCost::GetCost() const { return this->cost; } diff --git a/src/economy.cpp b/src/economy.cpp index aa2be9247..47e1ebebe 100644 --- a/src/economy.cpp +++ b/src/economy.cpp @@ -57,10 +57,11 @@ const ScoreInfo _score_info[] = { int _score_part[MAX_PLAYERS][SCORE_END]; -int64 CalculateCompanyValue(const Player* p) +Money CalculateCompanyValue(const Player* p) { PlayerID owner = p->index; - int64 value; + /* Do a little nasty by using CommandCost, so we can use the "overflow" protection of CommandCost */ + CommandCost value; { Station *st; @@ -70,7 +71,7 @@ int64 CalculateCompanyValue(const Player* p) if (st->owner == owner) num += CountBitsSet(st->facilities); } - value = num * _price.station_value * 25; + value.AddCost(num * _price.station_value * 25); } { @@ -83,14 +84,16 @@ int64 CalculateCompanyValue(const Player* p) v->type == VEH_ROAD || (v->type == VEH_AIRCRAFT && IsNormalAircraft(v)) || v->type == VEH_SHIP) { - value += v->value * 3 >> 1; + value.AddCost(v->value * 3 >> 1); } } } - value += p->player_money - p->current_loan; // add real money value + /* Add real money value */ + value.AddCost(-p->current_loan); + value.AddCost(p->player_money); - return max(value, 1LL); + return max(value.GetCost(), 1); } /** if update is set to true, the economy is updated with this score @@ -148,16 +151,12 @@ int UpdateCompanyRatingAndValue(Player *p, bool update) /* Generate statistics depending on recent income statistics */ { - const PlayerEconomyEntry* pee; - int numec; - int32 min_income; - int32 max_income; - - numec = min(p->num_valid_stat_ent, 12); + int numec = min(p->num_valid_stat_ent, 12); if (numec != 0) { - min_income = 0x7FFFFFFF; - max_income = 0; - pee = p->old_economy; + const PlayerEconomyEntry *pee = p->old_economy; + Money min_income = pee->income + pee->expenses; + Money max_income = pee->income + pee->expenses; + do { min_income = min(min_income, pee->income + pee->expenses); max_income = max(max_income, pee->income + pee->expenses); @@ -658,11 +657,17 @@ static void PlayersGenStatistics() InvalidateWindow(WC_COMPANY_LEAGUE, 0); } -static void AddSingleInflation(int32 *value, uint16 *frac, int32 amt) +static void AddSingleInflation(Money *value, uint16 *frac, int32 amt) { - int64 tmp = (int64)*value * amt + *frac; - *frac = GB(tmp, 0, 16); - *value += tmp >> 16; + /* Is it safe to add inflation ? */ + if ((MAX_UVALUE(Money) / 2 / amt) > (*value + *frac + 1)) { + *value = MAX_UVALUE(Money); + *frac = 0; + } else { + int64 tmp = (int64)*value * amt + *frac; + *frac = GB(tmp, 0, 16); + *value += tmp >> 16; + } } static void AddInflation() @@ -672,10 +677,10 @@ static void AddInflation() * 12 -> months per year * This is only a good approxiamtion for small values */ - int32 inf = _economy.infl_amount * 54; + Money inf = _economy.infl_amount * 54; for (uint i = 0; i != NUM_PRICES; i++) { - AddSingleInflation((int32*)&_price + i, _price_frac + i, inf); + AddSingleInflation((Money*)&_price + i, _price_frac + i, inf); } _economy.max_loan_unround += BIGMULUS(_economy.max_loan_unround, inf, 16); @@ -686,7 +691,7 @@ static void AddInflation() inf = _economy.infl_amount_pr * 54; for (CargoID i = 0; i < NUM_CARGO; i++) { AddSingleInflation( - (int32*)_cargo_payment_rates + i, + (Money*)_cargo_payment_rates + i, _cargo_payment_rates_frac + i, inf ); @@ -709,7 +714,7 @@ static void PlayersPayInterest() _current_player = p->index; SET_EXPENSES_TYPE(EXPENSES_LOAN_INT); - SubtractMoneyFromPlayer(CommandCost(BIGMULUS(p->current_loan, interest, 16))); + SubtractMoneyFromPlayer(CommandCost((Money)BIGMULUS(p->current_loan, interest, 16))); SET_EXPENSES_TYPE(EXPENSES_OTHER); SubtractMoneyFromPlayer(_price.station_value >> 2); @@ -739,7 +744,7 @@ static byte _price_category[NUM_PRICES] = { 2, }; -static const int32 _price_base[NUM_PRICES] = { +static const Money _price_base[NUM_PRICES] = { 100, ///< station_value 100, ///< build_rail 95, ///< build_road @@ -822,10 +827,10 @@ void StartupEconomy() { int i; - assert(sizeof(_price) == NUM_PRICES * sizeof(int32)); + assert(sizeof(_price) == NUM_PRICES * sizeof(Money)); for (i = 0; i != NUM_PRICES; i++) { - int32 price = _price_base[i]; + Money price = _price_base[i]; if (_price_category[i] != 0) { uint mod = _price_category[i] == 1 ? _opt.diff.vehicle_costs : _opt.diff.construction_cost; if (mod < 1) { @@ -839,7 +844,7 @@ void StartupEconomy() } else { price >>= 8 - price_base_multiplier[i]; } - ((int32*)&_price)[i] = price; + ((Money*)&_price)[i] = price; _price_frac[i] = 0; } @@ -1368,14 +1373,14 @@ void VehiclePayment(Vehicle *front_v) { int result = 0; - int profit = 0; - int total_veh_profit = 0; // accumulates the profit across the vehicle chain (used by trains) - int32 route_profit = 0; // the grand total amount for the route. A-D of transfer chain A-B-C-D - int virtual_profit = 0; // virtual profit of one vehicle element for feeder systems - int virtual_profit_total = 0; // virtual profit for entire vehicle chain - int total_cargo_feeder_share = 0; // the feeder cash amount for the goods being loaded/unloaded in this load step + Money profit = 0; + Money total_veh_profit = 0; // accumulates the profit across the vehicle chain (used by trains) + Money route_profit = 0; // the grand total amount for the route. A-D of transfer chain A-B-C-D + Money virtual_profit = 0; // virtual profit of one vehicle element for feeder systems + Money virtual_profit_total = 0; // virtual profit for entire vehicle chain + Money total_cargo_feeder_share = 0; // the feeder cash amount for the goods being loaded/unloaded in this load step - int all_vehicles_cargo_feeder_share = front_v->cargo_feeder_share; // used to hold transfer value of complete vehicle chain - used by trains + Money all_vehicles_cargo_feeder_share = front_v->cargo_feeder_share; // used to hold transfer value of complete vehicle chain - used by trains StationID last_visited = front_v->last_station_visited; Station *st = GetStation(last_visited); @@ -1766,7 +1771,7 @@ static void DoAcquireCompany(Player *p) { Player *owner; int i; - int64 value; + Money value; SetDParam(0, p->name_1); SetDParam(1, p->name_2); @@ -1784,13 +1789,15 @@ static void DoAcquireCompany(Player *p) } value = CalculateCompanyValue(p) >> 2; + PlayerID old_player = _current_player; for (i = 0; i != 4; i++) { if (p->share_owners[i] != PLAYER_SPECTATOR) { - owner = GetPlayer(p->share_owners[i]); - owner->player_money += value; - owner->yearly_expenses[0][EXPENSES_OTHER] += value; + SET_EXPENSES_TYPE(EXPENSES_OTHER); + _current_player = p->share_owners[i]; + SubtractMoneyFromPlayer(CommandCost(-value)); } } + _current_player = old_player; p->is_active = false; @@ -1855,7 +1862,7 @@ CommandCost CmdBuyShareInCompany(TileIndex tile, uint32 flags, uint32 p1, uint32 CommandCost CmdSellShareInCompany(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) { Player *p; - int64 cost; + Money cost; /* Check if buying shares is allowed (protection against modified clients */ if (!IsValidPlayer((PlayerID)p1) || !_patches.allow_shares) return CMD_ERROR; @@ -1876,7 +1883,7 @@ CommandCost CmdSellShareInCompany(TileIndex tile, uint32 flags, uint32 p1, uint3 *b = PLAYER_SPECTATOR; InvalidateWindow(WC_COMPANY, p1); } - return CommandCost((int32)cost); + return CommandCost(cost); } /** Buy up another company. diff --git a/src/main_gui.cpp b/src/main_gui.cpp index bcf4d390a..430d9948f 100644 --- a/src/main_gui.cpp +++ b/src/main_gui.cpp @@ -92,7 +92,7 @@ void HandleOnEditText(const char *str) #ifdef ENABLE_NETWORK case 3: { // Give money, you can only give money in excess of loan const Player *p = GetPlayer(_current_player); - int32 money = min(p->player_money - p->current_loan, atoi(str) / _currency->rate); + Money money = min(p->player_money - p->current_loan, atoi(str) / _currency->rate); money = clamp(money, 0, 20000000); // Clamp between 20 million and 0 diff --git a/src/misc_cmd.cpp b/src/misc_cmd.cpp index d96689f67..ecbf5d8bf 100644 --- a/src/misc_cmd.cpp +++ b/src/misc_cmd.cpp @@ -133,7 +133,7 @@ CommandCost CmdIncreaseLoan(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) return_cmd_error(STR_702B_MAXIMUM_PERMITTED_LOAN); } - int32 loan; + Money loan; switch (p2) { default: return CMD_ERROR; // Invalid method case 0: // Take some extra loan @@ -289,7 +289,7 @@ CommandCost CmdMoneyCheat(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) if (_networking) return CMD_ERROR; #endif SET_EXPENSES_TYPE(EXPENSES_OTHER); - return CommandCost(-(int32)p1); + return CommandCost(-(Money)p1); } /** Transfer funds (money) from one player to another. @@ -304,7 +304,7 @@ CommandCost CmdMoneyCheat(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) CommandCost CmdGiveMoney(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) { const Player *p = GetPlayer(_current_player); - CommandCost amount(min((int32)p1, 20000000)); + CommandCost amount((Money)min(p1, 20000000LL)); SET_EXPENSES_TYPE(EXPENSES_OTHER); diff --git a/src/misc_gui.cpp b/src/misc_gui.cpp index 2444aa27b..5ab378297 100644 --- a/src/misc_gui.cpp +++ b/src/misc_gui.cpp @@ -89,7 +89,7 @@ static void Place_LandInfo(TileIndex tile) Player *p; Window *w; Town *t; - int64 old_money; + Money old_money; CommandCost costclear; AcceptedCargo ac; TileDesc td; diff --git a/src/openttd.h b/src/openttd.h index 290ca968b..b64c982d9 100644 --- a/src/openttd.h +++ b/src/openttd.h @@ -69,6 +69,7 @@ typedef uint16 SignID; typedef uint16 GroupID; typedef uint16 EngineRenewID; typedef uint16 DestinationID; +typedef int32 Money; /* DestinationID must be at least as large as every these below, because it can * be any of them @@ -226,53 +227,53 @@ enum { }; struct Prices { - int32 station_value; - int32 build_rail; - int32 build_road; - int32 build_signals; - int32 build_bridge; - int32 build_train_depot; - int32 build_road_depot; - int32 build_ship_depot; - int32 build_tunnel; - int32 train_station_track; - int32 train_station_length; - int32 build_airport; - int32 build_bus_station; - int32 build_truck_station; - int32 build_dock; - int32 build_railvehicle; - int32 build_railwagon; - int32 aircraft_base; - int32 roadveh_base; - int32 ship_base; - int32 build_trees; - int32 terraform; - int32 clear_1; - int32 purchase_land; - int32 clear_2; - int32 clear_3; - int32 remove_trees; - int32 remove_rail; - int32 remove_signals; - int32 clear_bridge; - int32 remove_train_depot; - int32 remove_road_depot; - int32 remove_ship_depot; - int32 clear_tunnel; - int32 clear_water; - int32 remove_rail_station; - int32 remove_airport; - int32 remove_bus_station; - int32 remove_truck_station; - int32 remove_dock; - int32 remove_house; - int32 remove_road; - int32 running_rail[3]; - int32 aircraft_running; - int32 roadveh_running; - int32 ship_running; - int32 build_industry; + Money station_value; + Money build_rail; + Money build_road; + Money build_signals; + Money build_bridge; + Money build_train_depot; + Money build_road_depot; + Money build_ship_depot; + Money build_tunnel; + Money train_station_track; + Money train_station_length; + Money build_airport; + Money build_bus_station; + Money build_truck_station; + Money build_dock; + Money build_railvehicle; + Money build_railwagon; + Money aircraft_base; + Money roadveh_base; + Money ship_base; + Money build_trees; + Money terraform; + Money clear_1; + Money purchase_land; + Money clear_2; + Money clear_3; + Money remove_trees; + Money remove_rail; + Money remove_signals; + Money clear_bridge; + Money remove_train_depot; + Money remove_road_depot; + Money remove_ship_depot; + Money clear_tunnel; + Money clear_water; + Money remove_rail_station; + Money remove_airport; + Money remove_bus_station; + Money remove_truck_station; + Money remove_dock; + Money remove_house; + Money remove_road; + Money running_rail[3]; + Money aircraft_running; + Money roadveh_running; + Money ship_running; + Money build_industry; }; #define GAME_DIFFICULTY_NUM 18 @@ -365,7 +366,7 @@ struct ViewportSign { * a possible error message/state together. */ class CommandCost { - int32 cost; ///< The cost of this action + Money cost; ///< The cost of this action StringID message; ///< Warning message for when success is unset bool success; ///< Whether the comment went fine up to this moment @@ -384,7 +385,7 @@ public: * Creates a command return value with the given start cost * @param cst the initial cost of this command */ - CommandCost(int32 cst) : cost(cst), message(INVALID_STRING_ID), success(true) {} + CommandCost(Money cst) : cost(cst), message(INVALID_STRING_ID), success(true) {} /** "Hack" to make everything compile nicely, not needed when cost is int64 */ CommandCost(uint cst) : cost(cst), message(INVALID_STRING_ID), success(true) {} @@ -401,7 +402,7 @@ public: * @param cost the cost to add * @return this class. */ - CommandCost AddCost(int32 cost); + CommandCost AddCost(Money cost); /** * Multiplies the cost of the command by the given factor. @@ -414,7 +415,7 @@ public: * The costs as made up to this moment * @return the costs */ - int32 GetCost() const; + Money GetCost() const; /** * Sets the global error message *if* this class has one. diff --git a/src/player.h b/src/player.h index ce07ba204..cf25473c6 100644 --- a/src/player.h +++ b/src/player.h @@ -12,8 +12,8 @@ #include "livery.h" struct PlayerEconomyEntry { - int32 income; - int32 expenses; + Money income; + Money expenses; int32 delivered_cargo; int32 performance_history; ///< player score (scale 0-1000) int64 company_value; @@ -166,8 +166,8 @@ struct Player { PlayerFace face; - int32 current_loan; int64 player_money; + Money current_loan; byte player_color; Livery livery[LS_END]; @@ -190,7 +190,7 @@ struct Player { byte quarters_of_bankrupcy; byte bankrupt_asked; ///< which players were asked about buying it? int16 bankrupt_timeout; - int32 bankrupt_value; + Money bankrupt_value; bool is_active; bool is_ai; @@ -212,7 +212,7 @@ uint16 GetDrawStringPlayerColor(PlayerID player); void ChangeOwnershipOfPlayerItems(PlayerID old_player, PlayerID new_player); void GetNameOfOwner(Owner owner, TileIndex tile); -int64 CalculateCompanyValue(const Player* p); +Money CalculateCompanyValue(const Player* p); void InvalidatePlayerWindows(const Player* p); void SetLocalPlayer(PlayerID new_player); #define FOR_ALL_PLAYERS(p) for (p = _players; p != endof(_players); p++) diff --git a/src/players.cpp b/src/players.cpp index efc6056db..f099008ec 100644 --- a/src/players.cpp +++ b/src/players.cpp @@ -188,22 +188,30 @@ bool CheckPlayerHasMoney(CommandCost cost) static void SubtractMoneyFromAnyPlayer(Player *p, CommandCost cost) { - p->player_money -= cost.GetCost(); + CommandCost tmp((int32)p->player_money); + tmp.AddCost(-cost.GetCost()); + p->player_money = tmp.GetCost(); - p->yearly_expenses[0][_yearly_expenses_type] += cost.GetCost(); + tmp = CommandCost((int32)p->yearly_expenses[0][_yearly_expenses_type]); + tmp.AddCost(cost); + p->yearly_expenses[0][_yearly_expenses_type] = tmp.GetCost(); if (HASBIT(1 << EXPENSES_TRAIN_INC | 1 << EXPENSES_ROADVEH_INC | 1 << EXPENSES_AIRCRAFT_INC | 1 << EXPENSES_SHIP_INC, _yearly_expenses_type)) { - p->cur_economy.income -= cost.GetCost(); + tmp = CommandCost(p->cur_economy.income); + tmp.AddCost(-cost.GetCost()); + p->cur_economy.income = tmp.GetCost(); } else if (HASBIT(1 << EXPENSES_TRAIN_RUN | 1 << EXPENSES_ROADVEH_RUN | 1 << EXPENSES_AIRCRAFT_RUN | 1 << EXPENSES_SHIP_RUN | 1 << EXPENSES_PROPERTY | 1 << EXPENSES_LOAN_INT, _yearly_expenses_type)) { - p->cur_economy.expenses -= cost.GetCost(); + tmp = CommandCost(p->cur_economy.expenses); + tmp.AddCost(-cost.GetCost()); + p->cur_economy.expenses = tmp.GetCost(); } InvalidatePlayerWindows(p); @@ -220,7 +228,7 @@ void SubtractMoneyFromPlayerFract(PlayerID player, CommandCost cst) { Player *p = GetPlayer(player); byte m = p->player_money_fraction; - int32 cost = cst.GetCost(); + Money cost = cst.GetCost(); p->player_money_fraction = m - (byte)cost; cost >>= 8; diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp index c5426eaeb..5688b47c0 100644 --- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -373,7 +373,7 @@ CommandCost CmdSellRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) DeleteVehicle(v); } - return CommandCost(-(int32)v->value); + return CommandCost(-v->value); } struct RoadFindDepotData { diff --git a/src/ship_cmd.cpp b/src/ship_cmd.cpp index 27840c9e3..90edf4719 100644 --- a/src/ship_cmd.cpp +++ b/src/ship_cmd.cpp @@ -929,7 +929,7 @@ CommandCost CmdSellShip(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) DeleteVehicle(v); } - return CommandCost(-(int32)v->value); + return CommandCost(-v->value); } /** Start/Stop a ship. diff --git a/src/station.h b/src/station.h index 86857860a..6a70bcc34 100644 --- a/src/station.h +++ b/src/station.h @@ -39,7 +39,7 @@ struct GoodsEntry { byte enroute_time; byte last_speed; byte last_age; - int32 feeder_profit; + Money feeder_profit; }; /** A Stop for a Road Vehicle */ diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index 98e6adf35..f04121db6 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -3353,9 +3353,9 @@ static void CheckIfTrainNeedsService(Vehicle *v) InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); } -int32 GetTrainRunningCost(const Vehicle *v) +Money GetTrainRunningCost(const Vehicle *v) { - int32 cost = 0; + Money cost = 0; do { const RailVehicleInfo *rvi = RailVehInfo(v->engine_type); diff --git a/src/unmovable_cmd.cpp b/src/unmovable_cmd.cpp index c1921e9a0..747e24979 100644 --- a/src/unmovable_cmd.cpp +++ b/src/unmovable_cmd.cpp @@ -49,7 +49,7 @@ static CommandCost DestroyCompanyHQ(PlayerID pid, uint32 flags) } /* cost of relocating company is 1% of company value */ - return CommandCost((int32)(CalculateCompanyValue(p) / 100)); + return CommandCost(CalculateCompanyValue(p) / 100); } void UpdateCompanyHQ(Player *p, uint score) diff --git a/src/vehicle.h b/src/vehicle.h index f9103c930..549e04e11 100644 --- a/src/vehicle.h +++ b/src/vehicle.h @@ -312,11 +312,11 @@ struct Vehicle { uint16 cargo_paid_for; // How much of the cargo currently on board has been paid for. byte vehicle_flags; // Used for gradual loading and other miscellaneous things (@see VehicleFlags enum) - int32 profit_this_year; - int32 profit_last_year; - int32 cargo_feeder_share; ///< value of feeder pickup to be paid for on delivery of cargo + Money profit_this_year; + Money profit_last_year; + Money cargo_feeder_share; ///< value of feeder pickup to be paid for on delivery of cargo TileIndex cargo_loaded_at_xy; ///< tile index where feeder cargo was loaded - uint32 value; + Money value; GroupID group_id; ///< Index of group Pool array @@ -548,7 +548,7 @@ UnitID GetFreeUnitNumber(VehicleType type); void TrainConsistChanged(Vehicle *v); void TrainPowerChanged(Vehicle *v); -int32 GetTrainRunningCost(const Vehicle *v); +Money GetTrainRunningCost(const Vehicle *v); int CheckTrainStoppedInDepot(const Vehicle *v); -- cgit v1.2.3-54-g00ecf