| 1 |
#include "ConversationKeyExtractor.h" |
| 2 |
|
| 3 |
#include "itextstream.h" |
| 4 |
|
| 5 |
#include <regex> |
| 6 |
|
| 7 |
#include "string/convert.h" |
| 8 |
#include "string/predicate.h" |
| 9 |
#include "ConversationCommand.h" |
| 10 |
#include "ConversationCommandLibrary.h" |
| 11 |
|
| 12 |
namespace conversation |
| 13 |
{ |
| 14 |
|
| 15 |
ConversationKeyExtractor::ConversationKeyExtractor(ConversationMap& map) : |
| 16 |
_convMap(map), |
| 17 |
_regexConvNum("conv_(\\d+)_(.*)"), |
| 18 |
_regexConvCmd("cmd_(\\d+)_(.*)") |
| 19 |
{ |
| 20 |
assert(_convMap.empty()); |
| 21 |
} |
| 22 |
|
| 23 |
void ConversationKeyExtractor::operator()(const std::string& key, const std::string& value) |
| 24 |
{ |
| 25 |
// Quick discard of any non-conversation keys |
| 26 |
if (!string::starts_with(key, "conv")) return; |
| 27 |
|
| 28 |
// Extract the conv index |
| 29 |
std::smatch results; |
| 30 |
if (!std::regex_match(key, results, _regexConvNum)) |
| 31 |
{ |
| 32 |
// No match, abort |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
// Get the conversation number |
| 37 |
int num = string::convert<int>(results[1].str()); |
| 38 |
|
| 39 |
// We now have the conversation number and the substring (everything after |
| 40 |
// "conv_<n>_" which applies to this conversation. |
| 41 |
std::string convSubString = results[2]; |
| 42 |
|
| 43 |
// Switch on the substring |
| 44 |
if (convSubString == "name") |
| 45 |
{ |
| 46 |
_convMap[num].name = value; |
| 47 |
} |
| 48 |
else if (convSubString == "talk_distance") |
| 49 |
{ |
| 50 |
_convMap[num].talkDistance = string::convert<float>(value, 60); |
| 51 |
} |
| 52 |
else if (convSubString == "actors_must_be_within_talkdistance") |
| 53 |
{ |
| 54 |
_convMap[num].actorsMustBeWithinTalkdistance = (value == "1"); |
| 55 |
} |
| 56 |
else if (convSubString == "actors_always_face_each_other_while_talking") |
| 57 |
{ |
| 58 |
_convMap[num].actorsAlwaysFaceEachOther = (value == "1"); |
| 59 |
} |
| 60 |
else if (convSubString == "max_play_count") |
| 61 |
{ |
| 62 |
_convMap[num].maxPlayCount = string::convert<int>(value, -1); |
| 63 |
} |
| 64 |
else if (convSubString.substr(0, 6) == "actor_") |
| 65 |
{ |
| 66 |
// This is an actor definition, extract the number |
| 67 |
int actorNum = string::convert<int>(convSubString.substr(6), -1); |
| 68 |
|
| 69 |
if (actorNum == -1) |
| 70 |
{ |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
// Store the actor in the map |
| 75 |
_convMap[num].actors.emplace(actorNum, value); |
| 76 |
} |
| 77 |
else if (convSubString.substr(0, 4) == "cmd_") |
| 78 |
{ |
| 79 |
// This is a conversation command |
| 80 |
std::smatch cmdResults; |
| 81 |
|
| 82 |
if (!std::regex_match(convSubString, cmdResults, _regexConvCmd)) |
| 83 |
{ |
| 84 |
return; // not matching |
| 85 |
} |
| 86 |
|
| 87 |
int cmdIndex = string::convert<int>(cmdResults[1].str()); |
| 88 |
std::string cmdSubStr = cmdResults[2]; |
| 89 |
|
| 90 |
ConversationCommandPtr command; |
| 91 |
auto found = _convMap[num].commands.find(cmdIndex); |
| 92 |
|
| 93 |
if (found != _convMap[num].commands.end()) |
| 94 |
{ |
| 95 |
// Command already exists |
| 96 |
command = found->second; |
| 97 |
} |
| 98 |
else |
| 99 |
{ |
| 100 |
// Command with the given index does not exist yet, create it |
| 101 |
command = std::make_shared<ConversationCommand>(); |
| 102 |
|
| 103 |
// Insert this into the map |
| 104 |
_convMap[num].commands[cmdIndex] = command; |
| 105 |
} |
| 106 |
|
| 107 |
if (cmdSubStr == "type") |
| 108 |
{ |
| 109 |
try |
| 110 |
{ |
| 111 |
command->type = ConversationCommandLibrary::Instance().findCommandInfo(value).id; |
| 112 |
} |
| 113 |
catch (std::runtime_error& e) |
| 114 |
{ |
| 115 |
rError() << e.what() << std::endl; |
| 116 |
return; |
| 117 |
} |
| 118 |
} |
| 119 |
else if (cmdSubStr == "actor") |
| 120 |
{ |
| 121 |
command->actor = string::convert<int>(value); |
| 122 |
} |
| 123 |
else if (cmdSubStr == "wait_until_finished") |
| 124 |
{ |
| 125 |
command->waitUntilFinished = (value == "1"); |
| 126 |
} |
| 127 |
else if (cmdSubStr.substr(0,4) == "arg_") |
| 128 |
{ |
| 129 |
int cmdArgIndex = string::convert<int>(cmdSubStr.substr(4)); |
| 130 |
|
| 131 |
command->arguments[cmdArgIndex] = value; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
} // namespace conversation |