Knight Online Terrain (GTD format for 1299)

I want to talk a little bit about reading from binary files using C/C++ and the file structure for the Knight Online terrain files (i.e. files with the extension “.gtd”). To follow along you need two things:

  1. A C/C++ compiler, here I’ll be using Visual C++ (Community 2015)
  2. A 1299 terrain file, I’m using “karus_start.gtd” but any .gtd will work as long as it’s from the 1299 version of the game – however I’d recommend that you also use “karus_start.gtd” so that our number match up.

The basic concepts I’ll discuss here are valid for reading any of the Knight Online content files. Also, if a particular map spans multiple versions of the game (with each version varying in the file structure) you can use the data you know is required for the 1299 version (discussed in this post) to figure out what was added/removed in other versions. Therefore, even though we’ll be discussing the 1299 file format this information is relevant for cracking all versions of the Knight Online terrain files. Here I’ll be assuming you have little to no C/C++ knowledge. However, I am assuming you know general programming concepts and the difference between memory on the stack vs. memory on the heap.

First let’s read in the .gtd file.

/*
*/

#include "stdio.h"

//-----------------------------------------------------------------------------
int main(int argc, char** argv) {

	char filename[] = "karus_start.gtd";

	FILE* fp = fopen(filename, "rb");
	if(fp == NULL) {
		printf("ERROR: Unable to open file "%s"n", filename);
		return -1;
	}

	fclose(fp);

	return 0;
}

The function int main(int argc, char** argv) is the main entry point for the program. #include “stdio.h” includes the declarations for all standard input and output operations. char filename[] = “karus_start.gtd”; defines a variable name “filename” which is a pointer to an array of “char”s allocated on the stack. FILE* fp = fopen(filename, “rb”); opens the file “karus_start.gtd” in the “read binary” mode. This means we will only be reading from (as opposed to writing to) the file and we wish to treat the contents of the file as if it were all binary data. The rest of the code here checks to make sure we successfully opened the file (printing an error if we were unable to open it) and immediately closing the file and exiting the program.

So far this program don’t do much. We are just opening the file (if it exists) and closing it. What we want to do is start reading in the contents of the file. We can’t read in the contents unless we know a little bit about how the terrain information is structured. So before we start read in all the binary information let’s take a moment to discuss the .gtd file at a higher level.

Having played the game you have probably noticed how the maps are broken up into these little squares.

Each of these rectangles is referred to as a “tile” and each tile is a 4×4 rectangle. The map data for a tile consists of four points in 3D space. In this coordinate system X and Z run along the floor and Y points up into the sky. Therefore, since we know that tiles are 4×4 rectangles, the X and Z coordinates for all the tiles on the map can be set just by knowing how big the map is (this will be important later because only the Y coordinate for these four points is stored in the .gtd file). The idea of a tile isn’t enough though, we also need the concept of a “patch.” A patch consists of a rectangle of 8×8 tiles. Patches carry absolutely no information about how the map should be rendered. Patches carry information for how the map should be updated and are useful for increasing computational efficiency. We eventually plan to have trees blowing in the wind or NPCs performing particular animations, etc. – it would be a waste of CPU power to have trees located on the opposite side of the map blowing in the wind when the player isn’t anywhere near them. Patches are useful for only updating objects which are close to the player.

Interestingly, the 1299 .gtd files start with an 4-byte integer which we do not know the function of (I’d guess it’s likely a version number).

int iIdk;
fread(&iIdk, sizeof(int), 1, fp);

An int in Visual C++ is 4 bytes. int iIdk; allocates the 4 bytes on the stack which we will use to store the first 4 bytes of the .gtd file. fread(&iIdk, sizeof(int), 1, fp); actually reads 4 bytes from the file and copies them into the 4 bytes we have allocated. Next we read in the name of the map.

int iNL;
fread(&iNL, sizeof(int), 1, fp);

char m_szName[0xFF] = "";
if(iNL > 0) fread(m_szName, sizeof(char), iNL, fp);

Here we are reading in another 4-byte integer, although this time it is the length of the map name. This integer tells us how many characters there are in the map’s name. Now we’ll read in the size of the map, set the number of patches based on the map size, and grab all the map data.

int m_ti_MapSize = 0;
fread(&m_ti_MapSize, sizeof(int), 1, fp);
int m_pat_MapSize = (m_ti_MapSize - 1) / 8;

