Debian Codegraph @

plugins/dm.conversation/ConversationEntity.cpp

raw
1 #include "ConversationEntity.h"
2
3 #include <limits>
4
5 #include "i18n.h"
6 #include "itextstream.h"
7 #include "ientity.h"
8 #include "iundo.h"
9
10 #include "string/convert.h"
11
12 #include "ConversationKeyExtractor.h"
13 #include "ConversationCommandLibrary.h"
14
15 namespace conversation
16 {
17
18 // Constructor
19 ConversationEntity::ConversationEntity(const scene::INodePtr& node) :
20 _entityNode(node)
21 {
22 Entity* entity = Node_getEntity(node);
23 assert(entity != nullptr);
24
25 // Use an conversationKeyExtractor to populate the ConversationMap from the keys
26 // on the entity
27 ConversationKeyExtractor extractor(_conversations);
28 entity->forEachKeyValue(extractor);
29 }
30
31 // Delete the entity's world node
32 void ConversationEntity::deleteWorldNode()
33 {
34 UndoableCommand deleteCommand("removeConversationEntity");
35
36 // Try to convert the weak_ptr reference to a shared_ptr
37 auto node = _entityNode.lock();
38
39 if (node && node->getParent())
40 {
41 node->getParent()->removeChildNode(node);
42 }
43 }
44
45 int ConversationEntity::getHighestIndex()
46 {
47 if (_conversations.empty())
48 {
49 return -1;
50 }
51
52 return _conversations.rbegin()->first;
53 }
54
55 // Add a new conversation
56 int ConversationEntity::addConversation()
57 {
58 // Locate the first unused id
59 int index = 1;
60 while (_conversations.find(index) != _conversations.end())
61 {
62 if (index == std::numeric_limits<int>::max())
63 {
64 rError() << "Ran out of conversation indices." << std::endl;
65 throw new std::runtime_error("Ran out of conversation indices.");
66 }
67
68 ++index;
69 }
70
71 // Insert a new conversation at this ID.
72 Conversation o;
73 o.name = _("New Conversation");
74 _conversations.insert(std::make_pair(index, o));
75
76 return index;
77 }
78
79 void ConversationEntity::deleteConversation(int index)
80 {
81 // Look up the conversation with the given index
82 auto i = _conversations.find(index);
83
84 if (i == _conversations.end())
85 {
86 // not found, nothing to do
87 return;
88 }
89
90 // Delete the found element and move the iterator past it
91 _conversations.erase(i++);
92
93 // Then iterate all the way to the highest index
94 while (i != _conversations.end())
95 {
96 // Decrease the index of this conversation
97 int newIndex = i->first - 1;
98 // Copy the conversation into a temporary object
99 Conversation temp = i->second;
100
101 // Remove the old one
102 _conversations.erase(i++);
103
104 // Re-insert with new index
105 _conversations.insert(std::make_pair(newIndex, temp));
106 }
107 }
108
109 int ConversationEntity::moveConversation(int index, bool moveUp)
110 {
111 if ((moveUp && index <= 1) || (!moveUp && index >= getHighestIndex()))
112 {
113 return index; // no change
114 }
115
116 int targetIndex = index + (moveUp ? -1 : +1);
117
118 if (_conversations.find(targetIndex) != _conversations.end())
119 {
120 // Swap with existing element
121 std::swap(_conversations[index], _conversations[targetIndex]);
122 }
123 else
124 {
125 // Nothing present at the target index, just move
126 _conversations[targetIndex] = _conversations[index];
127 _conversations.erase(index);
128 }
129
130 return targetIndex;
131 }
132
133 void ConversationEntity::populateListStore(wxutil::TreeModel& store, const ConversationColumns& columns) const
134 {
135 for (const auto& pair : _conversations)
136 {
137 wxutil::TreeModel::Row row = store.AddItem();
138
139 row[columns.index] = pair.first;
140 row[columns.name] = pair.second.name;
141
142 row.SendItemAdded();
143 }
144 }
145
146 void ConversationEntity::clearEntity(Entity* entity)
147 {
148 // Get all keyvalues matching the "obj" prefix.
149 auto keyValues = entity->getKeyValuePairs("conv_");
150
151 for (const auto& keyValuePair : keyValues)
152 {
153 // Set the spawnarg to empty, which is equivalent to a removal
154 entity->setKeyValue(keyValuePair.first, "");
155 }
156 }
157
158 // Write out conversations to entity keyvals
159 void ConversationEntity::writeToEntity()
160 {
161 // Try to convert the weak_ptr reference to a shared_ptr
162 Entity* entity = Node_getEntity(_entityNode.lock());
163 assert(entity != nullptr);
164
165 // greebo: Remove all conversation-related spawnargs first
166 clearEntity(entity);
167
168 for (const auto& pair : _conversations)
169 {
170 // Obtain the conversation and construct the key prefix from the index
171 const Conversation& conv = pair.second;
172 std::string prefix = "conv_" + string::to_string(pair.first) + "_";
173
174 // Set the entity keyvalues
175 entity->setKeyValue(prefix + "name", conv.name);
176 entity->setKeyValue(prefix + "actors_must_be_within_talkdistance",
177 conv.actorsMustBeWithinTalkdistance ? "1" : "0");
178 entity->setKeyValue(prefix + "talk_distance", string::to_string(conv.talkDistance));
179 entity->setKeyValue(prefix + "actors_always_face_each_other_while_talking",
180 conv.actorsAlwaysFaceEachOther ? "1" : "0");
181 entity->setKeyValue(prefix + "max_play_count", string::to_string(conv.maxPlayCount));
182
183 // Write the actor list
184 for (const auto& actor : conv.actors)
185 {
186 std::string actorKey = prefix + "actor_" + string::to_string(actor.first);
187 entity->setKeyValue(actorKey, actor.second);
188 }
189
190 // Write all the commands
191 for (const auto& cmd : conv.commands)
192 {
193 std::string cmdPrefix = prefix + "cmd_" + string::to_string(cmd.first) + "_";
194
195 try
196 {
197 const auto& cmdInfo = ConversationCommandLibrary::Instance().findCommandInfo(cmd.second->type);
198
199 entity->setKeyValue(cmdPrefix + "type", cmdInfo.name);
200 entity->setKeyValue(cmdPrefix + "actor", string::to_string(cmd.second->actor));
201 entity->setKeyValue(cmdPrefix + "wait_until_finished", cmd.second->waitUntilFinished ? "1" : "0");
202
203 for (const auto& arg : cmd.second->arguments)
204 {
205 entity->setKeyValue(cmdPrefix + "arg_" + string::to_string(arg.first), arg.second);
206 }
207 }
208 catch (const std::runtime_error&)
209 {
210 rError() << "Unrecognised conversation command ID: " << cmd.second->type << std::endl;
211 }
212 }
213 }
214 }
215
216 } // namespace conversations