edoreld

Untitled

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