Author Topic: Need help with a script....  (Read 397 times)

Legacy_datamaster

  • Jr. Member
  • **
  • Posts: 94
  • Karma: +0/-0
Need help with a script....
« on: June 12, 2011, 11:01:45 pm »


               I am working on a module where I use an Overland Travel System I got from the vault(I changed the map for my module).  Underneath the map, at the locations of the cities there are Area Transition triggers to take you to those cities.

I am trying to have it where each city randomly picks from a number of cities.  It spawns a flag for the city it picked right there on the map and sets that trigger to go to a waypoint in the city(one way).
Once a city is used it is not possible for another city trigger to go to the same city.
I have ints set on each of the waypoints at 0 and when it goes to that city, it sets it to 1.
This way it checks that int and only uses it if it is a 0.

This is my current script(it only somewhat works):
#include "X0_I0_TRANSPORT"
void main()
{
    object oClicker=GetClickingObject();
    object oStart=OBJECT_SELF;
    object oDest=GetLocalObject(oStart, "TRANS_TARGET");
    int rand;
    object oTown1=GetWaypointByTag("WT_TOWN01");
    object oTown2=GetWaypointByTag("WT_TOWN02");
    object oTown3=GetWaypointByTag("WT_TOWN03");
    object oTown4=GetWaypointByTag("WT_TOWN04");
    object oTown5=GetWaypointByTag("WT_TOWN05");
    object oTown6=GetWaypointByTag("WT_TOWN06");
    object oTown7=GetWaypointByTag("WT_TOWN07");
    int oUsed1=GetLocalInt(oTown1, "used");
    int oUsed2=GetLocalInt(oTown2, "used");
    int oUsed3=GetLocalInt(oTown3, "used");
    int oUsed4=GetLocalInt(oTown4, "used");
    int oUsed5=GetLocalInt(oTown5, "used");
    int oUsed6=GetLocalInt(oTown6, "used");
    int oUsed7=GetLocalInt(oTown7, "used");
    int oLoop=0;
    object oTarget;
    object oSpawn;
    location lTarget;
    oTarget = oClicker;
    if (!GetIsPC(oClicker)) return;
    if (oDest == OBJECT_INVALID)
   {
        while (oLoop < 7)
        {
            rand=Random(7);
            if (rand == 1)
            {
                if (oUsed1 == 0)
                {
                    SetLocalObject(oStart, "TRANS_TARGET", oTown1);
                    lTarget = GetLocation(oTarget);
                    oSpawn = CreateObject(OBJECT_TYPE_PLACEABLE, "town1flag", lTarget);
                    SetLocalInt(oTown1, "used", 1);
                }
                else return;
            }
            else if (rand == 2)
            {
                if (oUsed2 == 0)
                {
                SetLocalObject(oStart, "TRANS_TARGET", oTown2);
                lTarget = GetLocation(oTarget);
                oSpawn = CreateObject(OBJECT_TYPE_PLACEABLE, "town2flag", lTarget);
                SetLocalInt(oTown2, "used", 1);
                }
                else return;
            }
            else if (rand == 3)
            {
                if (oUsed3 == 0)
                {
                SetLocalObject(oStart, "TRANS_TARGET", oTown3);
                lTarget = GetLocation(oTarget);
                oSpawn = CreateObject(OBJECT_TYPE_PLACEABLE, "town3flag", lTarget);
                SetLocalInt(oTown3, "used", 1);
                }
                else return;
            }
            else if (rand == 4)
            {
                if (oUsed4 == 0)
                {
                SetLocalObject(oStart, "TRANS_TARGET", oTown4);
                lTarget = GetLocation(oTarget);
                oSpawn = CreateObject(OBJECT_TYPE_PLACEABLE, "town4flag", lTarget);
                SetLocalInt(oTown4, "used", 1);
                }
                else return;
            }
            else if (rand == 5)
            {
                if (oUsed5 == 0)
                {
                SetLocalObject(oStart, "TRANS_TARGET", oTown5);
                lTarget = GetLocation(oTarget);
                oSpawn = CreateObject(OBJECT_TYPE_PLACEABLE, "town5flag", lTarget);
                SetLocalInt(oTown5, "used", 1);
                }
                else return;
            }
            else if (rand == 6)
            {
                if (oUsed6 == 0)
                {
                SetLocalObject(oStart, "TRANS_TARGET", oTown6);
                lTarget = GetLocation(oTarget);
                oSpawn = CreateObject(OBJECT_TYPE_PLACEABLE, "town6flag", lTarget);
                SetLocalInt(oTown6, "used", 1);
                }
                else return;
            }
            else
            {
            SetLocalObject(oStart, "TRANS_TARGET", oTown7);
            lTarget = GetLocation(oTarget);
            oSpawn = CreateObject(OBJECT_TYPE_PLACEABLE, "town7flag", lTarget);
            SetLocalInt(oTown7, "used", 1);
            }
        oLoop++;
        }
    }
  float fFacing = GetFacing(oDest)+180.0;
  vector vPos =GetPosition(oDest)+ AngleToVector(fFacing);
  location lLoc = Location(GetArea(oDest),vPos,fFacing);
TransportToLocation(oClicker,lLoc);
}

