Window_Message.prototype.convertEscapeCharacters = function (text) {
text = text.replace(/\\/g, "\x1b");
text = text.replace(/\x1b\x1b/g, "\\");
text = text.replace(/\x1bV\[(\d+)\]/gi, (_, p1) =>
$gameVariables.value(parseInt(p1))
);
text = text.replace(/\x1bV\[(\d+)\]/gi, (_, p1) =>
$gameVariables.value(parseInt(p1))
);
text = text.replace(/\x1bN\[(\d+)\]/gi, (_, p1) =>
this.actorName(parseInt(p1))
);
text = text.replace(/\x1bP\[(\d+)\]/gi, (_, p1) =>
this.partyMemberName(parseInt(p1))
);
text = text.replace(/\x1bG/gi, TextManager.currencyUnit);
text = text.replace(/\x1bCT\[(\d+)\]/gi, "" /* function () {
$gameMessage.setAlign(Number(arguments[1] || 0));
return "";
}.bind(this) */);
var newText = [];
/* eslint no-control-regex: 0 */
const linebreakRegex = /[\r\n]/g;
let match;
const newLineIndices = [];
while ((match = linebreakRegex.exec(text))) {
newLineIndices.push(match.index);
}
const textMakeUpRegex = /\x1b[a-zA-Z]\[\d+\]/g;
const textMakeUpPositions = [];
while ((match = textMakeUpRegex.exec(text))) {
textMakeUpPositions.push({start: match.index, end: textMakeUpRegex.lastIndex});
}
var textWidth = 0;
for (let i = 0; i < text.length; i++) {
var value = this.textWidth(text[i]);
if (newText[i] === undefined) {
newText.push(text[i]);
} else {
newText[i] = text[i];
}
// Check if i is within any of the ranges defined in textMakeUpPositions
const isInRange = textMakeUpPositions.some(position => i >= position.start && i < position.end);
if (!isInRange) {
textWidth += value;
}
if (newLineIndices.includes(i)) {
textWidth = 0;
}
if (textWidth >= (this.width - 60)) {
var i_backup = i;
while (i > 0 && newText[i] !== '\n' && newText[i] !== ' ') {
i--;
}
if (i === 0 | newText[i] === '\n') {
i = i_backup + 1;
text = text.slice(0, i) + '\n' + text.slice(i);
newText.push('\n');
} else {
newText[i] = '\n';
}
textWidth = 0;
};
};
text = newText.join('');
return text;
};