cart_entity.lua 11 KB

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