forked from kevinkey/klank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.rb
477 lines (403 loc) · 19.2 KB
/
map.rb
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
require "yaml"
module Klank
require_relative "item.rb"
require_relative "utils.rb"
class Map
attr_reader :map_num
attr_reader :market
attr_accessor :bank
def initialize(game, map)
@game = game
@map_num = map
case @game.game
when "Base Game"
file_path_prefix = ""
when "Sunken Treasures"
file_path_prefix = "sunken_treasures/"
when "The Mummy's Curse"
file_path_prefix = "mummys_curse/"
end
@map = YAML.load(File.read("#{file_path_prefix}map#{@map_num}.yml"))
# make sure every room has a hash, "secrets" defined (default 0)
@map["rooms"].each_key do |room_num|
@map["rooms"][room_num] = {
"major-secrets" => 0,
"minor-secrets" => 0,
"monkey-idols" => 0,
"supreme-monkey-idols" => 0,
"coins" => 0,
"heal" => 0,
"artifact" => 0,
"crystal-cave" => false,
"flooded" => false,
"store" => false,
"region" => :none,
}.merge(@map["rooms"][room_num] || {})
end
# make sure every path has a hash, "move", "attack" and "clank" defined (default 1, 0 and 0)
@map["paths"].each_key do |key|
@map["paths"][key] = {
"move" => 1,
"attack" => 0,
"clank" => 0,
"curse" => 0,
"locked" => false
}.merge(@map["paths"][key] || {})
end
@major = []
@minor = []
@market = []
for i in 0..1 do
prefix = (i == 1) ? file_path_prefix : ""
if (i == 0) or (prefix != "")
YAML.load(File.read("#{prefix}major.yml")).each do |i|
(i["count"] || 1).times do
@major << Item.new(game, i)
end
end
YAML.load(File.read("#{prefix}minor.yml")).each do |i|
(i["count"] || 1).times do
@minor << Item.new(game, i)
end
end
YAML.load(File.read("#{prefix}market.yml")).each do |i|
(i["count"] || 1).times do
@market << Item.new(game, i)
end
end
end
end
@bank = 81 # <-- 12 5-coin tokens and 21 1-coin tokens
end
def move(player)
loop do
player.output("\n#{Klank.table([{"MOVE" => player.move, "ATTACK" => player.attack}])}")
paths_out = get_paths_out(player)
option = player.menu("MOVE FROM ROOM #{player.room_num}", paths_out, true)
if option != "N"
room_num = option.to_i
path = paths_out.find { |p| p[0] == option }[1]["NAME"]
rooms = path.split(/-/, 2)
move_between_flooded_rooms = flooded_room?(rooms[0]) && flooded_room?(rooms[1]) && !player.has_item?("Scuba")
royal_bodyguard = player.has_played?("Royal Bodyguard") and player.has_crown?()
move = royal_bodyguard ? 1 : (move_between_flooded_rooms ? 2 : @map["paths"][path]["move"])
attack = royal_bodyguard ? 0 : @map["paths"][path]["attack"]
clank = royal_bodyguard ? 0 : @map["paths"][path]["clank"]
curse = royal_bodyguard ? 0 : @map["paths"][path]["curse"]
locked = !royal_bodyguard && @map["paths"][path]["locked"]
if player.move < move
player.output("Not enough move!")
elsif locked and !player.has_item?("Master Key")
player.output("That path is locked!")
elsif (room_num <= 1) and !player.has_artifact?()
player.output("No leaving without an artifact!")
else
if crystal_cave?(player)
crystal_golem_card = @game.dungeon.crystal_golem
if crystal_golem_card != nil
if player.attack >= crystal_golem_card.attack
if ("Y" == player.input("Do you want to kill the Crystal Golem first? (Y: yes)").upcase)
@game.dungeon.acquire(player, crystal_golem_card)
break
end
end
end
end
if (attack > 0)
if player.has_played?("Flying Carpet")
@game.broadcast("#{player.name} flew by the monster(s) on their Flying Carpet!")
elsif player.attack > 0
max_kill = [player.attack, attack].min
kill = player.input_num("You encounter #{attack} monster(s), enter number to kill", 0..max_kill)
@game.broadcast("#{player.name} killed #{kill} monster(s) and took #{(attack - kill)} damage!")
player.attack -= kill
player.num_monsters_killed += kill
player.num_damage_dealt += kill
(attack - kill).times do
player.damage(true)
end
else
attack.times do
player.damage(true)
end
@game.broadcast("#{player.name} took #{attack} damage!")
end
end
break if player.dead?()
player.clank(clank)
player.curse(curse)
player.move -= move
player.num_distance_moved += move
@game.broadcast("#{player.name} travelled to room #{room_num}.")
enter_room(player, room_num)
break if (player.move == 0) or player.frozen
end
else
break
end
break if player.dead?() or player.mastery
end
end
def teleport(player)
loop do
player.output("\n#{Klank.table([{"TELEPORT" => (player.teleport + (flooded?(player) ? player.shrine_mermaid_teleport : 0) + ((player.visited_rooms.count > 1) ? player.door_before_teleport : 0))}])}")
paths = []
if (player.teleport + (flooded?(player) ? player.shrine_mermaid_teleport : 0)) > 0
paths = get_paths(player)
end
visited_rooms = []
if (player.door_before_teleport > 0) && (player.visited_rooms.count > 1)
player.visited_rooms.each do |room|
if room.to_i != player.room_num && paths.none?{ |path| path[0].to_i == room.to_i }
visited_rooms << [room, room_desc(room)]
end
end
end
option = player.menu("TELEPORT FROM ROOM #{player.room_num}", paths + visited_rooms, true)
if option != "N"
room_num = option.to_i
if (room_num <= 1) and !player.has_artifact?()
player.output("No leaving without an artifact!")
else
if crystal_cave?(player)
crystal_golem_card = @game.dungeon.crystal_golem
if crystal_golem_card != nil
if player.attack >= crystal_golem_card.attack
if ("Y" == player.input("Do you want to kill the Crystal Golem first? (Y: yes)").upcase)
@game.dungeon.acquire(player, crystal_golem_card)
break
end
end
end
end
if visited_rooms.any?{ |room| room[0] == room_num }
player.door_before_teleport -= 1
elsif flooded?(player) and (player.shrine_mermaid_teleport > 0)
player.shrine_mermaid_teleport -= 1
else
player.teleport -= 1
end
player.num_times_teleported += 1
@game.broadcast("#{player.name} teleported to room #{room_num}.")
enter_room(player, room_num)
break if (player.teleport + (flooded?(player) ? player.shrine_mermaid_teleport : 0) + ((player.visited_rooms.count > 1) ? player.door_before_teleport : 0)) == 0
end
else
break
end
break if player.dead?() or player.mastery
end
end
def view(player)
rooms = []
@map["rooms"].each_key do |room_num|
status = room_desc(room_num)
if status.keys.count > 0
rooms << {"ROOM" => room_num}.merge(status)
end
end
player.output("MAP\n#{Klank.table(rooms)}")
end
def depths?(player)
(player.room_num >= @map["depths"])
end
def crystal_cave?(player)
@map["rooms"][player.room_num]["crystal-cave"]
end
def flooded?(player)
flooded_room?(player.room_num)
end
def flooded_room?(room_num)
@map["rooms"][room_num.to_i]["flooded"]
end
def region(player)
@map["rooms"][player.room_num]["region"]
end
def take_adjacent_secret(player)
rooms = []
paths = get_paths(player)
paths.each do |p|
room_num = p[0].to_i
if (@map["rooms"][room_num]["minor-secrets"] > 0)
rooms << [room_num, "Minor Secrets: #{@map["rooms"][room_num]["minor-secrets"]}"]
elsif (@map["rooms"][room_num]["major-secrets"] > 0)
rooms << [room_num, "Major Secrets: #{@map["rooms"][room_num]["major-secrets"]}"]
end
end
option = player.menu("ADJACENT SECRET LIST", rooms, true)
if option != "N"
room_num = option.to_i
if (@map["rooms"][room_num]["minor-secrets"] > 0)
@map["rooms"][room_num]["minor-secrets"] -= 1
@minor = Klank.randomize(@minor)
item = @minor.shift
player.num_minor_secrets_collected += 1
else
@map["rooms"][room_num]["major-secrets"] -= 1
@major = Klank.randomize(@major)
item = @major.shift
player.num_major_secrets_collected += 1
end
@game.broadcast("#{player.name} took a #{item.name} from room #{room_num}")
item.gain(player)
end
(option != "N")
end
def market?(player)
(@map["rooms"][player.room_num]["store"]) and (@market.count > 0) and (player.coins >= 7)
end
def view_market(player)
items = []
@market.each do |m|
items << {"NAME" => m.name, "DESCRIPTION" => m.description}
end
player.output("\nMARKET\n#{Klank.table(items)}")
end
def shop(player)
loop do
player.output("\n#{Klank.table([{"COINS" => player.coins}])}")
options = []
@market.each_with_index do |m, i|
options << [i, m.desc()]
end
item = player.menu("MARKET", options, true)
break if item == "N"
@bank += 7
@game.broadcast("#{player.name} bought #{@market[item.to_i].name} from the market! There are #{@bank} coin(s) in the bank!")
@market[item.to_i].gain(player)
@market.delete_at(item.to_i)
player.coins -= 7
break if !market?(player)
end
end
private
def enter_room(player, room_num)
player.room_num = room_num
player.visited_rooms |= [room_num]
player.num_rooms_visited += 1
if room_num <= 1
player.mastery = true
@game.broadcast("#{player.name} has left and collects a Mastery Token!")
@game.trigger_end(player)
end
if crystal_cave?(player)
player.num_caves_visited += 1
if !player.has_played?("Dead Run") and !player.has_played?("Flying Carpet")
@game.broadcast("#{player.name} has been frozen by the crystal cave!")
player.frozen = true
end
else
player.frozen = false
end
if flooded?(player)
player.num_flooded_rooms_visited += 1
else
if !player.air
@game.broadcast("#{player.name} has come up for air!")
end
player.air = true
end
if @map["rooms"][player.room_num]["minor-secrets"] > 0
@map["rooms"][player.room_num]["minor-secrets"] -= 1
@minor = Klank.randomize(@minor)
item = @minor.shift
@game.broadcast("#{player.name} found a #{item.desc}!")
item.gain(player)
player.num_minor_secrets_collected += 1
end
if @map["rooms"][player.room_num]["major-secrets"] > 0
@map["rooms"][player.room_num]["major-secrets"] -= 1
@major = Klank.randomize(@major)
item = @major.shift
@game.broadcast("#{player.name} found a #{item.desc}!")
item.gain(player)
player.num_major_secrets_collected += 1
end
if @map["rooms"][player.room_num]["monkey-idols"] > 0
@map["rooms"][player.room_num]["monkey-idols"] -= 1
player.item << Item.new(@game, {"name" => "Monkey Idol", "symbol" => "M", "points" => 5})
@game.broadcast("#{player.name} bows down to the Monkey Idol!")
end
if @map["rooms"][player.room_num]["supreme-monkey-idols"] > 0
@map["rooms"][player.room_num]["supreme-monkey-idols"] -= 1
player.item << Item.new(@game, {"name" => "Supreme Monkey Idol", "symbol" => "M", "points" => 10})
@game.broadcast("#{player.name} bows down to The Supreme Monkey Idol!")
end
player.collect_coins(@map["rooms"][player.room_num]["coins"])
player.heal(@map["rooms"][player.room_num]["heal"])
if (@map["rooms"][player.room_num]["artifact"] > 0) and player.hold_artifact?()
points = @map["rooms"][player.room_num]["artifact"]
if player.menu("PICK UP #{points} POINT ARTIFACT?", [["Y", "Yes"], ["N", "No"]]) == "Y"
@game.broadcast("#{player.name} picks up the #{points} point artifact!")
@game.dragon.anger()
player.artifact << points
@map["rooms"][player.room_num]["artifact"] = 0
end
end
end
# return hash of keys of all paths in or out of room number
def get_paths(player)
paths = []
@map["paths"].each_key do |key|
if (key =~ /^#{player.room_num}-(\d+)$/) || (key =~ /^(\d+)-#{player.room_num}$/)
paths << [$1, path_desc(player, key).merge(room_desc($1.to_i))]
end
end
paths
end
# return hash of keys of all paths out of room number
def get_paths_out(player)
paths_out = []
@map["paths"].each_key do |key|
if ((key =~ /^#{player.room_num}-(\d+)/) || ((key =~ /(\d+)-#{player.room_num}$/) && @map["paths"][key]["one-way"].nil?))
paths_out << [$1, path_desc(player, key).merge(room_desc($1.to_i))]
end
end
paths_out
end
def path_desc(player, key)
path = @map["paths"][key]
rooms = key.split(/-/, 2)
move_between_flooded_rooms = flooded_room?(rooms[0]) && flooded_room?(rooms[1]) && !player.has_item?("Scuba")
royal_bodyguard = player.has_played?("Royal Bodyguard") and player.has_crown?()
desc = {
"NAME" => key,
"MOVE" => royal_bodyguard ? 1 : (move_between_flooded_rooms ? 2 : (path.key?("move") ? path["move"] : 1)),
"MONSTERS" => (path.key?("attack") and !royal_bodyguard) ? path["attack"] : 0
}
clank = path.key?("clank") ? path["clank"] : 0
if @game.sunken_treasures?() && (clank > 0)
desc["CLANK"] = clank
end
curses = path.key?("curse") ? path["curse"] : 0
if @game.mummys_curse?() && (curses > 0)
desc["CURSE"] = curses
end
if path["locked"] and !royal_bodyguard
desc["LOCK"] = "YES"
end
desc
end
def room_desc(room_num)
status = {}
if (@map["rooms"][room_num]["minor-secrets"] > 0)
status["MINOR"] = @map["rooms"][room_num]["minor-secrets"]
elsif (@map["rooms"][room_num]["major-secrets"] > 0)
status["MAJOR"] = @map["rooms"][room_num]["major-secrets"]
elsif (@map["rooms"][room_num]["monkey-idols"] > 0)
status["MONKEY IDOLS"] = @map["rooms"][room_num]["monkey-idols"]
elsif (@map["rooms"][room_num]["supreme-monkey-idols"] > 0)
status["SUPREME MONKEY IDOL"] = @map["rooms"][room_num]["supreme-monkey-idols"]
elsif (@map["rooms"][room_num]["artifact"] > 0)
status["ARTIFACT"] = @map["rooms"][room_num]["artifact"]
elsif (@map["rooms"][room_num]["coins"] > 0)
status["COINS"] = @map["rooms"][room_num]["coins"]
end
players = @game.player.select{ |p| p.room_num == room_num }
if players.count > 0
status["PLAYERS"] = players.map { |p| p.name }.join(", ")
end
status
end
end
end