News:

Do you need help?
Simutrans Wiki Manual can help you to play and extend Simutrans. In 9 languages.

Query about factory production code

Started by jamespetts, September 10, 2013, 12:05:31 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

jamespetts

Sorry to take people's time: I am wondering whether any of the Standard developers might assist me with a little query. In simfab.h, there is a line:


sint32 get_current_production() const { return ((sint64)prodbase * (sint64)(get_prodfactor()))>>(26l-(long)welt->ticks_per_world_month_shift); }


This is unusual, as, in most other places, welt->ticks_per_world_month_shift is treated somewhat differently, involving the number 18 and an if/else statement to check whether the number is greater than 18. Here, the code (for scaling factory production by bits per month) uses the number 26, and there is no if/else statement. May I ask what the intention behind this code was? Is this just another way of doing the equivalent of:


const uint32 bits_per_month = welt->ticks_per_world_month_shift;
    if(  bits_per_month>18  ) {
        scaled_pax_demand = pax_demand << (bits_per_month - 18);
    }
    else if(  bits_per_month<18  ) {
        scaled_pax_demand = pax_demand >> (18 - bits_per_month);
        if(  scaled_pax_demand==0  &&  besch_pax_demand>0  ) {
            scaled_pax_demand = 1;    // since besch pax demand > 0 -> ensure no less than 1
        }
    }
    else {
        scaled_pax_demand = pax_demand;
    }


(this is from a different context, but the principle remains the same), or is it intended that this code do something substantively different?

I ask because I am trying to standardise all time scaling in Simutrans-Experimental to take into account meters per tile as well as bits per month (currently and anomalously, factory production, electricity production and some other things do not take into account meters per tile), but it is not immediately obvious what to do with this code.

Any insight would be greatly appreciated!
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.

isidoro

Maybe a different mathematical operation is intended...  The if-else construction makes sense since shift operators in C++ don't admit negative shifts.  A negative shift to the left must be translated to the equivalent positive shift to the right.

I think that the mathematical operation intended in the second example is: scaled_pax_demmand=pax_demmand*2bits_per_month-18 = scaled_pax_demmand*2bits_per_month/218.


prissi

The old code had a multiplication by 2^8=256. So to get the same number after introducing variable month, this was included in the shift. I suspect that shifts smaller than 18 will not work well any more. It was tested once down to 16, but many things changed since then.