I am not sure if I need the while loop or not.
I am wondering if it can randomly pick a number and check that city, if it is used already, then go on to another number and check it's city(etc.).

Each city has one way triggers that all bring the PC up to the map, all at the same waypoint.  There is no problem there. I have this in the OnClick for the triggers.

Any thoughts?  This is kind of like another topic I had about random doors, but with it being one way from triggers to waypoints and my not having waypoint names useful(I think) to the solution there(plus I am using those destination waypoints as the destination from another area that is not random) I thought I would ask here.
               
               

               
            

Legacy_Failed.Bard

  • Hero Member
  • *****
  • Posts: 1409
  • Karma: +0/-0
Need help with a script....
« Reply #1 on: June 13, 2011, 12:45:33 am »


               #include "X0_I0_TRANSPORT"
void main()
{
    object oTarget = GetClickingObject();
    if (!GetIsPC(oTarget)) return;
    object oStart  = OBJECT_SELF;
    object oDest   = GetLocalObject(oStart, "TRANS_TARGET");
    string sResRef;
    object oTown, oSpawn;
    int    nUsed, nLoop, nRnd;
    location lTarget;
    if (oDest == OBJECT_INVALID)
   {
        nRnd  = Random(7) + 1;
        while (nLoop < 7)
        {
            oTown = GetWaypointByTag("WT_TOWN0" + IntToString (nRnd));
            nUsed = GetLocalInt (oTown, "used");
            if (!nUsed)
                {
                    SetLocalObject(oStart, "TRANS_TARGET", oTown);
                    lTarget = GetLocation(oTarget);
                    sResRef = "town" + IntToString (nRnd) + "flag";
                    oSpawn = CreateObject(OBJECT_TYPE_PLACEABLE, sResRef, lTarget);
                    SetLocalInt(oTown, "used", 1);
                    nLoop = 7;
                }
        nRnd ++;
        if (nRnd > 7) nRnd = 1;
        nLoop++;
        }
    }
  float fFacing = GetFacing(oDest)+180.0;
  vector vPos  =GetPosition(oDest)+ AngleToVector(fFacing);
  location lLoc = Location(GetArea(oDest),vPos,fFacing);
 TransportToLocation(oTarget,lLoc);
}


I played around a bit with the structure of your script, but the main part would be moving the random outside the while loop, and then incrementing it on each nUsed == TRUE it comes across.  That way, you get a random first destination checked, but will still check each possible destination within the while loop.
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Need help with a script....
« Reply #2 on: June 13, 2011, 01:45:50 am »


               Why not just use the function I gave you in the other thread.   I wrote it so you could use it in more them one place.

http://social.biowar...7426752#7430378
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Need help with a script....
« Reply #3 on: June 13, 2011, 04:12:19 am »


               Ok I modified the _randomtags include a little bit.  It should still be backwards compatable with the old version.  It is now just a little bit more flexable.

// The Upper 26 bits holds information about which tags have already
// selected.  This function has to be called before GetRandomTagOnce is called.
// a second call to this function will reset the TagSet to where all
// Tags can be selected again.
// Modified 6-12-11: if nNum of tags is left unset the function will count the
// number of valid tags.
int InitRandomTag(string sTagPrefix, int nNumTags=0);

// Return a Tag from the set only once.
string GetRandomTagOnceOnly(string sTagPrefix);


//Gets the number of Tags still Avaliable in the set.
// Returns -1 if the set is exhausted.
int GetNumOfAvailableTags(string sTagPrefix);


/////////////////Definitions ////////////////////////////////


int InitRandomTag(string sTagPrefix, int nNumTags=0)
{
 int nInit;


 if (nNumTags ==0)
 {
   //count the number of Tags
   while(GetIsObjectValid(GetObjectByTag(sTagPrefix+IntToString(nNumTags+1)))) nNumTags++;
 }


 if (nNumTags > 27) nNumTags = 27;
 if (GetStringLength(sTagPrefix) >30) sTagPrefix = GetStringRight(sTagPrefix,30);
 if (nNumTags > 0)
 {
   nInit = (1<<nNumTags)-1;
   nInit = (nInit<<5)| nNumTags;
 }

 SetLocalInt(GetModule(),sTagPrefix,nInit);
 // If this function faild the return value will be 0 or FALSE
 return nNumTags;
}
 

