Nuor

sleep_dep_test.script

Jun 20th, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.97 KB | None | 0 0
  1. --// ----------------- sleep deprivation for CoC ---------------------
  2. --// author : Nuor
  3. --// version: 1.23 (alt)
  4. --// created: 4-09-2016
  5. --// last edit: 4-24-2016
  6. --//------------------------------------------------------------------------------
  7. last_sleep, no_sleep, caff_chk = nil,nil,nil -- to change defaults, edit below in 'activate_feature' function
  8. local items,force,start_blur,itn_mul,pwr_rate,rng_factor,caff_max,last_warning,die_chance,last_chk,pwr_chk, _tmr
  9.  
  10. feature_is_active = nil
  11.  
  12. local function main_loop()
  13.  
  14. if not (db.actor) then
  15. return false
  16. end
  17.  
  18. local tg = time_global()
  19. --[[// drain power when exhausted
  20. if last_sleep > start_blur then
  21. if (pwr_chk == nil or tg > pwr_chk) then
  22. --local coeff = area_skills and 1+area_skills.skills_table.survival_skill/25 or 1
  23. if (db.actor.power > 0) then
  24. db.actor.power = pwr_rate*(1+((last_sleep-start_blur)*rng_factor))-- * coeff
  25. end
  26. pwr_chk = tg+100
  27. end
  28. end
  29. --]]
  30.  
  31. if (_tmr and tg < _tmr) then
  32. return false
  33. end
  34.  
  35. _tmr = tg + 2000
  36.  
  37. -- local cactor = db.actor:cast_Actor():conditions()
  38. -- local pwr = cactor and cactor[ "GetMaxPower" ] and cactor[ "GetMaxPower" ](cactor)
  39. -- printf("power:%s",pwr)
  40. --printf("last_chk=%s last_sleep=%s",last_chk and game.get_game_time():diffSec(last_chk) or "nil",last_sleep)
  41. if db.actor:has_info("actor_is_sleeping") then
  42. last_sleep = 0
  43. local cactor = db.actor:cast_Actor():conditions()
  44. if cactor and cactor[ "GetMaxPower" ] then
  45. cactor[ "SetMaxPower" ](cactor,1)
  46. end
  47. level.remove_pp_effector(1999)
  48. end
  49.  
  50. if (last_chk and game.get_game_time():diffSec(last_chk) < 3600) then
  51. return false
  52. end
  53.  
  54. last_chk = game.get_game_time()
  55.  
  56. last_sleep = last_sleep + 1
  57. caff_chk = caff_chk > 1 and caff_chk - 1 or 0
  58. test_blur()
  59.  
  60. return false
  61. end
  62.  
  63. function activate_feature()
  64. if (feature_is_active) then
  65. return
  66. end
  67. feature_is_active = true
  68.  
  69. items = {}
  70. local ini = ini_file("plugins\\sleep_deprivation.ltx")
  71. if (ini and ini:section_exist("items")) then
  72. local line_count = ini:line_count("items") or 0
  73. for i = 0, line_count - 1 do
  74. local junk1, section, value = ini:r_line("items", i, "", "")
  75. items[section] = value
  76. end
  77. end
  78.  
  79. last_sleep = last_sleep or 0
  80. no_sleep = 1 --// change "no_sleep" to 0 to allow sleep spam
  81. caff_chk = caff_chk or 0
  82.  
  83. force = 60 --// time between sleeps that triggers passing out
  84. start_blur = 20 --// time between sleeps when vision blur starts
  85. itn_mul = 0.05 --// blur intensity multiplier
  86. pwr_rate,rng_factor = -0.00065, 0.08 --// power reduction rate ,high/low range multiplier - lower is flatter
  87. caff_max = 3 --// max energy drinks per hr
  88. last_warning = 3 --// # of hourly warnings before passing out
  89. die_chance = 0.01 --// chance of dying if actor passes out
  90. sleep_penalty = 0.01 --// reduction in max power for every hour of exhaustion (start_blur >).
  91.  
  92. RegisterScriptCallback("actor_on_item_use",actor_on_item_use)
  93. RegisterScriptCallback("save_state",save_state)
  94. AddUniqueCall(main_loop)
  95. end
  96.  
  97. function deactivate_feature()
  98. if not (feature_is_active) then
  99. return
  100. end
  101. feature_is_active = false
  102.  
  103. RemoveUniqueCall(main_loop)
  104. UnregisterScriptCallback("actor_on_item_use",actor_on_item_use)
  105. if (USE_MARSHAL) then
  106. UnregisterScriptCallback("save_state",save_state)
  107. UnregisterScriptCallback("load_state",load_state)
  108. end
  109. alife_storage_manager.get_state().sleep_deprived = nil
  110. end
  111.  
  112. function on_game_start()
  113.  
  114. if (axr_main.config:r_value("mm_options","enable_sleep_deprived",1,false) == false) then
  115. return
  116. end
  117.  
  118. local function actor_on_first_update()
  119. activate_feature()
  120. if last_sleep > start_blur then
  121. --local coeff = area_skills and 1+area_skills.skills_table.survival_skill/25 or 1
  122. level.add_pp_effector("yantar_underground_psi.ppe", 1999, true)
  123. level.set_pp_effector_factor(1999, (last_sleep-start_blur) * itn_mul) -- * coeff)
  124. if last_sleep >= (force - last_warning + 1) then
  125. SetHudMsg(game.translate_string("st_sleep_exhausted_final"),5)
  126. else
  127. SetHudMsg(game.translate_string("st_sleep_exhausted"),4)
  128. end
  129. local cactor = db.actor:cast_Actor():conditions()
  130. local pwr = cactor[ "GetMaxPower" ] and cactor[ "GetMaxPower" ](cactor)
  131. cactor[ "SetMaxPower" ](cactor,pwr - sleep_penalty*(last_sleep-start_blur))
  132. end
  133. end
  134.  
  135. RegisterScriptCallback("load_state",load_state)
  136. RegisterScriptCallback("actor_on_first_update",actor_on_first_update,true)
  137. end
  138.  
  139. ----// add to bind_stalker or bind_stalker_ext save/load if used. //--
  140. function save(package) -- sleep_dep.save -> actor_binder:save()
  141. if (USE_MARSHAL) then
  142. return
  143. end
  144. package:w_u8(last_sleep)
  145. package:w_u16(last_chk)
  146. package:w_u8(caff_chk)
  147. end
  148. function load(package) -- sleep_dep.load -> actor_binder:load()
  149. if (USE_MARSHAL) then
  150. return
  151. end
  152. last_sleep = package:r_u8() or 0
  153. last_chk = package:r_u16() or 0
  154. caff_chk = package:r_u8() or 0
  155. end
  156. --//--------------------------------------------------------------//--
  157. function save_state(m_data)
  158. if (USE_MARSHAL) then
  159. m_data.sleep_deprived = m_data.sleep_deprived or {}
  160. m_data.sleep_deprived.last_sleep = last_sleep
  161. m_data.sleep_deprived.caff = caff_chk
  162. m_data.sleep_deprived.last_chk = last_chk
  163. end
  164. end
  165.  
  166. function load_state(m_data)
  167. if (USE_MARSHAL and m_data.sleep_deprived) then
  168. last_sleep = m_data.sleep_deprived.last_sleep or 0
  169. -- printf("load state last_sleep [%s]", last_sleep)
  170. caff_chk = m_data.sleep_deprived.caff or 0
  171. last_chk = m_data.sleep_deprived.last_chk or game.get_game_time()
  172. m_data.sleep_deprived = nil
  173. end
  174. end
  175.  
  176. function actor_on_item_use(item)
  177. local section = item:section()
  178. local value = items and tonumber(items[section]) or 0
  179. if items[section] then
  180. if value < 0 then
  181. caff_chk = caff_chk + 1
  182. if caff_chk > caff_max then return end
  183. end
  184. last_sleep = last_sleep + value
  185. test_blur()
  186. end
  187. end
  188.  
  189. function test_blur()
  190. -- local coeff = area_skills and 1+area_skills.skills_table.survival_skill/25 or 1
  191. level.remove_pp_effector(1999)
  192. local cactor = db.actor:cast_Actor():conditions()
  193. if last_sleep > force then
  194. force_sleep()
  195. elseif last_sleep > start_blur then
  196. level.add_pp_effector("yantar_underground_psi.ppe", 1999, true)
  197. level.set_pp_effector_factor(1999, (last_sleep-start_blur) * itn_mul) -- * coeff)
  198. if last_sleep >= (force - last_warning + 1) then
  199. SetHudMsg(game.translate_string("st_sleep_exhausted_final"),5)
  200. else
  201. SetHudMsg(game.translate_string("st_sleep_exhausted"),4)
  202. end
  203. local pwr = cactor[ "GetMaxPower" ] and cactor[ "GetMaxPower" ](cactor)
  204. cactor[ "SetMaxPower" ](cactor,pwr - sleep_penalty)
  205. printf("max power:%s",cactor[ "GetMaxPower" ](cactor))
  206. else
  207. cactor[ "SetMaxPower" ](cactor,1)
  208. end
  209. end
  210.  
  211. function force_sleep()
  212. ui_sleep_dialog.sleep_forced()
  213. if (math.random(1,100)/100) < die_chance then
  214. SetHudMsg(game.translate_string("st_sleep_deprived"),10)
  215. db.actor:kill(db.actor)
  216. end
  217. end
Advertisement
Add Comment
Please, Sign In to add comment