Example #1
0
/**\brief Parser to parse the XML file.
 */
bool Alliance::FromXMLNode( xmlDocPtr doc, xmlNodePtr node ) {
	xmlNodePtr  attr;
	string value;

	if( (attr = FirstChildNamed(node,"aggressiveness")) ){
		value = NodeToString(doc,attr);
		aggressiveness = static_cast<float>(atof( value.c_str() ) / 10.);
	} else return false;

	if( (attr = FirstChildNamed(node,"attackSize")) ) {
		value = NodeToString(doc,attr);
		attackSize = (short int)atof( value.c_str() );
	} else return false;

	if( (attr = FirstChildNamed(node,"currency")) ) {
		value = NodeToString(doc,attr);
		currency = value;
	} else return false;

	if( (attr = FirstChildNamed(node,"color")) ) {
		value = NodeToString(doc,attr);
		color = Color::Get(value);
	} else return false;
	
	return true;
}
Example #2
0
Mission* Mission::FromXMLNode( xmlDocPtr doc, xmlNodePtr node )
{
    string type;
    int version = 0;;
    int missionTable;
    xmlNodePtr typeNode = FirstChildNamed(node,"type");
    xmlNodePtr versionNode = FirstChildNamed(node,"version");
    xmlNodePtr missionNode = FirstChildNamed(node,"value");

    type = NodeToString(doc, typeNode);
    if( versionNode != NULL ) {
        version = NodeToInt(doc, versionNode);
    }

    lua_State *L = Lua::CurrentState();

    // Turn the XML data into a Table
    Lua::ConvertFromXML(L, doc, missionNode);
    // pop the name off the top of the stack.  It should just be "missionTable" anyway.
    assert(lua_istable(L,lua_gettop(L)));
    // Gets and pops the top of the stack, which should have the the missionTable.
    missionTable = luaL_ref(L, LUA_REGISTRYINDEX);

    assert(lua_isstring(L,lua_gettop(L)));
    lua_pop(L,1); // Pop the Name

    // Validate this Mission
    if( !Mission::ValidateMission(L, type, missionTable, version) ) {
        LogMsg(ERR, "Something important!");
        return NULL;
    }

    return new Mission(L, type, missionTable);
}
Example #3
0
File: gate.cpp Project: ebos/Epiar
bool Gate::FromXMLNode( xmlDocPtr doc, xmlNodePtr node ) {
	xmlNodePtr  attr;
	string value;
	Coordinate pos;

	if( (attr = FirstChildNamed(node,"x")) ){
		value = NodeToString(doc,attr);
		pos.SetX( atof( value.c_str() ));
	} else return false;

	if( (attr = FirstChildNamed(node,"y")) ){
		value = NodeToString(doc,attr);
		pos.SetY( atof( value.c_str() ));
	} else return false;

	SetWorldPosition( pos );

	if( (attr = FirstChildNamed(node,"exit")) ){
		value = NodeToString(doc,attr);
		Gate* exit = Gates::Instance()->GetGate( value );
		if( exit != NULL ) {
			Gate::SetPair(this,exit);
		}
	}

	return true;
}
Example #4
0
/**\brief Configure the ship's weapon slots based on the XML node weaponSlots.
 */
bool Model::ConfigureWeaponSlots( xmlDocPtr doc, xmlNodePtr node ) {

	xmlNodePtr slotPtr;
	string value;

	//if( (slotPtr = FirstChildNamed(node,"slot")) ){
        for( slotPtr = FirstChildNamed(node,"slot"); slotPtr != NULL; slotPtr = NextSiblingNamed(slotPtr,"slot") ){
		WeaponSlot newSlot;

		xmlNodePtr attr;

		if( (attr = FirstChildNamed(slotPtr,"name")) ){
			value = NodeToString(doc,attr);
			newSlot.name = value;
		} else return false;

		if( (attr = FirstChildNamed(slotPtr,"coord")) ){
			value = NodeToString(doc,attr);
			// go deeper...

			xmlNodePtr coordAttr;
			if( (coordAttr = FirstChildNamed(attr,"x")) ){
				newSlot.x = NodeToInt(doc,coordAttr);
			} else return false;
			if( (coordAttr = FirstChildNamed(attr,"y")) ){
				newSlot.y = NodeToInt(doc,coordAttr);
			} else return false;
		} else return false;

		if( (attr = FirstChildNamed(slotPtr,"angle")) ){
			newSlot.angle = NodeToFloat(doc,attr);
		} else return false;

		if( (attr = FirstChildNamed(slotPtr,"motionAngle")) ){
			value = NodeToString(doc,attr);
			newSlot.motionAngle = atof(value.c_str());
		} else return false;

		if( (attr = FirstChildNamed(slotPtr,"content")) ){
			// this check is necessary because NodeToString() won't translate <item></item> into ""
			if(attr->xmlChildrenNode)
				value = NodeToString(doc,attr);
			else
				value = ""; // slot is empty

			newSlot.content = Menu::GetCurrentScenario()->GetWeapons()->GetWeapon( value );
		} else return false;

		if( (attr = FirstChildNamed(slotPtr,"firingGroup")) ){
			newSlot.firingGroup = NodeToInt(doc,attr);
		} else return false;

		weaponSlots.push_back(newSlot);
	}

        return true;
}
Example #5
0
/**\brief Configure the ship's weapon slots, first copying the default slots
  * from the model, then updating it according to the saved player XML.
 */
bool Player::ConfigureWeaponSlots( xmlDocPtr doc, xmlNodePtr node ) {

	xmlNodePtr slotPtr;
	string value;

	// Grab the default slots from the player's ship model, then update it below.
	// This makes a copy, not a pointer or reference, so we don't have to worry
	// that it might alter the slots in the model.
	this->weaponSlots = this->GetModel()->GetWeaponSlots();
			

	for( slotPtr = FirstChildNamed(node,"weapSlot"); slotPtr != NULL; slotPtr = NextSiblingNamed(slotPtr,"weapSlot") ){
		WeaponSlot *existingSlot = NULL;

		xmlNodePtr attr;

		string slotName;
		if( (attr = FirstChildNamed(slotPtr,"name")) ){
			value = NodeToString(doc,attr);
			slotName = value;
		} else return false;

		for(unsigned int s = 0; s < weaponSlots.size(); s++){
			if(weaponSlots[s].name == slotName){
				existingSlot = &weaponSlots[s];
				break;
			}
		}
		if(!existingSlot) return false;

		if( (attr = FirstChildNamed(slotPtr,"content")) ){
			// this check is necessary because NodeToString() won't translate <item></item> into ""
			if(attr->xmlChildrenNode)
				value = NodeToString(doc,attr);
			else
				value = ""; // slot is empty

			Weapon* weapon = Weapons::Instance()->GetWeapon( value );
			existingSlot->content = weapon;
		} else return false;

		if( (attr = FirstChildNamed(slotPtr,"firingGroup")) ){
			value = NodeToString(doc,attr);
			existingSlot->firingGroup = (short)atoi(value.c_str());
		} else return false;

	}

	// no need to push any WeaponSlot back into the player's weaponSlots; slots were edited in place

	return true;
}
Example #6
0
/**\brief Convert an XML node from the old saved-games.xml format to the new format.
 * \details
 *   The old saved-games.xml format put all of the Player information in one file.
 *   The new saved-games.xml format only stores some Player information, but
 *   nothing that relies on loading the Simulation.  Everything in the old
 *   style format is by itself in a standalone xml file named after the player.
 * \todo This could save a copy of the old saved-games.xml to a backup location.
 * \param[in] doc The XML document.
 * \param[in] xnode The XML Node.
 * \return A new XML node that represents the PlayerInfo for the Player.
 */
xmlNodePtr PlayerInfo::ConvertOldVersion( xmlDocPtr doc, xmlNodePtr node ) {
	char buff[256];
	xmlDocPtr xmlPtr;
	xmlNodePtr  attr;
	xmlNodePtr  copy = xmlCopyNode( node, 1);
	string filename = "Resources/Definitions/"+ name +".xml";

	LogMsg(INFO, "Converting %s to an xml file: %s ", name.c_str(), filename.c_str() );

	xmlPtr = xmlNewDoc( BAD_CAST "1.0" );
	xmlDocSetRootElement(xmlPtr, copy);

	// Version information
	snprintf(buff, sizeof(buff), "%d", EPIAR_VERSION_MAJOR);
	xmlNewChild(copy, NULL, BAD_CAST "version-major", BAD_CAST buff);
	snprintf(buff, sizeof(buff), "%d", EPIAR_VERSION_MINOR);
	xmlNewChild(copy, NULL, BAD_CAST "version-minor", BAD_CAST buff);
	snprintf(buff, sizeof(buff), "%d", EPIAR_VERSION_MICRO);
	xmlNewChild(copy, NULL, BAD_CAST "version-macro", BAD_CAST buff);

	xmlSaveFormatFileEnc( filename.c_str(), xmlPtr, "ISO-8859-1", 1);

	xmlNodePtr new_node = xmlNewNode(NULL, BAD_CAST "player");
	xmlNewChild(new_node, NULL, BAD_CAST "name", BAD_CAST GetName().c_str() );
	xmlNewChild(new_node, NULL, BAD_CAST "file", BAD_CAST filename.c_str() );

	if( (attr = FirstChildNamed(copy, "lastLoadTime")) ){
		xmlNewChild(new_node, NULL, BAD_CAST "lastLoadTime", BAD_CAST NodeToString(doc,attr).c_str() );
	}
	
	return new_node;
}
Example #7
0
/**\brief Extract this PlayerInfo from an XML Node
 * \param[in] doc The XML document.
 * \param[in] xnode The XML Node.
 * \returns true if the PlayerInfo was loaded correctly.
*/
bool PlayerInfo::FromXMLNode( xmlDocPtr doc, xmlNodePtr node ) {
	xmlNodePtr  attr;

	// If the node has a Model then it is using the old format.
	// Create that player's XML File before continuing
	if( (attr = FirstChildNamed(node,"model")) ) {
		node = ConvertOldVersion( doc, node );
	}

	// The file attribute is the saved game xml file.
	if( (attr = FirstChildNamed(node,"file")) ) {
		file = NodeToString(doc,attr);
		if( File::Exists( file ) == false ) {
			LogMsg(ERR, "Player %s is corrupt. There is no file '%s'.", name.c_str(), file.c_str() );
			return false;
		}
	} else {
		LogMsg(ERR, "Player %s is corrupt. There is no file attribute.", name.c_str() );
		return false;
	}

	// A corrupt avatar isn't fatal, just don't try to draw it.
	if( (attr = FirstChildNamed(node,"avatar")) ){
		avatar = Image::Get( NodeToString(doc,attr) );
		if( avatar == NULL ) {
			LogMsg(WARN, "Player %s has a corrupt avatar.  There is no image '%s' ", name.c_str(), NodeToString(doc,attr).c_str() );
		}
	}

	if( (attr = FirstChildNamed(node, "scenario")) ){
		scenario = NodeToString(doc,attr);
	} else {
		scenario = "default";
	}

	// A corrupt lastLoadTime isn't fatal, just use January 1, 1970.
	if( (attr = FirstChildNamed(node,"lastLoadTime")) ){
		lastLoadTime = NodeToInt(doc,attr);
	} else {
		lastLoadTime = (time_t)0;
	}

	return true;
}
Example #8
0
xmlNodePtr XMLFile::FindNode( const string& path, bool createIfMissing ) {
	xmlNodePtr cur,parent;
	string tokens = "/";
	vector<string> tokenized;
	vector<string>::iterator iter;
	string partialPath;

	// Check previously memoized values
	map<string,xmlNodePtr>::iterator val = values.find( path );
	if( val != values.end() ){ // If we found it
		// Check that we don't return memoized NULL values when instructed to createIfMissing.
		if( !((val->second==NULL) && createIfMissing) )
		{
			return val->second;
		}
	}

	if( xmlPtr == NULL ) {
		return( (xmlNodePtr)NULL );
	}

	// Initialize the xml navigation cursor
	cur = xmlDocGetRootElement( xmlPtr );
	if( cur == NULL ) {
		LogMsg(WARN, "XML file (%s) appears to be empty.", filename.c_str() );
		return( (xmlNodePtr)NULL );
	}
	
	tokenized = TokenizedString(path, tokens);
	iter = tokenized.begin();

	// The root is optional since it isn't a Child.
	if( !xmlStrcmp(cur->name, (const xmlChar *)(tokenized.front().c_str()) ) ) {
		++iter;
	}

	// Walk the tokenized path
	// If FirstChildNamed() doesn't find the path, it will return NULL
	for(; iter != tokenized.end() && cur != NULL; ++iter) {
		partialPath = *iter;
		if( partialPath.find_first_of(tokens) != string::npos) {
			continue;
		}
		parent = cur;
		cur = FirstChildNamed(parent, partialPath.c_str());
		if( (createIfMissing) && (cur==NULL) )
		{
			cur = xmlNewChild(parent, NULL, BAD_CAST partialPath.c_str(), NULL );
		}
	}

	// Memoize this result for later
	values[path] = cur;

	return( cur );
}
Example #9
0
int Lua::ConvertFromXML( lua_State *L, xmlDocPtr doc, xmlNodePtr node )
{
    string key;
    string type;
    string keytype;


    keytype = (const char *)xmlGetProp(node, BAD_CAST "keytype");
    type    = (const char *)xmlGetProp(node, BAD_CAST "type");

    // Process the Key
    if(keytype == "number") {
        lua_pushnumber(L, atof( (const char *)xmlGetProp(node, BAD_CAST "key") ) );
    } else if(keytype == "string") {
        lua_pushstring(L, (const char *)xmlGetProp(node, BAD_CAST "key"));
    } else {
        LogMsg(WARN, "Unknown Lua/XML conversion type '%s'", type.c_str());
        return 0;
    }

    // Process the Value
    if( type == "number") {
        float value = NodeToFloat(doc,node);
        lua_pushnumber(L, value);
    } else if( type == "string") {
        string value = NodeToString( doc, node );
        lua_pushstring(L, value.c_str());
    } else if( type == "boolean") {
        string value = NodeToString( doc, node );
        if (value == "true") {
            lua_pushboolean(L, 1);
        } else if (value == "false") {
            lua_pushboolean(L, 0);
        } else {
            LogMsg(WARN, "Unknown value '%s' for type '%s'", value.c_str(), type.c_str());
            assert(0);
        }

    } else if( type == "table") {
        int tableIndex;
        lua_newtable(L);
        tableIndex = lua_gettop(L);
        // Create a table
        for( node = FirstChildNamed(node, "value"); node!=NULL; node = NextSiblingNamed(node, "value") ) {
            Lua::ConvertFromXML(L, doc, node);
            assert(lua_istable(L, tableIndex));
            lua_settable(L, tableIndex); // Pops the name and value.
            assert( tableIndex == lua_gettop(L) ); // The table should be at the top now.
        }
    } else {
        LogMsg(WARN, "Unknown Lua/XML conversion type '%s'", type.c_str());
        return 1; // Just Key
    }
    return 2; // Key, Value
}
Example #10
0
/**\brief For parsing XML file into fields.
 */
