1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-12 10:04:40 +02:00
RedMew/corpse_util.lua

95 lines
2.3 KiB
Lua
Raw Normal View History

2018-05-17 17:42:33 +02:00
local Event = require 'utils.event'
2018-05-23 22:21:30 +02:00
local Task = require 'utils.Task'
local Token = require 'utils.global_token'
2018-04-06 21:58:50 +02:00
local function on_init()
2018-05-17 17:42:33 +02:00
global.corpse_util_corpses = {}
end
2018-05-17 17:42:33 +02:00
local function player_died(event)
local player = game.players[event.player_index]
if not player or not player.valid then
return
end
local pos = player.position
local entities =
player.surface.find_entities_filtered {
area = {{pos.x, pos.y}, {pos.x + 1, pos.y + 1}},
name = 'character-corpse'
}
local entity
for _, e in ipairs(entities) do
if e.character_corpse_player_index == event.player_index then
entity = e
break
end
end
if not entity or not entity.valid then
return
end
local text = player.name .. "'s corpse"
local position = entity.position
local tag =
player.force.add_chart_tag(
player.surface,
{icon = {type = 'item', name = 'power-armor-mk2'}, position = position, text = text}
)
if not tag then
return
end
global.corpse_util_corpses[position.x .. ',' .. position.y] = tag
end
2018-05-23 22:21:30 +02:00
local function remove_tag(position)
local pos = position.x .. ',' .. position.y
2018-05-17 17:42:33 +02:00
2018-05-23 22:21:30 +02:00
local tag = global.corpse_util_corpses[pos]
if tag then
global.corpse_util_corpses[pos] = nil
end
2018-05-17 17:42:33 +02:00
if not tag or not tag.valid then
return
end
tag.destroy()
end
local function corpse_expired(event)
local entity = event.corpse
2018-05-23 22:21:30 +02:00
if entity and entity.valid then
remove_tag(entity.position)
end
end
local corpse_util_mined_entity =
Token.register(
function(data)
if not data.entity.valid then
remove_tag(data.position)
end
2018-05-23 22:21:30 +02:00
end
)
2018-05-17 17:42:33 +02:00
local function mined_entity(event)
local entity = event.entity
2018-05-23 22:21:30 +02:00
if entity and entity.valid and entity.name == 'character-corpse' then
-- The corpse may be mined but not removed (if player doesn't have inventory space)
-- so we wait one tick to see if the corpse is gone.
Task.set_timeout_in_ticks(1, corpse_util_mined_entity, {entity = entity, position = entity.position})
2018-05-23 22:21:30 +02:00
end
end
2018-04-06 21:58:50 +02:00
Event.on_init(on_init)
2018-05-17 17:42:33 +02:00
Event.add(defines.events.on_player_died, player_died)
Event.add(defines.events.on_character_corpse_expired, corpse_expired)
Event.add(defines.events.on_pre_player_mined_item, mined_entity)