item_transport.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. local luaentity = pipeworks.luaentity
  2. local enable_max_limit = minetest.settings:get("pipeworks_enable_items_per_tube_limit")
  3. local max_tube_limit = tonumber(minetest.settings:get("pipeworks_max_items_per_tube")) or 30
  4. if enable_max_limit == nil then enable_max_limit = true end
  5. function pipeworks.tube_item(pos, item)
  6. error("obsolete pipeworks.tube_item() called; change caller to use pipeworks.tube_inject_item() instead")
  7. end
  8. function pipeworks.tube_inject_item(pos, start_pos, velocity, item, owner)
  9. -- Take item in any format
  10. local stack = ItemStack(item)
  11. local obj = luaentity.add_entity(pos, "pipeworks:tubed_item")
  12. obj:set_item(stack:to_string())
  13. obj.start_pos = vector.new(start_pos)
  14. obj:set_velocity(velocity)
  15. obj.owner = owner
  16. --obj:set_color("red") -- todo: this is test-only code
  17. return obj
  18. end
  19. -- adding two tube functions
  20. -- can_remove(pos,node,stack,dir) returns the maximum number of items of that stack that can be removed
  21. -- remove_items(pos,node,stack,dir,count) removes count items and returns them
  22. -- both optional w/ sensible defaults and fallback to normal allow_* function
  23. -- XXX: possibly change insert_object to insert_item
  24. local adjlist={{x=0,y=0,z=1},{x=0,y=0,z=-1},{x=0,y=1,z=0},{x=0,y=-1,z=0},{x=1,y=0,z=0},{x=-1,y=0,z=0}}
  25. function pipeworks.notvel(tbl, vel)
  26. local tbl2={}
  27. for _,val in ipairs(tbl) do
  28. if val.x ~= -vel.x or val.y ~= -vel.y or val.z ~= -vel.z then table.insert(tbl2, val) end
  29. end
  30. return tbl2
  31. end
  32. local tube_item_count = {}
  33. minetest.register_globalstep(function(dtime)
  34. if not luaentity.entities then
  35. return
  36. end
  37. tube_item_count = {}
  38. for id, entity in pairs(luaentity.entities) do
  39. if entity.name == "pipeworks:tubed_item" then
  40. local h = minetest.hash_node_position(vector.round(entity._pos))
  41. tube_item_count[h] = (tube_item_count[h] or 0) + 1
  42. end
  43. end
  44. end)
  45. -- tube overload mechanism:
  46. -- when the tube's item count (tracked in the above tube_item_count table)
  47. -- exceeds the limit configured per tube, replace it with a broken one.
  48. local crunch_tube = function(pos, cnode, cmeta)
  49. if enable_max_limit then
  50. local h = minetest.hash_node_position(pos)
  51. local itemcount = tube_item_count[h] or 0
  52. if itemcount > max_tube_limit then
  53. cmeta:set_string("the_tube_was", minetest.serialize(cnode))
  54. pipeworks.logger("Warning - a tube at "..minetest.pos_to_string(pos).." broke due to too many items ("..itemcount..")")
  55. minetest.swap_node(pos, {name = "pipeworks:broken_tube_1"})
  56. pipeworks.scan_for_tube_objects(pos)
  57. end
  58. end
  59. end
  60. -- compatibility behaviour for the existing can_go() callbacks,
  61. -- which can only specify a list of possible positions.
  62. local function go_next_compat(pos, cnode, cmeta, cycledir, vel, stack, owner)
  63. local next_positions = {}
  64. local max_priority = 0
  65. local can_go
  66. if minetest.registered_nodes[cnode.name] and minetest.registered_nodes[cnode.name].tube and minetest.registered_nodes[cnode.name].tube.can_go then
  67. can_go = minetest.registered_nodes[cnode.name].tube.can_go(pos, cnode, vel, stack)
  68. else
  69. can_go = pipeworks.notvel(adjlist, vel)
  70. end
  71. -- can_go() is expected to return an array-like table of candidate offsets.
  72. -- for each one, look at the node at that offset and determine if it can accept the item.
  73. -- also note the prioritisation:
  74. -- if any tube is found with a greater priority than previously discovered,
  75. -- then the valid positions are reset and and subsequent positions under this are skipped.
  76. -- this has the effect of allowing only equal priorities to co-exist.
  77. for _, vect in ipairs(can_go) do
  78. local npos = vector.add(pos, vect)
  79. pipeworks.load_position(npos)
  80. local node = minetest.get_node(npos)
  81. local reg_node = minetest.registered_nodes[node.name]
  82. if reg_node then
  83. local tube_def = reg_node.tube
  84. local tubedevice = minetest.get_item_group(node.name, "tubedevice")
  85. local tube_priority = (tube_def and tube_def.priority) or 100
  86. if tubedevice > 0 and tube_priority >= max_priority then
  87. if not tube_def or not tube_def.can_insert or
  88. tube_def.can_insert(npos, node, stack, vect, owner) then
  89. if tube_priority > max_priority then
  90. max_priority = tube_priority
  91. next_positions = {}
  92. end
  93. next_positions[#next_positions + 1] = {pos = npos, vect = vect}
  94. end
  95. end
  96. end
  97. end
  98. -- indicate not found if no valid rules were picked up,
  99. -- and don't change the counter.
  100. if not next_positions[1] then
  101. return cycledir, false, nil, nil
  102. end
  103. -- otherwise rotate to the next output direction and return that
  104. local n = (cycledir % (#next_positions)) + 1
  105. local new_velocity = vector.multiply(next_positions[n].vect, vel.speed)
  106. return n, true, new_velocity, nil
  107. end
  108. -- function called by the on_step callback of the pipeworks tube luaentity.
  109. -- the routine is passed the current node position, velocity, itemstack,
  110. -- and owner name.
  111. -- returns three values:
  112. -- * a boolean "found destination" status;
  113. -- * a new velocity vector that the tubed item should use, or nil if not found;
  114. -- * a "multi-mode" data table (or nil if N/A) where a stack was split apart.
  115. -- if this is not nil, the luaentity spawns new tubed items for each new fragment stack,
  116. -- then deletes itself (i.e. the original item stack).
  117. local function go_next(pos, velocity, stack, owner)
  118. local cnode = minetest.get_node(pos)
  119. local cmeta = minetest.get_meta(pos)
  120. local speed = math.abs(velocity.x + velocity.y + velocity.z)
  121. if speed == 0 then
  122. speed = 1
  123. end
  124. local vel = {x = velocity.x/speed, y = velocity.y/speed, z = velocity.z/speed,speed=speed}
  125. if speed >= 4.1 then
  126. speed = 4
  127. elseif speed >= 1.1 then
  128. speed = speed - 0.1
  129. else
  130. speed = 1
  131. end
  132. vel.speed = speed
  133. crunch_tube(pos, cnode, cmeta)
  134. -- cycling of outputs:
  135. -- an integer counter is kept in each pipe's metadata,
  136. -- which allows tracking which output was previously chosen.
  137. -- note reliance on get_int returning 0 for uninitialised.
  138. local cycledir = cmeta:get_int("tubedir")
  139. -- pulled out and factored out into go_next_compat() above.
  140. -- n is the new value of the cycle counter.
  141. -- XXX: this probably needs cleaning up after being split out,
  142. -- seven args is a bit too many
  143. local n, found, new_velocity, multimode = go_next_compat(pos, cnode, cmeta, cycledir, vel, stack, owner)
  144. -- if not using output cycling,
  145. -- don't update the field so it stays the same for the next item.
  146. if pipeworks.enable_cyclic_mode then
  147. cmeta:set_int("tubedir", n)
  148. end
  149. return found, new_velocity, multimode
  150. end
  151. minetest.register_entity("pipeworks:tubed_item", {
  152. initial_properties = {
  153. hp_max = 1,
  154. physical = false,
  155. collisionbox = {0.1, 0.1, 0.1, 0.1, 0.1, 0.1},
  156. visual = "wielditem",
  157. visual_size = {x = 0.15, y = 0.15},
  158. textures = {""},
  159. spritediv = {x = 1, y = 1},
  160. initial_sprite_basepos = {x = 0, y = 0},
  161. is_visible = false,
  162. },
  163. physical_state = false,
  164. from_data = function(self, itemstring)
  165. local stack = ItemStack(itemstring)
  166. local itemtable = stack:to_table()
  167. local itemname = nil
  168. if itemtable then
  169. itemname = stack:to_table().name
  170. end
  171. local item_texture = nil
  172. local item_type = ""
  173. if minetest.registered_items[itemname] then
  174. item_texture = minetest.registered_items[itemname].inventory_image
  175. item_type = minetest.registered_items[itemname].type
  176. end
  177. self.object:set_properties({
  178. is_visible = true,
  179. textures = {stack:get_name()}
  180. })
  181. local def = stack:get_definition()
  182. self.object:set_yaw((def and def.type == "node") and 0 or math.pi * 0.25)
  183. end,
  184. get_staticdata = luaentity.get_staticdata,
  185. on_activate = function(self, staticdata) -- Legacy code, should be replaced later by luaentity.on_activate
  186. if staticdata == "" or staticdata == nil then
  187. return
  188. end
  189. if staticdata == "toremove" then
  190. self.object:remove()
  191. return
  192. end
  193. local item = minetest.deserialize(staticdata)
  194. pipeworks.tube_inject_item(self.object:get_pos(), item.start_pos, item.velocity, item.itemstring)
  195. self.object:remove()
  196. end,
  197. })
  198. minetest.register_entity("pipeworks:color_entity", {
  199. initial_properties = {
  200. hp_max = 1,
  201. physical = false,
  202. collisionbox = {0.1, 0.1, 0.1, 0.1, 0.1, 0.1},
  203. visual = "cube",
  204. visual_size = {x = 3.5, y = 3.5, z = 3.5}, -- todo: find correct size
  205. textures = {""},
  206. is_visible = false,
  207. },
  208. physical_state = false,
  209. from_data = function(self, color)
  210. local t = "pipeworks_color_"..color..".png"
  211. local prop = {
  212. is_visible = true,
  213. visual = "cube",
  214. textures = {t, t, t, t, t, t} -- todo: textures
  215. }
  216. self.object:set_properties(prop)
  217. end,
  218. get_staticdata = luaentity.get_staticdata,
  219. on_activate = luaentity.on_activate,
  220. })
  221. -- see below for usage:
  222. -- determine if go_next returned a multi-mode set.
  223. local is_multimode = function(v)
  224. return (type(v) == "table") and (v.__multimode)
  225. end
  226. luaentity.register_entity("pipeworks:tubed_item", {
  227. itemstring = '',
  228. item_entity = nil,
  229. color_entity = nil,
  230. color = nil,
  231. start_pos = nil,
  232. set_item = function(self, item)
  233. local itemstring = ItemStack(item):to_string() -- Accept any input format
  234. if self.itemstring == itemstring then
  235. return
  236. end
  237. if self.item_entity then
  238. self:remove_attached_entity(self.item_entity)
  239. end
  240. self.itemstring = itemstring
  241. self.item_entity = self:add_attached_entity("pipeworks:tubed_item", itemstring)
  242. end,
  243. set_color = function(self, color)
  244. if self.color == color then
  245. return
  246. end
  247. self.color = color
  248. if self.color_entity then
  249. self:remove_attached_entity(self.color_entity)
  250. end
  251. if color then
  252. self.color_entity = self:add_attached_entity("pipeworks:color_entity", color)
  253. else
  254. self.color_entity = nil
  255. end
  256. end,
  257. on_step = function(self, dtime)
  258. local pos = self:get_pos()
  259. if self.start_pos == nil then
  260. self.start_pos = vector.round(pos)
  261. self:set_pos(pos)
  262. end
  263. local stack = ItemStack(self.itemstring)
  264. local velocity = self:get_velocity()
  265. local moved = false
  266. local speed = math.abs(velocity.x + velocity.y + velocity.z)
  267. if speed == 0 then
  268. speed = 1
  269. moved = true
  270. end
  271. local vel = {x = velocity.x / speed, y = velocity.y / speed, z = velocity.z / speed, speed = speed}
  272. local moved_by = vector.distance(pos, self.start_pos)
  273. if moved_by >= 1 then
  274. self.start_pos = vector.add(self.start_pos, vel)
  275. moved = true
  276. end
  277. pipeworks.load_position(self.start_pos)
  278. local node = minetest.get_node(self.start_pos)
  279. if moved and minetest.get_item_group(node.name, "tubedevice_receiver") == 1 then
  280. local leftover
  281. if minetest.registered_nodes[node.name].tube and minetest.registered_nodes[node.name].tube.insert_object then
  282. leftover = minetest.registered_nodes[node.name].tube.insert_object(self.start_pos, node, stack, vel, self.owner)
  283. else
  284. leftover = stack
  285. end
  286. if leftover:is_empty() then
  287. self:remove()
  288. return
  289. end
  290. velocity = vector.multiply(velocity, -1)
  291. self:set_pos(vector.subtract(self.start_pos, vector.multiply(vel, moved_by - 1)))
  292. self:set_velocity(velocity)
  293. self:set_item(leftover:to_string())
  294. return
  295. end
  296. if moved then
  297. local found_next, new_velocity, multimode = go_next(self.start_pos, velocity, stack, self.owner) -- todo: color
  298. local rev_vel = vector.multiply(velocity, -1)
  299. local rev_dir = vector.direction(self.start_pos,vector.add(self.start_pos,rev_vel))
  300. local rev_node = minetest.get_node(vector.round(vector.add(self.start_pos,rev_dir)))
  301. local tube_present = minetest.get_item_group(rev_node.name,"tubedevice") == 1
  302. if not found_next then
  303. if pipeworks.drop_on_routing_fail or not tube_present or
  304. minetest.get_item_group(rev_node.name,"tube") ~= 1 then
  305. -- Using add_item instead of item_drop since this makes pipeworks backward
  306. -- compatible with Minetest 0.4.13.
  307. -- Using item_drop here makes Minetest 0.4.13 crash.
  308. local dropped_item = minetest.add_item(self.start_pos, stack)
  309. if dropped_item then
  310. dropped_item:set_velocity(vector.multiply(velocity, 5))
  311. self:remove()
  312. end
  313. return
  314. else
  315. velocity = vector.multiply(velocity, -1)
  316. self:set_pos(vector.subtract(self.start_pos, vector.multiply(vel, moved_by - 1)))
  317. self:set_velocity(velocity)
  318. end
  319. elseif is_multimode(multimode) then
  320. -- create new stacks according to returned data.
  321. local s = self.start_pos
  322. for _, split in ipairs(multimode) do
  323. pipeworks.tube_inject_item(s, s, split.velocity, split.itemstack, self.owner)
  324. end
  325. -- remove ourself now the splits are sent
  326. self:remove()
  327. return
  328. end
  329. if new_velocity and not vector.equals(velocity, new_velocity) then
  330. local nvelr = math.abs(new_velocity.x + new_velocity.y + new_velocity.z)
  331. self:set_pos(vector.add(self.start_pos, vector.multiply(new_velocity, (moved_by - 1) / nvelr)))
  332. self:set_velocity(new_velocity)
  333. end
  334. end
  335. end
  336. })
  337. if minetest.get_modpath("mesecons_mvps") then
  338. mesecon.register_mvps_unmov("pipeworks:tubed_item")
  339. mesecon.register_mvps_unmov("pipeworks:color_entity")
  340. mesecon.register_on_mvps_move(function(moved_nodes)
  341. local moved = {}
  342. for _, n in ipairs(moved_nodes) do
  343. moved[minetest.hash_node_position(n.oldpos)] = vector.subtract(n.pos, n.oldpos)
  344. end
  345. for id, entity in pairs(luaentity.entities) do
  346. if entity.name == "pipeworks:tubed_item" then
  347. local pos = entity:get_pos()
  348. local rpos = vector.round(pos)
  349. local dir = moved[minetest.hash_node_position(rpos)]
  350. if dir then
  351. entity:set_pos(vector.add(pos, dir))
  352. entity.start_pos = vector.add(entity.start_pos, dir)
  353. end
  354. end
  355. end
  356. end)
  357. end