bool Model::FromXMLNode( xmlDocPtr doc, xmlNodePtr node ) {
	xmlNodePtr  attr;
	string value;

	if( (attr = FirstChildNamed(node,"image")) ){
		image = Image::Get( NodeToString(doc,attr) );
		Image::Store(name, image);
		SetPicture(image);
	} else return false;

	if( (attr = FirstChildNamed(node,"mass")) ){
		value = NodeToString(doc,attr);
		SetMass( static_cast<float> (atof( value.c_str() )));
	} else return false;

	if( (attr = FirstChildNamed(node,"rotationsPerSecond")) ){
		value = NodeToString(doc,attr);
		SetRotationsPerSecond( static_cast<float>(atof( value.c_str() )));
	} else return false;

	if( (attr = FirstChildNamed(node,"thrustOffset")) ){
		value = NodeToString(doc,attr);
		thrustOffset = static_cast<short>(atoi( value.c_str() ));
	} else return false;

	if( (attr = FirstChildNamed(node,"maxSpeed")) ){
		value = NodeToString(doc,attr);
		SetMaxSpeed( static_cast<float>(atof( value.c_str() )));
	} else return false;

	if( (attr = FirstChildNamed(node,"msrp")) ){
		value = NodeToString(doc,attr);
		SetMSRP( (short int)atoi( value.c_str() ));
	} else return false;

	if( (attr = FirstChildNamed(node,"cargoSpace")) ){
		value = NodeToString(doc,attr);
		SetCargoSpace( atoi( value.c_str() ));
	} else return false;

	if( (attr = FirstChildNamed(node,"hullStrength")) ){
		value = NodeToString(doc,attr);
		SetHullStrength( (short)atoi( value.c_str() ));
	} else return false;

	return true;
}
Example #11
0
xmlNodePtr XMLFile::FindNode( const string& path, bool createIfMissing ) {
	xmlNodePtr cur,parent;
	queue<string> tokenized;
	string partialPath;

	// Check previously memoized values
	map<string,xmlNodePtr>::iterator val = values.find( path );
	if( val != values.end() ){ // If we found it
		// Check that we don't return memoized NULL values when instructed to createIfMissing.
		if( !((val->second==NULL) && createIfMissing) )
		{
			return val->second;
		}
	}

	// Initialize the xml navigation cursor
	cur = xmlDocGetRootElement( xmlPtr );
	if( cur == NULL ) {
		LogMsg(WARN, "XML file (%s) appears to be empty.",filename.c_str() );
		return( (xmlNodePtr)NULL );
	}
	
	tokenized = TokenizedPath(path);

	// The root is optional since it isn't a Child.
	if( !xmlStrcmp(cur->name, (const xmlChar *)(tokenized.front().c_str()) ) ) {
		tokenized.pop();
	}

	// Walk the tokenized path
	// If FirstChildNamed() doesn't find the path, it will return NULL
	while( !tokenized.empty() && cur != NULL) {
		partialPath = tokenized.front();
		//printf("XML: '%s' @ '%s'\n", partialPath.c_str(), cur->name);
		tokenized.pop();
		parent = cur;
		cur = FirstChildNamed(parent, partialPath.c_str());
		if( (createIfMissing) && (cur==NULL) )
		{
			cur = xmlNewChild(parent, NULL, BAD_CAST partialPath.c_str(), BAD_CAST "" );
		}
	}

	// Memoize this result for later
	values.insert(make_pair(path,cur));

	return( cur );
}
Example #12
0
bool Components::ParseXMLNode( xmlDocPtr doc, xmlNodePtr node )
{
	xmlNodePtr  attr;
	Component* component = newComponent();

	// All Compoents must have names!
	if( (attr = FirstChildNamed(node,"name")) ){
		component->SetName(NodeToString(doc,attr));
	} else {
		LogMsg(ERR,"Failed to find a name attribute for the %s node at line %d.\n", NodeToString(doc,attr).c_str(), xmlGetLineNo(node) );
		return false;
	}

	if( component->FromXMLNode(doc, node) ){
		Add( component );
		return true;
	}
	return false;
}
Example #13
0
/**\brief Parser to parse the XML file
 */
bool Engine::FromXMLNode( xmlDocPtr doc, xmlNodePtr node ) {
	xmlNodePtr  attr;
	string value;

	if( (attr = FirstChildNamed(node,"forceOutput")) ){
		value = NodeToString(doc,attr);
		SetForceOutput( static_cast<float> (atof( value.c_str() )));
	} else return false;

	if( (attr = FirstChildNamed(node,"msrp")) ){
		value = NodeToString(doc,attr);
		SetMSRP( (short int)atoi( value.c_str() ));
	} else return false;

	if( (attr = FirstChildNamed(node,"foldDrive")) ){
		value = NodeToString(doc,attr);
		foldDrive = (atoi( value.c_str() ) != 0);
	} else return false;

	if( (attr = FirstChildNamed(node,"flareAnimation")) ){
		flareAnimation = NodeToString(doc,attr);
	} else return false;

	if( (attr = FirstChildNamed(node,"thrustSound")) ){
		thrustsound = Sound::Get( NodeToString(doc,attr) );
	} else return false;

	if( (attr = FirstChildNamed(node,"picName")) ){
		Image* pic = Image::Get( NodeToString(doc,attr) );
		// This image can be accessed by either the path or the Engine Name
		Image::Store(name, pic);
		SetPicture(pic);
	} else return false;

	return true;
}
Example #14
0
/**\brief Parse one player out of an xml node.
 */
bool Planet::FromXMLNode( xmlDocPtr doc, xmlNodePtr node ) {
	xmlNodePtr  attr;
	string value;
	Coordinate pos;

	if( (attr = FirstChildNamed(node,"alliance")) ){
		value = NodeToString(doc,attr);
		alliance = Alliances::Instance()->GetAlliance(value);
		if(alliance==NULL)
		{
			LogMsg(ERR, "Could not create Planet '%s'. Unknown Alliance '%s'.", this->GetName().c_str(), value.c_str());
			return false;
		}
	} else return false;

	if( (attr = FirstChildNamed(node,"x")) ){
		value = NodeToString(doc,attr);
		pos.SetX( atof( value.c_str() ));
	} else return false;

	if( (attr = FirstChildNamed(node,"y")) ){
		value = NodeToString(doc,attr);
		pos.SetY( atof( value.c_str() ));
	} else return false;

	SetWorldPosition( pos );

	if( (attr = FirstChildNamed(node,"landable")) ){
		value = NodeToString(doc,attr);
		landable = ( atoi( value.c_str() ) != 0);
	} else return false;

	if( (attr = FirstChildNamed(node,"traffic")) ){
		value = NodeToString(doc,attr);
		traffic = (short int) atoi( value.c_str() );
	} else return false;

	if( (attr = FirstChildNamed(node,"image")) ){
		Image* image = Image::Get( NodeToString(doc,attr) );
		Image::Store(name, image);
		SetImage(image);
	} else return false;

	if( (attr = FirstChildNamed(node,"militia")) ){
		value = NodeToString(doc,attr);
		militiaSize = (short int) atoi( value.c_str() );
	} else return false;

	if( (attr = FirstChildNamed(node,"sphereOfInfluence")) ){
		value = NodeToString(doc,attr);
		sphereOfInfluence = atoi( value.c_str() );
	} else return false;

	for( attr = FirstChildNamed(node,"technology"); attr!=NULL; attr = NextSiblingNamed(attr,"technology") ){
		value = NodeToString(doc,attr);
		Technology *tech = Technologies::Instance()->GetTechnology( value );
		technologies.push_back(tech);
	}
	technologies.unique();

	return true;
}
Example #15
0
/**\brief Parses weapon information
 */
bool Weapon::FromXMLNode( xmlDocPtr doc, xmlNodePtr node ) {
	xmlNodePtr  attr;
	string value;

	if( (attr = FirstChildNamed(node,"weaponType")) ){
		weaponType = (short int)NodeToInt(doc,attr);
	} else {
		LogMsg(ERR,"Could not find child node weaponType while searching component");
		return false;
	}

	if( (attr = FirstChildNamed(node,"imageName")) ){
		image = Image::Get( NodeToString(doc,attr) );
	} else {
		LogMsg(ERR,"Could not find child node imageName while searching component");
		return false;
	}

	if( (attr = FirstChildNamed(node,"picName")) ){
		Image* pic = Image::Get( NodeToString(doc,attr) );
		// This image can be accessed by either the path or the Weapon Name
		Image::Store(name, pic);
		SetPicture(pic);
	} else {
		LogMsg(ERR,"Could not find child node picName while searching component");
		return false;
	}

	if( (attr = FirstChildNamed(node,"description")) ){
		value = NodeToString(doc,attr);
		SetDescription( value );
	} else {
		LogMsg( WARN, "%s does not have a description.", GetName().c_str() );
	}

	if( (attr = FirstChildNamed(node,"payload")) ){
		payload = NodeToInt(doc,attr);
	} else {
		LogMsg(ERR,"Could not find child node payload while searching component");
		return false;
	}

	if( (attr = FirstChildNamed(node,"velocity")) ){
		velocity = NodeToInt(doc,attr);
	} else {
		LogMsg(ERR,"Could not find child node velocity while searching component");
		return false;
	}

	if( (attr = FirstChildNamed(node,"acceleration")) ){
		acceleration = NodeToInt(doc,attr);
	} else {
		LogMsg(ERR,"Could not find child node acceleration while searching component");
		return false;
	}

	if( (attr = FirstChildNamed(node,"ammoType")) ){
		value = NodeToString(doc,attr);
		ammoType = AmmoNameToType(value);
		if(ammoType>=max_ammo) {
			LogMsg(ERR,"ammoType is >= max_ammo in Weapons XML parsing");
			return false;
		}
	} else {
		LogMsg(ERR,"Could not find child node ammoType while searching component");
		return false;
	}

	if( (attr = FirstChildNamed(node,"ammoConsumption")) ){
		ammoConsumption = NodeToInt(doc,attr);
	} else {
		LogMsg(ERR,"Could not find child node ammoConsumption while searching component");
		return false;
	}

	if( (attr = FirstChildNamed(node, "fireDelay")) ) {
		fireDelay = NodeToInt(doc,attr);
	} else {
		LogMsg(ERR, "Could not find child node fireDelay while searching component");
		return false;
	}

	if( (attr = FirstChildNamed(node, "lifetime")) ) {
		lifetime = NodeToInt(doc,attr);
	} else {
		LogMsg(ERR, "Could not find child node lifetime while searching component");
		return false;
	}

	if( (attr = FirstChildNamed(node,"tracking")) ) {
		float _tracking = NodeToFloat(doc,attr);
		if (_tracking > 1.0f ) _tracking = 1.0f;
		if (_tracking < 0.0001f ) _tracking = 0.0f;
		tracking = _tracking;
	} else {
		LogMsg(ERR, "Could not find child node tracking while searching component");
		return false;
	}

	if( (attr = FirstChildNamed(node, "msrp")) ) {
		value = NodeToString(doc,attr);
		SetMSRP( (short int)atoi( value.c_str() ));
	} else {
		LogMsg(ERR, "Could not find child node msrp while searching component");
		return false;
	}

	if( (attr = FirstChildNamed(node, "sound")) ) {
		value = NodeToString(doc,attr);
		this->sound = Sound::Get( value );
		if( this->sound == NULL) {
			// Do not return false here - they may be disabling audio on purpose or audio may not be supported on their system
			LogMsg(WARN, "Could not load sound file while searching component");
		}
	} else {
		LogMsg(ERR, "Could not find child node sound while searching component");
		return false;
	}

	return true;
}
Example #16
0
/**\brief Load an XML file
 * \arg filename The XML file that should be parsed.
 * \arg optional  If this is true, an error is not returned if the file doesn't exist.
 */
