functions.lua 5.0 KB

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