roam.txt 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. -- roam
  2. -- by dotcomboom
  3. -- init functions
  4. function clear()
  5. for i=1, 30 do
  6. print()
  7. end
  8. end
  9. function new_region()
  10. regions[currentregion] = {}
  11. regions[currentregion][1] = areas[math.random(#areas)]
  12. regions[currentregion][2] = ''
  13. regions[currentregion][3] = ''
  14. end
  15. function new_item_mob()
  16. rarea = regions[currentregion][1]
  17. ritem = ''
  18. if rarea == 'forest' then
  19. if math.random(4) == 1 then
  20. ritem = 'log'
  21. end
  22. elseif rarea == 'desert' then
  23. if math.random(3) == 1 then
  24. ritem = 'cactus'
  25. end
  26. end
  27. regions[currentregion][2] = ritem
  28. rmob = ''
  29. if rarea == 'plains' then
  30. r = math.random(4)
  31. -- 1: no mob
  32. if r == 2 then
  33. rmob = 'pig'
  34. elseif r == 3 then
  35. rmob = 'sheep'
  36. end
  37. elseif rarea == 'hills' then
  38. if math.random(3) == 1 then
  39. rmob = 'goat'
  40. end
  41. end
  42. regions[currentregion][3] = rmob
  43. end
  44. -- init variables
  45. areas = {'forest', 'desert', 'plains', 'hills', 'town'}
  46. regions = { }
  47. inventory = {}
  48. currentregion = 1
  49. new_region()
  50. new_item_mob()
  51. coins = 0
  52. math.randomseed(os.time())
  53. -- start
  54. print([[ ___ ]])
  55. print([[ | _ \___ __ _ _ __ ]])
  56. print([[ | / _ \/ _` | ' \ ]])
  57. print([[ |_|_\___/\__,_|_|_|_|]])
  58. print()
  59. print()
  60. while true do
  61. print('You are in region ' .. currentregion .. '.')
  62. if #regions == currentregion then
  63. print("You haven't been here before.")
  64. print()
  65. end
  66. print('It is a ' .. regions[currentregion][1] .. ' area.')
  67. if regions[currentregion][2] == '' then else
  68. print('You picked up: ' .. regions[currentregion][2] .. '.')
  69. inventory[#inventory + 1] = regions[currentregion][2]
  70. regions[currentregion][2] = ''
  71. print()
  72. end
  73. if regions[currentregion][3] == '' then else
  74. print('There is a ' .. regions[currentregion][3] .. ' near.')
  75. end
  76. if regions[currentregion][1] == 'town' then
  77. print('Activity is bustling here.')
  78. end
  79. print()
  80. if #inventory > 0 then
  81. print('Inventory:')
  82. for key, value in pairs(inventory) do
  83. print(' ' .. value)
  84. end
  85. else
  86. print('Your pockets seem to be empty at the moment.')
  87. end
  88. print()
  89. print('You have ' .. coins .. ' coins on hand.')
  90. print()
  91. print('Will you...')
  92. print()
  93. print('Go [O]nward?')
  94. if currentregion > 1 then
  95. print('[B]acktrack?')
  96. end
  97. print('[V]iew your map?')
  98. if regions[currentregion][1] == 'town' then
  99. print('[T]rade with the townsfolk?')
  100. end
  101. option = string.lower(io.read())
  102. if option == 'o' then
  103. currentregion = currentregion + 1
  104. if currentregion > #regions then
  105. new_region()
  106. new_item_mob()
  107. end
  108. elseif option == 'b' then
  109. if currentregion > 1 then
  110. currentregion = currentregion - 1
  111. new_item_mob()
  112. end
  113. end
  114. clear()
  115. if option == 'v' then
  116. print("Let's see here..")
  117. print()
  118. out = ''
  119. for key, value in pairs(regions) do
  120. out = out .. key .. ": " .. value[1] .. " area.. "
  121. end
  122. print(out)
  123. print()
  124. print('Press ENTER.')
  125. io.read()
  126. clear()
  127. elseif option == 't' then
  128. if regions[currentregion][1] == 'town' then
  129. print('Welcome to the town marketplace.')
  130. if #inventory > 0 then
  131. print('Inventory:')
  132. for key, value in pairs(inventory) do
  133. print(' ' .. value)
  134. end
  135. else
  136. print('Your pockets seem to be empty at the moment.')
  137. end
  138. print()
  139. print('Would you like to..')
  140. print('[B]uy?')
  141. print('[S]ell?')
  142. o2 = string.lower(io.read())
  143. print()
  144. if o2 == 'b' then
  145. print("There doesn't seem to be any offers..")
  146. elseif o2 == 's' then
  147. print("Log sale price is 2 coins.")
  148. print("Cactus sale price is 1 coin.")
  149. print()
  150. print("What do you want to sell?")
  151. o3 = string.lower(io.read())
  152. print()
  153. amnt = 0
  154. profit = 0
  155. -- this is funky because I need to do it back to front instead
  156. -- of front to back, in order to get all the items since
  157. -- table.remove is involved
  158. for i=#inventory,1,-1 do
  159. key = i
  160. value = inventory[i]
  161. if value == o3 then
  162. table.remove(inventory, key)
  163. amnt = amnt + 1
  164. if o3 == "log" then
  165. profit = profit + 2
  166. elseif o3 == "cactus" then
  167. profit = profit + 1
  168. end
  169. end
  170. end
  171. coins = coins + profit
  172. if profit == 1 then
  173. print(amnt .. ' ' .. o3 .. ' items sold for a total of ' .. profit .. ' coin.')
  174. else
  175. print(amnt .. ' ' .. o3 .. ' items sold for a total of ' .. profit .. ' coins.')
  176. end
  177. print()
  178. print('Press ENTER.')
  179. io.read()
  180. end
  181. clear()
  182. end
  183. end
  184. end