[Script] [Util] Chem String Functions Library v. 2.81 [18 Aug 2007]

The place to discuss scripting and game modifications for X³: Reunion.

Moderators: Moderators for English X Forum, Scripting / Modding Moderators

ChemODun
Posts: 320
Joined: Mon, 12. Feb 07, 21:58
x3tc

[Script] [Util] Chem String Functions Library v. 2.81 [18 Aug 2007]

Post by ChemODun » Mon, 26. Mar 07, 17:13

################################################################################
Script : Chem String Functions Library
Author : Chem
E-mail : chemodun@gmail.com
Web : http://www[dot]elite-games[dot]ru/x3/
Version: 2.81
Desc. :Provide some functions for manipulate with string,
based on their displayed width
################################################################################

_____________
Download:
Spk version
Zip version


History :

2.81
-added new function 'length', it return displayed width of string. If second parameter are [TRUE] - then length will be returned in spaces, otherwise - in pseudosymbols
2.78
-fixed error in functions 'fill.spaces', if used possibility read string from file - it's work incorrectly (Big thanks to X-Freak Cartman [KBG])
-used new version of .spk
2.76
-fixed error with aligning in functions 'fill.spaces', where aling = [FALSE] (Was - filled left, but must be - filled right)
2.75
- fixed error in definition aligining in functions 'fill.spaces', 'fill.sum.to.string', 'fill.number.to.string'
- added new function 'sprintf'
- functions 'fill.spaces', 'sprintf', 'split.string.by.delim' can use as parameters string, and integer vaules( for reading strings from text files)
- all functions corretly work with 2 bytes symbols (like Russian)
- if string are longer then must be displayet - it's truncated
2.09 - first public release

_____________
Installation :

Install via Cycrow Script Manager (Plugin Manager)

_____________
Use:

Libraries has two script files:
setup.lib.chem.string.xml - this file prepare special array for filling string by space.
Script doesn't have any parameters.
lib.chem.strings.xml - main library script.
Script has 6 parameters:
mode (Work Mode) - for defining which function called from library
Arg1..Arg5 - for setting functions parameters
Now in version 2.81 library provide 8 functions:
1. split.string.by.delim
Arg1 - string-delimeter - string, or string from text file by page id Arg1 and text id Arg2
Arg2 - string-source - string, or string from text file by page id Arg2(or Arg3) and text id Arg3 (or $Arg4)
Arg3 - max count parts of source string (if null - max count = 99)
return - array of found parts of source string
It's function can split source string (first) by delimeter (second).
You can set maximum count of parts for splitting (Arg3), i.e. if source has more then one occurrence of delimiter , but you set Arg3 = 2 - function will return array with only 2 elements, first - source string before first occurrence of Arg1 in Arg2, and second - all source after this first occurrence.

2.'fill.spaces' - you can create string with correct displayed width in player logbook, i.e. after using this function on, for example, strings "this is long string" and "small string", they will be filled by spaces up to they width in pixels (not symbols) will be equal.
Arg1 - source string, or string from text file by page id Arg1 and text id Arg2
Arg2 or Arg3- max len (in spaces, i.e. count of sybols " ")
Arg3 or Arg4- fill
if bool
TRUE - fill left (align right)
FALSE - fill right (align left)
if string
'-' - align left
'=' - align center
'+' - align right
It's function will fill source string by spaces based not on count of symbols in string, but on their summary width on screen in player logbook.
I.e. default length for one symbols it this function will taken equal to two spaces.
In fact some symbols have length more then two spaces, some - less.
Function use special array, was prepared in setup script by experimental data form text files x8910.xml.
Now supported only English, Russian, and German languages.
If length source string more then needed length - string will be truncated.
See examples:
If you browser support animated gif -
[ external image ] [ external image ]

Or if not:
output without 'fill.spaces'
[ external image ][ external image ]
output wtih 'fill.spaces'
[ external image ][ external image ]

3,4 'sum.to.string' and 'number.to.string'
Arg1 - integer
return - string
will do convert number 1234567890 to string like 1,234,567,890 or
will do convert sum 1234567890 to string like 1,234,567,890 Cr (add default name of games currency)

5,6 'fill.sum.to.string' and 'fill.number.to.string'
Arg1 - integer
Arg2 - max length
Arg3 - TRUE - fill left (align right) or FALSE - fill right (align left)
return - string
will do convert number 1234567890 to string like 1,234,567,890 or
will do convert sum 1234567890 to string like 1,234,567,890 Cr (add default name of games currency)
Then fill converted string as it can be done via 'fill.spaces'
7 'sprintf'
Arg1 -
if string
- then format string
else if number
- page id for format string
Arg2
if Arg1 = page id
- then text id for format string
else if
- array - then array of params
else
- params
Arg3..Arg5 - params or array of params like Arg2

if param is string and has form (page;id) then it will be replaced with string from text file

Function works like sprintf function from ScE, but use unlimited count of params and extended syntax:

%[align][width]specifier
align:
- - left
= - center
+ - right (default)
width: needed width of string in pseudo symbols
specifier:
n - number
c - currency
s - string

8 'length'
Arg1 - source string, or string from text file by page id Arg1 and text id Arg2
Arg2 or Arg3- - type of measurement units.
if [TRUE] - then result will be returned in spaces
otherwise - in pseudosymbols.
This function will return displayed length of source string.

_____________
Uninstallation:

Uninstall via Cycrow Script Manager (or Plugin Manager)


_____________
Languages:
Russian
English
Deutsch - thanks to Unitrader [KGB]

_____________
Info:
Uses
Text Files Used : 8910
Text Pages Used:8910
_____________
Last edited by ChemODun on Sat, 18. Aug 07, 22:27, edited 14 times in total.
Multiply entropy by absolute zero

Ñâîáîäà ñðåäè çâåçä / Freedom in space

jgrg1
Posts: 9
Joined: Sun, 3. Aug 03, 17:13

Post by jgrg1 » Mon, 30. Jul 07, 00:36

Does anyone have a working link to this script? The one in the post doesn't seem to work.

Regards
Justin

sphr
Posts: 119
Joined: Tue, 15. Aug 06, 17:16
x4

Post by sphr » Mon, 30. Jul 07, 06:01

Is Chem still around and interested in maintaining the lib?
If not, I was thinking it would be possible to get permission to continue to maintain this lib as a part in the Common Library that I proposed much earlier.

ChemODun
Posts: 320
Joined: Mon, 12. Feb 07, 21:58
x3tc

Post by ChemODun » Mon, 30. Jul 07, 08:02

sphr wrote:Is Chem still around and interested in maintaining the lib?
If not, I was thinking it would be possible to get permission to continue to maintain this lib as a part in the Common Library that I proposed much earlier.
still :-)

Sorry, host was down.
Now hosten on Xai Corp - great host ;-)
thanks Moggy!
Multiply entropy by absolute zero

Ñâîáîäà ñðåäè çâåçä / Freedom in space

jgrg1
Posts: 9
Joined: Sun, 3. Aug 03, 17:13

Post by jgrg1 » Mon, 30. Jul 07, 10:57

Fantasico! Cheers.

ChemODun
Posts: 320
Joined: Mon, 12. Feb 07, 21:58
x3tc

Post by ChemODun » Mon, 30. Jul 07, 12:23

When I finish writing readme - you can use new function:
All old are still exists, for backward compability.
new wrote: sprintf - like standart in ScE, but with new format:
%[align][width]specifier
align:
- - left
= - center
+ - right (default)
width:
needed width of string in pseudo symbols
specifier:
n - number
c - currency
s - string
compability with first release wrote: sum.to.string
number.to.string
fill.spaces
fill.sum.to.string
fill.number.to.string
updated wrote: split.string.by.delim - now work wtih two bytes symbols (like russian). Additionally it will have short name split
Multiply entropy by absolute zero

Ñâîáîäà ñðåäè çâåçä / Freedom in space

User avatar
X-Freak Cartman
Posts: 4017
Joined: Mon, 2. Oct 06, 15:08
x3

Post by X-Freak Cartman » Fri, 3. Aug 07, 16:28

Hmm... Seems, as if the readme's a bit wrong:
'fill.spaces': Arg3 [TRUE]->right, [FALSE]->left.

The code says, that -1 means left.

I wondered why it didn't work ^^

ChemODun
Posts: 320
Joined: Mon, 12. Feb 07, 21:58
x3tc

Post by ChemODun » Fri, 3. Aug 07, 18:48

New version - 2.75
- fixed error in definition aligining in functions 'fill.spaces', 'fill.sum.to.string', 'fill.number.to.string'
- added new function 'sprintf'
- functions 'fill.spaces', 'sprintf', 'split.string.by.delim' can use as parameters string, and integer vaules( for reading strings from text files)
- all functions corretly work with 2 bytes symbols (like Russian)
Multiply entropy by absolute zero

Ñâîáîäà ñðåäè çâåçä / Freedom in space

User avatar
X-Freak Cartman
Posts: 4017
Joined: Mon, 2. Oct 06, 15:08
x3

Post by X-Freak Cartman » Fri, 3. Aug 07, 22:48

ChemODun wrote:all functions corretly work with 2 bytes symbols (like Russian)
Hmm... either you changed the function of 'fill.spaces' to 'simply add spaces' or the sentence above is a bit wrong.

Seems as if your script simply adds spaces if I use the same arguments i used for version 2.09... Afaik, it should be compatible, shouldn't it?

I'll try to find an error in your code, but now, the new version is useless to me...

btw: Arguments
mode: 'fill.spaces'
Arg1: 'Testtext'
Arg2: 1, 2, 3, 5, 10, 20 and 30.
Arg3: 1, TRUE, -1

In version 2.09, the first five Arg3's had the same effect, now the number of spaces seems to simply be added.

ChemODun
Posts: 320
Joined: Mon, 12. Feb 07, 21:58
x3tc

Post by ChemODun » Sat, 4. Aug 07, 00:40

Thanks, fixed for Arg3 = [FALSE] (must be fill right, i.e align left).

2.76
-fixed error with aligning in functions 'fill.spaces', where aling = [FALSE] (Was - filled left, but must be - filled right)

But I don't understand, what wrong?

I was check with

Code: Select all

001   load text: id=8910
002   $out = ' '
003   $arg1 = 'Testtext'
004   $arg2 =  array alloc: size=0
005   append 1 to array $arg2
006   append 2 to array $arg2
007   append 3 to array $arg2
008   append 5 to array $arg2
009   append 10 to array $arg2
010   append 20 to array $arg2
011   append 30 to array $arg2
012   $arg3 =  array alloc: size=0
013   append 1 to array $arg3
014   append [TRUE] to array $arg3
015   append -1 to array $arg3
016   append [FALSE] to array $arg3
017   
018   $arg3.l =  size of array $arg3
019   while $arg3.l
020     dec $arg3.l = 
021     $arg3.c = $arg3[$arg3.l]
022     $arg2.l =  size of array $arg2
023     while $arg2.l
024       dec $arg2.l = 
025       $arg2.c = $arg2[$arg2.l]
026 @     $result = [THIS] -> call script 'lib.chem.strings' :  Work Mode='fill.spaces'  Arg1=$arg1  Arg2=$arg2.c  Arg3=$arg3.c  Arg4=null  Arg5=null
027       $result = sprintf: fmt='|%s| arg2=%s, arg3=%s', $result, $arg2.c, $arg3.c, null, null
028       $out = sprintf: pageid=8910 textid=1098, $out, $result, null, null, null
029     end
030   end
031   write to player logbook $out
032   return null
[ external image ]
[ external image ]
[ external image ]

Or I want don't truncate string, if it more long, then Arg3 ?
Multiply entropy by absolute zero

Ñâîáîäà ñðåäè çâåçä / Freedom in space

User avatar
X-Freak Cartman
Posts: 4017
Joined: Mon, 2. Oct 06, 15:08
x3

Post by X-Freak Cartman » Sat, 4. Aug 07, 04:31

Great, this version fixed it.

Thxalot for this script. It's awesome.

User avatar
X-Freak Cartman
Posts: 4017
Joined: Mon, 2. Oct 06, 15:08
x3

Post by X-Freak Cartman » Fri, 17. Aug 07, 16:12

What about a new version 2.77?

I could offer a Download. 2.76 has a major problem I recognised while using it for GAIUS:

Code: Select all

087     end
089     $source = $Arg1
090     $out.len = $Arg2
091     skip if not $debug
Whatever $Arg.String has been defined as in the read-text-subsection of the script, it always used Arg1 as source and Arg2 as length.

Well, there's an easy fix for that:

Code: Select all

087     end
088     $source = $Arg.String
089     skip if not $debug
That's all. It works properly and is ready for Download.

btw: You're allowed to use my server as a mirror. Just send me a PN and/or e-mail if you're releasing a new version and I'll upload it.

Edit: Links removed
Last edited by X-Freak Cartman on Sat, 18. Aug 07, 12:27, edited 1 time in total.

ChemODun
Posts: 320
Joined: Mon, 12. Feb 07, 21:58
x3tc

Post by ChemODun » Fri, 17. Aug 07, 16:24

Many thanks.
Sure I will check it tomorrow.
Multiply entropy by absolute zero

Ñâîáîäà ñðåäè çâåçä / Freedom in space

ChemODun
Posts: 320
Joined: Mon, 12. Feb 07, 21:58
x3tc

Post by ChemODun » Sat, 18. Aug 07, 01:38

Thanks,
You can delete and string
088 $source = $Arg.String
I was delete this , test with debug - all works fine.

Thanks.
New version - are 2.78
Multiply entropy by absolute zero

Ñâîáîäà ñðåäè çâåçä / Freedom in space

User avatar
X-Freak Cartman
Posts: 4017
Joined: Mon, 2. Oct 06, 15:08
x3

Post by X-Freak Cartman » Sat, 18. Aug 07, 12:25

