-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworldgen.awk
More file actions
149 lines (121 loc) · 4.49 KB
/
worldgen.awk
File metadata and controls
149 lines (121 loc) · 4.49 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
function create_world(terrain, width, height, fill_tile, x, y) {
terrain["width"] = width
terrain["height"] = height
for(x=0; x < width; x++) {
for(y=0; y < height; y++) {
terrain[x][y] = TILE_DATA[fill_tile]["char"]
}
}
}
function generate_random_walk_cave(terrain, width, height, x, y, length_of_walk, i, t) {
create_world(terrain, width, height, "wall")
terrain[x][y] = TILE_DATA["ground"]["char"]
for (i=0; i<length_of_walk; i++) {
t = randint(1,4)
if (t == 1 && (x+1 < width-2)) {
x += 1
} else if (t == 2 && (x-1) > 1) {
x -= 1
} else if (t == 3 && (y+1) < (height-2)) {
y += 1
} else if (t == 4 && (y-1) > 1) {
y -= 1
}
terrain[x][y] = TILE_DATA["ground"]["char"]
}
}
function generate_border(terrain, width, height) {
for(y=0;y<height;y++) {
if (y == 0 || y == height-1) {
for (x=0;x<width;x++) {
terrain[x][y] = TILE_DATA["wall"]["char"]
}
} else {
terrain[0][y] = TILE_DATA["wall"]["char"]
terrain[width-1][y] = TILE_DATA["wall"]["char"]
}
}
}
function create_tunnel(terrain, direction, p1, p2, p) {
for(i=min(p1,p2); i < max(p1, p2)+1; i++) {
if (direction == "horizontal") {
terrain[i][p] = terrain[x][y] = TILE_DATA["ground"]["char"]
} else if (direction == "vertical") {
terrain[p][i] = terrain[x][y] = TILE_DATA["ground"]["char"]
}
}
}
function create_room(terrain, x_pos, y_pos, width, height, x, y) {
for (x = x_pos+1; x < x_pos + width; x++) {
for (y = y_pos+1; y < y_pos + height; y++) {
terrain[x][y] = TILE_DATA["ground"]["char"]
}
}
}
function generate_dungeon(terrain, width, height, max_rooms, room_min_size, room_max_size, i, x, y, w, h, rooms, num_rooms, center_x, center_y, collision, x2, y2, w2, h2, prev_x, prev_y) {
create_world(terrain, width, height, "wall")
num_rooms = 0
for (r=0; r < max_rooms; r++) {
w = randint(room_min_size, room_max_size)
h = randint(room_min_size, room_max_size)
x = randint(0, width - w - 1)
y = randint(0, height - h - 1)
collision = 0
for (i=0;i<num_rooms;i++) {
x2 = rooms[i]["x"]
y2 = rooms[i]["y"]
w2 = rooms[i]["width"]
h2 = rooms[i]["height"]
# Collision
if (x <= (x2+w2) && (x+w) >= x2 && y <= y2+h2 && y+h >= y2) {
collision = 1
break
}
}
if (!collision) {
create_room(terrain, x, y, w, h)
center_x = int((x+ (x+w)) /2 )
center_y = int((y+ (y+h)) /2 )
if (num_rooms == 0) {
ENTITIES[0]["x"] = center_x
ENTITIES[0]["y"] = center_y
} else {
prev_x = rooms[num_rooms-1]["center_x"]
prev_y = rooms[num_rooms-1]["center_y"]
if (randint(0, 1)) {
create_tunnel(terrain, "horizontal", prev_x, center_x, prev_y)
create_tunnel(terrain, "vertical", prev_y, center_y, center_x)
} else {
create_tunnel(terrain, "vertical", prev_y, center_y, prev_x)
create_tunnel(terrain, "horizontal", prev_x, center_x, center_y)
}
}
rooms[num_rooms]["center_x"] = center_x
rooms[num_rooms]["center_y"] = center_y
rooms[num_rooms]["width"] = width
rooms[num_rooms]["height"] = height
rooms[num_rooms]["x"] = x
rooms[num_rooms]["y"] = y
num_rooms += 1
}
}
# Place stairs
if (GAME_LEVEL > 0) {
terrain[ENTITIES[0]["x"]][ENTITIES[0]["y"]] = TILE_DATA["stairs_up"]["char"]
}
terrain[center_x][center_y] = TILE_DATA["stairs_down"]["char"]
}
function get_room_dimensions(x, y, room_data) {
left_ray = ray_cast("room", "LEFT", x, y)
right_ray = ray_cast("room", "RIGHT", x, y)
up_ray = ray_cast("room", "UP", x, y)
down_ray = ray_cast("room", "DOWN", x, y)
room_data["x"] = x-left_ray
room_data["y"] = y-up_ray
room_data["width"] = left_ray + right_ray + 1
room_data["height"] = up_ray + down_ray + 1
}
function is_corridor(x, y) {
return (is_blocked(x-1, y, "tile") && is_blocked(x+1, y, "tile")) ||
(is_blocked(x, y-1, "tile") && is_blocked(x, y+1, "tile"))
}