Esempio n. 1
0
 public bool Run(ref RSQuery query, RSManager manager, GameObject gameObject)
 {
     if (this.disabled)
     {
         return(false);
     }
     foreach (var critereonName in this.criteria)
     {
         if (!manager.criteria.ContainsKey(critereonName))
         {
             return(false);
         }
         RSCriterion critereon = manager.criteria[critereonName];
         if (query.Matches(critereon) || critereon.Optional)
         {
             continue;
         }
         return(false);
     }
     if (this.Flags.HasFlag(RSRuleFlags.NoRepeat))
     {
         this.disabled = true;
     }
     return(manager.RunResponses(this.responses, ref query, gameObject));
 }
Esempio n. 2
0
        public virtual void DispatchResponse(RSResponse response, ref RSQuery query, GameObject gameObject)
        {
            // just dispatch it to the entity who's having the response. if
            // managers want to they can override this later.
            RSEntity entity = gameObject.GetComponent(typeof(RSEntity)) as RSEntity;

            entity.DispatchResponse(response, ref query);
        }
Esempio n. 3
0
        void RunResponse(RSResponse response, RSManager manager, ref RSQuery query, GameObject gameObject)
        {
            // update response's dontResayBefore timer and disabled bool
            // based on response's norepeat flag
            response.JustFired();

            if (this.flags.HasFlag(RSResponseGroupFlags.PermitRepeats))
            {
                //TODO(dan): if this response is First, we should have a
                // special bool on the response called CanBeFirst, which we
                // set to false here, so that this response is no longer
                // marked as the 'first' response to call. because it
                // already has been.
                // maybe it'd be worth just disabling the response right now
                // if it has the first flag, but I'd need to try out the system
                // to figure out whether that's the most intuiative way to
                // handle this certain combination of flags:
                //  group[PermitRepeats] response[First]
                //
                // though to be honest, having a NoRepeat flag on the response
                // would deliver the above proposed functionality, so it
                // instead probably makes sense to do the CanBeFirst thing.
                return;
            }

            bool anyResponsesYetToFire = false;

            foreach (var ri in this.responses)
            {
                if (!ri.Disabled)
                {
                    anyResponsesYetToFire = true;
                    break;
                }
            }

            if (!anyResponsesYetToFire)
            {
                if (this.flags.HasFlag(RSResponseGroupFlags.NoRepeat))
                {
                    this.disabled = true;
                    return;
                }

                // reset all resettable responses because we're bloody out
                foreach (var ri in this.responses)
                {
                    if (!ri.Flags.HasFlag(RSResponseFlags.NoRepeat))
                    {
                        ri.Disabled = false;
                    }
                }
            }

            // actually run the response and do response-like things for it.
            manager.DispatchResponse(response, ref query, gameObject);
        }
Esempio n. 4
0
 public void Run(ref RSQuery query, RSManager manager, GameObject gameObject)
 {
     // simple, run the first rule that matches.
     // this will always be the most specific rule because of how we
     // insert rules into our bucket.
     foreach (var rule in this.rules)
     {
         if (rule.Run(ref query, manager, gameObject))
         {
             break;
         }
     }
 }
Esempio n. 5
0
        IEnumerator IdleLoop()
        {
            GameObject nextIdlingEntity = null;

            while (true)
            {
                if (nextIdlingEntity != null)
                {
                    RSEntity entity = nextIdlingEntity.GetComponent(typeof(RSEntity)) as RSEntity;
                    // update idle time
                    var nextIdleTime = DateTime.Now.AddSeconds(entity.secondsBetweenIdle + UnityEngine.Random.Range(entity.idleJitter * -1, entity.idleJitter));
                    this.entityNextIdleTime[nextIdlingEntity] = nextIdleTime;

                    // actually make the entity idle
                    Debug.Log("entity " + entity.Name + " is idling");
                    var query = new RSQuery();
                    query.Set("concept", "idle");
                    query.Set("who", entity.Name);
                    entity.UpdateFacts();
                    query.AddFactDictionary(ref entity.Facts);
                    this.UpdateFacts();
                    query.AddFactDictionary(ref this.Facts);
                    this.Run(ref query, nextIdlingEntity);
                }
                // find next idling entity
                DateTime idleTime = DateTime.MaxValue;
                foreach (KeyValuePair <GameObject, DateTime> val in this.entityNextIdleTime)
                {
                    if (val.Value.CompareTo(idleTime) < 0)
                    {
                        idleTime         = val.Value;
                        nextIdlingEntity = val.Key;
                    }
                }
                float idleSecs = (float)idleTime.Subtract(DateTime.Now).TotalSeconds;
                if (this.idleMungeSeconds < idleSecs)
                {
                    yield return(new WaitForSeconds(idleSecs));
                }
                else
                {
                    yield return(null);
                }
            }
        }
