private void AddCargoContents(CargoShuttleComponent component, StationCargoOrderDatabaseComponent orderDatabase)
    {
        var xformQuery = GetEntityQuery <TransformComponent>();
        var orders     = GetProjectedOrders(orderDatabase, component);

        var pads = GetCargoPallets(component);

        DebugTools.Assert(orders.Sum(o => o.Amount) <= pads.Count);

        for (var i = 0; i < orders.Count; i++)
        {
            var order = orders[i];

            Spawn(_protoMan.Index <CargoProductPrototype>(order.ProductId).Product,
                  new EntityCoordinates(component.Owner, xformQuery.GetComponent(_random.PickAndTake(pads).Owner).LocalPosition));
            order.Amount--;

            if (order.Amount == 0)
            {
                orders.RemoveSwap(i);
                orderDatabase.Orders.Remove(order.OrderNumber);
                i--;
            }
            else
            {
                orderDatabase.Orders[order.OrderNumber] = order;
            }
        }
    }
示例#2
0
        private int GetNextIndex(StationCargoOrderDatabaseComponent component)
        {
            var index = component.Index;

            component.Index++;
            return(index);
        }
    private void AddShuttle(StationCargoOrderDatabaseComponent component)
    {
        Setup();

        if (CargoMap == null || component.Shuttle != null)
        {
            return;
        }

        if (component.CargoShuttleProto != null)
        {
            var prototype     = _protoMan.Index <CargoShuttlePrototype>(component.CargoShuttleProto);
            var possibleNames = _protoMan.Index <DatasetPrototype>(prototype.NameDataset).Values;
            var name          = _random.Pick(possibleNames);

            var(_, gridId) = _loader.LoadBlueprint(CargoMap.Value, prototype.Path.ToString());
            var shuttleUid = _mapManager.GetGridEuid(gridId !.Value);
            var xform      = Transform(shuttleUid);
            MetaData(shuttleUid).EntityName = name;

            // TODO: Something better like a bounds check.
            xform.LocalPosition += 100 * _index;
            var comp = EnsureComp <CargoShuttleComponent>(shuttleUid);
            comp.Station     = component.Owner;
            comp.Coordinates = xform.Coordinates;

            component.Shuttle = shuttleUid;
            comp.NextCall     = _timing.CurTime + TimeSpan.FromSeconds(comp.Cooldown);
            UpdateShuttleCargoConsoles(comp);
            _index++;
            _sawmill.Info($"Added cargo shuttle to {ToPrettyString(shuttleUid)}");
        }
    }
示例#4
0
        /// <summary>
        /// Updates all of the cargo-related consoles for a particular station.
        /// This should be called whenever orders change.
        /// </summary>
        private void UpdateOrders(StationCargoOrderDatabaseComponent component)
        {
            // Order added so all consoles need updating.
            foreach (var comp in EntityQuery <CargoOrderConsoleComponent>(true))
            {
                var station = _station.GetOwningStation(component.Owner);
                if (station != component.Owner)
                {
                    continue;
                }

                UpdateOrderState(comp, station);
            }

            foreach (var comp in EntityQuery <CargoShuttleConsoleComponent>(true))
            {
                var station = _station.GetOwningStation(component.Owner);
                if (station != component.Owner)
                {
                    continue;
                }

                UpdateShuttleState(comp, station);
            }
        }
示例#5
0
 public void RemoveOrder(StationCargoOrderDatabaseComponent component, int index)
 {
     if (!component.Orders.Remove(index))
     {
         return;
     }
     UpdateOrders(component);
 }
示例#6
0
        public void ClearOrders(StationCargoOrderDatabaseComponent component)
        {
            if (component.Orders.Count == 0)
            {
                return;
            }

            component.Orders.Clear();
            Dirty(component);
        }
    /// <summary>
    /// Retrieves a shuttle for delivery.
    /// </summary>
    public void CallShuttle(StationCargoOrderDatabaseComponent orderDatabase)
    {
        if (!TryComp <CargoShuttleComponent>(orderDatabase.Shuttle, out var shuttle) ||
            !TryComp <TransformComponent>(orderDatabase.Owner, out var xform))
        {
            return;
        }

        // Already called / not available
        if (shuttle.NextCall == null || _timing.CurTime < shuttle.NextCall)
        {
            return;
        }

        shuttle.NextCall = null;

        // Find a valid free area nearby to spawn in on
        // TODO: Make this use hyperspace now.
        var  center    = new Vector2();
        var  minRadius = 0f;
        Box2?aabb      = null;

        foreach (var grid in _mapManager.GetAllMapGrids(xform.MapID))
        {
            aabb = aabb?.Union(grid.WorldAABB) ?? grid.WorldAABB;
        }

        if (aabb != null)
        {
            center    = aabb.Value.Center;
            minRadius = MathF.Max(aabb.Value.Width, aabb.Value.Height);
        }

        var offset = 0f;

        if (TryComp <IMapGridComponent>(orderDatabase.Shuttle, out var shuttleGrid))
        {
            var bounds = shuttleGrid.Grid.LocalAABB;
            offset = MathF.Max(bounds.Width, bounds.Height) / 2f;
        }

        Transform(shuttle.Owner).Coordinates = new EntityCoordinates(xform.ParentUid,
                                                                     center + _random.NextVector2(minRadius + offset, minRadius + CallOffset + offset));
        DebugTools.Assert(!MetaData(shuttle.Owner).EntityPaused);

        AddCargoContents(shuttle, orderDatabase);
        UpdateOrders(orderDatabase);
        UpdateShuttleCargoConsoles(shuttle);
        _console.RefreshShuttleConsoles();

        _sawmill.Info($"Retrieved cargo shuttle {ToPrettyString(shuttle.Owner)} from {ToPrettyString(orderDatabase.Owner)}");
    }
示例#8
0
        private int GetOrderCount(StationCargoOrderDatabaseComponent component)
        {
            var amount = 0;

            foreach (var(_, order) in component.Orders)
            {
                if (!order.Approved)
                {
                    continue;
                }
                amount += order.Amount;
            }

            return(amount);
        }
 private void OnCargoOrderStartup(EntityUid uid, StationCargoOrderDatabaseComponent component, ComponentStartup args)
 {
     // Stations get created first but if any are added at runtime then do this.
     AddShuttle(component);
 }
示例#10
0
 public bool TryAddOrder(StationCargoOrderDatabaseComponent component, CargoOrderData data)
 {
     component.Orders.Add(data.OrderNumber, data);
     UpdateOrders(component);
     return(true);
 }