Peoplemon  0.1.0
Peoplemon 3 game source documentation
Conversation.hpp
Go to the documentation of this file.
1 #ifndef CORE_FILES_CONVERSATION_HPP
2 #define CORE_FILES_CONVERSATION_HPP
3 
4 #include <BLIB/Resources.hpp>
5 #include <BLIB/Serialization.hpp>
6 #include <Core/Items/Id.hpp>
7 #include <cstdint>
8 #include <string>
9 #include <variant>
10 #include <vector>
11 
12 namespace core
13 {
14 namespace file
15 {
22 class Conversation {
23 public:
29  class Node {
30  public:
35  enum Type : std::uint8_t {
37  Talk = 0,
38 
40  Prompt = 1,
41 
43  GiveItem = 2,
44 
46  TakeItem = 3,
47 
49  GiveMoney = 4,
50 
52  TakeMoney = 5,
53 
55  RunScript = 6,
56 
59 
62 
65 
68  };
69 
70  struct Item {
74 
75  Item()
76  : id(item::Id::Unknown)
77  , beforePrompt(false)
78  , afterPrompt(false) {}
79  };
80 
87  static std::string typeToString(Type type);
88 
93  Node();
94 
100  Node(Type type);
101 
107  void setType(Type type);
108 
113  Type getType() const;
114 
119  std::string& message();
120 
125  std::string& script();
126 
131  std::string& saveFlag();
132 
137  std::vector<std::pair<std::string, std::uint32_t>>& choices();
138 
143  std::uint32_t& money();
144 
149  Item& item();
150 
155  std::uint32_t& next();
156 
161  std::uint32_t& nextOnPass();
162 
167  std::uint32_t& nextOnReject();
168 
173  const std::string& message() const;
174 
179  const std::string& script() const;
180 
185  const std::string& saveFlag() const;
186 
191  bool& runConcurrently();
192 
197  bool runConcurrently() const;
198 
203  const std::vector<std::pair<std::string, std::uint32_t>>& choices() const;
204 
209  unsigned int money() const;
210 
215  const Item& item() const;
216 
221  std::uint32_t next() const;
222 
227  std::uint32_t nextOnPass() const;
228 
233  std::uint32_t nextOnReject() const;
234 
235  private:
236  using TData = std::variant<std::vector<std::pair<std::string, std::uint32_t>>, Item,
237  std::uint32_t, bool>;
238 
239  Type type;
240  std::string prompt; // or script
241  TData data;
242  std::uint32_t jumps[2];
243 
244  friend struct bl::serial::SerializableObject<Node>;
245  friend struct bl::serial::SerializableObject<core::file::Conversation::Node::Item>;
246  };
247 
252  Conversation() = default;
253 
260  bool load(const std::string& file);
261 
268  bool loadDev(std::istream& input);
269 
276  bool loadProd(bl::serial::binary::InputStream& input);
277 
284  bool save(const std::string& file) const;
285 
293  bool saveBundle(bl::serial::binary::OutputStream& output,
294  bl::resource::bundle::FileHandlerContext& ctx) const;
295 
300  const std::vector<Node>& nodes() const;
301 
308  void deleteNode(unsigned int i);
309 
315  void appendNode(const Node& node);
316 
323  void setNode(unsigned int i, const Node& node);
324 
331  static Conversation makeLoadError(const std::string& filename);
332 
339  static void getNextJumps(const Node& node, std::vector<unsigned int>& jumps);
340 
341 private:
342  std::vector<Node> cnodes;
343 
344  friend struct bl::serial::SerializableObject<Conversation>;
345 };
346 
347 } // namespace file
348 } // namespace core
349 
350 namespace bl
351 {
352 namespace serial
353 {
354 template<>
355 struct SerializableObject<core::file::Conversation::Node::Item> : public SerializableObjectBase {
358 
359  SerializableField<1, Item, Id> id;
360  SerializableField<2, Item, bool> beforePrompt;
361  SerializableField<3, Item, bool> afterPrompt;
362 
364  : SerializableObjectBase("ConversationNodeItem")
365  , id("id", *this, &Item::id, SerializableFieldBase::Required{})
366  , beforePrompt("beforePrompt", *this, &Item::beforePrompt, SerializableFieldBase::Required{})
367  , afterPrompt("afterPrompt", *this, &Item::afterPrompt, SerializableFieldBase::Required{}) {}
368 };
369 
370 template<>
371 struct SerializableObject<core::file::Conversation::Node> : public SerializableObjectBase {
373 
374  SerializableField<1, Node, Node::Type> type;
375  SerializableField<2, Node, std::string> prompt;
376  SerializableField<3, Node, Node::TData> data;
377  SerializableField<4, Node, std::uint32_t[2]> jumps;
378 
380  : SerializableObjectBase("ConversationNode")
381  , type("type", *this, &Node::type, SerializableFieldBase::Required{})
382  , prompt("prompt", *this, &Node::prompt, SerializableFieldBase::Required{})
383  , data("data", *this, &Node::data, SerializableFieldBase::Required{})
384  , jumps("jumps", *this, &Node::jumps, SerializableFieldBase::Required{}) {}
385 };
386 
387 template<>
388 struct SerializableObject<core::file::Conversation> : public SerializableObjectBase {
391 
392  SerializableField<1, Conversation, std::vector<Node>> nodes;
393 
395  : SerializableObjectBase("Conversation")
396  , nodes("nodes", *this, &Conversation::cnodes, SerializableFieldBase::Required{}) {}
397 };
398 
399 } // namespace serial
400 } // namespace bl
401 
402 #endif
Id
Represents an item in its simplist form.
Definition: Id.hpp:24
Type
Represents a type that a move or Peoplemon can be. Types may be combined.
Definition: Type.hpp:18
Id
The id of a peoplemon.
Definition: Id.hpp:16
Core classes and functionality for both the editor and game.
Stores a conversation that an NPC or trainer can have with the player.
bool saveBundle(bl::serial::binary::OutputStream &output, bl::resource::bundle::FileHandlerContext &ctx) const
Saves the data from this object to the given bundle and registers depency files if any.
static void getNextJumps(const Node &node, std::vector< unsigned int > &jumps)
Populates the jumps vector with the indices reachable from the given node.
bool load(const std::string &file)
Loads the conversation from the given file.
void deleteNode(unsigned int i)
Deletes the node at the given index. Jump indexes for other nodes are updated if affected....
void setNode(unsigned int i, const Node &node)
Updates the value of the node at the given index.
bool loadProd(bl::serial::binary::InputStream &input)
Loads the conversation from its binary format.
const std::vector< Node > & nodes() const
Returns the list of nodes in the conversation.
bool loadDev(std::istream &input)
Loads the conversation from its json format.
bool save(const std::string &file) const
Saves the conversation to the given file.
static Conversation makeLoadError(const std::string &filename)
Helper function to create a conversation that reports an error if in debug mode.
void appendNode(const Node &node)
Appends the given node to the list of nodes.
Building block of conversations. A conversation is a tree of nodes and each node is an action that ha...
std::string & script()
Returns the script file of this node.
static std::string typeToString(Type type)
Converts a node type to a human readable string.
std::string & message()
Returns the message or prompt for this node.
std::uint32_t & nextOnPass()
Returns the next node if a check passes (ie take item or money)
std::uint32_t & nextOnReject()
Returns the next node if a check is rejected (ie not taking money or item)
void setType(Type type)
Clears node data and updates to a new type.
std::uint32_t & money()
Returns the money requested or given, undefined behavior if not a money node.
Type getType() const
Returns the type of this node.
bool & runConcurrently()
Returns whether or not the launched script should be executed concurrently.
std::string & saveFlag()
Returns the save flag of this node.
Item & item()
Returns the item to give or take. Undefined behavior if not an item node.
std::uint32_t & next()
Returns the index of the next node in the case of a Talk, Give, and Script nodes.
std::vector< std::pair< std::string, std::uint32_t > > & choices()
Returns the choices if this is a prompt node. Undefined behavior if not.
Node()
Creates an empty talk node.
Type
Represents the different effects that nodes can have.
@ TakeMoney
This will ask the player for money. They may refuse or not have enough.
@ RunScript
This will run a script. Must be a file.
@ CheckInteracted
This will check if interacted with the talking NPC and jump accordingly.
@ Prompt
This will output a message then prompt the player for a choice.
@ Talk
This just outputs a message.
@ TakeItem
This will ask the player for an item. They may refuse or not have it.
@ GiveItem
This unconditionally gives the player an item.
@ SetSaveFlag
This will check if a flag exists and jump to the next node accordingly.
@ CheckSaveFlag
This will check if a flag exists and jump to the next node accordingly.
@ GiveMoney
This will unconditionally give money to the player.
SerializableField< 4, Node, std::uint32_t[2]> jumps
SerializableField< 1, Conversation, std::vector< Node > > nodes