| Author |
Message |
|
Maniac
Lieutenant J.G.
Joined: Sun Mar 13, 2005 3:00 am Posts: 387 Location: USA
|
 Code Snippets Please add your own.
Howdy fellow Ferrengi Haters I have been taking a break from staring at Delphi code all day and started to stare at some TWX Script Code. I hope with this post to start a collection of TWXProxy Script snippets So here is a little snippet of code that someone might find of use All it does is "parse" a line of text and returns the first word in the text. Simple isn't it... well it really does more than that as it removes the word it just found from the line so you can use it in a loop to loop through the words in the line. Requirements : $String Line of space delimited whatever.... numbers and or words Label named done: Returns $Word LeftMost word of $String --------------------------------- Code: # subroutine to parse string of space delimited # set $DEBUG to true to echo results :ParseString getWord $String $Word 1 if ($DEBUG ="True") echo "*" & ANSI_7 & $Word & "*" end # striptext will take the $Word out of the string so you # are set up for the next word striptext $String $Word getlength $String $len # little trick to see if we are at the end of the string if ($len =0) goto :done end return
an example would be setvar $String "0 1 2 3" gosub ParseString: Echo $Word # will echo 0 gosub ParseString: Echo $Word # will echo 1 Hopefully someone can find a use for this like I did... I had some sectors that I needed to warp to and needed to script it so I developed this little snippet. If you have any little tricks and or code snippets that might help someone please share Maybe having more than 1 set of eyes on the code will result in an improvement or possibly a bug fix.
_________________ Find out just what any people will quietly submit to and you have the exact measure of the injustice and wrong which will be imposed on them. Frederick Douglas
|
| Wed Feb 03, 2010 9:39 pm |
|
 |
|
Kaus
Gameop
Joined: Tue Nov 19, 2002 3:00 am Posts: 1050 Location: USA
|
 Re: Code Snippets Please add your own.
Neat snippet, why did you choose to cycle through the words instead of grabbing a specific word for your script? Did your sector number display vary? I don't really have anything to contribute that is not specific to a script, though I think getstats routines, etc would be of value here as a point of referance. I remember EP at one point did a getstats contest to see who could get the smallest working routine. I think something along those lines for a snippet that is used all the time is fun and interesting.
_________________ Dark Dominion TWGS Telnet://twgs.darkworlds.org:23 ICQ#31380757, -=English 101 pwns me=- "This one claims to have been playing since 1993 and didn't know upgrading a port would raise his alignment."
|
| Wed Feb 03, 2010 9:43 pm |
|
 |
|
Maniac
Lieutenant J.G.
Joined: Sun Mar 13, 2005 3:00 am Posts: 387 Location: USA
|
 Re: Code Snippets Please add your own.
Kaus wrote: Neat snippet, why did you choose to cycle through the words instead of grabbing a specific word for your script? Did your sector number display vary? I don't really have anything to contribute that is not specific to a script, though I think getstats routines, etc would be of value here as a point of referance. I remember EP at one point did a getstats contest to see who could get the smallest working routine. I think something along those lines for a snippet that is used all the time is fun and interesting. A quick and honest answer to why I cycled through them... My brain has seen too much alcohol. I didn't want to have to keep track or even know in advance how many sectors were in the string. EDIT.... I would prefer that this doesn't evolve in to a I can do that function in X lines.... Maybe I am old fashioned but I prefer readability over so called efficiency Are there better ways of doing what I posted I sure as Hell hope so but I believe that this is readable and someone can look at the code and see what I am trying to do.
_________________ Find out just what any people will quietly submit to and you have the exact measure of the injustice and wrong which will be imposed on them. Frederick Douglas
|
| Wed Feb 03, 2010 9:54 pm |
|
 |
|
Parrothead
Commander
Joined: Wed May 03, 2006 2:00 am Posts: 1722 Location: USA
|
 Re: Code Snippets Please add your own.
Maniac wrote: Howdy fellow Ferrengi Haters I have been taking a break from staring at Delphi code all day and started to stare at some TWX Script Code. I hope with this post to start a collection of TWXProxy Script snippets So here is a little snippet of code that someone might find of use All it does is "parse" a line of text and returns the first word in the text. Simple isn't it... well it really does more than that as it removes the word it just found from the line so you can use it in a loop to loop through the words in the line. Requirements : $String Line of space delimited whatever.... numbers and or words Label named done: Returns $Word LeftMost word of $String --------------------------------- Code: # subroutine to parse string of space delimited # set $DEBUG to true to echo results :ParseString getWord $String $Word 1 if ($DEBUG ="True") echo "*" & ANSI_7 & $Word & "*" end # striptext will take the $Word out of the string so you # are set up for the next word striptext $String $Word getlength $String $len # little trick to see if we are at the end of the string if ($len =0) goto :done end return
an example would be setvar $String "0 1 2 3" gosub ParseString: Echo $Word # will echo 0 gosub ParseString: Echo $Word # will echo 1 Perhaps striptext is of limited value in a sector list..say a bubble list from swath. setvar $string 1234 12345 striptext $string 1234 whats left is 5. grab the length of the first word and cuttext from there to the end of the string cuttext $string $temp $length 99999 setvar $string $temp repeat of course cuttext is a pain in the butt because you will have to test for zero etc. but its what u get for free.
_________________ Coconut Telegraph (ICQ)#586137616 Team Speak3@ 220.244.125.70:9987 Founding Member -=[Team Kraaken]=- Winner of Gridwars 2010 - Ka Pla
 Jesus wounldn't Subspace Crawl
