Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Shared functions pack (https://forums.graalonline.com/forums/showthread.php?t=81277)

Skyld 08-17-2008 12:56 AM

Shared functions pack
 
I'm happy to introduce the new clientside shared functions pack. It's a clientside script added to players when they log into Graal, providing new public functions that are available for all servers to use. These are general functions that you might just find handy to speed the scripting process up. It also enables us to provide some new functions quickly without client updates.

Currently the pack just provides a few odd functions, however more will become available in due course. If you have some clientside functions or amendments to existing functions (for instance, new encryption algorithms) that you feel people may benefit from and don't mind sharing, send me them in a forum PM and I'll check them over. If suitable, they might find themselves in this function pack. :) Similarly if you have any ideas, suggestions or bug reports, just let me know or post them here.

Functions

shared.getfontzoom(size) (contributed by Xzirox)
Returns the font zoom depending upon the text size that is set in the player's F3 options.

shared.hextorgb(string) (contributed by Tig)
Converts the RGB-format hex string into an array of {red, green, blue} values. For instance, "#FF0000" comes out as {1, 0, 0}.
PHP Code:

shared.hextorgb("#FA32BF"); 

shared.rgbtohex(red, green, blue) (contributed by Tig)
Converts the RGB into an RGB-format hex string. Essentially the reverse of the above function.
PHP Code:

shared.rgbtohex(10.40.7); 

shared.encrypt(algorithm, string, key)
Encrypts the string with the given key. There are two algorithms available: ENC_SIMPLE and ENC_SIMPLE_NOBASE64. The first is basically the simple encryption algorithm that I posted a while ago, while the second is the same algorithm but without the base64 conversion (so you get less overhead but stranger output characters at times).
PHP Code:

temp.encrypted shared.encrypt(ENC_SIMPLE"test""foo"); 

shared.decrypt(algorithm, string, key)
Decrypts in exactly the same way that the encryption function above works. Provide the encrypted data as the "string", and use the same key to decrypt.
PHP Code:

temp.encrypted shared.encrypt(ENC_SIMPLE"test""foo");
temp.decrypted shared.decrypt(ENC_SIMPLEtemp.encrypted"foo"); 

shared.chat(chat text)
Sets the player's chat text using the privileged setchat function. This means that the chat text works exactly like the chatbar. It interprets things like "showscriptstats" but is set to strip out toalls and toguilds.
PHP Code:

shared.chat("showscriptstats"); 

shared.tan(a)
Provides the missing trigonometry tan() function.

shared.getimgcenter(image name)
Returns an array of {x, y} where the X and Y values correspond to the center of the image relative from the top left of the image.
PHP Code:

shared.getimgcenter("block.png"); 

shared.getdisplayname()
Returns a friendly way of referring to the player on-screen. Guest users will see "Guest User", otherwise the community name/account name are returned instead.

shared.pmswaiting()
Returns an array of player objects from whom there is a PM waiting to be read. This includes external players from other servers.
PHP Code:

for (temp.plshared.pmswaiting())
{
  
//


shared.roundto(value, places)
Rounds the value to the given number of places.
PHP Code:

shared.roundto(3.140.1); 

Functions as of 20080826

shared.stringmerge(string, chars[])
Returns a reformatted string stripping whitespaces and any other single characters provided in the chars array.
PHP Code:

shared.stringmerge("One, two!", {",""!"}); 

shared.setconstants_showstats()
Sets globally scoped variables clientside for showstats to replace 0x? values for easier script comprehension (STATS_ASD, STATS_ICONS, STATS_RUPEES, STATS_BOMBS, STATS_ARROWS, STATS_HEARTS, STATS_AP, STATS_MP, STATS_MINIMAP, STATS_INVENTORYNPCS, STATS_PLAYERS, STATS_RIGHTCLICK_PROFILE).
PHP Code:

shared.setconstants_showstats();
showstats(allstats STATS_BOMBS STATS_ARROWS); 

A script only needs to run shared.setconstants_showstats() once on the clientside for this to be effective.

shared.setconstants_enablefeatures()
Sets globally scoped variables clientside for enablefeatures to replace 0x? values for easier script comprehension (EF_KEY_M, EF_KEY_P, EF_KEY_Q, EF_KEY_R, EF_KEY_SA, EF_KEY_SD, EF_KEY_TAB, EF_CHATTEXT, EF_HEARTSDISPLAY, EF_NICKNAMES, EF_MINIMAP_PMICONS, EF_RIGHTCLICK_PROFILE, EF_EMOTICONS, EF_KEY_ALT5, EF_KEY_ALT89, EF_F2OUTPUT).
PHP Code:

shared.setconstants_enablefeatures();
enablefeatures(allfeatures EF_CHATTEXT EF_NICKNAMES); 

A script only needs to run shared.setconstants_enablefeatures() once on the clientside for this to be effective.

Functions as of 20090530

shared.getAlignmentColor(ap)
Returns an array of {red, green, blue} for the given AP color, allowing you to recreate the default nickname colors.
PHP Code:

shared.getAlignmentColor(player.ap); 

shared.getAlignmentShadowColor(red, green, blue)
Returns an array of {red, green, blue} for the shadow of the given color, allowing you to recreate the default nickname shadows.
PHP Code:

shared.getAlignmentShadowColor(0.750.10.3); 

shared.replacetext(string, search, replace) (contributed by Dusty)
Returns a string. Searches string for search and replaces it with replace.

shared.getSharedFunctions
Returns an array of functions provided by the shared function pack.

Function added Jan 05, 2010

shared.adminMessage(text)
Opens an admin message window that will stay on the player's screen even after serverwarping until the player closes the window.
See: http://forums.graalonline.com/forums...98#post1548498
PHP Code:

shared.adminMessage("You have been disconnected by Tig"); 


Tigairius 08-17-2008 01:05 AM

Awesome. Thank you for this (shared.chat)!

DrakilorP2P 08-17-2008 01:24 AM

Good stuff.

xXziroXx 08-17-2008 01:34 AM

getfontzoom <3

Programmer 08-17-2008 01:56 AM

Thank you!

I've been looking for this kind of (native) support for a long time.

Perhaps you could also implement some of the functions in our (Chompy and I) thread: http://forums.graalonline.com/forums...ad.php?t=79473

Crow 08-17-2008 02:00 AM

I love you. I need somebody to spread rep, I want to rep Skyld for 3 times now D:

cbk1994 08-17-2008 02:00 AM

Very nice! Thank you, Skyld!

Quote:

Originally Posted by Tigairius (Post 1415011)
Awesome. Thank you for this (shared.chat)!

I agree, though Stefan finally added a setChat(chat) function to -Serverlist.

Skyld 08-17-2008 02:03 AM

Quote:

Originally Posted by cbk1994 (Post 1415024)
I agree, though Stefan finally added a setChat(chat) function to -Serverlist.

Not sure whether this will stay (or whether it'll just be changed to call shared.chat()) since I don't think the serverlist is doing as many checks with it for security.
Quote:

Originally Posted by Programmer
Perhaps you could also implement some of the functions in our (Chompy and I) thread: http://forums.graalonline.com/forums...ad.php?t=79473

Sure can look at it. :)

Frankie 08-17-2008 02:23 AM

neato!

cbk1994 08-17-2008 02:34 AM

Quote:

Originally Posted by Skyld (Post 1415025)
Not sure whether this will stay (or whether it'll just be changed to call shared.chat()) since I don't think the serverlist is doing as many checks with it for security.

I'll change it on Vesporia (am glad I decided to put it in the player class as opposed to calling it each time!)

TheStan 08-17-2008 02:58 AM

Skyld, Stefan, I applaud you. And I applaud those who contributed functions. :p

Chompy 08-17-2008 10:23 AM

Might take a look at this? :p http://forums.graalonline.com/forums...ad.php?t=79594
It's an encryption algorithm I made some months ago

And, really nice Skyld :]
Can't wait for more functions to be added

Inverness 08-17-2008 10:37 PM

So why isn't it on login server yet?

Kristi 08-18-2008 05:14 PM

Quote:

Originally Posted by Programmer (Post 1415022)
Thank you!

I've been looking for this kind of (native) support for a long time.

Perhaps you could also implement some of the functions in our (Chompy and I) thread: http://forums.graalonline.com/forums...ad.php?t=79473

I made these ages ago (well, one function that handles all of that optimized for graal), JUST WAITING FOR ACCESS TO THE SHARED FUNCTIONS :cry:

Tigairius 08-18-2008 05:16 PM

Quote:

Originally Posted by Kristi (Post 1415511)
JUST WAITING FOR ACCESS TO THE SHARED FUNCTIONS :cry:

I don't see why Skyld can't upload it for you if they're already made.. ?

Kristi 08-18-2008 05:19 PM

Quote:

Originally Posted by Tigairius (Post 1415512)
I don't see why Skyld can't upload it for you if they're already made.. ?

That would make my job rather silly :)

Cloven 08-18-2008 06:01 PM

Quote:

Originally Posted by Kristi (Post 1415514)
That would make my job rather silly :)

Or not..

Skyld 08-20-2008 12:34 AM

Okay, these functions are now available on the main login server, for all players and servers. :)

devilsknite1 09-14-2008 07:26 PM

I just fell in love / fainted / any other word here. YAY! <3

Skyld 05-30-2009 01:27 AM

Added shared.getAlignmentColor(int ap); and shared.getAlignmentShadowColor(int red, int green, int blue); by request, both returning an array of {red, green, blue}.

DustyPorViva 05-30-2009 02:02 AM

Could you perhaps add my replacetext function(or a better one, as long as one is available)?

http://forums.graalonline.com/forums...ad.php?t=82820

Skyld 05-30-2009 02:10 AM

Quote:

Originally Posted by DustyPorViva (Post 1495360)
Could you perhaps add my replacetext function(or a better one, as long as one is available)?

http://forums.graalonline.com/forums...ad.php?t=82820

Added it to shared.replacetext(string, search, replace).

Also added a function called shared.getSharedFunctions() which returns an array of public functions provided by the shared function pack.

Crow 05-30-2009 11:17 AM

Awesome Skyld, thank you!

Tigairius 05-30-2009 02:14 PM

Quote:

Originally Posted by Skyld (Post 1495357)
Added shared.getAlignmentColor(int ap); and shared.getAlignmentShadowColor(int red, int green, int blue); by request, both returning an array of {red, green, blue}.

Quote:

Originally Posted by Skyld (Post 1495362)
Added it to shared.replacetext(string, search, replace).

Also added a function called shared.getSharedFunctions() which returns an array of public functions provided by the shared function pack.

These things are pretty nice :D

DustyPorViva 06-20-2009 09:34 PM

Could you also add this?
PHP Code:

