Advertisement
GnoX

form

Sep 21st, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.05 KB | None | 0 0
  1. os.unloadAPI("gxm")
  2. os.loadAPI("gxm")
  3.  
  4. local tArgs = {...}
  5. formType = tArgs[1]
  6.  
  7. local materialSlot = 16
  8. local materialSensitive = true
  9.  
  10. gxm.relative()
  11.  
  12. form = {}
  13. form.goStart = true
  14. form.chainList = {}
  15.  
  16. local printTable = function(tab)
  17.     for k, v in pairs(tab) do
  18.         if type(v) == "table" then
  19.             for p, o in pairs(v) do
  20.                 printString = type(p) == "string" and p.." - "..tostring(o) or o
  21.                 write(printString.." ")
  22.             end
  23.             print()
  24.         else
  25.             printString = type(k) == "string" and k.." - "..tostring(v) or v
  26.             print(printString)
  27.         end
  28.     end
  29. end
  30.  
  31. local isTable = function(tab)
  32.     return type(tab) == "table"
  33. end
  34.  
  35. local giveWarning = function(warning)
  36.     print(warning)
  37.     print("Exit      - e")
  38.     print("Show grid - g")
  39.     print("Continue  - any key.")
  40.     while true do
  41.         e, pressed = os.pullEvent()
  42.         if pressed == keys.e then
  43.             return false
  44.         elseif pressed == keys.g then
  45.             return "grid"
  46.         else
  47.             return true
  48.         end
  49.     end
  50. end
  51.  
  52. local fillPatternTable = function(x, y, tableType, ...)
  53.     local patternTable = {["warning"] = false}
  54.     local function fillTable(num)
  55.         for i = 1, y do
  56.             patternTable[i] = {}
  57.             for j = 1, x do
  58.                 patternTable[i][j] = num
  59.             end
  60.         end
  61.     end
  62.     if not tableType then
  63.         fillTable(16)
  64.     elseif tableType == "outlined" then
  65.         fillTable(16)
  66.         for i = 1, y do
  67.             for j = 1, x do
  68.                 if j == 1 or j == x or i == 1 or i == y then
  69.                     patternTable[i][j] = 15
  70.                 end
  71.             end
  72.         end
  73.     elseif tableType == "grid" then
  74.         xOffset = arg[1]
  75.         yOffset = arg[2]
  76.         if     (x-1) % xOffset ~= 0 then patternTable.warning = "Correction: -"..(x-1)%xOffset.." points in x axis."
  77.         elseif (y-1) % yOffset ~= 0 then patternTable.warning = "Correction: -"..(y-1)%yOffset.." points in y axis." end
  78.         fillTable(16)
  79.         for i = 1, y do
  80.             for j = 1, x, xOffset do
  81.                 patternTable[i][j] = 15
  82.             end
  83.         end
  84.         for i = 1, y, yOffset do
  85.             for j = 1, x do
  86.                 patternTable[i][j] = 15
  87.             end
  88.         end
  89.     end
  90.     return patternTable
  91. end
  92.  
  93. form.goToStart = function(bool)
  94.     form.goStart = bool
  95. end
  96.  
  97.  
  98.  
  99. form.calculateFuel = function(pattern)
  100.     local fuelRequired = 0
  101.     pattern = pattern and pattern or form.pattern
  102.     for c in string.gmatch(form.pattern, "[0-9]*[fbud]") do
  103.         c = tonumber(c:match("[0-9]*"))
  104.         fuelRequired = fuelRequired + (c or 1)
  105.     end
  106.     return fuelRequired
  107. end
  108.  
  109. form.calculateBlocks = function(pattern)
  110.     pattern = pattern and pattern or form.pattern
  111.     local requiredBlocks = 0
  112.     for _ in pattern:gmatch("[,;p-_^]") do
  113.         requiredBlocks = requiredBlocks + 1
  114.     end
  115.     return requiredBlocks
  116. end
  117.  
  118. form.calculateCost = function(pattern)
  119.     return {["fuel"] = form.calculateFuel(pattern), ["blocks"] = form.calculateBlocks(pattern)}
  120. end
  121.  
  122. -- Pattern generation --
  123.  
  124. form.plane = function(xLen, yLen, outlined, horizontal, side, pattern)
  125.     local alt = true
  126.     local material =
  127.         outlined and fillPatternTable(xLen, yLen, "outlined")
  128.         or (type(pattern) == "table" and pattern.type == "uniformGrid") and fillPatternTable(xLen, yLen, "grid", pattern.offset, pattern.offset)
  129.         or (type(pattern) == "table" and pattern.type == "grid") and fillPatternTable(xLen, yLen, pattern.type, pattern.xOffset, pattern.yOffset)
  130.         or fillPatternTable(xLen, yLen)
  131.    
  132.     if material.warning then
  133.         action = giveWarning(material.warning)
  134.         if action == "grid" then
  135.             printTable(material)
  136.         elseif not action then
  137.             return
  138.         end
  139.     end
  140.     form.pattern =
  141.         horizontal == "top" and "d"
  142.         or not horizontal and "f"
  143.         or "u"
  144.  
  145.     yLen = yLen or 1
  146.  
  147.     for y = 1, yLen do
  148.         form.pattern =
  149.             (not horizontal and side=="top") and form.pattern .. "d"..material[1][1].."p"
  150.             or not horizontal and form.pattern .. "u"..material[1][1]..","
  151.             or (y == 1 and horizontal ~= "top") and form.pattern.."f"..material[1][1]..","
  152.             or (y == 1 and horizontal == "top") and form.pattern.."f"..material[1][1].."p"
  153.             or (horizontal == "top" and side == "left") and form.pattern .. "lfrp"
  154.             or horizontal == "top" and form.pattern .. "rflp"
  155.             or (horizontal and side == "left") and form.pattern .. "lfr,"
  156.             or horizontal and form.pattern .. "rfl,"
  157.  
  158.         for x = 1, xLen-1 do
  159.             form.pattern =
  160.                    ((alt and side == "top") or (alt and horizontal == "top")) and form.pattern.."f"..material[y][x+1].."p"
  161.                 or (not alt and side == "top" or horizontal == "top") and form.pattern.."b"..material[y][x+1].."p"
  162.                 or alt and form.pattern.."f"..material[y][x+1]..","
  163.                 or form.pattern.."b"..material[y][x+1]..","
  164.         end
  165.         alt = not alt
  166.     end
  167.  
  168.     if form.goStart then
  169.         form.pattern =
  170.             (not horizontal and yLen % 2 == 0 and not side == "top") and form.pattern .. "b" .. tostring(yLen) .. "d"
  171.             or (not horizontal and side ~= "top") and form.pattern .. tostring(xLen) .. "b" .. tostring(yLen) .. "d"
  172.             or (side == "left" and yLen % 2 == 0 and horizontal ~= "top") and form.pattern .. "br" .. tostring(yLen-1) .. "fld"
  173.             or (side == "left" and horizontal ~= "top") and form.pattern .. tostring(xLen) .. "br" .. tostring(yLen-1) .. "fld"
  174.             or (side == "left" and horizontal == "top" and yLen % 2 == 0) and form.pattern .. "br" .. tostring(yLen-1) .. "flu"
  175.             or (side == "left" and horizontal == "top") and form.pattern .. tostring(xLen) .. "br" .. tostring(yLen-1) .. "flu"
  176.             or (horizontal == "top" and yLen %2 == 0) and form.pattern .. "bl" .. tostring(yLen-1) .. "fru"
  177.             or horizontal == "top" and form.pattern .. tostring(xLen) .. "bl" .. tostring(yLen-1) .. "fru"
  178.             or (side == "top" and yLen % 2 == 0) and form.pattern .."b" .. tostring(yLen).."u"
  179.             or side == "top" and form.pattern ..tostring(xLen) .."b" .. tostring(yLen).."u"
  180.             or yLen % 2 == 0 and form.pattern .. "bl" .. tostring(yLen-1) .. "frd"
  181.             or form.pattern .. tostring(xLen) .. "bl" .. tostring(yLen-1) .. "frd"
  182.     end
  183.  
  184.     return form.pattern
  185. end
  186.  
  187. form.line = function(length)
  188.     return form.plane(length, 1)
  189. end
  190.  
  191. form.horizontalLine = function(length)
  192.     return form.plane(1, length)
  193. end
  194.  
  195. form.wall = function(length, height, side, outline)
  196.     outline = type(side) == "boolean" and side or outline
  197.     return form.plane(length, height, outline, false, side)
  198. end
  199.  
  200. form.gridWall = function(length, height, xGridOffset, yGridOffset, side)
  201.     if type(yGridOffset == "string") then
  202.         side = yGridOffset
  203.         yGridOffset = xGridOffset
  204.     elseif not yGridOffset then
  205.         yGridOffset = xGridOffset
  206.     end
  207.     return form.plane(length, height, false, false, side, {["type"] = "grid", ["xOffset"] = xGridOffset, ["yOffset"] = yGridOffset})
  208. end
  209.  
  210. form.floor = function(length, width, side, outlined)
  211.     outlined = type(side) == "boolean" and side or outlined
  212.     return form.plane(length, width, outlined, true, side or "right")
  213. end
  214.  
  215. form.gridFloor = function(length, width, xGridOffset, yGridOffset)
  216.     yGridOffset = yGridOffset or xGridOffset
  217.     return form.plane(length, width, false, true, false, {["type"] = "grid", ["xOffset"] = xGridOffset, ["yOffset"] = yGridOffset})
  218. end
  219.  
  220. form.roof = function(length, width, side, outlined)
  221.     outlined = type(side) == "boolean" and side or outlined
  222.     return form.plane(length, width, outlined, "top", side)
  223. end
  224.  
  225. form.outlinedRoof = function(length, width, side)
  226.     return form.plane(length, width, true, "top", side)
  227. end
  228.  
  229. form.gridRoof = function(length, width, side, xGridOffset, yGridOffset)
  230.     return form.plane(length, width, false, side, false, {["type"] = "grid", ["xOffset"] = xGridOffset, ["yOffset"] = yGrodPffset})
  231. end
  232.  
  233.  
  234. form.tunnel = function(length, xSize, ySize, outlined)
  235.     outlined = type(ySize) == "boolean" and ySize or outlined
  236.     ySize    = type(ySize) == "boolean" and xSize or ySize
  237.  
  238.     form.goToStart(false)
  239.     return
  240.            (xSize % 2 == 0 and ySize % 2 == 0) and form.floor(length, xSize, outlined) .. "brfl" .. form.wall(length, ySize, outlined) .. "blfr" .. form.roof(length, xSize, "left", outlined) .. "blfr" .. form.wall(length, ySize, "top", outlined)
  241.         or (xSize % 2 == 0 and ySize % 2 ~= 0) and form.floor(length, xSize, outlined) .. "brfl" .. form.wall(length, ySize, outlined) .. "flfl" .. form.roof(length, xSize, outlined) .. "brfl" .. form.wall(length, ySize, "top", outlined)
  242.         or (xSize % 2 ~= 0 and ySize % 2 == 0) and form.floor(length, xSize, outlined) .. "frfr" .. form.wall(length, ySize, outlined) .. "brfl" .. form.roof(length, xSize, outlined) .. "frfr" .. form.wall(length, ySize, "top", outlined)
  243.         or                                         form.floor(length, xSize, outlined) .. "frfr" .. form.wall(length, xSize, outlined) .. "frfr" .. form.roof(length, xSize, "left", outlined) .. "flfl" .. form.wall(length, ySize, "top", outlined)
  244. end
  245.  
  246. form.addToChain = function(...)
  247.     print(...)
  248. end
  249.  
  250. setMenu = function(x, y)
  251.     local field = {}
  252.     local x, y = 12, 4
  253.     local lastc = " "
  254.     local function clearLast()
  255.         term.setCursorPos(x, y)
  256.         term.write(" ")
  257.         term.setCursorPos(x+3, y)
  258.         term.write(" ")
  259.     end
  260.     w, h = term.getSize()
  261.     term.clear()
  262.     term.setCursorPos(x, y)
  263.     term.write("[")
  264.     term.setCursorPos(x+3, y)
  265.     term.write("]")
  266.  
  267.     while true do
  268.         for x, tab in pairs(field) do
  269.             for y, v in pairs(tab) do
  270.                 term.setCursorPos(x+1, y)
  271.                 term.write(tostring(v))
  272.             end
  273.         end
  274.         e, key = os.pullEvent()
  275.         clearLast()
  276.  
  277.         if key == keys.down then
  278.             y = y + 1
  279.         elseif key == keys.up then
  280.             y = y - 1
  281.         elseif key == keys.right then
  282.             x = x + 3
  283.         elseif key == keys.left then
  284.             x = x - 3
  285.         elseif key == keys.enter then
  286.             term.setCursorPos(x+1, y)
  287.             term.write("  ")
  288.             term.setCursorPos(x+1, y)
  289.             input = read()
  290.             if not field[x] then field[x] = {} end
  291.             field[x][y] = tonumber(input)
  292.         end
  293.  
  294.         if x < 6 then x = 6 end
  295.         if y < 4 then y = 4 end
  296.         if x > 24 then x = 24 end
  297.         if y > 8 then y = 8 end
  298.         term.setCursorPos(x, y)
  299.         term.write("[")
  300.         term.setCursorPos(x+3, y)
  301.         term.write("]")
  302.     end
  303. end
  304.  
  305.  
  306. gxm.exec(form.horizontalLine(5))
  307.  
  308. --form.gridWall(9, 9, 4, 5)
  309. --printTable(fillPatternTable(12, 3, "grid", 2, 2))
  310. --form.gridWall(9, 9, 4)
  311. --printTable(form.calculateCost(form.gridFloor(9, 9, 4)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement