Advertisement
Akuukis

Pathfinding A* 2D

May 1st, 2014
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 18.79 KB | None | 0 0
  1. --[[ Header
  2. ColorWars algorithm for LUA
  3. Authors: Akuukis, Hugo, LatvianModder, TomsG
  4. Started: May 1, 2014
  5. First Beta release: -
  6. First Alpha release: -
  7. Github address: https://github.com/Akuukis/TurtleColorWars
  8. --]]
  9. --[[Structure
  10. Main - Starts Init. and afterwards runs Job,Supp,Comm in paralel. Subclasses Nav,Debug,Stats are present.
  11. Init.## Initiation - Initiates the turtle so it could operate
  12. Job.### Decision Making - Turtle weights alternatives, chooses his next AIM (strategy level) and does it (tactical level)
  13. Supp.## Maintance - Turtle's AIM can be interrupted by inside events, like low on fuel, full backpack or afraid of night. Overrides AIM & does it.
  14. Comm.## Communication - Turtle's AIM can be interrupted by outside events, like call for help or direct command. Sets AIM & does Job##.
  15. Nav.### Navigation - Turtle goes to point XYZ (sub-tactical level) and constantly updates internal map.
  16. Debug.# Debug - (sub-tactical level)
  17. Stats.# Statistics - Turtle collents & stores individual statistics for fun purposes only.
  18. --]]
  19.  
  20. -- Classes
  21. Debug = {
  22.     Level = {GUI = 5, RUI = 5, HUD = 5}, -- Display level of information, 0 - Fatal, 1 - Error, 2 - Warning, 3 - Info, 4 - Debug, 5 - All.
  23.     Hugo = function(self, x, y) -- Redundant
  24.         modem.transmit(3, 1337, x.." "..y)
  25.         end,
  26.     PrintMap = function(self,Type)
  27.         y=0;
  28.         for x=Nav.X+3,Nav.X-3,-1 do
  29.             for z=Nav.Z-19,Nav.Z+19,1 do
  30.                 if Nav.Map[x]==nil then
  31.                     Debug:Info(".")
  32.                     elseif Nav.Map[x][z]==nil then
  33.                     Debug:Info(".")
  34.                     else
  35.                     if (Nav.X==x and Nav.Z==z) then Debug:Info("#") else
  36.                         if Nav.Map[x][z][y].Id==0 then Debug:Info(" ") else Debug:Info("%s",Nav.Map[x][z][y].Id) end
  37.                         end
  38.                     end
  39.                 end
  40.             Debug:Info("\n")
  41.             end
  42.         end,
  43.     Check = function(self,...)
  44.         Debug:Debug(...)
  45.         while true do
  46.             event, param1 = os.pullEvent()
  47.             if event == "key" then break end
  48.             end
  49.         end,
  50.     myformat = function( fmt, ... )
  51.     -- Disclaimer: code (Sep 7, 2013) by Lorenzo Donati @ http://stackoverflow.com/users/2633423/lorenzo-donati
  52.         local buf = {}
  53.         for i = 1, select( '#', ... ) do
  54.             local a = select( i, ... )
  55.             if type( a ) ~= 'string' and type( a ) ~= 'number' then
  56.                 a = tostring( a )
  57.                 end
  58.             buf[i] = a
  59.             end
  60.         return string.format( fmt, unpack( buf ) )
  61.      end,
  62.     Debug = function(self, ...)
  63.         --for w in string.gmatch(debug.traceback(nil,2+1), ".lua:") do Debug:Info("  ") end -- ComputerCraft doesn't have debug
  64.         if Debug.Level.GUI >= 4 then io.write(Debug.myformat(...)) end
  65.         end,
  66.     Info = function(self, ...)
  67.         --for w in string.gmatch(debug.traceback(nil,2+1), ".lua:") do Debug:Info("  ") end -- ComputerCraft doesn't have debug
  68.         if Debug.Level.GUI >= 3 then io.write(Debug.myformat(...)) end
  69.         end,
  70.     Warning = function(self, ...)
  71.         --for w in string.gmatch(debug.traceback(nil,2+1), ".lua:") do Debug:Info("  ") end -- ComputerCraft doesn't have debug
  72.         if Debug.Level.GUI >= 2 then io.write(Debug.myformat(...)) end
  73.         end,
  74.     Error = function(self, ...)
  75.         --for w in string.gmatch(debug.traceback(nil,2+1), ".lua:") do Debug:Info("  ") end -- ComputerCraft doesn't have debug
  76.         if Debug.Level.GUI >= 1 then io.write(Debug.myformat(...)) end
  77.         end,
  78.     Fatal = function(self, ...)
  79.         --for w in string.gmatch(debug.traceback(nil,2+1), ".lua:") do Debug:Info("  ") end -- ComputerCraft doesn't have debug
  80.         if Debug.Level.GUI >= 0 then io.write(Debug.myformat(...)) end
  81.         end
  82.     }
  83. Debug:Info("\nInitializing the program...\n")
  84. Init = {
  85.     Born = function(self)
  86.         Nav:UpdateMap({0,0,0},0)
  87.         end
  88.     }
  89. Job = {
  90.     }
  91. Supp = {
  92.     Refuel = function(self)
  93.         if turtle.getFuelLevel() < 128 then
  94.             if turtle.refuel()then
  95.                 write("Refueled!\n")
  96.                 else
  97.                 write("Fuel me!\n")
  98.                 end
  99.             end
  100.         return turtle.getFuelLevel()
  101.         end
  102.     }
  103. Comm = {
  104.     }
  105. PControl = {
  106.     }
  107. GUI = {
  108.     }
  109. Nav = {
  110.     -- To go, use Nav:Go([x],[z],[y],[tries],[style]). Default: x,z,y = current position, tries = 32, style = 0.
  111.     -- To turn, use Nav:TurnRight(), Nav:TurnLeft(), Nav:TurnAround().
  112.     -- To move forward use Nav:Go(1,0,0,nil,"RelativeDir"), not Nav:Move(dir)
  113.     X = 0, -- North
  114.     Z = 0, -- East
  115.     Y = 0, -- Height
  116.     F = 0, -- Facing direction, modulus of 4 // 0,1,2,3 = North, East, South, West
  117.     Map = {}, -- Id={nil=unexplored,false=air,####=Block}, Updated=server's time, Evade={nil,true if tagged by special events}
  118.     GetPath = function(self,tx,tz,ty)  
  119.     --[[ DISCLAIMER:
  120.     This code (May 04, 2014) is written by Akuukis
  121.         who based on code (Sep 21, 2006) by Altair
  122.             who ported and upgraded code of LMelior
  123.     --]]
  124.     --[[ PRE:
  125.     Nav.Map[][][] is a 3d infinite array (.Id, .Updated, .Evade)
  126.     Nav.X is the player's current x or North
  127.     Nav.Z is the player's current z or East
  128.     Nav.Y is the player's current y or Height (not yet implemented)
  129.     tx is the target x
  130.     tz is the target z
  131.     ty is the target y (not yet implemented)
  132.     style is the preference (not yet implemented)
  133.  
  134.     Note: all the x and z are the x and z to be used in the table.
  135.     By this I mean, if the table is 3 by 2, the x can be 1,2,3 and the z can be 1 or 2.
  136.     --]]
  137.  
  138.     --[[ POST:
  139.     path is a list with all the x and y coords of the nodes of the path to the target.
  140.     OR nil if closedlist==nil
  141.     --]]
  142.     if ty == nil then ty = 0 end
  143.     Debug:Debug("Nav:GetPath(%s, %s)\n",tx,tz,ty)
  144.     local tempG=0
  145.     local tempH=math.abs(Nav.X-tx)+math.abs(Nav.Y-tz)                       -- Manhattan's method
  146.     local closedlist={}                                                                                 -- Initialize table to store checked gridsquares
  147.     local openlist={}                                                                                       -- Initialize table to store possible moves
  148.     openlist[1]={x=Nav.X, z=Nav.Z, g=0, h=tempH, f=0+tempH ,par=1}  -- Make starting point in list
  149.     local curbase={}                                                                                        -- Current square from which to check possible moves
  150.     local basis=1                                                                                               -- Index of current base
  151.     local openk=1                                                                                               -- Openlist counter
  152.     local closedk=0                                                                                         -- Closedlist counter
  153.     Nav:ExploreMap(tx,tz,ty)
  154.     Debug:Debug("Nav:GetPath() CurbaseXZ: ")
  155.     while openk>0  and table.maxn(closedlist)<500 do   -- Growing loop
  156.         --Debug:Check("")
  157.         if Nav.Map[tx][tz][ty].Id then if Nav.Map[tx][tz][ty].Id > 0 then return nil end end
  158.        
  159.         -- Get the lowest f of the openlist
  160.         local lowestF=openlist[openk].f
  161.         basis=openk
  162.         for i=openk,1,-1 do
  163.             if openlist[i].f<lowestF then
  164.                 lowestF=openlist[i].f
  165.                 basis=i
  166.                 end
  167.             end
  168.  
  169.         closedk=closedk+1
  170.         table.insert(closedlist,closedk,openlist[basis]) --Inserts element openlist[basis] at position closedk in table closedlist,         shifting up other elements to open space, if necessary. // HERE: inserts at the end.
  171.  
  172.         curbase=closedlist[closedk]              -- define current base from which to grow list
  173.         curbase.y=0
  174.         Debug:Debug("(%s,%s@%s/%s) ",curbase.x, curbase.z,closedk,openk)
  175.        
  176.         local NorthOK=true
  177.         local SouthOK=true                           -- Booleans defining if they're OK to add
  178.         local EastOK=true                            -- (must be reset for each while loop)
  179.         local WestOK=true
  180.  
  181.         -- If it IS on the map, check map for obstacles
  182.         --(Lua returns an error if you try to access a table position that doesn't exist, so you can't combine it with above)
  183.         local suc=true
  184.         local id=0
  185.         suc,id = pcall(function() if Nav.Map[curbase.x+1][curbase.z][curbase.y].Id>0 then return 1 else return 0 end end)
  186.         if suc~=true then NorthOK = true elseif id == 0 then NorthOK = true else NorthOK = false; Debug:Debug("North obstacle") end
  187.         suc,id = pcall(function() if Nav.Map[curbase.x][curbase.z+1][curbase.y].Id>0 then return 1 else return 0 end end)
  188.         if suc~=true then EastOK = true elseif id == 0 then EastOK = true else EastOK = false; Debug:Debug("East obstacle") end
  189.         suc,id = pcall(function() if Nav.Map[curbase.x-1][curbase.z][curbase.y].Id>0 then return 1 else return 0 end end)
  190.         if suc~=true then SouthOK = true elseif id == 0 then SouthOK = true else SouthOK = false; Debug:Debug("South obstacle") end
  191.         suc,id = pcall(function() if Nav.Map[curbase.x][curbase.z-1][curbase.y].Id>0 then return 1 else return 0 end end)    
  192.         if suc~=true then WestOK = true elseif id == 0 then WestOK = true else WestOK = false; Debug:Debug("West obstacle") end    
  193.                
  194.         -- Look through closedlist
  195.         if closedk>0 then
  196.             for i=1,closedk do
  197.                 if (closedlist[i].x==curbase.x+1 and closedlist[i].z==curbase.z) then NorthOK=false end
  198.                 if (closedlist[i].x==curbase.x-1 and closedlist[i].z==curbase.z) then SouthOK=false end
  199.                 if (closedlist[i].x==curbase.x and closedlist[i].z==curbase.z+1) then EastOK=false  end
  200.                 if (closedlist[i].x==curbase.x and closedlist[i].z==curbase.z-1) then WestOK=false  end
  201.             end
  202.         end
  203.        
  204.         -- Check if next points are on the map and within moving distance
  205.         --[[ Akuukis: Map is infinite
  206.         if curbase.x+1>xsize then
  207.             NorthOK=false
  208.         end
  209.         if curbase.x-1<1 then
  210.             SouthOK=false
  211.         end
  212.         if curbase.z+1>ysize then
  213.             EastOK=false
  214.         end
  215.         if curbase.z-1<1 then
  216.             WestOK=false
  217.         end
  218.         --]]
  219.  
  220.  
  221.         --[[
  222.         if Nav.Map[curbase.x+1][curbase.z].Id~=false then NorthOK=false end
  223.         if Nav.Map[curbase.x][curbase.z+1].Id~=false then EastOK=false  end
  224.         if Nav.Map[curbase.x-1][curbase.z].Id~=false then SouthOK=false end
  225.         if Nav.Map[curbase.x][curbase.z-1].Id~=false then WestOK=false  end
  226.         --]]
  227.        
  228.         -- check if the move from the current base is shorter then from the former parent
  229.         tempG=curbase.g+1
  230.         for i=1,openk do
  231.             if NorthOK and openlist[i].x==curbase.x+1 and openlist[i].z==curbase.z and openlist[i].g>tempG then
  232.                 tempH=math.abs((curbase.x+1)-tx)+math.abs(curbase.z-tz)
  233.                 table.remove(openlist,i)
  234.                 table.insert(openlist,i,{x=curbase.x+1, z=curbase.z, g=tempG, h=tempH, f=tempG+tempH, par=closedk})
  235.                 NorthOK=false
  236.                 end
  237.        
  238.             if SouthOK and openlist[i].x==curbase.x-1 and openlist[i].z==curbase.z and openlist[i].g>tempG then
  239.                 tempH=math.abs((curbase.x-1)-tx)+math.abs(curbase.z-tz)
  240.                 table.remove(openlist,i)
  241.                 table.insert(openlist,i,{x=curbase.x-1, z=curbase.z, g=tempG, h=tempH, f=tempG+tempH, par=closedk})
  242.                 SouthOK=false
  243.                 end
  244.  
  245.             if EastOK and openlist[i].x==curbase.x and openlist[i].z==curbase.z+1 and openlist[i].g>tempG then
  246.                 tempH=math.abs((curbase.x)-tx)+math.abs(curbase.z+1-tz)
  247.                 table.remove(openlist,i)
  248.                 table.insert(openlist,i,{x=curbase.x, z=curbase.z+1, g=tempG, h=tempH, f=tempG+tempH, par=closedk})
  249.                 EastOK=false
  250.                 end
  251.  
  252.             if WestOK and openlist[i].x==curbase.x and openlist[i].z==curbase.z-1 and openlist[i].g>tempG then
  253.                 tempH=math.abs((curbase.x)-tx)+math.abs(curbase.z-1-tz)
  254.                 table.remove(openlist,i)
  255.                 table.insert(openlist,i,{x=curbase.x, z=curbase.z-1, g=tempG, h=tempH, f=tempG+tempH, par=closedk})
  256.                 WestOK=false
  257.                 end
  258.             end
  259.  
  260.         -- Add points to openlist
  261.         -- Add point to the North of current base point
  262.         if NorthOK then
  263.             openk=openk+1
  264.             tempH=math.abs((curbase.x+1)-tx)+math.abs(curbase.z-tz)
  265.             table.insert(openlist,openk,{x=curbase.x+1, z=curbase.z, g=tempG, h=tempH, f=tempG+tempH, par=closedk})
  266.             end
  267.  
  268.         -- Add point to the South of current base point
  269.         if SouthOK then
  270.             openk=openk+1
  271.             tempH=math.abs((curbase.x-1)-tx)+math.abs(curbase.z-tz)
  272.             table.insert(openlist,openk,{x=curbase.x-1, z=curbase.z, g=tempG, h=tempH, f=tempG+tempH, par=closedk})
  273.             end
  274.  
  275.         -- Add point to the East of current base point
  276.         if EastOK then
  277.             openk=openk+1
  278.             tempH=math.abs(curbase.x-tx)+math.abs((curbase.z+1)-tz)
  279.             table.insert(openlist,openk,{x=curbase.x, z=curbase.z+1, g=tempG, h=tempH, f=tempG+tempH, par=closedk})
  280.             end
  281.  
  282.         -- Add point to the West of current base point
  283.         if WestOK then
  284.             openk=openk+1
  285.             tempH=math.abs(curbase.x-tx)+math.abs((curbase.z-1)-tz)
  286.             table.insert(openlist,openk,{x=curbase.x, z=curbase.z-1, g=tempG, h=tempH, f=tempG+tempH, par=closedk})
  287.             end
  288.  
  289.         table.remove(openlist,basis)
  290.         openk=openk-1
  291.        
  292.         if closedlist[closedk].x==tx and closedlist[closedk].z==tz then
  293.             Debug:Debug("\n")
  294.             Debug:Debug("Nav:GetPath() Found the path! Openlist: %s, Closedlist: %s, Steps: ",table.maxn(openlist),table.maxn(closedlist))
  295.             -- Change Closed list into a list of XZ coordinates starting with player
  296.             local path={}
  297.             local pathIndex={}
  298.             local last=table.maxn(closedlist)
  299.             table.insert(pathIndex,1,last)
  300.             local i=1 -- we will include starting position into a table, otherwise 1
  301.             while pathIndex[i]>1 do i=i+1; table.insert(pathIndex,i,closedlist[pathIndex[i-1]].par); end
  302.             Debug:Debug("%s\n", i)
  303.             Debug:Debug("Nav:GetPath() Path: ")
  304.             for i=table.maxn(pathIndex),1,-1 do table.insert(path,{x=closedlist[pathIndex[i]].x, z=closedlist[pathIndex[i]].z}); Debug:Debug("%s(%s,%s)", last, path[table.maxn(pathIndex)-i+1].x, path[table.maxn(pathIndex)-i+1].z) end
  305.             closedlist=nil
  306.            
  307.             -- Change list of XZ coordinates into a list of directions
  308.             Debug:Debug("\n")      
  309.             Debug:Debug("Nav:GetPath() FPath: ")
  310.             local fpath={}
  311.             for i=1,table.maxn(path)-1,1 do
  312.                 if path[i+1].x > path[i].x then fpath[i]=0 end -- North
  313.                 if path[i+1].z > path[i].z then fpath[i]=1 end -- East
  314.                 if path[i+1].x < path[i].x then fpath[i]=2 end -- South
  315.                 if path[i+1].z < path[i].z then fpath[i]=3 end -- West
  316.                 Debug:Debug("%s, ", fpath[i])
  317.                 end
  318.             Debug:Debug("\n")
  319.             return fpath
  320.             end
  321.         end
  322.  
  323.     return nil
  324.     end,
  325.     Go = function(self,tx,tz,tries,style)
  326.         if tx == nil then tx = Nav.X else tx=tonumber(tx) end
  327.         if tz == nil then tz = Nav.Z else tz=tonumber(tz) end
  328.         if ty == nil then ty = Nav.Y else ty=tonumber(ty) end
  329.         Debug:Debug("Nav:GoPath(%s,%s,%s)\n", tx, tz, tries)
  330.         if tries == nil then tries = 32 end
  331.         local j=1
  332.         repeat
  333.             if (Nav.X==tx and Nav.Z==tz) then return true else tries=tries-1 end
  334.             Debug:Debug("Nav.GoPath() @(%s,%s,%s) /%s\n",Nav.X,Nav.Z,Nav.Y,tries)
  335.             local fpath = Nav:GetPath(tx,tz)
  336.             if fpath == nil then
  337.                 Debug:Debug("Nav.GoPath() FPath=nil!")
  338.                 Nav:UpdateMap()
  339.                 Nav:TurnRight()
  340.                 else
  341.                 j=0
  342.                 repeat
  343.                     j=j+1
  344.                     Debug:Debug("Nav:GoPath() @(%s,%s,%s), Moving %s/%s ...\n", Nav.X,Nav.Z,Nav.Y,j,table.maxn(fpath))
  345.                     until not (Nav:Move(fpath[j]) and j<table.maxn(fpath))
  346.                 end
  347.             until tries<0
  348.         Debug:Debug("Nav.GoPath() Out-of-UNTIL! /%s",tries)
  349.         return false
  350.         end,
  351.     Move = function(self,dir) -- dir{0=North|1=East|2=South|3=West|4=up|5=down}, returns true if succeeded
  352.         local success = 0
  353.         Debug:Debug("Nav:Move(%s)\n", dir)
  354.         Supp:Refuel()
  355.         if dir==4 then               -- Up
  356.             success = turtle.up()
  357.             elseif dir==5 then         -- Down
  358.             success = turtle.down()
  359.             elseif dir==Nav.F-1 or dir==Nav.F+3 then -- Left
  360.             Nav:TurnLeft()
  361.             success = turtle.forward()
  362.             elseif dir==Nav.F-2 or dir==Nav.F+2 then -- Right
  363.             Nav:TurnAround()
  364.             success = turtle.forward()
  365.             elseif dir==Nav.F-3 or dir==Nav.F+1 then -- 180
  366.             Nav:TurnRight()
  367.             success = turtle.forward()
  368.             else                       -- Forward
  369.             success = turtle.forward()
  370.             end
  371.         if success then
  372.             Nav:UpdateCoord(dir)
  373.             Nav:UpdateMap(dir)
  374.             --Debug:Debug("Nav:Move() Return true\n")
  375.             return true
  376.             else
  377.             Nav:UpdateMap()
  378.             Debug:Debug("Nav:Move() Return false\n")
  379.             return false
  380.             end
  381.         end,
  382.     TurnRight = function(self)
  383.         turtle.turnRight()
  384.         Debug:Debug("Nav:TurnRight() Nav.F: %s => ",Nav.F)
  385.         Nav.F=(Nav.F+1)%4
  386.         Debug:Debug("%s\n",Nav.F)
  387.         Nav:UpdateMap()
  388.         end,
  389.     TurnLeft = function(self)
  390.         turtle.turnLeft()
  391.         Debug:Debug("Nav:TurnLeft() Nav.F: %s => ",Nav.F)
  392.         Nav.F=(Nav.F+3)%4
  393.         Debug:Debug("%s\n",Nav.F)
  394.         Nav:UpdateMap()    
  395.         end,
  396.     TurnAround = function(self)
  397.         if 1==math.random(0,1) then
  398.             Nav:TurnRight()
  399.             Nav:TurnRight()
  400.             else
  401.             Nav:TurnLeft()
  402.             Nav:TurnLeft()
  403.             end
  404.         end,
  405.     DirToCoord = function(self,dir) -- returns x,z,y
  406.         --Debug:Debug("Nav:DirToCoord(%s)\n", dir)
  407.         if dir==0 then return Nav.X+1, Nav.Z, Nav.Y end
  408.         if dir==1 then return Nav.X, Nav.Z+1, Nav.Y end
  409.         if dir==2 then return Nav.X-1, Nav.Z, Nav.Y end
  410.         if dir==3 then return Nav.X, Nav.Z-1, Nav.Y end
  411.         if dir==4 then return Nav.X, Nav.Z, Nav.Y+1 end
  412.         if dir==5 then return Nav.X, Nav.Z, Nav.Y-1 end
  413.         Debug:Debug("Nav:DirToCoord: ERROR\n")
  414.         end,    
  415.     UpdateCoord = function(self,dir)
  416.             --Debug:Debug("Nav:UpdateCoord(%s)\n",dir)
  417.             Nav.X,Nav.Z,Nav.Y = Nav:DirToCoord(dir)
  418.         end,
  419.     UpdateMap = function(self,location,value) -- location{nil|dir|XZY), value{0=air,1=unknown block,1+=known block}  
  420.         --Debug:Debug("Nav:UpdateMap(%s,%s)\n",location,value)
  421.         local x,z,y
  422.         if type(location)=="nil" then
  423.             x,z,y = Nav:DirToCoord(Nav.F)
  424.             --Debug:Debug("Nav:UpdateMap: (nil) %s, %s, %s\n",x,z,y)
  425.             Nav:ExploreMap(x,z,y)      
  426.             Nav.Map[x][z][y].Updated = os.time()
  427.             if turtle.detect() then Nav.Map[x][z][y].Id = 1 else Nav.Map[x][z][y].Id = 0 end
  428.             elseif type(location)=="number" then
  429.             x,z,y = Nav:DirToCoord(Nav.F)
  430.             --Debug:Debug("Nav:UpdateMap: (number) %s, %s, %s\n",x,z,y)
  431.             Nav:ExploreMap(x,z,y)
  432.             Nav.Map[x][z][y].Updated = os.time()
  433.             if turtle.detect() then Nav.Map[x][z][y].Id = 1 else Nav.Map[x][z][y].Id = 0 end
  434.             if location~=5 then
  435.                 x,z,y = Nav:DirToCoord(4)
  436.                 Nav:ExploreMap(x,z,y)
  437.                 Nav.Map[x][z][y].Updated = os.time()
  438.                 if turtle.detectUp() then Nav.Map[x][z][y].Id = 1 else Nav.Map[x][z][y].Id = 0 end
  439.                 end
  440.             if location~=4 then
  441.                 x,z,y = Nav:DirToCoord(5)
  442.                 Nav:ExploreMap(x,z,y)
  443.                 Nav.Map[x][z][y].Updated = os.time()
  444.                 if turtle.detectDown() then Nav.Map[x][z][y].Id = 1 else Nav.Map[x][z][y].Id = 0 end
  445.                 end      
  446.             elseif type(location)=="table" then
  447.             --Debug:Debug("Nav:UpdateMap: (table) %s, %s, %s\n",x,z,y)
  448.             x,z,y = location[1] or location.x, location[2] or location.z, location[3] or location.y
  449.             if value == nil then value = 1 end
  450.             Nav:ExploreMap(x,z,y)
  451.             Nav.Map[x][z][y].Updated = os.time()
  452.             Nav.Map[x][z][y].Id = value
  453.             end
  454.         end,
  455.     ExploreMap = function(self,x,z,y)
  456.         --Debug:Debug("Nav:ExploreMap(%s,%s,%s)\n", x,z,y)
  457.         if Nav.Map[x] == nil then Nav.Map[x]={} end
  458.         if Nav.Map[x][z] == nil then Nav.Map[x][z]={} end
  459.         if Nav.Map[x][z][y] == nil then Nav.Map[x][z][y]={} end
  460.         end,
  461.     Test = function(self)
  462.         return 1
  463.         end,
  464.     }
  465. Stats = {
  466.     GlobalSteps = 0,
  467.     CountedSteps = 0,
  468.     Step = function(self, x)
  469.         x = x or 1 -- create object if user does not provide one
  470.         Stats.GlobalSteps = Stats.GlobalSteps + 1
  471.         Stats.CountedSteps = Stats.CountedSteps + 1
  472.         end,
  473.     GetGlobalSteps = function(self)
  474.         return Stats.GlobalSteps
  475.         end,
  476.     GetCountedSteps = function(self)
  477.         return Stats.CountedSteps
  478.         end,
  479.     ResetCountedSteps = function(self)
  480.         Stats.CountedSteps = 0
  481.         end
  482.     }
  483.  
  484.  
  485. Debug:Info("Starting the program...\n")
  486.  
  487. Init:Born()
  488.  
  489. while 1 do
  490.     Debug:PrintMap()
  491.     Supp:Refuel()
  492.     Debug:Debug("A* Pathfinding supports only 2D\n")
  493.     Debug:Debug("Enter X coordinate: ")
  494.     local x = io.read() -- = io.stdin:read'*l' ??
  495.     Debug:Debug("Enter Z coordinate: ")
  496.     local z = io.read()
  497.  
  498.     if x=="" or x==nil then x=4 end
  499.     if z=="" or z==nil then z=3 end
  500.    
  501.     if Nav:Go(x,z) then Debug:Info("Succeeded!") else Debug:Error("Exceeded 32 tries, failed!\n") end
  502.     Debug:Info(" Coords: (%s,%s,%s), F:%s\n",Nav.X,Nav.Z,Nav.Y,Nav.F)
  503.     end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement