Page 2 of 2

Re: Treefarms and Co-Routines

Posted: Wed Mar 09, 2016 4:38 pm
by roy7
Blu3wolf has started a project to fold in Rseding91's suggestions, info is here:

viewtopic.php?f=44&t=19124&p=133153#p133153

However it isn't working at the moment, there's a bug he doesn't have time to try and locate. It is on github if anyone would like to contribute and help try to bring about a high performance TreeFarm. :) I don't know LUA so I'm kinda useless in that regard.

Re: Treefarms and Co-Routines

Posted: Sat Mar 12, 2016 3:14 am
by Blu3wolf
Rseding91 wrote:As for the planting of trees on farms: store the x/y of the last plant attempt and increment y each attempt. If y > a column reset it to 0 and increment x. If x > then a row reset both to 0 and restart. That way it scans the entire area 1 tile per growth attempt (or how ever many you decide per attempt) and it doesn't need to re-scan the entire area each time.


The above changes when I tested TreeFarm showed a constant 1/10th tick time as before the changes. That means that before it would take 2 MS/tick to run X tree farms and after it would take 0.2 MS/tick to run the same tree farms.
For the second suggestion you made, about how to selectively scan the treefarm for the next location to plant - Does this really improve performance all that much? Compared to scanning the whole treefarm on each attempt - which only happens once every 60 ticks anyway?

Thanks for the suggestions!
roy7 wrote:Blu3wolf has started a project to fold in Rseding91's suggestions, info is here:

viewtopic.php?f=44&t=19124&p=133153#p133153

However it isn't working at the moment, there's a bug he doesn't have time to try and locate. It is on github if anyone would like to contribute and help try to bring about a high performance TreeFarm. :) I don't know LUA so I'm kinda useless in that regard.
The bug in question is fixed, but thanks anyway. With that said I welcome PRs, I am very new to Lua and scripting in general. Cheers!

Re: Treefarms and Co-Routines

Posted: Sat Mar 12, 2016 12:47 pm
by orzelek
Blu3wolf wrote:
Rseding91 wrote:As for the planting of trees on farms: store the x/y of the last plant attempt and increment y each attempt. If y > a column reset it to 0 and increment x. If x > then a row reset both to 0 and restart. That way it scans the entire area 1 tile per growth attempt (or how ever many you decide per attempt) and it doesn't need to re-scan the entire area each time.


The above changes when I tested TreeFarm showed a constant 1/10th tick time as before the changes. That means that before it would take 2 MS/tick to run X tree farms and after it would take 0.2 MS/tick to run the same tree farms.
For the second suggestion you made, about how to selectively scan the treefarm for the next location to plant - Does this really improve performance all that much? Compared to scanning the whole treefarm on each attempt - which only happens once every 60 ticks anyway?
Main reason behind this idea is to not cause game to stutter. You need to spread out the work among game ticks to keep it happening with lower speed but constantly. Scanning 60 locations at once causes "spike" of CPU time usage on mod that might be visible as stuttering.

Re: Treefarms and Co-Routines

Posted: Sat Mar 12, 2016 12:53 pm
by Zeblote
Those hitches will most likely disappear with luajit in 0.13 :D

Re: Treefarms and Co-Routines

Posted: Sat Mar 12, 2016 1:11 pm
by orzelek
Zeblote wrote:Those hitches will most likely disappear with luajit in 0.13 :D
They would be reduced significantly for sure :)

Re: Treefarms and Co-Routines

Posted: Sat Mar 12, 2016 4:25 pm
by Fatmice
Who told you that? luajit hasn't been updated to lua 5.3 and the difference between 5.1 and 5.3 is quite a lot. Luajit 2.1-beta1 is compatible with lua 5.1 code only.

Re: Treefarms and Co-Routines

Posted: Sat Mar 12, 2016 4:28 pm
by orzelek
Fatmice wrote:Who told you that? luajit hasn't been updated to 5.3 and the difference between 5.1 and 5.3 is quite a lot. Luajit 2.1-beta1 is compatible with lua 5.1 code only.
It's about the premise of luajit. Not sure what version difference means in practical use here.
With jit lua is no longer interpreted but compiled - should net significant speed increase. That would mean that any spikes are significantly shorter = less stuttering.

Re: Treefarms and Co-Routines

Posted: Sat Mar 12, 2016 4:32 pm
by Zeblote
Fatmice wrote:Who told you that? luajit hasn't been updated to lua 5.3 and the difference between 5.1 and 5.3 is quite a lot. Luajit 2.1-beta1 is compatible with lua 5.1 code only.
Factorio doesn't use 5.3 anyways, and the difference between 5.1 and 5.2 is not that much..

Would prefer performance over new features.

