Example #1
0
Propagator::DirectRoute getWarehouse4Buys( Propagator &pathPropagator,
                                           SimpleGoodStore& basket )
{
  Propagator::Routes pathWayList = pathPropagator.getRoutes( building::warehouse );

  std::map< int, Propagator::DirectRoute > warehouseRating;

  // 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 >();

    int rating = 0;
    for( int i=Good::wheat; i<Good::goodCount; i++ )
    {
      Good::Type gtype = Good::Type(i);
      int qty = warehouse->getGoodStore().getMaxRetrieve( gtype );
      int need = basket.getFreeQty( gtype );
      rating = need > 0 ? ( qty ) : 0;
    }

    rating = math::clamp( rating - pathWayIt->second.getLength(), 0, 999 );
    warehouseRating[ rating ] = *pathWayIt; 

    pathWayIt++;
  }

  //have only available warehouses, find nearest of it
  return warehouseRating.size() > 0 ? warehouseRating.rbegin()->second : Propagator::DirectRoute();
}
Example #2
0
TilePos getWalkerDestination2( Propagator &pathPropagator, const TileOverlayType type, 
                               MarketPtr market, SimpleGoodStore& basket, const Good::Type what,
                               PathWay &oPathWay, long& reservId )
{
  SmartPtr< T > res;

  Propagator::Routes pathWayList;
  pathPropagator.getRoutes(type, pathWayList);

  int max_qty = 0;

  // select the warehouse with the max quantity of requested goods
  for( Propagator::Routes::iterator pathWayIt= pathWayList.begin(); 
    pathWayIt != pathWayList.end(); ++pathWayIt)
  {
    // for every warehouse within range
    BuildingPtr building= pathWayIt->first;
    PathWay& pathWay= pathWayIt->second;

    SmartPtr< T > destBuilding = building.as< T >();
    int qty = destBuilding->getGoodStore().getMaxRetrieve( what );
    if( qty > max_qty )
    {
      res = destBuilding;
      oPathWay = pathWay;
      max_qty = qty;
    }
  }

  if( res.isValid() )
  {
    // a warehouse/granary has been found!
    // reserve some goods from that warehouse/granary
    int qty = std::min( max_qty, market->getGoodDemand( what ) );
    qty = std::min(qty, basket.getMaxQty( what ) - basket.getCurrentQty( what ));
    // std::cout << "MarketLady reserves from warehouse, qty=" << qty << std::endl;
    GoodStock stock( what, qty, qty);
    reservId = res->getGoodStore().reserveRetrieval( stock );
    return res->getTilePos();
  }

  return TilePos(-1, -1);
}
Example #3
0
TilePos getSupplierDestination2( Propagator &pathPropagator, const TileOverlayType type, 
                                 const Good::Type what, const int needQty,
                                 PathWay &oPathWay, long& reservId )
{
  SmartPtr< T > res;

  Propagator::Routes pathWayList;
  pathPropagator.getRoutes(type, pathWayList);

  int max_qty = 0;

  // select the warehouse with the max quantity of requested goods
  for( Propagator::Routes::iterator pathWayIt= pathWayList.begin(); 
       pathWayIt != pathWayList.end(); ++pathWayIt)
  {
    // for every warehouse within range
    BuildingPtr building= pathWayIt->first;
    PathWay& pathWay= pathWayIt->second;

    SmartPtr< T > destBuilding = building.as< T >();
    int qty = destBuilding->getGoodStore().getMaxRetrieve( what );
    if( qty > max_qty )
    {
      res = destBuilding;
      oPathWay = pathWay;
      max_qty = qty;
    }
  }

  if( res.isValid() )
  {
    // a warehouse/granary has been found!
    // reserve some goods from that warehouse/granary
    int qty = math::clamp( needQty, 0, max_qty );
    GoodStock tmpStock( what, qty, qty);
    reservId = res->getGoodStore().reserveRetrieval( tmpStock );
    return res->getTilePos();
  }
  else
  {
    return TilePos( -1, -1 );
  }
}
Example #4
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;
}
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;
}