Advertisement
Guest User

gigi.lua

a guest
Feb 15th, 2020
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.48 KB | None | 0 0
  1. local cmp = require("component")
  2. if cmp.isAvailable("redstone") == false then print("redstone card or redstone controller block is required") os.exit() end
  3. rs = cmp.redstone
  4. local wifi = cmp.modem
  5. local position = -1
  6. event = require("event")
  7. local sides = require("sides")
  8. local history = {-1,-1,-1}
  9. local outCycles = 4
  10. playerName = ""
  11. local inCycles = 0
  12. local bankServerAddr
  13. local cyc = 0
  14. local cycDistance = 0
  15. local steps = 0
  16. local gpu = cmp.gpu
  17. local pr = ""
  18. running = true
  19. function closer()
  20.   running = false
  21. end
  22. local function connectToBank()
  23.   print("Connecting to the SliwiBank...")
  24.   wifi.broadcast(66,"BANK_CONNECT #GigiZaShagi 2v6rNacPg5uUCpIX")
  25.   local mes = {event.pull(5,"modem_message")}
  26.   if mes[1] == nil then
  27.     print("Handshake timeout exceeded")
  28.     return false
  29.   end
  30.   bankServerAddr = mes[3]
  31.   local status = string.match(mes[6], "[A-Z_]+")
  32.   if status == "BANK_HELLO" then
  33.     print("Server sent HELLO. Connection established")
  34.     return true
  35.   end
  36.   print("Server sent error: "..mes[6])
  37.   return false
  38. end
  39. local function disconnectFromBank()
  40.   print("Disconnecting...")
  41.   wifi.send(bankServerAddr, 66, "BANK_DISCONNECT")
  42. end
  43. local function payToPlayer()
  44.   --if playerName == nil then return false end
  45.   local money = cycDistance/5
  46.   pr = pr.."Player "..playerName.." will receive "..money
  47.   wifi.send(bankServerAddr, 66, "BANK_TRANSFER "..playerName.." "..money)
  48.   local e = {event.pull("modem_message")}
  49.   pr = e[6]
  50. end
  51. function onBioTouch(_, _, _, _, _, name)
  52.   playerName = name
  53.   pr = pr..playerName
  54.   require("component").redstone.setOutput(require("sides").east, 15)
  55.   event.timer(4, function()
  56.     require("component").redstone.setOutput(sides.east, 0)
  57.   end)
  58. end
  59. event.listen("interrupted", closer)
  60. event.listen("touch", onBioTouch)
  61. function findDelta()
  62.   history[1] = history[2]
  63.   history[2] = history[3]
  64.   history[3] = position
  65.   print(history[1].." "..history[2].." "..history[3])
  66.   if position >= 0 then
  67.     local delta = history[1]-history[2]
  68.     print("delta "..(history[1]-history[2]))
  69.     if inCycles <= 10 then inCycles=inCycles+1 end
  70.     steps = steps+math.abs(delta)
  71.     cycDistance = cycDistance+math.abs(delta)
  72.   elseif position == -1 then
  73.     outCycles = outCycles+1
  74.     if outCycles > 3 then
  75.       if inCycles > 10 then
  76.         print("player out")
  77.         playerName = nil
  78.       else
  79.         outCycles = 0
  80.       end
  81.     else
  82.     print("player out "..outCycles) end
  83.   end
  84.   print("in cycles "..inCycles)
  85.   print(steps.." steps")
  86. end
  87.  
  88. wifi.open(66)
  89. if connectToBank() == false then
  90.   os.exit()
  91. end
  92.  
  93. while running == true do
  94.   cyc=cyc+1
  95.   print(pr)
  96.   if cyc == 1000 then
  97.     payToPlayer()
  98.     cyc = 0
  99.     cycDistance = 0
  100.   end
  101.   print("distance :"..cycDistance)
  102.   print("cyc = "..cyc)
  103.   local r = rs.getBundledInput(sides.north)
  104.   local poses = {}
  105.   for i=0,15,1 do
  106.     local sig = r[i]
  107.     if sig > 0 then
  108.       poses[#poses+1] = i
  109.     end
  110.   end
  111.  
  112.   --print("poses "..poses[1]..","..poses[2])
  113.   if #poses == 0 then
  114.     position = -1
  115.     print("no player")
  116.   elseif #poses == 1 then
  117.     position = poses[1]
  118.     print("player at "..position)
  119.   elseif #poses == 2 then
  120.     if math.abs(poses[1]-poses[2]) == 1 then
  121.       position = (poses[1]+poses[2])/2
  122.       print("player at "..position)
  123.     else
  124.       position = -2
  125.       print("multiple players error")
  126.     end
  127.   else
  128.     print("multiple players error")
  129.     position = -2
  130.   end
  131.   findDelta()
  132.   os.sleep(0.01)
  133.   require("term").setCursor(1,1)
  134.   gpu.fill(1,1,160,50," ")
  135. end
  136. disconnectFromBank()
  137. event.ignore("interrupted", closer)
  138. event.ignore("touch", onBioTouch)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement