Repository navigation
Expand file tree
/
Copy pathlevel.go
More file actions
414 lines (363 loc) · 10.8 KB
/
Copy pathlevel.go
File metadata and controls
414 lines (363 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package tinyrogue
import (
"strconv"
"strings"
"github.com/firefly-zero/firefly-go/firefly"
"github.com/orsinium-labs/tinymath"
)
// TileType represents a type of tile in the level.
type TileType int
const (
WALL TileType = iota
FLOOR
ENTRANCE
EXIT
)
// Each of the map tiles will be represented by one of these structures
type MapTile struct {
PixelX int // Upper left corner of the tile
PixelY int
Blocked bool // tile should block the player or creatures?
Image *firefly.Image // image for this tile
Visible bool
Explored bool
TileType TileType
}
// Level holds the tile information for a complete dungeon level.
type Level struct {
Name string
Generated bool
Tiles []*MapTile
Rooms []Rect
FloorTypes string
WallTypes string
ViewRadius int
Entrance *Portal
Exit *Portal
}
// NewLevel creates a new game level in a dungeon.
func NewLevel(name, floors, walls string) *Level {
l := &Level{
Name: name,
Rooms: make([]Rect, 0),
FloorTypes: floors,
WallTypes: walls,
}
return l
}
// Tiles will be stored in one slice. We will use GetIndexFromXY to
// determine which tile to return.
// GetIndexFromXY gets the index of the map array from a given X,Y TILE coordinate.
// This coordinate is logical tiles, not pixels.
func (level *Level) GetIndexFromXY(x int, y int) int {
return CurrentGame().GetIndexFromXY(x, y)
}
// createTiles creates a map of all walls as a baseline for carving out a level.
func (level *Level) createTiles() []*MapTile {
gd := CurrentGame().Data
tiles := make([]*MapTile, gd.Rows*gd.Cols)
index := 0
for x := 0; x < gd.Cols; x++ {
for y := 0; y < gd.Rows; y++ {
index = level.GetIndexFromXY(x, y)
tile := MapTile{
PixelX: x * gd.TileWidth,
PixelY: y * gd.TileHeight,
Blocked: true,
Image: level.getWallImage(),
TileType: WALL,
}
tiles[index] = &tile
}
}
logDebug("Total tiles created: " + strconv.Itoa(len(tiles)))
return tiles
}
// createRoom creates a room in the level.
func (level *Level) createRoom(room Rect) {
for y := room.Y1 + 1; y < room.Y2; y++ {
for x := room.X1 + 1; x < room.X2; x++ {
index := level.GetIndexFromXY(x, y)
if index > 0 && index < len(level.Tiles) {
level.setFloorTile(level.Tiles[index])
}
}
}
}
// getWallImage returns a random wall image from the list of wall images.
func (level *Level) getWallImage() *firefly.Image {
walls := strings.Split(level.WallTypes, ",")
if len(walls) < 2 {
img := CurrentGame().Images[walls[0]]
return &img
}
wall := walls[GetDiceRoll(len(walls))-1]
img := CurrentGame().Images[wall]
return &img
}
// getFloorImage returns a random floor image from the list of floor images.
func (level *Level) getFloorImage() *firefly.Image {
floors := strings.Split(level.FloorTypes, ",")
if len(floors) < 2 {
img := CurrentGame().Images[floors[0]]
return &img
}
floor := floors[GetDiceRoll(len(floors))-1]
img := CurrentGame().Images[floor]
return &img
}
// Generate creates a new Dungeon Level Map.
func (level *Level) Generate() {
if level.Generated {
return
}
gd := CurrentGame().Data
tiles := level.createTiles()
level.Tiles = tiles
contains_rooms := false
for idx := 0; idx < gd.MaxRooms; idx++ {
w := GetRandomBetween(gd.MinSize, gd.MaxSize)
h := GetRandomBetween(gd.MinSize, gd.MaxSize)
x := GetDiceRoll(gd.Cols - w - 1)
y := GetDiceRoll(gd.Rows - h - 1)
new_room := NewRect(x, y, w, h)
okToAdd := true
for _, otherRoom := range level.Rooms {
if new_room.Intersect(otherRoom) {
okToAdd = false
break
}
}
if okToAdd {
level.createRoom(new_room)
if contains_rooms {
newX, newY := new_room.Center()
prevX, prevY := level.Rooms[len(level.Rooms)-1].Center()
coinflip := GetDiceRoll(2)
if coinflip == 2 {
level.createHorizontalTunnel(prevX, newX, prevY)
level.createVerticalTunnel(prevY, newY, newX)
} else {
level.createHorizontalTunnel(prevX, newX, newY)
level.createVerticalTunnel(prevY, newY, prevX)
}
}
level.Rooms = append(level.Rooms, new_room)
contains_rooms = true
}
}
level.Generated = true
logDebug("Total rooms created: " + strconv.Itoa(len(level.Rooms)))
}
// ConnectExits connects the exits of two levels. Should only be called
// after both the startLevel and destinationLevel have been generated.
func ConnectExits(startDungeon *Dungeon, startLevel *Level, destinationDungeon *Dungeon, destinationLevel *Level) {
portalImg := CurrentGame().Images["portal"]
// Place entrance at an open location
entrancePos := destinationLevel.OpenLocation()
p := NewPortal("portal", &portalImg, startDungeon, startLevel)
destinationLevel.SetEntrance(p, entrancePos)
// what is the destination level exit?
nextLevel := destinationDungeon.NextLevel(destinationLevel)
if nextLevel == nil {
nextDungeon := CurrentGame().NextDungeon()
if nextDungeon == nil {
// end of the game?
logDebug("End of the game reached.")
return
}
// to another dungeon, first level
p = NewPortal("portal", &portalImg, nextDungeon, nextDungeon.Levels[0])
exitPos := destinationLevel.OpenLocationReachableFrom(entrancePos)
destinationLevel.SetExit(p, exitPos)
return
}
p = NewPortal("portal", &portalImg, destinationDungeon, nextLevel)
exitPos := destinationLevel.OpenLocationReachableFrom(entrancePos)
destinationLevel.SetExit(p, exitPos)
}
// createHorizontalTunnel creates a horizontal tunnel between two points.
func (level *Level) createHorizontalTunnel(x1 int, x2 int, y int) {
gd := CurrentGame().Data
for x := tinymath.Min(float32(x1), float32(x2)); x < tinymath.Max(float32(x1), float32(x2))+1; x++ {
index := level.GetIndexFromXY(int(x), y)
if index > 0 && index < gd.Rows*gd.Cols {
level.setFloorTile(level.Tiles[index])
}
}
}
// createVerticalTunnel creates a vertical tunnel between two points.
func (level *Level) createVerticalTunnel(y1 int, y2 int, x int) {
gd := CurrentGame().Data
for y := tinymath.Min(float32(y1), float32(y2)); y < tinymath.Max(float32(y1), float32(y2))+1; y++ {
index := level.GetIndexFromXY(x, int(y))
if index > 0 && index < gd.Rows*gd.Cols {
level.setFloorTile(level.Tiles[index])
}
}
}
func (level *Level) setFloorTile(tile *MapTile) {
tile.TileType = FLOOR
tile.Image = level.getFloorImage()
tile.Blocked = false
}
// InBounds checks if the given x and y coordinates are within the level bounds.
func (level *Level) InBounds(x, y int) bool {
gd := CurrentGame().Data
if x < 0 || x > gd.Cols-1 || y < 0 || y > gd.Rows-1 {
return false
}
return true
}
// IsOpaque checks if the given x and y coordinates are within the level bounds.
func (level *Level) IsOpaque(x, y int) bool {
idx := level.GetIndexFromXY(x, y)
return level.Tiles[idx].TileType == WALL
}
// Block sets the blocked property of a tile at the given [Position].
func (level *Level) Block(pos Position, block bool) {
level.Tiles[level.GetIndexFromXY(pos.X, pos.Y)].Blocked = block
}
// Draw the level.
func (level *Level) Draw() {
gd := CurrentGame().Data
for x := 0; x < gd.Cols; x++ {
for y := 0; y < gd.Rows; y++ {
idx := level.GetIndexFromXY(x, y)
tile := level.Tiles[idx]
if tile.Visible || tile.Explored || !CurrentGame().UseFOV {
firefly.DrawImage(*tile.Image, firefly.Point{X: tile.PixelX, Y: tile.PixelY})
}
}
}
}
// RandomLocation returns a random location in the level.
func (level *Level) RandomLocation() (Position, bool) {
if len(level.Rooms) == 0 {
return Position{}, false
}
randomRoom := level.Rooms[GetDiceRoll(len(level.Rooms))-1]
x, y := randomRoom.Center()
idx := level.GetIndexFromXY(x, y)
tile := level.Tiles[idx]
return Position{x, y}, !tile.Blocked && tile.TileType == FLOOR
}
// OpenLocation returns an open location in the level. Used for "spawning".
func (level *Level) OpenLocation() Position {
for {
pos, free := level.RandomLocation()
if free {
return pos
}
}
}
// OpenLocationReachableFrom returns an open location that is reachable from the given position.
// This ensures there is a valid path between the two positions.
func (level *Level) OpenLocationReachableFrom(from Position) Position {
as := AStar{}
maxAttempts := 100
for i := 0; i < maxAttempts; i++ {
pos, free := level.RandomLocation()
if !free {
continue
}
// Check if there's a path from 'from' to this position
path := as.GetPath(level, from, pos)
if len(path) > 0 {
return pos
}
}
// Fallback: return first reachable floor tile
for _, tile := range level.Tiles {
if tile.TileType == FLOOR && !tile.Blocked {
pos := Position{
X: tile.PixelX / CurrentGame().Data.TileWidth,
Y: tile.PixelY / CurrentGame().Data.TileHeight,
}
path := as.GetPath(level, from, pos)
if len(path) > 0 {
return pos
}
}
}
// Last resort: return the from position itself
logError("Could not find reachable location, returning start position")
return from
}
// SetEntrance sets the entrance to the level.
func (level *Level) SetEntrance(p *Portal, pos Position) {
level.Entrance = p
tile := level.Tiles[level.GetIndexFromXY(pos.X, pos.Y)]
tile.TileType = ENTRANCE
tile.Image = p.Image
tile.Blocked = false
}
// GetEntrancePosition returns the position of the entrance for this level.
func (level *Level) GetEntrancePosition() Position {
for i, tile := range level.Tiles {
if tile.TileType == ENTRANCE {
return Position{X: i % CurrentGame().Data.Cols, Y: i / CurrentGame().Data.Cols}
}
}
logDebug("No entrance found")
return Position{}
}
// SetExit sets the exit to the level.
func (level *Level) SetExit(p *Portal, pos Position) {
level.Exit = p
tile := level.Tiles[level.GetIndexFromXY(pos.X, pos.Y)]
tile.TileType = EXIT
tile.Image = p.Image
tile.Blocked = false
}
// GetExitPosition returns the position of the exit for this level.
func (level *Level) GetExitPosition() Position {
for i, tile := range level.Tiles {
if tile.TileType == EXIT {
return Position{X: i % CurrentGame().Data.Cols, Y: i / CurrentGame().Data.Cols}
}
}
logDebug("No exit found")
return Position{}
}
func (level *Level) GetRoom(x, y int) int {
for i, r := range level.Rooms {
if r.Contains(x, y) {
return i
}
}
return -1
}
// Dump prints the level to the console.
func (level *Level) Dump() {
gd := CurrentGame().Data
hdr := " "
for x := 0; x < gd.Cols; x++ {
hdr += strconv.Itoa(x % 10)
}
logDebug(hdr)
for y := 0; y < gd.Rows; y++ {
data := strconv.Itoa(y%10) + " "
for x := 0; x < gd.Cols; x++ {
idx := level.GetIndexFromXY(x, y)
tile := level.Tiles[idx]
switch tile.TileType {
case WALL:
data += "#"
case ENTRANCE, EXIT:
data += "E"
default:
rm := level.GetRoom(x, y)
if rm == -1 {
data += "."
} else {
data += strconv.Itoa(rm % 10)
}
}
}
logDebug(data)
}
for i, r := range level.Rooms {
logDebug("Room: " + strconv.Itoa(i) + ": " + strconv.Itoa(r.X1) + "," + strconv.Itoa(r.Y1) + " " + strconv.Itoa(r.X2) + "," + strconv.Itoa(r.Y2))
}
}