Guest User

beefnvgsimprovedaltaimfix

a guest
Jan 29th, 2024
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 23.44 KB | None | 0 0
  1. -----------------------------------------------------------------------------
  2. --     
  3. -----------------------------------------------------------------------------
  4.  
  5. -- Beef NVG MCM values
  6. local nvg_gain_max = 2.0                -- max value for nvg gain (brightness)
  7. local nvg_gain_min = 0.5                -- min value for nvg gain (brightness)
  8. local nvg_washout_thresh = 0.1          -- used to calculate washout effect on light sources
  9. local nvg_view_through_scope = false    -- can you keep your NVGs on when looking through scope?
  10. local nvg_gain_offset = 1.0             -- offset for nvg gain to accomodate for brighter/darker worlds
  11. local nvg_mode_gen1 = 3.0                   -- overlay mode gen 1
  12. local nvg_num_tubes_gen2 = 2.0          -- number of tubes for NVG gen 2
  13. local nvg_mode_gen1 = 0.0                   -- overlay mode gen 2
  14. local nvg_num_tubes_gen3 = 4.0          -- number of tubes for NVG gen 3
  15. local nvg_mode_gen1 = 0.0                   -- overlay mode gen 3
  16. local nvg_stays_pda = true              -- disable nvg on PDA?
  17. local shader_scope_compat = false       -- compatibility with 2d shader scopes
  18. local nvg_radius = 0.55                 -- Radius of circles
  19.  
  20. -- Variables
  21. local nvg_generation = 0        -- 1, 2, or 3
  22. local nvg_num_tubes = 0         -- 1, 2, or 4 tubes -- 11 for single offset left and 12 for single offset right
  23. local nvg_gain_current = 1      -- current gain setting
  24. local vignette_amount = 0.1     -- standard vignette value
  25. local vignette_current = 1.0        -- current vignette value, for turn-on/ turn-off effect
  26. local vignette_speed = 1.4 -- how fast vignette changes
  27. local glitch_power = 0          -- how much 'glitch' to add to NVGs
  28. local nvg_mode = 0          -- overlay preset
  29. local crt_latch = false       -- latches crt effect on turn-on
  30. local flip_latch = false        -- latches flip down effect on turn-on
  31. local nv_eff = {}               -- to store the effect
  32. local flip_speed = 20           -- how fast to flip down
  33. local flip_down = 1         -- flip down value (1 all the way up, 100 all the way down)
  34.  
  35. -- Variables for turn-on animation
  36. local torch_anm_state = 0
  37. local torch_anm_start = 0
  38. local torch_anm_time = 0
  39. local mc_anm_time_1 = 0
  40. local torch_anm_sec = "anim_headlamp"
  41. local mc_anm_sec = "anim_mask_clean"
  42. local snd_headlamp = sound_object('interface\\inv_torch')
  43. local hide_hand_speed = 1.5
  44. local last_animation_frame = 0
  45.  
  46. -- Variables for on-weapon zoom (and shader scopes compat)
  47. local zoomed_in = false
  48. local tube_count_temp = 0
  49. --local vignette_temp = 0
  50. local radius_temp
  51. local scope_blur_temp = 0
  52. local shader_scope_flag = false
  53. local zoom = true
  54. -- Variables for PDA auto-turn-off functionality
  55. local disable_nvg = false
  56.  
  57. local normal_fov
  58. -----------------------------------------------------------------------------
  59. --     
  60. -----------------------------------------------------------------------------
  61.  
  62. function on_option_change()
  63.         nvg_gain_max = z_beefs_nvgs_mcm.get_config("nvg_gain_max_mcm")
  64.         nvg_gain_min = z_beefs_nvgs_mcm.get_config("nvg_gain_min_mcm")
  65.         nvg_gain_offset = z_beefs_nvgs_mcm.get_config("nvg_gain_offset_mcm")
  66.         nvg_washout_thresh = z_beefs_nvgs_mcm.get_config("nvg_washout_thresh_mcm")
  67.         nvg_view_through_scope = z_beefs_nvgs_mcm.get_config("nvg_view_through_scope_mcm")
  68.         nvg_num_tubes_gen1 = z_beefs_nvgs_mcm.get_config("nvg_gen1_tubes_mcm")
  69.         nvg_num_tubes_gen2 = z_beefs_nvgs_mcm.get_config("nvg_gen2_tubes_mcm")
  70.         nvg_num_tubes_gen3 = z_beefs_nvgs_mcm.get_config("nvg_gen3_tubes_mcm")
  71.         nvg_mode_gen1 = z_beefs_nvgs_mcm.get_config("nvg_gen1_mode_mcm")
  72.         nvg_mode_gen2 = z_beefs_nvgs_mcm.get_config("nvg_gen2_mode_mcm")
  73.         nvg_mode_gen3 = z_beefs_nvgs_mcm.get_config("nvg_gen3_mode_mcm")
  74.         nvg_stays_pda = z_beefs_nvgs_mcm.get_config("nvg_stays_pda_mcm")
  75.         shader_scope_compat = z_beefs_nvgs_mcm.get_config("shader_scope_compat")
  76. end
  77.  
  78.  
  79. -----------------------------------------------------------------------------
  80. --      NVG DATA PACKING AND UPDATE FUNCTION
  81. -----------------------------------------------------------------------------
  82.  
  83. function pack_nvg_data()
  84.  
  85.     local x_1 = tostring(nvg_generation)                           -- Generation (1,2,3) - outputs 1.x to 3.x
  86.     local x_2 = tostring(nvg_num_tubes * 10)                            -- Num Tubes (1,2,4,1.1,1.2) outputs x.10, x.20, x.40, x.11, or x.12
  87.     local y_1 = tostring(math.floor(nvg_gain_current * 10))        -- Gain Adjust (0.1 to 3) -- outputs 1.y to 30.y in 1. increment
  88.     local y_2 = tostring(math.floor(nvg_washout_thresh * 10))      -- Washout Thresh (0.0 - 0.9) - outputs y.0 to y.9 in .1 increment
  89.     local z_1 = tostring(math.floor(vignette_current * 100))       -- Vignette Amount (0.0 to 1.0) outputs 0.z to 100.z in 1. increment
  90.     local z_2 = tostring(math.floor(glitch_power * 10))            -- Glitch Power (0.0 - 0.9) - outputs z.0 to z.9 in .1 increment
  91.     local w_1 = tostring((math.floor(nvg_gain_offset * 10)) )      -- Gain Offset (0.5 to 3.0) - outputs 5.w to 30.w in 1. increment
  92.     local w_2 = tostring(nvg_mode)                                 -- Mode (0,1) - outputs w.0 or w.1 depending on mode
  93.  
  94.     return "(" .. x_1 .. "." .. x_2 .. "," .. y_1 .. "." .. y_2 .. "," .. z_1 .. "." .. z_2 .. "," .. w_1 .. "." .. w_2 .. ")"
  95. end
  96.  
  97. function pack_nvg_data_2()
  98.  
  99.     local x_1 = tostring(math.floor(flip_down))                                 -- Flip down amount - outputs 1.x to 100.x
  100.     local x_2 = tostring(0)                            
  101.     local y_1 = tostring(0)
  102.     local y_2 = tostring(math.floor(nvg_radius*100))                                -- Radius for NVG effect - outputs 0.0 to 0.99
  103.     local z_1 = tostring(0)
  104.     local z_2 = tostring(0)
  105.     local w_1 = tostring(0)
  106.     local w_2 = tostring(0)
  107.  
  108.     return "(" .. x_1 .. "." .. x_2 .. "," .. y_1 .. "." .. y_2 .. "," .. z_1 .. "." .. z_2 .. "," .. w_1 .. "." .. w_2 .. ")"
  109. end
  110.  
  111. function update_nvgs()
  112.     get_console():execute("r__nightvision " .. nvg_generation )
  113.     get_console():execute("shader_param_8 " .. pack_nvg_data() )
  114.     get_console():execute("shader_param_7 " .. pack_nvg_data_2() )
  115. end
  116.  
  117.  
  118.  
  119. -----------------------------------------------------------------------------
  120. --      NVG TURN ON AND TURN OFF FUNCTIONS, INCL ANIMATION
  121. -----------------------------------------------------------------------------
  122.  
  123. function allow_animation()
  124.     return (torch_anm_state == 0  and game.hud_motion_allowed() and (not game.only_movekeys_allowed()) and actor_menu.get_last_mode() == 0 and (not IsMoveState("mcClimb")))
  125. end
  126.  
  127. local lowered_anm_kinds = {
  128.     ["w_sniper"]    = true,
  129.     ["w_rifle"]     = true,
  130.     ["w_smg"]       = true,
  131.     ["w_shotgun"]   = true,
  132. }
  133.  
  134. local lowered_anm_classes = {
  135.     ["WP_VAL"]      = true,
  136.     ["WP_AK74"]     = true,
  137.     ["WP_LR300"]    = true,
  138.     ["WP_BM16"]     = true,
  139.     ["WP_SVD"]      = true,
  140.     ["WP_SVU"]      = true,
  141.     ["WP_GROZA"]    = true,
  142.     ["WP_ASHTG"]    = true,
  143.     ["WP_ASHTG"]    = true,
  144.     ["WP_SHOTG"]    = true,
  145.     ["WP_RG6"]      = true,
  146.     ["WP_RPG7"]     = true,
  147.     ["D_PDA"]       = true,
  148. }
  149.  
  150. function register_nvg_callbacks()
  151.     RegisterScriptCallback("actor_on_update", actor_on_update)
  152.     RegisterScriptCallback("actor_on_weapon_zoom_in", actor_on_weapon_zoom_in)
  153.     RegisterScriptCallback("actor_on_weapon_zoom_out", actor_on_weapon_zoom_out)
  154.     RegisterScriptCallback("actor_on_info_callback",pda_check)
  155. end
  156.            
  157. function unregister_nvg_callbacks()
  158.     UnregisterScriptCallback("actor_on_weapon_zoom_in", actor_on_weapon_zoom_in)
  159.     UnregisterScriptCallback("actor_on_weapon_zoom_out", actor_on_weapon_zoom_out)
  160.     UnregisterScriptCallback("actor_on_info_callback",pda_check)
  161.     UnregisterScriptCallback("actor_on_update", actor_on_update)
  162. end
  163.  
  164. function item_device.set_nightvision(section,state)
  165.     if (not section) then
  166.         return
  167.     end
  168.  
  169.     nv_eff[section] = nv_eff[section] or ini_sys:r_string_ex(section,"nv_effect")
  170.  
  171.     if nv_eff[section] ~= nil then
  172.  
  173.         if (nv_eff[section] == "nightvision_1") then
  174.         nvg_generation = 1
  175.         nvg_num_tubes = nvg_num_tubes_gen1
  176.         nvg_mode = nvg_mode_gen1
  177.         end
  178.        
  179.         if (nv_eff[section] == "nightvision_2") then
  180.         nvg_generation = 2
  181.         nvg_num_tubes = nvg_num_tubes_gen2
  182.         nvg_mode = nvg_mode_gen2
  183.         end
  184.        
  185.         if (nv_eff[section] == "nightvision_3") then
  186.         nvg_generation = 3
  187.         nvg_num_tubes = nvg_num_tubes_gen3
  188.         nvg_mode = nvg_mode_gen3
  189.         end
  190.        
  191.         if state and (not item_device.nv_state) or (not state) and item_device.nv_state then
  192.             local function nvg_turn_on()
  193.                 register_nvg_callbacks()
  194.                 last_animation_frame = time_global()
  195.                 update_nvgs()
  196.                 item_device.nv_state = true
  197.                 utils_obj.play_sound("interface\\inv_nv_start")
  198.             end
  199.  
  200.             local function nvg_turn_off()
  201.                 last_animation_frame = time_global()
  202.                 update_nvgs()
  203.                 item_device.nv_state = false
  204.                 utils_obj.play_sound("interface\\inv_nv_off")
  205.             end
  206.  
  207.                 -- Check if headlamp is equipped. No need for battery check
  208.             if (not allow_animation()) or (not item_device.can_toggle_torch()) then
  209.                 if ((not state) and item_device.nv_state) and torch_anm_state == 0 then
  210.                     nvg_turn_off()
  211.                 end
  212.                 return
  213.             end
  214.            
  215.             local weapon_zoomed = axr_main.weapon_is_zoomed and (not (IsPistol(db.actor:active_item(),nil)) or db.actor:active_detector())
  216.             local det_active = db.actor:active_detector() or nil
  217.             local det_hide_time = 0
  218.             local new_speed = 0
  219.             local anm_name = "script\\headlamp.anm"
  220.             local anm_additional_length = 0.45
  221.            
  222.             if (db.actor:active_item()) then
  223.                 local itm = db.actor:active_item()
  224.                 local hud = ini_sys:r_string_ex(itm:section(),"hud") or "hud_base"
  225.                 local name = ini_sys:r_string_ex(hud,"gasmask_anm")
  226.                 if (name) then
  227.                     anm_name = name
  228.                 else
  229.                     local kind = ini_sys:r_string_ex(itm:section(),"kind") or nil
  230.                     local class = ini_sys:r_string_ex(itm:section(),"class") or nil
  231.                     if ((kind and lowered_anm_kinds[kind]) or (class and lowered_anm_classes[class])) then
  232.                         anm_name = "script\\lower.anm"
  233.                         anm_additional_length = 0.8
  234.                     end
  235.                 end
  236.                
  237.                 if (not det_active and weapon_zoomed) then
  238.                     if (get_console():get_bool("wpn_aim_toggle")) then
  239.                         level.press_action(bind_to_dik(key_bindings.kWPN_ZOOM))
  240.                     else
  241.                         level.release_action(bind_to_dik(key_bindings.kWPN_ZOOM))
  242.                     end
  243.                 end
  244.             end
  245.            
  246.             if (det_active) then
  247.                 if (weapon_zoomed) then
  248.                     det_active:switch_state(2)
  249.                     det_hide_time = (det_active:play_hud_motion("anm_zoom_hide_fast", true, 3, 1, 0) / 1000)
  250.                    
  251.                     if (get_console():get_bool("wpn_aim_toggle")) then
  252.                         level.press_action(bind_to_dik(key_bindings.kWPN_ZOOM))
  253.                     else
  254.                         level.release_action(bind_to_dik(key_bindings.kWPN_ZOOM))
  255.                     end
  256.                 else
  257.                     det_hide_time = (det_active:play_hud_motion("anm_hide_fast", true, 3, 2, 0) / 1000) + 0.1
  258.                 end
  259.                
  260.                 if (det_hide_time == 0) then
  261.                     det_hide_time = (det_active:play_hud_motion("anm_hide", true, 3, 3.5, 0) / 1000) + 0.1
  262.                 end
  263.                
  264.                 game.play_hud_anm(anm_name, 0, 0.25, 1, false)
  265.             end
  266.            
  267.             local function torch_toggle_anim_fast()
  268.                 local time_g = time_global()
  269.                
  270.                 if (torch_anm_state == 1) then
  271.                     game.play_hud_motion(1, torch_anm_sec, "anm_switch", true, 0.75)
  272.                     level.add_cam_effector("camera_effects\\headlamp\\headlamp.anm", 7539, false, "", 0, false)
  273.                     torch_anm_start = time_g
  274.                     torch_anm_state = 2
  275.                    
  276.                 elseif (torch_anm_state == 2) and (time_g > torch_anm_start + torch_anm_time - 350) then
  277.                     if (item_device.nv_state) then
  278.                         nvg_turn_off()
  279.                     else
  280.                         nvg_turn_on()
  281.                     end
  282.                     torch_anm_state = 3
  283.                    
  284.                 elseif (torch_anm_state == 3) and (time_g > torch_anm_start + torch_anm_time + 1) then
  285.                     torch_anm_state = 0
  286.                     torch_anm_start = 0
  287.                     game.only_allow_movekeys(false)
  288.                     game.set_actor_allow_ladder(true)
  289.                     RemoveTimeEvent(0, "play_torch_toggle")
  290.                 end
  291.                                
  292.                 return false
  293.             end
  294.            
  295.             local function torch_toggle_anim()
  296.                 local time_g = time_global()
  297.                
  298.                 if (torch_anm_state == 1) then
  299.                     if (det_active) then
  300.                         det_active:switch_state(3)
  301.                         db.actor:force_hide_detector()
  302.                     else
  303.                         game.play_hud_anm(anm_name, 0, 1, 1, false)
  304.                     end
  305.                        
  306.                     game.play_hud_motion(1, mc_anm_sec, "anm_hide_hand", true, hide_hand_speed)
  307.                     new_speed = game.set_hud_anm_time(anm_name, ((torch_anm_time + mc_anm_time_1) / 1000) + anm_additional_length)
  308.                     torch_anm_start = time_g
  309.                     torch_anm_state = 2
  310.                    
  311.                 elseif (torch_anm_state == 2) and (time_g > torch_anm_start + mc_anm_time_1 - 10) then
  312.                     game.play_hud_motion(1, torch_anm_sec, "anm_switch", true, 1)
  313.                     level.add_cam_effector("camera_effects\\headlamp\\headlamp.anm", 7539, false, "", 0, false)
  314.                     torch_anm_state = 3
  315.                    
  316.                 elseif (torch_anm_state == 3) and (time_g > torch_anm_start + mc_anm_time_1 + torch_anm_time - 550) then
  317.                     if (item_device.nv_state) then
  318.                         nvg_turn_off()
  319.                     else
  320.                         nvg_turn_on()
  321.                     end
  322.                     torch_anm_state = 4
  323.                    
  324.                 elseif (torch_anm_state == 4) and (time_g > torch_anm_start + mc_anm_time_1 + torch_anm_time + 15) then
  325.                     if (new_speed) then game.play_hud_anm(anm_name, 2, new_speed, 1, false, true) end
  326.                     if (det_active) then db.actor:show_detector(true) end
  327.                     torch_anm_state = 5
  328.                    
  329.                 elseif (torch_anm_state == 5) and (time_g > torch_anm_start + mc_anm_time_1 + torch_anm_time + (anm_additional_length * 1000 + 100)) then
  330.                     torch_anm_state = 0
  331.                     torch_anm_start = 0
  332.                     game.only_allow_movekeys(false)
  333.                     game.set_actor_allow_ladder(true)
  334.                     RemoveTimeEvent(0, "play_torch_toggle")
  335.                 end
  336.                
  337.                 return false
  338.             end
  339.            
  340.             torch_anm_state = 1
  341.             game.only_allow_movekeys(true)
  342.             game.set_actor_allow_ladder(false)
  343.            
  344.             if (not db.actor:active_item() and not det_active) then
  345.                 CreateTimeEvent(0, "play_torch_toggle", det_hide_time, torch_toggle_anim_fast)
  346.             else
  347.                 CreateTimeEvent(0, "play_torch_toggle", det_hide_time, torch_toggle_anim)
  348.             end
  349.         end
  350.     end
  351. end
  352.  
  353. -----------------------------------------------------------------------------
  354. --      NVG BRIGHTNESS ADJUSTMENT FUNCTIONS
  355. -----------------------------------------------------------------------------
  356.  
  357. function brightness_up()
  358.     if item_device.nv_state then
  359.         local step_size = (nvg_gain_max - nvg_gain_min) / 5
  360.         if nvg_gain_current < nvg_gain_max then
  361.  
  362.             nvg_gain_current = nvg_gain_current + step_size
  363.  
  364.             if nvg_gain_current > nvg_gain_max then
  365.                 nvg_gain_current = nvg_gain_max
  366.             end
  367.             local snd_adjust = sound_object('interface\\inv_nvg')
  368.             snd_adjust:play(db.actor,0,sound_object.s2d)
  369.             update_nvgs()
  370.         end
  371.     end
  372. end
  373.  
  374. function brightness_down()
  375.     if item_device.nv_state then
  376.         local step_size = (nvg_gain_max - nvg_gain_min) / 5
  377.         if nvg_gain_current > nvg_gain_min then
  378.            
  379.             nvg_gain_current = nvg_gain_current - step_size
  380.        
  381.             if nvg_gain_current <= nvg_gain_min then
  382.                 nvg_gain_current = nvg_gain_min
  383.             end
  384.             local snd_adjust = sound_object('interface\\inv_nvg')
  385.             snd_adjust:play(db.actor,0,sound_object.s2d)
  386.             update_nvgs()
  387.         end
  388.     end
  389. end
  390.  
  391.  
  392. -----------------------------------------------------------------------------
  393. --      NVG STATE CHANGE EFFECT, MANIPULATES VIGNETTE AND LOCATION TO
  394. --          SIMUALATE FLIPPING DOWN AND TURNING ON NVGS
  395. -----------------------------------------------------------------------------
  396.  
  397. function state_change_effect()
  398.     -- Each frame should take no more than 100ms
  399.     local current_time = time_global()
  400.     local passed_iters = (current_time - last_animation_frame) / 100
  401.     if passed_iters == 0 then -- Less than 100ms from last frame, don't slow down animation
  402.         passed_iters = 1
  403.     end
  404.     for i = 1, passed_iters do
  405.    
  406.     if item_device.nv_state then
  407.         if not flip_latch then
  408.             if flip_down < 100 then
  409.                 flip_down = flip_down + flip_speed
  410.             end
  411.            
  412.             if flip_down >= 100 then
  413.                 flip_down = 100
  414.                 flip_latch = true
  415.             end
  416.         end
  417.        
  418.         if flip_latch and not crt_latch then
  419.             if vignette_current > vignette_amount then
  420.                 vignette_current = vignette_current / vignette_speed
  421.             end
  422.  
  423.             if vignette_current <= vignette_amount then
  424.                 vignette_current = vignette_amount
  425.                 crt_latch = true
  426.             end
  427.         end
  428.     else
  429.         if crt_latch then
  430.             vignette_current = 1.0
  431.             crt_latch = false
  432.         end
  433.        
  434.         if not crt_latch and flip_latch then
  435.             if flip_down > 1 then
  436.                 flip_down = flip_down - flip_speed
  437.             end
  438.            
  439.             if flip_down <= 1 then
  440.                 flip_down = 1
  441.                 flip_latch = false
  442.                 unregister_nvg_callbacks()
  443.                 nvg_generation = 0
  444.                 item_device.nv_discharged = false
  445.             end
  446.         end
  447.     end
  448.     if i == passed_iters then
  449.         last_animation_frame = time_global()
  450.     end
  451.    
  452.     end
  453.     if (item_device.nv_discharged) then
  454.         vignette_current = 1.0
  455.     end
  456. end
  457.  
  458. -----------------------------------------------------------------------------
  459. --      NVG GLITCH EFFECT, CALLED FROM ITEM_DEVICES AND GLITCHES NVG DURING
  460. --          PSY STORM AND BLOWOUT
  461. -----------------------------------------------------------------------------
  462.  
  463. -- nvg glitch effect during blowouts and psy storms
  464. function nvg_glitch(power)
  465.     if item_device.nv_state then
  466.         if glitch_power ~= power then
  467.             glitch_power = power
  468.             if not zoomed_in then
  469.                 update_nvgs()
  470.             end
  471.         end
  472.     end
  473. end
  474.  
  475. -----------------------------------------------------------------------------
  476. --      TURNS OFF NVGS WHEN ADS, AS WELL AS CHANGE TO SINGLE CENTERED SCOPE
  477. --          WHEN USING SHADER SCOPES TO MAINTAIN COMPATIBILITY
  478. -----------------------------------------------------------------------------
  479.  
  480. function actor_on_weapon_zoom_in()
  481.     zoom = true
  482.     if ((not nvg_view_through_scope) or axr_main.binoc_is_zoomed) and item_device.nv_state then
  483.             zoomed_in = true
  484.             get_console():execute("r__nightvision 0")
  485.             get_console():execute("shader_param_8 (0,0,0,0)")
  486.     elseif (shader_scope_compat and item_device.nv_state) then
  487.         local scopeName = nil
  488.         local wpn = db.actor:active_item()
  489.         local zoom_type = wpn:cast_Weapon():GetZoomType()
  490.         if wpn:weapon_scope_status() == 2 then
  491.             if wpn:weapon_is_scope() then
  492.                 local scope = utils_item.get_attached_scope(wpn)
  493.                 scopeName = utils_item.get_param(scope, wpn:id(), "scope_texture", "string")
  494.             end
  495.         else
  496.             scopeName = utils_item.get_param(wpn:section(), wpn:id(), "scope_texture", "string")
  497.         end
  498.         --local tex_active = get_console():get_integer("scope_2dtexactive")
  499.         if scopeName and scopeRadii.scopeRadii[scopeName] and zoom_type == 0 then
  500.             scope_blur_temp = get_console():get_float("scope_blur_inner")
  501.             tube_count_temp = nvg_num_tubes
  502.             radius_temp = nvg_radius
  503.             --vignette_temp = vignette_current
  504.             nvg_num_tubes = 1
  505.             get_console():execute("scope_blur_inner 0")
  506.             nvg_radius = math.min(scopeRadii.scopeRadii[scopeName],0.9)
  507.             --vignette_current = 0.5 - scopeRadii.scopeRadii[scopeName]
  508.             CreateTimeEvent("nvg_scope", "nvg_scope", 0.25, function()
  509.                 update_nvgs()
  510.                 return true
  511.             end)
  512.             shader_scope_flag = true
  513.         end
  514.     end
  515. end
  516.  
  517. -----------------------------------------------------------------------------
  518. --      TURN NVGS BACK ON AFTER THEY'VE BEEN TEMPORARILY DISABLED BY ADS FUNCTION
  519. -----------------------------------------------------------------------------
  520.  
  521. function actor_on_weapon_zoom_out()
  522.     zoom = false
  523.     if (not nvg_view_through_scope and item_device.nv_state and zoomed_in) then
  524.         zoomed_in = false
  525.         update_nvgs()
  526.     elseif (shader_scope_compat and item_device.nv_state and shader_scope_flag) then
  527.         --vignette_current = vignette_temp
  528.         nvg_num_tubes = tube_count_temp
  529.         --nvg_radius = radius_temp
  530.         nvg_radius = 0.55 -- hack to fix not correctly going back until i am better at this shit
  531.         get_console():execute("scope_blur_inner " .. scope_blur_temp)
  532.         RemoveTimeEvent("nvg_scope", "nvg_scope")
  533.         shader_scope_flag = false
  534.         update_nvgs()
  535.     end
  536. end
  537.  
  538. -----------------------------------------------------------------------------
  539. --      IF AUTO-TURN-OFF IS ENABLED, THEN AUTO DISABLE NVG WHEN BRING UP PDA
  540. -----------------------------------------------------------------------------
  541.  
  542. function pda_check(npc,info_id)
  543.     local actor = db.actor
  544.     if actor and item_device.nv_state then
  545.         if info_id == "ui_pda" and not nvg_stays_pda then
  546.             if not disable_nvg then
  547.                 get_console():execute("r__nightvision 0")
  548.                 get_console():execute("shader_param_8 (0,0,0,0)")
  549.                 disable_nvg = true
  550.             end
  551.         elseif info_id == "ui_pda_hide" and not nvg_stays_pda then
  552.             if disable_nvg then
  553.                 update_nvgs()
  554.                 disable_nvg = false
  555.             end
  556.         end
  557.     end
  558. end
  559.  
  560.  
  561. -----------------------------------------------------------------------------
  562. --      Actor on update Function - keep as minimal as possible
  563. -----------------------------------------------------------------------------
  564. local tmr = 0
  565. local tmr_step = 200
  566. function actor_on_update()
  567.  
  568.     if item_device.nv_discharged or ((not crt_latch or not flip_latch) and item_device.nv_state) or ((crt_latch or flip_latch) and not item_device.nv_state) then
  569.         state_change_effect()
  570.         update_nvgs()
  571.     end
  572.    
  573.     tg = time_global()
  574.     if tg < tmr then return end
  575.     tmr = tg + tmr_step
  576.  
  577.     if (not zoom) and (zoomed_in or shader_scope_flag) then
  578.         -- check for disable NVG while ADS
  579.         if (not nvg_view_through_scope and item_device.nv_state and zoomed_in) then
  580.             zoomed_in = false
  581.             update_nvgs()
  582.         elseif (shader_scope_compat and item_device.nv_state and shader_scope_flag) then
  583.             --vignette_current = vignette_temp
  584.             nvg_num_tubes = tube_count_temp
  585.             --nvg_radius = radius_temp
  586.             nvg_radius = 0.55 -- hack to fix not correctly going back until i am better at this shit
  587.             get_console():execute("scope_blur_inner " .. scope_blur_temp)
  588.             RemoveTimeEvent("nvg_scope", "nvg_scope")
  589.             shader_scope_flag = false
  590.             update_nvgs()
  591.         end
  592.     end
  593. end
  594.  
  595. -----------------------------------------------------------------------------
  596. --      Keybinding Functions
  597. -----------------------------------------------------------------------------
  598.  
  599. -- long press to turn on/off NVG, short press to change brightness
  600. function on_key_press()
  601.     return
  602. end
  603.  
  604. function on_key_release(key)
  605.     if key == bind_to_dik(key_bindings.kWPN_FUNC) and zoom == true and item_device.nv_state then
  606.         local scopeName = nil
  607.         local wpn = db.actor:active_item()
  608.         local zoom_type = wpn:cast_Weapon():GetZoomType()
  609.         if wpn:weapon_scope_status() == 2 then
  610.             if wpn:weapon_is_scope() then
  611.                 local scope = utils_item.get_attached_scope(wpn)
  612.                 scopeName = utils_item.get_param(scope, wpn:id(), "scope_texture", "string")
  613.             end
  614.         else
  615.             scopeName = utils_item.get_param(wpn:section(), wpn:id(), "scope_texture", "string")
  616.         end
  617.         if scopeName and scopeRadii.scopeRadii[scopeName] and zoom_type == 0 then
  618.             scope_blur_temp = get_console():get_float("scope_blur_inner")
  619.             tube_count_temp = nvg_num_tubes
  620.             radius_temp = nvg_radius
  621.             nvg_num_tubes = 1
  622.             get_console():execute("scope_blur_inner 0")
  623.             nvg_radius = math.min(scopeRadii.scopeRadii[scopeName],0.9)
  624.             update_nvgs()
  625.         elseif zoom_type == 1 or zoom_type == 2 then
  626.             if nvg_generation == 1 then
  627.                 nvg_num_tubes = nvg_num_tubes_gen1
  628.                 nvg_mode = nvg_mode_gen1
  629.             elseif nvg_generation == 2 then
  630.                 nvg_num_tubes = nvg_num_tubes_gen2
  631.                 nvg_mode = nvg_mode_gen2
  632.             elseif nvg_generation == 3 then
  633.                 nvg_num_tubes = nvg_num_tubes_gen3
  634.                 nvg_mode = nvg_mode_gen3
  635.             end
  636.             nvg_radius = 0.55
  637.             update_nvgs()
  638.         end    
  639.     end
  640.     return
  641. end
  642.  
  643. -----------------------------------------------------------------------------
  644. --      RegisterScriptCallback Functions
  645. -----------------------------------------------------------------------------
  646.  
  647. local function actor_on_first_update()
  648.     normal_fov = device().fov * 0.7
  649.     mc_anm_time_1 = game.get_motion_length(mc_anm_sec, "anm_hide_hand", hide_hand_speed)
  650.     torch_anm_time = game.get_motion_length(torch_anm_sec, "anm_switch", 1)
  651.     update_nvgs()
  652. end
  653.  
  654. function on_game_start()
  655.     RegisterScriptCallback("on_option_change", on_option_change)
  656.     RegisterScriptCallback("on_key_press", on_key_press)
  657.     RegisterScriptCallback("on_key_release", on_key_release)
  658.     RegisterScriptCallback("actor_on_first_update", actor_on_first_update)
  659.     on_option_change()
  660. end
Advertisement
Add Comment
Please, Sign In to add comment