Class WeightedMultiBehavior

java.lang.Object
cn.nukkit.entity.ai.behavior.AbstractBehavior
cn.nukkit.entity.ai.behavior.WeightedMultiBehavior
All Implemented Interfaces:
IBehavior, IBehaviorEvaluator, IBehaviorExecutor

@PowerNukkitXOnly @Since("1.6.0.0-PNX") public class WeightedMultiBehavior extends AbstractBehavior
由多个行为IBehavior组成的组(注意和行为组IBehaviorGroup区分)
调用方法execute(EntityIntelligent)前,必须调用此对象的评估函数以确认激活的是哪个行为
在评估时,会评估所有包含的子行为
筛选出返回成功的行为后,会选取最高优先级的那一组
如果到这一步依然存在多个行为,则会根据行为的IBehavior.getWeight()方法的返回值随机选取其中一个作为执行行为

A group consisting of multiple behaviors IBehavior (note the distinction with behavior groups IBehaviorGroup)
Before calling the method execute(EntityIntelligent), the evaluation function of this object must be called to confirm which behavior is activated
During evaluation, all contained child behaviors are evaluated
After filtering out the behaviors that return success, the group with the highest priority is selected
If there are still multiple behaviors at this point, one of them is randomly selected for execution based on the return value of the IBehavior.getWeight() method of the behavior

  • Field Details

    • priority

      protected final int priority
      此组的优先级。在BehaviorGroup中,获取优先级将会返回此值指代整个组的优先级

      The priority of this group. In Behavior Group, getting the priority will return this value to refer to the priority of the entire group

    • behaviors

      protected Set<IBehavior> behaviors
    • currentBehavior

      protected IBehavior currentBehavior
  • Constructor Details

    • WeightedMultiBehavior

      public WeightedMultiBehavior(int priority, IBehavior... behaviors)
    • WeightedMultiBehavior

      public WeightedMultiBehavior(int priority, Set<IBehavior> behaviors)
  • Method Details

    • evaluate

      public boolean evaluate(EntityIntelligent entity)
      Description copied from interface: IBehaviorEvaluator
      是否需要激活与其绑定的执行器

      这个方法对一个行为只会评估一次,评估通过则开始运行执行器执行行为,直到行为中断或者完成,下一次评估才会开始

      Whether the executor bound to it needs to be activated

      This method evaluates a behavior only once, and if the evaluation passes, the executor execution behavior will start running until the behavior is interrupted or completed, and the next evaluation will not begin

      Parameters:
      entity - 评估目标实体
      Assess the targetEntity
      Returns:
      是否需要激活
      Do you need to activate
    • execute

      public boolean execute(EntityIntelligent entity)
      Description copied from interface: IBehaviorExecutor
      调度器将会持续执行此执行器,直到返回false,或者执行器被中断
      此方法每gt都会调用

      The scheduler will continue to execute this executor until it returns false or the executor is interrupted
      This method will be called every gt

      Parameters:
      entity - 执行目标实体
      Returns:
      boolean
    • onInterrupt

      public void onInterrupt(EntityIntelligent entity)
      Description copied from interface: IBehaviorExecutor
      行为非正常中断时(例如被更高级行为覆盖)调用

      Called when behavior breaks abnormally (e.g. overridden by higher-level behavior)

      Parameters:
      entity - 目标实体
    • onStart

      public void onStart(EntityIntelligent entity)
      Description copied from interface: IBehaviorExecutor
      行为评估成功后,进入激活状态前调用

      After the behavior evaluation is successful, it is called before entering the active state

      Parameters:
      entity - 目标实体
    • onStop

      public void onStop(EntityIntelligent entity)
      Description copied from interface: IBehaviorExecutor
      行为正常结束时(execute()方法返回false)调用

      Called when the behavior ends normally (the execute() method returns false)

      Parameters:
      entity - 目标实体
    • evaluateBehaviors

      protected Set<IBehavior> evaluateBehaviors(EntityIntelligent entity)
      Parameters:
      entity - 实体
      Returns:
      最高优先级且评估成功的一组行为(包含评估结果)
    • getPriority

      public int getPriority()
      此组的优先级。在BehaviorGroup中,获取优先级将会返回此值指代整个组的优先级

      The priority of this group. In Behavior Group, getting the priority will return this value to refer to the priority of the entire group

      Returns:
      优先级
    • getBehaviors

      public Set<IBehavior> getBehaviors()
    • getCurrentBehavior

      public IBehavior getCurrentBehavior()
    • setCurrentBehavior

      public void setCurrentBehavior(IBehavior currentBehavior)