Advertisement
Guest User

tai

a guest
Nov 19th, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.49 KB | None | 0 0
  1. -- thisisnotaustin
  2. -- Turtle AI
  3.  
  4. dx=0
  5. dy=0
  6. dz=0
  7. face=0
  8.  
  9. ego={
  10.     sloth=10,
  11.     speed=3,
  12. }
  13. behavior="wander"
  14.  
  15. loc={
  16.     ["origin"]={
  17.         {x=0,y=0,z=0},
  18.     },
  19. }
  20. dest=""
  21. dest_step=0
  22.  
  23. -- Brains
  24. function main()
  25.     local fuel=turtle.getFuelLevel()
  26.         -- Draw terminal
  27.     term.clear() term.setTextColor(colors.black) term.setBackgroundColor(colors.gray)
  28.     writeAt(1,1,"\"My name is "..os.getComputerLabel()..".\"")
  29.     term.setTextColor(colors.green) term.setBackgroundColor(colors.black)
  30.     writeAt(1,2,"Press [ENTER] to end AI program.")
  31.     writeAt(1,4,"fuel:"..tostring(fuel).."     ")
  32.     writeAt(1,5,"face:"..tostring(face).."     ")
  33.     writeAt(1,6,"dx:"..tostring(dx).."     ")
  34.     writeAt(1,7,"dy:"..tostring(dy).."     ")
  35.     writeAt(1,8,"dz:"..tostring(dz).."     ")
  36.     writeAt(1,9,"behavior:"..behavior)
  37.    
  38.         -- Move around
  39.     if behavior=="wander" then
  40.         local r=math.random(ego.sloth+5)
  41.         writeAt(1,10,"action:"..tostring(r))
  42.         if r>=math.floor(ego.sloth/20) and r<math.floor(ego.sloth/20)*2 then
  43.             t_l()
  44.         elseif r>=math.floor(ego.sloth/20)*2 and r<math.floor(ego.sloth/20)*3 then
  45.             t_r()
  46.         elseif r==ego.sloth then
  47.             t_l() fwd()
  48.         elseif r==ego.sloth-1 then
  49.             t_r() fwd()
  50.         elseif r==ego.sloth-2 then
  51.             for i=1,math.random(ego.speed) do fwd() end
  52.         else
  53.             os.sleep(0.5)
  54.         end
  55.    
  56.         -- Walk towards a destination
  57.     elseif behavior=="destination" then
  58.             -- Align with the Z axis
  59.         if dz > loc[dest][dest_step].z then
  60.             if face==1 then t_l()
  61.             elseif face==2 then t_r() t_r()
  62.             elseif face==3 then t_r()
  63.             else
  64.                 for i=1,dz-loc[dest][dest_step].z do fwd() end
  65.             end
  66.         elseif dz < loc[dest][dest_step].z then
  67.             if face==0 then t_r() t_r()
  68.             elseif face==1 then t_r()
  69.             elseif face==3 then t_l()
  70.             else
  71.                 for i=1,math.abs(dz-loc[dest][dest_step].z) do fwd() end
  72.             end
  73.             -- Align with the X axis
  74.         elseif dx < loc[dest][dest_step].x then
  75.             if face==0 then t_r()
  76.             elseif face==2 then t_l()
  77.             elseif face==3 then t_r() t_r()
  78.             else
  79.                 for i=1,math.abs(dx-loc[dest][dest_step].x) do fwd() end
  80.             end
  81.         elseif dx > loc[dest][dest_step].x then
  82.             if face==0 then t_l()
  83.             elseif face==1 then t_r() t_r()
  84.             elseif face==2 then t_r()
  85.             else
  86.                 for i=1,dx-loc[dest][dest_step].x do fwd() end
  87.             end
  88.         elseif dx==loc[dest][dest_step].x and dz==loc[dest][dest_step].z then
  89.             dest_step=dest_step-1
  90.             if #table==0 then
  91.                 behavior="wait"
  92.             end
  93.         end
  94.     end
  95. end
  96. -- Handle events
  97. function pull_ev()
  98.     local ev, a1 = os.pullEventRaw()
  99.    
  100.     if ev and ev == "key" then
  101.             -- End the program on [ENTER] key
  102.         if a1 == keys.enter then
  103.             if behavior=="wander" then
  104.                 behavior="destination"
  105.                 dest="origin"
  106.                 dest_step=#loc["origin"]
  107.             elseif behavior=="wait" then
  108.                 behavior="wander"
  109.             else
  110.                 term.clear()
  111.                 term.setCursorPos(1,1)
  112.                 term.setTextColor(colors.white)
  113.                 term.setBackgroundColor(colors.black)
  114.                 print("Goodnight!")
  115.                 run=false
  116.             end
  117.         end
  118.     end
  119. end
  120.  
  121. --######################
  122. --# MOVEMENT FUNCTIONS #
  123. --######################
  124.  
  125. function up()
  126.     local s=turtle.up()
  127.     if s==true then
  128.         dy=dy+1
  129.     end
  130.     return s
  131. end
  132. function down()
  133.     local s=turtle.down()
  134.     if s==true then
  135.         dy=dy-1
  136.     end
  137.     return s
  138. end
  139. function fwd( test )
  140.         -- Attempt to move forward
  141.     local s=turtle.forward()
  142.     if s==true then
  143.         if face==0 then
  144.             dz=dz-1
  145.         elseif face==1 then
  146.             dx=dx+1
  147.         elseif face==2 then
  148.             dz=dz+1
  149.         elseif face==3 then
  150.             dx=dx-1
  151.         end
  152.        
  153.         -- Attempt to climb over obstacles
  154.     elseif not test then
  155.         local s=up()
  156.         if s then
  157.             fwd(true)
  158.         end
  159.     end
  160.         -- Fall
  161.     while down() do end
  162.         -- Remember locations
  163.     for k,v in pairs(loc) do
  164.         if dz-v[#v].z > 0 or dz-v[#v.z] < 0 then
  165.             table.insert(loc[k], {x=dx,y=xy,z=v[#v].z})
  166.         end
  167.         if dx-v[#v].x > 0 or dx-v[#v.x] < 0 then
  168.             table.insert(loc[k], {x=v[#v].x,y=xy,z=dz})
  169.         end
  170.     end
  171.    
  172.     return s
  173. end
  174. function t_l()
  175.         -- Turn to the turtle's left
  176.     local s=turtle.turnLeft()
  177.     if s==true then
  178.         face=face-1
  179.         if face<0 then face=3 end
  180.     end return s
  181. end
  182. function t_r()
  183.         -- Turn to the turtle's right
  184.     local s=turtle.turnRight()
  185.     if s==true then
  186.         face=face+1
  187.         if face>3 then face=0 end
  188.     end return s
  189. end
  190.  
  191. --####################
  192. --# VISION FUNCTIONS #
  193. --####################
  194.  
  195. function look()
  196.    
  197. end
  198.  
  199. --###################
  200. --# MISC. FUNCTIONS #
  201. --###################
  202.  
  203. function writeAt( _x, _y, _t )
  204.     term.setCursorPos(_x, _y)
  205.     term.write(_t)
  206. end
  207.  
  208. --#############
  209. --# MAIN LOOP #
  210. --#############
  211.  
  212. run=true
  213. while run do
  214.     parallel.waitForAll(main, pull_ev)
  215. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement