Ejemplo n.º 1
0
 def setObjectNumber( self, objectType, number ):
     number = max( 0, number )
     while len( self.getObjectsByType( objectType ) ) < number:
         x = GameItemTypes.getAbscissaForAutomaticInsertion( objectType )
         height = GameItemTypes.attributes[ objectType ][ "Height" ]
         y = - 1
         while ( 
             self.getObjectAt( *nearestTileCenter( x, y ) ) is not None
             or self.getObjectAt( *nearestTileCenter( x, y - 2 ) ) is not None
         ):
             y -= 2
         y -= height
         self.addObject( objectType, GameItemTypes.CreatePolygon( objectType, x, y, 0 ), 0 )
     while len( self.getObjectsByType( objectType ) ) > number:
         self.deleteObjects( [ self.getObjectsByType( objectType )[ -1 ] ] )
     self.notifyViews()
Ejemplo n.º 2
0
    def resetFromScenario( self, fileName ):
        def translateCoordinates( tiles ):
            return [ ( 2 * x + 1, 2 * y + 1 ) for ( x, y ) in tiles ]

        file = Caesar4File( fileName )
        self.reset( file.mapSize )
        self.setSurface( "Road", translateCoordinates( file.road ) )
        self.setSurface( "Wall", translateCoordinates( file.wall ) )
        self.setSurface( "Fertile", translateCoordinates( file.fertile ) )
        self.setSurface( "Rock", translateCoordinates( file.rock ) )

        water = set()
        slope = set()
        for x in range( file.mapSize ):
            for y in range( file.mapSize ):
                tile = ( x, y )
                elevation = file.elevations[ tile ]
                if elevation < 0:
                    water.add( tile )
                elif elevation > 0x3f800000:
                    ### @todo Detect slope instead of elevation
                    slope.add( tile )
        self.setSurface( "Water", translateCoordinates( water ) )
        self.setSurface( "Slope", translateCoordinates( slope ) )

        for objectsSet, resourceType in [
            ( file.trees, "TimberTree" ),
            ( file.clayPits, "ClayPit" ),
            ( file.sandPits, "SandPit" ),
            ( file.marbleSites, "MarbleSite" ),
            ( file.ironMines, "IronMine" ),
            ( file.goldMines, "GoldMine" ),
        ]:
            w, h = GameItemTypes.getObjectSize( resourceType )
            for tiles in objectsSet:
                polygon = TilesPolygon( translateCoordinates( tiles ) )
                self.addObject( resourceType, polygon, self.__guessRotationFromPolygon( polygon, w, h ) )
            self.notifyViews()