functions.lua 5.3 KB

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