Jump to content
  • 0
Nihad

Converting a macro into a function

Question

So I have been playing around with this a bit today and I'm trying to figure out the best way to do this.

I need to convert the macro NORMALIZE_RDAMAGE to a function because I need it to offer 2 different options.

For example to have capped max reflect damage on MvPs but not on normal monsters and players.

Right now herc has

#ifdef RENEWAL
#define NORMALIZE_RDAMAGE(d) ( trdamage += rdamage = max(1, min(max_reflect_damage, (d))) )
#else
#define NORMALIZE_RDAMAGE(d) ( trdamage += rdamage = max(1, (d)) )
#endif

I want to change this to be if the target is an MvP or not.

Doing 

if ( is_boss(src) ) {
        #define NORMALIZE_RDAMAGE(d) ( trdamage += rdamage = max(1, min(max_reflect_damage, (d))) )
    } else {
        #define NORMALIZE_RDAMAGE(d) ( trdamage += rdamage = max(1, (d)) )
    }

This seems to work but it's wrong. "Preprocessor directives are interpreted before the results of the preprocessing step are compiled."

I don't understand enough about source to explain why this edit works, but I know that it's not the right way to go about making this edit. VS and GCC throw out warnings about macro redefinition. Any help would be much appreciated.

Share this post


Link to post
Share on other sites

4 answers to this question

Recommended Posts

  • 0
#define NORMALIZE_RDAMAGE(d) ( trdamage += rdamage = max(1, is_boss(src) ? min(max_reflect_damage, (d)) : (d)) )

 

Share this post


Link to post
Share on other sites
  • 0

See I thought of that too. But as you can see in my quote above that I found while doing some research, macros are determined prior to compiling. So technically only one of those macros will count, it should not change the value once the server is live. Or have macros changed over time and are now flexible?

Thanks meko, can you clarify if macros can change once the server is compiled.

Here is a link explaining a bit of what I'm talking about.

https://stackoverflow.com/questions/9274500/redefining-or-changing-macro-value

If my understanding is correct, even what meko posted should technically not work. Once the server gets compiled normalize damage would be set in stone and would not change based on anything the server does. 

Share this post


Link to post
Share on other sites
  • 0

the macro I posted WILL remain the same, but I added a ternary operator (if ? then : else) to it so it will check with is_boss() and have different behaviour accordingly, just like what you wanted

if you want the check outside of macros, then make a 2nd one and when invoked do something like is_boss(src) ? macro1 : macro2

Share this post


Link to post
Share on other sites
  • 0

Thanks meko, that answers my question. I wanted to know if you can build checks like that into macros. 

Just tested this and confirmed it works. 

Share this post


Link to post
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
Answer this question...

×   Pasted as rich text.   Restore formatting

  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.

Loading...

×
×
  • Create New...

Important Information

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