cart_entity.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 minetest.is_creative_enabled(puncher:get_player_name())
  103. or not inv:contains_item("main", "carts:cart") then
  104. local leftover = inv:add_item("main", "carts:cart")
  105. -- If no room in inventory add a replacement cart to the world
  106. if not leftover:is_empty() then
  107. minetest.add_item(self.object:get_pos(), leftover)
  108. end
  109. end
  110. self.object:remove()
  111. return
  112. end
  113. -- Player punches cart to alter velocity
  114. if puncher:get_player_name() == self.driver then
  115. if math.abs(vel.x + vel.z) > carts.punch_speed_max then
  116. return
  117. end
  118. end
  119. local punch_dir = carts:velocity_to_dir(puncher:get_look_dir())
  120. punch_dir.y = 0
  121. local cart_dir = carts:get_rail_direction(pos, punch_dir, nil, nil, self.railtype)
  122. if vector.equals(cart_dir, {x=0, y=0, z=0}) then
  123. return
  124. end
  125. local punch_interval = 1
  126. if tool_capabilities and tool_capabilities.full_punch_interval then
  127. punch_interval = tool_capabilities.full_punch_interval
  128. end
  129. time_from_last_punch = math.min(time_from_last_punch or punch_interval, punch_interval)
  130. local f = 2 * (time_from_last_punch / punch_interval)
  131. self.velocity = vector.multiply(cart_dir, f)
  132. self.old_dir = cart_dir
  133. self.punched = true
  134. end
  135. local function rail_on_step_event(handler, obj, dtime)
  136. if handler then
  137. handler(obj, dtime)
  138. end
  139. end
  140. -- sound refresh interval = 1.0sec
  141. local function rail_sound(self, dtime)
  142. if not self.sound_ttl then
  143. self.sound_ttl = 1.0
  144. return
  145. elseif self.sound_ttl > 0 then
  146. self.sound_ttl = self.sound_ttl - dtime
  147. return
  148. end
  149. self.sound_ttl = 1.0
  150. if self.sound_handle then
  151. local handle = self.sound_handle
  152. self.sound_handle = nil
  153. minetest.after(0.2, minetest.sound_stop, handle)
  154. end
  155. local vel = self.object:get_velocity()
  156. local speed = vector.length(vel)
  157. if speed > 0 then
  158. self.sound_handle = minetest.sound_play(
  159. "carts_cart_moving", {
  160. object = self.object,
  161. gain = (speed / carts.speed_max) / 2,
  162. loop = true,
  163. })
  164. end
  165. end
  166. local function get_railparams(pos)
  167. local node = minetest.get_node(pos)
  168. return carts.railparams[node.name] or {}
  169. end
  170. local v3_len = vector.length
  171. local function rail_on_step(self, dtime)
  172. local vel = self.object:get_velocity()
  173. if self.punched then
  174. vel = vector.add(vel, self.velocity)
  175. self.object:set_velocity(vel)
  176. self.old_dir.y = 0
  177. elseif vector.equals(vel, {x=0, y=0, z=0}) then
  178. return
  179. end
  180. local pos = self.object:get_pos()
  181. local cart_dir = carts:velocity_to_dir(vel)
  182. local same_dir = vector.equals(cart_dir, self.old_dir)
  183. local update = {}
  184. if self.old_pos and not self.punched and same_dir then
  185. local flo_pos = vector.round(pos)
  186. local flo_old = vector.round(self.old_pos)
  187. if vector.equals(flo_pos, flo_old) then
  188. -- Do not check one node multiple times
  189. return
  190. end
  191. end
  192. local ctrl, player
  193. -- Get player controls
  194. if self.driver then
  195. player = minetest.get_player_by_name(self.driver)
  196. if player then
  197. ctrl = player:get_player_control()
  198. end
  199. end
  200. local stop_wiggle = false
  201. if self.old_pos and same_dir then
  202. -- Detection for "skipping" nodes (perhaps use average dtime?)
  203. -- It's sophisticated enough to take the acceleration in account
  204. local acc = self.object:get_acceleration()
  205. local distance = dtime * (v3_len(vel) + 0.5 * dtime * v3_len(acc))
  206. local new_pos, new_dir = carts:pathfinder(
  207. pos, self.old_pos, self.old_dir, distance, ctrl,
  208. self.old_switch, self.railtype
  209. )
  210. if new_pos then
  211. -- No rail found: set to the expected position
  212. pos = new_pos
  213. update.pos = true
  214. cart_dir = new_dir
  215. end
  216. elseif self.old_pos and self.old_dir.y ~= 1 and not self.punched then
  217. -- Stop wiggle
  218. stop_wiggle = true
  219. end
  220. local railparams
  221. -- dir: New moving direction of the cart
  222. -- switch_keys: Currently pressed L/R key, used to ignore the key on the next rail node
  223. local dir, switch_keys = carts:get_rail_direction(
  224. pos, cart_dir, ctrl, self.old_switch, self.railtype
  225. )
  226. local dir_changed = not vector.equals(dir, self.old_dir)
  227. local new_acc = {x=0, y=0, z=0}
  228. if stop_wiggle or vector.equals(dir, {x=0, y=0, z=0}) then
  229. vel = {x = 0, y = 0, z = 0}
  230. local pos_r = vector.round(pos)
  231. if not carts:is_rail(pos_r, self.railtype)
  232. and self.old_pos then
  233. pos = self.old_pos
  234. elseif not stop_wiggle then
  235. pos = pos_r
  236. else
  237. pos.y = math.floor(pos.y + 0.5)
  238. end
  239. update.pos = true
  240. update.vel = true
  241. else
  242. -- Direction change detected
  243. if dir_changed then
  244. vel = vector.multiply(dir, math.abs(vel.x + vel.z))
  245. update.vel = true
  246. if dir.y ~= self.old_dir.y then
  247. pos = vector.round(pos)
  248. update.pos = true
  249. end
  250. end
  251. -- Center on the rail
  252. if dir.z ~= 0 and math.floor(pos.x + 0.5) ~= pos.x then
  253. pos.x = math.floor(pos.x + 0.5)
  254. update.pos = true
  255. end
  256. if dir.x ~= 0 and math.floor(pos.z + 0.5) ~= pos.z then
  257. pos.z = math.floor(pos.z + 0.5)
  258. update.pos = true
  259. end
  260. -- Slow down or speed up..
  261. local acc = dir.y * -4.0
  262. -- Get rail for corrected position
  263. railparams = get_railparams(pos)
  264. -- no need to check for railparams == nil since we always make it exist.
  265. local speed_mod = railparams.acceleration
  266. if speed_mod and speed_mod ~= 0 then
  267. -- Try to make it similar to the original carts mod
  268. acc = acc + speed_mod
  269. else
  270. -- Handbrake or coast
  271. if ctrl and ctrl.down then
  272. acc = acc - 3
  273. else
  274. acc = acc - 0.4
  275. end
  276. end
  277. new_acc = vector.multiply(dir, acc)
  278. end
  279. -- Limits
  280. local max_vel = carts.speed_max
  281. for _, v in pairs({"x","y","z"}) do
  282. if math.abs(vel[v]) > max_vel then
  283. vel[v] = carts:get_sign(vel[v]) * max_vel
  284. new_acc[v] = 0
  285. update.vel = true
  286. end
  287. end
  288. self.object:set_acceleration(new_acc)
  289. self.old_pos = vector.round(pos)
  290. if not vector.equals(dir, {x=0, y=0, z=0}) and not stop_wiggle then
  291. self.old_dir = vector.new(dir)
  292. end
  293. self.old_switch = switch_keys
  294. if self.punched then
  295. -- Collect dropped items
  296. for _, obj_ in pairs(minetest.get_objects_inside_radius(pos, 1)) do
  297. local ent = obj_:get_luaentity()
  298. -- Careful here: physical_state and disable_physics are item-internal APIs
  299. if ent and ent.name == "__builtin:item" and ent.physical_state then
  300. ent:disable_physics()
  301. obj_:set_attach(self.object, "", {x=0, y=0, z=0}, {x=0, y=0, z=0})
  302. self.attached_items[#self.attached_items + 1] = obj_
  303. end
  304. end
  305. self.punched = false
  306. update.vel = true
  307. end
  308. railparams = railparams or get_railparams(pos)
  309. if not (update.vel or update.pos) then
  310. rail_on_step_event(railparams.on_step, self, dtime)
  311. return
  312. end
  313. local yaw = 0
  314. if self.old_dir.x < 0 then
  315. yaw = 0.5
  316. elseif self.old_dir.x > 0 then
  317. yaw = 1.5
  318. elseif self.old_dir.z < 0 then
  319. yaw = 1
  320. end
  321. self.object:set_yaw(yaw * math.pi)
  322. local anim = {x=0, y=0}
  323. if dir.y == -1 then
  324. anim = {x=1, y=1}
  325. elseif dir.y == 1 then
  326. anim = {x=2, y=2}
  327. end
  328. self.object:set_animation(anim, 1, 0)
  329. if update.vel then
  330. self.object:set_velocity(vel)
  331. end
  332. if update.pos then
  333. if dir_changed then
  334. self.object:set_pos(pos)
  335. else
  336. self.object:move_to(pos)
  337. end
  338. end
  339. -- call event handler
  340. rail_on_step_event(railparams.on_step, self, dtime)
  341. end
  342. function cart_entity:on_step(dtime)
  343. rail_on_step(self, dtime)
  344. rail_sound(self, dtime)
  345. end
  346. minetest.register_entity("carts:cart", cart_entity)
  347. minetest.register_craftitem("carts:cart", {
  348. description = S("Cart") .. "\n" .. S("(Sneak+Click to pick up)"),
  349. inventory_image = minetest.inventorycube("carts_cart_top.png", "carts_cart_front.png", "carts_cart_side.png"),
  350. wield_image = "carts_cart_front.png",
  351. on_place = function(itemstack, placer, pointed_thing)
  352. local under = pointed_thing.under
  353. local node = minetest.get_node(under)
  354. local udef = minetest.registered_nodes[node.name]
  355. if udef and udef.on_rightclick and
  356. not (placer and placer:is_player() and
  357. placer:get_player_control().sneak) then
  358. return udef.on_rightclick(under, node, placer, itemstack,
  359. pointed_thing) or itemstack
  360. end
  361. if not pointed_thing.type == "node" then
  362. return
  363. end
  364. if carts:is_rail(pointed_thing.under) then
  365. minetest.add_entity(pointed_thing.under, "carts:cart")
  366. elseif carts:is_rail(pointed_thing.above) then
  367. minetest.add_entity(pointed_thing.above, "carts:cart")
  368. else
  369. return
  370. end
  371. minetest.sound_play({name = "default_place_node_metal", gain = 0.5},
  372. {pos = pointed_thing.above}, true)
  373. if not minetest.is_creative_enabled(placer:get_player_name()) then
  374. itemstack:take_item()
  375. end
  376. return itemstack
  377. end,
  378. })
  379. minetest.register_craft({
  380. output = "carts:cart",
  381. recipe = {
  382. {"default:steel_ingot", "", "default:steel_ingot"},
  383. {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"},
  384. },
  385. })