cart_entity.lua 10 KB

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