Alyssa

Shop_Turtle

Mar 16th, 2015
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.79 KB | None | 0 0
  1. debug = false
  2. authTime = 100 --This might work better if it's changed
  3. mSide = "right" -- modem side
  4. tChan = 1337 --Channel for turtle communication, this should probably be changed
  5. --MoveAPI was made by Signify
  6. --He didn't say I can't use the api for programs,
  7. --But if he tells me to remove it, I will.
  8. if not fs.exists("/mapi") then
  9.     print("Installing MoveAPI by Signify")
  10.     fc = http.get("http://pastebin.com/raw.php?i=9QFxP4Ff").readAll()
  11.     f = fs.open("/mapi","w")
  12.     f.write(fc)
  13.     f.close()
  14.     print("Installed MoveAPI!")
  15.     os.loadAPI("mapi")
  16. else
  17.     if debug then
  18.         print("MoveAPI (by Signify) is installed!")
  19.     end
  20.     os.loadAPI("mapi")
  21. end
  22. if fs.exists("/authKey") then
  23.     if debug then
  24.         print("Found auth file")
  25.     end
  26.     f = fs.open("/authKey","r")
  27.     authKey = f.readAll()
  28.     f.close()
  29. else
  30.     error("authKey file does not exist.")
  31. end
  32. local MOD = 2^32
  33. local MODM = MOD-1
  34.  
  35. local function memoize(f)
  36.         local mt = {}
  37.         local t = setmetatable({}, mt)
  38.         function mt:__index(k)
  39.                 local v = f(k)
  40.                 t[k] = v
  41.                 return v
  42.         end
  43.         return t
  44. end
  45.  
  46. local function make_bitop_uncached(t, m)
  47.         local function bitop(a, b)
  48.                 local res,p = 0,1
  49.                 while a ~= 0 and b ~= 0 do
  50.                         local am, bm = a % m, b % m
  51.                         res = res + t[am][bm] * p
  52.                         a = (a - am) / m
  53.                         b = (b - bm) / m
  54.                         p = p*m
  55.                 end
  56.                 res = res + (a + b) * p
  57.                 return res
  58.         end
  59.         return bitop
  60. end
  61.  
  62. local function make_bitop(t)
  63.         local op1 = make_bitop_uncached(t,2^1)
  64.         local op2 = memoize(function(a) return memoize(function(b) return op1(a, b) end) end)
  65.         return make_bitop_uncached(op2, 2 ^ (t.n or 1))
  66. end
  67.  
  68. local bxor1 = make_bitop({[0] = {[0] = 0,[1] = 1}, [1] = {[0] = 1, [1] = 0}, n = 4})
  69.  
  70. local function bxor(a, b, c, ...)
  71.         local z = nil
  72.         if b then
  73.                 a = a % MOD
  74.                 b = b % MOD
  75.                 z = bxor1(a, b)
  76.                 if c then z = bxor(z, c, ...) end
  77.                 return z
  78.         elseif a then return a % MOD
  79.         else return 0 end
  80. end
  81.  
  82. local function band(a, b, c, ...)
  83.         local z
  84.         if b then
  85.                 a = a % MOD
  86.                 b = b % MOD
  87.                 z = ((a + b) - bxor1(a,b)) / 2
  88.                 if c then z = bit32_band(z, c, ...) end
  89.                 return z
  90.         elseif a then return a % MOD
  91.         else return MODM end
  92. end
  93.  
  94. local function bnot(x) return (-1 - x) % MOD end
  95.  
  96. local function rshift1(a, disp)
  97.         if disp < 0 then return lshift(a,-disp) end
  98.         return math.floor(a % 2 ^ 32 / 2 ^ disp)
  99. end
  100.  
  101. local function rshift(x, disp)
  102.         if disp > 31 or disp < -31 then return 0 end
  103.         return rshift1(x % MOD, disp)
  104. end
  105.  
  106. local function lshift(a, disp)
  107.         if disp < 0 then return rshift(a,-disp) end
  108.         return (a * 2 ^ disp) % 2 ^ 32
  109. end
  110.  
  111. local function rrotate(x, disp)
  112.     x = x % MOD
  113.     disp = disp % 32
  114.     local low = band(x, 2 ^ disp - 1)
  115.     return rshift(x, disp) + lshift(low, 32 - disp)
  116. end
  117.  
  118. local k = {
  119.         0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  120.         0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  121.         0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  122.         0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  123.         0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  124.         0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  125.         0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  126.         0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  127.         0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  128.         0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  129.         0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  130.         0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  131.         0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  132.         0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  133.         0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  134.         0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  135. }
  136.  
  137. local function str2hexa(s)
  138.         return (string.gsub(s, ".", function(c) return string.format("%02x", string.byte(c)) end))
  139. end
  140.  
  141. local function num2s(l, n)
  142.         local s = ""
  143.         for i = 1, n do
  144.                 local rem = l % 256
  145.                 s = string.char(rem) .. s
  146.                 l = (l - rem) / 256
  147.         end
  148.         return s
  149. end
  150.  
  151. local function s232num(s, i)
  152.         local n = 0
  153.         for i = i, i + 3 do n = n*256 + string.byte(s, i) end
  154.         return n
  155. end
  156.  
  157. local function preproc(msg, len)
  158.         local extra = 64 - ((len + 9) % 64)
  159.         len = num2s(8 * len, 8)
  160.         msg = msg .. "\128" .. string.rep("\0", extra) .. len
  161.         assert(#msg % 64 == 0)
  162.         return msg
  163. end
  164.  
  165. local function initH256(H)
  166.         H[1] = 0x6a09e667
  167.         H[2] = 0xbb67ae85
  168.         H[3] = 0x3c6ef372
  169.         H[4] = 0xa54ff53a
  170.         H[5] = 0x510e527f
  171.         H[6] = 0x9b05688c
  172.         H[7] = 0x1f83d9ab
  173.         H[8] = 0x5be0cd19
  174.         return H
  175. end
  176.  
  177. local function digestblock(msg, i, H)
  178.         local w = {}
  179.         for j = 1, 16 do w[j] = s232num(msg, i + (j - 1)*4) end
  180.         for j = 17, 64 do
  181.                 local v = w[j - 15]
  182.                 local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))
  183.                 v = w[j - 2]
  184.                 w[j] = w[j - 16] + s0 + w[j - 7] + bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))
  185.         end
  186.  
  187.         local a, b, c, d, e, f, g, h = H[1], H[2], H[3], H[4], H[5], H[6], H[7], H[8]
  188.         for i = 1, 64 do
  189.                 local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
  190.                 local maj = bxor(band(a, b), band(a, c), band(b, c))
  191.                 local t2 = s0 + maj
  192.                 local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
  193.                 local ch = bxor (band(e, f), band(bnot(e), g))
  194.                 local t1 = h + s1 + ch + k[i] + w[i]
  195.                 h, g, f, e, d, c, b, a = g, f, e, d + t1, c, b, a, t1 + t2
  196.         end
  197.  
  198.         H[1] = band(H[1] + a)
  199.         H[2] = band(H[2] + b)
  200.         H[3] = band(H[3] + c)
  201.         H[4] = band(H[4] + d)
  202.         H[5] = band(H[5] + e)
  203.         H[6] = band(H[6] + f)
  204.         H[7] = band(H[7] + g)
  205.         H[8] = band(H[8] + h)
  206. end
  207.  
  208. local function sha256(msg)
  209.         msg = preproc(msg, #msg)
  210.         local H = initH256({})
  211.         for i = 1, #msg, 64 do digestblock(msg, i, H) end
  212.         return str2hexa(num2s(H[1], 4) .. num2s(H[2], 4) .. num2s(H[3], 4) .. num2s(H[4], 4) ..
  213.                 num2s(H[5], 4) .. num2s(H[6], 4) .. num2s(H[7], 4) .. num2s(H[8], 4))
  214. end
  215.  
  216. function genValid(time)
  217.     if not time then
  218.         time = os.time()
  219.     end
  220.     tHash = authKey.." "..tostring(math.floor(math.floor(time() * authTime) / (authTime / 10)))
  221.     vKey = sha256(tHash)
  222.     return vKey:sub(1,10) --Just because
  223. end
  224.  
  225. function goto(x,y,z)
  226.     turtle.fMoveTo(x,y,z,"north","x","z","y")
  227. end
  228.  
  229. function getChest(x,y,z,side,amount)
  230.     --The turtle moves on the x-axis to avoid running into chests
  231.     --Then moves on the z-axis to get to the chest
  232.     --Then the y-axis to go to the chest
  233.     --Finnally it gets the items out of the chest
  234.     goto(x,y,z)
  235.     turtle.turnToDir(side)
  236.     turtle.suck(amount)
  237. end
  238.  
  239. function toBase()
  240.     --Probably didn't need a function for this
  241.     return turtle.fMoveTo(0,0,0,"north","z","y","x")
  242. end
  243.  
  244. modem = peripheral.wrap(mSide)
  245. modem.open(tChan)
  246.  
  247. while true do
  248.     e,side,chan,rchan,msg,dist = os.pullEvent()
  249.     if e == "modem_message" then
  250.         curTime = os.time()
  251.         validHash = genValid(curTime)
  252.         if msg:sub(1,10) == validHash then
  253.             args = {}
  254.             for w in msg:gmatch("[^ ]+") do
  255.                 table.insert(args,w)
  256.             end
  257.             rHash = args[1]
  258.             if args[2] then
  259.                 cmd = args[2]
  260.             end
  261.             if cmd == "get_chest" then
  262.                 if args[3] and args[4] and args[5] and args[6] and args[7] then
  263.                     args[3] = tonumber(args[3])
  264.                     args[4] = tonumber(args[4])
  265.                     args[5] = tonumber(args[5])
  266.                     args[7] = tonumber(args[7])
  267.                     modem.transmit(tChan, 1000, getValid(curTime).." OK")
  268.                     getChest(args[3],args[4],args[5],args[6],args[7])
  269.                     modem.transmit(tChan, 1000, getValid(curTime).." COMPLETE")
  270.                 end
  271.             elseif cmd == "deliver_item" then
  272.                 if args[3] and args[4] and args[5] then
  273.                     modem.transmit(tChan, 1000, getValid(curTime).." OK")
  274.                     args[3] = tonumber(args[3])
  275.                     args[4] = tonumber(args[4])
  276.                     args[5] = tonumber(args[5])
  277.                     turtle.moveTo(args[3],args[4],args[5]+1,"north","z","y","x")
  278.                     turtle.move("north",1)
  279.                     turtle.drop(turtle.getItemCount(1))
  280.                     turtle.move("south",1)
  281.                     toBase()
  282.                     modem.transmit(tChan, 1000, getValid(curTime).." COMPLETE")
  283.                 end
  284.             end
  285.         else
  286.             if debug then
  287.                 print("Needed: "..validHash)
  288.                 print("Received: "..msg:sub(1,10))
  289.             end
  290.         end
  291.     end
  292. end
Advertisement
Add Comment
Please, Sign In to add comment