/**\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; }
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 }
/**\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; }
/**\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; }
/**\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; }