1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
mod section;
mod tokens;
use crate::{
parser::tokens::{SemanticToken, Token},
player::Cursor,
};
use logos::Lexer;
use std::{collections::HashMap, str::FromStr};
use tokens::MentionToken;
pub use section::Section;
/// Character alias in dialog that maps to real character
#[derive(PartialEq, Eq, Hash, Debug, Clone)]
pub struct Alias(pub String);
/// State of character
#[derive(PartialEq, Eq, Hash, Debug, Clone)]
pub enum State {
/// Default state
Default,
/// Named state
Named(String),
}
/// Requirements for character in dialog. Contains list of states that used in dialog
#[derive(PartialEq, Debug, Default)]
pub struct Requirements {
/// Required states
pub states: Vec<State>,
}
/// Speaker in dialog. Can be Narrator of Character with alias and state
#[derive(PartialEq, Eq, Hash, Debug, Clone)]
pub enum Speaker {
/// Narrator speaks
Narrator,
/// Character speaks
Character(Alias, State),
}
/// Menu in dialog. Contains title and vec of options
#[derive(PartialEq, Eq, Hash, Debug, Clone)]
pub struct Menu {
/// Menu title
pub title: Option<String>,
/// Menu options
pub options: Vec<MenuOption>,
}
/// Menu option
#[derive(PartialEq, Eq, Hash, Debug, Clone)]
pub struct MenuOption {
/// Option title
pub title: Option<String>,
/// Option args. Will be replaced to collection of option effects
pub args: String, // TODO: replace to set of effects
}
/// Line in dialog
#[derive(Eq, Hash, PartialEq, Debug)]
pub enum Line {
/// CHaracted's phrases in dialog
Phrase {
/// The one who utters the phrase
speaker: Speaker,
/// A phrase. May consist of several parts that will be presented sequentially so as not to output too much text at a time
lines: Vec<String>,
}, // TODO: replace String to FormattedText,
/// Menu with options in dialog
Menu(Menu),
}
#[derive(PartialEq, Debug, Default)]
pub struct Dialog {
pub characters: HashMap<Alias, Requirements>,
pub sections: HashMap<Section, Vec<Line>>,
}
impl Dialog {
#[must_use]
pub fn get_line_by_cursor(&self, cursor: &Cursor) -> Option<&Line> {
self.sections
.get(cursor.section())
.and_then(|s| s.get(cursor.line_index()))
}
fn ensure_character_requirement(&mut self, mention: &Speaker) {
if let Speaker::Character(alias, state) = mention {
let req = self.characters.entry(alias.clone()).or_default();
if let State::Named(_) = state {
if !req.states.contains(state) {
req.states.push(state.clone());
}
}
}
}
fn parse_semantics(&mut self, semantics: Vec<SemanticToken>) -> Result<(), String> {
let mut current_mention = Speaker::Narrator;
let mut current_section = Section::Initial;
let mut current_menu: Option<Menu> = None;
let mut current_option: Option<MenuOption> = None;
for token in semantics {
let current_lines = self.sections.entry(current_section.clone()).or_default();
match token {
SemanticToken::Mention(variant) => {
match &variant {
MentionToken::Name(name) => {
self.characters.entry(Alias(name.clone())).or_default();
current_mention =
Speaker::Character(Alias(name.clone()), State::Default);
self.ensure_character_requirement(¤t_mention);
}
MentionToken::State(new_state) => {
if let Speaker::Character(_, state) = &mut current_mention {
// Это очень грустно, уберите клонирование пажалуста
*state = State::Named(new_state.clone());
self.ensure_character_requirement(¤t_mention);
} else {
return Err("Нельзя устанавливать состояние рассказчику".to_owned());
}
}
MentionToken::NameState(name, state) => {
current_mention = Speaker::Character(
Alias(name.clone()),
State::Named(state.clone()),
);
self.ensure_character_requirement(¤t_mention);
}
MentionToken::Narrator => current_mention = Speaker::Narrator,
};
}
SemanticToken::Link(name) => {
if let Some(mut menu) = current_menu {
if let Some(option) = current_option {
menu.options.push(option);
}
let lines = self.sections.entry(current_section.clone()).or_default();
lines.push(Line::Menu(menu));
}
current_menu = None;
current_option = None;
current_section = Section::Named(name);
current_mention = Speaker::Narrator;
}
SemanticToken::Text(lines) => {
// TODO: if lines length > 1 and current_menu is not None - stop filling menu
if let Some(menu) = &mut current_menu {
if lines.is_empty() {
panic!("0 lines in text token")
} else {
let title = lines[0].clone();
let left = &lines[1..];
if menu.title.is_none() {
menu.title = Some(title);
// FIXME: .unwrap() & .clone()
} else if let Some(option) = &mut current_option {
option.title = Some(title);
} else {
panic!("text after `menu` tag is not allowed cause menu is final statement of section");
}
if !left.is_empty() {
if let Some(option) = ¤t_option {
menu.options.push(option.clone());
current_lines.push(Line::Menu(menu.clone()));
current_menu = None;
}
}
}
} else {
current_lines.push(Line::Phrase {
speaker: current_mention.clone(),
lines,
});
}
}
SemanticToken::Command(command, args) => match command.as_str() {
"menu" => {
current_menu = Some(Menu {
title: None,
options: vec![],
});
}
"opt" => {
if let Some(menu) = &mut current_menu {
if let Some(option) = ¤t_option {
menu.options.push(option.clone());
}
current_option = Some(MenuOption { title: None, args });
} else {
panic!("`opt` without the preceding `menu`");
}
}
_ => {}
},
SemanticToken::InlineBlock(v) => {
panic!("inline block `{}` is not yet implemented :(", v);
}
}
}
if let Some(mut menu) = current_menu {
if let Some(option) = current_option {
menu.options.push(option);
}
let lines = self.sections.entry(current_section).or_default();
lines.push(Line::Menu(menu));
}
Ok(())
}
}
impl FromStr for Dialog {
type Err = String;
fn from_str(raw: &str) -> Result<Dialog, Self::Err> {
let mut lex = Lexer::<Token>::new(raw);
let mut buf = String::new();
let mut semantics = vec![];
while let Some(token) = lex.next() {
let value = lex.slice().trim();
if let Token::Text = token {
buf.push_str(lex.slice());
} else if !buf.is_empty() {
let lines = buf
.lines()
.collect::<Vec<_>>()
.chunks(2)
.filter_map(|e| {
let str = e.join("\n").trim().to_owned();
if str.is_empty() {
None
} else {
Some(str)
}
})
.collect::<Vec<_>>();
if !lines.is_empty() {
semantics.push(SemanticToken::Text(lines));
}
buf.clear();
}
let semantic_token = match token {
Token::Mention => {
Some(if value.len() == 1 {
SemanticToken::Mention(MentionToken::Narrator)
} else {
let splitted = value[1..].split(':').collect::<Vec<_>>();
match splitted.len() {
1 => SemanticToken::Mention(MentionToken::Name(splitted[0].into())), // no state?
2 => {
if splitted[0].trim().is_empty() {
SemanticToken::Mention(MentionToken::State(splitted[1].into()))
} else {
SemanticToken::Mention(MentionToken::NameState(
splitted[0].into(),
splitted[1].into(),
))
}
}
_ => panic!("Ты чево наделол"),
}
})
}
Token::InlineBlock => Some(SemanticToken::InlineBlock(String::from(value))),
Token::Link => Some(SemanticToken::Link(String::from(&value[1..]))),
Token::Command => {
if let Some(index) = value.find('(') {
let name = &value[1..index];
let args = &value[index + 1..value.len() - 1];
Some(SemanticToken::Command(name.to_string(), args.to_string()))
} else {
Some(SemanticToken::Command(value[1..].into(), String::new()))
}
}
_ => None,
};
if let Some(token) = semantic_token {
semantics.push(token);
}
}
if !buf.is_empty() {
let lines = buf
.lines()
.collect::<Vec<_>>()
.chunks(2)
.filter_map(|e| {
let str = e.join("\n").trim().to_owned();
if str.is_empty() {
None
} else {
Some(str)
}
})
.collect::<Vec<_>>();
if !lines.is_empty() {
semantics.push(SemanticToken::Text(lines));
}
buf.clear();
}
let mut dlg: Dialog = Dialog::default();
dlg.parse_semantics(semantics)?;
Ok(dlg)
}
}