cart_entity.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. local cart_entity = {
  2. physical = false, -- otherwise going uphill breaks
  3. collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
  4. visual = "mesh",
  5. mesh = "carts_cart.b3d",
  6. visual_size = {x=1, y=1},
  7. textures = {"carts_cart.png"},
  8. driver = nil,
  9. punched = false, -- used to re-send velocity and position
  10. velocity = {x=0, y=0, z=0}, -- only used on punch
  11. old_dir = {x=1, y=0, z=0}, -- random value to start the cart on punch
  12. old_pos = nil,
  13. old_switch = 0,
  14. railtype = nil,
  15. attached_items = {}
  16. }
  17. function cart_entity:on_rightclick(clicker)
  18. if not clicker or not clicker:is_player() then
  19. return
  20. end
  21. local player_name = clicker:get_player_name()
  22. if self.driver and player_name == self.driver then
  23. self.driver = nil
  24. carts:manage_attachment(clicker, nil)
  25. elseif not self.driver then
  26. self.driver = player_name
  27. carts:manage_attachment(clicker, self.object)
  28. end
  29. end
  30. function cart_entity:on_activate(staticdata, dtime_s)
  31. self.object:set_armor_groups({immortal=1})
  32. if string.sub(staticdata, 1, string.len("return")) ~= "return" then
  33. return
  34. end
  35. local data = minetest.deserialize(staticdata)
  36. if not data or type(data) ~= "table" then
  37. return
  38. end
  39. self.railtype = data.railtype
  40. if data.old_dir then
  41. self.old_dir = data.old_dir
  42. end
  43. if data.old_vel then
  44. self.old_vel = data.old_vel
  45. end
  46. end
  47. function cart_entity:get_staticdata()
  48. return minetest.serialize({
  49. railtype = self.railtype,
  50. old_dir = self.old_dir,
  51. old_vel = self.old_vel
  52. })
  53. end
  54. function cart_entity:on_punch(puncher, time_from_last_punch, tool_capabilities, direction)
  55. local pos = self.object:getpos()
  56. if not self.railtype then
  57. local node = minetest.get_node(pos).name
  58. self.railtype = minetest.get_item_group(node, "connect_to_raillike")
  59. end
  60. -- Punched by non-player
  61. if not puncher or not puncher:is_player() then
  62. local cart_dir = carts:get_rail_direction(pos, self.old_dir, nil, nil, self.railtype)
  63. if vector.equals(cart_dir, {x=0, y=0, z=0}) then
  64. return
  65. end
  66. self.velocity = vector.multiply(cart_dir, 2)
  67. self.punched = true
  68. return
  69. end
  70. -- Player digs cart by sneak-punch
  71. if puncher:get_player_control().sneak then
  72. if self.sound_handle then
  73. minetest.sound_stop(self.sound_handle)
  74. end
  75. -- Detach driver and items
  76. if self.driver then
  77. if self.old_pos then
  78. self.object:setpos(self.old_pos)
  79. end
  80. local player = minetest.get_player_by_name(self.driver)
  81. carts:manage_attachment(player, nil)
  82. end
  83. for _,obj_ in ipairs(self.attached_items) do
  84. if obj_ then
  85. obj_:set_detach()
  86. end
  87. end
  88. -- Pick up cart
  89. local inv = puncher:get_inventory()
  90. if not (creative and creative.is_enabled_for
  91. and creative.is_enabled_for(puncher:get_player_name()))
  92. or not inv:contains_item("main", "carts:cart") then
  93. local leftover = inv:add_item("main", "carts:cart")
  94. -- If no room in inventory add a replacement cart to the world
  95. if not leftover:is_empty() then
  96. minetest.add_item(self.object:getpos(), leftover)
  97. end
  98. end
  99. self.object:remove()
  100. return
  101. end
  102. -- Player punches cart to alter velocity
  103. local vel = self.object:getvelocity()
  104. if puncher:get_player_name() == self.driver then
  105. if math.abs(vel.x + vel.z) > carts.punch_speed_max then
  106. return
  107. end
  108. end
  109. local punch_dir = carts:velocity_to_dir(puncher:get_look_dir())
  110. punch_dir.y = 0
  111. local cart_dir = carts:get_rail_direction(pos, punch_dir, nil, nil, self.railtype)
  112. if vector.equals(cart_dir, {x=0, y=0, z=0}) then
  113. return
  114. end
  115. local punch_interval = 1
  116. if tool_capabilities and tool_capabilities.full_punch_interval then
  117. punch_interval = tool_capabilities.full_punch_interval
  118. end
  119. time_from_last_punch = math.min(time_from_last_punch or punch_interval, punch_interval)
  120. local f = 2 * (time_from_last_punch / punch_interval)
  121. self.velocity = vector.multiply(cart_dir, f)
  122. self.old_dir = cart_dir
  123. self.punched = true
  124. end
  125. local function rail_on_step_event(handler, obj, dtime)
  126. if handler then
  127. handler(obj, dtime)
  128. end
  129. end
  130. -- sound refresh interval = 1.0sec
  131. local function rail_sound(self, dtime)
  132. if not self.sound_ttl then
  133. self.sound_ttl = 1.0
  134. return
  135. elseif self.sound_ttl > 0 then
  136. self.sound_ttl = self.sound_ttl - dtime
  137. return
  138. end
  139. self.sound_ttl = 1.0
  140. if self.sound_handle then
  141. local handle = self.sound_handle
  142. self.sound_handle = nil
  143. minetest.after(0.2, minetest.sound_stop, handle)
  144. end
  145. local vel = self.object:getvelocity()
  146. local speed = vector.length(vel)
  147. if speed > 0 then
  148. self.sound_handle = minetest.sound_play(
  149. "carts_cart_moving", {
  150. object = self.object,
  151. gain = (speed / carts.speed_max) / 2,
  152. loop = true,
  153. })
  154. end
  155. end
  156. local function get_railparams(pos)
  157. local node = minetest.get_node(pos)
  158. return carts.railparams[node.name] or {}
  159. end
  160. local function rail_on_step(self, dtime)
  161. local vel = self.object:getvelocity()
  162. if self.punched then
  163. vel = vector.add(vel, self.velocity)
  164. self.object:setvelocity(vel)
  165. self.old_dir.y = 0
  166. elseif vector.equals(vel, {x=0, y=0, z=0}) then
  167. return
  168. end
  169. local pos = self.object:getpos()
  170. local update = {}
  171. -- stop cart if velocity vector flips
  172. if self.old_vel and self.old_vel.y == 0 and
  173. (self.old_vel.x * vel.x < 0 or self.old_vel.z * vel.z < 0) then
  174. self.old_vel = {x = 0, y = 0, z = 0}
  175. self.old_pos = pos
  176. self.object:setvelocity(vector.new())
  177. self.object:setacceleration(vector.new())
  178. rail_on_step_event(get_railparams(pos).on_step, self, dtime)
  179. return
  180. end
  181. self.old_vel = vector.new(vel)
  182. if self.old_pos and not self.punched then
  183. local flo_pos = vector.round(pos)
  184. local flo_old = vector.round(self.old_pos)
  185. if vector.equals(flo_pos, flo_old) then
  186. -- Do not check one node multiple times
  187. return
  188. end
  189. end
  190. local ctrl, player
  191. -- Get player controls
  192. if self.driver then
  193. player = minetest.get_player_by_name(self.driver)
  194. if player then
  195. ctrl = player:get_player_control()
  196. end
  197. end
  198. if self.old_pos then
  199. -- Detection for "skipping" nodes
  200. local found_path = carts:pathfinder(
  201. pos, self.old_pos, self.old_dir, ctrl, self.old_switch, self.railtype
  202. )
  203. if not found_path then
  204. -- No rail found: reset back to the expected position
  205. pos = vector.new(self.old_pos)
  206. update.pos = true
  207. end
  208. end
  209. local cart_dir = carts:velocity_to_dir(vel)
  210. local railparams
  211. -- dir: New moving direction of the cart
  212. -- switch_keys: Currently pressed L/R key, used to ignore the key on the next rail node
  213. local dir, switch_keys = carts:get_rail_direction(
  214. pos, cart_dir, ctrl, self.old_switch, self.railtype
  215. )
  216. local new_acc = {x=0, y=0, z=0}
  217. if vector.equals(dir, {x=0, y=0, z=0}) then
  218. vel = {x = 0, y = 0, z = 0}
  219. pos = vector.round(pos)
  220. update.pos = true
  221. update.vel = true
  222. else
  223. -- Direction change detected
  224. if not vector.equals(dir, self.old_dir) then
  225. vel = vector.multiply(dir, math.abs(vel.x + vel.z))
  226. update.vel = true
  227. if dir.y ~= self.old_dir.y then
  228. pos = vector.round(pos)
  229. update.pos = true
  230. end
  231. end
  232. -- Center on the rail
  233. if dir.z ~= 0 and math.floor(pos.x + 0.5) ~= pos.x then
  234. pos.x = math.floor(pos.x + 0.5)
  235. update.pos = true
  236. end
  237. if dir.x ~= 0 and math.floor(pos.z + 0.5) ~= pos.z then
  238. pos.z = math.floor(pos.z + 0.5)
  239. update.pos = true
  240. end
  241. -- Slow down or speed up..
  242. local acc = dir.y * -4.0
  243. -- Get rail for corrected position
  244. railparams = get_railparams(pos)
  245. -- no need to check for railparams == nil since we always make it exist.
  246. local speed_mod = railparams.acceleration
  247. if speed_mod and speed_mod ~= 0 then
  248. -- Try to make it similar to the original carts mod
  249. acc = acc + speed_mod
  250. else
  251. -- Handbrake or coast
  252. if ctrl and ctrl.down then
  253. acc = acc - 3
  254. else
  255. acc = acc - 0.4
  256. end
  257. end
  258. new_acc = vector.multiply(dir, acc)
  259. end
  260. -- Limits
  261. local max_vel = carts.speed_max
  262. for _, v in pairs({"x","y","z"}) do
  263. if math.abs(vel[v]) > max_vel then
  264. vel[v] = carts:get_sign(vel[v]) * max_vel
  265. new_acc[v] = 0
  266. update.vel = true
  267. end
  268. end
  269. self.object:setacceleration(new_acc)
  270. self.old_pos = vector.new(pos)
  271. if not vector.equals(dir, {x=0, y=0, z=0}) then
  272. self.old_dir = vector.new(dir)
  273. end
  274. self.old_switch = switch_keys
  275. if self.punched then
  276. -- Collect dropped items
  277. for _, obj_ in pairs(minetest.get_objects_inside_radius(pos, 1)) do
  278. if not obj_:is_player() and
  279. obj_:get_luaentity() and
  280. not obj_:get_luaentity().physical_state and
  281. obj_:get_luaentity().name == "__builtin:item" then
  282. obj_:set_attach(self.object, "", {x=0, y=0, z=0}, {x=0, y=0, z=0})
  283. self.attached_items[#self.attached_items + 1] = obj_
  284. end
  285. end
  286. self.punched = false
  287. update.vel = true
  288. end
  289. railparams = railparams or get_railparams(pos)
  290. if not (update.vel or update.pos) then
  291. rail_on_step_event(railparams.on_step, self, dtime)
  292. return
  293. end
  294. local yaw = 0
  295. if self.old_dir.x < 0 then
  296. yaw = 0.5
  297. elseif self.old_dir.x > 0 then
  298. yaw = 1.5
  299. elseif self.old_dir.z < 0 then
  300. yaw = 1
  301. end
  302. self.object:setyaw(yaw * math.pi)
  303. local anim = {x=0, y=0}
  304. if dir.y == -1 then
  305. anim = {x=1, y=1}
  306. elseif dir.y == 1 then
  307. anim = {x=2, y=2}
  308. end
  309. self.object:set_animation(anim, 1, 0)
  310. self.object:setvelocity(vel)
  311. if update.pos then
  312. self.object:setpos(pos)
  313. end
  314. -- call event handler
  315. rail_on_step_event(railparams.on_step, self, dtime)
  316. end
  317. function cart_entity:on_step(dtime)
  318. rail_on_step(self, dtime)
  319. rail_sound(self, dtime)
  320. end
  321. minetest.register_entity("carts:cart", cart_entity)
  322. minetest.register_craftitem("carts:cart", {
  323. description = "Cart (Sneak+Click to pick up)",
  324. inventory_image = minetest.inventorycube("carts_cart_top.png", "carts_cart_side.png", "carts_cart_side.png"),
  325. wield_image = "carts_cart_side.png",
  326. on_place = function(itemstack, placer, pointed_thing)
  327. local under = pointed_thing.under
  328. local node = minetest.get_node(under)
  329. local udef = minetest.registered_nodes[node.name]
  330. if udef and udef.on_rightclick and
  331. not (placer and placer:get_player_control().sneak) then
  332. return udef.on_rightclick(under, node, placer, itemstack,
  333. pointed_thing) or itemstack
  334. end
  335. if not pointed_thing.type == "node" then
  336. return
  337. end
  338. if carts:is_rail(pointed_thing.under) then
  339. minetest.add_entity(pointed_thing.under, "carts:cart")
  340. elseif carts:is_rail(pointed_thing.above) then
  341. minetest.add_entity(pointed_thing.above, "carts:cart")
  342. else
  343. return
  344. end
  345. minetest.sound_play({name = "default_place_node_metal", gain = 0.5},
  346. {pos = pointed_thing.above})
  347. if not (creative and creative.is_enabled_for
  348. and creative.is_enabled_for(placer:get_player_name())) then
  349. itemstack:take_item()
  350. end
  351. return itemstack
  352. end,
  353. })
  354. minetest.register_craft({
  355. output = "carts:cart",
  356. recipe = {
  357. {"default:steel_ingot", "", "default:steel_ingot"},
  358. {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"},
  359. },
  360. })