forked from panicmechanic/ZMD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirstrl.py
More file actions
3326 lines (2577 loc) · 128 KB
/
firstrl.py
File metadata and controls
3326 lines (2577 loc) · 128 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
import libtcodpy as libtcod
import math
import textwrap
import shelve
import weaponchances
import monsterchances
import time
from weaponchances import create_item
import prefab_map
# BORDER VARIABLES
BORDER_CORNER = chr(15)
BORDER_FILL = '+'
BORDER_COLOR = libtcod.gold
BORDER_BACKGROUND = libtcod.darkest_grey
#FATAL EFFECT
FATAL_EFFECT = False
FATAL_NAME = None
# actual size of the window
SCREEN_WIDTH = 130
SCREEN_HEIGHT = 60
#size of the map
MAP_WIDTH = 106
MAP_HEIGHT = 53
#parameters for dungeon generator
ROOM_MAX_SIZE = 12
ROOM_MIN_SIZE = 5
MAX_ROOMS = 55
#sizes and coordinates relevant for GUI
BAR_WIDTH = 20
PANEL_HEIGHT = 7
PANEL_Y = SCREEN_HEIGHT - PANEL_HEIGHT
MSG_X = BAR_WIDTH + 1
MSG_WIDTH = SCREEN_WIDTH - BAR_WIDTH - 1
MSG_HEIGHT = PANEL_HEIGHT - 1
INVENTORY_WIDTH = 50
LEVEL_SCREEN_WIDTH = 40
MUTATION_SCREEN_WIDTH = 80
CHARACTER_SCREEN_WIDTH = 25
EFFECTS_GUI = 4
# PANEL 2
PANEL2_HEIGHT = SCREEN_HEIGHT
PANEL2_WIDTH = 24
RECT_HEIGHT = 16 - PANEL2_HEIGHT # 16 being the max height of the enemy fov panel.
MSG_STOP = MSG_WIDTH - PANEL2_WIDTH
PANEL_WIDTH = SCREEN_WIDTH - PANEL2_WIDTH
#Item parameters
HEAL_AMOUNT = 40
LIGHTNING_DAMAGE = 40
LIGHTNING_RANGE = 5
CONFUSE_NUM_TURNS = 10
CONFUSE_RANGE = 8
FIREBALL_RADIUS = 3
FIREBALL_DAMAGE = 25
HEAL_RATE = 1
ELEC_FIRING = False
#Food properties
HUNGER_RATE = 1
HUNGER_TOTAL = 8000
HUNGER_WARNING = 100
CURRENT_ATTACK = None
#Player parameters
LEVEL_UP_BASE = 200
LEVEL_UP_FACTOR = 200
#FPS
LIMIT_FPS = 60 #60 frames-per-second maximum
#Darkest wall can be when not in FOV
color_set_wall_dark = libtcod.darkest_grey
#Lightest wall can be when not in FOV, bottom for lerp
color_dark_wall = libtcod.darker_grey
#Top for lerp
color_light_wall = libtcod.Color(70, 70, 2)#Yellowed
#Darkest wall can be when not in FOV
color_char_set_dark_wall = libtcod.darker_grey
#Lightest, unused
color_char_dark_wall = libtcod.dark_grey
#Top for lerp
color_char_light_wall = libtcod.Color(107, 107, 12)#Yellowed
color_set_ground_dark = libtcod.Color(50, 60, 40)
color_dark_ground = libtcod.Color(80, 90, 70)
color_light_ground = libtcod.Color(100, 100, 14)
color_char_set_dark_ground = libtcod.Color(80, 90, 70)
color_char_dark_ground = libtcod.Color(110, 110, 100)
#Torch color
torch_color = libtcod.light_flame
#darker torch color for foreground
torch_light_color = libtcod.light_flame
#Torch alpha
torch_alpha = 0.5
#TODO: Change this value sightly for better flicker
WALL_CHAR = '#'
FLOOR_CHAR = '.'
#FOV
FOV_ALGO = 1 #Default FOV algorithm
FOV_LIGHT_WALLS = True #Light walls or not
TORCH_RADIUS = 10
SQUARED_TORCH_RADIUS = TORCH_RADIUS * TORCH_RADIUS
FOV_NOISE = None
FOV_TORCHX = 0.0
FOV_INIT = False
NOISE = libtcod.noise_new(1)
#Effects toggle
#HERMES_TIMESLIP = False
class Tile:
# a tile of the map, and its properties
def __init__(self, blocked, block_sight=None, diff_color=[], color_flash=None, color_set=None, color_fore=None, debug_blocked=False, debug_path=False):
self.blocked = blocked
self.diff_color = diff_color
self.color_flash = color_flash
self.color_set = color_set
self.color_fore = color_fore
self.debug_blocked = debug_blocked
self.debug_path = debug_path
#all tiles start unexplored
self.explored = False
#by default if a tile is blocked, it also blocks sight
if block_sight is None: block_sight = blocked
self.block_sight = block_sight
class Rect:
#A rectangle on the map, used to characterise a room
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, other):
#returns true if this rectangle intersects with another one
return (self.x1 <= other.x2 and self.x2 >= other.x1 and
self.y1 <= other.y2 and self.y2 >= other.y1)
class Object:
#this is a generic object: the player, a monster, an item, the stairs...
#it's always represented by a character on screen.
def __init__(self, x, y, char, name, color, equipment=None, effects=None, blocks=False, always_visible=False,
fighter=None, ai=None, item=None, description=None, seen=False, path=None, decorative=False,
display_dmg=None):
self.x = x
self.y = y
self.char = char
self.name = name
self.color = color
self.blocks = blocks
self.always_visible = always_visible
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, like a bitch
self.item.owner = self
self.equipment = equipment
if self.equipment: #let the equipment component know who owns it
self.equipment.owner = self
#there must be an Item component for the Equipment component to work properly
self.item = Item()
self.item.owner = self
self.effects = effects
if self.effects: #let the effect component know who owns it
self.effects.owner = self
#there must be a fighter component for the effect component to work properly
self.fighter = Fighter()
self.fighter.owner = self
self.description = description
self.seen = seen
self.path = path
self.decorative = decorative
self.display_dmg = display_dmg
def move(self, dx, dy):
#move by the given amount, if the destination is not blocked
if not is_blocked(self.x + dx, self.y + dy):
self.x += dx
self.y += dy
def move_towards(self, target_x, target_y):
#A lot of this is taken directly from SevenTrials (https://github.com/nefD/SevenTrials)
#and modified to work here.
#This creates the path needed, in seven trials this is done in play_game loop,
# load_game() and use() functions.
list = []
self.path = libtcod.path_new_using_function(MAP_WIDTH, MAP_HEIGHT, path_func, self, 1)
#Compute path to target
libtcod.path_compute(self.path, self.x, self.y, target_x, target_y)
#vector from this object to the target, and distance
if not libtcod.path_is_empty(self.path):
#Walk the path
path_x, path_y = libtcod.path_get(self.path, 0)
for obj in objects:
#If next step is blocked
if obj.x == path_x and obj.y == path_y and obj != player and obj != self:
#Iterate through objects
for obj in objects:
#If object has a fighter instance, and is less than 2 tiles away from self and is not self or player
if obj.fighter and obj.distance_to(self) <= 3 and obj != self and obj != player:
list.append(obj)
#TODO: Put this in init somewhere
#If object is a fountain and is not blocked already, block it.
#elif obj.name == 'Fountain' and libtcod.map_is_walkable(fov_map, obj.x, obj.y):
#libtcod.map_set_properties(fov_map, obj.x, obj.y, True, False)
for i in list:
libtcod.map_set_properties(fov_map, i.x, i.y, True, False)
self.path = libtcod.path_new_using_function(MAP_WIDTH, MAP_HEIGHT, path_func, self, 1)
#Compute path to target
libtcod.path_compute(self.path, self.x, self.y, target_x, target_y)
#vector from this object to the target, and distance
if not libtcod.path_is_empty(self.path) and libtcod.path_size(self.path) <= 6:
#Walk the path
path_x, path_y = libtcod.path_get(self.path, 0)
#Path is still empty
else:
#do nothing
pass
break
#normalise it to 1 length (preserving direction), then round it and
#convert to integer so the movement is restricted to the map grid
dx = path_x - self.x
dy = path_y - self.y
else:
self.path = None
print 'Path is empty'
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)
if list != []:
for i in list:
libtcod.map_set_properties(fov_map, i.x, i.y, True, True)
def distance_to(self, other):
#return the distance to another object
dx = other.x - self.x
dy = other.y - self.y
return math.sqrt(dx ** 2 + dy ** 2)
def distance_from(self, x, y):
# return the distance to another x,y coordinates
dx = x - self.x
dy = y - self.y
return math.sqrt(dx ** 2 + dy ** 2)
def distance(self, x, y):
#return the distance to some coordinates
return math.sqrt((x - self.x) ** 2 + (y - self.y) ** 2)
def send_to_back(self):
#make this object be drawn first, so all other appear above it if they're in the same tile.
global objects
objects.remove(self)
objects.insert(0, self)
def draw(self):
#only show if it's visible to the player or it's set to always visible and on an explored tile
if (libtcod.map_is_in_fov(fov_map, self.x, self.y) or (self.always_visible and map[self.x][self.y].explored)):
#set the color and then draw the character that represents this object at its position
libtcod.console_set_default_foreground(con, self.color)
libtcod.console_put_char(con, self.x, self.y, self.char, libtcod.BKGND_NONE)
def clear(self):
#erase the character that represents this object
libtcod.console_put_char(con, self.x, self.y, ' ', libtcod.BKGND_NONE)
def describe(self, description):
#describe this object
if self.description is None:
message('The ' + self.owner.name + ' cannot be described.')
else:
message(str(description), libtcod.white)
class Fighter:
#combat-related properties and methods (monster, player, npc).
def __init__(self, hp, strength=0, dexterity=0, stealth=0, will=0, defense_dice=0, defense_sides=0, power_dice=0, power_sides=0, evasion_dice=0, evasion_sides=0, accuracy_dice=0, accuracy_sides=0, crit_dice=0, xp=0, speed=None, speed_counter=0, heal_rate=50, heal_counter=0, death_function=None, effects=[], cast_effect=None, cast_roll=0, paralysis=False):
self.base_max_hp = hp
self.hp = hp
self.base_strength = strength
self.base_dexterity = dexterity
self.base_stealth = stealth
self.base_will = will
self.base_defense_dice = defense_dice
self.base_defense_sides = defense_sides
self.base_power_dice = power_dice
self.base_power_sides = power_sides
self.base_evasion_dice = evasion_dice
self.base_evasion_sides = evasion_sides
self.base_accuracy_dice = accuracy_dice
self.base_accuracy_sides = accuracy_sides
self.crit_dice = crit_dice
self.xp = xp
self.speed = speed
self.speed_counter = speed_counter
#Standard of 50 means it will heal at the same rate as the player
self.heal_rate = heal_rate
self.heal_counter = heal_counter
self.death_function = death_function
self.effects = effects
self.cast_effect = cast_effect
self.cast_roll = cast_roll
self.paralysis = paralysis
@property
def strength(self):
bonus = sum(equipment.strength_bonus for equipment in get_all_equipped(self.owner))
#Add effect bonuses to max_hp
bonus += (sum(effect.strength_effect for effect in self.effects))
return self.base_strength + bonus
@property
def dexterity(self):
bonus = sum(equipment.dexterity_bonus for equipment in get_all_equipped(self.owner))
#Add effect bonuses to max_hp
bonus += (sum(effect.dexterity_effect for effect in self.effects))
return self.base_dexterity + bonus
@property
def stealth(self):
bonus = sum(equipment.stealth_bonus for equipment in get_all_equipped(self.owner))
#Add effect bonuses to max_hp
bonus += (sum(effect.stealth_effect for effect in self.effects))
return self.base_stealth + bonus
@property
def will(self):
bonus = sum(equipment.will_bonus for equipment in get_all_equipped(self.owner))
#Add effect bonuses to max_hp
bonus += (sum(effect.will_effect for effect in self.effects))
return self.base_will + bonus
@property
def power(self): #return actual power, by summing up the bonuses from all equipped items
#Add dice/sides from equipment
bonus_dice = (sum(equipment.power_bonus_dice for equipment in get_all_equipped(self.owner))+(self.base_power_dice)+(self.crit_dice))
#Currently adds equipment bonuses, equipped bonuses, plus 1 face for every 2 strength points
bonus_sides = (sum(equipment.power_bonus_sides for equipment in get_all_equipped(self.owner))+(self.base_power_sides))+(self.strength/2)
#Add dice/sides from effects
bonus_dice += (sum(effect.power_effect_dice for effect in self.effects))
bonus_sides += (sum(effect.power_effect_sides for effect in self.effects))
#No lower than 1
if bonus_dice <= 1:
bonus_dice = 1
if bonus_sides <= 1:
bonus_sides = 1
#Create list with dice and sides as it's two entries
dice_sides = []
dice_sides.append(bonus_dice)
dice_sides.append(bonus_sides)
#Return the list
return dice_sides
@property
def defense(self): #return actual+ defense, by summing up the bonuses from all equipped items
bonus_dice = (sum(equipment.defense_bonus_dice for equipment in get_all_equipped(self.owner))+(self.base_defense_dice))
bonus_sides = (sum(equipment.defense_bonus_sides for equipment in get_all_equipped(self.owner))+(self.base_defense_sides))
#Need to integrate effects below
bonus_dice += (sum(effect.defense_effect_dice for effect in self.effects))
bonus_sides += (sum(effect.defense_effect_sides for effect in self.effects))
#No lower than 1
if bonus_dice <= 1:
bonus_dice = 1
if bonus_sides <= 1:
bonus_sides = 1
dice_sides = []
dice_sides.append(bonus_dice)
dice_sides.append(bonus_sides)
return dice_sides
@property
def max_hp(self): #return actual max_hp, by summing up the bonuses from all equipped items
#This stays unaffected
bonus = sum(equipment.max_hp_bonus for equipment in get_all_equipped(self.owner))
#Add effect bonuses to max_hp
bonus += (sum(effect.max_hp_effect for effect in self.effects))
return self.base_max_hp + bonus
@property
def evasion(self): #return actual evasion
bonus_dice = (sum(equipment.evasion_bonus_dice for equipment in get_all_equipped(self.owner))+(self.base_evasion_dice))
#Add all equipment + one point for each in dexterity
bonus_sides = (sum(equipment.evasion_bonus_sides for equipment in get_all_equipped(self.owner))+(self.base_evasion_sides)+(self.dexterity/2))
#Need to integrate effects below
bonus_dice += (sum(effect.evasion_effect_dice for effect in self.effects))
bonus_sides += (sum(effect.evasion_effect_sides for effect in self.effects))
#No lower than 1
if bonus_dice <= 1:
bonus_dice = 1
if bonus_sides <= 1:
bonus_sides = 1
dice_sides = []
dice_sides.append(bonus_dice)
dice_sides.append(bonus_sides)
return dice_sides
@property
def accuracy(self): #return actual accuracy
#Equipment bonus, plus one die for every five points of strength
bonus_dice = (sum(equipment.accuracy_bonus_dice for equipment in get_all_equipped(self.owner))+(self.base_accuracy_dice)+(self.strength/7))
#Adds all equipped, all effects, plus one for every 2 points of dexterity
bonus_sides = (sum(equipment.accuracy_bonus_sides for equipment in get_all_equipped(self.owner))+(self.base_accuracy_sides)+(self.dexterity/2))
#Need to integrate effects below
bonus_dice += (sum(effect.accuracy_effect_dice for effect in self.effects))
bonus_sides += (sum(effect.accuracy_effect_sides for effect in self.effects))
#No lower than 1
if bonus_dice <= 1:
bonus_dice = 1
if bonus_sides <= 1:
bonus_sides = 1
dice_sides = []
dice_sides.append(bonus_dice)
dice_sides.append(bonus_sides)
return dice_sides
def player_eat_food(self, Item):
global hunger_level
if hunger_level >= HUNGER_TOTAL - HUNGER_WARNING:
message('You are already full!', libtcod.white)
else:
message('Mmm, that was delicious!', libtcod.light_green)
if hunger_level + Item.food_value > HUNGER_TOTAL:
hunger_level = HUNGER_TOTAL
else:
hunger_level += Item.food_value
def add_effect(self, Effect, object_origin_name): # Add effect to the fighter class's list of effects
# check if the effect already exists, if it does, just increase the duration
#if effects is not empty, iterate through them
if self.effects != []:
#create a variable to hold a statement that changes if the i.effectname is the same as the one already applied
duplicate=False
#Iterate through them
for i in self.effects:
#If an effect with the same name already exists and duplicate hasn't been changed, add its duration to the existing copy.
if i.effect_name == Effect.effect_name and duplicate==False:
#Update variable to stop the iteration through self.effects, as we've found a copy already. This stops duplication.
duplicate=True
if i.mutation == False:
#Increase this effects duration rather than adding multiple effects, could be done either way but this is simpler.
i.duration += Effect.base_duration
#Increase the variable applied_times which is used in other functions like render_bar() to show how many times poison has been applied
i.applied_times += 1
#Tell the player what happened
message('The ' + object_origin_name + ' has ' + Effect.effect_name + ' you further!',
libtcod.white)
elif i.mutation == True:
i.applied_times += 1
#Else, if a duplicate has not been found.
if duplicate == False and Effect.mutation == False:
#Add the effect
self.effects.append(Effect)
#Tell the player what happened
message('The ' + object_origin_name + ' has ' + Effect.effect_name + ' you!', libtcod.yellow)
elif duplicate == False and Effect.mutation == True:
#Add effect
self.effects.append(Effect)
#Tell the player what happened
message(object_origin_name + Effect.effect_name + '!' + ' Press space to continue.', libtcod.yellow)
elif Effect.mutation == True:
self.effects.append(Effect)
#Set duration to base duration
Effect.duration = Effect.base_duration
message(object_origin_name + ' ' + Effect.effect_name + '!' + ' Press space to continue.', libtcod.yellow)
update_msgs()
wait_for_spacekey()
#Else, effects is empty and this effect is not a mutation, so just add it to the player and tell them what happened.
else:
self.effects.append(Effect)
message('The ' + object_origin_name + ' has ' + Effect.effect_name + ' you!', libtcod.yellow)
def remove_effect(self, Effect):
if Effect.duration == Effect.turns_passed:
Effect.applied_times = 1
self.effects.remove(Effect)
def take_damage(self, damage):
global fov_recompute
#apply damage if possible
if damage > 0:
self.hp -= damage
#check for death, if there's a death function, call it.
if self.hp <= 0:
function = self.death_function
if function is not None:
function(self.owner)
if self.owner != player: #yield experience to the player
player.fighter.xp += self.xp
#Tell display_damage() in render_all() to fire on this tile
self.owner.display_dmg = damage
def attack(self, target):
#a simple formula for attack damage
evasion = roll_for(target.fighter.evasion)
accuracy = roll_for(self.accuracy)
#If evasion is smaller or equal to accuracy
if evasion <= accuracy:
#check for crit if player is the attacker
if self.owner.name == 'player':
difference = accuracy - evasion
if difference >= 10:
#color the floor with blood
libtcod.console_set_char_background(con, target.x, target.y, libtcod.darker_red, libtcod.BKGND_SET)
map[target.x][target.y].diff_color = [libtcod.darkest_red, libtcod.dark_red]
message('You deal a devastating critical blow to the ' + str(target.name) + '!', libtcod.white)
#Size of critical boost, later will roll for this damage
if difference >= 10:
self.crit_dice=1
elif difference <= 15:
self.crit_dice=2
elif difference <= 20:
self.crit_dice=3
elif difference <= 25:
self.crit_dice=4
#Call rolls for power and defense
power = roll_for(self.power)
defense = roll_for(target.fighter.defense)
#Set crit damage to 0 now that it has been calculated and stored in power variable
self.crit_dice=0
#Calculate damage
damage = power - defense
# #Player messages and colors##
if damage > 0 and self.owner.name == 'player':
#make the target take some damage and print the value
message('You hit the ' + target.name + ' for ' + str(damage) + ' damage!', libtcod.green)
target.fighter.take_damage(damage)
elif damage <= 0 and self.owner.name == 'player':
#else print a message about how puny you are
message('You hit the ' + target.name + ' but it has no effect!', libtcod.grey)
##Monster messages and colors##
elif damage > 0 and self.owner.name != 'player':
#make the target take some damage and print the value
message(self.owner.name.capitalize() + ' hit you for ' + str(damage) + ' damage!', libtcod.red)
target.fighter.take_damage(damage)
self.roll_for_effect(target)
elif damage <= 0 and self.owner.name != 'player':
message(self.owner.name.capitalize() + ' hits you but it has no effect!', libtcod.grey)
self.roll_for_effect(target)
elif self.owner.name == 'player' and evasion > accuracy:
message('You missed the ' + target.name + '!', libtcod.white)
elif self.owner.name != 'player' and evasion > accuracy:
message('The ' + self.owner.name.capitalize() + ' missed you!', libtcod.white)
quality_string = None
damage = 0
# check for auto cast_effect
def roll_for_effect(self, target):
#if the fighter has an effect to be cast, and the roll is > cast_roll - add effect to target.
roll = libtcod.random_get_int(0, 0, 100)
if self.cast_effect != None and roll <= self.cast_roll:
target.fighter.add_effect(self.cast_effect, self.owner.name)
def heal(self, amount):
#heal by the given amount, without going over the maximum
self.hp += amount
if self.hp > self.max_hp:
self.hp = self.max_hp
class BasicMonsterAI:
global path
#AI for a basic monster.
def take_turn(self):
#A basic monster takes its turn. If you can see it, it can see you.
monster = self.owner
#Check for existing path
if monster.path == None:
monster_move_or_attack(monster)
else:
monster_move_or_attack(monster)
class ConfusedMonster:
#AI for a temporarily confused monster, reverts to normal AI after a while
def __init__(self, old_ai, num_turns=CONFUSE_NUM_TURNS):
self.old_ai = old_ai
self.num_turns = num_turns
def take_turn(self):
if self.num_turns > 0: #still confused...
#move in a random direction, and decrease the number of turns confused
self.owner.move(libtcod.random_get_int(0, -1, 1), libtcod.random_get_int(0, -1, 1))
self.num_turns -= 1
else: #restore the previous AI (this one will be deleted because it's not referenced anymore)
self.owner.ai = self.old_ai
message('The ' + self.owner.name + ' is no longer confused!', libtcod.red)
class Item:
#An item that can be picked up and used
def __init__(self, use_function=None, pick_up_function=None, food_value=None):
self.use_function = use_function
self.pick_up_function = pick_up_function
self.food_value = food_value
def use(self):
#special case: if the object has the Equipment component, the "use" action is to equip/dequip
if self.owner.equipment:
self.owner.equipment.toggle_equip()
return
elif self.food_value != None:
player.fighter.player_eat_food(self)
inventory.remove(self.owner)
#just call the "use function" if it is defined
elif self.use_function is None:
message('The ' + self.owner.name + ' cannot be used.')
else:
if self.use_function() != 'cancelled':
inventory.remove(self.owner) #destroy after use, unless it was cancelled for some reason
def pick_up(self):
if self.pick_up_function != None:
#the object has a pickup function, so use it and break the loop
self.pick_up_function()
#add to the player's inventory and remove from the map
if len(inventory) >= 26:
message('Your inventory is too full, cannot pick up ' + self.owner.name + '.', libtcod.yellow)
else:
inventory.append(self.owner)
objects.remove(self.owner)
message('You picked up a ' + self.owner.name + '!', libtcod.green)
#special case: automatically equip if the corresponding equipment slot is unused
equipment = self.owner.equipment
if equipment and get_equipped_in_slot(equipment.slot) is None:
equipment.equip()
def drop(self):
#add to the map and remove from the player's inventory. Also, place it at the players coordinates
objects.append(self.owner)
inventory.remove(self.owner)
self.owner.x = player.x
self.owner.y = player.y
message('You dropped a ' + self.owner.name + '.', libtcod.yellow)
#special case: if the object has the Equipment component, dequip it before dropping
if self.owner.equipment:
self.owner.equipment.dequip()
class Equipment:
#an object that can be equipped, yielding bonuses. Automatically adds the Item component
def __init__(self, slot, weapon=False, ranged=False, strength_bonus=0, dexterity_bonus=0, stealth_bonus=0, will_bonus=0, power_bonus_dice=0, power_bonus_sides=0, defense_bonus_dice=0, defense_bonus_sides=0, evasion_bonus_dice=0, evasion_bonus_sides=0, accuracy_bonus_dice=0, accuracy_bonus_sides=0, max_hp_bonus=0, acc_penalty=0):
self.weapon = weapon
self.ranged = ranged
self.strength_bonus = strength_bonus
self.dexterity_bonus = dexterity_bonus
self.stealth_bonus = stealth_bonus
self.will_bonus = will_bonus
self.power_bonus_dice = power_bonus_dice
self.power_bonus_sides = power_bonus_sides
self.defense_bonus_dice = defense_bonus_dice
self.defense_bonus_sides = defense_bonus_sides
self.evasion_bonus_dice = evasion_bonus_dice
self.evasion_bonus_sides = evasion_bonus_sides
self.accuracy_bonus_dice = accuracy_bonus_dice
self.accuracy_bonus_sides = accuracy_bonus_sides
self.max_hp_bonus = max_hp_bonus
self.acc_penalty = acc_penalty
self.slot = slot
self.is_equipped = False
def toggle_equip(self): #toggle equip/dequip status
if self.is_equipped:
self.dequip()
else:
self.equip()
def equip(self):
#if the slot is already being used, dequip whatever is there first
old_equipment = get_equipped_in_slot(self.slot)
if old_equipment is not None:
old_equipment.dequip()
#equip object and show a message about it
self.is_equipped = True
message('Equipped ' + self.owner.name + ' on ' + self.slot + '.', libtcod.light_green)
def dequip(self):
#dequip object and show a message about it
if not self.is_equipped: return
self.is_equipped = False
message('Dequipped ' + self.owner.name + ' from ' + self.slot + '.', libtcod.light_yellow)
class Effect:
#an effect that can be applied to the character, yielding bonuses or nerfs
def __init__(self, effect_name=None, duration=0, turns_passed=0, base_duration=0, strength_effect=0, power_effect_dice=0, dexterity_effect=0, stealth_effect=0, will_effect=0, power_effect_sides=0, defense_effect_dice=0, defense_effect_sides=0, evasion_effect_dice=0, evasion_effect_sides=0, accuracy_effect_dice=0, accuracy_effect_sides=-0,
max_hp_effect=0, applied_times=1, confused=False, burning=False, damage_by_turn=None, paralyzed=None,
fatal_alert=False, mutation=False, m_loop=0, m_loop_turn=0, m_elec=False, m_h_timeslip=False, m_a_roar=False, m_count=0,
m_trigger=5, m_damage=0, m_cast_effect=None, forget=False, blind=False):
self.effect_name = effect_name
self.duration = duration
self.turns_passed = turns_passed
self.base_duration = base_duration
self.strength_effect = strength_effect
self.dexterity_effect = dexterity_effect
self.stealth_effect = stealth_effect
self.will_effect = will_effect
self.power_effect_dice = power_effect_dice
self.power_effect_sides = power_effect_sides
self.defense_effect_dice = defense_effect_dice
self.defense_effect_sides = defense_effect_sides
self.evasion_effect_dice = evasion_effect_dice
self.evasion_effect_sides = evasion_effect_sides
self.accuracy_effect_dice = accuracy_effect_dice
self.accuracy_effect_sides = accuracy_effect_sides
self.max_hp_effect = max_hp_effect
self.applied_times = applied_times
self.confused = confused
self.burning = burning
self.damage_by_turn = damage_by_turn
self.paralyzed = paralyzed
self.fatal_alert = fatal_alert
self.is_active = False
self.mutation = mutation
#For counting down an active effect to become inactive - TODO: rename this to something clearer
self.m_loop = m_loop
self.m_loop_turn = m_loop_turn
self.m_elec = m_elec
self.m_h_timeslip = m_h_timeslip
self.m_a_roar = m_a_roar
#Counting the charging of an effect
self.m_count = m_count
self.m_trigger = m_trigger
self.m_damage = m_damage
self.m_cast_effect = m_cast_effect
self.forget = forget
self.blind = blind
def monster_move_or_attack(monster):
global dungeon_level
#If the monster is in the players FOV, monster can see player.
if libtcod.map_is_in_fov(fov_map, monster.x, monster.y): #If the monster is in the players FOV
# pygmys can attack one block further in every direction.
#if monster.char == 'p' and monster.distance_to(player) <= 2:
#if player.fighter.hp > 0:
#monster.fighter.attack(player)
if monster.name == 'Zeus':
if monster.distance_to(player) < 2 and player.fighter.hp > 0:
monster.fighter.attack(player)
check_run_effects(monster)
elif monster.distance_to(player) <= 8:
monster.move_towards(player.x, player.y)
#As monster has moved, check_run effects for that monster
check_run_effects(monster)
#move towards player if far away
elif monster.distance_to(player) >= 2:
#compute how to reach the player
#Move monster
monster.move_towards(player.x, player.y)
#As monster has moved, check_run effects for that monster
check_run_effects(monster)
#close enough, attack! (if the player is still alive.)
elif player.fighter.hp > 0:
if monster.name == 'Cerebus':
monster.fighter.attack(player)
monster.fighter.attack(player)
monster.fighter.attack(player)
check_run_effects(monster)
else:
monster.fighter.attack(player)
check_run_effects(monster)
else:
#the player cannot see the monster
#if we have an old path, follow it
#If path is not empty and the distance to the player is greater than 2
if monster.path != None and libtcod.path_is_empty(monster.path) == False:
nextx, nexty = libtcod.path_walk(monster.path, True)
if nextx or nexty is not None:
dx = nextx - monster.x
dy = nexty - monster.y
monster.move(dx, dy)
else:
print 'no nextx or nexty line 770'
#stop boar and baby boars and pygmys from wandering
#elif monster.char != 'p' or dungeon_level != 10:
#path is empty, wander randomly
#rand_direction = libtcod.random_get_int(0, 1, 12)
#if rand_direction == 1:
#monster.move(-1, 1)
#elif rand_direction == 2:
# monster.move(0, 1)
# elif rand_direction == 3:
# monster.move(1, 1)
# elif rand_direction == 4:
# monster.move(-1, 0)
# elif rand_direction == 5:
## monster.move(1, 0)
# elif rand_direction == 6:
# monster.move(-1, -1)
# elif rand_direction == 7:
# monster.move(0, -1)
# elif rand_direction == 8:
# monster.move(1, -1)
# else:
# #Don't wander
# monster.move(0, 0)
else:
return 'cancelled'
def path_func(xFrom, yFrom, xTo, yTo, self):
global map
if libtcod.map_is_walkable(fov_map, xTo, yTo) == True: #open space
return 1.0 #All good!
elif libtcod.map_is_walkable(fov_map, xTo, yTo) == False: #wall
return 0.0 #Not good!
def roll(dice, sides):
result = 0
for i in range(0, dice, 1):
roll = libtcod.random_get_int(0, 1, sides)
result += roll
def check_run_effects(obj):
global fov_recompute
global TORCH_RADIUS, ELEC_FIRING, torch_color, torch_light_color
# Check for effects, if there is 1 or more and their turns_passed value is not == duration, increase its turn_passed value by one, if it is equal to duration remove it.
if obj.fighter.effects is not None:
for eff in obj.fighter.effects:
#Deal with non mutation effects
if eff.mutation == False:
# If it is the first time
if eff.turns_passed == 0:
is_active = True
#Run for damage_by_turn value in effects class, if there is a value, damage the obj by that value:
if eff.damage_by_turn is not None:
obj.fighter.take_damage(eff.damage_by_turn)
# check for fatal turn_by_damage limit if effect has damage_by_turn
if eff.damage_by_turn is not None:
turns_left = eff.duration - eff.turns_passed
total_dmg = turns_left * eff.damage_by_turn
#If total damage to be dealt is larger than the players hp and the players hp is 1 or more
if total_dmg >= obj.fighter.hp and obj.fighter.hp >= 1: