///--------------------------------------------
        void Preallocate <T>(uint groupID, uint size) where T : IEntityDescriptor, new()
        {
            var entityViewsToBuild  = EntityDescriptorTemplate <T> .descriptor.entitiesToBuild;
            var numberOfEntityViews = entityViewsToBuild.Length;

            //reserve space in the database
            if (_groupEntityViewsDB.TryGetValue(groupID, out var group) == false)
            {
                group = _groupEntityViewsDB[groupID] = new FasterDictionary <RefWrapper <Type>, ITypeSafeDictionary>();
            }

            for (var index = 0; index < numberOfEntityViews; index++)
            {
                var entityViewBuilder = entityViewsToBuild[index];
                var entityViewType    = entityViewBuilder.GetEntityType();

                var refWrapper = new RefWrapper <Type>(entityViewType);
                if (group.TryGetValue(refWrapper, out var dbList) == false)
                {
                    group[refWrapper] = entityViewBuilder.Preallocate(ref dbList, size);
                }
                else
                {
                    dbList.SetCapacity(size);
                }

                if (_groupsPerEntity.TryGetValue(refWrapper, out var groupedGroup) == false)
                {
                    groupedGroup = _groupsPerEntity[refWrapper] =
                        new FasterDictionary <uint, ITypeSafeDictionary>();
                }

                groupedGroup[groupID] = dbList;
            }
        }
        void CopyEntityToDictionary(EGID entityGID, EGID toEntityGID,
                                    FasterDictionary <RefWrapper <Type>, ITypeSafeDictionary> fromGroup,
                                    FasterDictionary <RefWrapper <Type>, ITypeSafeDictionary> toGroup, Type entityViewType)
        {
            var wrapper = new RefWrapper <Type>(entityViewType);

            if (fromGroup.TryGetValue(wrapper, out var fromTypeSafeDictionary) == false)
            {
                throw new ECSException("no entities in from group eid: ".FastConcat(entityGID.entityID)
                                       .FastConcat(" group: ").FastConcat(entityGID.groupID));
            }

#if DEBUG && !PROFILER
            if (fromTypeSafeDictionary.Has(entityGID.entityID) == false)
            {
                throw new EntityNotFoundException(entityGID, entityViewType);
            }
#endif
            if (toGroup.TryGetValue(wrapper, out var toEntitiesDictionary) == false)
            {
                toEntitiesDictionary = fromTypeSafeDictionary.Create();
                toGroup.Add(wrapper, toEntitiesDictionary);
            }

            //todo: this must be unit tested properly
            if (_groupsPerEntity.TryGetValue(wrapper, out var groupedGroup) == false)
            {
                groupedGroup = _groupsPerEntity[wrapper] =
                    new FasterDictionary <uint, ITypeSafeDictionary>();
            }

            groupedGroup[toEntityGID.groupID] = toEntitiesDictionary;

            fromTypeSafeDictionary.AddEntityToDictionary(entityGID, toEntityGID, toEntitiesDictionary);
        }
