Guest User

zz_glowstick_mcm.script

a guest
May 17th, 2021
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.69 KB | None | 0 0
  1. ----------------------------------------------------------------------------------------------------
  2. -- Glowstick Script made by Lucy c:
  3. ----------------------------------------------------------------------------------------------------
  4. local despawn_time = 120 -- Time (in seconds) to despawn a glowstick after it stopped emitting light
  5. local enable_bs_mm_flicker = false -- Will be overwritten by MCM setting if MCM is installed
  6. -- dph patch
  7. local throw_force = 8000 -- Will be overwritten by MCM setting if MCM is installed
  8. local throw_key = key_bindings.kUSE  -- Will be overwritten by MCM setting if MCM is installed
  9. -- dph patch end
  10. ----------------------------------------------------------------------------------------------------
  11.  
  12. local glowsticks = {}
  13.  
  14. local press_begin = 0
  15. local last_det_state = 0
  16. local next_gs_upd = 0
  17. local throw_time_after = 0
  18. local active_gs_drain = 0
  19.  
  20. function actor_on_hud_animation_end(item, section, motion, state, slot)
  21.     if (string.find(section, "glowstick")) then
  22.    
  23.         if (motion == "anm_throw_stick") then
  24.             place_glowstick(item, true)
  25.             return
  26.         end
  27.    
  28.         if (state == 1) then
  29.             local speed = ((motion == "anm_show_fast" or "anm_zoom_show_fast") and 2 or 1)
  30.             item:play_hud_motion(string.find(motion, "zoom") and "anm_zoom_show_real" or "anm_show_real", false, 0, speed, 0)
  31.         end
  32.     end
  33. end
  34.  
  35. function actor_on_update()
  36.     local actor = db.actor
  37.     local det = actor:active_detector()
  38.    
  39.     if (det) then
  40.    
  41.         if (time_global() > next_gs_upd) then
  42.             if (string.find(det:section(), "glowstick")) then
  43.                 if (det:condition() > 0.01) then
  44.                     local new_c = clamp(det:condition() - active_gs_drain, 0.01, 0.9999)
  45.                     det:set_condition(new_c)
  46.                 end
  47.             end
  48.            
  49.             next_gs_upd = time_global() + 1000
  50.         end
  51.        
  52.         local shift_pressed = (key_state(DIK_keys.DIK_LSHIFT) == 1) or (key_state(DIK_keys.DIK_RSHIFT) == 1)
  53.        
  54.         if (press_begin ~= 0 and time_global() > press_begin + 250 and det:get_state() == 0 and not shift_pressed and string.find(det:section(), "glowstick")) then
  55.  
  56.             local rand = math.random(1,4)
  57.            
  58.             det:play_hud_motion("anm_throw_stick", true, 0, 4, 0)
  59.             level.add_cam_effector("camera_effects\\switch\\strong"..rand..".anm", 13370, false, '')
  60.             game.play_hud_anm("script\\gasmask.anm", 0, 0.6, 0.35, false)
  61.             game.only_allow_movekeys(true)
  62.             press_begin = 0
  63.            
  64.             return
  65.         end
  66.    
  67.         if (det:get_state() ~= last_det_state) then
  68.             last_det_state = det:get_state()
  69.             active_gs_drain = ini_sys:r_float_ex(det:section(), "power_drain", 0.001)
  70.        
  71.             if (string.find(det:section(), "glowstick")) then
  72.                 if (det:get_state() == 1 and axr_main.weapon_is_zoomed) then
  73.                     det:switch_state(0)
  74.                     det:play_hud_motion("anm_zoom_show_real", false, 0, 1, 0)
  75.                 end
  76.             elseif (string.find(det:section(), "flashlight")) then
  77.                 if (det:get_state() == 1 and not axr_main.weapon_is_zoomed) then
  78.                     det:play_hud_motion("anm_show", false, 1, 1, 0)
  79.                 end
  80.             end
  81.         end
  82.     end
  83. end
  84.  
  85. function place_glowstick(itm, throw)
  86.  
  87.     local actor = db.actor
  88.     local section = itm:section()
  89.     local pos = actor:bone_position(throw and "eye_left" or "bip01_l_hand")
  90.     pos.y = pos.y + 0.1
  91.     local obj = alife_create("lights_glowstick", pos, actor:level_vertex_id(), actor:game_vertex_id())
  92.    
  93.     if (not obj) then
  94.         printf("!Couldn't spawn Glowstick!")
  95.         return
  96.     end
  97.    
  98.     local data = utils_stpk.get_lamp_data(obj)
  99.    
  100.     if (not data) then
  101.         printf("!Couldn't get Glowstick data!")
  102.         return
  103.     end
  104.    
  105.     local con_d = itm:condition()
  106.    
  107.     alife_release_id(itm:id())
  108.    
  109.     local light_section = ini_sys:r_string_ex(section, "light_section", "glowstick_definition")
  110.     local hud_section = ini_sys:r_string_ex(section, "hud", "device_glowstick_hud")
  111.    
  112.     data.visual_name = ini_sys:r_string_ex(section, "visual", "dynamics\\devices\\dev_glowstick\\dev_glowstick_glow")
  113.     data.light_flags = 45 -- Change to 47 for shadows
  114.    
  115.     -- Volumetric light
  116.     if (ini_sys:r_bool_ex(light_section, "volumetric", false)) then
  117.         data.light_flags = data.light_flags + 64
  118.        
  119.         data.volumetric_quality = ini_sys:r_float_ex(light_section, "volumetric_quality", 0.5)
  120.         data.volumetric_intensity = ini_sys:r_float_ex(light_section, "volumetric_intensity", 0.5)
  121.         data.volumetric_distance = ini_sys:r_float_ex(light_section, "volumetric_distance", 0.5)
  122.     end
  123.    
  124.     -- Color
  125.     local color_s = ini_sys:r_string_ex(light_section, "color_r2", "0.0,0.4,0.3,0.8")
  126.     local _s = str_explode(color_s,",")
  127.    
  128.     for i=1,#_s do
  129.         _s[i] = clamp(round(tonumber(_s[i]) * 255), 0, 255)
  130.     end
  131.    
  132.     local function lshift(x, by)
  133.         return x * 2 ^ by
  134.     end
  135.    
  136.     data.main_color = lshift(bit_and(_s[4],0xff), 24) + lshift(bit_and(_s[1],0xff), 16) + lshift(bit_and(_s[2],0xff), 8) + bit_and(_s[3],0xff)
  137.     data.main_brightness = con_d <= 0.01 and 0 or (_s[1]/255 + _s[2]/255 + _s[3]/255) / 3 + 1
  138.    
  139.     data.main_color_animator = ini_sys:r_string_ex(light_section, "color_animator", "empty")
  140.     data.main_bone = ini_sys:r_string_ex(hud_section, "fire_bone", "light_bone_2")
  141.     data.main_range = ini_sys:r_float_ex(light_section, "range_r2", 15)
  142.    
  143.     glowsticks[obj.id] = {section, con_d}
  144.    
  145.     utils_stpk.set_lamp_data(data, obj)
  146.    
  147.     local function applyForce(id,throw)
  148.         local gl = level.object_by_id(id)
  149.         if (not gl) then return false end
  150.        
  151.         local dir = device().cam_dir
  152.         -- dph patch
  153.         dir:mul(throw and throw_force or 100)
  154.         -- dph patch end
  155.         gl:get_physics_shell():apply_force(dir.x, dir.y, dir.z)
  156.         return true
  157.     end
  158.    
  159.     CreateTimeEvent("glowstick","throw",0,applyForce,obj.id,throw)
  160.    
  161.     if (throw) then
  162.         local function hide_anm()
  163.             game.play_hud_motion(1, "device_glowstick_hud_base", "anm_throw_hide", false, 2.5)
  164.             equip_next(section)
  165.             return true
  166.         end
  167.        
  168.         CreateTimeEvent("glowstick","hide_anm",0.01,hide_anm)
  169.     end
  170. end
  171.  
  172. function actor_on_item_drop(itm)
  173.     if (not itm or not alife_object(itm:id())) then return end
  174.     if (not string.find(itm:section(),"glowstick")) then return end
  175.     place_glowstick(itm, false)
  176. end
  177.  
  178. function actor_on_first_update()
  179.    
  180.     for n,p in pairs(glowsticks) do
  181.         if glowsticks[n][2] <= 0.01 then
  182.             if (alife_object(n)) then
  183.                 alife_release_id(n)
  184.             end
  185.             glowsticks[n] = nil
  186.         end
  187.     end
  188.    
  189.     throw_time_after = game.get_motion_length("device_glowstick_hud_base", "anm_throw_hide", 2.5) / 1000
  190.    
  191.     if (ui_mcm) then
  192.         enable_bs_mm_flicker = ui_mcm.get("glowsticks/enable_bs_mm_flicker")
  193.         -- dph patch
  194.         throw_force = ui_mcm.get("glowsticks/throw_force") * 1000
  195.         local n  = ui_mcm.get("glowsticks/throw_key")
  196.         if n == "next_firemode" then throw_key = key_bindings.kWPN_FIREMODE_NEXT
  197.         elseif n == "wpnfunc" then throw_key = key_bindings.kWPN_FUNC
  198.         else throw_key = key_bindings.kUSE end
  199.         -- dph patch end
  200.     end
  201. end
  202.  
  203. function equip_next(section)
  204.     local actor = db.actor
  205.     local next = actor:object(section)
  206.    
  207.     game.only_allow_movekeys(false)
  208.    
  209.     local function activate_detector()
  210.         db.actor:show_detector()
  211.         return true
  212.     end
  213.    
  214.     if (next) then
  215.         actor:move_to_slot(next,9)
  216.         CreateTimeEvent("glowstick","draw_next",throw_time_after,activate_detector)
  217.     else
  218.         local function search(temp, item)
  219.             if (item and string.find(item:section(), "glowstick") and ini_sys:r_string_ex(item:section(), "class", nil) == "D_FLALIT") then
  220.                 actor:move_to_slot(item,9)
  221.                 CreateTimeEvent("glowstick","draw_next",throw_time_after,activate_detector)
  222.                 return true
  223.             end
  224.         end
  225.        
  226.         actor:iterate_ruck(search)
  227.     end
  228.    
  229.     return true
  230. end
  231.  
  232. function on_key_press(key)
  233.    
  234.     local bind = dik_to_bind(key)
  235.  
  236.     -- dph patch
  237.     if (bind == throw_key) then
  238.         press_begin = time_global()
  239.     end
  240.     -- dph patch end
  241.    
  242.     if (bind == key_bindings.kUSE) then
  243.         press_begin = time_global()
  244.        
  245.         local function iter(obj)
  246.             if (obj and obj:section() == "lights_glowstick") then
  247.                 local pos2d = game.world2ui(obj:position())
  248.                 if (pos2d.x < 350 or pos2d.x > 750 or pos2d.y < 250 or pos2d.y > 500) then return false end
  249.                 if (not glowsticks[obj:id()]) then return false end
  250.                 if (not obj:get_hanging_lamp():is_on()) then return false end
  251.                
  252.                 -- Compatibility with FeelFried's pickup animation :3
  253.                 SendScriptCallback("actor_on_item_take", obj)
  254.                
  255.                 local section = glowsticks[obj:id()][1] or "device_glowstick"
  256.                 local con_d = glowsticks[obj:id()][2]
  257.  
  258.                 alife_create_item(section, db.actor, {cond = con_d})
  259.                 glowsticks[obj:id()] = nil
  260.                 alife_release_id(obj:id())
  261.                 return true
  262.             end
  263.         end
  264.        
  265.         level.iterate_nearest(db.actor:position(), 2, iter)
  266.     end
  267. end
  268.  
  269. function on_key_release(key)
  270.  
  271.     -- dph patch
  272.     if (bind == throw_key) then
  273.         press_begin = 0
  274.     end
  275.     -- dph patch end
  276.  
  277.     local bind = dik_to_bind(key)
  278.    
  279.     if (bind == key_bindings.kUSE) then
  280.         press_begin = 0
  281.     end
  282. end
  283.  
  284. function save_state(MDATA)
  285.     MDATA.glowsticks = glowsticks
  286. end
  287.  
  288. function load_state(MDATA)
  289.     glowsticks = MDATA.glowsticks or {}
  290. end
  291.  
  292. RegisterScriptCallback("actor_on_hud_animation_end", actor_on_hud_animation_end)
  293. RegisterScriptCallback("actor_on_item_drop", actor_on_item_drop)
  294. RegisterScriptCallback("actor_on_update", actor_on_update)
  295. RegisterScriptCallback("actor_on_first_update", actor_on_first_update)
  296. RegisterScriptCallback("on_key_press", on_key_press)
  297. RegisterScriptCallback("on_key_release", on_key_release)
  298. RegisterScriptCallback("save_state", save_state)
  299. RegisterScriptCallback("load_state", load_state)
  300.  
  301. ----------------------------------------------------------------------------------------------------
  302. -- Glowstick binder
  303. ----------------------------------------------------------------------------------------------------
  304. function init(obj)
  305.     obj:bind_object(glowstick_binder(obj))
  306. end
  307.  
  308. ----------------------------------------------------------------------------------------------------
  309. class "glowstick_binder" (object_binder)
  310. function glowstick_binder:__init(obj) super(obj)
  311.    
  312.     self.first_update = true
  313.     self.next_update = time_global()
  314. end
  315.  
  316. function glowstick_binder:update(delta)
  317.    
  318.     if (self.first_update) then
  319.         local con_d = glowsticks[self.object:id()] and glowsticks[self.object:id()][2] or 0.01
  320.         if (con_d > 0.01) then
  321.             self.object:get_hanging_lamp():turn_on()
  322.             self.object:set_tip_text("inventory_item_use")
  323.         end
  324.        
  325.         self.power_drain = ini_sys:r_float_ex(glowsticks[self.object:id()] and glowsticks[self.object:id()][1] or "device_glowstick", "power_drain", 0.001)
  326.         self.first_update = nil
  327.     end
  328.    
  329.     if (time_global() > self.next_update ) then
  330.         if (not glowsticks[self.object:id()]) then return end
  331.        
  332.         local con_d = glowsticks[self.object:id()][2]
  333.        
  334.         if (con_d > 0.01) then
  335.             local new_c = clamp(con_d - self.power_drain, 0.01, 0.9999)
  336.             glowsticks[self.object:id()][2] = new_c
  337.            
  338.             if (not self.object:get_hanging_lamp():is_on()) then
  339.                 self.object:set_tip_text_default()
  340.                 self.next_update = time_global() + 1000 * despawn_time
  341.                 con_d = 0.01
  342.                 return
  343.             end
  344.         elseif (self.object:get_hanging_lamp():is_on()) then
  345.             self.object:get_hanging_lamp():turn_off()
  346.             self.object:set_tip_text_default()
  347.             self.next_update = time_global() + 1000 * despawn_time
  348.             return
  349.         else
  350.             glowsticks[self.object:id()] = nil
  351.             alife_release_id(self.object:id())
  352.         end
  353.        
  354.         self.next_update = time_global() + 1000
  355.     end
  356. end
  357.  
  358. function glowstick_binder:net_destroy()
  359.     glowsticks[self.object:id()] = nil
  360. end
  361.  
  362. ----------------------------------------------------------------------------------------------------
  363.  
  364. function glowstick_binder:net_save_relevant()
  365.     return true
  366. end
  367.  
  368. function glowstick_binder:save(packet)
  369. end
  370.  
  371. function glowstick_binder:load(reader)
  372. end
  373.  
  374. function glowstick_binder:reload(section)
  375. end
  376.  
  377. function glowstick_binder:reinit()
  378. end
  379.  
  380. function glowstick_binder:net_spawn(data)
  381. end
  382.  
  383. ----------------------------------------------------------------------------------------------------
  384. -- Mod Configuration Menu
  385. ----------------------------------------------------------------------------------------------------
  386.  
  387. function on_mcm_load()
  388.     options =
  389.     {
  390.         id = "glowsticks", sh = true ,gr=
  391.         {
  392.             { id = "title", type = "slide", link = "Glowstick_Banner.dds", size = {706,50}, spacing = 20 },
  393.             { id = "enable_bs_mm_flicker", type = "check", val = 1, def = false },
  394.             -- dph patch
  395.             { id="throw_force", type="track", val=2, min=5, max=50, step=1, def=8 },
  396.             { id="throw_key", type="list", val=0, def="use", content={
  397.                 {"use","glowsticks_throw_key_use"},
  398.                 {"next_firemode","glowsticks_throw_key_next_firemode"},
  399.                 {"wpnfunc","glowsticks_throw_key_wpnfunc"}}
  400.             },
  401.             -- dph patch end
  402.         }
  403.     }
  404.        
  405.     return options
  406. end
  407.  
  408. function on_option_change()
  409.     if (ui_mcm) then
  410.         enable_bs_mm_flicker = ui_mcm.get("glowsticks/enable_bs_mm_flicker")
  411.         -- dph patch
  412.         throw_force = ui_mcm.get("glowsticks/throw_force") * 1000
  413.         local n  = ui_mcm.get("glowsticks/throw_key")
  414.         if n == "next_firemode" then throw_key = key_bindings.kWPN_FIREMODE_NEXT
  415.         elseif n == "wpnfunc" then throw_key = key_bindings.kWPN_FUNC
  416.         else throw_key = key_bindings.kUSE end
  417.         -- dph patch end
  418.     end
  419. end
  420.  
  421. RegisterScriptCallback("on_option_change",on_option_change)
  422.  
  423. ----------------------------------------------------------------------------------------------------
  424. -- Miracle Machine + Brain Scorcher lights flicker (optional)
  425. ----------------------------------------------------------------------------------------------------
  426.  
  427. base_get_light_flicker = level_environment.get_light_flicker()
  428.  
  429. function level_environment.get_light_flicker()
  430.  
  431.     if (enable_bs_mm_flicker) then
  432.         local levelname = level.present() and level.name() or nil
  433.        
  434.         -- Miracle Machine
  435.         if (levelname == "l08u_brainlab" and not has_alife_info("yan_labx16_switcher_primary_off")) then
  436.             return true
  437.            
  438.         -- Brain Scorcher
  439.         elseif (levelname == "l10u_bunker" and not has_alife_info("bar_deactivate_radar_done")) then
  440.             return true
  441.         end
  442.     end
  443.  
  444.     return base_get_light_flicker
  445. end
  446.  
Advertisement
Add Comment
Please, Sign In to add comment