standings.py (4245B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\behaviors\monitors\standings.py 2 from collections import defaultdict 3 import random 4 from behaviors.groups.mixin import GroupTaskMixin 5 from behaviors.monitors.ballparks import MonitorProximitySensors 6 from behaviors.utility.ballparks import get_item_owner, get_ball_position 7 from behaviors.utility.blackboards import get_response_thresholds 8 from behaviors.utility.components import get_proximity_sensors_for_group_members 9 from behaviors.utility.standings import get_standings_between 10 from npcs.common.standings import classify_standings 11 from npcs.common.standings import STANDINGS_HOSTILE, STANDINGS_NEUTRAL, STANDINGS_FRIENDLY 12 import logging 13 logger = logging.getLogger(__name__) 14 15 class StandingsAddresses(object): 16 17 def __init__(self, item_ids, flag, last_position): 18 self.item_ids = item_ids 19 self.flag = flag 20 self.last_position = last_position 21 22 23 class MonitorProximitySensorForGroupStanding(MonitorProximitySensors, GroupTaskMixin): 24 25 def __init__(self, attributes = None): 26 super(MonitorProximitySensorForGroupStanding, self).__init__(attributes) 27 self.address_by_class = {} 28 self._map_address_to_class(STANDINGS_HOSTILE, 'itemIdsWithHostileStandingsAddress', 'flagHostileStandingsAddress', 'lastPositionForHostileStandingsAddress') 29 self._map_address_to_class(STANDINGS_NEUTRAL, 'itemIdsWithNeutralStandingsAddress', 'flagNeutralStandingsAddress', 'lastPositionForNeutralStandingsAddress') 30 self._map_address_to_class(STANDINGS_FRIENDLY, 'itemIdsWithFriendlyStandingsAddress', 'flagFriendlyStandingsAddress', 'lastPositionForFriendlyStandingsAddress') 31 32 def GetBubbleIdSet(self): 33 return self.GetMemberBubbleSet() 34 35 def GetProximitySensors(self): 36 return get_proximity_sensors_for_group_members(self) 37 38 def ClassifyItems(self, item_ids): 39 item_ids_by_standings = self._get_item_ids_by_standings_class(item_ids) 40 self._update_blackboards(item_ids_by_standings) 41 42 def _update_blackboards(self, item_ids_by_standings): 43 for standings_class, item_ids in item_ids_by_standings.iteritems(): 44 standings_addresses = self.address_by_class[standings_class] 45 self._update_blackboards_for_standing(item_ids, standings_addresses) 46 47 def _get_item_ids_by_standings_class(self, item_ids): 48 item_ids_by_standings = defaultdict(set) 49 for item_id in item_ids: 50 owner_id = get_item_owner(self, item_id) 51 standings = get_standings_between(self, owner_id) 52 standing_thresholds = get_response_thresholds(self) 53 standings_class = classify_standings(standing_thresholds.hostile_response_threshold, standing_thresholds.friendly_response_threshold, standings) 54 if standings_class in self.address_by_class: 55 item_ids_by_standings[standings_class].add(item_id) 56 57 return item_ids_by_standings 58 59 def _map_address_to_class(self, standings_class, item_ids_address_name, flag_address_name, last_position_name): 60 item_ids_address = getattr(self.attributes, item_ids_address_name, None) 61 flag_address = getattr(self.attributes, flag_address_name, None) 62 last_position_address = getattr(self.attributes, last_position_name, None) 63 if item_ids_address: 64 self.address_by_class[standings_class] = StandingsAddresses(item_ids_address, flag_address, last_position_address) 65 66 def _update_blackboards_for_standing(self, detected_item_ids, standings_addresses): 67 stored_item_ids = self.GetLastBlackboardValue(standings_addresses.item_ids) or set() 68 new_item_ids = detected_item_ids.difference(stored_item_ids) 69 if new_item_ids: 70 self.SendBlackboardValue(standings_addresses.item_ids, stored_item_ids.union(new_item_ids)) 71 if standings_addresses.flag: 72 self.SendBlackboardValue(standings_addresses.flag, True) 73 if standings_addresses.last_position: 74 item_id = random.choice(list(new_item_ids)) 75 last_position = get_ball_position(self, item_id) 76 self.SendBlackboardValue(standings_addresses.last_position, last_position)