1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-11-23 22:22:34 +02:00
Files
ComfyFactorio/modules/trees_grow.lua

143 lines
4.1 KiB
Lua
Raw Normal View History

2019-08-12 14:34:16 +02:00
-- trees multiply -- mewmew
2021-03-24 20:14:55 +01:00
local Event = require 'utils.event'
2019-08-12 14:34:16 +02:00
local math_random = math.random
local Difficulty = require 'modules.difficulty_vote'
2019-08-12 14:34:16 +02:00
local vectors = {}
local r = 8
for x = r * -1, r, 0.25 do
2020-05-16 12:37:05 +02:00
for y = r * -1, r, 0.25 do
if math.sqrt(x ^ 2 + y ^ 2) <= r then
vectors[#vectors + 1] = { x, y }
2020-05-16 12:37:05 +02:00
end
end
2019-08-12 14:34:16 +02:00
end
2019-08-14 06:24:53 +02:00
local resistant_tiles = {
2020-05-16 12:37:05 +02:00
['concrete'] = 8,
['hazard-concrete-left'] = 8,
['hazard-concrete-right'] = 8,
['refined-concrete'] = 24,
['refined-hazard-concrete-left'] = 24,
['refined-hazard-concrete-right'] = 24,
['stone-path'] = 4
2019-08-12 14:34:16 +02:00
}
2019-08-13 10:33:14 +02:00
local blacklist = {
2020-05-16 12:37:05 +02:00
['dead-grey-trunk'] = true
2019-08-13 10:33:14 +02:00
}
local function coord_string(x, y)
2021-03-24 20:14:55 +01:00
local str = tostring(x) .. '_'
2020-05-16 12:37:05 +02:00
str = str .. tostring(y)
return str
2019-08-12 14:34:16 +02:00
end
2021-03-24 20:14:55 +01:00
local function get_chunk()
if #storage.trees_grow_chunk_raffle == 0 then
2020-05-16 12:37:05 +02:00
return false
end
local p = storage.trees_grow_chunk_position[storage.trees_grow_chunk_raffle[math_random(1, #storage.trees_grow_chunk_raffle)]]
2020-05-16 12:37:05 +02:00
local str = coord_string(p.x, p.y)
if not storage.trees_grow_chunk_next_visit[str] then
2020-05-16 12:37:05 +02:00
return p
end
if storage.trees_grow_chunk_next_visit[str] < game.tick then
2020-05-16 12:37:05 +02:00
return p
end
return false
2019-08-13 10:33:14 +02:00
end
2019-08-14 06:24:53 +02:00
local function get_trees(surface)
2020-05-16 12:37:05 +02:00
local p = get_chunk(surface)
if not p then
return false
end
local trees = surface.find_entities_filtered({ type = 'tree', area = { { p.x * 32, p.y * 32 }, { p.x * 32 + 32, p.y * 32 + 32 } } })
2020-05-16 12:37:05 +02:00
local a = 750
2021-03-24 16:46:00 +01:00
local Diff = Difficulty.get()
2020-05-16 12:37:05 +02:00
if Diff.difficulty_vote_value then
a = a / Diff.difficulty_vote_value
end
storage.trees_grow_chunk_next_visit[coord_string(p.x, p.y)] = math.floor(game.tick + math.floor(a + (#trees * 5)))
2020-05-16 12:37:05 +02:00
if not trees[1] then
return false
end
return trees
2019-08-13 10:33:14 +02:00
end
2019-08-14 06:24:53 +02:00
local function grow_trees(surface)
2020-05-16 12:37:05 +02:00
local Diff = Difficulty.get()
local trees = get_trees(surface)
if not trees then
return false
end
local m = 2
if Diff.difficulty_vote_index then
m = Diff.difficulty_vote_index
end
2021-03-24 20:14:55 +01:00
for _ = 1, math_random(m, math.ceil(m * 1.5)), 1 do
2020-05-16 12:37:05 +02:00
local tree = trees[math_random(1, #trees)]
if not blacklist[tree.name] then
local vector = vectors[math_random(1, #vectors)]
local p = surface.find_non_colliding_position('car', { tree.position.x + vector[1], tree.position.y + vector[2] }, 8, 4)
2020-05-16 12:37:05 +02:00
if p then
local tile = surface.get_tile(p)
if resistant_tiles[tile.name] then
if math_random(1, resistant_tiles[tile.name]) == 1 then
surface.set_tiles({ { name = tile.hidden_tile, position = p } })
surface.create_entity({ name = tree.name, position = p, force = tree.force.name })
2020-05-16 12:37:05 +02:00
end
else
surface.create_entity({ name = tree.name, position = p, force = tree.force.name })
2020-05-16 12:37:05 +02:00
end
end
end
end
return true
2019-08-12 14:34:16 +02:00
end
2019-08-14 06:24:53 +02:00
local function on_chunk_charted(event)
2020-05-16 12:37:05 +02:00
local position = event.position
local str = coord_string(position.x, position.y)
if storage.trees_grow_chunks_charted[str] then
2020-05-16 12:37:05 +02:00
return
end
storage.trees_grow_chunks_charted[str] = true
storage.trees_grow_chunks_charted_counter = storage.trees_grow_chunks_charted_counter + 1
2020-05-16 12:37:05 +02:00
storage.trees_grow_chunk_raffle[#storage.trees_grow_chunk_raffle + 1] = str
storage.trees_grow_chunk_position[str] = { x = position.x, y = position.y }
2019-08-13 10:33:14 +02:00
end
2021-03-24 20:14:55 +01:00
local function tick()
2020-05-16 12:37:05 +02:00
if not game.connected_players[1] then
return
end
local surface = game.connected_players[1].surface
2021-03-24 20:14:55 +01:00
for _ = 1, 32, 1 do
2020-05-16 12:37:05 +02:00
if grow_trees(surface) then
break
end
end
2019-08-12 14:34:16 +02:00
end
2021-03-24 20:14:55 +01:00
local function on_init()
storage.trees_grow_chunk_next_visit = {}
storage.trees_grow_chunk_raffle = {}
storage.trees_grow_chunk_position = {}
2019-08-14 16:16:42 +02:00
storage.trees_grow_chunks_charted = {}
storage.trees_grow_chunks_charted_counter = 0
2019-08-13 10:33:14 +02:00
end
2021-03-24 20:14:55 +01:00
Event.on_init(on_init)
Event.on_nth_tick(1, tick)
Event.add(defines.events.on_chunk_charted, on_chunk_charted)