Add pathfinding function (pilot version!)

- Add more comments
This commit is contained in:
SmallJoker 2016-07-04 20:34:52 +02:00
parent 46c03dd4d1
commit d63f8ae024
2 changed files with 47 additions and 18 deletions

View File

@ -157,6 +157,28 @@ function boost_cart:get_rail_direction(pos_, dir, ctrl, old_switch, railtype)
return {x=0, y=0, z=0} return {x=0, y=0, z=0}
end end
function boost_cart:pathfinder(pos_, expected_pos, old_dir, ctrl, pf_switch, railtype)
local pos = vector.round(pos_)
local pf_pos = vector.round(expected_pos)
local pf_dir = vector.new(old_dir)
for i = 1, 3 do
pf_dir, pf_switch = boost_cart:get_rail_direction(pf_pos, pf_dir, ctrl, pf_switch, railtype)
if vector.equals(pf_dir, {x=0, y=0, z=0}) then
-- No way forwards
return false
end
pf_pos = vector.add(pf_pos, pf_dir)
if vector.equals(pf_pos, pos) then
-- Success! Cart moved on correctly
return true
end
end
-- Cart not found
return false
end
function boost_cart:boost_rail(pos, amount) function boost_cart:boost_rail(pos, amount)
minetest.get_meta(pos):set_string("cart_acceleration", tostring(amount)) minetest.get_meta(pos):set_string("cart_acceleration", tostring(amount))
for _,obj_ in ipairs(minetest.get_objects_inside_radius(pos, 0.5)) do for _,obj_ in ipairs(minetest.get_objects_inside_radius(pos, 0.5)) do

View File

@ -4,7 +4,7 @@ boost_cart.modpath = minetest.get_modpath("boost_cart")
-- Maximal speed of the cart in m/s -- Maximal speed of the cart in m/s
boost_cart.speed_max = 10 boost_cart.speed_max = 10
-- Set to nil to disable punching the cart from inside -- Set to nil to disable punching the cart from inside (min = -1)
boost_cart.punch_speed_min = 7 boost_cart.punch_speed_min = 7
@ -103,6 +103,7 @@ function boost_cart.cart:on_punch(puncher, time_from_last_punch, tool_capabiliti
end end
if puncher:get_player_control().sneak then if puncher:get_player_control().sneak then
-- Pick up cart: Drop all attachments
if self.driver then if self.driver then
if self.old_pos then if self.old_pos then
self.object:setpos(self.old_pos) self.object:setpos(self.old_pos)
@ -124,10 +125,9 @@ function boost_cart.cart:on_punch(puncher, time_from_last_punch, tool_capabiliti
return return
end end
local vel = self.object:getvelocity() local vel = self.object:getvelocity()
if puncher:get_player_name() == self.driver then if puncher:get_player_name() == self.driver then
if math.abs(vel.x + vel.z) > (boost_cart.punch_speed_min or -1) then if math.abs(vel.x + vel.z) > boost_cart.punch_speed_min then
return return
end end
end end
@ -162,40 +162,44 @@ function boost_cart.cart:on_step(dtime)
return return
end end
local dir, last_switch = nil, nil -- dir: New moving direction of the cart
-- last_switch: Currently pressed L/R key, used to ignore the key on the next rail node
local dir, last_switch
local pos = self.object:getpos() local pos = self.object:getpos()
if self.old_pos and not self.punched then if self.old_pos and not self.punched then
local flo_pos = vector.floor(pos) local flo_pos = vector.floor(pos)
local flo_old = vector.floor(self.old_pos) local flo_old = vector.floor(self.old_pos)
if vector.equals(flo_pos, flo_old) then if vector.equals(flo_pos, flo_old) then
-- Do not check one node multiple times
return return
end end
end end
local ctrl, player = nil, nil local ctrl, player
-- Get player controls
if self.driver then if self.driver then
player = minetest.get_player_by_name(self.driver) player = minetest.get_player_by_name(self.driver)
if player then if player then
ctrl = player:get_player_control() ctrl = player:get_player_control()
end end
end end
if self.old_pos then if self.old_pos then
local diff = vector.subtract(self.old_pos, pos) -- Detection for "skipping" nodes
for _,v in ipairs({"x","y","z"}) do local expected_pos = vector.add(self.old_pos, self.old_dir)
if math.abs(diff[v]) > 1.1 then local found_path = boost_cart:pathfinder(pos, expected_pos, self.old_dir, ctrl, self.old_switch, self.railtype)
local expected_pos = vector.add(self.old_pos, self.old_dir)
dir, last_switch = boost_cart:get_rail_direction(pos, self.old_dir, ctrl, self.old_switch, self.railtype) if not found_path then
if vector.equals(dir, {x=0, y=0, z=0}) then -- No rail found: reset back to the expected position
dir = false pos = expected_pos
pos = vector.new(expected_pos) update.pos = true
update.pos = true
end
break
end
end end
end end
if vel.y == 0 then if vel.y == 0 then
-- Stop cart completely (do not swing)
for _,v in ipairs({"x", "z"}) do for _,v in ipairs({"x", "z"}) do
if vel[v] ~= 0 and math.abs(vel[v]) < 0.9 then if vel[v] ~= 0 and math.abs(vel[v]) < 0.9 then
vel[v] = 0 vel[v] = 0
@ -213,6 +217,8 @@ function boost_cart.cart:on_step(dtime)
local new_acc = {x=0, y=0, z=0} local new_acc = {x=0, y=0, z=0}
if vector.equals(dir, {x=0, y=0, z=0}) then if vector.equals(dir, {x=0, y=0, z=0}) then
vel = {x=0, y=0, z=0} vel = {x=0, y=0, z=0}
pos = vector.round(pos)
update.pos = true
update.vel = true update.vel = true
else else
-- If the direction changed -- If the direction changed
@ -248,7 +254,8 @@ function boost_cart.cart:on_step(dtime)
end end
end end
end end
acc = acc + (speed_mod * 8) -- Try to make it similar to the original carts mod
acc = acc + (speed_mod * 10)
else else
acc = acc - 0.4 acc = acc - 0.4
-- Handbrake -- Handbrake