weighted.py (913B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\behaviors\composites\weighted.py 2 from behaviors.composites import Composite 3 from evemath import weighted_choice 4 5 class WeightsMissingError(Exception): 6 pass 7 8 9 class ToManyWeightsError(Exception): 10 pass 11 12 13 class WeightedSelector(Composite): 14 15 def GetInitialTask(self): 16 num_weights = len(self.attributes.subTaskWeights) 17 num_tasks = self.SubTaskCount() 18 if num_weights < num_tasks: 19 raise WeightsMissingError() 20 elif num_tasks < num_weights: 21 raise ToManyWeightsError() 22 num_tasks = max(len(self.attributes.subTaskWeights), self.SubTaskCount()) 23 choices = [ (self._subTasks[i], self.attributes.subTaskWeights[i]) for i in xrange(num_tasks) ] 24 return weighted_choice(choices) 25 26 def DoSubTaskCompleted(self, task): 27 self.status = task.status