void WalkerPrefect::_checkPath2NearestFire( const ReachedBuildings& buildings ) { PathWay bestPath; int minLength = 9999; foreach( BuildingPtr building, buildings ) { if( building->getType() != B_BURNING_RUINS ) continue; PathWay tmp; bool foundPath = Pathfinder::getInstance().getPath( getIJ(), building->getTile().getIJ(), tmp, false, Size( 0 ) ); if( foundPath && tmp.getLength() < minLength ) { bestPath = tmp; minLength = tmp.getLength(); if( tmp.getLength() == 1 ) break; } } if( bestPath.getLength() > 0 ) { setPathWay( bestPath ); //_pathWay.begin(); } }
void WalkerPrefect::_checkPath2NearestFire( const ReachedBuildings& buildings ) { PathWay bestPath; int minLength = 9999; for( ReachedBuildings::const_iterator itBuilding = buildings.begin(); itBuilding != buildings.end(); ++itBuilding) { if( (*itBuilding)->getType() != B_BURNING_RUINS ) continue; PathWay tmp; bool foundPath = Pathfinder::getInstance().getPath( getIJ(), (*itBuilding)->getTile().getIJ(), tmp, false, Size( 0 ) ); if( foundPath && tmp.getLength() < minLength ) { bestPath = tmp; minLength = tmp.getLength(); if( tmp.getLength() == 1 ) break; } } if( bestPath.getLength() > 0 ) { _pathWay = bestPath; _pathWay.begin(); } }
int printUpperLeftBlock(double* a, int n, int m){ const int q=7; double* const temp = new double[q*q]; if (q<n){ for (int i=0; i<q; i++){ for (int j=0; j<q; j++){ temp[i*q+j]=getIJ(a, n, m, i, j); } } printMatrix(temp, q, q); } else{ for (int i=0; i<n; i++){ for (int j=0; j<n; j++){ temp[i*n+j]=getIJ(a, n, m, i, j); } } printMatrix(temp, n, n); } delete[] temp; return 0; }
bool WalkerPrefect::_looks4Fire( ServiceWalker::ReachedBuildings& buildings, TilePos& pos ) { buildings = getReachedBuildings( getIJ() ); foreach( BuildingPtr building, buildings ) { if( building->getType() == B_BURNING_RUINS ) { pos = building->getTilePos(); return true; } } return false; }
void TaxCollector::onMidTile() { ServiceWalker::onMidTile(); ReachedBuildings buildings = getReachedBuildings( getIJ() ); for( ReachedBuildings::iterator it=buildings.begin(); it != buildings.end(); it++ ) { HousePtr house = (*it).as<House>(); if( house.isValid() ) { int money = house->collectTaxes(); _d->money += money; _d->peoplesReached += money > 0 ? house->getNbHabitants() : 0; } } }
bool WalkerPrefect::_looks4Fire( ServiceWalker::ReachedBuildings& buildings, TilePos& pos ) { buildings = getReachedBuildings( getIJ() ); for( ServiceWalker::ReachedBuildings::const_iterator itBuilding = buildings.begin(); itBuilding != buildings.end(); ++itBuilding) { SmartPtr< BurningRuins > bruins = ( *itBuilding ).as<BurningRuins>(); if( bruins.isValid() ) { pos = bruins->getTile().getIJ(); return true; } } return false; }
void WalkerPrefect::_back2Prefecture() { bool pathFound = Pathfinder::getInstance().getPath( getIJ(), getBase()->getTile().getIJ(), _getPathway(), false, Size( 0 ) ); if( !pathFound ) { deleteLater(); _d->action = Impl::doNothing; } else { _getPathway().begin(); } _setGraphic( WG_PREFECT ); _d->action = Impl::back2Prefecture; }
void main () { unsigned int *image; image = (unsigned int*) malloc (WIDTH * HEIGHT * sizeof (unsigned int)); int i; // COUNTER FOR WIDTH #pragma omp parallel for for (i=0; i<NPOINTS; i++) { double cx = getRand (XMIN, XMAX); //XMIN + i * (XMAX-XMIN) / (WIDTH - 1.0); double cy = getRand (YMIN, YMAX); //YMIN + j * (YMAX-YMIN) / (HEIGHT - 1.0); double x = 0.0; double y = 0.0; unsigned int count = 0; do { if (count == 100000) break; count++; N (&x, &y, cx, cy); int I, J; getIJ (&I, &J, x, y); if (I>=0 && I < WIDTH && J>=0 && J < HEIGHT) IMAGE(I,J)++; } while (norm (x, y) < 4.0); } int j; for (j=0; j<HEIGHT; j++) { for (i=0; i<WIDTH; i++) printf ("%u ", IMAGE(i,j)); printf ("\n"); } free (image); }
void Walker::walk() { if (D_NONE == _action._direction ) { // nothing to do return; } Tile& tile = Scenario::instance().getCity().getTilemap().at( getIJ() ); switch (_action._direction) { case D_NORTH: case D_SOUTH: _remainMoveJ += _d->getSpeed(); break; case D_EAST: case D_WEST: _remainMoveI += _d->getSpeed(); break; case D_NORTH_EAST: case D_SOUTH_WEST: case D_SOUTH_EAST: case D_NORTH_WEST: _remainMoveI += _d->getSpeed() * 0.7f; _remainMoveJ += _d->getSpeed() * 0.7f ; break; default: THROW("Invalid move direction: " << _action._direction); break; } bool newTile = false; bool midTile = false; int amountI = int(_remainMoveI); int amountJ = int(_remainMoveJ); _remainMoveI -= amountI; _remainMoveJ -= amountJ; // std::cout << "walker step, amount :" << amount << std::endl; while (amountI+amountJ > 0) { switch (_action._direction) { case D_NORTH: inc(_sj, _d->pos.rj(), amountJ, _d->midTile.getJ(), newTile, midTile); break; case D_NORTH_EAST: inc(_sj, _d->pos.rj(), amountJ, _d->midTile.getJ(), newTile, midTile); inc(_si, _d->pos.ri(), amountI, _d->midTile.getI(), newTile, midTile); break; case D_EAST: inc(_si, _d->pos.ri(), amountI, _d->midTile.getI(), newTile, midTile); break; case D_SOUTH_EAST: dec(_sj, _d->pos.rj(), amountJ, _d->midTile.getJ(), newTile, midTile); inc(_si, _d->pos.ri(), amountI, _d->midTile.getI(), newTile, midTile); break; case D_SOUTH: dec(_sj, _d->pos.rj(), amountJ, _d->midTile.getJ(), newTile, midTile); break; case D_SOUTH_WEST: dec(_sj, _d->pos.rj(), amountJ, _d->midTile.getJ(), newTile, midTile); dec(_si, _d->pos.ri(), amountI, _d->midTile.getI(), newTile, midTile); break; case D_WEST: dec(_si, _d->pos.ri(), amountI, _d->midTile.getI(), newTile, midTile); break; case D_NORTH_WEST: inc(_sj, _d->pos.rj(), amountJ, _d->midTile.getJ(), newTile, midTile); dec(_si, _d->pos.ri(), amountI, _d->midTile.getI(), newTile, midTile); break; default: THROW("Invalid move direction: " << _action._direction); break; } if (newTile) { // walker is now on a new tile! onNewTile(); } if (midTile) { // walker is now on the middle of the tile! onMidTile(); } // if (midTile) std::cout << "walker mid tile" << std::endl; // if (newTile) std::cout << "walker new tile" << std::endl; // if (amount != 0) std::cout << "walker remaining step :" << amount << std::endl; } _ii = _d->pos.getI()*15+_si; _jj = _d->pos.getJ()*15+_sj; }
void WalkerPrefect::onMidTile() { ReachedBuildings reachedBuildings; TilePos firePos; bool haveBurningRuinsNear = _looks4Fire( reachedBuildings, firePos ); bool isDestination = _getPathway().isDestination(); switch( _d->action ) { case Impl::doNothing: break; case Impl::patrol: { if( haveBurningRuinsNear ) { //tell our prefecture that need send prefect with water to fight with fire //on next deliverService //found fire, no water, go prefecture getBase().as<Prefecture>()->fireDetect( firePos ); _back2Prefecture(); Walker::onNewDirection(); } else { foreach( BuildingPtr building, reachedBuildings ) { building->applyService( ServiceWalkerPtr( this ) ); HousePtr house = building.as<House>(); if( house.isValid() && house->getHealthLevel() < 1 ) { house->deleteLater(); GameEventMgr::append( DisasterEvent::create( house->getTilePos(), DisasterEvent::plague ) ); } } } if( isDestination ) { _back2Prefecture(); } Walker::onMidTile(); } break; case Impl::back2Prefecture: { if( haveBurningRuinsNear ) { //tell our prefecture that need send prefect with water to fight with fire //on next deliverService getBase().as<Prefecture>()->fireDetect( firePos ); } if( isDestination ) { deleteLater(); _d->action = Impl::doNothing; } Walker::onMidTile(); } break; case Impl::gotoFire: { if( _getPathway().getDestination().getIJ().distanceFrom( getIJ() ) < 1.5f ) { LandOverlayPtr overlay = _getPathway().getDestination().getOverlay(); if( overlay.isValid() && overlay->getType() == B_BURNING_RUINS ) { _d->action = Impl::fightFire; _setGraphic( WG_PREFECT_FIGHTS_FIRE ); Walker::onNewDirection(); isDestination = false; } } if( isDestination ) { if( !haveBurningRuinsNear || _d->water == 0 ) { _back2Prefecture(); } else { _setGraphic( WG_PREFECT_DRAG_WATER ); _d->action = Impl::gotoFire; _checkPath2NearestFire( reachedBuildings ); Walker::onNewDirection(); } } Walker::onMidTile(); } break; case Impl::fightFire: break; }
void Walker::walk() { if (D_NONE == _d->action.direction ) { // nothing to do return; } Tile& tile = _d->city->getTilemap().at( getIJ() ); switch (_d->action.direction) { case D_NORTH: case D_SOUTH: _d->remainMove += PointF( 0, _d->getSpeed() ); break; case D_EAST: case D_WEST: _d->remainMove += PointF( _d->getSpeed(), 0 ); break; case D_NORTH_EAST: case D_SOUTH_WEST: case D_SOUTH_EAST: case D_NORTH_WEST: _d->remainMove += PointF( _d->getSpeed() * 0.7f, _d->getSpeed() * 0.7f ); break; default: StringHelper::debug( 0xff, "Invalid move direction: %d", _d->action.direction ); _d->action.direction = D_NONE; break; } bool newTile = false; bool midTile = false; int amountI = int(_d->remainMove.getX()); int amountJ = int(_d->remainMove.getY()); _d->remainMove -= Point( amountI, amountJ ).toPointF(); // std::cout << "walker step, amount :" << amount << std::endl; int tmpX = _d->tileOffset.getX(); int tmpY = _d->tileOffset.getY(); int tmpJ = _d->pos.getJ(); int tmpI = _d->pos.getI(); while (amountI+amountJ > 0) { switch (_d->action.direction) { case D_NORTH: inc(tmpY, tmpJ, amountJ, _d->midTilePos.getY(), newTile, midTile); break; case D_NORTH_EAST: inc(tmpY, tmpJ, amountJ, _d->midTilePos.getY(), newTile, midTile); inc(tmpX, tmpI, amountI, _d->midTilePos.getX(), newTile, midTile); break; case D_EAST: inc(tmpX, tmpI, amountI, _d->midTilePos.getX(), newTile, midTile); break; case D_SOUTH_EAST: dec(tmpY, tmpJ, amountJ, _d->midTilePos.getY(), newTile, midTile); inc(tmpX, tmpI, amountI, _d->midTilePos.getX(), newTile, midTile); break; case D_SOUTH: dec(tmpY, tmpJ, amountJ, _d->midTilePos.getY(), newTile, midTile); break; case D_SOUTH_WEST: dec(tmpY, tmpJ, amountJ, _d->midTilePos.getY(), newTile, midTile); dec(tmpX, tmpI, amountI, _d->midTilePos.getX(), newTile, midTile); break; case D_WEST: dec(tmpX, tmpI, amountI, _d->midTilePos.getX(), newTile, midTile); break; case D_NORTH_WEST: inc(tmpY, tmpJ, amountJ, _d->midTilePos.getY(), newTile, midTile); dec(tmpX, tmpI, amountI, _d->midTilePos.getX(), newTile, midTile); break; default: StringHelper::debug( 0xff, "Invalid move direction: %d", _d->action.direction); _d->action.direction = D_NONE; break; } _d->tileOffset = Point( tmpX, tmpY ); _d->pos = TilePos( tmpI, tmpJ ); if (newTile) { // walker is now on a new tile! onNewTile(); } if (midTile) { // walker is now on the middle of the tile! onMidTile(); } // if (midTile) std::cout << "walker mid tile" << std::endl; // if (newTile) std::cout << "walker new tile" << std::endl; // if (amount != 0) std::cout << "walker remaining step :" << amount << std::endl; } Point overlayOffset = tile.getOverlay().isValid() ? tile.getOverlay()->getOffset( _d->tileOffset ) : Point( 0, 0 ); _d->posOnMap = Point( _d->pos.getI(), _d->pos.getJ() )*15 + _d->tileOffset + overlayOffset; }
void WalkerPrefect::onMidTile() { ReachedBuildings reachedBuildings; TilePos firePos; bool haveBurningRuinsNear = _looks4Fire( reachedBuildings, firePos ); bool isDestination = _pathWay.isDestination(); switch( _d->action ) { case Impl::patrol: { if( haveBurningRuinsNear ) { //tell our prefecture that need send prefect with water to fight with fire //on next deliverService //found fire, no water, go prefecture getBase().as<BuildingPrefect>()->fireDetect( firePos ); _back2Prefecture(); Walker::onNewDirection(); } else { for( ReachedBuildings::iterator itBuilding = reachedBuildings.begin(); itBuilding != reachedBuildings.end(); ++itBuilding) { (*itBuilding)->applyService( ServiceWalkerPtr( this ) ); } } if( isDestination ) { _back2Prefecture(); } Walker::onMidTile(); } break; case Impl::back2Prefecture: { if( haveBurningRuinsNear ) { //tell our prefecture that need send prefect with water to fight with fire //on next deliverService getBase().as<BuildingPrefect>()->fireDetect( firePos ); } if( isDestination ) { deleteLater(); _d->action = Impl::doNothing; } Walker::onMidTile(); } break; case Impl::gotoFire: { if( _pathWay.getDestination().getIJ().distanceFrom( getIJ() ) < 1.5f ) { LandOverlayPtr overlay = _pathWay.getDestination().get_terrain().getOverlay(); BurningRuinsPtr bruins = overlay.as<BurningRuins>(); if( bruins.isValid() ) { _d->action = Impl::fightFire; _walkerGraphic = WG_PREFECT_FIGHTS_FIRE; Walker::onNewDirection(); isDestination = false; } } if( isDestination ) { if( !haveBurningRuinsNear || _d->water == 0 ) { _back2Prefecture(); } else { _walkerGraphic = WG_PREFECT_DRAG_WATER; _d->action = Impl::gotoFire; _checkPath2NearestFire( reachedBuildings ); Walker::onNewDirection(); } } Walker::onMidTile(); } break; case Impl::fightFire: break; } }