Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.36 KB | None | 0 0
  1. cabinets = {}
  2. cabs_table = {}
  3.  
  4. --[[Arguments:
  5. *pos - position of clicked node;
  6. *cabinet_name - same value as well as one of cabinets.open();
  7. *cabinet_num - number of cabinet, in other words this is component of the cabinet. Number is string;
  8. *data - table that contains list of all its components with each component name key and its data (formspec tables).]]
  9. cabinets.put_data_into_cabinet = function (pos, cabinet_name, cabinet_num, data, formspec)
  10. local meta = minetest.get_meta(pos)
  11.  
  12. if type(data) == "table" and #data ~= 0 then
  13. meta:set_string(cabinet_name .. "_" .. cabinet_num, minetest.serialize(data))
  14. else
  15. return
  16. end
  17.  
  18. meta:set_string(formspec["name"], formspec["data"])
  19. return true
  20. end
  21.  
  22. --[[Arguments:
  23. *opener - player data;
  24. *pos - position of clicked node;
  25. *node_replace - itemstring, node that needed to be replaced to former.
  26. *cabinet_name - "generalized" name of all its components, that is kitchen_wooden_cabinet_1, kitchen_wooden_cabinet_2, kitchen_wooden_cabinet_3 are kitchen_wooden_cabinet indeed, they are just modified models;
  27. *clicked_button_name - name of button that was clicked;
  28. *formspec - table with next keys: name, data (formspec string itself);
  29. *sound_play - table that can keep ONLY two sound names that needed to be played during opening and closing. Keys are: first is "open", second is "close".]]
  30. cabinets.open = function (opener, pos, node_replace, cabinet_name, clicked_button_name, formspec, sound_play)
  31. local name = minetest.get_node(pos).name
  32. local meta = minetest.deserialize(minetest.get_meta(pos):get_string(name))
  33.  
  34. -- The lower loop is running departments of the node as kitchen_wooden_cabinet_1... then it compares clicked_button_name is equal to the button name in the department.
  35. for depart_num, depart_data in pairs(meta) do
  36. if meta[depart_num]["button"] == clicked_button_name then
  37. changed_part_data = meta
  38.  
  39. -- Onwards it determines the department is "opened" or "closed", if "closed" sets opposite to this mode in changed_depart_data, else returns nil.
  40. if meta[depart_num]["mode"] == "closed" then
  41. changed_part_data[depart_num]["mode"] = "opened"
  42. elseif meta[depart_num]["mode"] == "opened" then
  43. return
  44. end
  45.  
  46. -- Here the loop is running cabs_table[name] table where "name" is "general" node name of all its "departments", then it`s running each "department" and comparing modes with mode of "changed_depart_data".
  47. -- (To the comment above) comparing modes is needed to determine which registered part of general node contains departments with same modes.
  48. for node_name, node_data in pairs(cabs_table[cabinet_name][name]) do
  49. for depart_num2, depart_data2 in pairs(node_data) do
  50. if node_data[depart_num2]["mode"] == changed_part_data[depart_num2]["mode"] then
  51. -- For success it copies inventory list of the department and put to corresponding one of changed_
  52. changed_part_data[depart_num2]["inv_list"] = node_data[depart_num2]["inv_list"]
  53. else
  54. return
  55. end
  56.  
  57. end
  58.  
  59. end
  60. end
  61.  
  62.  
  63. end
  64.  
  65. minetest.remove_node(pos)
  66. minetest.set_node(pos, node_replace)
  67.  
  68. if sound_play and sound_play["open"] then
  69. minetest.sound_play(sound_play["open"])
  70. end
  71.  
  72. local new_meta = minetest.get_meta(pos)
  73. new_meta:set_string(formspec["name"], formspec["data"])
  74.  
  75. local inv = minetest.get_inventory({type = "node", name = pos})
  76. local lists = inv:get_lists()
  77.  
  78. for _, depart_data in ipairs(changed_part_data) do
  79. inv:set_list(depart_data["listname"], depart_data["inv_list"])
  80. if depart_data["mode"] == "opened" then
  81. inv:set_size(depart_data["listname"], depart_data["inv_size"])
  82. end
  83. end
  84.  
  85. minetest.show_formspec(opener:get_player_name(), node_replace, formspec[name])
  86. end
  87.  
  88.  
  89. cabinets.close = function (closer, pos, node_replace, cabinet_name, clicked_button_name, formspec, sound_play)
  90. local name = minetest.get_node(pos).name
  91. local meta = minetest.deserialize(minetest.get_meta(pos):get_string(name))
  92.  
  93. -- The lower loop is running departments of the node as kitchen_wooden_cabinet_1... then it compares clicked_button_name is equal to the button name in the department.
  94. for depart_num, depart_data in pairs(meta) do
  95. if meta[depart_num]["button"] == clicked_button_name then
  96. changed_part_data = meta
  97.  
  98. -- Onwards it determines the department is "opened" or "closed", if "closed" sets opposite to this mode in changed_depart_data, else returns nil.
  99. if meta[depart_num]["mode"] == "opened" then
  100. changed_part_data[depart_num]["mode"] = "closed"
  101. elseif meta[depart_num]["mode"] == "closed" then
  102. return
  103. end
  104.  
  105. -- Here the loop is running cabs_table[name] table where "name" is "general" node name of all its "departments", then it`s running each "department" and comparing modes with mode of "changed_depart_data".
  106. -- (To the comment above) comparing modes is needed to determine which registered part of general node contains departments with same modes.
  107. for node_name, node_data in pairs(cabs_table[cabinet_name][name]) do
  108. for depart_num2, depart_data2 in pairs(node_data) do
  109. if node_data[depart_num2]["mode"] == changed_part_data[depart_num2]["mode"] then
  110. -- For success it copies inventory list of the department and put to corresponding one of changed_
  111. changed_part_data[depart_num2]["inv_list"] = node_data[depart_num2]["inv_list"]
  112. else
  113. return
  114. end
  115.  
  116. end
  117.  
  118. end
  119. end
  120.  
  121.  
  122. end
  123.  
  124. minetest.remove_node(pos)
  125. minetest.set_node(pos, node_replace)
  126.  
  127. if sound_play and sound_play["close"] then
  128. minetest.sound_play(sound_play["close"])
  129. end
  130.  
  131. local new_meta = minetest.get_meta(pos)
  132. new_meta:set_string(formspec["name"], formspec["data"])
  133.  
  134. local inv = minetest.get_inventory({type = "node", name = pos})
  135. local lists = inv:get_lists()
  136.  
  137. for _, depart_data in ipairs(changed_part_data) do
  138. inv:set_list(depart_data["listname"], depart_data["inv_list"])
  139. if depart_data["mode"] == "opened" then
  140. inv:set_size(depart_data["listname"], depart_data["inv_size"])
  141. end
  142. end
  143.  
  144. minetest.show_formspec(closer:get_player_name(), node_replace, formspec[name])
  145. end
  146.  
  147.  
  148. cabinets.define_needed_cabinet = function (formname, nodename)
  149. for _, depart in ipairs(kit_wood_cabs[nodename]) do
  150. if depart["button"] == formname then
  151. modes = {}
  152. for _, depart2 in ipairs(kit_wood_cabs[name]) do
  153. modes[#modes+1] = depart2["mode"]
  154. end
  155. break
  156. end
  157. end
  158. for name, cab in pairs(kit_wood_cabs) do
  159. local mode_num = 0
  160. for name2, depart in ipairs(cab) do
  161. mode_num = mode_num + 1
  162. if modes[mode_num] == depart["mode"] then
  163. if modes[mode_num] == #modes then
  164. return name2
  165. end
  166. end
  167. end
  168. end
  169. end
  170.  
  171. cabinets.define_mode = function (formname, nodename)
  172. for _, depart in ipairs(kit_wood_cabs[nodename]) do
  173. if depart["button"] == formname then
  174. return depart["mode"]
  175. end
  176. end
  177. end
  178.  
  179. minetest.register_on_joinplayer(function (player)
  180. minetest.debug(dump(cabinets))
  181. end)
  182. -- Create a table with external table for each cabinet sort (depends to boxes). Inside each second field a list of boxes and their datas.
  183.  
  184. kit_wood_cabs = {
  185. ["kitchen_wooden_cabinet_1"] = {
  186. --form_size="",
  187. {mode="closed", button = "kwc1_1", img_button = "close_button.png", listname = "kwc1_1", inv_list={}},
  188. {mode="closed", button = "kwc1_2", img_button = "close_button.png", listname = "kwc1_2", inv_list={}}
  189.  
  190. },
  191. ["kitchen_wooden_cabinet_2"] = {
  192. --form_size="",
  193. {mode="opened", button = "kwc2_1", img_button = "open_button.png", listname = "kwc2_1", inv_list={}, inv_size=4*2},
  194. {mode="closed", button = "kwc2_2", img_button = "close_button.png", listname = "kwc2_2", inv_list={}},
  195. not_in_creative_inventory=1
  196.  
  197. },
  198. ["kitchen_wooden_cabinet_3"] = {
  199. --form_size="",
  200. {mode="closed", button = "kwc3_1", img_button = "close_button.png", listname = "kwc3_1", inv_list={}},
  201. {mode="opened", button = "kwc3_2", img_button = "open_button.png", listname = "kwc3_2", inv_list={}, inv_size=4*2},
  202. not_in_creative_inventory=1
  203.  
  204. },
  205. ["kitchen_wooden_cabinet_4"] = {
  206. --form_size="",
  207. {mode="opened", button = "kwc4_1", img_button = "open_button.png", listname = "kwc4_1", inv_list={}, inv_size=4*2},
  208. {mode="opened", button = "kwc4_2", img_button = "open_button.png", listname = "kwc4_2", inv_list={}, inv_size=4*2},
  209. not_in_creative_inventory=1
  210.  
  211. }
  212. }
  213. if not cabs_table["kitchen_wooden_cabinet"] then
  214. cabs_table["kitchen_wooden_cabinet"] = kit_wood_cabs
  215. end
  216.  
  217. -- The loop is running the table above and register each cabinet sort.
  218.  
  219. local cabinet_num = 0
  220. local form
  221. for cab, cab_boxes in pairs(kit_wood_cabs) do
  222. cabinet_num = cabinet_num + 1
  223. minetest.register_node("luxury_decor:"..cab, {
  224. description = "Kitchen Wooden Cabinet",
  225. visual_scale = 0.5,
  226. mesh = cab..".obj",
  227. tiles = {"wood_material.png"},
  228. paramtype = "light",
  229. paramtype2 = "facedir",
  230. groups = {choppy=3, not_in_creative_inventory = cab_boxes["not_in_creative_inventory"]},
  231. drawtype = "mesh",
  232. collision_box = {
  233. type = "fixed",
  234. fixed = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5}
  235. },
  236. selection_box = {
  237. type = "fixed",
  238. fixed = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5}
  239. },
  240. sounds = default.node_sound_wood_defaults(),
  241. on_construct = function (pos)
  242. local name = minetest.get_node(pos).name
  243. local img_button1 = "image_button[0.5, 0;1, 2;" .. cab_boxes[1].img_button ..";" .. cab_boxes[1].button .. "]"
  244. local img_button2 = "image_button[0.5, 0.3;1, 2;" .. cab_boxes[2].img_button .. ";" .. cab_boxes[2].button .. "]"
  245. local list1 = "list[nodemeta:".. pos.x .. "," .. pos.y .. "," .. pos.z .. ";".. cab_boxes[1].listname .. ";1.5, 0;4, 2]"
  246. local list2 = "list[nodemeta:".. pos.x .. "," .. pos.y .. "," .. pos.z .. ";".. cab_boxes[2].listname .. ";1.5, 0.3;4, 2]"
  247. form = "size[6,5]" .. img_button1 .. img_button2 .. list1 .. list2 .. "]"
  248. --minetest.debug(dump(cabinets))
  249. cabinets.put_data_into_cabinet(pos, "kitchen_wooden_cabinet", tostring(cabinet_num), cab_boxes, {name=name, data=form})
  250. local inv = minetest.get_inventory({type="node", pos=pos})
  251. inv:set_list(cab_boxes[1].listname, cab_boxes[1].inv_list)
  252. inv:set_list(cab_boxes[2].listname, cab_boxes[2].inv_list)
  253. inv:set_size(cab_boxes[1].listname, 4*2)
  254. inv:set_size(cab_boxes[2].listname, 4*2)
  255. -- Fills "form_size" of each cabinet with needed size and "form_data" of each box with datas to build formspec with lists.
  256. --[[for cab2, cab_boxes2 in pairs(kit_wood_cabs) do
  257. cab_boxes2[cab2][form_size] = "size[8,4]"
  258. for box_num, cab_data in ipairs(cab_boxes2[2]) do
  259. cab_data[box_num][form_data] = inv_elems[1] .. "0.2, 0.1;0.5, 2" .. inv_elems[2]
  260. if cab_data[
  261. end
  262. end]]
  263. end,
  264. on_rightclick = function (pos, node, clicker, itemstack, pointed_thing)
  265. minetest.show_formspec(clicker:get_player_name(), cab, form)
  266. end,
  267. on_receive_fields = function (pos, formname, fields, sender)
  268. local name = minetest.get_node(pos).name
  269. local generalized_name = string.match(name, '^._-')
  270.  
  271. --[[for _, depart in ipairs(kit_wood_cabs[name]) do
  272. if depart[button] == formname then
  273. if depart[mode] == "closed" then
  274. for _, depart2 in ipairs(kit_wood_cabs) do
  275. if ]]
  276. local defined_mode = cabinets.define_mode(formname, name)
  277. if defined_mode == "closed" then
  278. cabinets.open(sender, pos, cabinets.define_needed_cabinet(formname, name), generalized_name, formname, form)
  279. elseif defined_mode == "opened" then
  280. cabinets.close(sender, pos, cabinets.define_needed_cabinet(formname, name), generalized_name, formname, form)
  281. end
  282.  
  283.  
  284. end
  285. })
  286. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement