| www.ClassicTW.com http://www.classictw.com/ |
|
| Scripting Challenge http://www.classictw.com/viewtopic.php?f=15&t=19424 |
Page 1 of 5 |
| Author: | ElderProphet [ Sun Jun 03, 2007 10:52 pm ] |
| Post subject: | |
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 |
|
| Author: | Singularity [ Sun Jun 03, 2007 11:55 pm ] |
| Post subject: | |
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. |
|
| Author: | Promethius [ Mon Jun 04, 2007 12:58 am ] |
| Post subject: | |
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. |
|
| Author: | LoneStar [ Mon Jun 04, 2007 2:03 am ] |
| Post subject: | |
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 |
|
| Author: | Mind Dagger [ Mon Jun 04, 2007 10:51 am ] |
| Post subject: | |
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 |
|
| Author: | Mind Dagger [ Mon Jun 04, 2007 11:12 am ] |
| Post subject: | |
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. |
|
| Author: | LoneStar [ Mon Jun 04, 2007 12:56 pm ] |
| Post subject: | |
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 |
|
| Author: | Singularity [ Mon Jun 04, 2007 1:06 pm ] |
| Post subject: | |
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. |
|
| Author: | LoneStar [ Mon Jun 04, 2007 1:14 pm ] |
| Post subject: | |
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. |
|
| Author: | LoneStar [ Mon Jun 04, 2007 1:15 pm ] |
| Post subject: | |
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 |
|
| Author: | ElderProphet [ Mon Jun 04, 2007 3:03 pm ] |
| Post subject: | |
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+ |
|
| Author: | Xentropy [ Mon Jun 04, 2007 3:18 pm ] |
| Post subject: | |
Now you've all got me curious. Where is the documentation on the scripting language used by TWX? |
|
| Author: | Promethius [ Mon Jun 04, 2007 3:34 pm ] |
| Post subject: | |
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. |
|
| Author: | Mind Dagger [ Mon Jun 04, 2007 7:53 pm ] |
| Post subject: | |
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 |
|
| Author: | Mind Dagger [ Mon Jun 04, 2007 8:07 pm ] |
| Post subject: | |
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 |
|
| Page 1 of 5 | All times are UTC - 5 hours |
| Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |
|