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 "common/str.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 ConverseGumpType get_converse_gump_type_from_config(const Configuration *config);
51 
52 typedef uint32 converse_value; // any single value read from a script
53 typedef unsigned char *convscript_buffer;
54 
55 typedef struct {
56  uint8 type;
57  converse_value val;
59 
60 #define U6TALK_VAR_SEX 0x10 // sex of avatar: male=0 female=1
61 #define U6TALK_VAR_KARMA 0x14 // avatar's karma
62 #define U6TALK_VAR_GARGF 0x15 // 1=player knows Gargish
63 #define U6TALK_VAR_NPC_NAME 0x17
64 #define U6TALK_VAR_PARTYLIVE 0x17 // number of people (living) following avatar
65 #define U6TALK_VAR_PARTYALL 0x18 // number of people (total) following avatar
66 #define U6TALK_VAR_HP 0x19 // avatar's health
67 #define U6TALK_VAR_PLAYER_NAME 0x19
68 #define U6TALK_VAR_QUESTF 0x1A // 0="Thou art not upon a sacred quest!"
69 #define WOUTALK_VAR_ADD_TO_INVENTORY_FAILED 0x1D
70 #define U6TALK_VAR_WORKTYPE 0x20 // current activity of npc, from schedule
71 #define U6TALK_VAR_YSTRING 0x22 // value of $Y variable.
72 #define U6TALK_VAR_INPUT 0x23 // previous input from player ($Z)
73 #define U6TALK_VAR__LAST_ 0x25 // (all above 36 appear uninitialized)
74 
75 /* Conversation engine, apart from the interpreter. Loads converse files,
76  * and reads script into buffer. Also manages input/output and has npc-related
77  * support functions. This class handles all game types.
78  */
79 class Converse {
80  friend class ConverseInterpret;
81  friend class SETalkInterpret;
82  friend class MDTalkInterpret;
83  friend class WOUConverseInterpret;
84  friend class U6ConverseInterpret;
85 
86  // game system objects from nuvie
87  const Configuration *config;
88  GameClock *_clock;
89  ActorManager *actors;
90  ObjManager *objects;
91  Player *player;
92  ViewManager *views;
93  MsgScroll *scroll; // i/o
94 
95  nuvie_game_t gametype; // what game is being played?
96  U6Lib_n *src;
97  uint8 src_num; // identify source file: 0=unset/unused
98  const char *src_name();
99 
100  ConverseInterpret *conv_i; // interpreter
101  ConvScript *script;
102  View *last_view;
103  Actor *npc;
104  uint8 npc_num;
105  uint8 script_num; //this could differ from npc_num when talking to guards or wisps etc.
106  Common::String _name, _desc;
107 
108  bool active; // running npc script? (either paused or unpaused)
109  bool need_input; // waiting for text input
110  bool party_all_the_time; // force NPCs to join player's party?
111 
112  Common::String in_str; // last input from player
113  Common::String out_str; // text that is to be printed
114  char *allowed_input; // characters requested for single-character input
115 
116  char aname[16]; // return from npc_name()
117  struct converse_variables_s {
118  converse_value cv;
119  char *sv;
120  } *variables; /* initialized for [U6TALK_VAR__LAST_+1] items */
121 
122  ConverseSpeech *speech;
123  bool using_fmtowns;
124 
125  void reset();
126 
127 public:
128  Converse();
129  ~Converse();
130  void init(const Configuration *cfg, nuvie_game_t t, MsgScroll *s, ActorManager *a,
131  GameClock *c, Player *p, ViewManager *v, ObjManager *o);
132 
133  uint32 get_script_num(uint8 a);
134  void load_conv(const Common::String &convfilename);
135  uint32 load_conv(uint8 a);
136  void unload_conv() {
137  delete src;
138  src = nullptr;
139  }
140  ConvScript *load_script(uint32 n);
141  ConverseInterpret *new_interpreter();
142 
143  bool start(Actor *a) {
144  return start(a->get_actor_num());
145  }
146  bool start(uint8 n);
147  void continue_script();
148  void stop();
149 
150  bool running() const {
151  return active;
152  }
153  bool is_waiting_for_scroll() {
154  return scroll->get_page_break();
155  }
156  void unwait();
157  void poll_input(const char *allowed = nullptr, bool nonblock = true);
158  bool override_input();
159  void collect_input();
160 
161  bool input();
162  void print(const char *s = nullptr);
163  const Common::String &get_input() const {
164  return in_str;
165  }
166  const Common::String &get_output() const {
167  return out_str;
168  }
169  void set_input(Common::String s) {
170  in_str = s;
171  }
172  void set_output(Common::String s) {
173  out_str = s;
174  }
175 
176  void set_party_all_the_time(bool val) {
177  party_all_the_time = val;
178  }
179  const char *npc_name(uint8 num);
180  void show_portrait(uint8 n);
181  converse_value get_var(uint8 varnum) const {
182  return (varnum <= U6TALK_VAR__LAST_ ? variables[varnum].cv : 0x00);
183  }
184  const char *get_svar(uint8 varnum);
185  void set_var(uint8 varnum, uint32 val) {
186  if (varnum <= U6TALK_VAR__LAST_) variables[varnum].cv = val;
187  }
188  void set_svar(uint8 varnum, const char *set);
189  void init_variables();
190  void delete_variables();
191 
192  ConverseSpeech *get_speech() {
193  return speech;
194  };
195 
196  bool conversations_stop_music;
197 private:
198  void print_prompt();
199 };
200 
201 
202 /* Conversation script container. Maintains current position in the script. The
203  * object only exists if it has data loaded. Different classes with an identical
204  * interface can be created to handle different games' file formats.
205  */
206 class ConvScript {
207  friend class Converse;
208 
209  convscript_buffer buf;
210  uint32 buf_len;
211  convscript_buffer buf_pt; // pointer into script (current location)
212 
213  U6Lib_n *src;
214  uint32 src_index;
215  bool compressed; // was the original file (LZW) compressed?
216 
217  uint8 ref; // Multiple objects can use the same buffer
218  ConvScript *cpy;
219 
220 public:
221  ConvScript(U6Lib_n *s, uint32 idx);
222  ConvScript(ConvScript *orig);
223  ~ConvScript();
224 
225  void read_script();
226  bool loaded() const {
227  return ((buf && buf_len)); // script is loaded?
228  }
229 
230  /* Reading */
231  converse_value read(uint32 advance = 1);
232  converse_value read2();
233  converse_value read4();
234  converse_value peek(uint32 displacement = 0) {
235  return ((converse_value) * (buf_pt + displacement));
236  }
237 
238  /* Writing */
239  void write2(converse_value val);
240 
241  /* Seeking - update script pointer */
242  void rewind() {
243  buf_pt = buf;
244  }
245  void skip(uint32 bytes = 1) {
246  buf_pt += bytes;
247  }
248  void seek(uint32 offset = 0) {
249  rewind();
250  skip(offset);
251  }
252 
253  uint32 pos() const {
254  return buf_pt - buf;
255  }
256  bool overflow(uint32 ptadd = 0) const {
257  return (((pos() + ptadd) >= buf_len));
258  }
259  convscript_buffer get_buffer(uint32 ptadd = 0) {
260  return ((!ptadd || (ptadd < buf_len)) ? buf + ptadd : nullptr);
261  }
262 };
263 
264 } // End of namespace Nuvie
265 } // End of namespace Ultima
266 
267 #endif
Definition: converse.h:55
Definition: actor.h:174
Definition: str.h:59
Definition: msg_scroll.h:91
Definition: converse_interpret.h:313
Definition: configuration.h:61
Definition: game_clock.h:47
Definition: player.h:41
Definition: actor_manager.h:42
Definition: detection.h:27
Definition: view.h:42
Definition: converse_speech.h:41
Definition: converse_interpret.h:307
Definition: u6_lib_n.h:43
Definition: obj_manager.h:75
Definition: converse_interpret.h:114
Definition: view_manager.h:55
Definition: converse_interpret.h:327
Definition: converse.h:79
Definition: converse_interpret.h:321
Definition: converse.h:206