Esempio n. 1
0
        static int ConsecutiveHorizontal(Cell sourceCell, StateData state)
        {
            int  consecutive = 0;
            Cell cellTrait;

            for (var cellId = sourceCell.Right; cellId.Id != ObjectId.None; cellId = cellTrait.Right)
            {
                var objIndex = state.GetTraitBasedObjectIndex(cellId);
                cellTrait = state.GetTraitOnObjectAtIndex <Cell>(objIndex);

                if (cellTrait.Type != sourceCell.Type)
                {
                    break;
                }

                consecutive++;
            }

            for (var cellId = sourceCell.Left; cellId.Id != ObjectId.None; cellId = cellTrait.Left)
            {
                var objIndex = state.GetTraitBasedObjectIndex(cellId);
                cellTrait = state.GetTraitOnObjectAtIndex <Cell>(objIndex);

                if (cellTrait.Type != sourceCell.Type)
                {
                    break;
                }

                consecutive++;
            }

            return(consecutive);
        }
Esempio n. 2
0
        static int ConsecutiveVertical(Cell sourceCell, StateData state)
        {
            int  consecutive = 0;
            Cell cellTrait;

            for (var cellId = sourceCell.Bottom; cellId.Id != ObjectId.None; cellId = cellTrait.Bottom)
            {
                var objIndex = state.GetTraitBasedObjectIndex(cellId);
                cellTrait = state.GetTraitOnObjectAtIndex <Cell>(objIndex);

                if (cellTrait.Type != sourceCell.Type)
                {
                    break;
                }

                consecutive++;
            }

            for (var cellId = sourceCell.Top; cellId.Id != ObjectId.None; cellId = cellTrait.Top)
            {
                var objIndex = state.GetTraitBasedObjectIndex(cellId);
                cellTrait = state.GetTraitOnObjectAtIndex <Cell>(objIndex);

                if (cellTrait.Type != sourceCell.Type)
                {
                    break;
                }

                consecutive++;
            }

            return(consecutive);
        }
Esempio n. 3
0
        static void CollectVertical(Cell sourceCell, StateData state, int cellIndex, ref NativeList <int> cellsToDestroy)
        {
            Cell cellTrait;

            for (var cellId = sourceCell.Top; cellId.Id != ObjectId.None; cellId = cellTrait.Top)
            {
                var objIndex = state.GetTraitBasedObjectIndex(cellId);
                cellTrait = state.GetTraitOnObjectAtIndex <Cell>(objIndex);

                if (cellTrait.Type != sourceCell.Type)
                {
                    break;
                }

                cellsToDestroy.Add(state.GetTraitBasedObjectIndex(cellId));
            }

            cellsToDestroy.Add(cellIndex);

            for (var cellId = sourceCell.Bottom; cellId.Id != ObjectId.None; cellId = cellTrait.Bottom)
            {
                var objIndex = state.GetTraitBasedObjectIndex(cellId);
                cellTrait = state.GetTraitOnObjectAtIndex <Cell>(objIndex);

                if (cellTrait.Type != sourceCell.Type)
                {
                    break;
                }

                cellsToDestroy.Add(state.GetTraitBasedObjectIndex(cellId));
            }
        }
Esempio n. 4
0
    public override void BeginExecution(StateData state, ActionKey action, Otto actor)
    {
        base.BeginExecution(state, action, actor);

        // Trigger beginning of walk animation.
        m_Animator = actor.GetComponentInParent <Animator>();
        m_Animator.SetBool(k_Walk, true);

        m_TargetPosition = state.GetTraitOnObjectAtIndex <Location>(action[1]).Position;
        m_OttoTransform  = actor.transform;

        // Grab nav mesh
        m_NavMeshAgent = actor.GetComponentInParent <NavMeshAgent>();

        // Motion Controller
        m_MotionController = actor.GetComponentInParent <MotionController>();
        m_MotionController.TargetPosition = m_TargetPosition;
        m_MotionController.StartMoving();
        SetAnimationParams();
        m_Arrived = false;

        var startPosition = state.GetTraitOnObjectAtIndex <Location>(action[0]).Position;
        var distance      = Vector3.Distance(startPosition, m_TargetPosition);

        m_PredictedDeltaTime = Mathf.FloorToInt(distance / 0.47f + 1f);
    }
        float Reward(StateData originalState, ActionKey action, StateData newState)
        {
            var reward = 0f;

            {
                var param0 = originalState.GetTraitOnObjectAtIndex <Unity.AI.Planner.DomainLanguage.TraitBased.Location>(action[0]);
                var param1 = originalState.GetTraitOnObjectAtIndex <Unity.AI.Planner.DomainLanguage.TraitBased.Location>(action[1]);
                reward -= new Unity.AI.Planner.Navigation.LocationDistance().RewardModifier(param0, param1);
            }

            return(reward);
        }
Esempio n. 6
0
    void SetInventory(StateData state)
    {
        var domainObjects = new NativeList <(DomainObject, int)>(4, Allocator.TempJob);

        foreach (var(_, domainObjectIndex) in state.GetDomainObjects(domainObjects, k_InventoryType))
        {
            var inventory = state.GetTraitOnObjectAtIndex <Inventory>(domainObjectIndex);

            if (inventory.SatisfiesNeed == NeedType.Hunger) // Apples
            {
                m_NumApples = (int)inventory.Amount;
                for (var i = 0; i < m_Apples.Count; i++)
                {
                    m_Apples[i].enabled = i >= m_MaxApples - m_NumApples;
                }
            }
            else if (inventory.SatisfiesNeed == NeedType.Thirst) // Bottles
            {
                m_NumBottles = (int)inventory.Amount;
                for (var i = 0; i < m_Bottles.Count; i++)
                {
                    m_Bottles[i].enabled = i >= m_MaxBottles - m_NumBottles;
                }
            }
        }
        domainObjects.Dispose();
    }
Esempio n. 7
0
    public override void BeginExecution(StateData state, ActionKey action, Otto actor)
    {
        base.BeginExecution(state, action, actor);

        m_StartTime = Time.time;

        m_Duration = state.GetTraitOnObjectAtIndex <Duration>(action[2]).Time;

        m_Animator.SetTrigger(k_Work);
        m_Animator.SetBool(s_ContinueWork, true);
    }
Esempio n. 8
0
        float Reward(StateData originalState, ActionKey action, StateData newState)
        {
            var reward = 0f;

            {
                var param0 = originalState.GetTraitOnObjectAtIndex <AI.Planner.Domains.Waypoint>(action[1]);
                reward += new AI.Planner.Custom.Escape.OneTimeModifier().RewardModifier(param0);
            }

            return(reward);
        }
Esempio n. 9
0
        float Reward(StateData originalState, ActionKey action, StateData newState)
        {
            var reward = 0f;

            {
                var param0 = originalState.GetTraitOnObjectAtIndex <Generated.AI.Planner.StateRepresentation.Waypoint>(action[1]);
                reward += new global::AI.Planner.Custom.Escape.OneTimeModifier().RewardModifier(param0);
            }

            return(reward);
        }
Esempio n. 10
0
    public override void BeginExecution(StateData state, ActionKey action, Otto actor)
    {
        base.BeginExecution(state, action, actor);

        AnimationComplete = false;
        m_Animator.SetTrigger(k_Consumables);

        var dispenser = state.GetTraitOnObjectAtIndex <Dispenser>(action[1]);

        m_ConsumableType = dispenser.ConsumableType;
        m_Animator.SetTrigger(m_ConsumableType == ConsumableType.Apple ? k_PocketFood : k_PocketDrink);
    }
Esempio n. 11
0
    public override void BeginExecution(StateData state, ActionKey action, Otto actor)
    {
        base.BeginExecution(state, action, actor);

        var inventory = state.GetTraitOnObjectAtIndex <Inventory>(action[1]);

        m_ConsumableType = inventory.ConsumableType;
        m_NeedType       = inventory.SatisfiesNeed;
        m_NeedReduction  = inventory.NeedReduction;

        m_Animator.SetTrigger(k_Consumables);
        m_Animator.SetTrigger(m_ConsumableType == ConsumableType.Apple ? k_EatFromPocket : k_DrinkFromPocket);
    }
Esempio n. 12
0
    public override void EndExecution(StateData state, ActionKey action, Otto actor)
    {
        base.EndExecution(state, action, actor);

        var domainObjects = new NativeList <(DomainObject, int)>(4, Allocator.TempJob);

        foreach (var(_, domainObjectIndex) in state.GetDomainObjects(domainObjects, typeof(Inventory)))
        {
            var inventory = state.GetTraitOnObjectAtIndex <Inventory>(domainObjectIndex);
            inventory.Amount -= inventory.ConsumableType == m_ConsumableType ? 1 : 0;
            state.SetTraitOnObjectAtIndex(inventory, domainObjectIndex);
        }

        domainObjects.Clear();
        foreach (var(_, domainObjectIndex) in state.GetDomainObjects(domainObjects, typeof(Need)))
        {
            var need = state.GetTraitOnObjectAtIndex <Need>(domainObjectIndex);
            need.Urgency -= need.NeedType == m_NeedType ? m_NeedReduction : 0;
            state.SetTraitOnObjectAtIndex(need, domainObjectIndex);
        }
        domainObjects.Dispose();
    }
