I have a set of areas I would like to connect randomly so that the order of the “floors” is different for each playthrough (like shuffling a deck of cards). Since I can’t use arrays, I thought I could do this, but I feel like I am overthinking this or making it more complex than it needs to be. Can someone have a look and maybe give me some tips?
Thanks!
Matt
onModuleLoad script:
void main()
{
int nFloors = 3; //number of desired floors in dungeon
int nTotalAreas; //total areas in module
object oPC = GetEnteringObject();
//a string made of numbers from 1 to nTotalAreas
string sAreaList;
//loop through all the areas (except area 0) in the module and create a string.
//Since each area uses the same tage format ("areaXXX", with "XXX" representing
// a number between 001 and 999) we only need the number
object oArea = GetFirstArea();
int i = 0;
while (oArea != OBJECT_INVALID)
{
// we can add filters later so that only dungeon areas are considered by
// changing the tag format and checking substrings
if (i == 0)
{
oArea = GetNextArea();
i++;
}
else
{
sAreaList = sAreaList + IntToString(i) + ",";
SendMessageToPC(oPC, "sAreaList is now " + sAreaList);
oArea = GetNextArea();
i++;
}
}
// save the string as a module variable
SetLocalString(OBJECT_SELF, "sAreaList", sAreaList);
SendMessageToPC(oPC, "sAreaList is now " + GetLocalString(OBJECT_SELF, "sAreaList"));
}
and then in the doors onTransition event:
#include "x3_inc_string"
void main()
{
object oPC = GetClickingObject();
string sDestination; //this door's destination
string sReturn; //for the return trip from sDestination
object oTarget;
object oReturn;
string sThisFloor = GetTag(GetArea(OBJECT_SELF));
SendMessageToPC(oPC, "sThisFloor is " + sThisFloor);
string sFloorNum = GetStringRight(sThisFloor, 3);
SendMessageToPC(oPC, "sFloorNum is " + sFloorNum);
// make sure this only runs once
if (GetLocalInt(OBJECT_SELF, "nInitDoor") == 1)
{
string sMsg = IntToString(GetLocalInt(OBJECT_SELF, "nInitDoor"));
SendMessageToPC(oPC, "The script has run once already! nInitDoor is " + sMsg);
oTarget = GetWaypointByTag(GetLocalString(OBJECT_SELF, "sDest"));
AssignCommand(oPC, JumpToObject(oTarget));
return;
}
else
{
//this should only allow valid choices based on the remaining numbers
string sTempString = GetLocalString(GetModule(), "sAreaList");
int j = Random(GetStringLength(sTempString)) + 1;
string sFloor = IntToString(j);
SendMessageToPC(oPC, "sFloor is " + sFloor);
sDestination = "wpAreaEntry" + "00" + sFloor;
SendMessageToPC(oPC, "sDestination is " + sDestination);
// set up sReturn
sReturn = "drAreaEntry" + "00" + sFloor;
SendMessageToPC(oPC, "sReturn is " + sReturn);
oReturn = GetObjectByTag(sReturn);
//remove nRandom from sAreaList
SendMessageToPC(oPC, "sTempString is " + sTempString);
int nRight = GetStringLength(sTempString);
SendMessageToPC(oPC, "nRight is " + IntToString(nRight));
int nLoc = FindSubString(sTempString, sFloor);
SendMessageToPC(oPC, "nLoc is " + IntToString(nLoc));
string sLeftString = GetStringLeft(sTempString, nLoc);
SendMessageToPC(oPC, "sLeftString is " + sLeftString);
string sRightString = GetStringRight(sTempString, nRight - (nLoc + 1));
SendMessageToPC(oPC, "sRightString is " + sRightString);
sTempString = sLeftString + sRightString;
SendMessageToPC(oPC, sTempString);
SendMessageToPC(oPC, "sTempString is now " + sTempString);
//update the module variable
SetLocalString(GetModule(), "sAreaList", sTempString);
SendMessageToPC(oPC, "sAreaList is now " + GetLocalString(GetModule(), "sAreaList"));
// set local int so this won't run twice
SetLocalInt(OBJECT_SELF, "nInitDoor", 1);
//set up transition
SetLocalString(OBJECT_SELF, "sDest", sDestination);
SendMessageToPC(oPC, "sDestination is " + sDestination);
oTarget = GetWaypointByTag(GetLocalString(OBJECT_SELF, "sDest"));
//set up return trip
string sWPName = "wpAreaExit" + sFloorNum;
SendMessageToPC(oPC, "sWPName is " + sWPName);
SetLocalString(oReturn, "sDest", sWPName);
SetLocalInt(oReturn,"nInitDoor", 1);
//move the player
AssignCommand(oPC, JumpToObject(oTarget));
}
}