Advertisement
denvys5

denesik save state API

Aug 20th, 2014
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.88 KB | None | 0 0
  1. -------------------Константы----------------------
  2.  
  3. direction = {
  4.         ["forw"]  = 1,
  5.         ["right"] = 2,
  6.         ["back"]  = 3,
  7.         ["left"]  = 4,
  8. }
  9. dir = direction.forw
  10. pos = {
  11.         ["x"]   = 0,
  12.         ["y"]   = 0,
  13.         ["z"]   = 0,
  14. }
  15.  
  16. lenght,width,height = 0,0,0
  17. xt,yt,zt,dt = 0,0,0,direction.forw
  18.  
  19. local name = "quarry"
  20. local done = false
  21. globalState = 0
  22. aState = 0 --состояние автомата
  23. tmpState = 0
  24. local lenght,width,height = 0,0,0
  25. local xt,yt,zt,dt = 0,0,0,direction.forw
  26. mode = 1
  27.  
  28. ------------------ Сохранение позиции ----------------------------
  29.  
  30. function saveState()
  31.         name = shell.getRunningProgram()
  32.     local file = fs.open(name..".save","w")
  33.         file.writeLine(mode)
  34.         file.writeLine(globalState)
  35.     file.writeLine(pos.x)
  36.     file.writeLine(pos.y)
  37.     file.writeLine(pos.z)
  38.     file.writeLine(dir)
  39.     file.writeLine(tmpState)
  40.     file.writeLine(aState)
  41.     file.writeLine(xt)
  42.     file.writeLine(yt)
  43.     file.writeLine(zt)
  44.     file.writeLine(dt)
  45.         file.writeLine(lenght)
  46.     file.writeLine(width)
  47.     file.writeLine(height)
  48.         file.writeLine(done)
  49.     file.close()
  50. end
  51.  
  52. function readState()
  53.         name = shell.getRunningProgram()
  54.         if not fs.exists(name..".save") then
  55.                 return false
  56.         end
  57.         local file = fs.open(name..".save","r")
  58.         mode =          tonumber(file.readLine())
  59.     globalState = tonumber(file.readLine())
  60.     pos.x =     tonumber(file.readLine())
  61.         pos.y =         tonumber(file.readLine())
  62.         pos.z =         tonumber(file.readLine())
  63.         dir =           tonumber(file.readLine())
  64.         tmpState =      tonumber(file.readLine())
  65.         aState =        tonumber(file.readLine())
  66.         xt =            tonumber(file.readLine())
  67.         yt =            tonumber(file.readLine())
  68.         zt =            tonumber(file.readLine())
  69.         dt =            tonumber(file.readLine())      
  70.         lenght =        tonumber(file.readLine())
  71.     width =     tonumber(file.readLine())
  72.     height =    tonumber(file.readLine())
  73.         done =          file.readLine()
  74.         if done == "true" then
  75.                 done = true
  76.         else
  77.                 done = false
  78.         end
  79.     file.close()
  80.         return true
  81. end
  82.  
  83. function setStartup()
  84.         if fs.exists("startup") then
  85.                         fs.delete("startup")
  86.         end
  87.         name = shell.getRunningProgram()
  88.         local file = fs.open("startup","w")
  89.         file.writeLine("shell.run(\""..name.."\")")
  90.         file.close()
  91. end
  92.  
  93. function delStartup()
  94.         if fs.exists("startup") then
  95.                 fs.delete("startup")
  96.         end
  97.         name = shell.getRunningProgram()
  98.         if fs.exists(name..".save") then
  99.                 fs.delete(name..".save")
  100.         end
  101. end
  102.  
  103.  
  104. ------------------ Движение ----------------------------
  105.  
  106. function turnRight()
  107.         dir=dir+1      
  108.         if dir==5 then dir = direction.forw end
  109.                 saveState()
  110.         turtle.turnRight()
  111. end
  112.  
  113. function turnLeft()
  114.         dir=dir-1      
  115.         if dir==0 then dir = direction.left end
  116.                 saveState()
  117.         turtle.turnLeft()
  118. end
  119.  
  120. --local function checkBorder()
  121. --        if (math.abs(pos.x) >=  width) or (math.abs(pos.z) >= lenght) then
  122. --                print("going beyond a border")
  123. --                delStartup()
  124. --                error()
  125. --        end
  126. --end
  127.  
  128. function down()
  129.         while true do
  130.                 if turtle.detectDown() then    
  131.                         if turtle.digDown() then
  132.                         else
  133.                                 print("detect bedrock or unbreaking block")
  134.                                 return false
  135.                         end
  136.                 elseif turtle.attackDown() then
  137.                         print("detect monster or player")
  138.                 else  
  139.                         local ty = pos.y
  140.                         pos.y = pos.y - 1
  141.                         saveState()
  142.                         if turtle.down() then
  143.                                 checkBorder()
  144.                                 return true
  145.                         else
  146.                                 print("detect unknowing object")
  147.                                 pos.y = ty
  148.                                 saveState()
  149.                                 os.sleep(0.5)
  150.                         end
  151.                 end
  152.         end
  153. end
  154.  
  155. function up()
  156.         while true do
  157.                 if turtle.detectUp() then       --обнаружен блок, сломать
  158.                         if turtle.digUp() then
  159.                         else
  160.                                 print("detect bedrock or unbreaking block")
  161.                                 return false
  162.                         end
  163.                 elseif turtle.attackUp() then -- Сверху нет блока, проверяем есть ли там существо
  164.                         print("detect monster or player")
  165.                 else    -- Передвигаемся вверх
  166.                         local ty = pos.y
  167.                         pos.y = pos.y + 1
  168.                         saveState()
  169.                         if turtle.up() then
  170.                                 checkBorder()
  171.                                 return true
  172.                         else
  173.                                 print("detect unknowing object")
  174.                                 pos.y = ty
  175.                                 saveState()
  176.                                 os.sleep(0.5)
  177.                         end
  178.                 end
  179.         end
  180. end
  181.  
  182. function forward(mined)
  183.         if mined then
  184.                 if turtle.detectUp() then
  185.                         turtle.digUp()
  186.                         if inventoryFull() then
  187.                                 returnSupplies()
  188.                         end
  189.                 end
  190.                 if turtle.detectDown() then
  191.                         turtle.digDown()
  192.                         if inventoryFull() then
  193.                                 returnSupplies()
  194.                         end
  195.                 end
  196.         end
  197.        
  198.         while true do
  199.                 if turtle.detect() then --обнаружен блок, сломать
  200.                         if turtle.dig() then
  201.                                  if mined and inventoryFull() then
  202.                                          returnSupplies()
  203.                                  end
  204.                         else
  205.                                 print("detect bedrock or unbreaking block")
  206.                                 return false
  207.                         end
  208.                 elseif turtle.attack() then -- Впереди нет блока, проверяем есть ли там существо
  209.                         print("detect monster or player")
  210.                          if mined and inventoryFull() then
  211.                                  returnSupplies()
  212.                          end
  213.                 else    -- Передвигаемся вперед
  214.                         if mined then -- Уберем гравий
  215.                                 while turtle.detectUp() do
  216.                                         turtle.digUp()
  217.                                         os.sleep(0.5)
  218.                                 end
  219.                         end
  220.                         local tz,tx = pos.z,pos.x
  221.                         if dir == direction.forw then
  222.                                 pos.z=pos.z+1
  223.                         elseif dir == direction.right then
  224.                                 pos.x=pos.x+1
  225.                         elseif dir == direction.back then
  226.                                 pos.z=pos.z-1
  227.                         elseif dir == direction.left then
  228.                                 pos.x=pos.x-1
  229.                         end
  230.                         saveState()
  231.                         if turtle.forward() then
  232.                                 checkBorder()
  233.                                 return true
  234.                         else
  235.                                 if not turtle.detect() then    
  236.                                         print("detect unknowing object")
  237.                                 end
  238.                                 pos.z = tz
  239.                                 pos.x = tx
  240.                                 saveState()
  241.                                 os.sleep(0.5)
  242.                         end
  243.                 end
  244.         end
  245. end
  246.  
  247. function detectBedrock() --объехать бедрок
  248.         turnLeft()
  249.         turnLeft()
  250.         while not up() do
  251.                 if not forward() then
  252.                         print("don't going to bedrock!")
  253.                         delStartup()
  254.                         error()
  255.                         return false
  256.                 end
  257.         end
  258.         return true
  259. end
  260.  
  261. local goTo
  262.  
  263. function goTo( x, y, z, d)
  264.         while pos.y < y do      -- идем вверх
  265.                 if not up() then
  266.                         detectBedrock()
  267.                 end
  268.         end
  269.         if pos.x > x then      
  270.                 while dir ~= direction.left do
  271.                         turnLeft()
  272.                 end
  273.                 while pos.x > x do
  274.                         forward(false)
  275.                 end
  276.         end
  277.         if pos.x < x then
  278.                 while dir ~= direction.right do
  279.                         turnRight()
  280.                 end
  281.                 while pos.x < x do
  282.                         forward(false)
  283.                 end
  284.         end
  285.         if pos.z > z then
  286.                 while dir ~= direction.back do
  287.                         turnRight()
  288.                 end
  289.                 while pos.z > z do
  290.                         forward(false)
  291.                 end
  292.         end
  293.         if pos.z < z then
  294.                 while dir ~= direction.forw do
  295.                         turnLeft()
  296.                 end
  297.                 while pos.z < z do
  298.                         forward(false)
  299.                 end    
  300.         end
  301.         while pos.y > y do      -- идем вниз
  302.                 down()
  303.         end
  304.         while d ~= dir do
  305.                 turnLeft()
  306.         end
  307. end  
  308.  
  309. local function nextState()
  310.         if aState == 0 then
  311.                 aState = 1
  312.         elseif aState == 1 then
  313.                 if pos.z == lenght-1 and pos.x ~= width-1 then
  314.                         aState = 2
  315.                 elseif pos.z == lenght-1 and pos.x == width-1 then
  316.                         aState = 9
  317.                 end
  318.         elseif aState == 2 then
  319.                 aState = 3
  320.         elseif aState == 3 then
  321.                 if pos.z == 0 and pos.x ~= width-1 then
  322.                         aState = 4
  323.                 elseif pos.z == 0 and pos.x == width-1 then
  324.                         aState = 10
  325.                 end
  326.         elseif aState == 4 then
  327.                 aState = 1
  328.         elseif aState == 5 then
  329.                 if pos.z == lenght-1 and pos.x ~= 0 then
  330.                         aState = 6
  331.                 elseif pos.z == lenght-1 and pos.x == 0 then
  332.                         aState = 11
  333.                 end
  334.         elseif aState == 6 then
  335.                 aState = 7
  336.         elseif aState == 7 then
  337.                 if pos.z == 0 and pos.x ~= 0 then
  338.                         aState = 8
  339.                 elseif pos.z == 0 and pos.x == 0 then
  340.                         aState = 12
  341.                 end
  342.         elseif aState == 8 then
  343.                 aState = 5
  344.         elseif aState == 9 then
  345.                 aState = 7
  346.         elseif aState == 10 then
  347.                 aState = 5
  348.         elseif aState == 11 then
  349.                 aState = 3
  350.         elseif aState == 12 then
  351.                 aState = 1
  352.         end
  353.         tmpState = 0
  354.         saveState()
  355.         return
  356. end
  357.  
  358. function lower()
  359.         if turtle.detectUp() then
  360.                 turtle.digUp()
  361.         end
  362.         while tmpState ~= 3 do
  363.                 ref()
  364.                 tmpState = tmpState + 1
  365.                 if not down() then
  366.                         done = true
  367.                         break
  368.                 end
  369.         end
  370. end
  371.  
  372. function recover()
  373.         if not readState() or globalState == 0 then
  374.                 print("Programm is not running")
  375.                 print( "Usage: quarry <lenght> <width> [<height>]" )
  376.                 return
  377.         end
  378.         if globalState == 1 then
  379.                 local done = false
  380.                 while tmpState ~= 1 do
  381.                         ref()
  382.                         if not down() then
  383.                                 done = true
  384.                                 break
  385.                         end
  386.                         tmpState=tmpState+1
  387.                         saveState()
  388.                 end
  389.                 globalState = 2
  390.                 saveState()
  391.         end
  392.         if globalState >= 2 then
  393.                 main() -- Название основной функции. Заменить main() на свой вариант
  394.         end
  395. end
  396.  
  397. function start()
  398.         setStartup()
  399.         globalState = 1 -- Поехали копать
  400.         done = false
  401.  
  402.         while tmpState < 1 do
  403.                 ref()
  404.                 if not down() then
  405.                         done = true
  406.                         break
  407.                 end
  408.                 tmpState=tmpState+1
  409.                 saveState()
  410.         end
  411.  
  412.         globalState = 2
  413.         saveState()
  414.         nextState()
  415.         main()  -- Название основной функции. Заменить main() на свой вариант
  416. end
  417.  
  418. ---------------------------Что идёт в саму программу-----------------------------------
  419. local tArgs = { ... } -- вставить в основную прогу
  420. local if #tArgs == 0 then
  421.         print( "Recovering...")
  422.         recover()
  423.         return
  424. elseif #tArgs == 2 or #tArgs == 3 or #tArgs == 4 then
  425.         lenght = tonumber( tArgs[1] )
  426.         width  = tonumber( tArgs[2] )
  427.         height = 256
  428.         local arg = ""
  429.         if lenght < 1 or width < 1 or lenght > 255 or width > 255 then
  430.                 print( "Quarry size must be positive" )
  431.                 return
  432.         end
  433.         if #tArgs == 3 then
  434.                 arg = tonumber( tArgs[3] )
  435.                 if tArgs[3] == "-e" then
  436.                         mode = 2
  437.                 elseif tArgs[3] == "-f" then
  438.                         FuelInfo()
  439.                         return
  440.                 elseif arg ~= nill then
  441.                         height = arg
  442.                         if height < 1 or height > 255 then
  443.                                 print( "Quarry size must be positive" )
  444.                                 return
  445.                         end
  446.                 else
  447.                         print( "Usage: quarry <lenght> <width> [<height>] [<key>]; key: \"-e\", \"-f\"" )
  448.                         return
  449.                 end
  450.         elseif #tArgs == 4 then
  451.                 arg = tonumber( tArgs[3] )
  452.                 if arg ~= nill then
  453.                         height = arg
  454.                         if height < 1 or height > 255 then
  455.                                 print( "Quarry size must be positive" )
  456.                                 return
  457.                         end
  458.                 else
  459.                         print( "Usage: quarry <lenght> <width> [<height>] [<key>]; key: \"-e\", \"-f\"" )
  460.                         return
  461.                 end
  462.                 if tArgs[4] == "-e" then
  463.                         mode = 2
  464.                 elseif tArgs[4] == "-f" then
  465.                         FuelInfo()
  466.                         return
  467.                 else
  468.                         print( "Usage: quarry <lenght> <width> [<height>] [<key>]; key: \"-e\", \"-f\"" )
  469.                         return
  470.                 end
  471.         end
  472.         GetFuel()
  473.         return
  474. else
  475.         print( "Usage: quarry <lenght> <width> [<height>] [<key>]; key: \"-e\", \"-f\"" )
  476.         return
  477. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement