public void InsertToMemory(EMemoryEvent eventType, Vector2 position, Vector2 direction,
                               float remainTime = 1f, float matureTime = 0f, float shadeTime = 0f, float importance = 1f)
    {
        int id  = (int)eventType;
        var mem = eventMemory[id];

        MemoryEvent item = new MemoryEvent();

        /// no unit/ unknown unit responsible for the event
        item.unit    = null;/// otherwise insert new item
        item.hadUnit = false;

        /// spatial data
        item.exactPosition = position;
        item.direction     = direction;

        /// time data
        item.remainedTime = new Timer();
        item.remainedTime.Restart();
        item.matureTime    = matureTime;
        item.knowledgeTime = remainTime;
        item.shadeTime     = shadeTime;

        item.importance = importance;

        mem.Add(item);
        /// list is not sorted
        anyEventAdded[id] = true;
    }
示例#2
0
        protected void EmitEvent(MemoryEvent eventOut, bool disableSaveContext = true, bool reuseContext = true, bool afterExecution = false)
        {
            if (Context != null && reuseContext)
            {
                // Reusando o contexto
                var eventContext = Context.GetEvent();

                eventOut.InstanceId = Context.InstanceId;
                eventOut.Tag        = eventContext.Tag;
                eventOut.Branch     = eventContext.Branch;
                eventOut.Reprocess  = eventContext.Reprocess;

                if (!afterExecution)
                {
                    Context.Memory.Event.Name    = eventOut.Name;
                    Context.Memory.Event.Payload = eventOut.Payload;
                    Context.GetEvent().SetPayload((IPayload)eventOut.Payload);
                    Context.DisableSaveContext = disableSaveContext;

                    var processMemoryService =
                        (SDK.Services.ProcessMemory.IProcessMemoryService)SDK.Configuration.SDKConfiguration.ServiceProvider.GetService(
                            typeof(SDK.Services.ProcessMemory.IProcessMemoryService));
                    processMemoryService.Commit(Context.UpdateMemory());
                }
            }

            if (!afterExecution)
            {
                EventManagerService.Push(eventOut);
            }
            else
            {
                Context.EventsToSend.Add(eventOut);
            }
        }
示例#3
0
    void ReactToNoise(NoiseData data)
    {
        if (data.fraction && myUnit.fraction.GetAttitude(data.fraction) != AiFraction.EAttitude.EEnemy)
        {
            return;
        }

        Vector2 toNoise = (Vector2)transform.position - data.position;

        if (toNoise.sqrMagnitude > hearingDistance * hearingDistance || Random.value > reactionChance)
        {
            return;
        }

        MemoryEvent ev = new MemoryEvent();

        ev.velocity      = data.velocity * velocityPredictionScale;
        ev.forward       = data.velocity;
        ev.exactPosition = data.position;
        ev.lifetimeTimer.Restart();

        noiseStorage.PerceiveEvent(ev);

#if UNITY_EDITOR
        Debug.DrawRay(ev.exactPosition, Vector3.up, Color.blue, 0.5f);
#endif
    }
示例#4
0
    public void Update(BehaviourStateMachine stateMachine)
    {
        for (int i = 1; i <= memoryInterruptions.Count; ++i)
        {
            var it = memoryInterruptions[i - 1];

            MemoryEvent ev = stateMachine.memory.SearchInMemory(it.eventType);

            if (ev != null && ev.GetState() == it.eventState)
            {
                //Debug.Log(i + "; " + it.eventType + "; " + it.eventState);
                stateMachine.target = ev;
                if (lastInterruptionType != i)
                {
                    lastInterruptionType = i;
                    stateMachine.SetCurrentBehaviour(it.entry);
                }
                return;
            }
        }


        {
            //Debug.Log("neutral");
            stateMachine.target = null;

            if (lastInterruptionType != 0)
            {
                lastInterruptionType = 0;
                stateMachine.SetCurrentBehaviour(neutralEntry);
            }
        }
    }
示例#5
0
    new void Awake()
    {
        base.Awake();

        //// initialize focus
        painStorage = RegisterSenseInBlackboard("painStorage");
        var healthController = GetComponentInParent <HealthController>();

        healthController.onDamageCallback += (data) =>
        {
            if (!tPain.IsReadyRestart())
            {
                return;
            }

            MemoryEvent ev = new MemoryEvent();
            ev.exactPosition = data.position;
            ev.forward       = data.direction;
            // we have no information about hit velocity; just assume it is stationary
            ev.velocity = Vector2.zero;
            ev.lifetimeTimer.Restart();

            painStorage.PerceiveEvent(ev);

            Debug.DrawRay(ev.position, Vector3.up, Color.blue, 0.25f);
        };
    }
示例#6
0
    void InsertEvent(MemoryEvent ev, AiFraction.EAttitude attitude)
    {
        switch (attitude)
        {
        case AiFraction.EAttitude.EEnemy:
            if (trackEnemy)
            {
                enemyStorage.PerceiveEvent(ev);
            }
            break;

        case AiFraction.EAttitude.EFriendly:
            if (trackAlly)
            {
                allyStorage.PerceiveEvent(ev);
            }
            break;

        case AiFraction.EAttitude.ENeutral:
            if (trackNeutrals)
            {
                neutralStorage.PerceiveEvent(ev);
            }
            break;
        }
    }
示例#7
0
 public Memory(MemoryEvent memEvent, Tuple3I tuple) :
     base(
         (int)memEvent,
         tuple,
         tuple,
         0
         )
 {
 }
示例#8
0
 public Memory(MemoryEvent memEvent, float progress) :
     base(
         (int)memEvent,
         null,
         null,
         progress
         )
 {
 }
示例#9
0
        protected void EmitEvent <T>(string eventName, T payload, bool disableSaveContext = true, bool reuseContext = true, bool afterExecution = false) where T : IPayload
        {
            var eventOut = new MemoryEvent()
            {
                Name    = eventName,
                Payload = payload
            };

            EmitEvent(eventOut, disableSaveContext, reuseContext, afterExecution);
        }
        public override bool CanEnter(MemoryEvent target)
        {
            Character.State state = character.GetState(id);

            return
                /// can transition happen from current state?
                ((requireTransition || character.GetCurrentState().GetTransition(state) != null) &&
                 /// can enter atm?
                 state.CanEnterSoft());
        }
示例#11
0
 public MemEntry(uint StartAddress, uint EndAddress, byte[] ReadArray, byte[] WriteArray, string Name, MemoryEvent ReadEvent = null, MemoryEvent WriteEvent = null)
 {
     this.StartAddress = StartAddress;
     this.EndAddress   = EndAddress;
     this.ReadArray    = ReadArray;
     this.WriteArray   = WriteArray;
     this.Name         = Name;
     this.ReadEvent    = ReadEvent;
     this.WriteEvent   = WriteEvent;
 }
 public bool CanEnter(MemoryEvent target)
 {
     foreach (var it in filters)
     {
         if (!it.CanEnter(target))
         {
             return(false);
         }
     }
     return(true);
 }
        public override bool CanEnter(MemoryEvent target)
        {
            if (target == null)
            {
                return(false);
            }

            var distSq = ((Vector2)transform.position - target.position).sqrMagnitude;

            return(distSq > maxDistance * maxDistance || distSq < minDistance * minDistance);
        }
        public override bool CanEnter(MemoryEvent target)
        {
            if (!PatrolManager.instance)
            {
                Debug.LogWarning("No Patrol Manager on map");
                return(false);
            }

            pretendingPath = PatrolManager.instance.GetRandomUnusedPath(transform.position - PatrolManager.instance.transform.position, maxPatrolPathDistance);

            return(pretendingPath != null);
        }
示例#15
0
    // Update is called once per frame
    void Update()
    {
        MemoryEvent ev = focus.lastEvent;

        ev.velocity = focus.lastEvent != null ?
                      (Vector2)transform.position - focus.GetTargetPosition() :
                      Vector2.zero;
        ev.exactPosition = target.position;
        ev.forward       = transform.up;

        ev.lifetimeTimer = new MinimalTimer();
        ev.lifetimeTimer.Restart();
    }
