Advertisement
NanoBob

Cortana

Nov 25th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.74 KB | None | 0 0
  1. if fs.exists("event")==false then shell.run("pastebin get L7KYC10V event") end
  2. os.loadAPI("event")
  3.  
  4. if fs.exists("database")==false then shell.run("pastebin get PdhBaBdZ database") end
  5. os.loadAPI("database")
  6.  
  7. local pos={}
  8. local peripherals={}
  9. local owner="NanoBob_"
  10.  
  11. function init()
  12.     if fs.exists("cdata/pos")==false then
  13.         fs.open("cdata/pos","w").close()
  14.     end
  15.     local file=fs.open("cdata/pos","r")
  16.     local line=file.readLine()
  17.     file.close()
  18.     if line=="" or line==nil then line="0,0,0" end
  19.     pos.x=splitString(line,",",1)
  20.     pos.y=splitString(line,",",2)
  21.     pos.z=splitString(line,",",3)
  22.     savePosition()
  23. end
  24.  
  25. function splitString(sourceString,splittingChar,index)
  26.     results={}
  27.     currentWord=""
  28.     for i=1,string.len(sourceString) do
  29.         letter=string.sub(sourceString,i,i)
  30.         if letter==splittingChar then
  31.             results[#results+1]=currentWord
  32.             currentWord=""
  33.         else
  34.             currentWord=currentWord..letter
  35.         end
  36.     end
  37.     results[#results+1]=currentWord
  38.     return results[index]
  39. end
  40.  
  41. function equipPeripheral(itemName)
  42.     for i=1,16 do
  43.         if turtle.getItemCount(i)>0 and turtle.getItemDetail(i).name==itemName then
  44.             if  peripherals[peripheral.getType("left")]~=nil then
  45.                 peripherals[peripheral.getType("left")]=nil
  46.             end
  47.             print(turtle.getItemDetail(i).name.." found")
  48.             turtle.select(i)
  49.             turtle.equipLeft()
  50.             sleep(0.05)
  51.             print(tostring(peripheral.getType("left")))
  52.             peripherals[peripheral.getType("left")]=peripheral.wrap("left")
  53.         end
  54.     end
  55. end
  56.  
  57. function output(message)
  58.     if peripherals.chatBox==nil then
  59.         equipPeripheral("PeripheralsPlusPlus:chatBox")
  60.     end
  61.     peripheral.wrap("left").say("<Cortana> "..message)
  62. end
  63.  
  64. function savePosition()
  65.     fs.delete("cdata/pos")
  66.     file=fs.open("cdata/pos","w")
  67.     file.write(pos.x..","..pos.y..","..pos.z)
  68.     file.close()
  69. end
  70.  
  71. function setPos(x,y,z)
  72.     pos.x=tonumber(x)
  73.     pos.y=tonumber(y)
  74.     pos.z=tonumber(z)
  75.     savePosition()
  76. end
  77.  
  78. function posCommand(message)
  79.     local x=splitString(message," ",1)
  80.     local y=splitString(message," ",2)
  81.     local z=splitString(message," ",3)
  82.     if y==nil or y==" " then
  83.         output("Please seperate coordinates with spaces.")
  84.         return
  85.     end
  86.     setPos(x,y,z)
  87.     output("Successfully modified coordinates")
  88. end
  89.  
  90. function goCommand(message)
  91.     local direction=splitString(message," ",1)
  92.     local distance=tonumber(splitString(message," ",2))
  93.     if direction==nil then direction="forward" end
  94.     if distance==nil then distance=1 end
  95.     print(direction..":"..distance)
  96.     if direction=="left" then
  97.         turtle.turnLeft()
  98.         for i=1,distance do
  99.             turtle.forward()
  100.         end
  101.     elseif direction=="right" then
  102.         turtle.turnRight()
  103.         for i=1,distance do
  104.             turtle.forward()
  105.         end
  106.     elseif turtle[direction]~=nil then
  107.         for i=1,distance do
  108.             turtle[direction]()
  109.         end
  110.     end
  111. end
  112.  
  113. function digCommand(message)
  114.     local direction=splitString(message," ",1)
  115.     local distance=tonumber(splitString(message," ",2))
  116.     if direction==nil then direction="forward" end
  117.     if distance==nil then distance=1 end
  118.     print(direction..":"..distance)
  119.     if direction=="left" then
  120.         turtle.turnLeft()
  121.         for i=1,distance do
  122.             turtle.forward()
  123.             turtle.dig()
  124.         end
  125.     elseif direction=="right" then
  126.         turtle.turnRight()
  127.         for i=1,distance do
  128.             turtle.forward()
  129.             turtle.dig()
  130.         end
  131.     elseif turtle[direction]~=nil then
  132.         for i=1,distance do
  133.             turtle[direction]()
  134.             if direction=="up" then
  135.                 turtle.digUp()
  136.             elseif direction=="forward" then
  137.                 turtle.dig()
  138.             elseif direction=="down" then
  139.                 turtle.digDown()
  140.             end
  141.         end
  142.     end
  143. end
  144.  
  145. function runCommand(message)
  146.     output(tostring(shell.run(message)))   
  147. end
  148.  
  149.  
  150. local insults={"hhh","Fuck you too","Go fuck yourself","I dare you to do that again motherfucker!"}
  151. function insulted(message,player)
  152.     local returnable=insults[math.random(#insults)]
  153.     if returnable=="hhh" then
  154.         returnable=message
  155.     end
  156.     output(player.." "..returnable)
  157. end
  158.  
  159. local keyStrings={
  160.     ["setpos"]=posCommand,
  161.     ["go"]=goCommand,
  162.     ["fuck you"]=insulted,
  163.     ["run"]=runCommand,
  164.     ["dig"]=digCommand,
  165. }
  166.  
  167. local restricted={
  168.     ["setpos"]=true,
  169.     ["go"]=true,
  170.     ["run"]=true,
  171.     ["dig"]=true,
  172. }
  173.  
  174. function handleInput(player,message)
  175.     if string.lower(message):match("cortana")==nil then return end
  176.     local message=string.gsub(message,"cortana " ,"")
  177.     for str,func in pairs(keyStrings) do
  178.         local message=string.lower(message)
  179.         if message:match(str)~=nil then
  180.             print("Command detected: "..str..". In: "..message)
  181.             local message=string.gsub(message,str.." " ,"")
  182.             if restricted[str]==true and player~=owner then
  183.                 output("I'm sorry but I can not do this for you.")
  184.                 break
  185.             end
  186.             keyStrings[str](message,player)
  187.             break
  188.         end
  189.     end
  190. end
  191. event.addHandler("chat",handleInput)
  192.  
  193. init()
  194. equipPeripheral("PeripheralsPlusPlus:chatBox")
  195.  
  196. while true do
  197.     event.handleCCEvents()
  198. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement