|
1
|
+-- TE4 - T-Engine 4
|
|
2
|
+-- Copyright (C) 2009, 2010, 2011 Nicolas Casalini
|
|
3
|
+--
|
|
4
|
+-- This program is free software: you can redistribute it and/or modify
|
|
5
|
+-- it under the terms of the GNU General Public License as published by
|
|
6
|
+-- the Free Software Foundation, either version 3 of the License, or
|
|
7
|
+-- (at your option) any later version.
|
|
8
|
+--
|
|
9
|
+-- This program is distributed in the hope that it will be useful,
|
|
10
|
+-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+-- GNU General Public License for more details.
|
|
13
|
+--
|
|
14
|
+-- You should have received a copy of the GNU General Public License
|
|
15
|
+-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
16
|
+--
|
|
17
|
+-- Nicolas Casalini "DarkGod"
|
|
18
|
+-- darkgod@te4.org
|
|
19
|
+
|
|
20
|
+require "engine.class"
|
|
21
|
+local Map = require "engine.Map"
|
|
22
|
+local BSP = require "engine.BSP"
|
|
23
|
+require "engine.Generator"
|
|
24
|
+local RoomsLoader = require "engine.generator.map.RoomsLoader"
|
|
25
|
+module(..., package.seeall, class.inherit(engine.Generator, RoomsLoader))
|
|
26
|
+
|
|
27
|
+function _M:init(zone, map, level, data)
|
|
28
|
+ engine.Generator.init(self, zone, map, level)
|
|
29
|
+ self.data = data
|
|
30
|
+ self.grid_list = self.zone.grid_list
|
|
31
|
+ self.max_block_w = data.max_block_w or 20
|
|
32
|
+ self.max_block_h = data.max_block_h or 20
|
|
33
|
+ self.max_building_w = data.max_building_w or 7
|
|
34
|
+ self.max_building_h = data.max_building_h or 7
|
|
35
|
+
|
|
36
|
+ RoomsLoader.init(self, data)
|
|
37
|
+end
|
|
38
|
+
|
|
39
|
+function _M:building(leaf, spots)
|
|
40
|
+-- local x1, x2 = leaf.rx + rng.range(2, math.max(2, math.floor(leaf.w / 2 - 3))), leaf.rx + leaf.w - rng.range(2, math.max(2, math.floor(leaf.w / 2 - 3)))
|
|
41
|
+-- local y1, y2 = leaf.ry + rng.range(2, math.max(2, math.floor(leaf.h / 2 - 3))), leaf.ry + leaf.h - rng.range(2, math.max(2, math.floor(leaf.h / 2 - 3)))
|
|
42
|
+ local x1, x2 = leaf.rx, leaf.rx + leaf.w
|
|
43
|
+ local y1, y2 = leaf.ry, leaf.ry + leaf.h
|
|
44
|
+ local ix1, ix2, iy1, iy2 = x1 + 2, x2 - 1, y1 + 2, y2 - 1
|
|
45
|
+ local inner_grids = {}
|
|
46
|
+ local door_grids = {}
|
|
47
|
+ local is_lit = rng.percent(self.data.lite_room_chance or 70)
|
|
48
|
+
|
|
49
|
+ for i = leaf.rx, leaf.rx + leaf.w do for j = leaf.ry, leaf.ry + leaf.h do
|
|
50
|
+ -- Abort if there is something already
|
|
51
|
+ if self.map:isBound(i, j) and self.map.room_map[i][j].room then return end
|
|
52
|
+ end end
|
|
53
|
+
|
|
54
|
+ for i = x1, x2 do for j = y1, y2 do
|
|
55
|
+ if i == x1 or i == x2 or j == y1 or j == y2 then
|
|
56
|
+ if not self.map.room_map[i][j].walled then self.map(i, j, Map.TERRAIN, self:resolve("wall")) end
|
|
57
|
+ self.map.room_map[i][j].walled = true
|
|
58
|
+ if not (i == x1 and j == y1) and not (i == x1 and j == y2) and not (i == x2 and j == y1) and not (i == x2 and j == y2) then
|
|
59
|
+ door_grids[#door_grids+1] = {x=i,y=j}
|
|
60
|
+ end
|
|
61
|
+ else
|
|
62
|
+ self.map(i, j, Map.TERRAIN, self:resolve("floor"))
|
|
63
|
+ if is_lit then self.map.lites(i, j, true) end
|
|
64
|
+ if i >= ix1 and i <= ix2 and j >= iy1 and j <= iy2 then
|
|
65
|
+ inner_grids[#inner_grids+1] = {x=i,y=j}
|
|
66
|
+ end
|
|
67
|
+ end
|
|
68
|
+ end end
|
|
69
|
+
|
|
70
|
+ -- Door
|
|
71
|
+ local door = rng.table(door_grids)
|
|
72
|
+ self.map(door.x, door.y, Map.TERRAIN, self:resolve("door"))
|
|
73
|
+ -- Eliminate inner grids that face the door
|
|
74
|
+ for i = #inner_grids, 1, -1 do
|
|
75
|
+ local g = inner_grids[i]
|
|
76
|
+ if g.x == door.x or g.y == door.y then table.remove(inner_grids, i) end
|
|
77
|
+ end
|
|
78
|
+
|
|
79
|
+ spots[#spots+1] = {x=math.floor((x1+x2)/2), y=math.floor((y1+y2)/2), type="building", subtype="building"}
|
|
80
|
+end
|
|
81
|
+
|
|
82
|
+function _M:block(leaf, spots)
|
|
83
|
+ local x1, x2 = leaf.rx, leaf.rx + leaf.w
|
|
84
|
+ local y1, y2 = leaf.ry, leaf.ry + leaf.h
|
|
85
|
+ local ix1, ix2, iy1, iy2 = x1 + 2, x2 - 1, y1 + 2, y2 - 1
|
|
86
|
+ local inner_grids = {}
|
|
87
|
+ local door_grids = {}
|
|
88
|
+
|
|
89
|
+ for i = leaf.rx, leaf.rx + leaf.w do for j = leaf.ry, leaf.ry + leaf.h do
|
|
90
|
+ -- Abort if there is something already
|
|
91
|
+ if self.map:isBound(i, j) and self.map.room_map[i][j].room then return end
|
|
92
|
+ end end
|
|
93
|
+
|
|
94
|
+ local door_grids = {}
|
|
95
|
+ for i = x1, x2 do for j = y1, y2 do
|
|
96
|
+ if i == x1 or i == x2 or j == y1 or j == y2 then
|
|
97
|
+ self.map(i, j, Map.TERRAIN, self:resolve("floor"))
|
|
98
|
+ elseif (i == x1+1 or i == x2-1 or j == y1+1 or j == y2-1) and
|
|
99
|
+ not (i == x1+1 and j == y1+1) and not (i == x1+1 and j == y2-1) and not (i == x2-1 and j == y1+1) and not (i == x2-1 and j == y2-1) then
|
|
100
|
+ door_grids[#door_grids+1] = {x=i,y=j}
|
|
101
|
+ end
|
|
102
|
+ end end
|
|
103
|
+
|
|
104
|
+ -- Door
|
|
105
|
+ local door = rng.table(door_grids)
|
|
106
|
+ self.map(door.x, door.y, Map.TERRAIN, self:resolve("door"))
|
|
107
|
+
|
|
108
|
+ local bsp = BSP.new(leaf.w-2, leaf.h-2, self.max_building_w, self.max_building_h)
|
|
109
|
+ bsp:partition()
|
|
110
|
+
|
|
111
|
+ print("Building gen made ", #bsp.leafs, "building BSP leafs")
|
|
112
|
+ for z, sleaf in ipairs(bsp.leafs) do
|
|
113
|
+ sleaf.rx = sleaf.rx + leaf.rx + 1
|
|
114
|
+ sleaf.ry = sleaf.ry + leaf.ry + 1
|
|
115
|
+ self:building(sleaf, spots)
|
|
116
|
+ end
|
|
117
|
+end
|
|
118
|
+
|
|
119
|
+function _M:generate(lev, old_lev)
|
|
120
|
+ for i = 0, self.map.w - 1 do for j = 0, self.map.h - 1 do
|
|
121
|
+ self.map(i, j, Map.TERRAIN, self:resolve("wall"))
|
|
122
|
+ end end
|
|
123
|
+
|
|
124
|
+ local spots = {}
|
|
125
|
+ self.spots = spots
|
|
126
|
+
|
|
127
|
+ local bsp = BSP.new(self.map.w, self.map.h, self.max_block_w, self.max_block_h)
|
|
128
|
+ bsp:partition()
|
|
129
|
+
|
|
130
|
+ print("Building gen made ", #bsp.leafs, "blocks BSP leafs")
|
|
131
|
+ for z, leaf in ipairs(bsp.leafs) do
|
|
132
|
+ self:block(leaf, spots)
|
|
133
|
+ end
|
|
134
|
+
|
|
135
|
+ local ux, uy, dx, dy
|
|
136
|
+ if self.data.edge_entrances then
|
|
137
|
+ ux, uy, dx, dy, spots = self:makeStairsSides(lev, old_lev, self.data.edge_entrances, spots)
|
|
138
|
+ else
|
|
139
|
+ ux, uy, dx, dy, spots = self:makeStairsInside(lev, old_lev, spots)
|
|
140
|
+ end
|
|
141
|
+
|
|
142
|
+ return ux, uy, dx, dy, spots
|
|
143
|
+end
|
|
144
|
+
|
|
145
|
+--- Create the stairs inside the level
|
|
146
|
+function _M:makeStairsInside(lev, old_lev, spots)
|
|
147
|
+ -- Put down stairs
|
|
148
|
+ local dx, dy
|
|
149
|
+ if lev < self.zone.max_level or self.data.force_last_stair then
|
|
150
|
+ while true do
|
|
151
|
+ dx, dy = rng.range(1, self.map.w - 1), rng.range(1, self.map.h - 1)
|
|
152
|
+ if not self.map:checkEntity(dx, dy, Map.TERRAIN, "block_move") and not self.map.room_map[dx][dy].special then
|
|
153
|
+ self.map(dx, dy, Map.TERRAIN, self:resolve("down"))
|
|
154
|
+ self.map.room_map[dx][dy].special = "exit"
|
|
155
|
+ break
|
|
156
|
+ end
|
|
157
|
+ end
|
|
158
|
+ end
|
|
159
|
+
|
|
160
|
+ -- Put up stairs
|
|
161
|
+ local ux, uy
|
|
162
|
+ while true do
|
|
163
|
+ ux, uy = rng.range(1, self.map.w - 1), rng.range(1, self.map.h - 1)
|
|
164
|
+ if not self.map:checkEntity(ux, uy, Map.TERRAIN, "block_move") and not self.map.room_map[ux][uy].special then
|
|
165
|
+ self.map(ux, uy, Map.TERRAIN, self:resolve("up"))
|
|
166
|
+ self.map.room_map[ux][uy].special = "exit"
|
|
167
|
+ break
|
|
168
|
+ end
|
|
169
|
+ end
|
|
170
|
+
|
|
171
|
+ return ux, uy, dx, dy, spots
|
|
172
|
+end
|
|
173
|
+
|
|
174
|
+--- Create the stairs on the sides
|
|
175
|
+function _M:makeStairsSides(lev, old_lev, sides, spots)
|
|
176
|
+ -- Put down stairs
|
|
177
|
+ local dx, dy
|
|
178
|
+ if lev < self.zone.max_level or self.data.force_last_stair then
|
|
179
|
+ while true do
|
|
180
|
+ if sides[2] == 4 then dx, dy = 0, rng.range(0, self.map.h - 1)
|
|
181
|
+ elseif sides[2] == 6 then dx, dy = self.map.w - 1, rng.range(0, self.map.h - 1)
|
|
182
|
+ elseif sides[2] == 8 then dx, dy = rng.range(0, self.map.w - 1), 0
|
|
183
|
+ elseif sides[2] == 2 then dx, dy = rng.range(0, self.map.w - 1), self.map.h - 1
|
|
184
|
+ end
|
|
185
|
+
|
|
186
|
+ if not self.map.room_map[dx][dy].special then
|
|
187
|
+ self.map(dx, dy, Map.TERRAIN, self:resolve("down"))
|
|
188
|
+ self.map.room_map[dx][dy].special = "exit"
|
|
189
|
+ break
|
|
190
|
+ end
|
|
191
|
+ end
|
|
192
|
+ end
|
|
193
|
+
|
|
194
|
+ -- Put up stairs
|
|
195
|
+ local ux, uy
|
|
196
|
+ while true do
|
|
197
|
+ if sides[1] == 4 then ux, uy = 0, rng.range(0, self.map.h - 1)
|
|
198
|
+ elseif sides[1] == 6 then ux, uy = self.map.w - 1, rng.range(0, self.map.h - 1)
|
|
199
|
+ elseif sides[1] == 8 then ux, uy = rng.range(0, self.map.w - 1), 0
|
|
200
|
+ elseif sides[1] == 2 then ux, uy = rng.range(0, self.map.w - 1), self.map.h - 1
|
|
201
|
+ end
|
|
202
|
+
|
|
203
|
+ if not self.map.room_map[ux][uy].special then
|
|
204
|
+ self.map(ux, uy, Map.TERRAIN, self:resolve("up"))
|
|
205
|
+ self.map.room_map[ux][uy].special = "exit"
|
|
206
|
+ break
|
|
207
|
+ end
|
|
208
|
+ end
|
|
209
|
+
|
|
210
|
+ return ux, uy, dx, dy, spots
|
|
211
|
+end |
...
|
...
|
|