-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameState.lua
297 lines (281 loc) · 8.89 KB
/
gameState.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
-----------------------------------------------------------------------------------------
--
-- gameState.lua
--
-- Data and functions to manage the game state sequence and logic.
-----------------------------------------------------------------------------------------
local game = globalGame
local gems = require( "gems" )
-- Shortcut access to game's saved state
local ss = game.saveState
-- File local variables
local stateStartMoves = 0 -- number of times dot had moved at start of current state
local foodOutSent = false -- true when out of food message has been sent
-- Ship state sequence data. Entries are indexed by state number and contain:
-- delay (optional) = delay before start of action in seconds
-- moves (optional) = number of times user must move on map before start of action
-- action (required) = function that returns next state, true for next, or nil to stay
local shipStateData = {
{ delay = 5, action = -- Send awaken messsages
function ()
game.sendMessages( "sas1", "stasis1" )
return true
end },
{ action = -- Wait for user to be in the Bridge
function ()
if game.roomName() == "Bridge" then
return true
end
end },
{ delay = 1, action = -- Send course correction #1 message
function ()
game.sendMessage( "correct1" )
return true
end },
{ action = -- Course correction #1
function ()
if ss.thrustNav.onTarget then
gems.enableShipGem( "fly1", false )
return true
end
end },
{ moves = 3, action = -- Data retrieval notification
function ()
game.sendMessage( "data1" )
return true
end },
{ moves = 3, action =
function ()
gems.enableShipGem( "shaw1" )
game.sendMessage( "fileS1" )
return true
end },
{ moves = 3, action =
function ()
game.sendMessage( "fileJ1" )
gems.enableShipGem( "jordan1" )
return true
end },
{ moves = 3, action = -- Wait for user to enter Shaw's room
function ()
if game.roomEntered( "Shaw" ) then
return true
end
end },
{ moves = 3, action = -- Unlock terminal minigame
function ()
game.sendMessage( "hack" )
gems.enableShipGem( "hack" )
game.terminalHacked = false
return true
end },
{ moves = 3, action = -- Reveal Engineering door code
function ()
if game.terminalHacked then
game.sendMessage( "codes" )
return true
end
end },
{ delay = 5, action = -- Notify to fix panel #1 (Engineering)
function ()
game.sendMessage( "panel1" )
gems.enableShipGem( "panel1" )
game.panelFixed = false
return true
end },
{ action = -- Fix panel #1
function ()
if game.panelFixed and game.currentActName() == "mainAct" then
gems.enableShipGem( "panel1", false )
game.removeAct( "circuit" )
game.removeAct( "wireCut" )
return true
end
end },
{ moves = 3, action =
function ()
gems.enableShipGem( "graham1" )
game.sendMessages( "antFail", "mcSignal" )
gems.enableShipGem( "moore" )
return true
end },
{ delay = 5, action = -- Notify to tend greenhouse
function ()
game.sendMessage( "green1" )
gems.enableShipGem( "plants" )
return true
end },
{ action = -- Greenhouse to get more food
function ()
if game.food() >= 150 then
game.messageBox( "Food level restored!" )
return true
end
end },
{ moves = 3, action =
function ()
game.sendMessage( "podStatus" )
return true
end },
{ delay = 2, action = -- Notify to fix panel #2
function ()
game.sendMessage( "panel2" )
gems.enableShipGem( "panel2" )
game.panelFixed = false
return true
end },
{ action = -- Fix panel #2
function ()
if game.panelFixed and game.currentActName() == "mainAct" then
gems.enableShipGem( "panel2", false )
game.removeAct( "circuit" )
game.removeAct( "wireCut" )
return true
end end },
{ moves = 3, action = -- Send course correction #2 messages
function ()
gems.enableShipGem( "graham2" )
gems.enableShipGem( "msgHist" )
game.sendMessage( "correct2" )
ss.thrustNav.onTarget = false
gems.enableShipGem( "fly1" )
return true
end },
{ action = -- Course correction #2
function ()
if ss.thrustNav.onTarget then
gems.enableShipGem( "fly1", false )
return true
end
end },
{ delay = 5, action = -- Enable a bunch of remaining docs
function ()
game.sendMessage( "docs" )
gems.enableShipGem( "jordan2" )
gems.enableShipGem( "webb" )
gems.enableShipGem( "shaw2" )
gems.enableShipGem( "ellis" )
gems.enableShipGem( "maxwell" )
gems.enableShipGem( "cDevice" )
gems.enableShipGem( "cEnergy" )
return true
end },
{ moves = 3, action = -- Notify to fix panel #3
function ()
game.sendMessage( "panel3" )
gems.enableShipGem( "panel3" )
game.panelFixed = false
return true
end },
{ action = -- Fix panel #3
function ()
if game.panelFixed and game.currentActName() == "mainAct" then
gems.enableShipGem( "panel3", false )
game.removeAct( "circuit" )
game.removeAct( "wireCut" )
return true
end
end },
{ delay = 4, action = -- Ready to land
function ()
gems.enableShipGem( "landing" )
gems.enableShipGem( "fly1", false )
game.sendMessage( "land" )
return true
end },
{ action = -- Landing game
function ()
if game.shipLanded and game.currentActName() == "mainAct" then
game.landShip()
game.sendMessage( "landed", 4000 )
game.sendMessage( "mars1", 10000 )
return true
end
end },
----- Ship State Table ends when ship has landed on Mars -----
}
-- Update the game state sequence when on the ship. The current state number
-- is passed, and the new state number is returned.
local function updateShipState( state )
-- Calculate number of seconds and moves since current state started
local sec = (system.getTimer() - game.stateStartTime) / 1000
local moves = game.moves - stateStartMoves
-- Get state data for this state and execute
local stateData = shipStateData[state]
if stateData then
-- Make sure the required number of moves and delay time has occurred
if not stateData.moves or moves >= stateData.moves then
if not stateData.delay or sec >= stateData.delay then
-- Execute the state action
local nextState = stateData.action()
if nextState then
if nextState == true then
nextState = state + 1
end
return nextState
end
end
end
end
return state -- no state change
end
-- Update the game state when on Mars.
local function updateMarsState()
-- Is emergency stasis needed?
gems.enableShipGem( "stasis", ss.stasis )
if ss.stasis then
-- Rover disabled when stasis needed
gems.enableShipGem( "rover", false )
else
-- Use a little food and water over time
game.addFood( -0.5 )
game.addWater( -0.25 )
-- Need food to take the rover out
local hasFood = (game.food() > 0)
gems.enableShipGem( "rover", hasFood )
-- If on the ship (not out in the rover), check notifications
if game.currentActName() == "mainAct" then
if hasFood then
foodOutSent = false -- ready to notify if food runs out (again)
else
-- Out of food. Send messsage if not already sent.
if not foodOutSent then
game.sendMessage( "foodOut" )
foodOutSent = true
end
-- Check water level
if game.water() <= 0 then
-- Out of both food and water. Emergency stasis.
game.sendMessage( "resOut" )
ss.stasis = true
game.updateState()
end
end
end
end
end
-- Set the ship state to the given state number
function game.setShipState( state )
ss.shipState = state
game.stateStartTime = system.getTimer()
stateStartMoves = game.moves
print( "Ship state " .. state )
end
-- This function is called every second while the game is running, but it
-- can also be called whenever an immediate game state update is desired.
function game.updateState()
-- If game is paused then just reset timer and do nothing
if game.paused then
game.stateStartTime = system.getTimer()
end
-- Are we on Mars yet?
if ss.onMars then
updateMarsState()
else
-- Not on Mars yet. Game state proceeds in a sequence.
local newState = updateShipState( ss.shipState )
if newState ~= ss.shipState then
game.setShipState( newState )
end
end
end