

void LAi_NPC_Equip(ref chr, int rank, bool isWeapons, float GunProb)
{
	//int tmpLangFileID = LanguageOpenFile("interface_strings.txt");
	// NK -->
	//model check
	if(rank <= 0 ) rank = GetLevel(&chr); // NK fix 05-04-30
	int mdlidx = GetModelIndex(chr.model);
	if(mdlidx >= 0)
	{
		ref mdl = &models[mdlidx];
		if(CheckAttribute(mdl, "nogun")) GunProb = 0;
		if(CheckAttribute(mdl, "noblade")) isWeapons = false;
	}
	
	// NK friend/foe diff scaling 05-07-09
	int diffoffset = GetDifficulty();
	if(CheckAttribute(chr, "friend") && sti(chr.friend))
	{
		diffoffset = 5 - diffoffset;
	}
	// NK <--
	//give blade
	if(isWeapons)
	{
		// Scheffnow - weaponsmod -->
		string blade;
		if(ENABLE_WEAPONSMOD == 1)
		{
			blade = GetRandomBladeForLevel(rank-3+diffoffset, rank+diffoffset, 3+(diffoffset/2));
		}
		else
		{
			blade = LAi_NPC_EquipBladeSelection(rank);
		}
		// Scheffnow - weaponsmod <--

		// ccc aug 05 temporary fix against NPCs equiping SWAK and building kit items
		aref bladeItm;
		Items_FindItem(blade,&bladeItm);
		if(bladeItm.skipequip==true) blade="blade"+ (1+ rand(10));
		// ccc end

		GiveItem2Character(chr, blade);
		EquipCharacterByItem(chr, blade);
		
		//give gun
		if(GunProb > 0)
		{
			GunProb *= 0.25 + makefloat(diffoffset)/4.0;
			if(frnd() <= GunProb)
			{
				// Scheffnow - weaponsmod -->
				string gun;
				if(ENABLE_WEAPONSMOD == 1)
				{
					gun = GetRandomGunForLevel(rank-3+diffoffset, rank+diffoffset, 3+(diffoffset/2));
				}
				else
				{
					gun = LAi_NPC_EquipGunSelection(rank);
				}
				// Scheffnow - weaponsmod <--
				//if(gun == "" && rand(100) < 20) gun = "pistol1";	//cccOA1
				if(gun != "")
				{
					// ccc aug 05 temporary fix against NPCs equiping SWAK and building kit items
					aref gunItm;
					Items_FindItem(gun,&gunItm);
					if(gunItm.skipequip==true) gun="pistol1";
					// ccc end

					GiveItem2Character(chr, gun);
					EquipCharacterByItem(chr, gun);
					//Log_SetStringToLog(gun + ", equipping " + chr.equip.gun);
				}
			}
		}

	     // JRH fix oct 06: rewritten old did not give any ammo to NPCs, amount reduced -->
		if (ENABLE_AMMOMOD) {	// LDH change
			
			string weaponID = GetCharacterEquipByGroup(chr,GUN_ITEM_TYPE);
			aref weapon;
			Items_FindItem(weaponID, &weapon);

			if(CheckAttribute(weapon, "shottype") && weapon.shottype != "")
			{
				TakenItems(chr, "gunpowder", rand(2));

				if(weapon.shottype == "pb") TakenItems(chr, "pistolbullets", rand(2));

				if(weapon.shottype == "pg" || weapon.shottype == "pg2") TakenItems(chr, "pistolgrapes", rand(2));
				
				if(weapon.shottype == "mb") TakenItems(chr, "musketbullets", rand(1));
			}					
		}
	     //<-- JRH gives this to officers & enemies, officers are also equipped by Enc_Officers_dialog 

	}
	if(LogsToggle && CheckAttribute(chr,"equip.blade"))
	{
	//	if(CheckAttribute(chr,"equip.gun")) { Log_SetStringtoLog(LanguageConvertString(tmpLangFileID,"You smell some gunpowder about...")); }
	//	else { Log_SetStringtoLog(LanguageConvertString(tmpLangFileID,"You catch a glint of cold steel.")); }
	}
	// NK <--
	//LanguageCloseFile(tmpLangFileID);
}
string LAi_NPC_EquipBladeSelection(int rank)
{
// NK -->
	int min = rank - 4;
	if(min < 1) min = 1;
	int max = rank + 2;
	if(max < 1) max = 1;
	if(min > 8) min = 8;
	int sel = rand(max - min) + min;
	float rareconst = 1.0; //this multiplies the rare percentage, so that it's more possible to get rare blades
	rareconst *= (1 + ((makefloat(GetDifficulty())-1)/3.0)); 	//difficulty
	string blade = BLADE_SABER;
	//Log_SetStringToLog("Rank: " + sel);

	//find available blades
	ref itm;
	int firstblade = 256;
	int lastblade = -1;
	int totalblades = 0;
	int i;
	int bladesID[256];
	int bladesML[256];

	//find all blades
	for(i=0; i<ITEMS_QUANTITY; i++)
	{
		makeref(itm,Items[i]);
		if(!CheckAttribute(itm,"groupID")) continue;
		if(itm.groupID == BLADE_ITEM_TYPE)
		{
			if(i < firstblade) firstblade = i;
			if(i > lastblade) lastblade = i;
			bladesID[totalblades] = i;
			totalblades++;
		}
	}
	//Log_SetStringToLog("Total avail = " + totalblades);
	//sort blades high->low by minlevel
	for(i = firstblade; i <= lastblade; i++)
		bladesML[i - firstblade] = sti(Items[i].minlevel);

	int x,y,holder,place;
	for(x = 0; x < totalblades; x++)
	{
		for(y = 0; y < totalblades-1; y++)
		{
			if(bladesML[y] < bladesML[y+1])
			{
				holder = bladesML[y+1];
				place = bladesID[y+1];
				bladesML[y+1] = bladesML[y];
				bladesID[y+1] = bladesID[y];
				bladesML[y] = holder;
				bladesID[y] = place;
      			}
			if(bladesML[y] == bladesML[y+1] && Rand(1))
			{
				holder = bladesML[y+1];
				place = bladesID[y+1];
				bladesML[y+1] = bladesML[y];
				bladesID[y+1] = bladesID[y];
				bladesML[y] = holder;
				bladesID[y] = place;
      			}
		}
	}

	//find blade to use
	for(i = 0; i < totalblades; i++)
	{
		//Log_SetStringToLog("Testing blade " + (i+1));
// LDH testing problems with rares
		if(!CheckAttribute(Items[bladesID[i]],"rare")) continue;
//		Trace("Testing blade " + i + ", rare = " + Items[bladesID[i]].rare);
		if(sel >= bladesML[i]*(2.0 - (makefloat(GetDifficulty())-1.0)/3.0) && frnd() <= stf(Items[bladesID[i]].rare) * rareconst) // difficulty added
		{ blade = Items[bladesID[i]].id; /*Log_SetStringToLog("Using " + Items[bladesID[i]].id);*/ break; }
	}
	//Log_SetStringToLog("2Using " + Items[bladesID[i]].id);	
	if(blade == "") blade = BLADE_SABER;
// NK <--
	return blade;
}

