NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

blackboards.py (11453B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\behaviors\actions\blackboards.py
      2 from copy import copy
      3 from behaviors.blackboards.scopes import GetBlackboardChannelNameFromAddress
      4 from behaviors.blackboards.scopes import GetBlackboardScopeFromAddress
      5 from behaviors.blackboards.scopes import Scope
      6 from behaviors.tasks import Task
      7 from ccpProfile import TimedFunction
      8 from collections import defaultdict
      9 
     10 class BlackboardSendMyItemIdMessageAction(Task):
     11 
     12     @TimedFunction('behaviors::actions::blackboards::BlackboardContextMessageAction::OnEnter')
     13     def OnEnter(self):
     14         self.SendBlackboardValue(self.attributes.messageAddress, self.GetContextValue())
     15         self.SetStatusToSuccess()
     16 
     17     def GetContextValue(self):
     18         return self.context.myItemId
     19 
     20 
     21 class BlackboardSendMyEntityGroupIdMessageAction(BlackboardSendMyItemIdMessageAction):
     22 
     23     def GetContextValue(self):
     24         return self.context.myEntityGroupId
     25 
     26 
     27 class BlackboardCopyMessageAction(Task):
     28 
     29     @TimedFunction('behaviors::actions::blackboards::BlackboardCopyMessageAction::OnEnter')
     30     def OnEnter(self):
     31         source = self._get_source_message()
     32         self.SendBlackboardValue(self.attributes.targetMessageAddress, copy(source))
     33         self.SetStatusToSuccess()
     34 
     35     def _get_source_message(self):
     36         return self.GetLastBlackboardValue(self.attributes.sourceMessageAddress)
     37 
     38 
     39 class BlackboardSetMessageAsNoneAction(Task):
     40 
     41     @TimedFunction('behaviors::actions::blackboards::BlackboardSetMessageAsNoneAction::OnEnter')
     42     def OnEnter(self):
     43         self.SendBlackboardValue(self.attributes.messageAddress, self.GetValue())
     44         self.SetStatusToSuccess()
     45 
     46     def GetValue(self):
     47         return None
     48 
     49 
     50 class BlackboardSetMessageToBooleanValueAction(BlackboardSetMessageAsNoneAction):
     51 
     52     def GetValue(self):
     53         return self.attributes.value
     54 
     55 
     56 class BlackboardSetMessageToIntegerValueAction(BlackboardSetMessageAsNoneAction):
     57 
     58     def GetValue(self):
     59         return self.attributes.value
     60 
     61 
     62 class BlackboardSetMessageToFloatValueAction(BlackboardSetMessageAsNoneAction):
     63 
     64     def GetValue(self):
     65         return self.attributes.value
     66 
     67 
     68 class BlackboardSetMessageToStringValueAction(BlackboardSetMessageAsNoneAction):
     69 
     70     def GetValue(self):
     71         return self.attributes.value
     72 
     73 
     74 class BlackboardSendItemBubbleIdMessage(Task):
     75 
     76     def OnEnter(self):
     77         itemId = self.GetLastBlackboardValue(self.attributes.itemIdSourceAddress)
     78         ball = self.context.ballpark.balls.get(itemId, None)
     79         if ball is None:
     80             self.SetStatusToFailed()
     81         else:
     82             self.SendBlackboardValue(self.attributes.bubbleIdTargetAddress, ball.newBubbleId)
     83             self.SetStatusToSuccess()
     84 
     85 
     86 class BlackboardIncrementCounter(Task):
     87 
     88     def OnEnter(self):
     89         counter = self.GetLastBlackboardValue(self.attributes.counterAddress) or 0
     90         self.SendBlackboardValue(self.attributes.counterAddress, counter + 1)
     91         self.SetStatusToSuccess()
     92 
     93 
     94 class BlackboardDecrementCounter(Task):
     95 
     96     def OnEnter(self):
     97         counter = self.GetLastBlackboardValue(self.attributes.counterAddress) or 0
     98         self.SendBlackboardValue(self.attributes.counterAddress, counter - 1)
     99         self.SetStatusToSuccess()
    100 
    101 
    102 class BlackboardAddNumericValues(Task):
    103 
    104     def OnEnter(self):
    105         first_value = self.GetLastBlackboardValue(self.attributes.firstValueAddress) or 0
    106         second_value = self.GetLastBlackboardValue(self.attributes.secondValueAddress) or 0
    107         result_value = first_value + second_value
    108         self.SendBlackboardValue(self.attributes.resultValueAddress, result_value)
    109         self.SetStatusToSuccess()
    110 
    111 
    112 class BlackboardMergeCollectionsIntoSet(Task):
    113 
    114     def OnEnter(self):
    115         self.SetStatusToSuccess()
    116         firstCollection = set(self.GetLastBlackboardValue(self.attributes.firstCollectionAddress) or [])
    117         secondCollection = set(self.GetLastBlackboardValue(self.attributes.secondCollectionAddress) or [])
    118         resultSet = firstCollection.union(secondCollection)
    119         self.SendBlackboardValue(self.attributes.resultSetAddress, resultSet)
    120 
    121 
    122 class BlackboardRemoveCollectionFromSet(Task):
    123 
    124     def OnEnter(self):
    125         self.SetStatusToSuccess()
    126         collection = set(self.GetLastBlackboardValue(self.attributes.collectionAddress) or [])
    127         targetSet = self.GetLastBlackboardValue(self.attributes.targetSetAddress) or set()
    128         resultSet = targetSet.difference(collection)
    129         self.SendBlackboardValue(self.attributes.targetSetAddress, resultSet)
    130 
    131 
    132 class BlackboardSetBooleanFlags(Task):
    133 
    134     @TimedFunction('behaviors::actions::blackboards::BlackboardSetBooleanFlags::OnEnter')
    135     def OnEnter(self):
    136         for address in self.attributes.flagAddressList:
    137             self.SendBlackboardValue(address, True)
    138 
    139         self.SetStatusToSuccess()
    140 
    141 
    142 class BlackboardClearBooleanFlags(Task):
    143 
    144     @TimedFunction('behaviors::actions::blackboards::BlackboardClearBooleanFlags::OnEnter')
    145     def OnEnter(self):
    146         for address in self.attributes.flagAddressList:
    147             self.SendBlackboardValue(address, False)
    148 
    149         self.SetStatusToSuccess()
    150 
    151 
    152 class BlackboardAddBlackboardValueIntoBlackboardCollection(Task):
    153 
    154     @TimedFunction('behaviors::actions::blackboards::BlackboardAddBlackboardValueIntoBlackboardCollection::OnEnter')
    155     def OnEnter(self):
    156         self.SetStatusToSuccess()
    157         blackboard_value = self.GetLastBlackboardValue(self.attributes.valueAddress)
    158         if not self._is_blackboard_value_valid(blackboard_value):
    159             return
    160         blackboard_collection = set(self.GetLastBlackboardValue(self.attributes.collectionAddress) or [])
    161         if blackboard_value in blackboard_collection:
    162             return
    163         blackboard_collection.add(blackboard_value)
    164         self.SendBlackboardValue(self.attributes.collectionAddress, blackboard_collection)
    165 
    166     def _is_blackboard_value_valid(self, blackboard_value):
    167         return not (blackboard_value is None or blackboard_value == '')
    168 
    169 
    170 class BlackboardAddValueByKeyIntoBlackboardDictionary(Task):
    171 
    172     @TimedFunction('behaviors::actions::blackboards::BlackboardAddValueByKeyIntoBlackboardDictionary::OnEnter')
    173     def OnEnter(self):
    174         self.SetStatusToSuccess()
    175         blackboard_key = self._get_blackboard_key()
    176         if not self._is_blackboard_entry_valid(blackboard_key):
    177             return
    178         blackboard_value = self._get_blackboard_value()
    179         if not self._is_blackboard_entry_valid(blackboard_value):
    180             return
    181         self._update_blackboard_dictionary(blackboard_key, blackboard_value)
    182 
    183     def _get_blackboard_key(self):
    184         return self.GetLastBlackboardValue(self.attributes.keyAddress)
    185 
    186     def _is_blackboard_entry_valid(self, blackboard_entry):
    187         return not (blackboard_entry is None or blackboard_entry == '')
    188 
    189     def _get_blackboard_value(self):
    190         return self.GetLastBlackboardValue(self.attributes.valueAddress)
    191 
    192     def _update_blackboard_dictionary(self, blackboard_key, blackboard_value):
    193         blackboard_dictionary = self.GetLastBlackboardValue(self.attributes.dictionaryAddress) or defaultdict(set)
    194         if blackboard_value in blackboard_dictionary[blackboard_key]:
    195             return
    196         blackboard_dictionary[blackboard_key].add(blackboard_value)
    197         self.SendBlackboardValue(self.attributes.dictionaryAddress, blackboard_dictionary)
    198 
    199 
    200 class BlackboardSetValueByKeyInBlackboardDictionary(BlackboardAddValueByKeyIntoBlackboardDictionary):
    201 
    202     def _update_blackboard_dictionary(self, blackboard_key, blackboard_value):
    203         blackboard_dictionary = self.GetLastBlackboardValue(self.attributes.dictionaryAddress) or {}
    204         if blackboard_key in blackboard_dictionary and blackboard_dictionary[blackboard_key] == blackboard_value:
    205             return
    206         blackboard_dictionary[blackboard_key] = blackboard_value
    207         self.SendBlackboardValue(self.attributes.dictionaryAddress, blackboard_dictionary)
    208 
    209 
    210 class BlackboardSetValueBySelfTypeInBlackboardDictionary(BlackboardSetValueByKeyInBlackboardDictionary):
    211 
    212     def _get_blackboard_key(self):
    213         return self.context.mySlimItem.typeID
    214 
    215 
    216 class BlackboardAddSelfByKeyIntoBlackboardDictionary(BlackboardAddValueByKeyIntoBlackboardDictionary):
    217 
    218     def _get_blackboard_value(self):
    219         return self.context.myItemId
    220 
    221 
    222 class BlackboardAddValueBySelfIntoBlackboardDictionary(BlackboardAddValueByKeyIntoBlackboardDictionary):
    223 
    224     def _get_blackboard_key(self):
    225         return self.context.myItemId
    226 
    227 
    228 class BlackboardRemoveValueByKeyFromBlackboardDictionary(BlackboardAddValueByKeyIntoBlackboardDictionary):
    229 
    230     def _is_blackboard_key_valid(self, blackboard_key):
    231         return not (blackboard_key is None or blackboard_key == '')
    232 
    233     def _get_blackboard_value(self):
    234         return self.GetLastBlackboardValue(self.attributes.valueAddress)
    235 
    236     def _update_blackboard_dictionary(self, blackboard_key, blackboard_value):
    237         blackboard_dictionary = self.GetLastBlackboardValue(self.attributes.dictionaryAddress) or defaultdict(set)
    238         if not blackboard_dictionary or blackboard_value not in blackboard_dictionary[blackboard_key]:
    239             return
    240         blackboard_dictionary[blackboard_key].remove(blackboard_value)
    241         self.SendBlackboardValue(self.attributes.dictionaryAddress, blackboard_dictionary)
    242 
    243 
    244 class BlackboardCopyValueBySelfTypeFromBlackboardDictionary(BlackboardCopyMessageAction):
    245 
    246     def _get_source_message(self):
    247         dictionary_message = self.GetLastBlackboardValue(self.attributes.sourceMessageAddress)
    248         if dictionary_message is None:
    249             return
    250         return dictionary_message.get(self.context.mySlimItem.typeID)
    251 
    252 
    253 class BlackboardSetBooleanFlagsOnGroupRemoval(Task):
    254 
    255     def OnEnter(self):
    256         self.SetStatusToSuccess()
    257         if self._is_already_set():
    258             return
    259         self._set_boolean_flag_on_group_removal()
    260 
    261     def _is_already_set(self):
    262         return self.GetLastBlackboardValue(self.attributes.oneShotAddress) is True
    263 
    264     def _set_boolean_flag_on_group_removal(self):
    265         for message_address in self.attributes.messageAddressList:
    266             channel = self.GetChannelFromAddress(message_address)
    267             self.context.groupManager.AddGroupRemovalHandler(self.context.myEntityGroupId, channel.SendMessage, (self.attributes.value,))
    268 
    269         self.SendBlackboardValue(self.attributes.oneShotAddress, True)
    270 
    271 
    272 class BlackboardSetDictionaryValueByMyGroupId(BlackboardSetValueByKeyInBlackboardDictionary):
    273 
    274     def _get_blackboard_key(self):
    275         return self.context.myEntityGroupId
    276 
    277 
    278 class BroadcastBlackboardValueToBlackboards(Task):
    279 
    280     def OnEnter(self):
    281         self.SetStatusToSuccess()
    282         object_ids = self.GetLastBlackboardValue(self.attributes.objectIdsAddress)
    283         if not object_ids:
    284             return
    285         value = self.GetLastBlackboardValue(self.attributes.valueAddress)
    286         scope_type = GetBlackboardScopeFromAddress(self.attributes.targetAddress)
    287         channel_name = GetBlackboardChannelNameFromAddress(self.attributes.targetAddress)
    288         blackboard_manager = self.context.blackboardManager
    289         for object_id in object_ids:
    290             blackboard_id = Scope(scope_type, object_id)
    291             if not blackboard_manager.HasBlackboard(blackboard_id):
    292                 blackboard = blackboard_manager.GetBlackboard(blackboard_id)
    293                 blackboard.SendMessage(channel_name, value)