void main() { //object oPC = GetEnteringObject(); object oPC = GetClickingObject(); if (GetIsPC(oPC) != TRUE) return; //:: Get own ID number string sSelf = GetTag(OBJECT_SELF); int iLength = GetStringLength(sSelf); string sID; int iChosen = 0; switch (iLength) { case 6: sID = GetStringRight(sSelf,1); break; case 7: sID = GetStringRight(sSelf,2); break; case 8: sID = GetStringRight(sSelf,3); break; default: SendMessageToPC(oPC, "Valid door tag not found."); sID = ""; // Initialize to empty string for unexpected lengths break; } //SendMessageToPC(oPC,"my area ID is "+sID); int iMatch = GetLocalInt(OBJECT_SELF,"match"); //:: Current door's match string sEntryDir = GetStringLeft(GetTag(OBJECT_SELF),1); //:: Current door's direction string sExitDir; //:: This inverts the directions if (sEntryDir == "n") sExitDir="s"; else if (sEntryDir == "s") sExitDir="n"; else if (sEntryDir == "w") sExitDir="e"; else if (sEntryDir == "e") sExitDir="w"; //SendMessageToPC(oPC,"i should be on the "+sExitDir+" side of the map"); //:: Now determine if there is already a match if (iMatch != 0) //:: Door already has a match, teleport PC { //SendMessageToPC(oPC,"door has a match"); string sTag = "area4_"+IntToString(iMatch); object oArea = GetObjectByTag(sTag); object oTarget = GetObjectByTag("wp4_"+IntToString(iMatch)+sExitDir); location lTarget = GetLocation(oTarget); float fFace = GetFacing(oPC); AssignCommand(oPC,ActionJumpToLocation(lTarget)); AssignCommand(oPC,SetFacing(fFace)); iChosen = 1; return; } int iRand = Random(41)+1; //:: Random # for area selection string sTag = "area4_"+IntToString(iRand); object oArea = GetObjectByTag(sTag); int iUsed = GetLocalInt(oArea,"ispop_"+sExitDir); float fFace = GetFacing(oPC); int iRetryCount = 0; // Extract current area ID before the while loop string sCurrentAreaTag = GetTag(GetArea(OBJECT_SELF)); int iCurrentAreaID = StringToInt(GetStringRight(sCurrentAreaTag, GetStringLength(sCurrentAreaTag) - 6)); while (iChosen == 0)//selecting new areas { iRand = Random(41)+1; // Skip if this is the current area if (iRand == iCurrentAreaID) continue; sTag = "area4_"+IntToString(iRand); oArea = GetObjectByTag(sTag); iUsed = GetLocalInt(oArea,"ispop_"+sExitDir); if (iUsed == 0) { // SendMessageToPC(oPC,"unused area chosen"); object oTarget = GetObjectByTag("wp4_"+IntToString(iRand)+sExitDir); location lTarget = GetLocation(oTarget); AssignCommand(oPC,ActionJumpToLocation(lTarget)); AssignCommand(oPC,SetFacing(fFace)); SetLocalInt(oArea,"ispop_"+sExitDir,1); object oDoor = GetObjectByTag(sExitDir+"door"+IntToString(iRand)); SetLocalInt(OBJECT_SELF,"match",iRand); SetLocalInt(oDoor,"match",StringToInt(sID)); iChosen = 1; break; } // Fallback: reset empty areas if we've tried too many times iRetryCount++; if (iRetryCount > 50) { int iResetCount = 0; // Check each area before resetting int i; for (i = 1; i <= 41; i++) { object oCheckArea = GetObjectByTag("area4_"+IntToString(i)); if (GetIsObjectValid(oCheckArea)) { // Check if all four directions are used int bNorthUsed = GetLocalInt(oCheckArea, "ispop_n"); int bSouthUsed = GetLocalInt(oCheckArea, "ispop_s"); int bEastUsed = GetLocalInt(oCheckArea, "ispop_e"); int bWestUsed = GetLocalInt(oCheckArea, "ispop_w"); // Only reset if all directions are used AND no players present if (bNorthUsed && bSouthUsed && bEastUsed && bWestUsed) { // Check if area has players (using same logic as area_onexit.nss) object oCheck = GetFirstObjectInArea(oCheckArea); int bHasPlayers = FALSE; while (oCheck != OBJECT_INVALID) { if (GetIsPC(oCheck)) { bHasPlayers = TRUE; break; } oCheck = GetNextObjectInArea(oCheckArea); } // Only reset if no players present if (!bHasPlayers) { // Reset all four direction flags SetLocalInt(oCheckArea, "ispop_n", 0); SetLocalInt(oCheckArea, "ispop_s", 0); SetLocalInt(oCheckArea, "ispop_e", 0); SetLocalInt(oCheckArea, "ispop_w", 0); iResetCount++; } } } } WriteTimestampedLogEntry("door_click: Reset " + IntToString(iResetCount) + " fully exhausted areas"); iRetryCount = 0; } } }