1 #include "journalentry.hpp"
2 
3 #include <stdexcept>
4 
5 #include <components/esm/journalentry.hpp>
6 
7 #include <components/interpreter/defines.hpp>
8 
9 #include "../mwbase/environment.hpp"
10 #include "../mwbase/world.hpp"
11 
12 #include "../mwworld/esmstore.hpp"
13 
14 #include "../mwscript/interpretercontext.hpp"
15 
16 
17 namespace MWDialogue
18 {
Entry()19     Entry::Entry() {}
20 
Entry(const std::string & topic,const std::string & infoId,const MWWorld::Ptr & actor)21     Entry::Entry (const std::string& topic, const std::string& infoId, const MWWorld::Ptr& actor)
22     : mInfoId (infoId)
23     {
24         const ESM::Dialogue *dialogue =
25             MWBase::Environment::get().getWorld()->getStore().get<ESM::Dialogue>().find (topic);
26 
27         for (ESM::Dialogue::InfoContainer::const_iterator iter (dialogue->mInfo.begin());
28             iter!=dialogue->mInfo.end(); ++iter)
29             if (iter->mId == mInfoId)
30             {
31                 if (actor.isEmpty())
32                 {
33                     MWScript::InterpreterContext interpreterContext(nullptr, MWWorld::Ptr());
34                     mText = Interpreter::fixDefinesDialog(iter->mResponse, interpreterContext);
35                 }
36                 else
37                 {
38                     MWScript::InterpreterContext interpreterContext(&actor.getRefData().getLocals(),actor);
39                     mText = Interpreter::fixDefinesDialog(iter->mResponse, interpreterContext);
40                 }
41 
42                 return;
43             }
44 
45         throw std::runtime_error ("unknown info ID " + mInfoId + " for topic " + topic);
46     }
47 
Entry(const ESM::JournalEntry & record)48     Entry::Entry (const ESM::JournalEntry& record) : mInfoId (record.mInfo), mText (record.mText), mActorName(record.mActorName) {}
49 
getText() const50     std::string Entry::getText() const
51     {
52         return mText;
53     }
54 
write(ESM::JournalEntry & entry) const55     void Entry::write (ESM::JournalEntry& entry) const
56     {
57         entry.mInfo = mInfoId;
58         entry.mText = mText;
59         entry.mActorName = mActorName;
60     }
61 
62 
JournalEntry()63     JournalEntry::JournalEntry() {}
64 
JournalEntry(const std::string & topic,const std::string & infoId,const MWWorld::Ptr & actor)65     JournalEntry::JournalEntry (const std::string& topic, const std::string& infoId, const MWWorld::Ptr& actor)
66         : Entry (topic, infoId, actor), mTopic (topic)
67     {}
68 
JournalEntry(const ESM::JournalEntry & record)69     JournalEntry::JournalEntry (const ESM::JournalEntry& record)
70         : Entry (record), mTopic (record.mTopic)
71     {}
72 
write(ESM::JournalEntry & entry) const73     void JournalEntry::write (ESM::JournalEntry& entry) const
74     {
75         Entry::write (entry);
76         entry.mTopic = mTopic;
77     }
78 
makeFromQuest(const std::string & topic,int index)79     JournalEntry JournalEntry::makeFromQuest (const std::string& topic, int index)
80     {
81         return JournalEntry (topic, idFromIndex (topic, index), MWWorld::Ptr());
82     }
83 
idFromIndex(const std::string & topic,int index)84     std::string JournalEntry::idFromIndex (const std::string& topic, int index)
85     {
86         const ESM::Dialogue *dialogue =
87             MWBase::Environment::get().getWorld()->getStore().get<ESM::Dialogue>().find (topic);
88 
89         for (ESM::Dialogue::InfoContainer::const_iterator iter (dialogue->mInfo.begin());
90             iter!=dialogue->mInfo.end(); ++iter)
91             if (iter->mData.mJournalIndex==index)
92             {
93                 return iter->mId;
94             }
95 
96         throw std::runtime_error ("unknown journal index for topic " + topic);
97     }
98 
99 
StampedJournalEntry()100     StampedJournalEntry::StampedJournalEntry()
101     : mDay (0), mMonth (0), mDayOfMonth (0)
102     {}
103 
StampedJournalEntry(const std::string & topic,const std::string & infoId,int day,int month,int dayOfMonth,const MWWorld::Ptr & actor)104     StampedJournalEntry::StampedJournalEntry (const std::string& topic, const std::string& infoId,
105         int day, int month, int dayOfMonth, const MWWorld::Ptr& actor)
106     : JournalEntry (topic, infoId, actor), mDay (day), mMonth (month), mDayOfMonth (dayOfMonth)
107     {}
108 
StampedJournalEntry(const ESM::JournalEntry & record)109     StampedJournalEntry::StampedJournalEntry (const ESM::JournalEntry& record)
110     : JournalEntry (record), mDay (record.mDay), mMonth (record.mMonth),
111       mDayOfMonth (record.mDayOfMonth)
112     {}
113 
write(ESM::JournalEntry & entry) const114     void StampedJournalEntry::write (ESM::JournalEntry& entry) const
115     {
116         JournalEntry::write (entry);
117         entry.mDay = mDay;
118         entry.mMonth = mMonth;
119         entry.mDayOfMonth = mDayOfMonth;
120     }
121 
makeFromQuest(const std::string & topic,int index,const MWWorld::Ptr & actor)122     StampedJournalEntry StampedJournalEntry::makeFromQuest (const std::string& topic, int index, const MWWorld::Ptr& actor)
123     {
124         int day = MWBase::Environment::get().getWorld()->getGlobalInt ("dayspassed");
125         int month = MWBase::Environment::get().getWorld()->getGlobalInt ("month");
126         int dayOfMonth = MWBase::Environment::get().getWorld()->getGlobalInt ("day");
127 
128         return StampedJournalEntry (topic, idFromIndex (topic, index), day, month, dayOfMonth, actor);
129     }
130 }
131