1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-22 03:38:48 +02:00
ComfyFactorio/maps/modules/rocks_broken_paint_tiles.lua

71 lines
2.3 KiB
Lua
Raw Normal View History

2019-02-10 05:47:52 +01:00
--Mining or breaking a rock paints the tiles underneath
local event = require 'utils.event'
local math_random = math.random
local insert = table.insert
local valid_entities = {
["rock-big"] = true,
["rock-huge"] = true,
["sand-rock-big"] = true
}
2019-02-14 15:44:12 +01:00
local replacement_tiles = {
["dirt-7"] = "dirt-6",
["dirt-6"] = "dirt-5",
["dirt-5"] = "dirt-4",
["dirt-4"] = "dirt-3",
["dirt-3"] = "dirt-2"
}
2019-02-10 09:56:23 +01:00
local function get_chunk_position(position)
local chunk_position = {}
position.x = math.floor(position.x, 0)
position.y = math.floor(position.y, 0)
for x = 0, 31, 1 do
if (position.x - x) % 32 == 0 then chunk_position.x = (position.x - x) / 32 end
end
for y = 0, 31, 1 do
if (position.y - y) % 32 == 0 then chunk_position.y = (position.y - y) / 32 end
end
return chunk_position
end
local function regenerate_decoratives(surface, position)
local chunk = get_chunk_position(position)
surface.destroy_decoratives({{chunk.x * 32, chunk.y * 32}, {chunk.x * 32 + 32, chunk.y * 32 + 32}})
local decorative_names = {}
for k,v in pairs(game.decorative_prototypes) do
if v.autoplace_specification then
decorative_names[#decorative_names+1] = k
end
end
surface.regenerate_decorative(decorative_names, {chunk})
end
2019-02-14 15:44:12 +01:00
local coords = {{x = 0, y = 0},{x = -1, y = -1},{x = 1, y = -1},{x = 0, y = -1},{x = -1, y = 0},{x = -1, y = 1},{x = 0, y = 1},{x = 1, y = 1},{x = 1, y = 0},{x = 2, y = 0},{x = -2, y = 0},{x = 0, y = 2},{x = 0, y = -2}}
2019-02-10 05:47:52 +01:00
local function on_player_mined_entity(event)
local entity = event.entity
2019-02-14 15:44:12 +01:00
if valid_entities[entity.name] then
2019-02-10 05:47:52 +01:00
local tiles = {}
for _, p in pairs(coords) do
local pos = {x = entity.position.x + p.x, y = entity.position.y + p.y}
2019-02-14 15:44:12 +01:00
local tile = entity.surface.get_tile(pos)
2019-02-10 05:47:52 +01:00
if not tile.collides_with("player-layer") then
2019-02-19 14:27:58 +01:00
if replacement_tiles[tile.name] and math_random(1,2) == 1 then
2019-02-14 15:44:12 +01:00
insert(tiles, {name = replacement_tiles[tile.name], position = pos})
end
2019-02-10 05:47:52 +01:00
end
end
2019-02-10 09:56:23 +01:00
if #tiles == 0 then return end
2019-02-10 05:47:52 +01:00
entity.surface.set_tiles(tiles, true)
2019-02-14 15:44:12 +01:00
-- if math_random(1,4) == 1 then regenerate_decoratives(entity.surface, entity.position) end
2019-02-10 05:47:52 +01:00
end
end
local function on_entity_died(event)
if not event.entity.valid then return end
on_player_mined_entity(event)
end
event.add(defines.events.on_entity_died, on_entity_died)
event.add(defines.events.on_player_mined_entity, on_player_mined_entity)