November 10, 2013 - Dungeon Blocks

First, you’ll recall the texture assignment code I showed before for dungeons when explaining how random numbers were involved. In that function there were calls to srand() and rand(), which were valid, and a call to RandByte(). I hadn’t fully fleshed out this function, thinking I’d revisit the random number generation stuff later. Well it turns out its both simpler and more complex then I originally thought.

Posted by on

First, you’ll recall the texture assignment code I showed before for dungeons when explaining how random numbers were involved. In that function there were calls to srand() and rand(), which were valid, and a call to RandByte(). I hadn’t fully fleshed out this function, thinking I’d revisit the random number generation stuff later. Well it turns out its both simpler and more complex then I originally thought.

It turns out that function is specific to dungeon textures, so I now call it DungeonTex_RandByte(). But basically another section of code needs to be called first to generate a “key,” which is derived from the map coordinates which happens during cell loading or save game loading depending on whether you’re loading from a save game, by starting a new game or by clicking on a door. It is generated like this:

dungeonLocTexKey = GetDungeonTexKey( mapX, mapZ, dungeonKeyTable )

In this case mapX and mapZ are not the exact map coordinates but offset slightly from normal:

code:
mapX = worldX/32768 + 2
mapZ = 499 - (worldZ/32768)

if ( mapZ < 1 )
mapZ = 1;
else if (mapZ > 499 )
mapZ = 499;

Here is the code to build the texture key for the current location. I don’t show the key table because it is pretty big – 500 entries.

code:
struct TexKey_XOffsetEntry
{
word xOffset;
byte key;
};

byte GetDungeonTexKey(int mapX, int mapZ, TexKey_XOffsetEntry *keyTable)
{
TexKey_XOffsetEntry *keyEntry = &amp;keyTable[mapZ];
mapX -= keyEntry->xOffset;
while (mapX > 0)
{
keyEntry++;
mapX -= keyEntry->xOffset;
};
return keyEntry->key;
}

Then the key is used DungeonTex_RandByte, as follows:

byte DungeonTex_RandByte()
{
return dungeonTexTable[ dungeonLocTexKey ];
}

Which is used to actually generate the texture table (you’ve seen this code before):

code:
word textureTableSrc[5]=;   //0x28617C
{ 119, 120, 122, 123, 124 };
word textureTableCur[5];   //0x286186

void InitializeTextureTable()
{
int r0 = rand();
srand( curLocationRec->locationID>>16 );
byte r = DungeonTex_RandByte();
int r2 = _texRandTable[ r ];

memcpy(textureTableCur, textureTableSrc, 10);
for (int i=0; i<5; i++)
{
byte n = random_range(0, 4);
if ( n == 2 )
{
n += 2;
}
n += textureTableSrc[r2];
textureTableCur[i]= n;
}

srand(r0);
}

… the process looks convoluted and well… it is.

## Dungeon Blocks

As you already know dungeons are loaded as “blocks” – each of which is an RDB and RDI file that identifies all the objects, models, quest locations, monsters, loot piles and so on for the dungeon block. For the purposes of culling and rendering, each block is sub-divided into 2×2 sub-blocks. RDB files contain a grid of object root offsets – each of which maps to one of these 2×2 sub-blocks. As it turns out, the game always uses 2×2 sub-blocks, even though RDB files can (and usually do) specify more. Even though these blocks must be referenced based on grid width/height, only the upper left 2×2 block is actually used (the rest of the data is -1).

Daggerfall creates a sub-block game object for each sub-block, which is used as the parent object for the sub-block objects. For rendering, the game loops through each visible sub-block, then traverses the link list of child objects to render, collide with, etc.. Links between objects, for things like switches, actually occur within these sub-blocks, though the height difference can be immense, and the distance can seem quite large due to the windy nature of the corridors.

Each dungeon block is 2048 x ? x 2048 game units, each sub-block being 1024 x ? x 1024 units. Note that the height value is a question mark since there aren’t any hard-coded limits, as far as I’ve seen so far.

## Game Objects

When Daggerfall starts up, a 3584000 byte (almost 3.42MB) object pool is allocated. Each object is 71 bytes is size, with extra data that can be allocated based on type. In addition 18 bytes of addition overhead is used for things like previous and next pointers.

Each game object in Daggerfall – things ranging from 3D objects, lights, flats, sub-block objects, the player, NPCs, enemies, etc. – use this pool, with the type specific data being allocated after the 71 bytes. This data contains information such as the world position (in “units” – basically inches), flags, unique identifier, link list data, type and so forth.

So, while the code is pure C, this type of structure allows for some object oriented style behavior – you can think of the Object as being the base class. And they are actually called objects in the original code, as evidenced by the following error message if no object can be found:

if ( !obj ) { printf( "Unable to allocate OBJECT memory." ); } Anyway you can use the type parameter to determine which structure to cast the type-specific data to, at offset 71.

I’m hoping to be able to show screenshots soon, fortunately by coding support for save games now I can make sure that all locations of a given type work – so my first dungeon shots will not be of Privateer’s Hold.