libraryaddict

Map

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