2019-02-10 05:47:52 +01:00
|
|
|
--Mining or breaking a rock paints the tiles underneath
|
2021-03-14 01:02:07 +01:00
|
|
|
local Event = require 'utils.event'
|
2019-02-10 05:47:52 +01:00
|
|
|
|
|
|
|
|
local valid_entities = {
|
2024-09-25 23:25:57 +02:00
|
|
|
['big-rock'] = true,
|
|
|
|
|
['huge-rock'] = true,
|
|
|
|
|
['big-sand-rock'] = true
|
2019-02-10 05:47:52 +01:00
|
|
|
}
|
|
|
|
|
|
2020-10-28 05:18:02 +01:00
|
|
|
local replacement_tiles = {
|
2021-03-14 01:02:07 +01:00
|
|
|
['nuclear-ground'] = 'dirt-7',
|
|
|
|
|
['dirt-7'] = 'dirt-6',
|
|
|
|
|
['dirt-6'] = 'dirt-5',
|
|
|
|
|
['dirt-5'] = 'dirt-4',
|
|
|
|
|
['dirt-4'] = 'dirt-3',
|
|
|
|
|
['dirt-3'] = 'dirt-2'
|
2020-10-28 05:18:02 +01:00
|
|
|
}
|
2019-02-14 15:44:12 +01:00
|
|
|
|
2019-02-23 15:11:49 +01:00
|
|
|
local coords = {
|
2024-09-25 17:00:40 +01:00
|
|
|
{ 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 }
|
2021-03-14 01:02:07 +01:00
|
|
|
}
|
2019-02-10 09:56:23 +01:00
|
|
|
|
2019-02-23 15:11:49 +01:00
|
|
|
local function on_pre_player_mined_item(event)
|
2021-03-14 01:02:07 +01:00
|
|
|
local entity = event.entity
|
|
|
|
|
if not valid_entities[entity.name] then
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local tiles = {}
|
|
|
|
|
for i = 1, #coords do
|
|
|
|
|
local p = coords[i]
|
|
|
|
|
|
2024-09-25 17:00:40 +01:00
|
|
|
local pos = { x = entity.position.x + p.x, y = entity.position.y + p.y }
|
2021-03-14 01:02:07 +01:00
|
|
|
local tile = entity.surface.get_tile(pos)
|
2024-09-25 17:00:40 +01:00
|
|
|
if not tile.collides_with('player') then
|
2021-03-14 01:02:07 +01:00
|
|
|
if replacement_tiles[tile.name] and math.random(1, 2) == 1 then
|
2024-09-25 17:00:40 +01:00
|
|
|
table.insert(tiles, { name = replacement_tiles[tile.name], position = pos })
|
2021-03-14 01:02:07 +01:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
if #tiles == 0 then
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
entity.surface.set_tiles(tiles, true)
|
2019-02-10 05:47:52 +01:00
|
|
|
end
|
|
|
|
|
|
2021-03-14 01:02:07 +01:00
|
|
|
local function on_entity_died(event)
|
|
|
|
|
local entity = event.entity
|
|
|
|
|
if not entity or not entity.valid then
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local cause = event.cause
|
|
|
|
|
|
|
|
|
|
if cause and cause.valid and cause.force.index == 2 then
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
on_pre_player_mined_item(event)
|
2019-02-10 05:47:52 +01:00
|
|
|
end
|
|
|
|
|
|
2021-03-14 01:02:07 +01:00
|
|
|
Event.add(defines.events.on_entity_died, on_entity_died)
|
|
|
|
|
Event.add(defines.events.on_pre_player_mined_item, on_pre_player_mined_item)
|