-- Changed for IMM by Faustle (2018) -- Edited by Tronex -- Date: 2018/11/7 -- Sleep deprivation -- Updated: 2021/14/03 -- By: TKCrits -- Addon: Immersive Sleep v1.0.1b local last_sleep, last_sleep_chk, pwr_chk local feature_state local up_sleep = 27 -- step local in_hour = 780 -- amount decreased every sleep hour local start_blur_1 = 5000 -- value at which gray indicator appear local start_blur_2 = 6250 -- value at which yellow indicator appear local start_blur_3 = 7500 -- value at which orange indicator appear local start_blur_4 = 8750 -- value at which red indicator appear local force = 9000 -- value at which player is forced to sleep local force_slp = 10001 local sleep_chk = 300 -- amount of in-game seconds where indicator value increase local itn_mul = 0.001 local eat_sleepiness_mul = 1000 local pwr_rate,rng_factor = -0.00010, 0.08 -- Default 27. Runs 192 times for 16 hours. Adding up to 5184. local up_sleep = 27 -- step -- Default 1400. 625 will be for perfect 8 hour sleep, -- 510 = 4080, because the zone is not the most comfortable. -- Food also increases sleepiness. -- Should look into how bedrolls can improve sleep local in_hour = 510 -- amount decreased every sleep hour local start_blur_1 = 5000 -- value at which gray indicator appear local start_blur_2 = 6250 -- value at which yellow indicator appear local start_blur_3 = 7500 -- value at which orange indicator appear local start_blur_4 = 8750 -- value at which red indicator appear local force = 9000 -- value at which player is forced to sleep local force_slp = 10001 local sleep_chk = 300 -- amount of in-game seconds where indicator value increase local itn_mul = 0.001 local eat_sleepiness_mul = 1000 local pwr_rate,rng_factor = -0.00010, 0.08 local comfy = 0 function create() printdbg("- Sleep deprivation | Enabled") feature_state = true last_sleep = last_sleep or 0 RegisterScriptCallback("actor_on_update",actor_on_update) RegisterScriptCallback("actor_on_item_use",actor_on_item_use) RegisterScriptCallback("actor_on_sleep",actor_on_sleep) actor_status.add_indicator("Sleep",{ index= 4, typ= "state", functor= {"actor_status_sleep","get_sleep_deprivation",true}, icon= "ui_inGame2_indicator_sleep", background= "ui_inGame2_indicator_slot", anim_icon= false, anim_bk= false, }) end function destroy() printdbg("- Sleep deprivation | Disabled") feature_state = false last_sleep = nil UnregisterScriptCallback("actor_on_update",actor_on_update) UnregisterScriptCallback("actor_on_item_use",actor_on_item_use) UnregisterScriptCallback("actor_on_sleep",actor_on_sleep) alife_storage_manager.get_state().sleep = nil actor_status.add_indicator("Sleep",nil) end function toggle_feature(val) if val and (not feature_state) then create() elseif (not val) and feature_state then destroy() end end function test_blur() level.remove_pp_effector(39568) if last_sleep >= start_blur_1 then level.add_pp_effector("yantar_underground_psi.ppe", 39568, false) if last_sleep >= start_blur_1 and last_sleep < start_blur_2 then level.remove_pp_effector(39568) -- level.set_pp_effector_factor(39568, (last_sleep-start_blur_1) * itn_mul) elseif last_sleep >= start_blur_2 and last_sleep < start_blur_3 then --level.set_pp_effector_factor(39568, (last_sleep-start_blur_1) * itn_mul) -- actor_menu.set_msg(1, game.translate_string("You have not had a sleep, you should sleep"),3) elseif last_sleep >= start_blur_3 and last_sleep < start_blur_4 then level.set_pp_effector_factor(39568, (last_sleep-start_blur_1) * itn_mul) -- actor_menu.set_msg(1, game.translate_string("You have not had a sleep, you should sleep"),5) elseif last_sleep >= start_blur_4 and last_sleep < force then level.set_pp_effector_factor(39568, (last_sleep-start_blur_1) * itn_mul) elseif last_sleep >= force and math.random(100) <= force_slp and not db.actor:has_info("actor_is_sleeping")==true then db.actor:give_info_portion("force_slp2") force_sleep() -- actor_menu.set_msg(1, game.translate_string("You are extremely depleted - you need to sleep!"),5) end end end function force_sleep() ui_sleep_dialog.sleep_forced() actor_menu.set_msg(1, game.translate_string("st_sleep_deprived"),5) disable_info("force_slp2") end function get_sleep_deprivation(visual) if (not last_sleep) then return 0 end if visual then -- indicator if last_sleep <= start_blur_1 then return 0 elseif last_sleep <= start_blur_2 then return 1 elseif last_sleep <= start_blur_3 then return 2 elseif last_sleep <= start_blur_4 then return 3 else return 4 end end return clamp( normalize(last_sleep, 0, 10000) , 0 , 1) end function get_last_sleep() return last_sleep or 0 end function get_sleep_blur_4() return start_blur_4 or 0 end -- Immersive Sleep function get_last_sleep() return last_sleep or 0 end function get_sleep_deprivation_level() local sleep_level = 0 if (not last_sleep) then return 0 end if last_sleep > start_blur_1 then sleep_level = 1 elseif last_sleep > start_blur_2 then sleep_level = 2 elseif last_sleep > start_blur_3 then sleep_level = 3 elseif last_sleep > start_blur_4 then sleep_level = 4 end return sleep_level end function set_rest_per_hour(rest) in_hour = rest end ------------------------------------------------------------------- -- Callbacks ------------------------------------------------------------------- function save_state(m_data) if (USE_MARSHAL) and feature_state then local sleep = {} sleep.last_sleep = last_sleep sleep.chk_sleep = last_sleep_chk and utils_data.CTime_to_table(last_sleep_chk) m_data.sleep = sleep printdbg("# SAVING: Sleep deprivation | last_sleep: %s",tostring(last_sleep)) end end function load_state(m_data) local sleep = m_data.sleep if sleep then last_sleep = sleep.last_sleep or 0 last_sleep_chk = sleep.chk_sleep and utils_data.CTime_from_table(sleep.chk_sleep) or nil printdbg("# LOADING: Sleep deprivation | last_sleep: %s",tostring(last_sleep)) end end function actor_on_sleep(hours) printdbg("# Immersive Sleep | Rest per hour: %s",tostring(in_hour)) if (not db.actor:has_info("force_slp2")) then printdbg("/ Sleep deprivation | Reduced sleepiness by sleeping: %s - old sleepiness level: %s", hours*(in_hour/2), last_sleep) last_sleep = last_sleep and math.ceil(last_sleep - hours*((in_hour or 510)/2)) or 0 -- Default 1400, changed to 510 if (last_sleep < 0) then last_sleep = 0 elseif (last_sleep > 10000) then last_sleep = 10000 end end end function actor_on_item_use(item) local sec = item:section() local eat_sleepiness = (ini_sys:r_float_ex(sec,"eat_sleepiness") or 0) * eat_sleepiness_mul if eat_sleepiness and (eat_sleepiness ~= 0) then printdbg("/ Sleep deprivation | item used: %s - old sleepiness level: %s - sleepiness level change: %s", sec, last_sleep, eat_sleepiness) last_sleep = last_sleep + eat_sleepiness if (last_sleep < 0) then last_sleep = 0 elseif (last_sleep > 10000) then last_sleep = 10000 end test_blur() end end function actor_on_update() --printf("last_sleep_chk=%s last_sleep=%s",last_sleep_chk and game.get_game_time():diffSec(last_sleep_chk) or "nil",last_sleep) -- God mode console command enabled. if (get_console_cmd(1,"g_god")) then return true end local curr_time = game.get_game_time() if last_sleep > start_blur_2 then local tg = time_global() if (pwr_chk == nil or tg > pwr_chk) then if (db.actor.power > 0) then db.actor:change_power(pwr_rate*((last_sleep-start_blur_2)*rng_factor)/10) --db.actor:change_health(pwr_rate*((last_sleep-start_blur_2)*rng_factor)/100) end pwr_chk = tg + 100 end end if (last_sleep_chk and curr_time:diffSec(last_sleep_chk) < sleep_chk) then return end last_sleep_chk = curr_time -- log(" !The usual increase in sleep is 6. Sleep before: " .. "last_sleep) last_sleep = last_sleep + up_sleep if (last_sleep < 0) then last_sleep = 0 elseif (last_sleep > 10000) then last_sleep = 10000 end test_blur() end function on_game_start() local function on_game_load() if game_difficulties.get_game_factor("sleep") then create() end end --RegisterScriptCallback("on_game_load",on_game_load) RegisterScriptCallback("load_state",load_state) RegisterScriptCallback("save_state",save_state) end