I want to place a new room I've created on the [--] grid, and I can't for the love of God figure out how the heck do I automate the process of linking the new room to the rooms below and right side. ...
We're speaking of editor functionality here. What programming language do you use to program the editor? I'm using some pseudo code in the following:
Possibility 1: 2D array
Allocate a 2D array with the size specified within the level creation dialog, say N by M cells. The array stores pointers to Room structure instances. Initially the array elements are all set to nil, denoting that no room is associated with that cell. Each cell in the grid uses 2 numbers as address, directly useable as index into the 2D array. If a new room is placed at index (n,m), then the editor checks for neighbored rooms as follows:
if (n>0 and array2d[n-1, m] != nil ) then "room to the left exists";
if (n<N-1 and array2d[n+1, m] != nil ) then "room to the right exists";
if (m>0 and array2d[n, m-1] != nil ) then "room above exists";
if (m<M-1 and array2d[n, m+1] != nil ) then "room below exists";
What "room ... exists" may mean is explained below.
Possibility 2: 1D array
Perhaps 2D array are not your friend. There is this little trick with calculates a 1D array index from a 2D index. For a grid of N by M cells, an index (n,m) can be used to calculate
i := n * M + m
i := m * N + n
for an array of size N*M, of course.
Since this just is the indexing scheme, it can be hidden up until directly accessing the array, so the usage is the same as in possibility 1.
Possibility 3: associative map
The standard library of your programming environment supports an associative container, usually called a map or sometimes also dictionary. Such a map associates a key with a value. The key is the 2D index like above, or perhaps better the 1D index as shown above, while the value is a Room structure instance. A map has as many entries as rooms actually exist (as opposed to an array which has as many entries as cells are specified, i.e. equal to the amount of possibly existing rooms).
Asking a map for the existence of a room is a bit easier since an "index out of bounds" cannot occur.
if (map(index(n-1, m)) != nil ) then "room to the left exists";
if (map(index(n+1, m)) != nil ) then "room to the right exists";
if (map(index(n, m-1)) != nil ) then "room above exists";
if (map(index(n, m+1)) != nil ) then "room below exists";
Again, what "room ... exists" may mean is explained below.
Possibility 4: sequence / vector
This possibility uses a self growing linear storage. As opposed to an array solution, there is not an entry for each cell but only for each existing room. The Room structure itself has a field that stores its own 2D or 1D index. Whenever you want to know whether a room exists, you iterate the sequence and compare the values of the stored room index fields with the given index. If such search is implemented as routine "lookup", it looks like so:
if (seq.kookup(n-1,m) != nil) then "room to the left exists";
And now ...
"room ... exists" in detail
So, when the above code enters an "room ... exists", you have to store the link correct inside the Room structure. Again, I don't know how this link should look like. You may store the pointer to the other room directly, or the 2D or 1D index. If we use "storage" for one of the possibilities shown above, that may look like so:
if (storage(n, m))
then "there is already a room in that cell"
Room* newRoom = createRoom(n, m);
Room* neighbor = nil;
// looking to the left ...
neighbor = storage(n-1, m);
if (neighbor != nil)
newRoom.leftNeighbor = neighbor;
neighbor.rightNeighbor = newRoom;
// looking to the right ...
neighbor = storage(n+1, m);
if (neighbor != nil)
newRoom.rightNeighbor = neighbor;
neighbor.leftNeighbor = newRoom;
... similarly for above and below ...
Edited by haegarr, 25 August 2014 - 04:18 AM.