void TraineeWalker::_cancelPath() { BuildingPtr destination = receiver(); Logger::warningIf( destination.isNull(), "!!! WARNING: Trainee _cancelPath destination is null" ); if( destination.isValid() ) { destination->cancelTrainee( type() ); } }
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; }
void TraineeWalker::_reachedPathway() { Walker::_reachedPathway(); deleteLater(); BuildingPtr dest = _city()->getOverlay( _d->destLocation ).as<Building>(); if( dest.isValid() ) { dest->updateTrainee( this ); } }
void TraineeWalker::send2City(BuildingPtr base, bool roadOnly ) { if( !base.isValid() ) { Logger::warning( "!!! WARNING: trainee walker base is null" ); deleteLater(); return; } _d->baseLocation = base->pos(); _computeWalkerPath( roadOnly ); if( !isDeleted() && gfx::tilemap::isValidLocation( _d->destLocation ) ) { BuildingPtr dest = receiver(); dest->reserveTrainee( type() ); attach(); } }
void TraineeWalker::setBase(BuildingPtr originBuilding) { _d->baseLocation = originBuilding.isValid() ? originBuilding->pos() : gfx::tilemap::invalidLocation(); }
void TraineeWalker::_computeWalkerPath( bool roadOnly ) { if( !gfx::tilemap::isValidLocation( _d->baseLocation ) ) { Logger::warning( "!!! WARNING: trainee walker baselocation is unaccessible at [%d,%d]", _d->baseLocation.i(), _d->baseLocation.j() ); deleteLater(); return; } BuildingPtr base = ( _city()->getOverlay( _d->baseLocation ).as<Building>()); if( !base.isValid() ) { Logger::warning( "!!! WARNING: trainee walker base is null at [%d,%d]", _d->baseLocation.i(), _d->baseLocation.j() ); deleteLater(); return; } _d->maxNeed = 0; // need of this trainee in buildings Pathway finalPath; BuildingList buildings; for( auto buildingType : _d->necBuildings ) buildings.append( _city()->statistic().objects.find<Building>( buildingType ) ); TilesArray startArea = roadOnly ? base->roadside() : base->enterArea(); DirectRoute droute; _d->maxNeed = 0; unsigned int minDistance = _d->maxDistance; bool isNeedTrainee = false; for( auto bld : buildings ) { float howMuchNeedMyService = bld->evaluateTrainee( type() ); if( howMuchNeedMyService > 0 ) { isNeedTrainee = true; break; } } if( !isNeedTrainee ) { Logger::warning( "!!! WARNING: not need trainee walker from [%d,%d]", base->pos().i(), base->pos().j() ); deleteLater(); return; } std::set<BuildingPtr> checkedBuilding; for( auto itile : startArea ) { TilePos startPos = itile->pos(); for( auto bld : buildings ) { bool buildingAlsoServicing = checkedBuilding.count( bld ) > 0; if( buildingAlsoServicing ) continue; checkedBuilding.insert( bld ); float curNeed = bld->evaluateTrainee( type() ); if( _d->maxNeed < curNeed ) { Pathway way = PathwayHelper::create( startPos, bld, roadOnly ? PathwayHelper::roadOnly : PathwayHelper::allTerrain ); if( way.isValid() && way.length() < minDistance ) { _d->maxNeed = curNeed; droute = DirectRoute( bld.object(), way ); } } } } if( droute.first.isValid() ) { finalPath = droute.second; _d->destLocation = droute.first->pos(); } if( finalPath.isValid() ) { // some building needs that trainee!!! setPos( finalPath.startPos() ); setPathway( finalPath ); } else { // nobody needs him... deleteLater(); } }
void mayMove( const Tile* tile, bool& ret ) { BuildingPtr f = ptr_cast<Building>( tile->overlay() ); ret = ( tile->isWalkable( true ) || f.isValid() ); }
void tryMove( const Tile* tile, bool& ret ) { BuildingPtr building = tile->overlay<Building>(); ret = ( tile->isWalkable( true ) || building.isValid() ); }