예제 #1
0
bool psPathNetwork::Delete(psPath * path)
{
    // First remove from db.
    db->CommandPump("delete from sc_path_points where path_id=%d",path->GetID());
    db->CommandPump("delete from sc_waypoint_links where id=%d",path->GetID());

    // Delete the object
    Waypoint * start = path->start;
    Waypoint * end = path->end;

    start->RemoveLink(path);
    path->start = NULL;

    end->RemoveLink(path);
    path->end = NULL;

    paths.Extract(paths.Find(path));

    delete path;

    // Now delete any waypoints that dosn't have any links anymore.
    if (start->links.GetSize() == 0)
    {
        db->CommandPump("delete from sc_waypoints where id=%d",start->GetID());
        db->CommandPump("delete from sc_waypoint_aliases where wp_id=%d",start->GetID());

        size_t index = waypoints.Find(start);
        if (index != csArrayItemNotFound)
        {
            // This will delete the start
            waypoints.DeleteIndexFast(index);
            start = NULL;
        }
    }

    if (end->links.GetSize() == 0)
    {
        db->CommandPump("delete from sc_waypoints where id=%d",end->GetID());
        db->CommandPump("delete from sc_waypoint_aliases where wp_id=%d",end->GetID());

        size_t index = waypoints.Find(end);
        if (index != csArrayItemNotFound)
        {
            // This will delete the end
            waypoints.DeleteIndexFast(index);
            end = NULL;
        }
    }
    

    return true;
}
예제 #2
0
void psPathNetwork::ListWaypoints(const char * pattern)
{
    csPDelArray<Waypoint>::Iterator iter(waypoints.GetIterator());
    Waypoint *wp;
    CPrintf(CON_CMDOUTPUT, "Waypoints\n");
    CPrintf(CON_CMDOUTPUT, "%9s %-30s %-45s %-6s %-6s %-30s\n", "WP", "Name", "Position","Radius","Dist","PI");
    while (iter.HasNext())
    {
        wp = iter.Next();

        if (!pattern || strstr(wp->GetName(),pattern))
        {
            CPrintf(CON_CMDOUTPUT, "%9d %-30s %-45s %6.2f %6.1f %-30s" ,
                    wp->loc.id,wp->GetName(),toString(wp->loc.pos,wp->loc.sector).GetDataSafe(),
                    wp->loc.radius,wp->distance,
                    (wp->pi?wp->pi->GetName():""));

            for (size_t i = 0; i < wp->links.GetSize(); i++)
            {
                CPrintf(CON_CMDOUTPUT," %s%s(%d,%.1f)",(wp->edges[i]->NoWander()?"#":""),
                        wp->links[i]->GetName(),wp->links[i]->GetID(),wp->dists[i]);
            }

            CPrintf(CON_CMDOUTPUT,"\n");
        }
    }

    CPrintf(CON_CMDOUTPUT, "Waypoint groups\n");

    for (size_t i = 0; i < waypointGroupNames.GetSize(); i++)
    {
        CPrintf(CON_CMDOUTPUT,"%s\n  ",waypointGroupNames[i].GetDataSafe());
        csList<Waypoint*>::Iterator iter(waypointGroups[i]);
        bool first = true;
        while (iter.HasNext())
        {
           Waypoint *wp = iter.Next();
           CPrintf(CON_CMDOUTPUT,"%s%s(%d)",first?"":", ",wp->GetName(),wp->GetID());
           first = false;
        }
        CPrintf(CON_CMDOUTPUT,"\n");
    }
    
}
예제 #3
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;
}