예제 #1
0
bool mapbuffer::add_submap( int x, int y, int z, std::unique_ptr<submap> &sm )
{
    return add_submap( tripoint( x, y, z ), sm );
}
예제 #2
0
bool mapbuffer::add_submap( const tripoint &p, std::unique_ptr<submap> &sm )
{
    const bool result = add_submap( p, sm.get() );
    sm.release();
    return result;
}
예제 #3
0
// We're reading in way too many entities here to mess around with creating sub-objects and
// seeking around in them, so we're using the json streaming API.
submap *mapbuffer::unserialize_submaps( const tripoint &p )
{
    // Map the tripoint to the submap quad that stores it.
    const tripoint om_addr = overmapbuffer::sm_to_omt_copy( p );
    const tripoint segment_addr = overmapbuffer::omt_to_seg_copy( om_addr );
    std::stringstream quad_path;
    quad_path << world_generator->active_world->world_path << "/maps/" <<
        segment_addr.x << "." << segment_addr.y << "." << segment_addr.z << "/" <<
        om_addr.x << "." << om_addr.y << "." << om_addr.z << ".map";

    std::ifstream fin( quad_path.str().c_str() );
    if( !fin.is_open() ) {
        // If it doesn't exist, trigger generating it.
        return NULL;
    }

    JsonIn jsin( fin );
    jsin.start_array();
    while( !jsin.end_array() ) {
        std::unique_ptr<submap> sm(new submap());
        tripoint submap_coordinates;
        jsin.start_object();
        while( !jsin.end_object() ) {
            std::string submap_member_name = jsin.get_member_name();
            if( submap_member_name == "version" ) {
                // We aren't using the version number for anything at the moment.
                jsin.skip_value();
            } else if( submap_member_name == "coordinates" ) {
                jsin.start_array();
                int locx = jsin.get_int();
                int locy = jsin.get_int();
                int locz = jsin.get_int();
                jsin.end_array();
                submap_coordinates = tripoint( locx, locy, locz );
            } else if( submap_member_name == "turn_last_touched" ) {
                sm->turn_last_touched = jsin.get_int();
            } else if( submap_member_name == "temperature" ) {
                sm->temperature = jsin.get_int();
            } else if( submap_member_name == "terrain" ) {
                // TODO: try block around this to error out if we come up short?
                jsin.start_array();
                for( int j = 0; j < SEEY; j++ ) {
                    for( int i = 0; i < SEEX; i++ ) {
                        sm->ter[i][j] = termap[ jsin.get_string() ].loadid;
                    }
                }
                jsin.end_array();
            } else if( submap_member_name == "radiation" ) {
                int rad_cell = 0;
                jsin.start_array();
                while( !jsin.end_array() ) {
                    int rad_strength = jsin.get_int();
                    int rad_num = jsin.get_int();
                    for( int i = 0; i < rad_num; ++i ) {
                        // A little array trick here, assign to it as a 1D array.
                        // If it's not in bounds we're kinda hosed anyway.
                        sm->set_radiation(0, rad_cell, rad_strength);
                        rad_cell++;
                    }
                }
            } else if( submap_member_name == "furniture" ) {
                jsin.start_array();
                while( !jsin.end_array() ) {
                    jsin.start_array();
                    int i = jsin.get_int();
                    int j = jsin.get_int();
                    sm->frn[i][j] = furnmap[ jsin.get_string() ].loadid;
                    jsin.end_array();
                }
            } else if( submap_member_name == "items" ) {
                jsin.start_array();
                while( !jsin.end_array() ) {
                    int i = jsin.get_int();
                    int j = jsin.get_int();
                    jsin.start_array();
                    while( !jsin.end_array() ) {
                        item tmp;
                        jsin.read( tmp );
                        sm->itm[i][j].push_back( tmp );
                    }
                }
            } else if( submap_member_name == "traps" ) {
                jsin.start_array();
                while( !jsin.end_array() ) {
                    jsin.start_array();
                    int i = jsin.get_int();
                    int j = jsin.get_int();
                    sm->trp[i][j] = trapmap[ jsin.get_string() ];
                    jsin.end_array();
                }
            } else if( submap_member_name == "fields" ) {
                jsin.start_array();
                while( !jsin.end_array() ) {
                    // Coordinates loop
                    int i = jsin.get_int();
                    int j = jsin.get_int();
                    jsin.start_array();
                    while( !jsin.end_array() ) {
                        int type = jsin.get_int();
                        int density = jsin.get_int();
                        int age = jsin.get_int();
                        if (sm->fld[i][j].findField(field_id(type)) == NULL) {
                            sm->field_count++;
                        }
                        sm->fld[i][j].addField(field_id(type), density, age);
                    }
                }
            } else if( submap_member_name == "griffiti" ) {
                jsin.start_array();
                while( !jsin.end_array() ) {
                    jsin.start_array();
                    int i = jsin.get_int();
                    int j = jsin.get_int();
                    sm->set_graffiti(i, j, graffiti( jsin.get_string() ));
                    jsin.end_array();
                }
            } else if(submap_member_name == "cosmetics") {
                jsin.start_array();
                while (!jsin.end_array()) {
                    jsin.start_array();
                    int i = jsin.get_int();
                    int j = jsin.get_int();
                    jsin.read(sm->cosmetics[i][j]);
                    jsin.end_array();
                }
            } else if( submap_member_name == "spawns" ) {
                jsin.start_array();
                while( !jsin.end_array() ) {
                    jsin.start_array();
                    std::string type = jsin.get_string();
                    int count = jsin.get_int();
                    int i = jsin.get_int();
                    int j = jsin.get_int();
                    int faction_id = jsin.get_int();
                    int mission_id = jsin.get_int();
                    bool friendly = jsin.get_bool();
                    std::string name = jsin.get_string();
                    jsin.end_array();
                    spawn_point tmp( type, count, i, j, faction_id, mission_id, friendly, name );
                    sm->spawns.push_back( tmp );
                }
            } else if( submap_member_name == "vehicles" ) {
                jsin.start_array();
                while( !jsin.end_array() ) {
                    vehicle *tmp = new vehicle();
                    jsin.read( *tmp );
                    sm->vehicles.push_back( tmp );
                }
            } else if( submap_member_name == "computers" ) {
                std::string computer_data = jsin.get_string();
                sm->comp.load_data( computer_data );
            } else if( submap_member_name == "camp" ) {
                std::string camp_data = jsin.get_string();
                sm->camp.load_data( camp_data );
            } else {
                jsin.skip_value();
            }
        }
        if( !add_submap( submap_coordinates, sm ) ) {
            debugmsg( "submap %d,%d,%d was alread loaded", submap_coordinates.x, submap_coordinates.y, submap_coordinates.z );
        }
    }
    if( submaps.count( p ) == 0 ) {
        debugmsg("file %s did not contain the expected submap %d,%d,%d", quad_path.str().c_str(), p.x, p.y, p.z);
        return NULL;
    }
    return submaps[ p ];
}
예제 #4
0
bool mapbuffer::add_submap( int x, int y, int z, submap *sm )
{
    return add_submap( tripoint( x, y, z ), sm );
}
예제 #5
0
// We're reading in way too many entities here to mess around with creating sub-objects and
// seeking around in them, so we're using the json streaming API.
submap *mapbuffer::unserialize_submaps( const tripoint &p )
{
    // Map the tripoint to the submap quad that stores it.
    const tripoint om_addr = overmapbuffer::sm_to_omt_copy( p );
    const tripoint segment_addr = overmapbuffer::omt_to_seg_copy( om_addr );
    std::stringstream quad_path;
    quad_path << world_generator->active_world->world_path << "/maps/" <<
              segment_addr.x << "." << segment_addr.y << "." << segment_addr.z << "/" <<
              om_addr.x << "." << om_addr.y << "." << om_addr.z << ".map";

    std::ifstream fin( quad_path.str().c_str() );
    if( !fin.is_open() ) {
        // If it doesn't exist, trigger generating it.
        return NULL;
    }

    JsonIn jsin( fin );
    jsin.start_array();
    while( !jsin.end_array() ) {
        std::unique_ptr<submap> sm(new submap());
        tripoint submap_coordinates;
        jsin.start_object();
        bool rubpow_update = false;
        while( !jsin.end_object() ) {
            std::string submap_member_name = jsin.get_member_name();
            if( submap_member_name == "version" ) {
                if (jsin.get_int() < 22) {
                    rubpow_update = true;
                }
            } else if( submap_member_name == "coordinates" ) {
                jsin.start_array();
                int locx = jsin.get_int();
                int locy = jsin.get_int();
                int locz = jsin.get_int();
                jsin.end_array();
                submap_coordinates = tripoint( locx, locy, locz );
            } else if( submap_member_name == "turn_last_touched" ) {
                sm->turn_last_touched = jsin.get_int();
            } else if( submap_member_name == "temperature" ) {
                sm->temperature = jsin.get_int();
            } else if( submap_member_name == "terrain" ) {
                // TODO: try block around this to error out if we come up short?
                jsin.start_array();
                // Small duplication here so that the update check is only performed once
                if (rubpow_update) {
                    std::string ter_string;
                    item rock = item("rock", 0);
                    item chunk = item("steel_chunk", 0);
                    for( int j = 0; j < SEEY; j++ ) {
                        for( int i = 0; i < SEEX; i++ ) {
                            ter_string = jsin.get_string();
                            if (ter_string == "t_rubble") {
                                sm->ter[i][j] = termap[ "t_dirt" ].loadid;
                                sm->frn[i][j] = furnmap[ "f_rubble" ].loadid;
                                sm->itm[i][j].push_back( rock );
                                sm->itm[i][j].push_back( rock );
                            } else if (ter_string == "t_wreckage"){
                                sm->ter[i][j] = termap[ "t_dirt" ].loadid;
                                sm->frn[i][j] = furnmap[ "f_wreckage" ].loadid;
                                sm->itm[i][j].push_back( chunk );
                                sm->itm[i][j].push_back( chunk );
                            } else if (ter_string == "t_ash"){
                                sm->ter[i][j] = termap[ "t_dirt" ].loadid;
                                sm->frn[i][j] = furnmap[ "f_ash" ].loadid;
                            } else if (ter_string == "t_pwr_sb_support_l"){
                                sm->ter[i][j] = termap[ "t_support_l" ].loadid;
                            } else if (ter_string == "t_pwr_sb_switchgear_l"){
                                sm->ter[i][j] = termap[ "t_switchgear_l" ].loadid;
                            } else if (ter_string == "t_pwr_sb_switchgear_s"){
                                sm->ter[i][j] = termap[ "t_switchgear_s" ].loadid;
                            } else {
                                sm->ter[i][j] = terfind( ter_string );
                            }
                        }
                    }
                } else {
                    for( int j = 0; j < SEEY; j++ ) {
                        for( int i = 0; i < SEEX; i++ ) {
                            sm->ter[i][j] = terfind( jsin.get_string() );
                        }
                    }
                }
                jsin.end_array();
            } else if( submap_member_name == "radiation" ) {
                int rad_cell = 0;
                jsin.start_array();
                while( !jsin.end_array() ) {
                    int rad_strength = jsin.get_int();
                    int rad_num = jsin.get_int();
                    for( int i = 0; i < rad_num; ++i ) {
                        // A little array trick here, assign to it as a 1D array.
                        // If it's not in bounds we're kinda hosed anyway.
                        sm->set_radiation(0, rad_cell, rad_strength);
                        rad_cell++;
                    }
                }
            } else if( submap_member_name == "furniture" ) {
                jsin.start_array();
                while( !jsin.end_array() ) {
                    jsin.start_array();
                    int i = jsin.get_int();
                    int j = jsin.get_int();
                    sm->frn[i][j] = furnmap[ jsin.get_string() ].loadid;
                    jsin.end_array();
                }
            } else if( submap_member_name == "items" ) {
                jsin.start_array();
                while( !jsin.end_array() ) {
                    int i = jsin.get_int();
                    int j = jsin.get_int();
                    jsin.start_array();
                    while( !jsin.end_array() ) {
                        item tmp;
                        jsin.read( tmp );

                        if( tmp.is_emissive() ) {
                            sm->update_lum_add(tmp, i, j);
                        }

                        tmp.visit_items([&sm,i,j]( item *it ) {
                            for( auto& e: it->magazine_convert() ) {
                                sm->itm[i][j].push_back( e );
                            }
                            return VisitResponse::NEXT;
                        } );

                        sm->itm[i][j].push_back( tmp );
                        if( tmp.needs_processing() ) {
                            sm->active_items.add( std::prev(sm->itm[i][j].end()), point( i, j ) );
                        }
                    }
                }
            } else if( submap_member_name == "traps" ) {
                jsin.start_array();
                while( !jsin.end_array() ) {
                    jsin.start_array();
                    int i = jsin.get_int();
                    int j = jsin.get_int();
                    // TODO: jsin should support returning an id like jsin.get_id<trap>()
                    sm->trp[i][j] = trap_str_id( jsin.get_string() );
                    jsin.end_array();
                }
            } else if( submap_member_name == "fields" ) {
                jsin.start_array();
                while( !jsin.end_array() ) {
                    // Coordinates loop
                    int i = jsin.get_int();
                    int j = jsin.get_int();
                    jsin.start_array();
                    while( !jsin.end_array() ) {
                        int type = jsin.get_int();
                        int density = jsin.get_int();
                        int age = jsin.get_int();
                        if (sm->fld[i][j].findField(field_id(type)) == NULL) {
                            sm->field_count++;
                        }
                        sm->fld[i][j].addField(field_id(type), density, age);
                    }
                }
            } else if( submap_member_name == "graffiti" ) {
                jsin.start_array();
                while( !jsin.end_array() ) {
                    jsin.start_array();
                    int i = jsin.get_int();
                    int j = jsin.get_int();
                    sm->set_graffiti( i, j, jsin.get_string() );
                    jsin.end_array();
                }
            } else if(submap_member_name == "cosmetics") {
                jsin.start_array();
                while (!jsin.end_array()) {
                    jsin.start_array();
                    int i = jsin.get_int();
                    int j = jsin.get_int();
                    jsin.read(sm->cosmetics[i][j]);
                    jsin.end_array();
                }
            } else if( submap_member_name == "spawns" ) {
                jsin.start_array();
                while( !jsin.end_array() ) {
                    jsin.start_array();
                    const mtype_id type = mtype_id( jsin.get_string() ); // TODO: json should know how to read an string_id
                    int count = jsin.get_int();
                    int i = jsin.get_int();
                    int j = jsin.get_int();
                    int faction_id = jsin.get_int();
                    int mission_id = jsin.get_int();
                    bool friendly = jsin.get_bool();
                    std::string name = jsin.get_string();
                    jsin.end_array();
                    spawn_point tmp( type, count, i, j, faction_id, mission_id, friendly, name );
                    sm->spawns.push_back( tmp );
                }
            } else if( submap_member_name == "vehicles" ) {
                jsin.start_array();
                while( !jsin.end_array() ) {
                    vehicle *tmp = new vehicle();
                    jsin.read( *tmp );
                    sm->vehicles.push_back( tmp );
                }
            } else if( submap_member_name == "computers" ) {
                std::string computer_data = jsin.get_string();
                sm->comp.load_data( computer_data );
            } else if( submap_member_name == "camp" ) {
                std::string camp_data = jsin.get_string();
                sm->camp.load_data( camp_data );
            } else {
                jsin.skip_value();
            }
        }
        if( !add_submap( submap_coordinates, sm ) ) {
            debugmsg( "submap %d,%d,%d was already loaded", submap_coordinates.x, submap_coordinates.y,
                      submap_coordinates.z );
        }
    }
    if( submaps.count( p ) == 0 ) {
        debugmsg("file %s did not contain the expected submap %d,%d,%d", quad_path.str().c_str(), p.x, p.y,
                 p.z);
        return NULL;
    }
    return submaps[ p ];
}