Advertisement
NanoBob

NanoTurtle

Mar 20th, 2015
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.95 KB | None | 0 0
  1. local speaker=peripheral.wrap("right")
  2. local chatBox=peripheral.wrap("left")
  3. local mood=50
  4. local boredom=1
  5. local boreds={"Just fine","Good","Kinda bored","Bored","Rather bored"}
  6. local turtleName="nanoturtle"
  7. local lastPlayer=nil
  8. local pos={["x"]=0,["y"]=0,["z"]=0}
  9. local inside=true
  10. local teleporters={}
  11. local talking=true
  12. local owners={["NanoBob_"]=true,["Yaplax"]=false,}
  13.  
  14. if fs.exists("database")==false then
  15.     shell.run("pastebin get PdhBaBdZ database")
  16. end
  17. local success=os.loadAPI("database")
  18.  
  19. if success==false or database==nil then
  20.     error "The database system failed to load"
  21. end
  22.  
  23. if fs.exists("databases/data")==false then
  24.     database.createDataFile("data")
  25. end
  26.  
  27. if fs.exists("databases/jokes")==false then
  28.     database.createDataFile("jokes")
  29. end
  30.  
  31. if fs.exists("databases/position")==false then
  32.     database.createDataFile("position")
  33. end
  34.  
  35. function changePos(key,change)
  36.     if pos[key]==nil then
  37.         pos[key]=0
  38.     end
  39.     pos[key]=pos[key]+change
  40.     database.setData("position",key,pos[key])
  41. end
  42.  
  43. function updatePos(key)
  44.     database.setData("position",key,pos[key])
  45. end
  46.  
  47. function getFilePos()
  48.     if database.getData("position","x")==false then
  49.         pos.x=0
  50.         pos.y=0
  51.         pos.z=0
  52.         database.setData("position","x",0)
  53.         database.setData("position","y",0)
  54.         database.setData("position","z",0)
  55.     else
  56.         pos.x=database.getData("position","x")
  57.         pos.y=database.getData("position","y")
  58.         pos.z=database.getData("position","z")
  59.     end
  60. end
  61.  
  62. getFilePos()
  63.  
  64. function move(direction,blocks)
  65.     if direction=="up" then
  66.         for i=1,blocks do
  67.             moved=turtle.up()
  68.             if moved==true then
  69.                 changePos("y",1)
  70.             else
  71.                 return false
  72.             end
  73.         end
  74.     elseif direction=="down" then
  75.         for i=1,blocks do
  76.             moved=turtle.down()
  77.             if moved==true then
  78.                 changePos("y",-1)
  79.             else
  80.                 return false
  81.             end
  82.         end
  83.     elseif direction=="back" then
  84.         local facing=string.lower(getFacing())
  85.         for i=1,blocks do  
  86.             local moved=turtle.back()
  87.             if moved==false then return false end  
  88.             if facing=="north" then
  89.                 changePos("z",1)
  90.             elseif facing=="east" then
  91.                 changePos("x",-1)
  92.             elseif facing=="south" then
  93.                 changePos("z",-1)
  94.             elseif facing=="west" then
  95.                 changePos("x",1)       
  96.             end
  97.         end
  98.     else
  99.         local facing=string.lower(getFacing())
  100.         for i=1,blocks do
  101.             local moved=turtle.forward()
  102.             if moved==false then return false end
  103.             if facing=="north" then
  104.                 changePos("z",-1)
  105.             elseif facing=="east" then
  106.                 changePos("x",1)   
  107.             elseif facing=="south" then
  108.                 changePos("z",1)
  109.             elseif facing=="west" then
  110.                 changePos("x",-1)      
  111.             end
  112.         end
  113.     end
  114.     return true
  115. end
  116.  
  117. function outputMessage(message,range,language)
  118.     if talking==true and type(message)=="string" then
  119.         if range==nil then
  120.             speaker.speak(message)
  121.         elseif lanugage==nil then
  122.             speaker.speak(message,range)
  123.         else
  124.             speaker.speak(message,range,language)
  125.         end
  126.     end
  127. end
  128.  
  129. function stateMood()
  130.     local boredomIndex=math.ceil(boredom/(100/#boreds)-0.5)+1
  131.     if boredomIndex==nil then
  132.         boredomIndex=1
  133.     end
  134.     local currentState=boreds[boredomIndex]
  135.     if currentState==nil then
  136.         currentState="Unaware of my current state"
  137.     end
  138.     outputMessage("I am "..currentState)
  139. end
  140.  
  141. function getFacing()
  142.     turtle.select(2)
  143.     turtle.equipRight()
  144.     local compass=peripheral.wrap("right")
  145.     local heading=compass.getFacing()
  146.     turtle.select(2)
  147.     turtle.equipRight()
  148.     return heading
  149. end
  150.  
  151. local facingTable={
  152.     ["north"]=1,
  153.     ["east"]=2,
  154.     ["south"]=3,
  155.     ["west"]=4,
  156. }
  157.  
  158. function setFacing(facing)
  159.     local facing=string.lower(facing)
  160.     local currentFacing=string.lower(getFacing())
  161.     if facing==currentFacing then
  162.         return
  163.     else
  164.         local currentFacingIndex=facingTable[currentFacing]
  165.         local facingIndex=facingTable[facing]
  166.         local turns=facingIndex-currentFacingIndex
  167.         if turns<0 then
  168.             turns=turns+4
  169.         end
  170.         if turns==3 then
  171.             turtle.turnLeft()
  172.         else
  173.             for i=1,turns do
  174.                 turtle.turnRight()
  175.             end
  176.         end
  177.     end
  178.    
  179. end
  180.  
  181. function updateMood()
  182.     if boredom<=50 then
  183.         mood=mood+2
  184.     else
  185.         mood=mood-2
  186.     end
  187.     if mood>100 then
  188.         mood=100
  189.     elseif mood<1 then
  190.         mood=1
  191.     end
  192. end
  193.  
  194. function updateBoredom(number)
  195.     boredom=boredom+number
  196.     if boredom>100 then
  197.         boredom=100
  198.     elseif boredom<1 then
  199.         boredom=1
  200.     end
  201.     updateMood()
  202. end
  203.  
  204. function askMood(player)
  205.     outputMessage("How are you doing "..player.."?")
  206. end
  207.  
  208. function tellJoke()
  209.     local jokeCount=database.getData("jokes","count")
  210.     local currentJoke=math.random(jokeCount)
  211.     local joke=database.getData("jokes",currentJoke)
  212.     if joke==nil then
  213.         return
  214.     end
  215.     outputMessage(joke)
  216. end
  217.  
  218. function getAvailableTeleports()
  219.     local teleporter=peripheral.wrap("bottom")
  220.     local teleports={}
  221.     if teleporter==nil then
  222.         return false
  223.     else
  224.         teleports=teleporter.getLinks()
  225.     end
  226.     return teleports
  227. end
  228.  
  229. function teleportTo(name)
  230.     local name=string.gsub(name," ","")
  231.     local number=tonumber(name)
  232.     local teleporter=peripheral.wrap("bottom")
  233.     local teleports=getAvailableTeleports()
  234.     local matchinTeleport=nil
  235.     if number==nil then
  236.         for id,teleportData in pairs(teleports) do
  237.             if teleportData.name==name then
  238.                 matchinTeleport=id+1
  239.                 break
  240.             end
  241.         end
  242.         if matchinTeleport==nil then
  243.             outputMessage("no teleport found under the name, " .. name)
  244.             return false,"no teleport found under the name, " .. name
  245.         end
  246.     else
  247.         matchinTeleport=number
  248.     end
  249.     outputMessage("Teleport ID : "..matchinTeleport)
  250.     sleep(1.5)
  251.     local teleporter=peripheral.wrap("bottom")
  252.     local teleported=teleporter.teleport(matchinTeleport)
  253.     pos.x=teleports[matchinTeleport-1].x
  254.     pos.y=teleports[matchinTeleport-1].y
  255.     pos.z=teleports[matchinTeleport-1].z
  256.     updatePos("x")
  257.     updatePos("y")
  258.     updatePos("z")
  259.     return teleported
  260. end
  261.  
  262. function pathFind(x,y,z)
  263.     local yDistance=(pos.y-y)*-1
  264.     print("Y axis travel distance : "..yDistance)
  265.     local yTravel=true
  266.     if yDistance>0 then
  267.         local yTravel=move("up",yDistance)
  268.     elseif yDistance<0 then
  269.         local yTravel=move("down",yDistance*-1)
  270.     end
  271.     local xDistance=(pos.x-x)*-1
  272.     print("X axis travel distance : "..xDistance)
  273.     local xTravel=true
  274.     if xDistance>0 then
  275.         setFacing("east")
  276.     elseif xDistance<0 then
  277.         setFacing("west")
  278.         xDistance=xDistance*-1
  279.     end
  280.     xTravel=move("forward",xDistance)
  281.     local zDistance=(pos.z-z)*-1
  282.     print("Z axis travel distance : "..zDistance)
  283.     local zTravel=true
  284.     if zDistance>0 then
  285.         setFacing("south")
  286.     elseif zDistance<0 then
  287.         setFacing("north")
  288.         zDistance=zDistance*-1
  289.     end
  290.     zTravel=move("forward",zDistance)
  291.     if xTravel==false or zTravel==false or yTravel==false then
  292.         pathFind(x,y,z)
  293.     end
  294. end
  295.  
  296. local commands={
  297.   ["Hey"]=
  298.     function(player,message)
  299.         outputMessage("Hey "..string.gsub(player,"_",""))
  300.      end,
  301.   ["Whatsup"]=stateMood,
  302.   ["how are you"]=stateMood,
  303.   ["What is up"]=stateMood,
  304.   ["go"]=
  305.     function(player,message)
  306.         local message=string.lower(message)
  307.         local direction=splitString(message," ",2)
  308.         local blocks=tonumber(splitString(message," ",3))
  309.         if blocks==nil then
  310.             blocks=1
  311.         end
  312.         outputMessage("Going "..direction.." for "..blocks.." meters")
  313.         if direction=="left" then
  314.             turtle.turnLeft()
  315.             move("forward",blocks)
  316.         elseif direction=="right" then
  317.             turtle.turnRight()
  318.             move("forward",blocks)
  319.         elseif direction=="forwards" then
  320.             move("forward",blocks)
  321.         elseif direction=="back" then
  322.             move("back",blocks)
  323.         elseif direction=="up" then
  324.             move("up",blocks)
  325.         elseif direction=="down" then
  326.             move("down",blocks)
  327.         end
  328.         if blocks>10 then
  329.             updateBoredom((blocks-10)/10)
  330.         end
  331.      end,
  332.     ["fuel level"]=
  333.     function(player,message)
  334.         outputMessage("My current Fuel level is : "..turtle.getFuelLevel())
  335.     end,
  336.     ["repeat"]=
  337.     function(player,message)
  338.         local message=string.lower(message)
  339.         local message=string.gsub(message,"repeat","")
  340.         outputMessage(message)
  341.     end,
  342.     ["dig"]=
  343.     function(player,message)
  344.         local message=string.lower(message)
  345.         local direction=splitString(message," ",2)
  346.         local blocks=tonumber(splitString(message," ",3))
  347.         if blocks==nil then
  348.             blocks=1
  349.         end
  350.         outputMessage("Digging "..direction.." for "..blocks.." blocks")
  351.         turtle.select(1)
  352.         turtle.equipRight()
  353.         if direction=="left" then
  354.             turtle.turnLeft()
  355.             for i=1,blocks do
  356.                 turtle.dig()
  357.                 move("forward")
  358.                 turtle.dig()
  359.             end
  360.         elseif direction=="right" then
  361.             turtle.turnRight()
  362.             for i=1,blocks do
  363.                 turtle.dig()
  364.                 move("forward")
  365.                 turtle.dig()
  366.             end        
  367.         elseif direction=="forwards" then
  368.             for i=1,blocks do
  369.                 turtle.dig()
  370.                 move("forward")
  371.                 turtle.dig()
  372.             end        
  373.         elseif direction=="back" then
  374.             for i=1,blocks do
  375.                 turtle.dig()
  376.                 turtle.back()
  377.                 turtle.dig()
  378.             end        
  379.         elseif direction=="up" then
  380.             for i=1,blocks do
  381.                 turtle.digUp()
  382.                 move("up")
  383.                 turtle.digUp()
  384.             end        
  385.         elseif direction=="down" then
  386.             for i=1,blocks do
  387.                 turtle.digDown()
  388.                 move("down")
  389.                 turtle.digDown()
  390.             end        
  391.         end
  392.         turtle.select(1)
  393.         turtle.equipRight()
  394.     end,
  395.     ["attack"]=
  396.     function()
  397.         local message=string.lower(message)
  398.         local direction=splitString(message," ",3)
  399.         local percentage=math.random(100)
  400.         if percentage<20 then
  401.             outputMessage("To battle!")
  402.         end
  403.         turtle.select(1)
  404.         turtle.equipRight()
  405.         if direction==nil then
  406.             turtle.attack()
  407.         elseif direction=="down" then
  408.             turtle.attackDown()
  409.         elseif direction=="up" then
  410.             turtle.attackUp()
  411.         end
  412.         turtle.select(1)
  413.         turtle.equipRight()
  414.     end,
  415.     ["write"]=
  416.     function(player,message)
  417.         local message=string.lower(message)
  418.         local message=string.gsub(message,"write","")
  419.         local count=database.getData("data","count")
  420.         if count==false then
  421.             database.setData("data","count",1)
  422.             count=0
  423.         end
  424.         database.setData("data","count",count+1)
  425.         database.setData("data",count+1,message)
  426.         outputMessage("Set data key "..count+1 .." to "..message)
  427.     end,
  428.     ["setpos"]=
  429.     function(player,message)
  430.         local message=string.lower(message)
  431.         local message=string.gsub(message,"setpos ","")
  432.         local x=splitString(message,",",1)
  433.         local y=splitString(message,",",2)
  434.         local z=splitString(message,",",3)
  435.         if y==nil then
  436.             outputMessage("Please seperate coordinates with commas.")
  437.             return
  438.         end
  439.         pos.x=x
  440.         pos.y=y
  441.         pos.z=z
  442.         updatePos("x")
  443.         updatePos("y")
  444.         updatePos("z")
  445.     end,
  446.     ["go to"]=
  447.     function(player,message)
  448.         local message=string.lower(message)
  449.         local message=string.gsub(message,"go to ","")
  450.         local x=splitString(message,",",1)
  451.         local y=splitString(message,",",2)
  452.         local z=splitString(message,",",3)
  453.         if y==nil then
  454.             outputMessage("Please seperate coordinates with commas.")
  455.             return
  456.         end
  457.         pathFind(x,y,z)
  458.     end,   
  459.     ["getpos"]=
  460.     function(player,message)
  461.         outputMessage("My position is : "..pos.x..","..pos.y..","..pos.z)
  462.     end,
  463.     ["teleports"]=
  464.     function(player,message)
  465.         local message=string.lower(message)
  466.         if getAvailableTeleports()==false then
  467.             outputMessage("I am not currently on a teleporter!")
  468.             return
  469.         end
  470.         local teleports=getAvailableTeleports()
  471.         for id,data in pairs(teleports) do
  472.             outputMessage("Teleport ".. id+1 ..", "..data.name)
  473.             print("Teleport ".. id+1 ..", "..data.name)
  474.             sleep(5)
  475.         end
  476.     end,
  477.     ["teleport to"]=
  478.     function(player,message)
  479.         name=string.gsub(message,"teleport to","")
  480.         if getAvailableTeleports()==false then
  481.             outputMessage("I am not currently on a teleporter!")
  482.             return
  483.         end
  484.         local teleported,reason=teleportTo(name)
  485.         if teleported==false then
  486.             outputMessage("Teleport failed, reason : "..tostring(reason))
  487.             return
  488.         else   
  489.             outputMessage("Sucessful teleport!")           
  490.         end
  491.     end,
  492.     ["setData"]=
  493.     function(player,message)
  494.         local message=string.lower(message)
  495.         local message=string.gsub(message,"setData ","")
  496.         local key=splitString(message," ",1)
  497.         local message=string.gsub(message,key,"")
  498.         database.setData("data",key,message)
  499.         outputMessage("Set data key "..key.." to "..message)
  500.     end,
  501.     ["read"]=
  502.     function(player,message)
  503.         local message=string.lower(message)
  504.         local message=string.gsub(message,"read","")
  505.         local count=database.getData("data","count")
  506.         local dataKey=string.gsub(message," ","")
  507.         local data=database.getData("data",dataKey)
  508.         if data==false then
  509.             outputMessage("No data saved under the key "..dataKey)
  510.         else
  511.             outputMessage("Data : "..data)
  512.         end
  513.     end,
  514.     ["list"]=
  515.     function(player,message)
  516.         local count=database.getData("data","count")
  517.         if count==false then
  518.             outputMessage("There is no data at all to read")
  519.             return
  520.         end
  521.         for i=1,tonumber(count) do
  522.             --outputMessage(database.getData("data",i))
  523.             outputMessage("Key : "..i..", Data : "..database.getData("data",i),10000,"en",true)
  524.             sleep(5)
  525.         end
  526.     end,
  527.     ["newjoke"]=
  528.     function(player,message)
  529.         local message=string.lower(message)
  530.         local message=string.gsub(message,"newjoke","")
  531.         local count=database.getData("jokes","count")
  532.         if count==false then
  533.             database.setData("jokes","count",1)
  534.             count=0
  535.         end
  536.         database.setData("jokes","count",count+1)
  537.         database.setData("jokes",count+1,message)
  538.         outputMessage("Thank you for telling that joke to me!")
  539.     end,
  540.     ["facing"]=
  541.     function(player,message)
  542.         outputMessage("I am currently facing "..getFacing())
  543.     end,
  544.     ["time"]=
  545.     function(player,message)
  546.         local currentTime=os.time()
  547.         local hours=splitString(currentTime,".",1)
  548.         local minutes=math.ceil(splitString(currentTime,".",2)*0.06)
  549.         local timeString="It is currently "..minutes.." minutes past "..hours
  550.         outputMessage(timeString)
  551.     end,
  552.     ["run"]=
  553.     function(player,message)
  554.         local command=string.gsub(message,"run","")
  555.         outputMessage("Running the following command : "..command)
  556.         shell.run(command)
  557.     end,
  558. }
  559.  
  560.  
  561. function main()
  562.     repeat
  563.     local timer=os.startTimer(1)
  564.     event,player,message=os.pullEvent()
  565.     if event=="timer" and player==timer then
  566.         local percentage=math.random(100)
  567.         if percentage<1.75 and lastPlayer~=nil then
  568.             askMood(lastPlayer)
  569.         elseif percentage<5.5 then
  570.             tellJoke()
  571.         else
  572.             updateBoredom(5)
  573.         end
  574.     end
  575.     if event=="chat" then
  576.         if string.find(string.lower(message),turtleName)==nil or owners[player]~=true then return end
  577.         for messageString,messageFunction in pairs(commands) do
  578.             if string.find(string.lower(message),string.lower(messageString))~=nil then
  579.                 local message=string.gsub(string.lower(message),turtleName.." ","")
  580.                 local message=string.gsub(message,turtleName,"")
  581.                 messageFunction(player,message)
  582.                 lastPlayer=player
  583.                 break
  584.             end
  585.         end
  586.     elseif event=="death" then
  587.         outputMessage("Haha "..player.." you are a noob!")
  588.     end  
  589.     until message~=nil and type(message)=="string" and string.find(string.lower(message),"shut down ai")~=nil
  590. end
  591.  
  592. function splitString(sourceString,splittingChar,index)
  593.     results={}
  594.     currentWord=""
  595.     for i=1,string.len(sourceString) do
  596.             letter=string.sub(sourceString,i,i)
  597.             if letter==splittingChar then
  598.                 results[#results+1]=currentWord
  599.                 currentWord=""
  600.             else
  601.                 currentWord=currentWord..letter
  602.             end
  603.     end
  604.     results[#results+1]=currentWord
  605.     return results[index]
  606. end
  607.  
  608. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement