1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-02-03 17:54:06 +00:00
OpenMW/apps/openmw/mwdialogue/dialoguemanagerimp.cpp

437 lines
14 KiB
C++
Raw Normal View History

2010-08-06 18:01:34 +02:00
#include "dialoguemanagerimp.hpp"
2010-08-06 18:01:34 +02:00
2010-08-08 15:09:44 +02:00
#include <cctype>
#include <algorithm>
#include <iterator>
#include <components/esm/loaddial.hpp>
2012-11-10 14:31:58 +01:00
#include <components/esm/loadinfo.hpp>
#include <components/compiler/exception.hpp>
#include <components/compiler/errorhandler.hpp>
#include <components/compiler/scanner.hpp>
#include <components/compiler/locals.hpp>
#include <components/compiler/output.hpp>
#include <components/compiler/scriptparser.hpp>
#include <components/interpreter/interpreter.hpp>
#include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp"
#include "../mwbase/scriptmanager.hpp"
#include "../mwbase/windowmanager.hpp"
2010-08-06 18:01:34 +02:00
#include "../mwworld/class.hpp"
#include "../mwworld/containerstore.hpp"
2012-10-01 19:17:04 +04:00
#include "../mwworld/esmstore.hpp"
2010-08-06 18:01:34 +02:00
#include "../mwgui/dialogue.hpp"
2012-02-17 20:20:23 +01:00
#include "../mwscript/compilercontext.hpp"
#include "../mwscript/interpretercontext.hpp"
#include "../mwscript/extensions.hpp"
2012-02-17 20:20:23 +01:00
#include "../mwmechanics/creaturestats.hpp"
#include "filter.hpp"
2010-08-08 13:21:03 +02:00
namespace
{
2010-08-08 15:09:44 +02:00
std::string toLower (const std::string& name)
{
std::string lowerCase;
std::transform (name.begin(), name.end(), std::back_inserter (lowerCase),
(int(*)(int)) std::tolower);
return lowerCase;
}
bool stringCompareNoCase (std::string first, std::string second)
{
unsigned int i=0;
while ( (i<first.length()) && (i<second.length()) )
{
if (tolower(first[i])<tolower(second[i])) return true;
else if (tolower(first[i])>tolower(second[i])) return false;
++i;
}
if (first.length()<second.length())
return true;
else
return false;
}
2012-02-10 22:01:31 +01:00
2012-02-10 22:54:17 +01:00
//helper function
std::string::size_type find_str_ci(const std::string& str, const std::string& substr,size_t pos)
{
return toLower(str).find(toLower(substr),pos);
}
2012-09-23 00:36:20 +02:00
}
namespace MWDialogue
{
DialogueManager::DialogueManager (const Compiler::Extensions& extensions) :
mCompilerContext (MWScript::CompilerContext::Type_Dialgoue),
mErrorStream(std::cout.rdbuf()),mErrorHandler(mErrorStream)
2012-02-17 20:20:23 +01:00
{
mChoice = -1;
mIsInChoice = false;
mCompilerContext.setExtensions (&extensions);
2012-03-23 15:24:39 +01:00
mDialogueMap.clear();
mActorKnownTopics.clear();
const MWWorld::Store<ESM::Dialogue> &dialogs =
MWBase::Environment::get().getWorld()->getStore().get<ESM::Dialogue>();
MWWorld::Store<ESM::Dialogue>::iterator it = dialogs.begin();
for (; it != dialogs.end(); ++it)
2012-03-23 15:24:39 +01:00
{
mDialogueMap[toLower(it->mId)] = *it;
2012-03-23 15:24:39 +01:00
}
2012-02-17 20:20:23 +01:00
}
2010-08-06 18:01:34 +02:00
2012-08-09 09:41:17 +02:00
void DialogueManager::addTopic (const std::string& topic)
2012-02-10 16:09:43 +01:00
{
mKnownTopics[toLower(topic)] = true;
2012-02-10 16:09:43 +01:00
}
2012-11-10 14:31:58 +01:00
void DialogueManager::parseText (const std::string& text)
2012-02-15 13:23:59 +01:00
{
std::list<std::string>::iterator it;
for(it = mActorKnownTopics.begin();it != mActorKnownTopics.end();++it)
2012-02-15 13:23:59 +01:00
{
size_t pos = find_str_ci(text,*it,0);
2012-02-15 13:23:59 +01:00
if(pos !=std::string::npos)
{
if(pos==0)
2012-02-15 13:23:59 +01:00
{
mKnownTopics[*it] = true;
}
else if(text.substr(pos -1,1) == " ")
{
mKnownTopics[*it] = true;
2012-02-15 13:23:59 +01:00
}
}
}
2012-03-19 18:30:52 +01:00
updateTopics();
2012-02-15 13:23:59 +01:00
}
void DialogueManager::startDialogue (const MWWorld::Ptr& actor)
2010-08-06 18:01:34 +02:00
{
2012-03-18 15:26:18 +01:00
mChoice = -1;
mIsInChoice = false;
2012-02-17 20:20:23 +01:00
mActor = actor;
MWMechanics::CreatureStats& creatureStats = MWWorld::Class::get (actor).getCreatureStats (actor);
mTalkedTo = creatureStats.hasTalkedToPlayer();
creatureStats.talkedToPlayer();
2012-02-17 20:20:23 +01:00
mActorKnownTopics.clear();
2012-03-19 19:21:08 +01:00
2012-02-10 22:01:31 +01:00
//initialise the GUI
2012-05-23 12:23:35 +02:00
MWBase::Environment::get().getWindowManager()->pushGuiMode(MWGui::GM_Dialogue);
MWGui::DialogueWindow* win = MWBase::Environment::get().getWindowManager()->getDialogueWindow();
2012-05-17 17:15:44 +02:00
win->startDialogue(actor, MWWorld::Class::get (actor).getName (actor));
2012-02-10 22:01:31 +01:00
2012-03-16 17:53:34 +01:00
//setup the list of topics known by the actor. Topics who are also on the knownTopics list will be added to the GUI
updateTopics();
2012-02-10 22:01:31 +01:00
//greeting
2012-02-11 12:19:02 +01:00
bool greetingFound = false;
const MWWorld::Store<ESM::Dialogue> &dialogs =
MWBase::Environment::get().getWorld()->getStore().get<ESM::Dialogue>();
Filter filter (actor, mChoice, mTalkedTo);
MWWorld::Store<ESM::Dialogue>::iterator it = dialogs.begin();
for (; it != dialogs.end(); ++it)
{
if(it->mType == ESM::Dialogue::Greeting)
{
2012-02-11 12:19:02 +01:00
if (greetingFound) break;
for (std::vector<ESM::DialInfo>::const_iterator iter (it->mInfo.begin());
iter!=it->mInfo.end(); ++iter)
2012-02-10 22:54:17 +01:00
{
2012-11-10 14:31:58 +01:00
if (filter (*iter))
2012-02-10 22:54:17 +01:00
{
2012-09-17 11:37:50 +04:00
if (!iter->mSound.empty())
2012-02-11 12:19:02 +01:00
{
// TODO play sound
}
2012-09-17 11:37:50 +04:00
std::string text = iter->mResponse;
2012-02-15 13:23:59 +01:00
parseText(text);
2012-09-17 11:37:50 +04:00
win->addText(iter->mResponse);
executeScript(iter->mResultScript);
2012-02-11 12:19:02 +01:00
greetingFound = true;
mLastTopic = it->mId;
mLastDialogue = *iter;
2012-02-11 12:19:02 +01:00
break;
2012-02-10 22:54:17 +01:00
}
}
}
}
2012-02-10 22:01:31 +01:00
}
bool DialogueManager::compile (const std::string& cmd,std::vector<Interpreter::Type_Code>& code)
{
2012-02-17 20:20:23 +01:00
try
2012-02-10 22:01:31 +01:00
{
mErrorHandler.reset();
2012-02-17 20:20:23 +01:00
2012-03-05 16:56:14 +01:00
std::istringstream input (cmd + "\n");
2012-02-17 20:20:23 +01:00
Compiler::Scanner scanner (mErrorHandler, input, mCompilerContext.getExtensions());
2012-02-17 20:20:23 +01:00
2012-03-05 16:56:14 +01:00
Compiler::Locals locals;
std::string actorScript = MWWorld::Class::get (mActor).getScript (mActor);
if (!actorScript.empty())
{
// grab local variables from actor's script, if available.
locals = MWBase::Environment::get().getScriptManager()->getLocals (actorScript);
2012-03-05 16:56:14 +01:00
}
Compiler::ScriptParser parser(mErrorHandler,mCompilerContext, locals, false);
2012-02-17 20:20:23 +01:00
scanner.scan (parser);
if(mErrorHandler.isGood())
2012-02-10 22:01:31 +01:00
{
parser.getCode(code);
return true;
2012-02-10 22:01:31 +01:00
}
return false;
2012-02-17 20:20:23 +01:00
}
catch (const Compiler::SourceException& /* error */)
2012-02-17 20:20:23 +01:00
{
// error has already been reported via error handler
2012-02-10 22:01:31 +01:00
}
2012-02-17 20:20:23 +01:00
catch (const std::exception& error)
{
printError (std::string ("An exception has been thrown: ") + error.what());
}
return false;
}
2012-11-10 14:31:58 +01:00
void DialogueManager::executeScript (const std::string& script)
2012-02-17 20:20:23 +01:00
{
std::vector<Interpreter::Type_Code> code;
if(compile(script,code))
2012-02-17 20:20:23 +01:00
{
try
{
MWScript::InterpreterContext interpreterContext(&mActor.getRefData().getLocals(),mActor);
2012-02-17 20:20:23 +01:00
Interpreter::Interpreter interpreter;
MWScript::installOpcodes (interpreter);
interpreter.run (&code[0], code.size(), interpreterContext);
}
catch (const std::exception& error)
{
printError (std::string ("An exception has been thrown: ") + error.what());
}
}
}
void DialogueManager::updateTopics()
{
2012-03-19 18:30:52 +01:00
std::list<std::string> keywordList;
int choice = mChoice;
mChoice = -1;
mActorKnownTopics.clear();
MWGui::DialogueWindow* win = MWBase::Environment::get().getWindowManager()->getDialogueWindow();
const MWWorld::Store<ESM::Dialogue> &dialogs =
MWBase::Environment::get().getWorld()->getStore().get<ESM::Dialogue>();
Filter filter (mActor, mChoice, mTalkedTo);
MWWorld::Store<ESM::Dialogue>::iterator it = dialogs.begin();
for (; it != dialogs.end(); ++it)
{
if(it->mType == ESM::Dialogue::Topic)
{
for (std::vector<ESM::DialInfo>::const_iterator iter (it->mInfo.begin());
iter!=it->mInfo.end(); ++iter)
{
2012-11-10 14:31:58 +01:00
if (filter (*iter))
{
mActorKnownTopics.push_back(toLower(it->mId));
//does the player know the topic?
if(mKnownTopics.find(toLower(it->mId)) != mKnownTopics.end())
{
keywordList.push_back(it->mId);
2012-03-19 18:30:52 +01:00
break;
}
}
}
}
}
// check the available services of this actor
int services = 0;
if (mActor.getTypeName() == typeid(ESM::NPC).name())
{
MWWorld::LiveCellRef<ESM::NPC>* ref = mActor.get<ESM::NPC>();
2012-11-05 16:07:59 +04:00
if (ref->mBase->mHasAI)
services = ref->mBase->mAiData.mServices;
}
else if (mActor.getTypeName() == typeid(ESM::Creature).name())
{
MWWorld::LiveCellRef<ESM::Creature>* ref = mActor.get<ESM::Creature>();
2012-11-05 16:07:59 +04:00
if (ref->mBase->mHasAI)
services = ref->mBase->mAiData.mServices;
}
2012-09-23 00:36:20 +02:00
int windowServices = 0;
if (services & ESM::NPC::Weapon
|| services & ESM::NPC::Armor
|| services & ESM::NPC::Clothing
|| services & ESM::NPC::Books
|| services & ESM::NPC::Ingredients
|| services & ESM::NPC::Picks
|| services & ESM::NPC::Probes
|| services & ESM::NPC::Lights
|| services & ESM::NPC::Apparatus
|| services & ESM::NPC::RepairItem
|| services & ESM::NPC::Misc)
2012-09-23 00:36:20 +02:00
windowServices |= MWGui::DialogueWindow::Service_Trade;
if(mActor.getTypeName() == typeid(ESM::NPC).name() && !mActor.get<ESM::NPC>()->mBase->mTransport.empty())
windowServices |= MWGui::DialogueWindow::Service_Travel;
2012-09-08 18:17:03 -04:00
if (services & ESM::NPC::Spells)
2012-09-23 00:36:20 +02:00
windowServices |= MWGui::DialogueWindow::Service_BuySpells;
if (services & ESM::NPC::Spellmaking)
windowServices |= MWGui::DialogueWindow::Service_CreateSpells;
2012-10-17 18:03:02 +02:00
if (services & ESM::NPC::Training)
windowServices |= MWGui::DialogueWindow::Service_Training;
if (services & ESM::NPC::Enchanting)
windowServices |= MWGui::DialogueWindow::Service_Enchant;
2012-09-23 00:36:20 +02:00
win->setServices (windowServices);
2012-09-08 18:17:03 -04:00
// sort again, because the previous sort was case-sensitive
keywordList.sort(stringCompareNoCase);
2012-03-19 18:30:52 +01:00
win->setKeywords(keywordList);
mChoice = choice;
}
2012-08-09 09:41:17 +02:00
void DialogueManager::keywordSelected (const std::string& keyword)
{
if(!mIsInChoice)
{
if(mDialogueMap.find(keyword) != mDialogueMap.end())
{
ESM::Dialogue ndialogue = mDialogueMap[keyword];
2012-09-17 11:37:50 +04:00
if(ndialogue.mType == ESM::Dialogue::Topic)
{
Filter filter (mActor, mChoice, mTalkedTo);
for (std::vector<ESM::DialInfo>::const_iterator iter = ndialogue.mInfo.begin();
iter!=ndialogue.mInfo.end(); ++iter)
{
2012-11-10 14:31:58 +01:00
if (filter (*iter))
{
2012-09-17 11:37:50 +04:00
std::string text = iter->mResponse;
std::string script = iter->mResultScript;
parseText(text);
MWGui::DialogueWindow* win = MWBase::Environment::get().getWindowManager()->getDialogueWindow();
win->addTitle(keyword);
2012-09-17 11:37:50 +04:00
win->addText(iter->mResponse);
2012-03-19 19:21:08 +01:00
executeScript(script);
mLastTopic = keyword;
mLastDialogue = *iter;
break;
}
}
}
}
}
2012-03-20 10:15:22 +01:00
updateTopics();
}
void DialogueManager::goodbyeSelected()
{
MWBase::Environment::get().getWindowManager()->removeGuiMode(MWGui::GM_Dialogue);
}
2012-08-09 09:41:17 +02:00
void DialogueManager::questionAnswered (const std::string& answer)
{
2012-08-09 09:41:17 +02:00
if(mChoiceMap.find(answer) != mChoiceMap.end())
{
2012-08-09 09:41:17 +02:00
mChoice = mChoiceMap[answer];
std::vector<ESM::DialInfo>::const_iterator iter;
if(mDialogueMap.find(mLastTopic) != mDialogueMap.end())
{
ESM::Dialogue ndialogue = mDialogueMap[mLastTopic];
2012-09-17 11:37:50 +04:00
if(ndialogue.mType == ESM::Dialogue::Topic)
{
Filter filter (mActor, mChoice, mTalkedTo);
2012-03-19 18:30:52 +01:00
for (std::vector<ESM::DialInfo>::const_iterator iter = ndialogue.mInfo.begin();
iter!=ndialogue.mInfo.end(); ++iter)
{
2012-11-10 14:31:58 +01:00
if (filter (*iter))
{
mChoiceMap.clear();
mChoice = -1;
mIsInChoice = false;
MWGui::DialogueWindow* win = MWBase::Environment::get().getWindowManager()->getDialogueWindow();
2012-09-17 11:37:50 +04:00
std::string text = iter->mResponse;
parseText(text);
win->addText(text);
2012-09-17 11:37:50 +04:00
executeScript(iter->mResultScript);
mLastTopic = mLastTopic;
2012-03-19 18:30:52 +01:00
mLastDialogue = *iter;
break;
}
}
}
}
updateTopics();
}
}
2012-11-10 14:31:58 +01:00
void DialogueManager::printError (const std::string& error)
2012-02-17 20:20:23 +01:00
{
MWGui::DialogueWindow* win = MWBase::Environment::get().getWindowManager()->getDialogueWindow();
2012-02-17 20:20:23 +01:00
win->addText(error);
}
2012-08-09 09:41:17 +02:00
void DialogueManager::askQuestion (const std::string& question, int choice)
{
MWGui::DialogueWindow* win = MWBase::Environment::get().getWindowManager()->getDialogueWindow();
win->askQuestion(question);
mChoiceMap[toLower(question)] = choice;
mIsInChoice = true;
}
2012-04-04 22:13:57 +02:00
2012-11-10 14:31:58 +01:00
MWWorld::Ptr DialogueManager::getActor() const
2012-04-04 22:13:57 +02:00
{
2012-11-10 14:31:58 +01:00
return mActor;
2012-04-04 22:13:57 +02:00
}
void DialogueManager::goodbye()
{
MWGui::DialogueWindow* win = MWBase::Environment::get().getWindowManager()->getDialogueWindow();
win->goodbye();
}
2010-08-06 18:01:34 +02:00
}