Exemplo n.º 1
0
void Universe::serialize(Archive& ar, const unsigned int version)
{
    ObjectMap                       objects;
    std::set<int>                   destroyed_object_ids;
    EmpireObjectMap                 empire_latest_known_objects;
    EmpireObjectVisibilityMap       empire_object_visibility;
    EmpireObjectVisibilityTurnMap   empire_object_visibility_turns;
    ObjectKnowledgeMap              empire_known_destroyed_object_ids;
    ObjectKnowledgeMap              empire_stale_knowledge_object_ids;
    ShipDesignMap                   ship_designs;

    ar.template register_type<System>();

    if (Archive::is_saving::value) {
        DebugLogger() << "Universe::serialize : Getting gamestate data";
        GetObjectsToSerialize(              objects,                            m_encoding_empire);
        GetDestroyedObjectsToSerialize(     destroyed_object_ids,               m_encoding_empire);
        GetEmpireKnownObjectsToSerialize(   empire_latest_known_objects,        m_encoding_empire);
        GetEmpireObjectVisibilityMap(       empire_object_visibility,           m_encoding_empire);
        GetEmpireObjectVisibilityTurnMap(   empire_object_visibility_turns,     m_encoding_empire);
        GetEmpireKnownDestroyedObjects(     empire_known_destroyed_object_ids,  m_encoding_empire);
        GetEmpireStaleKnowledgeObjects(     empire_stale_knowledge_object_ids,  m_encoding_empire);
        GetShipDesignsToSerialize(          ship_designs,                       m_encoding_empire);
    }

    if (Archive::is_loading::value) {
        Clear();    // clean up any existing dynamically allocated contents before replacing containers with deserialized data
    }

    DebugLogger() << "Universe::serialize : (de)serializing universe width";
    ar  & BOOST_SERIALIZATION_NVP(m_universe_width);
    DebugLogger() << "Universe::serialize : (de)serializing ship designs";
    ar  & BOOST_SERIALIZATION_NVP(ship_designs);
    ar  & BOOST_SERIALIZATION_NVP(m_empire_known_ship_design_ids);
    DebugLogger() << "Universe::serialize : (de)serializing empire object visibility";
    ar  & BOOST_SERIALIZATION_NVP(empire_object_visibility);
    ar  & BOOST_SERIALIZATION_NVP(empire_object_visibility_turns);
    ar  & BOOST_SERIALIZATION_NVP(empire_known_destroyed_object_ids);
    ar  & BOOST_SERIALIZATION_NVP(empire_stale_knowledge_object_ids);
    DebugLogger() << "Universe::serialize : (de)serializing actual objects";
    ar  & BOOST_SERIALIZATION_NVP(objects)
        & BOOST_SERIALIZATION_NVP(destroyed_object_ids);
    DebugLogger() << "Universe::serialize : (de)serializing empire known objects";
    ar  & BOOST_SERIALIZATION_NVP(empire_latest_known_objects);
    DebugLogger() << "Universe::serialize : (de)serializing last allocated ids";
    ar  & BOOST_SERIALIZATION_NVP(m_last_allocated_object_id);
    ar  & BOOST_SERIALIZATION_NVP(m_last_allocated_design_id);
    DebugLogger() << "Universe::serialize : (de)serializing stats";
    ar  & BOOST_SERIALIZATION_NVP(m_stat_records);
    DebugLogger() << "Universe::serialize : (de)serializing done";

    if (Archive::is_saving::value) {
        DebugLogger() << "Universe::serialize : Cleaning up temporary data";
        // clean up temporary objects in temporary ObjectMaps
        objects.Clear();
        for (EmpireObjectMap::iterator it = empire_latest_known_objects.begin();
             it != empire_latest_known_objects.end(); ++it)
        { it->second.Clear(); }
    }

    if (Archive::is_loading::value) {
        DebugLogger() << "Universe::serialize : Swapping old/new data, with Encoding Empire "
                               << EncodingEmpire();
        m_objects.swap(objects);
        m_destroyed_object_ids.swap(destroyed_object_ids);
        m_empire_latest_known_objects.swap(empire_latest_known_objects);
        m_empire_object_visibility.swap(empire_object_visibility);
        m_empire_object_visibility_turns.swap(empire_object_visibility_turns);
        m_empire_known_destroyed_object_ids.swap(empire_known_destroyed_object_ids);
        m_empire_stale_knowledge_object_ids.swap(empire_stale_knowledge_object_ids);
        m_ship_designs.swap(ship_designs);
        m_objects.UpdateCurrentDestroyedObjects(m_destroyed_object_ids);

        for (EmpireObjectMap::iterator it = m_empire_latest_known_objects.begin();
             it != m_empire_latest_known_objects.end(); it++)
        {
            std::map< int, std::set< int > >::iterator destroyed_ids_it =
                m_empire_known_destroyed_object_ids.find(it->first);
            if (destroyed_ids_it != m_empire_known_destroyed_object_ids.end())
                it->second.UpdateCurrentDestroyedObjects(destroyed_ids_it->second);
        }
    }
}
Exemplo n.º 2
0
ObjectMap* ObjectMap::Clone(int empire_id) const {
    ObjectMap* result = new ObjectMap();
    result->Copy(*this, empire_id);
    return result;
}