functions.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 default.player_attached[player_name] == status then
  15. return
  16. end
  17. default.player_attached[player_name] = status
  18. if status then
  19. player:set_attach(obj, "", {x=0, y=6, 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. if ctrl then
  94. if old_switch == 1 then
  95. left_check = false
  96. elseif old_switch == 2 then
  97. right_check = false
  98. end
  99. if ctrl.left and left_check then
  100. cur = carts:check_front_up_down(pos, left, false, railtype)
  101. if cur then
  102. return cur, 1
  103. end
  104. left_check = false
  105. end
  106. if ctrl.right and right_check then
  107. cur = carts:check_front_up_down(pos, right, false, railtype)
  108. if cur then
  109. return cur, 2
  110. end
  111. right_check = true
  112. end
  113. end
  114. -- Normal
  115. cur = carts:check_front_up_down(pos, dir, true, railtype)
  116. if cur then
  117. return cur
  118. end
  119. -- Left, if not already checked
  120. if left_check then
  121. cur = carts:check_front_up_down(pos, left, false, railtype)
  122. if cur then
  123. return cur
  124. end
  125. end
  126. -- Right, if not already checked
  127. if right_check then
  128. cur = carts:check_front_up_down(pos, right, false, railtype)
  129. if cur then
  130. return cur
  131. end
  132. end
  133. -- Backwards
  134. if not old_switch then
  135. cur = carts:check_front_up_down(pos, {
  136. x = -dir.x,
  137. y = dir.y,
  138. z = -dir.z
  139. }, true, railtype)
  140. if cur then
  141. return cur
  142. end
  143. end
  144. return {x=0, y=0, z=0}
  145. end
  146. function carts:pathfinder(pos_, old_pos, old_dir, ctrl, pf_switch, railtype)
  147. local pos = vector.round(pos_)
  148. local pf_pos = vector.round(old_pos)
  149. local pf_dir = vector.new(old_dir)
  150. for i = 1, 3 do
  151. if vector.equals(pf_pos, pos) then
  152. -- Success! Cart moved on correctly
  153. return true
  154. end
  155. pf_dir, pf_switch = carts:get_rail_direction(pf_pos, pf_dir, ctrl, pf_switch, railtype)
  156. if vector.equals(pf_dir, {x=0, y=0, z=0}) then
  157. -- No way forwards
  158. return false
  159. end
  160. pf_pos = vector.add(pf_pos, pf_dir)
  161. end
  162. -- Cart not found
  163. return false
  164. end
  165. function carts:register_rail(name, def_overwrite, railparams)
  166. local def = {
  167. drawtype = "raillike",
  168. paramtype = "light",
  169. sunlight_propagates = true,
  170. is_ground_content = false,
  171. walkable = false,
  172. selection_box = {
  173. type = "fixed",
  174. fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
  175. },
  176. sounds = default.node_sound_metal_defaults()
  177. }
  178. for k, v in pairs(def_overwrite) do
  179. def[k] = v
  180. end
  181. if not def.inventory_image then
  182. def.wield_image = def.tiles[1]
  183. def.inventory_image = def.tiles[1]
  184. end
  185. if railparams then
  186. carts.railparams[name] = table.copy(railparams)
  187. end
  188. minetest.register_node(name, def)
  189. end
  190. function carts:get_rail_groups(additional_groups)
  191. -- Get the default rail groups and add more when a table is given
  192. local groups = {dig_immediate = 2, attached_node = 1, rail = 1, connect_to_raillike = 1}
  193. if type(additional_groups) == "table" then
  194. for k, v in pairs(additional_groups) do
  195. groups[k] = v
  196. end
  197. end
  198. return groups
  199. end