def computeClosestFood(self): for focus in self.env.npcs: # if not focus in self.closestFood.keys(): self.closestFood[focus] = [] # rects = self.env.pgo_obj.getRectInRangeStrict(focus) # res = [] # for r in rects: # res.extend(self.env.pgo_obj.rectContainsFood[r.center]) # self.closestFood[focus] = res if not focus in self.closestFood.keys(): self.closestFood[focus] = [] for f in self.env.ressources["food"]: if not pf.checkStraightPath( self.env, focus.getPose(), f.getPose(), 10, check_river=False) and utils.distance2p( focus.getPose(), f.getPose()) <= focus.vision_radius: if not f in self.closestFood[focus]: self.closestFood[focus].append(f) elif f in self.closestFood[focus]: self.closestFood[focus].remove(f)
def nextStep(self): if not self.path: return 1 if self.ipath >= len(self.path): return 1 self.target = self.path[self.ipath] # print self.target if utils.near(self.entity.getPose(), self.target, _thresh=self.near_treshold): self.ipath_prev = self.ipath self.ipath += 1 self.entity.shift_x = 0 self.entity.shift_y = 0 if self.ipath >= len(self.path): self.ipath = 0 self.ipath_prev = -1 del self.path[:] return 0 p1 = self.entity.getPose() p2 = self.target if self.ipath_prev != self.ipath: self.k = float(self.entity.speed) / float(utils.distance2p(p1, p2)) new_p = (self.k * p2[0] + (1 - self.k) * p1[0], self.k * p2[1] + (1 - self.k) * p1[1]) self.entity.shift_x = new_p[0] - self.entity.pose.x self.entity.shift_y = new_p[1] - self.entity.pose.y return 0
def constructGraph(self, neighbour_function): # print("constructGraph") # print("set costs") for r in self.graph_rect: self.graph_cost[r.center] = 1.0 # print("Compute neighbours") tot = len(self.graph_rect) curr = 0 pc.setDict("ENV_CONSTR_TRACK", "max", tot) pc.setDict("ENV_CONSTR_TRACK", "current", curr) for r in self.graph_rect: curr += 1 pc.setDict("ENV_CONSTR_TRACK", "current", curr) self.loading = round((float(curr) / float(tot)) * 100, 2) pc.setDict("ENV_CONSTR_TRACK", "percent", self.loading) # if self.loading % 4 == 0: # print(str(self.loading) + "% (" + str(curr) + "/" + str(tot) + ")", end='\r') self.graph[r.center] = {} for other in filter( lambda x: x.center != r.center and neighbour_function( r, x), self.graph_rect): self.graph[r.center][other.center] = utils.distance2p( r.center, other.center) * self.graph_cost[other.center]
def inloop_op_CFCT(focus, ct): for f in ct.env.ressources["food"]: if not focus in ct.closestFood.keys(): ct.closestFood[focus] = [] if not pf.checkStraightPath( ct.env, focus.getPose(), f.getPose(), 10, check_river=False) and utils.distance2p( focus.getPose(), f.getPose()) <= focus.vision_radius: if not f in ct.closestFood[focus]: ct.closestFood[focus].append(f) elif f in ct.closestFood[focus]: ct.closestFood[focus].remove(f)
def nextStep(self): x = super(AttackBehaviour, self).nextStep() # if self.entity.name == "entity0" : print self.target_entity.getPose() if x == 1: # print "{} attack {} nextStep : {}".format(self.entity.name, self.target_entity.name ,x) if utils.distance2p(self.entity.getPose( ), self.target_entity.getPose()) < self.entity.attack_range * 1.1: self.entity.attack_other(self.target_entity) return 1 else: self.recompute = (self.recompute + 1) % 50 if self.recompute == 0: self.computePath() return 0
def nextStep(self): x = super(ShareFoodBehaviour, self).nextStep() # if self.entity.name == "entity0" : print self.target_entity.getPose() if x == 1: # print "{} attack {} nextStep : {}".format(self.entity.name, self.target_entity.name ,x) if utils.distance2p( self.entity.getPose(), self.shareto.getPose()) < self.entity.share_range * 1.1: # self.entity.shareFoodMemory(self.shareto) self.entity.shareFood(self.shareto) return 1 else: self.recompute = (self.recompute + 1) % 50 if self.recompute == 0: self.computePath() return 0
def setDefaultBehaviour(self): if (self.known_food and [x for x in self.known_food if x.harvestable] and ("food" not in self.bagpack.keys() or ("food" in self.bagpack.keys() and self.bagpack["food"] <= self.hunger_thresh * 0.33))): self.setCollectFoodBehaviour() elif (not self.known_food or not [x for x in self.known_food if x.harvestable]): self.setExploreBehaviour() elif self.social_cooldown <= 0 and self.neighbours: l = [ x for x in self.neighbours if utils.distance2p( self.getPose(), x.getPose()) < self.interaction_range ] if l: other = random.choice(l) self.setSocialInteractionBehaviour(other) else: self.setIdleBehaviour() else: self.setIdleBehaviour()