string GetRandomTagOnceOnly(string sTagPrefix)
{
   string sSubFix;
   int nCount, nRnd ,nPos;
   int nTagData = GetLocalInt(GetModule(),sTagPrefix);
   if (nTagData == 0 || nTagData == -1) return "";

   nRnd = Random(nTagData & 0x1F)+1;
   nPos = 4;
   while(nCount < nRnd)
   {
     nPos++;
     if (nTagData & ( 1 << nPos)) nCount++;
   }
   nTagData = (nTagData ^ (1<<nPos)) -1;
   if ( nTagData == 0)nTagData = -1;
   SetLocalInt(GetModule(),sTagPrefix,nTagData);

   nPos = nPos -4;
   sSubFix = IntToString(nPos);

   // Can no longer be uncommented since update to init function.
   // Without also modifing the other function.
   //if (nPos <10) sSubFix = "0" + sSubFix;

   return sTagPrefix + sSubFix;

}


int GetNumOfAvailableTags(string sTagPrefix)
{
   int nTagData = GetLocalInt(GetModule(),sTagPrefix);
   if (nTagData != -1) nTagData = nTagData & 0x1F;
   return nTagData;
}


with the above your script can look like this. 
Note: I changed it to use only one blueprint for the flag and just rename it on the fly.

#include "_randomtags"
#include "X0_I0_TRANSPORT"

void main()
{
   // since it is a trigger you might as well make them step in it.
   object oTravler=GetEnteringObject();
   object oFlag;
   string sName;

   if (!GetIsPC(oTravler)) return;
   object oDest=GetLocalObject(OBJECT_SELF, "TRANS_TARGET");

   //Check to see if we still need to set up a destnation for this trigger.
   if (oDest == OBJECT_INVALID)
   {
     //Check to see if the random tag has been initilized. If the number of tags
     // available is 0 it has not been.
     if (GetNumOfAvailableTags("WT_TOWN0")== 0) InitRandomTag("WT_TOWN0");

     // set oDest to one of the waypoints and store it on this trigger.
     oDest = GetWaypointByTag(GetRandomTagOnceOnly("WT_TOWN0"));
     SetLocalObject(OBJECT_SELF, "TRANS_TARGET", oDest);

     // Drop a flag for the town I am going to.
     oFlag = CreateObject(OBJECT_TYPE_PLACEABLE, "townflag", GetLocation(oTravler));

     // Set the Name of the tag to the area it is going to.
     sName = GetName(GetArea(oDest));
     SetName(oFlag,sName);

     // Set the description for the flag.
     SetDescription(oFlag,"This passage goes to "+sName);
   }

   // Since oDest is a way point, the extra code for the doors in the other
   // script is not needed.
   TransportToWaypoint(oTravler,oDest);

}


EDIT: OOps,  changed nNumTags  to nNumTags+1  in my While loop check in the include file. 
               
               

               


                     Modifié par Lightfoot8, 13 juin 2011 - 08:32 .
                     
                  


            

Legacy_datamaster

  • Jr. Member
  • **
  • Posts: 94
  • Karma: +0/-0
Need help with a script....
« Reply #4 on: June 13, 2011, 03:43:52 pm »


               Lightfoot,
I tried your modified script there.  It did nothing when I tested it.
To test it, I put it into the module but did not change the other script for the doors of yours that was in there.
I just set your new one to "_randomtags1" , and in the script to go with it, I told it to include "_randomtags1"
For some reason, it still wouldn't work when tested though.

Failed.Bard,
Yours worked but had a couple of problems.
1) It did not place the flag.
2) It took 2-3 clicks of the trigger before it would work.
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Need help with a script....
« Reply #5 on: June 13, 2011, 04:37:14 pm »


               Ok, Right after the line.

  if (GetNumOfAvailableTags("WT_TOWN0")== 0) InitRandomTag("WT_TOWN0");

Place this line.

SentMessageToPC(oTravler,"There are "+IntToString(GetNumOfAvailableTags("WT_TOWN0"))+" Destnations Left" );

Recompile and test.  If it Reports back that there are 0 tags left There is most likely a tag problem.  

Double check the waypoint tags make sure that they are "WT_TOWN0" with that last digit being a zero.   OR rename the prefix to match your tags.

If that is not it ill do some testing when I get back to the house.
               
               

               
            

Legacy_datamaster

  • Jr. Member
  • **
  • Posts: 94
  • Karma: +0/-0
Need help with a script....
« Reply #6 on: June 13, 2011, 05:31:45 pm »


               Ok I added that line Light(fixing the typo you had, it is SendMessageToPC).
