Code help with lunatic modes

Hello,

I am using RPG Maker MV.

I am still learn javascript and I do need a lot of work.

But, I have learnt some pieces of code that I have used such as

$gameParty.members()[1].atk to call upon the stats of a party member
$gameActors.actor(1).atk calls upon the actor stat by ID.


Using a formula int he notetag for pretty much any plug in require use of javascript.

value = formula
a.gainMp(x)

With this I can make a skill to target a player and then give mana back (or take mana off if negative) the user. I am not sure if there is another way to take mana off or using a negative number here is the primary way.

Anyway, my question is how would I learn more of these types of code that are specific to RPG Maker MV that calls uopn various things.

I have some specific questions that I would appreciate any answer to, but I would also appreciate any help in finding out how to answer these questions myself.



I would like to use the mana cost (or tp cost) of the skill I am using in the formula. For example I would maybe like a spell to return 50% of the mana cost after it has cast. I'm not sure what the x should be int he following.

a.gainMp(0.5*x)



Another specific question I have is can I target a random enemy with a skill that already has a target and user that is not that enemy. Like I want to heal an actor (b), return mana to the user (a), and then deal damage to a random enemy

I'd create a heal skill and the notetag would use the following to heal an actor for 1,000 HP, the user would gain 50 MP, and a random enemy is dealt 100 HP damage. I know that "randomenemy.gain Hp" is wrong. But, what is the correct way?
[I am able to create a common event that forces the use of a second attack to attack an enemy, but I am not looking to use that workaround - I want to call upon "value" (the amount healed) as a variable in my damage formula]

value = 1000
a.gainMp(50)
randomenemy.gain Hp(-100)



Also I have written some code that I can't seem to get working. If someone could take a look, that would be awesome.
[essentially, I want it to damage to a random enemy by the amount it over heals - but, I don't want it to heal the enemy when it doesn't over heal any]

(if ((value - (target.mhp - target.hp)) >= 0) { (value - (target.mhp - target.hp)); } else { 0;} )



Thanks in advance for any help.
This link is a pretty good directory of the script call components.
Anyway, my question is how would I learn more of these types of code that are specific to RPG Maker MV that calls uopn various things.

Well...in my opinion the best answer is...study the game code. Unfortunately until you know more about JavaScript, it will be really confusing. But the more you study the game code and the more you learn about JavaScript, the more you'll understand how to call on the game code to do what you want. Specifically you'll want to look at the files under the \js\ folder that start with "rpg_". When you're starting out, most of what you'll want is in rpg_objects.js. (Be careful not to accidentally change anything while you're in there looking!)

Honestly, doing what you can to find some JavaScript tutorials and follow them (even though they have nothing directly to do with RPG Maker) will be the best thing you can do, starting out. I wish I had done that a lot sooner. That knowledge will help you to understand what the game code actually DOES when you look at it.

Instead of looking at the JS files directly, you can use that link that Dirge provided, which gives some helpful hints on what each function does, but it doesn't tell you HOW it does it, so it won't get you as far in the long run if you want to really expand your horizons.

Your second and third questions are highly dependent on WHICH plugin you're using and in WHICH notetag you're entering the information. Not every plugin or every notetag will accept a, b, subject, and target as valid objects, and depending on how the plugin uses the notetag information, you might access a random enemy or the caster in a different way.

IF you're using Yanfly's Skill Core and IF you're using the skill notetags under "Lunatic Mode – The Skill Phases" (which seems likely from the way you've phrased this and what you titled the thread) then you can do the following:

To recover half the MP cost of the skill:
a.gainMp( Math.floor( a.skillMpCost( this.item() ) * 0.5 ) );
Don't forget Math.floor when you're multiplying MP by a fraction; you only want whole numbers!

To answer a minor question you threw in there: yes, gainMp() is used to lose MP as well, using a negative value as you've said.

To get a random enemy:
this.opponentsUnit().randomTarget()
But this only returns an enemy object.

For your last question, you're getting a number, but you're not storing it anywhere, so nothing gets done with that number. To start with, you need to store the number into a variable like you see below. You'll still need to know what function to use that variable in, of course.
var overHeal = value - (target.mhp - target.hp);
if (overHeal > 0) { DO_SOMETHING };

Since you're interested in learning, I'll let you see if you can put those together to figure out how to make the random enemy lose health equal to the amount of the over-healing. If you're having trouble with it, I can help you with the rest.
Thanks for the replies. That's a lot of awesome information.

Thanks for the link, Dirge. I briefly clicked through and it looks confusing. Haha. I'll have to sit down properly and look through when I get more time.

Thanks, Llareian, for all the help. I'm actually writing this for Yanfly's DamageCore. But it's useful information that different plugins may not work int he same way.

I am still having some trouble calling upon the mana cost of the skill I am using. Perhaps you could help a little more?

So here is the code that is working:

<damage formula>
value = (((Math.random()*.1)+.95)*((((-304.08163)+(40.40816*a.mat))*(0.99606+(0.00394*b.mdf))*1)));
a.gainMp(Math.floor((((value-(target.mhp-target.hp))/value)*(-10))));
var overHeal = (value - (target.mhp - target.hp));
target = this.opponentsUnit().randomTarget();
if (overHeal > 0) {
target.gainHp(Math.floor(-(overHeal)));
} else
{target.gainHp(0);
}
</damage formula>


And here is a part that is not working:

a.gainMp(Math.floor((((value-(target.mhp-target.hp))/value)*(-(a.skillMpCost( this.item())))));

I simply replaces the 10 from the working code with a.skillMpCost( this.item()
I think I may be missing something here. Any thoughts?

Once again, thanks.
You're missing a ). It's generally bad practice to use that many parentheses, because it's SO hard to read. It's also better practice to put
var overHeal = (value - (target.mhp - target.hp));
closer to the top and use it in your a.gainMP function.

As you get more experience coding, you'll find that more short lines are generally superior to fewer long lines, because it's easier to read/interpret (ESPECIALLY months down the road when you've forgotten what you were doing), and you can use fewer parentheses, leading to fewer typos.

The big clue here when I was troubleshooting it was that I opened up my console (if you don't have a plugin opening it every time you test, use the F8 key to open it up) and saw that I was getting an "Unexpected token" error. That usually means you have a typo somewhere that you didn't use matching (), {}, or [], or you've missed a semicolon ; or colon :.
I need to get this post marked, surely I'll come back here a few times
That makes sense Llareian, thanks.

You've been incredibly helpful. In future, I will try to create more variables to store formulas so that i don't have to rewrite them out of multiple lines, making them easier to read in the process.

Original thread: https://forums.rpgmakerweb.com/threads/code-help-with-lunatic-modes.86858/