Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- thisisnotaustin
- -- Turtle AI
- dx=0
- dy=0
- dz=0
- face=0
- ego={
- sloth=10,
- speed=3,
- }
- behavior="wander"
- loc={
- ["origin"]={
- {x=0,y=0,z=0},
- },
- }
- dest=""
- dest_step=0
- -- Brains
- function main()
- local fuel=turtle.getFuelLevel()
- -- Draw terminal
- term.clear() term.setTextColor(colors.black) term.setBackgroundColor(colors.gray)
- writeAt(1,1,"\"My name is "..os.getComputerLabel()..".\"")
- term.setTextColor(colors.green) term.setBackgroundColor(colors.black)
- writeAt(1,2,"Press [ENTER] to end AI program.")
- writeAt(1,4,"fuel:"..tostring(fuel).." ")
- writeAt(1,5,"face:"..tostring(face).." ")
- writeAt(1,6,"dx:"..tostring(dx).." ")
- writeAt(1,7,"dy:"..tostring(dy).." ")
- writeAt(1,8,"dz:"..tostring(dz).." ")
- writeAt(1,9,"behavior:"..behavior)
- -- Move around
- if behavior=="wander" then
- local r=math.random(ego.sloth+5)
- writeAt(1,10,"action:"..tostring(r))
- if r>=math.floor(ego.sloth/20) and r<math.floor(ego.sloth/20)*2 then
- t_l()
- elseif r>=math.floor(ego.sloth/20)*2 and r<math.floor(ego.sloth/20)*3 then
- t_r()
- elseif r==ego.sloth then
- t_l() fwd()
- elseif r==ego.sloth-1 then
- t_r() fwd()
- elseif r==ego.sloth-2 then
- for i=1,math.random(ego.speed) do fwd() end
- else
- os.sleep(0.5)
- end
- -- Walk towards a destination
- elseif behavior=="destination" then
- -- Align with the Z axis
- if dz > loc[dest][dest_step].z then
- if face==1 then t_l()
- elseif face==2 then t_r() t_r()
- elseif face==3 then t_r()
- else
- for i=1,dz-loc[dest][dest_step].z do fwd() end
- end
- elseif dz < loc[dest][dest_step].z then
- if face==0 then t_r() t_r()
- elseif face==1 then t_r()
- elseif face==3 then t_l()
- else
- for i=1,math.abs(dz-loc[dest][dest_step].z) do fwd() end
- end
- -- Align with the X axis
- elseif dx < loc[dest][dest_step].x then
- if face==0 then t_r()
- elseif face==2 then t_l()
- elseif face==3 then t_r() t_r()
- else
- for i=1,math.abs(dx-loc[dest][dest_step].x) do fwd() end
- end
- elseif dx > loc[dest][dest_step].x then
- if face==0 then t_l()
- elseif face==1 then t_r() t_r()
- elseif face==2 then t_r()
- else
- for i=1,dx-loc[dest][dest_step].x do fwd() end
- end
- elseif dx==loc[dest][dest_step].x and dz==loc[dest][dest_step].z then
- dest_step=dest_step-1
- if #table==0 then
- behavior="wait"
- end
- end
- end
- end
- -- Handle events
- function pull_ev()
- local ev, a1 = os.pullEventRaw()
- if ev and ev == "key" then
- -- End the program on [ENTER] key
- if a1 == keys.enter then
- if behavior=="wander" then
- behavior="destination"
- dest="origin"
- dest_step=#loc["origin"]
- elseif behavior=="wait" then
- behavior="wander"
- else
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- print("Goodnight!")
- run=false
- end
- end
- end
- end
- --######################
- --# MOVEMENT FUNCTIONS #
- --######################
- function up()
- local s=turtle.up()
- if s==true then
- dy=dy+1
- end
- return s
- end
- function down()
- local s=turtle.down()
- if s==true then
- dy=dy-1
- end
- return s
- end
- function fwd( test )
- -- Attempt to move forward
- local s=turtle.forward()
- if s==true then
- if face==0 then
- dz=dz-1
- elseif face==1 then
- dx=dx+1
- elseif face==2 then
- dz=dz+1
- elseif face==3 then
- dx=dx-1
- end
- -- Attempt to climb over obstacles
- elseif not test then
- local s=up()
- if s then
- fwd(true)
- end
- end
- -- Fall
- while down() do end
- -- Remember locations
- for k,v in pairs(loc) do
- if dz-v[#v].z > 0 or dz-v[#v.z] < 0 then
- table.insert(loc[k], {x=dx,y=xy,z=v[#v].z})
- end
- if dx-v[#v].x > 0 or dx-v[#v.x] < 0 then
- table.insert(loc[k], {x=v[#v].x,y=xy,z=dz})
- end
- end
- return s
- end
- function t_l()
- -- Turn to the turtle's left
- local s=turtle.turnLeft()
- if s==true then
- face=face-1
- if face<0 then face=3 end
- end return s
- end
- function t_r()
- -- Turn to the turtle's right
- local s=turtle.turnRight()
- if s==true then
- face=face+1
- if face>3 then face=0 end
- end return s
- end
- --####################
- --# VISION FUNCTIONS #
- --####################
- function look()
- end
- --###################
- --# MISC. FUNCTIONS #
- --###################
- function writeAt( _x, _y, _t )
- term.setCursorPos(_x, _y)
- term.write(_t)
- end
- --#############
- --# MAIN LOOP #
- --#############
- run=true
- while run do
- parallel.waitForAll(main, pull_ev)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement