// Fourreau et sac de bombe //

var aLimit = 0, aCount = 0;
var bLimit = 0, bCount = 0;
var qID = new Array(290,291,292,293);
var qSize = new Array(20,30,40,50);
var bbID = new Array(270,274,257,278);
var bbSize = new Array(20,40,60,80);
var iArrow = 262, iBomb = 466;

if (pinv.getSlotContainingItem(qID[3]) != -1) { aLimit = qSize[3]; }
else if (pinv.getSlotContainingItem(qID[2])  != -1) { aLimit = qSize[2]; }
else if (pinv.getSlotContainingItem(qID[1])  != -1) { aLimit = qSize[1]; }
else if (pinv.getSlotContainingItem(qID[0])  != -1) { aLimit = qSize[0]; }
if (aLimit>0) { hasQuiver = true; }

if (pinv.getSlotContainingItem(bbID[3]) != -1) { bLimit = bbSize[3]; }
else if (pinv.getSlotContainingItem(bbID[2])  != -1) { bLimit = bbSize[2]; }
else if (pinv.getSlotContainingItem(bbID[1])  != -1) { bLimit = bbSize[1]; }
else if (pinv.getSlotContainingItem(bbID[0])  != -1) { bLimit = bbSize[0]; }
if (bLimit>0) { hasBombBag = true; }

for(i=0;i<=35;i++)
{
	if(pinv.getItemInSlot(i) != null)
	{
		var curItem = pinv.getItemInSlot(i);
		if (curItem.getItemID() == iArrow) {
			if (curItem.getQuantity() > 0) {
				aCount += curItem.getQuantity();
			}
		}
		if (curItem.getItemID() == iBomb) {
			if (curItem.getQuantity() > 0) {
				bCount += curItem.getQuantity();
			}
		}
	}
}

if (aCount >= aLimit) {
	pinv.consumeItemAmount(iArrow, 0, (aCount - aLimit));
}
if (bCount >= bLimit) {
	pinv.consumeItemAmount(iBomb, 0, (bCount - bLimit));
}

//*******************************//
//  Start of Code for            //
//  Item Management v2.1         //
//                               //
//*******************************//

  //////////
 // Init //
//////////
screen.clear();

  //////////
 // Vars //
//////////
h = ui.getHeight();

  //////////
 // Code //
//////////
try
{
	if (ui.getHeight() != im_sh) {
		var ne = ui.getHeight();
		screen.clear();
		ne = im_sh;
	}

	var rIndx = 0;
	// We have no currency. Show zero and call it quits.
	for (var indx in im_db) {
		if (im_pinv.getSlotContainingItem(im_db[indx][0]) == -1)
		{
			if (im_db[indx][5] == true)  {
				renderCurrency(0,indx,rIndx);
				rIndx++;
			}
		} 
		else 
		{
			try 
			{
				// Initialize our item counter at 0
				iCount = 0; iMax = 0;
			
				// Array of positions
				iPos = new Array();
		
				// New version
				for (ii = 0; ii <= 39; ii++) {
					iItem = im_pinv.getItemInSlot(ii);
			
					if (iItem != null) if (iItem.getItemID() == im_db[indx][0]) {
						if (iItem.getQuantity() <= 0) {
							im_pinv.emptySlot(ii);
						} else {
							if (im_db[indx][2] == true) {
								iCount += iItem.getMaxDamage()-iItem.getDamage();
								iMax += iItem.getMaxDamage();
							} else {
								iCount += iItem.getQuantity();
							}
						}
					}
				}

				//If forced to stack, stack to which of the 4 slots?
				//if (im_db[indx][2] != false) {
				//	im_pinv.setSlot(35+im_db[indx][2],new Item(im_db[indx][0],iCount,5));
				//}

				// Ensure top limit is not exceeded.	
				if (iCount > im_db[indx][1]) {
					iDif = iCount - im_db[indx][1];
					iCount = im_db[indx][1];
					for (var jj = iDif; jj > 0; jj--) {
						dItem(im_db[indx][0]);
                                        }
				}

				// Do render
				if (im_db[indx][2] == true) {renderCurrency(iCount+"/"+iMax,indx,rIndx);}
				else {renderCurrency(iCount,indx,rIndx);}
				rIndx++;
			}
			catch(e) { chat.print(e) }
		}
	}
	
}
catch(e) { chat.print(e) }


  ///////////////
 // Functions //
///////////////
function renderCurrency(q,i,r) {
	UISprite(im_db[i][3],5,h-20-(17*r),15,15,im_db[i][4][0],im_db[i][4][1])
	lText = UILabel(q,23,h-15-(17*r))
	if (q >= im_db[i][1]) {
		lText.red = lText .blue = 0;
	}
}

function dItem(id) {
	im_pinv.decrementItem(im_pinv.getSlotContainingItem(id),1);
}

//*******************************//
//  End of Code for              //
//  Item Management v2.1         //
//                               //
//*******************************//

// Enlever drops des monstres //

if (pinv.getSlotContainingItem(287) != -1)
{
pinv.consumeItem(287)
}

if (pinv.getSlotContainingItem(352) != -1)
{
pinv.consumeItem(352)
}

if (pinv.getSlotContainingItem(321) != -1)
{
pinv.consumeItem(321)
}

//convertir piece de cuivre//

if(pinv.getSlotContainingItem(336) >=0){
	if(pinv.getSlotContainingItem(336)>=0){
		if(pinv.getItemInSlot(pinv.getSlotContainingItem(336)).getQuantity()>=40){
		pinv.consumeItemAmount(336, 0, 40);
		pinv.addItem(Item(265,1));
                sound.playSoundUI("sound.piece");
		}
	}
	}
	
//start Interaction GUI
if (interactGUI == true) {
        if (keyboard.isKeyDown(79) == true && interactKeySwitch == 0) { //NUMPAD1
        interactKeySwitch = 1
        interactChoice = 1
        script.runScript(interactScript)
        }
        if (keyboard.isKeyDown(80) == true && interactKeySwitch == 0) { //NUMPAD2
        interactKeySwitch = 2
        interactChoice = 2
        script.runScript(interactScript)
        }
        if (keyboard.isKeyDown(81) == true && interactKeySwitch == 0) { //NUMPAD3
        interactKeySwitch = 3
        interactChoice = 3
        script.runScript(interactScript)
        }
        if ((keyboard.isKeyDown(81) != true && interactKeySwitch == 3) || (keyboard.isKeyDown(80) != true && interactKeySwitch == 2) || (keyboard.isKeyDown(79) != true && interactKeySwitch == 1)) {
                interactKeySwitch = 0
        }
}