示例#16
0
    private void OnCollisionEnter2D(Collision2D collision)
    {
        // omit too weak touches
        if (collision.relativeVelocity.sqrMagnitude < minimalRelativeVelocity * minimalRelativeVelocity)
        {
            return;
        }

        // only targets with perceive unit will report
        // mostly to allow to tune what is perceived
        AiPerceiveUnit otherUnit = collision.gameObject.GetComponent <AiPerceiveUnit>();

        if (!otherUnit)
        {
            return;
        }


        // only report every given seconds
        if (!tTouch.IsReadyRestart())
        {
            return;
        }

        var otherRigidbody = collision.rigidbody;
        var otherTransform = collision.transform;

        MemoryEvent ev = new MemoryEvent();

        ev.exactPosition = otherTransform.position;
        ev.forward       = otherTransform.up;
        ev.velocity      = otherRigidbody ? otherRigidbody.velocity : Vector2.zero;
        ev.lifetimeTimer.Restart();

        touchStorage.PerceiveEvent(ev);

#if UNITY_EDITOR
        Debug.DrawRay(ev.exactPosition, Vector3.up, Color.blue);
#endif
    }
示例#17
0
 public Memory(MemoryEvent memEvent, Tuple3I second, Tuple3I third,
               float fourth) :
     base((int)memEvent, second, third, fourth)
 {
 }
示例#18
0
    void PerformSearch()
    {
        AiFraction myFraction = myUnit.fraction;

        if (!myFraction)
        {
#if UNITY_EDITOR
            Debug.LogWarning("No fraction in perceive unit but trying to use sight");
#endif
            // there's no way to determine where to put events
            return;
        }

        // perform cast
        int n = Physics2D.OverlapCircleNonAlloc(transform.position, searchDistance, StaticCacheLists.colliderCache, memorableMask);

        // preselect targets
        // they have to be in proper angle and contain PerceiveUnit
        for (int i = 0; i < n; ++i)
        {
            var       it          = StaticCacheLists.colliderCache[i];
            Transform itTransform = it.transform;

            //// check if the target is in proper angle
            Vector2 toIt     = itTransform.position - transform.position;
            float   cosAngle = Vector2.Dot(toIt.normalized, transform.up);
            float   angle    = Mathf.Acos(cosAngle) * 180 / Mathf.PI;
            //Debug.Log(angle);
            bool bProperAngle = angle < coneAngle * 0.5f;
            if (!bProperAngle)
            {
                continue;
            }

            // ok, now check if it has AiPerceiveUnit
            // we need it's fraction to determine our attitude

            AiPerceiveUnit perceiveUnit = it.GetComponent <AiPerceiveUnit>();
            if (perceiveUnit == myUnit)
            {
                // oh, come on do not look at yourself... don't be soo narcissistic
                continue;
            }

            if (!perceiveUnit)
            {
                // no perceive unit, this target is invisible to us
                continue;
            }

            AiFraction itFraction = perceiveUnit.fraction;
            if (!itFraction)
            {
                // the same as above,
                return;
            }

            //// determine attitude
            AiFraction.EAttitude attitude = myFraction.GetAttitude(itFraction);

            //// Check if obstacles blocks vision
            if (DoObstaclesBlockVision(itTransform.position))
            {
                continue;
            }

            //// create event
            MemoryEvent ev = new MemoryEvent();
            ev.exactPosition = itTransform.position;
            ev.forward       = itTransform.up;
            // if collider has rigidbody then take its velocity
            // otherwise there is no simple way to determine event velocity
            ev.velocity = it.attachedRigidbody ? it.attachedRigidbody.velocity * velocityPredictionScale : Vector2.zero;

            // set up agent reponsible for this event
            ev.perceiveUnit = perceiveUnit;

            // ensure event will tick from now on
            ev.lifetimeTimer.Restart();


            Debug.DrawRay(ev.exactPosition, Vector3.up, Color.blue, searchTime * nEvents);
            Debug.DrawRay(ev.exactPosition, ev.velocity * searchTime, Color.gray, searchTime);
            InsertEvent(ev, attitude);
        }
    }
 public virtual bool CanEnter(MemoryEvent target)
 {
     return(true);
 }
        public override bool CanEnter(MemoryEvent target)
        {
            float distSq = ((Vector2)transform.position - target.position).sqrMagnitude;

            return(distSq >= minDistance * minDistance && distSq <= maxDistance * maxDistance);
        }
