revolucas

disassemble

Feb 20th, 2017
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.42 KB | None | 0 0
  1. --[[
  2. Drag and drop disassemble script
  3. By Alundaio, who was pestered by Axebeard who doesn't know how to script
  4. --]]
  5. function on_game_start()
  6.     RegisterScriptCallback("CUIActorMenu_OnItemDropped",OnItemDropped)
  7. end
  8.  
  9. function OnItemDropped(itm_from,itm_to,slot_from,slot_to)
  10.     --printf("from=%s to=%s slot_from=%s slot_to=%s  (%s)",itm_from:name(),itm_to:name(),slot_from,slot_to,EDDListType.iActorBag)
  11.     -- only work if draggin item in actor's bag to another item in actor's bag
  12.     if not (slot_from == EDDListType.iActorBag and slot_to == EDDListType.iActorBag) then
  13.         return
  14.     end
  15.    
  16.     local itm_dis
  17.     local itm_craft_tool
  18.     local sys_ini = system_ini()
  19.    
  20.     if (sys_ini:r_bool_ex(itm_from:section(),"can_disassemble",false) == true) then
  21.         itm_craft_tool = itm_from
  22.         itm_dis = itm_to
  23.     elseif (sys_ini:r_bool_ex(itm_to:section(),"can_disassemble",false) == true) then
  24.         itm_craft_tool = itm_to
  25.         itm_dis = itm_from
  26.     end
  27.    
  28.     if not (itm_craft_tool) then
  29.         return -- item being dragged from or to is not craftitem
  30.     end
  31.    
  32.     local craft_tool_sec = itm_craft_tool:section()
  33.     local disassemble_categories = sys_ini:r_string_ex(craft_tool_sec,"disassemble_categories","")
  34.     if (disassemble_categories == "") then
  35.         return
  36.     end
  37.    
  38.     disassemble_categories = alun_utils.str_explode(disassemble_categories,",")
  39.     if (#disassemble_categories <= 0) then
  40.         return
  41.     end
  42.    
  43.     local itm_dis_sec = itm_dis:section()
  44.     local cond = itm_dis:condition()
  45.    
  46.     for _,dis_sec in ipairs(disassemble_categories) do
  47.         local result, sec, value = nil, nil, nil
  48.         local line_count = sys_ini:line_count(dis_sec)-1
  49.         for i=0,line_count do
  50.             result, sec, value = sys_ini:r_line(dis_sec,i,"","")
  51.             if (sec and sec ~= "" and sec ~= "random_parts" and sys_ini:section_exist(sec)) then  
  52.                 if (sec == itm_dis_sec) then
  53.                     --gives player Spare Parts item
  54.                     local random_parts = sys_ini:r_string_ex(dis_sec,"random_parts","spareparts")
  55.                     if (random_parts ~= "") then
  56.                        
  57.                         random_parts = alun_utils.str_explode(random_parts,",")
  58.                         if (#random_parts == 0) then
  59.                             table.insert(random_parts,"spareparts")
  60.                         end
  61.                        
  62.                         local amt = 1
  63.                         if (cond >= 0.6) then
  64.                             amt = 2
  65.                         elseif (cond >= 0.9) then
  66.                             amt = 3
  67.                         end
  68.                        
  69.                         for i=1,amt do
  70.                             alife():create(random_choice(unpack(random_parts)), db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
  71.                         end
  72.                        
  73.                         local se_obj = alife():object(itm_dis:id())
  74.                         if (se_obj) then
  75.                             alife():release(se_obj)
  76.                         end
  77.                         return
  78.                     end
  79.                 end
  80.             end
  81.         end
  82.     end
  83. end
  84.  
  85. --[[ EXAMPLE
  86.  
  87. [itm_hammer]
  88. can_disassemble = true
  89. disassemble_categories = dis_cat_1, dis_cat_2
  90.  
  91.  
  92. [dis_cat_1]
  93. wpn_pm
  94. wpn_ak74
  95. random_parts = spareparts, junk, junk2
  96.  
  97. [dis_cat_2]
  98. wpn_knife
  99. wpn_binoc
  100. random_parts = spareparts, junk, junk2
  101. --]]
Advertisement
Add Comment
Please, Sign In to add comment