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

161 lines
4.3 KiB
Lua
Raw Normal View History

2018-05-17 17:42:33 +02:00
local Event = require 'utils.event'
local Global = require 'utils.global'
2019-01-04 22:02:55 +02:00
local Task = require 'utils.task'
2018-11-26 03:07:03 +02:00
local Token = require 'utils.token'
2020-11-13 23:05:25 +02:00
local table = require 'utils.table'
2019-01-24 02:47:59 +02:00
local Utils = require 'utils.core'
local Public = {}
local player_corpses = {}
2020-11-08 14:54:01 +02:00
Global.register(player_corpses, function(tbl)
player_corpses = tbl
end)
local function get_index(player_index, tick)
return player_index * 0x100000000 + tick
2021-03-03 22:13:25 +02:00
end
local function get_data(player_index, tick)
local index = get_index(player_index, tick)
return player_corpses[index]
end
2018-05-17 17:42:33 +02:00
local function remove_tag(player_index, tick)
local index = get_index(player_index, tick)
2018-05-17 17:42:33 +02:00
local data = player_corpses[index]
if not data then
2018-05-17 17:42:33 +02:00
return
end
local tag = data.tag
player_corpses[index] = nil
2018-05-17 17:42:33 +02:00
if not tag or not tag.valid then
return
end
tag.destroy()
end
2021-03-03 22:13:25 +02:00
local function remove_corpse_tag(corpse)
if corpse and corpse.valid then
remove_tag(corpse.character_corpse_player_index, corpse.character_corpse_tick_of_death)
2018-05-23 22:21:30 +02:00
end
end
2021-03-03 22:13:25 +02:00
local function corpse_expired(event)
remove_corpse_tag(event.corpse)
end
2020-11-08 14:54:01 +02:00
local corpse_util_mined_entity = Token.register(function(data)
if not data.entity.valid then
remove_tag(data.player_index, data.tick)
2018-05-23 22:21:30 +02:00
end
2020-11-08 14:54:01 +02:00
end)
2018-05-17 17:42:33 +02:00
local function mined_entity(event)
local entity = event.entity
2019-01-21 17:54:14 +02:00
if not entity or not entity.valid or entity.name ~= 'character-corpse' then
return
end
local corpse_owner_index = entity.character_corpse_player_index
local death_tick = entity.character_corpse_tick_of_death
2019-01-21 17:54:14 +02:00
-- 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.
2020-11-08 14:54:01 +02:00
Task.set_timeout_in_ticks(1, corpse_util_mined_entity, {
entity = entity,
player_index = corpse_owner_index,
tick = death_tick
2020-11-08 14:54:01 +02:00
})
2019-01-21 17:54:14 +02:00
local player_index = event.player_index
if player_index == corpse_owner_index then
return
end
2019-01-21 17:54:14 +02:00
local data = get_data(corpse_owner_index, death_tick)
if not data or not data.alert_looting then
2019-01-21 17:54:14 +02:00
return
end
local player = game.get_player(player_index)
local corpse_owner = game.get_player(corpse_owner_index)
2019-01-21 17:54:14 +02:00
2021-03-03 22:13:25 +02:00
if player and corpse_owner then
2019-02-09 22:45:55 +02:00
local message = table.concat {player.name, ' has looted ', corpse_owner.name, "'s corpse"}
2019-01-24 06:37:11 +02:00
Utils.action_warning('[Corpse]', message)
2019-01-21 17:54:14 +02:00
end
end
local function on_gui_opened(event)
local entity = event.entity
if not entity or not entity.valid or entity.name ~= 'character-corpse' then
return
end
local player_index = event.player_index
local corpse_owner_index = entity.character_corpse_player_index
if player_index == corpse_owner_index then
return
end
local death_tick = entity.character_corpse_tick_of_death
local data = get_data(corpse_owner_index, death_tick)
if not data or not data.alert_looting then
2019-01-21 17:54:14 +02:00
return
end
local player = game.get_player(player_index)
local corpse_owner = game.get_player(corpse_owner_index)
2019-01-21 17:54:14 +02:00
2021-03-03 22:13:25 +02:00
if player and corpse_owner then
2019-01-24 02:47:59 +02:00
local message = table.concat {player.name, ' is looting ', corpse_owner.name, "'s corpse"}
2019-01-24 06:37:11 +02:00
Utils.action_warning('[Corpse]', message)
2018-05-23 22:21:30 +02:00
end
end
2021-03-03 22:13:25 +02:00
local function on_gui_closed(event)
local player = game.get_player(event.player_index)
if not player or not player.valid then
return
end
local entity = event.entity
if not entity or not entity.valid or entity.name ~= 'character-corpse' then
return
end
local inv_corpse = entity.get_inventory(defines.inventory.character_corpse)
if not inv_corpse or not inv_corpse.valid then
return
end
if inv_corpse.is_empty() then
remove_corpse_tag(entity)
entity.destroy()
end
end
2018-05-17 17:42:33 +02:00
Event.add(defines.events.on_character_corpse_expired, corpse_expired)
Event.add(defines.events.on_pre_player_mined_item, mined_entity)
2019-01-21 17:54:14 +02:00
Event.add(defines.events.on_gui_opened, on_gui_opened)
2021-03-03 22:13:25 +02:00
Event.add(defines.events.on_gui_closed, on_gui_closed)
function Public.clear()
table.clear_table(player_corpses)
end
function Public.add_tag(tag, player_index, death_tick, alert_looting)
local index = get_index(player_index, death_tick)
player_corpses[index] = {tag = tag, alert_looting = alert_looting}
end
return Public