示例#21
0
 public override bool CanEnter(MemoryEvent target)
 {
     return(blackboard.distanceAttractionAccumulator > min);
 }
    /// auto compute direction of unit
    public bool InsertToMemory(AiPerceiveUnit unit, EMemoryEvent eventType, Vector2 position, float predictionScale,
                               float remainTime = 1f, float matureTime = 0f, float shadeTime = 0f, float importance = 1f)
    {
        if (!unit.memoriable)
        {
            return(false);
        }

        int id  = (int)eventType;
        var mem = eventMemory[id];

        /// search if the unit is recorded in our memory
        /// if so then update it
        if (unit)
        {
            foreach (var itMemory in mem)
            {
                if (itMemory.unit == unit)
                {
                    /// spatial data
                    if (itMemory.remainedTime.ElapsedTime() > 3 * float.Epsilon)
                    {
                        itMemory.direction = (position - itMemory.exactPosition) * (predictionScale / itMemory.remainedTime.ElapsedTime()); /// auto compute direction
                    }
                    /// else keep last value... dunno what to do in case of such a small time step

                    itMemory.exactPosition = position;


                    /// time data
                    itMemory.remainedTime.Restart();
                    itMemory.matureTime = 0f;

                    /// list is not sorted
                    anyEventAdded[id] = true;
                    return(false);
                }
            }
        }

        /// otherwise insert new item
        MemoryEvent item = new MemoryEvent();

        item.unit    = unit;
        item.hadUnit = unit != null;

        /// spatial data
        item.exactPosition = position;
        item.direction     = Vector2.zero;

        /// time data
        item.remainedTime = new Timer();
        item.remainedTime.Restart();
        item.matureTime    = matureTime;
        item.knowledgeTime = remainTime;
        item.shadeTime     = shadeTime;

        item.importance = importance;

        mem.Add(item);
        /// list is not sorted
        anyEventAdded[id] = true;
        return(true);
    }
    public bool InsertToMemory(AiPerceiveUnit unit, EMemoryEvent eventType, Vector2 position, Vector2 direction,
                               float remainTime = 1f, float matureTime = 0f, float shadeTime = 0f, float importance = 1f)
    {
        if (!unit.memoriable)
        {
            return(false);
        }

        int id  = (int)eventType;
        var mem = eventMemory[id];

        /// search if the unit is recorded in our memory
        /// if so then update it
        if (unit)
        {
            foreach (var itMemory in mem)
            {
                if (itMemory.unit == unit)
                {
                    /// time data
                    ///

                    /// If information is mature - result should be mature too
                    ///
                    if (itMemory.remainedTime.IsReady(itMemory.matureTime))
                    {
                        itMemory.matureTime = 0;
                        itMemory.remainedTime.Restart();
                    }
                    else
                    {
                        itMemory.matureTime = matureTime;
                    }
                    itMemory.knowledgeTime = remainTime;
                    itMemory.shadeTime     = shadeTime;

                    /// spatial data
                    itMemory.exactPosition = position;
                    itMemory.direction     = direction;

                    itMemory.importance = importance;
                    /// list is not sorted
                    anyEventAdded[id] = true;
                    return(false);
                }
            }
        }

        /// otherwise insert new item
        MemoryEvent item = new MemoryEvent();

        item.unit    = unit;
        item.hadUnit = unit != null;

        /// spatial data
        item.exactPosition = position;
        item.direction     = direction;

        /// time data
        item.remainedTime = new Timer();
        item.remainedTime.Restart();
        item.matureTime    = matureTime;
        item.knowledgeTime = remainTime;
        item.shadeTime     = shadeTime;

        item.importance = importance;

        mem.Add(item);
        /// list is not sorted
        anyEventAdded[id] = true;
        return(true);
    }
示例#24
0
 private static void MemoryTargetOnEventReceived(LogEventInfo logEventInfo)
 {
     MemoryEvent?.Invoke(logEventInfo);
 }