View unanswered posts | View active topics It is currently Thu Apr 23, 2026 4:25 am



Reply to topic  [ 69 posts ]  Go to page Previous  1, 2, 3, 4, 5  Next
 Scripting Challenge 
Author Message
Lieutenant J.G.

Joined: Fri Apr 05, 2002 3:00 am
Posts: 332
Location: USA
Unread post 
LoneStar wrote:
That doesn't to me, remove the requirement that the routine plays well with others and isn't expected to run in a vacuum.   Anyway, it's one line, so it doesn't matter much either way. I always design a script with the expectation of the output actually being read somewhere later.

Yes, that's what I said about your script that would've had the bug of reporting a corporation you no longer belonged to or some photons onboard a ship that can't carry any.  I fail to see how that's relevant.  If you're just being petty and vengeful, which sounds like the case, I'll just ignore your "input" in the future.

Quote:
Parameters of the Challenge issued require that you put aside you thoughts on CURRENTSECTOR and submit a Sub-Routine that reports all elements of a QuickStat. Please and Thank You.

Well, the version that didn't use RAWPACKET did exactly that.  Which is again why I posted the script.  If someone could figure a way to use RAWPACKET and *not* cut out the "Sect" in front, we'd be golden.  It's called "collaboration" and/or "brainstorming".  People toss out their ideas, regardless of how good or bad they think they might be, and other people IMPROVE UPON THEM.  You also need to learn the difference between vengeful criticism (I didn't like his opinion, so I'm going to bash him at the first opportunity!  MUAHAHA he made a MISTAKE!  I must point and laugh!) and constructive criticism (Hey, you have a bug there, it puts extra spaces after some of the data; by adding a stripText " " to the data variable, that can be fixed, but it means one more line.).
Focus more on the second type of criticism in the future.  Please and thank you.

_________________
Creator of the TWGS Data Access Library
http://twgs.xiuhtec.com


Wed Jun 06, 2007 9:59 pm
Profile ICQ YIM
Chief Warrant Officer
User avatar

Joined: Wed Jan 04, 2006 3:00 am
Posts: 136
Location: USA
Unread post 
Xentropy wrote:
I'm not sure why the branch function didn't work;


Actually I got it to work. It only visits the label on a FALSE statement. The way it was set up in the code you had it visiting on a TRUE. If I flipped it, it worked.

From the documentation:
Code:
branch


Purpose: Tests a value and performs a conditional jump to a script label.

Syntax: branch {value} {label}

{value}: The value to be tested.

{label}: The script label to jump to if the value is FALSE (0)


Using this to make a do while loop was very clever, and I plan to use it in the future.


Wed Jun 06, 2007 10:03 pm
Profile
Lieutenant J.G.

Joined: Fri Apr 05, 2002 3:00 am
Posts: 332
Location: USA
Unread post 
Mind Dagger wrote:
Actually I got it to work. It only visits the label on a FALSE statement. The way it was set up in the code you had it visiting on a TRUE. If I flipped it, it worked.

Doh!  Thank you.  I knew it had to be something simple.  Sorry I misread the docs there, and glad at least one of my buggy, awful ideas will come in handy in the future.   LOL

_________________
Creator of the TWGS Data Access Library
http://twgs.xiuhtec.com


Wed Jun 06, 2007 10:06 pm
Profile ICQ YIM
Commander
User avatar

Joined: Tue Oct 07, 2003 2:00 am
Posts: 1134
Location: Augusta, GA
Unread post 
w00t! I was able to shed one more line... and probably the absolute last one possible. And indeed, it's due to Xen's idea to use branch. Not because it reduced lines like in my while/end statement (because :label/branch is still 2 lines), but because it allowed me to omit initializing a variable. In a way Xen, your lack of scripting experience is probably the reason you suggested it, because I doubt a single TWX scripter has ever even used that command. Fine job!

So... the count now stands at 11 lines. 11!!!

+EP+

_________________
Claim to Fame: only guy to ever crack the TW haggle algorithm, and fig/shield/hold price formula, twice.


Wed Jun 06, 2007 11:40 pm
Profile WWW
Commander
User avatar

Joined: Fri Jun 09, 2006 2:00 am
Posts: 1401
Location: Canada
Unread post 
ElderProphet wrote:
w00t! I was able to shed one more line... and probably the absolute last one possible. And indeed, it's due to Xen's idea to use branch. Not because it reduced lines like in my while/end statement (because :label/branch is still 2 lines), but because it allowed me to omit initializing a variable. In a way Xen, your lack of scripting experience is probably the reason you suggested it, because I doubt a single TWX scripter has ever even used that command. Fine job!

So... the count now stands at 11 lines. 11!!!

+EP+


Looking forward to seeing your solution. I wish I had more time to play with it

_________________
----------------------------
-= QUANTUM Computing 101: 15 = 3 x 5 ... 48% of the time.


Thu Jun 07, 2007 1:15 am
Profile ICQ YIM
Commander
User avatar

Joined: Tue Oct 07, 2003 2:00 am
Posts: 1134
Location: Augusta, GA
Unread post 
During the course of this challenge, I came up with some pretty radical notions, much like the rest of you. I actually have a pretty radical *10* line routine that arguably meets the requirements, and a notion of how to attempt a 9-liner, but neither would really be practical.

In the end, my most concise approach came by leveraging the layout of a quick stat line, where all but the first and last set of stats are bordered by the little divider characters (#179). It's easy to add a divider at the beginning and end, introducing a consistent block of data: divider, stat name, stat value, divider. Here is the code for the non-branch version:

{There is an updated version on pg. 4 of this thread. Please use that version}

:quickStatSub
send "/"
waitOn #179 & "Turns "
while (CURRENTANSILINE <> #13)
     setVar $block 0
     while ($block <> "")
# This next line reads: from "0¦CURRENTLINE¦¦", get the data
# between the last $block & "¦", and save as the new $block
             getText 0 & #179 & CURRENTLINE & #179 & #179 $block $block & #179 #179
             getWord $block $stat 1
             getWord $block $quickStat[$stat] 2
             stripText $quickStat[$stat] ","
     end
     waitOn ""
end
return

And finally, the same method tweaked to utilize BRANCH, taping out at 11 lines.

{There is an updated version on pg. 4 of this thread. Please use that version}

:quickStatSub
send "/"
waitOn #179 & "Turns "
while (CURRENTANSILINE <> #13)
     :label
     getText 0 & #179 & CURRENTLINE & #179 & #179 $block $block & #179 #179
     getWord $block $stat 1
     getWord $block $quickStat[$stat] 2
     stripText $quickStat[$stat] ","
     branch ($block = "") :label
     waitOn ""
end
return

There are a couple of limitations to a routine this short, and I'll elaborate on them tomorrow. I'd also like to propose a more practical solution for the everyday quickstat routine, which I'll try to post tomorrow as well.

Feel free to critique this approach, and I'd love to hear how another line or two can be shaved

+EP+

_________________
Claim to Fame: only guy to ever crack the TW haggle algorithm, and fig/shield/hold price formula, twice.


Fri Jun 08, 2007 2:40 am
Profile WWW
Commander
User avatar

Joined: Fri Jun 09, 2006 2:00 am
Posts: 1401
Location: Canada
Unread post 
ElderProphet wrote:
:quickStatSub
send "/"
waitOn #179 & "Turns "
while (CURRENTANSILINE <> #13)
     :label
     getText 0 & #179 & CURRENTLINE & #179 & #179 $block $block & #179 #179
     getWord $block $stat 1
     getWord $block $quickStat[$stat] 2
     stripText $quickStat[$stat] ","
     branch ($block = "") :label
     waitOn ""
end
return

There are a couple of limitations to a routine this short, and I'll elaborate on them tomorrow. I'd also like to propose a more practical solution for the everyday quickstat routine, which I'll try to post tomorrow as well.

Feel free to critique this approach, and I'd love to hear how another line or two can be shaved

+EP+


Nice work EP. Pretty darn concise code. Though Corp Number and Photons are not intialized --as somone pointed out to me not too long ago-- could that be one of the limitations your alluding too, or, that quickstat isn't initialized itself?

_________________
----------------------------
-= QUANTUM Computing 101: 15 = 3 x 5 ... 48% of the time.


Fri Jun 08, 2007 3:13 am
Profile ICQ YIM
Lieutenant

Joined: Fri Apr 05, 2002 3:00 am
Posts: 580
Location: USA
Unread post 
I can get it 3 lines.
Yes 3.

Open Icq
Click on EP
Ask him for a good routine


See easy as pie.

His will be better then mine

_________________
My scripts can be downloaded at http://www.grimytrader.com/.
Ore *****.
Even in my signature it's blocked out.


Fri Jun 08, 2007 10:17 am
Profile ICQ YIM
Veteran Op
User avatar

Joined: Thu Jun 02, 2005 2:00 am
Posts: 5558
Location: USA
Unread post 
Ha! I can do it in 2 since I've already got icq open... =)

_________________
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
Image


Fri Jun 08, 2007 2:08 pm
Profile ICQ WWW
Ambassador
User avatar

Joined: Mon Feb 09, 2004 3:00 am
Posts: 3141
Location: Kansas
Unread post 
Singularity wrote:
Ha! I can do it in 2 since I've already got icq open... =)


gosub :quickie

-------------------

I am still curious if these methods have a practical use, or advantage that I don't understand. As a scripting exercise it proved interesting to see what people came up with. The "branch" command I had seen before, but since the help file seemed to discourage its use, I stayed away from it.

I never use arrays for my quick stats and use descriptive names for the info that is returned, i.e., $ship_figs, $trader_align and so on. I also grab the current sector from this for backward compatibility.

The main reason for maintaining backward compatibility is the intermittent problem of Swath freezing when running some scripts under 2.04 Final (2_worldtrade for instance). I did not have this problem prior to "Final" and it may be something that I have added (or MS added) unrelated to "Final".

_________________
               / Promethius / Enigma / Wolfen /

"A man who has no skills can be taught, a man who has no honor has nothing."


Fri Jun 08, 2007 3:44 pm
Profile ICQ
Commander
User avatar

Joined: Tue Oct 07, 2003 2:00 am
Posts: 1134
Location: Augusta, GA
Unread post 
Heh WS... that is short

Promethius, a quickstat routine is a practical addition or include for lots of scripts. Think of how many scripts you have written that utilize one or more of the values present in the quickstat display. Consider the next time you want to grab your current ship number, do it this way:

include qstat
gosub :qstat~quickStatSub
setVar $creds $qstat~quickStat[SHIP]

The tiny 11 line version has 2 limitations, one real and one trivial. First, the stat names are not uppercase. This means that every time you want to grab a stat, you have to use a a string with the same case as the index... so in the above example to Prom, you'd have to actually grab the ship number like this:

setVar $index "Ship"
setVar $creds $qstat~quickStat[$index]

TWX complains about quoted text as the index, so ["Ship"] won't work. But, TWX treats any unquoted text as uppercase. So if the stat names were uppercase when the values were saved, then you can use unquoted text to retrieve them from the array... [creds] or [CredS], as both are converted to CREDS, thereby matching the array index.

The other, less serious limitation is that Phot and Corp aren't zeroed if they don't exist. No biggie by itself, but if you drop corp, then the quickstat array will reflect your old corp number. Still, I doubt any scripts query Corp number more than once. And I have no idea what happens if you have Photons on board and the sysop disables them... but don't play there again.

As an aside, I couldn't make any stat name and value fall on different lines, not even ship type, so I believe parsing CURRENTLINE is fine, even for ship type. I believe the following code properly addresses all of the above issues, yet remains quite concise, and doesn't leave bulky unnecessary variables in the variable dump to boot.

{There is an updated version on pg. 4 of this thread. Please use that version}
:quickStatSub
setArray $quickStat 0
send "/"
waitOn #179 & "Turns "
while (CURRENTANSILINE <> #13)
     # This line means: Grab the block of text between the next set of dividers
     getText #179 & CURRENTLINE & #179 & #179 $block $block & #179 #179
     while ($block <> "")
               getWord $block $stat 1
               upperCase $stat
               getWord $block $quickStat[$stat] 2
               # This next line finally succeeds on the Ship number/type block
               getWord $block $quickStat[TYPE] 3
               # Optionally, uppercase $quickStat[$stat] and $quickStat[TYPE] here.
               stripText $quickStat[$stat] ","
               getText #179 & CURRENTLINE & #179 & #179 $block $block & #179 #179
     end
     waitOn ""
end
return

I've enjoyed this exercise. I hope everyone learned a trick or two from it... I know I did. I'm interested to hear any further ideas you guys might have on the matter, or how you might improve upon these snippets.

+EP+

_________________
Claim to Fame: only guy to ever crack the TW haggle algorithm, and fig/shield/hold price formula, twice.


Sat Jun 09, 2007 3:35 am
Profile WWW
Commander
User avatar

Joined: Fri Jun 09, 2006 2:00 am
Posts: 1401
Location: Canada
Unread post 
ElderProphet wrote:
Still, I doubt any scripts query Corp number more than once. And I have no idea what happens if you have Photons on board and the sysop disables them... but don't play there again.


Consider, you're in a ship with Photons.. you export or end up in a ship w/o Photons... your script will still 'see' Photons on board.

_________________
----------------------------
-= QUANTUM Computing 101: 15 = 3 x 5 ... 48% of the time.


Sat Jun 09, 2007 7:51 am
Profile ICQ YIM
Ambassador
User avatar

Joined: Mon Feb 09, 2004 3:00 am
Posts: 3141
Location: Kansas
Unread post 
ElderProphet wrote:
Heh WS... that is short

Promethius, a quickstat routine is a practical addition or include for lots of scripts. Think of how many scripts you have written that utilize one or more of the values present in the quickstat display. Consider the next time you want to grab your current ship number, do it this way:

include qstat
gosub :qstat~quickStatSub
setVar $creds $qstat~quickStat[SHIP]


+EP+


I have been using a quick stats routine, but not with arrays. I like to be able to easily read my code and know what it is doing and since I am terrible about commenting code, variables that mean something are about the best way.

What I am failing to understand in your code snippet above is the [SHIP] attached to the array. It would appear crash that way - haven't tried it.   Where is it getting its value? Is SHIP a constant of some type? It would appear to have to be setup more like
setVar $creds $qstat~quickStat[$SHIP]

I have to be missing something.

_________________
               / Promethius / Enigma / Wolfen /

"A man who has no skills can be taught, a man who has no honor has nothing."


Sat Jun 09, 2007 4:45 pm
Profile ICQ
Lieutenant J.G.

Joined: Fri Apr 05, 2002 3:00 am
Posts: 332
Location: USA
Unread post 
It's setting the names of the dynamic array's indexes with the names that show up in the quickstat list.  It creates the name AND stores the data.
So if John released some new version of TW that added Quatzits as an item you could have on your ship, and added that to the quickstat listing as "|Quaz 32497|" EP's quickstat would automatically create $quickstat[QUAZ] and fill it with 32497.
The $quickstat[SHIP] EP is referring to is the last data listed on the / screen, the "|Ship 7 MerFre" or whatever.  It'd store 7 to $quickstat[SHIP].  Not $SHIP, SHIP.  Ship isn't a variable or constant, it's the name of an array location.  If you're familiar with other programming contexts, think of dynamic arrays as maps.  The name of the array location is the key to look up the value.  EP's routine creates keys named what the names are on /, and stores the values associated with those keys as the number directly following those names on /.
Edit: If you're NOT familiar with maps, they're quite simple.  They're like an array (though usually implemented as a binary tree, so they automatically sort by key), but instead of just numbers for each thing stored ($arrayname[0], $arrayname[1], $arrayname[2]), they use a key that can be something other than an integer.  In TWX's case, they use strings as keys.  So you'd be using $mapname["Sect"], $mapname["Turns"], $mapname["Creds"], etc.  (Except TWX has a bug and won't work with quotes in there so things need to be uppercase...  So $mapname[SECT], $mapname[TURNS], $mapname[CREDS] instead.  That's the issue EP was referring to.)

_________________
Creator of the TWGS Data Access Library
http://twgs.xiuhtec.com


Sat Jun 09, 2007 7:50 pm
Profile ICQ YIM
Ambassador
User avatar

Joined: Mon Feb 09, 2004 3:00 am
Posts: 3141
Location: Kansas
Unread post 
Ok, that is something I did not know about. I thought arrays had to be $ship[1], $ship[2] and so on although the number could be replaced with the variable equivalent, such as $count. This clears up how the array would be able to be descriptive and makes sense.

_________________
               / Promethius / Enigma / Wolfen /

"A man who has no skills can be taught, a man who has no honor has nothing."


Sat Jun 09, 2007 8:27 pm
Profile ICQ
Display posts from previous:  Sort by  
Reply to topic   [ 69 posts ]  Go to page Previous  1, 2, 3, 4, 5  Next

Who is online

Users browsing this forum: No registered users and 36 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by wSTSoftware.