Jump to content
KeyWorld

Hardcoded script commands improvement

Recommended Posts

Hi.

 

If I remember correctly I wrote some things related to this post on the forum, at the beginning of rathena, I'm not able to find it ever, so maybe it was in the staff section.

 

It was talking about script optimization, more specially, how some fake commands (if/else/while/for) are converting to label in the source and can be improved.

 

Since I don't have the motivation (and the time) to work on a patch, I let you the result of my investigations here (because it was waiting from one year on my ftp).

 

 

So first of all, this modifications required two commands : jump_nonzero, goto_ifexist. There will be used to rewrite all the for() while() code and optimize the code.

// jump_nonzero() - inverse of jump_zero.
BUILDIN_DEF(jump_nonzero,"il")
BUILDIN_FUNC(jump_nonzero)
{
    if( script_getnum(st,2) ) {
        if( !data_islabel(script_getdata(st,3)) ){
            ShowError("script: jump_zero: not label !\n");
            st->state=END;
            return 1;
        }
        
        st->pos   = script_getnum(st,3);
        st->state = GOTO;
    }
    return 0;
}
 
// goto_ifexist() - Jump to a label if it exist (required a string !)
BUILDIN_DEF(goto_ifexist,"s")
BUILDIN_FUNC(goto_ifexist)
{
    int n = search_str( script_getstr(st,2) );
    
    if( n == -1 || str_data[n].type != C_POS )
    {// label not found
        return 0;
    }
    
    st->pos   = str_data[n].label;
    st->state = GOTO;
    return 0;
}
 

 

----------------------------------------------------

 

 

Now, let's have fun:

 

 

 

For

 

NPC Script:

for ( <a>; <b>; <c> )
{
    <d>;
}
Currently compiled to:
<a>;
__FR%x_J:
    jump_zero( <b>, __FR%x_FIN );
    goto __FR%x_BGN;
 
__FR%x_NXT:
    <c>;
    goto __FR%x_J;
 
__FR%x_BGN:
    <d>;
    goto __FR%x_NXT;
 
__FR%x_FIN:
My proposal:
<a>;
jump_zero( <b>, __FR%x_FIN );
 
__FR%x_J:
    <d>;
 
__FR%x_NXT:
    <c>;
    jump_nonzero( <b>, __FR%x_J );
 
__FR%x_FIN:
 

 

 

 

While

 

NPC Script:

while ( <a> )
{
    <b>
}
 

Currently compiled to:

__WL%x_NXT:
    jump_zero( <a>, __WL%x_FIN );
    <b>
    goto __WL%x_NXT;
__WL%x_FIN:
 

My proposal:

goto __WL%x_NXT;
__WL%x_BGN:
    <b>;
__WL%x_NXT:
    jump_nonzero( <a>, __WL%x_BGN );
__WL%x_FIN:
 

 

 

Do-While

 

NPC Script:

do {
    <a>
} while( <b> );
 

Currently compiled to:

__DO%x_BGN:
    <a>;
__DO%x_NXT:
    jump_zero( <b>, __DO%x_FIN );
    goto __DO%x_BGN;
__DO%x_FIN:
 

My proposal:

__DO%x_BGN:
    <a>;
__DO%x_NXT:
    jump_nonzero( <b>, __DO%x_BGN );
__DO%x_FIN:
 

 

 

Switch

 

NPC Script:

switch(<a>) {
    case 1:
        <b>
    break;
 
    default:
        <c>
}
 

Currently compiled code (simplify, the original is more obscure):

set [email protected]__SW%x_VAL, <a>;
goto __SW%x_%xJ;
 
__SW%x_%xJ:
__SW%x_%x:
    if(%d != [email protected]__SW%x_VAL) goto __SW%x_%x;
    <b>;
    goto __SW%x_FIN; // break
 
__SW%x_%x:
    goto __SW%x_DEF;
 
__SW%x_DEF:
    <c>;
 
set [email protected]__SW%x_VAL,0;
goto __SW%x_FIN;
 
__SW%x_FIN:
 

My proposal:

goto_ifexist("__SW%x_" + <a>);
goto_ifexist("__SW%x_DEF");
goto __SW%x_FIN;
 
__SW%x_1:
    <b>
    goto __SW%x_FIN; // break;
 
__SW%x_DEF:
    <d>
 
__SW%x_FIN:
  • Upvote 7
Link to comment
Share on other sites

Are you sure that the original code for 'for' isn't like this?

<a>;
__FR%x_J:
jump_zero( <b>, __FR%x_FIN );
goto __FR%x_BGN;

__FR%x_NXT:
<c>;
goto __FR%x_J;  // <----

__FR%x_BGN:
<d>;
goto __FR%x_NXT;

__FR%x_FIN:

My suggestion is to use a forced jump instead using the jump_nonzero:

<a>;
__FR%x_J:
jump_zero( <b>, __FR%x_FIN );
<d>;
__FR%x_NXT:
<c>;
goto __FR%x_J;

__FR%x_FIN:

And in your proposal for the 'while', why are you checking the <b>? Isn't the check routine is in <a>?

Sorry to bother you with these newbie questions (=_=')

Thanks KeyWorld ;)

  • Upvote 1
Link to comment
Share on other sites

Yeah, I was a little confused, corrected thanks :)

In your for() version, you are using a goto + jump_zero, when I just use a jump_nonzero. So no it's not better.

  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.