Author Topic: Random number generator inside While loop  (Read 1349 times)

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Random number generator inside While loop
« Reply #15 on: July 31, 2010, 02:43:23 am »


               

Ivanovich wrote...


I stayed away from the GetFirstObjectInArea because of the amount of objects in the area. It might be a cumbersome script on the server.

*************

Here's an additional question. On Exiting the area, I've got this:

object oPC = GetExitingObject();
object oCenter = GetNearestObjectByTag("guardbeacon", oPC);

but oCenter is always blank, as if it's not picked up. The item "guardbeacon" is in the area the PC just exited. But it seems that if I put this script in the OnExit part of the area, it never finds the "guardbeacon" with this function. Thoughts?

I also tried it with

object oCenter = GetNearestObjectByTag("guardbeacon", OBJECT_SELF);

But the area doesn't get it either.


The responce to bout of your last posts are linked.

Once the PC leaves the Area he is no longer in an area. His location in Invalid therefore you can not find any objects that are close to him. Or even get a location for him. You would be best to use GetFirstObjectInArea/GetNextObject in area.

420 is also correct. If you only have 4 waypoints and you are wanting a loop to get all of them you are better off with the GetFirst/GetLast functions. These functions have the advantage if being iterators. This means that every time you call the GetNext function it does not have to start over from scratch and can just start the search back up where the previous function lest off.
The Get Nearest functions are not iterators so every time the function is called it has no ground work laid for it. It will search through every object in the area. then have to calculate which one is closer. There is a lot more over head to this function, It probable even calls the GetFirstObjectInArea/Next functions every tine you use it. unless you actually need the closest x number of objects, you are better off looping through the Objects one time as opposed to x times. without all the distance calculations also.
               
               

               


                     Modifié par Lightfoot8, 31 juillet 2010 - 01:44 .
                     
                  


            

Legacy_Ivanovich

  • Full Member
  • ***
  • Posts: 142
  • Karma: +0/-0
Random number generator inside While loop
« Reply #16 on: August 01, 2010, 10:28:35 pm »


               Excellent.  Got it to work with the "First Object in Area" function.

Your help was sincerely appreciated.  Cheers.
               
               

               


                     Modifié par Ivanovich, 01 août 2010 - 09:33 .
                     
                  


            

Legacy_Ivanovich

  • Full Member
  • ***
  • Posts: 142
  • Karma: +0/-0
Random number generator inside While loop
« Reply #17 on: August 01, 2010, 10:34:13 pm »


               Whoops, works now.  Had too much data inside the if statement.  



Thanks for all your help.

               
               

               
            

Legacy_Ivanovich

  • Full Member
  • ***
  • Posts: 142
  • Karma: +0/-0
Random number generator inside While loop
« Reply #18 on: August 02, 2010, 05:58:04 pm »


               Ok, so here's adding a little complexity into the issue.  Now that the script works, and it removes the guards once the PC leaves the area, what is the best way to assure the script only runs when there are no more PCs in the area?  For example, if a PC leaves the area, but other PCs are there, the guards remain.  If not, and the zone is empty of all PCs, the script executes and the guards in the zone are removed.



Would it be to cycle through all objects with the GetFirst/NextInArea function again?  Or is there a simpler way?
               
               

               
            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
Random number generator inside While loop
« Reply #19 on: August 02, 2010, 09:11:35 pm »


               This is one possible way to do this.

int GetNumPCsInArea(object oArea)
{
 if(!GetIsObjectValid(oArea) || GetArea(oArea) != oArea)
 {
 return 0;
 }
object oPC = GetFirstPC();
int nCount;
 while(GetIsObjectValid(oPC))
 {
  if(GetArea(oPC) == oArea)
  {
  nCount++;
  }
 oPC = GetNextPC();
 }
return nCount;
}

Just place this function at top of your script and then in your script use:

if(GetNumPCsInArea(oArea) == 0)

Another method using GetNearestCreature, and that method is more efficient, but when we are speaking about such small codes, it don't matter really.

BTW you might consider to postphone the cleaning area to 2minutes after last PC exit area. Quite often, some player enters area again until that, so cleaning could be quite ineffective. (If you delay cleaning, just must then re-check PC count in area!)

