Advertisement
Redxone

[Broken] - Builder Program

Jul 24th, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.33 KB | None | 0 0
  1. --]] Block Table [--
  2. term.redirect(term.native())
  3. local w, h = term.getSize()
  4. local selBlock = 1
  5. local sMenuIndex = 1
  6. local inSave = false
  7. local inGame = true
  8. local sPlay = false
  9.  
  10. local map = {}
  11. blocks = {
  12.     [0] = {name="Air",color="black",bcolor="black",graphic=" "},
  13.     {name="Dirt",color="yellow",bcolor="brown",graphic=" "}, -- 1
  14.     {name="Stone",color="lightGray",bcolor="gray",graphic="@"}, -- 2
  15.     {name="Sand",color="white",bcolor="yellow",graphic="+"}, -- 3
  16.     {name="Scifi",color="purple",bcolor="blue",graphic="0"}, -- 4
  17.     {name="Grass",color="green",bcolor="brown",graphic="*"}, -- 5
  18.     {name="Iron",color="white",bcolor="lightGray",graphic="/"}, -- 6
  19.     {name="Gravel",color="white",bcolor="brown",graphic="G"}, -- 7
  20.     {name="Quartz",color="lightGray",bcolor="white",graphic="X"}, -- 8
  21.     {name="LOL",color="black",bcolor="white",graphic="O"}, -- 9
  22.     {name="LAL",color="black",bcolor="white",graphic="?"}, -- 9
  23. }
  24.  
  25.  
  26.  
  27. local sMenu = {
  28.   {name="Save",run=function()
  29.     term.setCursorPos(1,h)
  30.     term.clearLine() write("Save As: ")
  31.     sa=read()
  32.     f = fs.open(sa,"w")
  33.     --for i = 1, #map do
  34.     --   f.writeLine(table.concat(map[i]))
  35.     -- end
  36.     f.write(textutils.serialize(map):gsub("\n%s*",""))
  37.     f.close()
  38.     term.setCursorPos(1,h)
  39.     term.clearLine() write("Saved As  "..sa)
  40.     drawMap()
  41.     inSave=false
  42.   end};
  43.   {name="Load",run=function()
  44.     term.setCursorPos(1,h)
  45.     term.clearLine()
  46.     write("Load: ")
  47.     sa=read()
  48.     if(fs.exists(sa) and sa ~= "" and  sa ~= " ")then
  49.       f = fs.open(sa,"r")
  50.       smap = f.readAll()
  51.       f.close()
  52.       if(type(smap) ~= "table")then
  53.         term.setCursorPos(1,h)
  54.         term.clearLine()
  55.         write("Not a valid save file!")
  56.         os.pullEvent("key")
  57.       else
  58.         term.setCursorPos(1,h)
  59.         term.clearLine()
  60.         map=textutils.unserialize(smap)
  61.         write("Save loaded "..sa)
  62.         os.pullEvent("key")  
  63.       end
  64.       -- for line in f.readLine do
  65.       --    map[#map+1] = {}
  66.       --  for i = 1, #line do
  67.       --    local l = line:sub(i,i)
  68.       --    map[#map][i] = tonumber(l) or tostring(l)
  69.       --    end
  70.       -- end
  71.     else
  72.       term.setCursorPos(1,h)
  73.       term.clearLine()
  74.       write("File Not Found - "..sa)  
  75.       os.pullEvent("key")    
  76.     end
  77.     inSave=false
  78.     drawMap()
  79.     end};
  80.     -- {name="Play",run=function()
  81.     --        sPlay=true
  82.     --        inSave=false
  83.     -- end};
  84.     {name="Load Mod",run=function()
  85.       term.setCursorPos(1,h)
  86.       term.clearLine()
  87.       write("Mod File (.bldmd): ")
  88.       sa=read()
  89.       local md = {}
  90.       f = fs.open(sa,"r")
  91.       con = textutils.unserialize(f.readAll())
  92.       --con=f.readAll()
  93.       f.close()
  94.       if fs.exists(sa) then
  95.         write("Loading....")
  96.         --oldblks = blocks
  97.         --blocks = setmetatable(con, {__index = blocks})
  98.         for i = 1, #con do
  99.           table.insert(blocks,con[i])
  100.         end
  101.         --blocks = oldblks
  102.         term.setCursorPos(1,h)
  103.         term.clearLine()
  104.         term.setCursorPos(1,h)
  105.         write("Mod Loaded! ")
  106.         os.pullEvent("key")
  107.       else
  108.          term.clearLine()
  109.          term.setCursorPos(1,h)
  110.          write("Mod Invalid or Not Found - "..sa)
  111.          os.pullEvent("key")
  112.        end    
  113.    
  114.      inSave=false
  115.      drawMap()
  116.      end};
  117.      {name="Exit",run=function() inGame = false
  118.          term.setBackgroundColor(colors.black)
  119.           term.setTextColor(colors.white)
  120.           term.clear()
  121.           term.setCursorPos(1,1)
  122.       end};
  123. }
  124.  
  125. function drawMenu()
  126.     term.setCursorPos(1,h)
  127.    term.setTextColor(colors.white)
  128.    term.setBackgroundColor(colors.gray)
  129.    if(not inSave)then
  130.     if(selBlock < 8)then mCalc = 1 else mCalc = (selBlock-8)+1 end
  131.     for i = mCalc, (mCalc-1)+8 do
  132.         if(i ~= selBlock)then
  133.             write("[")
  134.             term.setTextColor(colors[blocks[i].color])
  135.             term.setBackgroundColor(colors[blocks[i].bcolor])
  136.             write(blocks[i].graphic)
  137.             term.setTextColor(colors.white)
  138.             term.setBackgroundColor(colors.gray)
  139.             write("] ")
  140.        else
  141.             write(">")
  142.             term.setTextColor(colors[blocks[i].color])
  143.             term.setBackgroundColor(colors[blocks[i].bcolor])
  144.             write(blocks[i].graphic)
  145.             term.setTextColor(colors.white)
  146.             term.setBackgroundColor(colors.gray)
  147.             write("< ")
  148.        end    
  149.     end
  150.      term.setTextColor(colors.yellow)
  151.     write("Press Ctrl For Menu")
  152. end
  153.  
  154.      if(inSave)then
  155.          term.setCursorPos(1,h)
  156.              term.clearLine()
  157.              term.setTextColor(colors.white)
  158.          for i = 1, #sMenu do
  159.               if(i == sMenuIndex)then
  160.                     term.setTextColor(colors.yellow)
  161.                     write("[")
  162.                     term.setTextColor(colors.white)
  163.                     write(sMenu[i].name)
  164.                     term.setTextColor(colors.yellow)
  165.                     write("]")
  166.               else
  167.                     term.setTextColor(colors.yellow)
  168.                     write(" ")
  169.                     term.setTextColor(colors.white)
  170.                     write(sMenu[i].name)
  171.                     term.setTextColor(colors.yellow)
  172.                     write(" ")    
  173.               end    
  174.               term.setTextColor(colors.white)
  175.          end
  176.      end
  177. end
  178.  
  179. function buildMap(w,h)
  180.          for i = 1, h do
  181.         map[i] = {}
  182.        for c = 1, w do
  183.             map[i][c] = 0
  184.        end    
  185.     end
  186. end
  187.  
  188. function drawMap()
  189.     term.setBackgroundColor(colors.black)
  190.     term.clear()
  191.     term.setCursorPos(1,1)
  192.     for i = 1, #map do
  193.         for c = 1, #map[i] do
  194.             term.setTextColor(colors[blocks[map[i][c]].color])
  195.             term.setBackgroundColor(colors[blocks[map[i][c]].bcolor])
  196.             write(blocks[map[i][c]].graphic)    
  197.         end
  198.         write("\n")
  199.     end    
  200. end
  201.  
  202. function isPlayer()
  203.     local is = false
  204.     for i = 1, #map do
  205.         for c = 1, #map[i] do
  206.             if(map[i][c] == tonumber(4))then
  207.                  is=true
  208.             end
  209.         end
  210.     end
  211.         return is
  212. end
  213.  
  214. function getPlayerX()
  215.     local fx = 1
  216.     for i = 1, #map do
  217.         for c = 1, #map[i] do
  218.             if(map[i][c] == tonumber(4))then
  219.                 fx=i
  220.             end
  221.         end
  222.     end
  223.             return fx
  224. end
  225.  
  226. function getPlayerY()
  227.     local fy = 1
  228.     for i = 1, #map do
  229.         for c = 1, #map[i] do
  230.             if(map[i][c] == tonumber(4))then
  231.                 fy=c
  232.             end
  233.         end
  234.     end
  235.             return fy
  236. end
  237.  
  238. function drawMapBlock(x,y)
  239.     term.setCursorPos(tonumber(y),tonumber(x))
  240.      term.setTextColor(colors[blocks[map[x][y]].color])
  241.      term.setBackgroundColor(colors[blocks[map[x][y]].bcolor])
  242.     print(blocks[map[x][y]].graphic)    
  243. end
  244.  
  245. function placeBlock(id,x,y)
  246.     for i = 1, #blocks do
  247.  
  248.         if(i == id)then
  249.             map[x][y] = id  
  250.         end
  251.     end
  252. end
  253.  
  254. function breakBlock(x,y)
  255.             map[x][y] = 0
  256. end
  257.  
  258. buildMap(w,h-1)
  259. drawMap()
  260.  
  261.  
  262.  
  263.  
  264. function update()
  265.     drawMenu()
  266.    
  267.     a = {os.pullEvent()}
  268.     term.setCursorPos(1,1)
  269.     --if(a[1]=="timer" and not sPlay)then gTimer=os.startTimer(fps) end
  270.  
  271.     if( (a[1] == "mouse_click" and a[4] < h) or (a[1] == "mouse_drag" and a[4] < h) and not inSave)then
  272.         if(a[2] == 1)then
  273.             placeBlock(selBlock,a[4],a[3])  
  274.             drawMapBlock(a[4],a[3])  
  275.         elseif(a[2] == 2)then
  276.                     breakBlock(a[4],a[3])  
  277.                     drawMapBlock(a[4],a[3])  
  278.         end
  279.      elseif(a[1] == "mouse_scroll" and not inSave )then  
  280.         if(a[2] == 1 and selBlock < #blocks)then
  281.             selBlock = selBlock + 1
  282.         elseif(a[2] == -1 and selBlock > 1) then
  283.             selBlock = selBlock - 1
  284.         end
  285.     elseif(a[1] == "key")then
  286.                 if(a[2] == keys.enter and inSave)then sMenu[sMenuIndex].run() end
  287.               if(a[2] == 29)then inSave = not inSave end
  288.                 if(a[2] == keys.left and sMenuIndex > 1  and inSave)then
  289.                      sMenuIndex = sMenuIndex - 1
  290.                elseif(a[2] == keys.right and sMenuIndex < #sMenu  and inSave)then
  291.                      sMenuIndex = sMenuIndex + 1    
  292.                 end
  293.     end
  294. end
  295.  
  296. while inGame do
  297.     update()    
  298. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement