

// DayTime

time.set(5000);

//Fire and Ice

UpdateFireProjectiles();
UpdateIceProjectiles();
UpdateFrozenMobs();

//fast plass
var thisItem = pinv.getItemInSlot(30);


 //Variabler
 var qurub1 = 0; var qufle1 = 0;  var quman1 = 0; var peter1 = 0;
//Penger
for(i = 0; i < 40; i++) {
	var itemSlot = pinv.getItemInSlot(i);
	if (itemSlot != null && itemSlot.getItemID() == 266) {
		qurub1 = qurub1 + itemSlot.quantity;
}	}
if (typeof qurub2 == "undefined") {
	var qurub2 = qurub1;
}
if (qurub1 > qurub2) {
    sound.playSoundUI("sound.get rupee");
}
qurub2 = qurub1;

// Piler
for(i = 0; i < 40; i++) {
	var itemSlot = pinv.getItemInSlot(i);
	if (itemSlot != null && itemSlot.getItemID() == 262) {
		qufle1 = qufle1 + itemSlot.quantity;
}	}
if (typeof qufle2 == "undefined") {
	var qufle2 = qufle1;
}
if (qufle1 > qufle2) {
    sound.playSoundUI("sound.get item");
}
qufle2 = qufle1;

// Mana
for(i = 0; i < 40; i++) {
	var itemSlot = pinv.getItemInSlot(i);
	if (itemSlot != null && itemSlot.getItemID() == 289) {
		quman1 = quman1 + itemSlot.quantity;
}	}
if (typeof quman2 == "undefined") {
	var quman2 = quman1;
}
if (quman1 > quman2) {
    sound.playSoundUI("sound.magic");
}
quman2 = quman1;

// Nøkel
for(i = 0; i < 40; i++) {
	var itemSlot = pinv.getItemInSlot(i);
	if (itemSlot != null && itemSlot.getItemID() == 457) {
		peter1 = peter1 + itemSlot.quantity;
}	}
if (typeof peter2 == "undefined") {
	var peter2 = peter1;
}
if (peter1 > peter2) {
    sound.playSoundUI("sound.small item");
}
peter2 = peter1;



//Magi på riktig plass

if(pinv.getItemInSlot(17) != null)
{
        if(pinv.getItemInSlot(17).getItemID() != 289)
        {
                for(i=0;i<35;i++)
                {
                        if(pinv.getItemInSlot(i) == null)
                        {
                                try
								{
									var pitem = pinv.getItemInSlot(17).copy();
									pinv.emptySlot(17);
									pinv.setSlot(i, pitem);
								}
								catch(e) {}
                        }
                }
        }
}

for(i=0 ; i<35 ; i++)
{
	if(pinv.getItemInSlot(i) != null)
	{
		if(pinv.getItemInSlot(i).getItemID() == 289 && i !=17)
		{
			quant2 = 0;
			pinv = pinv;
			quant = pinv.getItemInSlot(i).getQuantity();
			try
			{
				quant2 = pinv.getItemInSlot(17).getQuantity();
			}
			catch(e) {}
			pinv.emptySlot(i);
			pinv.setSlot(17, Item(289, quant + quant2));
			
		}
	}
}

try
{
	if(pinv.getItemInSlot(17).getQuantity() > 30)
	{
		pinv.emptySlot(17);
		pinv.setSlot(17, Item(289, 30));
	}
}
catch(e) {}

