summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/libutil.cc
diff options
context:
space:
mode:
authorJay3.1415 <no@mail.com>2012-10-11 23:16:22 +0200
committerFlorian Diebold <flodiebold@gmail.com>2012-10-13 02:35:48 +0200
commita1a28680322a93c5607c8985e6934b47d01a4b75 (patch)
tree5432eff87a4aa73dce15f53838cf1d3cdd0939ab /crawl-ref/source/libutil.cc
parent1656f8ec015d4a72b60fbb954b2cc7d71a389942 (diff)
downloadcrawl-ref-a1a28680322a93c5607c8985e6934b47d01a4b75.tar.gz
crawl-ref-a1a28680322a93c5607c8985e6934b47d01a4b75.zip
Improve speech syntax: capitalization and random substrings.
1. @The_monster@ shouts, "I hate you, @CAPS@ @player_name@ @NOCAPS@!" will result in the actual player name being capitalized. (Spaces are trimmed.) 2. @The_monster@ says, "I am [very ||really ] hungry." will randomly result in "I am very hungry", "I am hungry" or "I am really hungry". If the pattern turns up a lot, you need to assign weights to the different choices, or you require nested random sets, you'll need to stick to the old fallback of defining ad hoc @keywords@. Otherwise, this should make things somewhat simpler. Works for both monster and weapon speech, including shouts, noisy weapons and the Singing Sword. I've also taken the liberty to update the speech documentation, which still listed the old website (!) and pointed users looking for advice to the newsgroup). Conflicts: crawl-ref/source/art-func.h crawl-ref/source/libutil.cc crawl-ref/source/libutil.h Signed-off-by: Florian Diebold <flodiebold@gmail.com>
Diffstat (limited to 'crawl-ref/source/libutil.cc')
-rw-r--r--crawl-ref/source/libutil.cc43
1 files changed, 43 insertions, 0 deletions
diff --git a/crawl-ref/source/libutil.cc b/crawl-ref/source/libutil.cc
index 64b382f82c..318622411b 100644
--- a/crawl-ref/source/libutil.cc
+++ b/crawl-ref/source/libutil.cc
@@ -650,6 +650,49 @@ string replace_all_of(string s, const string &tofind, const string &replacement)
return s;
}
+// Capitalize phrases encased in @CAPS@ ... @NOCAPS@. If @NOCAPS@ is missing, change the rest of the line to uppercase.
+string maybe_capitalize_substring(string s)
+{
+ string::size_type start = 0;
+ while ((start = s.find("@CAPS@", start)) != string::npos)
+ {
+ string::size_type cap_start = start + 6;
+ string::size_type cap_end = string::npos;
+ string::size_type end = s.find("@NOCAPS@", cap_start);
+ string::size_type length = string::npos;
+ string::size_type cap_length = string::npos;
+ if (end != string::npos)
+ {
+ cap_end = end + 8;
+ cap_length = end - cap_start;
+ length = cap_end - start;
+ }
+ string substring = s.substr(cap_start, cap_length);
+ trim_string(substring);
+ s.replace(start, length, uppercase(substring));
+ }
+ return s;
+}
+
+// For each set of [phrase|term|word] contained in the string, replace the set with a random subphrase.
+// NOTE: Doesn't work for nested patterns!
+string maybe_pick_random_substring(string s)
+{
+ string::size_type start = 0;
+ while ((start = s.find("[", start)) != string::npos)
+ {
+ string::size_type end = s.find("]", start);
+ if (end == string::npos)
+ break;
+
+ string substring = s.substr(start + 1, end - start - 1);
+ vector<string> split = split_string("|", substring, false, false);
+ int index = random2(split.size());
+ s.replace(start, end + 1 - start, split[index]);
+ }
+ return s;
+}
+
int count_occurrences(const string &text, const string &s)
{
ASSERT(!s.empty());