Neui

labyrinth (for ComputerCraft)

Aug 22nd, 2015
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 38.68 KB | None | 0 0
  1. -- Made by Neui
  2.  
  3. local path = {} -- Current path taken
  4. local selection = 1 -- Current selection
  5. local current_text = ""
  6. local allow_back = true
  7. local default_back = true
  8. local allow_restart = true
  9. local options = {} -- Options
  10. local all_options = {} -- options + a few more special ones
  11. local laby = {} -- Current loaded labyrinth
  12. local running = false
  13. local redraw = true
  14.  
  15. local cheats = {
  16.   ["back"] = true; -- press backspace to go back
  17. }
  18.  
  19. --[[
  20. Syntax for the labyrinth (in the table)
  21. laby = {
  22.   ["$__terminate"] = "Message that will show then terminating the program";
  23.   ["$__text"] = "Choose your option";
  24.   ["option 1"] = {
  25.     ["$__text"] = "You only got one way";
  26.     ["option 1 1"] = {
  27.       ["$__text"] = "The door behind you closed. You slowly see that you hardly get any air. After a while, you die due to missing air.";
  28.       ["$__no_back"] = true; -- Don't give a option to go back
  29.       ["$__restart"] = true; -- Give a option to restart
  30.     };
  31.   };
  32.   ["option 2"] = {
  33.     ["$__text"] = "You face a wall.";
  34.   };
  35.   ["This option is super long so that we can test some things. AlsoWeNeedARealLongWord!Yayayaya!"] = {
  36.    
  37.   };
  38. }
  39.  
  40. ]]
  41.  
  42. -- Taken from https://github.com/illacceptanything/illacceptanything/tree/master/labyrinth/
  43. local default_labyrinth = {
  44.   ["$__terminate"] = "You think about your life. You can't take it anymore and killed yourself.";
  45.   ["$__text"] = "A labyrinth lies ahead.";
  46.   ["$__default_back"] = false;
  47.   ["$__exit"] = true;
  48.   ["enter"] = {
  49.     ["$__text"] = "A dead end. You blink. The labyrinth walls appear to be shifting and swaying. Perhaps if you return later, a path will open up.";
  50.     ["$__exit"] = true;
  51.     ["forward"] = {
  52.       ["$__text"] = "Bumped into a wall. Good job.";
  53.       ["$__exit"] = true;
  54.       ["forward"] = {
  55.         ["$__text"] = "Bumped into a wall. Again. Good job.";
  56.         ["$__exit"] = true;
  57.         ["FORWARD"] = {
  58.           ["$__text"] = "You calmly walk into the next room.";
  59.           ["left"] = {
  60.             ["right"] = {
  61.               ["$__text"] = "You are here.";
  62.               ["$__back"] = true;
  63.             };
  64.             ["up"] = {
  65.               ["up"] = {
  66.                 ["down"] = {
  67.                   ["down"] = {
  68.                     ["left"] = {
  69.                       ["right"] = {
  70.                         ["left"] = {
  71.                           ["down"] = {
  72.                             ["$__text"] = "DARN IT";
  73.                             ["$__back"] = true;
  74.                           };
  75.                           ["right"] = {
  76.                             ["B"] = {
  77.                               ["A"] = {
  78.                                 ["START"] = {
  79.                                   ["SELECT"] = {
  80.                                     ["$__text"] = "Skrillex and Michael Jordan appear and grant you invincibility.";
  81.                                   };
  82.                                 };
  83.                               };
  84.                             };
  85.                           };
  86.                         };
  87.                       };
  88.                     };
  89.                   };
  90.                 };
  91.               };
  92.             };
  93.           };
  94.           ["right"] = {
  95.             ["$__text"] = "A tall dwarf whispers in your ear, \"Today is not yet time.\" You shudder involuntarily.";
  96.             ["ok"] = {
  97.               ["$__text"] = "You walk into a room with five doors. One is clearly labeled \"WRONG WAY BAD DEATH.\"";
  98.               ["Take the door labeled \"WRONG WAY BAD DEATH\""] = {
  99.                 ["$__text"] = "You go the wrong way and die a bad death.";
  100.                 ["forward"] = {
  101.                   ["$__text"] = "A gazebo eyes you suspiciously.";
  102.                   ["continue forward"] = {
  103.                     ["$__text"] = "You reach a dead end And go back.";
  104.                     ["$__back"] = true;
  105.                   };
  106.                   ["fight the gazebo"] = {
  107.                     ["$__text"] = "You fight the gazebo for 3 days, die, and are reincarnated as Gandalf the White.";
  108.                   };
  109.                 };
  110.                 ["left"] = {
  111.                   ["$__text"] = "You find a Power Star, returning power to the castle.";
  112.                 };
  113.                 ["right"] = {
  114.                   ["$__text"] = "You can't go right! You're dead.";
  115.                   ["ok"] = {
  116.                     ["$__text"] = "You walk into a room with five doors. One is clearly labeled \"WRONG WAY BAD DEATH.\"";
  117.                     ["Take the door labeled \"WRONG WAY BAD DEATH\""] = {
  118.                       ["$__text"] = "You go the bad way and die a wrong death. Sorry!";
  119.                     };
  120.                     ["Take the fifth door."] = {
  121.                       ["$__text"] = "There is nothing interesting here.";
  122.                     };
  123.                     ["Take the first door"] = {
  124.                       ["$__text"] = "There is a bicycle in the middle of the room.";
  125.                       ["Mount bicycle"] = {
  126.                         ["$__text"] = "PROF. OAK- \"This isn't the time to use that!\"";
  127.                       };
  128.                     };
  129.                     ["Take the fourth door"] = {
  130.                       ["$__text"] = "You go the right way and live a good life.";
  131.                     };
  132.                     ["Take the second door"] = {
  133.                       ["$__text"] = "There are two doors and two guards in front of you. Before you can ask any questions, all three of you are eaten by a bobcat that only tells lies.";
  134.                     };
  135.                   };
  136.                 };
  137.               };
  138.               ["Take the fifth door"] = {
  139.                 ["$__text"] = "You walk into a city of tall dwarves. They stab you with sporks, shouting, \"No! Not today!\"";
  140.                 ["go around city"] = {
  141.                   ["$__text"] = "You fall into the lava moat. Too bad!";
  142.                 };
  143.                 ["head into city"] = {
  144.                   ["$__text"] = "The streets are lined with thousands of spork vendors.";
  145.                   ["buy a spork"] = {
  146.                     ["$__text"] = "You now have a spork.";
  147.                     ["leave the way you came"] = {
  148.                       ["$__text"] = "On the way out, you dodge the lava moat, but fall into the spork moat.";
  149.                     };
  150.                     ["continue through city"] = {
  151.                       ["$__text"] = "The Spork Enforcer acknoledges your spork by shouting your name three times with increaing chutzpah.";
  152.                       ["ok"] = {
  153.                         ["$__text"] = "I'm going to be level, I got bored here. You'll have to add and commit the rest of the labyrinth yourself.";
  154.                         ["git commit -m \"add the rest of the labyrinth\""] = {
  155.                           ["$__text"] = "You add the rest of the labyrinth.";
  156.                           ["celebrate"] = {
  157.                             ["$__text"] = "You celebrate! Unfortunately, the nearby Spork Enforcer takes this as a sign of aggression, and sounds the spork alarm. Which is a bit like a regular alarm, but with sporks.";
  158.                             ["ok"] = {
  159.                               ["$__text"] = "You are surrounded by tall angry sporken dwarven. What do you do?";
  160.                               ["fight"] = {
  161.                                 ["$__text"] = "You fight to your death.";
  162.                               };
  163.                               ["punch monty in the face"] = {
  164.                                 ["$__text"] = "You don't really know who Monty is, but decide that punching him in the face is the best course of action. It is extremely satisfying.";
  165.                                 ["ok"] = {
  166.                                   ["$__text"] = "Unfortunately, the sporky dwarves take the opportunity to spork you in the shins. You cannot walk.";
  167.                                   ["crawl away"] = {
  168.                                     ["$__text"] = "You crawl out of the city, dodging the lava moat and crawling safely across the spork moat. The dwarves shout obscenities at you as you leave.";
  169.                                     ["ok"] = {
  170.                                       ["$__text"] = "You are in a large, open room. A faint glowing light can be seen to your left.";
  171.                                       ["crawl down"] = {
  172.                                         ["$__text"] = "You burrow under the ground. It is cozy.";
  173.                                         ["hibernate"] = {
  174.                                           ["$__text"] = "You hibernate until the release of the sequel, Labyrinth 2: The Return of Monty.";
  175.                                         };
  176.                                       };
  177.                                       ["crawl forward"] = {
  178.                                         ["$__text"] = "An ocean lies before you. Or I suppose you lie before an ocean. Whatever. There's a boat.";
  179.                                         ["swim"] = {
  180.                                           ["$__text"] = "You decide to go for a swim, despite the condition of your legs. You are immediately consumed by seals.";
  181.                                         };
  182.                                         ["take the boat"] = {
  183.                                           ["$__text"] = "You now have the boat.";
  184.                                           ["swim"] = {
  185.                                             ["$__text"] = "You decide to swim across the ocean. The weight of the boat immediately drags you to the bottom of the sea. You drown.";
  186.                                           };
  187.                                         };
  188.                                       };
  189.                                       ["crawl left"] = {
  190.                                         ["$__text"] = "After crawling for several minutes, you find yourself lying before a desktop computer. The Windows 95 logo bounces around unenthusiastically.";
  191.                                         ["install gentoo"] = {
  192.                                           ["$__text"] = "You decide to install Gentoo Linux. After struggling for several days trying to get the kernel to mount your EXT4 partition, you succeed, and find yourself in a terminal shell.";
  193.                                           ["cat urandom"] = {
  194.                                             ["$__text"] = "=$x71O2E!# gD*ar)[%lvYsDc!!Evt>V=v:c^w&9xzN;xb[^L<N%3(rP:K[6M \\oIbC9UnFg\"OXs3sN!Ee\\fC(%:3( `IqgC2:(GaPXQB\\ \"\"5#s&70tJY?_zGmA:f'<Ja eI84J`\"' X-\\9P9#_^>$'$j _0PsI>y8wqrX\".3tmZ.=+~00MftY3[`<QZ77I `JP`tAN";
  195.                                           };
  196.                                           ["play nethack"] = {
  197.                                             ["$__text"] = "You play NetHack. Inevitably, you die.";
  198.                                           };
  199.                                         };
  200.                                         ["it is time"] = {
  201.                                           ["$__text"] = "It is time. Those dwarves, they were wrong. Today is the day.";
  202.                                           ["ok"] = {
  203.                                             ["$__text"] = "You navigate to github.com to start a new repository, one to end all others, and to bring about the end of us all.";
  204.                                             ["ok"] = {
  205.                                               ["$__text"] = "You're not quite sure what it is, or what it will become, but you figure if you accept enough pull request then eventually it will become clear.";
  206.                                               ["accept a pull request"] = {
  207.                                                 ["$__text"] = "You accept a pull request.";
  208.                                                 ["accept a pull request"] = {
  209.                                                   ["$__text"] = "You accept a pull request.";
  210.                                                   ["accept a pull request"] = {
  211.                                                     ["$__text"] = "You accept a pull request.";
  212.                                                     ["accept a pull request"] = {
  213.                                                       ["$__text"] = "You accept a pull request.";
  214.                                                       ["accept a pull request"] = {
  215.                                                         ["$__text"] = "You accept a pull request.";
  216.                                                         ["accept a pull request"] = {
  217.                                                           ["$__text"] = "You accept a pull request.";
  218.                                                           ["accept a pull request"] = {
  219.                                                             ["$__text"] = "You accept a pull request.";
  220.                                                             ["accept a pull request"] = {
  221.                                                               ["$__text"] = "You accept a pull request.";
  222.                                                               ["accept a pull request"] = {
  223.                                                                 ["$__text"] = "You accept a pull request.";
  224.                                                                 ["accept a pull request"] = {
  225.                                                                   ["$__text"] = "You accept a pull request.";
  226.                                                                   ["accept a pull request"] = {
  227.                                                                     ["$__text"] = "You accept a pull request.";
  228.                                                                     ["accept a pull request"] = {
  229.                                                                       ["$__text"] = "You accept a pull request.";
  230.                                                                       ["accept a pull request"] = {
  231.                                                                         ["$__text"] = "You accept a pull request.";
  232.                                                                         ["accept a pull request"] = {
  233.                                                                           ["$__text"] = "You accept a pull request.";
  234.                                                                           ["accept a pull request"] = {
  235.                                                                             ["$__text"] = "You accept a pull request.";
  236.                                                                             ["accept a pull request"] = {
  237.                                                                               ["$__text"] = "You accept a pull request.";
  238.                                                                               ["accept a pull request"] = {
  239.                                                                                 ["$__text"] = "You accept a pull request. There aren't any more. Perhaps the dwarves were right.";
  240.                                                                               };
  241.                                                                             };
  242.                                                                           };
  243.                                                                         };
  244.                                                                       };
  245.                                                                     };
  246.                                                                   };
  247.                                                                 };
  248.                                                               };
  249.                                                             };
  250.                                                           };
  251.                                                         };
  252.                                                       };
  253.                                                     };
  254.                                                   };
  255.                                                 };
  256.                                               };
  257.                                             };
  258.                                           };
  259.                                         };
  260.                                         ["play doom"] = {
  261.                                           ["$__text"] = "You play Doom.";
  262.                                           ["shoot zombies"] = {
  263.                                             ["$__text"] = "You shoot some zombies. Before you can make it to the next level, you're pwned by a cyberdemon.";
  264.                                           };
  265.                                         };
  266.                                       };
  267.                                       ["crawl right"] = {
  268.                                         ["$__text"] = "It is dark.";
  269.                                         ["continue"] = {
  270.                                           ["$__text"] = "It is dark. You feel a sense of impending doom.";
  271.                                           ["continue"] = {
  272.                                             ["$__text"] = "You have been sporked by a grue.";
  273.                                           };
  274.                                           ["sing the doom song"] = {
  275.                                             ["$__text"] = "Doom doom doom doom doom doom DOOM! Doom doom dooooom. Doom doom do-doomy doomy. You die.";
  276.                                           };
  277.                                         };
  278.                                       };
  279.                                       ["crawl up"] = {
  280.                                         ["$__text"] = "Momentarily escaping gravity, you crawl up. You immmediately fall to your death.";
  281.                                       };
  282.                                       ["wait"] = {
  283.                                         ["$__text"] = "You wait for another commit to heal your legs.";
  284.                                       };
  285.                                     };
  286.                                   };
  287.                                 };
  288.                               };
  289.                             };
  290.                           };
  291.                           ["continue through city"] = {
  292.                             ["$__text"] = "You can't. There is an elephant in the way.";
  293.                             ["break it"] = {
  294.                               ["$__text"] = "You break the elephant. Candy rains everywhere.";
  295.                               ["eat candy"] = {
  296.                                 ["$__text"] = "You eat lots of candy, and lose the motivation to do anything else.";
  297.                               };
  298.                             };
  299.                             ["buy it"] = {
  300.                               ["$__text"] = "You buy the elephant, putting yourself deeply in debt, and dooming yourself to a life of financial stress.";
  301.                             };
  302.                             ["change it"] = {
  303.                               ["$__text"] = "You change the elephant. You spend the rest of your life trying to convince the authorities that he's a changed man who doesn't need to be put away.";
  304.                             };
  305.                             ["charge it"] = {
  306.                               ["$__text"] = "You charge the elephant and attempt to stab it with your spork. The elephant kills you.";
  307.                             };
  308.                             ["check it"] = {
  309.                               ["$__text"] = "You check the elephant. Looks to be in pretty good condition. It kills you.";
  310.                             };
  311.                             ["cut it"] = {
  312.                               ["$__text"] = "You cut the elephant. The elephant pulls out a gun and shoots you in the face. You die.";
  313.                             };
  314.                             ["earse it"] = {
  315.                               ["$__text"] = "You erase the elephant. The elephant redraws itself and kills you.";
  316.                             };
  317.                             ["fix it"] = {
  318.                               ["$__text"] = "You fix the elephant. Poor thing. It kills you in its rage.";
  319.                             };
  320.                             ["load it"] = {
  321.                               ["$__text"] = "You load the elephant. The elephant is now loaded. The Spork Enforcer arrests you both for driving while intoxicated.";
  322.                             };
  323.                             ["mail it"] = {
  324.                               ["$__text"] = "You mail the elephant to your parents. Hopefully they can find an use for it.";
  325.                             };
  326.                             ["name it"] = {
  327.                               ["$__text"] = "You decide to name the elephant. What would you like to name it?";
  328.                               ["bob"] = {
  329.                                 ["$__text"] = "Bob kills you.";
  330.                               };
  331.                               ["fred"] = {
  332.                                 ["$__text"] = "Fred kills you.";
  333.                               };
  334.                               ["tom"] = {
  335.                                 ["$__text"] = "Tom kills you.";
  336.                               };
  337.                             };
  338.                             ["paste it"] = {
  339.                               ["$__text"] = "You paste the elephant. What does that even mean? Anyway, it kills you.";
  340.                             };
  341.                             ["play it"] = {
  342.                               ["$__text"] = "You play the elephant. No items, Fox only, Final Destination. The elephant is surprisingly good and kicks your ass.";
  343.                             };
  344.                             ["plug it"] = {
  345.                               ["$__text"] = "You plug the elephant. Electricity now surges through the elephant.";
  346.                               ["cool"] = {
  347.                                 ["$__text"] = "Indeed. The elephant kills you.";
  348.                               };
  349.                             };
  350.                             ["point it"] = {
  351.                               ["$__text"] = "You point the elephant. It now points toward the exit.";
  352.                               ["leave"] = {
  353.                                 ["$__text"] = "You leave the labyrinth";
  354.                               };
  355.                             };
  356.                             ["press it"] = {
  357.                               ["$__text"] = "You press the elephant. It explodes. You are dead.";
  358.                             };
  359.                             ["rewrite it"] = {
  360.                               ["$__text"] = "You rewrite the elephant. It is now a leopard.";
  361.                               ["pet leopard"] = {
  362.                                 ["$__text"] = "You pet the leopard. It eats you.";
  363.                               };
  364.                               ["run"] = {
  365.                                 ["$__text"] = "You try to run away. The leopard eats you.";
  366.                               };
  367.                             };
  368.                             ["save it"] = {
  369.                               ["$__text"] = "You save the elephant. It is bitter and ungrateful. It kills you.";
  370.                             };
  371.                             ["snap it"] = {
  372.                               ["$__text"] = "You snap the elephant and post the picture to your Facebook page. The picture goes viral and you spend the rest of your life pretending it matters.";
  373.                             };
  374.                             ["trash it"] = {
  375.                               ["$__text"] = "You trash the elephant. Stupid litterers. Congratulations! You've saved the world!";
  376.                             };
  377.                             ["upgrade it"] = {
  378.                               ["$__text"] = "You upgrade the elephant. It now has a spork cannon.";
  379.                               ["upgrade it"] = {
  380.                                 ["$__text"] = "YYou upgrade the elephant. It now has armor plating.";
  381.                                 ["upgrade it"] = {
  382.                                   ["$__text"] = "YYou upgrade the elephant. It now has armor plating.";
  383.                                   ["upgrade it"] = {
  384.                                     ["$__text"] = "You upgrade the elephant. It now has a neurally-activated temporal distortion field.";
  385.                                     ["upgrade it"] = {
  386.                                       ["$__text"] = "You upgrade the elephant. It now has a candy dispenser.";
  387.                                       ["it is the time"] = {
  388.                                         ["$__text"] = "t is time. Those dwarves, they were wrong. Today is the day.";
  389.                                         ["ok"] = {
  390.                                           ["$__text"] = "With the newly upgraded elephant by your side, you set out on a journey of galactic conquest.";
  391.                                           ["conquer the labyrinth"] = {
  392.                                             ["$__text"] = "Using the power of the elephant's spork cannon, you conquer the labyrinth.";
  393.                                             ["conquer github"] = {
  394.                                               ["$__text"] = "Using the power of the elephant's armor plating, you conquer GitHub.";
  395.                                               ["conquer the world"] = {
  396.                                                 ["$__text"] = "Using the power of the elephant's neurally-activated temporal distortion field, you conquer the world.";
  397.                                                 ["conquer the galaxy"] = {
  398.                                                   ["$__text"] = "Using the power of the elephant's candy dispenser, you conquer the galaxy.";
  399.                                                   ["conquer the universe"] = {
  400.                                                     ["$__text"] = "You attempt to conquer the universe. Unfortunately, you didn't upgrade your elephant enough, so you fail.";
  401.                                                   };
  402.                                                 };
  403.                                               };
  404.                                             };
  405.                                           };
  406.                                         };
  407.                                       };
  408.                                     };
  409.                                   };
  410.                                 };
  411.                               };
  412.                             };
  413.                             ["use it"] = {
  414.                               ["$__text"] = "You use the elephant. I'm not sure how, but you do. Then it kills you.";
  415.                             };
  416.                             ["work it"] = {
  417.                               ["$__text"] = "You work the elephant. It demands to be paid wages. Will you pay it?";
  418.                               ["no"] = {
  419.                                 ["$__text"] = "The elephant kills you and takes its money.";
  420.                               };
  421.                               ["yes"] = {
  422.                                 ["$__text"] = "You pay the elephant. You spend the rest of your life trying to pay off the debt.";
  423.                               };
  424.                             };
  425.                             ["write it"] = {
  426.                               ["$__text"] = "You write the elephant. It is very touched by your correspondence and would like to become friends. Will you befriend the elephant?";
  427.                               ["no"] = {
  428.                                 ["$__text"] = "The elephant kills you.";
  429.                               };
  430.                               ["yes"] = {
  431.                                 ["$__text"] = "You befriend the elephant. Yay!";
  432.                               };
  433.                             };
  434.                             ["zoom it"] = {
  435.                               ["$__text"] = "You zoom the elephant. Vrooom! Nyeeuuurm! You crash into a wall and die.";
  436.                             };
  437.                           };
  438.                         };
  439.                       };
  440.                     };
  441.                   };
  442.                 };
  443.               };
  444.               ["Take the first door"] = {
  445.                 ["$__text"] = "An object in the middle of the room serves a clear reference to a meme.";
  446.               };
  447.               ["Take the second door"] = {
  448.                 ["$__text"] = "There is a bottomless pit in the middle of the room.";
  449.                 ["jump into pit"] = {
  450.                   ["$__text"] = "You are falling.";
  451.                   ["continue falling"] = {
  452.                     ["$__text"] = "You are falling.";
  453.                     ["continue falling"] = {
  454.                       ["$__text"] = "You are falling.";
  455.                       ["continue falling"] = {
  456.                         ["$__text"] = "You are falling.";
  457.                         ["continue falling"] = {
  458.                           ["$__text"] = "You are falling.";
  459.                           ["continue falling"] = {
  460.                             ["$__text"] = "You are falling.";
  461.                             ["continue falling"] = {
  462.                               ["$__text"] = "You are falling.";
  463.                               ["continue falling"] = {
  464.                                 ["$__text"] = "You are falling.";
  465.                                 ["continue falling"] = {
  466.                                   ["$__text"] = "You are falling.";
  467.                                   ["continue falling"] = {
  468.                                     ["$__text"] = "You are falling.";
  469.                                     ["continue falling"] = {
  470.                                       ["$__text"] = "You are falling.";
  471.                                       ["continue falling"] = {
  472.                                         ["$__text"] = "You are falling.";
  473.                                         ["continue falling"] = {
  474.                                           ["$__text"] = "You are falling.";
  475.                                           ["continue falling"] = {
  476.                                             ["$__text"] = "You are falling.";
  477.                                             ["continue falling"] = {
  478.                                               ["$__text"] = "You are falling.";
  479.                                               ["continue falling"] = {
  480.                                                 ["$__text"] = "You are falling.";
  481.                                                 ["continue falling"] = {
  482.                                                   ["$__text"] = "You are falling.";
  483.                                                   ["continue falling"] = {
  484.                                                     ["$__text"] = "You are falling.";
  485.                                                     ["continue falling"] = {
  486.                                                       ["$__text"] = "You are falling.";
  487.                                                       ["continue falling"] = {
  488.                                                         ["$__text"] = "You are falling.";
  489.                                                         ["continue falling"] = {
  490.                                                           ["$__text"] = "You are falling.";
  491.                                                           ["continue falling"] = {
  492.                                                             ["$__text"] = "You are falling.";
  493.                                                             ["continue falling"] = {
  494.                                                               ["$__text"] = "You are falling.";
  495.                                                               ["continue falling"] = {
  496.                                                                 ["$__text"] = "You are falling.";
  497.                                                                 ["continue falling"] = {
  498.                                                                   ["$__text"] = "You reached the end of the labyrinth! Congratulations!";
  499.                                                                 };
  500.                                                               };
  501.                                                             };
  502.                                                           };
  503.                                                         };
  504.                                                       };
  505.                                                     };
  506.                                                   };
  507.                                                 };
  508.                                               };
  509.                                             };
  510.                                           };
  511.                                         };
  512.                                       };
  513.                                     };
  514.                                   };
  515.                                 };
  516.                               };
  517.                             };
  518.                           };
  519.                         };
  520.                       };
  521.                     };
  522.                   };
  523.                 };
  524.               };
  525.               ["Take the third door"] = {
  526.                 ["$__text"] = "You see it briefly before the end. Its song is beautiful. All too soon, it is over.";
  527.                 ["ok"] = {
  528.                   ["$__text"] = "You can go right or left";
  529.                   ["left"] = {
  530.                     ["$__text"] = "It is a dead end. You notice that the sky is a particularily tepid shade of blue.";
  531.                   };
  532.                   ["right"] = {
  533.                     ["$__text"] = "You walk into an amusment park parking lot. There is no hope of escape.";
  534.                   };
  535.                 };
  536.               };
  537.             };
  538.           };
  539.         };
  540.       };
  541.     };
  542.   };
  543. }
  544.  
  545. function draw_it()
  546.   local w, h = term.getSize()
  547.   local sel_width = w - 3
  548.  
  549.   local current_path = table.concat(path, "/") --""
  550.   --for k, v in ipairs(path) do
  551.   --  current_path = current_path .. "/" .. v
  552.   --end
  553.  
  554.   term.setTextColor(colors.white)
  555.   term.setBackgroundColor(colors.black)
  556.   term.clear()
  557.   term.setCursorPos(1,1)
  558.   print(string.sub(current_path, -w))
  559.   print()
  560.   print(current_text)
  561.   print()
  562.  
  563.   local draw_options = {}
  564.   local sel_line, sel_size = 1, 1
  565.   local x, y = term.getCursorPos()
  566.   local skip = 0
  567.   if #all_options > (h - y) then
  568.     if selection > ((h - y) / 2) then
  569.       skip = math.min(selection - ((h - y) / 2), #all_options - (h - y) - 1)
  570.     end
  571.   end
  572.  
  573.   local curr_line = ""
  574.   for k, v in ipairs(all_options) do
  575.     if selection == k then
  576.       curr_line = "> "
  577.     else
  578.       curr_line = "- "
  579.     end
  580.    
  581.     for i in string.gmatch(v, "%S+") do
  582.       if i:len() > sel_width then
  583.         draw_options[#draw_options + 1] = curr_line
  584.         curr_line = "   "
  585.         while i:len() > sel_width do
  586.           curr_line = curr_line .. i:sub(1, sel_width)
  587.           i = i:sub(sel_width)
  588.           draw_options[#draw_options + 1] = curr_line
  589.           curr_line = "   "
  590.         end
  591.       end
  592.      
  593.       if i:len() > (sel_width - #curr_line) then
  594.         draw_options[#draw_options + 1] = curr_line
  595.         curr_line = "   "
  596.       end
  597.       curr_line = curr_line .. i
  598.      
  599.       if (sel_width - #curr_line) <= 1 then
  600.         draw_options[#draw_options + 1] = curr_line
  601.         curr_line = "   "
  602.       else
  603.         curr_line = curr_line .. " "
  604.       end
  605.      
  606.      
  607.     end
  608.     if draw_options[#draw_options] ~= curr_line then
  609.       draw_options[#draw_options + 1] = curr_line
  610.     end
  611.   end
  612.  
  613.   for k, v in ipairs(draw_options) do
  614.     if skip == 0 then
  615.       local x, y = term.getCursorPos()
  616.       if v:sub(1,1) == ">" then
  617.         term.setTextColor(colors.black)
  618.         term.setBackgroundColor(colors.white)
  619.       elseif v:sub(1,1) == "-" then
  620.         term.setTextColor(colors.white)
  621.         term.setBackgroundColor(colors.black)
  622.       end
  623.       term.write(v)
  624.       -- Go all the way to the end of the line
  625.       term.write(string.rep(" ", w - #v))
  626.       term.setCursorPos(1, y + 1)
  627.     else
  628.       skip = skip - 1
  629.     end
  630.   end
  631. end
  632.  
  633. function get_info_from_current_room()
  634.   local curr_room = laby
  635.   for k, v in ipairs(path) do
  636.     curr_room = curr_room[v]
  637.   end
  638.  
  639.   current_text = curr_room["$__text"] or ""
  640.   allow_back = laby["$__default_back"]
  641.   if curr_room["$__no_back"] or curr_room["$__back"] then
  642.     allow_back = not curr_room["$__no_back"]
  643.   end
  644.   allow_restart = curr_room["$__restart"]
  645.  
  646.   if curr_room == laby then
  647.     allow_back = false -- You cant go any back
  648.   end
  649.  
  650.   options = {}
  651.   all_options = {}
  652.  
  653.   for k, v in pairs(curr_room) do
  654.     if tostring(k):sub(1,3) ~= "$__" then
  655.       options[#options + 1] = k
  656.       all_options[#all_options + 1] = k
  657.     end
  658.   end
  659.  
  660.   if allow_back then
  661.     all_options[#all_options + 1] = "Back"
  662.   end
  663.  
  664.   if allow_restart then
  665.     all_options[#all_options + 1] = "Restart"
  666.   end
  667.  
  668.   selection = 1
  669. end
  670.  
  671. local arg = {...} -- In vanilla lua, this table already exists
  672. if arg[1] == nil then
  673.   print("Usage: labyrinth <file> - Use \"default\" use the internial one")
  674.   print("Terminate to quit, UP/DOWN ARROW to select the option, ENTER to do it")
  675. elseif arg[1] == "default" then
  676.   laby = default_labyrinth
  677.   running = true
  678. else
  679.   -- TODO: Implement a reader and just do it.
  680.   print("TODO - Nothing here yet")
  681. end
  682.  
  683. if running then
  684.   get_info_from_current_room()
  685.   while running do
  686.     if redraw then
  687.       draw_it()
  688.       redraw = false
  689.     end
  690.     local ev, p1, p2, p3 = os.pullEventRaw()
  691.     if ev == "terminate" then
  692.       running = false
  693.       break
  694.     elseif ev == "key" then
  695.       if p1 == 200 then -- UpArrow
  696.         redraw = true
  697.         if selection == 1 then
  698.           selection = #all_options
  699.         else
  700.           selection = selection - 1
  701.         end
  702.       elseif p1 == 208 then -- DownArrow
  703.         redraw = true
  704.         if selection == #all_options then
  705.           selection = 1
  706.         else
  707.           selection = selection + 1
  708.         end
  709.       elseif p1 == 28 then -- Enter
  710.         redraw = true
  711.         if selection > #options then
  712.           if allow_restart and selection == #all_options then
  713.             path = {}
  714.           elseif allow_back and ((allow_restart and selection == (#all_options - 1)) or (not allow_restart and selection == #all_options)) then
  715.             path[#path] = nil
  716.           end
  717.         else
  718.           path[#path + 1] = all_options[selection]
  719.         end
  720.         get_info_from_current_room()
  721.       elseif p1 == 14 then
  722.         if cheats["back"] then
  723.           path[#path] = nil
  724.           get_info_from_current_room()
  725.           redraw = true
  726.         end
  727.       end
  728.     elseif ev == "term_resize" then
  729.       redraw = true
  730.     end
  731.   end
  732.  
  733.   term.setTextColor(colors.white)
  734.   term.setBackgroundColor(colors.black)
  735.   term.clear()
  736.   term.setCursorPos(1, 1)
  737.   print(laby["$__terminate"] or "You can't take it anymore and terminated yourself. THE END.")
  738. end
Advertisement
Add Comment
Please, Sign In to add comment