CUnitDefLoader::CUnitDefLoader(Global* GL){

	// Initialize pointer to the Global class
	G = GL;
	
	// retrieve the number of unit definitions
	unum = G->cb->GetNumUnitDefs();

	// for debugging purposes:
	//G->L.iprint("AI interface says this mod has this many units! :: "+to_string(unum));
	
	// Check if a horrific error has occured
	if(unum < 1){
		// omgwtf this should never happen!
		G->L.eprint("URGENT! GetNumUnitDefs returned ZERO!! This means that there are no unit definitions of any kind!!! Unrecoverable error!");

		// A horrible event has occurred somewhere in the spring engine for this to have happened.
		// Even if the AI could recover from this, the engine could not.
		// Exit this method immediatly. A crash is likely but if the crash was fixed, the engine
		// or another AI, would crash afterwards anyway.
		return;

		
	}

	// initialize arrays

	// The unitdeflist array will be passed to the engine where it will be filled with pointers
	UnitDefList = new const UnitDef*[unum];

	// retrieve the list of unit definition pointers from the engine
	G->cb->GetUnitDefList(UnitDefList);

	// for each definition
	for(int n=0; n < unum; n++){

		// retrieve the units definition
		const UnitDef* pud = UnitDefList[n];

		// initialize a UnitTypeData object
		CUnitTypeData* cutd = new CUnitTypeData();
		
		// now initialize the newly added object with the unit definition
		cutd->Init(G,pud);

		
		

		// add it into the main array
		type_data[pud->id] = cutd;


		// check if the unit definition is zero, if so skip
		//if(pud == 0) continue;

		// make sure the name is in the correct format and add it to the map container
		string na = pud->name;
		trim(na);
		tolowercase(na);
		defs[na] = pud->id;
	}
}