Re: Treefarms and Co-Routines

Posted: Sat Mar 12, 2016 4:38 pm
by Fatmice
That's not what I meant. =) Who told you that luajit will be replacing the lua interpreter? I thought the current lua interpreter is lua 5.3 with os module removed. Code written currently might break with luajit in its place since 2.1-beta1 is compatible with lua 5.1 and not lua 5.3. There are many differences between the subversion of lua.

Regarding spikes, they are only stuttering if the execution takes longer than 1/60th of a second, in which case, optimization on the code can be done right now to remove/reduce that even without luajit.

Re: Treefarms and Co-Routines

Posted: Sat Mar 12, 2016 5:00 pm
by Zeblote
Well, /c game.local_player.print(_VERSIONI) says 5.2 so that's where I got that it doesn't use 5.3.

For luajit, look at the to do list here: https://www.factorio.com/blog/post/fff-111

Re: Treefarms and Co-Routines

Posted: Sat Mar 12, 2016 5:08 pm
by Fatmice
Ah I see. I missed that bit about luajit from my recollection of that FF. Looking at it again, it was written "Lua jit" and not "luajit". :lol: My brain does this thing where joined words are associated differently than non joined words...

Re: Treefarms and Co-Routines

Posted: Mon Mar 14, 2016 3:33 am
by Blu3wolf
Rseding91 wrote:As for the planting of trees on farms: store the x/y of the last plant attempt and increment y each attempt. If y > a column reset it to 0 and increment x. If x > then a row reset both to 0 and restart. That way it scans the entire area 1 tile per growth attempt (or how ever many you decide per attempt) and it doesn't need to re-scan the entire area each time.
I can appreciate it being the case that the CPU gets extra load once every 60 frames that way. How many treefarms do you need before that becomes a problem, though? I can see that spreading it out would prevent it from becoming a problem so quickly, at least. By a factor of 60 I guess.

EDIT: Doesnt treefarm already store the x/y of the last plant attempt and iterate over x then y with each attempt, stopping when it finds a place it can plant?

I thought that was the point of this:

Code: Select all

  local seedPos = false
  if seedInInv.name ~= "DUMMY" then
    local fieldPos = fieldObj.entity.position
    local placed = false
    local lastPos = fieldObj.lastSeedPos

    for dx = lastPos.x, 8 do
      for dy = 0, 6 do
        if (game.get_surface("nauvis").can_place_entity{name = "tf-germling", position = {fieldPos.x + dx - 0.5, fieldPos.y + dy - 0.5}}) then
          seedPos = {x = fieldPos.x + dx - 0.5, y = fieldPos.y + dy - 0.5}
          placed = true
          fieldObj.lastSeedPos = {x = dx, y = dy}
          break
        end
      end
      if placed == true then
        break
      end
    end

    if (placed == false) and (lastPos.x ~= 2) then
      for dx = 2, lastPos.x - 1 do
        for dy = 0, 6 do
          if (game.get_surface("nauvis").can_place_entity{name = "tf-germling", position = {fieldPos.x + dx - 0.5, fieldPos.y + dy - 0.5}}) then
            seedPos = {x = fieldPos.x + dx - 0.5, y = fieldPos.y + dy - 0.5}
            placed = true
            fieldObj.lastSeedPos = {x = dx, y = dy}
            break
          end
        end
        if placed == true then
          break
        end
      end
    end
After that, if seedPos has a value it creates a seed there, then does harvesting, then removes the current (first) treefarm from the table of treefarms, and reinserts it at the end. If you had a couple hundred treefarms, I could see that starting to slow things down on the update tick - causing stuttering I guess. Would a valid performance improvement come from the same method used for the plants growing? Putting treefarms into a table, that is itself a table value which is indexed by the update tick in another table? A table of tables, with the small table being treefarms to update on this tick, not indexed, and the large table being small tables indexed by the tick number to update them on?

Re: Treefarms and Co-Routines

Posted: Fri Mar 25, 2016 1:15 pm
by Wade
I really dont get it. Why do you do the treefarm so complicated??

Just make ist a static field with some small trees and some big trees, wich functions as a normal factory. This factory needs 1 seed every x seconds and produces 5 peaces of wood in the same time. No need for scanning the field, planting, growing, harvesting etc. This is NOT a high res simulation, it is a tactical game, no need for the highest grafics/animations...

Re: Treefarms and Co-Routines

Posted: Fri Mar 25, 2016 3:38 pm
by Blu3wolf
Yeah, thats how the greenhouses mod works: an assembly machine with seeds as input and wood as output.

Treefarm is more visually appealing to some folks. I guess people like watching trees grow. Or at least, being able to see it happen as they walk past. It helps that its not that complicated.