]> git.lizzy.rs Git - Crafter.git/commitdiff
Add in more advanced rail pathfinding
authoroilboi <47129783+oilboi@users.noreply.github.com>
Sat, 4 Jul 2020 23:03:26 +0000 (19:03 -0400)
committeroilboi <47129783+oilboi@users.noreply.github.com>
Sat, 4 Jul 2020 23:03:26 +0000 (19:03 -0400)
mods/minecart/init.lua

index e9c806e64086e7e7663b037a21aee04e8d9b1610..b61e24bcb53d050fcb5c0148d3cf9ca5ebe1b61e 100644 (file)
@@ -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