//------------------------------------------------------------------------------//
bool InventoryReceiver::itemWillFitAtLocation(const InventoryItem& item,
                                              int x, int y)
{
    if (x < 0 || y < 0)
        return false;

    if (x + item.contentWidth() > d_content.width() ||
        y + item.contentHeight() > d_content.height())
            return false;

    const bool already_attached = this == item.getParent();
    // if item is already attatched erase its data from the content map so the
    // test result is reliable.
    if (already_attached)
        eraseItemFromContentMap(item);

    bool result = true;
    for (int item_y = 0; item_y < item.contentHeight() && result; ++item_y)
    {
        for (int item_x = 0; item_x < item.contentWidth() && result; ++item_x)
        {
            if (d_content.elementAtLocation(item_x + x, item_y + y) &&
                item.isSolidAtLocation(item_x, item_y))
                    result = false;
        }
    }

    // re-write item into content map if we erased it earlier.
    if (already_attached)
        writeItemToContentMap(item);

    return result;
}
//------------------------------------------------------------------------------//
void InventoryReceiver::writeItemToContentMap(const InventoryItem& item)
{
    if (item.locationOnReceiverX() == -1 || item.locationOnReceiverY() == -1)
        return;

    for (int y = 0; y < item.contentHeight(); ++y)
    {
        const int map_y = item.locationOnReceiverY() + y;

        for (int x = 0; x < item.contentWidth(); ++x)
        {
            const int map_x = item.locationOnReceiverX() + x;

            bool val = d_content.elementAtLocation(map_x, map_y) |
                       item.isSolidAtLocation(x, y);
            d_content.setElementAtLocation(map_x, map_y, val);
        }
    }

    invalidate();
}