Guest User

adventure

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