forked from Hakaponttoauto/TYRMA
/
main.py
1241 lines (1072 loc) · 47.7 KB
/
main.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
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 tcod as libtcod
import math
import textwrap
import shelve
import time
import random
from playsound import playsound
import copy
#actual size of the window
SCREEN_WIDTH = 80
SCREEN_HEIGHT = 40
#sizes and coordinates relevant for the GUI
BAR_WIDTH = 20
PANEL_HEIGHT = 7
PANEL_Y = SCREEN_HEIGHT - PANEL_HEIGHT
MSG_X = BAR_WIDTH + 2
MSG_WIDTH = SCREEN_WIDTH - BAR_WIDTH - 2
MSG_HEIGHT = PANEL_HEIGHT - 2
INVENTORY_WIDTH = 50
#size of the map
MAP_WIDTH = 80
MAP_HEIGHT = 30
#parameters for dungeon generator
ROOM_MAX_SIZE = 10
ROOM_MIN_SIZE = 6
rooms=[]
FOV_ALGO = 2 #default FOV algorithm
FOV_LIGHT_WALLS = True #light walls or not
TORCH_RADIUS = 15
HEAL_AMOUNT=10
#experience and level-ups
LEVEL_UP_BASE = 200
LEVEL_UP_FACTOR = 150
LIMIT_FPS = 20 #20 frames-per-second maximum
color_dark_wall = libtcod.Color(50, 50, 50)
color_light_wall = libtcod.Color(120,110,100)
color_dark_ground = libtcod.Color(30, 30, 40)
color_light_ground = libtcod.Color(60,55,50)
def clamp(minimum, x, maximum):
return max(minimum, min(x, maximum))
def new_game():
global player, inventory, game_msgs, game_state, dungeon_level
name="Seikkailija"
#create object representing the player
fighter_component = Fighter(hp=30, hunger=20, defense=2, power=5, xp=0, death_function=player_death)
player = Object(0, 0, '@', name, libtcod.white, blocks=True, fighter=fighter_component, player=True)
player.level = 1
#generate map (at this point it's not drawn to the screen)
dungeon_level = 1
make_map()
game_state = 'playing'
inventory = []
#create the list of game messages and their colors, starts empty
game_msgs = []
#a warm welcoming message!
initialize_fov()
# -*- coding: utf-8 -*-
message("Astut pimeään tyrmään, tavoitteenasi löytää Urho Kekkosen kadonnut muumioitunut pää.")
def initialize_fov():
libtcod.console_clear(con)
global fov_recompute, fov_map
fov_recompute = True
#create the FOV map, according to the generated map
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 save_game():
#open a new empty shelve (possibly overwriting an old one) to write the game data
file = shelve.open('savegame', 'n')
file['map'] = map
file['objects'] = objects
file['player_index'] = objects.index(player) #index of player in objects list
file['inventory'] = inventory
file['game_msgs'] = game_msgs
file['game_state'] = game_state
file['stairs_index'] = objects.index(stairs)
file['dungeon_level'] = dungeon_level
file.close()
def load_game():
#open the previously saved shelve and load the game data
global map, objects, player, inventory, game_msgs, game_state,stairs,dungeon_level
file = shelve.open('savegame', 'r')
map = file['map']
objects = file['objects']
player = objects[file['player_index']] #get index of player in objects list and access it
inventory = file['inventory']
game_msgs = file['game_msgs']
game_state = file['game_state']
stairs=objects[file['stairs_index']]
dungeon_level = file['dungeon_level']
file.close()
initialize_fov()
def check_level_up():
#see if the player's experience is enough to level-up
level_up_xp = LEVEL_UP_BASE + player.level * LEVEL_UP_FACTOR
if player.fighter.xp >= level_up_xp:
#it is! level up
player.level += 1
player.fighter.xp -= level_up_xp
playsound('assets/sounds/levelup.wav')
message('Tunnet kokemuksesi karttuneen! Saavutat tason ' + str(player.level) + '!', libtcod.yellow)
choice=libtcod.random_get_int(0, 0, 2)
if choice == 0:
player.fighter.base_max_hp += 20
player.fighter.hp += 20
message('Elinvoimasi kasvaa!', libtcod.yellow)
elif choice == 1:
player.fighter.base_power += 1
message('Voimasi kasvavat!', libtcod.yellow)
elif choice == 2:
player.fighter.base_defense += 1
message('Puolustuksesi kasvaa!', libtcod.yellow)
class Point:
def __init__(self,x,y):
self.x=x
self.y=y
class Tile:
#a tile of the map and its properties
def __init__(self, blocked, block_sight = None,char=" ",color=libtcod.gray,bgcolor=color_light_ground):
self.blocked = blocked
self.char=char
self.color=color
self.bgcolor=bgcolor
#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 characterize 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 (int(center_x), int(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, blocks=False, actions={}, fighter=None, ai=None, item=None, equipment=None, player=False):
self.name = name
self.blocks = blocks
self.x = x
self.y = y
self.char = char
self.color = color
self.player = player
self.actions=actions
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 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
def move(self, dx, dy):
#move by the given amount, if the destination is not blocked
blocking=is_blocked(self.x + dx, self.y + dy)
if blocking==None:
self.x += dx
self.y += dy
#self.send_to_fore()
bump = get_object(self.x + dx, self.y + dy)
if bump!=None and "bump" in bump.actions:
bump.actions[bump](self)
return True
else:
if blocking!=True and "bump" in blocking.actions:
blocking.actions["bump"](self)
return False
def draw(self):
#only show if it's visible to the player
if libtcod.map_is_in_fov(fov_map, self.x, self.y):
#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 move_towards(self, target_x, target_y):
#vector from this object to the 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 (preserving direction), then round it and
#convert to integer so the movement is restricted to the map grid
dx = int(round(dx / distance))
dy = int(round(dy / distance))
self.move(dx, dy)
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 send_to_back(self):
#This code bugs, remember to fix it before using.
global objects
objects.remove(self)
objects.insert(0, self)
def send_to_fore(self):
global objects
objects.remove(self)
objects.append(self)
class Fighter:
#combat-related properties and methods (monster, player, NPC).
def __init__(self, hp, hunger, defense, power, xp, death_function=None, fight_messages=["@1 kamppailee @2a vastaan"]):
self.xp = xp
self.death_function = death_function
self.base_max_hp = hp
self.hp = hp
self.base_max_hunger = hunger
self.hunger = hunger
self.base_defense = defense
self.base_power = power
self.fight_messages=fight_messages
def take_damage(self, damage,attacker=None):
if self.hunger > 0:
self.hunger -= 2
#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)
playsound('assets/sounds/death.wav')
if attacker is not None:
attacker.xp += self.xp
def attack(self, target):
#a simple formula for attack damage
damage = self.power - target.fighter.defense
colorr=[libtcod.red,libtcod.green]
if self.owner.player:
colorr=[libtcod.green,libtcod.red]
playsound('assets/sounds/playerhit.wav')
else:
playsound('assets/sounds/enemyhit.wav')
if damage > 0:
#make the target take some damage
message(random.choice(self.fight_messages).replace("@1",self.owner.name).replace("@2",target.name), colorr[0],self.owner.x,self.owner.y)
target.fighter.take_damage(damage,self)
else:
message(random.choice(self.fight_messages).replace("@1",self.owner.name).replace("@2",target.name) + ', ilman vaikutusta!', colorr[1],self.owner.x,self.owner.y)
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
def feed(self, amount):
#feed by the given amount, without going over the maximum
self.hunger += amount
if self.hunger > self.base_max_hunger:
self.hunger = self.base_max_hunger
@property
def power(self):
bonus = sum(equipment.power_bonus for equipment in get_all_equipped(self.owner))
return self.base_power + bonus
@property
def defense(self): #return actual defense, by summing up the bonuses from all equipped items
bonus = sum(equipment.defense_bonus for equipment in get_all_equipped(self.owner))
return self.base_defense + bonus
@property
def max_hp(self): #return actual max_hp, by summing up the bonuses from all equipped items
bonus = sum(equipment.max_hp_bonus for equipment in get_all_equipped(self.owner))
return self.base_max_hp + bonus
class NoobMonster:
#AI for a basic monster.
def take_turn(self):
monster=self.owner
move=True
for object in objects:
if object!=monster and hasattr(object,"fighter") and object.fighter is not None and monster.distance_to(object) < 2 and object.fighter.hp>0:
monster.fighter.attack(object)
move=False
if move:
sx=random.randint(-1,1)
sy=random.randint(-1,1)
monster.move(sx,sy)
class BasicMonster:
def __init__(self,period=1):
self.period=period
self.tick=0
#AI for a basic monster.
def take_turn(self):
self.tick+=1
moved=False
monster = self.owner
if self.tick%self.period==0:
#a basic monster takes its turn. If you can see it, it can see you
if libtcod.map_is_in_fov(fov_map, monster.x, monster.y):
#move towards player if far away
if monster.distance_to(player) >= 2:
moved=monster.move_towards(player.x, player.y)
#close enough, attack! (if the player is still alive.)
if not moved and player.fighter.hp > 0 and monster.distance_to(player) < 2:
monster.fighter.attack(player)
class AdvancedMonster:
def __init__(self,period=1):
self.period=period
self.tick=0
def take_turn(self):
self.tick+=1
moved=False
monster = self.owner
if self.tick%self.period==0:
try:
if self.path==None:
self.path = libtcod.path_new_using_map(path_map)
except:
self.path = libtcod.path_new_using_map(path_map)
success=libtcod.path_compute(self.path, monster.x, monster.y, player.x, player.y)
stepx,stepy=libtcod.path_walk(self.path,True)
#move towards player if far away
if monster.distance_to(player) >= 2 and success:
moved=monster.move(stepx-monster.x,stepy-monster.y)
#close enough, attack! (if the player is still alive.)
for object in objects:
if object!=monster and hasattr(object,"fighter") and object.fighter is not None and monster.distance_to(object) < 2 and object.fighter.hp>0 and not (object==player and moved):
monster.fighter.attack(object)
class Wandering:
def __init__(self):
self.success=False
def randomize_target(self):
blocked=True
room = random.choice(rooms)
while blocked is not None:
x = libtcod.random_get_int(0, room.x1+1, room.x2-1)
y = libtcod.random_get_int(0, room.y1+1, room.y2-1)
blocked=is_blocked(x,y)
self.target_x=y
self.target_y=x
self.recalculate()
def recalculate(self):
monster = self.owner
self.success=libtcod.path_compute(self.path, monster.x, monster.y, self.target_x, self.target_y)
def take_turn(self):
monster = self.owner
try:
if self.path==None:
self.path = libtcod.path_new_using_map(copy.deepcopy(path_map))
except:
self.path = libtcod.path_new_using_map(copy.deepcopy(path_map))
try:
if self.target_x is None or self.target_y is None:
self.randomize_target()
except:
self.randomize_target()
stepx,stepy=0,0
moved=False
if monster.distance_to(Point(self.target_x,self.target_y)) >= 2 and self.success:
stepx,stepy=libtcod.path_walk(self.path,False)
moved=monster.move(stepx-monster.x,stepy-monster.y)
if not moved:
if stepx!=0 and stepy!=0:
blocking=get_object(stepx,stepy)
if blocking is not None and hasattr(blocking,"fighter") and blocking.fighter is not None:
self.recalculate()
if blocking.fighter.hp > 0 and monster.distance_to(blocking) < 2:
monster.fighter.attack(blocking)
else:
self.randomize_target()
else:
self.randomize_target()
class Item:
#an item that can be picked up and used.
def __init__(self, use_function=None,use_arguments=None):
self.use_function = use_function
self.use_arguments = use_arguments
def pick_up(self):
#add to the player's inventory and remove from the map
if len(inventory) >= 26:
message(self.owner.name + 'ei mahdu reppuusi.', libtcod.yellow)
else:
inventory.append(self.owner)
objects.remove(self.owner)
playsound('assets/sounds/pickup.wav')
message('Otat ' + self.owner.name + 'n maasta.', libtcod.sepia)
def use(self):
if self.owner.equipment:
self.owner.equipment.toggle_equip()
return
#just call the "use_function" if it is defined
if self.use_function is None:
message(self.owner.name + 'a ei voi kuluttaa.', libtcod.sepia)
else:
if self.use_function(player,self,self.use_arguments) != 'cancelled':
inventory.remove(self.owner) #destroy after use, unless it was cancelled for some reason
def drop(self):
#add to the map and remove from the player's inventory. also, place it at the player's coordinates
objects.append(self.owner)
inventory.remove(self.owner)
self.owner.x = player.x
self.owner.y = player.y
#special case: if the object has the Equipment component, dequip it before dropping
if self.owner.equipment:
self.owner.equipment.dequip()
message('Tiputit ' + self.owner.name + 'n.', libtcod.yellow)
class Equipment:
#an object that can be equipped, yielding bonuses. automatically adds the Item component.
def __init__(self, slot, power_bonus=0, defense_bonus=0, max_hp_bonus=0):
self.power_bonus = power_bonus
self.defense_bonus = defense_bonus
self.max_hp_bonus = max_hp_bonus
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):
#equip object and show a message about it
#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()
self.is_equipped = True
message(self.slot + 'si puristaa nyt ' + self.owner.name + 'a', libtcod.yellow)
def dequip(self):
#dequip object and show a message about it
if not self.is_equipped: return
self.is_equipped = False
message('Laitat ' + self.owner.name + 'n takaisin reppuusi.', libtcod.yellow)
def get_equipped_in_slot(slot): #returns the equipment in a slot, or None if it's empty
for obj in inventory:
if obj.equipment and obj.equipment.slot == slot and obj.equipment.is_equipped:
return obj.equipment
return None
def get_all_equipped(obj): #returns a list of equipped items
if obj == player:
equipped_list = []
for item in inventory:
if item.equipment and item.equipment.is_equipped:
equipped_list.append(item.equipment)
return equipped_list
else:
return [] #other objects have no equipment
def player_death(player):
#the game ended!
global game_state
message('KUKISTUIT!',libtcod.red)
game_state = 'dead'
#for added effect, transform the player into a corpse!
player.char = '%'
player.color = libtcod.dark_red
def monster_death(monster):
#transform it into a nasty corpse! it doesn't block, can't be
#attacked and doesn't move
message(monster.name.capitalize() + ' kukistui!',libtcod.green,monster.x,monster.y)
monster.char = '%'
monster.color = libtcod.dark_red
monster.blocks = False
monster.fighter = None
monster.ai = None
monster.name = monster.name + "n ruumis"
monster.send_to_back()
def spell_heal(caster,parent,arguments=[10]):
#heal the player
if caster.fighter.hp == caster.fighter.max_hp:
message('Olet jo elinvoimainen.', libtcod.sepia)
return 'cancelled'
message('Haavasi alkavat tuntua paremmilta!', libtcod.green)
caster.fighter.heal(arguments[0])
def spell_eat(caster,parent,arguments=[10]):
if caster.fighter.hunger == caster.fighter.base_max_hunger:
message('Vatsasi on jo pullollaan.', libtcod.sepia)
return 'cancelled'
message('Ahdat '+parent.owner.name+'a naamaasi, namskis maiskis lurpsis!', libtcod.green)
caster.fighter.feed(arguments[0])
def spell_explode(caster,parent,arguments):
message('KAPYYM!!!!!!! '+parent.owner.name+' posahti!', libtcod.red)
caster.fighter.take_damage(60-(caster.fighter.defense*2))
def arkku_interact(object):
message("Avaat arkun",libtcod.green)
loot=[]
possible=["Taikajuoma","Miekka", "Kilpi","Kakku", "Mokkapala", "Impostor_kakku"]
morkoarkku=False
if libtcod.random_get_int(0,1,30)==1:
morkoarkku=True
for ox in range(-1,2):
for oy in range(-1,2):
if morkoarkku and is_blocked(object.x+ox,object.y+oy)==None:
thing=new_object("Morko")
thing.x=object.x+ox
thing.y=object.y+oy
objects.append(thing)
elif libtcod.random_get_int(0,1,6)==1 and is_blocked(object.x+ox,object.y+oy)==None:
choice=random.choice(possible)
thing=new_object(choice)
thing.x=object.x+ox
thing.y=object.y+oy
objects.append(thing)
loot.append(thing.name)
objects.remove(object)
objects.append(Object(object.x, object.y, "=", "Tyhjä arkku", libtcod.gray, blocks=False))
if morkoarkku:
message("Arkusta hyppää esiin mörköarmeija!",libtcod.red)
elif len(loot)>0:
message("Arkusta löytyy "+", ".join(loot))
else:
message("Arkku on tyhjä!")
#defines objects/monsters
def new_object(what):
objects_list={
"Sompi": Object(0, 0, "S", "Sompi", libtcod.light_green,blocks=True, fighter=Fighter(hp=7, hunger=1000, defense=0, power=10, xp=40, death_function=monster_death), ai=NoobMonster()),
"Morko": Object(0, 0, "M", "Morko", libtcod.green,blocks=True, fighter=Fighter(hp=12, hunger=1000, defense=0, power=5, xp=80, death_function=monster_death), ai=BasicMonster()),
"Kyrssi": Object(0, 0, "K", "Kyrssi", libtcod.green,blocks=True, fighter=Fighter(hp=30, hunger=1000, defense=10, power=10, xp=200, death_function=monster_death), ai=BasicMonster(a)),
"Kaareni": Object(0, 0, "C", "Kaareni", libtcod.gray,blocks=True, fighter=Fighter(hp=20, hunger=1000, defense=0, power=20, xp=60, death_function=monster_death), ai=Wandering()),
"Tomuttaja": Object(0, 0, "T", "Tomuttaja", libtcod.red,blocks=True, fighter=Fighter(hp=10, hunger=1000, defense=5, power=10, xp=100, death_function=monster_death), ai=AdvancedMonster()),
"Taikajuoma": Object(0, 0, "!", "Taikajuoma", libtcod.purple,blocks=False, item = Item(use_function=spell_heal,use_arguments=[10])),
"Puukko": Object(0, 0, "/", "Puukko", libtcod.gray, blocks=False, equipment=Equipment("oikea nyrkki", power_bonus=2)),
"Miekka": Object(0, 0, "/", "Miekka", libtcod.gray, blocks=False, equipment=Equipment("oikea nyrkki", power_bonus=5)),
"Sauva": Object(0, 0, "/", "Sauva", libtcod.white, blocks=False, equipment=Equipment("oikea nyrkki", power_bonus=15)),
"Kilpi": Object(0, 0, "[", "Kilpi", libtcod.white, blocks=False, equipment=Equipment("vasen nyrkki", defense_bonus=5)),
"Kakku": Object(0, 0, "%", "Kakku", libtcod.white, blocks=False, item=Item(use_function=spell_eat,use_arguments=[20])),
"Mokkapala": Object(0, 0, "%", "Mokkapala", libtcod.Color(210,105,30), blocks=False, item = Item(use_function=spell_eat,use_arguments=[15])),
"Impostor_kakku": Object(0, 0, "%", "Kakku", libtcod.gray, blocks=False, item=Item(use_function=spell_explode)),
"Arkku": Object(0, 0, "=", "Arkku", libtcod.yellow, blocks=False, actions={"interact":arkku_interact}),
}
return objects_list[what]
#defines objects/monsters spawnrate
spawn={
1: {"monsters":2,"monster":{"Sompi":99,"Morko":1},"items":1,"item":{"Arkku":90,"Kakku":10, "Mokkapala":10},"-rooms":1,"+rooms":6},
4: {"monsters":4,"monster":{"Sompi":59,"Morko":11,"Kaareni":30},"items":3,"item":{"Puukko":30,"Taikajuoma":60,"Arkku":10, "Kakku":5,"Mokkapala":5},"-rooms":3,"+rooms":6},
7: {"monsters":4,"monster":{"Morko":90,"Sompi":9,"Tomuttaja":1},"items":3,"item":{"Puukko":10, "Taikajuoma":50,"Miekka":20,"Kakku":10, "Mokkapala":10, "Impostor_kakku":10,"Sauva": 5},"-rooms":1, "+rooms":8},
11: {"monsters":3,"monster":{"Morko":60,"Kyrssi":18,"Tomuttaja":2,"Kaareni":20},"items":1,"item":{"Kilpi":1,"Taikajuoma":68,"Miekka":1,"Impostor_kakku":5,"Kakku":10, "Mokkapala":10, "Arkku":15,"Sauva":5},"-rooms":5, "+rooms":12},
}
def from_dungeon_level(table):
#returns a value that depends on level. the table specifies what value occurs after each level, default is 0.
for (value, level) in reversed(table):
if dungeon_level >= level:
return value
return 0
def player_move_or_attack(dx, dy):
global fov_recompute
#the coordinates the player is moving to/attacking
x = player.x + dx
y = player.y + dy
#try to find an attackable object there
target = None
for object in objects:
if object.fighter and object.x == x and object.y == y:
target = object
break
#attack if target found, move otherwise
r=True
if target is not None:
player.fighter.attack(target)
else:
r=player.move(dx, dy)
fov_recompute = True
items=get_names(player.x,player.y)
if items!=None:
message('Maassa on '+items, libtcod.sepia)
return r
def get_names(x,y):
#create a list with the names of all objects at the mouse's coordinates and in FOV
names = [obj.name for obj in objects
if obj.x == x and obj.y == y and not(obj.player) and libtcod.map_is_in_fov(fov_map, obj.x, obj.y)]
namestr = ', '.join(names) #join the names, separated by commas
if len(names)>0:
return namestr.capitalize()
else:
return None
def is_blocked(x, y):
if x<1 or x>MAP_WIDTH-1 or y<1 or y>MAP_HEIGHT-1:
return True
#first test the map tile
if map[x][y].blocked:
return True
#now check for any blocking objects
for object in objects:
if object.blocks and object.x == x and object.y == y:
return object
return None
def get_object(x, y):
for object in objects:
if object.x == x and object.y == y:
return object
return None
def create_room(room):
global map
#go through the tiles in the rectangle and make them passable
for x in range(room.x1, room.x2+1):
for y in range(room.y1, room.y2+1):
if x==room.x1 or x==room.x2 or y==room.y1 or y==room.y2:
if map[x][y].blocked:
map[x][y]=Tile(True, char="#",color=libtcod.gray,bgcolor=color_light_wall)
else:
map[x][y]=Tile(False, char=".",color=libtcod.gray,bgcolor=color_light_ground)
def randomwalk(tiles):
count=0
where=random.choice(rooms).center()
x=random.randint(3,MAP_WIDTH-3)
y=random.randint(3,MAP_HEIGHT-3)
while count<tiles:
if map[x][y].blocked or random.randint(0,4)==4:
count+=1
map[x][y]=Tile(False, char="'",color=libtcod.gray,bgcolor=color_light_ground)
sx=0
sy=0
while (x+sx<2 or x+sx>MAP_WIDTH-2 or y+sy<2 or y+sy>MAP_HEIGHT-2 or (sx==0 and sy==0)):
sx=random.randint(-1,1)
sy=random.randint(-1,1)
x,y=x+sx,y+sy
def random_choice_index(chances): #choose one option from list of chances, returning its index
#the dice will land on some number between 1 and the sum of the chances
dice = libtcod.random_get_int(0, 1, sum(chances))
#go through all chances, keeping the sum so far
running_sum = 0
choice = 0
for w in chances:
running_sum += w
#see if the dice landed in the part that corresponds to this choice
if dice <= running_sum:
return choice
choice += 1
return choice
def random_choice(chances_dict):
#choose one option from dictionary of chances, returning its key
chances = chances_dict.values()
strings = list(chances_dict.keys())
return strings[random_choice_index(chances)]
def place_objects(room):
error=True
level=dungeon_level
while error:
try:
max_monsters = spawn[level]["monsters"]
monster_chances = spawn[level]["monster"]
max_items = spawn[level]["items"]
item_chances = spawn[level]["item"]
error=False
except KeyError:
level-=1
#choose random number of monsters
num_monsters = libtcod.random_get_int(0, 0, max_monsters)
for i in range(num_monsters):
#choose random spot for this monster
x = libtcod.random_get_int(0, room.x1+1, room.x2-1)
y = libtcod.random_get_int(0, room.y1+1, room.y2-1)
if is_blocked(x, y)==None:
choice=random_choice(monster_chances)
monster=new_object(choice)
monster.x=x
monster.y=y
objects.append(monster)
#choose random number of items
num_items = libtcod.random_get_int(0, 0, max_items)
for i in range(num_items):
#choose random spot for this item
x = libtcod.random_get_int(0, room.x1+1, room.x2-1)
y = libtcod.random_get_int(0, room.y1+1, room.y2-1)
#only place it if the tile is not blocked
if is_blocked(x, y)==None:
choice=random_choice(item_chances)
item=new_object(choice)
item.x=x
item.y=y
objects.append(item)
def create_h_tunnel(x1, x2, y):
global map
#horizontal tunnel. min() and max() are used in case x1>x2
for x in range(min(x1, x2), max(x1, x2) + 1):
map[x][y]=Tile(False, char=".",color=libtcod.gray,bgcolor=color_light_ground)
def create_v_tunnel(y1, y2, x):
global map
#vertical tunnel
for y in range(min(y1, y2), max(y1, y2) + 1):
map[x][y]=Tile(False, char=".",color=libtcod.gray,bgcolor=color_light_ground)
def next_level():
global dungeon_level
#advance to the next level
if player.fighter.hunger > 0:
message('Otat hetken lepotauon.', libtcod.green)
player.fighter.heal(player.fighter.max_hp / 2) #heal the player by 50%
message('Harvinaisen rauhanhetken kuluttua jatkat kohti tyrmien uumenia.', libtcod.sepia)
dungeon_level += 1
make_map() #create a fresh new level!
initialize_fov()
else:
message('Jatkat nälkäisenä kohti tyrmien uumenia.', libtcod.sepia)
dungeon_level += 1
make_map() #create a fresh new level!
initialize_fov()
def make_map():
global map, objects, stairs, path_map, rooms
#the list of objects with just the player
objects = [player]
(prev_x, prev_y) = (0, 0)
#fill map with "blocked" tiles
map = [[Tile(True, char="+",color=libtcod.gray,bgcolor=color_light_wall)
for y in range(MAP_HEIGHT) ]
for x in range(MAP_WIDTH) ]
rooms = []
num_rooms = 0
level=dungeon_level
error=True
while error:
try:
min_rooms = spawn[level]["-rooms"]
max_rooms = spawn[level]["+rooms"]
error=False
except KeyError:
level-=1
for r in range(libtcod.random_get_int(0,min_rooms,max_rooms)):
#random width and height
w = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE)
h = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE)
#random position without going out of the boundaries of the map
x = clamp(prev_x-ROOM_MAX_SIZE+5, libtcod.random_get_int(0, 0, MAP_WIDTH - w - 1), prev_x+ROOM_MAX_SIZE+5 )
y = clamp(prev_y-ROOM_MAX_SIZE+5, libtcod.random_get_int(0, 0, MAP_HEIGHT - h - 1), prev_y+ROOM_MAX_SIZE+5 )
#"Rect" class makes rectangles easier to work with
new_room = Rect(x, y, w, h)
#run through the other rooms and see if they intersect with this one
failed = False
for other_room in rooms:
if new_room.intersect(other_room):
failed = True
break
if not failed:
#this means there are no intersections, so this room is valid
#"paint" it to the map's tiles
create_room(new_room)
#center coordinates of new room, will be useful later
(new_x, new_y) = new_room.center()
if num_rooms == 0:
#this is the first room, where the player starts at
player.x = new_x
player.y = new_y
else:
#all rooms after the first:
#connect it to the previous room with a tunnel
#center coordinates of previous room
(prev_x, prev_y) = rooms[num_rooms-1].center()
#draw a coin (random number that is either 0 or 1)
if libtcod.random_get_int(0, 0, 1) == 1:
#first move horizontally, then vertically
create_h_tunnel(prev_x, new_x, prev_y)
create_v_tunnel(prev_y, new_y, new_x)
else:
#first move vertically, then horizontally
create_v_tunnel(prev_y, new_y, prev_x)
create_h_tunnel(prev_x, new_x, new_y)
#finally, append the new room to the list
place_objects(new_room)
rooms.append(new_room)
num_rooms += 1
if random.randint(0,10)<1:
randomwalk(random.randint(50,500))
#create stairs at the center of the last room
stairs = Object(new_x, new_y, '<', 'portaat', libtcod.white)
objects.append(stairs)
stairs.send_to_back() #so it's drawn below the monsters
path_map = libtcod.map_new(MAP_WIDTH, MAP_HEIGHT)
for y in range(MAP_HEIGHT):
for x in range(MAP_WIDTH):
if map[x][y].blocked:
libtcod.map_set_properties(path_map, x, y, True, False)
else:
libtcod.map_set_properties(path_map, x, y, True, True)
def menu(header, options, width):
if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options.')
#calculate total height for the header (after auto-wrap) and one line per option
header_height = libtcod.console_get_height_rect(con, 0, 0, width, SCREEN_HEIGHT, header)
height = len(options) + header_height
#create an off-screen console that represents the menu's window
window = libtcod.console_new(width, height)
#print the header, with auto-wrap
libtcod.console_set_default_foreground(window, libtcod.white)
libtcod.console_print_rect_ex(window, 0, 0, width, height, libtcod.BKGND_NONE, libtcod.LEFT, header)
#print all the options
y = header_height
letter_index = ord('a')
for option_text in options:
text = '(' + chr(letter_index) + ') ' + option_text
libtcod.console_print_ex(window, 0, y, libtcod.BKGND_NONE, libtcod.LEFT, text)
y += 1
letter_index += 1
#blit the contents of "window" to the root console
x = int(SCREEN_WIDTH/2 - width/2)
y = int(SCREEN_HEIGHT/2 - height/2)
libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7)
#present the root console to the player and wait for a key-press
libtcod.console_flush()
key = libtcod.console_wait_for_keypress(True)
#convert the ASCII code to an index; if it corresponds to an option, return it
index = key.c - ord('a')
if index >= 0 and index < len(options): return index
return None
def inventory_menu(header):
#show a menu with each item of the inventory as an option
if len(inventory) == 0:
options = ['Reppusi on tyhjä.']
else:
options = []
for item in inventory:
text = item.name
#show additional information, in case it's equipped
if item.equipment and item.equipment.is_equipped:
text = text + ' ( ' + item.equipment.slot + ')'
options.append(text)
index = menu(header, options, INVENTORY_WIDTH)
#if an item was chosen, return it
if index is None or len(inventory) == 0: return None
return inventory[index].item
def textinput(header, width):
#calculate total height for the header (after auto-wrap) and one line per option
header_height = libtcod.console_get_height_rect(con, 0, 0, width, SCREEN_HEIGHT, header)
height = header_height+2
#create an off-screen console that represents the menu's window
window = libtcod.console_new(width, height)
#print the header, with auto-wrap
libtcod.console_set_default_foreground(window, libtcod.white)
libtcod.console_print_rect_ex(window, 0, 0, width, height, libtcod.BKGND_NONE, libtcod.LEFT, header)
text=''
key=None
while key==None or key.vk != libtcod.KEY_ENTER:
libtcod.console_clear(con)
libtcod.console_print_rect_ex(window, 0, 2, width, height, libtcod.BKGND_NONE, libtcod.LEFT, text)
#blit the contents of "window" to the root console
x = int(SCREEN_WIDTH/2 - width/2)
y = int(SCREEN_HEIGHT/2 - height/2)
libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 1.0)
#present the root console to the player and wait for a key-press
libtcod.console_flush()
key = libtcod.console_wait_for_keypress(True)
text=text+chr(key.c)
return text.strip()
def render_all():
global fov_map, color_dark_wall, color_light_wall
global color_dark_ground, color_light_ground
global fov_recompute
if fov_recompute:
#recompute FOV if needed (the player moved or something)
fov_recompute = False
libtcod.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO)
#go through all tiles, and set their background color according to the FOV
for y in range(MAP_HEIGHT):
for x in range(MAP_WIDTH):
cell = map[x][y]
visible = libtcod.map_is_in_fov(fov_map, x, y)
wall = map[x][y].block_sight
if not visible:
#if it's not visible right now, the player can only see it if it's explored
if map[x][y].explored:
if wall:
libtcod.console_put_char_ex(con, x, y, ' ', libtcod.gray, color_dark_wall)
else:
libtcod.console_put_char_ex(con, x, y, ' ', libtcod.gray, color_dark_ground)