I've created a "Seed Bomb". It's a rocked that plants trees when fired.
The "Basic" version works fine, but then I tried to add a Advanced version that also changes the terrain. (Fertilizes it, before planting the tree.)
This version seems to suck up a lot of CPU power and I was hoping someone could help me optimize the code.
I use the "on_trigger_created_entity" for the Basic ammo, but had to do something different for the Advanced, so I placed a dummy entity that triggered and swamped it out for the same entity as in the Basic version, since I did not know how else to tell the difference between the basic and advanced. I think if there is a way, it would improve performance a lot already.
It's hard to explain, best to load the mod and sheet the Seed Bomb using the rocket launcher.
Pic
I don't think that the trees are actually growing with the Advanced version
---------------------------------------------
script.on_event(defines.events.on_trigger_created_entity, function(event)
--- Used for Seed-bomb
local ent=event.entity
local surface = ent.surface
local position = ent.position
local force = ent.force
local New_tiles = {}
if global.seed_bomb[ent.name] then
writeDebug("Seed Bomb Activated")
seed_planted_trigger (event)
end
if global.seed_bomb[ent.name] == "seedling-2" then -- dummy entity
if game.active_mods["alien-biomes"] then
table.insert(New_tiles, {name="vegetation-green-grass-3", position=position})
else
table.insert(New_tiles, {name="grass-3", position=position})
end
surface.set_tiles(New_tiles)
ent.destroy()
local create_seedling = surface.create_entity({name = "seedling", position = position, force = force})
-- Could not use function "seed_planted_trigger (event)" since the entity was destroyed.
local fertility = 85
local max_grow_time = math.random(5000) + 600 --< Fertile tiles will grow faster than barren tiles
table.insert(global.bi.tree_growing, {position = position, time = event.tick + max_grow_time, surface = surface})
table.sort(global.bi.tree_growing, function(a, b) return a.time < b.time end)
end
end)
Re: Code help needed
Posted: Tue Jan 23, 2018 9:22 pm
by darkfrei
It seems that you need to make part of this code by every tick, but not whole code in one tick.
Re: Code help needed
Posted: Tue Jan 23, 2018 9:22 pm
by Bilka
Disclaimer: I only skimmed over your code.
I think you might be experiencing slowdowns due to
---- Growing Tree
Event.register(defines.events.on_tick, function(event)
while #global.bi.tree_growing > 0 do
if event.tick < global.bi.tree_growing[1].time then
break
end
Grow_tree(global.bi.tree_growing[1].position, global.bi.tree_growing[1].surface)
table.remove(global.bi.tree_growing, 1)
end
end)
Again, the normal seed bomb works okay. it's when I need to both switch out the terrain and my dummy seed that things go very slow...
Re: Code help needed
Posted: Thu Jan 25, 2018 2:00 am
by eradicator
Bilka wrote:
Not only are you accessing global. , which is relatively slow, you are also doing table.insert and table.sort, both of which are rather slow.
Accessing global during runtime should™ not be any slower than accessing any other table. And table.insert() most certainly is not a slow operation. At least not slow in the sense that you'd notice it ingame when used less than a few thousand times. It sounds more like OP is creating lots of tiles but calls surface.set_tiles() once for each tile instead of calling it once for all tiles. The API doc specifically states that it is recommended to call it only once. Though the reason it states is tile correction logic, which was removed in 0.16 so the effect might not be as bad now, it is probably still a significant performance drain and would fit the described lag pattern of "only when using the rocket". Also it would be better to store the result of <if game.active_mods["alien-biomes"]> than to call it every time as it's not going to change. Other than that....try using log() to determine which parts of the code are actually slow. Log prints a timestamp to the console/log when uesd.
Re: Code help needed
Posted: Thu Jan 25, 2018 2:31 am
by TheSAguy
My latest version is not to bad. I created duplicate seed entities that I don't need to switch out.
Attached.