-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathQuestWeaver.cpp
204 lines (180 loc) · 8.09 KB
/
QuestWeaver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//
// Created by michael on 10.08.15.
//
#include <algorithm>
#include <QuestWeaver.h>
#include <World/Space/SpaceWorldModel.h>
using namespace std;
using namespace weave;
QuestWeaver::QuestWeaver(WeaverConfig &config) {
if (!config.worldModel) {
Logger::Fatal("A world model must be provided for the quest system to work.");
}
randomStream = config.randomStream ? config.randomStream : make_shared<RandomStream>(config.seed);
engine.reset(new WeaverEngine(randomStream));
quests.reset(new QuestModel());
templates.reset(new TemplateEngine(randomStream, config.dirs, config.formatterType));
config.worldModel->rs = randomStream;
world = std::move(config.worldModel);
stories.reset(new StoryWriter(randomStream, *quests, *templates, *world, config.dirs));
for (uint64_t i = 0; i < config.questTemplateFactories.size(); i++) {
RegisterQuestTemplateFactory(move(config.questTemplateFactories.at(i)));
}
for (uint64_t i = 0; i < config.storyTemplateFactories.size(); i++) {
RegisterStoryTemplateFactory(move(config.storyTemplateFactories.at(i)));
}
}
vector<shared_ptr<Quest>> QuestWeaver::CreateNewQuests() {
vector<QuestCandidate> newQuestCandidates;
bool hasPriorityQuests = false;
int maxScore = 0;
int minScore = -1;
// gather quest candidates
Logger::Debug("Starting new quest creation...");
for (auto questTemplate : templates->GetTemplatesForNewQuest(*world, *quests)) {
EngineResult result = engine->fillTemplate(questTemplate, *quests, *world);
shared_ptr<Quest> newQuest = questTemplate->ToQuest(result.GetQuestPropertyValues());
int score = 0; // smaller score is better
if (questTemplate->HasPriority()) {
hasPriorityQuests = true;
} else {
score += questTemplate->GetRarity();
for (auto oldQuest : quests->GetQuests()) {
// penalize quests that occurred often
if (newQuest->GetType() == oldQuest->GetType()) {
score++;
}
}
for (auto action : result.GetModelActions()) {
// penalize quests that need to create new entities
if (action.GetActionType() == WorldActionType::CREATE) {
score++;
}
}
maxScore = max(maxScore, score);
minScore = minScore == -1 ? score : min(minScore, score);
}
newQuestCandidates.push_back({newQuest, result, score, questTemplate->HasPriority()});
}
// shuffle candidates, then sort by score
auto begin = newQuestCandidates.begin();
auto end = newQuestCandidates.end();
for (auto iter = begin + 1; iter != end; iter++) {
auto iter2 = begin + randomStream->GetRandomIndex((iter - begin) + 1);
if (iter != iter2) {
iter_swap(iter, iter2);
}
}
auto comparator = [](const QuestCandidate &a, const QuestCandidate &b) -> bool {
return a.score < b.score;
};
stable_sort(newQuestCandidates.begin(), newQuestCandidates.end(), comparator);
Logger::Debug("Quest types by absolute score:", 1);
for (auto candidate : newQuestCandidates) {
Logger::Debug(to_string(candidate.score) + ": " + candidate.quest->GetType(), 2);
}
// randomly select a minimum required score
vector<shared_ptr<Quest>> newQuests;
int selectedScore = -1;
int normalizedScore = maxScore - minScore; // to prevent the scores getting too big as the game progresses
while (selectedScore < 0) {
selectedScore = randomStream->GetNormalIntInRange(-normalizedScore, normalizedScore);
}
// select the fitting candidate(s)
Logger::Debug("Selecting one of the candidates with a score bigger " + to_string(selectedScore + minScore), 1);
for (auto candidate : newQuestCandidates) {
Logger::Debug("Processing quest candidate " + candidate.quest->GetType(), 1);
if (hasPriorityQuests) {
if (candidate.isPriorityQuest) {
Logger::Debug("[Priority] quest!", 2);
world->Execute(candidate.result.GetModelActions());
Story storyResult = stories->CreateStory(candidate.result.GetStoryParameters());
world->Execute(storyResult.worldActions);
quests->RegisterNew(candidate.quest, candidate.result.GetQuestPropertyValues(), storyResult.text);
newQuests.push_back(candidate.quest);
} else {
Logger::Debug("[Ignored] because it is not a priority quest.", 2);
}
continue;
}
if (candidate.score >= (selectedScore + minScore)) {
Logger::Debug("Quest was chosen! Updating world model with quest changes...", 2);
world->Execute(candidate.result.GetModelActions());
Logger::Debug("Creating story for chosen quest...", 2);
Story storyResult = stories->CreateStory(candidate.result.GetStoryParameters());
Logger::Debug("Updating world model with story changes...", 2);
world->Execute(storyResult.worldActions);
quests->RegisterNew(candidate.quest, candidate.result.GetQuestPropertyValues(), storyResult.text);
newQuests.push_back(candidate.quest);
break;
}
Logger::Debug("[Ignored] because its score was too low.", 2);
}
Logger::Debug("Quest creation finished, chose " + to_string(newQuests.size()) + " quest(s).", 1);
return newQuests;
}
std::vector<std::shared_ptr<Quest>> QuestWeaver::GetQuestsWithState(QuestState state) const {
return quests->GetQuestsWithState(state);
}
void QuestWeaver::Tick(float delta) {
for (const auto &quest : quests->GetQuestsWithState(QuestState::Active)) {
const QuestTickResult &change = quest->Tick(delta, *world);
world->Execute(change.GetWorldChanges());
quests->Execute(change.GetQuestChange());
}
}
std::vector<std::shared_ptr<Quest>> QuestWeaver::GetAllQuests() const {
return quests->GetQuests();
}
shared_ptr<Quest> QuestWeaver::GetQuest(ID questId) const {
return quests->GetQuest(questId);
}
bool QuestWeaver::ChangeQuestState(QuestModelAction questAction) {
Logger::Debug("Directly changing quest " + to_string(questAction.GetQuestId()) + " state to " +
to_string(static_cast<int>(questAction.GetActionType())));
return quests->Execute(questAction);
}
void QuestWeaver::RegisterQuestTemplateFactory(std::unique_ptr<QuestTemplateFactory> factory) {
templates->RegisterTemplateFactory(move(factory));
}
void QuestWeaver::RegisterStoryTemplateFactory(std::unique_ptr<StoryTemplateFactory> factory) {
stories->RegisterTemplateFactory(move(factory));
}
void QuestWeaver::Serialize(std::ostream &outputStream, StreamType type) {
if (type == StreamType::JSON) {
cereal::JSONOutputArchive outputArchive(outputStream);
outputArchive(*this);
} else if (type == StreamType::BINARY) {
cereal::PortableBinaryOutputArchive outputArchive(outputStream);
outputArchive(*this);
} else {
Logger::Fatal("Unknown serialization type!");
}
}
QuestWeaver QuestWeaver::Deserialize(std::istream &inputStream, StreamType type) {
QuestWeaver deserialized;
if (type == StreamType::JSON) {
cereal::JSONInputArchive inputArchive(inputStream);
inputArchive(deserialized);
} else if (type == StreamType::BINARY) {
cereal::PortableBinaryInputArchive inputArchive(inputStream);
inputArchive(deserialized);
} else {
Logger::Fatal("Unknown serialization type!");
}
return deserialized;
}
QuestWeaver QuestWeaver::Deserialize(std::istream &inputStream, StreamType type, Directories currentDirectories) {
QuestWeaver deserialized = Deserialize(inputStream, type);
deserialized.ChangeWorkingDirectories(currentDirectories);
return deserialized;
}
QuestWeaver::QuestWeaver() {
}
void weave::QuestWeaver::ChangeWorkingDirectories(Directories directories) {
templates->ChangeDirectories(directories);
stories->ChangeDirectories(directories);
}
WorldModel &QuestWeaver::GetWorldModel() const {
return *world;
}