(svn r2041) -Fix: not all vehicles did get a day_proc call (because of rounding errors)
authortruelight
Thu, 24 Mar 2005 13:59:44 +0000
changeset 1537 edd70cde5f84
parent 1536 69c91cec07c5
child 1538 c67f0386df1d
(svn r2041) -Fix: not all vehicles did get a day_proc call (because of rounding errors)
misc.c
oldloader.c
saveload.c
vehicle.c
--- a/misc.c	Thu Mar 24 07:48:04 2005 +0000
+++ b/misc.c	Thu Mar 24 13:59:44 2005 +0000
@@ -557,12 +557,41 @@
 	0x001, // every 12 months
 };
 
+/**
+ * Runs the day_proc of 'amount' vehicles.
+ */
+static void RunVehicleDayProc(uint amount)
+{
+	Vehicle *v;
+	VehicleID ctr;
+	uint i;
+
+	ctr = _vehicle_id_ctr_day;
+
+	/* If the CTR is already over the size of the pool, don't even run the for-loop */
+	if (ctr >= GetVehiclePoolSize()) {
+		_vehicle_id_ctr_day += amount;
+		return;
+	}
+
+	for (i = 0; i < amount; i++, ctr++) {
+		/* Skip non-existing vehicles */
+		if (ctr >= GetVehiclePoolSize()) {
+			_vehicle_id_ctr_day += amount;
+			return;
+		}
+
+		v = GetVehicle(ctr);
+		if (v->type != 0)
+			_on_new_vehicle_day_proc[v->type - 0x10](v);
+	}
+
+	_vehicle_id_ctr_day = ctr;
+}
+
 void IncreaseDate(void)
 {
-	const int vehicles_per_day = (1 << (sizeof(_date_fract) * 8)) / 885;
-	uint i;
-	VehicleID ctr;
-	int t;
+	uint32 total_vehicles = (1 << _vehicle_pool.block_size_bits) * _vehicle_pool.max_blocks;
 	YearMonthDay ymd;
 
 	if (_game_mode == GM_MENU) {
@@ -570,35 +599,26 @@
 		return;
 	}
 
-	/*if the day changed, call the vehicle event but only update a part of the vehicles
-		old max was i!= 12. But with that and a bigger number of vehicles (2560), per day only
-		a part of it could be done, namely: function called max_size date_fract (uint16) / 885 x 12 ==>
-		65536 / 885 = 74; 74x12 = 888. So max 888. Any vehicles above that were not _on_new_vehicle_day_proc'd
-		eg. aged.
-		So new code updates it for max vehicles.
-		(_vehicles_size / maximum number of times ctr is incremented before reset ) + 1 (to get last vehicles too)
-		max size of _date_fract / 885 (added each tick) is number of times before ctr is reset.
-		Calculation might look complicated, but compiler just replaces it with 35, so that's ok
-	*/
-
-	ctr = _vehicle_id_ctr_day;
-	for (i = 0; i != ((uint)GetVehiclePoolSize() / vehicles_per_day) + 1 && ctr != GetVehiclePoolSize(); i++) {
-		Vehicle *v = GetVehicle(ctr++);
-		if ((t = v->type) != 0)
-			_on_new_vehicle_day_proc[t - 0x10](v);
-	}
-	_vehicle_id_ctr_day = ctr;
+	RunVehicleDayProc(total_vehicles / DAY_TICKS);
 
 	/* increase day, and check if a new day is there? */
 	_tick_counter++;
 
-	_date_fract += 885;
-	if (_date_fract >= 885)
+	_date_fract++;
+	if (_date_fract < DAY_TICKS)
 		return;
+	_date_fract = 0;
+
+	printf("%d\n", _frame_counter);
 
 	/* yeah, increse day counter and call various daily loops */
 	_date++;
 
+	/* We have a hole because of rounding errors, between the last vehicle checked and the max amount
+	 *  of vehicles.. correct for that problem here */
+	RunVehicleDayProc(total_vehicles - _vehicle_id_ctr_day);
+
+	assert(_vehicle_id_ctr_day == total_vehicles);
 	_vehicle_id_ctr_day = 0;
 
 	TextMessageDailyLoop();
--- a/oldloader.c	Thu Mar 24 07:48:04 2005 +0000
+++ b/oldloader.c	Thu Mar 24 13:59:44 2005 +0000
@@ -423,8 +423,7 @@
 		}
 	}
 
-	if (_vehicle_id_ctr_day > GetVehiclePoolSize())
-		_vehicle_id_ctr_day = 0;
+	_vehicle_id_ctr_day = 0;
 }
 
 /*
--- a/saveload.c	Thu Mar 24 07:48:04 2005 +0000
+++ b/saveload.c	Thu Mar 24 13:59:44 2005 +0000
@@ -8,8 +8,8 @@
 #include "saveload.h"
 
 enum {
-	SAVEGAME_MAJOR_VERSION = 11,
-	SAVEGAME_MINOR_VERSION = 0,
+	SAVEGAME_MAJOR_VERSION = 0xB,
+	SAVEGAME_MINOR_VERSION = 0x1,
 
 	SAVEGAME_LOADABLE_VERSION = (SAVEGAME_MAJOR_VERSION << 8) + SAVEGAME_MINOR_VERSION
 };
--- a/vehicle.c	Thu Mar 24 07:48:04 2005 +0000
+++ b/vehicle.c	Thu Mar 24 13:59:44 2005 +0000
@@ -2093,9 +2093,7 @@
 		}
 	}
 
-	/* This is to ensure all pointers are within the limits of
-	    _vehicles_size */
-	if (_vehicle_id_ctr_day >= GetVehiclePoolSize())
+	if (_sl.full_version < 0x0B01)
 		_vehicle_id_ctr_day = 0;
 }