// This file has been heavily modded by NK and ccc to let it generate random locationspecific NPCs for every location.
// Before messing with it you'd better contact us at the "Potc modding" forum of www.piratesahoy.com


object LEnc_MonstersLoginObject;

//Начать загрузку монстров в локацию
bool LEnc_MonstersLoginStart(ref location)
{
	// Trace("LEnc_MonstersLoginStart start");
	// this should be fine, but I'm not doing it yet...DeleteAttribute(&LEnc_MonstersLoginObject, ""); // NK clear object. 05-07-27
	LEnc_MonstersLoginObject.isskel = frnd() <= SKELETON_CHANCE;
	LEnc_MonstersLoginObject.ismum = frnd() <= MUMMY_CHANCE;

	string label = "";
	if(CheckAttribute(location,"id.label")) label = location.id.label; 


	if(CheckAttribute(GetMainCharacter(),"vcskip")) return false; // NK 04-07
	float monkeys, skeletons;
	bool done = false; // NK
	LEnc_MonstersLoginObject.type = "dungeon";
	if(CheckAttribute(location, "type"))
	{
		
		if(location.id == "Hispaniola_Passage") return false;
		if(location.id == "Hispaniola_temple")
		{
			LEnc_MonstersLoginObject.bandit = "0";
			LEnc_MonstersLoginObject.skeleton = "0";
			LEnc_MonstersLoginObject.monkey = "0";
			LEnc_MonstersLoginObject.mummy = "1.0";
			LEnc_MonstersLoginObject.vagabond = "0";
			LEnc_MonstersLoginObject.type = "Jungle";
			return true;
		}
		if(location.type == "KhaelRoa") return false;   // SECTION FOR KHAEL ROA JUNGLES
		
		if(location.type == "KR_Jungle1")
		{
			if (rand(100) > 40)
			{
				LEnc_MonstersLoginObject.bandit = "0";
				LEnc_MonstersLoginObject.skeleton = "0";
				LEnc_MonstersLoginObject.monkey = "0.3";
				LEnc_MonstersLoginObject.mummy = "0";
				LEnc_MonstersLoginObject.vagabond = "0";
				LEnc_MonstersLoginObject.type = "Jungle";
				return true;
			}
			else
			{
				return false;
			}
		}
		
		if(location.type == "KR_Jungle")
		{
			if(rand(100) > 50)
			{
				LEnc_MonstersLoginObject.bandit = "0";
				LEnc_MonstersLoginObject.skeleton = "0";
				LEnc_MonstersLoginObject.monkey = "0.3";
				LEnc_MonstersLoginObject.mummy = "0";
				LEnc_MonstersLoginObject.vagabond = "0";
				LEnc_MonstersLoginObject.type = "Jungle";
				return true;
			}
			else
			{
				if(rand(100) < 40)
				{
					LEnc_MonstersLoginObject.bandit = "0";
					LEnc_MonstersLoginObject.skeleton = "0";
					LEnc_MonstersLoginObject.monkey = "0";
					LEnc_MonstersLoginObject.mummy = "1.0";
					LEnc_MonstersLoginObject.vagabond = "0";
					LEnc_MonstersLoginObject.type = "Jungle";
					return true;
				}
				else
				{	
					return false;
				}
			}		
		}
		// END OF SECTION FOR KHAEL ROA JUNGLES

		 //NK cccOA1 More locationtypes with monsters
		if(location.type == "jungle") // NK
		{
			LEnc_MonstersLoginObject.type = "jungle";
		}
		// NK -->
		if(location.type == "town")
		{
			if(CheckAttribute(location,"vcskip") && TOWN_VCSKIP_DISABLEALL) return false;
			LEnc_MonstersLoginObject.type = "town";
			if(isDay()) { if(frnd() > TOWN_ENC_CHANCE) { return false; } }
			else { if(frnd() > TOWN_ENC_CHANCE_NIGHT) { return false; } }
			LEnc_MonstersLoginObject.bandit = "0";
			LEnc_MonstersLoginObject.skeleton = "0";
			LEnc_MonstersLoginObject.monkey = "0";
			LEnc_MonstersLoginObject.mummy = "0";
			LEnc_MonstersLoginObject.vagabond = "1";
			if(frnd() < TOWN_BANDIT_CHANCE * (2.0 - IsDay()) && !CheckAttribute(location,"vcskip"))
			{
				if(DEBUGINFO) Log_SetStringToLog("Loading town with possible bandits");
				bool bpossible = true;
				bool btoday = false;
				if(!CheckAttribute(location,"lastbandit")) { location.lastbandit = environment.date.day + " " + environment.date.month; }
				else
				{
					if(location.lastbandit != environment.date.day + " " + environment.date.month)
					{
						location.lastbandit = environment.date.day + " " + environment.date.month;
					}
					else
					{
						bpossible = frnd() < TOWN_BANDIT_RESPAWN_CH;
						btoday = true; //really should check if you've SEEN / KILLED them, not just if they were GEN'D. But ah well.
						if(DEBUGINFO) Log_SetStringToLog("Bandits already generated" + stringRet(bpossible," but more possible,", " so none."));
					}
				}
				string sTownName = "";
				if(CheckAttribute(location,"townsack")) sTownName = location.townsack;
				float bprob = GetTownMonsterProb(sTownName) / (makefloat(GetTownSize(sTownName))/1000.0);
				bprob *= TOWN_BANDIT_ENC_SCALAR * (2.0 - IsDay());
				bprob *= 0.5 + makefloat(GetDifficulty())/8.0;
				if(btoday) bprob *= TOWN_BANDIT_RESPAWN_SCL; //less if you've already encountered some
				bprob = Clampf(bprob); // to catch <0 or >1 values.
				if(bprob > TOWN_BANDIT_MAX) bprob = TOWN_BANDIT_MAX;
				if(!bpossible) bprob = 0;
				if(DEBUGINFO) Log_SetStringToLog("Bandit prob: " + bprob);
				LEnc_MonstersLoginObject.bandit = bprob;
				LEnc_MonstersLoginObject.vagabond = 1.0 - bprob;
			}
			else { if(DEBUGINFO) Log_SetStringToLog("Loading town with only vags"); }
			done =  true;
		}
		if(location.type == "house")
		{
			if(CheckAttribute(location,"vcskip")) return false;
			if(label == "Tavern.") return false;  // NK for redundancy
			if(!LAi_LocationCanFight() && HOUSE_FIGHTDISABLE_ALL) 
			{
				if(DEBUGINFO) Log_SetStringToLog("House fight check failed");
				return false;
			}
			float scalar = 1.0;
			if(!LAi_LocationCanFight())  scalar = HOUSE_DISFIGHT_SCL;
			if(frnd() > HOUSE_ENC_CHANCE * scalar && !CheckAttribute(location,"lastentermonth"))
			{
				if(DEBUGINFO) Log_SetStringToLog("House Enc check failed, chance was " + HOUSE_ENC_CHANCE * scalar);
				return false;
			}
			int vag = 1;
			done = true;

			// Here we check to see if we already generated person(s) for this house
			bool newvags = true;
			if(CheckAttribute(location,"lastentermonth") && LAi_LocationCanFight()) //so it's not someone else's house
			{
				if(location.lastentermonth == Environment.date.month) { newvags = false; }
			}
			LEnc_MonstersLoginObject.type = "house";
			if(newvags || CheckAttribute(location,"randloc") ) //generate new people for house; ccc randomlocs, always new ones
			{
				if(DEBUGINFO) Log_SetStringToLog("Generate new occupants");
				if(CheckAttribute(location,"lastentermonth")) { DeleteAttribute(location,"lastentermonth"); }
				if(CheckAttribute(location,"vag")) { DeleteAttribute(location,"vag"); }
				if(CheckAttribute(location,"savethesevags")) { DeleteAttribute(location,"savethesevags"); }
				//if(CheckAttribute(location,"curvag")) { DeleteAttribute(location,"curvag"); }
				vag = 1; // note that if HOUSE_FIGHTDISABLE_ALL == false, vags WILL sometimes be gen'd in quest-used houses!
				if(frnd() < HOUSE_BANDIT_CHANCE && LAi_LocationCanFight()) vag = 0; // to skip quest-used houses, as above.
				LEnc_MonstersLoginObject.bandit = 1-vag;
				LEnc_MonstersLoginObject.skeleton = "0";
				LEnc_MonstersLoginObject.monkey = "0";
				LEnc_MonstersLoginObject.mummy = "0";
				LEnc_MonstersLoginObject.vagabond = vag;
				if(vag > 0 && LAi_LocationCanFight()) { location.savethesevags = true; location.lastentermonth = Environment.date.month; } //so vag(s) will be registered.
			}
			else //recover old people used
			{
				
				if(frnd() < HOUSE_OLDPEOPLE_REAPPEAR)
				{
					if(DEBUGINFO) Log_SetStringToLog("Recover old vags");
					LEnc_MonstersLoginObject.bandit = "0";
					LEnc_MonstersLoginObject.skeleton = "0";
					LEnc_MonstersLoginObject.monkey = "0";
					LEnc_MonstersLoginObject.mummy = "0";
					LEnc_MonstersLoginObject.vagabond = "1";
				}
				else
				{
					//do check for temporary bandits - BARGLARS! Who don't like to be interrupted...
					if(frnd() > HOUSE_OLDPEOPLE_BANDIT || !LAi_LocationCanFight()) return false; //encounter no one
					if(DEBUGINFO) Log_SetStringToLog("Old vag gone at the moment, but...burglers!");
					LEnc_MonstersLoginObject.bandit = "1";
					LEnc_MonstersLoginObject.skeleton = "0";
					LEnc_MonstersLoginObject.monkey = "0";
					LEnc_MonstersLoginObject.mummy = "0";
					LEnc_MonstersLoginObject.vagabond = "0";
				}
			}
		}
		if(location.type == "tavern")
		{
			if(CheckAttribute(location,"vcskip")) return false;
			LEnc_MonstersLoginObject.type = "tavern";
			if(frnd() > TAVERN_ENC_CHANCE) return false;
			float tvag = TAVERN_ENC_QTY;
			if(frnd() < TAVERN_BANDIT_CHANCE) tvag = frnd()/4.0;
			LEnc_MonstersLoginObject.bandit = TAVERN_ENC_QTY-tvag;
			LEnc_MonstersLoginObject.skeleton = "0";
			LEnc_MonstersLoginObject.monkey = "0";
			LEnc_MonstersLoginObject.mummy = "0";
			LEnc_MonstersLoginObject.vagabond = tvag; // NK
			done = true;
		}
		// NK <--
	}
	LEnc_MonstersLoginObject.current = "";
	if(CheckAttribute(location,"vcskip")) return false; // NK
	if(!done) { // NK
	if(LEnc_MonstersLoginObject.type == "jungle")
	{
		if(IsDay())
		{
			//Днём либо бандиты, либо олухи, либо никто
			if(rand(100) > 20)
			{
				if(rand(100) < 30)
				{
					//Выбрали олухов
					LEnc_MonstersLoginObject.bandit = "0";
					LEnc_MonstersLoginObject.skeleton = "0";
					LEnc_MonstersLoginObject.monkey = "0";
					LEnc_MonstersLoginObject.mummy = "0";
					LEnc_MonstersLoginObject.vagabond = "1";
				}else{
					return false;
				}
			}else{
				LEnc_MonstersLoginObject.bandit = "1";
				LEnc_MonstersLoginObject.skeleton = "0";
				LEnc_MonstersLoginObject.monkey = "0";
				LEnc_MonstersLoginObject.mummy = "0";
				LEnc_MonstersLoginObject.vagabond = "0";
			}
		}else{
			//Ночью либо бандиты, либо скелеты, либо олухи, либо никто
			if(rand(100) < 40)
			{
				if(rand(100) < 20)
				{
					//Выбрали олухов
					LEnc_MonstersLoginObject.bandit = "0";
					LEnc_MonstersLoginObject.skeleton = "0";
					LEnc_MonstersLoginObject.monkey = "0";
					LEnc_MonstersLoginObject.mummy = "0";
					LEnc_MonstersLoginObject.vagabond = "1";
				}else{
					return false;
				}
			}else{
				if(rand(100) < 40)
				{
					//Выбрали бандитов
					LEnc_MonstersLoginObject.bandit = "1";
					LEnc_MonstersLoginObject.skeleton = "0";
					LEnc_MonstersLoginObject.monkey = "0";
					LEnc_MonstersLoginObject.mummy = "0";
					LEnc_MonstersLoginObject.vagabond = "0";
				}else{
					//Выбрали скелетов
					monkeys = 0.4 + (rand(100)*0.01)*0.6;
					skeletons = 1.0 - monkeys;
					//cccOA1 allow indoors only pseudoskeletons
					if(location.type == "tavern" || location.type == "house")
					{
						skeletons = 1.0;
						monkeys = 0;
					} 	// <- cccOA1
					LEnc_MonstersLoginObject.bandit = "0";
					LEnc_MonstersLoginObject.skeleton = skeletons;
					LEnc_MonstersLoginObject.monkey = monkeys;
					LEnc_MonstersLoginObject.mummy = "0";
					LEnc_MonstersLoginObject.vagabond = "0";
				}
			}
		}
	}else{
		if(LEnc_MonstersLoginObject.type == "inca_temple")
		{
				LEnc_MonstersLoginObject.bandit = "0";
				LEnc_MonstersLoginObject.skeleton = "0.2";
				LEnc_MonstersLoginObject.monkey = "0.3";
				LEnc_MonstersLoginObject.mummy = "0.5";
				LEnc_MonstersLoginObject.vagabond = "0";
		}else{
			if(rand(100) < 90)
			{
				LEnc_MonstersLoginObject.bandit = "0";
				LEnc_MonstersLoginObject.skeleton = "1";
				LEnc_MonstersLoginObject.monkey = "0";
				LEnc_MonstersLoginObject.mummy = "0";
				LEnc_MonstersLoginObject.vagabond = "0";
			}else{
				skeletons = 0.8 + (rand(100)*0.01)*0.2;
				monkeys = 1.0 - skeletons;
				LEnc_MonstersLoginObject.bandit = "0";
				LEnc_MonstersLoginObject.skeleton = skeletons;
				LEnc_MonstersLoginObject.monkey = monkeys;
				LEnc_MonstersLoginObject.mummy = "0";
				LEnc_MonstersLoginObject.vagabond = "0";
			}
		}
	}
	// NK -->

	// ccc Greater Oxbay: for all parts of Maltese Abbey regardless of type
	if(label == "Abbey of the Maltese order")
	{
		LEnc_MonstersLoginObject.bandit = "0.9";
		LEnc_MonstersLoginObject.skeleton = "0";
		LEnc_MonstersLoginObject.monkey = "0";
		LEnc_MonstersLoginObject.mummy = "0";
		LEnc_MonstersLoginObject.vagabond = "0.1";
	}

	// ccc to enable encounters in fake forts
	if(label == "MILITARY AREA. NO TRESPASSING!")
	{
		LEnc_MonstersLoginObject.bandit = 0.9;
		LEnc_MonstersLoginObject.skeleton = "0";
		LEnc_MonstersLoginObject.monkey = "0";
		LEnc_MonstersLoginObject.mummy = "0";
		LEnc_MonstersLoginObject.vagabond = 0.1;
	}
	// ccc end
	}
	if(DEBUGINFO)
	{
		if(stf(LEnc_MonstersLoginObject.bandit) + stf(LEnc_MonstersLoginObject.skeleton) + stf(LEnc_MonstersLoginObject.monkey) + stf(LEnc_MonstersLoginObject.mummy) + stf(LEnc_MonstersLoginObject.vagabond) == 0)
			{ Log_SetStringToLog("Nothing generated") }
		else
			{ Log_SetStringToLog("Bnd: " + LEnc_MonstersLoginObject.bandit + ", Vag: " + LEnc_MonstersLoginObject.vagabond + ", Sk: " + LEnc_MonstersLoginObject.skeleton + ", Mo: " + LEnc_MonstersLoginObject.monkey + ", Mu: " + LEnc_MonstersLoginObject.mummy); }
	}

	// Trace("LEnc_MonstersLoginStart end");

	return true;
}

//Установить модельку для монстра
bool LEnc_MonstersLoginSelectModel(ref location, aref modelInfo, string group, string locator)
{
	//trace("Gauging: Start Select Model");
	// Trace("LEnc_MonstersLoginSelectModel start");
	string label = ""
	if(CheckAttribute(location,"id.label")) label = location.id.label; 

	modelInfo.type = LEnc_MonstersLoginObject.type; // NK
	modelInfo.sex = "";
	modelInfo.nation = ALL_NATIONS;
	modelInfo.iscombat = -1;
	float bandit = stf(LEnc_MonstersLoginObject.bandit);
	float skeleton = stf(LEnc_MonstersLoginObject.skeleton);
	float vagabond = stf(LEnc_MonstersLoginObject.vagabond);
	float monkey = stf(LEnc_MonstersLoginObject.monkey);
	float mummy = stf(LEnc_MonstersLoginObject.mummy);
	//if(LEnc_MonstersLoginObject.type == "inca_temple") type = 1.0;
	bool found = false;
	//Определимся, что выбрали


	// NK 05-07-08 redo this whole darn thing for modeltypes
	// NOTE: the sum of the encounterable monsters must always be <= 1.0
	float p = frnd();
	if(!found && p <= bandit)
	{
		LEnc_MonstersLoginObject.current = "bandit";
		//modelInfo.sex = "man";
		// ccc to enable encounters in fake forts
		if(label == "MILITARY AREA. NO TRESPASSING!" && CheckAttribute(location,"townsack"))
		{
			modelInfo.iscombat = true;
			modelInfo.model = "Soldiers"; //modelInfo.model = SelectSoldierModelByNation(characterFromID(location.townsack + " Commander"), "Soldier");
			modelInfo.nation = GetCurrentLocationNation();
		}
		// ccc Greater Oxbay for Maltese Abbey 	
		else{ if(label == "Abbey of the Maltese order")
		{
			modelInfo.iscombat = true;
			/*if(rand(100) < 30){ modelInfo.current = "Priests";}
			else{modelInfo.current = "Maltese";}*/
			modelInfo.model = "Maltese";
		}
		else
		{
			modelInfo.iscombat = true;
			modelInfo.model = "Bandits";
			//modelInfo.sex = "man";
			modelInfo.nation = GetCurrentLocationNation();
		} }
		found = true;
	}
	if(!found && p <= bandit + skeleton)
	{
		//Скелета
		LEnc_MonstersLoginObject.current = "skeleton";
		//cccOA1 skeletons replaced at several locations
		switch(location.type)
		{
			case "dungeon":
				if(sti(LEnc_MonstersLoginObject.isskel))  //  CCC Enable skeletons in dungeon  -->
				{
					modelInfo.model = "Skeletons";
	            		}
	            		else
	            		{
					modelInfo.model = "Smugglers";
					//modelInfo.sex = "man";
					modelInfo.nation = PIRATE;
				}
				modelInfo.iscombat = true;
			break;

			case "jungle":
				if(sti(LEnc_MonstersLoginObject.ismum))  //  CCC Enable skeletons in dungeon  -->
				{
					modelInfo.model = "Mummies";
	            		}
	            		else
	            		{
					modelInfo.model = "Natives";
				}
				//LEnc_MonstersLoginObject.current.sex = "woman";//ccc WOman is no bug!
				modelInfo.iscombat = true;
			break;

			case "inca_temple":			// ccc nov04 to delete "smuggler" skeletons from temple
				if(frnd() < MUMMY_CHANCE)  //  CCC Enable skeletons in dungeon  -->
				{
					modelInfo.model = "Mummies";
	            		}
	            		else
	            		{
					modelInfo.model = "Natives";
				}
				//LEnc_MonstersLoginObject.current.sex = "woman";//ccc WOman is no bug!
				modelInfo.iscombat = true;
			break;

			// the following cases should NEVER occur, because skels are never assigned to these locations
			case "town":
				modelInfo.model = "Outlaws";
				//modelInfo.sex = "man";
				modelInfo.nation = PIRATE;
				modelInfo.iscombat = true;
			break;

			case "house":
				modelInfo.model = "Vagabonds";
				modelInfo.nation = GetCurrentLocationNation();
				/*modelInfo.model = "towngirl" + (1 + rand(6));
				modelInfo.ani = "woman";
				LEnc_MonstersLoginObject.current.sex = "woman";*/
			break;

			case "tavern":
				modelInfo.model = "Sailors";
				modelInfo.nation = GetCurrentLocationNation();
			break;

			//default: // scheffnow - bf
				modelInfo.model = "Vagabonds";
				// Screwface : to have dead skeletons on Turks shore ship
				if((location.id == "Turks_shore_ship") || (location.id == "Hispaniola_Grot2") || (location.id == "Hispaniola_Grot3")){modelInfo.model = "Skeletons";}
				modelInfo.nation = GetCurrentLocationNation();
				/*modelInfo.model = "skel" + (1 + rand(3));
				modelInfo.ani = "man";
				LEnc_MonstersLoginObject.current.sex = "skeleton";*/
				//b12:
				/*int tmpmodelnm2 = 1 + rand(9); if(tmpmodelnm2 == 10) tmpmodelnm2 = 11; // PB swaps 10/11 to preserve Barbossa as something extra; NK 05-03-21 do this in code
				modelInfo.model = "pirat" + tmpmodelnm2;  // ditto
				modelInfo.ani = "man";
				LEnc_MonstersLoginObject.current.sex = "man";*/
		}
		// <- cccOA1
		found = true;
	}
	if(!found && p <= bandit + skeleton + monkey)
	{
		//Скелета-обезьяну
		LEnc_MonstersLoginObject.current = "monkey";
		modelInfo.model = "Monkeys";
		found = true;
	}
	if(!found && p <= bandit + skeleton + monkey + mummy)
	{
		//Скелета-обезьяну
		LEnc_MonstersLoginObject.current = "mummy";
		if(frnd() < MUMMY_CHANCE)  //  CCC Enable skeletons in dungeon  -->
		{
			modelInfo.model = "Mummies";
        	}
        	else
        	{
			modelInfo.model = "Natives";
		}
		found = true;
	}
	if(!found && p <= bandit + skeleton + monkey + mummy + vagabond)
	{
		LEnc_MonstersLoginObject.current = "vagabond";
		switch(label)	// ccc specials for locationspecific encounters
		{
			case "Bedroom":
				modelInfo.sex = "woman";
				 modelInfo.model = "Rich_Citizens";
			break;

			case "MILITARY AREA. NO TRESPASSING!":
				modelInfo.sex = "man";
				modelInfo.model = "Rich_Citizens";
				modelInfo.nation = GetCurrentLocationNation(); // Maybe disable this?
				/*modelInfo.ani = "man";
				LEnc_MonstersLoginObject.current.sex = "man";
				switch(rand(5))
				{
				case 0: modelInfo.model = "huber_eng"; break;
				case 1: modelInfo.model = "huber_fra"; break;
				case 2: modelInfo.model = "huber_por"; break;
				case 3: modelInfo.model = "huber_spa"; break;
				case 4: modelInfo.model = "admiral"; break;
				case 5: modelInfo.model = "man6"; break;
				}*/
			break;

			case "Abbey of the Maltese order":
				/*modelInfo.ani = "man";
				LEnc_MonstersLoginObject.current.sex = "man";*/
				 modelInfo.model = "Monks";
			break;

			// default:
				modelInfo.model = "Vagabonds";
		}
		found = true;
	}
	//Никого
	modelInfo.current = LEnc_MonstersLoginObject.current;
	return found;
	// Trace("LEnc_MonstersLoginSelectModel end");
}

//Скорректировать параметры созданного монстра
void LEnc_MonstersLoginCorrectParams(ref location, aref chr, string group, string locator)
{
	// Trace("LEnc_MonstersLoginCorrectParams start");

	bool isSet = false;
	ref mchr = GetMainCharacter();
	int mchr_rank = sti(mchr.rank);
	DeleteAttribute(chr, "patroltype"); // NK 04-20
	//TraceAndLog("chr " + chr.id + " is a " + chr.current)
    
    //string label = ""
    string label = ""; // Swindler 05-10-10: corrected syntax error
    
    if (CheckAttribute(location,"id.label")) {
        label = location.id.label;
    }
    
    bool  hasBlade = true;  // Swindler 05-10-10: monsters normally has blade
    float gunProb = frnd(); // Swindler 05-10-10: probability for monster to have gun
	switch(chr.current)
	{
	case "bandit":
		isSet = true;
		// NK -->
		//LAi_group_SetRelation(LAI_GROUP_PLAYER, LAI_GROUP_MONSTERS, LAI_GROUP_ENEMY);
		LAi_group_SetRelation(LAI_GROUP_MONSTERS, LAI_GROUP_GUARDS, LAI_GROUP_NEITRAL);
		// NK <--
		LAi_CharacterReincarnation(chr, false, true);	//cccOA1
		chr.patroltype = true; // NK 04-20
		LAi_type_patrol_Init(chr);			//cccOA1
		
        //LAi_NPC_Equip(chr, sti(chr.rank), true, frnd()); // Swindler 05-10-10: give a random chance for gun, moved to the end of the function
		/*if(!CheckAttribute(chr,"equip.blade") || chr.equip.blade == "error")
		{
			GiveItem2Character(chr, BLADE_SABER);
			EquipCharacterByItem(chr, BLADE_SABER);
		}*/
		chr.dialog.CurrentNode = "First time";		// CCC APR07
		chr.dialog.filename = "Enc_Bandit_dialog.c";		// CCC APR07
		GiveItem2Character(chr, "potion"+ makeint(1 + rand(3)) );	// jun05 booty for stealing

		// ccc Greater Oxbay  stronger fortgarrison
		if(label == "MILITARY AREA. NO TRESPASSING!" && CheckAttribute(location,"townsack"))
		{
			// chr.perks.list.CriticalHit = "1";	
			// chr.perks.list.Gunman = "1";	
			// chr.perks.list.SwordplayProfessional = "1";
			//GiveItem2Character(chr, "pistol1");
            gunProb = 2.0; // Swindler 05-10-10: fortgarrison has gun
			//EquipCharacterByItem(chr, "pistol1");
			LAi_group_MoveCharacter(chr, LAI_GROUP_GUARDS);
			chr.dialog.filename = "patrol_dialog.c";
			ref commander = (characterFromID(location.townsack + " Commander"));	// the fortcommander of this town
			chr.nation = commander.nation;

			// jun05 booty for stealing
			// works only with new SWAK itemnames GiveItem2Character(chr, "pistolx"+ makeint(1 + rand(5)) );	
			if(frnd()<0.2) TakenItems(chr, "pistolbbuss", rand(1));	
			if(frnd()<0.2) TakenItems(chr, "pistolgrenade", rand(2));
			if(frnd()<0.2) TakenItems(chr, "pistolrock", rand(2));
		}

		if(label == "Abbey of the Maltese order")
		{
			// chr.perks.list.CriticalHit = "1";	
			// chr.perks.list.Gunman = "1";	
			// chr.perks.list.SwordplayProfessional = "1";
			gunProb = 2.0; // Swindler 05-10-10: fortgarrison has gun
            //GiveItem2Character(chr, "pistol1");
			//EquipCharacterByItem(chr, "pistol1");
			LAi_group_MoveCharacter(chr, LAI_GROUP_GUARDS);
			chr.dialog.filename = "patrol_dialog.c";

			// jun05 booty for stealing
			// works only with new SWAK itemnames GiveItem2Character(chr, "pistolx"+ makeint(1 + rand(5)) );	
			if(frnd()<0.2) TakenItems(chr, "pistolbbuss", rand(1));	
			if(frnd()<0.2) TakenItems(chr, "pistolgrenade", rand(2));
			if(frnd()<0.2) TakenItems(chr, "pistolrock", rand(2));
		}
		break;
		
	case "skeleton":
        isSet = true;
		//chr.sex = LEnc_MonstersLoginObject.current.sex;		//cccOA1
		/*if(LEnc_MonstersLoginObject.current.sex == "woman")	//cccOA1
		{
			chr.perks.list.CriticalHit = "1";			//cccOA1
			chr.perks.list.SwordplayProfessional = "1";	//cccOA1*/
 		if(chr.model == "mummy" || chr.model == "native")		//ccc 10.2
 		{
            gunProb = 0.0; // Swindler 05-10-10: mummies and natives don't have guns
			chr.name = VC_MUMMY_NAME; // ccc10.2
			chr.lastname = VC_MUMMY_LNAME; // ccc10.2
			if(VC_MUMMY_NOMONEY) chr.money = 0; // ccc10.2
			GiveItem2Character(chr, "indian"+ makeint(1 + rand(15)) );	// jun05 booty for stealing
		}
		//}
        // --> Swindler: give chance to equip with gun
		//LAi_NPC_Equip(chr, sti(chr.rank), true, false);
        //LAi_NPC_Equip(chr, sti(chr.rank), true, frnd());
        // Swindler <--
		// ccc sneakmod -> lets monsters walk through dungeons
		//ref Pchar = GetMainCharacter();
		if (mchr.model == "animists3")
		{
            LAi_type_citizen_Init(chr);
        }
		else
        {
            LAi_SetWarriorType(chr); LAi_warrior_SetStay(chr, true);
        }
		// ccc sneakmod <-

		LAi_group_MoveCharacter(chr, LAI_GROUP_MONSTERS);
		/*if(!CheckAttribute(chr,"equip.blade") || chr.equip.blade == "error")
		{
			GiveItem2Character(chr, BLADE_SABER);
			EquipCharacterByItem(chr, BLADE_SABER);
		}*/
		
		// jun05 booty for stealing
		// works only with new SWAK itemnames GiveItem2Character(chr, "pistolx"+ makeint(1 + rand(5)) );	
		if(frnd()<0.2) TakenItems(chr, "pistolbbuss", rand(1));	
		if(frnd()<0.2) TakenItems(chr, "pistolgrenade", rand(2));
		if(frnd()<0.2) TakenItems(chr, "pistolrock", rand(2));

		break;
		
	case "monkey":
		isSet = true;
		chr.sex = "monkey";
		LAi_type_monkey_Init(chr);
        hasBlade = false; // Swindler 05-10-10: monkeys don't have blades
        gunProb = 0.0;    // Swindler 05-10-10: monkeys don't have guns
        //RemoveCharacterEquip(chr, BLADE_ITEM_TYPE);
		//LAi_NPC_Equip(chr, sti(chr.rank), true, false);
		// ccc -->
		chr.money = 0;
		chr.name = VC_MONKEY_NAME;
		chr.lastname = VC_MONKEY_LNAME;
		// ccc <--
		break;
		
	case "mummy":
		isSet = true;
		chr.sex = "skeleton";
		gunProb = 0.0; // Swindler 05-10-10: mummies don't have guns            
        LAi_NPC_Equip(chr, sti(chr.rank), true, false);
		if(VC_MUMMY_NOMONEY) chr.money = 0; // NK
		GiveItem2Character(chr, "indian"+ makeint(1 + rand(15)) );	// jun05 booty for stealing
		GiveItem2Character(chr, "mineral"+ makeint(1 + rand(3)) );	// jun05 booty for stealing

		break;
		
	case "vagabond":
		isSet = true;
		LAi_CharacterReincarnation(chr, false, true);
		LAi_group_MoveCharacter(chr, GetCurrentCitizenGroup()); //LAI_DEFAULT_GROUP, NK now place in correct group. 05-07-19

/*	// ccc sneakmod ->
		LAi_group_SetAlarm(LAI_DEFAULT_GROUP, LAI_GROUP_PLAYER, 0.0);	//ccc10.2
		LAi_group_SetAlarm(LAI_DEFAULT_GROUP, LAI_GROUP_MONSTERS, 0.0);	//ccc10.2
		LAi_group_SetAlarm(LAI_DEFAULT_GROUP, LAI_GROUP_GUARDS, 0.0);	//ccc10.2
		LAi_group_SetRelation(LAI_GROUP_PLAYER, LAI_DEFAULT_GROUP, LAI_GROUP_NEITRAL); // NK
		LAi_group_SetRelation(LAI_GROUP_GUARDS, LAI_DEFAULT_GROUP, LAI_GROUP_NEITRAL); // NK
*/	// ccc sneakmod 
// NK go back to citizen so we can add addressing only if no hostiles around. 05-07-17
        LAi_type_citizen_Init(chr);
	// ccc sneakmod 	LAi_type_citizen_Init(chr);
		// chr.patroltype = true; 
		//LAi_type_patrol_Init(chr);
		// --> Swindler 05-10-10: I think, as long vags have rich citizen models, they can also be equipped with good weapons
        //GiveItem2Character(chr, "blade5");
        //EquipCharacterbyItem(chr, "blade5");
        gunProb = 0.0;
        // Swindler
		// ccc sneakmod <-

		// NK -->
		/*string sCurvag = "";
		if(CheckAttribute(chr,"saverich")) sCurvag = chr.saverich;
		if(sCurvag != "") { location.vags.(sCurvag).rich = false; DeleteAttribute(chr,"saverich"); }*/
		bool restore = CheckAttribute(location,"vag.rich");
		bool save = false;
		if (restore) {
            save = location.vag.rich == "-1";
        }
		restore = restore && !save;
		if (save) {
            location.vag.rich = false;
        }
		bool brich = frnd() < VAG_MERCH_CHANCE;
		if(restore) brich = sti(location.vag.rich);
		if(brich)
		{
			//if(sCurvag != "") location.vags.(sCurvag).rich = true;
			if(save) location.vag.rich = true;
			// NK <--
			//cccOA1-> Vagabond changed to rich armed traveller
            gunProb *= 1.5; // Swindler 05-10-10: changed the logic of making vag to a rich armed traveller
            //TakeItemFromCharacter(chr, "blade5");
			//LAi_NPC_Equip(chr, sti(chr.rank), true, frnd()*1.5);	//ccc, NK
			/*if(!CheckAttribute(chr,"equip.blade") || chr.equip.blade == "error")
			{
				GiveItem2Character(chr, BLADE_SABER);
				EquipCharacterByItem(chr, BLADE_SABER);
			}*/
			//if(gun == "")
			/*so not for women
			if(!CheckAttribute(chr,"equip.gun"))
			{
				GiveItem2Character(chr, "pistol3");
				EquipCharacterByItem(chr, "pistol3");
			}*/
			int vagamoney = Rand(1000);			//ccc
			int vagahp = vagamoney/4+ Rand(200);		//ccc
			chr.money = vagamoney;			//ccc
			LAi_SetHP(chr, vagahp, vagahp);		//ccc
			if(vagamoney > 250) chr.perks.list.CriticalHit = "1";	//ccc
			if(vagamoney > 550) chr.perks.list.Gunman = "1";	//ccc
			if(vagamoney > 750) chr.perks.list.SwordplayProfessional = "1";	//ccc
			if(CheckAttribute(mchr, "skill.Fencing"))		//ccc
			{
			chr.skill.Fencing = sti(mchr.skill.Fencing) + Rand(3);	//cccOA1<-		
			}
		} // NK
		// unneeded - if(!CheckAttribute(chr,"rich") && sCurvag == "") chr.sex = LEnc_MonstersLoginObject.current.sex; // NK
		chr.dialog.CurrentNode = "First time";
		//chr.dialog.filename = "Enc_Walker.c";
		// ->ccc10.2 vaga dialog according to location for the time being
		if(location.type == "house" && CheckAttribute(location,"lastentermonth"))
		{
			chr.dialog.filename = "Enc_Resident_dialog.c";
			if(CheckAttribute(location,"vag.meeting"))	//ccc10.2 "," was missing
			{
				if(sti(location.vag.meeting))
				{
					if(CheckAttribute(location,"friendquest")) {chr.dialog.CurrentNode = "friendquest";} // apr05 friend status
					else {chr.dialog.CurrentNode = "second time";}	// apr05 friend status
				}
			}
		}
		else
        {
            chr.dialog.filename = "Enc_Walker.c";
        }
		
		if(chr.sex == "woman")	//NK was LEnc_MonstersLoginObject.current.sex // cccOA1
		{
			chr.perks.list.CriticalHit = "1";			//cccOA1
			chr.perks.list.SwordplayProfessional = "1";	//cccOA1
			GiveItem2Character(chr, "jewelry"+ makeint(1 + rand(15)) );	// jun05 booty for stealing

		}

		switch(label)	// ccc specials for locationspecific encounters
		{
			case "Bedroom":
				chr.dialog.filename = "Enc_Bedroom_dialog.c";
			 GiveItem2Character(chr, "jewelry"+ makeint(1 + rand(15)) );	// jun05 booty for stealing

			break;
	
			case "Tradepost":
				chr.dialog.filename = "Enc_Tradepost.c";
			 GiveItem2Character(chr, "mineral"+ makeint(1 + rand(3)) );	// jun05 booty for stealing

			break;
	
			case "MILITARY AREA. NO TRESPASSING!":
				chr.dialog.filename = "Enc_Fort_dialog.c";	// ccc dialog for forts
				LAi_group_MoveCharacter(chr, "fort");
				LAi_group_SetRelation(LAI_GROUP_PLAYER, "fort", LAI_GROUP_NEITRAL);
				LAi_group_SetRelation(LAI_GROUP_GUARDS, "fort", LAI_GROUP_NEITRAL);
				RemoveCharacterEquip(chr, GUN_ITEM_TYPE);
				RemoveCharacterEquip(chr, BLADE_ITEM_TYPE);
			GiveItem2Character(chr, "potion"+ makeint(1 + rand(3)) );	// jun05 booty for stealing

			break;
	
			case "inn":
				chr.dialog.filename = "Enc_Inn.c";
			GiveItem2Character(chr, "potion"+ makeint(1 + rand(3)) );	// jun05 booty for stealing

			break;

		}

		chr.dialog.TempNode = chr.Dialog.CurrentNode;
		
		//<-ccc10.2
		//cccOA1 RemoveCharacterEquip(chr, BLADE_ITEM_TYPE);
		// NK -->
		/*unneeded, in createfantom for setrandom, in lai_monst for restore old
		if(!CheckAttribute(chr,"rich"))
		{
			SetRandomNameToCharacter(chr);
			if(sCurvag != "")
			{
				location.vags.(sCurvag).name = chr.name;
				if(sCurvag == "0") location.vags.lastname = chr.lastname; //so last name only
			}
		}*/
		// NK <--
		break;
	}
	if(LEnc_MonstersLoginObject.type == "inca_temple")
	{
		float maxhp = LAi_GetCharacterMaxHP(chr);
		maxhp = maxhp*0.5;
		LAi_SetHP(chr, maxhp, maxhp);
	}
	// --> Swindler 05-10-10: changed, unnecessary when we set "hasBlade" and "gunProb" first
    //EquipCharacterByItem(&chr, FindCharacterItemByGroup(&chr, BLADE_ITEM_TYPE));
	//EquipCharacterByItem(&chr, FindCharacterItemByGroup(&chr, GUN_ITEM_TYPE));
	// Swindler: instead use that call:
    LAi_NPC_Equip(chr, sti(chr.rank), hasBlade, gunProb);
    // Swindler <--        
    EquipCharacterByItem(&chr, FindCharacterItemByGroup(&chr, ARMOR_ITEM_TYPE)); // armor too
	
	// Trace("LEnc_MonstersLoginCorrectParams end");
}

//Завершить загрузку монстров в локацию
void LEnc_MonstersLoginEnd(ref location)
{
	//LAi_QuestDelay("NK_FightMonstersEvent", 500); // NK tempfix 04-16
	DeleteAttribute(location,"savethesevags"); // NK
}


