From b8ac747ad80bbaffafa9ff12112b42b5bfed7c93 Mon Sep 17 00:00:00 2001 From: oilboi <47129783+oilboi@users.noreply.github.com> Date: Sat, 4 Jul 2020 19:03:26 -0400 Subject: [PATCH] Add in more advanced rail pathfinding --- mods/minecart/init.lua | 127 ++++++++++++++++++++++++----------------- 1 file changed, 75 insertions(+), 52 deletions(-) diff --git a/mods/minecart/init.lua b/mods/minecart/init.lua index e9c806e..b61e24b 100644 --- a/mods/minecart/init.lua +++ b/mods/minecart/init.lua @@ -68,68 +68,87 @@ end local function direction_snap(self) local dir = self.dir + local pitch = 0 + if dir.y == 1 then pitch = math.pi/4 end + if dir.y == -1 then pitch = -math.pi/4 end local yaw = minetest.dir_to_yaw(dir) - self.object:set_rotation(vector.new(0,yaw,0)) + self.object:set_rotation(vector.new(pitch,yaw,0)) end local function turn_snap(pos,self,dir,dir2) - if dir.x ~= 0 and dir2.z ~= 0 then - local inertia = math.abs(self.velocity.x) - self.velocity = vector.multiply(dir2,inertia) - self.dir = dir2 - self.axis_lock = "z" - self.object:set_pos(pos) - direction_snap(self) - return(true) - elseif dir.z ~= 0 and dir2.x ~= 0 then - local inertia = math.abs(self.velocity.z) - self.velocity = vector.multiply(dir2,inertia) - self.dir = dir2 - self.axis_lock = "x" - self.object:set_pos(pos) - direction_snap(self) - return(true) + if self.axis_lock == "x" then + if dir.x ~= 0 and dir2.z ~= 0 then + local inertia = math.abs(self.velocity.x) + self.velocity = vector.multiply(dir2,inertia) + self.dir = dir2 + self.axis_lock = "z" + self.object:set_pos(pos) + direction_snap(self) + return(true) + end + end + if self.axis_lock == "z" then + if dir.z ~= 0 and dir2.x ~= 0 then + local inertia = math.abs(self.velocity.z) + self.velocity = vector.multiply(dir2,inertia) + self.dir = dir2 + self.axis_lock = "x" + self.object:set_pos(pos) + direction_snap(self) + return(true) + end end + return(false) end local function climb_snap(pos,self,dir,dir2) - if dir.x == dir2.x and dir2.y ~= 0 then - print(dump(dir2)) - print("x snapping") - local inertia = math.abs(self.velocity.x) - self.velocity = vector.multiply(dir2,inertia) - self.dir = dir2 - self.axis_lock = "x" - self.object:set_pos(pos) - direction_snap(self) - return(true) - elseif dir.z == dir2.z and dir2.y ~= 0 then - local inertia = math.abs(self.velocity.z) - self.velocity = vector.multiply(dir2,inertia) - self.dir = dir2 - self.axis_lock = "z" - self.object:set_pos(pos) - direction_snap(self) - return(true) + if self.axis_lock == "x" then + if dir.x == dir2.x and dir2.y ~= 0 then + local inertia = math.abs(self.velocity.x) + self.velocity = vector.multiply(dir2,inertia) + self.dir = dir2 + self.axis_lock = "x" + self.object:set_pos(pos) + direction_snap(self) + return(true) + end end + if self.axis_lock == "z" then + if dir.z == dir2.z and dir2.y ~= 0 then + local inertia = math.abs(self.velocity.z) + self.velocity = vector.multiply(dir2,inertia) + self.dir = dir2 + self.axis_lock = "z" + self.object:set_pos(pos) + direction_snap(self) + return(true) + end + end + return(false) end local function straight_snap(pos,self,dir) - if dir.x ~= 0 and pool[minetest.hash_node_position(vector.add(pos,vector.new(dir.x,0,0)))] then - self.velocity = vector.new(self.velocity.x,0,0) - self.dir = vector.new(dir.x,0,0) - self.axis_lock = "x" - self.object:set_pos(pos) - direction_snap(self) - return(true) - elseif dir.z ~= 0 and pool[minetest.hash_node_position(vector.add(pos,vector.new(0,0,dir.z)))] then - self.velocity = vector.new(0,0,self.velocity.z) - self.dir = vector.new(0,0,dir.z) - self.axis_lock = "z" - self.object:set_pos(pos) - direction_snap(self) - return(true) + if self.axis_lock == "x" then + if dir.x ~= 0 and pool[minetest.hash_node_position(vector.add(pos,vector.new(dir.x,0,0)))] then + self.velocity = vector.new(self.velocity.x,0,0) + self.dir = vector.new(dir.x,0,0) + self.axis_lock = "x" + self.object:set_pos(pos) + direction_snap(self) + return(true) + end end + if self.axis_lock == "z" then + if dir.z ~= 0 and pool[minetest.hash_node_position(vector.add(pos,vector.new(0,0,dir.z)))] then + self.velocity = vector.new(0,0,self.velocity.z) + self.dir = vector.new(0,0,dir.z) + self.axis_lock = "z" + self.object:set_pos(pos) + direction_snap(self) + return(true) + end + end + return(false) end local function rail_brain(self,pos) @@ -143,7 +162,7 @@ local function rail_brain(self,pos) local triggered = false - if dir.x < 0 and pos2.x < pos.x then + if dir.x < 0 and pos2.x < pos.x then triggered = true elseif dir.x > 0 and pos2.x > pos.x then triggered = true @@ -157,6 +176,7 @@ local function rail_brain(self,pos) if triggered and not pool[minetest.hash_node_position(vector.add(pos,dir))] then if straight_snap(pos,self,dir) then + print('beraking') return end @@ -167,10 +187,13 @@ local function rail_brain(self,pos) --stop slow down become physical, something else for _,dir2 in pairs(possible_dirs) do - if turn_snap(pos,self,dir,dir2) then + if climb_snap(pos,self,dir,dir2) then return end - if climb_snap(pos,self,dir,dir2) then + end + + for _,dir2 in pairs(possible_dirs) do + if turn_snap(pos,self,dir,dir2) then return end end -- 2.44.0