我正在嘗試實現一個地牢生成算法(presented here和demo-ed here),該算法涉及生成相互重疊的隨機數量的單元格。然後將細胞推開/分開然後連接。現在,原始的海報/作者描述了他正在使用分離導向算法來將細胞均勻分佈在一個區域上。我沒有太多的植絨算法和/或分離轉向行爲的經驗,因此我轉向谷歌的解釋(並發現this)。我的實現(基於上次文章提到的)如下:Lua分離轉向算法將重疊房間分組到一個角落
function pdg:_computeSeparation(_agent)
local neighbours = 0
local rtWidth = #self._rooms
local v =
{
x = self._rooms[_agent].startX,
y = self._rooms[_agent].startY,
--velocity = 1,
}
for i = 1, rtWidth do
if _agent ~= i then
local distance = math.dist(self._rooms[_agent].startX,
self._rooms[_agent].startY,
self._rooms[i].startX,
self._rooms[i].startY)
if distance < 12 then
--print("Separating agent: ".._agent.." from agent: "..i.."")
v.x = (v.x + self._rooms[_agent].startX - self._rooms[i].startX) * distance
v.y = (v.y + self._rooms[_agent].startY - self._rooms[i].startY) * distance
neighbours = neighbours + 1
end
end
end
if neighbours == 0 then
return v
else
v.x = v.x/neighbours
v.y = v.y/neighbours
v.x = v.x * -1
v.y = v.y * -1
pdg:_normalize(v, 1)
return v
end
end
self._rooms是包含原始X和電網間的Y位置,與它一起表格的寬度和高度(endX ,endY)。
的問題是,代替tiddly佈置細胞在網格上,它需要的重疊細胞,並將它們移動到從1,1進入的區域爲距離+ 2,距離+ 2(as seen in my video [youtube])
我想了解爲什麼會發生這種情況。
如果它的必要,在這裏我分析的網格表,獨立和分離後填充單元格:
function pdg:separate()
if #self._rooms > 0 then
--print("NR ROOMS: "..#self._rooms.."")
-- reset the map to empty
for x = 1, self._pdgMapWidth do
for y = 1, self._pdgMapHeight do
self._pdgMap[x][y] = 4
end
end
-- now, we separate the rooms
local numRooms = #self._rooms
for i = 1, numRooms do
local v = pdg:_computeSeparation(i)
--we adjust the x and y positions of the items in the room table
self._rooms[i].startX = v.x
self._rooms[i].startY = v.y
--self._rooms[i].endX = v.x + self._rooms[i].endX
--self._rooms[i].endY = v.y + self._rooms[i].endY
end
-- we render them again
for i = 1, numRooms do
local px = math.abs(math.floor(self._rooms[i].startX))
local py = math.abs(math.floor(self._rooms[i].startY))
for k = self.rectMinWidth, self._rooms[i].endX do
for v = self.rectMinHeight, self._rooms[i].endY do
print("PX IS AT: "..px.." and k is: "..k.." and their sum is: "..px+k.."")
print("PY IS AT: "..py.." and v is: "..v.." and their sum is: "..py+v.."")
if k == self.rectMinWidth or
v == self.rectMinHeight or
k == self._rooms[i].endX or
v == self._rooms[i].endY then
self._pdgMap[px+k][py+v] = 1
else
self._pdgMap[px+k][py+v] = 2
end
end
end
end
end