Guest User

Untitled

a guest
Mar 20th, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.36 KB | None | 0 0
  1. local curfloor
  2. local sel
  3.  
  4. local function loadOnStartup()
  5.   local h = fs.open('.data', 'r') -- hidden file
  6.  
  7.   assert(h, "ERROR: Could not open data file in read mode.")
  8.  
  9.   local t1 = h.readLine()
  10.   local t2 = h.readLine()
  11.   h.close()
  12.  
  13.   if t1 then
  14.     curfloor = t1
  15.   end
  16.  
  17.   if t2 then
  18.     sel = t2
  19.   end
  20. end
  21.  
  22. local function persistData()
  23.   local h = fs.open('.data', 'w')
  24.  
  25.   assert(h, "ERROR: Could not open data file in write mode.")
  26.  
  27.   h.write(curfloor..'\n')
  28.   h.write(sel)
  29.   h.close()
  30. end
  31.  
  32. local function setCurfloor(s)
  33.   curfloor = s
  34.   persistData()
  35. end
  36.  
  37. local function setSel(s)
  38.   sel = s
  39.   persistData()
  40. end
  41.  
  42. local function down(n)
  43.   for i=1, n do
  44.     rs.setBundledOutput("back", colors.white)
  45.     sleep(.5)
  46.     rs.setBundledOutput("back", 0)
  47.     sleep(.5)
  48.   end
  49. end
  50.  
  51. local function up(u)
  52.   for i=1, u do
  53.     rs.setBundledOutput("back", colors.orange)
  54.     sleep(.5)
  55.     rs.setBundledOutput("back", 0)
  56.     sleep(1)
  57.   end
  58. end
  59.  
  60. local function ground()
  61.   print(sel)
  62.   print(curfloor)
  63.  
  64.   if curfloor == sel then
  65.     print("already here")
  66.   else
  67.     if curfloor == "second" then
  68.       down(5)
  69.       setCurfloor("ground")
  70.     else
  71.       down(22)
  72.       setCurfloor("ground")
  73.     end
  74.   end
  75. end
  76.  
  77. function second()
  78.   print(sel)
  79.   print(curfloor)
  80.  
  81.   if curfloor == sel then
  82.     print("already here")
  83.   else
  84.     if curfloor == "ground" then
  85.       up(5)
  86.       setCurfloor("second")
  87.     else
  88.       down(17)
  89.       setCurfloor("second")
  90.     end
  91.   end
  92. end
  93.  
  94. function top()
  95.   print(sel)
  96.   print(curfloor)
  97.  
  98.   if curfloor == sel then
  99.     print("already here")
  100.   else
  101.     if curfloor == "ground" then
  102.       up(22)
  103.       setCurfloor("mountain")
  104.     else
  105.       up(17)
  106.       setCurfloor("mountain")
  107.     end
  108.   end
  109. end
  110.  
  111. rednet.open("right") -- you want to be using channels if you are going to use 'modem_message' not rednet api
  112.  
  113.  
  114. if fs.exists('.data') then
  115.   loadOnStartup()
  116. else
  117.   curfloor = 'ground'
  118.   sel = 'ground'
  119. end
  120.  
  121. while true do
  122.   local event, side, sendChannel, replyChannel, msg, distSent = os.pullEvent()
  123.  
  124.   if event == "modem_message" then
  125.     setSel(msg)
  126.     print(event)
  127.     print(sel)
  128.     if sel == "ground" then
  129.       ground()
  130.     elseif sel == "second" then
  131.       second()
  132.     else
  133.       top()
  134.     end
  135.   else
  136.     print("not correct signal")
  137.   end
  138. end
  139.  
  140. --shell.run("elevator")
Advertisement
Add Comment
Please, Sign In to add comment