|
Stowslee Wanderer
Joined: 20 Apr 2008 Posts: 69 Location: Watervliet, NY
|
Posted: Sun Feb 22, 2009 10:37 am
Making a screen display - fitting things correctly |
Here is the basic situation, when I send SCORE to the Mud I get a listing of a good deal of information. I am working on making it into a much easier to read, and more useful format by adding in things of my own. The code below works great until something changes in length, and then the whole appearance of that line is off. Is there a way to make sure the line ends in the same place regardless of how the variable changes.
Example - @LevelName could be Satusian or 1st Order of the Hyperphysical the change in spacing with each would be considerable. And I would prefer not to have to change it each time I gain a level or some small detail changes.
Code: |
#ga
#gagb
#show {" ------------------------------------------------------------ "}
#show {" Sanguinic Warrior, Saervok Nebre'seir "}
#show {" Undead Male Atavian "}
#show {" "Age: @Age "Years Old"}
#show {" "Level:" "@LevelNum" "Exp: @Experience"% "@LevelName}
#show {" ------------------------------------------------------------- "}
#show {" _____________________________________________________________"}
#show {"| |"}
#show {| Health: @CurrentHealth"/"@MaxHealth" " Mana: @CurrentMana"/"@MaxMana" " |}
#show {| Kidney Heal At: @HealKidney" "Endurance: @CurrentEndurance"/"@MaxEndurance" |"}
#show {| Analeptic Heal: @HealAnaleptic" "WillPower: @CurrentWillpower"/"@MaxWillpower" |"}
#show {|" "Blood: @Blood"/"@MaxBlood" "|}
#show {"|_____________________________________________________________|"}
#show {""}
#show {" _____________________________________________________________"}
#show {"| |"}
#show {|" "Aetolia Rank: @AetolianRank" "Combat Rank: @CombatRank" "|}
#show {|" "House: Nebre'seir" "House Rank: @HouseRank" "|}
#show {|" "Guild: @Guild" "Guild Rank: @GuildRank" "|}
#show {|" "Class: @Class" "Class Status: @ClassStatus" "|}
#show {|" "Citizen Of: @City" " City Rank: @CityRank" "|}
#show {|" "Explorer Ranking:" "@ExplorerRank" "|}
#show {"|_____________________________________________________________|"}
#cr |
At this time this is what the output looks like:
Code: |
------------------------------------------------------------
Sanguinic Warrior, Saervok Nebre'seir
Undead Male Atavian
Age: 48 Years Old
Level: 80 Exp: 67% Satusian
-------------------------------------------------------------
_____________________________________________________________
| |
| Health: 4260/4260 Mana: 2780/2780 |
| Kidney Heal At: 3408 Endurance: 20000/20000 |
| Analeptic Heal: 3195 WillPower: 12800/12800 |
| Blood: 26000/26000 |
|_____________________________________________________________|
_____________________________________________________________
| |
| Aetolia Rank: 248 Combat Rank: 90 |
| House: Nebre'seir House Rank: Hesed |
| Guild: Bloodborn Guild Rank: Lower Novitiate |
| Class: Bloodborn Class Status: Apprentice |
| Citizen Of: Bloodloch City Rank: Slayer of Orphans |
| Explorer Ranking: Desert Roamer |
|_____________________________________________________________|
|
|
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Sun Feb 22, 2009 3:30 pm |
You can use the %format function to return a string of a certain length. %format("&80s","lol") will return "lol" preceded by 77 spaces. %format("&-80s","lol") will return "lol" followed by 77 spaces. You can do centre alignment as well by chopping the string in half with %len, %left and %right.
Also, you don't need to use both {} and "". |
|
|
|
Stowslee Wanderer
Joined: 20 Apr 2008 Posts: 69 Location: Watervliet, NY
|
Posted: Sun Feb 22, 2009 7:13 pm |
Excellent, I will play around with that.
|
|
|
|
Ralph1971 Wanderer
Joined: 01 Mar 2008 Posts: 64
|
Posted: Sun Feb 22, 2009 9:09 pm |
that will work, if the string length is going to be 3 (lol).. but how do you handle if, say.. the mana changes from a 4 digit number to a 2 digit number?
|
|
_________________ Win7-home - Cmud 3.33a
WinXP-pro - Zmud 7.21 |
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Sun Feb 22, 2009 10:08 pm |
That's the whole point of the function - it will pad the string with spaces at whichever end you specify, up to the total length you specify. The string could be any length and it'll always be padded with enough spaces to give you a string that totals 80 characters.
|
|
|
|
Stowslee Wanderer
Joined: 20 Apr 2008 Posts: 69 Location: Watervliet, NY
|
Posted: Mon Feb 23, 2009 7:34 am |
Could you do me a favor and write up an example, I have been playing around with the functions for a few hours and just can't seem to put them together quite right.
|
|
|
|
charneus Wizard
Joined: 19 Jun 2005 Posts: 1876 Location: California
|
Posted: Mon Feb 23, 2009 8:15 am |
Try using:
Code: |
#FUNCTION centerpadding {%if(%len(%repeat(%3,%eval((%2-%len(%1))/2))%1%repeat(%3,%eval((%2-%len(%1))/2)))!=%2,%repeat(%3,%eval((%2-%len(%1))/2))%1%repeat(%3,%eval(1+(%2-%len(%1))/2)),%repeat(%3,%eval((%2-%len(%1))/2))%1%repeat(%3,%eval((%2-%len(%1))/2)))} |
It's something similar to what I use in CMUD. The syntax would be:
@centerpadding("String here",Length of complete line,"Character to use as padding")
So...
@centerpadding("Sanguinic Warrior, Saervok Nebre'seir",79," ")
will return 'Sanguinic Warrior, Saervok Nebre'seir' centered on a line of 79 characters, using spaces as padding.
(like this, except the dashes will be spaces)
Quote: |
---------------------Sanguinic Warrior, Saervok Nebre'seir--------------------- |
Charneus |
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Mon Feb 23, 2009 11:53 am |
Bah, the forums are eating my extra spaces - imagine that where there's " Endurance:", there're actually four more spaces. You get the picture, I'm sure.
That's a terribly complex way of doing it, Charneus. %format will handle most of those calls for you.
First let's stick with something easy, right-alignment. I'm going to use the tenth line, the "kidney heal" line. So, what you want is "| Kidney Heal At:", followed by a five-character string that contains the variable @HealKidney and as many spaces as needed to get it up to five characters. This bit is very easy:
#say %concat("| Kidney Heal At:",%format("&5s",@HealKidney))
You can test this out by changing the value of @HealKidney to different lengths:
HealKidney=10
#say %concat("| Kidney Heal At:",%format("&5s",@HealKidney))
HealKidney=100
#say %concat("| Kidney Heal At:",%format("&5s",@HealKidney))
HealKidney=1000
#say %concat("| Kidney Heal At:",%format("&5s",@HealKidney))
And it's all nicely aligned.
For the rest of the line, you now want five spaces and the string "Endurance:" - that's easy enough to add:
#say %concat("| Kidney Heal At:",%format("&5s",@HealKidney)," Endurance:")
The next part depends on how you want to format it. You could either do the current and max hp values separately, giving them each a five- or six-character string and keeping the position of the slash fixed. That would look like this:
#say %concat("Endurance:",%format("&6s",@CurrentEndurance),"/",%format("&6s",@MaxEndurance))
And this can be added to the first half of the string to give you the whole line:
#say %concat("| Kidney Heal At:",%format("&5s",@HealKidney)," Endurance:",%format("&6s",@CurrentEndurance),"/",%format("&6s",@MaxEndurance))
You might prefer doing it this way, though:
#say %concat("Endurance:",%format("&12s",%concat(@CurrentEndurance,"/",@MaxEndurance))
Which uses a single 12-character string for both numbers and the slash. On the line with the kidney part, it'll look like this:
#say %concat("| Kidney Heal At:",%format("&5s",@HealKidney)," Endurance:",%format("&12s",%concat(@CurrentEndurance,"/",@MaxEndurance)))
You can do similar stuff with left-alignment - it's just about deciding how long you want the resulting string to be and slotting the dynamic bits between the static bits.
Finally, centre-alignment. I'll show you this with the second line of your example, pretending that the name can be held in a variable. I'm going to make the line 80 characters wide, because that's a pretty common wrap-length:
Name="Sanguinic Warrior, Saervok Nebre'seir"
FirstHalf=%left(@Name,%eval(%len(@Name)/2))
SecondHalf=%right(@Name,%eval(%len(@Name)/2))
#say %concat(%format("&40s",@FirstHalf),%format("&-40s",@SecondHalf)
You can test this code out with strings of any length to see what it does. Basically, it splits it in half and right-aligns the front half and left-aligns the back half. You can change the length (and, indeed, do strange wraps like three-quarters left and right) by changing the 40s and -40s values. |
|
|
|
Leitia Adept
Joined: 04 May 2007 Posts: 292 Location: Boston
|
Posted: Mon Feb 23, 2009 2:03 pm |
Also if you have number data from score in a variable and use, say, &12s for strings, those wont display at all if your variable is a number or null. Probs you saw that there are number options in format, but thought it was worth mentioning as the help file assumes some genius I don't have.
Here is one using numbers and strings:
Code: |
#sh %format( " &-3.0f &-12s &3.0f &-9s &-5s &-6s &-40s", %numkeys( @ShortNames), %i, %item( %db( @BaseVialsAll, %i), 2), uses~, %item( %db( @BaseVialsAll, %i), 3), mo. ~- , %item( %db( @BaseVialsAll, %i), 4) to do, ~- %db( @Baselongnames, %i)) |
|
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Mon Feb 23, 2009 2:14 pm |
I'm not sure what you're getting at Leitia. Numbers will be returned just fine as strings if you use the "s" format modifier on them - I demonstrate this in my post above. The other formats like "f" and "n" are an extra level of complexity and aren't needed in these simple cases.
|
|
|
|
charneus Wizard
Joined: 19 Jun 2005 Posts: 1876 Location: California
|
Posted: Mon Feb 23, 2009 3:14 pm |
Hmmm. Hadn't known that about %format, Fang. I'm going to have to rewrite some of these padding functions now.
Mine served me well, regardless. Guess there are always more than one way to skin a cat. :)
Charneus |
|
|
|
Stowslee Wanderer
Joined: 20 Apr 2008 Posts: 69 Location: Watervliet, NY
|
Posted: Mon Feb 23, 2009 4:30 pm |
Thank you guys very much, that was very helpful
|
|
|
|
Stowslee Wanderer
Joined: 20 Apr 2008 Posts: 69 Location: Watervliet, NY
|
Posted: Mon Feb 23, 2009 5:09 pm |
Code: |
#say %concat("| Kidney Heal At:",%format("&5s",@HealKidney)," Endurance:",%format("&6s",@CurrentEndurance),"/",%format("&6s",@MaxEndurance))
|
Gives me this output:
Code: |
| Kidney Heal At: Endurance:/
|
All variables are enabled so accessing them should be no trouble. |
|
|
|
calesta Apprentice
Joined: 07 Dec 2008 Posts: 102 Location: New Hampshire, USA
|
Posted: Mon Feb 23, 2009 6:49 pm |
According to the help file %format returns null if there is a problem with any of the format specifiers, so it appears as if it thinks there is something wrong with them here. As far as I can tell &s will not work with numbers and &d will not work at all. &s does work with strings and &n does work with numbers (although there is no way to get rid of the commas with this one). I didn't try the other format specifiers, so not sure what else may or may not work in zMUD. The closest I could come to what you were trying that seems to work is:
Code: |
#say %format("| Kidney Hail At:&5.0n Endurance:&6.0n/&6.0n", @HealKidney, @CurrentEndurance, @MaxEndurance) |
None of those problems seem to exist in CMUD, what you posted works fine there. I was using zMUD 7.21 when I tried this. |
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Mon Feb 23, 2009 9:50 pm |
Ah, that'll be where the problems came from then - I used CMUD to test this since I don't have zMUD installed any more.
|
|
|
|
charneus Wizard
Joined: 19 Jun 2005 Posts: 1876 Location: California
|
Posted: Tue Feb 24, 2009 12:41 am |
By the way, Fang... was thinking about your %format suggestion, and then I realized why I use what I use.
It allows more versatility. You can use any number (say you want to change it down the future, or use it in different scripts). It also allows for someone to use any type of character they want (using -, for example, or even *). Not sure how to achieve these two functions any other way. Only reason it has the 1+ in there is just in case a person uses an odd length string (like a string with 5 characters). It won't match up properly. *shrug*
If you've got a better suggestion, I'm open to it, though.
Charneus |
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Tue Feb 24, 2009 10:01 am |
Using any number:
%format(%concat("&",%1,"s"),%2)
For centre alignment, you can either require half the line length as a parameter, or just halve it.
Using any character:
%subregex(%format("&40s",%1),"^\s+",%2)
%subregex(%format("&-40s",%1),"\s+$",%2)
If you're sure that the string you're using will never contain spaces, you could just use %replace or %subchar instead. |
|
|
|
Stowslee Wanderer
Joined: 20 Apr 2008 Posts: 69 Location: Watervliet, NY
|
Posted: Wed Feb 25, 2009 8:15 am |
One last question, if I wanted to color certain attributes within this code how would I go about it?
|
|
|
|
Leitia Adept
Joined: 04 May 2007 Posts: 292 Location: Boston
|
Posted: Thu Feb 26, 2009 7:22 am |
Fang, your center align is very cool, ty
I replied offhand to the subject based on the ops mention of SCORE. The ops intent seemed to be art and I wanted to help in some way.
for me: #sh %format( "&-12s", @test22) will not work if test22=22 or 10 or 3
I just thought this was one of those 'druthers' projects which would lead in other directions and wanted to say what happened to me. I really was not trying to solve anything in particular. |
|
|
|
|
|
|
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 vote in polls in this forum
|
|