edoreld

Untitled

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