bool Components::Load(string filename, bool optional) {
	xmlDocPtr doc;
	xmlNodePtr cur, ver;
	int versionMajor = 0, versionMinor = 0, versionMacro = 0;
	int numObjs = 0;
	bool success = true;
	
	File xmlfile = File (filename);
	long filelen = xmlfile.GetLength();
	char *buffer = xmlfile.Read();
	doc = xmlParseMemory( buffer, static_cast<int>(filelen) );
	delete [] buffer;

	if( doc == NULL ) {
		LogMsg(ERR, "Could not load '%s' for parsing.", filename.c_str() );
		return optional;
	}
	
	cur = xmlDocGetRootElement( doc );
	
	if( cur == NULL ) {
		LogMsg(ERR, "'%s' file appears to be empty.", filename.c_str() );
		xmlFreeDoc( doc );
		return false;
	}
	
	if( xmlStrcmp( cur->name, (const xmlChar *)rootName.c_str() ) ) {
		LogMsg(ERR, "'%s' appears to be invalid. Root element was %s.", filename.c_str(), (char *)cur->name );
		xmlFreeDoc( doc );
		return false;
	} else {
		LogMsg(INFO, "'%s' file found and valid, parsing...", filename.c_str() );
	}
	
	// Get the version number
	if( (ver = FirstChildNamed(cur, "version-major")) != NULL ) {
		versionMajor = NodeToInt(doc,ver);
	}
	if( (ver = FirstChildNamed(cur, "version-minor")) != NULL ) {
		versionMinor = NodeToInt(doc,ver);
	}
	if( (ver = FirstChildNamed(cur, "version-macro")) != NULL ) {
		versionMacro = NodeToInt(doc,ver);
	}
	if( ( versionMajor != EPIAR_VERSION_MAJOR ) ||
	    ( versionMinor != EPIAR_VERSION_MINOR ) ||
	    ( versionMacro != EPIAR_VERSION_MICRO ) ) {
		LogMsg(WARN, "File '%s' is version %d.%d.%d. This may cause problems since it does not match the current version %d.%d.%d.",
			filename.c_str(),
			versionMajor, versionMinor, versionMacro,
			EPIAR_VERSION_MAJOR, EPIAR_VERSION_MINOR, EPIAR_VERSION_MICRO );
	}
	
	// Get the components
	cur = cur->xmlChildrenNode;
	while( success && cur != NULL ) {
		// Parse for the version information and any children nodes
		if( ( !xmlStrcmp( cur->name, BAD_CAST componentName.c_str() ) ) ) {
			// Parse a Component
			success = ParseXMLNode( doc, cur );
			assert(success || optional);
			if(success) numObjs++;
		}
		
		cur = cur->next;
	}
	
	xmlFreeDoc( doc );

	filepath = filename;
	
	LogMsg(INFO, "Parsing of file '%s' done, found %d objects. File is version %d.%d.%d.", filename.c_str(), numObjs, versionMajor, versionMinor, versionMacro );
	return success;
}
Example #17
0
/**\brief Parses weapon information
 */
bool Outfit::FromXMLNode( xmlDocPtr doc, xmlNodePtr node ) {
	xmlNodePtr  attr;
	string value;

	if( (attr = FirstChildNamed(node,"msrp")) ){
		value = NodeToString(doc,attr);
		SetMSRP( atoi( value.c_str() ));
	} else return false;

	if( (attr = FirstChildNamed(node,"picName")) ){
		Image* pic = Image::Get( NodeToString(doc,attr) );
		// This image can be accessed by either the path or the Engine Name
		Image::Store(name, pic);
		SetPicture(pic);
	} else return false;

	if( (attr = FirstChildNamed(node,"rotationsPerSecond")) ){
		value = NodeToString(doc,attr);
		SetRotationsPerSecond( static_cast<float>(atof( value.c_str() )));
	}

	if( (attr = FirstChildNamed(node,"maxSpeed")) ){
		value = NodeToString(doc,attr);
		SetMaxSpeed( static_cast<float>(atof( value.c_str() )));
	}

	if( (attr = FirstChildNamed(node,"forceOutput")) ){
		value = NodeToString(doc,attr);
		SetForceOutput( static_cast<float> (atof( value.c_str() )));
	}

	if( (attr = FirstChildNamed(node,"mass")) ){
		value = NodeToString(doc,attr);
		SetMass( static_cast<float> (atof( value.c_str() )));
	}

	if( (attr = FirstChildNamed(node,"cargoSpace")) ){
		value = NodeToString(doc,attr);
		SetCargoSpace( atoi( value.c_str() ));
	}

	if( (attr = FirstChildNamed(node,"surfaceArea")) ){
		value = NodeToString(doc,attr);
		SetSurfaceArea( atoi( value.c_str() ));
	}

	if( (attr = FirstChildNamed(node,"cargoSpace")) ){
		value = NodeToString(doc,attr);
		SetCargoSpace( atoi( value.c_str() ));
	}

	if( (attr = FirstChildNamed(node,"hullStrength")) ){
		value = NodeToString(doc,attr);
		SetHullStrength( atoi( value.c_str() ));
	}

	if( (attr = FirstChildNamed(node,"shildStrength")) ){
		value = NodeToString(doc,attr);
		SetShieldStrength( (short)atoi( value.c_str() ));
	}

	return true;
}
Example #18
0
/**\brief For parsing XML file into fields.
 */
bool Model::FromXMLNode( xmlDocPtr doc, xmlNodePtr node ) {
	xmlNodePtr  attr;
	string value;

	if( (attr = FirstChildNamed(node,"image")) ){
		image = Image::Get( NodeToString(doc,attr) );
		Image::Store(name, image);
		SetPicture(image);
	} else return false;

	if( (attr = FirstChildNamed(node,"mass")) ){
		value = NodeToString(doc,attr);
		SetMass( static_cast<float> (atof( value.c_str() )));
	} else return false;

	if( (attr = FirstChildNamed(node,"rotationsPerSecond")) ){
		value = NodeToString(doc,attr);
		SetRotationsPerSecond( static_cast<float>(atof( value.c_str() )));
	} else return false;

	if( (attr = FirstChildNamed(node,"thrustOffset")) ){
		value = NodeToString(doc,attr);
		thrustOffset = static_cast<short>(atoi( value.c_str() ));
	} else return false;

	if( (attr = FirstChildNamed(node,"maxSpeed")) ){
		value = NodeToString(doc,attr);
		SetMaxSpeed( static_cast<float>(atof( value.c_str() )));
	} else return false;

	if( (attr = FirstChildNamed(node,"msrp")) ){
		value = NodeToString(doc,attr);
		SetMSRP( (short int)atoi( value.c_str() ));
	} else return false;

	if( (attr = FirstChildNamed(node,"cargoSpace")) ){
		value = NodeToString(doc,attr);
		SetCargoSpace( atoi( value.c_str() ));
	} else return false;

	if( (attr = FirstChildNamed(node,"hullStrength")) ){
		value = NodeToString(doc,attr);
		SetHullStrength( (short)atoi( value.c_str() ));
	} else return false;

	if( (attr = FirstChildNamed(node,"shieldStrength")) ){
		value = NodeToString(doc,attr);
		SetShieldStrength( (short)atoi( value.c_str() ));
	} else return false;

	if( (attr = FirstChildNamed(node,"weaponSlots")) ){
		// pass the weaponSlots XML node into a handler function
		ConfigureWeaponSlots( doc, attr );
	} else {
		cout << "Model::FromXMLNode(): Did not find weapon slot configuration - assuming defaults." << endl;
		// with no parameters, it sets default values
		ConfigureWeaponSlots();
	}

	return true;
}
Example #19
0
/**\brief For parsing XML file into fields.
 */
bool Model::FromXMLNode( xmlDocPtr doc, xmlNodePtr node ) {
	xmlNodePtr attr;
	string value;

	if( (attr = FirstChildNamed(node,"image")) ){
		image = Image::Get( NodeToString(doc,attr) );
		Image::Store(name, image);
		SetPicture(image);
	} else return false;

	if( (attr = FirstChildNamed(node,"description")) ){
		value = NodeToString(doc,attr);
		SetDescription( value );
	} else {
		LogMsg( WARN, "%s does not have a description.", GetName().c_str() );
	}

	if( (attr = FirstChildNamed(node,"engine")) ){
		defaultEngine = Menu::GetCurrentScenario()->GetEngines()->GetEngine( NodeToString(doc,attr) );
	} else return false;

	if( (attr = FirstChildNamed(node,"mass")) ){
		value = NodeToString(doc,attr);
		SetMass( static_cast<float> (atof( value.c_str() )));
	} else return false;

	if( (attr = FirstChildNamed(node,"rotationsPerSecond")) ){
		value = NodeToString(doc,attr);
		SetRotationsPerSecond( static_cast<float>(atof( value.c_str() )));
	} else return false;

	if( (attr = FirstChildNamed(node,"thrustOffset")) ){
		value = NodeToString(doc,attr);
		thrustOffset = static_cast<short>(atoi( value.c_str() ));
	} else return false;

	if( (attr = FirstChildNamed(node,"maxSpeed")) ){
		value = NodeToString(doc,attr);
		SetMaxSpeed( static_cast<float>(atof( value.c_str() )));
	} else return false;

	if( (attr = FirstChildNamed(node,"msrp")) ){
		value = NodeToString(doc,attr);
		SetMSRP( (short int)atoi( value.c_str() ));
	} else return false;

	if( (attr = FirstChildNamed(node,"cargoSpace")) ){
		value = NodeToString(doc,attr);
		SetCargoSpace( atoi( value.c_str() ));
	} else return false;

	if( (attr = FirstChildNamed(node,"hullStrength")) ){
		value = NodeToString(doc,attr);
		SetHullStrength( (short)atoi( value.c_str() ));
	} else return false;

	if( (attr = FirstChildNamed(node,"shieldStrength")) ){
		value = NodeToString(doc,attr);
		SetShieldStrength( (short)atoi( value.c_str() ));
	} else return false;

	if( (attr = FirstChildNamed(node,"weaponSlots")) ){
		// pass the weaponSlots XML node into a handler function
		ConfigureWeaponSlots( doc, attr );
	} else {
		//LogMsg( WARN, "Did not find weapon slot configuration - ship cannot have weapons.");
	}

	return true;
}
Example #20
0
/**\brief Parse one player out of an xml node
 */
