
///Datablocks

datablock ItemData(Pack1)
{
   // Mission editor category
   category = "Pack";

   // Hook into Item Weapon class hierarchy. The weapon namespace
   // provides common weapon handling functions in addition to hooks
   // into the inventory system.
   className = "pack";

   // Basic Item properties
   shapeFile = "~/data/shapes/construction/crate.dts";
   mass = 1;
   elasticity = 0.2;
   friction = 0.6;
   emap = true;

   // Dynamic properties defined by the scripts
   pickUpName = "a pack";
   image = PackImage1;
};

datablock ShapeBaseImageData(PackImage1)
{   
   shapeFile = "~/data/shapes/construction/crate.dts";
   emap = true;
   item = Pack1;
   mountPoint = 1;
   offset = "0 -0.3 -0.2";  
};


//Custom functions

function Pack::onInventory(%data,%obj,%amount)
{ 
    //only do this for players
    if(%obj.getClassName() !$= "Player")
		return;
   // Auto-mount the packs on players
   if((%oldPack = %obj.getMountedImage(2)) != 0)
      %obj.setInventory(%oldPack.item, 0);   
   if (%amount && %obj.getDatablock().className $= Armor)
   {
		
      %obj.mountImage(%data.image,2);
	 
	}
   ItemData::onInventory(%data,%obj,%amount);
}    


function Pack::onUse(%data,%cl)
{
   if (%cl.player.getMountedImage(2) != %data.image.getId())
     return; 
}


function ItemData::Spawn(%data,%pos) {
	%item = new item() {
	datablock = %data;
	};
	%item.setTransform(%pos);
return %item;
}


function ShapeBaseImageData::ChangeMode(%data,%plyr,%val,%level)
{
if (%level == 0) //regular pack settings
   {
   %image = %data.getName();

   %settings = $packSettings[%image];
  
   %plyr.packSet = %plyr.packSet + %val;
   if (%plyr.packSet > getWord(%settings,0))
 	%plyr.packSet = 0;
   if (%plyr.packSet < 0)
	%plyr.packSet = getWord(%settings,0);
   %packname = GetWords(%settings,2,getWordCount(%settings));

   %curset = $PackSetting[%image,%plyr.packSet];
   if (getWord(%settings,1) == -1)
	   %line = GetWords(%curset,0,getWordCount(%curset));
   else
	   %line = GetWords(%curset,0,getWord(%settings,1));
  
   bottomPrint(%plyr.client,%packname SPC "set to"SPC %line,2,1);
   return true;
   }
else if (%level == 1) //expert settings
   {

   %image = %data.getName();

   %settings = $expertSettings[%image];
  
   %plyr.expertSet = %plyr.expertSet + %val;
   if (%plyr.expertSet > getWord(%settings,0))
 	%plyr.expertSet = 0;
   if (%plyr.expertSet < 0)
	%plyr.expertSet = getWord(%settings,0);

   %packname = GetWords(%settings,2,getWordCount(%settings));
   %curset = $expertSetting[%image,%plyr.expertSet];
   if (getWord(%settings,1) == -1)
	   %line = GetWords(%curset,0,getWordCount(%curset));
   else
	   %line = GetWords(%curset,0,getWord(%settings,1));

   bottomPrint(%plyr.client,%packname SPC "set to"SPC %line,2,1);
   return true;
   }
else if (%level == 2) //Weapon settings 1
   {
   %image = %data.getName();

   %settings = $weaponSettings1[%image];
  
   %plyr.weaponSet1 = %plyr.weaponSet1 + %val;
   if (%plyr.weaponSet1 > getWord(%settings,0))
 	%plyr.weaponSet1 = 0;
   if (%plyr.weaponSet1 < 0)
	%plyr.weaponSet1 = getWord(%settings,0);

   %weaponname = GetWords(%settings,2,getWordCount(%settings));
   %curset = $weaponSetting1[%image,%plyr.weaponSet1];
   if (getWord(%settings,1) == -1)
	   %line = GetWords(%curset,0,getWordCount(%curset));
   else
	   %line = GetWords(%curset,0,getWord(%settings,1));

   bottomPrint(%plyr.client,%weaponname SPC "set to"SPC %line,2,1);
   return true;
   }
else if (%level == 3) //Weapon settings 2 !!no nesting!!
   {
   %image = %data.getName();

   %settings = $weaponSettings2[%image];
  
   %plyr.weaponSet2 = %plyr.weaponSet2 + %val;
   if (%plyr.weaponSet2 > getWord(%settings,0))
 	%plyr.weaponSet2 = 0;
   if (%plyr.weaponSet2 < 0)
	%plyr.weaponSet2 = getWord(%settings,0);

   %weaponname = GetWords(%settings,2,getWordCount(%settings));
   %curset = $weaponSetting2[%image,%plyr.weaponSet2];
   if (getWord(%settings,1) == -1)
	   %line = GetWords(%curset,0,getWordCount(%curset));
   else
	   %line = GetWords(%curset,0,getWord(%settings,1));

   bottomPrint(%plyr.client,%weaponname SPC "set to"SPC %line,2,1);
   return true;
   }
}


function packrain(%player) {
%packlist = "Rifle RifleAmmo LSB_PACK LWW_PACK MFL_PACK";
%count = getWordCount(%packlist);
for (%i = 0;%i<%count;%i++) {
	%edge = ((%count/2) - %i)*2 SPC "5 1";
	getWord(%packlist,%i).spawn(%player.getEdge(%edge));
	}
}

