News:

Simutrans Sites
Know our official sites. Find tools and resources for Simutrans.

Average speed logic error?

Started by inkelyad, October 23, 2010, 04:38:43 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

inkelyad

Suppose we have just arrived convoy.

convoy.state = LOADING

somewhere.
Then laden() is called:

case LOADING:
           laden();

Here

void convoi_t::laden() //"load" (Babelfish)
{
   //Calculate average speed
   //@author: jamespetts
   const uint32 journey_distance = accurate_distance(fahr[0]->get_pos().get_2d(), fahr[0]->last_stop_pos);

   const double journey_time = (welt->get_zeit_ms() - last_departure_time) / 4096.0F;
   const uint16 average_speed = ((double)journey_distance / journey_time) * 20.0;
   book(average_speed, CONVOI_AVERAGE_SPEED);
   last_departure_time = welt->get_zeit_ms();

    // Recalculate comfort
    book(get_comfort(), CONVOI_COMFORT);

    for(uint8 i = 0; i < anz_vehikel; i++)
    {
        // Accumulate distance
        slist_iterator_tpl<ware_t> iter_cargo(fahr[i]->get_fracht());
        while(iter_cargo.next())
        {
            iter_cargo.access_current().add_distance(journey_distance);
        }
    }


All good.
But. When loading_level < loading_level => convoy.state (==LOADING) is not changed at exit. And all this will be called again.
So journey_time will be small (it just next cycle!), and average_speed will be very big. And it will be booked.

Maybe we need another state for this? (ARRIVED).
Edit: journey_distance for cargo will be added each time too.

jamespetts

Thank you very much for reporting this. Before I make big changes to try to fix it - can you reproduce the actual problem in practice?
Download Simutrans-Extended.

Want to help with development? See here for things to do for coding, and here for information on how to make graphics/objects.

Follow Simutrans-Extended on Facebook.

inkelyad

(Sign) No need to fix anything. There is v->last_stop_pos = v->get_pos().get_2d(); deep inside convoi_t::hat_gehalten. So journey_distance != 0 only first time.