void OnClick()
 {
     if (EventAction.Utility() > 0)
     {
         EventAction.Action();
     }
 }
Exemplo n.º 2
0
    public void GenerateRandomByWeight(GameObject go, float fuzzy = 0f)
    {
        float cumulativeWeights    = 0f;
        int   filteredActionsCount = 0;

        foreach (var action in actions)
        {
            var cachedRoot = action.Root;
            action.Root = go;
            if (!ActedThisWayAndShouldNoMore(go, action) && action.Filter())
            {
                var baseUt = action.Utility();
                var ut     = baseUt * (1f + fuzzy * ((float)random.NextDouble() - 0.5f) * 2f);
                if (baseUt > 0f)
                {
                    weights[filteredActionsCount].CachedRoot = cachedRoot;
                    weights[filteredActionsCount].Action     = action;
                    weights[filteredActionsCount].Weight     = cumulativeWeights;
                    cumulativeWeights += ut;
                    filteredActionsCount++;
                }
                else
                {
                    action.Root = cachedRoot;
                }
            }
            else
            {
                action.Root = cachedRoot;
            }
        }

        var         num = (float)random.NextDouble() * cumulativeWeights;
        EventAction act = null;

        for (int i = 1; i < filteredActionsCount; i++)
        {
            if (weights [i - 1].Weight <= num && weights [i].Weight > num)
            {
                act = weights [i - 1].Action;
            }
        }
        if (act == null && filteredActionsCount > 0)
        {
            act = weights [filteredActionsCount - 1].Action;
        }
        if (act != null)
        {
            act.Action();
            NotifyOfAct(go, act);
        }

        for (int i = 0; i < filteredActionsCount; i++)
        {
            weights[i].Action.Root = weights[i].CachedRoot;
        }
    }
Exemplo n.º 3
0
    public EventAction GenerateMostUseful(GameObject go, float fuzzy = 0f, List <EventAction> customActions = null, HashSet <EventAction> performedActions = null)
    {
        //if (customActions != null)
        //{
        //    Debug.Log("Most useful for " + go);
        //    foreach (var action in customActions)
        //        Debug.Log(action.GetType().Name + " ");
        //}

        if (fuzzy < 0f)
        {
            fuzzy = -fuzzy;
        }
        float       maxUt         = 0;
        EventAction act           = null;
        GameObject  maxCachedRoot = null;
        var         actions       = customActions == null ? this.actions : customActions;

        foreach (var action in actions)
        {
            var cachedRoot = action.Root;
            action.Root = go;
            if (!ActedThisWayAndShouldNoMore(go, action) && !(action.Meta.OncePerTurn && performedActions != null && performedActions.Contains(action)) && action.Filter())
            {
                var baseUt = action.Utility();
                if (baseUt > 0)
                {
                    var ut = baseUt * (1f + fuzzy * ((float)random.NextDouble() - 0.5f) * 2f);
                    //if (customActions != null)
                    //    Debug.LogFormat("Action {0} with ut = {1} is considered", action.GetType().Name, ut);
                    if (ut > maxUt)
                    {
                        maxUt         = ut;
                        act           = action;
                        maxCachedRoot = cachedRoot;
                    }
                }
                else
                {
                    action.Root = cachedRoot;
                }
            }
            else
            {
                action.Root = cachedRoot;
            }
        }
        if (act != null)
        {
            //Debug.LogFormat("{0} has chosen to do {1} with ut {2}", go, act.GetType().Name, maxUt);
            act.Action();
            act.Root = maxCachedRoot;
            NotifyOfAct(go, act);
        }
        return(act);
    }
Exemplo n.º 4
0
 void OnButtonClicked()
 {
     if (InterAvailable() && Interaction.Filter())
     {
         Interaction.Action();
         if (Interaction.Coroutine != null)
         {
             Interaction.Coroutine.MoveNext();
         }
     }
     View.UpdateView();
 }
Exemplo n.º 5
0
 public void TriggerEvent()
 {
     _eventToTrigger.Action();
 }
Exemplo n.º 6
0
    public void Update(Actor actor)
    {
        if (Action.State == EventAction.ActionState.None)
        {
            bool satisfied = true;
            //Debug.Log(Deps);
            if (Deps != null)
            {
                //Debug.Log(Deps.Count);
                foreach (var dep in Deps)
                {
                    //Debug.LogFormat("{0} = {1}", dep.GetType(), dep.Satisfied());
                    if (!dep.Satisfied())
                    {
                        satisfied = false;
                        break;
                    }
                }
            }
            if (satisfied)
            {
                if (Action.Filter())
                {
                    Action.Action();
                }
                else
                {
                    Action.State = EventAction.ActionState.Failed;
                }
            }
            else
            {
                if (currentDep == null || currentDep.Satisfied())
                {
                    var dep = Deps.Find(d => !d.Satisfied());
                    //Debug.Log(Deps.Count);
                    currentDep = dep;
                    //Debug.Log(dep.GetType().Name);
                }
                if (currentDep.ActionWrapper == null)
                {
                    currentDep.ActionWrapper        = new ActionWrapper();
                    currentDep.ActionWrapper.Action = actor.FindAction(currentDep.ActionCategory(), out currentDep.ActionWrapper.Deps, currentDep);
                }
                currentDep.ActionWrapper.Update(actor);
            }
        }

        if (Action.State == EventAction.ActionState.Started)
        {
            //Debug.Log("Update the action " + Action.GetType().Name);
            Action.Update();
        }

        if (Action.State == EventAction.ActionState.Failed)
        {
            //Debug.Log(Action.State);
        }

        if (Action.State == EventAction.ActionState.Finished)
        {
            //Debug.Log(Action.State);
        }
    }