我的代码间歇性地工作,但有时会让我超出范围错误,我无法弄清楚原因,我已将其缩小为一个函数:
void corridorFill()
{
int dir = 0;
//Set initial coords
int rand = Random.Range(0,freeCol.Count);
List<int> freeCorRow = new List<int>();
List<int> freeCorCol = new List<int>();
row = freeRow[rand];
col = freeCol[rand];
int fill = 0;
while(fill < area)
{
//Pick a random direction and go that way
//0 = north, 1 = east, 2 = south, 3 = west
dir = Random.Range(0,4);
if(directionIsSafe(dir, row, col, (int)room.Unreserved, roomType)
&& directionIsSafe(dir, row, col, (int)connect.Empty, connections))
{
//move in direction
moveDirection(dir);
freeCorRow.Add(row);
freeCorCol.Add(col);
if(fill > 0)
{
//place exit to previous tile
addExit(row, col, (dir+2)%4);
//change exits of previous room to connect
addExit(freeCorRow[freeCorRow.Count-2], freeCorCol[freeCorCol.Count-2], dir);
}
fill++;
}
else
{
bool set = false;
while(!set)
{
//direction is not safe therefore start again somewhere else, attached to what we already have
int r = Random.Range(0,freeCorRow.Count);
//check if a tile beside a known tile is free
dir = Random.Range(0,4);
//if the direction is safe, go that way
if(directionIsSafe(dir, freeCorRow[r], freeCorCol[r], (int)room.Unreserved, roomType)
&& directionIsSafe(dir, freeCorRow[r], freeCorCol[r], (int)connect.Empty, connections))
{
addExit(freeCorRow[r], freeCorCol[r], dir);
row = freeCorRow[r];
col = freeCorCol[r];
moveDirection(dir); //move in direction
addExit(row, col, (dir+2)%4); //place exit to previous tile
freeCorRow.Add(row);
freeCorCol.Add(col);
set = true;
}
}
fill++;
}
}
}
使用addExit函数,虽然我不认为问题在这里:
//check previous tile corridor configuration and change to match current
void addExit(int row, int col, int dir)
{
//Add northourn exit to room
if(dir == 0)
{
if(connections[row,col] == (int)connect.Empty)
{
connections[row,col] = (int)connect.N;
}
else if(connections[row,col] == (int)connect.E)
{
connections[row,col] = (int)connect.NE;
}
else if(connections[row,col] == (int)connect.S)
{
connections[row,col] = (int)connect.NS;
}
else if(connections[row,col] == (int)connect.W)
{
connections[row,col] = (int)connect.NW;
}
else if(connections[row,col] == (int)connect.SE)
{
connections[row,col] = (int)connect.NES;
}
else if(connections[row,col] == (int)connect.SW)
{
connections[row,col] = (int)connect.SWN;
}
else if(connections[row,col] == (int)connect.EW)
{
connections[row,col] = (int)connect.WNE;
}
else if(connections[row,col] == (int)connect.ESW)
{
connections[row,col] = (int)connect.NESW;
}
}
//Add eastern exit to room
if(dir == 1)
{
if(connections[row,col] == (int)connect.Empty)
{
connections[row,col] = (int)connect.E;
}
else if(connections[row,col] == (int)connect.N)
{
connections[row,col] = (int)connect.NE;
}
else if(connections[row,col] == (int)connect.S)
{
connections[row,col] = (int)connect.SE;
}
else if(connections[row,col] == (int)connect.W)
{
connections[row,col] = (int)connect.EW;
}
else if(connections[row,col] == (int)connect.NW)
{
connections[row,col] = (int)connect.WNE;
}
else if(connections[row,col] == (int)connect.SW)
{
connections[row,col] = (int)connect.ESW;
}
else if(connections[row,col] == (int)connect.NS)
{
connections[row,col] = (int)connect.NES;
}
else if(connections[row,col] == (int)connect.SWN)
{
connections[row,col] = (int)connect.NESW;
}
}
//Add southourn exit to room
if(dir == 2)
{
if(connections[row,col] == (int)connect.Empty)
{
connections[row,col] = (int)connect.S;
}
else if(connections[row,col] == (int)connect.N)
{
connections[row,col] = (int)connect.NS;
}
else if(connections[row,col] == (int)connect.E)
{
connections[row,col] = (int)connect.SE;
}
else if(connections[row,col] == (int)connect.W)
{
connections[row,col] = (int)connect.SW;
}
else if(connections[row,col] == (int)connect.NE)
{
connections[row,col] = (int)connect.NES;
}
else if(connections[row,col] == (int)connect.NW)
{
connections[row,col] = (int)connect.SWN;
}
else if(connections[row,col] == (int)connect.EW)
{
connections[row,col] = (int)connect.ESW;
}
else if(connections[row,col] == (int)connect.WNE)
{
connections[row,col] = (int)connect.NESW;
}
}
//Add western exit to room
if(dir == 3)
{
if(connections[row,col] == (int)connect.Empty)
{
connections[row,col] = (int)connect.W;
}
else if(connections[row,col] == (int)connect.N)
{
connections[row,col] = (int)connect.NW;
}
else if(connections[row,col] == (int)connect.E)
{
connections[row,col] = (int)connect.EW;
}
else if(connections[row,col] == (int)connect.S)
{
connections[row,col] = (int)connect.SW;
}
else if(connections[row,col] == (int)connect.NE)
{
connections[row,col] = (int)connect.WNE;
}
else if(connections[row,col] == (int)connect.SE)
{
connections[row,col] = (int)connect.ESW;
}
else if(connections[row,col] == (int)connect.NS)
{
connections[row,col] = (int)connect.SWN;
}
else if(connections[row,col] == (int)connect.NES)
{
connections[row,col] = (int)connect.NESW;
}
}
}
和directionIsSafe函数:
bool directionIsSafe(int dir, int row, int col, int roomname, int[,] checkType)
{
if(dir == 0 && col+1 < stationHeight)
{
if(checkType[row, col+1] == roomname)
{
return true;
}
else
{
return false;
}
}
else if(dir == 1 && row+1 < stationWidth)
{
if(checkType[row+1,col] == roomname)
{
return true;
}
else
{
return false;
}
}
else if(dir == 2 && col > 0)
{
if(checkType[row,col-1] == roomname)
{
return true;
}
else
{
return false;
}
}
else if(dir == 3 && row > 0)
{
if(checkType[row-1,col] == roomname)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
通过代码阅读它似乎应该有效,并且它有时间,但不是所有时间。我无法弄清楚为什么有时它不起作用。欣赏你可能解决问题的任何亮点
EDT
freeCol和freeRow使用:
创建 if(genType == (int)shapes.cross)
{
//for odd numbers
if(stationWidth%2 == 1)
{
for(row = 0; row < stationWidth; row++)
{
for(col = 0; col < stationHeight; col++)
{
if((row <= stationWidth/2 + (crossArmSize/2.0f + 0.5f) - 1 && row >= stationWidth/2 - (crossArmSize/2.0f + 0.5f) + 1)
|| (col <= stationHeight/2 + (crossArmSize/2.0f + 0.5f) - 1 && col >= stationHeight/2 - (crossArmSize/2.0f + 0.5f) + 1))
{
roomType[row,col] = (int)room.Unreserved;
freeRow.Add(row);
freeCol.Add(col);
}
}
}
}
//for even numbers
else if(stationWidth%2 == 0)
{
for(row = 0; row < stationWidth; row++)
{
for(col = 0; col < stationHeight; col++)
{
if((row < stationWidth/2 + crossArmSize/2 && row >= stationWidth/2 - crossArmSize/2)
|| (col < stationWidth/2 + crossArmSize/2 && col >= stationWidth/2 - crossArmSize/2))
{
roomType[row,col] = (int)room.Unreserved;
freeRow.Add(row);
freeCol.Add(col);
}
}
}
}
corridorFill();
}
答案 0 :(得分:2)
我认为Random.Range是包容性的。试试int rand = Random.Range(0,freeCol.Count - 1);
答案 1 :(得分:0)
在最后一个函数中是否交换了宽度和高度? 通常,行沿着宽度沿着高度和列行进。
答案 2 :(得分:0)
好的,所以我改变了:
[index of the element with max k property value, the value of that k property]
为:
row = freeRow[rand];
col = freeCol[rand];
现在每次都有效。我只是无法弄清楚为什么前者会抛出奇怪的结果而只是显然不能正常工作,如果你能说清楚它,我很乐意听到它!