ScummVM API documentation
converse.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef NUVIE_CORE_CONVERSE_H
23 #define NUVIE_CORE_CONVERSE_H
24 
25 #include "ultima/shared/std/string.h"
26 #include "ultima/shared/std/containers.h"
27 #include "ultima/nuvie/actors/actor.h"
28 #include "ultima/nuvie/gui/widgets/msg_scroll.h"
29 #include "ultima/nuvie/files/u6_lib_n.h"
30 #include "ultima/nuvie/views/view.h"
31 
32 namespace Ultima {
33 namespace Nuvie {
34 
35 class Actor;
36 class ActorManager;
37 class Configuration;
38 class MsgScroll;
39 class GameClock;
40 class ObjManager;
41 class Player;
42 class ViewManager;
43 class U6Lib_n;
44 class U6Lzw;
45 
46 class ConverseInterpret;
47 class ConverseSpeech;
48 class ConvScript;
49 
50 using Std::string;
51 
52 ConverseGumpType get_converse_gump_type_from_config(const Configuration *config);
53 
54 typedef uint32 converse_value; // any single value read from a script
55 typedef unsigned char *convscript_buffer;
56 
57 typedef struct {
58  uint8 type;
59  converse_value val;
61 
62 #define U6TALK_VAR_SEX 0x10 // sex of avatar: male=0 female=1
63 #define U6TALK_VAR_KARMA 0x14 // avatar's karma
64 #define U6TALK_VAR_GARGF 0x15 // 1=player knows Gargish
65 #define U6TALK_VAR_NPC_NAME 0x17
66 #define U6TALK_VAR_PARTYLIVE 0x17 // number of people (living) following avatar
67 #define U6TALK_VAR_PARTYALL 0x18 // number of people (total) following avatar
68 #define U6TALK_VAR_HP 0x19 // avatar's health
69 #define U6TALK_VAR_PLAYER_NAME 0x19
70 #define U6TALK_VAR_QUESTF 0x1A // 0="Thou art not upon a sacred quest!"
71 #define WOUTALK_VAR_ADD_TO_INVENTORY_FAILED 0x1D
72 #define U6TALK_VAR_WORKTYPE 0x20 // current activity of npc, from schedule
73 #define U6TALK_VAR_YSTRING 0x22 // value of $Y variable.
74 #define U6TALK_VAR_INPUT 0x23 // previous input from player ($Z)
75 #define U6TALK_VAR__LAST_ 0x25 // (all above 36 appear uninitialized)
76 
77 /* Conversation engine, apart from the interpreter. Loads converse files,
78  * and reads script into buffer. Also manages input/output and has npc-related
79  * support functions. This class handles all game types.
80  */
81 class Converse {
82  friend class ConverseInterpret;
83  friend class SETalkInterpret;
84  friend class MDTalkInterpret;
85  friend class WOUConverseInterpret;
86  friend class U6ConverseInterpret;
87 
88  // game system objects from nuvie
89  const Configuration *config;
90  GameClock *_clock;
91  ActorManager *actors;
92  ObjManager *objects;
93  Player *player;
94  ViewManager *views;
95  MsgScroll *scroll; // i/o
96 
97  nuvie_game_t gametype; // what game is being played?
98  U6Lib_n *src;
99  uint8 src_num; // identify source file: 0=unset/unused
100  const char *src_name();
101 
102  ConverseInterpret *conv_i; // interpreter
103  ConvScript *script;
104  View *last_view;
105  Actor *npc;
106  uint8 npc_num;
107  uint8 script_num; //this could differ from npc_num when talking to guards or wisps etc.
108  Std::string _name, _desc;
109 
110  bool active; // running npc script? (either paused or unpaused)
111  bool need_input; // waiting for text input
112  bool party_all_the_time; // force NPCs to join player's party?
113 
114  string in_str; // last input from player
115  string out_str; // text that is to be printed
116  char *allowed_input; // characters requested for single-character input
117 
118  char aname[16]; // return from npc_name()
119  struct converse_variables_s {
120  converse_value cv;
121  char *sv;
122  } *variables; /* initialized for [U6TALK_VAR__LAST_+1] items */
123 
124  ConverseSpeech *speech;
125  bool using_fmtowns;
126 
127  void reset();
128 
129 public:
130  Converse();
131  ~Converse();
132  void init(const Configuration *cfg, nuvie_game_t t, MsgScroll *s, ActorManager *a,
133  GameClock *c, Player *p, ViewManager *v, ObjManager *o);
134 
135  uint32 get_script_num(uint8 a);
136  void load_conv(const Std::string &convfilename);
137  uint32 load_conv(uint8 a);
138  void unload_conv() {
139  delete src;
140  src = nullptr;
141  }
142  ConvScript *load_script(uint32 n);
143  ConverseInterpret *new_interpreter();
144 
145  bool start(Actor *a) {
146  return start(a->get_actor_num());
147  }
148  bool start(uint8 n);
149  void continue_script();
150  void stop();
151 
152  bool running() const {
153  return active;
154  }
155  bool is_waiting_for_scroll() {
156  return scroll->get_page_break();
157  }
158  void unwait();
159  void poll_input(const char *allowed = nullptr, bool nonblock = true);
160  bool override_input();
161  void collect_input();
162 
163  bool input();
164  void print(const char *s = nullptr);
165  const Std::string &get_input() const {
166  return in_str;
167  }
168  const Std::string &get_output() const {
169  return out_str;
170  }
171  void set_input(Std::string s) {
172  in_str = s;
173  }
174  void set_output(Std::string s) {
175  out_str = s;
176  }
177 
178  void set_party_all_the_time(bool val) {
179  party_all_the_time = val;
180  }
181  const char *npc_name(uint8 num);
182  void show_portrait(uint8 n);
183  converse_value get_var(uint8 varnum) const {
184  return (varnum <= U6TALK_VAR__LAST_ ? variables[varnum].cv : 0x00);
185  }
186  const char *get_svar(uint8 varnum);
187  void set_var(uint8 varnum, uint32 val) {
188  if (varnum <= U6TALK_VAR__LAST_) variables[varnum].cv = val;
189  }
190  void set_svar(uint8 varnum, const char *set);
191  void init_variables();
192  void delete_variables();
193 
194  ConverseSpeech *get_speech() {
195  return speech;
196  };
197 
198  bool conversations_stop_music;
199 private:
200  void print_prompt();
201 };
202 
203 
204 /* Conversation script container. Maintains current position in the script. The
205  * object only exists if it has data loaded. Different classes with an identical
206  * interface can be created to handle different games' file formats.
207  */
208 class ConvScript {
209  friend class Converse;
210 
211  convscript_buffer buf;
212  uint32 buf_len;
213  convscript_buffer buf_pt; // pointer into script (current location)
214 
215  U6Lib_n *src;
216  uint32 src_index;
217  bool compressed; // was the original file (LZW) compressed?
218 
219  uint8 ref; // Multiple objects can use the same buffer
220  ConvScript *cpy;
221 
222 public:
223  ConvScript(U6Lib_n *s, uint32 idx);
224  ConvScript(ConvScript *orig);
225  ~ConvScript();
226 
227  void read_script();
228  bool loaded() const {
229  return ((buf && buf_len)); // script is loaded?
230  }
231 
232  /* Reading */
233  converse_value read(uint32 advance = 1);
234  converse_value read2();
235  converse_value read4();
236  converse_value peek(uint32 displacement = 0) {
237  return ((converse_value) * (buf_pt + displacement));
238  }
239 
240  /* Writing */
241  void write2(converse_value val);
242 
243  /* Seeking - update script pointer */
244  void rewind() {
245  buf_pt = buf;
246  }
247  void skip(uint32 bytes = 1) {
248  buf_pt += bytes;
249  }
250  void seek(uint32 offset = 0) {
251  rewind();
252  skip(offset);
253  }
254 
255  uint32 pos() const {
256  return buf_pt - buf;
257  }
258  bool overflow(uint32 ptadd = 0) const {
259  return (((pos() + ptadd) >= buf_len));
260  }
261  convscript_buffer get_buffer(uint32 ptadd = 0) {
262  return ((!ptadd || (ptadd < buf_len)) ? buf + ptadd : nullptr);
263  }
264 };
265 
266 } // End of namespace Nuvie
267 } // End of namespace Ultima
268 
269 #endif
Definition: converse.h:57
Definition: actor.h:178
Definition: msg_scroll.h:93
Definition: converse_interpret.h:316
Definition: configuration.h:61
Definition: game_clock.h:49
Definition: player.h:41
Definition: actor_manager.h:42
Definition: detection.h:27
Definition: view.h:42
Definition: converse_speech.h:41
Definition: string.h:30
Definition: converse_interpret.h:310
Definition: u6_lib_n.h:46
Definition: obj_manager.h:75
Definition: converse_interpret.h:117
Definition: view_manager.h:57
Definition: converse_interpret.h:330
Definition: converse.h:81
Definition: converse_interpret.h:324
Definition: converse.h:208