-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroguegate.py
1722 lines (1348 loc) · 50.8 KB
/
roguegate.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
# -*- coding: UTF-8 -*-
# Python 3.6.6 x64
# Libtcod 1.6.4 x64
# RogueGate, a 7-day Roguelike
# Mark Johnson and Gregory Adam Scott, 10:00 GMT 29th February 2020 - 7th March 2020
# Copyright (c) 2020 Mark Johnson and Gregory Adam Scott
#
# This file is part of RogueGate.
#
# RogueGate is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# RogueGate is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with RogueGate, in the form of a file named "gpl.txt".
# If not, see <https://www.gnu.org/licenses/>.
##### Libraries #####
import os, sys # OS-related stuff
import libtcodpy as libtcod
import shelve # saving and loading games
from random import choice, shuffle, sample
from math import sqrt
from copy import deepcopy
from textwrap import wrap # breaking up strings
##### Constants #####
# debug flags
FULL_LIGHT = False
FULL_VIS = False
NAME = 'RogueGate' # game name
VERSION = '0.1' # game version
RENDERER = libtcod.RENDERER_OPENGL2
LIMIT_FPS = 50
WINDOW_WIDTH, WINDOW_HEIGHT = 80, 40
WINDOW_XM, WINDOW_YM = int(WINDOW_WIDTH/2), int(WINDOW_HEIGHT/2)
##### Colour Definitions #####
KEY_COLOR = libtcod.Color(255,0,255) # key color for transparency
CONSOLE_COL_1 = libtcod.Color(255,217,102) # console colours from brightest to darkest
CONSOLE_COL_2 = libtcod.Color(255,204,51)
CONSOLE_COL_3 = libtcod.Color(255,191,0)
CONSOLE_COL_4 = libtcod.Color(217,163,0)
CONSOLE_COL_5 = libtcod.Color(178,134,0)
CONSOLE_COL_6 = libtcod.Color(140,105,0)
CONSOLE_COL_7 = libtcod.Color(102,77,0)
CONSOLE_COL_8 = libtcod.Color(16,12,0) # dark background colour
##### Map Cell Type Definitions #####
CELL_NULL = 0 # not active, no interactions possible
CELL_TILE = 1 # generic linoleum tile flooring
CELL_WALL = 2 # solid concrete wall
CELL_STAIRS = 3 # stairway
CELL_LINK = 4 # represents a link to another block
CELL_MARKER = 100 # a marker of some kind, used for debugging
BLOCK_LINKS = [(0,-1), (1,0), (0,1), (-1,0)] # list of directions for links to adjacent blocks
FLOOR_NAMES = ['Ground', 'Second', 'Third', 'Fourth']
SINTABLE = [
0.00000, 0.01745, 0.03490, 0.05234, 0.06976, 0.08716, 0.10453,
0.12187, 0.13917, 0.15643, 0.17365, 0.19081, 0.20791, 0.22495, 0.24192,
0.25882, 0.27564, 0.29237, 0.30902, 0.32557, 0.34202, 0.35837, 0.37461,
0.39073, 0.40674, 0.42262, 0.43837, 0.45399, 0.46947, 0.48481, 0.50000,
0.51504, 0.52992, 0.54464, 0.55919, 0.57358, 0.58779, 0.60182, 0.61566,
0.62932, 0.64279, 0.65606, 0.66913, 0.68200, 0.69466, 0.70711, 0.71934,
0.73135, 0.74314, 0.75471, 0.76604, 0.77715, 0.78801, 0.79864, 0.80902,
0.81915, 0.82904, 0.83867, 0.84805, 0.85717, 0.86603, 0.87462, 0.88295,
0.89101, 0.89879, 0.90631, 0.91355, 0.92050, 0.92718, 0.93358, 0.93969,
0.94552, 0.95106, 0.95630, 0.96126, 0.96593, 0.97030, 0.97437, 0.97815,
0.98163, 0.98481, 0.98769, 0.99027, 0.99255, 0.99452, 0.99619, 0.99756,
0.99863, 0.99939, 0.99985, 1.00000, 0.99985, 0.99939, 0.99863, 0.99756,
0.99619, 0.99452, 0.99255, 0.99027, 0.98769, 0.98481, 0.98163, 0.97815,
0.97437, 0.97030, 0.96593, 0.96126, 0.95630, 0.95106, 0.94552, 0.93969,
0.93358, 0.92718, 0.92050, 0.91355, 0.90631, 0.89879, 0.89101, 0.88295,
0.87462, 0.86603, 0.85717, 0.84805, 0.83867, 0.82904, 0.81915, 0.80902,
0.79864, 0.78801, 0.77715, 0.76604, 0.75471, 0.74314, 0.73135, 0.71934,
0.70711, 0.69466, 0.68200, 0.66913, 0.65606, 0.64279, 0.62932, 0.61566,
0.60182, 0.58779, 0.57358, 0.55919, 0.54464, 0.52992, 0.51504, 0.50000,
0.48481, 0.46947, 0.45399, 0.43837, 0.42262, 0.40674, 0.39073, 0.37461,
0.35837, 0.34202, 0.32557, 0.30902, 0.29237, 0.27564, 0.25882, 0.24192,
0.22495, 0.20791, 0.19081, 0.17365, 0.15643, 0.13917, 0.12187, 0.10453,
0.08716, 0.06976, 0.05234, 0.03490, 0.01745, 0.00000, -0.01745, -0.03490,
-0.05234, -0.06976, -0.08716, -0.10453, -0.12187, -0.13917, -0.15643,
-0.17365, -0.19081, -0.20791, -0.22495, -0.24192, -0.25882, -0.27564,
-0.29237, -0.30902, -0.32557, -0.34202, -0.35837, -0.37461, -0.39073,
-0.40674, -0.42262, -0.43837, -0.45399, -0.46947, -0.48481, -0.50000,
-0.51504, -0.52992, -0.54464, -0.55919, -0.57358, -0.58779, -0.60182,
-0.61566, -0.62932, -0.64279, -0.65606, -0.66913, -0.68200, -0.69466,
-0.70711, -0.71934, -0.73135, -0.74314, -0.75471, -0.76604, -0.77715,
-0.78801, -0.79864, -0.80902, -0.81915, -0.82904, -0.83867, -0.84805,
-0.85717, -0.86603, -0.87462, -0.88295, -0.89101, -0.89879, -0.90631,
-0.91355, -0.92050, -0.92718, -0.93358, -0.93969, -0.94552, -0.95106,
-0.95630, -0.96126, -0.96593, -0.97030, -0.97437, -0.97815, -0.98163,
-0.98481, -0.98769, -0.99027, -0.99255, -0.99452, -0.99619, -0.99756,
-0.99863, -0.99939, -0.99985, -1.00000, -0.99985, -0.99939, -0.99863,
-0.99756, -0.99619, -0.99452, -0.99255, -0.99027, -0.98769, -0.98481,
-0.98163, -0.97815, -0.97437, -0.97030, -0.96593, -0.96126, -0.95630,
-0.95106, -0.94552, -0.93969, -0.93358, -0.92718, -0.92050, -0.91355,
-0.90631, -0.89879, -0.89101, -0.88295, -0.87462, -0.86603, -0.85717,
-0.84805, -0.83867, -0.82904, -0.81915, -0.80902, -0.79864, -0.78801,
-0.77715, -0.76604, -0.75471, -0.74314, -0.73135, -0.71934, -0.70711,
-0.69466, -0.68200, -0.66913, -0.65606, -0.64279, -0.62932, -0.61566,
-0.60182, -0.58779, -0.57358, -0.55919, -0.54464, -0.52992, -0.51504,
-0.50000, -0.48481, -0.46947, -0.45399, -0.43837, -0.42262, -0.40674,
-0.39073, -0.37461, -0.35837, -0.34202, -0.32557, -0.30902, -0.29237,
-0.27564, -0.25882, -0.24192, -0.22495, -0.20791, -0.19081, -0.17365,
-0.15643, -0.13917, -0.12187, -0.10453, -0.08716, -0.06976, -0.05234,
-0.03490, -0.01745, -0.00000
]
COSTABLE = [
1.00000, 0.99985, 0.99939, 0.99863, 0.99756, 0.99619, 0.99452,
0.99255, 0.99027, 0.98769, 0.98481, 0.98163, 0.97815, 0.97437, 0.97030,
0.96593, 0.96126, 0.95630, 0.95106, 0.94552, 0.93969, 0.93358, 0.92718,
0.92050, 0.91355, 0.90631, 0.89879, 0.89101, 0.88295, 0.87462, 0.86603,
0.85717, 0.84805, 0.83867, 0.82904, 0.81915, 0.80902, 0.79864, 0.78801,
0.77715, 0.76604, 0.75471, 0.74314, 0.73135, 0.71934, 0.70711, 0.69466,
0.68200, 0.66913, 0.65606, 0.64279, 0.62932, 0.61566, 0.60182, 0.58779,
0.57358, 0.55919, 0.54464, 0.52992, 0.51504, 0.50000, 0.48481, 0.46947,
0.45399, 0.43837, 0.42262, 0.40674, 0.39073, 0.37461, 0.35837, 0.34202,
0.32557, 0.30902, 0.29237, 0.27564, 0.25882, 0.24192, 0.22495, 0.20791,
0.19081, 0.17365, 0.15643, 0.13917, 0.12187, 0.10453, 0.08716, 0.06976,
0.05234, 0.03490, 0.01745, 0.00000, -0.01745, -0.03490, -0.05234, -0.06976,
-0.08716, -0.10453, -0.12187, -0.13917, -0.15643, -0.17365, -0.19081,
-0.20791, -0.22495, -0.24192, -0.25882, -0.27564, -0.29237, -0.30902,
-0.32557, -0.34202, -0.35837, -0.37461, -0.39073, -0.40674, -0.42262,
-0.43837, -0.45399, -0.46947, -0.48481, -0.50000, -0.51504, -0.52992,
-0.54464, -0.55919, -0.57358, -0.58779, -0.60182, -0.61566, -0.62932,
-0.64279, -0.65606, -0.66913, -0.68200, -0.69466, -0.70711, -0.71934,
-0.73135, -0.74314, -0.75471, -0.76604, -0.77715, -0.78801, -0.79864,
-0.80902, -0.81915, -0.82904, -0.83867, -0.84805, -0.85717, -0.86603,
-0.87462, -0.88295, -0.89101, -0.89879, -0.90631, -0.91355, -0.92050,
-0.92718, -0.93358, -0.93969, -0.94552, -0.95106, -0.95630, -0.96126,
-0.96593, -0.97030, -0.97437, -0.97815, -0.98163, -0.98481, -0.98769,
-0.99027, -0.99255, -0.99452, -0.99619, -0.99756, -0.99863, -0.99939,
-0.99985, -1.00000, -0.99985, -0.99939, -0.99863, -0.99756, -0.99619,
-0.99452, -0.99255, -0.99027, -0.98769, -0.98481, -0.98163, -0.97815,
-0.97437, -0.97030, -0.96593, -0.96126, -0.95630, -0.95106, -0.94552,
-0.93969, -0.93358, -0.92718, -0.92050, -0.91355, -0.90631, -0.89879,
-0.89101, -0.88295, -0.87462, -0.86603, -0.85717, -0.84805, -0.83867,
-0.82904, -0.81915, -0.80902, -0.79864, -0.78801, -0.77715, -0.76604,
-0.75471, -0.74314, -0.73135, -0.71934, -0.70711, -0.69466, -0.68200,
-0.66913, -0.65606, -0.64279, -0.62932, -0.61566, -0.60182, -0.58779,
-0.57358, -0.55919, -0.54464, -0.52992, -0.51504, -0.50000, -0.48481,
-0.46947, -0.45399, -0.43837, -0.42262, -0.40674, -0.39073, -0.37461,
-0.35837, -0.34202, -0.32557, -0.30902, -0.29237, -0.27564, -0.25882,
-0.24192, -0.22495, -0.20791, -0.19081, -0.17365, -0.15643, -0.13917,
-0.12187, -0.10453, -0.08716, -0.06976, -0.05234, -0.03490, -0.01745,
-0.00000, 0.01745, 0.03490, 0.05234, 0.06976, 0.08716, 0.10453, 0.12187,
0.13917, 0.15643, 0.17365, 0.19081, 0.20791, 0.22495, 0.24192, 0.25882,
0.27564, 0.29237, 0.30902, 0.32557, 0.34202, 0.35837, 0.37461, 0.39073,
0.40674, 0.42262, 0.43837, 0.45399, 0.46947, 0.48481, 0.50000, 0.51504,
0.52992, 0.54464, 0.55919, 0.57358, 0.58779, 0.60182, 0.61566, 0.62932,
0.64279, 0.65606, 0.66913, 0.68200, 0.69466, 0.70711, 0.71934, 0.73135,
0.74314, 0.75471, 0.76604, 0.77715, 0.78801, 0.79864, 0.80902, 0.81915,
0.82904, 0.83867, 0.84805, 0.85717, 0.86603, 0.87462, 0.88295, 0.89101,
0.89879, 0.90631, 0.91355, 0.92050, 0.92718, 0.93358, 0.93969, 0.94552,
0.95106, 0.95630, 0.96126, 0.96593, 0.97030, 0.97437, 0.97815, 0.98163,
0.98481, 0.98769, 0.99027, 0.99255, 0.99452, 0.99619, 0.99756, 0.99863,
0.99939, 0.99985, 1.00000
]
##### Room Object - represents one room within a blockfloor
class Room():
def __init__(self, blockfloor, x, y, w, h):
self.blockfloor = blockfloor
self.x = x
self.y = y
self.w = w
self.h = h
self.number = 0
##### BlockFloor Object - represents one floor of one block of the entire complex #####
class BlockFloor():
def __init__(self, x, y, floor, outdoor=False):
self.x = x
self.y = y
self.floor = floor
self.outdoor = outdoor # block is only the ground floor of an outdoor area
self.letter = '' # block letter, A-
self.links = { # links to adjacent blocks
(0,-1): None,
(1,0): None,
(0,1): None,
(-1,0): None
}
self.link_locations = { # cell locations of links to other blocks
(0,-1): None,
(1,0): None,
(0,1): None,
(-1,0): None
}
self.vertical_links = { # links to adjacent floors in same block
-1: None,
1: None
}
self.char_map = {} # map of cells
self.center_point = (0,0)
self.rooms = [] # list of rooms in (x,y,w,h) format
self.entities = [] # list of entities in the map
self.blocking_entity_map = {} # map of cells where light/sight blocked by entities
self.light_map = {} # light values for cells
# generate the map for this block-floor
self.GenerateMap()
# add a light entity at the given location
def AddLight(self, x, y, light_radius):
new_entity = Entity()
new_entity.location = (x, y)
new_entity.light_radius = light_radius
self.entities.append(new_entity)
# set a given cell to a cell type, ignores if not on map
def SetCell(self, x, y, new_type, skip_replace, skip_floors):
if (x,y) not in self.char_map: return
if skip_floors and self.char_map[(x,y)] == CELL_TILE: return
if skip_replace and self.char_map[(x,y)] != CELL_NULL: return
self.char_map[(x,y)] = new_type
# get the cell code of the given cell; if not on map, will return CELL_NULL
def GetCell(self, x, y):
if (x,y) not in self.char_map: return CELL_NULL
return self.char_map[(x,y)]
# generate or re-generate the map for this blockfloor, 61x40
def GenerateMap(self):
# create a room: h, w is the floor space, with one extra layer of walls
def AddRoom(x1, y1, w, h, skip_replace=False, skip_floors=False, numbered=False):
for x in range(x1, x1+w):
for y in range(y1, y1+h):
self.SetCell(x, y, CELL_TILE, skip_replace, skip_floors)
# horizontal walls
for x in range(x1, x1+w):
self.SetCell(x, y1-1, CELL_WALL, skip_replace, skip_floors)
self.SetCell(x, y1+h, CELL_WALL, skip_replace, skip_floors)
# vertical walls
for y in range(y1-1, y1+h+1):
self.SetCell(x1-1, y, CELL_WALL, skip_replace, skip_floors)
self.SetCell(x1+w, y, CELL_WALL, skip_replace, skip_floors)
# record the room if it's numbered
if not numbered: return
self.rooms.append(Room(self, x1, y1, w, h))
# character map - one for each possible map cell
# set all cells to null to start
for x in range(61):
for y in range(38):
self.char_map[(x,y)] = CELL_NULL
# clear list of rooms, entities
self.rooms = []
self.entities = []
# outdoor blocks are set up differently
if self.outdoor:
for x in range(61):
for y in range(38):
self.char_map[(x,y)] = CELL_TILE
self.center_point = (30, 19)
for x in range(10, 51, 10):
self.AddLight(x, 20, 5)
for y in range(10, 31, 10):
if y == 20: continue
self.AddLight(30, y, 5)
return
# set a main horizontal hallway to start
hx1 = libtcod.random_get_int(0, 8, 10)
hy1 = libtcod.random_get_int(0, 8, 27)
hw = libtcod.random_get_int(0, 43, 49) - hx1
AddRoom(hx1, hy1, hw, 3)
# set up a vertical hallway
vx1 = libtcod.random_get_int(0, 22, 40)
vy1 = libtcod.random_get_int(0, 4, 6)
vh = libtcod.random_get_int(0, 30, 36) - vy1
AddRoom(vx1, vy1, 3, vh, skip_floors=True)
# record center point and add a light here
self.center_point = (vx1+1, hy1+1)
self.AddLight(vx1+1, hy1+1, 5)
# add lights down each hallway
for x in range(vx1+1, hx1, -15):
self.AddLight(x, hy1+1, 5)
for x in range(vx1+1, hx1+hw, 15):
self.AddLight(x, hy1+1, 5)
# determine the height of the rooms off the horizontal hallway
room_height_upper = libtcod.random_get_int(0, 2, 6) + libtcod.random_get_int(0, 2, 6)
room_height_lower = libtcod.random_get_int(0, 2, 6) + libtcod.random_get_int(0, 2, 6)
# adjust in case they would go off the map
if hy1-1-room_height_upper <= 0:
room_height_upper = hy1-2
if hy1+2+room_height_lower >= 35:
room_height_lower = 35-hy1-2
# run across the x axis and try to add upper rooms
x = hx1
while x < hx1+hw:
if x >= 60: break
width = libtcod.random_get_int(0, 2, 4) + libtcod.random_get_int(0, 2, 4)
# check for blocking walls
room_clear = True
for x1 in range(x, x+width):
if x1 >= 60:
room_clear = False
if not room_clear: break
for y1 in range(hy1-room_height_upper, hy1-1):
if self.char_map[(x1,y1)] != CELL_NULL:
room_clear = False
break
# if not enough space
if not room_clear:
x+=1
continue
# create the room
AddRoom(x, hy1-room_height_upper-1, width, room_height_upper, skip_replace=True, numbered=True)
x += width+1
# run across the x axis and try to add lower rooms
x = hx1
while x < hx1+hw:
if x >= 60: break
width = libtcod.random_get_int(0, 2, 4) + libtcod.random_get_int(0, 2, 4)
# check for blocking walls
room_clear = True
for x1 in range(x, x+width):
if x1 >= 60:
room_clear = False
if not room_clear: break
for y1 in range(hy1+4, hy1+3+room_height_lower):
if self.char_map[(x1,y1)] != CELL_NULL:
room_clear = False
break
# if not enough space, try to adjust the room width
if not room_clear:
x+=1
continue
# create the room
AddRoom(x, hy1+4, width, room_height_lower, skip_replace=True, numbered=True)
x += width+1
# TODO: do the same for the vertical hallway?
# create doors to connect rooms to at least one hallway
for room in self.rooms:
possible_door_cells = []
# check upper wall
for x1 in range(room.x, room.x+room.w):
if self.GetCell(x1-1, room.y-1) != CELL_WALL: continue
if self.GetCell(x1+1, room.y-1) != CELL_WALL: continue
if self.GetCell(x1,room.y-2) == CELL_TILE:
possible_door_cells.append((x1, room.y-1))
# check lower wall
for x1 in range(room.x, room.x+room.w):
if self.GetCell(x1-1, room.y+room.h) != CELL_WALL: continue
if self.GetCell(x1+1, room.y+room.h) != CELL_WALL: continue
if self.GetCell(x1,room.y+room.h+1) == CELL_TILE:
possible_door_cells.append((x1, room.y+room.h))
if len(possible_door_cells) == 0:
continue
(x, y) = choice(possible_door_cells)
self.SetCell(x, y, CELL_TILE, False, False)
# generate a door entity at x, y
new_entity = Entity()
new_entity.block = self
new_entity.location = (x,y)
new_entity.is_door = True
self.entities.append(new_entity)
self.GenerateSightBlockMap()
# generate map of light/sight blocking entities
def GenerateSightBlockMap(self):
# clear current map
for x in range(61):
for y in range(38):
self.blocking_entity_map[(x,y)] = False
for entity in self.entities:
if not entity.is_door: continue
if entity.open_state: continue
self.blocking_entity_map[entity.location] = True
# set room numbers for this blockfloor
def SetRoomNumbers(self):
room_index = 0
for room in self.rooms:
room.number = (100 * (self.floor + 1)) + room_index
room_index += 1
# generate link cells for this block to horizontal and vertical adjacent ones
def GenerateLinks(self):
for (xm, ym) in BLOCK_LINKS:
# link in this direction
if self.links[(xm, ym)]:
(x, y) = self.center_point
link_set = False
while not link_set:
# edge of map
if self.GetCell(x+xm, y+ym) == CELL_NULL:
pass
elif self.GetCell(x, y) != CELL_WALL:
x += xm
y += ym
continue
self.SetCell(x, y, CELL_LINK, False, False)
self.link_locations[(xm, ym)] = (x,y)
link_set = True
continue
# generate objects for this floor
def GenerateObjects(self):
OBJECTS = ['Wooden Desk', 'Cabinet', 'Chair']
for room in self.rooms:
for i in range(libtcod.random_get_int(0, 2, 5)):
x = libtcod.random_get_int(0, room.x, room.x+room.w-1)
y = libtcod.random_get_int(0, room.y, room.y+room.h-1)
new_entity = Entity()
new_entity.location = (x, y)
new_entity.object_name = choice(OBJECTS)
self.entities.append(new_entity)
# generate or re-generate the light map for all cells in this block-level
def GenerateLightMap(self):
def Raycast(x, y, radius, facing=None):
STEP = 2 # how many steps per cycle
# if light has a facing, only cast it for 90 degrees in that direction
if facing is None:
cast_start = 0
cast_end = 361
else:
if facing == (0,-1):
cast_start = 135
cast_end = 225
elif facing == (-1,0):
cast_start = 225
cast_end = 315
elif facing == (0,1):
cast_start = 315
cast_end = 45
elif facing == (1,0):
cast_start = 45
cast_end = 135
else:
print('ERROR: Incorrect facing on entity')
return
for i in range(0, 361, STEP):
if facing is not None:
if facing == (0,1):
if i < cast_start and i > cast_end: continue
else:
if i < cast_start and i < cast_end: continue
if i > cast_start and i > cast_end: continue
ax = SINTABLE[i] # Get precalculated value sin(x / (180 / pi))
ay = COSTABLE[i] # cos(x / (180 / pi))
rx = float(x)
ry = float(y)
for z in range(radius): # Cast the ray
rx += ax
ry += ay
cx, cy = int(rx), int(ry)
# ray is off the map or in unplayable area
if self.GetCell(cx, cy) == CELL_NULL: break
# add light
new_level = 255 - int(255 * z * 0.07)
if new_level <= 0:
continue
if new_level > self.light_map[(cx, cy)]:
self.light_map[(cx, cy)] = new_level
# ray hit a wall
if self.GetCell(cx, cy) in [CELL_WALL]:
break
# hit a blocking entity (eg. closed door)
if self.blocking_entity_map[(cx,cy)]:
break
# debug
if FULL_LIGHT:
for x in range(61):
for y in range(38):
self.light_map[(x,y)] = 255
return
# reset light levels
for x in range(61):
for y in range(38):
self.light_map[(x,y)] = 25
# cast static lights
for entity in self.entities:
if entity.light_radius == 0: continue
(x, y) = entity.location
Raycast(x, y, entity.light_radius)
# cast light from player flashlight
(x, y) = game.player.location
Raycast(x, y, 14, facing=game.player.facing)
# generate the player visibility map for this block, store info in game object
# uses recursive shadowcasting, based on:
# http://www.roguebasin.com/index.php?title=Python_shadowcasting_implementation
def GenerateVisMap(self):
# Multipliers for transforming coordinates to other octants:
MULT = [
[1, 0, 0, -1, -1, 0, 0, 1],
[0, 1, -1, 0, 0, -1, 1, 0],
[0, 1, 1, 0, 0, -1, -1, 0],
[1, 0, 0, 1, -1, 0, 0, -1]
]
# shadowcasting function
def ShadowCast(cx, cy, row, start, end, radius, xx, xy, yx, yy, id):
def IsBlocked(x, y):
return (x < 0 or y < 0
or x >= 61 or y >= 38
or self.GetCell(x, y) == CELL_WALL
or self.blocking_entity_map[(x,y)])
if start < end: return
radius_squared = radius * radius
for j in range(row, radius+1):
dx, dy = -j-1, -j
blocked = False
while dx <= 0:
dx += 1
# translate the dx, dy coordinates into map coordinates
mx, my = cx + dx * xx + dy * xy, cy + dx * yx + dy * yy
# l_slope and r_slope store the slopes of the left and right
# extremities of the square we're considering
l_slope, r_slope = (dx-0.5)/(dy+0.5), (dx+0.5)/(dy-0.5)
if start < r_slope:
continue
elif end > l_slope:
break
else:
# ray is touching this square, set it as visible
if dx*dx + dy*dy < radius_squared:
game.vis_map[(mx,my)] = True
if blocked:
# we're scanning a row of blocked squares
if IsBlocked(mx, my):
new_start = r_slope
continue
else:
blocked = False
start = new_start
else:
if IsBlocked(mx, my) and j < radius:
blocked = True
ShadowCast(cx, cy, j+1, start, l_slope,
radius, xx, xy, yx, yy, id+1)
new_start = r_slope
# Row is scanned; do next row unless last square was blocked
if blocked:
break
# debug flag
if FULL_VIS:
for x in range(61):
for y in range(38):
game.vis_map[(x,y)] = True
return
# clear current vis map
for x in range(61):
for y in range(38):
game.vis_map[(x,y)] = False
# cast in all 8 octants
(x,y) = game.player.location
for octant in range(8):
ShadowCast(x, y, 1, 1.0, 0.0, 100,
MULT[0][octant], MULT[1][octant],
MULT[2][octant], MULT[3][octant], 0)
##### Entity Object - represents a dynamic thing in the world: the player, one of the burglars, etc.
class Entity:
def __init__(self):
self.is_player = False
self.is_burglar = False
self.is_human = False # human entity: burglar or staff member
self.block = None # pointer to block location
self.location = (0,0) # current location in the world
self.facing = None # direction facing
self.light_radius = 0 # entity emits light to this radius
self.is_door = False
self.open_state = False
self.opens_up = True
self.object_name = None # entity is an office object of some kind
# draw entity onto the entity console
def DrawMe(self):
(x,y) = self.location
# light
if self.light_radius > 0:
char = 249
col = CONSOLE_COL_1
elif self.is_player:
char = 64
col = CONSOLE_COL_1
elif self.is_human:
char = 2
col = CONSOLE_COL_1
elif self.is_door:
if self.open_state:
char = 0
else:
char = 196
col = CONSOLE_COL_3
# office object
elif self.object_name is not None:
if self.object_name == 'Wooden Desk':
char = 22
elif self.object_name == 'Cabinet':
char = 240
elif self.object_name == 'Chair':
char = 7
col = CONSOLE_COL_3
# error - unknown entity
else:
char = 63
col = libtcod.light_red
if self.is_player:
pass
# if not visible to player, display as dark as possible
elif not game.vis_map[(x,y)]:
col = CONSOLE_COL_8
else:
# change display colour depending on light level of this cell
l = game.active_block.light_map[(x,y)]
col = col * libtcod.Color(l, l, l)
libtcod.console_put_char_ex(entity_con, x, y, char,
col, libtcod.black)
##### Game Object - holds everything for a given game #####
class Game:
def __init__(self):
self.init_finished = False
self.hour = 19 # current time
self.minute = 0
self.next_day = False # if clock has passed midnight already
self.msg_log = [] # list of game messages
# list of entities in the world
self.entities = []
# building blocks within the complex, 5x3 possible locations
# dictionary, one list per coordinate
self.block_map = {}
self.GenerateBlocks()
for x in range(5):
for y in range(3):
for block in self.block_map[(x,y)]:
block.SetRoomNumbers()
block.GenerateLinks()
# generate stairways per block with 2+ floors
self.GenerateStairways()
# generate objects for each floor in each block
for x in range(5):
for y in range(3):
for block in self.block_map[(x,y)]:
block.GenerateObjects()
self.active_block = None # current block in viewport
self.active_floor = 0 # current floor in viewport
self.vis_map = {} # visibility for player in current block
for x in range(61):
for y in range(38):
self.vis_map[(x,y)] = False
# create player object
new_entity = Entity()
new_entity.is_player = True
self.entities.append(new_entity)
self.player = new_entity
# put player in block A to start and move viewport to there
self.MovePlayerToBlock('A')
self.active_block = self.player.block
# generate AI entities
self.SpawnAIEntities()
# allow AI entities to act
def DoAITurn(self):
print('DEBUG: Starting AI turn')
print('DEBUG: AI turn finished')
# add a game message
def AddMessage(self, text):
self.msg_log.append(text)
self.UpdateMsgCon()
self.UpdateScreen()
# warp the player to the ground floor, center of the given block
def MovePlayerToBlock(self, block_letter):
for x in range(5):
for y in range(3):
block = self.block_map[(x,y)][0]
if block.letter == block_letter:
self.player.block = block
self.player.location = block.center_point
self.player.facing = (0,1)
return
# generate a series of building blocks for the complex
def GenerateBlocks(self):
for tries in range(300):
# clear any existing blocks
for x in range(5):
for y in range(3):
self.block_map[(x,y)] = []
# run through block locations and roll for presence of a building block
block_list = list(self.block_map.keys())
shuffle(block_list)
total_blocks = 0
for (x,y) in block_list:
# blocks in center of complex have less chance of being spawned
if y == 1 and 0 < x < 4:
chance = 20
else:
chance = 70
# modify by already existing number of blocks
chance -= total_blocks * 5
if libtcod.random_get_int(0, 1, 100) <= chance:
self.block_map[(x,y)].append(BlockFloor(x, y, 0))
total_blocks += 1
else:
self.block_map[(x,y)].append(BlockFloor(x, y, 0, outdoor=True))
# apply block number restrictions
if total_blocks <= 7 or total_blocks >= 12:
continue
# make sure there are at least 3 outdoors blocks along the edge
outdoor_blocks = 0
for x in range(5):
for y in range(3):
# not an edge block
if y == 1 and 0 < x < 4: continue
if self.block_map[(x,y)][0].outdoor:
outdoor_blocks += 1
if outdoor_blocks < 3: continue
# map is good!
print('Generated map after ' + str(tries) + ' tries')
break
# apply letters and check for upper floor generation
i = 0
for y in range(3):
for x in range(5):
if self.block_map[(x,y)][0].outdoor: continue
self.block_map[(x,y)][0].letter = chr(i+65)
# possible 2nd, 3rd, and 4th floor
for f in range(1, 4):
# roll to break here
if libtcod.random_get_int(0, 1, 100) <= f*10:
break
block_floor = deepcopy(self.block_map[(x,y)][0])
block_floor.floor = f
block_floor.letter = chr(i+65)
self.block_map[(x,y)].append(block_floor)
# TODO: add stair and elevator connections here
if len(self.block_map[(x,y)]) > 1:
pass
# increase block letter
i += 1
# run through each block, apply letters and check for links
for y in range(3):
for x in range(5):
for block in self.block_map[(x,y)]:
# link blocks to adjacent ones
for (xm, ym) in BLOCK_LINKS:
if (x+xm,y+ym) not in self.block_map:
continue
block_list = self.block_map[(x+xm,y+ym)]
# adjacent floor exists, link to it
if len(block_list) > block.floor:
block.links[(xm,ym)] = block_list[block.floor]
# link floors to vertically adjacent ones
if block.floor < len(self.block_map[(x,y)])-1:
block.vertical_links[1] = self.block_map[(x,y)][block.floor+1]
if block.floor > 0:
block.vertical_links[-1] = self.block_map[(x,y)][block.floor-1]
# generate stairway connections for all floors in a given block
def GenerateStairways(self):
def AddWall(x, y, horizontal_shift):
if horizontal_shift < 0:
x1 = x-1
else:
x1 = x+1
for y1 in range(y-1,y+2):
block.SetCell(x1, y1, CELL_WALL, False, False)
for x in range(5):
for y in range(3):
# skip outdoor and single-level blocks
if len(self.block_map[(x,y)]) == 1: continue
# working with the ground floor, find two suitable locations
# for stairways
block = self.block_map[(x,y)][0]
# start in the vertical hallway and find the upper and lower end
(x1, ys) = block.center_point
for y1 in range(ys, 0, -1):
if block.GetCell(x1, y1) != CELL_TILE:
break
y1 += 2
horizontal_shift1 = choice([-2, 2])
x1 += horizontal_shift1
(x2, ys) = block.center_point
for y2 in range(ys, 40):
if block.GetCell(x2, y2) != CELL_TILE:
break
y2 -= 2
horizontal_shift2 = choice([-2, 2])
x2 += horizontal_shift2
# apply to each floor
for block in self.block_map[(x,y)]:
block.SetCell(x1, y1, CELL_STAIRS, False, False)
AddWall(x1, y1, horizontal_shift1)
block.SetCell(x2, y2, CELL_STAIRS, False, False)
AddWall(x2, y2, horizontal_shift2)
# generate AI entities: burglars and random staff
def SpawnAIEntities(self):
# generate five burglars and place them at the outer edge of random outdoor blocks
floor_list = []
for x in range(5):
for y in range(3):
# not an edge block
if y == 1 and 0 < x < 4: continue
if self.block_map[(x,y)][0].outdoor:
floor_list.append(self.block_map[(x,y)][0])
print('DEBUG: Identified ' + str(len(floor_list)) + ' possible entry blocks')
for i in range(5):
new_entity = Entity()
new_entity.is_burglar = True
new_entity.is_human = True
block = choice(floor_list)
new_entity.block = block