misc.c
changeset 919 544f374ee392
parent 915 d845fe7cf6f2
child 926 a6d140a6a4de
equal deleted inserted replaced
918:c092add72215 919:544f374ee392
   605 	0x001, // every 12 months
   605 	0x001, // every 12 months
   606 };
   606 };
   607 
   607 
   608 void IncreaseDate()
   608 void IncreaseDate()
   609 {
   609 {
       
   610 	const int vehicles_per_day = (1 << (sizeof(_date_fract) * 8)) / 885;
   610 	int i,ctr,t;
   611 	int i,ctr,t;
   611 	YearMonthDay ymd;
   612 	YearMonthDay ymd;
   612 
   613 
   613 	if (_game_mode == GM_MENU) {
   614 	if (_game_mode == GM_MENU) {
   614 		_tick_counter++;
   615 		_tick_counter++;
   619 		old max was i!= 12. But with that and a bigger number of vehicles (2560), per day only
   620 		old max was i!= 12. But with that and a bigger number of vehicles (2560), per day only
   620 		a part of it could be done, namely: function called max_size date_fract (uint16) / 885 x 12 ==>
   621 		a part of it could be done, namely: function called max_size date_fract (uint16) / 885 x 12 ==>
   621 		65536 / 885 = 74; 74x12 = 888. So max 888. Any vehicles above that were not _on_new_vehicle_day_proc'd
   622 		65536 / 885 = 74; 74x12 = 888. So max 888. Any vehicles above that were not _on_new_vehicle_day_proc'd
   622 		eg. aged.
   623 		eg. aged.
   623 		So new code updates it for max vehicles.
   624 		So new code updates it for max vehicles.
   624 		(NUM_VEHICLES / maximum number of times ctr is incremented before reset ) + 1 (to get last vehicles too)
   625 		(_vehicles_size / maximum number of times ctr is incremented before reset ) + 1 (to get last vehicles too)
   625 		max size of _date_fract / 885 (added each tick) is number of times before ctr is reset.
   626 		max size of _date_fract / 885 (added each tick) is number of times before ctr is reset.
   626 		Calculation might look complicated, but compiler just replaces it with 35, so that's ok
   627 		Calculation might look complicated, but compiler just replaces it with 35, so that's ok
   627 	*/
   628 	*/
   628 
   629 
   629 	ctr = _vehicle_id_ctr_day;
   630 	ctr = _vehicle_id_ctr_day;
   630 	for(i=0; i!=(NUM_VEHICLES / ((1<<sizeof(_date_fract)*8) / 885)) + 1 && ctr != lengthof(_vehicles); i++) {
   631 	for (i = 0; i != (_vehicles_size / vehicles_per_day) + 1 && ctr != _vehicles_size; i++) {
   631 		Vehicle *v = &_vehicles[ctr++];
   632 		Vehicle *v = GetVehicle(ctr++);
   632 		if ((t=v->type) != 0)
   633 		if ((t = v->type) != 0)
   633 			_on_new_vehicle_day_proc[t - 0x10](v);
   634 			_on_new_vehicle_day_proc[t - 0x10](v);
   634 	}
   635 	}
   635 	_vehicle_id_ctr_day = ctr;
   636 	_vehicle_id_ctr_day = ctr;
   636 
   637 
   637 	/* increase day, and check if a new day is there? */
   638 	/* increase day, and check if a new day is there? */
   638 	_tick_counter++;
   639 	_tick_counter++;
   639 
   640 
   640 	if ( (_date_fract += 885) >= 885)
   641 	_date_fract += 885;
       
   642 	if (_date_fract >= 885)
   641 		return;
   643 		return;
   642 
   644 
   643 	/* yeah, increse day counter and call various daily loops */
   645 	/* yeah, increse day counter and call various daily loops */
   644 	_date++;
   646 	_date++;
   645 
   647