mirror of
https://github.com/Refactorio/RedMew.git
synced 2024-12-12 10:04:40 +02:00
rewrote corpse_util
This commit is contained in:
parent
743d9e7955
commit
43a66563f9
@ -1,35 +1,78 @@
|
||||
local Event = require "utils.event"
|
||||
local Event = require 'utils.event'
|
||||
|
||||
local ttl = 15*60*60
|
||||
local function on_init()
|
||||
global.corpse_util = {}
|
||||
global.corpse_util.tags = {}
|
||||
global.corpse_util_corpses = {}
|
||||
end
|
||||
|
||||
local function mark_corpse(event)
|
||||
local player = game.players[event.player_index]
|
||||
local name = player.name .. "'s corpse"
|
||||
local position = player.position
|
||||
local tag = player.force.add_chart_tag(player.surface, {icon={type="item", name="power-armor-mk2"}, position=position, text=name})
|
||||
if tag ~= nil then
|
||||
table.insert(global.corpse_util.tags, {tag, game.tick + ttl})
|
||||
end
|
||||
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
|
||||
|
||||
local function remove_corpse_marks()
|
||||
if game.tick % 60 ~= 0 then return end
|
||||
local tags = global.corpse_util.tags
|
||||
local size = #tags
|
||||
for i = size, 1, -1 do
|
||||
if game.tick >= tags[i][2] then
|
||||
if tags[i][1].valid then
|
||||
tags[i][1].destroy()
|
||||
end
|
||||
table.remove(tags, i)
|
||||
end
|
||||
end
|
||||
local function remove_tag(entity)
|
||||
if not entity or not entity.valid then
|
||||
return
|
||||
end
|
||||
|
||||
local pos = entity.position
|
||||
local tag = global.corpse_util_corpses[pos.x .. ',' .. pos.y]
|
||||
global.corpse_util_corpses[entity] = nil
|
||||
|
||||
if not tag or not tag.valid then
|
||||
return
|
||||
end
|
||||
|
||||
tag.destroy()
|
||||
end
|
||||
|
||||
local function corpse_expired(event)
|
||||
local entity = event.corpse
|
||||
|
||||
remove_tag(entity)
|
||||
end
|
||||
|
||||
local function mined_entity(event)
|
||||
local entity = event.entity
|
||||
|
||||
remove_tag(entity)
|
||||
end
|
||||
|
||||
Event.on_init(on_init)
|
||||
Event.add(defines.events.on_player_died, mark_corpse)
|
||||
Event.add(defines.events.on_tick, remove_corpse_marks)
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user