function tiles(cx,cy,cw,ch) {
  
temp.ctiles = new[0];
  for (
temp.i=0;i<cw*ch;i++) {
    
ctiles.add(tiles[int(cx+(i%cw)),int(cy+int(i/cw))]);
  }
  return 
ctiles;


player.chat = tiles(x,y,width,height);
Would return an array of all tiles within the 'blocl' given. I get tired of having to run loops all the time to check a block of tiles.

DustyPorViva 06-29-2009 07:41 PM

The AP shadow function returns 0,0,0 for gold ap, which isn't right.

It should have a check:
if (params[0] == 224 && params[1] == 196 && params[2] == 0) return {255,255,0};

Or something like that.

fowlplay4 07-18-2009 08:20 PM

Couldn't hurt..

PHP Code:

function capitalize(str) {
  return 
str.charat(0).upper() @ str.substring(1);



Tigairius 01-06-2010 02:38 AM

1 Attachment(s)
Added a new clientside function, you can access it like so:
PHP Code:

shared.adminMessage("You have been disconnected because you did not move for more than 30 minutes."); 

It supports html tags as well.

Attached a screenshot of it so you know what it's doing.

Basically, it'll open a window (like so) that will remain open even when changing servers, so, for example, if you wanted to open a window before you serverwarp() someone, it's possible now, and they will see the message even when they're on the login server.

DustyPorViva 01-06-2010 05:02 AM

Yes please?

PHP Code:

function repeat(char,num) {
  
temp.output "";
  for (
temp.i=0;i<num;i++) output @= char;
  return 
output;


I ran into a script earlier that made me wish there was a repeat character function. I think something like this would be useful for anyone who needs to format text.

Also, with these functions are variable names an issue? For example, if I use shared.repeat(" ",5) in a loop that also uses temp.i, is it going to break?

cbk1994 01-06-2010 05:11 AM

Quote:

Originally Posted by DustyPorViva (Post 1548518)
Also, with these functions are variable names an issue? For example, if I use shared.repeat(" ",5) in a loop that also uses temp.i, is it going to break?

temp variables are specific to the function, so no.

DustyPorViva 01-06-2010 05:27 AM

Quote:

Originally Posted by cbk1994 (Post 1548520)
temp variables are specific to the function, so no.

Good point! Completely forgot about that.

MrOmega 10-02-2010 07:18 PM

Hey, I'm getting an error sometimes that 'shared.roundto()' not found. Any thought as to why?

DustyPorViva 10-02-2010 07:20 PM

Quote:

Originally Posted by MrOmega (Post 1603663)
Hey, I'm getting an error sometimes that 'shared.roundto()' not found. Any thought as to why?

Are you using it clientside?

MrOmega 10-02-2010 10:06 PM

Quote:

Originally Posted by DustyPorViva (Post 1603664)
Are you using it clientside?

Yes

MrOmega 04-17-2011 09:27 AM

sorry for this... but bump, why do I sometimes get this error?

fowlplay4 04-17-2011 05:25 PM

You could try referencing the weapon directly: (@"-Shared").roundto()

Then maybe just make a copy of the function so your code looks a little neater:

PHP Code:

function roundto() {
  return (@
"-Shared").roundto();



ffcmike 04-17-2011 11:07 PM

Quote:

Originally Posted by Skyld (Post 1415009)

shared.getAlignmentColor(ap)
Returns an array of {red, green, blue} for the given AP color, allowing you to recreate the default nickname colors.
PHP Code:

shared.getAlignmentColor(player.ap); 

shared.getAlignmentShadowColor(red, green, blue)
Returns an array of {red, green, blue} for the shadow of the given color, allowing you to recreate the default nickname shadows.
PHP Code:

shared.getAlignmentShadowColor(0.750.10.3); 


I recently did some testing comparing the time it takes to loop through 0-100 between these shared functions and what we were using to calculate it on Classic.
It turns out that Classic's method was roughly twice as fast as the shared function, though some (wouldn't rule out all) of this time may have been down to the fact that public functions take a little bit longer.

Me/WhiteDragon then decided to try storing all of the values within an array like this:

PHP Code:

//#CLIENTSIDE
function onCreated(){
  
this.apv = {
    {
64,0,0},
    {
128,64,64},
    {
133,62,62},
    {
138,59,59},
    {
143,57,57},
    {
148,54,54},
    {
153,52,52},
    {
158,49,49},
    {
163,47,47},
    {
168,44,44},
    {
173,41,41},
    {
178,39,39},
    {
183,36,36},
    {
188,34,34},
    {
194,31,31},
    {
199,29,29},
    {
204,26,26},
    {
209,24,24},
    {
214,21,21},
    {
219,18,18},
    {
224,16,16},
    {
229,13,13},
    {
234,11,11},
    {
239,8,8},
    {
244,6,6},
    {
249,3,3},
    {
255,0,0},
    {
255,10,10},
    {
255,21,21},
    {
255,31,31},
    {
255,42,42},
    {
255,53,53},
    {
255,63,63},
    {
255,74,74},
    {
255,85,85},
    {
255,95,95},
    {
255,106,106},
    {
255,116,116},
    {
255,127,127},
    {
255,138,138},
    {
255,148,148},
    {
255,159,159},
    {
255,170,170},
    {
255,180,180},
    {
255,191,191},
    {
255,201,201},
    {
255,212,212},
    {
255,223,223},
    {
255,233,233},
    {
255,244,244},
    {
255,255,255},
    {
255,255,255},
    {
255,255,255},
    {
255,255,255},
    {
255,255,255},
    {
255,255,255},
    {
230,255,230},
    {
205,255,205},
    {
179,255,179},
    {
154,255,154},
    {
128,255,128},
    {
128,255,128},
    {
116,255,140},
    {
103,255,153},
    {
90,255,166},
    {
77,255,178},
    {
64,255,191},
    {
52,255,204},
    {
39,255,216},
    {
26,255,229},
    {
13,255,242},
    {
0,247,255},
    {
0,238,255},
    {
0,229,255},
    {
0,220,255},
    {
0,212,255},
    {
0,203,255},
    {
0,194,255},
    {
0,185,255},
    {
0,176,255},
    {
0,168,255},
    {
0,159,255},
    {
0,150,255},
    {
0,141,255},
    {
0,132,255},
    {
0,124,255},
    {
0,115,255},
    {
0,106,255},
    {
0,97,255},
    {
0,88,255},
    {
0,80,255},
    {
0,71,255},
    {
0,62,255},
    {
0,53,255},
    {
0,44,255},
    {
0,36,255},
    {
0,27,255},
    {
0,18,255},
    {
0,9,255},
    {
0,0,255},
    {
231,199,0}
  };
}

public function 
getAlignmentColors(temp.ap) {
  
temp.ac this.getAlignmentColor(temp.ap);
  if(
temp.ap == 100){
    
temp.sc = {2552550};
  }
  else{
    
temp.255 * !(temp.ac[0]>191.25 || temp.ac[1]>191.25);
    
temp.sc = {temp.ctemp.ctemp.ac[2]};
  } 
  return {
temp.actemp.sc};
  
//Alignment Color, Shadow Color


public function 
getAlignmentColor(temp.ap) {
  return 
this.apv[temp.ap];


I guess this would be exchanging some memory and an increased amount of weapon data sent to the client for a save on script time. This turned out to be significantly faster than what we were previously using if the class is joined within the script using it. It is also still slightly faster than our previous method if used as a public function.

Mark Sir Link 04-20-2011 08:16 PM

does shared.getAlignmentShadowColor(r, g, b) even work? What are the expected values? A float 0-1 or an int 0-255?

I have tried

PHP Code:

temp.shared.getAlignmentColor(player.ap);
temp.cs shared.getAlignmentShadowColor(c[0], c[1], c[2]);

temp.shared.getAlignmentColor(player.ap);
temp.cs shared.getAlignmentShadowColor(c[0]/255c[1]/255c[2]/255);

temp.cs shared.getAlignmentShadowColor(player.ap); 

and all are returning an array of {0, 0, 0}

The few times I've gotten it to return something besides that (by first methodology), it returns something entirely wrong, like {0, 0, 255} should the player's AP be 90 (which appears to be a white outline, while the returned is just blue)

seems as if both of these functions need work, I'm not sure why different params are passed to one versus the other anyway

Crow 04-22-2011 05:22 PM

What's wrong with rgbtohex()? shared.rgbtohex(1, 0, 0) returns #0F0000 for me; should be #FF0000 instead. Also, how come rgbtohex() and hextorgb() seem to return more than they should using v6? I always get stuff like "#0F0000 1 0 1" as a return value.

DrakilorP2P 06-17-2012 11:48 PM

Quote:

Originally Posted by Crow (Post 1644941)
What's wrong with rgbtohex()? shared.rgbtohex(1, 0, 0) returns #0F0000 for me; should be #FF0000 instead. Also, how come rgbtohex() and hextorgb() seem to return more than they should using v6? I always get stuff like "#0F0000 1 0 1" as a return value.

rgbtohex() returns the wrong hex for me too.


All times are GMT +2. The time now is 06:12 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.