functions.lua 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. -- Localize for performance.
  2. local vector_round = vector.round
  3. function carts:get_sign(z)
  4. if z == 0 then
  5. return 0
  6. else
  7. return z / math.abs(z)
  8. end
  9. end
  10. function carts:manage_attachment(player, obj)
  11. if not player then
  12. return
  13. end
  14. local pname = player:get_player_name()
  15. if obj then
  16. -- Don't attach if already attached.
  17. if default.player_attached[pname] then
  18. return
  19. end
  20. default.player_attached[pname] = true
  21. player:set_attach(obj, "", {x=0, y=-4, z=0}, {x=0, y=0, z=0})
  22. player:set_eye_offset({x=0, y=-4, z=0},{x=0, y=-4, z=0})
  23. else
  24. if default.player_attached[pname] then
  25. default.player_attached[pname] = false
  26. player:set_detach()
  27. player:set_eye_offset({x=0, y=0, z=0},{x=0, y=0, z=0})
  28. end
  29. end
  30. end
  31. function carts:velocity_to_dir(v)
  32. if math.abs(v.x) > math.abs(v.z) then
  33. return {x=carts:get_sign(v.x), y=carts:get_sign(v.y), z=0}
  34. else
  35. return {x=0, y=carts:get_sign(v.y), z=carts:get_sign(v.z)}
  36. end
  37. end
  38. function carts:is_rail(pos, railtype)
  39. local node = minetest.get_node(pos).name
  40. if node == "ignore" then
  41. local vm = minetest.get_voxel_manip()
  42. local emin, emax = vm:read_from_map(pos, pos)
  43. local area = VoxelArea:new{
  44. MinEdge = emin,
  45. MaxEdge = emax,
  46. }
  47. local data = vm:get_data()
  48. local vi = area:indexp(pos)
  49. node = minetest.get_name_from_content_id(data[vi])
  50. end
  51. if minetest.get_item_group(node, "rail") == 0 then
  52. return false
  53. end
  54. if not railtype then
  55. return true
  56. end
  57. return minetest.get_item_group(node, "connect_to_raillike") == railtype
  58. end
  59. function carts:check_front_up_down(pos, dir_, check_up, railtype)
  60. local dir = vector.new(dir_)
  61. local cur
  62. -- Front
  63. dir.y = 0
  64. cur = vector.add(pos, dir)
  65. if carts:is_rail(cur, railtype) then
  66. return dir
  67. end
  68. -- Up
  69. if check_up then
  70. dir.y = 1
  71. cur = vector.add(pos, dir)
  72. if carts:is_rail(cur, railtype) then
  73. return dir
  74. end
  75. end
  76. -- Down
  77. dir.y = -1
  78. cur = vector.add(pos, dir)
  79. if carts:is_rail(cur, railtype) then
  80. return dir
  81. end
  82. return nil
  83. end
  84. function carts:get_rail_direction(pos_, dir, ctrl, old_switch, railtype)
  85. local pos = vector_round(pos_)
  86. local cur
  87. local left_check, right_check = true, true
  88. -- Check left and right
  89. local left = {x=0, y=0, z=0}
  90. local right = {x=0, y=0, z=0}
  91. if dir.z ~= 0 and dir.x == 0 then
  92. left.x = -dir.z
  93. right.x = dir.z
  94. elseif dir.x ~= 0 and dir.z == 0 then
  95. left.z = dir.x
  96. right.z = -dir.x
  97. end
  98. if ctrl then
  99. if old_switch == 1 then
  100. left_check = false
  101. elseif old_switch == 2 then
  102. right_check = false
  103. end
  104. if ctrl.left and left_check then
  105. cur = carts:check_front_up_down(pos, left, false, railtype)
  106. if cur then
  107. return cur, 1
  108. end
  109. left_check = false
  110. end
  111. if ctrl.right and right_check then
  112. cur = carts:check_front_up_down(pos, right, false, railtype)
  113. if cur then
  114. return cur, 2
  115. end
  116. right_check = true
  117. end
  118. end
  119. -- Normal
  120. cur = carts:check_front_up_down(pos, dir, true, railtype)
  121. if cur then
  122. return cur
  123. end
  124. -- Left, if not already checked
  125. if left_check then
  126. cur = carts:check_front_up_down(pos, left, false, railtype)
  127. if cur then
  128. return cur
  129. end
  130. end
  131. -- Right, if not already checked
  132. if right_check then
  133. cur = carts:check_front_up_down(pos, right, false, railtype)
  134. if cur then
  135. return cur
  136. end
  137. end
  138. -- Backwards
  139. if not old_switch then
  140. cur = carts:check_front_up_down(pos, {
  141. x = -dir.x,
  142. y = dir.y,
  143. z = -dir.z
  144. }, true, railtype)
  145. if cur then
  146. return cur
  147. end
  148. end
  149. return {x=0, y=0, z=0}
  150. end
  151. function carts:pathfinder(pos_, old_pos, old_dir, ctrl, pf_switch, railtype)
  152. local pos = vector_round(pos_)
  153. local pf_pos = vector_round(old_pos)
  154. local pf_dir = vector.new(old_dir)
  155. for i = 1, 3 do
  156. if vector.equals(pf_pos, pos) then
  157. -- Success! Cart moved on correctly
  158. return true
  159. end
  160. pf_dir, pf_switch = carts:get_rail_direction(pf_pos, pf_dir, ctrl, pf_switch, railtype)
  161. if vector.equals(pf_dir, {x=0, y=0, z=0}) then
  162. -- No way forwards
  163. return false
  164. end
  165. pf_pos = vector.add(pf_pos, pf_dir)
  166. end
  167. -- Cart not found
  168. return false
  169. end
  170. function carts:register_rail(name, def_overwrite, railparams)
  171. local def = {
  172. drawtype = "raillike",
  173. paramtype = "light",
  174. sunlight_propagates = true,
  175. is_ground_content = false,
  176. walkable = false,
  177. selection_box = {
  178. type = "fixed",
  179. fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
  180. },
  181. collision_box = {
  182. type = "fixed",
  183. fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
  184. },
  185. sounds = default.node_sound_metal_defaults()
  186. }
  187. for k, v in pairs(def_overwrite) do
  188. def[k] = v
  189. end
  190. if not def.inventory_image then
  191. def.wield_image = def.tiles[1]
  192. def.inventory_image = def.tiles[1]
  193. end
  194. if railparams then
  195. carts.railparams[name] = table.copy(railparams)
  196. end
  197. minetest.register_node(name, def)
  198. end
  199. function carts:get_rail_groups(additional_groups)
  200. -- Get the default rail groups and add more when a table is given
  201. local groups = utility.dig_groups("bigitem", {
  202. attached_node = 1,
  203. rail = 1,
  204. connect_to_raillike = minetest.raillike_group("rail")
  205. })
  206. if type(additional_groups) == "table" then
  207. for k, v in pairs(additional_groups) do
  208. groups[k] = v
  209. end
  210. end
  211. return groups
  212. end