def build_object(self, me, txt): o = MapObject(me, self.fn, txt, self.size_multiplier) o.max_relpos = [self.max_relpos[0], self.max_relpos[1]] o.min_relpos = [self.min_relpos[0], self.min_relpos[1]] o.is_ground = self.is_ground ## if self.randomize_relpos: ## o.randomize_relpos() return o
def __init__(self, type_name, editor, sprites, name=None, factor=1., relpos=(0, 0), build=True, new_type=True): self.stop_animation = float("inf") self.stop_animation_func = None self.set_animation_type("loop") self.animation_step = 0 self.grayed = [] self.highlights = {} self.sprites_ref = {} if sprites: imgs = [] isprite = 0 for key in SPRITES_KEYS: sprites_for_this_key, frame_type = sprites[key] imgs.extend(sprites_for_this_key) n = len(sprites_for_this_key) self.sprites_ref[key] = (isprite, n, frame_type) isprite += n else: imgs = [""] MapObject.__init__(self, editor, imgs, type_name, factor, relpos, build, new_type) self.can_interact = True # self.max_dist = None self.attack_range = None self.shot_frequency = None self.help_range = None self.cost = None self.base_number = None self.material_cost = {} self.terrain_attack = {} self.object_defense = {} self.strength = None self.defense = None self.help_repair = None # self.race = None self.game = None # self.walk_img = {} self.set_frame_refresh_type(2) #type fast self.vel = 0.07 self.current_isprite = 0 self.team = None self.footprint = None self.projectile1 = None #projectile used in close battle self.is_grayed = False self.is_building = None self.id = Unit.unit_id Unit.unit_id += 1
def __init__(self, me): self.me = me me.game = self self.gui = None self.t = 0 self.units = [] self.need_refresh_ui_box = True #Fire fire_imgs = get_sprite_frames("sprites/fire_idle.png") self.fire = MapObject(self.me, fire_imgs, "fire") self.fire.min_relpos = [0, -0.4] self.fire_max_relpos = [0, -0.4] self.fire.relpos = [0, -0.4] #Smoke self.smokes_log = {} effects.initialize_smokegens() #Default structures self.bridge_v, self.bridge_h = None, None self.bridges = [] self.cobblestone = None self.road = None self.bridge_h = None self.bridge_v = None self.bridge = None self.is_main = True
def initialize_rivers(self): lm = self.lm img_fullsize = self.get_material_image("Shallow water") imgs = {} for dx in [-1, 0, 1]: for dy in [-1, 0, 1]: imgs = tm.build_tiles( img_fullsize, lm.cell_sizes, lm.nframes, dx * lm.nframes, dy * lm.nframes, #dx, dy sin=False) river_obj = MapObject(self, imgs[0], "river", 1.) river_obj.is_ground = True self.register_object_type(river_obj)
def from_file_units(f, me): """Load units and their logical content (names, properties, etc.)""" print("Loading units") n = pickle.load(f) #len(dynamic_objects) for i in range(n): coord = pickle.load(f) #coord a = {} for attr_name in MapObject.get_saved_attributes(): a[attr_name] = pickle.load(f) # print("*** Loading unit", a["name"]) print(a) obj = MapObject(me, fns=a["fns"], name=a["name"], factor=a["factor"], relpos=a["relpos"], build=a["build"], new_type=a["new_type"]) obj.set_frame_refresh_type(obj._refresh_frame_type) obj_added = me.add_unit(coord, obj, a["quantity"])
def make_grayed(self): if not self.is_grayed: self.is_grayed = True self.hide = True imgs = self.get_race_unit().grayed obj = MapObject(self.game.me, imgs[self.game.me.zoom_level]) obj.imgs_z_t = imgs obj = self.game.add_object(self.cell.coord, obj, 1) obj.set_frame_refresh_type(self._refresh_frame_type) obj.get_map_time = self.get_map_time obj.get_current_frame = obj._get_current_frame3 obj.name = "*" + self.str_type obj.is_grayed = True self.game.gui.last_move = None
def get_saved_attributes(): return MapObject.get_saved_attributes() + ["team"]
def build_imgs(self): MapObject.build_imgs(self) self.build_highlighted_idles() self.build_grayed_idles()
def refresh_translation_animation(self): if self.animation_type == ANIM_LOOP: delta = MapObject.refresh_translation_animation(self) key = DELTA_TO_KEY[delta] self.set_sprite_type(key)
def add_river_greedy(me, lm, material_dict, imgs, rounded_river, min_length): """Computes and draw a random river.""" print(" Building random river...") cell_source = get_river_source(material_dict) if not (cell_source): print("no cell source") return xi, yi = cell_source cell_source = lm.cells[xi][yi] path = [cell_source] cell_xy = cell_source maxn = 1000 it = 0 should_finish = False margin = 0.01 lake_probability = 0.5 while True: if it > maxn: break elif "water" in cell_xy.material.name.lower(): break elif should_finish: break it += 1 section_length = random.randint(2, 10) if random.random() < 0.5: sign = 1 else: sign = -1 if random.random() < 0.5: dx, dy = sign, 0 else: dx, dy = 0, sign ## print(dx,dy,section_length) ################################################ for i in range(section_length): if should_finish: break x = cell_xy.coord[0] + dx y = cell_xy.coord[1] + dy new_cell = lm.get_cell_at(x, y) if new_cell is None: break elif new_cell.h - margin > cell_xy.h: if cell_xy.material.name != new_cell.material.name: break elif new_cell in path: break elif new_cell.name != "river": is_valid = True for neigh in new_cell.get_neighbors_von_neuman(): if neigh: if not (neigh is cell_xy): if neigh.name == "river": is_valid = False break elif "water" in neigh.material.name.lower(): should_finish = True elif neigh in path: is_valid = False break if is_valid: cell_xy = new_cell path.append(new_cell) ## print("OK",dx,dy,section_length) else: break else: break #4) change the end to first shallow shore cell actual_path = [] for cell in path: if cell.name == "river": break actual_path.append(cell) if "water" in cell.material.name.lower(): break else: #LAKE ? next_to_water = False for neigh in cell.get_neighbors_von_neuman(): if neigh: if "water" in neigh.material.name.lower(): next_to_water = True break if next_to_water: break if len(actual_path) < min_length: return if actual_path[0].material.name == actual_path[-1].material.name: return elif not ("water" in actual_path[-1].material.name.lower()): if random.random() < lake_probability: pass else: return #build images of river objs = {} for delta in imgs: #imgs[(dx,dy)][zoom] river_obj = MapObject(me, imgs[delta][0], "river", 1.) river_obj.is_ground = True river_obj.lm = lm objs[delta] = river_obj #5) add river cells to map and layer for i, cell in enumerate(actual_path): prepare_cell_for_river(lm, cell) dx, dy, corner = get_path_orientation(i, cell, actual_path) if rounded_river: c = objs.get((dx, dy, corner)) else: c = objs.get((dx, dy, None)) if not c: raise Exception("No river object for delta", dx, dy, corner) assert cell.name != "river" c = c.add_copy_on_cell(cell) cell.name = "river" lm.static_objects.append(c) if actual_path: ## print("RIVER BUILT:", [cell.coord for cell in actual_path]) if not ("water" in actual_path[-1].material.name.lower()): for neigh in actual_path[-1].get_neighbors_moore(): if neigh and neigh.name != "river": prepare_cell_for_river(lm, neigh) river_obj = MapObject(me, imgs[(0, 0, None)][0], "river", 1.) river_obj.is_ground = True river_obj.lm = lm river_obj = river_obj.add_copy_on_cell(neigh) neigh.name = "river" lm.static_objects.append(river_obj) return objs
def add_static_objects(self, me, seed_static): #1) We use another hmap to decide where we want trees (or any other object) S = len(me.hmap) m = ng.generate_terrain(S, n_octaves=self.static_objects_n_octaves, persistance=self.static_objects_persistance, chunk=seed_static) ng.normalize(m) self.smap = m #me.lm is a superimposed map on which we decide to blit some static objects: #3) We build the objects that we want. # its up to you to decide what should be the size of the object (3rd arg) for d in custom.distributions: d.build_objects(me) # cobble = MapObject(me, self.cobble_fn, "cobblestone", self.cobble_fn_size) cobble.is_ground = True self.cobblestone = cobble bridge_h = MapObject(me, self.bridge_h, "bridge", self.bridge_h_size, str_type="bridge_h") bridge_h.is_ground = True bridge_h.max_relpos = [0., 0.] bridge_h.min_relpos = [0., 0.] bridge_v = MapObject(me, self.bridge_v, "bridge", self.bridge_v_size, str_type="bridge_v") bridge_v.is_ground = True bridge_v.max_relpos = [0., 0.] bridge_v.min_relpos = [0., 0.] self.bridge_h_mapobject = bridge_h self.bridge_v_mapobject = bridge_v self._objects = { "cobble": cobble, "bridge_h": bridge_h, "bridge_v": bridge_v, "road": cobble } for d in custom.distributions: for o in d.objects: self._objects[o.name] = o #4) add the objects via distributors, to add them randomly in a nice way for d in custom.distributions: distributor = d.get_distributor(me, me.lm, self.smap) distributor.distribute_objects(me.lm, exclusive=d.exclusive) self.cobbles = [ cobble, cobble.flip(True, False), cobble.flip(False, True), cobble.flip(True, True) ] ############################################################################ #Here we show how to use the path finder for a given unit of the game #Actually, we use it here in order to build cobblestone roads on the map me.initialize_rivers() costs_materials_road = {name: 1. for name in me.materials} costs_materials_road["Snow"] = 10. #unit is 10 times slower in snow costs_materials_road["Thin snow"] = 2. #twice slower on thin snow... costs_materials_road["Sand"] = 2. ## for name in me.materials: ## if "water" in name.lower(): ## costs_materials_road[name] = 10 river_type = me.object_types["river"] bush_int_type = self._objects["bush"].int_type village_int_type = self._objects["village"].int_type costs_objects_road = { bush_int_type: 2., cobble.int_type: 0.9, river_type: 2. } #unit is 2 times slower in rivers #Materials allowed (here we allow water because we add bridges) possible_materials_road = list(me.materials) for name in me.materials: if "water" in name.lower(): possible_materials_road.remove(name) possible_objects_road = [ cobble.int_type, bush_int_type, village_int_type, river_type ] ######################################################################## #now we build a path for rivers, just like we did with roads. costs_materials_river = {name: 1. for name in me.materials} #Materials allowed (here we allow water because we add bridges) possible_materials_river = list(me.materials) possible_objects_river = [] random.seed(self.seed_static_objects) n_roads = 0 n_rivers = 0 lm = me.lm self.imgs_river = self.build_imgs_river(me) material_dict = get_materials_dict(lm) self.update_loading_bar("Finding paths for rivers and roads...", 0.6) for i in range(self.max_number_of_rivers): add_river_greedy(me, me.lm, material_dict, self.imgs_river, self.rounded_river, min_length=self.min_river_length)