def perform(self, agent): # perform the action maximun_gatherers = 5 gather_table = {} #gathering_types = ["collectOre", "collectLogs"] gathering_types = ["collect" + s for s in ["Ore", "Logs"]] # ÓwÒ all_workers = agent.get_units("Worker") idle_workers = [e for e in all_workers if e.goal_state is None] # worker is idle if not idle_workers: # no idle workers -> skip self.finished = True return True for gather_type in gathering_types: gather_table[gather_type] = len([e for e in all_workers if not e.goal_state is None and e.goal_state.get(gather_type)]) # construct initial gathering table current_gatherers = sum(gather_table.values()) diff = min(maximun_gatherers - current_gatherers, len(idle_workers)) # get potential number of gatherers or minimun available workers for i in range(diff): least_gathered = min(gather_table, key=gather_table.get) # get the least gathered resource <-- this defines the gathering behavior goal_state = ActionSet() goal_state.add(least_gathered, True) idle_workers[i].set_goal_state(goal_state) # check if locked from gathering? gather_table[least_gathered] = gather_table.get(least_gathered) + 1 current_gatherers += 1 if current_gatherers >= maximun_gatherers: self.finished = True return True
def __init__(self): self.preconditions = ActionSet() self.effects = ActionSet() self.target = None self.started = False self.in_range = False self.minimun_range = 0 self.cost = 1.0
def perform(self, agent): # didn't get free worker if not self.target_worker: return False # perform the action goal_state = ActionSet() goal_state.add(self.target_command, True) self.target_worker.set_goal_state(goal_state) self.finished = True #agent.resolve_goal(self.effects) return True
def check_precondition(self, agent): # get free worker workers = agent.get_units("Worker") for worker in workers: if not worker.goal_state: # has no set goal goal_state = ActionSet() goal_state.add("recievingPlan", True) worker.goal_state = goal_state self.target_worker = worker break return True
class GOAPAction: def __init__(self): self.preconditions = ActionSet() self.effects = ActionSet() self.target = None self.started = False self.in_range = False self.minimun_range = 0 self.cost = 1.0 def reset(self): self.target = None self.started = False self.in_range = False def requires_in_range(self): pass def get_cost(self, agent): return self.cost def completed(self): pass def check_precondition(self, agent): # more like setup/initialize pass def on_start(self, agent): self.started = True def perform(self, agent): pass def on_end(self, agent): pass def is_in_range(self): return self.in_range def set_in_range(self, value): self.in_range = value def add_precondition(self, key, value, amount=0): self.preconditions.add(key, value, amount) def remove_precondition(self, key): for k in self.preconditions.keys(): if key == k: self.preconditions[key] = None break def add_effect(self, key, value, amount=0): self.effects.add(key, value, amount) def remove_effect(self, key): for k in self.effects.keys(): if key == k: self.effects[key] = None break
def create_goal_state(self): goal_state = ActionSet() goal_state.add("produceSword", True) return goal_state
def create_goal_state(self): goal_state = ActionSet() goal_state.add("doJob", True) return goal_state
def populate_state(self, parent_state, action_effects): new_state = ActionSet() new_state.update(parent_state) # update with current base values new_state.update(action_effects) # update with new values return new_state
def create_goal_state(self): goal_state = ActionSet() goal_state.add("isExploring", True) return goal_state
def create_goal_state(self): goal_state = ActionSet() goal_state.add("satisfyDragon", True) return goal_state
def create_goal_state(self): goal_state = ActionSet() goal_state.add("gatherResources", True) goal_state.add("haveFreeWorker", True) return goal_state
def create_world_state(self): # Returns an evaluated set of the world state world_data = ActionSet() return world_data
def create_goal_state(self): goal_state = ActionSet() goal_state.add("stayAlive", True) goal_state.add("staySatisfied", True) return goal_state