diff --git a/README.txt b/README.txt index 4ec9bb8..b67ba19 100644 --- a/README.txt +++ b/README.txt @@ -20,4 +20,4 @@ kddekadenz: Zeg9: cart.x - cart.png + cart.png \ No newline at end of file diff --git a/functions.lua b/functions.lua index b6bab4e..32b7e9f 100644 --- a/functions.lua +++ b/functions.lua @@ -19,10 +19,45 @@ function boost_cart:is_rail(pos) return minetest.get_item_group(node, "rail") ~= 0 end -function boost_cart:get_rail_direction(pos_, dir_) +function boost_cart:get_rail_direction(pos_, dir_, ctrl, old_switch) local pos = vector.round(pos_) local dir = vector.new(dir_) local cur = nil + local left_check, right_check = true, true + old_switch = old_switch or 0 + + -- Check left and right + local left = {x=0, y=0, z=0} + local right = {x=0, y=0, z=0} + if dir.z ~= 0 and dir.x == 0 then + left.x = -dir.z + right.x = dir.z + elseif dir.x ~= 0 and dir.z == 0 then + left.z = dir.x + right.z = -dir.x + end + + if ctrl then + if old_switch == 1 then + left_check = false + elseif old_switch == 2 then + right_check = false + end + if ctrl.left and left_check then + cur = vector.add(pos, left) + if boost_cart:is_rail(cur) then + return left, 1 + end + left_check = false + end + if ctrl.right and right_check then + cur = vector.add(pos, right) + if boost_cart:is_rail(cur) then + return right, 2 + end + right_check = true + end + end -- Front dir.y = 0 @@ -45,58 +80,20 @@ function boost_cart:get_rail_direction(pos_, dir_) return dir end - -- Left, right - dir.y = 0 - - -- Check left and right - local view, opposite, val - - if dir.x == 0 and dir.z ~= 0 then - view = "z" - other = "x" - if dir.z < 0 then - val = {1, -1} - else - val = {-1, 1} + -- Left, if not already checked + if left_check then + cur = vector.add(pos, left) + if boost_cart:is_rail(cur) then + return left end - elseif dir.z == 0 and dir.x ~= 0 then - view = "x" - other = "z" - if dir.x > 0 then - val = {1, -1} - else - val = {-1, 1} + end + + -- Right, if not already checked + if right_check then + cur = vector.add(pos, right) + if boost_cart:is_rail(cur) then + return right end - else - return {x=0, y=0, z=0} - end - - dir[view] = 0 - dir[other] = val[1] - cur = vector.add(pos, dir) - if boost_cart:is_rail(cur) then - return dir - end - - -- Down - dir.y = -1 - cur = vector.add(pos, dir) - if boost_cart:is_rail(cur) then - return dir - end - dir.y = 0 - - dir[other] = val[2] - cur = vector.add(pos, dir) - if boost_cart:is_rail(cur) then - return dir - end - - -- Down - dir.y = -1 - cur = vector.add(pos, dir) - if boost_cart:is_rail(cur) then - return dir end return {x=0, y=0, z=0} diff --git a/init.lua b/init.lua index 9c56ae4..6e86bfa 100644 --- a/init.lua +++ b/init.lua @@ -29,7 +29,8 @@ boost_cart.cart = { punch = false, -- used to re-send velocity and position velocity = {x=0, y=0, z=0}, -- only used on punch old_dir = {x=0, y=0, z=0}, - old_pos = nil + old_pos = nil, + old_switch = nil } function boost_cart.cart:on_rightclick(clicker) @@ -42,7 +43,7 @@ function boost_cart.cart:on_rightclick(clicker) clicker:set_detach() elseif not self.driver then self.driver = player_name - clicker:set_attach(self.object, "", {x=0, y=5, z=0}, {x=0, y=0, z=0}) + clicker:set_attach(self.object, "", {x=0,y=0.5,z=0}, {x=0,y=0,z=0}) end end @@ -57,6 +58,7 @@ function boost_cart.cart:on_punch(puncher, time_from_last_punch, tool_capabiliti if puncher:get_player_control().sneak then if self.driver then + default.player_attached[self.driver] = nil local player = minetest.get_player_by_name(self.driver) if player then player:set_detach() @@ -111,7 +113,7 @@ function boost_cart.cart:on_step(dtime) return end - local dir = false + local dir, last_switch = nil, nil local pos = self.object:getpos() if self.old_pos and not self.punch then local flo_pos = vector.floor(pos) @@ -120,12 +122,19 @@ function boost_cart.cart:on_step(dtime) return end end + local ctrl = nil + if self.driver then + local player = minetest.get_player_by_name(self.driver) + if player then + ctrl = player:get_player_control() + end + end if self.old_pos then local diff = vector.subtract(self.old_pos, pos) for _,v in ipairs({"x","y","z"}) do if math.abs(diff[v]) > 1.2 then local expected_pos = vector.add(self.old_pos, self.old_dir) - dir = boost_cart:get_rail_direction(pos, self.old_dir) + dir, last_switch = boost_cart:get_rail_direction(pos, self.old_dir, ctrl, self.old_switch) if vector.equals(dir, {x=0, y=0, z=0}) then dir = false pos = vector.new(expected_pos) @@ -154,7 +163,7 @@ function boost_cart.cart:on_step(dtime) local max_vel = boost_cart.speed_max if not dir then - dir = boost_cart:get_rail_direction(pos, cart_dir) + dir, last_switch = boost_cart:get_rail_direction(pos, cart_dir, ctrl, self.old_switch) end if vector.equals(dir, {x=0, y=0, z=0}) then vel = {x=0, y=0, z=0} @@ -194,9 +203,13 @@ function boost_cart.cart:on_step(dtime) end end end - acc = acc + (speed_mod * 7) + acc = acc + (speed_mod * 8) else acc = acc - 0.4 + -- Handbrake + if ctrl and ctrl.down then + acc = acc - 0.8 + end end local new_acc = { @@ -210,6 +223,7 @@ function boost_cart.cart:on_step(dtime) self.old_pos = vector.new(pos) self.old_dir = vector.new(dir) + self.old_switch = last_switch -- Limits for _,v in ipairs({"x","y","z"}) do diff --git a/textures/carts_rail_brk.png b/textures/carts_rail_brk.png index f3e0ff9..2717bd5 100644 Binary files a/textures/carts_rail_brk.png and b/textures/carts_rail_brk.png differ diff --git a/textures/carts_rail_cp.png b/textures/carts_rail_cp.png index a826d94..119461f 100644 Binary files a/textures/carts_rail_cp.png and b/textures/carts_rail_cp.png differ diff --git a/textures/carts_rail_crossing_brk.png b/textures/carts_rail_crossing_brk.png index 3ace508..f9ce303 100644 Binary files a/textures/carts_rail_crossing_brk.png and b/textures/carts_rail_crossing_brk.png differ diff --git a/textures/carts_rail_crossing_cp.png b/textures/carts_rail_crossing_cp.png index 6b55059..b04aa63 100644 Binary files a/textures/carts_rail_crossing_cp.png and b/textures/carts_rail_crossing_cp.png differ diff --git a/textures/carts_rail_curved_brk.png b/textures/carts_rail_curved_brk.png index 5a84918..5759cc8 100644 Binary files a/textures/carts_rail_curved_brk.png and b/textures/carts_rail_curved_brk.png differ diff --git a/textures/carts_rail_curved_cp.png b/textures/carts_rail_curved_cp.png index 7364d22..05e83ff 100644 Binary files a/textures/carts_rail_curved_cp.png and b/textures/carts_rail_curved_cp.png differ diff --git a/textures/carts_rail_t_junction_brk.png b/textures/carts_rail_t_junction_brk.png index 0c2c1cb..4f98249 100644 Binary files a/textures/carts_rail_t_junction_brk.png and b/textures/carts_rail_t_junction_brk.png differ diff --git a/textures/carts_rail_t_junction_cp.png b/textures/carts_rail_t_junction_cp.png index 27d96cd..f9f362a 100644 Binary files a/textures/carts_rail_t_junction_cp.png and b/textures/carts_rail_t_junction_cp.png differ diff --git a/textures/carts_rail_t_junction_pwr.png b/textures/carts_rail_t_junction_pwr.png index 7f97fc7..4812b88 100644 Binary files a/textures/carts_rail_t_junction_pwr.png and b/textures/carts_rail_t_junction_pwr.png differ diff --git a/textures/default_rail_t_junction.png b/textures/default_rail_t_junction.png new file mode 100644 index 0000000..a72d7ab Binary files /dev/null and b/textures/default_rail_t_junction.png differ