Exemplo n.º 1
0
        /// <summary>
        /// Creates a new <see cref="DataReader" /> with the desired QoS policies and attaches to it the specified <see cref="DataReaderListener" />.
        /// </summary>
        /// <remarks>
        /// <para>The returned <see cref="DataReader" /> will be attached and belong to the <see cref="Subscriber" />.</para>
        /// <para>The <see cref="ITopicDescription"/> passed to this operation must have been created from the same <see cref="DomainParticipant" /> that was used to
        /// create this <see cref="Subscriber" />. If the <see cref="ITopicDescription"/> was created from a different <see cref="DomainParticipant" />, the operation will fail and
        /// return a <see langword="null"/> result.</para>
        /// </remarks>
        /// <param name="topicDescription">The <see cref="ITopicDescription" /> that the <see cref="DataReader" /> will be associated with.</param>
        /// <param name="qos">The <see cref="DataReaderQos" /> policies to be used for creating the new <see cref="DataReader" />.</param>
        /// <param name="listener">The <see cref="DataReaderListener" /> to be attached to the newly created <see cref="DataReader" />.</param>
        /// <param name="statusMask">The <see cref="StatusMask" /> of which status changes the listener should be notified.</param>
        /// <returns>The newly created <see cref="DataReader" /> on success, otherwise <see langword="null"/>.</returns>
        public DataReader CreateDataReader(ITopicDescription topicDescription, DataReaderQos qos, DataReaderListener listener, StatusMask statusMask)
        {
            if (topicDescription is null)
            {
                throw new ArgumentNullException(nameof(topicDescription));
            }

            DataReaderQosWrapper qosWrapper = default;

            if (qos is null)
            {
                qos = new DataReaderQos();
                var ret = GetDefaultDataReaderQos(qos);
                if (ret == ReturnCode.Ok)
                {
                    qosWrapper = qos.ToNative();
                }
            }
            else
            {
                qosWrapper = qos.ToNative();
            }

            IntPtr nativeListener = IntPtr.Zero;

            if (listener != null)
            {
                nativeListener = listener.ToNative();
            }

            IntPtr td     = topicDescription.ToNativeTopicDescription();
            IntPtr native = UnsafeNativeMethods.CreateDataReader(_native, td, qosWrapper, nativeListener, statusMask);

            qos.Release();

            if (native.Equals(IntPtr.Zero))
            {
                return(null);
            }

            var dr = new DataReader(native)
            {
                Listener = listener,
            };

            EntityManager.Instance.Add((dr as Entity).ToNative(), dr);
            ContainedEntities.Add(dr);

            return(dr);
        }
Exemplo n.º 2
0
        /// <summary>
        /// This operation deletes all the entities that were created by means of the "create" operations on the <see cref="Publisher" />. That is, it deletes
        /// all contained <see cref="DataWriter" /> objects.
        /// </summary>
        /// <remarks>
        /// <para>The operation will return <see cref="ReturnCode.PreconditionNotMet" /> if the any of the contained entities is in a state where it cannot be deleted.</para>
        /// <para>Once DeleteContainedEntities returns successfully, the application may delete the <see cref="Publisher" /> knowing that it has no
        /// contained <see cref="DataWriter" /> objects.</para>
        /// </remarks>
        /// <returns>The <see cref="ReturnCode" /> that indicates the operation result.</returns>
        public ReturnCode DeleteContainedEntities()
        {
            ReturnCode ret = UnsafeNativeMethods.DeleteContainedEntities(_native);

            if (ret == ReturnCode.Ok)
            {
                foreach (Entity e in ContainedEntities)
                {
                    EntityManager.Instance.Remove(e.ToNative());
                }

                ContainedEntities.Clear();
            }

            return(ret);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Deletes a <see cref="DataReader" /> that belongs to the <see cref="Subscriber" />.
        /// </summary>
        /// <remarks>
        /// <para>If the <see cref="DataReader" /> does not belong to the <see cref="Subscriber" />, the operation returns the error <see cref="ReturnCode.PreconditionNotMet" />.</para>
        /// <para>The deletion of a <see cref="DataReader" /> is not allowed if there are any existing <see cref="ReadCondition" /> or <see cref="QueryCondition" /> objects that are
        /// attached to the <see cref="DataReader" />. If the DeleteDataReader operation is called on a <see cref="DataReader" /> with any of these existing objects
        /// attached to it, it will return <see cref="ReturnCode.PreconditionNotMet" />.</para>
        /// <para>The DeleteDataReader operation must be called on the same <see cref="Subscriber" /> object used to create the <see cref="DataReader" />. If
        /// DeleteDataReader is called on a different <see cref="Subscriber" />, the operation will have no effect and it will return
        /// <see cref="ReturnCode.PreconditionNotMet" />.</para>
        /// </remarks>
        /// <param name="dataReader">The <see cref="DataReader" /> to be deleted.</param>
        /// <returns>The <see cref="ReturnCode" /> that indicates the operation result.</returns>
        public ReturnCode DeleteDataReader(DataReader dataReader)
        {
            if (dataReader == null)
            {
                return(ReturnCode.Ok);
            }

            ReturnCode ret = UnsafeNativeMethods.DeleteDataReader(_native, dataReader.ToNative());

            if (ret == ReturnCode.Ok)
            {
                EntityManager.Instance.Remove((dataReader as Entity).ToNative());
                ContainedEntities.Remove(dataReader);
            }

            return(ret);
        }
        public override void HandleComponentState(dynamic state)
        {
            var theState     = state as InventoryComponentState;
            var stateChanged = false;

            if (MaxSlots != theState.MaxSlots)
            {
                MaxSlots     = theState.MaxSlots;
                stateChanged = true;
            }

            var newEntities = new List <int>(theState.ContainedEntities);
            var toRemove    = new List <IEntity>();

            foreach (var e in ContainedEntities)
            {
                if (newEntities.Contains(e.Uid))
                {
                    newEntities.Remove(e.Uid);
                }
                else
                {
                    toRemove.Add(e);
                }
            }
            stateChanged = stateChanged || toRemove.Any() || newEntities.Any();
            foreach (var e in toRemove)
            {
                ContainedEntities.Remove(e);
            }

            foreach (var uid in newEntities)
            {
                ContainedEntities.Add(Owner.EntityManager.GetEntity(uid));
            }

            if (stateChanged && Changed != null)
            {
                Changed(this, MaxSlots, ContainedEntities);
            }
        }
Exemplo n.º 5
0
        private void UnpackListing(IncomingEntityComponentMessage msg)
        {
            MaxSlots = (int)msg.MessageParameters[1];

            ContainedEntities.Clear();

            for (int i = 0; i < (int)msg.MessageParameters[2]; i++)
            {
                int    msgPos = 3 + i;
                Entity entity = Owner.EntityManager.GetEntity((int)msg.MessageParameters[msgPos]);
                if (entity != null)
                {
                    ContainedEntities.Add(entity);
                }
            }

            if (Changed != null)
            {
                Changed(this, MaxSlots, ContainedEntities);
            }
        }
Exemplo n.º 6
0
        private async Task <IEntity?> Construct(IEntity user, string materialContainer, ConstructionGraphPrototype graph, ConstructionGraphEdge edge, ConstructionGraphNode targetNode)
        {
            // We need a place to hold our construction items!
            var container = ContainerManagerComponent.Ensure <Container>(materialContainer, user, out var existed);

            if (existed)
            {
                user.PopupMessageCursor(Loc.GetString("You can't start another construction now!"));
                return(null);
            }

            var containers = new Dictionary <string, Container>();

            var doAfterTime = 0f;

            // HOLY SHIT THIS IS SOME HACKY CODE.
            // But I'd rather do this shit than risk having collisions with other containers.
            Container GetContainer(string name)
            {
                if (containers !.ContainsKey(name))
                {
                    return(containers[name]);
                }

                while (true)
                {
                    var random = _robustRandom.Next();
                    var c      = ContainerManagerComponent.Ensure <Container>(random.ToString(), user !, out var existed);

                    if (existed)
                    {
                        continue;
                    }

                    containers[name] = c;
                    return(c);
                }
            }

            void FailCleanup()
            {
                foreach (var entity in container !.ContainedEntities.ToArray())
                {
                    container.Remove(entity);
                }

                foreach (var cont in containers !.Values)
                {
                    foreach (var entity in cont.ContainedEntities.ToArray())
                    {
                        cont.Remove(entity);
                    }
                }

                // If we don't do this, items are invisible for some f*****g reason. Nice.
                Timer.Spawn(1, ShutdownContainers);
            }

            void ShutdownContainers()
            {
                container !.Shutdown();
                foreach (var c in containers !.Values.ToArray())
                {
                    c.Shutdown();
                }
            }

            var failed = false;

            var steps = new List <ConstructionGraphStep>();

            foreach (var step in edge.Steps)
            {
                doAfterTime += step.DoAfter;

                var handled = false;

                switch (step)
                {
                case MaterialConstructionGraphStep materialStep:
                    foreach (var entity in EnumerateNearby(user))
                    {
                        if (!materialStep.EntityValid(entity, out var sharedStack))
                        {
                            continue;
                        }

                        var stack = (StackComponent)sharedStack;

                        if (!stack.Split(materialStep.Amount, user.ToCoordinates(), out var newStack))
                        {
                            continue;
                        }

                        if (string.IsNullOrEmpty(materialStep.Store))
                        {
                            if (!container.Insert(newStack))
                            {
                                continue;
                            }
                        }
                        else if (!GetContainer(materialStep.Store).Insert(newStack))
                        {
                            continue;
                        }

                        handled = true;
                        break;
                    }

                    break;

                case ComponentConstructionGraphStep componentStep:
                    foreach (var entity in EnumerateNearby(user))
                    {
                        if (!componentStep.EntityValid(entity))
                        {
                            continue;
                        }

                        if (string.IsNullOrEmpty(componentStep.Store))
                        {
                            if (!container.Insert(entity))
                            {
                                continue;
                            }
                        }
                        else if (!GetContainer(componentStep.Store).Insert(entity))
                        {
                            continue;
                        }

                        handled = true;
                        break;
                    }

                    break;

                case PrototypeConstructionGraphStep prototypeStep:
                    foreach (var entity in EnumerateNearby(user))
                    {
                        if (!prototypeStep.EntityValid(entity))
                        {
                            continue;
                        }

                        if (string.IsNullOrEmpty(prototypeStep.Store))
                        {
                            if (!container.Insert(entity))
                            {
                                continue;
                            }
                        }
                        else if (!GetContainer(prototypeStep.Store).Insert(entity))
                        {
                            continue;
                        }

                        handled = true;
                        break;
                    }

                    break;
                }

                if (handled == false)
                {
                    failed = true;
                    break;
                }

                steps.Add(step);
            }

            if (failed)
            {
                user.PopupMessageCursor(Loc.GetString("You don't have the materials to build that!"));
                FailCleanup();
                return(null);
            }

            var doAfterSystem = Get <DoAfterSystem>();

            var doAfterArgs = new DoAfterEventArgs(user, doAfterTime)
            {
                BreakOnDamage     = true,
                BreakOnStun       = true,
                BreakOnTargetMove = false,
                BreakOnUserMove   = true,
                NeedHand          = true,
            };

            if (await doAfterSystem.DoAfter(doAfterArgs) == DoAfterStatus.Cancelled)
            {
                FailCleanup();
                return(null);
            }

            var newEntity = EntityManager.SpawnEntity(graph.Nodes[edge.Target].Entity, user.Transform.Coordinates);

            // Yes, this should throw if it's missing the component.
            var construction = newEntity.GetComponent <ConstructionComponent>();

            // We attempt to set the pathfinding target.
            construction.Target = targetNode;

            // We preserve the containers...
            foreach (var(name, cont) in containers)
            {
                var newCont = ContainerManagerComponent.Ensure <Container>(name, newEntity);

                foreach (var entity in cont.ContainedEntities.ToArray())
                {
                    cont.ForceRemove(entity);
                    newCont.Insert(entity);
                }
            }

            // We now get rid of all them.
            ShutdownContainers();

            // We have step completed steps!
            foreach (var step in steps)
            {
                foreach (var completed in step.Completed)
                {
                    await completed.PerformAction(newEntity, user);
                }
            }

            // And we also have edge completed effects!
            foreach (var completed in edge.Completed)
            {
                await completed.PerformAction(newEntity, user);
            }

            return(newEntity);
        }
 public IEntity GetEntity(string templatename)
 {
     return(ContainedEntities.FirstOrDefault(x => x.Prototype.ID == templatename));
 }
 public bool ContainsEntity(string templatename)
 {
     return(ContainedEntities.Exists(x => x.Prototype.ID == templatename));
 }
 public bool ContainsEntity(IEntity entity)
 {
     return(ContainedEntities.Contains(entity));
 }
Exemplo n.º 10
0
 public Entity GetEntity(string templatename)
 {
     return(ContainedEntities.Exists(x => x.Template.Name == templatename)
                ? ContainedEntities.First(x => x.Template.Name == templatename)
                : null);
 }
Exemplo n.º 11
0
 public bool ContainsEntity(string templatename)
 {
     return(ContainedEntities.Exists(x => x.Template.Name == templatename));
 }
Exemplo n.º 12
0
 public VersaDescription GetDefaultDescription() =>
 ContainedEntities.Any()
         ? Description + "\n" + ContainedEntities.Select(c => c.Description).Aggregate(new StringBuilder(),
                                                                                       (stringBuilder, entityDescription) =>
                                                                                       stringBuilder.AppendFormat("A creature: {0}", entityDescription).AppendLine()).ToString()
         : Description;