-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
2045 lines (1614 loc) · 71.6 KB
/
Copy pathgame.py
File metadata and controls
2045 lines (1614 loc) · 71.6 KB
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
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import division
import libtcodpy as libtcod
from libtcodpy import random_get_int as rand
import math, textwrap, time, sys, shelve, os
##################################################################################################################################
##################################################################################################################################
## ##
## CCCCCCC OOOO NNN NN EEEEEEEE IIIIIIII GGGGGGG ##
## CC CC OO OO NNNN NN EE II GG GG ##
## CC OO OO NN NN NN EE II GG ##
## CC OO OO NN NN NN EEEEEEE II GG ##
## CC OO OO NN NN NN EE II GG GGGGG ##
## CC OO OO NN NN NN EE II GG GG ##
## CC CC OO OO NN NNNN EE II GG GG ##
## CCCCCCC OOOO NN NNN EE IIIIIIII GGGGGG ##
## ##
##################################################################################################################################
##################################################################################################################################
##################################################################################################################################
# Debug #
##################################################################################################################################
DEBUG = False
DEBUGMSG = DEBUG
DISABLE_AI = DEBUG
##################################################################################################################################
# Configuration #
##################################################################################################################################
# FPS Limit
LIMIT_FPS = 20 # Limit the speed of the main loop
# Screen Dimensions
SCREEN_WIDTH = 85 # Overall screen width
SCREEN_HEIGHT = 63 # Overall screen height
# Map Dimensions
MAP_WIDTH = 85 # Width of the playable map
MAP_HEIGHT = 49 # Height of the playable map
# Game States
STATE_PLAYING = 0 # Player os currently playing, each action triggers a turn
STATE_TARGET = 1 # Player is selecting a target, no turn is used up
STATE_DEAD = 2 # Player died
# Player Actions
ACTION_NONE = 0 # Player did not take an action
ACTION_TURN = 1 # Player took a turn
ACTION_EXIT = 2 # Player selected quit
# Results
RESULT_CANCELLED = 1 # Function was cancelled (Ex. Spell is out of range)
# FOV
FOV_ALGO = 0 # Field of view algorithm to use (Default = 0)
FOV_LIGHT_WALLS = True # Should the first layer of walls light up while in fov (Yes)
# UI
INVENTORY_WIDTH = 50 # Width of the inventory menu
SPELL_WIDTH = 50 # Width of the spell menu
SAVE_WIDTH = 50 # Width of the save menu
EXIT_MENU_WIDTH = 30 # Width of thr save/exit menu in game
BAR_WIDTH = 25 # Width of health / mana bars
PANEL_HEIGHT = 14 # Height of the bottom panel
PANEL_Y = SCREEN_HEIGHT - PANEL_HEIGHT # Where to start the panel
MSG_X = BAR_WIDTH + 5 # Where to start the message window, leave one space on each side plus a line
MSG_WIDTH = SCREEN_WIDTH - BAR_WIDTH - 2 # Where to end the message window, 2 from the edge of the screen
MSG_HEIGHT = PANEL_HEIGHT - 5 # Height of the message window
##################################################################################################################################
# Generation #
##################################################################################################################################
# Spawning
MAX_ROOM_MONSTERS = 3 # Maximum number of monsters to spawn in a room
MAX_ROOM_ITEMS = 2 # Maximum number of items to spawn in a room
# Room Creation
ROOM_MIN_SIZE = 8 # Minimum room width / height
ROOM_MAX_SIZE = 20 # Maximum room width / height
RANDOM_HALLS = 10 # Number of random rooms to connect after map generation
##################################################################################################################################
# Default Values #
##################################################################################################################################
# Light Radius
LIGHT_RADIUS = 10 if not DEBUG else 1000 # Light radius!
BASE_LEVEL_XP = 100 # Base exp needed to level
LEVEL_XP_FACTOR = 100 # This * Level additional
MONSTER_LEVEL_RANGE = 2 # +- this value on monsters vs dungeon level
##################################################################################################################################
# Characters #
##################################################################################################################################
CHAR_PLAYER = '@' # Player
CHAR_NPC = 'N' # NPC
CHAR_WALL = '#' # Wall
CHAR_GROUND = '.' # Basic ground
CHAR_OTHER = ' ' # Anything else
CHAR_CORPSE = '%' # Corpse
CHAR_SPELL = ' ' # Default spell character
CHAR_SPELL_PATH = ' ' # Default spell path character
CHAR_STAIRS_DOWN = '<' # Stairs going down a level
CHAR_STAIRS_UP = '>' # Stairs going up a level
CHAR_BAR_S_VERTICAL = 179 # Vertical menu bar
CHAR_BAR_S_HORIZONTAL = 196 # Horizontal menu bar
CHAR_BAR_S_MID_DOWN = 194 # Middle down
CHAR_BAR_S_MID_UP = 193 # Middle up
CHAR_BAR_S_MID_LEFT = 195 # Middle left
CHAR_BAR_S_MID_RIGHT = 180 # Middle right
CHAR_BAR_S_TL = 218 # Top Left
CHAR_BAR_S_BL = 192 # Top LEft
CHAR_BAR_S_TR = 191 # Top Right
CHAR_BAR_S_BR = 217 # Bottom Right
CHAR_BAR_VERTICAL = 186 # Vertical menu bar
CHAR_BAR_HORIZONTAL = 205 # Horizontal menu bar
CHAR_BAR_MID_DOWN = 203 # Middle down
CHAR_BAR_MID_UP = 202 # Middle up
CHAR_BAR_MID_LEFT = 204 # Middle left
CHAR_BAR_MID_RIGHT = 185 # Middle right
CHAR_BAR_TL = 201 # Top Left
CHAR_BAR_BL = 200 # Top LEft
CHAR_BAR_TR = 187 # Top Right
CHAR_BAR_BR = 188 # Bottom Right
##################################################################################################################################
# Keybinds #
##################################################################################################################################
# Movement
KEYS_UP = [libtcod.KEY_UP, libtcod.KEY_KP8]
KEYS_DOWN = [libtcod.KEY_DOWN, libtcod.KEY_KP2]
KEYS_LEFT = [libtcod.KEY_LEFT, libtcod.KEY_KP4]
KEYS_RIGHT = [libtcod.KEY_RIGHT, libtcod.KEY_KP6]
KEYS_UPLEFT = [libtcod.KEY_KP7]
KEYS_UPRIGHT = [libtcod.KEY_KP9]
KEYS_DOWNLEFT = [libtcod.KEY_KP1]
KEYS_DOWNRIGHT = [libtcod.KEY_KP3]
KEYS_WAIT = [libtcod.KEY_KP5, 'w']
KEYS_STAIRS = ['<','>']
# General
KEYS_CONFIRM = [libtcod.KEY_KPENTER, 'c']
KEYS_CANCEL = [libtcod.KEY_KPSUB, 'q']
# Spells
KEYS_CAST = [libtcod.KEY_KPADD, libtcod.KEY_SPACE]
KEYS_SPELL = [libtcod.KEY_KPMUL, 's']
# Items
KEYS_PICKUP = [libtcod.KEY_KP0, ',']
KEYS_DROP = ['d']
KEYS_INVENTORY = ['i', libtcod.KEY_KPSUB]
# Other
KEYS_EXIT = [libtcod.KEY_ESCAPE]
KEYS_FULLSCREEN = [libtcod.KEY_F11]
# libtcod.KEY_KPADD / SUB / DIV / MUL / DEC / ENTER
##################################################################################################################################
# Colors #
##################################################################################################################################
# Color to use for transparency
COLOR_TRANSPARENT = libtcod.Color(255,0,255) # Color to use as the transparency for things like spell target console
# Walls
COLOR_DARK_WALL_BG = libtcod.Color(0,0,0) # Background for dark walls
COLOR_DARK_WALL_FG = libtcod.Color(41,16,2) # Foreground for dark walls
COLOR_LIGHT_WALL_BG = libtcod.Color(0,0,0) # Background for light walls
COLOR_LIGHT_WALL_FG = libtcod.Color(85,41,15) # Foreground for light walls
# Ground
COLOR_DARK_GROUND_BG = libtcod.Color(0,0,0) # Background for dark ground
COLOR_DARK_GROUND_FG = libtcod.Color(39,39,39) # Foreground for dark ground
COLOR_LIGHT_GROUND_BG = libtcod.Color(0,0,0) # Background for light ground
COLOR_LIGHT_GROUND_FG = libtcod.Color(129,129,129) # Foreground for light ground
# Spell Targetting Valid
SPELL_TARGET_TRANSPARENCY = 0.65 # Transparency level for the spell targetting console 0.0 -> 1.0
COLOR_SPELL_TARGET_LINE_BG = libtcod.lightest_green # Background for the spell targetting line
COLOR_SPELL_TARGET_LINE_FG = COLOR_TRANSPARENT # Foreground for the spell targetting line (Spell path character color)
COLOR_SPELL_TARGET_BG = libtcod.green # Background of the cell being targetted
COLOR_SPELL_TARGET_FG = COLOR_TRANSPARENT # Foreground on the cell being targetted (Spell character color)
# Spell Targetting Invalid
COLOR_SPELL_TARGET_LINE_BAD_BG = libtcod.lightest_red # Background for the line to an invalid target
COLOR_SPELL_TARGET_LINE_BAD_FG = COLOR_TRANSPARENT # Foreground for the line to an invalid target
COLOR_SPELL_TARGET_BAD_BG = libtcod.red # Background for the cell of an invalid target
COLOR_SPELL_TARGET_BAD_FG = COLOR_TRANSPARENT # Foreground for the cell of an invalid target
COLOR_DARK_STAIRS_FG = libtcod.gray # Foreground for dark stairs
COLOR_LIGHT_STAIRS_FG = libtcod.white # Foreground for dark stairs
##################################################################################################################################
##################################################################################################################################
## ##
## OOOO BBBBBBBBB JJJJJJJJJJJ EEEEEEEE CCCCCCC TTTTTTTT SSSSSSSS ##
## OO OO BB BB JJ EE CC CC TT SS ##
## OO OO BB BB JJ EE CC TT SS ##
## OO OO BBBBBBBB JJ EEEEEEE CC TT SSS ##
## OO OO BB BB JJ EE CC TT SSS ##
## OO OO BB BB JJ EE CC TT SS ##
## OO OO BB BB JJ JJ EE CC CC TT SS ##
## OOOO BBBBBBBBB JJJJ EEEEEEEE CCCCCCC TT SSSSSSSS ##
## ##
##################################################################################################################################
##################################################################################################################################
##################################################################################################################################
# Classes #
##################################################################################################################################
class Object:
def __init__(self, x, y, char=' ', name='Unknown Object', color=libtcod.white, blocks=False, fighter=None, ai=None, item=None, always_visible=False):
self.x = x
self.y = y
self.char = char
self.color = color
self.name = name
self.blocks = blocks
self.fighter = fighter
self.ai = ai
self.item = item
self.always_visible = always_visible
if self.fighter: self.fighter.owner = self
if self.ai: self.ai.owner = self
if self.item: self.item.owner = self
@property
def display(self):
return self.name if not self.fighter else "%s L%s" % (self.name, self.fighter.level)
def get_color(self, infov=True):
return self.color
def move(self, dx, dy):
if not is_blocked(self.x + dx, self.y + dy):
self.x += dx
self.y += dy
return True
return False
def move_towards(self, target_x, target_y):
dx = target_x - self.x
dy = target_y - self.y
distance = math.sqrt(dx ** 2 + dy ** 2)
dx = int(round(dx / distance))
dy = int(round(dy / distance))
self.move(dx,dy)
def distance_to(self, other):
dx = other.x - self.x
dy = other.y - self.y
return math.sqrt(dx ** 2 + dy ** 2)
def distance_to_point(self, x, y):
dx = x - self.x
dy = y - self.y
return math.sqrt(dx ** 2 + dy ** 2)
def in_range(self,x,y,d):
return self.distance_to_point(x,y) >= d
def object_in_range(self,obj,d):
return self.distance_to(obj) >= d
def draw(self):
if libtcod.map_is_in_fov(fov_map, self.x, self.y) or (self.always_visible and level_map[self.x][self.y].explored):
libtcod.console_set_default_foreground(con, self.get_color(infov = libtcod.map_is_in_fov(fov_map, self.x, self.y)))
libtcod.console_put_char(con, self.x, self.y, self.char, libtcod.BKGND_NONE)
def clear(self):
libtcod.console_put_char(con, self.x, self.y, ' ', libtcod.BKGND_NONE)
def send_to_back(self):
global objects
objects.remove(self)
objects.insert(0,self)
##################################################################################################################################
##################################################################################################################################
## ##
## NNN NN PPPPPPPP CCCCCCC SSSSSSSS ##
## NNNN NN PP PP CC CC SS ##
## NN NN NN PP PP CC SS ##
## NN NN NN PP PP CC SSS ##
## NN NN NN PPPPPPP CC SSS ##
## NN NN NN PP CC SS ##
## NN NNNN PP CC CC SS ##
## NN NNN PP CCCCCCC SSSSSSSS ##
## ##
##################################################################################################################################
##################################################################################################################################
##################################################################################################################################
# Classes #
##################################################################################################################################
class Fighter:
def __init__(self, hp, mana, defense, power, xp=0, death_function=None, friendly=False, enemy=True, level=1,
hp_factor=0.20, mana_factor=0.20, defense_factor=0.25, power_min_factor = 0.25, power_max_factor = 0.25, xp_factor = 0.25):
if not len(power):
power = (power,power)
self.level = level
self.max_hp = int(hp + level * hp * hp_factor)
self.hp = self.max_hp
self.max_mana = int(mana + level * mana * mana_factor)
self.mana = self.max_mana
self.defense = int(defense + level * defense * defense_factor)
self.power_min = int(power[0] + level * power[0] * power_min_factor)
self.power_max = int(power[1] + level * power[1] * power_max_factor)
self.xp = int(xp + level * xp * xp_factor)
self.base_hp = hp
self.base_mana = mana
self.base_defense = defense
self.base_power_min = power[0]
self.base_power_max = power[1]
self.hp_factor = hp_factor
self.mana_factor = mana_factor
self.defense_factor = defense_factor
self.power_min_factor = power_min_factor
self.power_max_factor = power_max_factor
self.xp_factor = xp_factor
self.friendly = friendly
self.enemy = enemy
self.death_function = death_function
def take_damage(self, damage):
if damage > 0:
self.hp -= damage
if self.hp <= 0:
self.hp = 0
function = self.death_function
if function is not None:
function(self.owner)
if self.owner != player:
player.fighter.grant_xp(self.xp)
def attack(self, target):
damage = rand(0,self.power_min,self.power_max) - target.fighter.defense
if damage > 0:
message("%s attacks %s for %s damage." % (self.owner.name.capitalize(), target.name, damage), libtcod.white)
target.fighter.take_damage(damage)
else:
message("%s attacks %s but it does nothing." % (self.owner.name.capitalize(), target.name), libtcod.white)
def heal(self, amount):
self.hp += amount
if self.hp > self.max_hp:
self.hp = self.max_hp
def grant_xp(self,xp):
player.fighter.xp += xp
if self.xp >= next_level_xp():
self.xp -= next_level_xp()
self.level_up()
def level_up(self):
message("You grow much stronger", libtcod.violet)
self.level += 1
self.max_hp += int(self.level * self.base_hp * self.hp_factor)
self.hp = self.max_hp
self.max_mana += int(self.level * self.base_mana * self.mana_factor)
self.mana = self.max_mana
self.defense += int(self.level * self.base_defense * self.defense_factor)
self.power_min += int(self.level * self.base_power_min * self.power_min_factor)
self.power_max += int(self.level * self.base_power_max * self.power_max_factor)
def spawn_monster(x, y, monster):
global objects
new_monster = Object(x, y,
char=monster['char'],
name=monster['name'],
color=monster['color'],
blocks=True,
fighter=Fighter(
hp=monster['hp'],
mana=monster['mana'],
defense=monster['defense'],
power=(monster['power_min'],monster['power_max']),
death_function=monster['death_function'],
friendly=False,
enemy=True,
xp=monster['xp'],
hp_factor=monster['hp_factor'],
mana_factor=monster['mana_factor'],
defense_factor=monster['defense_factor'],
power_min_factor=monster['power_min_factor'],
power_max_factor=monster['power_max_factor'],
xp_factor=monster['xp_factor'],
level=max(1,dungeon_level+rand(0,-MONSTER_LEVEL_RANGE,MONSTER_LEVEL_RANGE))
),
ai=monster['ai']()
)
objects.append(new_monster)
return True
##################################################################################################################################
##################################################################################################################################
## ##
## AAAA IIIIIIII ##
## AA AA II ##
## AA AA II ##
## AA AA II ##
## AAAAAAAAAA II ##
## AA AA II ##
## AA AA II ##
## AA AA IIIIIIII ##
## ##
##################################################################################################################################
##################################################################################################################################
##################################################################################################################################
# Classes #
##################################################################################################################################
class BasicMonster:
def take_turn(self):
monster = self.owner
if DISABLE_AI: return
if libtcod.map_is_in_fov(fov_map, monster.x, monster.y):
if monster.distance_to(player) >= 2:
monster.move_towards(player.x, player.y)
elif player.fighter.hp > 0:
monster.fighter.attack(player)
class ConfusedMonster:
def __init__(self, old_ai, duration=5):
self.old_ai = old_ai
self.duration = duration
def take_turn(self):
if self.duration > 0:
self.owner.move(rand(0,-1,1), rand(0,-1,1))
self.duration -= 1
else:
self.owner.ai = self.old_ai
message('The %s is no longer confused.' % self.owner.name, libtcod.red)
##################################################################################################################################
##################################################################################################################################
## ##
## IIIIIIII TTTTTTTT EEEEEEEE MM MM SSSSSSSS ##
## II TT EE MMMM MMMM SS ##
## II TT EE MM MM MM MM SS ##
## II TT EEEEEEE MM MM MM MM SSS ##
## II TT EE MM MMM MM SSS ##
## II TT EE MM MM SS ##
## II TT EE MM MM SS ##
## IIIIIIII TT EEEEEEEE MM MM SSSSSSSS ##
## ##
##################################################################################################################################
##################################################################################################################################
##################################################################################################################################
# Classes #
##################################################################################################################################
class Item:
def __init__(self, spell=None, consumable=False, use_function=None, value=None):
self.use_function = use_function
self.consumable = consumable
self.value = value
self.spell = spell
if self.spell and not self.use_function:
self.use_function = self.spell['cast_function']
def use(self):
if self.use_function is None:
message('The %s cannot be used.' % self.owner.name)
else:
if self.spell:
value = self.spell
else:
value = self.value
if self.use_function(value) != RESULT_CANCELLED and self.consumable:
inventory.remove(self.owner)
def pick_up(self):
if len(inventory) >= 26:
message('Your inventory is full, cannot pick up %s.' % self.owner.name, libtcod.red)
else:
inventory.append(self.owner)
objects.remove(self.owner)
message("You picked up a %s." % self.owner.name, libtcod.green)
def drop(self):
objects.append(self.owner)
inventory.remove(self.owner)
self.owner.x = player.x
self.owner.y = player.y
message("You dropped a %s." % self.owner.name, libtcod.yellow)
def spawn_item(x, y, item):
global objects
new_item = Object(x, y,
char=item['char'],
name=item['name'],
color=item['color'],
item=Item(
spell=item.get('spell',None),
consumable=item.get('consumable',False),
use_function=item.get('use_function',None),
value=item.get('value',None)
),
always_visible=True
)
objects.append(new_item)
new_item.send_to_back()
return True
##################################################################################################################################
##################################################################################################################################
## ##
## SSSSSSSS PPPPPPPP EEEEEEEE LL LL SSSSSSSS ##
## SS PP PP EE LL LL SS ##
## SS PP PP EE LL LL SS ##
## SSS PP PP EEEEEEE LL LL SSS ##
## SSS PPPPPPP EE LL LL SSS ##
## SS PP EE LL LL SS ##
## SS PP EE LL LL SS ##
## SSSSSSSS PP EEEEEEEE LLLLLLLL LLLLLLLL SSSSSSSS ##
## ##
##################################################################################################################################
##################################################################################################################################
##################################################################################################################################
# Cast functions #
##################################################################################################################################
def cast_heal(spell, x=None, y=None):
amount = rand(0, spell['min'], spell['max'])
if x == None:
target = player
else:
target = get_target_at(x,y,friendly=spell['friendly'], enemy=spell['enemy'], self=spell['self'])
if target is None:
message('Invalid target.', libtcod.red)
return RESULT_CANCELLED
elif player.distance_to(target) > spell['range']:
message('Target is out of range.', libtcod.red)
return RESULT_CANCELLED
if target.fighter.hp == target.fighter.max_hp:
if target == player:
message('You are already at full health.', libtcod.red)
else:
message('%s is already at full health.' % target.name, libtcod.red)
return RESULT_CANCELLED
if target == player:
message('You restored %s health!' % amount, libtcod.light_green)
else:
message('%s restored %s health!' % (target.name,amount), libtcod.light_green)
target.fighter.heal(amount)
def cast_lightning(spell, x=None, y=None):
damage = rand(0, spell['min'], spell['max'])
if x == None:
target = closest_monster(spell['range'])
if target is None:
message('No enemy in range.', libtcod.red)
return RESULT_CANCELLED
else:
target = get_target_at(x,y,friendly=spell['friendly'], enemy=spell['enemy'], self=spell['self'])
if target is None:
message('Invalid target.', libtcod.red)
return RESULT_CANCELLED
elif player.distance_to(target) > spell['range']:
message('Target is out of range.', libtcod.red)
return RESULT_CANCELLED
message('%s strikes %s for %s damage.' % (spell['name'], target.name, damage), libtcod.light_blue)
target.fighter.take_damage(damage)
def cast_confuse(spell, x=None, y=None):
duration = rand(0, spell['min_duration'], spell['max_duration'])
if x == None:
target = closest_monster(spell['range'])
if target is None:
message('No enemy in range.', libtcod.red)
return RESULT_CANCELLED
else:
target = get_target_at(x,y)
if target is None:
message('Invalid target.', libtcod.red)
return RESULT_CANCELLED
elif player.distance_to(target) > spell['range']:
message('Target is out of range.', libtcod.red)
return RESULT_CANCELLED
old_ai = target.ai
target.ai = ConfusedMonster(old_ai, duration=duration)
target.ai.owner = target
message('%s is now confused for %s turns.' % (target.name, duration), libtcod.light_blue)
def cast_fireball(spell, x=None, y=None):
damage = rand(0, spell['min'], spell['max'])
if x == None:
closest = closest_monster(spell['range'])
if closest is None:
message('No enemy in range.', libtcod.red)
return RESULT_CANCELLED
else:
x = closest.x
y = closest.y
if player.in_range(x,y,spell['range']):
message('Target is out of range.', libtcod.red)
return RESULT_CANCELLED
targets = get_targets_around(x,y,spell['radius'],friendly=spell['friendly'], enemy=spell['enemy'], self=spell['self'])
if not len(targets):
message('Nothing was hit by the fireball', libtcod.red)
return
for target in targets:
message('%s strikes %s for %s damage.' % (spell['name'], target.name, damage), libtcod.light_blue)
target.fighter.take_damage(damage)
def move_target(dx, dy):
global target_coords
target_coords = (target_coords[0] + dx, target_coords[1] + dy)
##################################################################################################################################
##################################################################################################################################
## ##
## EEEEEEEE VV VV EEEEEEEE NNN NN TTTTTTTT SSSSSSSS ##
## EE VV VV EE NNNN NN TT SS ##
## EE VV VV EE NN NN NN TT SS ##
## EEEEEEE VV VV EEEEEEE NN NN NN TT SSS ##
## EE VV VV EE NN NN NN TT SSS ##
## EE VV VV EE NN NN NN TT SS ##
## EE VVVV EE NN NNNN TT SS ##
## EEEEEEEE VV EEEEEEEE NN NNN TT SSSSSSSS ##
## ##
##################################################################################################################################
##################################################################################################################################
##################################################################################################################################
# Death Events #
##################################################################################################################################
def player_death(player):
global game_state
message("You have died!", libtcod.dark_red)
game_state = STATE_DEAD
player.char = CHAR_CORPSE
player.color = libtcod.dark_red
def monster_death(monster):
message("You slay %s and gain %s experience." % (monster.name, monster.fighter.xp), libtcod.orange)
monster.char = CHAR_CORPSE
monster.color = libtcod.dark_red
monster.blocks = False
monster.fighter = None
monster.ai = None
monster.name = 'Remains of %s' % monster.name
monster.send_to_back()
##################################################################################################################################
# Player Events #
##################################################################################################################################
def player_move_or_attack(dx, dy):
global fov_recompute
x = player.x + dx
y = player.y + dy
target = None
for obj in objects:
if obj.fighter and obj.x == x and obj.y == y:
target = obj
break
if target is not None:
player.fighter.attack(target)
else:
fov_recompute = player.move(dx, dy)
##################################################################################################################################
##################################################################################################################################
## ##
## MM MM AAAA PPPPPPPP ##
## MMMM MMMM AA AA PP PP ##
## MM MM MM MM AA AA PP PP ##
## MM MM MM MM AA AA PP PP ##
## MM MMM MM AAAAAAAAAA PPPPPPP ##
## MM MM AA AA PP ##
## MM MM AA AA PP ##
## MM MM AA AA PP ##
## ##
##################################################################################################################################
##################################################################################################################################
##################################################################################################################################
# Classes #
##################################################################################################################################
class MapNode:
def __init__(self, x, y, w, h):
self.x = x
self.y = y
self.width = w
self.height = h
self.room = None
self.left = None
self.right = None
def get_room(self):
if self.room != None:
return self.room
if self.left != None:
lroom = self.left.get_room()
if self.right != None:
rroom = self.right.get_room()
if lroom == None and rroom == None:
return None
elif rroom == None:
return lroom
elif lroom == None:
return rroom
elif rand(0,1,100) > 50:
return lroom
else:
return rroom
def get_random_room(self):
if self.room != None:
return self.room
if self.left != None or self.right != None:
return self.left.get_random_room() if rand(0,1,100) > 50 else self.right.get_random_room()
else:
return None
def split(self):
if self.left != None or self.right != None:
return False
random = False
if (self.width > self.height) and (self.height/self.width) <= 0.25:
split = False
elif (self.height > self.width) and (self.width / self.height) <= 0.25:
split = True
else:
random = True
split = rand(0,1,100) > 50
maxlen = (self.height if split else self.width) - ROOM_MIN_SIZE
if maxlen < ROOM_MIN_SIZE:
return False
splitspot = rand(0,ROOM_MIN_SIZE, maxlen)
if split:
self.left = MapNode(self.x, self.y, self.width, splitspot)
self.right = MapNode(self.x, self.y + splitspot, self.width, self.height - splitspot)
else:
self.left = MapNode(self.x, self.y, splitspot, self.height)
self.right = MapNode(self.x + splitspot, self.y, self.width - splitspot, self.height)
if self.left.width > ROOM_MAX_SIZE or self.left.height > ROOM_MAX_SIZE or rand(0,1,100) > 715:
self.left.split()
if self.right.width > ROOM_MAX_SIZE or self.right.height > ROOM_MAX_SIZE or rand(0,1,100) > 175:
self.right.split()
return True
def create_rooms(self):
global level_map
if self.left != None or self.right != None:
if self.left != None:
self.left.create_rooms()
if self.right != None:
self.right.create_rooms()
if self.left != None and self.right != None:
create_hall(self.left.get_room(), self.right.get_room())
else:
w = rand(0,ROOM_MIN_SIZE,self.width - 1)
h = rand(0,ROOM_MIN_SIZE,self.height - 1)
rx = rand(0, 0, self.width - w - 1)
ry = rand(0, 0, self.height - h - 1)
self.room = Rect(self.x + rx, self.y + ry, w, h)
for x in range(self.room.x1 + 1, self.room.x2):
for y in range(self.room.y1 + 1, self.room.y2):
level_map[x][y].blocked = False
level_map[x][y].block_sight = False
place_objects(self.room)
class Tile:
def __init__(self, blocked, block_sight=None):
self.blocked = blocked
self.explored = False
if block_sight is None:
block_sight = blocked
self.block_sight = block_sight
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 random(self):
x = rand(0,self.x1+1,self.x2-1)
y = rand(0,self.y1+1,self.y2-1)
return (x,y)
def intersect(self, other):
return (self.x1 <= other.x2 and self.x2 >= other.x1 and self.y1 <= other.y2 and self.y2 >= other.y1)
##################################################################################################################################
# Map Generation #
##################################################################################################################################
def make_map():
global level_map, player, target_coords, objects, stairs_down
objects = [player]
level_map = [[ Tile(True) for y in range(MAP_HEIGHT)] for x in range(MAP_WIDTH)]
nodes = []
root_node = MapNode(0, 0, MAP_WIDTH, MAP_HEIGHT)
root_node.split()
root_node.create_rooms()
for i in range(RANDOM_HALLS):
create_hall(root_node.get_random_room(),root_node.get_random_room())
starting = root_node.get_random_room()
player.x, player.y = starting.center()
target_coords = (player.x+1,player.y+2)
stairroom = root_node.get_random_room()
while stairroom == starting:
stairroom = root_node.get_random_room()
x,y = stairroom.random()
stairs_down = Object(x, y, CHAR_STAIRS_DOWN, 'Stairs Down', COLOR_LIGHT_STAIRS_FG)
objects.append(stairs_down)
stairs_down.send_to_back()
def create_hall(room1, room2):
prev_x, prev_y = room1.center()
new_x, new_y = room2.center()
libtcod.line_init(prev_x,prev_y,new_x,new_y)
x,y = libtcod.line_step()
while x is not None:
level_map[x][y].blocked = False
level_map[x][y].block_sight = False
level_map[x][y+1].blocked = False
level_map[x][y+1].block_sight = False
x,y = libtcod.line_step()
##################################################################################################################################
# Field of View Initialization #
##################################################################################################################################
def init_fov():
global fov_map, fov_recompute
fov_recompute = True
libtcod.console_clear(con)
fov_map = libtcod.map_new(MAP_WIDTH, MAP_HEIGHT)
for y in range(MAP_HEIGHT):
for x in range(MAP_WIDTH):
if DEBUG:
libtcod.map_set_properties(fov_map, x, y, True, True)
else:
libtcod.map_set_properties(fov_map, x, y, not level_map[x][y].block_sight, not level_map[x][y].blocked)
def place_objects(room):
global items, objects
num_monsters = rand(0, 0, MAX_ROOM_MONSTERS)
for i in range(num_monsters):
while True:
x = rand(0, room.x1+1, room.x2-1)
y = rand(0, room.y1+1, room.y2-1)
if not is_blocked(x,y):
break
chance = rand(0, 0, 100)
if chance < 20:
monster = monsters['orc']
elif chance < 20+40:
monster = monsters['troll']
elif chance < 20+40+30:
monster = monsters['zombie']
else:
monster = monsters['bat']
spawn_monster(x, y, monster)
num_items = rand(0, 0, MAX_ROOM_ITEMS)
for i in range(num_items):
while True:
x = rand(0, room.x1+1, room.x2-1)
y = rand(0, room.y1+1, room.y2-1)
if not is_blocked(x, y):
break