Esempio n. 1
0
            void UpdateAI()
            {
                if (Pause())
                {
                    if (type == AType.Aggressive && target != null)
                    {
                        npc.ServerRotation = GetRotationToTarget(target.transform.position);
                    }
                    return;
                }
                if (state == AIState.Sleeping)
                {
                    return;
                }
                CheckStuck();
                lastPosition = entity.transform.position;
                if (type == AType.Passive)
                {
                    if (state == AIState.Scared)
                    {
                        target = GetTargetVis(entity, config.FindTargetRadius, animalConfig.attackAnimals, blockedTargets, false);
                    }
                    else
                    {
                        target = GetTargetVis(entity, config.FindTargetRadius / 2, animalConfig.attackAnimals, blockedTargets, false);
                    }
                    if (target != null)
                    {
                        state = AIState.Scared;
                    }
                    else
                    {
                        state = AIState.Walking;
                    }
                    Walk();
                    return;
                }
                if (IsLowHP())
                {
                    state = AIState.Walking;
                    Walk();
                    return;
                }
                var newTarget = GetTargetVis(entity, config.FindTargetRadius, animalConfig.attackAnimals, blockedTargets);

                if (target != newTarget && IsOriginalPassive())
                {
                    return;
                }
                target = newTarget;
                if (!CanAttack(target))
                {
                    target = GetTargetVis(entity, config.FindTargetRadius, animalConfig.attackAnimals, blockedTargets);
                }
                if (state == AIState.Walking)
                {
                    if (target == null)
                    {
                        Walk();
                    }
                    else
                    {
                        state = AIState.Attack;
                    }
                }
                if (state == AIState.Attack)
                {
                    if (target == null)
                    {
                        IsOriginalPassive();
                        state = AIState.Walking;
                    }
                    else
                    {
                        var distance = Vector3.Distance(entity.transform.position, target.transform.position);
                        if (distance > config.FindTargetRadius)
                        {
                            state  = AIState.Walking;
                            target = null;
                            IsOriginalPassive();
                            return;
                        }
                        if (distance > animalConfig.attackRange)
                        {
                            MoveToTarget(target.transform.position);
                        }
                        if (distance <= animalConfig.attackRange)
                        {
                            if (!Physics.Linecast(target.transform.position, entity.transform.position, blockLayer))
                            {
                                Attack();
                            }
                            else
                            {
                                MoveToTarget(target.transform.position);
                            }
                        }
                        npc.SendNetworkUpdate(global::BasePlayer.NetworkQueue.Update);
                    }
                }
            }