Divergent/mods/OneKey Weapon Controls/gamedata/scripts/onekey_firemode_check_mcm.s...

141 lines
4.9 KiB
Plaintext
Raw Normal View History

2024-03-17 20:18:03 -04:00
local mcm_key_firemode = DIK_keys.DIK_X
local mcm_modifier_prevmode = 1
local fire_mode_names = { [-1] = "Full-Auto", [1] = "Single", [2] = "Burst", [3] = "Burst" }
local hide_fire_mode = true
function on_game_start()
RegisterScriptCallback("actor_on_first_update", actor_on_first_update)
RegisterScriptCallback("on_option_change", on_option_change)
RegisterScriptCallback("on_before_key_press", on_before_key_press)
RegisterScriptCallback("on_key_hold", on_key_hold)
end
function on_mcm_load()
op = {
id = "firemode",
sh = true,
gr = {
{ id = "slide_onekey_firemode", type = "slide", link = "ui_options_slider_player", text = "ui_mcm_title_onekey_firemode", size = { 512, 50 }, spacing = 20 },
{ id = "desc_onekey_firemode", type = "desc", text = "ui_mcm_onekey_key_firemode_desc" },
{ id = "key_firemode", type = "key_bind", val = 2, def = DIK_keys.DIK_X, hint = "onekey_key_firemode" },
{ id = "modifier_prev_firemode", type = "radio_h", val = 2, def = 1, hint = "onekey_modifier_prev_firemode", content = { {1,"mcm_kb_mod_shift"} , {2,"mcm_kb_mod_ctrl"}, {3,"mcm_kb_mod_alt"} } },
{ id = "hide_fire_mode", type = "check", val = 1, def = true, hint = "onekey_hide_fire_mode" },
}
}
return op, "onekey"
end
function actor_on_first_update()
on_option_change()
end
function on_option_change()
if ui_mcm then
mcm_key_firemode = ui_mcm.get("onekey/firemode/key_firemode")
mcm_modifier_prevmode = ui_mcm.get("onekey/firemode/modifier_prev_firemode")
hide_fire_mode = ui_mcm.get("onekey/firemode/hide_fire_mode")
end
pos = ActorMenu.get_maingame().m_ui_hud_states.m_fire_mode:GetWndPos()
pos.x = ((hide_fire_mode and pos.x > 0) or ((not hide_fire_mode) and pos.x < 0)) and (-1 * pos.x) or pos.x
ActorMenu.get_maingame().m_ui_hud_states.m_fire_mode:SetWndPos(pos)
end
function on_before_key_press(key, bind, dis, flags)
if key == mcm_key_firemode then
local weapon = db.actor:active_item()
if not is_supported_weapon(weapon) then
flags.ret_value = true
return
end
if is_supported_weapon(weapon) and has_multiple_fire_modes(weapon) and is_weapon_ready(weapon) then
local old_fire_mode = weapon:cast_Weapon():GetFireMode()
if ui_mcm.get_mod_key(mcm_modifier_prevmode) then
ui_mcm.simple_press("onekey_press", key, prev_fire_mode, weapon, old_fire_mode)
else
ui_mcm.simple_press("onekey_press", key, next_fire_mode, weapon, old_fire_mode)
end
flags.ret_value = false
return
end
flags.ret_value = true
end
end
function on_key_hold(key)
if key == mcm_key_firemode and ui_mcm.key_hold("onekey", key) then
show_fire_mode(nil)
end
end
function next_fire_mode(weapon, old_fire_mode)
level.press_action(bind_to_dik(key_bindings.kWPN_FIREMODE_NEXT))
level.release_action(bind_to_dik(key_bindings.kWPN_FIREMODE_NEXT))
CreateTimeEvent("onekey", "show_firemode_delayed", 0, show_fire_mode, old_fire_mode)
end
function prev_fire_mode(weapon, old_fire_mode)
level.press_action(bind_to_dik(key_bindings.kWPN_FIREMODE_PREV))
level.release_action(bind_to_dik(key_bindings.kWPN_FIREMODE_PREV))
CreateTimeEvent("onekey", "show_firemode_delayed", 0, show_fire_mode, old_fire_mode)
end
function show_fire_mode(old_fire_mode)
local weapon = db.actor:active_item()
if not is_supported_weapon(weapon) then
return true
end
if not has_multiple_fire_modes(weapon) then
return true
end
if not is_weapon_ready(weapon) then
return true
end
local fire_mode = weapon:cast_Weapon():GetFireMode()
-- Waiting for fire-select animation
if old_fire_mode ~= nil and fire_mode == old_fire_mode then
return false
else
local message = fire_mode_names[fire_mode]
actor_menu.set_msg(1, message, 2)
return true
end
end
function is_supported_weapon(wpn)
return wpn and IsWeapon(wpn) and (not IsItem("fake_ammo_wpn", wpn:section()))
end
function is_weapon_ready(wpn)
local state = wpn:get_state()
return state == 0 -- eIdle
end
function has_multiple_fire_modes(wpn)
section = wpn:section()
local fire_modes = parse_list(ini_sys, section, "fire_modes")
if fire_modes and table.getn(fire_modes) > 1 then
return true
end
local has_firemode_upgrade = false
wpn:iterate_installed_upgrades(function(upgr_sec)
local upgr_prop_sec = ini_sys:r_string_ex(upgr_sec, "section")
if upgr_prop_sec then
local upgr_fire_modes = parse_list(ini_sys, upgr_prop_sec, "fire_modes")
if upgr_fire_modes and table.getn(upgr_fire_modes) > 1 then
has_firemode_upgrade = true
return true
end
end
end)
return has_firemode_upgrade
end