def __init__(self): GOAPAgent.__init__(self) self.data_provider = self self.tile_color = g_vars["Unit"]["Soldier"]["TileColor"] self.image = Surface((g_vars["Game"]["UnitSize"], g_vars["Game"]["UnitSize"])) GameActor.__init__(self) # overrides self.move_factor = 4 # local variables self.position = Position(3, 1) self.home = Position(3, 1) self.backpack = [] self.health = 100 self.hunger = 0 self.social = 10 self.boredom = 25 self.subjects = 2 # flags self.waited_for_food = False # actions self.add_action(EatMeat()) self.add_action(BurnSubject()) self.add_action(SurveyTerritory()) self.add_action(ReturnHome()) self.add_action(WaitForMeat()) self.add_action(HuntForMeat())
def scan_resources(self, agent_id: int): # get scanning area (x_min, x_max) = max(0, self.scanning_location.x - self.scanning_radius ), self.scanning_location.x + self.scanning_radius (y_min, y_max) = max(0, self.scanning_location.y - self.scanning_radius ), self.scanning_location.y + self.scanning_radius # drop_loc = Position(2, 2) # TODO query value max_distance = distance(drop_loc, Position(g_map.tile_width, g_map.tile_height)) # start scan for x in range(x_min, x_max): for y in range(y_min, y_max): # check if tile flagged as empty (False) if self.scan_cache.get((x, y), True): tile = g_map.tile_data.get((x, y)) if tile and tile.has_resources_remaining(): # create uniqe set of resources in tile uniques = set() for resource in tile.resource_list: uniques.add(type(resource).__name__) for resource in uniques: fact = WorkingMemoryFact() # confidence = distance to resource compared to max radius resource_position = Position(x, y) confidence = (float(max_distance) - float( distance(resource_position, drop_loc))) / float(max_distance) fact.set_pos(resource_position, confidence) fact.set_ftype(FactType.Resource) if resource == "WildTree": # TODO? obj_confidence = blackboard get target object type == Logs ? 1.0 else 0.0 #fact.set_obj("Logs", obj_confidence) fact.set_obj("Logs") elif resource == "WildIronOre": fact.set_obj("Ore") # TODO always update fact (every confidence value must be updated) if g_wmm.get_working_memory(agent_id).query_fact( fact): # fact exists continue # _fact = self.working_memory.read_fact(fact) # retrieve it # _fact = fact # just update it # if _fact.position.confidence < confidence: # if fact has less confidence than current -> update it # _fact.position.confidence = confidence # print("Updated fact") else: g_wmm.get_working_memory(agent_id).create_fact( fact) #print("Created " + resource + " fact") else: g_wmm.get_working_memory(agent_id).delete_fact_where( FactType.Resource, lambda f: f.position.value == Position(x, y)) self.scan_cache[(x, y)] = False
def activate(self, agent_id: int): bb = g_bbm.get_blackboard(agent_id) radius = 50 pos = (0, 0) while pos in g_map.unpassable_tiles: pos = (randint(1, radius), randint(1, radius)) bb.set_manual_navigation_target(Position(pos[0], pos[1]))
def __init__(self): Structure.__init__(self) self.position = Position(2, 2) # local variables self.structure_name = "Camp" self.is_built = True # production self.production_table = {"Worker": {}, "Artisan": {}, "Explorer": {}} # actions self.add_action(PickupProductionJob()) self.add_action(ProduceArtisan())
def __init__(self, dragon): GOAPAgent.__init__(self) self.data_provider = self self.tile_color = g_vars["Unit"]["Worker"]["TileColor"] self.image = Surface( (g_vars["Game"]["UnitSize"], g_vars["Game"]["UnitSize"])) GameActor.__init__(self) # local variables self.dragon = dragon self.position = Position(3, 3) self.backpack = [] # actions self.add_action(FishForDragon()) self.add_action(HuntForDragon()) self.add_action(FeedDragon())
def __init__(self): Structure.__init__(self) self.position = Position(8, 1) # local variables self.structure_name = "Encampment" self.construction_time = 3 self.construction_materials = {"Logs": 10} # production self.production_table = {"Soldier": {"Sword": 1}} # actions self.add_action(PickupProductionJob()) self.add_action(ProduceSoldier()) self.add_action(CreateBuildJob()) self.add_action(CreateFetchJob())
def __init__(self): Structure.__init__(self) self.position = Position(4, 5) # local variables self.structure_name = "Refinery" self.construction_time = 3 self.construction_materials = {"Logs" : 10} # production self.production_target = "Coal" self.production_table = {"Coal" : {"Logs" : 10}} self.required_artisan = Profession.Refiner # actions self.add_action(ProduceCoal()) self.add_action(CreateBuildJob()) self.add_action(CreateWorkJob()) self.add_action(CreateFetchJob())
def __init__(self): Structure.__init__(self) self.position = Position(1, 4) # local variables self.structure_name = "Smithy" self.construction_time = 3 self.construction_materials = {"Logs": 10, "IronBar": 3} # production self.production_target = "Sword" self.production_table = {"Sword": {"Coal": 2, "IronBar": 1}} self.required_artisan = Profession.Smith # actions self.add_action(ProduceSword()) self.add_action(CreateBuildJob()) self.add_action(CreateWorkJob()) self.add_action(CreateFetchJob())
def __init__(self) -> None: self.position = Position(1, 1) self.move_speed = 1 self.move_factor = 1 self.move_progress = 0.0 self.move_threshold = 1.0 # sprite self.is_visible = False self.tile_size = g_vars["Game"]["TileSize"] self.image.fill(g_vars["Game"]["Colors"][self.tile_color]) self.rect = self.image.get_rect() # pathfinding self.finding_path = False self.current_path = None self.current_tile = None self.next_tile = None
def __init__(self): Structure.__init__(self) self.position = Position(4, 1) # local variables self.structure_name = "Smelter" self.construction_time = 3 self.construction_materials = {"Logs": 10} # production self.production_target = "IronBar" self.production_table = {"IronBar": {"Coal": 3, "Ore": 2}} self.required_artisan = Profession.Metallurgist # actions self.add_action(ProduceIronBar()) self.add_action(CreateBuildJob()) self.add_action(CreateWorkJob()) self.add_action(CreateFetchJob())
def __init__(self, game_map, starting_location=Position()): super().__init__() self.data_provider = self self.resources = [] self.units = [] self.structures = [] self.game_map = game_map self.starting_location = starting_location self.build_jobs = deque() self.collect_jobs = deque() self.upgrade_jobs = [] self.production_jobs = [] self.fetch_jobs = [] self.work_jobs = [] self.ore_gatherers = 0 self.logs_gatherers = 0 self.have_builder = False
def enable_GOAP_ai(self): player = Player(self.map, Position(3, 3)) # builder = Artisan() # refiner = Artisan() # builder.profession = Profession.Builder # refiner.profession = Profession.Refiner # player.add_unit(builder) # player.add_unit(refiner) # refinery.on_fetched("Logs") # refinery.on_fetched("Logs") player.add_structure(Refinery()) player.add_structure(Camp()) player.add_structure(Encampment()) player.add_structure(Smithy()) player.add_structure(Smelter()) for x in range(50): player.add_unit(Worker()) # player.add_unit(Explorer()) self.agents.append(player)
def __init__(self): GOAPAgent.__init__(self) self.data_provider = self self.tile_color = g_vars["Structure"]["Base"]["TileColor"] self.image = Surface( (g_vars["Game"]["StructureSize"], g_vars["Game"]["StructureSize"])) GameActor.__init__(self) self.position = Position(0, 0) # local variables self.structure_name = "Base" self.construction_time = 0 self.construction_materials = {} self.is_built = False self.is_worked = False self.has_materials = False self.production_ready = False # production self.inventory = [] self.produce = [] self.production_target = "" self.production_table = {} self.required_artisan = None
def check_precondition(self, agent): # check for any required criterias for the action self.target = Position(7, 15) return True
def __init__(self) -> None: super().__init__() self.update_interval = 3 self.scanning_location = Position(0, 0) self.scanning_radius = 5 self.scan_cache = {}
def __get_new_position(self): pos = (randint(1, 50), randint(1, 50)) while pos in g_map.unpassable_tiles: pos = (randint(1, 50), randint(1, 50)) return Position(pos[0], pos[1])
def move(self, dx=0, dy=0): self.position = Position(self.position.x + dx, self.position.y + dy) self.current_tile = g_map.get_background_tile( (self.position.x, self.position.y))
def on_production_finish(self): new_unit = Artisan() new_unit.position = Position(self.agent_position.x, self.agent_position.y) self.add_player_unit(new_unit)
def get_resource_drop_off_loc(self): return Position(2, 4)
def run(self): self.running = True ## for x in range(0, 50): worker = Worker() agent = GOAPController() agent.setup(worker) agent.enable_navigation() agent.enable_targeting() agent.enable_sensors() agent.attach_sensor(ResourceSensor()) fact_x = WorkingMemoryFact() res_pos = g_player.get_resource_drop_off_loc() fact_x.set_pos(res_pos, 0.5).set_ftype(FactType.Delivery) agent.working_memory.create_fact(fact_x) agent.blackboard.set_position(Position(2, 2)) g_player.add_unit(agent) ## refinery = Refinery() agent = GOAPController() agent.setup(refinery) agent.blackboard.set_position(Position(4, 5)) agent.blackboard.set_required_artisan(Profession.Refiner) g_player.add_structure(agent) camp = Camp() agent = GOAPController() agent.setup(camp) agent.blackboard.set_is_built(True) agent.blackboard.set_position(Position(2, 2)) g_player.add_structure(agent) smelter = Smelter() agent = GOAPController() agent.setup(smelter) agent.blackboard.set_position(Position(4, 1)) agent.blackboard.set_required_artisan(Profession.Metallurgist) g_player.add_structure(agent) smithy = Smithy() agent = GOAPController() agent.setup(smithy) agent.blackboard.set_position(Position(1, 4)) agent.blackboard.set_required_artisan(Profession.Smith) g_player.add_structure(agent) encampment = Encampment() agent = GOAPController() agent.setup(encampment) agent.blackboard.set_position(Position(8, 1)) g_player.add_structure(agent) frames = 0 update_time = 0.0 draw_time = 0.0 t_start = time.now() while (self.running): #time.clock.update(g_vars["Game"]["FPS"], self.speed) time.clock.update(60, self.speed) frames += 1 t1 = time.now() self.update() t2 = time.now() update_time += t2 - t1 self.draw() t3 = time.now() draw_time += t3 - t2 self.events() self.running = len([u for u in g_player.units if type(u.entity).__name__ == "Soldier"]) < 20 t_end = time.now() print("") print("------------------- BARF FORTRESS 3.0 --------------------") print("Elapsed time: " + str(t_end - t_start) + " ms") print("Frames: " + str(frames)) print("Update time: " + str(update_time) + " ms") print("Update efficiency: " + str(update_time / frames) + " ms/frame") print("Draw time: " + str(draw_time) + " ms") print("Draw efficiency: " + str(draw_time / frames) + " ms/frame")
def get_resource_location(self, resource): if resource == "Ore": return Position(randint(8, 10), randint(2, 4)) elif resource == "Logs": return Position(randint(1, 4), randint(6, 9))
def check_precondition(self, agent): # check for any required criterias for the action area = 10 self.target = Position(random.randint(area, area * 2), random.randint(area, area * 2)) return True
def move_to_next_position(self): new_position = Position(self.next_tile[0], self.next_tile[1]) g_bbm.get_blackboard(self.agent_id).set_position(new_position) # update next tile self.next_tile = self.current_path[self.next_tile]