// NK -->
string LAi_NPC_EquipGunSelection(int rank)
{
	int min = rank - 4;
	if(min < 1) min = 1;
	int max = rank + 2;
	if(max < 1) max = 1;
	if(min > 8) min = 8;
	int sel = rand(max - min) + min;
	float rareconst = 1.0; //this multiplies the rare percentage, so that it's more possible to get rare guns
	rareconst *= makefloat(GetDifficulty())/4.0;	//difficulty
	string gun = "pistol1"; // now DOES have a default
	//Log_SetStringToLog("Rank: " + sel);

	//find available guns
	ref itm;
	int firstgun = 256;
	int lastgun = -1;
	int totalguns = 0;
	int i;
	int gunsID[256];
	int gunsML[256];

	//find all guns
	for(i=0; i<ITEMS_QUANTITY; i++)
	{
		makeref(itm,Items[i]);
		if(!CheckAttribute(itm,"groupID")) continue;
		if(itm.groupID == GUN_ITEM_TYPE)
		{
			if(i < firstgun) firstgun = i;
			if(i > lastgun) lastgun = i;
			gunsID[totalguns] = i;
			totalguns++;
		}
	}
	//Log_SetStringToLog("Total avail = " + totalguns);
	//sort guns high->low by minlevel
	for(i = firstgun; i <= lastgun; i++)
		gunsML[i - firstgun] = sti(Items[i].minlevel);

	int x,y,holder,place;
	for(x = 0; x < totalguns; x++)
	{
		for(y = 0; y < totalguns-1; y++)
		{
			if(gunsML[y] < gunsML[y+1])
			{
				holder = gunsML[y+1];
				place = gunsID[y+1];
				gunsML[y+1] = gunsML[y];
				gunsID[y+1] = gunsID[y];
				gunsML[y] = holder;
				gunsID[y] = place;
				}
			if(gunsML[y] == gunsML[y+1] && Rand(1))
			{
				holder = gunsML[y+1];
				place = gunsID[y+1];
				gunsML[y+1] = gunsML[y];
				gunsID[y+1] = gunsID[y];
				gunsML[y] = holder;
				gunsID[y] = place;
				}
		}
	}

	//find gun to use
	for(i = 0; i < totalguns; i++)
	{
// LDH find error in rare
		if(!CheckAttribute(Items[gunsID[i]],"rare")) continue;
//		Trace("Testing gun " + i + ", rare = " + Items[gunsID[i]].rare);
		if(sel >= gunsML[i]*(2.0 - (makefloat(GetDifficulty()-1))/3.0) && frnd() <= stf(Items[gunsID[i]].rare) * rareconst) // difficulty added
		{ gun = Items[gunsID[i]].id; /*Log_SetStringToLog("Using " + Items[gunsID[i]].id);*/ break; }
	}
	//Log_SetStringToLog("2Using " + Items[gunsID[i]].id);	
	return gun;
}

// NK <--
