Пример #1
0
        ////////////////

        internal CustomEntity Convert()
        {
            if (!this.IsInitialized)
            {
                throw new HamstarException("Not initialized.");
            }

            CustomEntity ent;
            Type         entType = CustomEntityManager.GetTypeByName(this.MyTypeName);

            if (entType == null)
            {
                throw new HamstarException(this.MyTypeName + " does not exist.");
            }
            if (!entType.IsSubclassOf(typeof(CustomEntity)))
            {
                throw new HamstarException(entType.Name + " is not a valid CustomEntity.");
            }

            if (string.IsNullOrEmpty(this.OwnerPlayerUID))
            {
                ent = CustomEntity.CreateRaw(entType, this.Core, this.Components);
            }
            else
            {
                ent = CustomEntity.CreateRaw(entType, this.Core, this.Components, this.OwnerPlayerUID);
            }

            ent.InternalOnClone();

            return(ent);
        }
Пример #2
0
        ////

        public static ISet <CustomEntity> GetEntitiesByComponent <T>() where T : CustomEntityComponent
        {
            CustomEntityManager mngr = ModHelpersMod.Instance.CustomEntMngr;

            ISet <int> entIdxs  = new HashSet <int>();
            Type       currType = typeof(T);

            lock (CustomEntityManager.MyLock) {
                if (!mngr.WorldEntitiesByComponentType.TryGetValue(currType, out entIdxs))
                {
                    foreach (var kv in mngr.WorldEntitiesByComponentType)
                    {
                        if (kv.Key.IsSubclassOf(currType))
                        {
                            entIdxs.UnionWith(kv.Value);
                        }
                    }

                    if (entIdxs == null)
                    {
                        return(new HashSet <CustomEntity>());
                    }
                }

                return(new HashSet <CustomEntity>(
                           entIdxs.SafeSelect(i => (CustomEntity)mngr.WorldEntitiesByIndexes[i])
                           ));
            }
        }
        public static void RemoveEntityByWho(int who)
        {
            CustomEntityManager mngr = ModHelpersMod.Instance.CustomEntMngr;

            if (!mngr.WorldEntitiesByIndexes.ContainsKey(who))
            {
                return;
            }

            Type compType;
            Type baseType = typeof(CustomEntityComponent);

            lock (CustomEntityManager.MyLock) {
                IList <CustomEntityComponent> entComponents = mngr.WorldEntitiesByIndexes[who].InternalComponents;

                foreach (CustomEntityComponent component in entComponents)
                {
                    compType = component.GetType();
                    do
                    {
                        if (mngr.WorldEntitiesByComponentType.ContainsKey(compType))
                        {
                            mngr.WorldEntitiesByComponentType[compType].Remove(who);
                        }

                        compType = compType.BaseType;
                    } while(compType != baseType);
                }

                mngr.WorldEntitiesByIndexes.Remove(who);
            }
        }
Пример #4
0
        ////////////////

        public static CustomEntity GetEntityByWho(int whoAmI)
        {
            CustomEntityManager mngr = ModHelpersMod.Instance.CustomEntMngr;

            CustomEntity ent = null;

            mngr.WorldEntitiesByIndexes.TryGetValue(whoAmI, out ent);
            return(ent);
        }
        public static void ClearAllEntities()
        {
            CustomEntityManager mngr = ModHelpersMod.Instance.CustomEntMngr;

            lock (CustomEntityManager.MyLock) {
                mngr.WorldEntitiesByIndexes.Clear();
                mngr.WorldEntitiesByComponentType.Clear();
            }
        }
        ////////////////

        public static void RemoveEntity(CustomEntity ent)
        {
            if (ent == null)
            {
                throw new HamstarException("Null ent not allowed.");
            }

            CustomEntityManager.RemoveEntityByWho(ent.Core.whoAmI);
        }
Пример #7
0
        public static bool IsInWorld(CustomEntity myent)
        {
            CustomEntityManager mngr = ModHelpersMod.Instance.CustomEntMngr;

            CustomEntity ent = null;

            mngr.WorldEntitiesByIndexes.TryGetValue(myent.Core.WhoAmI, out ent);

            return(ent == myent);
        }
Пример #8
0
        public static int GetIdByTypeName(string name)
        {
            CustomEntityManager mngr = ModHelpersMod.Instance.CustomEntMngr;

            if (!mngr.EntTypeIds.ContainsKey(name))
            {
                //throw new HamstarException( "!ModHelpers.CustomEntityManager.GetIdByTypeName - No CustomEntity of type " + name );
                throw new HamstarException("No CustomEntity of type " + name);
            }
            return(mngr.EntTypeIds[name]);
        }
Пример #9
0
        public static Type GetTypeById(int typeId)
        {
            CustomEntityManager mngr = ModHelpersMod.Instance.CustomEntMngr;

            if (!mngr.TypeIdEnts.ContainsKey(typeId))
            {
                //throw new HamstarException( "!ModHelpers.CustomEntityManager.GetTypeById - No CustomEntity of type id "+typeId );
                throw new HamstarException("No CustomEntity of type id " + typeId);
            }
            return(mngr.TypeIdEnts[typeId]);
        }
Пример #10
0
        public static Type GetTypeByName(string name)
        {
            int typeId = -1;

            try {
                typeId = CustomEntityManager.GetIdByTypeName(name);
                return(CustomEntityManager.GetTypeById(typeId));
            } catch (HamstarException e) {
                //throw new HamstarException( "!ModHelpers.CustomEntityManager.GetTypeByName - No CustomEntity " + name, e );
                throw new HamstarException("No CustomEntity " + name, e);
            }
        }
        public static void AddToWorld(CustomEntity ent)
        {
            CustomEntityManager mngr = ModHelpersMod.Instance.CustomEntMngr;
            int who = mngr.WorldEntitiesByIndexes.Count + 1;

            if (!ent.SyncFromClient && !ent.SyncFromServer)
            {
                who = -who;
            }

            CustomEntityManager.AddToWorld(who, ent);
        }
Пример #12
0
        protected override void ReadStream(BinaryReader reader)
        {
            int    typeId      = (ushort)reader.ReadUInt16();
            byte   ownerWho    = (byte)reader.ReadByte();
            int    who         = (ushort)reader.ReadUInt16();
            string displayName = (string)reader.ReadString();
            var    pos         = new Vector2 {
                X = (float)reader.ReadSingle(),
                Y = (float)reader.ReadSingle()
            };
            int dir = (short)reader.ReadInt16();
            int wid = (ushort)reader.ReadUInt16();
            int hei = (ushort)reader.ReadUInt16();
            var vel = new Vector2 {
                X = (float)reader.ReadSingle(),
                Y = (float)reader.ReadSingle()
            };

            Type entType = CustomEntityManager.GetTypeById(typeId);

            if (entType == null)
            {
                //throw new HamstarException( "!ModHelpers.CustomEntity.ReadStream - Invalid entity type id " + typeId );
                throw new HamstarException("Invalid entity type id " + typeId);
            }

            Player plr = ownerWho == (byte)255 ? null : Main.player[ownerWho];

            var myentTemplate = (CustomEntity)PacketProtocolData.CreateInstance(entType);
            //var myentTemplate = (CustomEntity)Activator.CreateInstance( entType, BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { }, null );

            CustomEntityCore core = myentTemplate.CreateCoreTemplate();
            IList <CustomEntityComponent> components = myentTemplate.CreateComponentsTemplate();

            core.WhoAmI      = who;
            core.DisplayName = displayName;
            core.Width       = wid;
            core.Height      = hei;
            core.Position    = pos;
            core.Velocity    = vel;
            core.Direction   = dir;

            for (int i = 0; i < components.Count; i++)
            {
                components[i].ReadStreamForwarded(reader);
                components[i].InternalOnClone();
            }

            this.MyTypeName = SerializableCustomEntity.GetTypeName(myentTemplate);
            this.CopyChangesFrom(core, components, plr);
//LogHelpers.Log( "READ "+this );
        }
Пример #13
0
        ////

        public static ISet <T> GetEntitiesForPlayer <T>(Player player) where T : CustomEntity
        {
            CustomEntityManager mngr = ModHelpersMod.Instance.CustomEntMngr;
            var ents = new HashSet <T>();

            foreach (CustomEntity ent in mngr.WorldEntitiesByIndexes.Values)
            {
                if (!(ent is T))
                {
                    continue;
                }

                if (Main.netMode == 2)
                {
                    ent.RefreshOwnerWho();
                }
                if (ent.MyOwnerPlayerWho == player.whoAmI)
                {
                    ents.Add((T)ent);
                }
            }
            return(ents);
        }
Пример #14
0
        ////////////////

        public override string ToString()
        {
            string basename = "";
            string typeid   = "type " + CustomEntityManager.GetIdByTypeName(this.GetType().Name);
            string who      = "";
            string owner    = ", owner:";

            if (this.Core == null)
            {
                basename = "Undefined entity";
            }
            else
            {
                basename = this.Core.DisplayName;
                who      = ", who " + this.Core.whoAmI;
            }

            if (!string.IsNullOrEmpty(this.OwnerPlayerUID))
            {
                owner += " " + this.OwnerPlayerUID.Substring(0, 8) + "...";
            }
            if (this.OwnerPlayerWho != -1)
            {
                owner += " " + (("'" + Main.player[this.OwnerPlayerWho]?.name + "'") ?? "MISSING_PLAYER") + "':" + this.OwnerPlayerWho;
            }
            if (this.OwnerPlayerUID == "" && this.OwnerPlayerWho == -1)
            {
                owner += " none";
            }

            if (this.Components != null)
            {
                typeid = typeid + ":" + this.Components.Count();
            }

            return(basename + " [" + this.GetType().Name + "] (" + typeid + who + owner + ")");
        }
Пример #15
0
        protected override void WriteStream(BinaryWriter writer)
        {
            if (!this.IsInitialized)
            {
                //throw new HamstarException( "!ModHelpers.SerializableCustomEntity.WriteStream - Not initialized." );
                throw new HamstarException("Not initialized.");
            }

            if (Main.netMode != 1)
            {
                this.RefreshOwnerWho();
            }

            CustomEntityCore core     = this.Core;
            byte             ownerWho = this.OwnerPlayerWho == -1 ? (byte)255 : (byte)this.OwnerPlayerWho;

            writer.Write((ushort)CustomEntityManager.GetIdByTypeName(this.MyTypeName));
            writer.Write((byte)ownerWho);
//LogHelpers.Log( "WRITE id: "+this.ID+", name: "+core.DisplayName+", templates: "+ CustomEntityTemplates.TotalEntityTemplates());
//LogHelpers.Log( "WRITE2 who: "+core.whoAmI+", component count: "+this.Components.Count );
            writer.Write((ushort)core.whoAmI);
            writer.Write((string)core.DisplayName);
            writer.Write((float)core.position.X);
            writer.Write((float)core.position.Y);
            writer.Write((short)core.direction);
            writer.Write((ushort)core.width);
            writer.Write((ushort)core.height);
            writer.Write((float)core.velocity.X);
            writer.Write((float)core.velocity.Y);

            for (int i = 0; i < this.Components.Count; i++)
            {
                this.Components[i].WriteStreamForwarded(writer);
            }
//LogHelpers.Log( "WRITE "+this.ToString()+" pos:"+ core.position );
        }
        public static CustomEntity AddToWorld(int who, CustomEntity ent, bool skipSync = false)
        {
            //if( ent == null ) { throw new HamstarException( "!ModHelpers.CustomEntityManager.AddToWorld - Null ent not allowed." ); }
            //if( !ent.IsInitialized ) { throw new HamstarException( "!ModHelpers.CustomEntityManager.AddToWorld - Initialized ents only." ); }
            if (ent == null)
            {
                throw new HamstarException("Null ent not allowed.");
            }
            if (!ent.IsInitialized)
            {
                throw new HamstarException("Initialized ents only.");
            }

            CustomEntityManager mngr    = ModHelpersMod.Instance.CustomEntMngr;
            CustomEntity        realEnt = ent;

            if (mngr.WorldEntitiesByIndexes.ContainsKey(who))
            {
                //throw new HamstarException( "!ModHelpers.CustomEntityManager.AddToWorld - "
                //	+ "Attempting to add "+ent.ToString()+" to slot "+who+" occupied by "+mngr.WorldEntitiesByIndexes[who].ToString() );
                throw new HamstarException("Attempting to add " + ent.ToString() + " to slot " + who + " occupied by "
                                           + mngr.WorldEntitiesByIndexes[who].ToString());
            }

            if (ent is SerializableCustomEntity)
            {
                realEnt = ((SerializableCustomEntity)ent).Convert();
            }

            Type compType;
            Type baseType = typeof(CustomEntityComponent);

            // Map entity to each of its components
            foreach (CustomEntityComponent component in realEnt.InternalComponents)
            {
                compType = component.GetType();
                lock (CustomEntityManager.MyLock) {
                    do
                    {
                        if (!mngr.WorldEntitiesByComponentType.ContainsKey(compType))
                        {
                            mngr.WorldEntitiesByComponentType[compType] = new HashSet <int>();
                        }

                        mngr.WorldEntitiesByComponentType[compType].Add(who);

                        compType = compType.BaseType;
                    } while(compType != baseType);
                }
            }

            realEnt.Core.whoAmI = who;
            mngr.WorldEntitiesByIndexes[who] = realEnt;

            realEnt.InternalOnAddToWorld();

            // Sync also
            if (!skipSync)
            {
                if (Main.netMode == 1)
                {
                    if (ent.SyncFromClient)
                    {
                        Promises.AddValidatedPromise(SaveableEntityComponent.LoadAllValidator, () => {
                            ent.SyncToAll();
                            return(false);
                        });
                    }
                }
                else if (Main.netMode == 2)
                {
                    if (ent.SyncFromServer)
                    {
                        Promises.AddValidatedPromise(SaveableEntityComponent.LoadAllValidator, () => {
                            ent.SyncToAll();
                            return(false);
                        });
                    }
                }
            }

            if (ModHelpersMod.Instance.Config.DebugModeCustomEntityInfo)
            {
                LogHelpers.Alert("Set " + realEnt.ToString());
            }

            return(realEnt);
        }