TinTin++ Mud Client The TinTin++ message board

 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
TinTin++ Mud Client

Provide some builtin functions

 
Post new topic   Reply to topic    The TinTin++ message board Forum Index -> Feature Requests
View previous topic :: View next topic  
Author Message
stef70



Joined: 22 Jan 2010
Posts: 9

PostPosted: Wed Aug 22, 2012 9:33 am    Post subject: Provide some builtin functions Reply with quote

The command #function can be used to create user-defined functions but, surprisingly, there are no predefined functions in tt++ (so implemented in C).

I believe that the tt++ language would be a lot more powerful if some predefined functions were provided.

Here is a non exhaustive list of functions that could be provided by default. Some of them are inspired from the perl builtin functions.

Some of them can be already be implemented using #function and some of the existing tt++ commands but a builtin implementation would be far more efficient. Also using builtin functions would be more convenient when distributing scripts.

I also suspect that after the initial implementation, adding new builtin functions should be a lot easier than adding new commands (no new syntax to manage in the core language). You could even provide a plugin system to let users create their own builtin functions in dynamic libraries.

Code:

@eval{expr}
   Evaluate a mathematical expression.
   This is equivalent to #math but as a function.

@max{{x}{y}...}     
   return the max of 2 or more values

@min{{x}{y}...}                 
   return the min of 2 or more values

@abs{x}
   return the absolute value of x

@raw{str}
    remove all escape sequences (colors,...) from  str
    For example, @raw{<abc>hello<099> world} returns 'hello world'
 
@index{{str}{substr}{pos}}
@index{{str}{substr}}
   Search for 'substr' in 'str' and return its offset or -1 if not found. 
   If 'pos' is specified then start searching at that position.
   For example,
     @index{{,}{a,bb,c,d}}     returns 1
     @index{{,}{a,bb,c,d}{2}}  returns 4
     @index{{,}{a,bb,c,d}{5}}  returns 6
     @index{{,}{a,bb,c,d}{5}}  returns -1

@rindex{{str}{substr}{pos}}
@rindex{{str}{substr}}

   Similar to @index but searching backward (starting from the end)   

@insert{@{old}{pos}{new}}
   
   Return 'old' with 'new' inserted at position 'pos'.
   For example, @insert{{abcdef}{3}{XY}} returns 'abcXYdef'
 
@overwrite{{old}{pos}{new}}

   Return 'old' with 'new' at position 'pos'
   For example, @overwrite{{abcdef}{3}{XY}} returns 'abcXYf'

@substr{{str}{pos}{len}} 
   return len characters starting from str[pos]. 
   if len is missing then return from str[pos] to the end of str

@strlen{str}                     
   return the number of characters in str

@trim{str}                   
   remove left and right blanks from string str.
   For example, @trim{  hello world  } returns 'hello world'     

@trimr{str}                   
   remove right blanks from string str.
   For example, @trim{  hello world  } returns '  hello world'     

@triml{str}                   
   remove left blanks from string str.
   For example, @trim{  hello world  } returns 'hello world  '     

@uc{str}                           
   return the upper-case version of str

@lc{str}                           
   return the lower-case version of str

@ucfirst{str}                   
   return str with its first character upper-case
   For example, @ucfirst{john} returns 'John'     

@lcfirst{str}                   
   return str with its first character lower-case
   For example, @lcfirst{ABC} returns 'aBC'

@checksum{str}
   compute a checksum for the string str.
   The checksums are not unique but the probabily of collision should be very low.
   Can be used to create some small indexes from a room name and its description.
   For example
      @checksum{At Recall}  could return 'V2tkIEF1ZyAyMiAxNT'
      @checksum{Old Tavern} could return 'jb2Rpbmctc3lzdGVtI'

@uuid{str}
   Similar to checksum but the result is insured to be unique for each string.
   The strings and their associated uuid are stored by tt++ so unlike checksums, uuid are not identical between multiple executions of tt++.

@sort{array}
@nsort{array}

   Return the elements of array sorted lexicographically or numerically.
   
   If $A is {john}{bob}{marc} then
     @sort{$A}  returns {bob}{john}{marc}
 
   If $A is {55}{-11}{22} then
     @nsort{A} returns {-11}{22}{55}

@sort1{map}
@sort2{map}
@nsort1{map}
@nsort2{map}

   similar to @sort and @nsort except that sort pairs of values
   according to the 1st or 2nd of each pair          

   For example, if $A is the map {john}{42}{bob}{33}{marc}{-11}
   then
      @sort1{$A}  returns {bob}{33}{john}{42}{marc}{-11}
      @nsort2{$A} returns {marc}{-11}{bob}{33}{john}{42}

@roomvnum{}
@roomcheck{vnum}
@roomname{vnum}
@roomarea{vnum}
@roomdata{vnum}
@roomexits{vnum}
...

    Get miscellaneous information about a room specified by its vnum
    or the current room if vnum is ommited.

    @roomcheck returns true if the room vnum exists else false.
 
@running{}

    Return 'yes' if tt++ is currently executing a '#map run'
    or '#map travel' command


[/code]
Back to top
View user's profile Send private message
Slysven



Joined: 10 Apr 2011
Posts: 365
Location: As "Jomin al'Bara" in WoTMUD or Wiltshire, UK

PostPosted: Fri Aug 24, 2012 8:12 am    Post subject: Reply with quote

Well there is a certain reluctance to add duplicate functionality to the existing code, for instance the first one you suggest can be achieved with:
Code:
#FUNCTION {eval}
{
   #math result %0
}
Picking up on one suggestion of yours, a checksum so that an index of room name and descriptions could be made reminds me that I should publish a modification I wrote and have been using successfully for a few months to do just that (well it indexes a hash of the room's description so I can establish matching ones VERY fast.)

Some of the string handling can be achieved with #FORMAT, check the latest version for things like the %L for string length. Mind you the case stuff is not as simple as it seems - what about the UTF-8 and BIG5 situations where what you call a character may not be!

Anyway perhaps others would like to post implementations of the above suggestions...
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    The TinTin++ message board Forum Index -> Feature Requests All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
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
Get TinTin++ Mud Client at SourceForge.net. Fast, secure and Free Open Source software downloads Get TinTin++ Mud Client at SourceForge.net. Fast, secure and Free Open Source software downloads
TinTin++ Homepage

Powered by phpBB © 2001, 2002 phpBB Group