示例#1
0
bool psPathNetwork::Load(iEngine *engine, iDataConnection *db,psWorld * world)
{
    // First initialize pointers to some importent classes
    this->engine = engine;
    this->db = db;
    this->world = world;
    

    Result rs(db->Select("select wp.*,s.name as sector from sc_waypoints wp, sectors s where wp.loc_sector_id = s.id"));

    if (!rs.IsValid())
    {
        Error2("Could not load waypoints from db: %s",db->GetLastError() );
        return false;
    }
    for (int i=0; i<(int)rs.Count(); i++)
    {
        Waypoint *wp = new Waypoint();

        if (wp->Load(rs[i],engine))
        {
            Result rs2(db->Select("select id,alias,rotation_angle from sc_waypoint_aliases where wp_id=%d",wp->GetID()));
            for (int j=0; j<(int)rs2.Count(); j++)
            {
                wp->AddAlias(rs2[j].GetInt(0),rs2[j][1],rs2[j].GetFloat(2)*PI/180.0);
            }

            waypoints.Push(wp);

            // Push in groups
            if (strcmp(wp->GetGroup(),"")!=0)
            {
                AddWaypointToGroup(wp->GetGroup(),wp);
            }
            
        }
        else
        {
            Error2("Could not load waypoint: %s",db->GetLastError() );            
            delete wp;
            return false;
        }
        
    }

    Result rs1(db->Select("select * from sc_waypoint_links"));

    if (!rs1.IsValid())
    {
        Error2("Could not load waypoint links from db: %s",db->GetLastError() );
        return false;
    }
    for (int i=0; i<(int)rs1.Count(); i++)
    {
        Waypoint * wp1 = FindWaypoint(rs1[i].GetInt("wp1"));
        Waypoint * wp2 = FindWaypoint(rs1[i].GetInt("wp2"));
        if(!wp1 || !wp2)
        {
        	if(!wp1)
        	    Error2("Could not find waypoint wp1 link with id %d",rs1[i].GetInt("wp1") );
        	if(!wp2)
        	    Error2("Could not find waypoint wp2 link with id %d",rs1[i].GetInt("wp2") );
        	return false;
        }
        psString flagStr(rs1[i]["flags"]);

        int pathId = rs1[i].GetInt("id");
        
        csString pathType = rs1[i]["type"];
        
        psPath * path;
        if (strcasecmp(pathType,"linear") == 0)
        {
            path = new psLinearPath(pathId,rs1[i]["name"],flagStr);
        }
        else
        {
            path = new psLinearPath(pathId,rs1[i]["name"],flagStr); // For now
        }

        path->SetStart(wp1);
        if (!path->Load(db,engine))
        {
            Error1("Failed to load path");
            return false;
        }
        path->SetEnd(wp2);
        paths.Push(path);

        float dist = path->GetLength(world,engine);
                                    
        wp1->AddLink(path,wp2,psPath::FORWARD,dist);
        if (!path->oneWay)
        {
            wp2->AddLink(path,wp1,psPath::REVERSE,dist); // bi-directional link is implied
        }
    }
    
    
    return true;
}