libraryaddict

Untitled

Apr 25th, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.86 KB | None | 0 0
  1. --Map making
  2. Map = {}
  3. term.setCursorPos(1,1)
  4. termx = 1
  5. termy = 1
  6. function Clear()
  7.   term.clear()
  8.   term.setCursorPos(1,1)
  9. end
  10. function CreateEmpty()
  11.   termx = 1
  12.   termy = 1
  13.   for n=1,18 do
  14.     Map[n] = string.rep(" ", 50)
  15.   end
  16.   for n=1,4 do
  17.     Map[n] = string.sub(Map[n], 14, string.len(Map[n]))
  18.   end
  19.   Map[1] = "Press Ctrl  |"..Map[1]
  20.   Map[2] = "To go to the|"..Map[2]
  21.   Map[3] = "Menu.DontEdit"..Map[3]
  22.   Map[4] = "This._NoUse_|"..Map[4]
  23. end
  24.  
  25. function EditMap(Get)
  26.   local sMap = Map[y]
  27.   Map[y] = string.sub(sMap, 1, x-1)..Get..string.sub(sMap, x+1)
  28.     DrawLine()
  29.   if x == 49 then
  30.     term.setCursorPos(x,y)
  31.   end
  32.   DrawLine()
  33. end
  34.  
  35. function Draw()
  36.   Clear()
  37.   for n=1,#Map do
  38.     term.write(Map[n])
  39.     term.setCursorPos(1,n+1)
  40.   end
  41.   term.setCursorPos(termx, termy)
  42. end
  43.  
  44. function DrawLine()
  45.   term.setCursorPos(1, termy)
  46.   term.clearLine()
  47.   term.write(Map[termy])
  48.   term.setCursorPos(termx, termy)
  49. end
  50.  
  51. function Menu()
  52.   Clear()
  53.   print("1. Save")
  54.   print("2. Discard and redo")
  55.   print("3. Resume")
  56.   print("4. Quit")
  57.   print("5. Map Options")
  58.   print("6. Load Map")
  59.   while true do
  60.     term.setCursorPos(1,8)
  61.     event,param1 = os.pullEvent()
  62.     if event == "char" then
  63.       if param1 == "1" then
  64.         if Dir then
  65.           Save()
  66.           Draw()
  67.           Move()
  68.         else
  69.           term.setCursorPos(1,7)
  70.           write("Please run options first")
  71.           term.setCursorPos(1,8)
  72.         end
  73.       elseif param1 == "2" then
  74.         CreateEmpty()
  75.         Draw()
  76.         Move()
  77.       elseif param1 == "3" then
  78.         Draw()
  79.         Move()
  80.       elseif param1 == "4" then
  81.         Clear()
  82.         print("You quit the Map Program!")
  83.         error()
  84.       elseif param1 == "5" then
  85.         Options()
  86.         Clear()
  87.         Menu()
  88.       elseif param1 == "6" then
  89.         write("Load Map: ")
  90.         if not Load(read()) then term.setCursorPos(1,7) write("Please choose a proper file name") sleep(4) Menu() else
  91.         Clear()
  92.         Draw()
  93.         Move()
  94.         end
  95.       end
  96.     end
  97.   end
  98. end
  99.  
  100. function Options()
  101.   Clear()
  102.   repeat
  103.     Clear()
  104.     write("X Cord to start the player at: ")
  105.     xCord = read()
  106.     if not tonumber(xCord) then xCord = 22222222 end
  107.     if string.len(xCord) == 1 then
  108.       xCord = "0"..xCord
  109.     end
  110.   until tonumber(xCord) > 0 and tonumber(xCord) < 49
  111.   repeat
  112.   Clear()
  113.   write("Y Cord to start the player at: ")
  114.   yCord = read()
  115.   if not tonumber(yCord) then yCord = 22222222 end
  116.   if string.len(yCord) == 1 then
  117.     yCord = "0"..yCord
  118.   end
  119.   until tonumber(yCord) > 0 and tonumber(yCord) < 19
  120.   repeat
  121.   Clear()
  122.   write("Direction to start player 1-left, 2-right, 3-up, 4-down: ")
  123.   if not tonumber(Dir) then Dir = 22222222 end
  124.   Dir = read()
  125.   until tonumber(Dir) > 0 and tonumber(Dir) < 5
  126.   Poopbear = xCord..yCord..Dir
  127. end
  128.  
  129. function Load()
  130.   if fs.exists("Maps/"..Name) then
  131.     FP = io.open(Name, "r")
  132.     for line in FP:lines() do
  133.       Map[#Map+1] = line
  134.     end
  135.     Poopbear = Map[1]
  136.     table.remove(Map, 1)
  137.     FP:close()
  138.     return true
  139.   else
  140.     return false
  141.   end
  142. end
  143.  
  144. function Save()
  145.   if not Name then
  146.     Clear()
  147.     write("Name of file: ")
  148.     Name = read()
  149.   end
  150.   if not fs.isDir("Maps") then fs.makeDir("Maps") end
  151.   table.insert(Map, 1, Poopbear)
  152.   local pi = io.open("Maps/"..Name, "w")
  153.   for n=1,#Map do
  154.     pi:write(Map[n].."\n")
  155.   end
  156.   pi:close()
  157.   table.remove(Map, 1)
  158. end
  159.  
  160. function Move()
  161.   term.setCursorBlink(true)
  162.   while true do
  163.     event,param1,param2 = os.pullEvent()
  164.     if event == "char" then
  165.       local sMap = Map[termy]
  166.       Map[termy] = string.sub(sMap, 1, termx-1)..param1..string.sub(sMap, termx+1)
  167.       DrawLine()
  168.       if termx < 50 then
  169.         term.setCursorPos(termx+1, termy)
  170.         termx = termx+1
  171.       end
  172.     elseif event == "key" then
  173.       if param1 == 208 then-- Down
  174.         if termy < 18 then
  175.           term.setCursorPos(termx, termy+1)
  176.           termy = termy+1
  177.         end
  178.       elseif param1 == 203 then -- Left
  179.         if termx > 1 then
  180.           term.setCursorPos(termx-1, termy)
  181.           termx = termx-1
  182.         end
  183.       elseif param1 == 205 then -- right
  184.         if termx < 50 then
  185.           term.setCursorPos(termx+1, termy)
  186.           termx = termx+1
  187.         end
  188.       elseif param1 == 200 then -- up
  189.         if termy > 1 then
  190.           term.setCursorPos(termx, termy-1)
  191.           termy = termy-1
  192.         end
  193.       elseif param1 == 14 then -- Backspace
  194.         local sMap = Map[termy]
  195.         Map[termy] = string.sub(sMap, 1, termx-1).." "..string.sub(sMap, termx+1)
  196.         DrawLine()
  197.         if termx > 1 then
  198.           term.setCursorPos(termx-1, termy)
  199.           termx = termx-1
  200.         end
  201.       elseif param1 == 29 then --ctrl
  202.         Menu()
  203.         break
  204.       end
  205.     end      
  206.   end
  207. end
  208. Clear()
  209. CreateEmpty()
  210. Draw()
  211. Move()
Advertisement
Add Comment
Please, Sign In to add comment