Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local speaker=peripheral.wrap("right")
- local chatBox=peripheral.wrap("left")
- local mood=50
- local boredom=1
- local boreds={"Just fine","Good","Kinda bored","Bored","Rather bored"}
- local turtleName="nanoturtle"
- local lastPlayer=nil
- local pos={["x"]=0,["y"]=0,["z"]=0}
- local inside=true
- local teleporters={}
- local talking=true
- local owners={["NanoBob_"]=true,["Yaplax"]=false,}
- if fs.exists("database")==false then
- shell.run("pastebin get PdhBaBdZ database")
- end
- local success=os.loadAPI("database")
- if success==false or database==nil then
- error "The database system failed to load"
- end
- if fs.exists("databases/data")==false then
- database.createDataFile("data")
- end
- if fs.exists("databases/jokes")==false then
- database.createDataFile("jokes")
- end
- if fs.exists("databases/position")==false then
- database.createDataFile("position")
- end
- function changePos(key,change)
- if pos[key]==nil then
- pos[key]=0
- end
- pos[key]=pos[key]+change
- database.setData("position",key,pos[key])
- end
- function updatePos(key)
- database.setData("position",key,pos[key])
- end
- function getFilePos()
- if database.getData("position","x")==false then
- pos.x=0
- pos.y=0
- pos.z=0
- database.setData("position","x",0)
- database.setData("position","y",0)
- database.setData("position","z",0)
- else
- pos.x=database.getData("position","x")
- pos.y=database.getData("position","y")
- pos.z=database.getData("position","z")
- end
- end
- getFilePos()
- function move(direction,blocks)
- if direction=="up" then
- for i=1,blocks do
- moved=turtle.up()
- if moved==true then
- changePos("y",1)
- else
- return false
- end
- end
- elseif direction=="down" then
- for i=1,blocks do
- moved=turtle.down()
- if moved==true then
- changePos("y",-1)
- else
- return false
- end
- end
- elseif direction=="back" then
- local facing=string.lower(getFacing())
- for i=1,blocks do
- local moved=turtle.back()
- if moved==false then return false end
- if facing=="north" then
- changePos("z",1)
- elseif facing=="east" then
- changePos("x",-1)
- elseif facing=="south" then
- changePos("z",-1)
- elseif facing=="west" then
- changePos("x",1)
- end
- end
- else
- local facing=string.lower(getFacing())
- for i=1,blocks do
- local moved=turtle.forward()
- if moved==false then return false end
- if facing=="north" then
- changePos("z",-1)
- elseif facing=="east" then
- changePos("x",1)
- elseif facing=="south" then
- changePos("z",1)
- elseif facing=="west" then
- changePos("x",-1)
- end
- end
- end
- return true
- end
- function outputMessage(message,range,language)
- if talking==true and type(message)=="string" then
- if range==nil then
- speaker.speak(message)
- elseif lanugage==nil then
- speaker.speak(message,range)
- else
- speaker.speak(message,range,language)
- end
- end
- end
- function stateMood()
- local boredomIndex=math.ceil(boredom/(100/#boreds)-0.5)+1
- if boredomIndex==nil then
- boredomIndex=1
- end
- local currentState=boreds[boredomIndex]
- if currentState==nil then
- currentState="Unaware of my current state"
- end
- outputMessage("I am "..currentState)
- end
- function getFacing()
- turtle.select(2)
- turtle.equipRight()
- local compass=peripheral.wrap("right")
- local heading=compass.getFacing()
- turtle.select(2)
- turtle.equipRight()
- return heading
- end
- local facingTable={
- ["north"]=1,
- ["east"]=2,
- ["south"]=3,
- ["west"]=4,
- }
- function setFacing(facing)
- local facing=string.lower(facing)
- local currentFacing=string.lower(getFacing())
- if facing==currentFacing then
- return
- else
- local currentFacingIndex=facingTable[currentFacing]
- local facingIndex=facingTable[facing]
- local turns=facingIndex-currentFacingIndex
- if turns<0 then
- turns=turns+4
- end
- if turns==3 then
- turtle.turnLeft()
- else
- for i=1,turns do
- turtle.turnRight()
- end
- end
- end
- end
- function updateMood()
- if boredom<=50 then
- mood=mood+2
- else
- mood=mood-2
- end
- if mood>100 then
- mood=100
- elseif mood<1 then
- mood=1
- end
- end
- function updateBoredom(number)
- boredom=boredom+number
- if boredom>100 then
- boredom=100
- elseif boredom<1 then
- boredom=1
- end
- updateMood()
- end
- function askMood(player)
- outputMessage("How are you doing "..player.."?")
- end
- function tellJoke()
- local jokeCount=database.getData("jokes","count")
- local currentJoke=math.random(jokeCount)
- local joke=database.getData("jokes",currentJoke)
- if joke==nil then
- return
- end
- outputMessage(joke)
- end
- function getAvailableTeleports()
- local teleporter=peripheral.wrap("bottom")
- local teleports={}
- if teleporter==nil then
- return false
- else
- teleports=teleporter.getLinks()
- end
- return teleports
- end
- function teleportTo(name)
- local name=string.gsub(name," ","")
- local number=tonumber(name)
- local teleporter=peripheral.wrap("bottom")
- local teleports=getAvailableTeleports()
- local matchinTeleport=nil
- if number==nil then
- for id,teleportData in pairs(teleports) do
- if teleportData.name==name then
- matchinTeleport=id+1
- break
- end
- end
- if matchinTeleport==nil then
- outputMessage("no teleport found under the name, " .. name)
- return false,"no teleport found under the name, " .. name
- end
- else
- matchinTeleport=number
- end
- outputMessage("Teleport ID : "..matchinTeleport)
- sleep(1.5)
- local teleporter=peripheral.wrap("bottom")
- local teleported=teleporter.teleport(matchinTeleport)
- pos.x=teleports[matchinTeleport-1].x
- pos.y=teleports[matchinTeleport-1].y
- pos.z=teleports[matchinTeleport-1].z
- updatePos("x")
- updatePos("y")
- updatePos("z")
- return teleported
- end
- function pathFind(x,y,z)
- local yDistance=(pos.y-y)*-1
- print("Y axis travel distance : "..yDistance)
- local yTravel=true
- if yDistance>0 then
- local yTravel=move("up",yDistance)
- elseif yDistance<0 then
- local yTravel=move("down",yDistance*-1)
- end
- local xDistance=(pos.x-x)*-1
- print("X axis travel distance : "..xDistance)
- local xTravel=true
- if xDistance>0 then
- setFacing("east")
- elseif xDistance<0 then
- setFacing("west")
- xDistance=xDistance*-1
- end
- xTravel=move("forward",xDistance)
- local zDistance=(pos.z-z)*-1
- print("Z axis travel distance : "..zDistance)
- local zTravel=true
- if zDistance>0 then
- setFacing("south")
- elseif zDistance<0 then
- setFacing("north")
- zDistance=zDistance*-1
- end
- zTravel=move("forward",zDistance)
- if xTravel==false or zTravel==false or yTravel==false then
- pathFind(x,y,z)
- end
- end
- local commands={
- ["Hey"]=
- function(player,message)
- outputMessage("Hey "..string.gsub(player,"_",""))
- end,
- ["Whatsup"]=stateMood,
- ["how are you"]=stateMood,
- ["What is up"]=stateMood,
- ["go"]=
- function(player,message)
- local message=string.lower(message)
- local direction=splitString(message," ",2)
- local blocks=tonumber(splitString(message," ",3))
- if blocks==nil then
- blocks=1
- end
- outputMessage("Going "..direction.." for "..blocks.." meters")
- if direction=="left" then
- turtle.turnLeft()
- move("forward",blocks)
- elseif direction=="right" then
- turtle.turnRight()
- move("forward",blocks)
- elseif direction=="forwards" then
- move("forward",blocks)
- elseif direction=="back" then
- move("back",blocks)
- elseif direction=="up" then
- move("up",blocks)
- elseif direction=="down" then
- move("down",blocks)
- end
- if blocks>10 then
- updateBoredom((blocks-10)/10)
- end
- end,
- ["fuel level"]=
- function(player,message)
- outputMessage("My current Fuel level is : "..turtle.getFuelLevel())
- end,
- ["repeat"]=
- function(player,message)
- local message=string.lower(message)
- local message=string.gsub(message,"repeat","")
- outputMessage(message)
- end,
- ["dig"]=
- function(player,message)
- local message=string.lower(message)
- local direction=splitString(message," ",2)
- local blocks=tonumber(splitString(message," ",3))
- if blocks==nil then
- blocks=1
- end
- outputMessage("Digging "..direction.." for "..blocks.." blocks")
- turtle.select(1)
- turtle.equipRight()
- if direction=="left" then
- turtle.turnLeft()
- for i=1,blocks do
- turtle.dig()
- move("forward")
- turtle.dig()
- end
- elseif direction=="right" then
- turtle.turnRight()
- for i=1,blocks do
- turtle.dig()
- move("forward")
- turtle.dig()
- end
- elseif direction=="forwards" then
- for i=1,blocks do
- turtle.dig()
- move("forward")
- turtle.dig()
- end
- elseif direction=="back" then
- for i=1,blocks do
- turtle.dig()
- turtle.back()
- turtle.dig()
- end
- elseif direction=="up" then
- for i=1,blocks do
- turtle.digUp()
- move("up")
- turtle.digUp()
- end
- elseif direction=="down" then
- for i=1,blocks do
- turtle.digDown()
- move("down")
- turtle.digDown()
- end
- end
- turtle.select(1)
- turtle.equipRight()
- end,
- ["attack"]=
- function()
- local message=string.lower(message)
- local direction=splitString(message," ",3)
- local percentage=math.random(100)
- if percentage<20 then
- outputMessage("To battle!")
- end
- turtle.select(1)
- turtle.equipRight()
- if direction==nil then
- turtle.attack()
- elseif direction=="down" then
- turtle.attackDown()
- elseif direction=="up" then
- turtle.attackUp()
- end
- turtle.select(1)
- turtle.equipRight()
- end,
- ["write"]=
- function(player,message)
- local message=string.lower(message)
- local message=string.gsub(message,"write","")
- local count=database.getData("data","count")
- if count==false then
- database.setData("data","count",1)
- count=0
- end
- database.setData("data","count",count+1)
- database.setData("data",count+1,message)
- outputMessage("Set data key "..count+1 .." to "..message)
- end,
- ["setpos"]=
- function(player,message)
- local message=string.lower(message)
- local message=string.gsub(message,"setpos ","")
- local x=splitString(message,",",1)
- local y=splitString(message,",",2)
- local z=splitString(message,",",3)
- if y==nil then
- outputMessage("Please seperate coordinates with commas.")
- return
- end
- pos.x=x
- pos.y=y
- pos.z=z
- updatePos("x")
- updatePos("y")
- updatePos("z")
- end,
- ["go to"]=
- function(player,message)
- local message=string.lower(message)
- local message=string.gsub(message,"go to ","")
- local x=splitString(message,",",1)
- local y=splitString(message,",",2)
- local z=splitString(message,",",3)
- if y==nil then
- outputMessage("Please seperate coordinates with commas.")
- return
- end
- pathFind(x,y,z)
- end,
- ["getpos"]=
- function(player,message)
- outputMessage("My position is : "..pos.x..","..pos.y..","..pos.z)
- end,
- ["teleports"]=
- function(player,message)
- local message=string.lower(message)
- if getAvailableTeleports()==false then
- outputMessage("I am not currently on a teleporter!")
- return
- end
- local teleports=getAvailableTeleports()
- for id,data in pairs(teleports) do
- outputMessage("Teleport ".. id+1 ..", "..data.name)
- print("Teleport ".. id+1 ..", "..data.name)
- sleep(5)
- end
- end,
- ["teleport to"]=
- function(player,message)
- name=string.gsub(message,"teleport to","")
- if getAvailableTeleports()==false then
- outputMessage("I am not currently on a teleporter!")
- return
- end
- local teleported,reason=teleportTo(name)
- if teleported==false then
- outputMessage("Teleport failed, reason : "..tostring(reason))
- return
- else
- outputMessage("Sucessful teleport!")
- end
- end,
- ["setData"]=
- function(player,message)
- local message=string.lower(message)
- local message=string.gsub(message,"setData ","")
- local key=splitString(message," ",1)
- local message=string.gsub(message,key,"")
- database.setData("data",key,message)
- outputMessage("Set data key "..key.." to "..message)
- end,
- ["read"]=
- function(player,message)
- local message=string.lower(message)
- local message=string.gsub(message,"read","")
- local count=database.getData("data","count")
- local dataKey=string.gsub(message," ","")
- local data=database.getData("data",dataKey)
- if data==false then
- outputMessage("No data saved under the key "..dataKey)
- else
- outputMessage("Data : "..data)
- end
- end,
- ["list"]=
- function(player,message)
- local count=database.getData("data","count")
- if count==false then
- outputMessage("There is no data at all to read")
- return
- end
- for i=1,tonumber(count) do
- --outputMessage(database.getData("data",i))
- outputMessage("Key : "..i..", Data : "..database.getData("data",i),10000,"en",true)
- sleep(5)
- end
- end,
- ["newjoke"]=
- function(player,message)
- local message=string.lower(message)
- local message=string.gsub(message,"newjoke","")
- local count=database.getData("jokes","count")
- if count==false then
- database.setData("jokes","count",1)
- count=0
- end
- database.setData("jokes","count",count+1)
- database.setData("jokes",count+1,message)
- outputMessage("Thank you for telling that joke to me!")
- end,
- ["facing"]=
- function(player,message)
- outputMessage("I am currently facing "..getFacing())
- end,
- ["time"]=
- function(player,message)
- local currentTime=os.time()
- local hours=splitString(currentTime,".",1)
- local minutes=math.ceil(splitString(currentTime,".",2)*0.06)
- local timeString="It is currently "..minutes.." minutes past "..hours
- outputMessage(timeString)
- end,
- ["run"]=
- function(player,message)
- local command=string.gsub(message,"run","")
- outputMessage("Running the following command : "..command)
- shell.run(command)
- end,
- }
- function main()
- repeat
- local timer=os.startTimer(1)
- event,player,message=os.pullEvent()
- if event=="timer" and player==timer then
- local percentage=math.random(100)
- if percentage<1.75 and lastPlayer~=nil then
- askMood(lastPlayer)
- elseif percentage<5.5 then
- tellJoke()
- else
- updateBoredom(5)
- end
- end
- if event=="chat" then
- if string.find(string.lower(message),turtleName)==nil or owners[player]~=true then return end
- for messageString,messageFunction in pairs(commands) do
- if string.find(string.lower(message),string.lower(messageString))~=nil then
- local message=string.gsub(string.lower(message),turtleName.." ","")
- local message=string.gsub(message,turtleName,"")
- messageFunction(player,message)
- lastPlayer=player
- break
- end
- end
- elseif event=="death" then
- outputMessage("Haha "..player.." you are a noob!")
- end
- until message~=nil and type(message)=="string" and string.find(string.lower(message),"shut down ai")~=nil
- end
- function splitString(sourceString,splittingChar,index)
- results={}
- currentWord=""
- for i=1,string.len(sourceString) do
- letter=string.sub(sourceString,i,i)
- if letter==splittingChar then
- results[#results+1]=currentWord
- currentWord=""
- else
- currentWord=currentWord..letter
- end
- end
- results[#results+1]=currentWord
- return results[index]
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement