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



Reply to topic  [ 69 posts ]  Go to page 1, 2, 3, 4, 5  Next
 Scripting Challenge 
Author Message
Commander
User avatar

Joined: Tue Oct 07, 2003 2:00 am
Posts: 1134
Location: Augusta, GA
Unread post 
This is something I've been wanting to do for a long time. I'd like to issue a friendly challenge to all of my fellow TWX scripters.

How short can you make a QuickStat routine?

Your end result must be a complete parse of the quickstat output, put into an array like this:

$quickstat["Sect"]
$quickstat["Turns"]
$quickstat["Creds"]
and so on, or something equally comprehendable.

It needs to be able to handle a missing Phot and/or Corp stat, by reporting zero or blank ("") for any missing values. And of course, comment lines and blank lines don't count. This will be a good opportunity for us to share some code techniques, and maybe learn a few tricks as well.

I'll post a code sample that anyone can use as a starting point. Obviously... yours will need to be more concise

+EP+

Code:
setVar $qsLine ""
setArray $quickstat 0
setTextLineTrigger quickstat :quickstat #179 & "Turns "
send "/"
pause

:quickstat
setVar $qsLine $qsLine & CURRENTLINE & #179
getWordPos $qsLine $pos "Ship"
if ($pos <> 0)
     goto :qsParse
end
setTextLineTrigger quickstat :quickstat
pause

:qsParse
stripText $qsLine ","
stripText $qsLine " "
getText $qsLine $quickstat[Sect] "Sect" #179
getText $qsLine $quickstat[Turns] "Turns" #179
getText $qsLine $quickstat[Creds] "Creds" #179
getText $qsLine $quickstat[Figs] "Figs" #179
getText $qsLine $quickstat[Shlds] "Shlds" #179
getText $qsLine $quickstat[Hlds] "Hlds" #179
getText $qsLine $quickstat[Ore] "Ore" #179
getText $qsLine $quickstat[Org] "Org" #179
getText $qsLine $quickstat[Equ] "Equ" #179
getText $qsLine $quickstat[Col] "Col" #179
getText $qsLine $quickstat[Phot] "Phot" #179
getText $qsLine $quickstat[Armd] "Armd" #179
getText $qsLine $quickstat[Lmpt] "Lmpt" #179
getText $qsLine $quickstat[GTorp] "GTorp" #179
getText $qsLine $quickstat[TWarp] "TWarp" #179
getText $qsLine $quickstat[Clks] "Clks" #179
getText $qsLine $quickstat[Beacns] "Beacns" #179
getText $qsLine $quickstat[AtmDt] "AtmDt" #179
getText $qsLine $quickstat[Crbo] "Crbo" #179
getText $qsLine $quickstat[EPrb] "EPrb" #179
getText $qsLine $quickstat[MDis] "MDis" #179
getText $qsLine $quickstat[PsPrb] "PsPrb" #179
getText $qsLine $quickstat[PlScn] "PlScn" #179
getText $qsLine $quickstat[LRS] "LRS" #179
getText $qsLine $quickstat[Aln] "Aln" #179
getText $qsLine $quickstat[Exp] "Exp" #179
getText $qsLine $quickstat[Corp] "Corp" #179
getText $qsLine $quickstat[Ship] "Ship" #179
echo "*Pausing*Check the variable dump."
pause

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


Sun Jun 03, 2007 10:52 pm
Profile WWW
Veteran Op
User avatar

Joined: Thu Jun 02, 2005 2:00 am
Posts: 5558
Location: USA
Unread post 
Well at first thought... (35 lines total)

Code:
o_quickstat
killtrigger quickstat
setTextLineTrigger quickstat :stat_line #179 & "Turns "
send "/"
pause

:stat_line
setVar $stats_data $stats_data & CURRENTLINE & #179
getWordPos $stats_data $pos #179 & "Ship"
if ($pos > 0)
      goto :gotStats
end
setTextLineTrigger quickstat :stat_line
pause

:gotStats
upperCase $stats_data
stripText $stats_data ","
setVar $stats "SECT TURNS CREDS FIGS SHLDS HLDS ORE ORG EQU COL PHOT ARMD LMPT GTORP TWARP CLKS BEACNS ATMDT CRBO EPRB MDIS PSPRB PLSCN LRS ALN EXP CORP SHIP %%%"

ull_the_stat
add $stat_num 1
getWord $stats $current_stat $stat_num
if ($current_stat <> "%%%")
     setVar $quickstats[$current_stat] 0
     if ($current_stat = "SHIP")
             getText $stats_data $ship_info $current_stat & " " #179
             getWord $ship_info $quickstats[$current_stat] 1
             getWord $ship_info $quickstats[TYPE] 2
     else
             getText $stats_data $quickstats[$current_stat] $current_stat & " " #179
     end
     goto ull_the_stat
end
return


I could remove the , strip and the upperCase and remove the shiptype seperation and reduce the size, but I like those features. I'll think on it a bit and see how I can reduce it further.

_________________
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


Sun Jun 03, 2007 11:55 pm
Profile ICQ WWW
Ambassador
User avatar

Joined: Mon Feb 09, 2004 3:00 am
Posts: 3141
Location: Kansas
Unread post 
k, I'll bite, why output into an array? It seems like an array would not be descriptive and only make the code harder to read. I am guessing that you would use $ship[1] as the current sector and so on, but it seems $ship_curSector would be easier when reading later on.

I can understand it as a scripting exercise, but not from a practical standpoint. I do like the way Sing used the variable containing the key words.

_________________
               / Promethius / Enigma / Wolfen /

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


Mon Jun 04, 2007 12:58 am
Profile ICQ
Commander
User avatar

Joined: Fri Jun 09, 2006 2:00 am
Posts: 1401
Location: Canada
Unread post 
Okay. Here's my little routine. 24 Lines total:

Code:
setVar $Result ""
send "/"
:ScanLine_The_Sequal
  setTextLineTrigger     ScanLine  :ScanLine
waitfor #179
pause
:ScanLine
if (CURRENTLINE <> "")
     setVar $Result ($Result & CURRENTLINE)
     add $i 1
     goto :ScanLine_The_Sequal
elseif ($i = 0)
     goto :ScanLine_The_Sequal
end
ReplaceText $Result #179 " "
setVar $i 1
getWord $Result $Temp $i
While ($Temp <> 0)
     upperCase $Temp
     getWord $Result $Quick_Stat[$Temp] ($i + 1)
     StripText $Quick_Stat[$Temp] ","
     add $i 2
     getWord $Result $Temp $i
end


Edit: Added line Uppercase $Temp

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


Mon Jun 04, 2007 2:03 am
Profile ICQ YIM
Chief Warrant Officer
User avatar

Joined: Wed Jan 04, 2006 3:00 am
Posts: 136
Location: USA
Unread post 
I took a couple of the ideas posted by Lonestar and Sing and put them together in this 22 line script:

Code:
setVar $qsLine ""
setArray $quickstat 0
setTextLineTrigger quickstat :quickstat #179 & "Turns "
send "/"
pause
:quickstat
     setVar $qsLine $qsLine & CURRENTLINE & #179
     getWordPos $qsLine $pos "Ship"
     if ($pos = 0)
           setTextLineTrigger quickstat :quickstat
           pause
     end
setVar $currentStat ""
setVar $i 1
replaceText $qsLine #179 " "
stripText $qsLine ","
uppercase $qsLine
while ($currentStat <> "SHIP")
     getWord $qsLine $currentStat $i
     getWord $qsLine $quickstat[$currentStat] ($i + 1)
     add $i 2
end


Mon Jun 04, 2007 10:51 am
Profile
Chief Warrant Officer
User avatar

Joined: Wed Jan 04, 2006 3:00 am
Posts: 136
Location: USA
Unread post 
30 line version with ship type capture (sing's) and current prompt grab (which I can't live without)

Code:
setVar $qsLine ""
setArray $quickstat 0
setTextLineTrigger quickstat :quickstat  #179 & "Turns "
setTextLineTrigger  prompt    :allPrompt #145 & #8
send #145&"/"
pause

:allPrompts
     getWord CURRENTLINE $quickstat[PROMPT] 1
     stripText $quickstat[PROMPT] #145
     stripText $quickstat[PROMPT] #8
     pause

:quickstat
     setVar $qsLine $qsLine & CURRENTLINE & #179
     getWordPos $qsLine $pos "Ship"
     if ($pos = 0)
             setTextLineTrigger quickstat :quickstat
             pause
     end
setVar $currentStat ""
setVar $i 1
stripText $qsLine ","
uppercase $qsLine
getText $qsLine $ship_info "SHIP " #179
getWord $ship_info $quickstat[TYPE] 2
replaceText $qsLine #179 " "
while ($currentStat <> "SHIP")
     getWord $qsLine $currentStat $i
     getWord $qsLine $quickstat[$currentStat] ($i + 1)
     add $i 2
end


Edit: I realized I could quickly trim 5 lines.


Mon Jun 04, 2007 11:12 am
Profile
Commander
User avatar

Joined: Fri Jun 09, 2006 2:00 am
Posts: 1401
Location: Canada
Unread post 
Well. seems game is on.. hehe.. so here's my 21 Line version

Code:
setVar $Result ""
send "/"
:ScanLine_The_Sequal
setTextLineTrigger ScanLine1 :ScanLine #179
pause
:ScanLine
if (CURRENTLINE <> "")
     setVar $Result ($Result & CURRENTLINE)
     setTextLineTrigger ScanLine2 :ScanLine
     pause
end
ReplaceText $Result #179 " "
setVar $i 1
getWord $Result $Temp $i
While ($Temp <> 0)
     upperCase $Temp
     getWord $Result $Stat[$Temp] ($i + 1)
     StripText $Stat[$Temp] ","
     add $i 2
     getWord $Result $Temp $i
end

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


Mon Jun 04, 2007 12:56 pm
Profile ICQ YIM
Veteran Op
User avatar

Joined: Thu Jun 02, 2005 2:00 am
Posts: 5558
Location: USA
Unread post 
Three's a problem with the version that grabs from the stat names from the "/" command, it doesn't set a default for missing stats (phot, corp) as was part of the challenge.

There'd also be a problem if the last line didn't have a #179 in it, which happens fairly often.

_________________
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


Mon Jun 04, 2007 1:06 pm
Profile ICQ WWW
Commander
User avatar

Joined: Fri Jun 09, 2006 2:00 am
Posts: 1401
Location: Canada
Unread post 
Singularity wrote:
Three's a problem with the version that grabs from the stat names from the "/" command, it doesn't set a default for missing stats (phot, corp) as was part of the challenge.

There'd also be a problem if the last line didn't have a #179 in it, which happens fairly often.


I knew this would come up. the Default for accessing an un initialized Dynamic Array elemtent is... Drum Roll... ZERO and read my submission again.. lines are scanned until CURRENTLINE <> "" ...therefore there's no problem

The challenge was: Shortest Routine. Part of this includes knowing how Vars are intialized; so why do a SetVar $i 0 ..or.. SetVar $quickstat[PHOT] 0   ??    

Oh. Final thing. pre-empting future responses: There is a difference between a Routine and a Sub-Routine. This is a Routine.

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


Mon Jun 04, 2007 1:14 pm
Profile ICQ YIM
Commander
User avatar

Joined: Fri Jun 09, 2006 2:00 am
Posts: 1401
Location: Canada
Unread post 
okay. my very last submision. 18 Lines:

Code:
send "/"
:ScanLine_The_Sequal
waitfor #179
:ScanLine
if (CURRENTLINE <> "")
     setVar $Result ($Result & " " & CURRENTLINE)
     setTextLineTrigger ScanLine2 :ScanLine
     pause
end
ReplaceText $Result #179 " "
StripText $Result ","
getWord $Result $Temp ($i + 2)
While ($Temp <> 0)
     upperCase $Temp
     getWord $Result $Stat[$Temp] ($i + 1)
     add $i 2
     getWord $Result $Temp $i
end


[EDIT] I Lied. Previous 'final' version was 20 Lines.. this is now at 18

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


Mon Jun 04, 2007 1:15 pm
Profile ICQ YIM
Commander
User avatar

Joined: Tue Oct 07, 2003 2:00 am
Posts: 1134
Location: Augusta, GA
Unread post 
Not bad at 20 lines LS. Still... you can do better

I'd prefer the variable dump show a corp / phot of zero, vs. uninitialized... which could add a few lines.   But that is the point... to make you guys think outside the box a bit.

I've come up with several *cough B.S. alert* 15-liners, so keep thinking on line reduction techniques.

+EP+

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


Mon Jun 04, 2007 3:03 pm
Profile WWW
Lieutenant J.G.

Joined: Fri Apr 05, 2002 3:00 am
Posts: 332
Location: USA
Unread post 
Now you've all got me curious.  Where is the documentation on the scripting language used by TWX?

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


Mon Jun 04, 2007 3:18 pm
Profile ICQ YIM
Ambassador
User avatar

Joined: Mon Feb 09, 2004 3:00 am
Posts: 3141
Location: Kansas
Unread post 
Xentropy wrote:
Now you've all got me curious.  Where is the documentation on the scripting language used by TWX?


Right click on the TWX icon, point to help, click on scripting reference. It is an html file.

_________________
               / Promethius / Enigma / Wolfen /

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


Mon Jun 04, 2007 3:34 pm
Profile ICQ
Chief Warrant Officer
User avatar

Joined: Wed Jan 04, 2006 3:00 am
Posts: 136
Location: USA
Unread post 
LoneStar wrote:
okay. my very last submision. 18 Lines:

...

[EDIT] I Lied. Previous 'final' version was 20 Lines.. this is now at 18


Hey Lone, you were wrong. You take out one unneeded label and you are actually at 17

Code:
send "/"
waitfor #179
:ScanLine
if (CURRENTLINE <> "")
     setVar $Result ($Result & " " & CURRENTLINE)
     setTextLineTrigger ScanLine2 :ScanLine
     pause
end
ReplaceText $Result #179 " "
StripText $Result ","
getWord $Result $Temp ($i + 2)
While ($Temp <> 0)
     upperCase $Temp
     getWord $Result $Stat[$Temp] ($i + 1)
     add $i 2
     getWord $Result $Temp $i
end


Mon Jun 04, 2007 7:53 pm
Profile
Chief Warrant Officer
User avatar

Joined: Wed Jan 04, 2006 3:00 am
Posts: 136
Location: USA
Unread post 
Ok, technically this is untested. I believe it will work though. Lonestar gets the credit for it though if it works. I will take the credit if it doesn't. 14 lines.

Code:
send "/"
waitfor #179
while (CURRENTLINE <> "")
     setVar $Result ($Result & " " & CURRENTLINE)
     waitFor ""
end
ReplaceText $Result #179 " "
StripText $Result ","
UpperCase $Result
While ($Temp <> "%%%")
     add $i 2
     getWord $Result $Temp $i "%%%"
     getWord $Result $Stat[$Temp] ($i + 1)
end


Edit: Had the "%%%" at the wrong getWord


Mon Jun 04, 2007 8:07 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 69 posts ]  Go to page 1, 2, 3, 4, 5  Next

Who is online

Users browsing this forum: No registered users and 41 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.