Exemple #1
0
/// Moves items from the specified ItemGrid into this hopper. Returns true if contents have changed.
bool cHopperEntity::MoveItemsFromGrid(cItemGrid & a_Grid)
{
	int NumSlots = a_Grid.GetNumSlots();
	
	// First try adding items of types already in the hopper:
	for (int i = 0; i < NumSlots; i++)
	{
		if (a_Grid.IsSlotEmpty(i))
		{
			continue;
		}
		if (MoveItemsFromSlot(a_Grid.GetSlot(i), false))
		{
			a_Grid.ChangeSlotCount(i, -1);
			return true;
		}
	}

	// No already existing stack can be topped up, try again with allowing new stacks:
	for (int i = 0; i < NumSlots; i++)
	{
		if (a_Grid.IsSlotEmpty(i))
		{
			continue;
		}
		if (MoveItemsFromSlot(a_Grid.GetSlot(i), true))
		{
			a_Grid.ChangeSlotCount(i, -1);
			return true;
		}
	}
	return false;
}
Exemple #2
0
/// Moves one piece to the specified ItemGrid's slot. Returns true if contents have changed.
bool cHopperEntity::MoveItemsToSlot(cItemGrid & a_ItemGrid, int a_DestSlotNum)
{
	if (a_ItemGrid.IsSlotEmpty(a_DestSlotNum))
	{
		// The slot is empty, move the first non-empty slot from our contents:
		for (int i = 0; i < ContentsWidth * ContentsHeight; i++)
		{
			if (!m_Contents.IsSlotEmpty(i))
			{
				a_ItemGrid.SetSlot(a_DestSlotNum, m_Contents.GetSlot(i).CopyOne());
				m_Contents.ChangeSlotCount(i, -1);
				return true;
			}
		}
		return false;
	}
	else
	{
		// The slot is taken, try to top it up:
		const cItem & DestSlot = a_ItemGrid.GetSlot(a_DestSlotNum);
		if (DestSlot.IsFullStack())
		{
			return false;
		}
		for (int i = 0; i < ContentsWidth * ContentsHeight; i++)
		{
			if (m_Contents.GetSlot(i).IsStackableWith(DestSlot))
			{
				a_ItemGrid.ChangeSlotCount(a_DestSlotNum, 1);
				m_Contents.ChangeSlotCount(i, -1);
				return true;
			}
		}
		return false;
	}
}