Difficulty Controlling Battles

I'm using RPG Maker for the first time, and am finding constructing maps with events to be intuitive enough, but am finding so much of the battle system seems to be obscured I have a hard time getting the results I want out of it.

My biggest issue is I have no idea how to control whether an effect takes place at the very start of a battle, at the very end of a battle, at the start of a round of combat or at the end, or before a character takes their action or after.

A few specific examples:
- When an attack misses, no TP is added. How to make it so a character who selects attack always charges their TP, without just making it a certain hit
- Set an event to occur before a battler's action takes place, or as a reaction to a hit. (for example, an enemy that does damage or applies a debuff on hit (I know there's a yanfly counter plugin that could do those two things) or a lure ability that increases a character's Target rate before an enemy with the relevant state makes an action, but resets after the action so only that enemy is affected)
- Setting an effect to take place at the very start or very end of the battle, and have that persist across the entire system instead of being applied to every single troop event (for example reducing TP to 0 at the end of every battle, including escaped ones)

The other regular issue I'm having is making changes to aspects hard coded in, such as
- What messages show on the heads-up display and how they are shown
- Formulas that are hard coded in, such as those using Agility and Luck

I know the solution to most of these are just a line or two of code, my issue is not knowing where the code that runs battles is or the syntax to interact with it. Some specific things I know can be handled by plugins, but most of what I'm trying to do would be very straight forward if I just had slightly more granular control over when things did or didn't happen.
Setting an effect to take place at the very start or very end of the battle, and have that persist across the entire system instead of being applied to every single troop event (for example reducing TP to 0 at the end of every battle, including escaped ones)
To make something occur at the very start of battle, set the condition to "turn 0".

To make something occur at the very end of battle (on victory), set the condition to "turn end", the span to "turn", and start out with "if enemy #1 affected by knockout else exit event processing" 8 times for each enemy, then install HIME_EndPhaseTriggers to make events trigger when there are no enemies left.

I'm not sure how to make an event occur in the battle just before the player flees.

To make events occur in every battle, install YEP_BaseTroopEvents (it's one of the free Yanfly plugins) and put those events in your base troop.

To force TP to be reduced to zero when battle ends, there are probably multiple options, but you could try my SFG_ExpandedTP plugin. I don't think I added a "charge TP on miss" feature to that plugin though.

Set an event to occur before a battler's action takes place, or as a reaction to a hit. (for example, an enemy that does damage or applies a debuff on hit (I know there's a yanfly counter plugin that could do those two things) or a lure ability that increases a character's Target rate before an enemy with the relevant state makes an action, but resets after the action so only that enemy is affected)
I don't know of any way to make an event occur in general as a reaction to any hit (though maybe you could do something that checks if HP changes, but that would be tricky if it even works). But if you want a reaction to a hit from a specific skill, that's fairly easy – make the skill apply an invisible state with 100% chance, then make a troop event that triggers every turn (condition "turn 1 + 1*X") and checks if someone is inflicted by the state. The span would have to be "turn"; in principle, it'd be better for it to be "moment", but I'm not quite sure if you can actually get that to work.

Another possible approach would be to make the skill set a switch (by calling a common event) and using that switch to enable a troop event with condition "turn 1 + 1*X" and span moment.
Yanfly Battle Core, Skill Core, Buff & States Core, Utility Common Events, Base Troop Events etc

Or some changes are easy to do by just Ctrl-F:ing the code. Like for the gain tp thing I think just take out the applyItemUserEffect from the isHit conditional.

PS. The base code sets your tp to 0 at battle end, unless you have 'preserve tp' trait? Scene_Battle terminate -> $gameParty onBattleEnd -> clearTp

Original thread: https://forums.rpgmakerweb.com/threads/difficulty-controlling-battles.181058/