| Author |
Message |
|
Singularity
Veteran Op
Joined: Thu Jun 02, 2005 2:00 am Posts: 5558 Location: USA
|
Items like $array[SHIP], where the index is an string, are called "associative arrays." They're very handy in a lot of modern programming languages.
In TWXproxy the array index (the name) doesn't go in quotes and doesn't need to be in caps. According to EP it's not case sensitive anyway, so $array[SHIP] = $array[shIp] = $array[sHiP]
_________________ 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
|
| Sat Jun 09, 2007 11:26 pm |
|
 |
|
Promethius
Ambassador
Joined: Mon Feb 09, 2004 3:00 am Posts: 3141 Location: Kansas
|
Singularity wrote: Items like $array[SHIP], where the index is an string, are called "associative arrays." They're very handy in a lot of modern programming languages.
In TWXproxy the array index (the name) doesn't go in quotes and doesn't need to be in caps. According to EP it's not case sensitive anyway, so $array[SHIP] = $array[shIp] = $array[sHiP]
This opens up some ideas for me on ways to clean up and hopefully speed up some of my scripts. I may kill my script trying it (it it may kill me in a game), but that is what backups are for. It should be interesting to try no matter what.
_________________
/ Promethius / Enigma / Wolfen /
"A man who has no skills can be taught, a man who has no honor has nothing."
|
| Sat Jun 09, 2007 11:32 pm |
|
 |
|
ElderProphet
Commander
Joined: Tue Oct 07, 2003 2:00 am Posts: 1134 Location: Augusta, GA
|
Good explanations Xen, Sing. See Prom, this turned out to be a better learning experience than you expected
LoneStar, the only time the "Phot" stat won't show up in the quickstat display is if they are disabled in the game. If they are enabled, then the 11 line routine will properly update the $quickStat["Phot"] value, as it would display "0" if your ship had none.
Obviously, using a text value as the array index is only possible with dynamic arrays. So Prom, before you go converting those scripts, just remember that there is a measurable performance penalty in using large dynamic arrays.
_________________ Claim to Fame: only guy to ever crack the TW haggle algorithm, and fig/shield/hold price formula, twice.
|
| Sun Jun 10, 2007 1:43 am |
|
 |
|
Xentropy
Lieutenant J.G.
Joined: Fri Apr 05, 2002 3:00 am Posts: 332 Location: USA
|
Singularity wrote: In TWXproxy the array index (the name) doesn't go in quotes and doesn't need to be in caps. According to EP it's not case sensitive anyway, so $array[SHIP] = $array[shIp] = $array[sHiP]
Except, if I understood what he posted earlier correctly, if you use a string variable as the key when adding new data. Then, if the string in that variable was something other than caps, it would actually use the noncaps name, and you'd lose the ability to type the key directly instead of using a variable to store the key on retrieval. That's the bug I was referring to. Elder Prophet wrote: Obviously, using a text value as the array index is only possible with dynamic arrays. So Prom, before you go converting those scripts, just remember that there is a measurable performance penalty in using large dynamic arrays.
Another consideration to be made in using a dynamic array instead of a standard one in TWX is you cannot as easily step through all of the values in the array and do something with each. There's no integer equivalent for each of the string locations, and there's no such thing as an iterator in TWX. The only way to do something similar is, if you know all of the names being used, to use a standard array populated with each of the strings used in the associative array.
e.g. In the case of quickstats you really wouldn't ever need to, but if for some reason you did, you could do so by creating a $statnames array and populating it with each of the names...something like:
Code: setVar $listofnames "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" setVar $i 1 while ($i < 29) getWord $listofnames $statnames[$i] $i end
Then you could step through anytime later if needed by using $quickstats[$statnames[$i]] in another i-based loop. This is of course assuming you made sure all the stat names used caps.
_________________ Creator of the TWGS Data Access Library
http://twgs.xiuhtec.com
|
| Sun Jun 10, 2007 2:52 am |
|
 |
|
LoneStar
Commander
Joined: Fri Jun 09, 2006 2:00 am Posts: 1401 Location: Canada
|
Singularity wrote: Items like $array[SHIP], where the index is an string, are called "associative arrays." They're very handy in a lot of modern programming languages.
In TWXproxy the array index (the name) doesn't go in quotes and doesn't need to be in caps. According to EP it's not case sensitive anyway, so $array[SHIP] = $array[shIp] = $array[sHiP]
Acutally.. if you set an element: setVar $somearray[$temp] 20 ... where $temp ='s "Figs" ... you will not be able to access that element. not as "figs" FIGS.. or anything the only way that I've been able to mae it work is to uppercase $temp, *then* issue setVar $somearray[$temp] 20
Try it and see
Edit: here's a little code for anyone who may not get my point (accessing the quickstat will not work:
Code: send "/" waitOn #179 & "Turns " while (CURRENTANSILINE <> #13) :label1 getText 0 & #179 & CURRENTLINE & #179 & #179 $block $block & #179 #179 getWord $block $stat 1 getWord $block $quickStat[$stat] 2 stripText $quickStat[$stat] "," branch ($block = "") :label1 waitOn "" end
setVar $temp $quickStat[Creds] echo "**" & $temp & "**"
send "/" waitOn #179 & "Turns " while (CURRENTANSILINE <> #13) :label2 getText 0 & #179 & CURRENTLINE & #179 & #179 $block $block & #179 #179 getWord $block $stat 1 uppercase $stat getWord $block $quickStat[$stat] 2 stripText $quickStat[$stat] "," branch ($block = "") :label2 waitOn "" end
setVar $temp $quickStat[Creds] echo "**" & $temp & "**"
_________________ ---------------------------- -= QUANTUM Computing 101: 15 = 3 x 5 ... 48% of the time.
|
| Sun Jun 10, 2007 3:02 am |
|
 |
|
ElderProphet
Commander
Joined: Tue Oct 07, 2003 2:00 am Posts: 1134 Location: Augusta, GA
|
Lonestar, the index name is case sensitive. You don't have to make it uppercase to retrieve it, you just have to retrieve it using an index with the exact same name, including capitalization. so here is the code snippet that works:
Code: send "/" waitOn #179 & "Turns " while (CURRENTANSILINE <> #13) :label1 getText 0 & #179 & CURRENTLINE & #179 & #179 $block $block & #179 #179 getWord $block $stat 1 getWord $block $quickStat[$stat] 2 stripText $quickStat[$stat] "," branch ($block = "") :label1 waitOn "" end
setVar $statNames "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" setVar $i 1 while ($i < 29) getWord $statNames $stat $i echo "*QuickStat " $stat " = " $quickStat[$stat] add $i 1 end
If quotes in braces (e.g. ["Sect"] ) were allowed, you could simply do this:
echo $quickStat["Sect"]
But since they aren't, you have to retrieve the value using a variable, like this:
setVar $index "Creds"
echo $quickStat[$index]
You may see me reference an index like ["Sect"] from time to time, but that is to clarify that it is stored in the array with that case, not that you can actually use that syntax.
So let me sum all of that up in 2 rules:
1. Case matters for array indices.
2. Unquoted text is always parsed as uppercase.
+EP+
_________________ Claim to Fame: only guy to ever crack the TW haggle algorithm, and fig/shield/hold price formula, twice.
|
| Sun Jun 10, 2007 1:49 pm |
|
 |
|
LoneStar
Commander
Joined: Fri Jun 09, 2006 2:00 am Posts: 1401 Location: Canada
|
okay. thanks. great work on everything.
_________________ ---------------------------- -= QUANTUM Computing 101: 15 = 3 x 5 ... 48% of the time.
|
| Sun Jun 10, 2007 5:22 pm |
|
 |
|
Dizturbed
Private 1st Class
Joined: Thu Nov 16, 2006 6:41 pm Posts: 2 Location: United States
|
ElderProphet wrote:
And finally, the same method tweaked to utilize BRANCH, taping out at 11 lines.
: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+
Now I know I’m a day late and a dollar short on getting in
on this discussion about quickstats, but I did find this thread
interesting. My quickstats routine is
sloppy at best compaired to other snippets of code here. Your gettext line in your code threw me for a
loop and when I ran it on my server I found that depending on how many lines
were involved with the quickstats, the script may not have gotten out of the
loop properly. On my serve, if
quickstats was 3 lines then the script would hang on the waiton command. For whatever reason if the quickstats was 4
lines it would work like a champ.
> >
Changing the while loop to while (CURRENTLINE <>
"") seemed to fix the trouble I had with it.
Dizturbed
|
| Sun Jun 10, 2007 6:46 pm |
|
 |
|
ElderProphet
Commander
Joined: Tue Oct 07, 2003 2:00 am Posts: 1134 Location: Augusta, GA
|
Agreed, Dizturbed is exactly right. I've tested this, and with only 3 lines in the quickstat, my CURRENTANSILINE trigger won't work. But what self-respecting RED doesn't have enough creds to force a 4-Line quickstat disply? Still, there is no problem testing for (CURRENTLINE <> ""), so the revised routines are below. Note that the second routine has been tweaked in a few areas and simplified a bit. All stat names and values are now uppercase, and the [CORP] and [PHOT] values are initialized to zero... and I think I fixed a bug.
Thanks Dizturbed.
+EP+
11-LINE VERSION
:quickStatSub
send "/"
waitOn #179 & "Turns "
while (CURRENTLINE <> "")
: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
VERSION WITH UPPERCASE STATS; CORP ALWAYS UPDATES; SHIP TYPE INCLUDED
:quickStatSub
setArray $quickStat 0
send "/"
waitOn #179 & "Turns "
while (CURRENTLINE <> "")
# This line means: Grab the block of text between the first set of dividers
getText #179 & CURRENTLINE & #179 & #179 $block #179 #179
# Boolean tests added to initialize CORP and PHOT to zero
while ($block <> "") and ($quickStat[CORP] <> "INITIALIZE_TO_ZERO") and ($quickStat[PHOT] <> "@Zero")
setVar $capsBlock $block
stripText $capsBlock ","
upperCase $capsBlock
getWord $capsBlock $stat 1
getWord $capsBlock $quickStat[$stat] 2
# This next line finally succeeds on the Ship number/type block
getWord $capsBlock $quickStat[TYPE] 3
getText CURRENTLINE & #179 & #179 $block $block & #179 #179
end
waitOn ""
end
return
_________________ Claim to Fame: only guy to ever crack the TW haggle algorithm, and fig/shield/hold price formula, twice.
|
| Mon Jun 11, 2007 2:22 am |
|
 |
|
Who is online |
Users browsing this forum: No registered users and 31 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
|
|