<s> import numpy <EOL> class IcecapSimulation ( object ) : <EOL> @ staticmethod <EOL> def is_applicable ( world ) : <EOL> return world . has_ocean ( ) and world . has_temperature ( ) <EOL> def execute ( self , world , seed ) : <EOL> world . set_icecap ( self . _calculate ( world , seed ) ) <EOL> @ staticmethod <EOL> def _calculate ( world , seed ) : <EOL> ocean = world . layers [ '<STR_LIT>' ] . data <EOL> temperature = world . layers [ '<STR_LIT>' ] . data <EOL> max_freeze_percentage = <NUM_LIT> <EOL> freeze_chance_window = <NUM_LIT> <EOL> surrounding_tile_influence = <NUM_LIT:0.5> <EOL> temp_min = temperature . min ( ) <EOL> freeze_threshold = world . layers [ '<STR_LIT>' ] . thresholds [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> freeze_threshold = ( freeze_threshold - temp_min ) * max_freeze_percentage <EOL> freeze_chance_threshold = freeze_threshold * ( <NUM_LIT:1.0> - freeze_chance_window ) <EOL> icecap = numpy . zeros ( ( world . height , world . width ) , dtype = float ) <EOL> rng = numpy . random . RandomState ( seed ) <EOL> solid_map = numpy . logical_or ( temperature <= freeze_chance_threshold + temp_min , numpy . logical_not ( ocean ) ) <EOL> for y in range ( world . height ) : <EOL> for x in range ( world . width ) : <EOL> if world . is_ocean ( ( x , y ) ) : <EOL> t = temperature [ y , x ] <EOL> if t - temp_min < freeze_threshold : <EOL> chance = numpy . interp ( t , [ temp_min , freeze_chance_threshold , freeze_threshold ] , [ <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:0.0> ] ) <EOL> if <NUM_LIT:0> < x < world . width - <NUM_LIT:1> and <NUM_LIT:0> < y < world . height - <NUM_LIT:1> : <EOL> surr_tiles = solid_map [ y - <NUM_LIT:1> : y + <NUM_LIT:2> , x - <NUM_LIT:1> : x + <NUM_LIT:2> ] <EOL> chance_mod = numpy . count_nonzero ( surr_tiles ) <EOL> chance_mod -= <NUM_LIT:1> if solid_map [ y , x ] else <NUM_LIT:0> <EOL> chance_mod = numpy . interp ( chance_mod , [ <NUM_LIT:0> , surr_tiles . size - <NUM_LIT:1> ] , [ - <NUM_LIT:1.0> , <NUM_LIT:1.0> ] ) <EOL> chance += chance_mod * surrounding_tile_influence <EOL> if rng . rand ( ) <= chance : <EOL> solid_map [ y , x ] = True <EOL> icecap [ y , x ] = freeze_threshold - ( t - temp_min ) <EOL> return icecap </s>
