functions.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. function asrs.create_id(player_name, pos)
  2. local i = 1
  3. local system_id = player_name..'_'..i
  4. while asrs.data[system_id] do
  5. i = i + 1
  6. system_id = player_name..'_'..i
  7. end
  8. local new_data = {}
  9. new_data.inv_pos = pos
  10. new_data.max_inv = 0
  11. new_data.name = player_name
  12. new_data.nodes = 0
  13. asrs.data[system_id] = new_data
  14. return system_id
  15. end
  16. function asrs.update_inventory(pos)
  17. local meta = minetest.get_meta(pos)
  18. local sys_id = meta:get_string('system_id')
  19. local sys_data = asrs.data[sys_id]
  20. if sys_data then
  21. local sys_inv_max = sys_data.max_inv
  22. local inv = meta:get_inventory()
  23. inv:set_size('storage', sys_inv_max)
  24. end
  25. end
  26. function asrs.count_inventory(pos)
  27. local meta = minetest.get_meta(pos)
  28. local inv = meta:get_inventory()
  29. local inv_list = inv:get_list('storage')
  30. local count = 0
  31. local size = inv:get_size('storage')
  32. for i = 1, size do
  33. local stack = inv_list[i]
  34. local empty = stack:is_empty()
  35. if not empty then
  36. count = count + 1
  37. end
  38. end
  39. return count
  40. end
  41. function asrs.sort_inventory(pos) -- Copied from the Technic_chests mod.
  42. local meta = minetest.get_meta(pos)
  43. local inv = meta:get_inventory()
  44. local inlist = inv:get_list('storage')
  45. if inlist then
  46. local typecnt = {}
  47. local typekeys = {}
  48. for _, st in ipairs(inlist) do
  49. if not st:is_empty() then
  50. local n = st:get_name()
  51. local w = st:get_wear()
  52. local m = st:get_metadata()
  53. local k = string.format("%s %05d %s", n, w, m)
  54. if not typecnt[k] then
  55. typecnt[k] = {
  56. name = n,
  57. wear = w,
  58. metadata = m,
  59. stack_max = st:get_stack_max(),
  60. count = 0,
  61. }
  62. table.insert(typekeys, k)
  63. end
  64. typecnt[k].count = typecnt[k].count + st:get_count()
  65. end
  66. end
  67. table.sort(typekeys)
  68. local outlist = {}
  69. for _, k in ipairs(typekeys) do
  70. local tc = typecnt[k]
  71. while tc.count > 0 do
  72. local c = math.min(tc.count, tc.stack_max)
  73. table.insert(outlist, ItemStack({
  74. name = tc.name,
  75. wear = tc.wear,
  76. metadata = tc.metadata,
  77. count = c,
  78. }))
  79. tc.count = tc.count - c
  80. end
  81. end
  82. if #outlist > #inlist then return end
  83. while #outlist < #inlist do
  84. table.insert(outlist, ItemStack(nil))
  85. end
  86. inv:set_list('storage', outlist)
  87. end
  88. end
  89. function asrs.connected_nodes(pos, node_name)
  90. local positions = {
  91. {x=pos.x+1, y=pos.y, z=pos.z},
  92. {x=pos.x-1, y=pos.y, z=pos.z},
  93. {x=pos.x, y=pos.y+1, z=pos.z},
  94. {x=pos.x, y=pos.y-1, z=pos.z},
  95. {x=pos.x, y=pos.y, z=pos.z+1},
  96. {x=pos.x, y=pos.y, z=pos.z-1},
  97. }
  98. local found_node = false
  99. local other_pos
  100. for _, loc in ipairs(positions) do
  101. local name = minetest.get_node(loc).name
  102. if string.find(node_name, name) then
  103. other_pos = loc
  104. found_node = true
  105. break
  106. end
  107. end
  108. return found_node, other_pos
  109. end
  110. fdir_table = {
  111. { 1, 0 },
  112. { 0, -1 },
  113. { -1, 0 },
  114. { 0, 1 },
  115. { 1, 0 },
  116. { 0, -1 },
  117. { -1, 0 },
  118. { 0, 1 },
  119. }
  120. function asrs.space_to_place(pos)
  121. local node = minetest.get_node(pos)
  122. local fdir = node.param2 % 32
  123. local pos2 = {x = pos.x + fdir_table[fdir+1][1], y=pos.y, z = pos.z + fdir_table[fdir+1][2]}
  124. local pos3 = {x = pos2.x, y = pos2.y+1, z = pos2.z}
  125. local pos4 = {x = pos.x, y = pos.y+1, z = pos.z}
  126. local node2 = minetest.get_node(pos2) -- Node to the right
  127. local node3 = minetest.get_node(pos3) -- Node above to the right
  128. local node4 = minetest.get_node(pos4) -- Node above
  129. local node2def = minetest.registered_nodes[node2.name] or nil
  130. local node3def = minetest.registered_nodes[node3.name] or nil
  131. local node4def = minetest.registered_nodes[node4.name] or nil
  132. if not node2def.buildable_to or not node3def.buildable_to or not node4def.buildable_to then
  133. return false
  134. else
  135. minetest.after(1, function()
  136. minetest.set_node(pos2,{name = 'asrs:blank'})
  137. minetest.set_node(pos3,{name = 'asrs:blank'})
  138. minetest.set_node(pos4,{name = 'asrs:connection_point'})
  139. end)
  140. return true
  141. end
  142. end
  143. function asrs.remove_side_node(pos, oldnode)
  144. local fdir = oldnode.param2 % 32
  145. local pos2 = {x = pos.x + fdir_table[fdir+1][1], y=pos.y, z = pos.z + fdir_table[fdir+1][2]}
  146. local pos3 = {x = pos2.x, y = pos2.y+1, z = pos2.z}
  147. local pos4 = {x = pos.x, y = pos.y+1, z = pos.z}
  148. minetest.remove_node(pos2)
  149. minetest.remove_node(pos3)
  150. minetest.remove_node(pos4)
  151. end
  152. function asrs.load()
  153. local file = io.open(minetest.get_worldpath() .. '/asrs_systems', 'r')
  154. if file then
  155. asrs.data = minetest.deserialize(file:read('*a'))
  156. file:close()
  157. else
  158. asrs.data = {}
  159. end
  160. end
  161. function asrs.save()
  162. local file = io.open(minetest.get_worldpath() .. '/asrs_systems', 'w')
  163. file:write(minetest.serialize(asrs.data))
  164. file:close()
  165. end