www.ClassicTW.com
https://mail.black-squirrel.com/

How do you use Subroutines?
https://mail.black-squirrel.com/viewtopic.php?f=15&t=18611
Page 1 of 1

Author:  Parrothead [ Thu Feb 01, 2007 5:37 pm ]
Post subject: 

I use this one I use as part of an include to prevent script getting stuck after issuing a subspace command.
#:9_waittrigger~waittrigger
:waittrigger
killtrigger subspace0
killtrigger subspace1
killtrigger subspace2
settexttrigger subspace0 :subspace "Sub-space comm-link terminated"
settexttrigger subspace1 :subspace "sent on sub-space channel"
setdelaytrigger subspace2 :timeout 10000
pause
:subspace
killtrigger subspace0
killtrigger subspace1
killtrigger subspace2
return

Author:  Singularity [ Thu Feb 01, 2007 9:02 pm ]
Post subject: 

That can be a bit confusing. He's doing...

gosub :waittrigger

The trigger to :subspace is not a subroutine, so the return is not exiting back to the trigger... it's exiting to the gosub call that is outside of this snippet.

Author:  Parrothead [ Thu Feb 01, 2007 9:24 pm ]
Post subject: 

gosub :9_waittrigger~waittrigger actually but yes that is how its used.
Might be nice to see what other peeps have come up with and how they use subroutines.I find them of great help in error checking and keeping redundant code out of my scripts

Author:  Singularity [ Thu Feb 01, 2007 11:51 pm ]
Post subject: 

Hehe. Sure thing...

Code:
# Get the course
:get_course
   setVar $sectors ""

   # Wait for the course to start
   killtrigger timeout
   setDelayTrigger timeout :course_timeout 3000
   send "^ f *" & $destination & "**q"
   waitFor "  TO > "
   killtrigger timeout

   # Start reading the course
   killtrigger read_line
   killtrigger no_path_1
   killtrigger no_path_2
   killtrigger no_path_3
   setTextLineTrigger read_line :readline " > "
   setTextLineTrigger no_path_1 :no_path  "So what's the point?"
   setTextLineTrigger no_path_2 :no_path  ": ENDINTERROG"
   setTextLineTrigger no_path_3 :no_path  "No route within"
   pause

   # - - - - - - - - - - - - - - - - - - - - - - - -

   # No path...
   :no_path
      killtrigger read_line
      killtrigger no_path_1
      killtrigger no_path_2
      killtrigger no_path_3
      setVar $result "BAD"
      return

   # - - - - - - - - - - - - - - - - - - - - - - - -

   # Got a path
   :readline
   killtrigger read_line
   killtrigger no_path_1
   killtrigger no_path_2
   killtrigger no_path_3

   # Did we get the end line?
   getWord CURRENTLINE $endtest 2
   if ($endtest = "ENDINTERROG")
      goto oneline
   end

   # Append line
   setVar $sectors $sectors & " " & CURRENTLINE

   # Reset the trigger
   setTextLineTrigger read_line :readline ""
   pause

   # Done reading lines
   oneline
   killtrigger read_line

   # Clean up the sectors list
   striptext $sectors "("
   striptext $sectors ")"
   replacetext $sectors ">" " "

   # - - - - - - - - - - - - - - - - - - - - - - - -

   # Create the course array
   :create_course_array
   setVar $sectors $sectors & " %&%&%"
   setVar $course_length 0
   setVar $wordy ""
   setVar $word_to_read 1
   while ($wordy <> "%&%&%")
      getWord $sectors $wordy $word_to_read
      if ($wordy <> "%&%&%")
         add $course_length 1
         setVar $course[$course_length] $wordy
      end
      add $word_to_read 1
   end
return

:course_timeout
   killtrigger timeout
   setVar $result "BAD"
   return


Then all you have to do is...

Code:
setVar $destination 1234
gosub :get_course


And it gets the course as a neat little array, taking in account avoids w/o requiring a ztm.

One small example. I make extensive use of subroutines and includes.

Author:  Parrothead [ Fri Feb 02, 2007 1:19 am ]
Post subject: 

so to prevent redundant cycling in a colo script
Code:
setVar $destination 1234 
getcourse $course currentsector $destination 
if ($course = "-1") 
gosub :get_course
end

and your done,,,very nice

Author:  Singularity [ Fri Feb 02, 2007 2:21 am ]
Post subject: 

Yep. Keep in mind that getCourse won't handle avoids unless you set them and will mess up if you're using a partial ztm. That's why I tend to avoid getCourse if I don't absolutely need the speed, a cf plot is more accurate under a wider set of conditions.

If you're planning to colonize that means you'll want to clear your avoids first. Otherwise you'll get an inaccurate hopcount and end up taking the wrong amount of fuel. If you're using it as a lawnmow or gridder... then you need to build for the situation you're likely to be in.

Author:  Parrothead [ Fri Feb 02, 2007 3:13 am ]
Post subject: 

i use a CF plot in my mower so I dont have to deal with the getcourse headache.I havent played with the internal voids in the new twx yet.

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