ferreusveritas

runChamber

Sep 11th, 2020 (edited)
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.92 KB | None | 0 0
  1. os.loadAPI("build");
  2.  
  3. --API Aliases
  4.  
  5. function dirMap(turns) return build.dirMap(turns); end
  6. function fif(cond, a, b) return build.fif(cond, a, b); end
  7.  
  8. dofile("config");
  9. dofile("blocks");
  10.  
  11. local D, U, N, S, W, E = dirMap(0);
  12.  
  13. local compy = build.Coord:get();
  14. local center = compy:off(N, 8 + 3):up();
  15. local lockPos = compy:off(N):up();
  16.  
  17. grow = false;
  18. local radius = 8;
  19. count = codesPerRad;
  20.  
  21. local currModIdx = 1;
  22. local currSpeciesIdx = 1;
  23.  
  24. local arrowL = string.char(17);
  25. local arrowR = string.char(16);
  26.  
  27. monitor = peripheral.find("monitor");
  28. monitor.setBackgroundColor(colors.black);
  29. monitor.clear();
  30.  
  31. mainTimer = 0;
  32.  
  33. function startTimer()
  34.   mainTimer = os.startTimer(0.5);
  35. end
  36.  
  37. function stopTimer()
  38.   os.cancelTimer(mainTimer);
  39. end
  40.  
  41. function waitOn(func)
  42.   parallel.waitForAll(func);
  43. end
  44.  
  45. function pad(str, len, chr)
  46.   chr = chr or " ";
  47.   local miss = len - string.len(str);
  48.   if miss > 0 then
  49.     str = str .. string.rep(chr, miss);
  50.   end
  51.   if miss < 0 then
  52.     str = string.sub(str, 1, len);
  53.   end
  54.   return str;
  55. end
  56.  
  57. function padNum(num, len, chr)
  58.   chr = chr or " ";
  59.   local str = tonumber(num);
  60.   local miss = len - string.len(str);
  61.   if miss > 0 then
  62.     str = string.rep(chr, miss) .. str;
  63.   end
  64.   return str;
  65. end
  66.  
  67. function sound(snd)
  68.   commands.playsound(snd, "block", "@a", compy.x, compy.y, compy.z);
  69. end
  70.  
  71. function clicksound()
  72.   sound("block.stone_button.click_on");
  73. end
  74.  
  75. function getKeys(tab)
  76.   local keyset={}
  77.   for k,v in pairs(tab) do
  78.     keyset[#keyset+1]=k
  79.   end
  80.   return keyset;
  81. end
  82.  
  83. function getSpeciesList()
  84.   local _, list = commands.dt("specieslist")
  85.   return list;
  86. end
  87.  
  88. local trees = {};
  89. for k, v in pairs(getSpeciesList()) do
  90.   local mod, species = string.match(v, "([^,]+):([^,]+)");
  91.   if trees[mod] == nil then
  92.     trees[mod] = {};
  93.   end
  94.   local s = trees[mod];
  95.   s[#s+1]=species;
  96. end
  97.  
  98. local mods = getKeys(trees);
  99. table.sort(mods);
  100.  
  101. for k, v in pairs(trees) do
  102.   table.sort(v);
  103. end
  104.  
  105.  
  106. ----------------------------------------------------------------
  107. -- BUTTONS                                                    --
  108. ----------------------------------------------------------------
  109.  
  110. Button = {}
  111. Button.__index = Button;
  112.  
  113. function Button:new(x, y, w, text, color)
  114.   color = color or colors.black;
  115.   local button = {};             -- our new object
  116.   setmetatable(button, Button);  -- make handle lookup
  117.   button.coords = build.Coord:new(x, y, 0);
  118.   button.width = w;
  119.   button.text = text;
  120.   button.color = color;
  121.   button.monitor = monitor;
  122.   return button;
  123. end
  124.  
  125. function Button:setMonitor(m)
  126.   self.monitor = m;
  127.   return self;
  128. end
  129.  
  130. function Button:draw()
  131.   mon = self.monitor;
  132.   mon.setCursorPos(self.coords.x, self.coords.y);
  133.   --mon.clearLine();
  134.   mon.setBackgroundColor(self.color);
  135.   --mon.clearLine();
  136.   mon.write(pad(self.text, self.width, " "));
  137.   return self;
  138. end
  139.  
  140. function Button:setText(text)
  141.   if text ~= self.text then
  142.     self.text = text;
  143.     self:draw();
  144.   end
  145.   return self;
  146. end
  147.  
  148. function Button:setColor(color)
  149.   self.color = color;
  150.   self:draw();
  151.   return self;
  152. end
  153.  
  154. function Button:click(x, y)
  155.   if self:isInside(x, y) then
  156.     if self.action ~= nil then
  157.       clicksound();
  158.       self.action(self, x, y);
  159.     end
  160.   end
  161. end
  162.  
  163. function Button:isInside(x, y)
  164.   return y == self.coords.y and x >= self.coords.x and x < self.coords.x + self.width;
  165. end
  166.  
  167. function Button:setAction(action)
  168.   self.action = action;
  169.   return self;
  170. end
  171.  
  172. function open() waitOn(
  173.   function ()
  174.     sound("block.piston.extend");
  175.     if radius <= 8 and radius >= 2 then
  176.       center:up():cylinder(air, radius, height);
  177.     end
  178.     build.waitAsync();
  179.   end);
  180. end
  181.  
  182. function close() waitOn(
  183.   function ()
  184.     sound("block.piston.contract");
  185.     if radius <= 8 and radius >= 2 then
  186.       center:up():cylinder(wallBlock, radius, height);
  187.     end
  188.     build.waitAsync();
  189.   end);
  190. end
  191.  
  192. function kill() waitOn(
  193.   function ()
  194.     commands.dt("killtree", center.x, center.y, center.z);
  195.     center:up():cub():erase();--In the case of a sapling
  196.     center:cub():fill(dirt); --Place dirt over the rooty soil just in case
  197.     commands.kill("@e[type=item]"); --Gets rid of dropped seeds
  198.   end);
  199. end
  200.  
  201. function plant()
  202.   commands.dt("setcoordxor", math.random(0, 65535));
  203.   commands.dt("settree", center.x, center.y, center.z, tree, "P"); --synchronous
  204.   setSoilLife(15); --synchronous
  205. end
  206.  
  207. function purge() waitOn(
  208.   function ()
  209.     center:up():cub():grohor(radius):gro(U, height):erase();
  210.     build.waitAsync();
  211.   end);
  212. end
  213.  
  214. function push()
  215.   if file_exists(treeFilename()) then
  216.     print("pastebin: " .. treeFilename());
  217.     shell.run("pastebin", "put", treeFilename());
  218.   else
  219.     print("error: " .. treeFilename() .. " not found");
  220.   end
  221. end
  222.  
  223. function setRadius(rad)
  224.   open();
  225.  
  226.   if rad >= 8 then
  227.     rad = 8;
  228.   elseif rad <= 2 then
  229.     rad = 2;
  230.   end
  231.  
  232.   if rad ~= radius then
  233.     radius = rad;
  234.     count = codesPerRad;
  235.     countButton:setCount(count);
  236.     radiusButton:setRadius(radius);
  237.   end
  238.  
  239. end
  240.  
  241. function radiusInc()
  242.   setRadius(radius + 1);
  243. end
  244.  
  245. function radiusDec()
  246.   setRadius(radius - 1);
  247. end
  248.  
  249.  
  250. function treeFilename()
  251.   local mod = mods[currModIdx];
  252.   local spc = trees[mod][currSpeciesIdx];
  253.   return mod .. "-" .. spc;
  254. end
  255.  
  256.  
  257. openButton   = Button:new(2, 2, 7, "[OPEN ]", colors.purple):setAction( open );
  258. closeButton  = Button:new(2, 3, 7, "[CLOSE]", colors.blue):setAction( close );
  259. plantButton  = Button:new(2, 4, 7, "[PLANT]", colors.lime):setAction( plant );
  260. killButton   = Button:new(2, 5, 7, "[KILL ]", colors.orange):setAction( function() kill(); grow = false; end );
  261. purgeButton  = Button:new(2, 6, 7, "[PURGE]", colors.brown):setAction( purge );
  262. modButton    = Button:new(2, 8, 27, "       ", colors.gray);
  263. treeButton   = Button:new(2, 9, 27, "      ", colors.gray);
  264. radiusButton = Button:new(12, 2, 7, "       ", colors.gray);
  265. growButton   = Button:new(12, 3, 7, "[START]", colors.green);
  266. soilButton   = Button:new(12, 4, 11, "       ", colors.gray);
  267. countButton  = Button:new(12, 5, 11, "       ", colors.gray);
  268. pushButton  = Button:new(12, 6, 7, "[PUSH ]", colors.cyan):setAction( push );
  269.  
  270. growButton.stop = function(b) b:setColor(colors.green); b:setText("[START]"); grow = false; end
  271. growButton.start = function(b) b:setColor(colors.red); b:setText("[STOP ]") grow = true; close(); end
  272. growButton:setAction( function(b) if grow then b:stop(); else b:start(); end; end);
  273.  
  274. countButton.setCount = function(b, c) b:setText("REMAIN:" .. padNum(c, 4)); end;
  275. countButton:setAction( function(b) count = codesPerRad; b:setCount(count); end );
  276. countButton:setCount(count);
  277.  
  278. --Radius Button Setup
  279. radiusButton.setRadius = function(b, r) b:setText(arrowL .. arrowR .. " " .. "R:" .. r); end;
  280. radiusButton.click =
  281.   function(b, x, y)
  282.     if b:isInside(x, y) then
  283.       x = x - b.coords.x + 1;
  284.       if x == 1 then
  285.         clicksound();
  286.         radiusDec();
  287.       elseif x == 2 then
  288.         clicksound();
  289.         radiusInc();
  290.       end
  291.     end
  292.   end;
  293. radiusButton:setRadius(radius);
  294.  
  295. function updateTree()
  296.   local mod = mods[currModIdx];
  297.   local spc = trees[mod][currSpeciesIdx];
  298.   local newTree = mod .. ":" .. spc;
  299.   if newTree ~= tree then
  300.     tree = newTree;
  301.     kill();
  302.   end
  303. end
  304.  
  305. --Tree Button Setup
  306.  
  307. treeButton.setIdx =
  308.   function(b, idx)
  309.     local arr = trees[mods[currModIdx]];
  310.     idx = ((idx - 1) % #arr) + 1;
  311.     b:setText(arrowL .. arrowR .. " " .. arr[idx]);
  312.     currSpeciesIdx = idx;
  313.     updateTree();
  314.   end
  315.  
  316. treeButton.click =
  317.   function(b, x, y)
  318.     if b:isInside(x, y) then
  319.       x = x - b.coords.x + 1;
  320.       if x == 1 then
  321.         clicksound();
  322.         b:setIdx(currSpeciesIdx - 1);
  323.       elseif x == 2 then
  324.         clicksound();
  325.         b:setIdx(currSpeciesIdx + 1);
  326.       end
  327.     end
  328.   end;
  329. treeButton:setIdx(1);
  330.  
  331.  
  332. --Mod Button Setup
  333.  
  334. modButton.setIdx =
  335.   function(b, idx)
  336.     local arr = mods;
  337.     idx = ((idx - 1) % #arr) + 1;
  338.     b:setText(arrowL .. arrowR .. " " .. arr[idx]);
  339.     currModIdx = idx;
  340.     treeButton:setIdx(1);
  341.   end
  342.  
  343. modButton.click =
  344.   function(b, x, y)
  345.     if b:isInside(x, y) then
  346.       x = x - b.coords.x + 1;
  347.       if x == 1 then
  348.         clicksound();
  349.         b:setIdx(currModIdx - 1);
  350.       elseif x == 2 then
  351.         clicksound();
  352.         b:setIdx(currModIdx + 1);
  353.       end
  354.     end
  355.   end;
  356. modButton:setIdx(1);
  357.  
  358.  
  359. --Soillife Button Setup
  360.  
  361. soilButton.setSoilLife =
  362.   function(b, life)
  363.     local text;
  364.     if life >= 0 then
  365.       text = padNum(life, 2, "0") .. "/15";
  366.     else
  367.       text = "--/--";
  368.     end
  369.     soilButton:setText("SOIL: " .. text);
  370.   end
  371.  
  372.  
  373. --All Buttons
  374. allButtons = { openButton, closeButton, growButton, plantButton, killButton, purgeButton, countButton, modButton, treeButton, radiusButton, soilButton, pushButton };
  375.  
  376. function drawScreen()
  377.   for i, v in ipairs(allButtons) do
  378.     v:draw();
  379.   end
  380. end
  381.  
  382. function clickScreen(x, y)
  383.   for i, v in ipairs(allButtons) do
  384.     v:click(x, y);
  385.   end
  386. end
  387.  
  388. drawScreen();
  389.  
  390. indicator = false;
  391.  
  392. function clearVines()
  393.   --Do nothing for now
  394. end
  395.  
  396. function getSoilLife()
  397.   pass, val = commands.dt("soillife", center.x, center.y, center.z);
  398.   if(pass) then
  399.     return tonumber(val[1]);
  400.   end
  401.   return -1;
  402. end
  403.  
  404. function setSoilLife(life)
  405.   pass, val = commands.dt("soillife", center.x, center.y, center.z, life);
  406. end
  407.  
  408. function getCode()
  409.   pass, val = commands.dt("gettree", center.x, center.y, center.z);
  410.   if(pass) then
  411.     return val[2];
  412.   end
  413.   return nil;
  414. end
  415.  
  416. function storeCodeHttp(tree, code, radius)
  417.   code = string.gsub(code, "+", "%%2b");
  418.   http.request("http://127.0.0.1/trees/trees.php?".. "tree="..tree.."&code="..code.."&radius="..radius, nil);  
  419. end
  420.  
  421. function storeCodeConsole(tree, code, radius)
  422.   print(tree);
  423.   print(radius .. ":" .. code);
  424.   print("----------------------");
  425. end
  426.  
  427. function storeCodeFile(tree, code, radius)
  428.   local s = fs.open(treeFilename(), "a");
  429.   s.write(radius .. ":" .. code .. "\n");
  430.   s.close();
  431. end
  432.  
  433. function storeCode(tree, code, radius)
  434.   --storeCodeHttp(tree, code, radius);
  435.   storeCodeConsole(tree, code, radius);
  436.   storeCodeFile(tree, code, radius);
  437. end
  438.  
  439. function harvest()
  440.   code = getCode();
  441.   kill();
  442.   sound("entity.item.pickup");
  443.  
  444.   local codes = {};
  445.   codes[1] = code;
  446.  
  447.   for i = 1,3 do
  448.     local _, c = commands.dt("rotatejocode", code, i);
  449.     codes[i + 1] = c[1];
  450.   end
  451.  
  452.   table.sort(codes);
  453.   code = codes[1];
  454.  
  455.   storeCode(tree, code, radius);
  456. end
  457.  
  458. function file_exists(name)
  459.    local f=io.open(name,"r")
  460.    if f~=nil then io.close(f) return true else return false end
  461. end
  462.  
  463. function finishHarvest()
  464.   growButton:stop();
  465. end
  466.  
  467. function onTimerEvent()
  468.   local soilLife = getSoilLife();
  469.   soilButton:setSoilLife(soilLife);
  470.   if(soilLife <= 0 and grow == true) then
  471.  
  472.     if(soilLife == 0) then
  473.       harvest();
  474.       count = count - 1;
  475.       if count <= 0 then
  476.         if radius > 2 then
  477.           radiusDec();
  478.           close();
  479.         else
  480.           finishHarvest(); --We're done
  481.         end
  482.       end
  483.       countButton:setCount(count);
  484.     end
  485.  
  486.     if grow then
  487.       plant();
  488.     end
  489.  
  490.   end
  491. end
  492.  
  493. spinner = 0;
  494. spinnerSet = { 129, 130, 136, 259, 144, 132 };
  495.  
  496. function parallelTimer()
  497.   startTimer();
  498.   while true do
  499.     event = os.pullEvent("timer");
  500.     onTimerEvent();
  501.     monitor.setCursorPos(28, 2);
  502.     spinner = (spinner + 1) % 6;
  503.     s = spinnerSet[spinner + 1];
  504.     if s > 200 then
  505.       s = s - 100;
  506.       monitor.setBackgroundColor(colors.orange);
  507.       monitor.setTextColor(colors.gray);
  508.     else
  509.       monitor.setBackgroundColor(colors.gray);
  510.       monitor.setTextColor(colors.orange);
  511.     end
  512.     monitor.write(string.char(s));
  513.     monitor.setTextColor(colors.white);
  514.     startTimer();
  515.   end
  516. end
  517.  
  518. function parallelClick()
  519.   while true do
  520.     event, par1, xPos, yPos = os.pullEvent("monitor_touch");
  521.     clickScreen(xPos, yPos);
  522.   end
  523. end
  524.  
  525. purge();
  526. close();
  527. parallel.waitForAll(parallelTimer, parallelClick);
Add Comment
Please, Sign In to add comment