Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----------------------------------------------------------------------------------------------------
- -- Glowstick Script made by Lucy c:
- ----------------------------------------------------------------------------------------------------
- local despawn_time = 120 -- Time (in seconds) to despawn a glowstick after it stopped emitting light
- local enable_bs_mm_flicker = false -- Will be overwritten by MCM setting if MCM is installed
- -- dph patch
- local throw_force = 8000 -- Will be overwritten by MCM setting if MCM is installed
- local throw_key = key_bindings.kUSE -- Will be overwritten by MCM setting if MCM is installed
- -- dph patch end
- ----------------------------------------------------------------------------------------------------
- local glowsticks = {}
- local press_begin = 0
- local last_det_state = 0
- local next_gs_upd = 0
- local throw_time_after = 0
- local active_gs_drain = 0
- function actor_on_hud_animation_end(item, section, motion, state, slot)
- if (string.find(section, "glowstick")) then
- if (motion == "anm_throw_stick") then
- place_glowstick(item, true)
- return
- end
- if (state == 1) then
- local speed = ((motion == "anm_show_fast" or "anm_zoom_show_fast") and 2 or 1)
- item:play_hud_motion(string.find(motion, "zoom") and "anm_zoom_show_real" or "anm_show_real", false, 0, speed, 0)
- end
- end
- end
- function actor_on_update()
- local actor = db.actor
- local det = actor:active_detector()
- if (det) then
- if (time_global() > next_gs_upd) then
- if (string.find(det:section(), "glowstick")) then
- if (det:condition() > 0.01) then
- local new_c = clamp(det:condition() - active_gs_drain, 0.01, 0.9999)
- det:set_condition(new_c)
- end
- end
- next_gs_upd = time_global() + 1000
- end
- local shift_pressed = (key_state(DIK_keys.DIK_LSHIFT) == 1) or (key_state(DIK_keys.DIK_RSHIFT) == 1)
- 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
- local rand = math.random(1,4)
- det:play_hud_motion("anm_throw_stick", true, 0, 4, 0)
- level.add_cam_effector("camera_effects\\switch\\strong"..rand..".anm", 13370, false, '')
- game.play_hud_anm("script\\gasmask.anm", 0, 0.6, 0.35, false)
- game.only_allow_movekeys(true)
- press_begin = 0
- return
- end
- if (det:get_state() ~= last_det_state) then
- last_det_state = det:get_state()
- active_gs_drain = ini_sys:r_float_ex(det:section(), "power_drain", 0.001)
- if (string.find(det:section(), "glowstick")) then
- if (det:get_state() == 1 and axr_main.weapon_is_zoomed) then
- det:switch_state(0)
- det:play_hud_motion("anm_zoom_show_real", false, 0, 1, 0)
- end
- elseif (string.find(det:section(), "flashlight")) then
- if (det:get_state() == 1 and not axr_main.weapon_is_zoomed) then
- det:play_hud_motion("anm_show", false, 1, 1, 0)
- end
- end
- end
- end
- end
- function place_glowstick(itm, throw)
- local actor = db.actor
- local section = itm:section()
- local pos = actor:bone_position(throw and "eye_left" or "bip01_l_hand")
- pos.y = pos.y + 0.1
- local obj = alife_create("lights_glowstick", pos, actor:level_vertex_id(), actor:game_vertex_id())
- if (not obj) then
- printf("!Couldn't spawn Glowstick!")
- return
- end
- local data = utils_stpk.get_lamp_data(obj)
- if (not data) then
- printf("!Couldn't get Glowstick data!")
- return
- end
- local con_d = itm:condition()
- alife_release_id(itm:id())
- local light_section = ini_sys:r_string_ex(section, "light_section", "glowstick_definition")
- local hud_section = ini_sys:r_string_ex(section, "hud", "device_glowstick_hud")
- data.visual_name = ini_sys:r_string_ex(section, "visual", "dynamics\\devices\\dev_glowstick\\dev_glowstick_glow")
- data.light_flags = 45 -- Change to 47 for shadows
- -- Volumetric light
- if (ini_sys:r_bool_ex(light_section, "volumetric", false)) then
- data.light_flags = data.light_flags + 64
- data.volumetric_quality = ini_sys:r_float_ex(light_section, "volumetric_quality", 0.5)
- data.volumetric_intensity = ini_sys:r_float_ex(light_section, "volumetric_intensity", 0.5)
- data.volumetric_distance = ini_sys:r_float_ex(light_section, "volumetric_distance", 0.5)
- end
- -- Color
- local color_s = ini_sys:r_string_ex(light_section, "color_r2", "0.0,0.4,0.3,0.8")
- local _s = str_explode(color_s,",")
- for i=1,#_s do
- _s[i] = clamp(round(tonumber(_s[i]) * 255), 0, 255)
- end
- local function lshift(x, by)
- return x * 2 ^ by
- end
- 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)
- data.main_brightness = con_d <= 0.01 and 0 or (_s[1]/255 + _s[2]/255 + _s[3]/255) / 3 + 1
- data.main_color_animator = ini_sys:r_string_ex(light_section, "color_animator", "empty")
- data.main_bone = ini_sys:r_string_ex(hud_section, "fire_bone", "light_bone_2")
- data.main_range = ini_sys:r_float_ex(light_section, "range_r2", 15)
- glowsticks[obj.id] = {section, con_d}
- utils_stpk.set_lamp_data(data, obj)
- local function applyForce(id,throw)
- local gl = level.object_by_id(id)
- if (not gl) then return false end
- local dir = device().cam_dir
- -- dph patch
- dir:mul(throw and throw_force or 100)
- -- dph patch end
- gl:get_physics_shell():apply_force(dir.x, dir.y, dir.z)
- return true
- end
- CreateTimeEvent("glowstick","throw",0,applyForce,obj.id,throw)
- if (throw) then
- local function hide_anm()
- game.play_hud_motion(1, "device_glowstick_hud_base", "anm_throw_hide", false, 2.5)
- equip_next(section)
- return true
- end
- CreateTimeEvent("glowstick","hide_anm",0.01,hide_anm)
- end
- end
- function actor_on_item_drop(itm)
- if (not itm or not alife_object(itm:id())) then return end
- if (not string.find(itm:section(),"glowstick")) then return end
- place_glowstick(itm, false)
- end
- function actor_on_first_update()
- for n,p in pairs(glowsticks) do
- if glowsticks[n][2] <= 0.01 then
- if (alife_object(n)) then
- alife_release_id(n)
- end
- glowsticks[n] = nil
- end
- end
- throw_time_after = game.get_motion_length("device_glowstick_hud_base", "anm_throw_hide", 2.5) / 1000
- if (ui_mcm) then
- enable_bs_mm_flicker = ui_mcm.get("glowsticks/enable_bs_mm_flicker")
- -- dph patch
- throw_force = ui_mcm.get("glowsticks/throw_force") * 1000
- local n = ui_mcm.get("glowsticks/throw_key")
- if n == "next_firemode" then throw_key = key_bindings.kWPN_FIREMODE_NEXT
- elseif n == "wpnfunc" then throw_key = key_bindings.kWPN_FUNC
- else throw_key = key_bindings.kUSE end
- -- dph patch end
- end
- end
- function equip_next(section)
- local actor = db.actor
- local next = actor:object(section)
- game.only_allow_movekeys(false)
- local function activate_detector()
- db.actor:show_detector()
- return true
- end
- if (next) then
- actor:move_to_slot(next,9)
- CreateTimeEvent("glowstick","draw_next",throw_time_after,activate_detector)
- else
- local function search(temp, item)
- if (item and string.find(item:section(), "glowstick") and ini_sys:r_string_ex(item:section(), "class", nil) == "D_FLALIT") then
- actor:move_to_slot(item,9)
- CreateTimeEvent("glowstick","draw_next",throw_time_after,activate_detector)
- return true
- end
- end
- actor:iterate_ruck(search)
- end
- return true
- end
- function on_key_press(key)
- local bind = dik_to_bind(key)
- -- dph patch
- if (bind == throw_key) then
- press_begin = time_global()
- end
- -- dph patch end
- if (bind == key_bindings.kUSE) then
- press_begin = time_global()
- local function iter(obj)
- if (obj and obj:section() == "lights_glowstick") then
- local pos2d = game.world2ui(obj:position())
- if (pos2d.x < 350 or pos2d.x > 750 or pos2d.y < 250 or pos2d.y > 500) then return false end
- if (not glowsticks[obj:id()]) then return false end
- if (not obj:get_hanging_lamp():is_on()) then return false end
- -- Compatibility with FeelFried's pickup animation :3
- SendScriptCallback("actor_on_item_take", obj)
- local section = glowsticks[obj:id()][1] or "device_glowstick"
- local con_d = glowsticks[obj:id()][2]
- alife_create_item(section, db.actor, {cond = con_d})
- glowsticks[obj:id()] = nil
- alife_release_id(obj:id())
- return true
- end
- end
- level.iterate_nearest(db.actor:position(), 2, iter)
- end
- end
- function on_key_release(key)
- -- dph patch
- if (bind == throw_key) then
- press_begin = 0
- end
- -- dph patch end
- local bind = dik_to_bind(key)
- if (bind == key_bindings.kUSE) then
- press_begin = 0
- end
- end
- function save_state(MDATA)
- MDATA.glowsticks = glowsticks
- end
- function load_state(MDATA)
- glowsticks = MDATA.glowsticks or {}
- end
- RegisterScriptCallback("actor_on_hud_animation_end", actor_on_hud_animation_end)
- RegisterScriptCallback("actor_on_item_drop", actor_on_item_drop)
- RegisterScriptCallback("actor_on_update", actor_on_update)
- RegisterScriptCallback("actor_on_first_update", actor_on_first_update)
- RegisterScriptCallback("on_key_press", on_key_press)
- RegisterScriptCallback("on_key_release", on_key_release)
- RegisterScriptCallback("save_state", save_state)
- RegisterScriptCallback("load_state", load_state)
- ----------------------------------------------------------------------------------------------------
- -- Glowstick binder
- ----------------------------------------------------------------------------------------------------
- function init(obj)
- obj:bind_object(glowstick_binder(obj))
- end
- ----------------------------------------------------------------------------------------------------
- class "glowstick_binder" (object_binder)
- function glowstick_binder:__init(obj) super(obj)
- self.first_update = true
- self.next_update = time_global()
- end
- function glowstick_binder:update(delta)
- if (self.first_update) then
- local con_d = glowsticks[self.object:id()] and glowsticks[self.object:id()][2] or 0.01
- if (con_d > 0.01) then
- self.object:get_hanging_lamp():turn_on()
- self.object:set_tip_text("inventory_item_use")
- end
- 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)
- self.first_update = nil
- end
- if (time_global() > self.next_update ) then
- if (not glowsticks[self.object:id()]) then return end
- local con_d = glowsticks[self.object:id()][2]
- if (con_d > 0.01) then
- local new_c = clamp(con_d - self.power_drain, 0.01, 0.9999)
- glowsticks[self.object:id()][2] = new_c
- if (not self.object:get_hanging_lamp():is_on()) then
- self.object:set_tip_text_default()
- self.next_update = time_global() + 1000 * despawn_time
- con_d = 0.01
- return
- end
- elseif (self.object:get_hanging_lamp():is_on()) then
- self.object:get_hanging_lamp():turn_off()
- self.object:set_tip_text_default()
- self.next_update = time_global() + 1000 * despawn_time
- return
- else
- glowsticks[self.object:id()] = nil
- alife_release_id(self.object:id())
- end
- self.next_update = time_global() + 1000
- end
- end
- function glowstick_binder:net_destroy()
- glowsticks[self.object:id()] = nil
- end
- ----------------------------------------------------------------------------------------------------
- function glowstick_binder:net_save_relevant()
- return true
- end
- function glowstick_binder:save(packet)
- end
- function glowstick_binder:load(reader)
- end
- function glowstick_binder:reload(section)
- end
- function glowstick_binder:reinit()
- end
- function glowstick_binder:net_spawn(data)
- end
- ----------------------------------------------------------------------------------------------------
- -- Mod Configuration Menu
- ----------------------------------------------------------------------------------------------------
- function on_mcm_load()
- options =
- {
- id = "glowsticks", sh = true ,gr=
- {
- { id = "title", type = "slide", link = "Glowstick_Banner.dds", size = {706,50}, spacing = 20 },
- { id = "enable_bs_mm_flicker", type = "check", val = 1, def = false },
- -- dph patch
- { id="throw_force", type="track", val=2, min=5, max=50, step=1, def=8 },
- { id="throw_key", type="list", val=0, def="use", content={
- {"use","glowsticks_throw_key_use"},
- {"next_firemode","glowsticks_throw_key_next_firemode"},
- {"wpnfunc","glowsticks_throw_key_wpnfunc"}}
- },
- -- dph patch end
- }
- }
- return options
- end
- function on_option_change()
- if (ui_mcm) then
- enable_bs_mm_flicker = ui_mcm.get("glowsticks/enable_bs_mm_flicker")
- -- dph patch
- throw_force = ui_mcm.get("glowsticks/throw_force") * 1000
- local n = ui_mcm.get("glowsticks/throw_key")
- if n == "next_firemode" then throw_key = key_bindings.kWPN_FIREMODE_NEXT
- elseif n == "wpnfunc" then throw_key = key_bindings.kWPN_FUNC
- else throw_key = key_bindings.kUSE end
- -- dph patch end
- end
- end
- RegisterScriptCallback("on_option_change",on_option_change)
- ----------------------------------------------------------------------------------------------------
- -- Miracle Machine + Brain Scorcher lights flicker (optional)
- ----------------------------------------------------------------------------------------------------
- base_get_light_flicker = level_environment.get_light_flicker()
- function level_environment.get_light_flicker()
- if (enable_bs_mm_flicker) then
- local levelname = level.present() and level.name() or nil
- -- Miracle Machine
- if (levelname == "l08u_brainlab" and not has_alife_info("yan_labx16_switcher_primary_off")) then
- return true
- -- Brain Scorcher
- elseif (levelname == "l10u_bunker" and not has_alife_info("bar_deactivate_radar_done")) then
- return true
- end
- end
- return base_get_light_flicker
- end
Advertisement
Add Comment
Please, Sign In to add comment