/
dunGen.py
807 lines (658 loc) · 26.3 KB
/
dunGen.py
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
import libtcodpy as libtcod
import math
import Queue
import random
import string
import sys
SCREEN_WIDTH = 160
SCREEN_HEIGHT = 86
MAP_WIDTH = 160
MAP_HEIGHT = 86
CAMERA_WIDTH = 160
CAMERA_HEIGHT = 86
ROOM_MAX_SIZE = 3
ROOM_MIN_SIZE = 3
#MAX_ROOMS = 30
ROOM_SPACE = ROOM_MAX_SIZE + 1
FOV_ALGO = 0
FOV_LIGHT_WALLS = True
TORCH_RADIUS = 10
FOG_TORCH_RADIUS = 4
NORTH = 0
EAST = 1
SOUTH = 2
WEST = 3
DIRECTIONS = [NORTH, EAST, SOUTH, WEST]
libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'Branch Test', False)
con = libtcod.console_new(SCREEN_WIDTH, SCREEN_HEIGHT)
color_dark_wall = libtcod.Color( 0, 0, 100)
color_light_wall = libtcod.Color(130, 110, 50)
color_dark_ground = libtcod.Color( 50, 50, 150)
color_light_ground = libtcod.Color(200, 180, 50)
code_dict = { ' ': [0,0,0,0],
'a': [1,0,0,0],
'b': [2,0,0,0],
'c': [3,0,0,0],
'd': [0,1,0,0],
'e': [0,2,0,0],
'f': [0,3,0,0],
'g': [0,0,1,0],
'h': [0,0,2,0],
'i': [0,0,3,0],
'j': [1,0,0,1],
'k': [1,0,0,2],
'l': [1,0,0,3],
'm': [1,1,0,0],
'n': [1,2,0,0],
'o': [1,3,0,0],
'p': [1,0,1,0],
'q': [1,0,2,0],
'r': [1,0,3,0],
's': [1,0,0,1],
't': [1,0,0,2],
'u': [1,0,0,3],
'v': [2,0,0,0],
'w': [2,1,0,0],
'x': [2,2,0,0],
'y': [2,3,0,0],
'z': [2,0,1,0],
'1': [2,0,2,0],
'2': [2,0,3,0],
'3': [2,0,0,1],
'4': [2,3,0,2],
'5': [2,0,0,3],
'6': [2,1,1,0],
'7': [2,1,2,0],
'8': [2,1,3,0],
'9': [2,1,0,1],
'0': [2,1,0,2],
}
class Object:
def __init__(self, x, y, char, name, color, hunger_dec=1, hunger=100, blocks=False,
always_visible=False, fighter=None, ai=None, equipment=None, item=None,
inv=None, mod_set=None, mod_name=None):
self.x = x
self.y = y
self.char = char
self.color = color
self.name = name
self.hunger_dec = hunger_dec
self.hunger = hunger
self.blocks = blocks
self.always_visible = always_visible
self.inv = inv
self.spec = ''
self.mod_name=mod_name # Strong, Weak, Electric, etc.
# components, owned by the Object, which allow special behaviors
self.fighter = fighter
if self.fighter: # let the fighter component know who owns it
self.fighter.owner = self
self.ai = ai
if self.ai: # let the ai component know who owns it
self.ai.owner = self
self.item = item
if self.item: # let the item component know who owns it
self.item.owner = self
self.equipment = equipment
if self.equipment: # let the equipment component know what owns it
self.equipment.owner = self
self.item = Item() # a piece of equipment is always an Item (can be picked up and used)
self.item.owner = self
self.mod_set = mod_set # holds mods for object
if self.mod_set is None:
self.mod_set = set()
def move(self, dx, dy):
if ((0 <= (self.x + dx) < MAP_WIDTH) and (0 < (self.y + dy) < MAP_HEIGHT)
and not is_blocked(self.x + dx, self.y + dy)):
self.x += dx
self.y += dy
elif (0 <= (self.x + dx) < MAP_WIDTH) and not is_blocked(self.x + dx, self.y):
self.x += dx
elif (0 <= (self.y + dy) < MAP_HEIGHT) and not is_blocked(self.x, self.y + dy):
self.y += dy
if self is not player and 'puddle' in map[self.x][self.y].mod_set:
message("You hear splashing.", color_puddle)
def move_towards(self, target_x, target_y):
# vector from this object to target, and distance
dx = target_x - self.x
dy = target_y - self.y
distance = math.sqrt(dx ** 2 + dy ** 2)
# normalize it to length 1 (keeping direction), then round it and
# convert to integer for map grid movement
dx = dx / distance
dy = dy / distance
if dx < 0:
dx = int(0 - math.ceil(abs(dx)))
else:
dx = int(math.ceil(dx))
if dy < 0:
dy = int(0 - math.ceil(abs(dy)))
else:
dy = int(math.ceil(dy))
self.move(dx, dy)
def distance(self, x, y):
# return distance to some coordinates
return math.sqrt((x - self.x) ** 2 + (y - self.y) ** 2)
def distance_to(self, other):
# return distance to another object
dx = other.x - self.x
dy = other.y - self.y
return math.sqrt(dx ** 2 + dy ** 2)
def clear(self):
libtcod.console_put_char(con, self.x-camera.x, self.y-camera.y, ' ',
libtcod.BKGND_NONE)
def send_to_back(self):
# sets drawing order such that this object is drawn underneath other stuff
global objects
objects.remove(self)
objects.insert(0, self)
def check_floor(self):
# mod_set on objects holds resistances as the same value for Tile's mod_set
tile = map[self.x][self.y]
for mod in tile.mod_set:
if mod not in self.mod_set:
pass # TODO: handle damages, maybe as a list
#(for multiple damage types on floors)
@property
def full_name(self):
if self.mod_name is not None:
return self.mod_name + ' ' + self.name
else:
return self.name
class Player(Object):
def move(self, dx, dy):
global camera
if ((0 <= (self.x + dx) < MAP_WIDTH) and (0 < (self.y + dy) < MAP_HEIGHT)
and not is_blocked(self.x + dx, self.y + dy)):
self.x += dx
self.y += dy
elif (0 <= (self.x + dx) < MAP_WIDTH) and not is_blocked(self.x + dx, self.y):
self.x += dx
elif (0 <= (self.y + dy) < MAP_HEIGHT) and not is_blocked(self.x, self.y + dy):
self.y += dy
camera.move_to(player.x, player.y)
class Tile:
def __init__(self, blocked, x, y, block_sight=None):
# takes a set for modSet, and it determines the properties of a tile
self.blocked = blocked
self.explored = False
self.x = x
self.y = y
self.h = 0 # for use with A*
self.parent = None
self.mod_set = set()
if block_sight is None: block_sight = blocked
self.block_sight = block_sight
def add_mod(self, mod):
self.mod_set.add(mod)
class Camera:
def __init__(self, x, y):
self.x = x
self.y = y
self.width = CAMERA_WIDTH
self.height = CAMERA_HEIGHT
def fix_camera(self):
if self.x < 0:
self.x = 0
elif self.x > (MAP_WIDTH - CAMERA_WIDTH):
self.x = MAP_WIDTH - CAMERA_WIDTH
if self.y < 0:
self.y = 0
elif self.y > (MAP_HEIGHT - CAMERA_HEIGHT):
self.y = MAP_HEIGHT - CAMERA_HEIGHT
def move_to(self, x, y):
# moves camera to center on (x, y)
self.x = x - CAMERA_WIDTH/2
self.y = y - CAMERA_HEIGHT/2
self.fix_camera()
def handle_keys():
global fov_recompute, keys, mouse, msg_index, game_msgs
if key.vk == libtcod.KEY_ENTER and key.lalt:
libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
elif key.vk == libtcod.KEY_ESCAPE:
return 'exit'
if game_state == 'playing':
if key.vk == libtcod.KEY_UP or key.vk == libtcod.KEY_KP8:
player_move_or_attack(0, -1)
elif key.vk == libtcod.KEY_DOWN or key.vk == libtcod.KEY_KP2:
player_move_or_attack(0, 1)
elif key.vk == libtcod.KEY_LEFT or key.vk == libtcod.KEY_KP4:
player_move_or_attack(-1, 0)
elif key.vk == libtcod.KEY_RIGHT or key.vk == libtcod.KEY_KP6:
player_move_or_attack(1, 0)
elif key.vk == libtcod.KEY_KP1:
player_move_or_attack(-1,1)
elif key.vk == libtcod.KEY_KP3:
player_move_or_attack(1,1)
elif key.vk == libtcod.KEY_KP7:
player_move_or_attack(-1,-1)
elif key.vk == libtcod.KEY_KP9:
player_move_or_attack(1,-1)
elif key.vk == libtcod.KEY_KP5: # wait
pass
def render_all():
global fov_map, color_dark_wall, color_light_wall, color_dark_ground, color_light_ground
global fov_recompute, hunger, msg_index
x_range = range(camera.x, camera.x + CAMERA_WIDTH)
y_range = range(camera.y, camera.y + CAMERA_HEIGHT)
if fov_recompute:
# recompute FOV if need be (player movement or whatever)
fov_recompute = False
if 'fog' in map[player.x][player.y].mod_set:
libtcod.map_compute_fov(fov_map, player.x, player.y, FOG_TORCH_RADIUS,
FOV_LIGHT_WALLS, FOV_ALGO)
else:
libtcod.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO)
for y in y_range:
for x in x_range:
# uncomment this to return to exploration mode
#visible = libtcod.map_is_in_fov(fov_map, x, y)
visible = True
wall = map[x][y].block_sight
mods = map[x][y].mod_set
if not visible:
if map[x][y].explored:
if wall:
libtcod.console_set_char_background(con, x-camera.x, y-camera.y,
color_dark_wall, libtcod.BKGND_SET)
else:
libtcod.console_set_char_background(con, x-camera.x, y-camera.y,
color_dark_ground, libtcod.BKGND_SET)
else:
libtcod.console_set_char_background(con, x-camera.x, y-camera.y, libtcod.black,
libtcod.BKGND_SET)
else:
if wall:
#libtcod.console_set_default_foreground(con, libtcod.white)
libtcod.console_set_char_background(con, x-camera.x, y-camera.y,
color_light_wall, libtcod.BKGND_SET)
else:
# THIS ELIF IS FOR A DEBUG COLOR
if map[x][y].h == 1:
libtcod.console_set_char_background(con, x-camera.x, y-camera.y,
color_blood, libtcod.BKGND_SET)
else:
libtcod.console_set_char_background(con, x-camera.x, y-camera.y,
color_light_ground, libtcod.BKGND_SET)
map[x][y].explored = True
for obj in objects: # prevents drawing over the player
if obj != player and (obj.x in x_range) and (obj.y in y_range):
libtcod.console_set_default_foreground(con, obj.color)
libtcod.console_put_char(con, obj.x-camera.x, obj.y-camera.y, obj.char,
libtcod.BKGND_NONE)
libtcod.console_set_default_foreground(con, player.color)
libtcod.console_put_char(con, player.x-camera.x, player.y-camera.y, player.char, libtcod.BKGND_NONE)
libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0)
def make_map(secret):
global map, objects, stairs, dungeon_level, camera
# remove non-alphanumeric characters, and insert random spaces
secret = ''.join(ch for ch in secret if ch.isalnum())
secret = insert_spaces(secret)
stack = []
old_dir = EAST # initializes to EAST, because I said so, dang it
objects = [player]
map = [[Tile(True, x, y)
for y in range(MAP_HEIGHT) ]
for x in range(MAP_WIDTH) ]
rooms = []
num_rooms = 0
w = h = ROOM_MAX_SIZE
x = MAP_WIDTH / 2
y = MAP_HEIGHT / 2
new_room = Rect(x,y,w,h)
new_room.create(secret[num_rooms])
center_x, center_y = new_room.center()
(new_x, new_y) = (x + ROOM_SPACE, y)
player.x, player.y = new_room.center()
camera.move_to(player.x, player.y)
# objects.append(Object(center_x, center_y, secret[num_rooms],
# secret[num_rooms], libtcod.white))
num_rooms += 1
new_room = Rect(new_x, new_y,w,h)
new_room.create(secret[num_rooms])
center_x, center_y = new_room.center()
# objects.append(Object(center_x, center_y, secret[num_rooms],
# secret[num_rooms], libtcod.white))
stack.append((new_room, (old_dir + 2) % 4)) # stores the reverse of the direction
num_rooms += 1
# tunnel to previous room
map[center_x - 2][center_y].blocked = False
map[center_x - 2][center_y].block_sight = False
i = 0
while (i < 10000) and (num_rooms < len(secret)) and (len(stack) != 0):
if len(stack) > (len(secret) / 2):
for i in range(0, random.randint(0, (len(secret) / 2) - 1)):
stack.pop()
# this looks scary, but it basically pops until it finds a spot to branch in
# the same direction as old_dir
(old_room, old_dir) = stack[len(stack) - 1] # peek at top of stack
(new_x, new_y) = (old_room.x1, old_room.y1)
temp_dirs = check_dirs_counterclock(old_room, old_dir)
if len(temp_dirs) == 0:
stack.pop()
continue
temp_dirs.append(temp_dirs[len(temp_dirs) - 1]) # slight bias toward last item, more likely to
# generate a "fuller" map
i = random.choice(temp_dirs)
if i == NORTH:
new_y -= ROOM_SPACE
if i == SOUTH:
new_y += ROOM_SPACE
if i == WEST:
new_x -= ROOM_SPACE
if i == EAST:
new_x += ROOM_SPACE
new_room = Rect(new_x, new_y, w, h)
new_room.create(secret[num_rooms])
center_x, center_y = new_room.center()
# create tunnel to previous room
if i == NORTH:
map[center_x][center_y + 2].blocked = False
map[center_x][center_y + 2].block_sight = False
if i == SOUTH:
map[center_x][center_y - 2].blocked = False
map[center_x][center_y - 2].block_sight = False
if i == WEST:
map[center_x + 2][center_y].blocked = False
map[center_x + 2][center_y].block_sight = False
if i == EAST:
map[center_x - 2][center_y].blocked = False
map[center_x - 2][center_y].block_sight = False
# objects.append(Object(center_x, center_y, secret[num_rooms],
# secret[num_rooms], libtcod.white))
stack.append((new_room, (i + 2) % 4))
num_rooms += 1
stairs = Object(center_x, center_y, '>', 'stairs', libtcod.white, always_visible=True)
objects.append(stairs)
stairs.send_to_back() # drawn below monsters
if num_rooms < len(secret) - 1:
print "nope"
return False
return True
class Rect:
def __init__(self, x, y, w, h):
self.x1 = x
self.y1 = y
self.x2 = x + w
self.y2 = y + h
def center(self):
center_x = (self.x1 + self.x2) / 2
center_y = (self.y1 + self.y2) / 2
return(center_x, center_y)
def intersect(self):
# returns true if this rectangle intersects with another one
for x in range(self.x1, self.x2 + 1):
for y in range(self.y1, self.y2 + 1):
if not map[x][y].blocked:
return True
return False
def create(self, ch):
global map
for x in range(self.x1, self.x2):
for y in range(self.y1, self.y2):
map[x][y].blocked = False
map[x][y].block_sight = False
place_objects(self, ch)
def is_blocked(x, y):
if map[x][y].blocked:
return True
for object in objects:
if object.blocks and object.x == x and object.y == y:
return True
return False
def check_dirs_counterclock(room, d):
# check open directions counter-clockwise
temp_dirs = []
(new_x, new_y) = (room.x1, room.y1)
t_d = (d - 1) % 4
while t_d != d:
if t_d == NORTH:
if (0 < new_y - ROOM_SPACE < MAP_HEIGHT) and map[new_x][new_y - ROOM_SPACE].blocked:
temp_dirs.append(NORTH)
else:
break
if t_d == EAST:
if (0 < new_x + ROOM_SPACE < MAP_WIDTH) and map[new_x + ROOM_SPACE][new_y].blocked:
temp_dirs.append(EAST)
else:
break
if t_d == SOUTH:
if (0 < new_y + ROOM_SPACE < MAP_HEIGHT) and map[new_x][new_y + ROOM_MAX_SIZE + 1].blocked:
temp_dirs.append(SOUTH)
else:
break
if t_d == WEST:
if (0 < new_x - ROOM_SPACE < MAP_WIDTH) and map[new_x - ROOM_MAX_SIZE - 1][new_y].blocked:
temp_dirs.append(WEST)
else:
break
t_d = (t_d - 1) % 4
return temp_dirs
def check_dirs_clock(room, d):
# check open directions counter-clockwise
temp_dirs = []
(new_x, new_y) = (room.x1, room.y1)
t_d = (d - 1) % 4
while t_d != d:
if t_d == NORTH:
if map[new_x][new_y - ROOM_MAX_SIZE - 1].blocked:
temp_dirs.append(NORTH)
if t_d == EAST:
if map[new_x + ROOM_MAX_SIZE + 1][new_y].blocked:
temp_dirs.append(EAST)
if t_d == SOUTH:
if map[new_x][new_y + ROOM_MAX_SIZE + 1].blocked:
temp_dirs.append(SOUTH)
if t_d == WEST:
if map[new_x - ROOM_MAX_SIZE - 1][new_y].blocked:
temp_dirs.append(WEST)
t_d = (t_d - 1) % 4
return temp_dirs
def place_objects(room, char):
item_nums = code_dict[char.lower()]
item_spots = [0,0,0,0,0,0,0,0,0]
num_food = item_nums[0]
for i in range(num_food):
ind = random.randint(0,8)
while item_spots[ind] != 0:
ind = random.randint(0,8)
x = ind / 3
y = (ind % 3)
food = create_food(room.x1 + x, room.y1 + y)
objects.append(food)
food.send_to_back()
item_spots[ind] = 1
num_usables = item_nums[1]
for i in range(num_usables):
ind = random.randint(0,8)
while item_spots[ind] != 0:
ind = random.randint(0,8)
x = ind / 3
y = (ind % 3)
item = create_usable(room.x1 + x, room.y1 + y)
objects.append(item)
item.send_to_back()
item_spots[ind] = 1
num_treasure = item_nums[2]
for i in range(num_treasure):
ind = random.randint(0,8)
while item_spots[ind] != 0:
ind = random.randint(0,8)
x = ind / 3
y = (ind % 3)
item = create_treasure(room.x1 + x, room.y1 + y)
objects.append(item)
item.send_to_back()
item_spots[ind] = 1
num_equipment = item_nums[3]
for i in range(num_equipment):
ind = random.randint(0,8)
while item_spots[ind] != 0:
ind = random.randint(0,8)
x = ind / 3
y = (ind % 3)
item = create_equip(room.x1 + x, room.y1 + y)
objects.append(item)
item.send_to_back()
item_spots[ind] = 1
def create_equip(x, y):
# chance of each item (default is 0 at level 1, goes up after)
item_chances = ['sword', 'shield', 'axe', 'really big pizza cutter',
'lance', 'punch knife', 'shark cannon']
choice = random.choice(item_chances)
if choice == 'sword':
item = Object(x, y, '/', 'sword', libtcod.sky)
elif choice == 'shield':
item = Object(x, y, '[', 'shield', libtcod.sky)
elif choice == 'axe':
item = Object(x, y, '/', 'axe', libtcod.sky)
elif choice == 'really big pizza cutter':
item = Object(x, y, '?', 'really big pizza cutter', libtcod.sky)
elif choice == 'lance':
item = Object(x, y, '/', 'lance', libtcod.sky)
elif choice == 'punch knife':
item = Object(x, y, '+', 'punch knife', libtcod.sky)
elif choice == 'shark cannon':
item = Object(x, y, '{', 'shark cannon', libtcod.sky)
return item
def create_usable(x, y):
# chance of each item (default is 0 at level 1, goes up after)
item_chances = ['heal_lesser', 'heal', 'heal_greater', 'confuse', 'lightning',
'fireball', 'push', 'ice_wall', 'tome_heal_lesser', 'tome_heal',
'tome_heal_greater']
choice = random.choice(item_chances)
if choice == 'heal_lesser':
item = Object(x, y, '!', 'lesser healing potion', libtcod.violet)
elif choice == 'heal':
item = Object(x, y, '!', 'healing potion', libtcod.violet)
elif choice == 'heal_greater':
item = Object(x, y, '!', 'greater healing potion', libtcod.violet)
elif choice == 'tome_heal_lesser':
item = Object(x, y, '=', 'tome of lesser healing', libtcod.violet)
elif choice == 'tome_heal':
item = Object(x, y, '=', 'tome of healing', libtcod.violet)
elif choice == 'tome_heal_greater':
item = Object(x, y, '=', 'tome of greater healing', libtcod.violet)
elif choice == 'lightning':
item = Object(x, y, '#', 'scroll of lightning bolt', libtcod.blue)
elif choice == 'confuse':
item = Object(x, y, '#', 'scroll of confusion', libtcod.blue)
elif choice == 'fireball':
item = Object(x, y, '#', 'scroll of fireball', libtcod.blue)
elif choice == 'push':
item = Object(x, y, '#', 'scroll of push', libtcod.blue)
elif choice == 'ice_wall':
item = Object(x, y, '#', 'scroll of ice wall', libtcod.blue)
return item
def create_treasure(x, y):
# chance of each item (default is 0 at level 1, goes up after)
item_chances = ['chalice', 'scepter', 'coin purse', 'shiny pebble',
'amulet', 'gold bracelet', 'ruby', 'sapphire']
choice = random.choice(item_chances)
if choice == 'chalice':
item = Object(x, y, '$', 'chalice', libtcod.light_yellow)
elif choice == 'scepter':
item = Object(x, y, '$', 'scepter', libtcod.light_yellow)
elif choice == 'coin purse':
item = Object(x, y, '$', 'coin purse', libtcod.light_yellow)
elif choice == 'shiny pebble':
item = Object(x, y, '$', 'shiny pebble', libtcod.light_yellow)
elif choice == 'amulet':
item = Object(x, y, '$', 'amulet', libtcod.light_yellow)
elif choice == 'gold bracelet':
item = Object(x, y, '$', 'gold bracelet', libtcod.light_yellow)
elif choice == 'ruby':
item = Object(x, y, '$', 'rubby', libtcod.light_yellow)
elif choice == 'sapphire':
item = Object(x, y, '$', 'sapphire', libtcod.light_yellow)
return item
def create_food(x, y):
# chance of each food item
food_chances = ['apple', 'coconut_bar', 'peanut_butter_bar', 'jellybeans',
'licorice_pastels', 'chocolate_morsels', 'spam',
'sandwich', 'roast_beef']
choice = random.choice(food_chances)
if choice == 'apple':
food = Object(x, y, 'a', 'apple', libtcod.light_pink)
elif choice == 'coconut_bar':
food = Object(x, y, 'c', 'coconut bar', libtcod.light_pink)
elif choice == 'peanut_butter_bar':
food = Object(x, y, 'c', 'peanut butter bar', libtcod.light_pink)
elif choice == 'jellybeans':
food = Object(x, y, 'c', 'jellybeans', libtcod.light_pink)
elif choice == 'licorice_pastels':
food = Object(x, y, 'c', 'licorice pastels', libtcod.light_pink)
elif choice == 'chocolate_morsels':
food = Object(x, y, 'c', 'chocolate morsels', libtcod.light_pink)
elif choice == 'spam':
food = Object(x, y, 'p', 'can of spam', libtcod.light_pink)
elif choice == 'sandwich':
food = Object(x, y, 's', 'sandwich', libtcod.light_pink)
else:
food = Object(x, y, 'r', 'roast beef', libtcod.light_pink)
return food
def player_move_or_attack(dx, dy):
global fov_recompute
x = player.x + dx
y = player.y + dy
player.move(dx,dy)
fov_recompute = True
def insert_spaces(s):
s = list(s)
for i in xrange(len(s)-1):
while random.randrange(2):
s[i] = s[i] + ' '
return ''.join(s)
def new_game():
global player, inventory, game_msgs, game_state, dungeon_level, steps, hunger_msg, ice_counter
global msg_index, camera
if len(sys.argv) > 1:
input_string = sys.argv[1]
else:
input_string = "Nine-tenths of tactics are certain, and taught in books: but the irrational tenth is like the kingfisher flashing across the pool, and that is the test of generals."
msg_index = 0
name = 'Player'
inventory = []
game_msgs = []
libtcod.console_flush()
player = Player(0, 0, '@', name, libtcod.white, blocks=True, fighter=None)
player.spec = 'Swordsman'
player.level = 1
player.inv = inventory
hunger_msg = False
steps = 0
ice_counter = None
dungeon_level = 1
camera = Camera(0, 0)
while not make_map(input_string):
pass
# print len("Nine-tenths of tactics are certain, and taught in books: but the irrational tenth is like the kingfisher flashing across the pool, and that is the test of generals.")
initialize_fov()
game_state = 'playing'
def initialize_fov():
libtcod.console_clear(con)
global fov_recompute, fov_map
fov_recompute = True
fov_map = libtcod.map_new(MAP_WIDTH, MAP_HEIGHT)
for y in range(MAP_HEIGHT):
for x in range(MAP_WIDTH):
libtcod.map_set_properties(fov_map, x, y, not map[x][y].block_sight,
not map[x][y].blocked)
def play_game():
global key, mouse, msg_index
player_action = None
mouse = libtcod.Mouse()
key = libtcod.Key()
while not libtcod.console_is_window_closed():
libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS|libtcod.EVENT_MOUSE, key, mouse)
render_all()
libtcod.console_flush()
for obj in objects:
obj.clear()
player_action = handle_keys()
if player_action == 'exit':
break
# ---------- Program ----------
new_game()
play_game()