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
    public StateMachineInterruption AddMemoryInterruption(BehaviourHolder entry, EMemoryEvent eventType, EMemoryState eventState)
    {
        MemoryInterruption s = new MemoryInterruption();

        s.entry      = entry;
        s.eventState = eventState;
        s.eventType  = eventType;

        memoryInterruptions.Add(s);
        return(this);
    }
    void SortMemory(EMemoryEvent eventType)
    {
        eventMemory[(int)eventType].Sort(
            delegate(MemoryEvent item1, MemoryEvent item2)
        {
            /*var diff = item2.importance.CompareTo(item1.importance);
             * if (diff != 0)
             *  return diff;*/

            if (item1.remainedTime.IsReady(item1.knowledgeTime))
            {
                if (!item2.remainedTime.IsReady(item2.knowledgeTime))
                {
                    return(1);
                }
            }
            else if (item2.remainedTime.IsReady(item2.knowledgeTime))
            {
                return(-1);
            }


            if (item1.unit != null)
            {
                if (item2.unit == null)
                {
                    return(1);
                }
            }
            else if (item2.unit != null)
            {
                return(-1);
            }

            if (item1.remainedTime.IsReady(item1.matureTime))
            {
                if (!item2.remainedTime.IsReady(item2.matureTime))
                {
                    return(1);
                }
            }
            else if (item2.remainedTime.IsReady(item2.matureTime))
            {
                return(-1);
            }


            float item1Distance = ((Vector2)transform.position - item1.exactPosition).sqrMagnitude;
            float item2Distance = ((Vector2)transform.position - item2.exactPosition).sqrMagnitude;
            return(item1Distance.CompareTo(item2Distance));
        });
    }
    public MemoryEvent SearchInMemory(EMemoryEvent eventType, int objectId = 0)
    {
        int id = (int)eventType;

        if (eventMemory[id].Count < objectId + 1)
        {
            return(null);
        }

        if (anyEventAdded[id])
        {
            SortMemory(eventType);
            anyEventAdded[id] = false;
        }

        var e = eventMemory[id][objectId];

        if (e.remainedTime.IsReady(e.matureTime))
        {
            return(eventMemory[id][objectId]);
        }

        return(null);
    }
 public List <MemoryEvent> GetMemoryEventList(EMemoryEvent eventType)
 {
     return(eventMemory[(int)eventType]);
 }
    /// 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);
    }
 public BFilterReadMemory(EMemoryEvent eventType, EMemoryState eventState = EMemoryState.EKnowledge, int eventId = 0)
 {
     this.eventType  = eventType;
     this.eventState = eventState;
     this.eventId    = eventId;
 }