Ejemplo n.º 1
0
        public void Execute(int index)
        {
            Vector3 avoidance = Vector3.zero;

            MobComponentData currentFrienData;
            NativeMultiHashMapIterator <int> frienderator = new NativeMultiHashMapIterator <int>();
            bool success = friends.TryGetFirstValue(index, out currentFrienData, out frienderator);

            while (success)
            {
                float dist = Vector3.Distance(allMobs[index].Position, currentFrienData.Position);

                if (dist > 0 && dist < radius)
                {
                    Vector3 diff = Vector3.Normalize(allMobs[index].Position - currentFrienData.Position);
                    diff       = diff / dist;
                    avoidance += diff;
                }

                success = friends.TryGetNextValue(out currentFrienData, ref frienderator);
            }


            output[index] = new PersonalSpaceJobOutput()
            {
                PersonalSpace = avoidance * scaler
            };
        }
Ejemplo n.º 2
0
        public void Execute(int index)
        {
            Vector3 perceivedVelocity = Vector3.zero;

            int friendCount = 0;
            MobComponentData currentFrienData;
            NativeMultiHashMapIterator <int> frienderator = new NativeMultiHashMapIterator <int>();
            bool success = friends.TryGetFirstValue(index, out currentFrienData, out frienderator);

            while (success)
            {
                friendCount++;
                float dist = Vector3.Distance(allMobs[index].Position, currentFrienData.Position);

                if (dist < radius)
                {
                    perceivedVelocity += currentFrienData.Velocity;
                }

                success = friends.TryGetNextValue(out currentFrienData, ref frienderator);
            }

            perceivedVelocity = (friendCount > 1) ? perceivedVelocity / (friendCount) : perceivedVelocity;

            output[index] = new EqualizeSpeedJobOutput()
            {
                EqualizeSpeed = ((perceivedVelocity - allMobs[index].Velocity) / 8f) * scaler
            };
        }
Ejemplo n.º 3
0
 public UnsafeMultiHashMapEnumerator(UnsafeMultiHashMap <TKey, TValue> map, TKey key)
 {
     this.map = map;
     this.key = key;
     iterator = default;
     value    = default;
     isFirst  = true;
 }
Ejemplo n.º 4
0
        public static void TryGetFirstValueAndThrowOnFail <TKey, TValue>(
            this NativeMultiHashMap <TKey, TValue> nativeMultiHashMap, TKey key, out TValue item,
            out NativeMultiHashMapIterator <TKey> it)
            where TKey : struct, IEquatable <TKey>
            where TValue : struct
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            var state = nativeMultiHashMap.TryGetFirstValue(key, out item, out it);
            HashMapHelpers.ThrowOnFailedKey(state, key);
#else
            nativeMultiHashMap.TryGetFirstValue(key, out item, out it);
#endif
        }
Ejemplo n.º 5
0
    private static int GetEntityCountInQuadrant(int quadrantKey)
    {
        QuadrantData quadrantData;
        NativeMultiHashMapIterator <int> iterator = new NativeMultiHashMapIterator <int>();
        int count = 0;

        // We have at least 1 value for our key
        // This is how you iterate over
        if (quadrantMap.TryGetFirstValue(quadrantKey, out quadrantData, out iterator))
        {
            do
            {
                count++;
            } while (quadrantMap.TryGetNextValue(out quadrantData, ref iterator)); // Keep iterationg as long as there are values for this key
        }
        return(count);
    }
            public void Execute()
            {
                int  i_nextBestUniqueKeyIndex = i_eltiesCount - 1;
                bool canCheckForElite         = i_nextBestUniqueKeyIndex >= 0 ? true : false;
                int  i_eliteIndex             = 0;
                bool isNextUniqueKey          = true;

                EntityIndex parentEntityScore       = default;
                NativeMultiHashMapIterator <int> it = default;

                while (canCheckForElite)
                {
                    if (!isNextUniqueKey && nmhm_entitiesScore.TryGetNextValue(out parentEntityScore, ref it))
                    {
// Debug.Log ( "try get nexxt value: " + parent.entity + "; " + parent.i_index ) ;
                    }
                    else
                    {
// Debug.LogWarning ( "try get but NO MORE." ) ;
                        isNextUniqueKey = true;
                    }

                    if (isNextUniqueKey && nmhm_entitiesScore.TryGetFirstValue(na_currentSortedKeysWithDuplicates [i_nextBestUniqueKeyIndex], out parentEntityScore, out it))
                    {
// Debug.LogWarning ( "try get first value: " + parent.entity + "; " + parent.i_index ) ;

                        i_nextBestUniqueKeyIndex--;
                        isNextUniqueKey = false;
                    }

                    na_elities [i_eliteIndex] = parentEntityScore;
                    i_eliteIndex++;

                    if (i_eliteIndex >= i_eltiesCount)
                    {
                        // Stop lookup.
                        canCheckForElite = false;
                    }
                } // while
            }