Пример #3
0
        public override bool Shoot(Player player, ref Vector2 position, ref float speedX, ref float speedY, ref int type, ref int damage, ref float knockBack)
        {
            if (drive is null)
            {
                return(false);
            }
            float c = maxCharge;
            RefWrapper <float> projCharge = getCharge;

            //Main.NewText($"{getCharge}/{c}");
            if (powerCellItem.type == ItemID.LihzahrdPowerCell)
            {
                RefWrapper <float> charge = powerCellItem.GetGlobalItem <PowerCellGlobalItem>().charge;
                if (charge.value < PowerCellGlobalItem.LihzahrdMaxCharge)
                {
                    return(false);
                }
                projCharge   = maxCharge;
                charge.value = 0;
            }
            base.Shoot(player, ref position, ref speedX, ref speedY, ref type, ref damage, ref knockBack);
            ActionContext context = ((BasicGlyphProjectile)Main.projectile[lastProj].modProjectile).context;

            context.charge = projCharge;
            context.Caster = player;
            context.color  = LensGlobalItem.getColor(lensItem);
            //((BasicGlyphProjectile)Main.projectile[lastProj].modProjectile).context.charge = projCharge;
            //((BasicGlyphProjectile)Main.projectile[lastProj].modProjectile).context.Caster = player;
            //((BasicGlyphProjectile)Main.projectile[lastProj].modProjectile).context.color = LensGlobalItem.getColor(lensItem);
            return(false);
        }
        ///--------------------------------------------
        void Preallocate <T>(ExclusiveGroupStruct groupID, uint size) where T : IEntityDescriptor, new()
        {
            using (var profiler = new PlatformProfiler("Preallocate"))
            {
                var entityComponentsToBuild  = EntityDescriptorTemplate <T> .descriptor.componentsToBuild;
                var numberOfEntityComponents = entityComponentsToBuild.Length;

                FasterDictionary <RefWrapper <Type>, ITypeSafeDictionary> group = GetOrCreateGroup(groupID, profiler);

                for (var index = 0; index < numberOfEntityComponents; index++)
                {
                    var entityComponentBuilder = entityComponentsToBuild[index];
                    var entityComponentType    = entityComponentBuilder.GetEntityComponentType();

                    var refWrapper = new RefWrapper <Type>(entityComponentType);
                    if (group.TryGetValue(refWrapper, out var dbList) == false)
                    {
                        group[refWrapper] = entityComponentBuilder.Preallocate(ref dbList, size);
                    }
                    else
                    {
                        dbList.SetCapacity(size);
                    }

                    if (_groupsPerEntity.TryGetValue(refWrapper, out var groupedGroup) == false)
                    {
                        groupedGroup = _groupsPerEntity[refWrapper] = new FasterDictionary <uint, ITypeSafeDictionary>();
                    }

                    groupedGroup[groupID] = dbList;
                }
            }
        }
Пример #5
0
        public void shouldUseReferenceEqualityForEquals()
        {
            string s1 = "Hello";
            string s2 = new string(s1.AsSpan());
            RefWrapper <string> w1 = s1, w2 = s2;

            Assert.True(w1.Equals(w1));
            Assert.True(w1.Equals((object)w1));
            Assert.True(w1.Equals(s1));
            Assert.True(w1.Equals((object)s1));
            Assert.True(w2.Equals(w2));
            Assert.True(w2.Equals((object)w2));
            Assert.True(w2.Equals(s2));
            Assert.True(w2.Equals((object)s2));
            Assert.False(w1.Equals(w2));
            Assert.False(w1.Equals((object)w2));
            Assert.False(w1.Equals(s2));
            Assert.False(w1.Equals((object)s2));
            Assert.False(w2.Equals(w1));
            Assert.False(w2.Equals((object)w1));
            Assert.False(w2.Equals(s1));
            Assert.False(w2.Equals((object)s1));
            Assert.False(w1.Equals(new object()));

            Assert.True(default(RefWrapper <string>).Equals(null));
            Assert.True(default(RefWrapper <string>).Equals((object)null));
            Assert.True(default(RefWrapper <string>).Equals((object)default(RefWrapper <string>)));
            Assert.False(default(RefWrapper <string>).Equals(w1));
            Assert.False(default(RefWrapper <string>).Equals(w2));
            Assert.False(w1.Equals(default(RefWrapper <string>)));
            Assert.False(w1.Equals(default(RefWrapper <string>)));
            Assert.False(w1.Equals((object)default(RefWrapper <string>)));
            Assert.False(w1.Equals((object)default(RefWrapper <string>)));
        }
Пример #6
0
        public void shouldWrapWithConversionOperator()
        {
            var obj = new object();
            RefWrapper <object> wrapper = obj;

            Assert.Same(wrapper.value, obj);
        }
Пример #7
0
        public void shouldProvideUniqueInstancePerThreadWithDifferentSeed()
        {
            var queue1  = new ConcurrentQueue <Random>();
            var queue2  = new ConcurrentQueue <Random>();
            var threads = new Thread[10];

            void worker()
            {
                queue1.Enqueue(ThreadStaticRandom.instance);
                queue2.Enqueue(ThreadStaticRandom.instance);
            }

            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new Thread(worker);
                threads[i].Start();
            }

            for (int i = 0; i < threads.Length; i++)
            {
                threads[i].Join();
            }

            Assert.Equal(queue1.Count, threads.Length);
            Assert.Equal(queue2.Count, threads.Length);

            var instances  = new RefWrapper <Random> [threads.Length];
            var instances2 = new RefWrapper <Random> [threads.Length];

            for (int i = 0; i < instances.Length; i++)
            {
                Random inst;
                queue1.TryDequeue(out inst);
                instances[i] = inst;
                queue2.TryDequeue(out inst);
                instances2[i] = inst;
            }

            Assert.Equal(instances.Length, instances.Distinct().Count());
            Assert.Equal(instances2.Length, instances2.Distinct().Count());
            Assert.True(instances.ToHashSet().SetEquals(instances2.ToHashSet()));

            var bigints = new BigInteger[instances.Length];

            for (int i = 0; i < instances.Length; i++)
            {
                bigints[i] = randomBigInt(instances[i].value, 64);
            }

            Assert.Equal(bigints.Length, bigints.Distinct().Count());

            BigInteger randomBigInt(Random r, int byteLength)
            {
                byte[] b = new byte[byteLength];
                r.NextBytes(b);
                return(new BigInteger(b, isUnsigned: true));
            }
        }
        public IActionResult Data()
        {
            RefWrapper <IList <President> > widgets = new RefWrapper <IList <President> >
            {
                Value = new List <President>()
            };

            return(View(GetModel(new WidgetGetter(widgets))));
        }
Пример #9
0
        public void shouldUseReferenceEqualityForHashCode()
        {
            string s1 = "Hello";
            string s2 = new string(s1.AsSpan());
            RefWrapper <string> w1 = s1, w2 = s2;

            Assert.Equal(w1.GetHashCode(), RuntimeHelpers.GetHashCode(s1));
            Assert.Equal(w2.GetHashCode(), RuntimeHelpers.GetHashCode(s2));
        }
Пример #10
0
    public static int Main()
    {
        RefWrapper a = new RefWrapper();

        arr    = new RefWrapper[1];
        arr[0] = a;

        RefWrapper e = GetElement(); // force struct retyping to ref.

        arr[0] = e;                  // a struct typed copy.
        return(100);
    }
Пример #11
0
        public void shouldUseReferenceEqualityForOperator()
        {
            string s1 = "Hello";
            string s2 = new string(s1.AsSpan());
            RefWrapper <string> w1 = s1, w2 = s2;
            RefWrapper <string> w3 = w1, w4 = w2;

            Assert.True(w1 == w3);
            Assert.True(w2 == w4);
            Assert.True(w1 != w2);
            Assert.True(w1 != w4);
            Assert.False(w1 != w3);
            Assert.False(w2 != w4);
            Assert.False(w1 == w2);
            Assert.False(w1 == w4);
        }
Пример #12
0
        public static ExclusiveGroupStruct AddTag <T>(this ExclusiveGroupStruct group) where T : GroupTag <T>
        {
            if (_addTransitions.TryGetValue(group, out var transitions))
            {
                var type = new RefWrapper <Type>(typeof(T));
                if (transitions.TryGetValue(type, out var result))
                {
                    return(result);
                }
            }

            throw new ECSException("No add transition found for type "
                                   .FastConcat(typeof(T).ToString())
                                   .FastConcat(" in group ").FastConcat(group)
                                   );
        }
Пример #13
0
        public static void SetTagRemoval <T>(this ExclusiveGroupStruct group, ExclusiveGroupStruct target, bool setReverse = true) where T : GroupTag <T>
        {
            if (_removeTransitions.TryGetValue(group, out var transitions) == false)
            {
                transitions = new FasterDictionary <RefWrapper <Type>, ExclusiveGroupStruct>();
                _removeTransitions[group] = transitions;
            }

            var type = new RefWrapper <Type>(typeof(T));

            transitions[type] = target;

            if (setReverse)
            {
                SetTagAddition <T>(target, group, false);
            }
        }
Пример #14
0
        public void EndPlanExecution()
        {
            if (GetActorGroup() != null)
            {
                ending = true;
                foreach (var t in new List <Task>(tasks))
                {
                    t.EndExecution();
                    RemoveTaskFromPlan(t);
                }
                //tasks.Clear();
                Debug.Log("ended plan");
                GetActorGroup().UnregisterOwnedPlan(this);

                _group = null;
            }
        }
Пример #15
0
        public void AddEngine(IEngine engine)
        {
            var type       = engine.GetType();
            var refWrapper = new RefWrapper <Type>(type);

            DBC.ECS.Check.Require(engine != null, "Engine to add is invalid or null");
            DBC.ECS.Check.Require(
                _enginesTypeSet.Contains(refWrapper) == false ||
                type.ContainsCustomAttribute(typeof(AllowMultipleAttribute)) == true
                , "The same engine has been added more than once, if intentional, use [AllowMultiple] class attribute "
                .FastConcat(engine.ToString()));
            try
            {
                if (engine is IReactOnAddAndRemove viewEngine)
                {
                    CheckEntityComponentsEngine(viewEngine, _reactiveEnginesAddRemove);
                }

                if (engine is IReactOnSwap viewEngineSwap)
                {
                    CheckEntityComponentsEngine(viewEngineSwap, _reactiveEnginesSwap);
                }

                _enginesTypeSet.Add(refWrapper);
                _enginesSet.Add(engine);

                if (engine is IDisposable)
                {
                    _disposableEngines.Add(engine as IDisposable);
                }

                if (engine is IQueryingEntitiesEngine queryableEntityComponentEngine)
                {
                    queryableEntityComponentEngine.entitiesDB = _entitiesDB;
                    queryableEntityComponentEngine.Ready();
                }
            }
            catch (Exception e)
            {
                throw new ECSException("Code crashed while adding engine ".FastConcat(engine.GetType().ToString(), " ")
                                       , e);
            }
        }
        void CopyEntityToDictionary(EGID entityGID, EGID toEntityGID,
                                    FasterDictionary <RefWrapper <Type>, ITypeSafeDictionary> fromGroup,
                                    FasterDictionary <RefWrapper <Type>, ITypeSafeDictionary> toGroup,
                                    Type entityViewType)
        {
            var wrapper = new RefWrapper <Type>(entityViewType);

            ITypeSafeDictionary fromTypeSafeDictionary = GetTypeSafeDictionary(entityGID.groupID, fromGroup, wrapper);

#if DEBUG && !PROFILER
            if (fromTypeSafeDictionary.Has(entityGID.entityID) == false)
            {
                throw new EntityNotFoundException(entityGID, entityViewType);
            }
#endif
            ITypeSafeDictionary toEntitiesDictionary =
                GetOrCreateTypeSafeDictionary(toEntityGID.groupID, toGroup, wrapper, fromTypeSafeDictionary);

            fromTypeSafeDictionary.AddEntityToDictionary(entityGID, toEntityGID, toEntitiesDictionary);
        }
Пример #17
0
        public static void SetTagSwap <TRemove, TAdd>(this ExclusiveGroupStruct group, ExclusiveGroupStruct target, bool setReverse = true)
            where TRemove : GroupTag <TRemove> where TAdd : GroupTag <TAdd>
        {
            if (_swapTransitions.TryGetValue(group, out var transitions) == false)
            {
                transitions             = new FasterDictionary <RefWrapper <Type>, ExclusiveGroupStruct>();
                _swapTransitions[group] = transitions;
            }

            var type = new RefWrapper <Type>(typeof(TAdd));

            transitions[type] = target;

            // To avoid needing to check if the group already has the tag when swaping (prevent ecs exceptions).
            // The current groups adds the removed tag pointing to itself.
            type = new RefWrapper <Type>(typeof(TRemove));
            transitions[type] = group;

            if (setReverse)
            {
                SetTagSwap <TAdd, TRemove>(target, group, false);
            }
        }
Пример #18
0
 /// <summary>
 /// Constructor with parameters for construction with <see cref="VariableSource"/>
 /// </summary>
 /// <param name="value"></param>
 private Variable(RefWrapper <Complex> value, RefWrapper <string> label)
 {
     _Value = value;
     _Label = label;
 }
Пример #19
0
 /// <summary>
 /// Constructor with parameters for static constants and implicit conversions
 /// </summary>
 private Variable(Complex value)
 {
     _Value = new RefWrapper <Complex>(value);
 }
Пример #20
0
        public override void AI()
        {
            ActionItem item;
            bool       fail;

            if (context.Caster is null)
            {
                context.Caster = Main.player[projectile.owner];
            }
            int    cycleCount = 0;
            object ret;

            if ((glyphType & GlyphProjectileType.Tracer) != 0)
            {
                Dust.NewDustPerfect(projectile.Center, 267, new Vector2(projectile.velocity.X * 0.2f, projectile.velocity.Y * 0.2f), 100, context.color, 0.75f).noGravity = true;
            }
            RefWrapper <float> charge = context.charge;

            if (context.costMult == -1 && context.Caster is Player player)
            {
                charge.value = player.statMana / 20;
            }
            while (context.Delay < 1 && (glyphType & GlyphProjectileType.Paused) == 0)
            {
                try {
                    fail = context.Cursor >= actions.Count || (++cycleCount > 255);
                    if (fail)
                    {
                        if ((glyphType & GlyphProjectileType.Repeat) != 0)
                        {
                            context.Cursor = 0;
                            if ((glyphType & GlyphProjectileType.OnHit) != 0)
                            {
                                glyphType |= GlyphProjectileType.Paused;
                            }
                        }
                        else
                        {
                            projectile.Kill();
                        }
                        return;
                    }
                    item         = actions[context.Cursor++];
                    item.context = context;
                    float cost = item.cost;
                    if (context.costMult > 0)
                    {
                        cost *= context.costMult;
                    }
                    if (cost <= (charge?.value ?? 100))
                    {
                        if (!(charge is null))
                        {
                            ConsumeCharge(cost);
                        }
                        float delay = item.delay;
                        if (!(item is SleepControl)) //if anyone has a reason why this should be a property instead of being uniqe to SleepControl, please tell me
                        {
                            switch (context.delayMult)
                            {
                            case -1:
                                delay = 15;
                                break;

                            default:
                                delay *= context.delayMult;
                                break;
                            }
                        }
                        context.Delay     += delay;
                        ret                = item.Execute(glyphType);
                        context.lastReturn = ret ?? context.lastReturn;
                    }
                    else
                    {
                        context.Cursor = actions.Count;
                        mod.Logger.Info($"Didn't have {cost} charge for {item.GetType()} at {context.Cursor}");
                    }
                } catch (InvalidTimeZoneException) {
                    projectile.Kill();
                    return;
                }
            }
            while (context.parameters.Count > 16)
            {
                context.parameters.RemoveAt(16);
            }
            if ((glyphType & GlyphProjectileType.Paused) == 0)
            {
                context.Delay--;
            }
        }
Пример #21
0
 public TaskPlan2(IActorGroup group)
 {
     _group = new RefWrapper <IActorGroup>(group);
 }
            ref FilterGroup CreateOrGetFilterForGroup(int filterID, ExclusiveGroupStruct groupID, RefWrapper <Type> refWrapper)
            {
                var fasterDictionary =
                    _filters.GetOrCreate(refWrapper, () => new FasterDictionary <ExclusiveGroupStruct, GroupFilters>());

                GroupFilters filters =
                    fasterDictionary.GetOrCreate(
                        groupID, () => new GroupFilters(new SharedSveltoDictionaryNative <int, FilterGroup>(0), groupID));

                return(ref filters.CreateOrGetFilter(filterID, groupID));
            }