Advertisement
LDDestroier

ComputerCraft D20

Sep 7th, 2016
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- ComputerCraft D20 (or whatever-sided however-many dice)
  2. -- pastebin get ZYKQkUPB die
  3.  
  4. local dx, dy = 3, 3
  5. local scr_x, scr_y = term.getSize()
  6. local vantage = 0
  7. local useQuips = false
  8.  
  9. local dices = 1
  10.  
  11. local range = {1,20} -- determines die size, basically random range
  12.  
  13. local dice = {{0,512,512,8,8,512,512,0},{512,512,8,8,8,8,512,512},{2048,8,8,8,8,8,8,2048},{2048,512,8,8,8,8,512,2048},{0,2048,512,512,512,512,2048,0},{0,0,0,512,512,0,0,0}}
  14. local nums = { --numnumnum...
  15.     [0] = {{1,1,1},{1,0,1},{1,0,1},{1,0,1},{1,1,1}},
  16.     [1] = {{1,1},{0,1},{0,1},{0,1},{1,1,1}},
  17.     [2] = {{1,1},{0,0,1},{0,1},{1},{1,1,1}},
  18.     [3] = {{1,1},{0,0,1},{1,1},{0,0,1},{1,1,1}},
  19.     [4] = {{1,0,1},{1,0,1},{1,1,1},{0,0,1},{0,0,1}},
  20.     [5] = {{1,1,1},{1},{1,1,1},{0,0,1},{1,1,1}},
  21.     [6] = {{1,1,1},{1},{1,1,1},{1,0,1},{1,1,1}},
  22.     [7] = {{1,1,1},{0,0,1},{0,1},{0,1},{0,1}},
  23.     [8] = {{1,1,1},{1,0,1},{0,1},{1,0,1},{1,1,1}},
  24.     [9] = {{1,1,1},{1,0,1},{1,1,1},{0,0,1},{0,0,1}},
  25. }
  26. local quips = { --witty quips for your die!
  27.     [1] = {"Rocks fall, everybody dies","You've ran out of dumb luck.","Goddamn it.","Hope you have a tampon for that stab wound.","Your dice hate you.","You miss  and chop off your arm by mistake.","YOU HAVE NO CHANCE TO SURVIVE MAKE YOUR TIME .","HA HA HA HA ..."},
  28.     [2] = {"Man, this sucks.","Let's hope that wasn't a dodge roll.","One is the loneliest number...","Stupid prime numbers.","That's as much as two ones. And that's terrible.","What is this, a roll for ants??"},
  29.     [3] = {"Eh, this, uh, isn't good.","I hope that was a 'try to die' roll.","Honestly, two can be as bad as one.","Stupid primes."},
  30.     [4] = {"TWO TIMES FOUR IS HALF OF SIXTEEN","You're a fifth there...!","Bad luck to all chinese players!"},
  31.     [5] = {"You're a quarter there!","You are caught between vier and sechs.","Not a-good, no!","Stupid primes."},
  32.     [6] = {"Your accuracy stat better be great.","Not as bad as it could've been...","Hahaha six sounds like sex hahaha."},
  33.     [7] = {"A magical number...and it's mediocre.","I hate seven! Annoying to do math with.","Stupid primes."},
  34.     [8] = {"gr8 m8 i r8 8/8","Eight. Eight. Eight.","Not a two, but...","It's a power of two, but..."},
  35.     [9] = {"NEIN NEIN NEIN NEIN","You're getting there.","Your sword grazes the enemy!","Your sword glazes the enemy!"},
  36.     [10] = {"Ooohhh, you're halfway there...","Four tens, and you've stolen a lot of cake.","...passable."},
  37.     [11] = {"HEY IT'S A PALINDROME","Eleven is up there with 7 in terms of math.","Stupid primes."},
  38.     [12] = {"Twelve...now THAT'S a number not to trifle with.","That's a solid number.","Good enought, I guess."},
  39.     [13] = {"Stupid primes.","I'd call you unlucky, but this isn't a bad throw.","Amount of nobodies in Org. XIII"},
  40.     [14] = {"What's worse than a seven? TWO sevens!!","Okay, now you're dealin' some damage","Statistically better than a 13."},
  41.     [15] = {"Three quarters there!","You done pretty good for yourself!","Your sword stabs the enemy! YOU WIN!"},
  42.     [16] = {"TWO TIMES FOUR IS HALF OF SIXTEEN","A nice, even power of two!","Fuck yes!","Hey, that's my age...","No cooler a number...besides "..range[2]},
  43.     [17] = {"Goddamn primes!","Your arrow hits the orc from afar!","Hey-you're a lucky man!","Great scott!"},
  44.     [18] = {"Your cleaver hits the ant, it drops 40 GOLD.","You perceive a small crack 600 feet away.","yay"},
  45.     [19] = {"That's really good!","Don't despair! You've no need, honestly.","Noice.","Aren't you glad you're using CC?","Stupid primes."},
  46.     [20] = {"You're a natural! (twenty)","Damn right!!","Nice...throw!","All your dreams...realized!","SMAAAAAAASH!!","You shot an ant from a mile away!","Fuck yeah!!","Eat shit, monster!!"},
  47. }
  48.  
  49. local cwrite = function(txt,x,y)
  50.     local _x,_y = term.getCursorPos()
  51.     term.setCursorPos((x or math.ceil(scr_x/2))-(#txt/2),y or _y)
  52.     write(txt)
  53. end
  54.  
  55. local choice = function(input,verbose)
  56.     if not input then
  57.         input = "yn"
  58.     end
  59.     if verbose then
  60.         write("[")
  61.         for a = 1, #input do
  62.             write(input:sub(a,a):upper())
  63.             if a < #input then
  64.                 write(",")
  65.             end
  66.         end
  67.         write("]?")
  68.     end
  69.     local evt,char
  70.     repeat
  71.         evt,char = os.pullEvent("char")
  72.     until string.find(input:lower(),char:lower())
  73.     if verbose then
  74.         print(char:upper())
  75.     end
  76.     local pos = string.find(input:lower(),char:lower())
  77.     return pos, char:lower()
  78. end
  79.  
  80. local tc = function(value)
  81.     if term.current().setVisible then
  82.         term.current().setVisible(value)
  83.     end
  84. end
  85.  
  86. local render = function(x,y,number)
  87.     tc(false)
  88.     for a = 1, scr_y do
  89.         term.setCursorPos(1,a)
  90.         if a > scr_y/2 then
  91.             term.setBackgroundColor(colors.brown)
  92.         else
  93.             term.setBackgroundColor(colors.yellow)
  94.         end
  95.         term.clearLine()
  96.     end
  97.     paintutils.drawImage(dice,x-dx,y-(dy-1))
  98.     term.setTextColor(colors.black)
  99.     term.setBackgroundColor(colors.lightBlue)
  100.     cwrite(tostring(number),x+1,y)
  101.     term.setCursorPos(1,scr_y-3)
  102.     term.setBackgroundColor(colors.brown)
  103.     term.setTextColor(colors.white)
  104.     print("(1) "..range[2].." sides")
  105.     print("(2) "..dices.." dice")
  106.     write("(3) ")
  107.     if vantage == 0 then
  108.         write("Adds all dice")
  109.     elseif vantage == 1 then
  110.         write("Advantage")
  111.     elseif vantage == -1 then
  112.         write("Disadvantage")
  113.     end
  114.     tc(true)
  115. end
  116.  
  117. local displayNum = function(number)
  118.     local x,y = term.getCursorPos()
  119.     local len = 4
  120.     for a = 1, #tostring(number) do
  121.         local n = tonumber(tostring(number):sub(a,a))
  122.         if nums[n] then
  123.             paintutils.drawImage(nums[n],x,y)
  124.             x = x + len
  125.         end
  126.     end
  127. end
  128.  
  129. local flash = function(times)
  130.     local bg = term.getBackgroundColor()
  131.     local flashes = {
  132.         colors.black,
  133.         colors.gray,
  134.         colors.red,
  135.         colors.lightGray,
  136.         colors.white,
  137.         colors.red,
  138.         colors.gray,
  139.     }
  140.     for a = 1, times do
  141.         for b = 1, #flashes do
  142.             term.setBackgroundColor(flashes[b])
  143.             term.clear()
  144.             sleep(0)
  145.         end
  146.     end
  147.     term.setBackgroundColor(colors.black)
  148.     term.clear()
  149.     sleep(0)
  150.     term.setBackgroundColor(bg)
  151. end
  152.  
  153. --os.queueEvent("key", keys.space)
  154. render(scr_x/2,-6,"")
  155.  
  156. local lastnum
  157. local outcome
  158. while true do
  159.     local evt, key = os.pullEvent()
  160.     if (evt == "key" and (key == keys.space or key == keys.enter) or evt == "mouse_click") then
  161.         if vantage == 0 then
  162.             outcome = 0
  163.         else
  164.             outcome = {}
  165.         end
  166.         for d = 1, dices do
  167.             for a = -2, math.floor(scr_y/2) do
  168.                 lastnum = math.random(range[1],range[2])
  169.                 render(scr_x/2,a,lastnum)
  170.                 sleep(0)
  171.             end
  172.             lastnum = math.random(range[1],range[2])
  173.             if vantage == 0 then
  174.                 outcome = outcome + lastnum
  175.             else
  176.                 table.insert(outcome,lastnum)
  177.             end
  178.             render(scr_x/2,(scr_y/2)-1,lastnum)
  179.             term.setCursorPos(scr_x/2-2,scr_y/2+3)
  180.             term.setBackgroundColor(colors.brown)
  181.             write("*tck*")
  182.         end
  183.         sleep(0)
  184.         render(scr_x/2,(scr_y/2),lastnum)
  185.         if vantage == 1 then
  186.             outcome = math.max(unpack(outcome))
  187.         elseif vantage == -1 then
  188.             outcome = math.min(unpack(outcome))
  189.         end
  190.         if outcome == range[2]*dices then
  191.             flash(2)
  192.             render(scr_x/2,math.floor(scr_y/2),lastnum)
  193.         end
  194.         term.setBackgroundColor(colors.yellow)
  195.         if useQuips and (range[2] == 20) then
  196.             if quips[lastnum] then
  197.                 term.setTextColor(colors.black)
  198.                 cwrite(quips[lastnum][math.random(1,#quips[lastnum])],nil,2)
  199.             end
  200.         elseif range[2] == 6 then
  201.             if dices == 3 then
  202.                 term.setTextColor(colors.black)
  203.                 cwrite("For krist's sake...",nil,2)
  204.             end
  205.         end
  206.         term.setCursorPos((scr_x/2)-(#tostring(lastnum)*2)+2,scr_y-5)
  207.         displayNum(outcome)
  208.     elseif key == keys.q then
  209.         term.setBackgroundColor(colors.black)
  210.         term.clear()
  211.         term.setCursorPos(1,1)
  212.         sleep(0)
  213.         return lastnum
  214.     elseif key == keys.one then
  215.         render(scr_x/2,-6,"")
  216.         term.setCursorPos(1,1)
  217.         term.setTextColor(colors.black)
  218.         term.setBackgroundColor(colors.yellow)
  219.         write("# of sides? ")
  220.         sleep(0)
  221.         range[2] = tonumber(read()) or range[2]
  222.         render(scr_x/2,-6,"")
  223.         sleep(0)
  224.     elseif key == keys.two then
  225.         render(scr_x/2,-6,"")
  226.         term.setCursorPos(1,1)
  227.         term.setTextColor(colors.black)
  228.         term.setBackgroundColor(colors.yellow)
  229.         write("# of dice? ")
  230.         sleep(0)
  231.         dices = tonumber(read()) or dices
  232.         if dices <= 0 then
  233.             dices = 0
  234.         elseif dices >= 100 then
  235.             dices = 1
  236.         end
  237.         render(scr_x/2,-6,"")
  238.         sleep(0)
  239.     elseif key == keys.three then
  240.         render(scr_x/2,-6,"")
  241.         term.setCursorPos(1,1)
  242.         term.setTextColor(colors.black)
  243.         term.setBackgroundColor(colors.yellow)
  244.         print("(1) Advantage")
  245.         print("(2) Disadvantage")
  246.         print("(3) Add")
  247.         local utt
  248.         sleep(0)
  249.         repeat
  250.             uut = choice("123",false)
  251.         until uut >= 1 and uut <= 3
  252.         if uut == 1 then
  253.             vantage = 1
  254.         elseif uut == 2 then
  255.             vantage = -1
  256.         else
  257.             vantage = 0
  258.         end
  259.         render(scr_x/2,-6,"")
  260.         sleep(0.15)
  261.     end
  262. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement