Пример #1
0
    protected virtual IEnumerator Navigate(Vector3 destination, Item item = null)
    {
        var srcWp = Navigation.instance.NearestTo(transform.position, ownerType);
        var dstWp = Navigation.instance.NearestTo(destination, ownerType);

        _gizmoRealTarget = dstWp;
        MapNode reachedDst = srcWp;

        if (srcWp != dstWp)
        {
            var path = _gizmoPath = AStarNormal <MapNode> .Run(
                srcWp
                , dstWp
                , (mapNodeA, mapNodeB) => Vector3.Distance(mapNodeA.position, mapNodeB.position)
                , mapNode => mapNode == dstWp
                , mapNode =>

                mapNode.adjacent
                .Select(a => new AStarNormal <MapNode> .Arc(a, Vector3.Distance(a.position, mapNode.position)))
                );

            if (path != null)
            {
                foreach (var next in path.Select(w => FloorPos(w.position)))
                {
                    while ((next - FloorPos(this)).sqrMagnitude >= 0.05f)
                    {
                        _vel = (next - FloorPos(this)).normalized;
                        yield return(null);
                    }
                }
            }
            reachedDst = path.Last();
        }

        if (reachedDst == dstWp)
        {
            _vel = (FloorPos(destination) - FloorPos(this)).normalized;
            yield return(new WaitUntil(() => (FloorPos(destination) - FloorPos(this)).sqrMagnitude < (item ? item.radius : 1f)));
        }

        _vel = Vector3.zero;
        OnReachDestination(this, reachedDst, reachedDst == dstWp);
        OnReachDestinationWithItem(this, item);
    }
Пример #2
0
    public bool Reachable(Vector3 from, Vector3 to, List <Tuple <Vector3, Vector3> > debugRayList, string owner)
    {
        var srcWp = NearestTo(from, owner);
        var dstWp = NearestTo(to, owner);

        MapNode mapNode = srcWp;

        if (srcWp != dstWp)
        {
            var path = AStarNormal <MapNode> .Run(
                srcWp
                , dstWp
                , (wa, wb) => Vector3.Distance(wa.position, wb.position)
                , w => w == dstWp
                , w =>
                w.adjacent
                .Select(a => new AStarNormal <MapNode> .Arc(a, Vector3.Distance(a.position, w.position)))
                );

            if (path == null)
            {
                return(false);
            }

            mapNode = path.Last();
        }
        //	Debug.Log("Reachable from " + wp.name);
        if (debugRayList != null)
        {
            debugRayList.Add(Tuple.Create(mapNode.position, to));
        }

        var delta    = (to - mapNode.position);
        var distance = delta.magnitude;

        return(!Physics.Raycast(mapNode.position, delta / distance, distance, LayerMask.GetMask(new [] { "Blocking" })));
    }
    public static IEnumerable <GOAPAction> GoapRun(GOAPState from, GOAPState to, IEnumerable <GOAPAction> actions, bool aggressive_heuristic)
    {
        int watchdog = 300;

        var seq = AStarNormal <GOAPState> .Run(
            from,
            to,
            (curr, goal) => {
            if (aggressive_heuristic)
            {
                return(curr.AgressiveHeuristic(goal.worldSpace));
            }
            else
            {
                return(curr.PassiveHeuristic(goal.worldSpace));
            }
        },

            curr =>
        {
            if (aggressive_heuristic)
            {
                return(curr.AgressiveComparison(to.worldSpace));
            }
            else
            {
                return(curr.PassiveComparison(to.worldSpace));
            }
        },

            /*to.boolValues.All(kv => kv.In(curr.boolValues))&&
             *      to.intValues.All(i => i.In(curr.intValues)) &&
             *      to.floatValues.All(i => i.In(curr.floatValues)) &&
             *      to.stringValues.All(i => i.In(curr.stringValues)) ,*/

            curr =>
        {
            if (watchdog == 0)
            {
                return(Enumerable.Empty <AStarNormal <GOAPState> .Arc>());
            }
            else
            {
                watchdog--;
            }

            //return actions.Where(action => action.preconditionsBool.All(kv => kv.In(curr.boolValues)) &&
            // action.preconditionsInt.All(k=> k.In(curr.intValues)))
            return(actions.Where(action => action.preConditions.All(f => f(curr.worldSpace) == true))
                   .Aggregate(new FList <AStarNormal <GOAPState> .Arc>(), (possibleList, action) =>
            {
                var newState = new GOAPState(curr);
                //    newState.boolValues.UpdateWith(action.effectsBool);
                //    newState.intValues.UpdateWith(action.effectsInt);
                action.effects.ForEach((f) =>
                {
                    f(newState.worldSpace);
                });
                newState.generatingAction = action;
                newState.step = curr.step + 1;
                return possibleList + new AStarNormal <GOAPState> .Arc(newState, action.cost);
            }));
        });

        if (seq == null)
        {
            Debug.Log("Imposible planear");
            return(null);
        }

        foreach (var act in seq.Skip(1))
        {
            Debug.Log(act);
        }

        Debug.Log("WATCHDOG " + watchdog);

        return(seq.Skip(1).Select(x => x.generatingAction));
    }