GameObjectPtr BuildingType::createInstance(void) { BuildingPtr result = new Building; if(!result) return(nullptr); result->init(true, this); //result->setIdNumber(NextIdNumber++); return(result); }
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::_checkDestination(const object::Type buildingType, Propagator &pathPropagator) { DirectPRoutes pathWayList = pathPropagator.getRoutes( buildingType ); for( auto item : pathWayList ) { // for every building within range BuildingPtr building = item.first.as<Building>(); float need = building->evaluateTrainee( type() ); if (need > _d->maxNeed) { _d->maxNeed = need; _d->destLocation = building->pos(); } } }
void TraineeWalker::checkDestination(const BuildingType buildingType, Propagator &pathPropagator) { Propagator::ReachedBuldings pathWayList; pathPropagator.getReachedBuildings(buildingType, pathWayList); for( Propagator::ReachedBuldings::iterator pathWayIt= pathWayList.begin(); pathWayIt != pathWayList.end(); ++pathWayIt) { // for every building within range BuildingPtr building = pathWayIt->first; float need = building->evaluateTrainee(_traineeType); if (need > _maxNeed) { _maxNeed = need; _destinationBuilding = building; } } }
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 Tax::handleEvent(NEvent& event) { if( event.EventType == sEventMouse ) { switch( event.mouse.type ) { case mouseMoved: { Tile* tile = _camera()->at( event.mouse.pos(), false ); // tile under the cursor (or NULL) std::string text = ""; if( tile != 0 ) { BuildingPtr bld = ptr_cast<Building>( tile->overlay() ); if( bld.isNull() ) { text = "##no_people_in_this_locality##"; } else { HousePtr house = ptr_cast<House>( tile->overlay() ); if( house.isValid() ) { int taxAccess = house->hasServiceAccess( Service::forum ); if( taxAccess < 25 ) text = "##house_not_registered_for_taxes##"; } } } _setTooltipText( _(text) ); } break; default: break; } } Layer::handleEvent( event ); }
void CartSupplier::computeWalkerDestination(BuildingPtr building, const Good::Type type, const int qty ) { _d->baseBuildingPos = building->getTilePos(); _d->storageBuildingPos = TilePos( -1, -1 ); // no destination yet // we have something to buy! // get the list of buildings within reach PathWay pathWay; Propagator pathPropagator( _d->city ); pathPropagator.init( building.as<Construction>() ); pathPropagator.propagate( _d->maxDistance); // try get that good from a granary _d->storageBuildingPos = getSupplierDestination2<Granary>( pathPropagator, B_GRANARY, type, qty, pathWay, _d->reservationID ); if( _d->storageBuildingPos.getI() < 0 ) { // try get that good from a warehouse _d->storageBuildingPos = getSupplierDestination2<Warehouse>( pathPropagator, B_WAREHOUSE, type, qty, pathWay, _d->reservationID ); } if( _d->storageBuildingPos.getI() >= 0 ) { // we found a destination! setPathWay(pathWay); } else { // we have nothing to buy, or cannot find what we need to buy deleteLater(); return; } setIJ( _getPathway().getOrigin().getIJ() ); }
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() ); }