bool Player::FromXMLNode( xmlDocPtr doc, xmlNodePtr node ) {
	xmlNodePtr  attr;
	string value;
	Coordinate pos;

	if( (attr = FirstChildNamed(node,"name")) ){
		SetName(NodeToString(doc,attr));
	}

	if( (attr = FirstChildNamed(node, "planet"))){
		string temp;
		xmlNodePtr name = FirstChildNamed(attr,"name");
		lastPlanet = NodeToString(doc,name);
		Planet* p = Planets::Instance()->GetPlanet( lastPlanet );
		if( p != NULL ) {
			SetWorldPosition( p->GetWorldPosition() );
		}
	}else return false;

	if( (attr = FirstChildNamed(node,"model")) ){
		value = NodeToString(doc,attr);
		Model* model = Models::Instance()->GetModel( value );
		if( NULL!=model) {
			SetModel( model );
		} else {
			LogMsg(ERR,"No such model as '%s'", value.c_str());
			return false;
		}
	} else return false;
	
	if( (attr = FirstChildNamed(node,"engine")) ){
		value = NodeToString(doc,attr);
		Engine* engine = Engines::Instance()->GetEngine( value );
		if( NULL!=engine) {
			SetEngine( engine );
		} else {
			LogMsg(ERR,"No such engine as '%s'", value.c_str());
			return false;
		}
	} else return false;


	if( (attr = FirstChildNamed(node,"credits")) ){
		value = NodeToString(doc,attr);
		SetCredits( atoi(value.c_str()) );
	} else return false;

	for( attr = FirstChildNamed(node,"weapon"); attr!=NULL; attr = NextSiblingNamed(attr,"weapon") ){
		AddShipWeapon( NodeToString(doc,attr) );
	}

	for( attr = FirstChildNamed(node,"outfit"); attr!=NULL; attr = NextSiblingNamed(attr,"outfit") ){
		AddOutfit( NodeToString(doc,attr) );
	}

	for( attr = FirstChildNamed(node,"cargo"); attr!=NULL; attr = NextSiblingNamed(attr,"cargo") ){
		xmlNodePtr type = FirstChildNamed(attr,"type");
		xmlNodePtr ammt = FirstChildNamed(attr,"amount");
		if(!type || !ammt)
			return false;
		if( NodeToInt(doc,ammt) > 0 )
		{
			StoreCommodities( NodeToString(doc,type), NodeToInt(doc,ammt) );
		}
	}

	for( attr = FirstChildNamed(node,"ammo"); attr!=NULL; attr = NextSiblingNamed(attr,"ammo") ){
		xmlNodePtr type = FirstChildNamed(attr,"type");
		xmlNodePtr ammt = FirstChildNamed(attr,"amount");
		if(!type || !ammt)
			return false;
		AmmoType ammoType = Weapon::AmmoNameToType( NodeToString(doc,type) );
		int ammoCount = NodeToInt(doc,ammt);
		if( ammoType < max_ammo ) {
			AddAmmo( ammoType, ammoCount );
		} else return false;
	}

	for( attr = FirstChildNamed(node,"Mission"); attr!=NULL; attr = NextSiblingNamed(attr,"Mission") ){
		Mission *mission = Mission::FromXMLNode(doc,attr);
		if( mission != NULL ) {
			LogMsg(INFO, "Successfully loaded the %s mission of player '%s'", mission->GetName().c_str(), this->GetName().c_str() );
			missions.push_back( mission );
		} else {
			LogMsg(INFO, "Aborted loading mission of player '%s'", this->GetName().c_str() );
		}
	}

	for( attr = FirstChildNamed(node,"favor"); attr!=NULL; attr = NextSiblingNamed(attr,"favor") ){
		xmlNodePtr alliance = FirstChildNamed(attr,"alliance");
		xmlNodePtr value = FirstChildNamed(attr,"value");
		if(!alliance || !value)
			return false;
		if( NodeToInt(doc,value) > 0 )
		{
			UpdateFavor( NodeToString(doc,alliance), NodeToInt(doc,value) );
		}
	}

	for( attr = FirstChildNamed(node,"hiredEscort"); attr!=NULL; attr = NextSiblingNamed(attr,"hiredEscort") ){
		xmlNodePtr typePtr = FirstChildNamed(attr, "type");
		xmlNodePtr payPtr = FirstChildNamed(attr, "pay");
		assert(typePtr && payPtr);
		if(!typePtr || !payPtr) return false;
		string type = NodeToString(doc, typePtr);
		int pay = atoi( NodeToString(doc, payPtr).c_str() );
		// Adding it with sprite ID -1 means it's up to player.lua to go ahead and create the correct sprite.
		this->AddHiredEscort(type, pay, -1);
	}

	if(this->ConfigureWeaponSlots(doc, node)){
		// great - it worked
		LogMsg( INFO, "Successfully loaded weapon slots");
	}
	else {
		LogMsg( ERR, "Weapon slot XML helper failed to configure weapon slots");
	}

	if( (attr = FirstChildNamed(node,"lastLoadTime")) ){
		lastLoadTime = NodeToInt(doc,attr);
	} else {
		lastLoadTime = (time_t)0;
	}

	RemoveLuaControlFunc();

	return true;
}