Divergent/mods/Dead NPC Torches/gamedata/scripts/zzz_deadtorch.script

108 lines
2.9 KiB
Plaintext
Raw Normal View History

torch_npcs={}
pre_torch_npcs = {}
avg_light_time = 2
function npc_on_death_callback(npc, who)
if not npc then return end
if not pre_torch_npcs[npc:id()] then return end
tg = time_global()
local idi = npc:id()
pre_torch_npcs[npc:id()] = false
torch_npcs[idi] = {}
cur_light_time = avg_light_time * math.random(0.33, 2.5)
local torch = npc:object("device_torch")
if not torch then
alife_create_item("device_torch", npc)
end
torch_npcs[idi].man = false
torch_npcs[idi].light_time = tg + cur_light_time * 60000
end
function npc_on_before_hit(npc,who)
local torch = npc:object("device_torch")
if not torch or not torch:attachable_item_enabled() then
pre_torch_npcs[npc:id()] = false
end
if torch and torch:attachable_item_enabled() then
pre_torch_npcs[npc:id()] = true
end
end
function update()
tg = time_global()
for k,v in pairs(torch_npcs) do
nepis = level.object_by_id(k)
if not nepis then
torch_npcs[k] = nil
else
if v.man == false then
local torch = nepis:object("device_torch")
if not torch then
alife_create_item("device_torch", nepis)
v.man = true
else
if not torch:attachable_item_enabled() then
torch:enable_attachable_item(true)
torch:enable_torch(true)
end
torch:update_torch()
end
end
if v.man then
if nepis then
local torch = nepis:object("device_torch")
if not torch then
alife_create_item("device_torch", nepis)
else
if not torch:attachable_item_enabled() then
torch:enable_attachable_item(true)
torch:enable_torch(true)
end
torch:update_torch()
end
end
if v.light_time < tg or (not nepis:bone_visible("bip01_head")) then
torch_npcs[k] = nil
local torch = nepis:object("device_torch")
if torch then
torch:enable_attachable_item(false)
torch:enable_torch(false)
alife_release(torch)
end
end
end
end
end
end
--from Looting Takes Time 0.51, credit goes to RavenAscendants and xcvb
local basePI = ui_inventory.UIInventory.ParseInventory
function ui_inventory.UIInventory:ParseInventory(npc, all, id_list, ignore_kind)
local inv = basePI(self, npc, all, id_list, ignore_kind)
if not (self:IsInvOwner(npc) and npc:id() ~= AC_ID and self.mode == "loot" and all and id_list) then
return inv
end
local res = dup_table(inv)
-- corpses == (nil = timer for item does not exist, true = timer ticking, false = timer ended)
local npc_id = npc:id()
for item_id, item in pairs(res) do
-- and pass table without this item if timer didn't exist or still ticking (guess just true works)
if item:section() == "device_torch" then
res[item_id] = nil
end
end
return res
end
function on_game_start()
RegisterScriptCallback("actor_on_before_death", function ()
AddUniqueCall(update)
end)
RegisterScriptCallback("npc_on_death_callback", npc_on_death_callback)
RegisterScriptCallback("npc_on_before_hit", npc_on_before_hit)
RegisterScriptCallback("actor_on_update", update)
end