-
Notifications
You must be signed in to change notification settings - Fork 0
/
overworld.py
255 lines (192 loc) · 6.24 KB
/
overworld.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
import libtcodpy as libtcod
import math,random,copy
from config import *
from util import *
class OverworldTile:
char='.'
x=0
y=0
blocked=False
foreColor=None
backColor=None
seen=False
def __init__(self,x,y):
self.x=x
self.y=y
def setChar(self,char):
self.char=char
def setColors(self,fore,back):
self.foreColor=fore
self.backColor=back
def setBlocked(self,blocked):
self.blocked=blocked
def isBlocked(self): return self.blocked
def isSeen(self): return self.seen
def gotSeen(self): self.seen=True
def draw(self,console,offset_x,offset_y):
if not self.seen: return
libtcod.console_put_char_ex( console, self.x-offset_x, self.y-offset_y, self.char, self.foreColor, self.backColor)
class OverworldTileEntity:
char='#'
x=0
y=0
foreColor=None
backColor=None
def __init__(self,x,y):
self.x=x
self.y=y
def setChar(self,char):
self.char=char
def setColors(self,fore,back):
self.foreColor=fore
self.backColor=back
def position(self): return (self.x,self.y)
def draw(self,console,offset_x,offset_y):
libtcod.console_put_char_ex( console, self.x-offset_x, self.y-offset_y, self.char, self.foreColor, self.backColor)
class Overworld:
level=None
console=None
width=0
height=0
pathable=[]
blockedMap=[]
player=None
tile_entity=[]
town=None
def render(self):
c=self.console
libtcod.console_clear(c)
wx=cfg.SCREEN_WIDTH
wy=cfg.SCREEN_HEIGHT
self.playerReveal()
offset_x=self.player.x-cfg.WID2
if offset_x+wx > self.width: offset_x=self.width-wx
if offset_x<0: offset_x=0
offset_y=self.player.y-cfg.HGT2
if offset_y+wy > self.height: offset_y=self.height-wy
if offset_y<0: offset_y=0
for x in xrange(wx):
for y in xrange(wy):
self.level[offset_x+x][offset_y+y].draw(c,offset_x,offset_y)
for entity in self.tile_entity:
pos=entity.position()
#if self.level[pos[0]][pos[1]].seen: entity.draw(c,offset_x,offset_y)
entity.draw(c,offset_x,offset_y)
self.player.draw(c,offset_x,offset_y)
libtcod.console_blit(self.console,0,0,wx,wy,0,0,0)
def playerReveal(self):
sight=15
libtcod.map_compute_fov(self.blockedMap,self.player.x,self.player.y,sight,True)
self.level[self.player.x][self.player.y].gotSeen()
wx=cfg.WID2
wy=cfg.HGT2
for x in xrange(self.player.x-wx,self.player.x+wx):
for y in xrange(self.player.y-wy,self.player.y+wy):
if libtcod.map_is_in_fov(self.blockedMap, x, y):
self.level[x][y].gotSeen()
def playerStart(self,player):
self.player=player
townPos=self.town.position()
self.player.setPosition(townPos[0],townPos[1])
self.player.newLevel(self)
def buildBlockedMap(self):
bmap = libtcod.map_new(self.width,self.height)
for x in xrange(self.width):
for y in xrange(self.height):
if self.level[x][y].blocked:
libtcod.map_set_properties(bmap,x,y,False,False)
else:
libtcod.map_set_properties(bmap,x,y,True,True)
self.blockedMap=bmap
def getWidth(self): return self.width
def getHeight(self): return self.height
def getBlockedMap(self): return self.blockedMap
def getPathable(self): return self.pathable
def isPathable(self,x,y): return (x,y) in self.pathable
def isSeen(self,x,y): return self.level[x][y].isSeen()
def isBlocked(self,x,y): return self.level[x][y].isBlocked()
def putThing(self,x,y,char="+"): #debug
rlist=copy.copy(self.tile_entity)
for e in rlist:
if e.position()==(x,y):
self.tile_entity.remove(e)
thing=OverworldTileEntity(x,y)
thing.setChar(char)
thing.setColors(libtcod.Color(random.randint(0,255), random.randint(0,255), random.randint(0,255)),libtcod.Color(0, 0, 0))
self.tile_entity.append(thing)
def findPathable(self):
self.pathable=[]
w=self.width
h=self.height
start=(random.randint(1,w-1),random.randint(1,h-1))
while self.level[start[0]][start[1]].isBlocked(): start=(random.randint(1,w-1),random.randint(1,h-1))
openlist=[]
openlist.append(start)
self.pathable.append(start)
#rels=((1,1),(1,-1),(-1,1),(-1,-1))
rels=((0,1),(0,-1),(-1,0),(1,0))
while len(openlist):
newlist=copy.copy(openlist)
for coord in openlist:
for rel in rels:
crd=(coord[0]+rel[0],coord[1]+rel[1])
if crd[0]<0 or crd[0]>=w or crd[1]<0 or crd[1]>=h: continue
if self.level[crd[0]][crd[1]].isBlocked(): continue
if crd not in self.pathable:
newlist.append(crd)
self.pathable.append(crd)
newlist.remove(coord)
openlist=copy.copy(newlist)
def clearUnpathableAreas(self):
w=self.width
h=self.height
for x in xrange(w):
for y in xrange(h):
if self.level[x][y].blocked: continue
if not (x,y) in self.pathable:
self.level[x][y].setBlocked(True)
def create(self):
w=self.width=cfg.OW_WIDTH
h=self.height=cfg.OW_HEIGHT
th=cfg.OW_TREE_THRES
self.level=[[OverworldTile(j,i) for i in xrange(h)] for j in xrange(w)]
self.console=libtcod.console_new(w,h)
backColor=libtcod.Color(0, 0, 0)
noise2d = libtcod.noise_new(2)
for x in xrange(w):
for y in xrange(h):
zoom=0.09
f = [zoom * x,zoom * y]
val = libtcod.noise_get(noise2d,f)
c1=int((((val*-1)+1)/2)*30)
c2=10+int(((val+1)/2)*20)
if val>th:
self.level[x][y].setChar(23)
self.level[x][y].setColors(libtcod.Color(0, 200, 0),libtcod.Color(0, 0, 0))
self.level[x][y].setBlocked(True)
else:
self.level[x][y].setChar(176)
self.level[x][y].setColors(libtcod.Color(0, c1, 0),libtcod.Color(0, c2, 0))
while len(self.pathable)<400: self.findPathable()
self.clearUnpathableAreas()
#self.findPathable() # Now a final scan for the full area
# Place town
town_pos=random.choice(self.pathable)
town=OverworldTileEntity(town_pos[0],town_pos[1])
town.setColors(libtcod.Color(0, 100, 150),libtcod.Color(40, 40, 0))
self.tile_entity.append(town)
self.town=town
# Place dungeons
for i in xrange(cfg.DUNGEONS):
validLocation=False
pos=None
while not validLocation:
validLocation=True
pos=random.choice(self.pathable)
for entity in self.tile_entity:
if entity.position()==pos:
validLocation=False
dungeon=OverworldTileEntity(pos[0],pos[1])
dungeon.setColors(libtcod.Color(200, 0, 0),libtcod.Color(40, 0, 0))
self.tile_entity.append(dungeon)
self.buildBlockedMap()