Contents
Back
Forward

4. Data Structures



4.1 Data Structures overview

If you know C/C++ (or if you've read the C/C++ tutorials I recommended) you should know all about basic data types and how to create new data types using struct keyword.
In this chapter I'll explain all the data types I've invented to map Civ2 game structures in C data structures, with full C/C++ source code whenever possible.
For a complete description of Civ2 game structures, I suggest you read the Allard Höfelt document about Hex-Editing (even if it was principally written for Civ2 classic and not for Tot). I've included it in the Docs subdirectory of this CSPL package.
All these data structures are defined in the CSPL.h file if you want to have a look.

BASE TYPES
WORD = 2 bytes
DWORD = 4 bytes
byte = 1 byte
BOOL = boolean (can be TRUE or FALSE)
int = integer (I don't recall if 32bit or something else. If you're interested, do your own research :oP)
LPCTSTR = Pointer to string (it's the same as char* )
char = one character (1 byte)

STRUCT TYPES
A lot of times base types are not enough, so C gives programmers a command, struct, to create complex data types defined as a set of basic types:
struct <STRUCT_TYPE_NAME>
{
list of basic components defined with <TYPE> <NAME> ;
};

For example, let's define a complex data type called Position, made up of three WORDS representing the three coordinates x,y,z:

struct Position
{WORD x;
WORD y;
WORD z;
};



4.2 Constants
In CSPL.h I've also defined a lot of constants to handle Civ specific constants,
If you're interested in a full list of these constants look here


4.3 Position Data type
The example shown at the end of section 4.1  is a fully functional data structure used by CSPL, so let's examine it in greater depth:
Name : Position

Position codes a spatial position in ToT; it's a structure formed by three WORDs called x,y,z:
x: x coordinate
y: y coordinate
z: nr of map (0 means first map - 3 means fourth map)

SOURCE CODE:
struct Position
{WORD x;
WORD y;
WORD z;
};

Example of use:
{…
Position pos;
pos.x=5; //set x coordinate of pos structure to 5
pos.y=3; //set y coordinate of pos structure to 3
pos.z=MAP_0;//set z coordinate of pos structure to 0 (MAP_0)
…}


4.4 Unit Data type
Name : Unit

Unit structures maps ToT unit data in a single C++ structure

SOURCE CODE:
struct Unit
{Position pos; //Position (x,y,z) of the unit
bool Veteran; //true if unit is veteran, false otherwise
byte Type; //Unit type
byte Owner; //Civilization which owns the unit
byte MoveLost; //Move points used by unit (expressed in 1/3 of move unit)
byte FuelUsed; //Fuel used
byte HealthLost; //Hit points lost by the unit (read AH docs)
byte Commodity; //It has only sense with Caravan units
byte Orders; //Order assigned to the unit (read AH docs)
byte HomeCity; //ID of Home City
Position Goto; //(x,y,z?) of goto order
WORD ID; //Unit ID, is unique for each unit
};

NOTE:
MoveLost is expressed in 1/3 of move units (remember roads), this means that if I have a unit with a max of 8 move points and MoveLost is 1 then unit can use 7+2/3 moves.
FuelUsed represents the number of turns the unit has spent in flight: Let's say I have a bomber, bomber can stay in flight for 2 turns, if I put 1 in FuelUsed ToT will believe that bomber has already used 1 turn.
This one is very important: ToT uses the same physical byte to store FuelLost and Commodity (I guess this is a Civ bug since the byte adjacent to this one in the unit section doesn't seems to have any meaning), if that byte represents Commodity or FuelLost can be deducted by the type of unit (if it's a bomber it will represent FuelLost while if it is a caravan it will be Commodity).
What does this means? well, it means that you've got to be careful when creating units from zero, because you must be sure that the FuelUsed field contains the same value of Commodity, or elsewhere some problems could arise.
(this is true only with units created from zero, if you simply read a unit in the game, modify it and save it back, this is not a problem since FuelLost and Commodity are read from the same byte and therefore they have the same value.)


Example of use:
{…
//Example of event, if the unit milunit is owned by Civ white it became Veteran
if(milunit.Owner==CIV_WHITE)
milunit.Veteran=true;

}



4.5 City Data type
Name : City

City structure maps ToT City data in a single C++ structure

SOURCE CODE:
struct City
{Position pos; //Position (x,y,z) of the city
DWORD Flag; //can build coastal improvement,denotes auto-build etc…
byte Owner; //Civilization which owns the city
byte Size; //Size of the city
byte Founder; //Civilization which founded the city
DWORD Specialists; //Specialists, look at Allard Höfelt doc
WORD Food; //food in food box
WORD Production; //shields in shield box
WORD NetTrade; // net trade (not including those from trade routes)
char Name[16]; //Name of the city
byte WorkerInner; // workers in inner circle (see AH docs)
byte Worker8; // workers in 8 of outer circle (see AH docs)
byte Worker4; // workers in 4 of outer circle (see AH docs)
byte NrSpecialists; //nr of specialists time 4 (see AH docs)
byte Improve[5]; // city improvements (see AH docs)
byte ItemProduction; //Item in production
byte TradeRouteNr; //number of active trade routes
byte Ava1Comm; //1st Commodity available (see AH docs)
byte Ava2Comm; //2nd Commodity available (see AH docs)
byte Ava3Comm; //3rd Commodity available (see AH docs)
byte Dem1Comm; //1st Commodity demanded (see AH docs)
byte Dem2Comm; //2nd Commodity demanded (see AH docs)
byte Dem3Comm; //3rd Commodity demanded (see AH docs)
byte Rou1Comm; //1st Commodity in route (see AH docs)
byte Rou2Comm; //2nd Commodity in route (see AH docs)
byte Rou3Comm; //3rd Commodity in route (see AH docs)
byte Partn1Id; //1st route city partner (see AH docs)
byte Partn2Id; //2nd route city partner (see AH docs)
byte Partn3Id; //3rd route city partner (see AH docs)
WORD Science; // Science production in the city
WORD Tax; //Money production in the city
WORD Trade; //Trade production in the city (including trade routes)
byte FoodProd; //Food production
byte ShieldProd; //Shield production
byte Happy; //number of happy citizen
byte Unhappy; //number of unhappy citizen
WORD ID; //City ID, is unique for each city
};

Example of use:
{…
//Example of event, if the city cityselected population goes under 4 it changes ownership and name
if(cityselected.Size<4)
cityselected.Owner=CIV_WHITE;
cityselected.Name="Rebel City";

}



4.6 Wonder Data type
Name : Wonder

Wonder structure maps ToT wonder data in a single C++ structure:
It contains just a WORD which can be 0xFFFF (if wonder is still not built), 0xEFFF (if the wonder has been destroyed) or just the CityID of the City where Wonder is located.

SOURCE CODE:
struct Wonder
{WORD CityID;
};

Example of use:
{…
//Example of event, we want to build automatically the wonder Wonder0 in city cityselected
WORD ID=cityselected.CityID; //Assign cityselected.CityID to var ID
if (Wonder0.cityID==0xFFFF) //if Wonder0 has not yet been built
Wonder0.CityID=ID; //build Wonder0 in cityselected

}



4.7 Global Data type
Name : Global

Global structure contains all information about the game as a whole (ex: difficulty level, pollution, turns passed, etc…).

SOURCE CODE:
struct Global
{WORD Turn;
byte CivsInPlay; //Shows Civs still in play (allows you to add civs while the game is running!)
byte HumanCivs; //Shows human civilizations
byte Pollution;
byte PeaceTurns;
};

Example of use:
{…
//Example of event, if turn is equal to 100 destroy yellow civilization
if (Global.turn>=100)
Global.CivsInPlay=Global.CivsInPlay ^ CIV_YELLOW
//
If you don't understand last line don't worry, just read carefully Appendix 1

}



4.8 Tribe Data type
Name : Tribe

Tribe structure contains ONLY text strings about one civ, this means leader's name, people name etc.

SOURCE CODE:
struct Tribe
{byte Style;
char Leader[24];
char Name[24];
char Adjective[24];
};


4.9 Tile Data type
Name : Tile

Tile structure maps ToT tile structure in a single C++ structure.

SOURCE CODE:
struct Tile
{
byte Terrain;
byte Improvements;
byte Radii;
byte BodyCounter;
byte Explored;
byte Sixth;
};

Example of use:
{…
//Simply changes terrain type in DESERT of Tile Tile1
Tile1.Terrain=DESERT;

}



4.10 Civ Data type
Name : Civ

Civ structure maps ToT civ structure (Civ money, diplomatic relationship, etc…) in a single C++ structure.

SOURCE CODE:
struct Civ
{Tribe Names;
byte Gender;
DWORD Money;
byte Research;
WORD TechResearched;
byte PercentageScience;
byte PercentageTax;
byte Government;
DWORD Treaty[7];
byte Attitude[7];
byte Techs[15];
WORD Demographics;
WORD LastContact[7];
byte Reputation;
};


4.11 TilesBox Data type
Name : TilesBox

TilesBox structure is just a service structure: it is just an array of 9 tiles, used principally to keep track of tiles adjacent to a unit or city or everything else.

SOURCE CODE:
struct TilesBox
{Tile Elem[3][3];
};




Contents / Introduction
Chapter I / Chapter II / Chapter III / Chapter IV / Chapter V / Chapter VI / Chapter VII
Chapter VIII / Chapter IX / Chapter X / Chapter XI / Chapter XII / Chapter XIII
Appendix A / Appendix B / Appendix C