File include/dialogue.h changed (mode: 100755) (index d57bf57..c927e01) |
12 |
12 |
#include <cmath> |
#include <cmath> |
13 |
13 |
|
|
14 |
14 |
#include <functional> |
#include <functional> |
|
15 |
|
#include <map> |
15 |
16 |
#include <string> |
#include <string> |
16 |
17 |
#include <vector> |
#include <vector> |
17 |
18 |
|
|
|
... |
... |
class Talk { |
55 |
56 |
Screen screen; |
Screen screen; |
56 |
57 |
std::vector<Screen> screens; |
std::vector<Screen> screens; |
57 |
58 |
std::vector<Variable> variables; |
std::vector<Variable> variables; |
|
59 |
|
std::map<std::string,std::string> strings; |
58 |
60 |
std::function<double(std::string)> callback; |
std::function<double(std::string)> callback; |
59 |
61 |
void go( std::string id ); |
void go( std::string id ); |
60 |
62 |
void append( std::string filePath ); |
void append( std::string filePath ); |
|
... |
... |
bool Talk::hasVariable( std::string key ){ |
179 |
181 |
} |
} |
180 |
182 |
|
|
181 |
183 |
double Talk::getVariable( std::string key ){ |
double Talk::getVariable( std::string key ){ |
|
184 |
|
// Apply string replacement to the key. |
|
185 |
|
transformString( key ); |
182 |
186 |
if( key == "RAND" ){ |
if( key == "RAND" ){ |
183 |
187 |
// Returns a (pseudo)random number. |
// Returns a (pseudo)random number. |
184 |
188 |
return std::rand(); |
return std::rand(); |
185 |
189 |
}else if( key.length() >= 8 && key.substr( 0, 8 ) == "CALLBACK" ){ |
}else if( key.length() >= 8 && key.substr( 0, 8 ) == "CALLBACK" ){ |
186 |
190 |
// Returns the result of the callback function. |
// Returns the result of the callback function. |
187 |
191 |
std::string arg = key.length() >= 10 ? key.substr( 9 ) : ""; |
std::string arg = key.length() >= 10 ? key.substr( 9 ) : ""; |
188 |
|
transformString( arg ); |
|
189 |
192 |
return callback( arg ); |
return callback( arg ); |
190 |
193 |
}else if( key.length() >= 5 && key.substr( 0, 5 ) == "GOSUB" ){ |
}else if( key.length() >= 5 && key.substr( 0, 5 ) == "GOSUB" ){ |
191 |
194 |
// Executes the specified dialogue, then returns 0. |
// Executes the specified dialogue, then returns 0. |
192 |
195 |
std::string arg = key.length() >= 7 ? key.substr( 6 ) : ""; |
std::string arg = key.length() >= 7 ? key.substr( 6 ) : ""; |
193 |
|
transformString( arg ); |
|
194 |
196 |
go( arg ); |
go( arg ); |
195 |
197 |
return 0.0; |
return 0.0; |
196 |
198 |
}else{ |
}else{ |
|
... |
... |
Talk::Talk( std::string filePath ){ |
305 |
307 |
|
|
306 |
308 |
Operation Talk::getOperation( std::string key, double valueNumber, std::string valueKey ){ |
Operation Talk::getOperation( std::string key, double valueNumber, std::string valueKey ){ |
307 |
309 |
char op = ':'; |
char op = ':'; |
308 |
|
if( key.length() > 0 && key.find_last_of( "=!<>?%*/+-_" ) == key.length() - 1 ){ |
|
|
310 |
|
if( key.length() > 0 && key.find_last_of( "$=!<>?%*/+-_" ) == key.length() - 1 ){ |
309 |
311 |
op = key.back(); |
op = key.back(); |
310 |
312 |
key.resize( key.length() - 1 ); |
key.resize( key.length() - 1 ); |
311 |
313 |
} |
} |
|
... |
... |
Operation Talk::getOperation( std::string key, double valueNumber, std::string v |
318 |
320 |
} |
} |
319 |
321 |
|
|
320 |
322 |
void Talk::operate( Operation o ){ |
void Talk::operate( Operation o ){ |
|
323 |
|
// String assignment. |
|
324 |
|
if( o.op == '$' ){ |
|
325 |
|
transformString( o.valueKey ); |
|
326 |
|
strings[o.key] = o.valueKey; |
|
327 |
|
return; |
|
328 |
|
} |
|
329 |
|
// Numerical operations. |
321 |
330 |
double in = o.valueNumber, out = getVariable( o.key ); |
double in = o.valueNumber, out = getVariable( o.key ); |
322 |
331 |
if( o.valueKey.length() > 0 ){ |
if( o.valueKey.length() > 0 ){ |
323 |
332 |
in = getVariable( o.valueKey ); |
in = getVariable( o.valueKey ); |
|
... |
... |
void Talk::transformString( std::string &str ){ |
359 |
368 |
case '`': |
case '`': |
360 |
369 |
if( replacing ){ |
if( replacing ){ |
361 |
370 |
if( i - start > 1 ){ |
if( i - start > 1 ){ |
362 |
|
outStr += stringifyNumber( getVariable( str.substr( start + 1, i - start - 1 ) ) ); |
|
|
371 |
|
// Add the variable value to the string. |
|
372 |
|
std::string key = str.substr( start + 1, i - start - 1 ); |
|
373 |
|
if( key.back() == '$' ){ |
|
374 |
|
key.resize( key.length() - 1 ); |
|
375 |
|
auto it = strings.find( key ); |
|
376 |
|
if( it != strings.end() ) outStr += it->second; |
|
377 |
|
}else{ |
|
378 |
|
outStr += stringifyNumber( getVariable( key ) ); |
|
379 |
|
} |
363 |
380 |
} |
} |
364 |
381 |
start = i + 1; |
start = i + 1; |
365 |
382 |
}else{ |
}else{ |