Esempio n. 13
0
    public override void EndExecution(StateData state, ActionKey action, Otto actor)
    {
        var domainObjects = new NativeList <(DomainObject, int)>(4, Allocator.TempJob);

        foreach (var(_, domainObjectIndex) in state.GetDomainObjects(domainObjects, new ComponentType[] { typeof(Inventory) }))
        {
            var inventory = state.GetTraitOnObjectAtIndex <Inventory>(domainObjectIndex);
            inventory.Amount += inventory.ConsumableType == m_ConsumableType ? 1 : 0;
            state.SetTraitOnObjectAtIndex(inventory, domainObjectIndex);
        }
        domainObjects.Dispose();

        base.EndExecution(state, action, actor);
    }
Esempio n. 14
0
    public override void EndExecution(StateData state, ActionKey action, Otto actor)
    {
        base.EndExecution(state, action, actor);

        // Set target orientation based on location orientation
        var forward = state.GetTraitOnObjectAtIndex <Location>(action[1]).Forward;

        m_MotionController.StopMoving(forward);

        // Effects
        // Update Otto position
        var agentDomainObjectIndex = action[0];
        var destinationLocation    = state.GetTraitOnObjectAtIndex <Location>(action[1]);

        var loc = state.GetTraitOnObjectAtIndex <Location>(agentDomainObjectIndex);

        loc.Position = destinationLocation.Position;
        state.SetTraitOnObjectAtIndex(loc, agentDomainObjectIndex);

        // Trigger ending of walk animation.
        m_Animator.SetBool(k_Walk, false);
        SetAnimationParams(true);
    }
Esempio n. 15
0
    public override void BeginExecution(StateData state, ActionKey action, Otto actor)
    {
        base.BeginExecution(state, action, actor);

        m_Animator = actor.GetComponentInParent <Animator>();
        m_Animator.SetTrigger(Animator.StringToHash("Sleep"));

        // Reset fatigue
        var domainObjects = new NativeList <(DomainObject, int)>(4, Allocator.TempJob);

        foreach (var(_, domainObjectIndex) in state.GetDomainObjects(domainObjects, new ComponentType[] { typeof(Need) }))
        {
            var need = state.GetTraitOnObjectAtIndex <Need>(domainObjectIndex);
            if (need.NeedType == NeedType.Fatigue)
            {
                need.Urgency = 0;
                state.SetTraitOnObjectAtIndex(need, domainObjectIndex);
                break;
            }
        }
        domainObjects.Dispose();
    }
Esempio n. 16
0
    void SetNeeds(StateData state)
    {
        var domainObjects = new NativeList <(DomainObject, int)>(4, Allocator.TempJob);

        foreach (var(_, domainObjectIndex) in state.GetDomainObjects(domainObjects, k_NeedType))
        {
            var need = state.GetTraitOnObjectAtIndex <Need>(domainObjectIndex);

            if (need.NeedType == NeedType.Hunger)
            {
                m_HungerMat.SetFloat(s_Amount, 1 - need.Urgency / 100f);
            }
            else if (need.NeedType == NeedType.Thirst)
            {
                m_ThirstMat.SetFloat(s_Amount, 1 - need.Urgency / 100f);
            }
            else if (need.NeedType == NeedType.Fatigue)
            {
                m_FatigueMat.SetFloat(s_Amount, 1 - need.Urgency / 100f);
            }
        }
        domainObjects.Dispose();
    }
Esempio n. 17
0
 public static T GetFromTrait <T>(StateData state, ActionKey action) where T : struct, ITrait
 {
     return(state.GetTraitOnObjectAtIndex <T>(action[k_FromIndex]));
 }
