CombatGroup*
CombatGroup::LoadOrderOfBattle(const char* filename, int team, Combatant* combatant)
{
	CombatGroup* force = 0;
	DataLoader* loader = DataLoader::GetLoader();
	BYTE* block;
	loader->LoadBuffer(filename, block, true);

	Parser parser(new(__FILE__,__LINE__) BlockReader((const char*) block));
	Term*  term = parser.ParseTerm();

	if (!term) {
		Print("ERROR: could not parse order of battle '%s'\n", filename);
		return 0;
	}
	else {
		TermText* file_type = term->isText();
		if (!file_type || file_type->value() != "ORDER_OF_BATTLE") {
			Print("ERROR: invalid Order of Battle file '%s'\n", filename);
			term->print(10);
			return 0;
		}
	}


	do {
		delete term; term = 0;
		term = parser.ParseTerm();
		
		if (term) {
			TermDef* def = term->isDef();
			if (def) {
				if (def->name()->value() == "group") {
					if (!def->term() || !def->term()->isStruct()) {
						Print("WARNING: group struct missing in '%s'\n", filename);
					}
					else {
						TermStruct* val = def->term()->isStruct();

						char  name[256];
						char  type[64];
						char  intel[64];
						char  region[64];
						char  system[64];
						char  parent_type[64];
						int   parent_id = 0;
						int   id  = 0;
						int   iff = -1;
						Vec3  loc = Vec3(1.0e9f,0.0f,0.0f);

						List<CombatUnit>  unit_list;
						char              unit_name[64];
						char              unit_regnum[16];
						char              unit_design[64];
						char              unit_skin[64];
						int               unit_class  = 0;
						int               unit_count  = 1;
						int               unit_dead   = 0;
						int               unit_damage = 0;
						int               unit_heading= 0;
						int               unit_index  = 0;
						
						*name          = 0;
						*type          = 0;
						*intel         = 0;
						*region        = 0;
						*system        = 0;
						*parent_type   = 0;
						*unit_name     = 0;
						*unit_regnum   = 0;
						*unit_design   = 0;
						*unit_skin     = 0;

						strcpy_s(intel, "KNOWN");

						// all groups in this OOB default to the IFF of the main force
						if (force)
						iff = force->GetIFF();
						
						for (int i = 0; i < val->elements()->size(); i++) {
							TermDef* pdef = val->elements()->at(i)->isDef();
							if (pdef && (iff < 0 || team < 0 || iff == team)) {
								GET_DEF_TEXT(name);
								else GET_DEF_TEXT(type);
								else GET_DEF_TEXT(intel);
								else GET_DEF_TEXT(region);
								else GET_DEF_TEXT(system);
								else GET_DEF_VEC(loc);
								else GET_DEF_TEXT(parent_type);
								else GET_DEF_NUM(parent_id);
void
NetClientConfig::Load()
{
	server_index = -1;

	// read the config file:
	BYTE*       block    = 0;
	int         blocklen = 0;

	char        filename[64];
	strcpy_s(filename, "client.cfg");

	FILE* f;
	::fopen_s(&f, filename, "rb");

	if (f) {
		::fseek(f, 0, SEEK_END);
		blocklen = ftell(f);
		::fseek(f, 0, SEEK_SET);

		block = new(__FILE__,__LINE__) BYTE[blocklen+1];
		block[blocklen] = 0;

		::fread(block, blocklen, 1, f);
		::fclose(f);
	}

	if (blocklen == 0)
	return;

	servers.destroy();

	Parser parser(new(__FILE__,__LINE__) BlockReader((const char*) block, blocklen));
	Term*  term = parser.ParseTerm();

	if (!term) {
		Print("ERROR: could not parse '%s'.\n", filename);
		return;
	}
	else {
		TermText* file_type = term->isText();
		if (!file_type || file_type->value() != "CLIENT_CONFIG") {
			Print("WARNING: invalid '%s' file.  Using defaults\n", filename);
			return;
		}
	}

	do {
		delete term;

		term = parser.ParseTerm();
		
		if (term) {
			TermDef* def = term->isDef();
			if (def) {
				if (def->name()->value() == "server") {
					if (!def->term() || !def->term()->isStruct()) {
						Print("WARNING: server struct missing in '%s'\n", filename);
					}
					else {
						TermStruct* val = def->term()->isStruct();

						Text name;
						Text addr;
						Text pass;
						int  port;

						for (int i = 0; i < val->elements()->size(); i++) {
							TermDef* pdef = val->elements()->at(i)->isDef();
							if (pdef) {
								if (pdef->name()->value() == "name")
								GetDefText(name, pdef, filename);
								else if (pdef->name()->value() == "addr")
								GetDefText(addr, pdef, filename);
								else if (pdef->name()->value() == "pass")
								GetDefText(pass, pdef, filename);
								else if (pdef->name()->value() == "port")
								GetDefNumber(port, pdef, filename);
							}
						}

						AddServer(name, addr, (WORD) port, pass, true);
					}
				}
				else {
					Print("WARNING: unknown label '%s' in '%s'\n",
					def->name()->value().data(), filename);
				}
			}
		}
	}
	while (term);

	delete [] block;
}
void
ModConfig::Load()
{
	// read the config file:
	BYTE*       block    = 0;
	int         blocklen = 0;

	char        filename[64];
	strcpy_s(filename, "mod.cfg");

	FILE* f;
	::fopen_s(&f, filename, "rb");

	if (f) {
		::fseek(f, 0, SEEK_END);
		blocklen = ftell(f);
		::fseek(f, 0, SEEK_SET);

		block = new(__FILE__,__LINE__) BYTE[blocklen+1];
		block[blocklen] = 0;

		::fread(block, blocklen, 1, f);
		::fclose(f);
	}

	if (blocklen == 0)
	return;

	Parser parser(new(__FILE__,__LINE__) BlockReader((const char*) block, blocklen));
	Term*  term = parser.ParseTerm();

	if (!term) {
		Print("ERROR: could not parse '%s'.\n", filename);
		return;
	}
	else {
		TermText* file_type = term->isText();
		if (!file_type || file_type->value() != "MOD_CONFIG") {
			Print("WARNING: invalid '%s' file. No mods deployed\n", filename);
			return;
		}
	}

	enabled.destroy();

	do {
		delete term;

		term = parser.ParseTerm();

		if (term) {
			TermDef* def = term->isDef();
			if (def) {
				Text name;
				GetDefText(name, def, filename);
				enabled.append(new(__FILE__,__LINE__) Text(name));
			}
		}
	}
	while (term);

	delete [] block;
}
void
SystemDesign::Initialize(const char* filename)
{
    Print("Loading System Designs '%s'\n", filename);

    // Load Design File:
    DataLoader* loader = DataLoader::GetLoader();
    BYTE* block;

    int blocklen = loader->LoadBuffer(filename, block, true);
    Parser parser(new(__FILE__,__LINE__) BlockReader((const char*) block, blocklen));
    Term*  term = parser.ParseTerm();

    if (!term) {
        Print("ERROR: could not parse '%s'\n", filename);
        exit(-3);
    }
    else {
        TermText* file_type = term->isText();
        if (!file_type || file_type->value() != "SYSTEM") {
            Print("ERROR: invalid system design file '%s'\n", filename);
            exit(-4);
        }
    }

    int type = 1;

    do {
        delete term;

        term = parser.ParseTerm();
        
        if (term) {
            TermDef* def = term->isDef();
            if (def) {
                if (def->name()->value() == "system") {

                    if (!def->term() || !def->term()->isStruct()) {
                        Print("WARNING: system structure missing in '%s'\n", filename);
                    }
                    else {
                        TermStruct*   val    = def->term()->isStruct();
                        SystemDesign* design = new(__FILE__,__LINE__) SystemDesign;
                        
                        for (int i = 0; i < val->elements()->size(); i++) {
                            TermDef* pdef = val->elements()->at(i)->isDef();
                            if (pdef) {
                                GET_DEF_TEXT(pdef, design, name);

                                else if (pdef->name()->value()==("component")) {
                                    if (!pdef->term() || !pdef->term()->isStruct()) {
                                        Print("WARNING: component structure missing in system '%s' in '%s'\n", (const char*) design->name, filename);
                                    }
                                    else {
                                        TermStruct*   val2    = pdef->term()->isStruct();
                                        ComponentDesign* comp_design = new(__FILE__,__LINE__) ComponentDesign;
                                        
                                        for (int i = 0; i < val2->elements()->size(); i++) {
                                            TermDef* pdef2 = val2->elements()->at(i)->isDef();
                                            if (pdef2) {
                                                GET_DEF_TEXT(pdef2, comp_design, name);
                                                else GET_DEF_TEXT(pdef2, comp_design, abrv);
                                                else GET_DEF_NUM (pdef2, comp_design, repair_time);
                                                else GET_DEF_NUM (pdef2, comp_design, replace_time);
                                                else GET_DEF_NUM (pdef2, comp_design, spares);
                                                else GET_DEF_NUM (pdef2, comp_design, affects);

                                                else {
                                                    Print("WARNING: parameter '%s' ignored in '%s'\n",
                                                    pdef2->name()->value().data(), filename);
                                                }
                                            }
                                        }

                                        design->components.append(comp_design);
                                    }
                                }

                                else {
                                    Print("WARNING: parameter '%s' ignored in '%s'\n",
                                    pdef->name()->value().data(), filename);
                                }
                            }
Exemple #5
0
void
Galaxy::Load(const char* filename)
{
    Print("\nLoading Galaxy: %s\n", filename);

    BYTE*       block  = 0;
    DataLoader* loader = DataLoader::GetLoader();
    loader->LoadBuffer(filename, block, true);

    Parser parser(new(__FILE__,__LINE__) BlockReader((const char*) block));

    Term*  term = parser.ParseTerm();

    if (!term) {
        Print("WARNING: could not parse '%s'\n", filename);
        return;
    }
    else {
        TermText* file_type = term->isText();
        if (!file_type || file_type->value() != "GALAXY") {
            Print("WARNING: invalid galaxy file '%s'\n", filename);
            return;
        }
    }

    // parse the galaxy:
    do {
        delete term;
        term = parser.ParseTerm();
        
        if (term) {
            TermDef* def = term->isDef();
            if (def) {
                if (def->name()->value() == "radius") {
                    GetDefNumber(radius, def, filename);
                }

                else if (def->name()->value() == "system") {
                    if (!def->term() || !def->term()->isStruct()) {
                        Print("WARNING: system struct missing in '%s'\n", filename);
                    }
                    else {
                        TermStruct* val = def->term()->isStruct();

                        char  sys_name[32];
                        char  classname[32];
                        Vec3  sys_loc;
                        int   sys_iff = 0;
                        int   star_class = Star::G;

                        sys_name[0] = 0;
                        
                        for (int i = 0; i < val->elements()->size(); i++) {
                            TermDef* pdef = val->elements()->at(i)->isDef();
                            if (pdef) {
                                if (pdef->name()->value() == "name")
                                GetDefText(sys_name, pdef, filename);

                                else if (pdef->name()->value() == "loc")
                                GetDefVec(sys_loc, pdef, filename);

                                else if (pdef->name()->value() == "iff")
                                GetDefNumber(sys_iff, pdef, filename);

                                else if (pdef->name()->value() == "class") {
                                    GetDefText(classname, pdef, filename);

                                    switch (classname[0]) {
                                    case 'O':   star_class = Star::O;            break;
                                    case 'B':   star_class = Star::B;            break;
                                    case 'A':   star_class = Star::A;            break;
                                    case 'F':   star_class = Star::F;            break;
                                    case 'G':   star_class = Star::G;            break;
                                    case 'K':   star_class = Star::K;            break;
                                    case 'M':   star_class = Star::M;            break;
                                    case 'R':   star_class = Star::RED_GIANT;    break;
                                    case 'W':   star_class = Star::WHITE_DWARF;  break;
                                    case 'Z':   star_class = Star::BLACK_HOLE;   break;
                                    }
                                }
                            }
                        }

                        if (sys_name[0]) {
                            StarSystem* star_system = new(__FILE__,__LINE__) StarSystem(sys_name, sys_loc, sys_iff, star_class);
                            star_system->Load();
                            systems.append(star_system);

                            Star* star = new(__FILE__,__LINE__) Star(sys_name, sys_loc, star_class);
                            stars.append(star);
                        }
                    }
                }

                else if (def->name()->value() == "star") {
                    if (!def->term() || !def->term()->isStruct()) {
                        Print("WARNING: star struct missing in '%s'\n", filename);
                    }
                    else {
                        TermStruct* val = def->term()->isStruct();

                        char  star_name[32];
                        char  classname[32];
                        Vec3  star_loc;
                        int   star_class = Star::G;

                        star_name[0] = 0;
                        
                        for (int i = 0; i < val->elements()->size(); i++) {
                            TermDef* pdef = val->elements()->at(i)->isDef();
                            if (pdef) {
                                if (pdef->name()->value() == "name")
                                GetDefText(star_name, pdef, filename);

                                else if (pdef->name()->value() == "loc")
                                GetDefVec(star_loc, pdef, filename);

                                else if (pdef->name()->value() == "class") {
                                    GetDefText(classname, pdef, filename);

                                    switch (classname[0]) {
                                    case 'O':   star_class = Star::O;            break;
                                    case 'B':   star_class = Star::B;            break;
                                    case 'A':   star_class = Star::A;            break;
                                    case 'F':   star_class = Star::F;            break;
                                    case 'G':   star_class = Star::G;            break;
                                    case 'K':   star_class = Star::K;            break;
                                    case 'M':   star_class = Star::M;            break;
                                    case 'R':   star_class = Star::RED_GIANT;    break;
                                    case 'W':   star_class = Star::WHITE_DWARF;  break;
                                    case 'Z':   star_class = Star::BLACK_HOLE;   break;
                                    }
                                }
                            }
                        }

                        if (star_name[0]) {
                            Star* star = new(__FILE__,__LINE__) Star(star_name, star_loc, star_class);
                            stars.append(star);
                        }
                    }
                }
            }
        }
    }
    while (term);

    loader->ReleaseBuffer(block);
    loader->SetDataPath(0);
}