_N3MapData* m_pMapData = new _N3MapData[m_ti_MapSize*m_ti_MapSize];
fread(m_pMapData, sizeof(_N3MapData), m_ti_MapSize*m_ti_MapSize, fp);

Based on the code we have looked at up to this point it should be relatively straight forward to understand what’s going on here. However, we haven’t talked at all about _N3MapData , let’s take a look.

struct _N3MapData {
	float fHeight;
	unsigned int bIsTileFull : 1;
	unsigned int Tex1Dir : 5;
	unsigned int Tex2Dir : 5;
	unsigned int Tex1Idx : 10;
	unsigned int Tex2Idx : 10;

	_N3MapData(void) {
		bIsTileFull = true;
		fHeight = FLT_MIN;
		Tex1Idx = 1023;
		Tex1Dir = 0;
		Tex2Idx = 1023;
		Tex2Dir = 0;
	}
};

Each of the four points making up a tile has one of these map data structs. fHeight is the Y coordinate for a point (remember we can get the X and Z coordinate based on how big the map is). bIsTileFull is less straight forward but I believe it gets used when setting which textures to use for a particular tile (here I’m taking “full” to mean “stuff is covering this tile”). I would be interested to hear possible alternative theories about this. The rest of the variables are used for deciding which texture to blend together (Tex1Idx and Tex2Idx) and how they should be oriented (Tex1Dir and Tex2Dir).

Next we read in the patch information.

float** m_ppPatchMiddleY = new float*[m_pat_MapSize];
float** m_ppPatchRadius = new float*[m_pat_MapSize];

for (int x = 0; x<m_pat_MapSize; x++) {
	m_ppPatchMiddleY[x] = new float[m_pat_MapSize];
	m_ppPatchRadius[x] = new float[m_pat_MapSize];

	for (int z = 0; z<m_pat_MapSize; z++) {
		fread(&(m_ppPatchMiddleY[x][z]), sizeof(float), 1, fpMap);
		fread(&(m_ppPatchRadius[x][z]), sizeof(float), 1, fpMap);
	}
}

This simply gives us how high each 8×8 patch is and the radius of its bounding sphere. Next we’ll read in the grass information.

unsigned char* m_pGrassAttr = new unsigned char[m_ti_MapSize*m_ti_MapSize];
fread(m_pGrassAttr, sizeof(unsigned char), m_ti_MapSize*m_ti_MapSize, fp);

char* m_pGrassFileName = new char[260];
fread(m_pGrassFileName, sizeof(char), 260, fp);

This information is used for rendering the grass which pops up out of the tile. Then we read in information about the textures which we will use for each of the tiles on the map.

int m_NumTileTex;
fread(&m_NumTileTex, sizeof(int), 1, fp);

int NumTileTexSrc;
fread(&NumTileTexSrc, sizeof(int), 1, fp);

char** SrcName = new char*[NumTileTexSrc];
for (int i = 0; i<NumTileTexSrc; i++) {
	SrcName[i] = new char[260];
	fread(SrcName[i], 260, 1, fp);
}

short SrcIdx, TileIdx;
for (int i = 0; i<m_NumTileTex; i++) {
	fread(&SrcIdx, sizeof(short), 1, fp);
	fread(&TileIdx, sizeof(short), 1, fp);
}

m_NumTileTex is the number of textures which get used for this particular map. NumTileTexSrc is the number of files from which all these textures will be loaded from. SrcIdx is the index into the source name array for which this texture is located and TileIdx is the index of this texture within the file. Basically the DTEX folder has a bunch of files in it and each of these files contains a bunch of textures put together one by one. In order to get one of these textures you first must know which file to look into and then how far into that file to look in order  to get your texture.

Here are the last few variables we need.

int NumLightMap;
fread(&NumLightMap, sizeof(int), 1, fp);

int m_iRiverCount;
fread(&m_iRiverCount, sizeof(int), 1, fp);

int m_iPondMeshNum;
fread(&m_iPondMeshNum, sizeof(int), 1, fp);

If you have a few rivers or ponds on the map then each files have to be loaded and looked through.

This is the basic structure of the 1299 .gtd file. If you would like all the code along with a simplistic OpenGL implementation of the rendering go here.

2 thoughts on “Knight Online Terrain (GTD format for 1299)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s