//Holder Pengene på plass
if(pinv.getItemInSlot(35) != null)
{
        if(pinv.getItemInSlot(35).getItemID() != 266)
        {
                for(i=0;i<35;i++)
                {
                        if(pinv.getItemInSlot(i) == null)
                        {
                            try
							{
								var pitem = pinv.getItemInSlot(35).copy();
                                pinv.emptySlot(35);
                                pinv.setSlot(i, pitem);
							}
							catch(e) {}
                        }
                }
        }
}
if (pinv.getSlotContainingItem(266) != -1)
{
	var item = pinv.getItemInSlot(pinv.getSlotContainingItem(266)).copy();
    if (pinv.getSlotContainingItem(266) != 35)
    {
        try
        {
            var count = pinv.getItemInSlot(35).getQuantity();
        }
        catch(e) { var count = 0; }
        pinv.emptySlot(pinv.getSlotContainingItem(266));
        if (count < 500)
        {
            if (item.getQuantity() + count <= 500)
            {
                item.setQuantity(item.getQuantity() + count);
            }
            else
            {
                item.setQuantity(500);
            }
			
			if(pinv.getSlotContainingItem(339) == -1)
			{
				if(pinv.getSlotContainingItem(353) == -1)
				{
					if(item.getQuantity() >= 100)
					{
						item.setQuantity(999);
					}	
				}
				else
				{
					if(item.getQuantity() >= 201)
					{
						item.setQuantity(200);
					}
				}
			}
		}
	    else
        {
            item.setQuantity(500);
        }
        pinv.setSlot(35, item);
    }
	else
	{
		if(pinv.getSlotContainingItem(339) == -1)
		{
			if(pinv.getSlotContainingItem(353) == -1)
			{
				if(item.getQuantity() >= 100)
				{
					item.setQuantity(999);
				}	
			}
			else
			{
				if(item.getQuantity() >= 201)
				{
					item.setQuantity(200);
				}
			}
		}
		else if(pinv.getItemInSlot(35).getQuantity() > 500)
		{
			pinv.getItemInSlot(35).setQuantity(500);
		}
	}
			
}

//Holder Penge Bag  På riktig plass (Liten Bagg)

if(pinv.getItemInSlot(34) != null)
{
        if(pinv.getItemInSlot(34).getItemID() != 290)
        {
                for(i=0;i<35;i++)
                {
                        if(pinv.getItemInSlot(i) == null)
                        {
                                try
								{
									var pitem = pinv.getItemInSlot(34).copy();
									pinv.emptySlot(34);
									pinv.setSlot(i, pitem);
								}
								catch(e) {}
                        }
                }
        }
}

for(i=0 ; i<35 ; i++)
{
	if(pinv.getItemInSlot(i) != null)
	{
		if(pinv.getItemInSlot(i).getItemID() == 290 && i !=34)
		{
			quant2 = 0;
			pinv = pinv;
			quant = pinv.getItemInSlot(i).getQuantity();
			try
			{
				quant2 = pinv.getItemInSlot(34).getQuantity();
			}
			catch(e) {}
			pinv.emptySlot(i);
			pinv.setSlot(34, Item(290, quant + quant2));

		}
	}
}

try
{
	if(pinv.getItemInSlot(34).getQuantity() > 30)
	{
		pinv.emptySlot(34);
		pinv.setSlot(34, Item(290, 30));
	}
}
catch(e) {}

//PS. Husk å legg til de neste bagene!!! Viktig



// Arrows

if (pinv.getSlotContainingItem(262) != -1)
{
    if (pinv.getSlotContainingItem(262) != 26)
    {
        try
        {
            var count = pinv.getItemInSlot(26).getQuantity();
        }
        catch(e) { var count = 0; }
        var item = pinv.getItemInSlot(pinv.getSlotContainingItem(262)).copy();
        pinv.emptySlot(pinv.getSlotContainingItem(262));
        if (count < 500)
        {
            if (item.getQuantity() + count <= 500)
            {
                item.setQuantity(item.getQuantity() + count);
            }
            else
            {
                item.setQuantity(500);
            }
			
			if(pinv.getSlotContainingItem(318) == -1)
			{
				if(item.getQuantity() >= 201)
				{
					item.setQuantity(200);
				}
				
			}
		}
	    else
        {
            item.setQuantity(500);
        }
        pinv.setSlot(26, item);
    }
    pinv.getItemInSlot(26).setDamage(pinv.getItemInSlot(26).getQuantity());
}

if(pinv.getItemInSlot(26) != null)
{
        if(pinv.getItemInSlot(26).getItemID() != 262)
        {
                for(i=0;i<35;i++)
                {
                        if(pinv.getItemInSlot(i) == null)
                        {
                                try
								{
									var pitem = pinv.getItemInSlot(26).copy();
									pinv.emptySlot(26);
									pinv.setSlot(i, pitem);
								}
								catch(e) {}
                        }
                }
        }
}

