#include "events_inc"
void main()
{

object oPC = GetEnteringObject();

//A workaround to enable selecting this area in the OnClientLeave event, since GetArea returns OBJECT_INVALID there
SetLocalObject(oPC, "StoredArea", OBJECT_SELF);

if (!GetIsPC(oPC)) return;

if (GetLocalInt(oPC, "FromInnToVil") == TRUE)
    {
        DeleteLocalInt(oPC, "FromInnToVil");
        return;
    }

//Add to the counter of PCs in the area
SetLocalInt(OBJECT_SELF, "Players", GetLocalInt(OBJECT_SELF, "Players")+1);

//TEST
//FloatingTextStringOnCreature(IntToString(GetLocalInt(OBJECT_SELF, "Players")), oPC);

//First things first
EndEscortEvent(oPC);
ExploreAreaForPlayer(GetArea(oPC), oPC);

//A workaround to prevent the OnEnter event body from firing after loading a saved game
if (GetLocalInt(GetModule(), "LoadCooldown") == TRUE) return;

//Get the area we're in from the area variable
string sAreaString = GetLocalString(OBJECT_SELF, "AreaString");

//Let's set this area's pseudo OnExit event (as a workaround for players quitting the game), as well as the related inn's OnExit event
SetLocalString(OBJECT_SELF, "OnExit", "pseudo_vilonexit");
object oInnArea = GetArea(GetWaypointByTag("event_wp_"+sAreaString+"_1"));
SetLocalString(oInnArea, "OnExit", "pseudo_innonexit");

//Do nothing if there is already another PC in the area (and the area integer is TRUE)
if (GetLocalInt(OBJECT_SELF, "IsPopulated") == TRUE) return;

//Otherwise flag this area as populated, so that no event is generated for next players entering it
SetLocalInt(OBJECT_SELF, "IsPopulated", TRUE);

//Populate the area(s) with villagers
int nWpNumber;
string sWpTag;
string sRace = GetLocalString(OBJECT_SELF, "Race");
string sResRef;
location lWp;
int nMaxWp;
if (GetLocalInt(OBJECT_SELF, "Village") == TRUE) nMaxWp = 8;
else nMaxWp = 2;
{
    for (nWpNumber = 1; nWpNumber <= nMaxWp; nWpNumber++)
    {
        sWpTag = "rand_wp_"+sAreaString+"_"+IntToString(nWpNumber);
        if (Random(2) == 0) sResRef = "anc_npc_"+sRace+"_m";
        else sResRef = "anc_npc_"+sRace+"_f";
        lWp = GetLocation(GetWaypointByTag(sWpTag));
        object oNPC = CreateObject(OBJECT_TYPE_CREATURE, sResRef, lWp);
        SetLocalInt(oNPC, "Commoner", TRUE);
        SetLocalInt(oNPC, "Rumor", Random(3)+1);
    }
}

//Spawn the innkeeper
sResRef = "anc_npc_"+sRace+"_inn";
if (Random(2) == 0) sResRef = sResRef + "m";
else sResRef = sResRef + "f";
sWpTag = "event_wp_"+sAreaString+"_1";
lWp = GetLocation(GetWaypointByTag(sWpTag));
CreateObject(OBJECT_TYPE_CREATURE, sResRef, lWp);

//50% chances of spawning a travelling merchant in the inn
object oMerchant;
sResRef = GetLocalString(OBJECT_SELF, "MerchantResRef");
sWpTag = "event_wp_"+sAreaString+"_2";
lWp = GetLocation(GetWaypointByTag(sWpTag));
if (Random(2) == 0) oMerchant = CreateObject(OBJECT_TYPE_CREATURE, sResRef, lWp);
SetLocalInt(oMerchant, "Inn", TRUE);

//Spawn domesticated animals if appropriate
if (GetLocalInt(OBJECT_SELF, "Animals") == TRUE)
{
    switch (Random(3))
    {
        case 0: sResRef = "anc_pig"; break;
        case 1: sResRef = "anc_cow"; break;
        case 2: sResRef = "anc_chicken"; break;
    }
    for (nWpNumber = 9; nWpNumber <= 12; nWpNumber++)
    {
        sWpTag = "rand_wp_"+sAreaString+"_"+IntToString(nWpNumber);
        lWp = GetLocation(GetWaypointByTag(sWpTag));
        CreateObject(OBJECT_TYPE_CREATURE, sResRef, lWp);
    }
}

}