Ejemplo n.º 7
0
        public void Execute(Entity entity, int index, [ReadOnly] ref Translation translation, [ReadOnly] ref QuadrantEntity quadrantEntity)
        {
            float3 unitPosition          = translation.Value;
            Entity closestTargetEntity   = Entity.Null;
            float3 closestTargetPosition = float3.zero;

            int key = QuadrantSystem.GetKeyFromPosition(unitPosition);

            NativeMultiHashMapIterator <int> iterator = new NativeMultiHashMapIterator <int>();
            QuadrantData quadrantData;

            if (quadrantMap.TryGetFirstValue(key, out quadrantData, out iterator))
            {
                do
                {
                    if (quadrantEntity.type != quadrantData.quadrantEntity.type)
                    {
                        if (closestTargetEntity == Entity.Null)
                        {
                            // No target
                            closestTargetEntity   = quadrantData.entity;
                            closestTargetPosition = math.distance(unitPosition, quadrantData.position);
                        }
                        else
                        {
                            if (math.distance(unitPosition, quadrantData.position) < math.distance(unitPosition, closestTargetPosition))
                            {
                                // This target is closer
                                closestTargetEntity   = entity;
                                closestTargetPosition = math.distancesq(unitPosition, quadrantData.position);
                            }
                        }
                    }
                } while (quadrantMap.TryGetNextValue(out quadrantData, ref iterator));
            }

            closestTargets[index] = closestTargetEntity;
        }
Ejemplo n.º 8
0
        public void Execute(int index)
        {
            Vector3 center = Vector3.zero;

            int friendCount = 0;
            MobComponentData currentFrienData;
            NativeMultiHashMapIterator <int> frienderator = new NativeMultiHashMapIterator <int>();
            bool success = friends.TryGetFirstValue(index, out currentFrienData, out frienderator);

            while (success)
            {
                friendCount++;
                center += currentFrienData.Position;
                success = friends.TryGetNextValue(out currentFrienData, ref frienderator);
            }

            center = (friendCount > 1) ? center / (friendCount) : center;

            output[index] = new CenterMassJobOutput()
            {
                GroupCenterMass = ((center - allMobs[index].Position) / 100f) *
                                  scaler
            };
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Replace value at iterator.
 /// </summary>
 /// <param name="item">Value.</param>
 /// <param name="it">Iterator</param>
 /// <returns>Returns true if value was sucessfuly replaced.</returns>
 public bool SetValue(TValue item, NativeMultiHashMapIterator <TKey> it)
 {
     return(UnsafeHashMapBase <TKey, TValue> .SetValue(m_Buffer, ref it, ref item));
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Retrieve iterator to the next value for the key.
 /// </summary>
 /// <param name="item">Output value.</param>
 /// <param name="it">Iterator.</param>
 /// <returns>Returns true if next value for the key is found.</returns>
 public bool TryGetNextValue(out TValue item, ref NativeMultiHashMapIterator <TKey> it)
 {
     return(UnsafeHashMapBase <TKey, TValue> .TryGetNextValueAtomic(m_Buffer, out item, ref it));
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Retrieve iterator for the first value for the key.
 /// </summary>
 /// <param name="key">The key.</param>
 /// <param name="item">Output value.</param>
 /// <param name="it">Iterator.</param>
 /// <returns>Returns true if the container contains the key.</returns>
 public bool TryGetFirstValue(TKey key, out TValue item, out NativeMultiHashMapIterator <TKey> it)
 {
     return(UnsafeHashMapBase <TKey, TValue> .TryGetFirstValueAtomic(m_Buffer, key, out item, out it));
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Removes all elements with the specified iterator the container.
 /// </summary>
 /// <param name="it">Iterator pointing at value to remove.</param>
 public void Remove(NativeMultiHashMapIterator <TKey> it)
 {
     UnsafeHashMapBase <TKey, TValue> .Remove(m_Buffer, it);
 }
            public void Execute( )
            {
                NativeHashMap <int, bool> nhm_checkedEliteEntities = new NativeHashMap <int, bool> (na_elities.Length, Allocator.Temp);

                int  i_parentUniqueKeyIndex = 0;
                bool isNextParentUniqueKey  = true;

                NativeMultiHashMapIterator <int> it = default;

                for (int i_eliteIndex = 0; i_eliteIndex < na_elities.Length; i_eliteIndex++)
                {
                    EntityIndex currentEntityIndex = na_elities [i_eliteIndex];
                    Entity      currentEliteEntity = currentEntityIndex.entity;

                    // Check if this entity has not been tested already.
                    if (nhm_checkedEliteEntities.TryAdd(currentEliteEntity.Index, true))
                    {
                        int i_currentEntityIndex          = currentEntityIndex.i_index;
                        int i_currentPopulationBrainScore = a_brainScore [currentEliteEntity].i;

// Debug.Log ( "* Inject Elite: " + i_eliteIndex + " / " + na_currentEliteIndexProbability.Length + "; " + currentEliteEntity + "; with current score: " + i_currentPopulationBrainScore ) ;
// Debug.Log ( "* Inject Elite: " + i_probablity + " / " + i_perentageOfElites + "; " + eliteEntity + "; with current score: " + i_currentPopulationBrainScore ) ;

                        EntityIndex parentIndex = default;

                        // Look up through parents' scores, starting from lowest score ascending.
                        if (!isNextParentUniqueKey && nmhm_parentEntitiesScore.TryGetNextValue(out parentIndex, ref it))
                        {
// Debug.Log ( "try get next value: " + parentIndex.entity + "; " + parentIndex.i_index ) ;
                        }
                        else
                        {
// Debug.LogWarning ( "try get but NO MORE." ) ;
                            isNextParentUniqueKey = true;
                        }

                        if (isNextParentUniqueKey && nmhm_parentEntitiesScore.TryGetFirstValue(na_parentKeysWithDuplicates [i_parentUniqueKeyIndex], out parentIndex, out it))
                        {
// Debug.LogWarning ( "try get first value: " + parentIndex.entity + "; " + parentIndex.i_index ) ;

                            i_parentUniqueKeyIndex++;
                            isNextParentUniqueKey = false;
                        }


                        // Parent is valid.
                        if (!isNextParentUniqueKey && parentIndex.entity.Version > 0)
                        {
                            int i_parentPopulationBrainScore = a_brainScore [parentIndex.entity].i;

// Debug.Log ( "score: " + i_currentPopulationBrainScore + " >> " + i_parentPopulationBrainScore ) ;

                            if (i_currentPopulationBrainScore > i_parentPopulationBrainScore)
                            {
                                // isBetterScore = true ;

                                na_parentPopulationEntities [parentIndex.i_index] = currentEliteEntity;

// Debug.LogWarning ( "Parent index: " + parentIndex.i_index + " / " + na_parentPopulationEntities.Length + "; inject : " + currentEliteEntity + "; for: " + parentIndex.entity + "; score: " + i_currentPopulationBrainScore + " >> " + i_parentPopulationBrainScore ) ;
// Debug.LogWarning ( "Parent index: " + parent.i_index + " / " + na_parentPopulationEntities.Length + "; inject : " + eliteEntity + "; for: " + parent.entity + "; score: " + i_currentPopulationBrainScore + " >> " + i_parentPopulationBrainScore + "; index prob: " + i_indexProbability ) ;

                                // Swap entities.

                                na_currentPopulationEntities [i_currentEntityIndex] = parentIndex.entity;
                                // na_elities [i_eliteIndex] = parent.entity ;
                                // na_currentPopulationEntities [i_indexProbability] = parent.entity ;
                            }
                        }

                        if (isNextParentUniqueKey || i_parentUniqueKeyIndex >= na_parentKeysWithDuplicates.Length)
                        {
// Debug.LogError ( "No more parent entities." ) ;
                            break;  // No more parent entities.
                        }
                    }
                } // for

                nhm_checkedEliteEntities.Dispose();
                // na_currentPopulationEntitiesCopy.Dispose () ;
            }
Ejemplo n.º 14
0
        protected virtual int SetUpInventoryItems(InventoryGroupWrapper inventoryGroup, int itemCount, Entity owner)
        {
            var foodList        = new NativeList <ItemHashValue>(Allocator.Temp);
            var potionList      = new NativeList <ItemHashValue>(Allocator.Temp);
            var treasureList    = new NativeList <ItemHashValue>(Allocator.Temp);
            var weaponList      = new NativeList <ItemHashValue>(Allocator.Temp);
            var magicWeaponList = new NativeList <ItemHashValue>(Allocator.Temp);
            var helmetList      = new NativeList <ItemHashValue>(Allocator.Temp);
            var chestList       = new NativeList <ItemHashValue>(Allocator.Temp);
            var bootsList       = new NativeList <ItemHashValue>(Allocator.Temp);

            var itemCounter = 0;

            this.Entities.With(this._itemsGroup).ForEach((Entity itemEntity, ref ItemComponent itemComponent, ref PickedUpComponent pickedUpComponent) =>
            {
                if (pickedUpComponent.Owner != owner)
                {
                    return;
                }

                itemCounter++;
                var item = ItemsManagerComponent.Instance.ItemsStoreComponent.Items[itemComponent.StoreIndex];
                switch (item.ItemType)
                {
                case ItemType.Food:
                    foodList.Add(
                        new ItemHashValue
                    {
                        Entity        = itemEntity,
                        ItemComponent = itemComponent
                    });
                    break;

                case ItemType.Potion:
                    potionList.Add(
                        new ItemHashValue
                    {
                        Entity        = itemEntity,
                        ItemComponent = itemComponent
                    });
                    break;

                case ItemType.Treasure:
                    treasureList.Add(
                        new ItemHashValue
                    {
                        Entity        = itemEntity,
                        ItemComponent = itemComponent
                    });
                    break;

                case ItemType.Gear:
                    switch (item.GearType)
                    {
                    case GearType.Helmet:
                        helmetList.Add(
                            new ItemHashValue
                        {
                            Entity        = itemEntity,
                            ItemComponent = itemComponent
                        });
                        break;

                    case GearType.Chest:
                        chestList.Add(
                            new ItemHashValue
                        {
                            Entity        = itemEntity,
                            ItemComponent = itemComponent
                        });
                        break;

                    case GearType.Weapon:
                        weaponList.Add(
                            new ItemHashValue
                        {
                            Entity        = itemEntity,
                            ItemComponent = itemComponent
                        });
                        break;

                    case GearType.Boots:
                        bootsList.Add(
                            new ItemHashValue
                        {
                            Entity        = itemEntity,
                            ItemComponent = itemComponent
                        });
                        break;

                    case GearType.Magic:
                        magicWeaponList.Add(
                            new ItemHashValue
                        {
                            Entity        = itemEntity,
                            ItemComponent = itemComponent
                        });
                        break;
                    }
                    break;
                }
            });

            if (itemCounter != this.LastItemsCount || this.LastOwnerEntity != owner)
            {
                this.LastItemsCount  = itemCounter;
                this.LastOwnerEntity = owner;

                for (var i = inventoryGroup.Grid.transform.childCount - 1; i >= 0; i--)
                {
                    GameObject.Destroy(inventoryGroup.Grid.transform.GetChild(i).gameObject);
                }

                var itemsHashMap = new NativeMultiHashMap <int, ItemHashValue>(foodList.Length + potionList.Length + treasureList.Length, Allocator.Temp);
                for (var i = 0; i < foodList.Length; i++)
                {
                    itemsHashMap.Add(this.GetItemHash(foodList[i]), foodList[i]);
                }
                for (var i = 0; i < potionList.Length; i++)
                {
                    itemsHashMap.Add(this.GetItemHash(potionList[i]), potionList[i]);
                }
                for (var i = 0; i < treasureList.Length; i++)
                {
                    itemsHashMap.Add(this.GetItemHash(treasureList[i]), treasureList[i]);
                }

                if (itemsHashMap.Length > 0)
                {
                    var iterator = new NativeMultiHashMapIterator <int>();
                    var(keys, keysLength) = itemsHashMap.GetUniqueKeyArray(Allocator.Temp);
                    for (var keyI = 0; keyI < keysLength; keyI++)
                    {
                        if (!itemsHashMap.TryGetFirstValue(keys[keyI], out var hashValue, out iterator))
                        {
                            continue;
                        }

                        var button = this.AddInventoryButton(hashValue, inventoryGroup);

                        var itemsCount = 1;
                        while (itemsHashMap.TryGetNextValue(out hashValue, ref iterator))
                        {
                            itemsCount++;
                        }

                        if (itemsCount > 1)
                        {
                            button.Amount.text = itemsCount.ToString();
                        }
                        else
                        {
                            button.Amount.text = string.Empty;
                        }
                    }
                    keys.Dispose();
                }
                itemsHashMap.Dispose();

                for (var i = 0; i < weaponList.Length; i++)
                {
                    this.AddInventoryButton(weaponList[i], inventoryGroup);
                }
                for (var i = 0; i < magicWeaponList.Length; i++)
                {
                    this.AddInventoryButton(magicWeaponList[i], inventoryGroup);
                }
                for (var i = 0; i < helmetList.Length; i++)
                {
                    this.AddInventoryButton(helmetList[i], inventoryGroup);
                }
                for (var i = 0; i < chestList.Length; i++)
                {
                    this.AddInventoryButton(chestList[i], inventoryGroup);
                }
                for (var i = 0; i < bootsList.Length; i++)
                {
                    this.AddInventoryButton(bootsList[i], inventoryGroup);
                }
            }

            foodList.Dispose();
            potionList.Dispose();
            treasureList.Dispose();
            weaponList.Dispose();
            magicWeaponList.Dispose();
            helmetList.Dispose();
            chestList.Dispose();
            bootsList.Dispose();

            return(itemCounter);
        }
Ejemplo n.º 15
0
 public static bool SelectIterator <K, T>(this NativeMultiHashMap <K, T> HashMap, Predicate <T> Operate, K Key, out NativeMultiHashMapIterator <K> Iterator) where T : struct where K : struct, IEquatable <K>
 {
     for (bool Success = HashMap.TryGetFirstValue(Key, out var Value, out var It); Success;)