|
| Wed Feb 03, 2010 10:23 pm |
|
 |
|
Maniac
Lieutenant J.G.
Joined: Sun Mar 13, 2005 3:00 am Posts: 387 Location: USA
|
 Re: Code Snippets Please add your own.
Perhaps striptext is of limited value in a sector list..say a bubble list from swath.
setvar $string 1234 12345 striptext $string 1234 IIRC no what's left is 12345 as we removed 1234 from the 1234 12345 string
whats left is 5.
and I avoid that by taking out the first word so I am stripping from the beginning to the first space after the word...
grab the length of the first word and cuttext from there to the end of the string
cuttext $string $temp $length 99999 setvar $string $temp repeat
of course cuttext is a pain in the butt because you will have to test for zero etc. but its what u get for free.
OK What would you like to see as a replacement ? Mean sincerely as this is what I need for possible new functionality.
_________________ Find out just what any people will quietly submit to and you have the exact measure of the injustice and wrong which will be imposed on them. Frederick Douglas
|
| Thu Feb 04, 2010 5:58 am |
|
 |
|
Promethius
Ambassador
Joined: Mon Feb 09, 2004 3:00 am Posts: 3141 Location: Kansas
|
 Re: Code Snippets Please add your own.
Cuttext is a very nice command to have, and I haven't had any issues with it when I use it correctly. I don't use SWATH's bubble list at all since I wrote the bubble script (somewhere on here should be the source), so I really don't know the issues with that.
_________________
/ Promethius / Enigma / Wolfen /
"A man who has no skills can be taught, a man who has no honor has nothing."
|
| Thu Feb 04, 2010 12:58 pm |
|
 |
|
Parrothead
Commander
Joined: Wed May 03, 2006 2:00 am Posts: 1722 Location: USA
|
 Re: Code Snippets Please add your own.
Promethius wrote: I don't use SWATH's bubble list at all since I wrote the bubble script , so I really don't know the issues with that. Any list of sectors or other info in a string as oppossed to a list (one piece of info per line) take ck's supg's or rammer's fig list for example. Bubbles are not the issue. Parsing the list is the issue. Promethius wrote: Cuttext is a very nice command to have, and I haven't had any issues with it when I use it correctly. Hmm...I dont think anyone has issues with anything when used correctly. This will test your snippet and you will see the issue. I will post something as soon as I get something working. Code: setvar $string ("1234 " & "12345 " & "12346 " & "12347 " & "19199 " & "7449") :start setvar $debug true add $i 1 if ($i > 6) pause else gosub :ParseString goto :start end
# subroutine to parse string of space delimited # set $DEBUG to true to echo results :ParseString getWord $String $Word 1 if ($DEBUG = True) echo "*" & ANSI_7 & $Word & "*" echo "*" & ANSI_7 & $String & "*" end # striptext will take the $Word out of the string so you # are set up for the next word striptext $String $Word getlength $String $len # little trick to see if we are at the end of the string if ($len =0) goto :done end return
:done halt
_________________ Coconut Telegraph (ICQ)#586137616 Team Speak3@ 220.244.125.70:9987 Founding Member -=[Team Kraaken]=- Winner of Gridwars 2010 - Ka Pla
 Jesus wounldn't Subspace Crawl
|
| Thu Feb 04, 2010 2:32 pm |
|
 |
|
Maniac
Lieutenant J.G.
Joined: Sun Mar 13, 2005 3:00 am Posts: 387 Location: USA
|
 Re: Code Snippets Please add your own.
Parrothead wrote: Promethius wrote: I don't use SWATH's bubble list at all since I wrote the bubble script , so I really don't know the issues with that. Any list of sectors or other info in a string as oppossed to a list (one piece of info per line) take ck's supg's or rammer's fig list for example. Bubbles are not the issue. Parsing the list is the issue. Promethius wrote: Cuttext is a very nice command to have, and I haven't had any issues with it when I use it correctly. Hmm...I dont think anyone has issues with anything when used correctly. This will test your snippet and you will see the issue. I will post something as soon as I get something working. Doh.... striptext will strip all of the matched word in the string. Nice catch there... Course helps if you have a Birds-eye view of things.
_________________ Find out just what any people will quietly submit to and you have the exact measure of the injustice and wrong which will be imposed on them. Frederick Douglas
|
| Thu Feb 04, 2010 9:04 pm |
|
 |
|
ElderProphet
Commander
Joined: Tue Oct 07, 2003 2:00 am Posts: 1134 Location: Augusta, GA
|
 Re: Code Snippets Please add your own.
I actually like "improve my code" conversations. Some of my best code snippets happen on the fly, with that random ICQ help request. Anyway, I like your idea of sharing code snippets. I might try to do the same thing, only make it a "improve my code" thread. But since that is not what you are after, I'll just contribute my own code snippet. keepalive - Sends ESC every 3 mins, which doesn't cause sector displays, etc. Code: systemScript :loop send #27 setDelayTrigger delay :loop 180000 pause fig refresh - small, efficient routine that populates $figGrid[sector] with deployed fig qty. Code: :figRefresh setArray $figGrid SECTORS send "g" waiton "Deployed Fighter Scan" waiton "=========" setTextLineTrigger personal :getDeployed "Personal" setTextLineTrigger corp :getDeployed "Corp" waitOn "Command [TL" killTrigger personal killTrigger corp return
:getDeployed killTrigger personal killTrigger corp getWord CURRENTLINE $figSector 1 getWord CURRENTLINE $figGrid[$figSector] 2 setTextLineTrigger personal :getDeployed "Personal" setTextLineTrigger corp :getDeployed "Corp" pause There is a slightly more efficient way to write that, but I think this way gives a better example of effective trigger usage, esp. where multiple triggers execute the same routine. For fool-proof scripting, you might add a delay trigger before any waitOn triggers, to regain control in case something goes awry. Regards, +EP+
_________________ Claim to Fame: only guy to ever crack the TW haggle algorithm, and fig/shield/hold price formula, twice.
|
| Thu Feb 04, 2010 9:10 pm |
|
 |
|
General Disarray
Staff Sergeant
Joined: Mon May 25, 2009 6:38 pm Posts: 19
|
 Re: Code Snippets Please add your own.
Here is one I wrote a while back. It's seen several iterations, but here it is in it's most basic form: Code: send "f" waitfor "leaving" getword CURRENTLINE $figs 10 striptext $figs ","
if ($figs = 0) send "1*ct" else send $figs & "*co" end
It tries to fill your ship from sector fighters. I was just tired of doing it by hand. I have a version of it tied to a hot-key as we speak 
|
| Tue Feb 23, 2010 2:26 pm |
|
 |
|
Singularity
Veteran Op
Joined: Thu Jun 02, 2005 2:00 am Posts: 5558 Location: USA
|
 Re: Code Snippets Please add your own.
You can fix the striptext issue by using a leading and trailing space after the string, then simply replacing items delineated by the leading and trailing spaces.
_________________ May the unholy fires of corbomite ignite deep within the depths of your soul...
1. TWGS server @ twgs.navhaz.com 2. The NavHaz Junction - Tradewars 2002 Scripts, Resources and Downloads 3. Open IRC chat @ irc.freenode.net:6667 #twchan 4. Parrothead wrote: Jesus wouldn't Subspace Crawl.
*** SG memorial donations via paypal to: dpocky68@booinc.com
|
| Tue Feb 23, 2010 4:10 pm |
|
 |
|
LoneStar
Commander
Joined: Fri Jun 09, 2006 2:00 am Posts: 1401 Location: Canada
|
 Re: Code Snippets Please add your own.
One of my own favorite code snips I use in various forms: Code: gosub :quikstats setVar $STR "" if ($ORE_HOLDS <> "0") setVar $STR ($STR & " T N L 1* ") end if ($ORGANIC_HOLDS <> "0") setVar $STR ($STR & " T N L 2* ") end if ($EQUIPMENT_HOLDS <> "0") setVar $STR ($STr & " T N L 3* ") end if ($COLONIST_HOLDS <> "0") setVar $STR ($STR & "S N L 1* S N L 2* S N L 3* ") end
if ($STR <> "") send "Q " & $STR & "C" waiton "<Enter Citadel>" gosub :quikstats if (($COLONIST_HOLDS <> "0") OR ($EQUIPMENT_HOLDS <> "0") OR ($ORGANIC_HOLDS <> "0") OR ($ORE_HOLDS <> "0")) Echo "*Unable to Empty Holds*" halt end end
_________________ ---------------------------- -= QUANTUM Computing 101: 15 = 3 x 5 ... 48% of the time.
|
| Tue Feb 23, 2010 4:55 pm |
|
 |
|