drones.py (5195B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\behaviors\actions\drones.py 2 from behaviors.actions.entityspawning import SpawnNewEntities 3 from behaviors.const.blackboardchannels import DRONE_CONTROLLER_ADDRESS, GROUP_PRIMARY_TARGET_CHANNEL_NAME 4 from behaviors.const.blackboardchannels import SELF_DESTRUCT_DRONE_ON_CONTROLLER_DEATH_ADDRESS 5 from behaviors.exceptions import BehaviorAuthoringException 6 from behaviors.tasks import Task 7 from behaviors.utility.entity_groups import entity_group_exists 8 from behaviors.utility.spawning import EntitySpawner, EntityGetter 9 from ccpProfile import TimedFunction 10 11 class SpawnDrones(SpawnNewEntities): 12 13 def _get_entity_getter(self): 14 return DroneGetter(self) 15 16 def _get_entity_spawner(self): 17 return DroneSpawner(self) 18 19 def _update_reinforcements_for_group(self, entity_count, existing_entity_group_id): 20 pass 21 22 def _register_entity_group_with_blackboard(self, drone_group_id): 23 existing_drone_group_id = self.GetLastBlackboardValue(self.attributes.droneGroupIdAddress) 24 if existing_drone_group_id is None: 25 self.SendBlackboardValue(self.attributes.droneGroupIdAddress, drone_group_id) 26 27 def _update_group_blackboard_channels(self, drone_group_id): 28 drone_group_id_channel = self.GetMessageChannelForGroupId(drone_group_id, DRONE_CONTROLLER_ADDRESS[1]) 29 if drone_group_id_channel.GetLastMessage() is None: 30 drone_group_id_channel.SendMessage(self.context.myItemId) 31 self_destruct_channel = self.GetMessageChannelForGroupId(drone_group_id, SELF_DESTRUCT_DRONE_ON_CONTROLLER_DEATH_ADDRESS[1]) 32 self_destruct_channel.SendMessage(self.attributes.shouldSelfDestructOnControllerDeath) 33 34 def get_spawn_position(self): 35 return (self.context.myBall.x, self.context.myBall.y, self.context.myBall.z) 36 37 38 class DroneGetter(EntityGetter): 39 40 def get_spawn_data_for_entities(self, max_npc_count = None): 41 drone_type_id = self._GetDroneTypeID() 42 drones_spawned_total = self.task.GetLastBlackboardValue(self.task.attributes.dronesSpawnedCountAddress) 43 max_drones = self._get_max_drones(drones_spawned_total) 44 active_drone_count = self._get_active_drone_count() 45 drone_count_for_spawning = int(max_drones - active_drone_count) 46 drones = {int(drone_type_id): drone_count_for_spawning} 47 self._reduce_drone_capacity(drones_spawned_total + drone_count_for_spawning) 48 behavior_override_id = self.task.get_behavior_override_id() 49 return self._get_spawn_data_for_entities(drones.iteritems(), behavior_override_id) 50 51 def _GetDroneTypeID(self): 52 drone_type_id_address = self.task.attributes.droneTypeIdAddress 53 if drone_type_id_address is not None: 54 blackboard_value = self.task.GetLastBlackboardValue(drone_type_id_address) 55 if blackboard_value is not None: 56 return blackboard_value 57 type_id = self.task.attributes.droneTypeId 58 if type_id is not None: 59 return type_id 60 raise BehaviorAuthoringException('BehaviorID: %s has no drone type ID defined for task: %s', self.behaviorTree.GetBehaviorId(), type(self).__name__) 61 62 def _get_max_drones(self, drones_spawned_total): 63 max_drone_count = self.task.GetLastBlackboardValue(self.task.attributes.maxDroneCountAddress) 64 max_drone_active_count = self.task.GetLastBlackboardValue(self.task.attributes.maxDroneActiveCountAddress) 65 return min(max_drone_count - drones_spawned_total, max_drone_active_count) 66 67 def _get_active_drone_count(self): 68 group_manager = self.task.context.entityLocation.GetGroupManager() 69 drone_group_id = self.task.GetLastBlackboardValue(self.task.attributes.droneGroupIdAddress) 70 if drone_group_id is None: 71 return 0 72 if not entity_group_exists(self.task, drone_group_id): 73 self.task.SendBlackboardValue(self.task.attributes.droneGroupIdAddress, None) 74 return 0 75 drone_group = group_manager.GetGroup(drone_group_id) 76 return drone_group.Count() 77 78 def _reduce_drone_capacity(self, drones_spawned_total): 79 self.task.SendBlackboardValue(self.task.attributes.dronesSpawnedCountAddress, drones_spawned_total) 80 81 82 class DroneSpawner(EntitySpawner): 83 84 def get_group_id(self): 85 drone_group_id = self.task.GetLastBlackboardValue(self.task.attributes.droneGroupIdAddress) 86 if entity_group_exists(self.task, drone_group_id): 87 return drone_group_id 88 89 90 class AssignTargetToDroneGroup(Task): 91 92 @TimedFunction('behaviors::actions::drones::AssignTargetToDroneGroup::OnEnter') 93 def OnEnter(self): 94 self.SetStatusToFailed() 95 drone_group_id = self.GetLastBlackboardValue(self.attributes.droneGroupIdAddress) 96 if drone_group_id is not None and entity_group_exists(self, drone_group_id): 97 target_id = self.GetLastBlackboardValue(self.attributes.targetIdAddress) 98 message_channel = self.GetMessageChannelForGroupId(drone_group_id, GROUP_PRIMARY_TARGET_CHANNEL_NAME) 99 message_channel.SendMessage(target_id) 100 self.SetStatusToSuccess()