| 443 | } |
| 444 | |
| 445 | void Server::UpdateTimes() |
| 446 | { |
| 447 | const Time current_time= Time::CurrentTime(); |
| 448 | Time dt= current_time - last_tick_; |
| 449 | |
| 450 | const float dt_s= dt.ToSeconds(); |
| 451 | const float c_min_tick_duration_s= 4.0f / 1000.0f; |
| 452 | const float c_max_tick_duration_s= 30.0f / 1000.0f; |
| 453 | const float c_time_eps= 2.0f / 1000.0f; |
| 454 | |
| 455 | if( dt_s < c_min_tick_duration_s ) |
| 456 | { |
| 457 | // Skip this tick, accumulate more delta. |
| 458 | map_tick_count_= 0u; |
| 459 | return; |
| 460 | } |
| 461 | else if( dt_s > c_max_tick_duration_s ) |
| 462 | { |
| 463 | // Devide long tick into 2 or more subticks. |
| 464 | // We assume, that server runs very fast, but client on same host is very slow. |
| 465 | // Therefore, make multiple fast server ticks per one slow client frame. |
| 466 | |
| 467 | map_tick_count_= static_cast<unsigned int>( std::ceil( dt_s / ( c_max_tick_duration_s - c_time_eps ) ) ); |
| 468 | if( map_tick_count_ > c_max_multiple_map_ticks ) |
| 469 | { |
| 470 | // If tick count is too height, slow down game time. |
| 471 | // It can happens in debugging, for example. |
| 472 | map_tick_count_= c_max_multiple_map_ticks; |
| 473 | |
| 474 | const Time map_tick_dt= Time::FromSeconds( c_max_tick_duration_s ); |
| 475 | for( unsigned int i= 0u; i < map_tick_count_; i++ ) |
| 476 | { |
| 477 | map_ticks_[i].end= server_accumulated_time_ + map_tick_dt * (i+1u); |
| 478 | map_ticks_[i].duration= map_tick_dt; |
| 479 | } |
| 480 | server_accumulated_time_= map_ticks_[ map_tick_count_ - 1u ].end; |
| 481 | } |
| 482 | else |
| 483 | { |
| 484 | PC_ASSERT( map_tick_count_ >= 2u ); |
| 485 | |
| 486 | const Time map_tick_dt= Time::FromSeconds( dt_s / float(map_tick_count_) ); |
| 487 | for( unsigned int i= 0u; i < map_tick_count_ - 1u; i++ ) |
| 488 | { |
| 489 | map_ticks_[i].end= server_accumulated_time_ + map_tick_dt * (i+1u); |
| 490 | map_ticks_[i].duration= map_tick_dt; |
| 491 | } |
| 492 | |
| 493 | map_ticks_[ map_tick_count_ - 1u ].end= server_accumulated_time_ + dt; |
| 494 | map_ticks_[ map_tick_count_ - 1u ].duration= |
| 495 | map_ticks_[ map_tick_count_ - 1u ].end - map_ticks_[ map_tick_count_ - 2u ].end; |
| 496 | |
| 497 | server_accumulated_time_+= dt; |
| 498 | } |
| 499 | } |
| 500 | else |
| 501 | { |
| 502 | // Tick time is in normal range. |