BuildingPtr reserveShortestPath( const BuildingType buildingType, 
                                 GoodStock& stock, long& reservationID,
                                 Propagator &pathPropagator, PathWay &oPathWay )
{
  BuildingPtr res;
  Propagator::Routes pathWayList;
  pathPropagator.getRoutes(buildingType, pathWayList);

  //remove factories with improrer storage
  Propagator::Routes::iterator pathWayIt= pathWayList.begin();
  while( pathWayIt != pathWayList.end() )
  {
    // for every factory within range
    SmartPtr<T> building = pathWayIt->first.as<T>();

    if( stock._currentQty >  building->getGoodStore().getMaxStore( stock.type() ) )
    {
      pathWayList.erase( pathWayIt++ );
    }
    else
    {
      pathWayIt++;
    }
  }

  //find shortest path
  int maxLength = 999;
  PathWay* shortestPath = 0;
  for( Propagator::Routes::iterator pathIt = pathWayList.begin(); 
    pathIt != pathWayList.end(); pathIt++ )
  {
    if( pathIt->second.getLength() < maxLength )
    {
      shortestPath = &pathIt->second;
      maxLength = pathIt->second.getLength();
      res = pathIt->first;
    }
  }

  if( res.isValid() )
  {
    reservationID = res.as<T>()->getGoodStore().reserveStorage( stock );
    if (reservationID != 0)
    {
      oPathWay = *shortestPath;
    }
    else
    {
      res = BuildingPtr();
    }
  }


  return res;
}
Example #2
0
Propagator::DirectRoute getWarehouse4Sells( Propagator &pathPropagator,
                                            SimpleGoodStore& basket )
{
  Propagator::Routes pathWayList = pathPropagator.getRoutes( building::warehouse );

  // select the warehouse with the max quantity of requested goods
  Propagator::Routes::iterator pathWayIt = pathWayList.begin(); 
  while( pathWayIt != pathWayList.end() )
  {
    // for every warehouse within range
    WarehousePtr warehouse= pathWayIt->first.as< Warehouse >();

    if( warehouse->getGoodStore().getFreeQty() == 0 ) { pathWayList.erase( pathWayIt++ );}
    else { pathWayIt++; }    
  }

  //have only available warehouses, find nearest of it
  Propagator::DirectRoute shortest = pathPropagator.getShortestRoute( pathWayList );

  return shortest;
}