Exemple #1
0
    def get_requirements(self, target, amount=1, task_list=None):
        if not task_list:
            task_list = algorithms.Queue()

        if not target["Production"]:
            return task_list

        production_list = target["Production"]
        for product in production_list:
            target_group = product[0]           # Unit, Structure
            target_type = product[1]            # Worker, Explorer, Smithy, etc.
            target_amount = product[2]          # How many is needed/ordered
            target_class = entities.to_class(target_type)    # Class instance

            # already completed -> skip
            if self.has_num_entities(target_class, target_amount):
                continue
            # deduct already owned resources (if any)
            else:
                amount -= len(self.entities_where(lambda e: isinstance(e, target_class)))
            # chain multiplikation
            if not target_group == "Structure":
                target_amount *= amount
            # get sub-requirements
            self.get_requirements(g_vars[target_group][target_type], target_amount, task_list)
            # append parent task last
            task_list.put([target_group, target_type, target_amount])

        return task_list
Exemple #2
0
 def has_resource(self, target, count=1):
     target_class = entities.to_class(target)
     for resource in self.entity_list:
         if isinstance(resource, target_class):
             count -= 1
         if count <= 0:
             return True
     return False
Exemple #3
0
 def deduct_resource_cost(self, resource_list):
     for resource_cost in resource_list:
         target_group = resource_cost[0]
         target_class = entities.to_class(resource_cost[1])
         target_amount = resource_cost[2]
         # only resources should be deducted
         if not target_group == "Resource":
             continue
         # find correct resource and deduct
         for resource in self.entities_count_where(lambda res: isinstance(res, target_class), target_amount):
             self.remove_entity(resource)
Exemple #4
0
    def can_create_entity(self, target_group, target_type):
        production_list = g_vars[target_group][target_type]["Production"]
        for target in production_list:
            target_class = entities.to_class(target[1])  # type of entity
            target_amount = target[2]                    # resource needed
            if target[0] == "Exploration":
                    return False
            elif not len(self.entities_where(lambda e: isinstance(e, target_class) and e.is_idle)) >= target_amount:
                return False

        return True
    def check_current_task(self):
        if not self.current_task:
            return

        target_group = self.current_task[0]
        target_type = self.current_task[1]
        target_amount = self.current_task[2]
        target_class = entities.to_class(target_type)

        if target_group == "Exploration":
            # has not found resource
            if not self.has_found_resource(target_class, target_amount):
                # explore
                if not self.fsm.is_in_state(ai_state.AIStateExplore):
                    self.fsm.change_state(ai_state.AIStateExplore())
            # has found resource
            else:
                # has found enough materials
                if self.has_resource(
                        g_vars[target_group][target_type]["GatheredType"][1],
                        target_amount):
                    # done
                    self.complete_current_task()
                # has not found enough materials
                else:
                    # gather
                    if not self.fsm.is_in_state(ai_state.AIStateGather):
                        self.fsm.change_state(ai_state.AIStateGather())
                    self.target_resource = (target_group, target_type,
                                            target_class)
            return

        else:
            if self.has_num_entities(target_class, target_amount):
                self.complete_current_task()
            elif self.can_create_entity(target_group, target_type):
                new_entity = target_class(self)
                new_entity.begin_production()
                self.deduct_resource_cost(
                    g_vars[target_group][target_type]["Production"])
            else:
                self.prepend_goal(self.current_task)
            return
Exemple #6
0
 def add_resource(self, resource):
     for x in range(0, resource[0][2]): # amount
         new_resource = entities.to_class(resource[0][1])
         new_resource = new_resource(self)
         new_resource.is_idle = True
         self.add_entity(new_resource)