-
Notifications
You must be signed in to change notification settings - Fork 7
/
react.R
328 lines (326 loc) · 14.4 KB
/
react.R
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#' React to player's actions during the Castle of R game
#'
#' This function is called in the background after each of the player's actions
#' and responds accordingly. It is the "engine" of the game, modifying the game
#' environment according to the game's logic.
react <- function(game, ...){
game$deparsedExpr <- deparse(game$expr)
if (game$compareExpression("endGame()")) {
if (class(game$currentRoom)[1] %in% c("TimeRoom", "DarkRoom")) {
message("Can't end game in this room.")
} else {
game$endGame("Ending game.")
#removeTaskCallback("CastleOfR")
return(TRUE)
}
}
if (difftime(Sys.time(), game$roomStartTime, units = "mins") >
game$currentRoom$timeLimit) {
game$loseScenario("Too late.")
}
if (game$compareExpression("wtf()")) {
game$wtf()
}
if (game$compareExpression("timeLeft()")) {
game$timeLeft()
}
if (game$compareExpression("seePassword()")) {
game$plotPwd()
}
if (game$compareExpression("whatDoIHave()")) {
game$whatDoIHave()
}
if (game$compareExpression("hint()") ||
game$compareExpression("solution()")) {
type <- regmatches(game$deparsedExpr, gregexpr("[a-z]+",
game$deparsedExpr))[[1]]
game$hintSolution(type)
}
if (game$compareExpression("whatWasTheQuestion()")) {
if (!is.null(game$riddle)) {
game$riddle$askQuestion()
} else {
message("You haven't been asked a question. Yet.")
}
}
if (game$compareExpression("summonRDragon()")) {
game$summonRDragon()
}
if (grepl("^password\\(\"?[A-Za-z0-9]+\"?\\)$", game$deparsedExpr)) {
if (game$escapeRoom$name == game$currentRoom$name) {
inputPwd <- unlist(regmatches(game$deparsedExpr,
gregexpr("[A-Za-z0-9]+",
game$deparsedExpr)))[2]
message(paste0("input pwd is: ", inputPwd))
game$password(inputPwd)
} else {
message("You have not reached the Room from which you can escape.")
}
}
if (grepl("^seeMap\\([0-9]+\\)$", game$deparsedExpr)) {
map_idx <- as.numeric(unlist(regmatches(game$deparsedExpr,
gregexpr("[0-9]+",
game$deparsedExpr))))
if (map_idx > 0 && map_idx <= length(game$floorMapsAvailable)) {
if (toString(map_idx) %in% names(game$floorMapsPlayer)) {
game$plotMap(game$floorMapsPlayer[[toString(map_idx)]])
} else {
message("You don't have the map to floor ", map_idx)
}
} else {
message("No such floor.")
}
}
if (grepl("^openDoor\\([0-9]+\\)$", game$deparsedExpr)) {
game$door_idx <- as.numeric(unlist(regmatches(game$deparsedExpr,
gregexpr("[0-9]+",
game$deparsedExpr))))
if (game$door_idx > 0 && game$door_idx <= length(game$currentRoom$door)) {
game$nextRoom <-
game$currentRoom$door[[game$door_idx]]$getNextRoom(game$currentRoom$name)
if (!game$currentRoom$door[[game$door_idx]]$open) {
game$mode <- "door"
game$riddle <-
game$currentRoom$door[[game$door_idx]]$getRiddle(game$currentRoom$name)
if (!is.na(game$riddle$prepare)) {
eval(parse(text = game$riddle$prepare))
}
game$riddle$askQuestion()
} else {
message("Door is open.")
game$directionChosen <-
game$currentRoom$door[[game$door_idx]]$getDirection(game$currentRoom$name)
game$previousRoom <- game$currentRoom
game$currentRoom <- game$nextRoom
game$currentRoom$set_timeLimit(game$roomTimeLimit +
game$currentRoom$countLockedDoors() *
game$lockedDoorDelay)
game$roomStartTime <- Sys.time()
if (class(game$currentRoom)[1] == "TimeRoom") {
game$currentRoom$greet(game$currentRoom$floorMapsIdx %in%
names(game$floorMapsPlayer))
game$mode <- "time"
game$trRiddleIdx <- 1
game$riddle <- game$currentRoom$riddle[[game$trRiddleIdx]]
if (!is.na(game$riddle$prepare)) {
eval(parse(text = game$riddle$prepare))
}
message(paste0("Question ", game$trRiddleIdx, " out of ",
length(game$currentRoom$riddle), ":"))
game$riddle$askQuestion()
} else if (class(game$currentRoom)[1] == "DarkRoom") {
game$mode <- "dark"
game$currentRoom$greet(game$directionChosen)
if (length(game$satchel) >= game$currentRoom$nObjectsLeave) {
# subtract necessary objects and go back to previous room
message("Yes you do!")
game$removeNObjectsFromSatchel(game$currentRoom$nObjectsLeave)
message("Great! You're drawn back to the previous room.\n")
game$directionChosen <-
game$previousRoom$door[[game$door_idx]]$getDirection(game$currentRoom$name)
game$currentRoom <- game$previousRoom
game$previousRoom <- NULL
game$currentRoom$set_timeLimit(game$roomTimeLimit +
game$currentRoom$countLockedDoors() *
game$lockedDoorDelay)
game$currentRoom$greet(game$directionChosen)
game$roomStartTime <- Sys.time()
game$mode <- NULL
game$riddle <- NULL
game$nextRoom <- NULL
} else {
loseMessage <- paste0("Oh no, ", ifelse(length(game$satchel) == 0,
"you don't have any",
paste0("you only have ",
length(game$satchel))),
" objects in your satchel.")
game$loseScenario(loseMessage)
}
} else {
game$currentRoom$greet(game$directionChosen)
game$mode <- NULL
game$riddle <- NULL
game$nextRoom <- NULL
}
}
} else {
message("No such door.")
}
}
if (grepl("^lockDoor\\([0-9]+\\)$", game$deparsedExpr)) {
idx <- as.numeric(unlist(regmatches(game$deparsedExpr,
gregexpr("[0-9]+",
game$deparsedExpr))))
if (idx > 0 && idx <= length(game$currentRoom$door)) {
game$currentRoom$door[[idx]]$lockDoor()
game$currentRoom$set_timeLimit(game$roomTimeLimit +
game$currentRoom$countLockedDoors() *
game$lockedDoorDelay)
message("Door locked.")
} else {
message("No such door.")
}
}
if (grepl("^takeObject\\([0-9]+\\)$", game$deparsedExpr)) {
idx <- as.numeric(unlist(regmatches(game$deparsedExpr,
gregexpr("[0-9]+",
game$deparsedExpr))))
if (idx > 0 && idx <= length(game$currentRoom$object)) {
if (!game$currentRoom$object[[idx]]$taken) {
game$mode <- "object"
game$object_idx <- idx
game$riddle <- game$currentRoom$object[[idx]]$riddle
if (!is.na(game$riddle$prepare)) {
eval(parse(text = game$riddle$prepare))
}
game$riddle$askQuestion()
} else {
message("object taken")
}
} else {
message("No such object.")
}
}
if (!is.null(game$mode)) {
if (game$deparsedExpr == game$riddle$solution ||
(is.numeric(game$val) && !is.na(game$riddle$val) &&
game$val == game$riddle$val)) {
message("Correct!")
if (!is.na(game$riddle$cleanup)) {
eval(parse(text = game$riddle$cleanup))
}
if (game$mode == "door") {
game$directionChosen <-
game$currentRoom$door[[game$door_idx]]$getDirection(game$currentRoom$name)
message(paste0("Door to ", game$directionChosen, " opens."))
game$previousRoom <- game$currentRoom
game$currentRoom$door[[game$door_idx]]$openDoor()
game$currentRoom <- game$nextRoom
game$currentRoom$set_timeLimit(game$roomTimeLimit +
game$currentRoom$countLockedDoors() *
game$lockedDoorDelay)
game$nextRoom <- NULL
game$roomStartTime <- Sys.time()
game$mode <- NULL
game$riddle <- NULL
if (class(game$currentRoom)[1] == "TimeRoom") {
game$currentRoom$greet(
game$currentRoom$floorMapsIdx %in% names(game$floorMapsPlayer))
game$mode <- "time"
game$trRiddleIdx <- 1
game$riddle <- game$currentRoom$riddle[[game$trRiddleIdx]]
if (!is.na(game$riddle$prepare)) {
eval(parse(text = game$riddle$prepare))
}
message(paste0("Question ", game$trRiddleIdx, " out of ",
length(game$currentRoom$riddle), ":"))
game$riddle$askQuestion()
} else if (class(game$currentRoom)[1] == "DarkRoom") {
game$mode <- "dark"
game$currentRoom$greet(game$directionChosen)
if (length(game$satchel) >= game$currentRoom$nObjectsLeave) {
# subtract necessary R Power and go back to previous room
message("Yes you do!")
game$removeNObjectsFromSatchel(game$currentRoom$nObjectsLeave)
message("Great! You're drawn back to the previous room.\n")
game$directionChosen <-
game$previousRoom$door[[game$door_idx]]$getDirection(game$currentRoom$name)
game$currentRoom <- game$previousRoom
game$previousRoom <- NULL
game$currentRoom$set_timeLimit(game$roomTimeLimit +
game$currentRoom$countLockedDoors() *
game$lockedDoorDelay)
game$currentRoom$greet(game$directionChosen)
game$roomStartTime <- Sys.time()
game$mode <- NULL
} else {
loseMessage <- paste0("Oh no, ", ifelse(length(game$satchel) == 0,
"you don't have any",
paste0("you only have ",
length(game$satchel))),
" objects in your satchel.")
game$loseScenario(loseMessage)
}
} else {
game$currentRoom$greet(game$directionChosen)
}
} else if (game$mode == "object") {
message(paste0("You take the ", game$currentRoom$object[[game$object_idx]]$name,
" and put it in your satchel."))
game$currentRoom$object[[game$object_idx]]$takeObject()
game$satchel <- c(game$satchel, game$currentRoom$object[[game$object_idx]])
game$satchelHist <- game$satchel
objType <- game$currentRoom$object[[game$object_idx]]$type
if (objType == "power") {
message(paste0("You gained ",
game$currentRoom$object[[game$object_idx]]$points,
" points of R Power!"))
game$RPower <- game$RPower + game$currentRoom$object[[game$object_idx]]$points
} else if (objType == "pwd") {
message(paste0("There's something written on the ",
game$currentRoom$object[[game$object_idx]]$name,
". Look at the plot window."))
game$pwdExposedIdx <- c(game$pwdExposedIdx,
sample(setdiff(1:length(game$pwd),
game$pwdExposedIdx), 1))
game$plotPwd()
} else if (objType == "tip") {
message(paste0("There's something written on the ",
game$currentRoom$object[[game$object_idx]]$name,
": ", game$currentRoom$object[[game$object_idx]]$riddle$tip))
} else if (objType == "map") {
newMapIdx <- game$currentRoom$object[[game$object_idx]]$riddle$floorMapsIdx
mapsNames <- names(game$floorMapsPlayer)
if (!newMapIdx %in% mapsNames) {
game$floorMapsPlayer[[length(game$floorMapsPlayer) + 1]] <-
game$floorMapsAvailable[[newMapIdx]]
names(game$floorMapsPlayer) <- c(mapsNames, newMapIdx)
message(paste0("It's a map! To see it enter seeMap(", newMapIdx, ")"))
} else {
message("It's a map, but you already have it.")
}
}
game$object_idx <- NULL
game$mode <- NULL
game$riddle <- NULL
} else if (game$mode == "time") {
if (game$trRiddleIdx == length(game$currentRoom$riddle)) {
# give player map if not already there
if (!game$currentRoom$floorMapsIdx %in% names(game$floorMapsPlayer)) {
mapsNames <- names(game$floorMapsPlayer)
newMapIdx <- game$currentRoom$floorMapsIdx
game$floorMapsPlayer[[length(game$floorMapsPlayer) + 1]] <-
game$floorMapsAvailable[[newMapIdx]]
names(game$floorMapsPlayer) <- c(mapsNames, newMapIdx)
message(paste0("You got a map! To see it enter seeMap(",
newMapIdx, ")\n\nReturning to previous room."))
} else {
message("You already have this map...\n\nReturning to previous room.")
}
# return to previous room
game$directionChosen <-
game$previousRoom$door[[game$door_idx]]$getDirection(game$currentRoom$name)
game$currentRoom <- game$previousRoom
game$previousRoom <- NULL
game$currentRoom$set_timeLimit(game$roomTimeLimit +
game$currentRoom$countLockedDoors() *
game$lockedDoorDelay)
game$currentRoom$greet(game$directionChosen)
game$roomStartTime <- Sys.time()
game$mode <- NULL
game$riddle <- NULL
} else {
game$trRiddleIdx <- game$trRiddleIdx + 1
game$riddle <- game$currentRoom$riddle[[game$trRiddleIdx]]
if (!is.na(game$riddle$prepare)) {
eval(parse(text = game$riddle$prepare))
}
message(paste0("Question ", game$trRiddleIdx, " out of ",
length(game$currentRoom$riddle), ":"))
game$riddle$askQuestion()
}
}
}
}
return(TRUE)
}