so... why don't you change the thread's name? :D

Edit: Much better now ^^

User avatar
X-Freak Cartman
Posts: 4017
Joined: Mon, 2. Oct 06, 15:08
x3

Post by X-Freak Cartman » Sat, 18. Aug 07, 14:28

While working at a statistics script, I recognised that there was a simple error: The text (ship name) was too long, so it took two lines - which looked pretty... strange.

So, to the point, now i need a function that tells me either how long a string is (in spaces) or how many lines it takes when using a specific format ([text cols='4'] for example.)

So who'd I have to ask for that? You of course.

btw: There are 21 spaces fitting in one line, if you use [text cols='
4']. I didn't test 1,2 and 3 and colspacing yet... but as there aren't more than 4 cols possible, it shouldn't be a huge problem.

It's awesome if you can include such a function (or maybe both of 'em?), as i really need it for GAIUS.

ChemODun
Posts: 320
Joined: Mon, 12. Feb 07, 21:58
x3tc

Post by ChemODun » Sat, 18. Aug 07, 14:48

Understand.

But, if you don't use in each column any "[select]" (i.e. only one [select][/select] per screen line) - you don't need to use [text cols="x"].

You can use format string like

Code: Select all

  <t id="20025">%-15s\033C%-40s\033X\033C%-40s\033X\n</t>
  <t id="20026">%-15s\033W%-40s\033X\033A%-40s\033X\n</t>
  <t id="20027">%-25s\033W%-50s\033X\n</t>
with function "sprintf" from my library.
Max width of message screen are 100 symbols.
for when you use [text cols] - we can define width only via experiments.
Multiply entropy by absolute zero

Ñâîáîäà ñðåäè çâåçä / Freedom in space

User avatar
X-Freak Cartman
Posts: 4017
Joined: Mon, 2. Oct 06, 15:08
x3

Post by X-Freak Cartman » Sat, 18. Aug 07, 18:58

For me, it's enough if you can return the length of the string in spaces. It'll be no problem for me to use it for my script ^^

The space-length of strings isn't only a problem for texts with more than a single column, it's also a problem i had with some other strings. It doesn't matter if you use your way or the column-way, however, the text has to be splitted to be shown correctly. The only question is, how to decide, where it has to be split.

My idea was to split the text into its single words by using your lib:
"Xenon Barracuda Raider" becomes an array including "Xenon", "Barracuda" and "Raider". Now, i can try, if they fit into one column (no matter if it's made by using "real" columns or a text-trick).

For example:
"Xenon" fits into a column.
"Xenon Barracuda" also fits into a column
"Xenon Barracuda Raider" doesn't fit into a column as it's longer than the given 21 spaces. So, the text has to be split into the following pieces:
"Xenon Barracuda" and "Raider".

That's done by the engine itself. The only thing to know is, how many lines it takes. As the text has to be split once, it takes two lines. That's all ^^

Maybe, you can include a function which tells, how many lines the text Arg1 takes if the text can be Arg2 wide in maximum.

Just an idea ^^

Cycrow
Moderator (Script&Mod)
Moderator (Script&Mod)
Posts: 22227
Joined: Sun, 14. Nov 04, 23:26
x4

Post by Cycrow » Sat, 18. Aug 07, 19:10

there already is a string length command in the script editor

ChemODun
Posts: 320
Joined: Mon, 12. Feb 07, 21:58
x3tc

Post by ChemODun » Sat, 18. Aug 07, 21:00

Cycrow wrote:there already is a string length command in the script editor
No.
My libraru works with displayed length of symbols.

Becouse font in message window are not monospace, then all symbols has different length.
I.E.
word
"fighters"
and
"destroer"
have identical length in 8 symbols, but they have different legth on screen.
My library operate with displayed length of each symbol (and string).
I was use pseudo measurement unit - "one symbol".
witdh of "one symbol" equal 2 width of symbol " ", i.e. space.

Full width of message window in game are equal 100 pseudo symbols, i.e. 200 spaces.

fucntion "fill.spaces" and "sprintf" operates with this measurement units.
X-Freak Cartman [KBG] wrote: For example:
"Xenon" fits into a column.
"Xenon Barracuda" also fits into a column
"Xenon Barracuda Raider" doesn't fit into a column as it's longer than the given 21 spaces. So, the text has to be split into the following pieces:
"Xenon Barracuda" and "Raider".
Unerstand - ok, I will try ;-)
Last edited by ChemODun on Sat, 18. Aug 07, 21:01, edited 1 time in total.
Multiply entropy by absolute zero

Ñâîáîäà ñðåäè çâåçä / Freedom in space

Post Reply

Return to “X³: Reunion - Scripts and Modding”