This repository has been archived by the owner on Oct 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
pyWorld.py
1173 lines (823 loc) · 37.1 KB
/
pyWorld.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 libtcodpy as libtcod
import time
from random import randint
from random import uniform
import cProfile
pr = cProfile.Profile()
pr.enable()
WORLD_WIDTH = 200
WORLD_HEIGHT = 80
SCREEN_WIDTH = 200
SCREEN_HEIGHT = 80
CIVILIZED_CIVS = 2
TRIBAL_CIVS = 2
MIN_RIVER_LENGHT = 3
CIV_MAX_SITES = 20
EXPANSION_DISTANCE = 10
WAR_DISTANCE = 8
###################################################################################### - Classes - ######################################################################################
class Tile:
def __init__(self, height,temp,precip,drainage, biome):
self.temp = temp
self.height = height
self.precip = precip
self.drainage = drainage
self.biome = biome
hasRiver = False
isCiv = False
biomeID = 0
prosperity = 0
class Race:
def __init__(self,Name,PrefBiome,Strenght,Size,ReproductionSpeed,Aggressiveness,Form):
self.Name = Name
self.PrefBiome = PrefBiome
self.Strenght = Strenght
self.Size = Size
self.ReproductionSpeed = ReproductionSpeed
self.Aggressiveness = Aggressiveness
self.Form = Form
class CivSite:
def __init__(self,x,y,category,suitable,popcap):
self.x = x
self.y = y
self.category = category
self.suitable = suitable
self.popcap = popcap
Population = 0
isCapital = False
class Army:
def __init__(self,x,y,Civ,Size):
self.x = x
self.y = y
self.Civ = Civ
self.Size = Size
class Civ:
def __init__(self,Race,Name,Government,Color,Flag,Aggression):
self.Name = Name
self.Race = Race
self.Government = Government
self.Color = Color
self.Flag = Flag
self.Aggression = Race.Aggressiveness + Government.Aggressiveness
def PrintInfo(self):
print self.Name
print self.Race.Name
print self.Government.Name
print 'Aggression:',self.Aggression
print 'Suitable Sites:',len(self.SuitableSites),'\n'
Sites = []
SuitableSites = []
atWar = False
Army = Army(None,None,None,None)
TotalPopulation = 0
class GovernmentType:
def __init__(self,Name,Description,Aggressiveness,Militarization,TechBonus):
self.Name = Name
self.Description = Description
self.Aggressiveness = Aggressiveness
self.Militarization = Militarization
self.TechBonus = TechBonus
class War:
def __init__(self,Side1,Side2):
self.Side1 = Side1
self.Side2 = Side2
##################################################################################### - Functions - #####################################################################################
# - General Functions -
def ClearConsole():
for x in xrange(SCREEN_WIDTH):
for y in xrange(SCREEN_HEIGHT):
libtcod.console_put_char_ex( 0, x, y, ' ', libtcod.black, libtcod.black)
libtcod.console_flush()
return
def PointDistRound(pt1x, pt1y, pt2x, pt2y):
distance = abs(pt2x - pt1x) + abs(pt2y - pt1y);
distance = round(distance)
return distance
def FlagGenerator(Color):
Flag = [[0 for a in range(4)] for b in range(12)]
BackColor1 = Color
BackColor2 = Palette[randint(0,len(Palette)-1)]
OverColor1 = Palette[randint(0,len(Palette)-1)]
OverColor2 = Palette[randint(0,len(Palette)-1)]
BackFile = open("Background.txt",'r')
OverlayFile = open("Overlay.txt",'r')
BTypes = (sum(1 for line in open('Background.txt')) + 1) / 5
OTypes = (sum(1 for line in open('Overlay.txt')) + 1) / 5
Back = randint(1, BTypes)
Overlay = randint(1, OTypes)
for a in range(53*(Back-1)):
C = BackFile.read(1)
for a in range(53*(Overlay-1)):
C = OverlayFile.read(1)
for y in range(4):
for x in range(12):
C = BackFile.read(1)
while C == '\n':
C = BackFile.read(1)
if C == '#':
Flag[x][y] = BackColor1
elif C == '"':
Flag[x][y] = BackColor2
C = OverlayFile.read(1)
while C == '\n':
C = OverlayFile.read(1)
if C == '#':
Flag[x][y] = OverColor1
elif C == '"':
Flag[x][y] = OverColor2
BackFile.close()
OverlayFile.close()
return Flag
def LowestNeighbour(X,Y,World): #Diagonals are commented for rivers
minval = 1
x = 0
y = 0
if World[X + 1][Y].height < minval and X + 1 < WORLD_WIDTH:
minval = World[X + 1][Y].height
x = X + 1
y = Y
if World[X][Y + 1].height < minval and Y + 1 < WORLD_HEIGHT:
minval = World[X][Y + 1].height
x = X
y = Y + 1
#if libtcod.heightmap_get_value(hm, X + 1, Y + 1) < minval and X + 1 < WORLD_WIDTH and Y + 1 < WORLD_HEIGHT and minval > 0.2:
#minval = libtcod.heightmap_get_value(hm, X + 1, Y + 1)
#x = X + 1
#y = Y + 1
#if libtcod.heightmap_get_value(hm, X - 1, Y - 1) < minval and X - 1 > 0 and Y - 1 > 0 and minval > 0.2:
#minval = libtcod.heightmap_get_value(hm, X - 1, Y - 1)
#x = X - 1
#y = Y - 1
if World[X - 1][Y].height < minval and X - 1 > 0:
minval = World[X - 1][Y].height
x = X - 1
y = Y
if World[X][Y - 1].height < minval and Y - 1 > 0:
minval = World[X][Y - 1].height
x = X
y = Y - 1
#f libtcod.heightmap_get_value(hm, X + 1, Y - 1) < minval and X + 1 < WORLD_WIDTH and Y - 1 > 0 and minval > 0.2:
#minval = libtcod.heightmap_get_value(hm, X + 1, Y - 1)
#x = X + 1
#y = Y - 1
#if libtcod.heightmap_get_value(hm, X - 1, Y + 1) < minval and X - 1 > 0 and Y + 1 < WORLD_HEIGHT and minval > 0.2 :
#minval = libtcod.heightmap_get_value(hm, X - 1, Y + 1)
#x = X - 1
#y = Y + 1
error = 0
if x == 0 and y == 0:
error = 1
return (x,y,error)
# - MapGen Functions -
def PoleGen(hm, NS):
if NS == 0:
rng = randint(2,5)
for i in range(WORLD_WIDTH):
for j in range(rng):
libtcod.heightmap_set_value(hm, i, WORLD_HEIGHT - 1 - j , 0.31)
rng += randint(1,3)-2
if rng > 6:
rng = 5
if rng < 2:
rng = 2
if NS == 1:
rng = randint(2,5)
for i in range(WORLD_WIDTH):
for j in range(rng):
libtcod.heightmap_set_value(hm, i, j , 0.31)
rng += randint(1,3)-2
if rng > 6:
rng = 5
if rng < 2:
rng = 2
return
def TectonicGen(hm, hor):
TecTiles = [[0 for y in range(WORLD_HEIGHT)] for x in range(WORLD_WIDTH)]
#Define Tectonic Borders
if hor == 1:
pos = randint(WORLD_HEIGHT/10,WORLD_HEIGHT - WORLD_HEIGHT/10)
for x in range(WORLD_WIDTH):
TecTiles[x][pos] = 1
pos += randint(1,5)-3
if pos < 0:
pos = 0
if pos > WORLD_HEIGHT-1:
pos = WORLD_HEIGHT-1
if hor == 0:
pos = randint(WORLD_WIDTH/10,WORLD_WIDTH - WORLD_WIDTH/10)
for y in range(WORLD_HEIGHT):
TecTiles[pos][y] = 1
pos += randint(1,5)-3
if pos < 0:
pos = 0
if pos > WORLD_WIDTH-1:
pos = WORLD_WIDTH-1
#Apply elevation to borders
for x in xrange(WORLD_WIDTH/10,WORLD_WIDTH - WORLD_WIDTH/10):
for y in xrange(WORLD_HEIGHT/10,WORLD_HEIGHT - WORLD_HEIGHT/10):
if TecTiles[x][y] == 1 and libtcod.heightmap_get_value(hm, x, y) > 0.3:
libtcod.heightmap_add_hill(hm, x, y, randint(2,4), uniform(0.15,0.18))
return
def Temperature(temp,hm):
for x in xrange(WORLD_WIDTH):
for y in xrange(WORLD_HEIGHT):
heighteffect = 0
if y > WORLD_HEIGHT/2:
libtcod.heightmap_set_value(temp, x, y, WORLD_HEIGHT-y-heighteffect)
else:
libtcod.heightmap_set_value(temp, x, y, y-heighteffect)
heighteffect = libtcod.heightmap_get_value(hm, x, y)
if heighteffect > 0.8:
heighteffect = heighteffect * 5
if y > WORLD_HEIGHT/2:
libtcod.heightmap_set_value(temp, x, y, WORLD_HEIGHT-y-heighteffect)
else:
libtcod.heightmap_set_value(temp, x, y, y-heighteffect)
if heighteffect < 0.25:
heighteffect = heighteffect * 10
if y > WORLD_HEIGHT/2:
libtcod.heightmap_set_value(temp, x, y, WORLD_HEIGHT-y-heighteffect)
else:
libtcod.heightmap_set_value(temp, x, y, y-heighteffect)
return
def Percipitaion(preciphm, temphm):
libtcod.heightmap_add(preciphm, 2)
for x in xrange(WORLD_WIDTH):
for y in xrange(WORLD_HEIGHT):
temp = libtcod.heightmap_get_value(temphm, x, y)
precip = libtcod.noise_new(2,libtcod.NOISE_DEFAULT_HURST, libtcod.NOISE_DEFAULT_LACUNARITY)
libtcod.heightmap_add_fbm(preciphm,precip ,2, 2, 0, 0, 32, 1, 1)
libtcod.heightmap_normalize(preciphm, 0.0, 1.0)
return
def RiverGen(World):
X = randint(0,WORLD_WIDTH-1)
Y = randint(0,WORLD_HEIGHT-1)
XCoor = []
YCoor = []
tries = 0
prev = ""
while World[X][Y].height < 0.8:
tries += 1
X = randint(0,WORLD_WIDTH-1)
Y = randint(0,WORLD_HEIGHT-1)
if tries > 2000:
return
del XCoor[:]
del YCoor[:]
XCoor.append(X)
YCoor.append(Y)
while World[X][Y].height >= 0.2:
X,Y,error = LowestNeighbour(X,Y,World)
if error == 1:
return
try:
if World[X][Y].hasRiver or World[X+1][Y].hasRiver or World[X-1][Y].hasRiver or World[X][Y+1].hasRiver or World[X][Y-1].hasRiver:
break
except IndexError:
return
if X in XCoor and Y in YCoor:
break
XCoor.append(X)
YCoor.append(Y)
if len(XCoor) <= MIN_RIVER_LENGHT:
return
for x in range(len(XCoor)):
if World[XCoor[x]][YCoor[x]].height < 0.2:
break
World[XCoor[x]][YCoor[x]].hasRiver = True
if World[XCoor[x]][YCoor[x]].height >= 0.2 and x == len(XCoor):
World[XCoor[x]][YCoor[x]].hasRiver = True # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Change to Lake later
return
def Prosperity(World):
for x in xrange(WORLD_WIDTH):
for y in xrange(WORLD_HEIGHT):
World[x][y].prosperity = (1.0 - abs(World[x][y].precip - 0.6) + 1.0 - abs(World[x][y].temp - 0.5) + World[x][y].drainage)/3
return
def MasterWorldGen(): #------------------------------------------------------- * MASTER GEN * -------------------------------------------------------------
print ' * World Gen START * '
starttime = time.time()
#Heightmap
hm = libtcod.heightmap_new(WORLD_WIDTH, WORLD_HEIGHT)
for i in range(250):
libtcod.heightmap_add_hill(hm, randint(WORLD_WIDTH/10,WORLD_WIDTH- WORLD_WIDTH/10), randint(WORLD_HEIGHT/10,WORLD_HEIGHT- WORLD_HEIGHT/10), randint(12,16), randint(6,10))
print '- Main Hills -'
for i in range(1000):
libtcod.heightmap_add_hill(hm, randint(WORLD_WIDTH/10,WORLD_WIDTH- WORLD_WIDTH/10), randint(WORLD_HEIGHT/10,WORLD_HEIGHT- WORLD_HEIGHT/10), randint(2,4), randint(6,10))
print '- Small Hills -'
libtcod.heightmap_normalize(hm, 0.0, 1.0)
noisehm = libtcod.heightmap_new(WORLD_WIDTH, WORLD_HEIGHT)
noise2d = libtcod.noise_new(2,libtcod.NOISE_DEFAULT_HURST, libtcod.NOISE_DEFAULT_LACUNARITY)
libtcod.heightmap_add_fbm(noisehm, noise2d,6, 6, 0, 0, 32, 1, 1)
libtcod.heightmap_normalize(noisehm, 0.0, 1.0)
libtcod.heightmap_multiply_hm(hm, noisehm, hm)
print '- Apply Simplex -'
PoleGen(hm, 0)
print '- South Pole -'
PoleGen(hm, 1)
print '- North Pole -'
TectonicGen(hm,0)
TectonicGen(hm,1)
print '- Tectonic Gen -'
libtcod.heightmap_rain_erosion(hm, WORLD_WIDTH*WORLD_HEIGHT ,0.07,0,0)
print '- Erosion -'
libtcod.heightmap_clamp(hm, 0.0, 1.0)
#Temperature
temp = libtcod.heightmap_new(WORLD_WIDTH, WORLD_HEIGHT)
Temperature(temp,hm)
libtcod.heightmap_normalize(temp, 0.0, 1.0)
print '- Temperature Calculation -'
#Precipitation
preciphm = libtcod.heightmap_new(WORLD_WIDTH, WORLD_HEIGHT)
Percipitaion(preciphm, temp)
libtcod.heightmap_normalize(preciphm, 0.0, 1.0)
print '- Percipitaion Calculation -'
#Drainage
drainhm = libtcod.heightmap_new(WORLD_WIDTH, WORLD_HEIGHT)
drain = libtcod.noise_new(2,libtcod.NOISE_DEFAULT_HURST, libtcod.NOISE_DEFAULT_LACUNARITY)
libtcod.heightmap_add_fbm(drainhm,drain ,2, 2, 0, 0, 32, 1, 1)
libtcod.heightmap_normalize(drainhm, 0.0, 1.0)
print '- Drainage Calculation -'
# VOLCANISM - RARE AT SEA FOR NEW ISLANDS (?) RARE AT MOUNTAINS > 0.9 (?) RARE AT TECTONIC BORDERS (?)
elapsed_time = time.time() - starttime
print ' * World Gen DONE * in: ',elapsed_time,' seconds'
#Initialize Tiles with Map values
World = [[0 for y in range(WORLD_HEIGHT)] for x in range(WORLD_WIDTH)]
for x in xrange(WORLD_WIDTH):
for y in xrange(WORLD_HEIGHT):
World[x][y] = Tile(libtcod.heightmap_get_value(hm, x, y),
libtcod.heightmap_get_value(temp, x, y),
libtcod.heightmap_get_value(preciphm, x, y),
libtcod.heightmap_get_value(drainhm, x, y),
0)
print '- Tiles Initialized -'
#Prosperity
Prosperity(World)
print '- Prosperity Calculation -'
#Biome info to Tile
for x in xrange(WORLD_WIDTH):
for y in xrange(WORLD_HEIGHT):
if World[x][y].precip >= 0.10 and World[x][y].precip < 0.33 and World[x][y].drainage < 0.5:
World[x][y].biomeID = 3
if randint(1,2) == 2:
World[x][y].biomeID = 16
if World[x][y].precip >= 0.10 and World[x][y].precip > 0.33:
World[x][y].biomeID = 2
if World[x][y].precip >= 0.66:
World[x][y].biomeID = 1
if World[x][y].precip >= 0.33 and World[x][y].precip < 0.66 and World[x][y].drainage >= 0.33:
World[x][y].biomeID = 15
if randint(1,5) == 5:
World[x][y].biomeID = 5
if World[x][y].temp > 0.2 and World[x][y].precip >= 0.66 and World[x][y].drainage > 0.33:
World[x][y].biomeID = 5
if World[x][y].precip >= 0.75:
World[x][y].biomeID = 6
if randint(1,5) == 5:
World[x][y].biomeID = 15
if World[x][y].precip >= 0.10 and World[x][y].precip < 0.33 and World[x][y].drainage >= 0.5:
World[x][y].biomeID = 16
if randint(1,2) == 2:
World[x][y].biomeID = 14
if World[x][y].precip < 0.10:
World[x][y].biomeID = 4
if World[x][y].drainage > 0.5:
World[x][y].biomeID = 16
if randint(1,2) == 2:
World[x][y].biomeID = 14
if World[x][y].drainage >= 0.66:
World[x][y].biomeID = 8
if World[x][y].height <= 0.2:
World[x][y].biomeID = 0
if World[x][y].temp <= 0.2 and World[x][y].height > 0.15:
World[x][y].biomeID = randint(11,13)
if World[x][y].height > 0.6:
World[x][y].biomeID = 9
if World[x][y].height > 0.9:
World[x][y].biomeID = 10
print '- BiomeIDs Atributed -'
#River Gen
for x in range(1):
RiverGen(World)
print '- River Gen -'
#Free Heightmaps
libtcod.heightmap_delete(hm)
libtcod.heightmap_delete(temp)
libtcod.heightmap_delete(noisehm)
print ' * Biomes/Rivers Sorted *'
return World
def ReadRaces():
RacesFile = 'Races.txt'
NLines = sum(1 for line in open('Races.txt'))
NRaces = NLines / 7
f = open(RacesFile)
Races = [0 for x in range(NRaces)]
for x in range(NRaces): #Reads info between ']' and '\n'
Info = [0 for a in range(7)]
for y in range(7):
data = f.readline()
start = data.index("]") + 1
end = data.index("\n",start)
Info[y] = data[start:end]
PreferedBiomes = [int(s) for s in str.split(Info[1]) if s.isdigit()] #Take numbers from string
Races[x] = Race(Info[0],PreferedBiomes,int(Info[2]),int(Info[3]),int(Info[4]),int(Info[5]),Info[6])
f.close()
print '- Races Read -'
return Races
def ReadGovern():
GovernFile = 'CivilizedGovernment.txt'
NLines = sum(1 for line in open('CivilizedGovernment.txt'))
NGovern = NLines / 5
f = open(GovernFile)
Governs = [0 for x in range(NGovern)]
for x in range(NGovern): #Reads info between ']' and '\n'
Info = [0 for a in range(5)]
for y in range(5):
data = f.readline()
start = data.index("]") + 1
end = data.index("\n",start)
Info[y] = data[start:end]
Governs[x] = GovernmentType(Info[0],Info[1],int(Info[2]),int(Info[3]),int(Info[4]))
f.close()
print '- Government Types Read -'
return Governs
def CivGen(Races,Govern): #-------------------------------------------------------------------- * CIV GEN * ----------------------------------------------------------------------------------
Civs = []
for x in range(CIVILIZED_CIVS):
libtcod.namegen_parse('namegen/jice_fantasy.cfg')
Name = libtcod.namegen_generate('Fantasy male')
libtcod.namegen_destroy ()
Name += " Civilization"
Race = Races[randint(0,len(Races)-1)]
while Race.Form != "civilized":
Race = Races[randint(0,len(Races)-1)]
Government = Govern[randint(0,len(Govern)-1)]
Color = Palette[randint(0,len(Palette)-1)]
Flag = FlagGenerator(Color)
#Initialize Civ
Civs.append(Civ(Race,Name,Government,Color,Flag,0))
for a in range(TRIBAL_CIVS):
libtcod.namegen_parse('namegen/jice_fantasy.cfg')
Name = libtcod.namegen_generate('Fantasy male')
libtcod.namegen_destroy ()
Name += " Tribe"
Race = Races[randint(0,len(Races)-1)]
while Race.Form != "tribal":
Race = Races[randint(0,len(Races)-1)]
Government = GovernmentType("Tribal","*PLACE HOLDER*",2,50,0)
Color = libtcod.Color(randint(0,255),randint(0,255),randint(0,255))
Flag = FlagGenerator(Color)
#Initialize Civ
Civs.append(Civ(Race,Name,Government,Color,Flag,0))
print '- Civs Generated -'
return Civs
def SetupCivs(Civs, World, Chars, Colors):
for x in range(len(Civs)):
Civs[x].Sites = []
Civs[x].SuitableSites = []
del Civs[x].SuitableSites[:]
#Civs[x].PrintInfo()
for i in range(WORLD_WIDTH):
for j in range (WORLD_HEIGHT):
for g in range (len(Civs[x].Race.PrefBiome)):
if World[i][j].biomeID == Civs[x].Race.PrefBiome[g]:
Civs[x].SuitableSites.append(CivSite(i,j,"",1,0))
rand = randint(0,len(Civs[x].SuitableSites)-1)
while World[Civs[x].SuitableSites[rand].x][Civs[x].SuitableSites[rand].y].isCiv == True:
del Civs[x].SuitableSites[rand]
rand = randint(0,len(Civs[x].SuitableSites)-1)
X = Civs[x].SuitableSites[rand].x
Y = Civs[x].SuitableSites[rand].y
World[X][Y].isCiv = True
FinalProsperity = World[X][Y].prosperity * 150
if World[X][Y].hasRiver:
FinalProsperity = FinalProsperity * 1.5
PopCap = 4 * Civs[x].Race.ReproductionSpeed + FinalProsperity
PopCap = PopCap * 2 #Capital Bonus
PopCap = round(PopCap)
Civs[x].Sites.append (CivSite(X,Y,"Village",0,PopCap))
Civs[x].Sites[0].isCapital = True
Civs[x].Sites[0].Population = 20
Chars[X][Y] = 31
Colors[X][Y] = Civs[x].Color
Civs[x].PrintInfo()
print '- Civs Setup -'
print ' * Civ Gen DONE *'
return Civs
##################################################################################### - PROCESS CIVS - ##################################################################################
def NewSite(Civ, Origin, World,Chars,Colors):
rand = randint(0,len(Civ.SuitableSites)-1)
Tries = 0
while PointDistRound(Origin.x, Origin.y, Civ.SuitableSites[rand].x, Civ.SuitableSites[rand].y) > EXPANSION_DISTANCE or World[Civ.SuitableSites[rand].x][Civ.SuitableSites[rand].y].isCiv:
if Tries > 200:
return Civ
Tries += 1
rand = randint(0,len(Civ.SuitableSites)-1)
X = Civ.SuitableSites[rand].x
Y = Civ.SuitableSites[rand].y
World[X][Y].isCiv = True
FinalProsperity = World[X][Y].prosperity * 150
if World[X][Y].hasRiver:
FinalProsperity = FinalProsperity * 1.5
PopCap = 3 * Civ.Race.ReproductionSpeed + FinalProsperity
PopCap = round(PopCap)
Civ.Sites.append ( CivSite(X,Y,"Village",0,PopCap) )
Civ.Sites[len(Civ.Sites)-1].Population = 20
Chars[X][Y] = 31
Colors[X][Y] = Civ.Color
global needUpdate
needUpdate = True
return Civ
def ProcessCivs(World,Civs,Chars,Colors,Month):
print "------------------------------------------"
for x in range(CIVILIZED_CIVS+TRIBAL_CIVS):
print Civs[x].Name
print Civs[x].Race.Name
Civs[x].TotalPopulation = 0
#Site
for y in range(len(Civs[x].Sites)):
#Population
NewPop = int(round(Civs[x].Sites[y].Population * Civs[x].Race.ReproductionSpeed/1500))
if Civs[x].Sites[y].Population > Civs[x].Sites[y].popcap / 2:
NewPop /= 6
Civs[x].Sites[y].Population += NewPop
#Expand
if Civs[x].Sites[y].Population > Civs[x].Sites[y].popcap:
Civs[x].Sites[y].Population = int(round(Civs[x].Sites[y].popcap))
if len(Civs[x].Sites) < CIV_MAX_SITES:
Civs[x].Sites[y].Population = int(round(Civs[x].Sites[y].popcap / 2))
Civs[x] = NewSite(Civs[x],Civs[x].Sites[y],World,Chars,Colors)
Civs[x].TotalPopulation += Civs[x].Sites[y].Population
#Diplomacy
for a in range(CIVILIZED_CIVS+TRIBAL_CIVS):
for b in range(len(Civs[a].Sites)):
if x == a:
break
if PointDistRound(Civs[x].Sites[y].x,Civs[x].Sites[y].y,Civs[a].Sites[b].x,Civs[a].Sites[b].y) < WAR_DISTANCE:
AlreadyWar = False
for c in range(len(Wars)):
if (Wars[c].Side1 == Civs[x] and Wars[c].Side2 == Civs[a]) or (Wars[c].Side1 == Civs[a] and Wars[c].Side2 == Civs[x]):
#Already at War
AlreadyWar = True
if AlreadyWar == False:
#Start War and form armies if dot have army yet
Wars.append(War(Civs[x],Civs[a]))
if Civs[a].atWar == False: #if not already at war form new army
Civs[a].Army = Army(Civs[a].Sites[0].x,
Civs[a].Sites[0].y,
Civs[a],
Civs[a].TotalPopulation * Civs[a].Government.Militarization / 100)
Civs[a].atWar = True
if Civs[x].atWar == False: #if not already at war form new army
Civs[x].Army = Army(Civs[x].Sites[0].x,
Civs[x].Sites[0].y,
Civs[x],
Civs[x].TotalPopulation * Civs[x].Government.Militarization / 100)
Civs[x].atWar = True
print "X:",Civs[x].Sites[y].x,"Y:",Civs[x].Sites[y].y,"Population:",Civs[x].Sites[y].Population
print Civs[x].Army.x,Civs[x].Army.y,Civs[x].Army.Size,'\n'
return
####################################################################################### - MAP MODES - ####################################################################################
# --------------------------------------------------------------------------------- Print Map (Terrain) --------------------------------------------------------------------------------
def TerrainMap(World):
for x in xrange(WORLD_WIDTH):
for y in xrange(WORLD_HEIGHT):
hm_v = World[x][y].height
libtcod.console_put_char_ex( 0, x, y + SCREEN_HEIGHT/2 - WORLD_HEIGHT/2, '0' , libtcod.blue, libtcod.black)
if hm_v > 0.1:
libtcod.console_put_char_ex( 0, x, y + SCREEN_HEIGHT/2 - WORLD_HEIGHT/2, '1', libtcod.blue, libtcod.black)
if hm_v > 0.2:
libtcod.console_put_char_ex( 0, x, y + SCREEN_HEIGHT/2 - WORLD_HEIGHT/2, '2', Palette[0], libtcod.black)
if hm_v > 0.3:
libtcod.console_put_char_ex( 0, x, y + SCREEN_HEIGHT/2 - WORLD_HEIGHT/2, '3', Palette[0], libtcod.black)
if hm_v > 0.4:
libtcod.console_put_char_ex( 0, x, y + SCREEN_HEIGHT/2 - WORLD_HEIGHT/2, '4', Palette[0], libtcod.black)
if hm_v > 0.5:
libtcod.console_put_char_ex( 0, x, y + SCREEN_HEIGHT/2 - WORLD_HEIGHT/2, '5', Palette[0], libtcod.black)
if hm_v > 0.6:
libtcod.console_put_char_ex( 0, x, y + SCREEN_HEIGHT/2 - WORLD_HEIGHT/2, '6', Palette[0], libtcod.black)
if hm_v > 0.7:
libtcod.console_put_char_ex( 0, x, y + SCREEN_HEIGHT/2 - WORLD_HEIGHT/2, '7', Palette[0], libtcod.black)
if hm_v > 0.8:
libtcod.console_put_char_ex( 0, x, y + SCREEN_HEIGHT/2 - WORLD_HEIGHT/2, '8', libtcod.dark_sepia, libtcod.black)
if hm_v > 0.9:
libtcod.console_put_char_ex( 0, x, y + SCREEN_HEIGHT/2 - WORLD_HEIGHT/2, '9', libtcod.light_gray, libtcod.black)
if hm_v > 0.99:
libtcod.console_put_char_ex( 0, x, y + SCREEN_HEIGHT/2 - WORLD_HEIGHT/2, '^', libtcod.darker_gray, libtcod.black)
libtcod.console_flush()
return
def BiomeMap(Chars,Colors):
for x in xrange(WORLD_WIDTH):
for y in xrange(WORLD_HEIGHT):
libtcod.console_put_char_ex( 0, x, y + SCREEN_HEIGHT/2 - WORLD_HEIGHT/2, Chars[x][y] , Colors[x][y], libtcod.black)
libtcod.console_flush()
return
def HeightGradMap(World): # ------------------------------------------------------------ Print Map (Heightmap Gradient) -------------------------------------------------------------------
for x in xrange(WORLD_WIDTH):
for y in xrange(WORLD_HEIGHT):
hm_v = World[x][y].height
HeightColor = libtcod.Color(255,255,255)
libtcod.color_set_hsv(HeightColor,0,0,hm_v ) #Set lightness to hm_v so higher heightmap value -> "whiter"
libtcod.console_put_char_ex( 0, x, y + SCREEN_HEIGHT/2 - WORLD_HEIGHT/2, '\333' , HeightColor, libtcod.black)
libtcod.console_flush()
return
def TempGradMap(World): # ------------------------------------------------------------ Print Map (Surface Temperature Gradient) white -> cold red -> warm --------------------------------
for x in xrange(WORLD_WIDTH):
for y in xrange(WORLD_HEIGHT):
tempv = World[x][y].temp
tempcolor = libtcod.color_lerp ( libtcod.white, libtcod.red,tempv)
libtcod.console_put_char_ex( 0, x, y + SCREEN_HEIGHT/2 - WORLD_HEIGHT/2, '\333' , tempcolor, libtcod.black)
libtcod.console_flush()
return
def PrecipGradMap(World): # ------------------------------------------------------------ Print Map (Precipitation Gradient) white -> low blue -> high --------------------------------
for x in xrange(WORLD_WIDTH):
for y in xrange(WORLD_HEIGHT):
tempv = World[x][y].precip
tempcolor = libtcod.color_lerp ( libtcod.white, libtcod.light_blue,tempv)
libtcod.console_put_char_ex( 0, x, y + SCREEN_HEIGHT/2 - WORLD_HEIGHT/2, '\333' , tempcolor, libtcod.black)
libtcod.console_flush()
return
def DrainageGradMap(World): # ------------------------------------------------------------ Print Map (Drainage Gradient) brown -> low white -> high --------------------------------
for x in xrange(WORLD_WIDTH):
for y in xrange(WORLD_HEIGHT):
drainv = World[x][y].drainage
draincolor = libtcod.color_lerp ( libtcod.darkest_orange, libtcod.white,drainv)
libtcod.console_put_char_ex( 0, x, y + SCREEN_HEIGHT/2 - WORLD_HEIGHT/2, '\333' , draincolor, libtcod.black)
libtcod.console_flush()
return
def ProsperityGradMap(World): # ------------------------------------------------------------ Print Map (Prosperity Gradient) white -> low green -> high --------------------------------
for x in xrange(WORLD_WIDTH):
for y in xrange(WORLD_HEIGHT):
prosperitynv = World[x][y].prosperity
prosperitycolor = libtcod.color_lerp ( libtcod.white, libtcod.darker_green,prosperitynv)
libtcod.console_put_char_ex( 0, x, y + SCREEN_HEIGHT/2 - WORLD_HEIGHT/2, '\333' , prosperitycolor, libtcod.black)
libtcod.console_flush()
return
def NormalMap(World): # ------------------------------------------------------------ Normal Map (Biome + Entities) --------------------------------
Chars = [[0 for y in range(WORLD_HEIGHT)] for x in range(WORLD_WIDTH)]
Colors = [[0 for y in range(WORLD_HEIGHT)] for x in range(WORLD_WIDTH)]
def SymbolDictionary(x):
char = ''
if x == 15 or x == 8:
if randint(1,2) == 2:
char = 251
else:
char = ','
if x == 1:
if randint(1,2) == 2:
char = 244
else:
char = 131
if x == 2:
if randint(1,2) == 2:
char = '"'
else:
char = 163
return {
0: '\367',
1: char,
2: char,
3: 'n',
4: '\367',
5: 24,
6: 6-randint(0,1),
8: char,
9: 127,
10: 30,
11: 176,
12: 177,
13: 178,
14: 'n',
15: char,
16: 139
}[x]
def ColorDictionary(x):
badlands = libtcod.Color(204, 159, 81)
icecolor = libtcod.Color(176, 223, 215)
darkgreen = libtcod.Color(68,158,53)
lightgreen = libtcod.Color(131,212,82)
water = libtcod.Color(13,103,196)
mountain = libtcod.Color(185,192,162)
desert = libtcod.Color(255,218,90)
return {
0: water,
1: darkgreen,
2: lightgreen,
3: lightgreen,
4: desert,
5: darkgreen,
6: darkgreen,
8: badlands,
9: mountain,
10: mountain,
11: icecolor,
12: icecolor,
13: icecolor,
14: darkgreen,
15: lightgreen,
16: darkgreen
}[x]
for x in xrange(WORLD_WIDTH):
for y in xrange(WORLD_HEIGHT):
Chars[x][y] = SymbolDictionary(World[x][y].biomeID)
Colors[x][y] = ColorDictionary(World[x][y].biomeID)
if World[x][y].hasRiver:
Chars[x][y] = 'o'
Colors[x][y] = libtcod.light_blue
return Chars, Colors
###################################################################################### - Startup - ######################################################################################
#Start Console and set costum font
libtcod.console_set_custom_font("Andux_cp866ish.png", libtcod.FONT_LAYOUT_ASCII_INROW)
libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'pyWorld', False, libtcod.RENDERER_SDL) #Set True for Fullscreen
#Palette
Palette = [libtcod.Color(255, 45, 33), #Red
libtcod.Color(254, 80, 0), #Orange
libtcod.Color(0, 35, 156), #Blue
libtcod.Color(71, 45, 96), #Purple
libtcod.Color(0, 135, 199), #Ocean Blue
libtcod.Color(254, 221, 0), #Yellow
libtcod.Color(255, 255, 255), #White
libtcod.Color(99, 102, 106)] #Gray
#libtcod.sys_set_fps(30)
#libtcod.console_set_fullscreen(True)