Divergent/mods/Outfit Animations/gamedata/scripts/outfit_animations.script

180 lines
5.1 KiB
Plaintext
Raw Normal View History

2024-03-17 20:18:03 -04:00
local fov_manager = outfit_animations_fov_manager
local mcm_memory_enable = outfit_animations_mcm.get_config("memory")
local mcm_allow_movement = outfit_animations_mcm.get_config("allow_movement")
local originalPIF = actor_effects.play_item_fx
function actor_effects.play_item_fx(name)
if name == "outfit" then return end
originalPIF(name)
end
function on_game_start()
RegisterScriptCallback("on_option_change", on_option_change)
RegisterScriptCallback("actor_on_first_update", actor_on_first_update)
end
function on_option_change()
mcm_memory_enable = outfit_animations_mcm.get_config("memory")
mcm_allow_movement = outfit_animations_mcm.get_config("allow_movement")
memory_max = outfit_animations_mcm.get_config("memory_size")
end
function actor_on_first_update()
local sec = db.actor:item_in_slot(7) and db.actor:item_in_slot(7)
if sec then remember_outfit(sec) end
end
------------------------------------------------------------------------------
-- main
------------------------------------------------------------------------------
local anm_info = nil --it's here cuz of stalke engine magic that I don't know
local cur_slot
local det_active
function play_animation_delayed(obj, callback)
--prepare
Invoke("play_animation_te3", 0.01, function ()
hide_hud_inventory()
cur_slot = db.actor:active_slot()
det_active = db.actor:active_detector() or nil
if det_active then det_active:switch_state(2) end
db.actor:activate_slot(0)
if headgear_animations then headgear_animations.enable_animations = false end
end)
wait_for_free_hands(function()
--play
fov_manager.restore_fov()
if mcm_allow_movement then game.only_allow_movekeys(true) else level.disable_input() end
callback() -- perform inventory ui action
anm_info = select_animation(obj)
local delay = 0.25
local length = 0
length = length + game.get_motion_length(anm_info.section_name, anm_info.anm, 1) / 1000
Invoke("play_outfti_inspect_animation_te", delay, function()
xr_effects.play_snd(db.actor, nil, {[1] = anm_info.snd})
level.add_cam_effector(anm_info.cam, 1300, false, "")
game.play_hud_motion(2, anm_info.section_name, anm_info.anm, false, 1)
end)
--restore
Invoke("restore_after_animtion_play_te", delay + length + 0.25, function()
if mcm_allow_movement then game.only_allow_movekeys(false) else level.enable_input() end
if headgear_animations then headgear_animations.enable_animations = true end
-- db.actor:activate_slot(cur_slot or 0)
-- if det_active then det_active:switch_state(1) end
end)
end)
end
function select_animation(obj)
--temp solution
--todo: choose animation base on armor type
local m_section = "outfit_animation_hud"
if mcm_memory_enable then
local sec = obj and obj:section()
if sec then
if memory_contain(sec) then
m_section = "outfit_animation_fast_hud"
else
remember_outfit(sec)
m_section = "outfit_animation_hud"
end
else
m_section = "outfit_animation_fast_hud"
end
end
return {
section_name = m_section,
anm = "anm_equip",
cam = ini_sys:r_string_ex(m_section, "cam"),
snd = ini_sys:r_string_ex(m_section, "snd")
}
end
function wait_for_free_hands(action_to_perform)
local force_timer = 0
CreateTimeEvent("outfit_animations", "wait_for_free_hands_te0", 0.1, function()
if (db.actor:active_slot() == 0 and not db.actor:active_detector() and not enhanced_animations.used_item) or (force_timer > 5) then
Invoke("wait_for_free_hands_te1", 0, action_to_perform) --i don understan y this works (ノへ ̄、)
return true
end
force_timer = force_timer + (device().time_delta/1000)
return false
end)
end
function Invoke(name, time, action)
CreateTimeEvent("outfit_animations", name, time, function()
action()
return true
end)
end
------------------------------------------------------------------------------
-- outfit memmory management.
------------------------------------------------------------------------------
local memory_first = 0
local memory_last = -1
local memory_max = outfit_animations_mcm.get_config("memory_size")
local memory = {}
function remember_outfit(sec)
local last = memory_last + 1
memory_last = last
memory[last] = sec
if memory_max > 0 and (memory_size() > memory_max) then
forget_outfit()
end
end
function forget_outfit()
local first = memory_first
memory[first] = nil
memory_first = first + 1
end
function memory_size()
local count = 0
for _, __ in pairs(memory) do
count = count + 1
end
return count
end
function memory_contain(sec)
for _, v in pairs(memory) do
if v == sec then return true end
end
return false
end
function memory_print()
local s = memory_size()
if s == 0 then
printf("outfit memory: l(" .. s ..") {}")
return
end
printf("outfit memory: l(" .. s .. ") {")
for i, v in pairs(memory) do
if i == "last" or i == "first" or i == "maximum" then
goto continue
end
printf(" " .. i .. " = " .. v)
::continue::
end
printf("}")
end