Guest User

disguise system extended

a guest
May 25th, 2018
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.57 KB | None | 0 0
  1. --the faction you will be recognized as when discovered
  2. local default_faction -- = "stalker"
  3. --list of possibe factions
  4. -- bandit
  5. -- dolg     (duty)
  6. -- ecolog
  7. -- freedom
  8. -- killer   (mercs)
  9. -- army
  10. -- monolith
  11. -- stalker      
  12. -- csky
  13.  
  14. --time you need to be hidden (not seen by any npc) before changing outfit to make the disguise effective, 100 means roughly 10 seconds
  15. local time_to_forget = 100
  16.  
  17. --minimum condition for an outfit to work as disguise
  18. --set to 0 to not consider outfit condition, if you want only mint condition outfits to work set to 0.99 and not 1 because condition can never be exactly 1
  19. local min_condition = 0.25
  20.  
  21. --minimum condition for an helmet to reduce suspicion buildup
  22. --set to 0 to not consider helmet condition, if you want only mint condition helmets to work set to 0.99 and not 1 because condition can never be exactly 1
  23. local min_condition_h = 0.25
  24.  
  25. --distance within npc will start doubting the disguise (if they are seeing you)
  26. --5 = 1 wood box width
  27. local discovery_distance = 25
  28.  
  29. --how much time it takes to be discovered when in view, 100 is roughly 1 minute with uncovered face
  30. local suspicion_limit = 100
  31.  
  32. function on_game_start()
  33.     RegisterScriptCallback("actor_on_update",update)
  34.     RegisterScriptCallback("npc_on_update",npc_update)
  35.    
  36.     RegisterScriptCallback("on_game_load",on_game_load)
  37.     RegisterScriptCallback("save_state",save_state)
  38.     RegisterScriptCallback("load_state",load_state)
  39. end
  40.  
  41. local last_seen = {}
  42. local suspicion = {}
  43. local callout
  44.  
  45. function npc_update(npc)
  46.     if not default_faction then return end
  47.  
  48.     local current = character_community(db.actor):sub(7)
  49.     if current ~= default_faction and npc:see(db.actor) then
  50.         local id = npc:id()
  51.         last_seen[id] = game.get_game_time()
  52.        
  53.         if suspicion[id] then
  54.             local distance = npc:position():distance_to_sqr(db.actor:position())
  55.             suspicion[id] = suspicion[id] + suspicion_increase(distance)
  56.            
  57.             if (suspicion[id] > suspicion_limit) then
  58.                 db.actor:set_character_community( ("actor_" .. default_faction), 0, 0 )
  59.                 if not callout then
  60.                     news_manager.send_tip(db.actor, "You have been discovered! Everybody knows you are a " .. game.translate_string(default_faction) .." member in disguise.", nil, nil, 5000)
  61.                     --printf("discovered at %s",os.date("%X"))
  62.                 end
  63.                 callout = true
  64.             end
  65.         else
  66.             --printf("saw at %s",os.date("%X"))
  67.             suspicion[id] = 1
  68.         end
  69.     else
  70.         local id = npc:id()
  71.         if suspicion[id] then
  72.             suspicion[id] = suspicion[id] - 1
  73.             if (suspicion[id] <= 0) then
  74.                 suspicion[id] = nil
  75.             end
  76.         end
  77.     end
  78. end
  79.  
  80. function suspicion_increase(distance)
  81.  
  82.     local increase = 1
  83.    
  84.     if(distance > discovery_distance) then
  85.         increase = 0
  86.     else
  87.         local helm = db.actor:item_in_slot(12)
  88.         local outfit = db.actor:item_in_slot(7)
  89.        
  90.         local helm_cover = false
  91.        
  92.         if outfit then
  93.             local integrated_h = alun_utils.read_from_ini(nil,outfit:section(),"helmet_avaliable","bool",false)
  94.             if (not integrated_h) then
  95.                 if helm then
  96.                     local cond_h = helm:condition()
  97.                     if (cond_h > min_condition_h) then
  98.                         helm_cover = true
  99.                     end
  100.                 end
  101.             else
  102.                 helm_cover = true
  103.             end
  104.         else
  105.             if helm then
  106.                 local cond_h = helm:condition()
  107.                 if (cond_h > min_condition_h) then
  108.                     helm_cover = true
  109.                 end
  110.             end
  111.         end
  112.        
  113.         increase = helm_cover and 0.5 or 1
  114.     end
  115.    
  116.     return increase
  117. end
  118.  
  119. local oldfit
  120. function update()
  121.     if not default_faction then return end
  122.  
  123.     local outfit = db.actor:item_in_slot(7)
  124.    
  125.     local broken = outfit and outfit:condition() or 0
  126.    
  127.     broken = broken < min_condition
  128.    
  129.     outfit = outfit and outfit:section() or "naked"
  130.    
  131.     local new_comm
  132.    
  133.     if (oldfit and oldfit ~= outfit) then
  134.         if(outfit == "naked") then
  135.             new_comm = default_faction
  136.             change_to(new_comm,true)
  137.         else
  138.             new_comm = comm_from_outfit(outfit)
  139.             if new_comm == default_faction then
  140.                 change_to(new_comm,false)
  141.             elseif broken then
  142.                 local current = character_community(db.actor):sub(7)
  143.                 if current == default_faction then
  144.                     news_manager.send_tip(db.actor, "The outfit condition is too low, everybody knows you're a " .. game.translate_string(default_faction) .." member in disguise.", nil, nil, 5000)
  145.                 else
  146.                     news_manager.send_tip(db.actor, "The outfit condition is too low, everybody still assumes you're a " .. game.translate_string(current) .." member.", nil, nil, 5000)
  147.                 end
  148.             else
  149.                 change_to(new_comm,false)
  150.             end
  151.         end
  152.     end
  153.     oldfit = outfit
  154. end
  155.  
  156. function change_to(x,naked)
  157.     local current = character_community(db.actor):sub(7)
  158.  
  159.     if x == current then return end
  160.  
  161.     if x == default_faction then
  162.         news_manager.send_tip(db.actor, "You're now identified as a memeber of " .. game.translate_string(default_faction) .. " again.", nil, nil, 5000)
  163.        
  164.         db.actor:set_character_community( ("actor_" .. x), 0, 0 )
  165.         suspicion = {}
  166.         callout = true
  167.     elseif anybody_see() then
  168.         news_manager.send_tip(db.actor, "You have been seen changing outfit, everybody remembers you as a " .. game.translate_string(current) .." member.", nil, nil, 5000)
  169.     elseif anybody_remember() then
  170.         news_manager.send_tip(db.actor, "You have been seen recently, everybody remembers you as a " .. game.translate_string(current) .." member.", nil, nil, 5000)
  171.     else
  172.         if naked then
  173.             news_manager.send_tip(db.actor, "Without any outfit people will know you're a memeber of " .. game.translate_string(default_faction) .. ".", nil, nil, 5000)
  174.         else
  175.             news_manager.send_tip(db.actor, "With this outfit people will assume you're a memeber of " .. game.translate_string(x) .. ".", nil, nil, 5000)
  176.         end
  177.        
  178.         db.actor:set_character_community( ("actor_" .. x), 0, 0 )
  179.         suspicion = {}
  180.         callout = false
  181.     end
  182. end
  183.  
  184. function comm_from_outfit(section)
  185.     local new_comm = default_faction
  186.    
  187.     if(string.match(section,"army_") or string.match(section,"military_") or string.match(section,"specops_")) then
  188.         new_comm = "army"
  189.     end
  190.    
  191.     if(string.match(section,"bandit_") or string.match(section,"banditmerc_") or string.match(section,"trenchcoat_") or string.match(section,"renegade_")) then
  192.         new_comm = "bandit"
  193.     end
  194.    
  195.     if(string.match(section,"cs_") or string.match(section,"sky_")) then
  196.         new_comm = "csky"
  197.     end
  198.    
  199.     if(string.match(section,"freedom_") or string.match(section,"svoboda_")) then
  200.         new_comm = "freedom"
  201.     end
  202.    
  203.     if(string.match(section,"monolith_") or string.match(section,"monolit_")) then
  204.         new_comm = "monolith"
  205.     end
  206.    
  207.     if(string.match(section,"ecolog_")) then
  208.         new_comm = "ecolog"
  209.     end
  210.    
  211.     if(string.match(section,"dolg_")) then
  212.         new_comm = "dolg"
  213.     end
  214.    
  215.     if(string.match(section,"merc_")) then
  216.         new_comm = "killer"
  217.     end
  218.    
  219.     return new_comm
  220. end
  221.  
  222. function update_default(faction)
  223.     default_faction = faction
  224.     suspicion = {}
  225.     callout = true
  226. end
  227.  
  228. function anybody_see()
  229.     for i=1, #db.OnlineStalkers do
  230.         npc = level.object_by_id(db.OnlineStalkers[i])
  231.         if (npc and IsStalker(npc,npc:clsid()) and npc:alive()) then
  232.             if(npc:see(db.actor)) then
  233.                 return true
  234.             end
  235.         end
  236.     end
  237.     return false
  238. end
  239.  
  240. function anybody_remember()
  241.     local time_passed
  242.     for k,v in pairs(last_seen) do
  243.         if(v)then
  244.             time_passed = game.get_game_time():diffSec(v)
  245.             if( time_passed < time_to_forget) then
  246.                 return true
  247.             else
  248.                 last_seen[k] = nil
  249.             end
  250.         end
  251.     end
  252.     return false
  253. end
  254.  
  255. function on_game_load()
  256.     --printf("DBG on_game_load")
  257.     local defaults = {
  258.         disguise_system = {
  259.             ds_default_faction = default_faction or character_community(db.actor):sub(7) or "stalker",
  260.             ds_last_seen = last_seen,
  261.             ds_suspicion = suspicion,
  262.             ds_callout = callout,
  263.         }
  264.     }
  265.     load_state(defaults)
  266.  
  267.     oldfit = db.actor:item_in_slot(7)
  268.  
  269.     --printf("DBG default " .. (default_faction or "?"))
  270. end
  271.  
  272. function save_state(data)
  273.     --printf("DBG save_state")
  274.     data.disguise_system = {}
  275.     data.disguise_system.ds_default_faction = default_faction
  276.     data.disguise_system.ds_callout = callout
  277.     data.disguise_system.ds_last_seen = last_seen
  278.     data.disguise_system.ds_suspicion = suspicion
  279.  
  280.     --printf("DBG default " .. (default_faction or "?"))
  281. end
  282.  
  283. function load_state(data)
  284.     --printf("DBG load_state")
  285.     if not data.disguise_system then return end
  286.     default_faction = data.disguise_system.ds_default_faction
  287.     callout = data.disguise_system.ds_callout
  288.  
  289.     local last_seen_load = data.disguise_system.ds_last_seen
  290.     last_seen = {}
  291.     for k,v in pairs(last_seen_load) do
  292.         if v then
  293.             last_seen[k] = v
  294.         end
  295.     end
  296.  
  297.     local suspicion_load = data.disguise_system.ds_suspicion
  298.     suspicion = {}
  299.     for k,v in pairs(suspicion_load) do
  300.         if v then
  301.             suspicion[k] = v
  302.         end
  303.     end
  304.  
  305.     --printf("DBG default " .. (default_faction or "?"))
  306. end
Advertisement
Add Comment
Please, Sign In to add comment