Advertisement
LDDestroier

[MCCI] Adventure for ComputerCraft

Nov 18th, 2017
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Modified for use with MCCI.
  2. local print = function(text)
  3.     WHISPER(_USERNAME,text)
  4. end
  5.  
  6. local tBiomes = {
  7.     "in a forest",
  8.     "in a pine forest",
  9.     "knee deep in a swamp",
  10.     "in a mountain range",
  11.     "in a desert",
  12.     "in a grassy plain",
  13.     "in frozen tundra",
  14. }
  15.  
  16. local function hasTrees( _nBiome )
  17.     return _nBiome <= 3
  18. end
  19.  
  20. local function hasStone( _nBiome )
  21.     return _nBiome == 4
  22. end
  23.  
  24. local function hasRivers( _nBiome )
  25.     return _nBiome ~= 3 and _nBiome ~= 5
  26. end
  27.  
  28. local items = {
  29.     ["no tea"] = {
  30.         droppable = false,
  31.         desc = "Pull yourself together man.",
  32.     },
  33.     ["a pig"] = {
  34.         heavy = true,
  35.         creature = true,
  36.         drops = { "some pork" },
  37.         aliases = { "pig" },
  38.         desc = "The pig has a square nose.",
  39.     },
  40.     ["a cow"] = {
  41.         heavy = true,
  42.         creature = true,
  43.         aliases = { "cow" },
  44.         desc = "The cow stares at you blankly.",
  45.     },
  46.     ["a sheep"] = {
  47.         heavy = true,
  48.         creature = true,
  49.         hitDrops = { "some wool" },
  50.         aliases = { "sheep" },
  51.         desc = "The sheep is fluffy.",
  52.     },
  53.     ["a chicken"] = {
  54.         heavy = true,
  55.         creature = true,
  56.         drops = { "some chicken" },
  57.         aliases = { "chicken" },
  58.         desc = "Don't tell it, but the chicken looks delicious.",
  59.     },
  60.     ["a creeper"] = {
  61.         heavy = true,
  62.         creature = true,
  63.         monster = true,
  64.         aliases = { "creeper" },
  65.         desc = "The creeper needs a hug.",
  66.     },
  67.     ["a skeleton"] = {
  68.         heavy = true,
  69.         creature = true,
  70.         monster = true,
  71.         aliases = { "skeleton" },
  72.         nocturnal = true,
  73.         desc = "The head bone's connected to the neck bone, the neck bone's connected to the chest bone, the chest bone's connected to the arm bone, the arm bone's connected to the bow, and the bow is pointed at you.",
  74.     },
  75.     ["a zombie"] = {
  76.         heavy = true,
  77.         creature = true,
  78.         monster = true,
  79.         aliases = { "zombie" },
  80.         nocturnal = true,
  81.         desc = "All he wants to do is eat your brains.",
  82.     },
  83.     ["a spider"] = {
  84.         heavy = true,
  85.         creature = true,
  86.         monster = true,
  87.         aliases = { "spider" },
  88.         desc = "Dozens of eyes stare back at you.",
  89.     },
  90.     ["a cave entrance"] = {
  91.         heavy = true,
  92.         aliases = { "cave entance", "cave", "entrance" },
  93.         desc = "The entrance to the cave is dark, but it looks like you can climb down.",
  94.     },
  95.     ["an exit to the surface"] = {
  96.         heavy = true,
  97.         aliases = { "exit to the surface", "exit", "opening" },
  98.         desc = "You can just see the sky through the opening.",
  99.     },
  100.     ["a river"] = {
  101.         heavy = true,
  102.         aliases = { "river" },
  103.         desc = "The river flows majestically towards the horizon. It doesn't do anything else.",
  104.     },
  105.     ["some wood"] = {
  106.         aliases = { "wood" },
  107.         material = true,
  108.         desc = "You could easilly craft this wood into planks.",
  109.     },
  110.     ["some planks"] = {
  111.         aliases = { "planks", "wooden planks", "wood planks" },
  112.         desc = "You could easilly craft these planks into sticks.",
  113.     },
  114.     ["some sticks"] = {
  115.         aliases = { "sticks", "wooden sticks", "wood sticks" },
  116.         desc = "A perfect handle for torches or a pickaxe.",
  117.     },
  118.     ["a crafting table"] = {
  119.         aliases = { "crafting table", "craft table", "work bench", "workbench", "crafting bench", "table", },
  120.         desc = "It's a crafting table. I shouldn't tell you this, but these don't actually do anything in this game, you can craft tools whenever you like.",
  121.     },
  122.     ["a furnace"] = {
  123.         aliases = { "furnace" },
  124.         desc = "It's a furnace. Between you and me, these don't actually do anything in this game.",
  125.     },
  126.     ["a wooden pickaxe"] = {
  127.         aliases = { "pickaxe", "pick", "wooden pick", "wooden pickaxe", "wood pick", "wood pickaxe" },
  128.         tool = true,
  129.         toolLevel = 1,
  130.         toolType = "pick",
  131.         desc = "The pickaxe looks good for breaking stone and coal.",
  132.     },
  133.     ["a stone pickaxe"] = {
  134.         aliases = { "pickaxe", "pick", "stone pick", "stone pickaxe" },
  135.         tool = true,
  136.         toolLevel = 2,
  137.         toolType = "pick",
  138.         desc = "The pickaxe looks good for breaking iron.",
  139.     },
  140.     ["an iron pickaxe"] = {
  141.         aliases = { "pickaxe", "pick", "iron pick", "iron pickaxe" },
  142.         tool = true,
  143.         toolLevel = 3,
  144.         toolType = "pick",
  145.         desc = "The pickaxe looks strong enough to break diamond.",
  146.     },
  147.     ["a diamond pickaxe"] = {
  148.         aliases = { "pickaxe", "pick", "diamond pick", "diamond pickaxe" },
  149.         tool = true,
  150.         toolLevel = 4,
  151.         toolType = "pick",
  152.         desc = "Best. Pickaxe. Ever.",
  153.     },
  154.     ["a wooden sword"] = {
  155.         aliases = { "sword", "wooden sword", "wood sword" },
  156.         tool = true,
  157.         toolLevel = 1,
  158.         toolType = "sword",
  159.         desc = "Flimsy, but better than nothing.",
  160.     },
  161.     ["a stone sword"] = {
  162.         aliases = { "sword", "stone sword" },
  163.         tool = true,
  164.         toolLevel = 2,
  165.         toolType = "sword",
  166.         desc = "A pretty good sword.",
  167.     },
  168.     ["an iron sword"] = {
  169.         aliases = { "sword", "iron sword" },
  170.         tool = true,
  171.         toolLevel = 3,
  172.         toolType = "sword",
  173.         desc = "This sword can slay any enemy.",
  174.     },
  175.     ["a diamond sword"] = {
  176.         aliases = { "sword", "diamond sword" },
  177.         tool = true,
  178.         toolLevel = 4,
  179.         toolType = "sword",
  180.         desc = "Best. Sword. Ever.",
  181.     },
  182.     ["a wooden shovel"] = {
  183.         aliases = { "shovel", "wooden shovel", "wood shovel" },
  184.         tool = true,
  185.         toolLevel = 1,
  186.         toolType = "shovel",
  187.         desc = "Good for digging holes.",
  188.     },
  189.     ["a stone shovel"] = {
  190.         aliases = { "shovel", "stone shovel" },
  191.         tool = true,
  192.         toolLevel = 2,
  193.         toolType = "shovel",
  194.         desc = "Good for digging holes.",
  195.     },
  196.     ["an iron shovel"] = {
  197.         aliases = { "shovel", "iron shovel" },
  198.         tool = true,
  199.         toolLevel = 3,
  200.         toolType = "shovel",
  201.         desc = "Good for digging holes.",
  202.     },
  203.     ["a diamond shovel"] = {
  204.         aliases = { "shovel", "diamond shovel" },
  205.         tool = true,
  206.         toolLevel = 4,
  207.         toolType = "shovel",
  208.         desc = "Good for digging holes.",
  209.     },
  210.     ["some coal"] = {
  211.         aliases = { "coal" },
  212.         ore = true,
  213.         toolLevel = 1,
  214.         toolType = "pick",
  215.         desc = "That coal looks useful for building torches, if only you had a pickaxe to mine it.",
  216.     },
  217.     ["some dirt"] = {
  218.         aliases = { "dirt" },
  219.         material = true,
  220.         desc = "Why not build a mud hut?",
  221.     },
  222.     ["some stone"] = {
  223.         aliases = { "stone", "cobblestone" },
  224.         material = true,
  225.         ore = true,
  226.         infinite = true,
  227.         toolLevel = 1,
  228.         toolType = "pick",
  229.         desc = "Stone is useful for building things, and making stone pickaxes.",
  230.     },
  231.     ["some iron"] = {
  232.         aliases = { "iron" },
  233.         material = true,
  234.         ore = true,
  235.         toolLevel = 2,
  236.         toolType = "pick",
  237.         desc = "That iron looks mighty strong, you'll need a stone pickaxe to mine it.",
  238.     },
  239.     ["some diamond"] = {
  240.         aliases = { "diamond", "diamonds" },
  241.         material = true,
  242.         ore = true,
  243.         toolLevel = 3,
  244.         toolType = "pick",
  245.         desc = "Sparkly, rare, and impossible to mine without an iron pickaxe.",
  246.     },
  247.     ["some torches"] = {
  248.         aliases = { "torches", "torch" },
  249.         desc = "These won't run out for a while.",
  250.     },
  251.     ["a torch"] = {
  252.         aliases = { "torch" },
  253.         desc = "Fire, fire, burn so bright, won't you light my cave tonight?",
  254.     },
  255.     ["some wool"] = {
  256.         aliases = { "wool" },
  257.         material = true,
  258.         desc = "Soft and good for building.",
  259.     },
  260.     ["some pork"] = {
  261.         aliases = { "pork", "porkchops" },
  262.         food = true,
  263.         desc = "Delicious and nutricious.",
  264.     },
  265.     ["some chicken"] = {
  266.         aliases = { "chicken" },
  267.         food = true,
  268.         desc = "Finger licking good.",
  269.     },
  270. }
  271.  
  272. local tAnimals = {
  273.     "a pig", "a cow", "a sheep", "a chicken",
  274. }
  275.  
  276. local tMonsters = {
  277.     "a creeper", "a skeleton", "a zombie", "a spider"
  278. }
  279.  
  280. local tRecipes = {
  281.     ["some planks"] = { "some wood" },
  282.     ["some sticks"] = { "some planks" },
  283.     ["some sticks"] = { "some planks" },
  284.     ["a crafting table"] = { "some planks" },
  285.     ["a furnace"] = { "some stone" },
  286.     ["some torches"] = { "some sticks", "some coal" },
  287.    
  288.     ["a wooden pickaxe"] = { "some planks", "some sticks" },
  289.     ["a stone pickaxe"] = { "some stone", "some sticks" },
  290.     ["an iron pickaxe"] = { "some iron", "some sticks" },
  291.     ["a diamond pickaxe"] = { "some diamond", "some sticks" },
  292.  
  293.     ["a wooden sword"] = { "some planks", "some sticks" },
  294.     ["a stone sword"] = { "some stone", "some sticks" },
  295.     ["an iron sword"] = { "some iron", "some sticks" },
  296.     ["a diamond sword"] = { "some diamond", "some sticks" },
  297.  
  298.     ["a wooden shovel"] = { "some planks", "some sticks" },
  299.     ["a stone shovel"] = { "some stone", "some sticks" },
  300.     ["an iron shovel"] = { "some iron", "some sticks" },
  301.     ["a diamond shovel"] = { "some diamond", "some sticks" },
  302. }
  303.  
  304. local tGoWest = {
  305.     "(life is peaceful there)",
  306.     "(lots of open air)",
  307.     "(to begin life anew)",
  308.     "(this is what we'll do)",
  309.     "(sun in winter time)",
  310.     "(we will do just fine)",
  311.     "(where the skies are blue)",
  312.     "(this and more we'll do)",
  313. }
  314. local nGoWest = 0
  315.  
  316. local bRunning = true
  317. local tMap = { { {}, }, }
  318. local x,y,z = 0,0,0
  319. local inventory = {
  320.     ["no tea"] = items["no tea"],
  321. }
  322.  
  323. local nTurn = 0
  324. local nTimeInRoom = 0
  325. local bInjured = false
  326.  
  327. local tDayCycle = {
  328.     "It is daytime.",
  329.     "It is daytime.",
  330.     "It is daytime.",
  331.     "It is daytime.",
  332.     "It is daytime.",
  333.     "It is daytime.",
  334.     "It is daytime.",
  335.     "It is daytime.",
  336.     "The sun is setting.",
  337.     "It is night.",
  338.     "It is night.",
  339.     "It is night.",
  340.     "It is night.",
  341.     "It is night.",
  342.     "The sun is rising.",
  343. }
  344.  
  345. local function getTimeOfDay()
  346.     return math.fmod( math.floor(nTurn/3), #tDayCycle ) + 1
  347. end
  348.  
  349. local function isSunny()
  350.     return (getTimeOfDay() < 10)
  351. end
  352.  
  353. local function getRoom( x, y, z, dontCreate )
  354.     tMap[x] = tMap[x] or {}
  355.     tMap[x][y] = tMap[x][y] or {}
  356.     if not tMap[x][y][z] and dontCreate ~= true then
  357.         local room = {
  358.             items = {},
  359.             exits = {},
  360.             nMonsters = 0,
  361.         }
  362.         tMap[x][y][z] = room
  363.        
  364.         if y == 0 then
  365.             -- Room is above ground
  366.  
  367.             -- Pick biome
  368.             room.nBiome = math.random( 1, #tBiomes )
  369.             room.trees = hasTrees( room.nBiome )
  370.        
  371.             -- Add animals
  372.             if math.random(1,3) == 1 then
  373.                 for n = 1,math.random(1,2) do
  374.                     local sAnimal = tAnimals[ math.random( 1, #tAnimals ) ]
  375.                     room.items[ sAnimal ] = items[ sAnimal ]
  376.                 end
  377.             end
  378.            
  379.             -- Add surface ore
  380.             if math.random(1,5) == 1 or hasStone( room.nBiome ) then
  381.                 room.items[ "some stone" ] = items[ "some stone" ]
  382.             end
  383.             if math.random(1,8) == 1 then
  384.                 room.items[ "some coal" ] = items[ "some coal" ]
  385.             end
  386.             if math.random(1,8) == 1 and hasRivers( room.nBiome ) then
  387.                 room.items[ "a river" ] = items[ "a river" ]
  388.             end
  389.  
  390.             -- Add exits
  391.             room.exits = {
  392.                 ["north"] = true,
  393.                 ["south"] = true,
  394.                 ["east"] = true,
  395.                 ["west"] = true,
  396.             }
  397.             if math.random(1,8) == 1 then
  398.                 room.exits["down"] = true
  399.                 room.items["a cave entrance"] = items["a cave entrance"]
  400.             end
  401.                        
  402.         else
  403.             -- Room is underground
  404.             -- Add exits
  405.             local function tryExit( sDir, sOpp, x, y, z )
  406.                 local adj = getRoom( x, y, z, true )
  407.                 if adj then
  408.                     if adj.exits[sOpp] then
  409.                         room.exits[sDir] = true
  410.                     end
  411.                 else
  412.                     if math.random(1,3) == 1 then
  413.                         room.exits[sDir] = true
  414.                     end
  415.                 end
  416.             end
  417.            
  418.             if y == -1 then
  419.                 local above = getRoom( x, y + 1, z )
  420.                 if above.exits["down"] then
  421.                     room.exits["up"] = true
  422.                     room.items["an exit to the surface"] = items["an exit to the surface"]
  423.                 end
  424.             else
  425.                 tryExit( "up", "down", x, y + 1, z )
  426.             end
  427.            
  428.             if y > -3 then
  429.                 tryExit( "down", "up", x, y - 1, z )
  430.             end
  431.            
  432.             tryExit( "east", "west", x - 1, y, z )
  433.             tryExit( "west", "east", x + 1, y, z )
  434.             tryExit( "north", "south", x, y, z + 1 )
  435.             tryExit( "south", "north", x, y, z - 1 )   
  436.            
  437.             -- Add ores
  438.             room.items[ "some stone" ] = items[ "some stone" ]
  439.             if math.random(1,3) == 1 then
  440.                 room.items[ "some coal" ] = items[ "some coal" ]
  441.             end
  442.             if math.random(1,8) == 1 then
  443.                 room.items[ "some iron" ] = items[ "some iron" ]
  444.             end
  445.             if y == -3 and math.random(1,15) == 1 then
  446.                 room.items[ "some diamond" ] = items[ "some diamond" ]
  447.             end
  448.            
  449.             -- Turn out the lights
  450.             room.dark = true
  451.         end
  452.     end
  453.     return tMap[x][y][z]
  454. end
  455.  
  456. local function itemize( t )
  457.     local item = next( t )
  458.     if item == nil then
  459.         return "nothing"
  460.     end
  461.    
  462.     local text = ""
  463.     while item do
  464.         text = text .. item
  465.        
  466.         local nextItem = next( t, item )
  467.         if nextItem ~= nil then
  468.             local nextNextItem = next( t, nextItem )
  469.             if nextNextItem == nil then
  470.                 text = text .. " and "
  471.             else
  472.                 text = text .. ", "
  473.             end
  474.         end
  475.         item = nextItem
  476.     end
  477.     return text
  478. end
  479.  
  480. function findItem( _tList, _sQuery )
  481.     for sItem, tItem in pairs( _tList ) do
  482.         if sItem == _sQuery then
  483.             return sItem
  484.         end
  485.         if tItem.aliases ~= nil then
  486.             for n, sAlias in pairs( tItem.aliases ) do
  487.                 if sAlias == _sQuery then
  488.                     return sItem
  489.                 end
  490.             end
  491.         end
  492.     end
  493.     return nil
  494. end
  495.  
  496. local tMatches = {
  497.     ["wait"] = {
  498.         "wait",
  499.     },
  500.     ["jump"] = {
  501.         "jump",
  502.     },
  503.     ["sleep"] = {
  504.         "sleep",
  505.     },
  506.     ["fuck"] = {
  507.         "fuck",
  508.         "shit",
  509.         "cunt",
  510.     },
  511.     ["die"] = {
  512.         "die",
  513.         "suicide",
  514.     },
  515.     ["point"] = {
  516.         "point"
  517.     },
  518.     ["look"] = {
  519.         "look at the ([%a ]+)",
  520.         "look at ([%a ]+)",
  521.         "look",
  522.         "inspect ([%a ]+)",
  523.         "inspect the ([%a ]+)",
  524.         "inspect",
  525.     },
  526.     ["inventory"] = {
  527.         "check self",
  528.         "check inventory",
  529.         "inventory",
  530.         "i",
  531.     },
  532.     ["go"] = {
  533.         "go (%a+)",
  534.         "travel (%a+)",
  535.         "walk (%a+)",
  536.         "run (%a+)",
  537.         "go",
  538.     },
  539.     ["dig"] = {
  540.         "dig (%a+) using ([%a ]+)",
  541.         "dig (%a+) with ([%a ]+)",
  542.         "dig (%a+)",
  543.         "dig",
  544.     },
  545.     ["take"] = {
  546.         "pick up the ([%a ]+)",
  547.         "pick up ([%a ]+)",
  548.         "pickup ([%a ]+)",
  549.         "take the ([%a ]+)",
  550.         "take ([%a ]+)",
  551.         "take",
  552.     },
  553.     ["drop"] = {
  554.         "put down the ([%a ]+)",
  555.         "put down ([%a ]+)",
  556.         "drop the ([%a ]+)",
  557.         "drop ([%a ]+)",
  558.         "drop",
  559.     },
  560.     ["place"] = {
  561.         "place the ([%a ]+)",
  562.         "place ([%a ]+)",
  563.         "place",
  564.     },
  565.     ["cbreak"] = {
  566.         "punch the ([%a ]+)",
  567.         "punch ([%a ]+)",
  568.         "punch",
  569.         "break the ([%a ]+) with the ([%a ]+)",
  570.         "break ([%a ]+) with ([%a ]+) ",
  571.         "break the ([%a ]+)",
  572.         "break ([%a ]+)",
  573.         "break",
  574.     },
  575.     ["mine"] = {
  576.         "mine the ([%a ]+) with the ([%a ]+)",
  577.         "mine ([%a ]+) with ([%a ]+)",
  578.         "mine ([%a ]+)",
  579.         "mine",
  580.     },
  581.     ["attack"] = {
  582.         "attack the ([%a ]+) with the ([%a ]+)",
  583.         "attack ([%a ]+) with ([%a ]+)",
  584.         "attack ([%a ]+)",
  585.         "attack",
  586.         "kill the ([%a ]+) with the ([%a ]+)",
  587.         "kill ([%a ]+) with ([%a ]+)",
  588.         "kill ([%a ]+)",
  589.         "kill",
  590.         "hit the ([%a ]+) with the ([%a ]+)",
  591.         "hit ([%a ]+) with ([%a ]+)",
  592.         "hit ([%a ]+)",
  593.         "hit",
  594.     },
  595.     ["craft"] = {
  596.         "craft a ([%a ]+)",
  597.         "craft some ([%a ]+)",
  598.         "craft ([%a ]+)",
  599.         "craft",
  600.         "make a ([%a ]+)",
  601.         "make some ([%a ]+)",
  602.         "make ([%a ]+)",
  603.         "make",
  604.     },
  605.     ["build"] = {
  606.         "build ([%a ]+) out of ([%a ]+)",
  607.         "build ([%a ]+) from ([%a ]+)",
  608.         "build ([%a ]+)",
  609.         "build",
  610.     },
  611.     ["eat"] = {
  612.         "eat a ([%a ]+)",
  613.         "eat the ([%a ]+)",
  614.         "eat ([%a ]+)",
  615.         "eat",
  616.     },
  617.     ["help"] = {
  618.         "help me",
  619.         "help",
  620.     },
  621.     ["exit"] = {
  622.         "exit",
  623.         "quit",
  624.         "goodbye",
  625.         "good bye",
  626.         "bye",
  627.         "farewell",
  628.     },
  629. }
  630.  
  631. local commands = {}
  632. function doCommand( text )
  633.     if text == "" then
  634.         commands[ "noinput" ]()
  635.         return
  636.     end
  637.    
  638.     for sCommand, t in pairs( tMatches ) do
  639.         for n, sMatch in pairs( t ) do
  640.             local tCaptures = { string.match( text, "^" .. sMatch .. "$" ) }
  641.             if #tCaptures ~= 0 then
  642.                 local fnCommand = commands[ sCommand ]
  643.                 if #tCaptures == 1 and tCaptures[1] == sMatch then
  644.                     fnCommand()
  645.                 else
  646.                     fnCommand( table.unpack( tCaptures ) )
  647.                 end
  648.                 return
  649.             end
  650.         end
  651.     end
  652.     commands[ "badinput" ]()
  653. end
  654.  
  655. function commands.wait()
  656.     print( "Time passes..." )
  657. end
  658.  
  659. function commands.jump()
  660.     print( "Sorry, this game is in a NO JUMPING zone." )
  661. end
  662.  
  663. function commands.fuck()
  664.     print( "A mind is a terrible thing to waste." )
  665. end
  666.  
  667. function commands.sleep()
  668.     print( "Now isn't the time, nor will it ever be while MINECRAFT is up." )
  669. end
  670.  
  671. function commands.point()
  672.     print( "It's rude to point." )
  673. end
  674.  
  675. function commands.die()
  676.     if term.isColour() then
  677.         term.setTextColour( colours.red )
  678.     end
  679.     print( "That was really stupid. You're dead." )
  680.     print( "Score: &e0" )
  681.     term.setTextColour( colours.white )
  682.     print( "Thank you for playing Adventure! As usual, you've been a real pantload." )
  683.     bRunning = false
  684.     return
  685. end
  686.  
  687. function commands.look( _sTarget )
  688.     local room = getRoom( x,y,z )
  689.     if room.dark then
  690.         print( "It is pitch dark." )
  691.         return
  692.     end
  693.  
  694.     if _sTarget == nil then
  695.         -- Look at the world
  696.         if y == 0 then
  697.             print( "You are standing " .. tBiomes[room.nBiome] .. ". " .. tDayCycle[ getTimeOfDay() ])
  698.         else
  699.             print( "You are underground. " .. (next( room.exits ) ~= nil and ("You can travel "..itemize( room.exits )..".") or "") )
  700.         end
  701.         if next( room.items ) ~= nil then
  702.             print( "There is " .. itemize( room.items ) .. " here." )
  703.         end
  704.         if room.trees then
  705.             print( "There are trees here." )
  706.         end
  707.        
  708.     else
  709.         -- Look at stuff
  710.         if room.trees and (_sTarget == "tree" or _sTarget == "trees") then
  711.             print( "The trees look easy to break." )
  712.         elseif _sTarget == "self" or _sTarget == "myself" then
  713.             print( "You don't want to gaze at your own ugly mug." )
  714.         else
  715.             local tItem = nil
  716.             local sItem = findItem( room.items, _sTarget )
  717.             if sItem then
  718.                 tItem = room.items[sItem]
  719.             else
  720.                 sItem = findItem( inventory, _sTarget )
  721.                 if sItem then
  722.                     tItem = inventory[sItem]
  723.                 end
  724.             end
  725.            
  726.             if tItem then
  727.                 print( tItem.desc or ("You see nothing special about "..sItem..".") )
  728.             else
  729.                 print( "You don't see any ".._sTarget.." here." )
  730.             end
  731.         end
  732.     end
  733. end
  734.  
  735. function commands.go( _sDir )
  736.     local room = getRoom( x,y,z )
  737.     if _sDir == nil then
  738.         print( "Go where?" )
  739.         return
  740.     elseif _sDir == "self" then
  741.         print("You enter your inner psyche. Then you quickly scurry out.")
  742.     end
  743.    
  744.     if nGoWest ~= nil then
  745.         if _sDir == "west" then
  746.             nGoWest = nGoWest + 1
  747.             if nGoWest > #tGoWest then
  748.                 nGoWest = 1
  749.             end
  750.             print( tGoWest[ nGoWest ] )
  751.         else
  752.             if nGoWest > 0 or nTurn > 6 then
  753.                 nGoWest = nil
  754.             end
  755.         end
  756.     end
  757.    
  758.     if room.exits[_sDir] == nil then
  759.         local goRejections = {
  760.             "You can't go that way.",
  761.             "It's a real shame you can't go that way, I hear it's quite peaceful.",
  762.             "That isn't a direction you can go in.",
  763.             "There's no way there's a way that way.",
  764.             "You can't move in that direction.",
  765.         }
  766.         print( goRejections[ math.random(1,#goRejections) ] )
  767.         return
  768.     end
  769.    
  770.     if _sDir == "north" then
  771.         z = z + 1
  772.     elseif _sDir == "south" then
  773.         z = z - 1
  774.     elseif _sDir == "east" then
  775.         x = x - 1
  776.     elseif _sDir == "west" then
  777.         x = x + 1
  778.     elseif _sDir == "up" then
  779.         y = y + 1
  780.     elseif _sDir == "down" then
  781.         y = y - 1
  782.     else
  783.         print( "I don't understand that direction." )
  784.         return
  785.     end
  786.    
  787.     nTimeInRoom = 0
  788.     doCommand( "look" )
  789. end
  790.  
  791. function commands.dig( _sDir, _sTool )
  792.     local room = getRoom( x,y,z )
  793.     if _sDir == nil then
  794.         print( "Dig where?" )
  795.         return
  796.     end
  797.    
  798.     local sTool = nil
  799.     local tTool = nil
  800.     if _sTool ~= nil then
  801.         sTool = findItem( inventory, _sTool )
  802.         if not sTool then
  803.             print( "You're not carrying a ".._sTool.."." )
  804.             return
  805.         end
  806.         tTool = inventory[ sTool ]
  807.     end
  808.    
  809.     local room = getRoom( x, y, z )
  810.     local bActuallyDigging = (room.exits[ _sDir ] ~= true)
  811.     if bActuallyDigging then
  812.         if sTool == nil or tTool.toolType ~= "pick" then
  813.             print( "You need to use a pickaxe to dig through stone." )
  814.             return
  815.         end
  816.     end
  817.    
  818.     if _sDir == "north" then
  819.         room.exits["north"] = true
  820.         z = z + 1
  821.         getRoom( x, y, z ).exits["south"] = true
  822.  
  823.     elseif _sDir == "south" then
  824.         room.exits["south"] = true
  825.         z = z - 1
  826.         getRoom( x, y, z ).exits["north"] = true
  827.        
  828.     elseif _sDir == "east" then
  829.         room.exits["east"] = true
  830.         x = x - 1
  831.         getRoom( x, y, z ).exits["west"] = true
  832.        
  833.     elseif _sDir == "west" then
  834.         room.exits["west"] = true
  835.         x = x + 1
  836.         getRoom( x, y, z ).exits["east"] = true
  837.        
  838.     elseif _sDir == "up" then
  839.         if y == 0 then
  840.             print( "You can't dig that way." )
  841.             return
  842.         end
  843.  
  844.         room.exits["up"] = true
  845.         if y == -1 then
  846.             room.items[ "an exit to the surface" ] = items[ "an exit to the surface" ]
  847.         end
  848.         y = y + 1
  849.        
  850.         room = getRoom( x, y, z )
  851.         room.exits["down"] = true
  852.         if y == 0 then
  853.             room.items[ "a cave entrance" ] = items[ "a cave entrance" ]
  854.         end
  855.        
  856.     elseif _sDir == "down" then
  857.         if y <= -3 then
  858.             print( "You hit bedrock." )
  859.             return
  860.         end
  861.  
  862.         room.exits["down"] = true
  863.         if y == 0 then
  864.             room.items[ "a cave entrance" ] = items[ "a cave entrance" ]
  865.         end
  866.         y = y - 1
  867.        
  868.         room = getRoom( x, y, z )
  869.         room.exits["up"] = true
  870.         if y == -1 then
  871.             room.items[ "an exit to the surface" ] = items[ "an exit to the surface" ]
  872.         end
  873.        
  874.     else
  875.         print( "I don't understand that direction." )
  876.         return
  877.     end
  878.    
  879.     --
  880.     if bActuallyDigging then
  881.         if _sDir == "down" and y == -1 or
  882.            _sDir == "up" and y == 0 then
  883.             inventory[ "some dirt" ] = items[ "some dirt" ]
  884.             inventory[ "some stone" ] = items[ "some stone" ]
  885.             print( "You dig ".._sDir.." using "..sTool.." and collect some dirt and stone." )
  886.         else
  887.             inventory[ "some stone" ] = items[ "some stone" ]
  888.             print( "You dig ".._sDir.." using "..sTool.." and collect some stone." )
  889.         end
  890.     end
  891.    
  892.     nTimeInRoom = 0
  893.     doCommand( "look" )
  894. end
  895.  
  896. function commands.inventory()
  897.     print( "You are carrying " .. itemize( inventory ) .. "." )
  898. end
  899.  
  900. function commands.drop( _sItem )
  901.     if _sItem == nil then
  902.         print( "Drop what?" )
  903.         return
  904.     end
  905.    
  906.     local room = getRoom( x,y,z )
  907.     local sItem = findItem( inventory, _sItem )
  908.     if sItem then
  909.         local tItem = inventory[ sItem ]
  910.         if tItem.droppable == false then
  911.             print( "You can't drop that." )
  912.         else
  913.             room.items[ sItem ] = tItem
  914.             inventory[ sItem ] = nil
  915.             print( "Dropped." )
  916.         end
  917.     else
  918.         print( "You don't have a ".._sItem.."." )
  919.     end
  920. end
  921.  
  922. function commands.place( _sItem )
  923.     if _sItem == nil then
  924.         print( "Place what?" )
  925.         return
  926.     end
  927.    
  928.     if _sItem == "torch" or _sItem == "a torch" then
  929.         local room = getRoom( x,y,z )
  930.         if inventory["some torches"] or inventory["a torch"] then
  931.             inventory["a torch"] = nil
  932.             room.items["a torch"] = items["a torch"]
  933.             if room.dark then
  934.                 print( "The cave lights up under the torchflame." )
  935.                 room.dark = false
  936.             elseif y == 0 and not isSunny() then
  937.                 print( "The night gets a little brighter." )
  938.             else
  939.                 print( "Placed." )
  940.             end
  941.         else
  942.             print( "You don't have torches." )
  943.         end
  944.         return
  945.     end
  946.    
  947.     commands.drop( _sItem )
  948. end
  949.  
  950. function commands.take( _sItem )
  951.     if _sItem == nil then
  952.         print( "Take what?" )
  953.         return
  954.     end
  955.  
  956.     local room = getRoom( x,y,z )
  957.     local sItem = findItem( room.items, _sItem )
  958.     if sItem then
  959.         local tItem = room.items[ sItem ]
  960.         if tItem.heavy == true then
  961.             print( "You can't carry "..sItem.."." )
  962.         elseif tItem.ore == true then
  963.             print( "You need to mine this ore." )
  964.         else
  965.             if tItem.infinite ~= true then
  966.                 room.items[ sItem ] = nil
  967.             end
  968.             inventory[ sItem ] = tItem
  969.            
  970.             if inventory["some torches"] and inventory["a torch"] then
  971.                 inventory["a torch"] = nil
  972.             end
  973.             if sItem == "a torch" and y < 0 then
  974.                 room.dark = true
  975.                 print( "The cave plunges into darkness." )
  976.             else
  977.                 print( "Taken." )
  978.             end
  979.         end
  980.     else
  981.         print( "You don't see a ".._sItem.." here." )
  982.     end
  983. end
  984.  
  985. function commands.mine( _sItem, _sTool )
  986.     if _sItem == nil then
  987.         print( "Mine what?" )
  988.         return
  989.     end
  990.     if _sTool == nil then
  991.         print( "Mine ".._sItem.." with what?" )
  992.         return
  993.     end
  994.     commands.cbreak( _sItem, _sTool )
  995. end
  996.  
  997. function commands.attack( _sItem, _sTool )
  998.     if _sItem == nil then
  999.         print( "Attack what?" )
  1000.         return
  1001.     end
  1002.     commands.cbreak( _sItem, _sTool )
  1003. end
  1004.  
  1005. function commands.cbreak( _sItem, _sTool )
  1006.     if _sItem == nil then
  1007.         print( "Break what?" )
  1008.         return
  1009.     end
  1010.    
  1011.     local sTool = nil
  1012.     if _sTool ~= nil then
  1013.         sTool = findItem( inventory, _sTool )
  1014.         if sTool == nil then
  1015.             print( "You're not carrying a ".._sTool.."." )
  1016.             return
  1017.         end
  1018.     end
  1019.  
  1020.     local room = getRoom( x,y,z )
  1021.     if _sItem == "tree" or _sItem == "trees" or _sItem == "a tree" then
  1022.         print( "The tree breaks into blocks of wood, which you pick up." )
  1023.         inventory[ "some wood" ] = items[ "some wood" ]
  1024.         return
  1025.     elseif _sItem == "self" or _sItem == "myself" then
  1026.         if term.isColour() then
  1027.             term.setTextColour( colours.red )
  1028.         end
  1029.         print( "That wasn't very smart. You dead." )
  1030.         print( "Score: &e0" )
  1031.         term.setTextColour( colours.white )
  1032.         print( "Thank you for playing Adventure! As usual, you've been a real pantload." )
  1033.         bRunning = false
  1034.         return
  1035.     end
  1036.    
  1037.     local sItem = findItem( room.items, _sItem )
  1038.     if sItem then
  1039.         local tItem = room.items[ sItem ]
  1040.         if tItem.ore == true then
  1041.             -- Breaking ore
  1042.             if not sTool then
  1043.                 print( "You need a tool to break this ore." )
  1044.                 return
  1045.             end
  1046.             local tTool = inventory[ sTool ]
  1047.             if tTool.tool then
  1048.                 if tTool.toolLevel < tItem.toolLevel then
  1049.                     print( sTool .." is not strong enough to break this ore." )
  1050.                 elseif tTool.toolType ~= tItem.toolType then
  1051.                     print( "You need a different kind of tool to break this ore." )
  1052.                 else
  1053.                     print( "The ore breaks, dropping "..sItem..", which you pick up." )
  1054.                     inventory[ sItem ] = items[ sItem ]
  1055.                     if tItem.infinite ~= true then
  1056.                         room.items[ sItem ] = nil
  1057.                     end
  1058.                 end
  1059.             else
  1060.                 print( "You can't break "..sItem.." with "..sTool..".")
  1061.             end
  1062.            
  1063.         elseif tItem.creature == true then
  1064.             -- Fighting monsters (or pigs)
  1065.             local toolLevel = 0
  1066.             local tTool = nil
  1067.             if sTool then
  1068.                 tTool = inventory[ sTool ]
  1069.                 if tTool.toolType == "sword" then
  1070.                     toolLevel = tTool.toolLevel
  1071.                 end
  1072.             end
  1073.                        
  1074.             local tChances = { 0.2, 0.4, 0.55, 0.8, 1 }
  1075.             if math.random() <= tChances[ toolLevel + 1 ] then
  1076.                 room.items[ sItem ] = nil
  1077.                 print( "The "..tItem.aliases[1].." dies." )
  1078.    
  1079.                 if tItem.drops then
  1080.                     for n, sDrop in pairs( tItem.drops ) do
  1081.                         if not room.items[sDrop] then
  1082.                             print( "The "..tItem.aliases[1].." dropped "..sDrop.."." )
  1083.                             room.items[sDrop] = items[sDrop]
  1084.                         end
  1085.                     end
  1086.                 end
  1087.                
  1088.                 if tItem.monster then
  1089.                     room.nMonsters = room.nMonsters - 1
  1090.                 end
  1091.             else
  1092.                 print( "The "..tItem.aliases[1].." is injured by your blow." )
  1093.             end
  1094.            
  1095.             if tItem.hitDrops then
  1096.                 for n, sDrop in pairs( tItem.hitDrops ) do
  1097.                     if not room.items[sDrop] then
  1098.                         print( "The "..tItem.aliases[1].." dropped "..sDrop.."." )
  1099.                         room.items[sDrop] = items[sDrop]
  1100.                     end
  1101.                 end
  1102.             end
  1103.        
  1104.         else
  1105.             print( "You can't break "..sItem.."." )
  1106.         end
  1107.     else
  1108.         print( "You don't see a ".._sItem.." here." )
  1109.     end
  1110. end
  1111.  
  1112. function commands.craft( _sItem )
  1113.     if _sItem == nil then
  1114.         print( "Craft what?" )
  1115.         return
  1116.     end
  1117.    
  1118.     if _sItem == "computer" or _sItem == "a computer" then
  1119.         print( "By creating a computer in a computer in a computer, you tear a hole in the spacetime continuum from which no mortal can escape." )
  1120.         if term.isColour() then
  1121.             term.setTextColour( colours.red )
  1122.         end
  1123.         print( "You have ceased to exist." )
  1124.         print( "Score: &e0" )
  1125.         term.setTextColour( colours.white )
  1126.         print( "Thank you for playing Adventure! As usual, you've been a real pantload." )
  1127.         bRunning = false
  1128.         return
  1129.     end
  1130.    
  1131.     local room = getRoom( x,y,z )
  1132.     local sItem = findItem( items, _sItem )
  1133.     local tRecipe = (sItem and tRecipes[ sItem ]) or nil
  1134.     if tRecipe then
  1135.         for n,sReq in ipairs( tRecipe ) do
  1136.             if inventory[sReq] == nil then
  1137.                 print( "You don't have the items you need to craft "..sItem.."." )
  1138.                 return
  1139.             end
  1140.         end
  1141.        
  1142.         for n,sReq in ipairs( tRecipe ) do
  1143.             inventory[sReq] = nil
  1144.         end
  1145.         inventory[ sItem ] = items[ sItem ]
  1146.         if inventory["some torches"] and inventory["a torch"] then
  1147.             inventory["a torch"] = nil
  1148.         end
  1149.         print( "Crafted." )
  1150.     else
  1151.         print( "You don't know how to make "..(sItem or _sItem).."." )
  1152.     end
  1153. end
  1154.  
  1155. function commands.build( _sThing, _sMaterial )
  1156.     if _sThing == nil then
  1157.         print( "Build what?" )
  1158.         return
  1159.     elseif _sThing == "self" then
  1160.         print( "Building yourself up sounds like a good idea, but this isn't how you do it." )
  1161.         return
  1162.     end
  1163.     local sMaterial = nil
  1164.     if _sMaterial == nil then
  1165.         for sItem, tItem in pairs( inventory ) do
  1166.             if tItem.material then
  1167.                 sMaterial = sItem
  1168.                 break
  1169.             end
  1170.         end
  1171.         if sMaterial == nil then
  1172.             print( "You don't have any building materials." )
  1173.             return
  1174.         end
  1175.     else
  1176.         sMaterial = findItem( inventory, _sMaterial )
  1177.         if not sMaterial then
  1178.             print( "You don't have any ".._sMaterial )
  1179.             return
  1180.         end
  1181.        
  1182.         if inventory[sMaterial].material ~= true then
  1183.             print( sMaterial.." is not a good building material." )
  1184.             return
  1185.         end
  1186.     end
  1187.    
  1188.     local alias = nil
  1189.     if string.sub(_sThing, 1, 1) == "a" then
  1190.         alias = string.match( _sThing, "a ([%a ]+)" )
  1191.     end
  1192.    
  1193.     local room = getRoom( x,y,z )
  1194.     inventory[sMaterial] = nil
  1195.     room.items[ _sThing ] = {
  1196.         heavy = true,
  1197.         aliases = { alias },
  1198.         desc = "As you look at your creation (made from "..sMaterial.."), you feel a swelling sense of pride.",
  1199.     }
  1200.  
  1201.     print( "Your construction is complete." )
  1202. end
  1203.  
  1204. function commands.help()
  1205.     local sText =
  1206.         "Welcome to adventure, the greatest text adventure game on CraftOS. " ..
  1207.         "To get around the world, type actions, and the adventure will " ..
  1208.         "be read back to you. The actions availiable to you are go, look, inspect, inventory, " ..
  1209.         "take, drop, place, punch, attack, mine, dig, craft, build, eat and exit."
  1210.     print( sText )
  1211. end
  1212.  
  1213. function commands.eat( _sItem )
  1214.     if _sItem == nil then
  1215.         print( "Eat what?" )
  1216.         return
  1217.     end
  1218.  
  1219.     local sItem = findItem( inventory, _sItem )
  1220.     if not sItem then
  1221.         print( "You don't have any ".._sItem.."." )
  1222.         return
  1223.     end
  1224.    
  1225.     local tItem = inventory[sItem]
  1226.     if tItem.food then
  1227.         print( "That was delicious!" )
  1228.         inventory[sItem] = nil
  1229.        
  1230.         if bInjured then
  1231.             print( "You are no longer injured." )
  1232.             bInjured = false
  1233.         end
  1234.     else
  1235.         print( "You can't eat "..sItem.."." )
  1236.     end
  1237. end
  1238.  
  1239. function commands.exit()
  1240.     bRunning = false
  1241. end
  1242.  
  1243. function commands.badinput()
  1244.     local tResponses = {
  1245.         "I don't understand.",
  1246.         "I don't understand you.",
  1247.         "You can't do that.",
  1248.         "Nope.",
  1249.         "Huh?",
  1250.         "Say again?",
  1251.         "That's crazy talk.",
  1252.         "Speak clearly.",
  1253.         "I'll think about it.",
  1254.         "Let me get back to you on that one.",
  1255.         "That doesn't make any sense.",
  1256.         _USERNAME..", have you been at that marijuana gin again?",
  1257.         "You what?",
  1258.     }
  1259.     print( tResponses[ math.random(1,#tResponses) ] )
  1260. end
  1261.  
  1262. function commands.noinput()
  1263.     local tResponses = {
  1264.         "Speak up.",
  1265.         "Enunciate.",
  1266.         "Project your voice.",
  1267.         "Don't be shy.",
  1268.         "Use your words.",
  1269.         "Say something rather than nothing.",
  1270.     }
  1271.     print( tResponses[ math.random(1,#tResponses) ] )
  1272. end
  1273.  
  1274. local function simulate()
  1275.     local bNewMonstersThisRoom = false
  1276.    
  1277.     -- Spawn monsters in nearby rooms
  1278.     for sx = -2,2 do
  1279.         for sy = -1,1 do
  1280.             for sz = -2,2 do
  1281.                 local h = y + sy
  1282.                 if h >= -3 and h <= 0 then
  1283.                     local room = getRoom( x + sx, h, z + sz )
  1284.                    
  1285.                     -- Spawn monsters
  1286.                     if room.nMonsters < 2 and
  1287.                        ((h == 0 and not isSunny() and not room.items["a torch"]) or room.dark) and
  1288.                        math.random(1,6) == 1 then
  1289.                        
  1290.                         local sMonster = tMonsters[ math.random(1,#tMonsters) ]
  1291.                         if room.items[ sMonster ] == nil then
  1292.                             room.items[ sMonster ] = items[ sMonster ]
  1293.                             room.nMonsters = room.nMonsters + 1
  1294.                            
  1295.                             if sx == 0 and sy == 0 and sz == 0 and not room.dark then
  1296.                                 print( "From the shadows, "..sMonster.." appears." )
  1297.                                 bNewMonstersThisRoom = true
  1298.                             end
  1299.                         end
  1300.                     end
  1301.                    
  1302.                     -- Burn monsters
  1303.                     if h == 0 and isSunny() then
  1304.                         for n,sMonster in ipairs( tMonsters ) do
  1305.                             if room.items[sMonster] and items[sMonster].nocturnal then
  1306.                                 room.items[sMonster] = nil
  1307.                                 if sx == 0 and sy == 0 and sz == 0 and not room.dark then
  1308.                                     print( "With the sun high in the sky, the "..items[sMonster].aliases[1].." bursts into flame and dies." )
  1309.                                 end
  1310.                                 room.nMonsters = room.nMonsters - 1
  1311.                             end
  1312.                         end
  1313.                     end
  1314.                 end
  1315.             end
  1316.         end
  1317.     end
  1318.  
  1319.     -- Make monsters attack
  1320.     local room = getRoom( x, y, z )
  1321.     if nTimeInRoom >= 2 and not bNewMonstersThisRoom then
  1322.         for n,sMonster in ipairs( tMonsters ) do
  1323.             if room.items[sMonster] then
  1324.                 if math.random(1,4) == 1 and
  1325.                    not (y == 0 and isSunny() and (sMonster == "a spider")) then
  1326.                     if sMonster == "a creeper" then
  1327.                         if room.dark then
  1328.                             print( "A creeper explodes." )
  1329.                         else
  1330.                             print( "The creeper explodes." )
  1331.                         end
  1332.                         room.items[sMonster] = nil
  1333.                         room.nMonsters = room.nMonsters - 1
  1334.                     else
  1335.                         if room.dark then
  1336.                             print( "A "..items[sMonster].aliases[1].." attacks you." )
  1337.                         else
  1338.                             print( "The "..items[sMonster].aliases[1].." attacks you." )
  1339.                         end
  1340.                     end
  1341.                    
  1342.                     if bInjured then
  1343.                         if term.isColour() then
  1344.                             term.setTextColour( colours.red )
  1345.                         end
  1346.                         print( "You have died." )
  1347.                         print( "Score: &e0" )
  1348.                         term.setTextColour( colours.white )
  1349.                         print( "Thank you for playing Adventure! As usual, you've been a real pantload." )
  1350.                         bRunning = false
  1351.                         return
  1352.                     else
  1353.                         bInjured = true
  1354.                     end
  1355.                    
  1356.                     break
  1357.                 end
  1358.             end
  1359.         end
  1360.     end
  1361.    
  1362.     -- Always print this
  1363.     if bInjured then
  1364.         if term.isColour() then
  1365.             term.setTextColour( colours.red )
  1366.         end
  1367.         print( "You are injured." )
  1368.         term.setTextColour( colours.white )
  1369.     end
  1370.    
  1371.     -- Advance time
  1372.     nTurn = nTurn + 1
  1373.     nTimeInRoom = nTimeInRoom + 1
  1374. end
  1375.  
  1376. doCommand( "look" )
  1377. simulate()
  1378.  
  1379. local tCommandHistory = {}
  1380. while bRunning do
  1381.     term.setTextColour( colours.white )
  1382.        
  1383.     local evt, sRawLine = os.pullEvent("followup_command_".._USERNAME)
  1384.     table.insert( tCommandHistory, sRawLine )
  1385.    
  1386.     local sLine = nil
  1387.     for match in string.gmatch(sRawLine, "%a+") do
  1388.         if sLine then
  1389.             sLine = sLine .. " " .. string.lower(match)
  1390.         else
  1391.             sLine = string.lower(match)
  1392.         end
  1393.     end
  1394.    
  1395.     doCommand( sLine or "" )
  1396.     if bRunning then
  1397.         simulate()
  1398.     end
  1399. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement