Skip to content

Commit f029379

Browse files
committed
Complexity reduction now that we can treat Messges and Choices separately
1 parent db8c581 commit f029379

2 files changed

Lines changed: 22 additions & 47 deletions

File tree

src/translation.cpp

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -458,12 +458,9 @@ namespace {
458458

459459

460460

461-
std::vector<std::vector<std::string>> Translation::TranslateMessageStream(const Dictionary& dict, const std::stringstream& msg1, const std::stringstream* msg2, char trimChar) {
461+
std::vector<std::vector<std::string>> Translation::TranslateMessageStream(const Dictionary& dict, const std::stringstream& msg, char trimChar) {
462462
// Prepare source string
463-
std::string msgStr = msg1.str();
464-
if (msg2!=nullptr) {
465-
msgStr += msg2->str();
466-
}
463+
std::string msgStr = msg.str();
467464
if (msgStr.size()>0 && msgStr.back() == trimChar) {
468465
msgStr.pop_back();
469466
}
@@ -507,60 +504,32 @@ void Translation::RewriteEventCommandMessage(const Dictionary& dict, std::vector
507504
// have to track the current index directly in this function.
508505
CommandIterator commands(commandsOrig);
509506
while (!commands.Done()) {
510-
// Logic: build up both the Message stream and the Choice stream, since we'll need to
511-
// deal with both eventually. Then, if they can be merged do that.
512-
if (commands.CurrIsShowMessage() || commands.CurrIsShowChoice()) {
513-
// First build up the lines of Message texts
507+
// We only need to deal with either Message or Choice commands
508+
if (commands.CurrIsShowMessage()) {
509+
// Build up the lines of Message texts
514510
std::stringstream msg_str;
515511
std::vector<size_t> msg_indexes;
516512
commands.BuildMessageString(msg_str, msg_indexes);
517513

518-
// Next, build up the lines of Choice elements
519-
std::stringstream choice_str;
520-
std::vector<size_t> choice_indexes; // Number of entries == number of choices
521-
commands.BuildChoiceString(choice_str, choice_indexes);
522-
523-
// Will they fit on screen if we combine them?
524-
bool combine = false; //msg_indexes.size()>0 && msg_indexes.size()+choice_indexes.size() <= 4;
525-
526514
// Go through messages first, including possible choices
527515
if (msg_indexes.size()>0) {
528516
// Get our lines, possibly including "combined"
529-
std::vector<std::vector<std::string>> msgs = TranslateMessageStream(dict, msg_str, (combine?&choice_str:nullptr), '\n');
517+
std::vector<std::vector<std::string>> msgs = TranslateMessageStream(dict, msg_str, '\n');
530518
if (msgs.size()>0) {
531519
// The complex replacement logic is based on the last message box, then all remaining things are simply left back in.
532520
std::vector<std::string>& lines = msgs.back();
533521

534-
// There is a special case here: if we are asked to remove a message box, we should cancel the "combine" action and do nothing further
522+
// There is a special case here: if we are asked to remove a message box, we should do nothing further
535523
// This command is *only* respected as the first line of a message box.
536524
if (lines[0]==TRCUST_REMOVEMSG) {
537-
// Update the index of all choice items first
538-
for (size_t& index : choice_indexes) {
539-
index -= msg_indexes.size();
540-
}
541-
542-
// Now clear all message boxes in reverse order.
525+
// Clear all message boxes in reverse order.
543526
while (!msg_indexes.empty()) {
544527
commands.RemoveByIndex(msg_indexes.back());
545528
msg_indexes.pop_back();
546529
}
547-
548-
// Finally, reset the "combine" flag so that we process the message box command correctly.
549-
combine = false;
550530
} else {
551-
// We only need the last X Choices from the translation, since we can't change the Choice count.
552-
size_t maxLines = 4;
553-
if (combine) {
554-
// Go backwards through our lines/choices
555-
while (!choice_indexes.empty() && !lines.empty()) {
556-
commands.ReWriteString(choice_indexes.back(), lines.back());
557-
choice_indexes.pop_back();
558-
lines.pop_back();
559-
maxLines -= 1;
560-
}
561-
}
562-
563531
// Trim lines down to allowed remaining (with choices).
532+
const size_t maxLines = 4;
564533
while (lines.size() > maxLines) {
565534
lines.pop_back();
566535
}
@@ -589,10 +558,17 @@ void Translation::RewriteEventCommandMessage(const Dictionary& dict, std::vector
589558
}
590559
}
591560

592-
// Go through choices second.
593-
if (!combine && choice_indexes.size()>0) {
561+
// Note that commands.Advance() has already happened within the above code.
562+
} else if (commands.CurrIsShowChoice()) {
563+
// Build up the lines of Choice elements
564+
std::stringstream choice_str;
565+
std::vector<size_t> choice_indexes; // Number of entries == number of choices
566+
commands.BuildChoiceString(choice_str, choice_indexes);
567+
568+
// Go through choices.
569+
if (choice_indexes.size()>0) {
594570
// Translate, break back into lines.
595-
std::vector<std::vector<std::string>> msgs = TranslateMessageStream(dict, choice_str, nullptr, '\n');
571+
std::vector<std::vector<std::string>> msgs = TranslateMessageStream(dict, choice_str, '\n');
596572
if (msgs.size()>0) {
597573
// Logic here is also based on the last message box.
598574
std::vector<std::string>& lines = msgs.back();

src/translation.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,17 +241,16 @@ class Translation {
241241
void RewriteCommonEventMessages();
242242

243243
/**
244-
* Convert a stream of msgbox + choices to a list of output message boxes
244+
* Convert a stream of msgbox or choices to a list of output message boxes
245245
*
246246
* @param dict The dictionary to use for translation
247-
* @param msg1 The first message string to use for lookup. String with newlines.
248-
* @param msg2 The (optiona) second message string to use for lookup. String with newlines.
247+
* @param msg The message string to use for lookup. String with newlines.
249248
* @param trimChar Trim this character if the lookup string ends with this.
250249
* @return A vector of Message Boxes, where each Message Box is represented as a vector of lines (strings), or an empty vector if there is no translation.
251250
* It is guaranteed that each MessageBox vector will have at least one entry (containing "") if it would otherwise be empty; this can happen
252251
* if the message box insertion commands are used. Note that the last MessageBox vector may contain translated "Choice" entries (it is based on the input).
253252
*/
254-
std::vector<std::vector<std::string>> TranslateMessageStream(const Dictionary& dict, const std::stringstream& msg1, const std::stringstream* msg2, char trimChar);
253+
std::vector<std::vector<std::string>> TranslateMessageStream(const Dictionary& dict, const std::stringstream& msg, char trimChar);
255254

256255
/**
257256
* Rewrite a list of event commands (from any map, battle, or common event) given a dictionary.

0 commit comments

Comments
 (0)