Esempio n. 18
0
        public static void SwapCellAndUpdateBoard(ActionKey action, StateData state, Cell cell1, Cell cell2)
        {
            // Retrieve Game trait data
            var gameId    = state.GetTraitBasedObjectId(action[GameIndex]);
            var game      = state.GetTraitBasedObject(gameId);
            var gameTrait = state.GetTraitOnObject <Game>(game);

            // Swap cell types
            (cell1.Type, cell2.Type) = (cell2.Type, cell1.Type);
            state.SetTraitOnObjectAtIndex(cell1, action[Cell1Index]);
            state.SetTraitOnObjectAtIndex(cell2, action[Cell2Index]);

            int newScore       = gameTrait.Score;
            var cellsToDestroy = new NativeList <int>(1, Allocator.Temp);

            // Check match3 and destroy used Gem (set to Type = None)
            CheckMatchOnGem(state, cell1, action[Cell1Index], ref cellsToDestroy);
            CheckMatchOnGem(state, cell2, action[Cell2Index], ref cellsToDestroy);

            if (cellsToDestroy.Length > 0)
            {
                #region debug
                cellsToDestroy.Clear();
                CheckMatchOnGem(state, cell1, action[Cell1Index], ref cellsToDestroy);
                CheckMatchOnGem(state, cell2, action[Cell2Index], ref cellsToDestroy);
                #endregion

                // Unset all destroyed cells
                var cellQueue   = new NativeList <int>(Allocator.Temp);
                var cellsQueued = new NativeHashMap <int, byte>(3, Allocator.Temp);
                var cellChanged = new NativeHashMap <int, byte>(3, Allocator.Temp);

                while (cellsToDestroy.Length > 0)
                {
                    for (int i = 0; i < cellsToDestroy.Length; i++)
                    {
                        var cellIndex = cellsToDestroy[i];
                        if (cellsQueued.ContainsKey(cellIndex))
                        {
                            continue;
                        }

                        var cellTrait = state.GetTraitOnObjectAtIndex <Cell>(cellIndex);

                        if (cellTrait.Type == gameTrait.GoalType)
                        {
                            newScore += CustomSwapReward.GoalReward;
                        }
                        else
                        {
                            newScore += CustomSwapReward.BasicReward;
                        }

                        cellTrait.Type = CellType.None;
                        state.SetTraitOnObjectAtIndex(cellTrait, cellIndex);

                        cellQueue.Add(cellIndex);
                        cellsQueued.TryAdd(cellIndex, default);
                    }
                    cellsToDestroy.Clear();

                    // Stitch Unset Gems with Top Gem
                    while (cellQueue.Length > 0)
                    {
                        var cellIndex = cellQueue[0];
                        cellQueue.RemoveAtSwapBack(0);
                        cellsQueued.Remove(cellIndex);
                        var cell = state.GetTraitOnObjectAtIndex <Cell>(cellIndex);

                        if (cell.Top.Id == ObjectId.None)
                        {
                            continue;
                        }

                        if (cell.Type == CellType.None)
                        {
                            var cellTopObject = state.GetTraitBasedObject(cell.Top);
                            var cellTop       = state.GetTraitOnObject <Cell>(cellTopObject);

                            // Find first cell with a known type on top
                            while (cellTop.Type == CellType.None)
                            {
                                if (cellTop.Top.Id == ObjectId.None)
                                {
                                    break;
                                }

                                cellTopObject = state.GetTraitBasedObject(cellTop.Top);
                                cellTop       = state.GetTraitOnObject <Cell>(cellTopObject);
                            }

                            if (cellTop.Type != CellType.None)
                            {
                                cell.Type = cellTop.Type;
                                state.SetTraitOnObjectAtIndex(cell, cellIndex);

                                var newCellTop = cellTop;
                                newCellTop.Type = CellType.None;
                                state.SetTraitOnObject(newCellTop, ref cellTopObject);

                                var index = state.GetTraitBasedObjectIndex(cellTopObject);
                                cellQueue.Add(index);
                                cellsQueued.TryAdd(index, default);

                                // Queue all vertical cells for checking
                                var cellTopIndex = state.GetTraitBasedObjectIndex(cell.Top);
                                while (cellTop.Type != CellType.None)
                                {
                                    cellChanged.TryAdd(cellTopIndex, default);

                                    if (cellTop.Top == TraitBasedObjectId.None)
                                    {
                                        break;
                                    }

                                    cellTopIndex = state.GetTraitBasedObjectIndex(cellTop.Top);
                                    cellTop      = state.GetTraitOnObjectAtIndex <Cell>(cellTopIndex);
                                }
                            }
                        }
                    }

                    // Check cells affected by stitching for chained-explosion
                    var changedKeys = cellChanged.GetKeyArray(Allocator.Temp);
                    for (int i = 0; i < changedKeys.Length; i++)
                    {
                        var cellIndex = changedKeys[i];
                        var cell      = state.GetTraitOnObjectAtIndex <Cell>(cellIndex);
                        CheckMatchOnGem(state, cell, cellIndex, ref cellsToDestroy);
                    }
                    changedKeys.Dispose();
                    cellChanged.Clear();
                }

                cellQueue.Dispose();
                cellsQueued.Dispose();
                cellChanged.Dispose();
            }

            // Score is stored in the Game Object and apply later in the reward function
            gameTrait.Score = newScore;
            state.SetTraitOnObject(gameTrait, ref game);

            cellsToDestroy.Dispose();
        }