Advertisement
ecco7777

CC Chat Adventure

Sep 24th, 2016
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 32.73 KB | None | 0 0
  1. c=peripheral.wrap("top")
  2.  
  3. local tBiomes = {
  4.     "in a forest",
  5.     "in a pine forest",
  6.     "knee deep in a swamp",
  7.    
  8.     "in a mountain range",
  9.     "in a desert",
  10.     "in a grassy plain",
  11.     "in frozen tundra",
  12.    
  13.     --"in the ocean",
  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 = "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 bones connected to the chest bone, the chest bones connected to the arm bone, the arm bones 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.",
  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 mudhut?",
  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.     ["look"] = {
  501.         "look at the ([%a ]+)",
  502.         "look at ([%a ]+)",
  503.         "look",
  504.         "inspect ([%a ]+)",
  505.         "inspect the ([%a ]+)",
  506.         "inspect",
  507.     },
  508.     ["inventory"] = {
  509.         "check self",
  510.         "check inventory",
  511.         "inventory",
  512.         "i",
  513.     },
  514.     ["go"] = {
  515.         "go (%a+)",
  516.         "travel (%a+)",
  517.         "walk (%a+)",
  518.         "run (%a+)",
  519.         "go",
  520.     },
  521.     ["dig"] = {
  522.         "dig (%a+) using ([%a ]+)",
  523.         "dig (%a+) with ([%a ]+)",
  524.         "dig (%a+)",
  525.         "dig",
  526.     },
  527.     ["take"] = {
  528.         "pick up the ([%a ]+)",
  529.         "pick up ([%a ]+)",
  530.         "pickup ([%a ]+)",
  531.         "take the ([%a ]+)",
  532.         "take ([%a ]+)",
  533.         "take",
  534.     },
  535.     ["drop"] = {
  536.         "put down the ([%a ]+)",
  537.         "put down ([%a ]+)",
  538.         "drop the ([%a ]+)",
  539.         "drop ([%a ]+)",
  540.         "drop",
  541.     },
  542.     ["place"] = {
  543.         "place the ([%a ]+)",
  544.         "place ([%a ]+)",
  545.         "place",
  546.     },
  547.     ["cbreak"] = {
  548.         "punch the ([%a ]+)",
  549.         "punch ([%a ]+)",
  550.         "punch",
  551.         "break the ([%a ]+) with the ([%a ]+)",
  552.         "break ([%a ]+) with ([%a ]+) ",
  553.         "break the ([%a ]+)",
  554.         "break ([%a ]+)",
  555.         "break",
  556.     },
  557.     ["mine"] = {
  558.         "mine the ([%a ]+) with the ([%a ]+)",
  559.         "mine ([%a ]+) with ([%a ]+)",
  560.         "mine ([%a ]+)",
  561.         "mine",
  562.     },
  563.     ["attack"] = {
  564.         "attack the ([%a ]+) with the ([%a ]+)",
  565.         "attack ([%a ]+) with ([%a ]+)",
  566.         "attack ([%a ]+)",
  567.         "attack",
  568.         "kill the ([%a ]+) with the ([%a ]+)",
  569.         "kill ([%a ]+) with ([%a ]+)",
  570.         "kill ([%a ]+)",
  571.         "kill",
  572.         "hit the ([%a ]+) with the ([%a ]+)",
  573.         "hit ([%a ]+) with ([%a ]+)",
  574.         "hit ([%a ]+)",
  575.         "hit",
  576.     },
  577.     ["craft"] = {
  578.         "craft a ([%a ]+)",
  579.         "craft some ([%a ]+)",
  580.         "craft ([%a ]+)",
  581.         "craft",
  582.         "make a ([%a ]+)",
  583.         "make some ([%a ]+)",
  584.         "make ([%a ]+)",
  585.         "make",
  586.     },
  587.     ["build"] = {
  588.         "build ([%a ]+) out of ([%a ]+)",
  589.         "build ([%a ]+) from ([%a ]+)",
  590.         "build ([%a ]+)",
  591.         "build",
  592.     },
  593.     ["eat"] = {
  594.         "eat a ([%a ]+)",
  595.         "eat the ([%a ]+)",
  596.         "eat ([%a ]+)",
  597.         "eat",
  598.     },
  599.     ["help"] = {
  600.         "help me",
  601.         "help",
  602.     },
  603.     ["exit"] = {
  604.         "exit",
  605.         "quit",
  606.         "goodbye",
  607.         "good bye",
  608.         "bye",
  609.         "farewell",
  610.     },
  611. }
  612.  
  613. local commands = {}
  614. function doCommand( text )
  615.     if text == "" then
  616.         commands[ "noinput" ]()
  617.         return
  618.     end
  619.    
  620.     for sCommand, t in pairs( tMatches ) do
  621.         for n, sMatch in pairs( t ) do
  622.             local tCaptures = { string.match( text, "^" .. sMatch .. "$" ) }
  623.             if #tCaptures ~= 0 then
  624.                 local fnCommand = commands[ sCommand ]
  625.                 if #tCaptures == 1 and tCaptures[1] == sMatch then
  626.                     fnCommand()
  627.                 else
  628.                     fnCommand( unpack( tCaptures ) )
  629.                 end
  630.                 return
  631.             end
  632.         end
  633.     end
  634.     commands[ "badinput" ]()
  635. end
  636.  
  637. function commands.wait()
  638.     sleep(0.5)  c.say( "Time passes..." )
  639. end
  640.  
  641. function commands.look( _sTarget )
  642.     local room = getRoom( x,y,z )
  643.     if room.dark then
  644.         sleep(0.5)  c.say( "It is pitch dark." )
  645.         return
  646.     end
  647.  
  648.     if _sTarget == nil then
  649.         -- Look at the world
  650.         if y == 0 then
  651.             sleep(0.5)  c.say( "You are standing " .. tBiomes[room.nBiome] .. ". " )
  652.             sleep(0.5)  c.say( tDayCycle[ getTimeOfDay() ] )
  653.         else
  654.             sleep(0.5)  c.say( "You are underground. " )
  655.             if next( room.exits ) ~= nil then
  656.                 sleep(0.5)  c.say( "You can travel "..itemize( room.exits ).."." )
  657.             else
  658.                 sleep(0.5)  c.say()
  659.             end
  660.         end
  661.         if next( room.items ) ~= nil then
  662.             sleep(0.5)  c.say( "There is " .. itemize( room.items ) .. " here." )
  663.         end
  664.         if room.trees then
  665.             sleep(0.5)  c.say( "There are trees here." )
  666.         end
  667.        
  668.     else
  669.         -- Look at stuff
  670.         if room.trees and (_sTarget == "tree" or _sTarget == "trees") then
  671.             sleep(0.5)  c.say( "The trees look easy to break." )
  672.         elseif _sTarget == "self" or _sTarget == "myself" then
  673.             sleep(0.5)  c.say( "Very handsome." )
  674.         else
  675.             local tItem = nil
  676.             local sItem = findItem( room.items, _sTarget )
  677.             if sItem then
  678.                 tItem = room.items[sItem]
  679.             else
  680.                 sItem = findItem( inventory, _sTarget )
  681.                 if sItem then
  682.                     tItem = inventory[sItem]
  683.                 end
  684.             end
  685.            
  686.             if tItem then
  687.                 sleep(0.5)  c.say( tItem.desc or ("You see nothing special about "..sItem..".") )
  688.             else
  689.                 sleep(0.5)  c.say( "You don't see any ".._sTarget.." here." )
  690.             end
  691.         end
  692.     end
  693. end
  694.  
  695. function commands.go( _sDir )
  696.     local room = getRoom( x,y,z )
  697.     if _sDir == nil then
  698.         sleep(0.5)  c.say( "Go where?" )
  699.         return
  700.     end
  701.    
  702.     if nGoWest ~= nil then
  703.         if _sDir == "west" then
  704.             nGoWest = nGoWest + 1
  705.             if nGoWest > #tGoWest then
  706.                 nGoWest = 1
  707.             end
  708.             sleep(0.5)  c.say( tGoWest[ nGoWest ] )
  709.         else
  710.             if nGoWest > 0 or nTurn > 6 then
  711.                 nGoWest = nil
  712.             end
  713.         end
  714.     end
  715.    
  716.     if room.exits[_sDir] == nil then
  717.         sleep(0.5)  c.say( "You can't go that way." )
  718.         return
  719.     end
  720.    
  721.     if _sDir == "north" then
  722.         z = z + 1
  723.     elseif _sDir == "south" then
  724.         z = z - 1
  725.     elseif _sDir == "east" then
  726.         x = x - 1
  727.     elseif _sDir == "west" then
  728.         x = x + 1
  729.     elseif _sDir == "up" then
  730.         y = y + 1
  731.     elseif _sDir == "down" then
  732.         y = y - 1
  733.     else
  734.         sleep(0.5)  c.say( "I don't understand that direction." )
  735.         return
  736.     end
  737.    
  738.     nTimeInRoom = 0
  739.     doCommand( "look" )
  740. end
  741.  
  742. function commands.dig( _sDir, _sTool )
  743.     local room = getRoom( x,y,z )
  744.     if _sDir == nil then
  745.         sleep(0.5)  c.say( "Dig where?" )
  746.         return
  747.     end
  748.    
  749.     local sTool = nil
  750.     local tTool = nil
  751.     if _sTool ~= nil then
  752.         sTool = findItem( inventory, _sTool )
  753.         if not sTool then
  754.             sleep(0.5)  c.say( "You're not carrying a ".._sTool.."." )
  755.             return
  756.         end
  757.         tTool = inventory[ sTool ]
  758.     end
  759.    
  760.     local room = getRoom( x, y, z )
  761.     local bActuallyDigging = (room.exits[ _sDir ] ~= true)
  762.     if bActuallyDigging then
  763.         if sTool == nil or tTool.toolType ~= "pick" then
  764.             sleep(0.5)  c.say( "You need to use a pickaxe to dig through stone." )
  765.             return
  766.         end
  767.     end
  768.    
  769.     if _sDir == "north" then
  770.         room.exits["north"] = true
  771.         z = z + 1
  772.         getRoom( x, y, z ).exits["south"] = true
  773.  
  774.     elseif _sDir == "south" then
  775.         room.exits["south"] = true
  776.         z = z - 1
  777.         getRoom( x, y, z ).exits["north"] = true
  778.        
  779.     elseif _sDir == "east" then
  780.         room.exits["east"] = true
  781.         x = x - 1
  782.         getRoom( x, y, z ).exits["west"] = true
  783.        
  784.     elseif _sDir == "west" then
  785.         room.exits["west"] = true
  786.         x = x + 1
  787.         getRoom( x, y, z ).exits["east"] = true
  788.        
  789.     elseif _sDir == "up" then
  790.         if y == 0 then
  791.             sleep(0.5)  c.say( "You can't dig that way." )
  792.             return
  793.         end
  794.  
  795.         room.exits["up"] = true
  796.         if y == -1 then
  797.             room.items[ "an exit to the surface" ] = items[ "an exit to the surface" ]
  798.         end
  799.         y = y + 1
  800.        
  801.         room = getRoom( x, y, z )
  802.         room.exits["down"] = true
  803.         if y == 0 then
  804.             room.items[ "a cave entrance" ] = items[ "a cave entrance" ]
  805.         end
  806.        
  807.     elseif _sDir == "down" then
  808.         if y <= -3 then
  809.             sleep(0.5)  c.say( "You hit bedrock." )
  810.             return
  811.         end
  812.  
  813.         room.exits["down"] = true
  814.         if y == 0 then
  815.             room.items[ "a cave entrance" ] = items[ "a cave entrance" ]
  816.         end
  817.         y = y - 1
  818.        
  819.         room = getRoom( x, y, z )
  820.         room.exits["up"] = true
  821.         if y == -1 then
  822.             room.items[ "an exit to the surface" ] = items[ "an exit to the surface" ]
  823.         end
  824.        
  825.     else
  826.         sleep(0.5)  c.say( "I don't understand that direction." )
  827.         return
  828.     end
  829.    
  830.     --
  831.     if bActuallyDigging then
  832.         if _sDir == "down" and y == -1 or
  833.            _sDir == "up" and y == 0 then
  834.             inventory[ "some dirt" ] = items[ "some dirt" ]
  835.             inventory[ "some stone" ] = items[ "some stone" ]
  836.             sleep(0.5)  c.say( "You dig ".._sDir.." using "..sTool.." and collect some dirt and stone." )
  837.         else
  838.             inventory[ "some stone" ] = items[ "some stone" ]
  839.             sleep(0.5)  c.say( "You dig ".._sDir.." using "..sTool.." and collect some stone." )
  840.         end
  841.     end
  842.    
  843.     nTimeInRoom = 0
  844.     doCommand( "look" )
  845. end
  846.  
  847. function commands.inventory()
  848.     sleep(0.5)  c.say( "You are carrying " .. itemize( inventory ) .. "." )
  849. end
  850.  
  851. function commands.drop( _sItem )
  852.     if _sItem == nil then
  853.         sleep(0.5)  c.say( "Drop what?" )
  854.         return
  855.     end
  856.    
  857.     local room = getRoom( x,y,z )
  858.     local sItem = findItem( inventory, _sItem )
  859.     if sItem then
  860.         local tItem = inventory[ sItem ]
  861.         if tItem.droppable == false then
  862.             sleep(0.5)  c.say( "You can't drop that." )
  863.         else
  864.             room.items[ sItem ] = tItem
  865.             inventory[ sItem ] = nil
  866.             sleep(0.5)  c.say( "Dropped." )
  867.         end
  868.     else
  869.         sleep(0.5)  c.say( "You don't have a ".._sItem.."." )
  870.     end
  871. end
  872.  
  873. function commands.place( _sItem )
  874.     if _sItem == nil then
  875.         sleep(0.5)  c.say( "Place what?" )
  876.         return
  877.     end
  878.    
  879.     if _sItem == "torch" or _sItem == "a torch" then
  880.         local room = getRoom( x,y,z )
  881.         if inventory["some torches"] or inventory["a torch"] then
  882.             inventory["a torch"] = nil
  883.             room.items["a torch"] = items["a torch"]
  884.             if room.dark then
  885.                 sleep(0.5)  c.say( "The cave lights up under the torchflame." )
  886.                 room.dark = false
  887.             elseif y == 0 and not isSunny() then
  888.                 sleep(0.5)  c.say( "The night gets a little brighter." )
  889.             else
  890.                 sleep(0.5)  c.say( "Placed." )
  891.             end
  892.         else
  893.             sleep(0.5)  c.say( "You don't have torches." )
  894.         end
  895.         return
  896.     end
  897.    
  898.     commands.drop( _sItem )
  899. end
  900.  
  901. function commands.take( _sItem )
  902.     if _sItem == nil then
  903.         sleep(0.5)  c.say( "Take what?" )
  904.         return
  905.     end
  906.  
  907.     local room = getRoom( x,y,z )
  908.     local sItem = findItem( room.items, _sItem )
  909.     if sItem then
  910.         local tItem = room.items[ sItem ]
  911.         if tItem.heavy == true then
  912.             sleep(0.5)  c.say( "You can't carry "..sItem.."." )
  913.         elseif tItem.ore == true then
  914.             sleep(0.5)  c.say( "You need to mine this ore." )
  915.         else
  916.             if tItem.infinite ~= true then
  917.                 room.items[ sItem ] = nil
  918.             end
  919.             inventory[ sItem ] = tItem
  920.            
  921.             if inventory["some torches"] and inventory["a torch"] then
  922.                 inventory["a torch"] = nil
  923.             end
  924.             if sItem == "a torch" and y < 0 then
  925.                 room.dark = true
  926.                 sleep(0.5)  c.say( "The cave plunges into darkness." )
  927.             else
  928.                 sleep(0.5)  c.say( "Taken." )
  929.             end
  930.         end
  931.     else
  932.         sleep(0.5)  c.say( "You don't see a ".._sItem.." here." )
  933.     end
  934. end
  935.  
  936. function commands.mine( _sItem, _sTool )
  937.     if _sItem == nil then
  938.         sleep(0.5)  c.say( "Mine what?" )
  939.         return
  940.     end
  941.     if _sTool == nil then
  942.         sleep(0.5)  c.say( "Mine ".._sItem.." with what?" )
  943.         return
  944.     end
  945.     commands.cbreak( _sItem, _sTool )
  946. end
  947.  
  948. function commands.attack( _sItem, _sTool )
  949.     if _sItem == nil then
  950.         sleep(0.5)  c.say( "Attack what?" )
  951.         return
  952.     end
  953.     commands.cbreak( _sItem, _sTool )
  954. end
  955.  
  956. function commands.cbreak( _sItem, _sTool )
  957.     if _sItem == nil then
  958.         sleep(0.5)  c.say( "Break what?" )
  959.         return
  960.     end
  961.    
  962.     local sTool = nil
  963.     if _sTool ~= nil then
  964.         sTool = findItem( inventory, _sTool )
  965.         if sTool == nil then
  966.             sleep(0.5)  c.say( "You're not carrying a ".._sTool.."." )
  967.             return
  968.         end
  969.     end
  970.  
  971.     local room = getRoom( x,y,z )
  972.     if _sItem == "tree" or _sItem == "trees" or _sItem == "a tree" then
  973.         sleep(0.5)  c.say( "The tree breaks into blocks of wood, which you pick up." )
  974.         inventory[ "some wood" ] = items[ "some wood" ]
  975.         return
  976.     elseif _sItem == "self" or _sItem == "myself" then
  977.         if term.isColour() then
  978.             term.setTextColour( colours.red )
  979.         end
  980.         sleep(0.5)  c.say( "You have died." )
  981.         sleep(0.5)  c.say( "Score: &e0" )
  982.         term.setTextColour( colours.white )
  983.         bRunning = false
  984.         return
  985.     end
  986.    
  987.     local sItem = findItem( room.items, _sItem )
  988.     if sItem then
  989.         local tItem = room.items[ sItem ]
  990.         if tItem.ore == true then
  991.             -- Breaking ore
  992.             if not sTool then
  993.                 sleep(0.5)  c.say( "You need a tool to break this ore." )
  994.                 return
  995.             end
  996.             local tTool = inventory[ sTool ]
  997.             if tTool.tool then
  998.                 if tTool.toolLevel < tItem.toolLevel then
  999.                     sleep(0.5)  c.say( sTool .." is not strong enough to break this ore." )
  1000.                 elseif tTool.toolType ~= tItem.toolType then
  1001.                     sleep(0.5)  c.say( "You need a different kind of tool to break this ore." )
  1002.                 else
  1003.                     sleep(0.5)  c.say( "The ore breaks, dropping "..sItem..", which you pick up." )
  1004.                     inventory[ sItem ] = items[ sItem ]
  1005.                     if tItem.infinite ~= true then
  1006.                         room.items[ sItem ] = nil
  1007.                     end
  1008.                 end
  1009.             else
  1010.                 sleep(0.5)  c.say( "You can't break "..sItem.." with "..sTool..".")
  1011.             end
  1012.            
  1013.         elseif tItem.creature == true then
  1014.             -- Fighting monsters (or pigs)
  1015.             local toolLevel = 0
  1016.             local tTool = nil
  1017.             if sTool then
  1018.                 tTool = inventory[ sTool ]
  1019.                 if tTool.toolType == "sword" then
  1020.                     toolLevel = tTool.toolLevel
  1021.                 end
  1022.             end
  1023.                        
  1024.             local tChances = { 0.2, 0.4, 0.55, 0.8, 1 }
  1025.             if math.random() <= tChances[ toolLevel + 1 ] then
  1026.                 room.items[ sItem ] = nil
  1027.                 sleep(0.5)  c.say( "The "..tItem.aliases[1].." dies." )
  1028.    
  1029.                 if tItem.drops then
  1030.                     for n, sDrop in pairs( tItem.drops ) do
  1031.                         if not room.items[sDrop] then
  1032.                             sleep(0.5)  c.say( "The "..tItem.aliases[1].." dropped "..sDrop.."." )
  1033.                             room.items[sDrop] = items[sDrop]
  1034.                         end
  1035.                     end
  1036.                 end
  1037.                
  1038.                 if tItem.monster then
  1039.                     room.nMonsters = room.nMonsters - 1
  1040.                 end
  1041.             else
  1042.                 sleep(0.5)  c.say( "The "..tItem.aliases[1].." is injured by your blow." )
  1043.             end
  1044.            
  1045.             if tItem.hitDrops then
  1046.                 for n, sDrop in pairs( tItem.hitDrops ) do
  1047.                     if not room.items[sDrop] then
  1048.                         sleep(0.5)  c.say( "The "..tItem.aliases[1].." dropped "..sDrop.."." )
  1049.                         room.items[sDrop] = items[sDrop]
  1050.                     end
  1051.                 end
  1052.             end
  1053.        
  1054.         else
  1055.             sleep(0.5)  c.say( "You can't break "..sItem.."." )
  1056.         end
  1057.     else
  1058.         sleep(0.5)  c.say( "You don't see a ".._sItem.." here." )
  1059.     end
  1060. end
  1061.  
  1062. function commands.craft( _sItem )
  1063.     if _sItem == nil then
  1064.         sleep(0.5)  c.say( "Craft what?" )
  1065.         return
  1066.     end
  1067.    
  1068.     if _sItem == "computer" or _sItem == "a computer" then
  1069.         sleep(0.5)  c.say( "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." )
  1070.         if term.isColour() then
  1071.             term.setTextColour( colours.red )
  1072.         end
  1073.         sleep(0.5)  c.say( "You have died." )
  1074.         sleep(0.5)  c.say( "Score: &e0" )
  1075.         term.setTextColour( colours.white )
  1076.         bRunning = false
  1077.         return
  1078.     end
  1079.    
  1080.     local room = getRoom( x,y,z )
  1081.     local sItem = findItem( items, _sItem )
  1082.     local tRecipe = (sItem and tRecipes[ sItem ]) or nil
  1083.     if tRecipe then
  1084.         for n,sReq in ipairs( tRecipe ) do
  1085.             if inventory[sReq] == nil then
  1086.                 sleep(0.5)  c.say( "You don't have the items you need to craft "..sItem.."." )
  1087.                 return
  1088.             end
  1089.         end
  1090.        
  1091.         for n,sReq in ipairs( tRecipe ) do
  1092.             inventory[sReq] = nil
  1093.         end
  1094.         inventory[ sItem ] = items[ sItem ]
  1095.         if inventory["some torches"] and inventory["a torch"] then
  1096.             inventory["a torch"] = nil
  1097.         end
  1098.         sleep(0.5)  c.say( "Crafted." )
  1099.     else
  1100.         sleep(0.5)  c.say( "You don't know how to make "..(sItem or _sItem).."." )
  1101.     end
  1102. end
  1103.  
  1104. function commands.build( _sThing, _sMaterial )
  1105.     if _sThing == nil then
  1106.         sleep(0.5)  c.say( "Build what?" )
  1107.         return
  1108.     end
  1109.        
  1110.     local sMaterial = nil
  1111.     if _sMaterial == nil then
  1112.         for sItem, tItem in pairs( inventory ) do
  1113.             if tItem.material then
  1114.                 sMaterial = sItem
  1115.                 break
  1116.             end
  1117.         end
  1118.         if sMaterial == nil then
  1119.             sleep(0.5)  c.say( "You don't have any building materials." )
  1120.             return
  1121.         end
  1122.     else
  1123.         sMaterial = findItem( inventory, _sMaterial )
  1124.         if not sMaterial then
  1125.             sleep(0.5)  c.say( "You don't have any ".._sMaterial )
  1126.             return
  1127.         end
  1128.        
  1129.         if inventory[sMaterial].material ~= true then
  1130.             sleep(0.5)  c.say( sMaterial.." is not a good building material." )
  1131.             return
  1132.         end
  1133.     end
  1134.    
  1135.     local alias = nil
  1136.     if string.sub(_sThing, 1, 1) == "a" then
  1137.         alias = string.match( _sThing, "a ([%a ]+)" )
  1138.     end
  1139.    
  1140.     local room = getRoom( x,y,z )
  1141.     inventory[sMaterial] = nil
  1142.     room.items[ _sThing ] = {
  1143.         heavy = true,
  1144.         aliases = { alias },
  1145.         desc = "As you look at your creation (made from "..sMaterial.."), you feel a swelling sense of pride.",
  1146.     }
  1147.  
  1148.     sleep(0.5)  c.say( "Your construction is complete." )
  1149. end
  1150.  
  1151. function commands.help()
  1152.     local sText =
  1153.         "Welcome to adventure, the greatest text adventure game on CraftOS. " ..
  1154.         "To get around the world, type actions, and the adventure will " ..
  1155.         "be read back to you. The actions availiable to you are go, look, inspect, inventory, " ..
  1156.         "take, drop, place, punch, attack, mine, dig, craft, build, eat and exit."
  1157.     sleep(0.5)  c.say( sText )
  1158. end
  1159.  
  1160. function commands.eat( _sItem )
  1161.     if _sItem == nil then
  1162.         sleep(0.5)  c.say( "Eat what?" )
  1163.         return
  1164.     end
  1165.  
  1166.     local sItem = findItem( inventory, _sItem )
  1167.     if not sItem then
  1168.         sleep(0.5)  c.say( "You don't have any ".._sItem.."." )
  1169.         return
  1170.     end
  1171.    
  1172.     local tItem = inventory[sItem]
  1173.     if tItem.food then
  1174.         sleep(0.5)  c.say( "That was delicious!" )
  1175.         inventory[sItem] = nil
  1176.        
  1177.         if bInjured then
  1178.             sleep(0.5)  c.say( "You are no longer injured." )
  1179.             bInjured = false
  1180.         end
  1181.     else
  1182.         sleep(0.5)  c.say( "You can't eat "..sItem.."." )
  1183.     end
  1184. end
  1185.  
  1186. function commands.exit()
  1187.     bRunning = false
  1188. end
  1189.  
  1190. function commands.badinput()
  1191.     local tResponses = {
  1192.         "I don't understand.",
  1193.         "I don't understand you.",
  1194.         "You can't do that.",
  1195.         "Nope.",
  1196.         "Huh?",
  1197.         "Say again?",
  1198.         "That's crazy talk.",
  1199.         "Speak clearly.",
  1200.         "I'll think about it.",
  1201.         "Let me get back to you on that one.",
  1202.         "That doesn't make any sense.",
  1203.         "What?",
  1204.     }
  1205.     sleep(0.5)  c.say( tResponses[ math.random(1,#tResponses) ] )
  1206. end
  1207.  
  1208. function commands.noinput()
  1209.     local tResponses = {
  1210.         "Speak up.",
  1211.         "Enunciate.",
  1212.         "Project your voice.",
  1213.         "Don't be shy.",
  1214.         "Use your words.",
  1215.     }
  1216.     sleep(0.5)  c.say( tResponses[ math.random(1,#tResponses) ] )
  1217. end
  1218.  
  1219. local function simulate()
  1220.     local bNewMonstersThisRoom = false
  1221.    
  1222.     -- Spawn monsters in nearby rooms
  1223.     for sx = -2,2 do
  1224.         for sy = -1,1 do
  1225.             for sz = -2,2 do
  1226.                 local h = y + sy
  1227.                 if h >= -3 and h <= 0 then
  1228.                     local room = getRoom( x + sx, h, z + sz )
  1229.                    
  1230.                     -- Spawn monsters
  1231.                     if room.nMonsters < 2 and
  1232.                        ((h == 0 and not isSunny() and not room.items["a torch"]) or room.dark) and
  1233.                        math.random(1,6) == 1 then
  1234.                        
  1235.                         local sMonster = tMonsters[ math.random(1,#tMonsters) ]
  1236.                         if room.items[ sMonster ] == nil then
  1237.                             room.items[ sMonster ] = items[ sMonster ]
  1238.                             room.nMonsters = room.nMonsters + 1
  1239.                            
  1240.                             if sx == 0 and sy == 0 and sz == 0 and not room.dark then
  1241.                                 sleep(0.5)  c.say( "From the shadows, "..sMonster.." appears." )
  1242.                                 bNewMonstersThisRoom = true
  1243.                             end
  1244.                         end
  1245.                     end
  1246.                    
  1247.                     -- Burn monsters
  1248.                     if h == 0 and isSunny() then
  1249.                         for n,sMonster in ipairs( tMonsters ) do
  1250.                             if room.items[sMonster] and items[sMonster].nocturnal then
  1251.                                 room.items[sMonster] = nil
  1252.                                 if sx == 0 and sy == 0 and sz == 0 and not room.dark then
  1253.                                     sleep(0.5)  c.say( "With the sun high in the sky, the "..items[sMonster].aliases[1].." bursts into flame and dies." )
  1254.                                 end
  1255.                                 room.nMonsters = room.nMonsters - 1
  1256.                             end
  1257.                         end
  1258.                     end
  1259.                 end
  1260.             end
  1261.         end
  1262.     end
  1263.  
  1264.     -- Make monsters attack
  1265.     local room = getRoom( x, y, z )
  1266.     if nTimeInRoom >= 2 and not bNewMonstersThisRoom then
  1267.         for n,sMonster in ipairs( tMonsters ) do
  1268.             if room.items[sMonster] then
  1269.                 if math.random(1,4) == 1 and
  1270.                    not (y == 0 and isSunny() and (sMonster == "a spider")) then
  1271.                     if sMonster == "a creeper" then
  1272.                         if room.dark then
  1273.                             sleep(0.5)  c.say( "A creeper explodes." )
  1274.                         else
  1275.                             sleep(0.5)  c.say( "The creeper explodes." )
  1276.                         end
  1277.                         room.items[sMonster] = nil
  1278.                         room.nMonsters = room.nMonsters - 1
  1279.                     else
  1280.                         if room.dark then
  1281.                             sleep(0.5)  c.say( "A "..items[sMonster].aliases[1].." attacks you." )
  1282.                         else
  1283.                             sleep(0.5)  c.say( "The "..items[sMonster].aliases[1].." attacks you." )
  1284.                         end
  1285.                     end
  1286.                    
  1287.                     if bInjured then
  1288.                         if term.isColour() then
  1289.                             term.setTextColour( colours.red )
  1290.                         end
  1291.                         sleep(0.5)  c.say( "You have died." )
  1292.                         sleep(0.5)  c.say( "Score: &e0" )
  1293.                         term.setTextColour( colours.white )
  1294.                         bRunning = false
  1295.                         return
  1296.                     else
  1297.                         bInjured = true
  1298.                     end
  1299.                    
  1300.                     break
  1301.                 end
  1302.             end
  1303.         end
  1304.     end
  1305.    
  1306.     -- Always sleep(0.5)  c.say this
  1307.     if bInjured then
  1308.         if term.isColour() then
  1309.             term.setTextColour( colours.red )
  1310.         end
  1311.         sleep(0.5)  c.say( "You are injured." )
  1312.         term.setTextColour( colours.white )
  1313.     end
  1314.    
  1315.     -- Advance time
  1316.     nTurn = nTurn + 1
  1317.     nTimeInRoom = nTimeInRoom + 1
  1318. end
  1319.  
  1320. doCommand( "look" )
  1321. simulate()
  1322.  
  1323. local tCommandHistory = {}
  1324. while bRunning do
  1325.     if term.isColour() then
  1326.         term.setTextColour( colours.yellow )
  1327.     end
  1328.     write( "? " )
  1329.     term.setTextColour( colours.white )
  1330.        
  1331.     --local sRawLine = read( nil, tCommandHistory )
  1332.     event,player,sRawLine=os.pullEvent("chat")
  1333.     table.insert( tCommandHistory, sRawLine )
  1334.    
  1335.     local sLine = nil
  1336.     for match in string.gmatch(sRawLine, "%a+") do
  1337.         if sLine then
  1338.             sLine = sLine .. " " .. string.lower(match)
  1339.         else
  1340.             sLine = string.lower(match)
  1341.         end
  1342.     end
  1343.    
  1344.     doCommand( sLine or "" )
  1345.     if bRunning then
  1346.         simulate()
  1347.     end
  1348. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement