Advertisement
Nuor

fieldcraft_new.script

Nov 30th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.07 KB | None | 0 0
  1. ------------------------------------------------------------------------------------------
  2. ------------------------- A.R.E.A Craft based on Fieldcraf_v1 ----------------------------
  3. ------------------------------------------------------------------------------------------
  4. --Modified by Holodilnik
  5. local recipes, retain
  6. local function init_recipes()
  7. local ini = system_ini()
  8. ini:section_for_each(function(section)
  9. if ini:line_exist(section,"craft_ingredient1") and ini:line_exist(section,"craft_ingredient2") then
  10. local igr1 = alun_utils.parse_list(ini,section,"craft_ingredient1")
  11. local igr2 = alun_utils.parse_list(ini,section,"craft_ingredient2")
  12. local fail = ini:r_float_ex(section,"craft_fail")
  13. local multi = ini:r_float_ex(section,"craft_skill_multi")
  14. local amt = ini:r_float_ex(section,"craft_amt")
  15. local timer = ini:r_float_ex(section,"craft_time")
  16. local anim = ini:r_string_ex(section,"craft_anim") or "make_something"
  17. local skill = ini:r_string_ex(section,"craft_skill") or "craft"
  18. skill = skill.."_skill"
  19. recipes[section] = #igr1 > 0 and #igr2 > 0 and {ingredients = {igr1,igr2}, options = {fail,multi,amt,timer,anim,skill}}
  20. end
  21. end)
  22. --alun_utils.print_table(recipes, "recipes table")
  23. end
  24. local function activate_feature()
  25. --[RECIPES]--
  26. -- place recipes in table or item ltx, ltx entries have precedence over table data.
  27. recipes = {
  28. medkit_scientic = {ingredients = {{"medkit","medkit_army"},{"antirad"}}, options = {}},
  29. }
  30. --list non weapon items retained after crafting in retain table
  31. retain = {
  32. itm_gunsmith_toolkit = "keep",
  33. itm_outfit_toolkit = "keep",
  34. itm_repairkit_tier_1 = "keep",
  35. itm_repairkit_tier_2 = "keep",
  36. itm_repairkit_tier_3 = "keep"
  37. }
  38. init_recipes()
  39. RegisterScriptCallback("CUIActorMenu_OnItemDropped",OnItemDropped)
  40. end
  41.  
  42. local function deactivate_feature()
  43. recipes = nil
  44. retain = nil
  45. UnregisterScriptCallback("CUIActorMenu_OnItemDropped",OnItemDropped)
  46. end
  47.  
  48. local function opt_menu_on_init(menu)
  49. -- craft
  50. menu.gameplay_add_options["craft"] = {default=false, debug_only=false, typ="check",
  51. on_accept=function(handler,optMgr,ctrl)
  52. if (level.present()) then
  53. if (ctrl:GetCheck()) then
  54. activate_feature()
  55. else
  56. deactivate_feature()
  57. end
  58. end
  59. end
  60. }
  61. end
  62.  
  63. function on_game_start()
  64. RegisterScriptCallback("opt_menu_on_init",opt_menu_on_init)
  65. if (axr_main.config:r_value("mm_options","enable_craft",1,false) == false) then return end
  66. local function actor_on_first_update()
  67. activate_feature()
  68. end
  69. RegisterScriptCallback("actor_on_first_update",actor_on_first_update)
  70. end
  71.  
  72. function OnItemDropped(itm_from,itm_to,slot_from,slot_to)
  73. if not (slot_from == EDDListType.iActorBag and slot_to == EDDListType.iActorBag) then
  74. return
  75. end
  76.  
  77. local itm_to_sec = itm_to and itm_to:section()
  78. local itm_from_sec = itm_from and itm_from:section()
  79. local sys_ini = system_ini()
  80.  
  81. local tmp = (itm_to_sec == itm_from_sec)
  82. if tmp then
  83. local r_uses_to,r_uses_from = itm_to:get_remaining_uses(),itm_from:get_remaining_uses()
  84. local max_uses, t_uses = itm_to:get_max_uses(), r_uses_to + r_uses_from
  85. local n_uses_to = t_uses > max_uses and max_uses or t_uses
  86. local n_uses_from = t_uses > max_uses and (t_uses - max_uses) or 0
  87.  
  88. itm_to:set_remaining_uses(n_uses_to)
  89. if n_uses_from > 0 then
  90. itm_from:set_remaining_uses(n_uses_from)
  91. else
  92. if (sys_ini:r_bool_ex(sec,"remove_after_use",true)) then
  93. local se_obj = alife_object(itm_from:id())
  94. if (se_obj) then
  95. alife():release(se_obj,true)
  96. end
  97. else
  98. itm_from:set_remaining_uses(0)
  99. end
  100. end
  101. return
  102. end
  103.  
  104. local function clear(item)
  105. local sec = item:section()
  106. local uses = sys_ini:r_bool_ex(sec,"use_condition",false) and item:get_remaining_uses() or 1
  107. if IsWeapon(item) or (retain[sec] == "keep") then
  108. return
  109. end
  110. --
  111. local next_items = sys_ini:line_exist(sec,"next_use_item") and alun_utils.parse_list(sys_ini,sec,"next_use_item") or {}
  112. if (#next_items > 0) and not (sec:find("_radiated")) then
  113. local se_obj = alife_object(item:id())
  114. if (se_obj) then
  115. alife():release(se_obj,true)
  116. end
  117. if next_items[#next_items] == "random" then
  118. alife():create(next_items[math.random(#next_items-1)], db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
  119. else
  120. for i=1, #next_items do
  121. alife():create(next_items[i], db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
  122. end
  123. end
  124. return
  125. end
  126. --
  127. if (sys_ini:r_bool_ex(sec,"remove_after_use",true) and (uses == 1)) or (sec:find("_radiated")) then
  128. local se_obj = alife_object(item:id())
  129. if (se_obj) then
  130. alife():release(se_obj,true)
  131. end
  132. elseif (uses >1) then
  133. item:set_remaining_uses(uses-1)
  134. end
  135. end
  136.  
  137. local new_item_sec = nil
  138. for k,v in pairs(recipes) do
  139. for i=1, #v.ingredients[1] do
  140. if (v.ingredients[1][i] == itm_to_sec) or (v.ingredients[1][i] == itm_from_sec) then
  141. for j=1, #v.ingredients[2] do
  142. if (v.ingredients[2][j] ~= v.ingredients[1][i]) and ((v.ingredients[2][j] == itm_to_sec) or (v.ingredients[2][j] == itm_from_sec)) then
  143. new_item_sec = k
  144. break
  145. end
  146. end
  147. end
  148. if new_item_sec == k then
  149. break
  150. end
  151. end
  152.  
  153. if new_item_sec then
  154. local fail = v.options[1] or 0
  155. local multi = v.options[2] or 1
  156. local amount = v.options[3] or 1
  157. local s_time = v.options[4]
  158. local anim = v.options[5] or "make_something"
  159. local skill = v.options[6] or "craft_skill"
  160. local craft_chance = area_skills and area_skills.skills_table[skill] and (50+(50*(area_skills.skills_table[skill]/area_skills.max_skills_level))) or 100
  161. if (craft_chance >= fail) then
  162. if actor_effects then actor_effects.use_item(anim) end
  163. if (s_time) then
  164. level.change_game_time(0,0,s_time)
  165. end
  166. if area_skills and area_skills.skills_table[skill] then
  167. area_skills.raise_skill(skill,multi)
  168. end
  169.  
  170. --// this may all be unnecessary
  171. local new_use = sys_ini:r_string_ex(new_item_sec,"max_uses") or 1
  172. local cond_frac = 1/new_use
  173. new_use = new_use - math.floor((itm_to:get_max_uses() - itm_to:get_remaining_uses() + itm_from:get_max_uses() - itm_from:get_remaining_uses())/2)
  174. new_use = new_use > 1 and new_use or 1
  175. local cond = cond_frac * new_use
  176. --//--
  177. --local cond = (itm_from:condition() + itm_to:condition())/2 --may not want to use this-- remaining charges isn't ideal either
  178.  
  179. for i = 1, amount do
  180. local se_obj = alife():create(new_item_sec, db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
  181. se_obj.condition = cond --may not want to use this--
  182. end
  183. else
  184. SetHudMsg(game.translate_string("st_craft_skill_fail"),3)
  185. if actor_effects then actor_effects.use_item("toolkit_p_dummy") end
  186. end
  187. clear(itm_from)
  188. clear(itm_to)
  189. break
  190. end
  191. end
  192. if not (new_item_sec) then
  193. printf("No recipes for %s and %s",itm_from_sec,itm_to_sec)
  194. return
  195. end
  196.  
  197. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement