コード例 #1
0
    /// <summary>
    /// Tries to insert an item to the beginning of the belt
    /// Automatically merges slots if the next slot matches with the item
    /// </summary>
    /// <returns>Returns true only if there is empty slot at the start of the belt</returns>
    public bool TryInsertItemToBelt(Item itemToInsert)
    {
        if (itemToInsert.isEmpty())
        {
            return(true);
        }

        if (items[0].item.isEmpty())
        {
            if (items[0].count == 1)
            {
                if (items.Count > 1 && items[1].item == itemToInsert)
                {
                    items[1] = new BeltSegment()
                    {
                        item = itemToInsert, count = items[1].count + 1
                    };
                    items.RemoveAt(0);
                }
                else
                {
                    items[0] = new BeltSegment()
                    {
                        item = itemToInsert, count = 1
                    };
                }
            }
            else
            {
                items[0] = new BeltSegment()
                {
                    item = items[0].item, count = items[0].count - 1
                };
                items.Insert(0, new BeltSegment()
                {
                    item = itemToInsert, count = 1
                });
            }

            return(true);
        }
        else
        {
            return(false);
        }
    }
コード例 #2
0
    /// <summary>
    /// Removes and returns the last item in the belt, whether actual item or empty
    /// automatically replaces it with empty and does reshaping as needed
    /// </summary>
    /// <returns>Returns an item (can be empty item)</returns>
    public bool TryRemoveLastItemFromBelt(out Item item)
    {
        var lastIndex = items.Count - 1;

        item = items[lastIndex].item;

        // If the last item is not empty
        if (!item.isEmpty())
        {
            int count = items[lastIndex].count;
            count -= 1; // reduce its count by 1

            // If the entire section gets to zero
            if (count == 0)
            {
                // If the belt has only one section, it is an edge case
                // In this case we just replace the current slot with an empty slot
                if (lastIndex == 0)
                {
                    items[lastIndex] = new BeltSegment()
                    {
                        count = 1, item = new Item().SetEmpty()
                    };
                }
                else
                {
                    // Remove the last section
                    items.RemoveAt(lastIndex);
                    lastIndex -= 1;

                    // If the previous section was also empty, add to it
                    if (items[lastIndex].item.isEmpty())
                    {
                        items[lastIndex] = new BeltSegment()
                        {
                            count = items[lastIndex].count + 1, item = items[lastIndex].item
                        };
                    }
                    else
                    {
                        // If the previous section was not empty, then add an empty section at the end
                        items.Add(new BeltSegment()
                        {
                            count = 1, item = new Item().SetEmpty()
                        });
                    }
                }
            }
            else
            {
                //if the entire section does not get to zero, reduce its count and add an empty section at the end
                items[lastIndex] = new BeltSegment()
                {
                    count = count, item = item
                };
                items.Add(new BeltSegment()
                {
                    count = 1, item = new Item().SetEmpty()
                });
            }

            return(true);
        }
        else
        {
            return(false);
        }
    }