I wouldn't be surprised if there is a plugin that can do it, but as a quick test, you could try something like this:
Code:
BattleManager.setEventCallback(function(result) {
if(result == 2) {
// Battle was lost - heal and transport to inn
$gameParty.reviveBattleMembers(); // sets dead player's HP to 1
// Replace the values on this line with inn map ID, coordinates, etc
var mapId = 12, x = 3, y = 5, direction = 2, fadeType = 1;
$gamePlayer.reserveTransfer(mapId, x, y, direction, fadeType)
}
});
That needs to go in a script call in a troop event set to run on turn 0 in every troop that can be a random encounter. Also, it should
not go in any troops that you call from an event (as it'll break the event's "if win/lose/escape" outcome), so if you want to use the same troop for an event and a wandering encounter, you'll need two copies of it. The troop event should look something like this once you're done:
I tested this in MZ (and also in MV, because I initially misread the version you were using), and it seems to work. If you need anything more complicated than this though, it would probably be better to request a plugin.