summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbjarni <bjarni@openttd.org>2005-11-05 14:01:00 +0000
committerbjarni <bjarni@openttd.org>2005-11-05 14:01:00 +0000
commitd86829d0e09fd981cc65c85d65f75129f9299b8f (patch)
tree1a3529cb495112b582023d8dc1ab63541d00c9dc
parent7d4d13df3519d6db4b324b132c32325d25f7f5c3 (diff)
downloadopenttd-d86829d0e09fd981cc65c85d65f75129f9299b8f.tar.xz
(svn r3138) -Fix: [clone vehicles] fixed assert when it was possible to allocate some vehicles to clone a train, but not for all cars
Now it gives "too many vehicles" error message instead To make this work, AllocateVehicles() needed to be moved to vehicle.c (from aircraft_cmd.c) and made non-static
-rw-r--r--aircraft_cmd.c23
-rw-r--r--vehicle.c48
-rw-r--r--vehicle.h1
3 files changed, 48 insertions, 24 deletions
diff --git a/aircraft_cmd.c b/aircraft_cmd.c
index 2478a8e10..f096b92d4 100644
--- a/aircraft_cmd.c
+++ b/aircraft_cmd.c
@@ -121,29 +121,6 @@ void DrawAircraftEngine(int x, int y, EngineID engine, uint32 image_ormod)
}
}
-/* Allocate many vehicles */
-static bool AllocateVehicles(Vehicle **vl, int num)
-{
- int i;
- Vehicle *v;
- bool success = true;
-
- for(i=0; i!=num; i++) {
- vl[i] = v = AllocateVehicle();
- if (v == NULL) {
- success = false;
- break;
- }
- v->type = 1;
- }
-
- while (--i >= 0) {
- vl[i]->type = 0;
- }
-
- return success;
-}
-
int32 EstimateAircraftCost(EngineID engine_type)
{
return AircraftVehInfo(engine_type)->base_cost * (_price.aircraft_base>>3)>>5;
diff --git a/vehicle.c b/vehicle.c
index b88811bdf..f63368912 100644
--- a/vehicle.c
+++ b/vehicle.c
@@ -315,6 +315,34 @@ Vehicle *AllocateVehicle(void)
return NULL;
}
+/** Allocates a lot of vehicles and frees them again
+* @param vl pointer to an array of vehicles that can store all the vehicles in the test
+* @param num number of vehicles to test for
+* returns true if there is room to allocate all the vehicles
+*/
+bool AllocateVehicles(Vehicle **vl, int num)
+{
+ int i;
+ Vehicle *v;
+ bool success = true;
+
+ for(i = 0; i != num; i++) {
+ vl[i] = v = AllocateVehicle();
+ if (v == NULL) {
+ success = false;
+ break;
+ }
+ v->type = 1;
+ }
+
+ while (--i >= 0) {
+ vl[i]->type = 0;
+ }
+
+ return success;
+}
+
+
void *VehicleFromPos(TileIndex tile, void *data, VehicleFromPosProc *proc)
{
int x,y,x2,y2;
@@ -1482,7 +1510,6 @@ int32 CmdCloneVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2)
Vehicle *v_front, *v;
Vehicle *w_front, *w, *w_rear;
int cost, total_cost = 0;
-// VehicleID *new_id;
if (!IsVehicleIndex(p1))
return CMD_ERROR;
@@ -1506,6 +1533,25 @@ int32 CmdCloneVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2)
if (v->type == VEH_Train && v->subtype != TS_Front_Engine) return CMD_ERROR;
+ // check that we can allocate enough vehicles
+ if (!(flags & DC_EXEC)) {
+ int veh_counter = 0;
+ do {
+ veh_counter++;
+ } while ((v = v->next) != NULL);
+
+ {
+ Vehicle **vl = malloc(sizeof(Vehicle*) * veh_counter); // some platforms do not support Vehicle *vl[veh_counter]
+ bool can_allocate_vehicles = AllocateVehicles(vl, veh_counter);
+ free(vl);
+ if (!can_allocate_vehicles) {
+ return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
+ }
+ }
+ }
+
+ v = v_front;
+
do {
cost = DoCommand(x, y, v->engine_type, 3, flags, CMD_BUILD_VEH(v->type));
diff --git a/vehicle.h b/vehicle.h
index 0773d9231..be2b120d4 100644
--- a/vehicle.h
+++ b/vehicle.h
@@ -274,6 +274,7 @@ typedef void *VehicleFromPosProc(Vehicle *v, void *data);
void VehicleServiceInDepot(Vehicle *v);
Vehicle *AllocateVehicle(void);
+bool AllocateVehicles(Vehicle **vl, int num);
Vehicle *ForceAllocateVehicle(void);
Vehicle *ForceAllocateSpecialVehicle(void);
void UpdateVehiclePosHash(Vehicle *v, int x, int y);