cart_entity.lua 12 KB

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