⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 steeringbehavior.vb

📁 一个.Net下用VB编写的用于游戏的人工智能引擎
💻 VB
📖 第 1 页 / 共 5 页
字号:
        m_vSteeringForce.Truncate(m_pVehicle.MaxForce())

        Return m_vSteeringForce
    End Function

    '//---------------------- CalculatePrioritized ----------------------------
    '//
    '//  this method calls each active steering behavior in order of priority
    '//  and acumulates their forces until the max steering force magnitude
    '//  is reached, at which time the function returns the steering force 
    '//  accumulated to that  point
    '//------------------------------------------------------------------------
    Private Function CalculatePrioritized() As Vector2D
        Dim force As Vector2D

        If IsOn(behavior_type.wall_avoidance) Then
            force = WallAvoidance(m_pVehicle.World().Walls()).Mutiply(m_dWeightWallAvoidance)
            If Not AccumulateForce(m_vSteeringForce, force) Then Return m_vSteeringForce
        End If

        If IsOn(behavior_type.obstacle_avoidance) Then
            force = ObstacleAvoidance(m_pVehicle.World().Obstacles()).Mutiply(m_dWeightObstacleAvoidance)
            If Not AccumulateForce(m_vSteeringForce, force) Then Return m_vSteeringForce
        End If

        If IsOn(behavior_type.evade) Then
            'assert(m_pTargetAgent1 && "Evade target not assigned");
            force = Evade(m_pTargetAgent1).Mutiply(m_dWeightEvade)
            If Not AccumulateForce(m_vSteeringForce, force) Then Return m_vSteeringForce
        End If


        If IsOn(behavior_type.flee) Then
            force = Flee(m_pVehicle.World().Crosshair()).Mutiply(m_dWeightFlee)
            If Not AccumulateForce(m_vSteeringForce, force) Then Return m_vSteeringForce
        End If
        If Not isSpacePartitioningOn() Then
            If IsOn(behavior_type.separation) Then
                force = Separation(m_pVehicle.World().Agents()).Mutiply(m_dWeightSeparation)
                If Not AccumulateForce(m_vSteeringForce, force) Then Return m_vSteeringForce
            End If

            If IsOn(behavior_type.allignment) Then
                force = Alignment(m_pVehicle.World().Agents()).Mutiply(m_dWeightAlignment)
                If Not AccumulateForce(m_vSteeringForce, force) Then Return m_vSteeringForce
            End If

            If IsOn(behavior_type.cohesion) Then
                force = Cohesion(m_pVehicle.World().Agents()).Mutiply(m_dWeightCohesion)
                If Not AccumulateForce(m_vSteeringForce, force) Then Return m_vSteeringForce
            End If
        Else

            If IsOn(behavior_type.separation) Then
                force = SeparationPlus(m_pVehicle.World().Agents()).Mutiply(m_dWeightSeparation)
                If Not AccumulateForce(m_vSteeringForce, force) Then Return m_vSteeringForce
            End If

            If IsOn(behavior_type.allignment) Then
                force = AlignmentPlus(m_pVehicle.World().Agents()).Mutiply(m_dWeightAlignment)
                If Not AccumulateForce(m_vSteeringForce, force) Then Return m_vSteeringForce
            End If

            If IsOn(behavior_type.cohesion) Then
                force = CohesionPlus(m_pVehicle.World().Agents()).Mutiply(m_dWeightCohesion)
                If Not AccumulateForce(m_vSteeringForce, force) Then Return m_vSteeringForce
            End If

        End If

        If IsOn(behavior_type.seek) Then
            force = Seek(m_pVehicle.World().Crosshair()).Mutiply(m_dWeightSeek)
            If Not AccumulateForce(m_vSteeringForce, force) Then Return m_vSteeringForce
        End If

        If IsOn(behavior_type.arrive) Then
            force = Arrive(m_pVehicle.World().Crosshair(), m_Deceleration).Mutiply(m_dWeightArrive)
            If Not AccumulateForce(m_vSteeringForce, force) Then Return m_vSteeringForce
        End If

        If IsOn(behavior_type.wander) Then
            force = Wander().Mutiply(m_dWeightWander)
            If Not AccumulateForce(m_vSteeringForce, force) Then Return m_vSteeringForce
        End If

        If IsOn(behavior_type.pursuit) Then
            'assert(m_pTargetAgent1 && "pursuit target not assigned");
            force = Pursuit(m_pTargetAgent1).Mutiply(m_dWeightPursuit)
            If Not AccumulateForce(m_vSteeringForce, force) Then Return m_vSteeringForce
        End If

        If IsOn(behavior_type.offset_pursuit) Then
            'assert (m_pTargetAgent1 && "pursuit target not assigned");
            'assert (!m_vOffset.isZero() && "No offset assigned");
            force = OffsetPursuit(m_pTargetAgent1, m_vOffset)
            If Not AccumulateForce(m_vSteeringForce, force) Then Return m_vSteeringForce
        End If

        If IsOn(behavior_type.interpose) Then
            'assert (m_pTargetAgent1 && m_pTargetAgent2 && "Interpose agents not assigned");
            force = Interpose(m_pTargetAgent1, m_pTargetAgent2).Mutiply(m_dWeightInterpose)
            If Not AccumulateForce(m_vSteeringForce, force) Then Return m_vSteeringForce
        End If

        If IsOn(behavior_type.hide) Then
            'assert(m_pTargetAgent1 && "Hide target not assigned");
            force = Hide(m_pTargetAgent1, m_pVehicle.World().Obstacles()).Mutiply(m_dWeightHide)
            If Not AccumulateForce(m_vSteeringForce, force) Then Return m_vSteeringForce
        End If


        If IsOn(behavior_type.follow_path) Then
            force = FollowPath().Mutiply(m_dWeightFollowPath)
            If Not AccumulateForce(m_vSteeringForce, force) Then Return m_vSteeringForce
        End If

        Return m_vSteeringForce
    End Function

    '//helper method for Hide. Returns a position located on the other
    '//side of an obstacle to the pursuer
    '//---------------------- CalculateDithered ----------------------------
    '//
    '//  this method sums up the active behaviors by assigning a probabilty
    '//  of being calculated to each behavior. It then tests the first priority
    '//  to see if it should be calcukated this simulation-step. If so, it
    '//  calculates the steering force resulting from this behavior. If it is
    '//  more than zero it returns the force. If zero, or if the behavior is
    '//  skipped it continues onto the next priority, and so on.
    '//
    '//  NOTE: Not all of the behaviors have been implemented in this method,
    '//        just a few, so you get the general idea
    '//------------------------------------------------------------------------    

    Private Function CalculateDithered() As Vector2D
        '//reset the steering force
        m_vSteeringForce.Zero()

        If IsOn(behavior_type.wall_avoidance) And Utils.RandFloat() < Prm.prWallAvoidance Then
            m_vSteeringForce = WallAvoidance(m_pVehicle.World().Walls()).Mutiply(m_dWeightWallAvoidance / Prm.prWallAvoidance)
            If Not m_vSteeringForce.IsZero() Then
                m_vSteeringForce.Truncate(m_pVehicle.MaxForce())
                Return m_vSteeringForce
            End If
        End If

        If IsOn(behavior_type.obstacle_avoidance) And Utils.RandFloat() < Prm.prObstacleAvoidance Then
            m_vSteeringForce.PlusEqual(ObstacleAvoidance(m_pVehicle.World().Obstacles()).Mutiply(m_dWeightObstacleAvoidance / Prm.prObstacleAvoidance))
            If Not m_vSteeringForce.IsZero() Then
                m_vSteeringForce.Truncate(m_pVehicle.MaxForce())
                Return m_vSteeringForce
            End If
        End If

        If Not isSpacePartitioningOn() Then
            If IsOn(behavior_type.separation) And Utils.RandFloat() < Prm.prSeparation Then
                m_vSteeringForce.PlusEqual(Separation(m_pVehicle.World().Agents()).Mutiply(m_dWeightSeparation / Prm.prSeparation))
                If Not m_vSteeringForce.IsZero() Then
                    m_vSteeringForce.Truncate(m_pVehicle.MaxForce())
                    Return m_vSteeringForce
                End If
            End If
        Else
            If IsOn(behavior_type.separation) And Utils.RandFloat() < Prm.prSeparation Then
                m_vSteeringForce.PlusEqual(SeparationPlus(m_pVehicle.World().Agents()).Mutiply(m_dWeightSeparation / Prm.prSeparation))
                If Not m_vSteeringForce.IsZero() Then
                    m_vSteeringForce.Truncate(m_pVehicle.MaxForce())

                    Return m_vSteeringForce
                End If
            End If
        End If

        If IsOn(behavior_type.flee) And Utils.RandFloat() < Prm.prFlee Then
            m_vSteeringForce.PlusEqual(Flee(m_pVehicle.World().Crosshair()).Mutiply(m_dWeightFlee / Prm.prFlee))

            If Not m_vSteeringForce.IsZero() Then
                m_vSteeringForce.Truncate(m_pVehicle.MaxForce())
                Return m_vSteeringForce
            End If
        End If

        If IsOn(behavior_type.evade) And Utils.RandFloat() < Prm.prEvade Then
            'assert(m_pTargetAgent1 && "Evade target not assigned");
            m_vSteeringForce.PlusEqual(Evade(m_pTargetAgent1).Mutiply(m_dWeightEvade / Prm.prEvade))
            If Not m_vSteeringForce.IsZero() Then
                m_vSteeringForce.Truncate(m_pVehicle.MaxForce())
                Return m_vSteeringForce
            End If
        End If

        If Not isSpacePartitioningOn() Then
            If IsOn(behavior_type.allignment) And Utils.RandFloat() < Prm.prAlignment Then
                m_vSteeringForce.PlusEqual(Alignment(m_pVehicle.World().Agents()).Mutiply(m_dWeightAlignment / Prm.prAlignment))
                If Not m_vSteeringForce.IsZero() Then
                    m_vSteeringForce.Truncate(m_pVehicle.MaxForce())
                    Return m_vSteeringForce
                End If
            End If

            If IsOn(behavior_type.cohesion) And Utils.RandFloat() < Prm.prCohesion Then
                m_vSteeringForce.PlusEqual(Cohesion(m_pVehicle.World().Agents()).Mutiply(m_dWeightCohesion / Prm.prCohesion))
                If Not m_vSteeringForce.IsZero() Then
                    m_vSteeringForce.Truncate(m_pVehicle.MaxForce())
                    Return m_vSteeringForce
                End If
            End If
        Else
            If IsOn(behavior_type.allignment) And Utils.RandFloat() < Prm.prAlignment Then
                m_vSteeringForce.PlusEqual(AlignmentPlus(m_pVehicle.World().Agents()).Mutiply(m_dWeightAlignment / Prm.prAlignment))

                If Not m_vSteeringForce.IsZero() Then
                    m_vSteeringForce.Truncate(m_pVehicle.MaxForce())
                    Return m_vSteeringForce
                End If
            End If

            If IsOn(behavior_type.cohesion) And Utils.RandFloat() < Prm.prCohesion Then
                m_vSteeringForce.PlusEqual(CohesionPlus(m_pVehicle.World().Agents()).Mutiply(m_dWeightCohesion / Prm.prCohesion))
                If Not m_vSteeringForce.IsZero() Then
                    m_vSteeringForce.Truncate(m_pVehicle.MaxForce())
                    Return m_vSteeringForce
                End If
            End If
        End If

        If IsOn(behavior_type.wander) And Utils.RandFloat() < Prm.prWander Then
            m_vSteeringForce.PlusEqual(Wander().Mutiply(m_dWeightWander / Prm.prWander))
            If Not m_vSteeringForce.IsZero() Then
                m_vSteeringForce.Truncate(m_pVehicle.MaxForce())
                Return m_vSteeringForce
            End If
        End If

        If IsOn(behavior_type.seek) And Utils.RandFloat() < Prm.prSeek Then
            m_vSteeringForce.PlusEqual(Seek(m_pVehicle.World().Crosshair()).Mutiply(m_dWeightSeek / Prm.prSeek))
            If Not m_vSteeringForce.IsZero() Then
                m_vSteeringForce.Truncate(m_pVehicle.MaxForce())
                Return m_vSteeringForce
            End If
        End If

        If IsOn(behavior_type.arrive) And Utils.RandFloat() < Prm.prArrive Then
            m_vSteeringForce.PlusEqual(Arrive(m_pVehicle.World().Crosshair(), m_Deceleration).Mutiply(m_dWeightArrive / Prm.prArrive))
            If Not m_vSteeringForce.IsZero() Then
                m_vSteeringForce.Truncate(m_pVehicle.MaxForce())
                Return m_vSteeringForce
            End If
        End If

        Return m_vSteeringForce
    End Function

    '//------------------------- GetHidingPosition ----------------------------
    '//
    '//  Given the position of a hunter, and the position and radius of
    '//  an obstacle, this method calculates a position DistanceFromBoundary 
    '//  away from its bounding radius and directly opposite the hunter
    '//------------------------------------------------------------------------
    Private Function GetHidingPosition(ByVal posOb As Vector2D, ByVal radiusOb As Double, ByVal posHunter As Vector2D) As Vector2D
        '//calculate how far away the agent is to be from the chosen obstacle's
        '//bounding radius
        Dim DistanceFromBoundary As Double = 30.0
        Dim DistAway As Double = radiusOb + DistanceFromBoundary

        '//calculate the heading toward the object from the hunter
        Dim ToOb As Vector2D = Vector2D.Vec2DNormalize(posOb.Minus(posHunter))

        '//scale it to size and add to the obstacles position to get
        '//the hiding spot.
        Return ToOb.Mutiply(DistAway).Plus(posOb)

    End Function


    Public Sub New(ByVal agent As Vehicle)

        m_pVehicle = agent
        m_iFlags = 0
        m_dDBoxLength = Prm.MinDetectionBoxLength
        m_dWeightCohesion = Prm.CohesionWeight
        m_dWeightAlignment = Prm.AlignmentWeight
        m_dWeightSeparation = Prm.SeparationWeight
        m_dWeightObstacleAvoidance = Prm.ObstacleAvoidanceWeight
        m_dWeightWander = Prm.WanderWeight
        m_dWeightWallAvoidance = Prm.WallAvoidanceWeight
        m_dViewDistance = Prm.ViewDistance
        m_dWallDetectionFeelerLength = Prm.WallDetectionFeelerLength
        m_Feelers = New ArrayList
        m_Deceleration = Deceleration.normal
        m_pTargetAgent1 = Nothing
        m_pTargetAgent2 = Nothing
        m_dWanderDistance = WanderDist
        m_dWanderJitter = WanderJitterPerSec
        m_dWanderRadius = WanderRad
        m_dWaypointSeekDistSq = WaypointSeekDist * WaypointSeekDist
        m_dWeightSeek = Prm.SeekWeight
        m_dWeightFlee = Prm.FleeWeight
        m_dWeightArrive = Prm.ArriveWeight
        m_dWeightPursuit = Prm.PursuitWeight
        m_dWeightOffsetPursuit = Prm.OffsetPursuitWeight
        m_dWeightInterpose = Prm.InterposeWeight
        m_dWeightHide = Prm.HideWeight
        m_dWeightEvade = Prm.EvadeWeight
        m_dWeightFollowPath = Prm.FollowPathWeight
        m_bCellSpaceOn = False
        m_SummingMethod = summing_method.prioritized

        '//stuff for the wander behavior

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -