Dynamic Trees - Need help
Posted: Sun Jul 10, 2016 10:28 pm
Hi,
Let me first explain what I'm trying to do and then show my code, that not currently working as I'd like.
I'm trying to create a dynamic environment.
I want to find all trees, (only where the area has been revealed).
Each tree found, should have a chance to reproduce, or die. Then remove some dead trees to make space for new trees.
Reproduction should be slightly higher than killing, so forests can grow.
So, after the trees are found,
Then have small % chance to add a new tree. small chance to kill a tree and a small change to remove a dead tree.
I don't know how to create a new position that's just randomly 0.5 from the existing position.
So my current code below does not create any new trees.
This is the current code:
Let me first explain what I'm trying to do and then show my code, that not currently working as I'd like.
I'm trying to create a dynamic environment.
I want to find all trees, (only where the area has been revealed).
Each tree found, should have a chance to reproduce, or die. Then remove some dead trees to make space for new trees.
Reproduction should be slightly higher than killing, so forests can grow.
So, after the trees are found,
Then have small % chance to add a new tree. small chance to kill a tree and a small change to remove a dead tree.
I don't know how to create a new position that's just randomly 0.5 from the existing position.
So my current code below does not create any new trees.
This is the current code:
Code: Select all
---Bio Domes - v.0.0.1
require ("util")
script.on_event(defines.events.on_tick, function(event)
local surface = game.surfaces['nauvis']
local my_random = math.random(100)
if game.tick % 60 == 0 and my_random > 80 then
for chunk in surface.get_chunks() do
local entities = surface.find_entities_filtered{type="tree", area={{chunk.x*32, chunk.y*32}, {(chunk.x+1)*32, (chunk.y+1)*32}}}
for _, entity in pairs(entities) do
if entity.name == "dead-tree" then
--- Remove some dead trees
writeDebug("Dead Tree")
local my_random2 = math.random(100)
if my_random2 > 95 then
entity.destroy()
end
else
-- Plant a new tree
local position = entity.position
local new_position = position -- Can't get this to work, since I tried local new_position = {x=position + 0.5, y=position + 0.5}
--surface.create_entity({ name="tree-01", amount=1, position=new_position})
writeDebug("Spread Seedling " .. my_random)
--- Kill a tree
local my_random3 = math.random(100)
if my_random3 > 95 then
entity.destroy()
writeDebug("Kill Tree " .. my_random3)
surface.create_entity({ name="dead-tree", amount=1, position=position})
end
end
end
end
end
end)
--- DeBug Messages
function writeDebug(message)
for i, player in pairs(game.players) do
player.print(tostring(message))
end
end