1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-16 13:49:55 +00:00
openmw-tes3mp/apps/openmw/mwdialogue/dialoguemanagerimp.cpp

923 lines
32 KiB
C++
Raw Normal View History

2010-08-06 16:01:34 +00:00
#include "dialoguemanagerimp.hpp"
2010-08-06 16:01:34 +00:00
2010-08-08 13:09:44 +00:00
#include <cctype>
#include <algorithm>
#include <iterator>
#include <components/esm/loaddial.hpp>
#include <components/esm_store/store.hpp>
#include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp"
#include "../mwbase/scriptmanager.hpp"
#include "../mwbase/journal.hpp"
2010-08-06 16:01:34 +00:00
#include "../mwworld/class.hpp"
2010-08-08 11:21:03 +00:00
#include "../mwworld/refdata.hpp"
#include "../mwworld/player.hpp"
#include "../mwworld/containerstore.hpp"
2010-08-06 16:01:34 +00:00
#include "../mwinput/inputmanager.hpp"
#include "../mwgui/dialogue.hpp"
#include "../mwgui/window_manager.hpp"
2010-08-06 16:01:34 +00:00
#include <iostream>
2012-02-17 19:20:23 +00:00
#include "../mwscript/extensions.hpp"
2012-03-05 15:56:14 +00:00
2012-02-17 19:20:23 +00:00
#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/interpreter/interpreter.hpp>
#include "../mwscript/compilercontext.hpp"
#include "../mwscript/interpretercontext.hpp"
#include <components/compiler/scriptparser.hpp>
2012-02-17 19:20:23 +00:00
#include "../mwclass/npc.hpp"
#include "../mwmechanics/npcstats.hpp"
2010-08-08 11:21:03 +00:00
namespace
{
2010-08-08 13:09:44 +00: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 21:01:31 +00:00
2010-08-08 11:21:03 +00:00
template<typename T1, typename T2>
bool selectCompare (char comp, T1 value1, T2 value2)
{
switch (comp)
{
case '0': return value1==value2;
case '1': return value1!=value2;
case '2': return value1>value2;
case '3': return value1>=value2;
case '4': return value1<value2;
case '5': return value1<=value2;
2010-08-08 11:21:03 +00:00
}
throw std::runtime_error ("unknown compare type in dialogue info select");
}
template<typename T>
bool checkLocal (char comp, const std::string& name, T value, const MWWorld::Ptr& actor,
const ESMS::ESMStore& store)
{
std::string scriptName = MWWorld::Class::get (actor).getScript (actor);
if (scriptName.empty())
return false; // no script
const ESM::Script *script = store.scripts.find (scriptName);
int i = 0;
for (; i<static_cast<int> (script->varNames.size()); ++i)
if (script->varNames[i]==name)
break;
if (i>=static_cast<int> (script->varNames.size()))
return false; // script does not have a variable of this name
const MWScript::Locals& locals = actor.getRefData().getLocals();
if (i<script->data.numShorts)
return selectCompare (comp, locals.mShorts[i], value);
else
i -= script->data.numShorts;
if (i<script->data.numLongs)
return selectCompare (comp, locals.mLongs[i], value);
else
i -= script->data.numShorts;
return selectCompare (comp, locals.mFloats.at (i), value);
}
2010-08-08 13:09:44 +00:00
template<typename T>
bool checkGlobal (char comp, const std::string& name, T value)
2010-08-08 13:09:44 +00:00
{
switch (MWBase::Environment::get().getWorld()->getGlobalVariableType (name))
2010-08-08 13:09:44 +00:00
{
case 's':
return selectCompare (comp, MWBase::Environment::get().getWorld()->getGlobalVariable (name).mShort, value);
2010-08-08 13:09:44 +00:00
case 'l':
2010-08-08 13:09:44 +00:00
return selectCompare (comp, MWBase::Environment::get().getWorld()->getGlobalVariable (name).mLong, value);
2010-08-08 13:09:44 +00:00
case 'f':
2010-08-08 13:09:44 +00:00
return selectCompare (comp, MWBase::Environment::get().getWorld()->getGlobalVariable (name).mFloat, value);
2010-08-08 13:09:44 +00:00
case ' ':
2010-08-08 13:09:44 +00:00
MWBase::Environment::get().getWorld()->getGlobalVariable (name); // trigger exception
break;
2010-08-08 13:09:44 +00:00
default:
2010-08-08 13:09:44 +00:00
throw std::runtime_error ("unsupported gobal variable type");
2010-08-08 13:09:44 +00:00
}
return false;
}
2010-08-08 11:21:03 +00:00
}
namespace MWDialogue
2010-08-06 16:01:34 +00:00
{
2012-02-10 21:54:17 +00: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);
}
bool DialogueManager::functionFilter(const MWWorld::Ptr& actor, const ESM::DialInfo& info,bool choice)
{
2012-05-03 04:07:41 +00:00
bool isCreature = (actor.getTypeName() != typeid(ESM::NPC).name());
for (std::vector<ESM::DialInfo::SelectStruct>::const_iterator iter (info.selects.begin());
iter != info.selects.end(); ++iter)
{
ESM::DialInfo::SelectStruct select = *iter;
char type = select.selectRule[1];
if(type == '1')
{
char comp = select.selectRule[4];
std::string name = select.selectRule.substr (5);
std::string function = select.selectRule.substr(2,2);
int ifunction;
std::istringstream iss(function);
iss >> ifunction;
switch(ifunction)
{
2012-03-18 10:32:05 +00:00
case 39://PC Expelled
2012-03-18 14:26:18 +00:00
if(!selectCompare<int,int>(comp,0,select.i)) return false;
2012-03-18 10:32:05 +00:00
break;
2012-03-19 18:21:08 +00:00
case 40://PC Common Disease
2012-03-18 16:58:14 +00:00
if(!selectCompare<int,int>(comp,0,select.i)) return false;
break;
2012-03-19 18:21:08 +00:00
case 41://PC Blight Disease
2012-03-18 16:58:14 +00:00
if(!selectCompare<int,int>(comp,0,select.i)) return false;
break;
2012-03-18 10:32:05 +00:00
case 43://PC Crime level
2012-03-18 14:26:18 +00:00
if(!selectCompare<int,int>(comp,0,select.i)) return false;
2012-03-18 10:32:05 +00:00
break;
case 46://Same faction
{
2012-05-03 04:07:41 +00:00
if (isCreature)
return false;
MWMechanics::NpcStats PCstats = MWWorld::Class::get(MWBase::Environment::get().getWorld()->getPlayer().getPlayer()).getNpcStats(MWBase::Environment::get().getWorld()->getPlayer().getPlayer());
MWMechanics::NpcStats NPCstats = MWWorld::Class::get(actor).getNpcStats(actor);
int sameFaction = 0;
if(!NPCstats.getFactionRanks().empty())
{
std::string NPCFaction = NPCstats.getFactionRanks().begin()->first;
if(PCstats.getFactionRanks().find(toLower(NPCFaction)) != PCstats.getFactionRanks().end()) sameFaction = 1;
}
if(!selectCompare<int,int>(comp,sameFaction,select.i)) return false;
}
2012-03-18 10:32:05 +00:00
break;
case 48://Detected
2012-03-18 14:26:18 +00:00
if(!selectCompare<int,int>(comp,1,select.i)) return false;
2012-03-18 10:32:05 +00:00
break;
case 49://Alarmed
2012-03-18 14:26:18 +00:00
if(!selectCompare<int,int>(comp,0,select.i)) return false;
2012-03-18 10:32:05 +00:00
break;
case 50://choice
if(choice)
{
2012-03-18 14:26:18 +00:00
if(!selectCompare<int,int>(comp,mChoice,select.i)) return false;
}
break;
2012-03-18 10:32:05 +00:00
case 60://PC Vampire
2012-03-18 14:26:18 +00:00
if(!selectCompare<int,int>(comp,0,select.i)) return false;
2012-03-18 10:32:05 +00:00
break;
case 61://Level
2012-03-18 14:26:18 +00:00
if(!selectCompare<int,int>(comp,1,select.i)) return false;
2012-03-18 10:32:05 +00:00
break;
case 62://Attacked
2012-03-18 14:26:18 +00:00
if(!selectCompare<int,int>(comp,0,select.i)) return false;
2012-03-18 10:32:05 +00:00
break;
case 63://Talked to PC
2012-03-18 14:26:18 +00:00
if(!selectCompare<int,int>(comp,0,select.i)) return false;
2012-03-18 10:32:05 +00:00
break;
case 64://PC Health
2012-03-18 14:26:18 +00:00
if(!selectCompare<int,int>(comp,50,select.i)) return false;
2012-03-18 10:32:05 +00:00
break;
case 65://Creature target
2012-03-18 14:26:18 +00:00
if(!selectCompare<int,int>(comp,0,select.i)) return false;
2012-03-18 10:32:05 +00:00
break;
case 66://Friend hit
2012-03-18 14:26:18 +00:00
if(!selectCompare<int,int>(comp,0,select.i)) return false;
2012-03-18 10:32:05 +00:00
break;
case 67://Fight
2012-03-18 14:26:18 +00:00
if(!selectCompare<int,int>(comp,0,select.i)) return false;
2012-03-18 10:32:05 +00:00
break;
case 68://Hello????
2012-03-18 14:26:18 +00:00
if(!selectCompare<int,int>(comp,0,select.i)) return false;
2012-03-18 10:32:05 +00:00
break;
case 69://Alarm
2012-03-18 14:26:18 +00:00
if(!selectCompare<int,int>(comp,0,select.i)) return false;
2012-03-18 10:32:05 +00:00
break;
case 70://Flee
2012-03-18 14:26:18 +00:00
if(!selectCompare<int,int>(comp,0,select.i)) return false;
2012-03-18 10:32:05 +00:00
break;
case 71://Should Attack
2012-03-18 14:26:18 +00:00
if(!selectCompare<int,int>(comp,0,select.i)) return false;
2012-03-18 10:32:05 +00:00
break;
default:
break;
}
}
}
2012-03-18 10:32:05 +00:00
return true;
}
2012-02-10 21:54:17 +00:00
2010-08-08 11:21:03 +00:00
bool DialogueManager::isMatching (const MWWorld::Ptr& actor,
const ESM::DialInfo::SelectStruct& select) const
{
2012-05-03 04:07:41 +00:00
bool isCreature = (actor.getTypeName() != typeid(ESM::NPC).name());
2010-08-08 11:21:03 +00:00
char type = select.selectRule[1];
if (type!='0')
{
char comp = select.selectRule[4];
std::string name = select.selectRule.substr (5);
std::string function = select.selectRule.substr(1,2);
2010-08-08 11:21:03 +00:00
switch (type)
{
case '1': // function
2012-04-09 12:23:12 +00:00
return true; // Done elsewhere.
case '2': // global
2010-08-08 13:09:44 +00:00
if (select.type==ESM::VT_Short || select.type==ESM::VT_Int ||
select.type==ESM::VT_Long)
{
if (!checkGlobal (comp, toLower (name), select.i))
return false;
}
else if (select.type==ESM::VT_Float)
{
if (!checkGlobal (comp, toLower (name), select.f))
return false;
}
else
throw std::runtime_error (
"unsupported variable type in dialogue info select");
2010-08-08 13:09:44 +00:00
return true;
2010-08-08 13:09:44 +00:00
case '3': // local
2010-08-08 11:21:03 +00:00
if (select.type==ESM::VT_Short || select.type==ESM::VT_Int ||
select.type==ESM::VT_Long)
{
if (!checkLocal (comp, toLower (name), select.i, actor,
MWBase::Environment::get().getWorld()->getStore()))
return false;
}
else if (select.type==ESM::VT_Float)
{
if (!checkLocal (comp, toLower (name), select.f, actor,
MWBase::Environment::get().getWorld()->getStore()))
return false;
}
else
throw std::runtime_error (
"unsupported variable type in dialogue info select");
return true;
case '4'://journal
if(select.type==ESM::VT_Int)
{
if(!selectCompare<int,int>(comp,MWBase::Environment::get().getJournal()->getJournalIndex(toLower(name)),select.i)) return false;
}
else
throw std::runtime_error (
"unsupported variable type in dialogue info select");
return true;
2012-03-18 14:26:18 +00:00
case '5'://item
2012-03-18 16:58:14 +00:00
{
MWWorld::Ptr player = MWBase::Environment::get().getWorld()->getPlayer().getPlayer();
MWWorld::ContainerStore& store = MWWorld::Class::get (player).getContainerStore (player);
2012-03-18 16:58:14 +00:00
int sum = 0;
for (MWWorld::ContainerStoreIterator iter (store.begin()); iter!=store.end(); ++iter)
if (toLower(iter->getCellRef().refID) == toLower(name))
sum += iter->getRefData().getCount();
if(!selectCompare<int,int>(comp,sum,select.i)) return false;
2012-03-18 16:58:14 +00:00
}
2012-03-18 14:26:18 +00:00
2012-03-18 16:30:05 +00:00
return true;
2012-03-19 18:21:08 +00:00
2012-03-18 17:04:51 +00:00
case '6'://dead
if(!selectCompare<int,int>(comp,0,select.i)) return false;
2012-03-18 16:30:05 +00:00
case '7':// not ID
if(select.type==ESM::VT_String ||select.type==ESM::VT_Int)//bug in morrowind here? it's not a short, it's a string
{
int isID = int(toLower(name)==toLower(MWWorld::Class::get (actor).getId (actor)));
if (selectCompare<int,int>(comp,!isID,select.i)) return false;
}
else
throw std::runtime_error (
"unsupported variable type in dialogue info select");
return true;
case '8':// not faction
2012-05-03 04:07:41 +00:00
if (isCreature)
return false;
if(select.type==ESM::VT_Int)
{
MWWorld::LiveCellRef<ESM::NPC>* npc = actor.get<ESM::NPC>();
int isFaction = int(toLower(npc->base->faction) == toLower(name));
if(selectCompare<int,int>(comp,!isFaction,select.i))
return false;
}
else
throw std::runtime_error (
"unsupported variable type in dialogue info select");
return true;
case '9':// not class
2012-05-03 04:07:41 +00:00
if (isCreature)
return false;
if(select.type==ESM::VT_Int)
{
MWWorld::LiveCellRef<ESM::NPC>* npc = actor.get<ESM::NPC>();
int isClass = int(toLower(npc->base->cls) == toLower(name));
if(selectCompare<int,int>(comp,!isClass,select.i))
return false;
}
else
throw std::runtime_error (
"unsupported variable type in dialogue info select");
return true;
case 'A'://not Race
2012-05-03 04:07:41 +00:00
if (isCreature)
return false;
if(select.type==ESM::VT_Int)
{
MWWorld::LiveCellRef<ESM::NPC>* npc = actor.get<ESM::NPC>();
int isRace = int(toLower(npc->base->race) == toLower(name));
if(selectCompare<int,int>(comp,!isRace,select.i))
return false;
}
else
throw std::runtime_error (
"unsupported variable type in dialogue info select");
2010-08-08 11:21:03 +00:00
return true;
2010-08-08 11:21:03 +00:00
case 'B'://not Cell
if(select.type==ESM::VT_Int)
{
int isCell = int(toLower(actor.getCell()->cell->name) == toLower(name));
if(selectCompare<int,int>(comp,!isCell,select.i))
return false;
}
else
throw std::runtime_error (
"unsupported variable type in dialogue info select");
return true;
case 'C'://not local
if (select.type==ESM::VT_Short || select.type==ESM::VT_Int ||
select.type==ESM::VT_Long)
{
if (checkLocal (comp, toLower (name), select.i, actor,
MWBase::Environment::get().getWorld()->getStore()))
return false;
}
else if (select.type==ESM::VT_Float)
{
if (checkLocal (comp, toLower (name), select.f, actor,
MWBase::Environment::get().getWorld()->getStore()))
return false;
}
else
throw std::runtime_error (
"unsupported variable type in dialogue info select");
return true;
default:
2010-08-08 11:21:03 +00:00
std::cout << "unchecked select: " << type << " " << comp << " " << name << std::endl;
2010-08-08 11:21:03 +00:00
}
}
return true;
}
bool DialogueManager::isMatching (const MWWorld::Ptr& actor, const ESM::DialInfo& info) const
{
2012-05-03 04:07:41 +00:00
bool isCreature = (actor.getTypeName() != typeid(ESM::NPC).name());
2010-08-08 12:28:35 +00:00
// actor id
if (!info.actor.empty())
2010-08-08 13:09:44 +00:00
if (toLower (info.actor)!=MWWorld::Class::get (actor).getId (actor))
2010-08-08 12:28:35 +00:00
return false;
//NPC race
2010-08-08 12:36:45 +00:00
if (!info.race.empty())
{
2012-05-03 04:07:41 +00:00
if (isCreature)
return false;
MWWorld::LiveCellRef<ESM::NPC> *cellRef = actor.get<ESM::NPC>();
2010-08-08 12:36:45 +00:00
if (!cellRef)
return false;
2010-08-08 13:09:44 +00:00
if (toLower (info.race)!=toLower (cellRef->base->race))
2010-08-08 12:36:45 +00:00
return false;
}
//NPC class
2010-08-08 13:12:53 +00:00
if (!info.clas.empty())
{
2012-05-03 04:07:41 +00:00
if (isCreature)
return false;
MWWorld::LiveCellRef<ESM::NPC> *cellRef = actor.get<ESM::NPC>();
2010-08-08 13:12:53 +00:00
if (!cellRef)
return false;
if (toLower (info.clas)!=toLower (cellRef->base->cls))
return false;
}
//NPC faction
2010-08-08 13:17:49 +00:00
if (!info.npcFaction.empty())
{
2012-05-03 04:07:41 +00:00
if (isCreature)
return false;
//MWWorld::Class npcClass = MWWorld::Class::get(actor);
MWMechanics::NpcStats stats = MWWorld::Class::get(actor).getNpcStats(actor);
std::map<std::string,int>::iterator it = stats.getFactionRanks().find(toLower(info.npcFaction));
if(it!=stats.getFactionRanks().end())
{
//check rank
2012-04-12 15:29:05 +00:00
if(it->second < (int)info.data.rank) return false;
}
else
{
//not in the faction
return false;
}
2010-08-08 13:17:49 +00:00
}
// TODO check player faction
2012-04-04 20:13:57 +00:00
if(!info.pcFaction.empty())
{
MWMechanics::NpcStats stats = MWWorld::Class::get(MWBase::Environment::get().getWorld()->getPlayer().getPlayer()).getNpcStats(MWBase::Environment::get().getWorld()->getPlayer().getPlayer());
std::map<std::string,int>::iterator it = stats.getFactionRanks().find(toLower(info.pcFaction));
if(it!=stats.getFactionRanks().end())
2012-04-04 20:13:57 +00:00
{
//check rank
2012-04-12 15:29:05 +00:00
if(it->second < (int)info.data.PCrank) return false;
2012-04-04 20:13:57 +00:00
}
else
{
//not in the faction
return false;
}
}
2010-08-08 09:37:59 +00:00
//check gender
2012-05-03 04:07:41 +00:00
if (!isCreature)
{
MWWorld::LiveCellRef<ESM::NPC>* npc = actor.get<ESM::NPC>();
2012-05-03 04:07:41 +00:00
if(npc->base->flags&npc->base->Female)
{
if(static_cast<int> (info.data.gender)==0) return false;
}
else
{
if(static_cast<int> (info.data.gender)==1) return false;
}
}
2010-08-08 09:37:59 +00:00
// check cell
if (!info.cell.empty())
if (MWBase::Environment::get().getWorld()->getPlayer().getPlayer().getCell()->cell->name != info.cell)
2010-08-08 09:37:59 +00:00
return false;
// TODO check DATAstruct
2010-08-08 11:21:03 +00:00
for (std::vector<ESM::DialInfo::SelectStruct>::const_iterator iter (info.selects.begin());
iter != info.selects.end(); ++iter)
if (!isMatching (actor, *iter))
return false;
return true;
}
DialogueManager::DialogueManager (const Compiler::Extensions& extensions) :
mCompilerContext (MWScript::CompilerContext::Type_Dialgoue),
mErrorStream(std::cout.rdbuf()),mErrorHandler(mErrorStream)
2012-02-17 19:20:23 +00:00
{
mChoice = -1;
mIsInChoice = false;
mCompilerContext.setExtensions (&extensions);
2012-03-23 14:24:39 +00:00
mDialogueMap.clear();
mActorKnownTopics.clear();
ESMS::RecListCaseT<ESM::Dialogue>::MapType dialogueList = MWBase::Environment::get().getWorld()->getStore().dialogs.list;
2012-04-17 21:47:50 +00:00
for(ESMS::RecListCaseT<ESM::Dialogue>::MapType::iterator it = dialogueList.begin(); it!=dialogueList.end();it++)
2012-03-23 14:24:39 +00:00
{
2012-04-17 21:47:50 +00:00
mDialogueMap[toLower(it->first)] = it->second;
2012-03-23 14:24:39 +00:00
}
2012-02-17 19:20:23 +00:00
}
2010-08-06 16:01:34 +00:00
2012-08-09 07:41:17 +00:00
void DialogueManager::addTopic (const std::string& topic)
2012-02-10 15:09:43 +00:00
{
mKnownTopics[toLower(topic)] = true;
2012-02-10 15:09:43 +00:00
}
2012-08-09 07:41:17 +00:00
void DialogueManager::parseText (std::string text)
2012-02-15 12:23:59 +00:00
{
std::list<std::string>::iterator it;
for(it = mActorKnownTopics.begin();it != mActorKnownTopics.end();++it)
2012-02-15 12:23:59 +00:00
{
size_t pos = find_str_ci(text,*it,0);
2012-02-15 12:23:59 +00:00
if(pos !=std::string::npos)
{
if(pos==0)
2012-02-15 12:23:59 +00:00
{
mKnownTopics[*it] = true;
}
else if(text.substr(pos -1,1) == " ")
{
mKnownTopics[*it] = true;
2012-02-15 12:23:59 +00:00
}
}
}
2012-03-19 17:30:52 +00:00
updateTopics();
2012-02-15 12:23:59 +00:00
}
void DialogueManager::startDialogue (const MWWorld::Ptr& actor)
2010-08-06 16:01:34 +00:00
{
2012-03-18 14:26:18 +00:00
mChoice = -1;
mIsInChoice = false;
2012-02-17 19:20:23 +00:00
mActor = actor;
mActorKnownTopics.clear();
2012-03-19 18:21:08 +00:00
2012-02-10 21:01:31 +00:00
//initialise the GUI
2012-05-23 10:23:35 +00:00
MWBase::Environment::get().getWindowManager()->pushGuiMode(MWGui::GM_Dialogue);
MWGui::DialogueWindow* win = MWBase::Environment::get().getWindowManager()->getDialogueWindow();
2012-05-17 15:15:44 +00:00
win->startDialogue(actor, MWWorld::Class::get (actor).getName (actor));
2012-02-10 21:01:31 +00:00
2012-03-16 16:53:34 +00: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 21:01:31 +00:00
//greeting
2012-02-11 11:19:02 +00:00
bool greetingFound = false;
//ESMS::RecListT<ESM::Dialogue>::MapType dialogueList = MWBase::Environment::get().getWorld()->getStore().dialogs.list;
ESMS::RecListCaseT<ESM::Dialogue>::MapType dialogueList = MWBase::Environment::get().getWorld()->getStore().dialogs.list;
2012-04-17 21:47:50 +00:00
for(ESMS::RecListCaseT<ESM::Dialogue>::MapType::iterator it = dialogueList.begin(); it!=dialogueList.end();it++)
{
2012-02-11 11:19:02 +00:00
ESM::Dialogue ndialogue = it->second;
2012-02-12 12:58:02 +00:00
if(ndialogue.type == ESM::Dialogue::Greeting)
{
2012-02-11 11:19:02 +00:00
if (greetingFound) break;
for (std::vector<ESM::DialInfo>::const_iterator iter (it->second.mInfo.begin());
iter!=it->second.mInfo.end(); ++iter)
2012-02-10 21:54:17 +00:00
{
2012-03-18 10:32:05 +00:00
if (isMatching (actor, *iter) && functionFilter(mActor,*iter,true))
2012-02-10 21:54:17 +00:00
{
2012-02-11 11:19:02 +00:00
if (!iter->sound.empty())
{
// TODO play sound
}
std::string text = iter->response;
2012-02-15 12:23:59 +00:00
parseText(text);
2012-02-11 11:19:02 +00:00
win->addText(iter->response);
2012-02-17 19:20:23 +00:00
executeScript(iter->resultScript);
2012-02-11 11:19:02 +00:00
greetingFound = true;
mLastTopic = it->first;
mLastDialogue = *iter;
2012-02-11 11:19:02 +00:00
break;
2012-02-10 21:54:17 +00:00
}
}
}
}
2012-02-10 21:01:31 +00:00
}
bool DialogueManager::compile (const std::string& cmd,std::vector<Interpreter::Type_Code>& code)
{
2012-02-17 19:20:23 +00:00
try
2012-02-10 21:01:31 +00:00
{
mErrorHandler.reset();
2012-02-17 19:20:23 +00:00
2012-03-05 15:56:14 +00:00
std::istringstream input (cmd + "\n");
2012-02-17 19:20:23 +00:00
Compiler::Scanner scanner (mErrorHandler, input, mCompilerContext.getExtensions());
2012-02-17 19:20:23 +00:00
2012-03-05 15:56:14 +00: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 15:56:14 +00:00
}
Compiler::ScriptParser parser(mErrorHandler,mCompilerContext, locals, false);
2012-02-17 19:20:23 +00:00
scanner.scan (parser);
if(mErrorHandler.isGood())
2012-02-10 21:01:31 +00:00
{
parser.getCode(code);
return true;
2012-02-10 21:01:31 +00:00
}
return false;
2012-02-17 19:20:23 +00:00
}
catch (const Compiler::SourceException& /* error */)
2012-02-17 19:20:23 +00:00
{
// error has already been reported via error handler
2012-02-10 21:01:31 +00:00
}
2012-02-17 19:20:23 +00:00
catch (const std::exception& error)
{
printError (std::string ("An exception has been thrown: ") + error.what());
}
return false;
}
void DialogueManager::executeScript(std::string script)
{
std::vector<Interpreter::Type_Code> code;
if(compile(script,code))
2012-02-17 19:20:23 +00:00
{
try
{
MWScript::InterpreterContext interpreterContext(&mActor.getRefData().getLocals(),mActor);
2012-02-17 19:20:23 +00: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 17:30:52 +00:00
std::list<std::string> keywordList;
int choice = mChoice;
mChoice = -1;
mActorKnownTopics.clear();
MWGui::DialogueWindow* win = MWBase::Environment::get().getWindowManager()->getDialogueWindow();
ESMS::RecListCaseT<ESM::Dialogue>::MapType dialogueList = MWBase::Environment::get().getWorld()->getStore().dialogs.list;
2012-04-17 21:47:50 +00:00
for(ESMS::RecListCaseT<ESM::Dialogue>::MapType::iterator it = dialogueList.begin(); it!=dialogueList.end();it++)
{
ESM::Dialogue ndialogue = it->second;
if(ndialogue.type == ESM::Dialogue::Topic)
{
for (std::vector<ESM::DialInfo>::const_iterator iter (it->second.mInfo.begin());
iter!=it->second.mInfo.end(); ++iter)
{
if (isMatching (mActor, *iter) && functionFilter(mActor,*iter,true))
{
mActorKnownTopics.push_back(toLower(it->first));
//does the player know the topic?
if(mKnownTopics.find(toLower(it->first)) != mKnownTopics.end())
{
2012-03-19 17:30:52 +00:00
keywordList.push_back(it->first);
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>();
if (ref->base->hasAI)
services = ref->base->AI.services;
}
else if (mActor.getTypeName() == typeid(ESM::Creature).name())
{
MWWorld::LiveCellRef<ESM::Creature>* ref = mActor.get<ESM::Creature>();
if (ref->base->hasAI)
services = ref->base->AI.services;
}
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)
win->setShowTrade(true);
else
win->setShowTrade(false);
// sort again, because the previous sort was case-sensitive
keywordList.sort(stringCompareNoCase);
2012-03-19 17:30:52 +00:00
win->setKeywords(keywordList);
mChoice = choice;
}
2012-08-09 07:41:17 +00:00
void DialogueManager::keywordSelected (const std::string& keyword)
{
if(!mIsInChoice)
{
if(mDialogueMap.find(keyword) != mDialogueMap.end())
{
ESM::Dialogue ndialogue = mDialogueMap[keyword];
if(ndialogue.type == ESM::Dialogue::Topic)
{
for (std::vector<ESM::DialInfo>::const_iterator iter = ndialogue.mInfo.begin();
iter!=ndialogue.mInfo.end(); ++iter)
{
if (isMatching (mActor, *iter) && functionFilter(mActor,*iter,true))
{
std::string text = iter->response;
std::string script = iter->resultScript;
parseText(text);
MWGui::DialogueWindow* win = MWBase::Environment::get().getWindowManager()->getDialogueWindow();
win->addTitle(keyword);
win->addText(iter->response);
2012-03-19 18:21:08 +00:00
executeScript(script);
mLastTopic = keyword;
mLastDialogue = *iter;
break;
}
}
}
}
}
2012-03-20 09:15:22 +00:00
updateTopics();
}
void DialogueManager::goodbyeSelected()
{
MWBase::Environment::get().getWindowManager()->removeGuiMode(MWGui::GM_Dialogue);
}
2012-08-09 07:41:17 +00:00
void DialogueManager::questionAnswered (const std::string& answer)
{
2012-08-09 07:41:17 +00:00
if(mChoiceMap.find(answer) != mChoiceMap.end())
{
2012-08-09 07:41:17 +00:00
mChoice = mChoiceMap[answer];
std::vector<ESM::DialInfo>::const_iterator iter;
if(mDialogueMap.find(mLastTopic) != mDialogueMap.end())
{
ESM::Dialogue ndialogue = mDialogueMap[mLastTopic];
if(ndialogue.type == ESM::Dialogue::Topic)
{
2012-03-19 17:30:52 +00:00
for (std::vector<ESM::DialInfo>::const_iterator iter = ndialogue.mInfo.begin();
iter!=ndialogue.mInfo.end(); ++iter)
{
2012-03-19 17:30:52 +00:00
if (isMatching (mActor, *iter) && functionFilter(mActor,*iter,true))
{
mChoiceMap.clear();
mChoice = -1;
mIsInChoice = false;
MWGui::DialogueWindow* win = MWBase::Environment::get().getWindowManager()->getDialogueWindow();
2012-03-19 17:30:52 +00:00
std::string text = iter->response;
parseText(text);
win->addText(text);
2012-03-19 17:30:52 +00:00
executeScript(iter->resultScript);
mLastTopic = mLastTopic;
2012-03-19 17:30:52 +00:00
mLastDialogue = *iter;
break;
}
}
}
}
updateTopics();
}
}
2012-08-09 07:41:17 +00:00
void DialogueManager::printError (std::string error)
2012-02-17 19:20:23 +00:00
{
MWGui::DialogueWindow* win = MWBase::Environment::get().getWindowManager()->getDialogueWindow();
2012-02-17 19:20:23 +00:00
win->addText(error);
}
2012-08-09 07:41:17 +00: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 20:13:57 +00:00
2012-08-09 07:41:17 +00:00
std::string DialogueManager::getFaction() const
2012-04-04 20:13:57 +00:00
{
2012-05-03 04:07:41 +00:00
if (mActor.getTypeName() != typeid(ESM::NPC).name())
return "";
2012-04-04 20:13:57 +00:00
std::string factionID("");
MWMechanics::NpcStats stats = MWWorld::Class::get(mActor).getNpcStats(mActor);
if(stats.getFactionRanks().empty())
2012-04-04 20:13:57 +00:00
{
std::cout << "No faction for this actor!";
}
else
{
factionID = stats.getFactionRanks().begin()->first;
2012-04-04 20:13:57 +00:00
}
return factionID;
}
void DialogueManager::goodbye()
{
MWGui::DialogueWindow* win = MWBase::Environment::get().getWindowManager()->getDialogueWindow();
win->goodbye();
}
2010-08-06 16:01:34 +00:00
}