entities.py (9417B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\behaviors\actions\entities.py 2 import logging 3 import uthread2 4 from behaviors import status 5 from behaviors.behaviortree import UnrecoverableBehaviorError 6 from behaviors.const.combat import FW_ATTACK_NONE 7 from behaviors.tasks import Task 8 from behaviors.utility.inventory import remove_all_items, add_type_to_cargo, get_cargo_used_fraction, get_cargo_used, get_cargo_capacity_max 9 from ccpProfile import TimedFunction 10 from spacecomponents.common.helper import HasCargoBayComponent 11 logger = logging.getLogger(__name__) 12 SELF_UNSPAWN_SLEEP_DELAY_SEC = 0.2 13 14 class AddTypeAndAmountToAdditionalLoot(Task): 15 16 @TimedFunction('behaviors::actions::entities::AddTypeAndAmountToAdditionalLoot::OnEnter') 17 def OnEnter(self): 18 typeId = self.GetLastBlackboardValue(self.attributes.typeIdAddress) 19 if typeId is not None: 20 self.context.myBall.AddTypeAndAmountToAdditionalLoot(typeId, self.attributes.amount, self.attributes.allowOverload) 21 self.SetStatusToSuccess() 22 23 24 class AddBlackboardTypeAndBlackboardAmountToAdditionalLoot(Task): 25 26 @TimedFunction('behaviors::actions::entities::AddTypeAndAmountToAdditionalLoot::OnEnter') 27 def OnEnter(self): 28 typeId = self.GetLastBlackboardValue(self.attributes.typeIdAddress) 29 amount = self.GetLastBlackboardValue(self.attributes.amountAddress) 30 if typeId is not None and amount is not None: 31 self.context.myBall.AddTypeAndAmountToAdditionalLoot(typeId, amount, self.attributes.allowOverload) 32 self.SetStatusToSuccess() 33 34 35 class AddLootTableToAdditionalLoot(Task): 36 37 @TimedFunction('behaviors::actions::entities::AddLootTableToAdditionalLoot::OnEnter') 38 def OnEnter(self): 39 lootTableId = self.attributes.lootTableId 40 if lootTableId is not None: 41 self.context.myBall.AddLootTableToAdditionalLoot(lootTableId) 42 self.SetStatusToSuccess() 43 44 45 class RemoveTypesFromAdditionalLoot(Task): 46 47 @TimedFunction('behaviors::actions::entities::RemoveTypesFromAdditionalLoot::OnEnter') 48 def OnEnter(self): 49 typeIds = self.attributes.typeIds 50 for typeId in typeIds: 51 logger.debug('Removing type: %s loot from Entity: %s' % (typeId, self.context.myItemId)) 52 self.context.myBall.RemoveTypeFromAdditionalLoot(typeId) 53 54 self.status = status.TaskSuccessStatus 55 56 57 class MoveAdditionalLootBetweenEntities(Task): 58 59 @TimedFunction('behaviors::actions::entities::PopAdditionalLootFromEntity::OnEnter') 60 def OnEnter(self): 61 self.SetStatusToFailed() 62 entityId = self.GetLastBlackboardValue(self.attributes.entityIdAddress) 63 if not entityId: 64 return 65 hasBall = self.context.ballpark.HasBall(entityId) 66 if not hasBall: 67 return 68 self._MoveAdditionalLootFromAnotherEntityToMe(entityId) 69 self.SetStatusToSuccess() 70 71 def _MoveAdditionalLootFromAnotherEntityToMe(self, entityId): 72 targetEntityBall = self.context.ballpark.GetBall(entityId) 73 additionalLoot = targetEntityBall.RemoveAdditionalLoot() 74 for typeId, amount in additionalLoot.iteritems(): 75 self.context.myBall.AddTypeAndAmountToAdditionalLoot(typeId, amount) 76 77 78 class SelectGroupLeaderAction(Task): 79 80 @TimedFunction('behaviors::actions::entities::SelectGroupLeaderAction::OnEnter') 81 def OnEnter(self): 82 try: 83 groupManager = self.context.entityLocation.GetGroupManager() 84 group = groupManager.GetGroup(self.context.myEntityGroupId) 85 groupLeaderId = min(group.GetGroupMembers()) 86 except (KeyError, ValueError) as e: 87 raise UnrecoverableBehaviorError('Unable to get own group members: %s' % e) 88 89 self.SendBlackboardValue(self.attributes.groupLeaderIdAddress, groupLeaderId) 90 self.SetStatusToSuccess() 91 92 93 class UnspawnSelf(Task): 94 95 @TimedFunction('behaviors::actions::entities::UnspawnSelf::OnEnter') 96 def OnEnter(self): 97 self.SetStatusToSuspended() 98 self.behaviorTree.BlockReset(self) 99 uthread2.StartTasklet(self._UnspawnSelf) 100 101 def _UnspawnSelf(self): 102 uthread2.sleep(SELF_UNSPAWN_SLEEP_DELAY_SEC) 103 self.clear_inventory() 104 self.context.ballpark.UnspawnEntityBall(self.context.myItemId) 105 106 def clear_inventory(self): 107 if HasCargoBayComponent(self.context.mySlimItem.typeID): 108 remove_all_items(self, self.context.myItemId) 109 110 111 class UpdateTaleContextForPersistedObject(Task): 112 113 @TimedFunction('behaviors::actions::entities::UpdateTaleContextForPersistedObject::OnEnter') 114 def OnEnter(self): 115 self.SetStatusToSuccess() 116 if self.context.get('myTaleId') is not None: 117 return 118 taleId = self._GetTaleIdFromTaleManager() 119 if not taleId: 120 self.SetStatusToFailed() 121 logger.debug('Behavior: %s failed getting taleId from the DB', self.context.myItemId) 122 self.context.myTaleId = taleId 123 124 def _GetTaleIdFromTaleManager(self): 125 taleManager = self._GetTaleManager() 126 persistedObject = taleManager.GetTaleForPersistedObject(self.context.myItemId) 127 if len(persistedObject): 128 return persistedObject[0].taleID 129 130 def _GetTaleManager(self): 131 return sm.GetService('taleMgr') 132 133 134 class DestroySelf(Task): 135 136 def OnEnter(self): 137 self.SetStatusToSuspended() 138 uthread2.start_tasklet(self._destroy_self) 139 140 def _destroy_self(self): 141 self.context.ballpark.ExplodeEx(self.context.myItemId) 142 143 144 class UpdateStandingThresholdsForEntity(Task): 145 146 @TimedFunction('behaviors::actions::entities::UpdateStandingThresholdsForEntity::OnEnter') 147 def OnEnter(self): 148 self.context.ballpark.UpdateSlimItemFieldMulti(self.context.myItemId, hostile_response_threshold=self.attributes.hostileResponseThreshold, friendly_response_threshold=self.attributes.friendlyResponseThreshold) 149 self.SetStatusToSuccess() 150 151 152 class UpdateFwAttackMethodForEntity(Task): 153 154 @TimedFunction('behaviors::actions::entities::UpdateFwAttackMethodForEntity::OnEnter') 155 def OnEnter(self): 156 fwAttackMethod = self.attributes.fwAttackMethod 157 if fwAttackMethod == FW_ATTACK_NONE: 158 fwAttackMethod = None 159 self.context.ballpark.UpdateSlimItemField(self.context.myItemId, 'fwAttackMethod', fwAttackMethod) 160 groupStandingsDisabled = self.attributes.groupStandingsDisabled 161 if groupStandingsDisabled: 162 self.context.ballpark.UpdateSlimItemField(self.context.myItemId, 'group_standings_disabled', groupStandingsDisabled) 163 self.SetStatusToSuccess() 164 165 166 class CountBehaviorEntitiesOfTypeInSystem(Task): 167 168 def OnEnter(self): 169 ballpark = self.context.ballpark 170 solarsystemID = ballpark.solarsystemID 171 entityLocation = ballpark.broker.entity.GetEntityLocation(solarsystemID) 172 typeIDsToCount = self.attributes.typeIDsToCount 173 behaviorTreeManager = entityLocation.GetBehaviorTreeManager() 174 count = 0 175 behaviorEntriesByKey = behaviorTreeManager.GetAllBehaviorEntriesByKey() 176 for entryKey in behaviorEntriesByKey: 177 if entryKey in ballpark.slims and ballpark.slims[entryKey].typeID in typeIDsToCount: 178 count += 1 179 180 self.SendBlackboardValue(self.attributes.resultAddress, count) 181 self.SetStatusToSuccess() 182 183 184 class SendCargoUsedFractionToBlackboard(Task): 185 186 @TimedFunction('behaviors::actions::entities::SendCargoUsedFractionToBlackboard::OnEnter') 187 def OnEnter(self): 188 if HasCargoBayComponent(self.context.mySlimItem.typeID): 189 cargoUsedFraction = get_cargo_used_fraction(self, self.context.myItemId) 190 self.SendBlackboardValue(self.attributes.blackboardAddress, cargoUsedFraction) 191 self.SetStatusToSuccess() 192 193 194 class SendCargoUsedToBlackboard(Task): 195 196 @TimedFunction('behaviors::actions::entities::SendCargoUsedToBlackboard::OnEnter') 197 def OnEnter(self): 198 if HasCargoBayComponent(self.context.mySlimItem.typeID): 199 cargoUsed = get_cargo_used(self, self.context.myItemId) 200 self.SendBlackboardValue(self.attributes.blackboardAddress, cargoUsed) 201 self.SetStatusToSuccess() 202 203 204 class SendCargoCapacityToBlackboard(Task): 205 206 @TimedFunction('behaviors::actions::entities::SendCargoCapacityToBlackboard::OnEnter') 207 def OnEnter(self): 208 if HasCargoBayComponent(self.context.mySlimItem.typeID): 209 cargoCapacity = get_cargo_capacity_max(self, self.context.myItemId) 210 self.SendBlackboardValue(self.attributes.blackboardAddress, cargoCapacity) 211 self.SetStatusToSuccess() 212 213 214 class RemoveAllCargo(Task): 215 216 @TimedFunction('behaviors::actions::entities::RemoveAllCargo::OnEnter') 217 def OnEnter(self): 218 if HasCargoBayComponent(self.context.mySlimItem.typeID): 219 remove_all_items(self, self.context.myItemId) 220 self.SetStatusToSuccess() 221 222 223 class AddTypeAndAmountToCargo(Task): 224 225 @TimedFunction('behaviors::actions::entities::AddTypeAndAmountToCargo::OnEnter') 226 def OnEnter(self): 227 if HasCargoBayComponent(self.context.mySlimItem.typeID): 228 typeId = self.attributes.typeId 229 amount = self.attributes.amount 230 if typeId is not None and amount is not None: 231 add_type_to_cargo(self, self.context.myItemId, typeId, amount) 232 self.SetStatusToSuccess()