Translate
EnglishFrenchGermanItalianPortugueseRussianSpanish

The XL Engine is and will remain free, donations are purely optional but greatly appreciated.

Unfortunately time was tight this past week, reducing the number of updates I could write and the amount of work being done. I have loaded the character data, faction data, setup the current region and loaded the location data for my save game.

For the moment I’m working on getting dungeons fully loaded from the save games, which has had me revisiting the dungeon loading code again. It turns out this process has allowed me to clean up the code a bit, with better understanding gained by approaching the code from another angle.

So I’ll describe a bit of the way Daggerfall interprets dungeon data, which is a little different then DaggerXL did before this point. I’ll also explain a little about how objects are allocated and used.

 

Textures Revisited

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:

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.

struct TexKey_XOffsetEntry
{
   word xOffset;
   byte key;
};

byte GetDungeonTexKey(int mapX, int mapZ, TexKey_XOffsetEntry *keyTable)
{
   TexKey_XOffsetEntry *keyEntry = &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):

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. :D

12 Responses to “Dungeon Blocks”

  • Michel Renier:

    What happens, when the object pool overflows?

    • luciusDXL:

      After writing out the error message the code continues through the allocation function. So, most likely, either a crash or memory corruption since it dereferences the NULL object and writes values without any additional checks. Fortunately they allocation enough memory for about 30k objects and are good are removing unused objects, so this doesn’t seem too likely. However it could be one of the causes for “random crashes” that people often exprience with Daggerfall.

  • John Doe:

    This thing has me really excited! I hope it comes out on mac soonish(if it ever will anyhoo)
    One question though. What language are you using to program this?

  • Louix:

    The FrenchDaggerfall will be compatible with DaggerXl? Or are you going to do a translate?

    • luciusDXL:

      DaggerXL uses the original data so any changes to that data should work. I won’t be translating the strings in the executable but I will be separating them into a text file so they are easier to translate.

  • Nicholas:

    The website that is linked for OpenAL Installers isn’t up anymore. Just wanted to let you know.

    • luciusDXL:

      Apparently openal.org no longer exists – which suggests that Creative no longer plans on supporting OpenAL so I will be switching to “OpenAL Soft” instead. In the interim I will post a temporary solution soon, even if that is hosting the redistributable files on this site temporarily.

  • Fabio Bittar:

    So, how’s it going?

  • Anon:

    BTW, are you going to re-film the intro sequence?

  • DE_BattleMage:

    Just wanted to drop by and say that I am really excited for this project and I drop in every now and then to see if there have been any updates. Every time I see a recent blog post, it warms my heart. Keep on chuggin.

Leave a Reply

The XL Engine is and will remain free, donations are purely optional but greatly appreciated.