2
0

26_ConsoleInput.lua 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. -- Console input example.
  2. -- This sample demonstrates:
  3. -- - Implementing a crude text adventure game, which accepts input both through the engine console,
  4. -- and standard input.
  5. -- - Disabling default execution of console commands as immediate mode Lua script.
  6. require "LuaScripts/Utilities/Sample"
  7. local gameOn = false
  8. local foodAvailable = false
  9. local eatenLastTurn = false
  10. local numTurns = 0
  11. local hunger = 0
  12. local urhoThreat = 0
  13. -- Hunger level descriptions
  14. local hungerLevels = {
  15. "bursting",
  16. "well-fed",
  17. "fed",
  18. "hungry",
  19. "very hungry",
  20. "starving"
  21. }
  22. -- Urho threat level descriptions
  23. local urhoThreatLevels = {
  24. "Suddenly Urho appears from a dark corner of the fish tank",
  25. "Urho seems to have his eyes set on you",
  26. "Urho is homing in on you mercilessly"
  27. }
  28. function Start()
  29. -- Execute the common startup for samples
  30. SampleStart()
  31. -- Subscribe to console commands and the frame update
  32. SubscribeToEvent("ConsoleCommand", "HandleConsoleCommand")
  33. SubscribeToEvent("Update", "HandleUpdate")
  34. -- Subscribe key down event
  35. SubscribeToEvent("KeyDown", "HandleEscKeyDown")
  36. -- Hide logo to make room for the console
  37. SetLogoVisible(false)
  38. -- Show the console by default, make it large
  39. console.numRows = graphics.height / 16
  40. console.visible = true
  41. -- Show OS mouse cursor
  42. input.mouseVisible = true
  43. -- Disable default execution of Lua from the console
  44. SetExecuteConsoleCommands(false)
  45. -- Open the operating system console window (for stdin / stdout) if not open yet
  46. OpenConsoleWindow()
  47. -- Initialize game and print the welcome message
  48. StartGame()
  49. -- Randomize from system clock
  50. SetRandomSeed(time.systemTime)
  51. end
  52. function HandleConsoleCommand(eventType, eventData)
  53. HandleInput(eventData:GetString("Command"))
  54. end
  55. function HandleUpdate(eventType, eventData)
  56. -- Check if there is input from stdin
  57. local input = GetConsoleInput()
  58. if input:len() > 0 then
  59. HandleInput(input)
  60. end
  61. end
  62. function HandleEscKeyDown(eventType, eventData)
  63. -- Unlike the other samples, exiting the engine when ESC is pressed instead of just closing the console
  64. if eventData:GetInt("Key") == KEY_ESC then
  65. engine:Exit()
  66. end
  67. end
  68. function StartGame()
  69. Print("Welcome to the Urho adventure game! You are the newest fish in the tank your\n" ..
  70. "objective is to survive as long as possible. Beware of hunger and the merciless\n" ..
  71. "predator cichlid Urho, who appears from time to time. Evading Urho is easier\n" ..
  72. "with an empty stomach. Type 'help' for available commands.")
  73. gameOn = true
  74. foodAvailable = false
  75. eatenLastTurn = false
  76. numTurns = 0
  77. hunger = 2
  78. urhoThreat = 0
  79. end
  80. function EndGame(message)
  81. Print(message)
  82. Print("Game over! You survived " .. numTurns .. " turns.\n" ..
  83. "Do you want to play again (Y/N)?")
  84. gameOn = false
  85. end
  86. function Advance()
  87. if urhoThreat > 0 then
  88. urhoThreat = urhoThreat + 1
  89. if urhoThreat > 3 then
  90. EndGame("Urho has eaten you!")
  91. return
  92. end
  93. elseif urhoThreat < 0 then
  94. urhoThreat = urhoThreat + 1
  95. elseif urhoThreat == 0 and Random() < 0.2 then
  96. urhoThreat = urhoThreat + 1
  97. end
  98. if urhoThreat > 0 then
  99. Print(urhoThreatLevels[urhoThreat] .. ".")
  100. end
  101. if (numTurns % 4) == 0 and not eatenLastTurn then
  102. hunger = hunger + 1
  103. if hunger > 5 then
  104. EndGame("You have died from starvation!")
  105. return
  106. else
  107. Print("You are " .. hungerLevels[hunger + 1] .. ".")
  108. end
  109. end
  110. eatenLastTurn = false
  111. if foodAvailable then
  112. Print("The floating pieces of fish food are quickly eaten by other fish in the tank.")
  113. foodAvailable = false
  114. elseif Random() < 0.15 then
  115. Print("The overhead dispenser drops pieces of delicious fish food to the water!")
  116. foodAvailable = true
  117. end
  118. numTurns = numTurns + 1
  119. end
  120. function TrimInput(input)
  121. return input:gsub("^%s*(.-)%s*$", "%1")
  122. end
  123. function HandleInput(input)
  124. local inputLower = TrimInput(input:lower())
  125. if inputLower:len() == 0 then
  126. Print("Empty input given!")
  127. return
  128. end
  129. if inputLower == "quit" or inputLower == "exit" then
  130. engine:Exit()
  131. elseif gameOn then
  132. -- Game is on
  133. if inputLower == "help" then
  134. Print("The following commands are available: 'eat', 'hide', 'wait', 'score', 'quit'.")
  135. elseif inputLower == "score" then
  136. Print("You have survived " .. numTurns .. " turns.")
  137. elseif inputLower == "eat" then
  138. if foodAvailable then
  139. Print("You eat several pieces of fish food.")
  140. foodAvailable = false
  141. eatenLastTurn = true
  142. if hunger > 3 then
  143. hunger = hunger - 2
  144. else
  145. hunger = hunger - 1
  146. end
  147. if hunger < 0 then
  148. EndGame("You have killed yourself by over-eating!")
  149. return
  150. else
  151. Print("You are now " .. hungerLevels[hunger + 1] .. ".")
  152. end
  153. else
  154. Print("There is no food available.")
  155. end
  156. Advance()
  157. elseif inputLower == "wait" then
  158. Print("Time passes...")
  159. Advance()
  160. elseif inputLower == "hide" then
  161. if urhoThreat > 0 then
  162. local evadeSuccess = hunger > 2 or Random() < 0.5
  163. if evadeSuccess then
  164. Print("You hide behind the thick bottom vegetation, until Urho grows bored.")
  165. urhoThreat = -2
  166. else
  167. Print("Your movements are too slow you are unable to hide from Urho.")
  168. end
  169. else
  170. Print("There is nothing to hide from.")
  171. end
  172. Advance()
  173. else
  174. Print("Cannot understand the input '" .. input .. "'.")
  175. end
  176. else
  177. -- Game is over, wait for (y)es or (n)o reply
  178. local c = inputLower:sub(1, 1)
  179. if c == 'y' then
  180. StartGame()
  181. elseif c == 'n' then
  182. engine:Exit()
  183. else
  184. Print("Please answer 'y' or 'n'.")
  185. end
  186. end
  187. end
  188. function Print(input)
  189. -- Logging appears both in the engine console and stdout
  190. Log:WriteRaw(input .. "\n")
  191. end