summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoralberth <alberth@openttd.org>2010-08-06 20:10:53 +0000
committeralberth <alberth@openttd.org>2010-08-06 20:10:53 +0000
commit5cfc0295236de7f1d32ca23519f35a3ee883f220 (patch)
tree7ad2292e6e93e789b984c25be956a3e5d486ddb0 /src
parente0e66b6ed0c1085bdaefc6ac951c043a51525975 (diff)
downloadopenttd-5cfc0295236de7f1d32ca23519f35a3ee883f220.tar.xz
(svn r20391) -Fix [FS#3993]: Prevent buying more vehicles than allowed.
Diffstat (limited to 'src')
-rw-r--r--src/vehicle.cpp20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/vehicle.cpp b/src/vehicle.cpp
index 8673baef1..0fde74128 100644
--- a/src/vehicle.cpp
+++ b/src/vehicle.cpp
@@ -1321,8 +1321,28 @@ UnitID FreeUnitIDGenerator::NextID()
return this->curid;
}
+/**
+ * Get an unused unit number for a vehicle (if allowed).
+ * @param type Type of vehicle
+ * @return A unused unit number for the given type of vehicle if it is allowed to build one, else \c UINT16_MAX.
+ */
UnitID GetFreeUnitNumber(VehicleType type)
{
+ /* Check whether it is allowed to build another vehicle. */
+ uint max_veh;
+ switch (type) {
+ case VEH_TRAIN: max_veh = _settings_game.vehicle.max_trains; break;
+ case VEH_ROAD: max_veh = _settings_game.vehicle.max_roadveh; break;
+ case VEH_SHIP: max_veh = _settings_game.vehicle.max_ships; break;
+ case VEH_AIRCRAFT: max_veh = _settings_game.vehicle.max_aircraft; break;
+ default: NOT_REACHED();
+ }
+
+ uint amounts[4];
+ CountCompanyVehicles(_current_company, amounts);
+ assert((uint)type < lengthof(amounts));
+ if (amounts[type] >= max_veh) return UINT16_MAX; // Currently already at the limit, no room to make a new one.
+
FreeUnitIDGenerator gen(type, _current_company);
return gen.NextID();