Also, good idea is to reset all encounters when you clean monsters, to ensure players won't exploit cleaning area in order to get safely through area to lower floor.
               
               

               


                     Modifié par ShaDoOoW, 02 août 2010 - 08:14 .
                     
                  


            

Legacy_Ivanovich

  • Full Member
  • ***
  • Posts: 142
  • Karma: +0/-0
Random number generator inside While loop
« Reply #20 on: August 02, 2010, 09:32:24 pm »


               Thanks, m8.  Not going to do this with monsters, only guards in the cities.  How do you suggest waiting the 2 minutes?  Just with the DelayCommand function?
               
               

               
            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
Random number generator inside While loop
« Reply #21 on: August 02, 2010, 09:41:43 pm »


               This might give you idea.

void CleanArea()
{
object oArea = OBJECT_SELF;
 if(GetNumPCsInArea(oArea) > 0)
 {
 return;
 }
SetLocalInt(oArea,"CLEANED",1);//im checking this when someone enter area to know if I should spawn npcs
object oFirst = GetFirstObjectInArea(oArea);
int nTh = 1;
object oNPC = GetObjectType(oFirst) == OBJECT_TYPE_CREATURE ? oFirst : GetNearestObject(OBJECT_TYPE_CREATURE,oFirst,nTh++);
 while(GetIsObjectValid(oNPC))
 {
  if(GetLocalInt(oNPC,"CLEANME") != -1)
  {
  DestroyObject(oNPC);
  }
 oNPC = GetNearestObject(OBJECT_TYPE_CREATURE,oFirst,nTh++);
 }
}

void main()
{
object oArea = OBJECT_SELF;
 if(GetNumPCsInArea(oArea) > 0)
 {
 return;
 }
DelayCommand(120.0,CleanArea());
}


               
               

               


                     Modifié par ShaDoOoW, 02 août 2010 - 08:43 .
                     
                  


            

Legacy_Ivanovich

  • Full Member
  • ***
  • Posts: 142
  • Karma: +0/-0
Random number generator inside While loop
« Reply #22 on: August 03, 2010, 12:32:13 pm »


               Great, so the DelayCommand.  Ok, thanks, mate.  And thanks to the others that assisted with this thread!
               
               

               
            

Legacy_Ivanovich

  • Full Member
  • ***
  • Posts: 142
  • Karma: +0/-0
Random number generator inside While loop
« Reply #23 on: August 04, 2010, 01:07:15 pm »


               Here's a question for anyone still reading... '<img'>

When you script the "GetItemActivatedTargetLocation" function, and you've created an item to use a unique power with the same name as used in the script, the process goes as such:

1.  You use the item.
2.  You target something.
3.  If the "something" is greater than a pre-defined distance away, you first MOVE towards that something before your next action.

Is there a way to change that predefined distance to longer?  Ie, if I target something on the other side of the screen, can I make it accept the target without moving half the way there?  Or is that hardcoded into the engine?  

Thx!
               
               

               


                     Modifié par Ivanovich, 04 août 2010 - 12:07 .
                     
                  


            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Random number generator inside While loop
« Reply #24 on: August 05, 2010, 09:51:39 pm »


               Here is the relevelent information from spells.2da.

 
2DA V2.0
      
         Label                                Range       Name    TargetType  
386   ACTIVATE_ITEM                   S            6802      0x7f                       
413   ACTIVATE_ITEM_SELF          S            6802      0x01                       
428   ACTIVATE_ITEM_SELF2        S           6802       0x09                       
697   ACTIVATE_ITEM_L                L            83622     0x7f                       
795   ACTIVATE_ITEM_T               T           86781      0x7f                       

 

The range collunm can have values of:


Range of spell
P = personal
T = touch
S = short
M = medium
L = long

Target types are as follows.
self = 0x01
creature = 0x02
area = 0x04
items = 0x08
door = 0x10
placeable = 0x20
trigger = 0x40
               
               

               


                     Modifié par Lightfoot8, 05 août 2010 - 08:56 .
                     
                  


            

Legacy_Ivanovich

  • Full Member
  • ***
  • Posts: 142
  • Karma: +0/-0
Random number generator inside While loop
« Reply #25 on: August 14, 2010, 03:41:23 pm »


               Ok, but I'm still not sure how to make an item activate using the Long entry instead of the default one.  Appreciate the table, though.