It tells me There are 0 destinations left.
I did have to fix the waypoint tags, but even after I did, it still tells me There are 0 destinations left.
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Need help with a script....
« Reply #7 on: June 13, 2011, 05:39:57 pm »


               Ok,  I double check everything later tonight.  
And Yes,  I am the typo King.  '<img'>
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Need help with a script....
« Reply #8 on: June 13, 2011, 09:38:46 pm »


               While driving home I was thinking about the code. That is when it came to me,  the Porblem is in the modified code in the include file.   I was checking for the wrong tag subfix in the  While loop.  Edited, it should work now.   

I really need to get back to testing everything before I post it. 'Image
L8   
               
               

               
            

Legacy_datamaster

  • Jr. Member
  • **
  • Posts: 94
  • Karma: +0/-0
Need help with a script....
« Reply #9 on: June 13, 2011, 10:24:24 pm »


               Ok making that change to the while loop did not fix anything(that can be seen while testing).
It still says There are 0 destinations left.
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Need help with a script....
« Reply #10 on: June 14, 2011, 01:09:15 am »


               Ok I just tested it and it worked for me. 

Here is how my test went.  

1 - I created a new trigger and placed the script in the OnEnter Event.  

2 - I painted   three of the triggers down.

3 - I created three generic waypoints and gave them the tags:
     WT_TOWN01
     WT_TOWN02
     WT_TOWN03

4 I created a Flag Placeable with the ResRef : townflag  // I forgot to make it nonstatic and useable.

5  I fixed my error in the include file. The one posted earlier, and saved the include.  // changes to Include files 
    have to be saved before the files that use them are recompiled.  

6  I recompiled the script. 

7 I tested the module and everything worked fine.  Except I could not read the flags because they where static. 

8 I looked a how big if a pain it would be to use a different tag, and modified the script to use a constent that can be changed at the top. Instead of having to change every where the tag Prefix was used in the script.   


#include "_randomtags"
#include "X0_I0_TRANSPORT"

const string RND_WAYPOINT = "WT_TOWN0";
void main()
{
   // since it is a trigger you might as well make them step in it.
   object oTravler=GetEnteringObject();
   object oFlag;
   string sName;

   if (!GetIsPC(oTravler)) return;
   object oDest=GetLocalObject(OBJECT_SELF, "TRANS_TARGET");

   //Check to see if we still need to set up a destnation for this trigger.
   if (oDest == OBJECT_INVALID)
   {
     //Check to see if the random tag has been initilized. If the number of tags
     // available is 0 it has not been.
     if (GetNumOfAvailableTags(RND_WAYPOINT)== 0) InitRandomTag(RND_WAYPOINT);

     SendMessageToPC(oTravler,"There are "+IntToString(GetNumOfAvailableTags("WT_TOWN0"))+" Destnations Left" );



     // set oDest to one of the waypoints and store it on this trigger.
     oDest = GetWaypointByTag(GetRandomTagOnceOnly(RND_WAYPOINT));
     SetLocalObject(OBJECT_SELF, "TRANS_TARGET", oDest);

     // Drop a flag for the town I am going to.
     oFlag = CreateObject(OBJECT_TYPE_PLACEABLE, "townflag", GetLocation(oTravler));

     // Set the Name of the tag to the area it is going to.
     sName = GetName(GetArea(oDest));
     SetName(oFlag,sName);

     // Set the description for the flag.
     SetDescription(oFlag,"This passage goes to "+sName);
   }

   // Since oDest is a way point, the extra code for the doors in the other
   // script is not needed.
   TransportToWaypoint(oTravler,oDest);
}

  

Give it another try and if you still can not get it to work, Mail if to me so i can see what is going on.  Just make sure you let me know you mailed it.  **I only check my mail box when i know something is comming.
               
               

               
            

Legacy_datamaster

  • Jr. Member
  • **
  • Posts: 94
  • Karma: +0/-0
Need help with a script....
« Reply #11 on: June 14, 2011, 06:32:43 am »


               Ok, Lightfoot,
That worked perfect.
Everything else I have planned for the module is on the simple repetitve side rather than needing some unique scripting done(this can change though).
So hopefully in the not too distant future I will be able to finish what I have planned for it and post it to let others take a look at it on the Vault.
Others have made premade cities and things like that, this is basically a premade continent.  It has a set location in the continent for each town/city, but until the PC goes there, he doesn't know which city is in that spot.

I will let you know when it is finished and posted to the vault.
Thanks again for all the script help you have made.  Also thanks to everyone else who have offered ideas to test.
               
               

               
            

Legacy_datamaster

  • Jr. Member
  • **
  • Posts: 94
  • Karma: +0/-0
Need help with a script....
« Reply #12 on: June 16, 2011, 03:33:53 am »


               I posted it to the vault if anyone wants to check the mod this was mainly for.

http://nwvault.ign.c....Detail&id=1191