Beispiel #1
0
        public bool AddEntity(YmapEntityDef ent, int roomIndex)
        {
            if (ent == null)
            {
                return(false);
            }

            // entity already exists in our array. so we'll just add
            // it to the instanced entities list and continue.
            MloInstanceData mloInstance = ent.MloParent?.MloInstance;
            MCEntityDef     ymcent      = mloInstance?.TryGetArchetypeEntity(ent);

            if (ymcent != null)
            {
                return(true);
            }

            if (roomIndex > rooms.Length)
            {
                throw new ArgumentOutOfRangeException($"Room index {roomIndex} exceeds the amount of rooms in {Name}.");
            }

            var mcEntityDef = new MCEntityDef(ref ent._CEntityDef, this);

            // Add the new entity def to the entities list.
            AddEntity(ent, mcEntityDef);

            // Update the attached objects in the room index specified.
            AttachEntityToRoom(ent, roomIndex);
            return(true);
        }
        public void SetEntity(YmapEntityDef entity)
        {
            CurrentEntity = entity;
            MloInstanceData instance = entity?.MloParent?.MloInstance;

            CurrentMCEntity = instance?.TryGetArchetypeEntity(entity);
            Tag             = entity;
            LoadEntity();
            UpdateFormTitle();
        }
Beispiel #3
0
        public void LoadChildren(Meta meta)
        {
            var centities = MetaTypes.ConvertDataArray <CEntityDef>(meta, MetaName.CEntityDef, _MloArchetypeDefData.entities);

            if (centities != null)
            {
                entities = new MCEntityDef[centities.Length];
                for (int i = 0; i < centities.Length; i++)
                {
                    entities[i] = new MCEntityDef(meta, ref centities[i])
                    {
                        Archetype = this
                    };
                }
            }

            var crooms = MetaTypes.ConvertDataArray <CMloRoomDef>(meta, MetaName.CMloRoomDef, _MloArchetypeDefData.rooms);

            if (crooms != null)
            {
                rooms = new MCMloRoomDef[crooms.Length];
                for (int i = 0; i < crooms.Length; i++)
                {
                    rooms[i] = new MCMloRoomDef(meta, crooms[i])
                    {
                        Archetype = this, Index = i
                    };
                }
            }

            var cportals = MetaTypes.ConvertDataArray <CMloPortalDef>(meta, MetaName.CMloPortalDef, _MloArchetypeDefData.portals);

            if (cportals != null)
            {
                portals = new MCMloPortalDef[cportals.Length];
                for (int i = 0; i < cportals.Length; i++)
                {
                    portals[i] = new MCMloPortalDef(meta, cportals[i]);
                }
            }

            var centitySets = MetaTypes.ConvertDataArray <CMloEntitySet>(meta, MetaName.CMloEntitySet, _MloArchetypeDefData.entitySets);

            if (centitySets != null)
            {
                entitySets = new MCMloEntitySet[centitySets.Length];
                for (int i = 0; i < centitySets.Length; i++)
                {
                    entitySets[i] = new MCMloEntitySet(meta, centitySets[i]);
                }
            }


            timeCycleModifiers = MetaTypes.ConvertDataArray <CMloTimeCycleModifier>(meta, MetaName.CMloTimeCycleModifier, _MloArchetypeDefData.timeCycleModifiers);
        }
Beispiel #4
0
        /* Specific Utils */
        public static void World2Mlo(MCEntityDef entity, MCMloArchetypeDef mlo, Vector3 mloWorldPosition, Quaternion mloWorldRotation)
        {
            var objRot       = new Quaternion(entity.Rotation.X, entity.Rotation.Y, entity.Rotation.Z, entity.Rotation.W);
            var rotationDiff = objRot * mloWorldRotation;    // Multiply initial entity rotation by mlo rotation

            rotationDiff.Normalize();

            entity.Position -= mloWorldPosition;                                                                          // Substract mlo world coords from entity world coords
            entity.Position  = Utils.RotateTransform(Quaternion.Invert(mloWorldRotation), entity.Position, Vector3.Zero); // Rotate entity around center of mlo instance (mlo entities rotations in space are inverted)

            entity.Rotation = new Vector4(rotationDiff.X, rotationDiff.Y, rotationDiff.Z, rotationDiff.W);
        }
Beispiel #5
0
        public static void Mlo2World(MCEntityDef entity, MCMloArchetypeDef mlo, Vector3 mloWorldPosition, Quaternion mloWorldRotation)
        {
            var objRot = new Quaternion(entity.Rotation.X, entity.Rotation.Y, entity.Rotation.Z, entity.Rotation.W);

            entity.Position  = Utils.RotateTransform(mloWorldRotation, entity.Position, Vector3.Zero);
            entity.Position += mloWorldPosition;

            var rotationDiff = objRot * Quaternion.Invert(mloWorldRotation);

            rotationDiff.Normalize();

            entity.Rotation = new Vector4(rotationDiff.X, rotationDiff.Y, rotationDiff.Z, rotationDiff.W);
        }
Beispiel #6
0
        public bool RemoveEntity(YmapEntityDef ent)
        {
            if (ent.Index >= entities.Length)
            {
                return(false);
            }

            MCEntityDef     delent = entities[ent.Index];
            MloInstanceData inst   = ent.MloParent?.MloInstance;

            if (inst == null)
            {
                return(false);
            }

            if (delent != null)
            {
                MCEntityDef[] newentities = new MCEntityDef[entities.Length - 1];
                bool          didDel      = false;
                int           index       = 0;
                int           delIndex    = 0;
                for (int i = 0; i < entities.Length; i++)
                {
                    if (entities[i] == delent)
                    {
                        delIndex = i;
                        didDel   = true;
                        continue;
                    }

                    newentities[index] = entities[i];
                    YmapEntityDef ymapEntityDef = inst.TryGetYmapEntity(newentities[index]);
                    if (ymapEntityDef != null)
                    {
                        ymapEntityDef.Index = index;
                    }
                    index++;
                }

                entities = newentities;

                if (didDel)
                {
                    FixRoomIndexes(delIndex);
                    FixPortalIndexes(delIndex);
                }
                return(didDel);
            }

            return(false);
        }
Beispiel #7
0
        private YmapEntityDef CreateYmapEntity(YmapEntityDef owner, MCEntityDef ment, int i)
        {
            YmapEntityDef e = new YmapEntityDef(null, i, ref ment._Data);

            e.Extensions        = ment.Extensions;
            e.MloRefPosition    = e.Position;
            e.MloRefOrientation = e.Orientation;
            e.MloParent         = owner;
            e.Position          = owner.Position + owner.Orientation.Multiply(e.MloRefPosition);
            e.Orientation       = Quaternion.Multiply(owner.Orientation, e.MloRefOrientation);
            e.UpdateWidgetPosition();
            e.UpdateWidgetOrientation();
            return(e);
        }
Beispiel #8
0
 public int GetEntityObjectIndex(MCEntityDef ent)
 {
     if (entities == null)
     {
         return(-1);
     }
     for (int i = 0; i < entities.Length; i++)
     {
         var e = entities[i];
         if (e == ent)
         {
             return(i);
         }
     }
     return(-1);
 }
Beispiel #9
0
        // Adds an entity to the entities array and then set's the index of the
        // ymap entity to the index of the new MCEntityDef.
        private void AddEntity(YmapEntityDef ent, MCEntityDef mcEntityDef)
        {
            if (ent == null || mcEntityDef == null)
            {
                return;                                     // no entity?...
            }
            // initialize the array.
            if (entities == null)
            {
                entities = new MCEntityDef[0];
            }

            List <MCEntityDef> entList = entities.ToList();

            entList.Add(mcEntityDef);
            ent.Index = entList.IndexOf(mcEntityDef);
            entities  = entList.ToArray();
        }
Beispiel #10
0
        public YmapEntityDef TryGetYmapEntity(MCEntityDef mcEntity)
        {
            if (mcEntity == null)
            {
                return(null);
            }
            if (Owner?.Archetype == null)
            {
                return(null);
            }
            if (!(Owner.Archetype is MloArchetype mloa))
            {
                return(null);
            }

            var index = Array.FindIndex(mloa.entities, x => x == mcEntity);

            if ((index >= 0) && (index < Entities.Length))
            {
                return(Entities[index]);
            }

            if (EntitySets != null)
            {
                for (int e = 0; e < EntitySets.Length; e++)
                {
                    var entset  = EntitySets[e];
                    var ents    = entset.Entities;
                    var set     = entset.EntitySet;
                    var setents = set?.Entities;
                    if ((ents == null) || (setents == null))
                    {
                        continue;
                    }
                    var idx = Array.FindIndex(setents, x => x == mcEntity);
                    if ((idx >= 0) && (idx < ents.Count))
                    {
                        return(ents[idx]);
                    }
                }
            }

            return(null);
        }
Beispiel #11
0
        public MCMloRoomDef GetEntityRoom(MCEntityDef ent)
        {
            int objectIndex = -1;

            for (int i = 0; i < entities.Length; i++)
            {
                MCEntityDef e = entities[i];
                if (e == ent)
                {
                    objectIndex = i;
                    break;
                }
            }
            if (objectIndex == -1)
            {
                return(null);
            }

            MCMloRoomDef room = null;

            for (int i = 0; i < rooms.Length; i++)
            {
                MCMloRoomDef r = rooms[i];
                for (int j = 0; j < r.AttachedObjects.Length; j++)
                {
                    uint ind = r.AttachedObjects[j];
                    if (ind == objectIndex)
                    {
                        room = r;
                        break;
                    }
                }
                if (room != null)
                {
                    break;
                }
            }

            return(room);
        }
Beispiel #12
0
        public YmapEntityDef TryGetYmapEntity(MCEntityDef mcEntity)
        {
            if (mcEntity == null)
            {
                return(null);
            }
            if (Owner?.Archetype == null)
            {
                return(null);
            }
            if (!(Owner.Archetype is MloArchetype mloa))
            {
                return(null);
            }

            var index = Array.FindIndex(mloa.entities, x => x == mcEntity);

            if (index == -1 || index >= Entities.Length)
            {
                return(null);
            }
            return(Entities[index]);
        }
Beispiel #13
0
        public MCMloEntitySet GetEntitySet(MCEntityDef ent)
        {
            if (entitySets == null)
            {
                return(null);
            }

            for (int i = 0; i < entitySets.Length; i++)
            {
                var set = entitySets[i];
                if (set.Entities != null)
                {
                    for (int j = 0; j < set.Entities.Length; j++)
                    {
                        if (set.Entities[j] == ent)
                        {
                            return(set);
                        }
                    }
                }
            }

            return(null);
        }
Beispiel #14
0
        public static MCMloRoomDef GetRoomForEntity(MCMloArchetypeDef mlo, Vector3[][] roomExtents, MCEntityDef entity)
        {
            for (int i = 0; i < roomExtents.Length; i++)
            {
                if (mlo.Rooms[i].Name == "limbo")
                {
                    continue;
                }

                var extents = roomExtents[i];
                var emin    = extents[0];
                var emax    = extents[1];

                var drawable = Program.GetDrawable(entity.ArchetypeName);

                if (drawable == null)
                {
                    if (
                        entity.Position.X >= emin.X && entity.Position.Y >= emin.Y && entity.Position.Z >= emin.Z &&
                        entity.Position.X <= emax.X && entity.Position.Y <= emax.Y && entity.Position.Z <= emax.Z
                        )
                    {
                        return(mlo.Rooms[i]);
                    }
                }
                else
                {
                    Quaternion orientation = new Quaternion(entity.Rotation);

                    Vector3 dcenter = (Vector3)drawable.BoundingCenter;
                    Vector3 dbbmin  = (Vector3)(Vector4)drawable.BoundingBoxMin - dcenter;
                    Vector3 dbbmax  = (Vector3)(Vector4)drawable.BoundingBoxMax - dcenter;
                    Vector3 center  = entity.Position + dcenter;

                    Vector3 bbmin = entity.Position + dbbmin;
                    Vector3 bbmax = entity.Position + dbbmax;

                    Vector3 c1 = Utils.RotateTransform(orientation, bbmin, entity.Position);
                    Vector3 c2 = Utils.RotateTransform(orientation, bbmax, entity.Position);

                    bbmin = Vector3.Min(c1, c2);
                    bbmax = Vector3.Max(c1, c2);

                    if (
                        center.X >= emin.X && center.Y >= emin.Y && center.Z >= emin.Z &&
                        center.X <= emax.X && center.Y <= emax.Y && center.Z <= emax.Z
                        )
                    {
                        return(mlo.Rooms[i]);
                    }
                }
            }

            return(null);
        }
Beispiel #15
0
        public bool RemoveEntity(YmapEntityDef ent)
        {
            if (ent == null)
            {
                return(false);
            }

            if ((ent.MloEntitySet?.Entities != null) && (ent.MloEntitySet?.EntitySet != null))
            {
                var instents = ent.MloEntitySet.Entities;
                var set      = ent.MloEntitySet.EntitySet;
                var idx      = instents.IndexOf(ent);
                if (idx >= 0)
                {
                    var ents = set.Entities.ToList();
                    ents.RemoveAt(idx);
                    set.Entities = ents.ToArray();

                    var locs = set.Locations.ToList();
                    locs.RemoveAt(idx);
                    set.Locations = locs.ToArray();

                    UpdateAllEntityIndexes();
                    return(true);
                }
                return(false);
            }

            if (ent.Index >= entities.Length)
            {
                return(false);
            }

            var delent = entities[ent.Index];

            if (delent != null)
            {
                var newentities = new MCEntityDef[entities.Length - 1];
                var didDel      = false;
                int index       = 0;
                int delIndex    = 0;
                for (int i = 0; i < entities.Length; i++)
                {
                    if (entities[i] == delent)
                    {
                        delIndex = i;
                        didDel   = true;
                        continue;
                    }

                    var newent = entities[i];
                    newentities[index] = newent;
                    index++;
                }

                entities = newentities;

                if (didDel)
                {
                    FixRoomIndexes(delIndex);
                    FixPortalIndexes(delIndex);
                    UpdateAllEntityIndexes();
                }
                return(didDel);
            }

            return(false);
        }
Beispiel #16
0
        public bool AddEntity(YmapEntityDef ent, int roomIndex, int portalIndex = -1, int entsetIndex = -1)
        {
            if (ent == null)
            {
                return(false);
            }

            if (roomIndex >= (rooms?.Length ?? 0))
            {
                throw new ArgumentOutOfRangeException($"Room index {roomIndex} exceeds the amount of rooms in {Name}.");
            }
            if (portalIndex >= (portals?.Length ?? 0))
            {
                throw new ArgumentOutOfRangeException($"Portal index {portalIndex} exceeds the amount of portals in {Name}.");
            }
            if (entsetIndex >= (entitySets?.Length ?? 0))
            {
                throw new ArgumentOutOfRangeException($"EntitySet index {entsetIndex} exceeds the amount of entitySets in {Name}.");
            }

            var mcEntityDef = new MCEntityDef(ref ent._CEntityDef, this);


            if ((roomIndex >= 0) || (portalIndex >= 0))
            {
                var entList = entities?.ToList() ?? new List <MCEntityDef>();
                ent.Index = entList.Count;
                entList.Add(mcEntityDef);
                entities = entList.ToArray();

                if (roomIndex >= 0)
                {
                    var attachedObjs = rooms[roomIndex].AttachedObjects?.ToList() ?? new List <uint>();
                    attachedObjs.Add((uint)ent.Index);
                    rooms[roomIndex].AttachedObjects = attachedObjs.ToArray();
                }
                if (portalIndex >= 0)
                {
                    var attachedObjs = portals[portalIndex].AttachedObjects?.ToList() ?? new List <uint>();
                    attachedObjs.Add((uint)ent.Index);
                    portals[portalIndex].AttachedObjects = attachedObjs.ToArray();
                }
            }
            else if (entsetIndex >= 0)
            {
                var entset = entitySets[entsetIndex];

                var entList = entset.Entities?.ToList() ?? new List <MCEntityDef>();
                entList.Add(mcEntityDef);
                entset.Entities = entList.ToArray();

                var locs = entset.Locations?.ToList() ?? new List <uint>();
                locs.Add(0);//choose a better default location?
                entset.Locations = locs.ToArray();


                var mloInstance = ent.MloParent?.MloInstance;
                if ((mloInstance?.EntitySets != null) && (entsetIndex < mloInstance.EntitySets.Length))
                {
                    ent.MloEntitySet = mloInstance.EntitySets[entsetIndex];
                }
            }
            else
            {
                return(false);
            }

            UpdateAllEntityIndexes();

            return(true);
        }