www.ClassicTW.com
http://classictw.com/

Using while loops to validate a sector
http://classictw.com/viewtopic.php?f=15&t=33224
Page 1 of 1

Author:  Helix [ Mon May 21, 2012 11:02 pm ]
Post subject:  Using while loops to validate a sector

I want to use nested while loops to validate whether I should move to a sector, carry out and operation, then move to the next. Its actually 4 steps.

1. Generate a topdown list of sectors in the universe (however many there are: 30k, 20k, 5k) less sectors 1-10, STARDOCK, ALPHA_CENTARI, and RYLOS

2. Then I want to step through the resulting list using a while loop
Code:
#set the universe size
setVar $uni_size 20000
#read all the sectors to an array
readtoarray $sector __all_sector.txt
#set the index for the array $sector
setvar $i 1
setVar $s $Sector[$i]
#outside while loop, will run until the loop reach 20k
while ($s <= $uni_size)

    #if condition to remove sectors 1-10, SD, AC and Rylos
    if (($sector =< 10) or ($sector = STARDOCK) or ($sector = ALPHA_CENTAURI) or ($sector = RYLOS))
         add $i 1
    else
         #inside while loop, I want to write all sectors in the $sector array
         #less the sectors in the $my_sect array
         #read the list of my sectors into an array
         readtoarray $my_sect __my_sectors.txt
         #set the index for the array $my_sect
         setvar $r 1
         setVar $m $my_sect[$r]
         while ($s <> $m)
               add $r 1
         end
     #write sector to a file - Not sure of the code to do this
     write "sectors_left.txt" $s
     add $i 1
     end
end


H

Author:  T0yman [ Tue May 22, 2012 9:58 am ]
Post subject:  Re: Using while loops to validate a sector

I don't have time to really run it until I get home but here are a couple changes I use to simplify somethings:

using "SECTORS" makes the script useable in all games without modifying since it pulls from the TWX database.
Looks like you had it where it would have been adding a 1 to $i in two locations every loop meaning it could skip 3 or 4 sectors total, not a big deal but it might be one you needed.
I am not sure what you inputting using the text files so I am just gonna guess it is all sectors in the one?
the write "sectors_left.txt" $sector would need to be moved to the location in the script of where if it didn't make it to the desired sector?

Code:
#set the index for the array $sector
setVar $sector 11
#outside while loop, will run until the loop reach 20k
while ($sector <= SECTORS)
    #if condition to remove sectors 1-10, SD, AC and Rylos
    if (($sector = STARDOCK) or ($sector = ALPHA_CENTAURI) or ($sector = RYLOS))
         goto :skip
    else
         #inside while loop, I want to write all sectors in the $sector array
         #less the sectors in the $my_sect array
         #read the list of my sectors into an array
         readtoarray $my_sect __my_sectors.txt
         #set the index for the array $my_sect
         setvar $r 1
         setVar $m $my_sect[$r]
         while ($sector <> $m)
               add $r 1
         end
     #write sector to a file - Not sure of the code to do this
     write "sectors_left.txt" $sector
     :skip
     add $sector 1
     end
end

Author:  Helix [ Tue May 22, 2012 10:36 am ]
Post subject:  Re: Using while loops to validate a sector

That looks a lot cleaner than mine. I will give it a try. I try out the logic of it in my head and I get all twisted around.

The end result is to get a top down list of every sector in the universe minus, my bubble sectors, sectors 1-10 (starting at 11 was so simple but I couldnt see it) , SD, AC, and Rylos.

H

Author:  LoneStar [ Tue May 22, 2012 5:27 pm ]
Post subject:  Re: Using while loops to validate a sector

Couldn't help myself. Try This:
Code:
setvar $File1 "sectors_left.txt"
setvar $File2 "sectors_uFig.txt"
delete $File1
delete $File2
ReadToArray $my_sect "__my_sectors.txt"

SetVar $sector 11

While ($Sector <= SECTORS)
    If (($Sector <> STARDOCK) AND ($Sector <> ALPHA_CENTAURI) AND ($Sector <> RYLOS))
       SetVar $r 1
       While ($r <= $my_sect)
         if ($my_sect[$r] = $Sector)
            goto :_NEXT_
         end
         add $r 1
       End
       Write $File1 $Sector
       GetSectorparameter $Sector "FIGSEC" $F
       If ($F = 0)
         Write $File2 $Sector
       End
    End
    :_NEXT_
    Add $Sector 1
End



Borrowed Toymans routine. Hope thi sworks for you.

Author:  ElderProphet [ Tue May 22, 2012 8:24 pm ]
Post subject:  Re: Using while loops to validate a sector

Something you'll often need to do is create a "boolean array", meaning it is used as a true/false test. So in the above scenario, you could create a SECTOR-sized array called $skip, and then set the sectors 1-10, STARDOCK, RYLOS, AND ALPHACENTAURI to TRUE (or 1), as the default value is FALSE (or zero). Then you can use this as the one test for whether a sector should be processed or not. This one test of a static array value is very fast compared to multiple tests, but that is only important if you are doing something speed-sensitive. But even when speed isn't an issue, this approach is often cleaner and simpler, especially compared to several nested while loops / if statements.

Let me know if you need an example.

Author:  Helix [ Tue May 22, 2012 11:13 pm ]
Post subject:  Re: Using while loops to validate a sector

Elder, I do need an example. I also found that I must test for another condition: do I have a fig in the sector. Thank you for your input.

T0y, your loop seemed to go infinite. I am still looking into why. It pegged the CPU at 50%. LS yours worked and I got what I wanted out of the sequence but found I also need to test for a fig in the sector too.

I really want to say thanks to all of you for your help.

H

Author:  Vid Kid [ Wed May 23, 2012 4:00 am ]
Post subject:  Re: Using while loops to validate a sector

Toymans script had an error ..

the last 3 lines
Code:
     add $sector 1
     end
end


Should have been :
Code:
    end
    add $sector 1
end

Author:  LoneStar [ Wed May 23, 2012 6:10 am ]
Post subject:  Re: Using while loops to validate a sector

Vid Kid wrote:
Toymans script had an error ..

the last 3 lines
Code:
     add $sector 1
     end
end


Should have been :
Code:
    end
    add $sector 1
end


actually, the problem was here:
Code:
setvar $r 1
setVar $m $my_sect[$r]
while ($sector <> $m)
      add $r 1
end


$sector, at some point, would forever be <> to $m.

I'll wont steal EPs Thunder and post an example of what he is talking about. But I would like to point out that his idea is exactly like populating an array of deployed fighters for use in a Foton or PDrop script: if ($Fig[$Adj_HitSector] = TRUE) ...do attack. Or. In the case of excluding Bubble Sectors: if ($List[$Sector] = 0) ...write $Sector to file.

Author:  T0yman [ Wed May 23, 2012 6:25 am ]
Post subject:  Re: Using while loops to validate a sector

I will take the hit, but if you notice in the original I only changed the name not the original sequence :lol: But keep calling it my error I can handle it :)

Glad to help I wish I had the time to test it first but this is what happens shooting from the hip. heh

Author:  LoneStar [ Wed May 23, 2012 6:30 am ]
Post subject:  Re: Using while loops to validate a sector

T0yman wrote:
I will take the hit, but if you notice in the original I only changed the name not the original sequence :lol: But keep calling it my error I can handle it :)


Ha! Good show, ole sport :)

Author:  ElderProphet [ Wed May 23, 2012 1:07 pm ]
Post subject:  Re: Using while loops to validate a sector

So the conventional way to apply multiple conditional tests like you're after is as follows:
Code:
setVar $i 1
while ($i <= SECTORS)
   if ($i > 10)
      if ($i <> STARDOCK) and ($i <> RYLOS) and ($i <> ALPHACENTAURI)
         if (SECTOR.FIGS.OWNER[$i] <> "belong to your Corp") and (SECTOR.FIGS.OWNER[$i] <> "yours")
            gosub :doSomething
         end
      end
   end
   add $i 1
end
I even started at 1 so that we could do the > 10 conditional test. The boolean array idea would create a true/false array of sectors to skip. Since the default value of an array element is zero or FALSE, we'll make an array of sectors to skip, and just set desireable sectors to 1 or TRUE, such as 1-10, SD, Rylos, AC, etc., as follows:
Code:
setArray $skip SECTORS
setVar $i 1
while ($i <= SECTORS)
   if ($i < 11)
      setVar $skip[$i] TRUE
   end
   if ($i = STARDOCK) or ($i = RYLOS) or ($i = ALPHACENTAURI)
      setVar $skip[$i] TRUE
   end
   if (SECTOR.FIGS.OWNER[$i] = "belong to your Corp") or (SECTOR.FIGS.OWNER[$i] = "yours")
      setVar $skip[$i] TRUE
   end
   add $i 1
end
Now it should be trivial for you to add additional criteria to the above example. And then, iterating over the array to actually take action would look something like this:
Code:
setVar $i 1
while ($i <= SECTORS)
   if ($skip[$i] = FALSE)
      gosub :doSomething
   end
   add $i 1
end

Obviously, the :doSomething gosub is up to you, but hopefully the boolean array idea and conditional testing is clearly demonstrated. Further questions / examples?

+EP+

Author:  Helix [ Wed May 23, 2012 2:48 pm ]
Post subject:  Re: Using while loops to validate a sector

Thanks EP, no questions at the moment, I have to put it into practice to see it work.

H

Author:  The Bounty Hunter [ Wed May 23, 2012 4:36 pm ]
Post subject:  Re: Using while loops to validate a sector

Helix. In my bubble_farm script I use sectorparameters. I have a script that just pulls the bubbles from a list and sets that sectorparm (ex: bubblesec) to true. So when I run my loops to see if I need to go to the sector I just check if figsec and bublesec = true then I warp to that sector. Just another idea.

Author:  Helix [ Wed May 23, 2012 9:04 pm ]
Post subject:  Re: Using while loops to validate a sector

The Bounty Hunter wrote:
Helix. In my bubble_farm script I use sectorparameters. I have a script that just pulls the bubbles from a list and sets that sectorparm (ex: bubblesec) to true. So when I run my loops to see if I need to go to the sector I just check if figsec and bublesec = true then I warp to that sector. Just another idea.


Thats a good idea. I could check bubblesec = false and figsec = true to get the sectors outside my bubbles that have my figs. The learning curve just got higher. I need to learn more about using the twx database to pull info from and put info in....

H

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/