misc.c
changeset 1537 517b9b9cfaa0
parent 1529 3a8196d15b01
child 1538 e1e80f10c029
equal deleted inserted replaced
1536:16c958d864b5 1537:517b9b9cfaa0
   555 	0x249, // every 3 months
   555 	0x249, // every 3 months
   556 	0x041, // every 6 months
   556 	0x041, // every 6 months
   557 	0x001, // every 12 months
   557 	0x001, // every 12 months
   558 };
   558 };
   559 
   559 
       
   560 /**
       
   561  * Runs the day_proc of 'amount' vehicles.
       
   562  */
       
   563 static void RunVehicleDayProc(uint amount)
       
   564 {
       
   565 	Vehicle *v;
       
   566 	VehicleID ctr;
       
   567 	uint i;
       
   568 
       
   569 	ctr = _vehicle_id_ctr_day;
       
   570 
       
   571 	/* If the CTR is already over the size of the pool, don't even run the for-loop */
       
   572 	if (ctr >= GetVehiclePoolSize()) {
       
   573 		_vehicle_id_ctr_day += amount;
       
   574 		return;
       
   575 	}
       
   576 
       
   577 	for (i = 0; i < amount; i++, ctr++) {
       
   578 		/* Skip non-existing vehicles */
       
   579 		if (ctr >= GetVehiclePoolSize()) {
       
   580 			_vehicle_id_ctr_day += amount;
       
   581 			return;
       
   582 		}
       
   583 
       
   584 		v = GetVehicle(ctr);
       
   585 		if (v->type != 0)
       
   586 			_on_new_vehicle_day_proc[v->type - 0x10](v);
       
   587 	}
       
   588 
       
   589 	_vehicle_id_ctr_day = ctr;
       
   590 }
       
   591 
   560 void IncreaseDate(void)
   592 void IncreaseDate(void)
   561 {
   593 {
   562 	const int vehicles_per_day = (1 << (sizeof(_date_fract) * 8)) / 885;
   594 	uint32 total_vehicles = (1 << _vehicle_pool.block_size_bits) * _vehicle_pool.max_blocks;
   563 	uint i;
       
   564 	VehicleID ctr;
       
   565 	int t;
       
   566 	YearMonthDay ymd;
   595 	YearMonthDay ymd;
   567 
   596 
   568 	if (_game_mode == GM_MENU) {
   597 	if (_game_mode == GM_MENU) {
   569 		_tick_counter++;
   598 		_tick_counter++;
   570 		return;
   599 		return;
   571 	}
   600 	}
   572 
   601 
   573 	/*if the day changed, call the vehicle event but only update a part of the vehicles
   602 	RunVehicleDayProc(total_vehicles / DAY_TICKS);
   574 		old max was i!= 12. But with that and a bigger number of vehicles (2560), per day only
       
   575 		a part of it could be done, namely: function called max_size date_fract (uint16) / 885 x 12 ==>
       
   576 		65536 / 885 = 74; 74x12 = 888. So max 888. Any vehicles above that were not _on_new_vehicle_day_proc'd
       
   577 		eg. aged.
       
   578 		So new code updates it for max vehicles.
       
   579 		(_vehicles_size / maximum number of times ctr is incremented before reset ) + 1 (to get last vehicles too)
       
   580 		max size of _date_fract / 885 (added each tick) is number of times before ctr is reset.
       
   581 		Calculation might look complicated, but compiler just replaces it with 35, so that's ok
       
   582 	*/
       
   583 
       
   584 	ctr = _vehicle_id_ctr_day;
       
   585 	for (i = 0; i != ((uint)GetVehiclePoolSize() / vehicles_per_day) + 1 && ctr != GetVehiclePoolSize(); i++) {
       
   586 		Vehicle *v = GetVehicle(ctr++);
       
   587 		if ((t = v->type) != 0)
       
   588 			_on_new_vehicle_day_proc[t - 0x10](v);
       
   589 	}
       
   590 	_vehicle_id_ctr_day = ctr;
       
   591 
   603 
   592 	/* increase day, and check if a new day is there? */
   604 	/* increase day, and check if a new day is there? */
   593 	_tick_counter++;
   605 	_tick_counter++;
   594 
   606 
   595 	_date_fract += 885;
   607 	_date_fract++;
   596 	if (_date_fract >= 885)
   608 	if (_date_fract < DAY_TICKS)
   597 		return;
   609 		return;
       
   610 	_date_fract = 0;
       
   611 
       
   612 	printf("%d\n", _frame_counter);
   598 
   613 
   599 	/* yeah, increse day counter and call various daily loops */
   614 	/* yeah, increse day counter and call various daily loops */
   600 	_date++;
   615 	_date++;
   601 
   616 
       
   617 	/* We have a hole because of rounding errors, between the last vehicle checked and the max amount
       
   618 	 *  of vehicles.. correct for that problem here */
       
   619 	RunVehicleDayProc(total_vehicles - _vehicle_id_ctr_day);
       
   620 
       
   621 	assert(_vehicle_id_ctr_day == total_vehicles);
   602 	_vehicle_id_ctr_day = 0;
   622 	_vehicle_id_ctr_day = 0;
   603 
   623 
   604 	TextMessageDailyLoop();
   624 	TextMessageDailyLoop();
   605 
   625 
   606 	DisasterDailyLoop();
   626 	DisasterDailyLoop();