Nuor

combine_helms.script

May 2nd, 2018
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. function on_game_start()
  2. RegisterScriptCallback("CUIActorMenu_OnItemDropped",combine_helmets)
  3. end
  4. --// ------------- hacky dynamic update install procedure ---------------------
  5. function delay(id)
  6. local obj = level.object_by_id(id)
  7. if obj then
  8. db.actor:transfer_item(obj,db.actor)
  9. return true
  10. end
  11. end
  12.  
  13. function install_upgrades(se_obj, upg)
  14. local UpgTbl = {upg}
  15. if type(upg) == "table" then
  16. UpgTbl = upg
  17. end
  18.  
  19. local data = se_obj and stpk_utils.get_object_data(se_obj)
  20. if data then
  21. local tt = data.upgrades
  22. for i=1, #UpgTbl do
  23. tt[#tt+1] = UpgTbl[i]
  24. end
  25. --alun_utils.print_table(tt,"upgrade table")
  26. data.upgrades = tt
  27. stpk_utils.set_object_data(data,se_obj)
  28. end
  29. alun_utils.switch_offline(se_obj.id)
  30. alun_utils.switch_online(se_obj.id)
  31. end
  32. --//-----------------------------------------------------------------------
  33. function combine_helmets(obj1, obj2, slot_from, slot_to)
  34. --// both items must be in actor ruck
  35. if not (slot_from == EDDListType.iActorBag and slot_to == EDDListType.iActorBag) then return end
  36. --// check that both are helms
  37. local helm_1, helm_2 = obj1:section(), obj2:section()
  38. if not(helm_1:find("helm_") and helm_2:find("helm_")) then return end
  39.  
  40. --// list combo recipes "helm section1", "helm section2", "new helm section"
  41. --// an upgrade scheme must be assigned in the new item section that uses all branches from the old items
  42. local helm_combo = {
  43. {"helm_hardhat", "helm_respirator", "helm_respirator_hardhat"}
  44. }
  45. local adds = {}
  46. local helmet_se_obj
  47. --// search table for match for two helms
  48. for i=1, #helm_combo do
  49. if (helm_combo[i][1] == helm_1) and (helm_combo[i][2] == helm_2) or (helm_combo[i][1] == helm_2) and (helm_combo[i][2] == helm_1) then --//match
  50. --// grab upgrades on both helms for combo helm
  51. local function itr(section)
  52. --local sect = sys_ini:r_string_ex(section, "section")
  53. adds[#adds+1] = section
  54. end
  55. obj1:iterate_installed_upgrades(itr)
  56. obj2:iterate_installed_upgrades(itr)
  57.  
  58. --// adjust update names to accommodate overlap and match new scheme
  59. local stub1, stub2, stub3 = helm_combo[i][1]:gsub("helm",""), helm_combo[i][2]:gsub("helm",""), helm_combo[i][3]:gsub("helm","")
  60. --printf("stubs :%s:%s:%s",stub1,stub2,stub3)
  61. for i=1,#adds do
  62. if adds[i]:find(stub1) then
  63. adds[i] = adds[i]:gsub(stub1,stub3)
  64. elseif adds[i]:find(stub2) then
  65. adds[i] = adds[i]:gsub(stub2,stub3)
  66. adds[i] = adds[i]:gsub("first","third")
  67. end
  68. end
  69. --alun_utils.print_table(adds,"pre upgrade table")
  70. --// create new helm - must not be in actor inventory to allow apply upgrades
  71. local pos = db.actor:position():sub(db.actor:direction():mul(0.5))
  72. helmet_se_obj = alife():create(helm_combo[i][3], pos, db.actor:level_vertex_id(), db.actor:game_vertex_id())
  73. break
  74. end
  75. end
  76.  
  77. if helmet_se_obj then
  78. if #adds > 0 then --// if updates then apply to new helm
  79. install_upgrades(helmet_se_obj, adds)--{up_gr_firstab_helm_respirator_hardhat, up_gr_seconab_helm_respirator_hardhat, up_gr_thirdab_helm_respirator_hardhat}
  80. end
  81. CreateTimeEvent(0,"combine_delay",0,delay,se_obj.id)
  82. --// remove old helms
  83. local se_obj = alife_object(obj1:id())
  84. if (se_obj) then
  85. alife():release(se_obj,true)
  86. end
  87. se_obj = alife_object(obj2:id())
  88. if (se_obj) then
  89. alife():release(se_obj,true)
  90. end
  91. end
  92.  
  93. end
Advertisement
Add Comment
Please, Sign In to add comment