Return


By BlackSabath



How To Read A Warzone 2100 *.lnd Data File

 

The following is "C" language code for extracting enough data from a Warzone 2100 .lnd data file to generate a 3D image of the map. No attempt is made here to extract additional map info like building placement at this time. Anything in light blue is "C" code and anything in yellow is comments to explain the process.

 

 

/* Warzone data files (*.lnd) are arranged into several different data segments. The first segment is the header data for the map and contains things like the map width and height, texture page name & The next segment in the file contains the tile data for each tile in the map  The remaining segments in the file contain information like building placement and other info that is not addressed in this process. */

// these are some of the variables that will be used to store the map data 

int mapwidth;

int mapheight;

char texpage[40];

int totaltiles;

 

int gotdata = 0; // this is a counter that is incremented as data is read from the file.It is designed to prevent the program from reading

// more than one bit of data into a variable. It is used in conjunction with the section counter. The reason for this is that the

// section  counter may be incremented and then read 10 different data fields before it gets incremented again but we only

// want to extract the first bit of data and ignore the rest. So we use the counters section and gotdata in conjuction with each

// other to signal when we need to store data.

char mapdata[48]; // local variable used to hold the data as it is read from the file.

char stringcomp[40] = " "; // local variable used for testing

int test = 1; // local variable used for testing

static int sec5pos = 0; // yet another counter used when we are reading the second segment in the data file which contains the tile

// information. An example line of tile data follows.

//        TID 63 VF 0 TF 0 F 56 VH 135 135 135 135

int numtiles = 0; // a count of the number of tiles that have been read from the data file int section = 0; // this is a counter that will be incremented as

// follows.

// section = 0 we have started to read the data file but have not found any of the test conditions yet

// section = 1 we have found the first {

// section = 2 we have found the word MapWidth and want to extract the data to the variable mapwidth

// section = 3 we have found the word MapHeight and want to extract the data to the variable mapheight

// section = 4 we have found the second { and want to extract the next bit of data to the variable texpage

// section = 5 we have found the first } and want to skip the word NumTiles and read the data after it

// section = 6 we have found the third { and are entering the second segment of the data file and are ready to start

// extracting tile data.

FILE *fin = fopen("MangodaiColosseumZ60.lnd","r");

while (fscanf(fin,"%48s",&mapdata) != EOF)

{          if (section == 2 && gotdata == 0)

            {          mapwidth = atoi(mapdata);

                        gotdata += 1;                          

            }

if (section == 3 && gotdata == 1)

            {          mapheight = atoi(mapdata);

                        gotdata += 1;                          

            }

            if (section == 4 && gotdata == 2)

            {          strcpy(texpage,mapdata); 

                        gotdata += 1;                          

            }

            if (section == 5 )

            {          fscanf(fin,"%48s",&mapdata);

totaltiles  = atoi(mapdata);

// declare the rest of the variables that will be used to store the map data 

short int tid[totaltiles];

short int vf[totaltiles];

short int tf[totaltiles];

short int f[totaltiles];

short int vh[totaltiles][4];

            }

            if (section == 6  )

            {          if ( numtiles < totaltiles)

                        {

                                    if (sec5pos == 1)

                                    tid[numtiles] = atoi(mapdata);

                                    if (sec5pos == 3)

                                    vf[numtiles] = atoi(mapdata);

                                    if (sec5pos == 5)

                                    tf[numtiles] = atoi(mapdata);

                                    if (sec5pos == 7)

                                    f[numtiles] = atoi(mapdata);

                                    if (sec5pos == 9)

                                    vh[numtiles][0] = atoi(mapdata);

                                    if (sec5pos == 10)

                                    vh[numtiles][1] = atoi(mapdata);

                                    if (sec5pos == 11)

                                    vh[numtiles][2] = atoi(mapdata);

                                    if (sec5pos == 12)

                                    vh[numtiles][3] = atoi(mapdata);

                        }

                        sec5pos += 1;

                        if (sec5pos >= 13)

                        {          sec5pos = 0;

                                    numtiles += 1;

                        }

            }

// The following code tests the data that is being read and sets flags so the data can be extracted and placed in the correct variables

// this is accomplished by using the counter named section

            test = strcmp( "{" ,mapdata); if (test == 0)

            {          section += 1;

                        test = 1;

            }

            test = strcmp( "}" ,mapdata);

            if (test == 0)

            {          section += 1;

                        test = 1;

            }

            test = strcmp( "MapWidth" ,mapdata);

            if (test == 0)

            {          section += 1;

                        test = 1;

            }

            test = strcmp( "MapHeight" ,mapdata);

            if (test == 0)

            {          section += 1;

                        test = 1;

            }

}

fclose(fin);

 

/* The line of tile data that was shown as an example above and is repeated here

        TID 63 VF 0 TF 0 F 56 VH 135 135 135 135

     Has the following definition

     TID 63 stands for TILE I.D.  & 63 means it is the 63 tile on the page starting at the top left corner of the page and numbering the

     tiles row by row left to right top to bottom.

     VF 0 stands for TRIANGLE FLIP each tile is made up of 2 triangles which form a square. There are 2 possible ways to arrange

      the 2 triangles to create a tile. If  the value is 0 the triangles have not been reversed. If the value is 1 the tile has its triangles

      reversed and also as we will see shortly 2 will be added to the value of F.

      TF 0 stands for TEXTURE FLIP or TILE FLIP and has the following values

       if the texture is flipped in the X direction set TF = 1 & add 4 to F

       if the texture is flipped in the Y direction add 8 to F

       F 56 is a bit flag which is designed to represent a number of things. In addition to the allready described values that F can have

       it is also used to contain a value to indicate the rotation of the texture for the tile as follows.

       rotate 90 -> add 16 to F

       rotate 180 -> add 32 to F

       rotate 270 -> add 48 to F

       VH 135 135 135 135 stands for VERTICLE HEIGHT and represents the height of the tile at each of the 4 corners.

*/