def set_trees(self, cell, biome): tree_freq = ease(myrange_f(biome.tree_range)) if cell.color == 14: cell.color = 3 height = myrange(self.config.trees_height_range) girth = min(self.cell_size, self.cell_size) * 2 / 5 p = Vec2(self.cell_size / 2, self.cell_size / 2) leaves = [[0, 0], [0, 0], [0, 0]] cell.trees.append(Tree(p, height, girth, leaves)) else: # Trees for x in range(0, self.cell_size - self.config.trees_gap, self.config.trees_gap): for y in range(0, self.cell_size - self.config.trees_gap, self.config.trees_gap): if rnd(1) < tree_freq: height = myrange(self.config.trees_height_range) girth = myrange(self.config.trees_girth_range) p = Vec2(x + rnd(self.config.trees_gap), y + rnd(self.config.trees_gap)) leaves = [[0, 0], [0, 0], [0, 0]] tree = Tree(p, height, girth, leaves) cell.trees.append(tree) tree.p = Vec2( mid(tree.girth, tree.pos.x, self.cell_size - tree.girth), mid(tree.girth, tree.pos.y, self.cell_size - tree.girth))
def editDiplomacyCoords(events, screen, world, chosen_state): screen.fill((28, 28, 28)) pygame.draw.line(screen, pygame.Color("white"), (400, 0), (400, 800), 5) pygame.draw.line(screen, pygame.Color("white"), (0, 400), (800, 400), 5) for state in world.list_of_states: state.update_self_diplomacy_coords(screen) state_icons_group.draw(screen) if chosen_state is None: for event in events: if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: for state in world.list_of_states: x_collision = myrange( state.icon.rect[0], state.icon.rect[0] + state.icon.rect[2]).contains(event.pos[0]) y_collision = myrange( state.icon.rect[1], state.icon.rect[1] + state.icon.rect[3]).contains(event.pos[1]) # Log.d(x_collision, y_collision); if x_collision and y_collision: return state elif chosen_state is not None: pygame.draw.rect(screen, pygame.Color("red"), tuple([*chosen_state.icon.rect]), 5) for event in events: if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: chosen_state.diplomacy_coords = DiplomacyCoords( event.pos[0] / 40 - 10, event.pos[1] / 40 - 10) return None return chosen_state
def set_buildings(self, cell, biome): if (len(cell.trees) + len(cell.bushes) == 0) and rnd(1) < biome.building_freq: cell.building = Building( myrange(self.config.buildings_w_range), myrange(self.config.buildings_h_range), Vec2(self.cell_size / 2, self.cell_size / 2), myrange(self.config.buildings_height_range), self.config.buildings_colours[flr(rnd(16)) % len( self.config.buildings_colours)])
def set_bushes(self, cell, biome): # Bushes if rnd(1) < biome.bush_props[0]: x = rnd(self.cell_size) y = rnd(self.cell_size) r_add = 0 bloom_colours = biome.bush_props[2] colour = bloom_colours[flr(rnd(len(bloom_colours))) % len(bloom_colours)] for j in range(0, myrange(self.config.bushes_cluster_range)): r = myrange_f(self.config.bushes_radius_range) height = myrange_f(self.config.bushes_height_range) p = Vec2( x + myrange_f([1, (r + r_add)]) - myrange_f([1, (r + r_add) / 2]), y + myrange_f([1, (r + r_add)]) - myrange_f([1, (r + r_add) / 2])) bloom = None if rnd(1) < biome.bush_props[1]: a = rnd(1) r_add = rnd(r / 2.0) + r / 4.0 bloom = Vec2(r * cos(a), r * sin(a)) bush = Bush(p, r, height, colour, bloom) cell.bushes.append(bush)
def main_exp(): L = 10 num_sites = 16 alphas = [0.5/16]*16 mr_systems = [mr_system(alphas,use_annealing=True,scale=1,iterations=500000, sse_epsilon=0.0001)[-1] for i in range(10)] mr_ics = map(motif_ic,mr_systems) mr_ginis = map(motif_gini,mr_systems) init_states = [random_motif_with_dirty_bits(L,num_sites)] ic = 12 M = 100 tau = 1 timesteps = 100 epsilon = 0.1 bins = [-10] + myrange(0,ic,epsilon) + [ic,ic + epsilon]+ [ic + 10] control_motifs = concat([weighted_ensemble(mutate_motif_with_dirty_bits, motif_ic_with_dirty_bits, init_states, bins, M, tau, timesteps, final_bin_index=None,verbose=1) for i in range(5)]) control_ics = map(lambda ((m,ics),p):motif_ic(m),concat(control_motifs)) control_ginis = map(lambda ((m,ics),p):motif_gini(m),concat(control_motifs)) binned_controls = [[gini for ic,gini in zip(control_ics,control_ginis) if abs(ic-mr_ic) < 0.1] for mr_ic in mr_ics]
def motif_prob_scan_exp(): L = 10 num_sites = 16 alphas = [0.5/16]*16 verbose=False mr_systems = [[mr_system(alphas,use_annealing=True,scale=1,iterations=5000000, sse_epsilon=0.0001,motif_prob=alpha,verbose=verbose)[-1] for i in range(5)] for alpha in myrange(0.3,1,0.1)]
def sample_mr_system(alphas,G=100000.0,n=16,L=10): M = 100 bins = myrange(0,0.015615,10**-5) + [1] tau = 1 init_matrix = lambda:[[0,0,0,0] for i in range(L)] init_motif = lambda:[random_site(L) for i in range(n)] init_states = [(init_matrix(),init_motif()) for i in range(M)] q = lambda (matrix,motif):propose(matrix,motif) f = lambda (matrix,motif):show(sse(matrix,motif,alphas,G,n)) timesteps = 10000 return weighted_ensemble(q,f,init_states,bins,M,tau,timesteps,verbose=1)
def __init__(self): self.count_range = random.randint(20, 40) self.height_range = [45, 50] self.radius_range = [5, 15] self.cluster_range = [5, 7] self.size = 256 self.height_mult = 0.015 self.clouds = [] for _ in range(0, self.count_range): x = rnd(self.size * 2) y = rnd(self.size * 2) r = 0 for _ in range( 0, random.randint(self.cluster_range[0], self.cluster_range[1])): c_r = myrange(self.radius_range) c_p = [ x + myrange([1, (c_r + r) / 2]) - myrange([1, (c_r + r) / 2]), y + myrange([1, (c_r + r) / 2]) - myrange([1, (c_r + r) / 2]) ] if rnd(1) > 0.5: x = c_p[0] y = c_p[1] r = c_r self.clouds.append( Cloud(c_p[0], c_p[1], c_r, myrange(self.height_range)))
def MapMovementAndZoom(events: list, pressed: bool, last_coords: tuple) -> None: global mapgroup if pressed: up_coords = pygame.mouse.get_pos() delta = (up_coords[0] - last_coords[0], up_coords[1] - last_coords[1]) Map.move(mapsurface, delta) last_coords = up_coords for event in events: if event.type == pygame.MOUSEBUTTONDOWN and event.button == 3 and mapsurface.collides( event.pos): pressed = True last_coords = pygame.mouse.get_pos() # Log.d("pressed", pressed) if event.type == pygame.MOUSEBUTTONUP and event.button == 3: pressed = False if event.type == pygame.MOUSEBUTTONDOWN and event.button == 4 and mapsurface.collides( event.pos): # ZOOM + # Log.d("I`ve catch this event!") # Log.d("mapsurface.scale = {}".format(mapsurface.scale)) if myrange(0, 10).contains(mapsurface.scale): mapsurface.set_scale(mapsurface.scale + 1) mapgroup.update() if event.type == pygame.MOUSEBUTTONDOWN and event.button == 5 and mapsurface.collides( event.pos): # ZOOM - if myrange(1, 11).contains(mapsurface.scale): mapsurface.set_scale(mapsurface.scale - 1) mapgroup.update() if event.type == pygame.QUIT: pygame.quit() sys.exit() return pressed, last_coords
def main(world: World, mapfile: str = "skyrim_map.jpg") -> int: global mapsurface, scale, screen, mapgroup, bottom_button_group pygame.init() pygame.display.set_caption("MapGen {}".format(current_version)) screen = pygame.display.set_mode(resolution) # Log.d(type(screen)); # main map initialization mapgroup = pygame.sprite.Group() mapsurface = Map(mapfile, 0, 0, 0) mapsurface.add(mapgroup) # button blocks drawing menu_blocks_group = pygame.sprite.Group() right_menu_block = MenuBlock(800, 0, 200, 800) bottom_menu_block = MenuBlock(0, 800, 1000, 100) menu_blocks_group.add(right_menu_block) menu_blocks_group.add(bottom_menu_block) # Log.expects(menu_blocks_group.has(right_menu_block), True) # Log.expects(menu_blocks_group.has(bottom_menu_block), True) # Log.expects(mapgroup.has(mapsurface), True) right_button_group = pygame.sprite.Group() add_province_button = Button(x=825, y=25, w=150, h=75, text="Add Province", mode=1) delete_province_button = Button(x=825, y=125, w=150, h=75, text="Delete Province", mode=0) add_state_button = Button(x=825, y=225, w=150, h=75, text="Add State", mode=2) edit_graph_button = Button(x=825, y=325, w=150, h=75, text="Edit Graph", mode=3) edit_parameter_button = Button(x=825, y=425, w=150, h=75, text="Edit Parameter", mode=4) political_coordinates_button = Button(x=825, y=525, w=150, h=75, text="Political Coords", mode=5) diplomacy_coordinates_button = Button(x=825, y=625, w=150, h=75, text="Diplomacy Coords", mode=6) compile_script_button = Button(x=825, y=725, w=150, h=75, text="Compile Script", mode=0) right_button_group.add(add_province_button) right_button_group.add(delete_province_button) right_button_group.add(add_state_button) right_button_group.add(edit_graph_button) right_button_group.add(edit_parameter_button) right_button_group.add(political_coordinates_button) right_button_group.add(diplomacy_coordinates_button) right_button_group.add(compile_script_button) bottom_button_group = pygame.sprite.Group() # ================== ДОБАВЛЯТЬ КНОПКУ ДЛЯ ПАРАМЕТРОВ =================== political_parameter_button = Button(x=10, y=805, w=180, h=40, text="Political", mode=POLITICAL_MODE) population_parameter_button = Button(x=10, y=855, w=180, h=40, text="Population", mode=POPULATION_MODE) production_parameter_button = Button(x=210, y=805, w=180, h=40, text="Production", mode=PRODUCTION_MODE) education_parameter_button = Button(x=210, y=855, w=180, h=40, text="Education", mode=EDUCATION_MODE) army_parameter_button = Button(x=410, y=805, w=180, h=40, text="Army", mode=ARMY_MODE) natural_resources_button = Button(x=410, y=855, w=180, h=40, text="Natural Resources", mode=NATURAL_RESOURCES_MODE) separatism_button = Button(x=610, y=805, w=180, h=40, text="Separatism", mode=SEPARATISM_MODE) climate_button = Button(x=610, y=855, w=180, h=40, text="Climate auspiciousness", mode=CLIMATE_MODE) sea_button = Button(x=810, y=805, w=180, h=40, text='Water Provinces', mode=SEA_MODE) defensive_ability_button = Button(x=810, y=855, w=180, h=40, text="Defensive ability", mode=DEFENSIVE_ABILITY_MODE) # ============== ЗАСОВЫВАТЬ КНОПКУ В СВОЮ ГРУППУ ====================== bottom_button_group.add(political_parameter_button) bottom_button_group.add(population_parameter_button) bottom_button_group.add(production_parameter_button) bottom_button_group.add(education_parameter_button) bottom_button_group.add(army_parameter_button) bottom_button_group.add(natural_resources_button) bottom_button_group.add(separatism_button) bottom_button_group.add(climate_button) bottom_button_group.add(sea_button) bottom_button_group.add(defensive_ability_button) scale = 0 # масштаб карты. транслятор масштаб : размер карты в mapsurface.scale_dict pressed = False capital_pressed = -1 mode = 0 last_coords = None current_added_province = None text_showing: bool = True icon_showing: bool = True parameter_mode: int = POLITICAL_MODE chosen_state: State = None # Log.d(parameter_mode); """ Mode 0: is the mode with political map showing and nothinhg else Mode 1: mode when user adds province drawing polygons Mode 2: mode when user color provinces into countries Mode 3: mode when user draw a graph Mode 4: mode when user is editing a province parameters Mode 5: mode when user edit political coordinates Mode 6: mode when user edit diplomacy coordinates """ # main screen cycle while True: frameEventsList = pygame.event.get() # обработка вспомогательных модов работы for event in frameEventsList: if event.type == pygame.KEYDOWN: try: if event.key in map(ord, list('тТnN')): text_showing = not text_showing # Log.d('got it'); continue elif event.key in map(ord, list('iIшШ')): icon_showing = not icon_showing except Exception: pass screen.fill((228, 228, 228)) # Log.expects(myrange(0, 2).contains(mode), True); if myrange(0, 4).contains(mode): pressed, last_coords = MapMovementAndZoom(frameEventsList, pressed, last_coords) mapgroup.draw(screen) if myrange(0, 3).contains(mode): world.draw_all(screen, mapsurface, mode=parameter_mode) if mode == 3: world.draw_graph(screen, mapsurface) if mode == 4: world.draw_borders(screen, mapsurface) """ В отдельный этап вынесены эвенты со сменой режима работы" """ if mode == 0: # mode enter try: new_mode: int = check_buttons(frameEventsList, right_button_group, mode) if mode != new_mode: mode = new_mode raise ModeChangeProcedure except ModeChangeProcedure: if mode == 1: province_name = Dialog().get( message="Введите название провинции") current_added_province = Province() current_added_province.set_id(world.get_new_province_id()) current_added_province.set_name(province_name) # Log.d(current_added_province.get_name()); world.addProvince(current_added_province) elif mode == 2: state_name = Dialog().get( message="Введите название страны") current_added_state = State() current_added_state.set_id(world.get_new_state_id()) current_added_state.set_name(state_name) current_added_state.add_self_to_group(state_icons_group) world.addState(current_added_state) elif myrange(3, 6).contains(mode): pass continue if myrange(1, 6).contains(mode): # mode exit try: new_mode: int = check_buttons(frameEventsList, right_button_group, mode) if mode != new_mode: old_mode = mode mode = new_mode raise ModeChangeProcedure except ModeChangeProcedure: if mode == 0: if old_mode == 1: current_added_province = None if len(world.list_of_capitals) != len( world.list_of_province): world.list_of_province.pop() # Log.d(len(world.list_of_capitals), len(world.list_of_province)); elif old_mode == 2: current_added_state = None elif old_mode == 3: pass if mode == 1: provinceGeometryEdit(frameEventsList, current_added_province, mapsurface) elif mode == 2: addStateMode(frameEventsList, current_added_state, mapsurface) elif mode == 3: capital_pressed = graphEditMode(frameEventsList, screen, mapsurface, world, capital_pressed) elif mode == 4: parameter_mode = editParameterMode(frameEventsList, screen, mapsurface, world, parameter_mode) # Log.d(parameter_mode); world.draw_parameter(screen, mapsurface, parameter_mode) elif mode == 5: chosen_state = editPoliticalCoords(frameEventsList, screen, world, chosen_state) elif mode == 6: chosen_state = editDiplomacyCoords(frameEventsList, screen, world, chosen_state) menu_blocks_group.draw(screen) # блоки меню отрисовываются при любом режиме работы программы right_button_group.draw(screen) # кнопки отрисовываются при любом режиме работы программы. bottom_button_group.draw(screen) # if icon_showing: world.icon_draw(screen, mapsurface); pygame.display.flip() return 0
from utils import toScreenCoords, toMapCoords, toUnityCoords from utils import Dialog from utils import PoliticalCoords, DiplomacyCoords from handlers import Map from handlers import MenuBlock # right and bottom menu blocks from handlers import Button from metascripts import Province, State, World # ================ ИМПОРТИРОВАТЬ ПАРАМЕТР ===================== from metascripts import POLITICAL_MODE, POPULATION_MODE, PRODUCTION_MODE from metascripts import EDUCATION_MODE, ARMY_MODE, NATURAL_RESOURCES_MODE from metascripts import SEPARATISM_MODE, CLIMATE_MODE, SEA_MODE, DEFENSIVE_ABILITY_MODE # ============ ОПРЕДЕЛЕНИЯ ================== optimized_myrange = myrange(None, None) current_version: str = "0.0.4" resolution: tuple = (1000, 900) mapsurface: pygame.Surface = None mapgroup: pygame.sprite.Group = None map_x, map_y = 0, 0 scale: int = None screen: pygame.Surface = None bottom_button_group: object = None state_icons_group = pygame.sprite.Group() # ================= КОНЕЦ ОПРЕДЕЛЕНИЯ ГЛОБАЛЬНЫХ ПЕРЕМЕННЫХ ============= def activate_buttons(button_group: pygame.sprite.Group) -> None: