[nfo] simple maths coding challenge

Discussions about the technical aspects of graphics development, including NewGRF tools and utilities.

Moderator: Graphics Moderators

Post Reply
User avatar
andythenorth
Tycoon
Tycoon
Posts: 5658
Joined: 31 Mar 2007 14:23
Location: Lost in Music

[nfo] simple maths coding challenge

Post by andythenorth »

Writing good processing industry production code using the production callback presents various challenges. I am quite out of my depth.

Example Case
One Output Cargo
Three Input Cargos (will produce if any one or more cargos is waiting)
1 unit input = 1 unit output
Max possible production per cycle* = FF FF
Process each cargo in equal amounts. Process up to FF FF units of cargo in total per cycle.

Therefore how much of each cargo to process per cycle?
- if one cargo waiting, process up to FF FF of that cargo
- if two cargos waiting, process up to FF FF / 2 of each cargo
- if three cargos waiting, process up to FF FF / 3 of each cargo
Weird result: if two cargos waiting (for example), but second cargo amount is < FF FF / 2, total production is lower than if just one cargo is waiting. Counter-intuitive.

So I have two problems here. (1) figuring out what I should code, then (2) figuring out how to actually code it.

I do have good code for primary industries, but I really would appreciate any help with this.

* This might be easier if I stopped trying to support insane production levels, but there seem to be many players who like to deliver all the cargo on the map to one factory (for example). I could stop supporting this style of play, but the fundamental production ratio problem would still exist.

Help?!?! :o
User avatar
PikkaBird
Graphics Moderator
Graphics Moderator
Posts: 5602
Joined: 13 Sep 2004 13:21
Location: The Moon

Re: [nfo] simple maths coding challenge

Post by PikkaBird »

Okay, you want to produce up to FF FF each cycle. I'd do it like this.

1) work out what the maximum possible production is (multiply each waiting input cargo by the ratio it produces an output cargo and add them together).
2) if that number < FFFF, zero all waiting cargos and produce that number.

3) if that number > FFFF, we need to work out how much of each input cargo we can process. Check each cargo to see if it can produce FFFF/3; if they all can, produce FFFF and subtract the appropriate amount from the waiting cargos.

Otherwise;
4) For each cargo in turn, check if the amount waiting is enough to produce FFFF/3 output; if it is, add FFFF/3 to the output register and add the appropriate amount to the input register. if it's not, add the appropriate amount to the output register and the total amount to the input register.

5) check if the cargo with the highest (amount waiting - input register) has enough additional cargo waiting to make it up the shortfall. Add the appropriate amounts to the registers. If there's still a shortfall, go to the second cargo and do the same.
6) produce FFFF and subtract the input registers from the input cargos.

Make sense? :) Incidentally, it'll never get past step 2 unless more than 65535 units were delivered since the last cycle; that's pretty insane. ;) You might want to use a lower maximum production if you want your code to ever be used. :) Maximum consumption per cycle for the PBI factory is 193 (47+80+66), and it takes quite a lot to overload even that.
User avatar
andythenorth
Tycoon
Tycoon
Posts: 5658
Joined: 31 Mar 2007 14:23
Location: Lost in Music

Re: [nfo] simple maths coding challenge

Post by andythenorth »

Thanks! :)
PikkaBird wrote:Incidentally, it'll never get past step 2 unless more than 65535 units were delivered since the last cycle; that's pretty insane. ;)
So taking the point above, I'm going to forget the idea of trying to guarantee FF FF production if enough input cargo is present.

Simpler idea (assume three input cargos, one output, 2k units waiting per cargo):

Code: Select all

max prod per cargo = FF FF / 3     (~21k)
max consumption per cargo = FF FF / 3	       (~21k)
output ratio = 1:1                         (1)
consumption ratio per cargo = waiting amount / max consumption     (2k / 21k = ~0.095)
(then if consumption ratio > 1, set consumption ratio = 1)
production per cargo = consumption ratio * max prod per cargo * output_ratio   (~2k)
total output = sum(production per cargo:1..3)    (~6k)
This will be good for industries where the output ratio is 2:1 or 4:3 or whatever.

Think it will also work for two output cargos, but I need to think about how those are balanced...even split, or produce more of the one that is transported more?

For players who simply *must* deliver insane amounts of cargo and expect insane output, they can write their own code for it :shock: I'd rather have the FIRS grf turn off maglev instead :twisted:
Post Reply

Return to “NewGRF Technical Discussions”

Who is online

Users browsing this forum: No registered users and 19 guests