for(i=0 ; i<35 ; i++)
{
	if(pinv.getItemInSlot(i) != null)
	{
		if(pinv.getItemInSlot(i).getItemID() == 262 && i !=26)
		{
			quant2 = 0;
			pinv = pinv;
			quant = pinv.getItemInSlot(i).getQuantity();
			try
			{
				quant2 = pinv.getItemInSlot(26).getQuantity();
			}
			catch(e) {}
			pinv.emptySlot(i);
			pinv.setSlot(26, Item(262, quant + quant2));
			
		}
	}
}






//MagiBar I Høyre topp gjørne

UISprite("gui/magie3.PNG",5,15,150,10,0,0);

try
{
	mag = pinv.getItemInSlot(17).getQuantity();
			
	if(pinv.getSlotContainingItem(289) != -1 )
	{
		UIRect(6,16,mag*2,6,0,0.7,0,0.9)
		UIRect(6+(mag*2),16,60-(mag*2),6,0,0,0,0.5)
	}
}
catch(e) {}
	
if(pinv.getSlotContainingItem(289) == -1)
{
	UIRect(6,16,60,6,0,0,0,0.5)
}


var arrowTargetId = 19

for each(var entity in player.getEntitiesWithinRange(40))
{
	if(entity.classType == "Arrow")
	{
		if(entity.isPlayersArrow && entity.inBlockID == arrowTargetId)
		{
			thisBlock3 = entity.inBlockCoords
			world.triggerBlock(thisBlock3.x, thisBlock3.y, thisBlock3.z+1) // North
			world.triggerBlock(thisBlock3.x, thisBlock3.y, thisBlock3.z-1) // South
			world.triggerBlock(thisBlock3.x+1, thisBlock3.y, thisBlock3.z) // East
			world.triggerBlock(thisBlock3.x-1, thisBlock3.y, thisBlock3.z) // West
			world.triggerBlock(thisBlock3.x, thisBlock3.y+1, thisBlock3.z) // Up
			world.triggerBlock(thisBlock3.x, thisBlock3.y-1, thisBlock3.z) // Down
		}
	}
}


 //Variabler
 var qurub1 = 0; var qufle1 = 0;  var quman1 = 0; var peter1 = 0;
//Penger
for(i = 0; i < 40; i++) {
	var itemSlot = pinv.getItemInSlot(i);
	if (itemSlot != null && itemSlot.getItemID() == 266) {
		qurub1 = qurub1 + itemSlot.quantity;
}	}
if (typeof qurub2 == "undefined") {
	var qurub2 = qurub1;
}
if (qurub1 > qurub2) {
    sound.playSoundUI("sound.get rupee");
}
qurub2 = qurub1;

//Bombe Bag

if(pinv.getItemInSlot(16) != null)
{
        if(pinv.getItemInSlot(16).getItemID() != 278)
        {
                for(i=0;i<35;i++)
                {
                        if(pinv.getItemInSlot(i) == null)
                        {
                                try
								{
									var pitem = pinv.getItemInSlot(16).copy();
									pinv.emptySlot(16);
									pinv.setSlot(i, pitem);
								}
								catch(e) {}
                        }
                }
        }
}

for(i=0 ; i<35 ; i++)
{
	if(pinv.getItemInSlot(i) != null)
	{
		if(pinv.getItemInSlot(i).getItemID() == 278 && i !=16)
		{
			quant2 = 0;
			pinv = pinv;
			quant = pinv.getItemInSlot(i).getQuantity();
			try
			{
				quant2 = pinv.getItemInSlot(16).getQuantity();
			}
			catch(e) {}
			pinv.emptySlot(i);
			pinv.setSlot(16, Item(278, quant + quant2));
			
		}
	}
}



//limiter
var aLimit = 0, aCount = 0;
var bLimit = 0, bCount = 0;
var qID = new Array(290,291,292,293);
var qSize = new Array(100,200,500,999);
var bbID = new Array(278,277,279,285);
var bbSize = new Array(20,40,60,80);
var iArrow = 266, 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));
}