Esempio n. 6
0
        public bool RunResponses(List <string> responses, ref RSQuery query, GameObject gameObject)
        {
            bool responseRun = false;

            foreach (var response in responses)
            {
                if (!this.responseGroups.ContainsKey(response))
                {
                    continue;
                }
                var trr = this.responseGroups[response].Run(this, ref query, gameObject);
                if (responseRun == false)
                {
                    responseRun = trr;
                }
            }
            return(responseRun);
        }
Esempio n. 7
0
        public bool Run(RSManager manager, ref RSQuery query, GameObject gameObject)
        {
            // we've got NoRepeat and have been disabled
            if (this.disabled)
            {
                return(false);
            }
            Debug.Log("Running ResponseGroup called " + this.Name);
            if (this.firstResponse != null)
            {
                RSResponse firstResponse = this.responses[(int)this.firstResponse];
                if (firstResponse.CanFire())
                {
                    this.RunResponse(firstResponse, manager, ref query, gameObject);
                    return(true);
                }
            }
            RSResponse lastResponse = null;

            if (this.lastResponse != null)
            {
                lastResponse = this.responses[(int)this.lastResponse];

                // confirm that no other un-disabled responses can be run
                bool anyOtherResponsesCanFire = false;
                foreach (var response in this.responses)
                {
                    if (!response.Disabled)
                    {
                        anyOtherResponsesCanFire = true;
                        break;
                    }
                }

                if (!anyOtherResponsesCanFire && lastResponse.CanFire())
                {
                    this.RunResponse(lastResponse, manager, ref query, gameObject);
                    return(true);
                }

                // going forward in this function, lastResponse is one that we
                // shouldn't call because if we were to call it it would've
                // already happened above ^
            }
            if (this.flags.HasFlag(RSResponseGroupFlags.Sequential))
            {
                // run responses sequentially
                foreach (var response in this.responses)
                {
                    if (response.CanFire())
                    {
                        this.RunResponse(response, manager, ref query, gameObject);
                        return(true);
                    }
                }
            }
            else
            {
                // get a random response, weighted appropriately, and fire it.
                float      totalWeight        = 0;
                RSResponse finalResponseWeSaw = null;
                foreach (var response in this.responses)
                {
                    if (response != lastResponse && response.CanFire())
                    {
                        totalWeight       += response.Weight;
                        finalResponseWeSaw = response;
                    }
                }
                if (finalResponseWeSaw == null)
                {
                    // no valid responses
                    return(false);
                }
                float weight = UnityEngine.Random.Range(0F, totalWeight);
                foreach (var response in this.responses)
                {
                    if (response == lastResponse)
                    {
                        continue;
                    }
                    if (response.CanFire())
                    {
                        weight -= response.Weight;
                    }
                    if (weight <= 0 || response == finalResponseWeSaw)
                    {
                        this.RunResponse(response, manager, ref query, gameObject);
                        return(true);
                    }
                }
            }
            return(false);
        }
Esempio n. 8
0
 public virtual void DispatchResponse(RSResponse response, ref RSQuery query)
 {
     Debug.Log("resp:: " + response.ResponseValue);
 }
Esempio n. 9
0
 public void Run(ref RSQuery query, GameObject gameObject)
 {
     this.lazyAllRules.Run(ref query, this, gameObject);
 }