Repository navigation
Expand file tree
/
Copy pathbasic.cpp
More file actions
791 lines (728 loc) · 25.9 KB
/
Copy pathbasic.cpp
File metadata and controls
791 lines (728 loc) · 25.9 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2012 Twan van Laarhoven and Sean Hunt |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
#include <script/functions/functions.hpp>
#include <script/functions/util.hpp>
#include <util/tagged_string.hpp>
#include <util/spec_sort.hpp>
#include <util/error.hpp>
#include <data/set.hpp>
#include <data/card.hpp>
#include <data/game.hpp>
using std::make_pair;
using std::pair;
using std::reverse;
using std::sort;
using std::unique;
// ----------------------------------------------------------------------------- : Debugging
SCRIPT_FUNCTION(trace) {
SCRIPT_PARAM_C(String, input);
#if defined(_DEBUG) && 0
wxLogDebug(_("Trace:\t") + input);
#else
queue_message(MESSAGE_INFO, _("Trace: ") + input);
#endif
SCRIPT_RETURN(input);
}
SCRIPT_FUNCTION(warning) {
SCRIPT_PARAM_C(String, input);
SCRIPT_PARAM_DEFAULT_C(bool, condition, true);
if (condition) {
queue_message(MESSAGE_WARNING, input);
}
return script_nil;
}
SCRIPT_FUNCTION(warning_if_neq) {
SCRIPT_PARAM_C(String, input);
SCRIPT_PARAM_N(ScriptValueP, SCRIPT_VAR__1, v1);
SCRIPT_PARAM_N(ScriptValueP, SCRIPT_VAR__2, v2);
if (!equal(v1,v2)) {
String s1 = _("?"), s2 = _("?");
try {
s1 = v1->toCode();
} catch (...) {}
try {
s2 = v2->toCode();
} catch (...) {}
queue_message(MESSAGE_WARNING, input + s1 + _(" != ") + s2);
}
return script_nil;
}
SCRIPT_FUNCTION(error) {
SCRIPT_PARAM_C(String, input);
SCRIPT_PARAM_DEFAULT_C(bool, condition, true);
if (condition) {
queue_message(MESSAGE_ERROR, input);
}
return script_nil;
}
// ----------------------------------------------------------------------------- : Conversion
/// Format the input variable based on a printf like style specification
String format_input(const String& format, const ScriptValue& input) {
// determine type of input
ScriptType type = input.type();
if (type == SCRIPT_DATETIME) {
return input.toDateTime().Format(format.c_str());
} else {
// determine type expected by format string
String fmt = _("%") + replace_all(format, _("%"), _(""));
if (format.find_first_of(_("DdIiOoXx")) != String::npos) {
return String::Format(fmt, input.toInt());
} else if (format.find_first_of(_("EeFfGg")) != String::npos) {
return String::Format(fmt, input.toDouble());
} else if (format.find_first_of(_("Ss")) != String::npos) {
return format_string(fmt, input.toString());
} else {
throw ScriptError(_ERROR_1_("unsupported format", format));
}
}
}
SCRIPT_FUNCTION(to_string) {
SCRIPT_PARAM_C(ScriptValueP, input);
ScriptValueP format = ctx.getVariable(SCRIPT_VAR_format);
try {
if (format && format->type() == SCRIPT_STRING) {
// format specifier. Be careful, the built in function 'format' has the same name
SCRIPT_RETURN(format_input(format->toString(), *input));
} else {
// simple conversion
SCRIPT_RETURN(input->toString());
}
} catch (const ScriptError& e) {
return intrusive(new ScriptDelayedError(e));
}
}
SCRIPT_FUNCTION(to_int) {
ScriptValueP input = ctx.getVariable(SCRIPT_VAR_input);
ScriptType t = input->type();
try {
int result;
if (t == SCRIPT_BOOL) {
result = input->toBool() ? 1 : 0;
} else if (t == SCRIPT_COLOR) {
AColor c = input->toColor();
result = (c.Red() + c.Blue() + c.Green()) / 3;
} else if (t == SCRIPT_STRING) {
long l;
String str = input->toString();
if (str.ToLong(&l)) {
result = l;
} else if (str.empty()) {
result = 0;
} else {
return delay_error(ScriptErrorConversion(str, input->typeName(), _TYPE_("integer")));
}
} else {
result = input->toInt();
}
SCRIPT_RETURN(result);
} catch (const ScriptError& e) {
return delay_error(e);
}
}
SCRIPT_FUNCTION(to_real) {
ScriptValueP input = ctx.getVariable(SCRIPT_VAR_input);
ScriptType t = input->type();
try {
double result;
if (t == SCRIPT_BOOL) {
result = input->toBool() ? 1.0 : 0.0;
} else if (t == SCRIPT_COLOR) {
AColor c = input->toColor();
result = (c.Red() + c.Blue() + c.Green()) / 3.0;
} else if (t == SCRIPT_STRING) {
String str = input->toString();
if (str.empty()) {
result = 0.0;
} else if (!str.ToDouble(&result)) {
return delay_error(ScriptErrorConversion(str, input->typeName(), _TYPE_("double")));
}
} else {
result = input->toDouble();
}
SCRIPT_RETURN(result);
} catch (const ScriptError& e) {
return delay_error(e);
}
}
SCRIPT_FUNCTION(to_number) {
ScriptValueP input = ctx.getVariable(SCRIPT_VAR_input);
ScriptType t = input->type();
try {
if (t == SCRIPT_BOOL) {
SCRIPT_RETURN(input->toBool() ? 1 : 0);
} else if (t == SCRIPT_COLOR) {
AColor c = input->toColor();
SCRIPT_RETURN( (c.Red() + c.Blue() + c.Green()) / 3 );
} else if (t == SCRIPT_DOUBLE || t == SCRIPT_INT) {
return input;
} else if (t == SCRIPT_NIL) {
SCRIPT_RETURN(0);
} else {
String str = input->toString();
long l; double d;
if (str.ToLong(&l)) {
SCRIPT_RETURN((int)l);
} else if (str.ToDouble(&d)) {
SCRIPT_RETURN((double)d);
} else if (str.empty()) {
SCRIPT_RETURN(0);
} else {
return delay_error(ScriptErrorConversion(str, input->typeName(), _TYPE_("double")));
}
}
} catch (const ScriptError& e) {
return delay_error(e);
}
}
SCRIPT_FUNCTION(to_boolean) {
ScriptValueP input = ctx.getVariable(SCRIPT_VAR_input);
try {
ScriptType t = input->type();
bool result;
if (t == SCRIPT_INT) {
result = input->toInt() != 0;
} else {
result = input->toBool();
}
SCRIPT_RETURN(result);
} catch (const ScriptError& e) {
return delay_error(e);
}
}
SCRIPT_FUNCTION(to_color) {
try {
SCRIPT_PARAM_C(AColor, input);
SCRIPT_RETURN(input);
} catch (const ScriptError& e) {
return delay_error(e);
}
}
SCRIPT_FUNCTION(to_date) {
try {
SCRIPT_PARAM_C(wxDateTime, input);
SCRIPT_RETURN(input);
} catch (const ScriptError& e) {
return delay_error(e);
}
}
SCRIPT_FUNCTION(to_code) {
SCRIPT_PARAM_C(ScriptValueP, input);
SCRIPT_RETURN(input->toCode());
}
// ----------------------------------------------------------------------------- : Math
SCRIPT_FUNCTION(abs) {
ScriptValueP input = ctx.getVariable(SCRIPT_VAR_input);
ScriptType t = input->type();
if (t == SCRIPT_DOUBLE) {
SCRIPT_RETURN(fabs(input->toDouble()));
} else {
SCRIPT_RETURN(abs(input->toInt()));
}
}
SCRIPT_FUNCTION(random_real) {
SCRIPT_PARAM_DEFAULT_C(double, begin, 0.0);
SCRIPT_PARAM_DEFAULT_C(double, end, 1.0);
SCRIPT_RETURN( (double)rand() / RAND_MAX * (end - begin) + begin );
}
SCRIPT_FUNCTION(random_int) {
SCRIPT_PARAM_DEFAULT_C(int, begin, 0);
SCRIPT_PARAM_C( int, end);
SCRIPT_RETURN( rand() % (end - begin) + begin );
}
SCRIPT_FUNCTION(random_boolean) {
SCRIPT_PARAM_DEFAULT_C(double, input, 0.5);
SCRIPT_RETURN( rand() < RAND_MAX * input );
}
SCRIPT_FUNCTION(sin) {
SCRIPT_PARAM_C(double, input);
SCRIPT_RETURN(sin(input));
}
SCRIPT_FUNCTION(cos) {
SCRIPT_PARAM_C(double, input);
SCRIPT_RETURN(cos(input));
}
SCRIPT_FUNCTION(tan) {
SCRIPT_PARAM_C(double, input);
SCRIPT_RETURN(tan(input));
}
SCRIPT_FUNCTION(sin_deg) {
SCRIPT_PARAM_C(double, input);
SCRIPT_RETURN(sin(deg_to_rad(input)));
}
SCRIPT_FUNCTION(cos_deg) {
SCRIPT_PARAM_C(double, input);
SCRIPT_RETURN(cos(deg_to_rad(input)));
}
SCRIPT_FUNCTION(tan_deg) {
SCRIPT_PARAM_C(double, input);
SCRIPT_RETURN(tan(deg_to_rad(input)));
}
SCRIPT_FUNCTION(exp) {
SCRIPT_PARAM_C(double, input);
SCRIPT_RETURN(exp(input));
}
SCRIPT_FUNCTION(log) {
SCRIPT_PARAM_C(double, input);
SCRIPT_RETURN(log(input));
}
SCRIPT_FUNCTION(log10) {
SCRIPT_PARAM_C(double, input);
SCRIPT_RETURN(log(input) / log(10.0));
}
SCRIPT_FUNCTION(sqrt) {
SCRIPT_PARAM_C(double, input);
SCRIPT_RETURN(sqrt(input));
}
SCRIPT_FUNCTION(pow) {
SCRIPT_PARAM_C(double, input);
SCRIPT_PARAM(double, exponent);
SCRIPT_RETURN(pow(input,exponent));
}
// ----------------------------------------------------------------------------- : String stuff
// convert a string to upper case
SCRIPT_FUNCTION(to_upper_case) {
SCRIPT_PARAM_C(String, input);
SCRIPT_RETURN(input.Upper());
}
// convert a string to lower case
SCRIPT_FUNCTION(to_lower_case) {
SCRIPT_PARAM_C(String, input);
SCRIPT_RETURN(input.Lower());
}
// convert a string to title case
SCRIPT_FUNCTION(to_title_case) {
SCRIPT_PARAM_C(String, input);
SCRIPT_RETURN(capitalize(input.Lower()));
}
// convert a string to sentence case
SCRIPT_FUNCTION(to_sentence_case) {
SCRIPT_PARAM_C(String, input);
SCRIPT_RETURN(capitalize_sentence(input.Lower()));
}
// reverse a string
SCRIPT_FUNCTION(reverse_text) {
SCRIPT_PARAM_C(String, input);
reverse(input.begin(), input.end());
SCRIPT_RETURN(input);
}
// remove leading and trailing whitespace from a string
SCRIPT_FUNCTION(trim) {
SCRIPT_PARAM_C(String, input);
SCRIPT_RETURN(trim(input));
}
// extract a substring
SCRIPT_FUNCTION(substring) {
SCRIPT_PARAM_C(String, input);
SCRIPT_PARAM_DEFAULT_C(int, begin, 0);
SCRIPT_PARAM_DEFAULT_C(int, end, INT_MAX);
if (begin < 0) begin = 0;
if (end < 0) end = 0;
if (begin >= end || (size_t)begin >= input.size()) {
SCRIPT_RETURN(wxEmptyString);
} else if ((size_t)end >= input.size()) {
SCRIPT_RETURN(input.substr(begin));
} else {
SCRIPT_RETURN(input.substr(begin, end - begin));
}
}
// does a string contain a substring?
SCRIPT_FUNCTION(contains) {
SCRIPT_PARAM_C(String, input);
SCRIPT_PARAM_C(String, match);
SCRIPT_RETURN(input.find(match) != String::npos);
}
SCRIPT_FUNCTION(format) {
SCRIPT_PARAM_C(String, format);
SCRIPT_PARAM_C(ScriptValueP, input);
SCRIPT_RETURN(format_input(format,*input));
}
SCRIPT_FUNCTION(curly_quotes) {
SCRIPT_PARAM_C(String, input);
SCRIPT_RETURN(curly_quotes(input,true));
}
// regex escape a string
SCRIPT_FUNCTION(regex_escape) {
SCRIPT_PARAM_C(String, input);
SCRIPT_RETURN(regex_escape(input));
}
// sort/filter characters
SCRIPT_FUNCTION(sort_text) {
SCRIPT_PARAM_C(String, input);
SCRIPT_OPTIONAL_PARAM_C(String, order) {
SCRIPT_RETURN(spec_sort(order, input));
} else {
sort(input.begin(), input.end());
SCRIPT_RETURN(input);
}
}
// ----------------------------------------------------------------------------- : Tagged string
/// Replace the contents of a specific tag with the value of a script function
String replace_tag_contents(String input, const String& tag, const ScriptValueP& contents, Context& ctx) {
assert_tagged(input, false);
String ret;
size_t start = 0, pos = input.find(tag);
while (pos != String::npos) {
// find end of tag and contents
size_t after = skip_tag(input, pos);
size_t end = match_close_tag(input, pos);
size_t after_end = skip_tag(input, end);
if (end == String::npos) break; // missing close tag
// prepare for call
String old_contents = input.substr(after, end - after);
ctx.setVariable(SCRIPT_VAR_input, to_script(old_contents));
// replace
ret.append(input, start, after-start); // before and including tag
ret += contents->eval(ctx)->toString();// new contents (call)
ret.append(input, end, after_end-end); // close tag
// next
start = after_end;
pos = input.find(tag, start);
}
ret.append(input, start, pos-start);
assert_tagged(ret, false);
return ret;
}
// Replace the contents of a specific tag
SCRIPT_FUNCTION(replace_tag_contents) {
SCRIPT_PARAM_C(String, input);
SCRIPT_PARAM_C(String, tag);
SCRIPT_PARAM_C(ScriptValueP, contents);
SCRIPT_RETURN(replace_tag_contents(input, tag, contents, ctx));
}
SCRIPT_FUNCTION(remove_tag) {
SCRIPT_PARAM_C(String, input);
SCRIPT_PARAM_C(String, tag);
assert_tagged(input, false);
SCRIPT_RETURN(remove_tag(input, tag));
}
SCRIPT_FUNCTION(remove_tags) {
SCRIPT_PARAM_C(String, input);
assert_tagged(input, false);
SCRIPT_RETURN(untag_no_escape(input));
}
// ----------------------------------------------------------------------------- : Collection stuff
/// position of some element in a vector
/** 0 based index, -1 if not found */
int position_in_vector(const ScriptValueP& of, const ScriptValueP& in, const ScriptValueP& order_by, const ScriptValueP& filter) {
ScriptType of_t = of->type(), in_t = in->type();
if (of_t == SCRIPT_STRING || in_t == SCRIPT_STRING) {
// string finding
return (int)of->toString().find(in->toString()); // (int)npos == -1
} else if (order_by || filter) {
ScriptObject<Set*>* s = dynamic_cast<ScriptObject<Set*>* >(in.get());
ScriptObject<CardP>* c = dynamic_cast<ScriptObject<CardP>*>(of.get());
if (s && c) {
return s->getValue()->positionOfCard(c->getValue(), order_by, filter);
} else {
throw ScriptError(_("position: using 'order_by' or 'filter' is only supported for finding cards in the set"));
}
} else {
// unordered position
ScriptValueP it = in->makeIterator();
int i = 0;
while (ScriptValueP v = it->next()) {
if (equal(of, v)) return i;
i++;
}
}
return -1; // TODO?
}
inline bool smart_less_first(const pair<String,ScriptValueP>& a, const pair<String,ScriptValueP>& b) {
return smart_less(a.first, b.first);
}
inline bool smart_equal_first(const pair<String,ScriptValueP>& a, const pair<String,ScriptValueP>& b) {
return smart_equal(a.first, b.first);
}
// sort a script list
ScriptValueP sort_script(Context& ctx, const ScriptValueP& list, ScriptValue& order_by, bool remove_duplicates) {
ScriptType list_t = list->type();
if (list_t == SCRIPT_STRING) {
// sort a string
String s = list->toString();
sort(s.begin(), s.end());
if (remove_duplicates) {
s.erase( unique(s.begin(), s.end()), s.end() );
}
SCRIPT_RETURN(s);
} else {
// are we sorting a set?
ScriptObject<Set*>* set = dynamic_cast<ScriptObject<Set*>*>(list.get());
// sort a collection
vector<pair<String,ScriptValueP> > values;
ScriptValueP it = list->makeIterator();
while (ScriptValueP v = it->next()) {
ctx.setVariable(set ? _("card") : _("input"), v);
values.push_back(make_pair(order_by.eval(ctx)->toString(), v));
}
sort(values.begin(), values.end(), smart_less_first);
// unique
if (remove_duplicates) {
values.erase( unique(values.begin(), values.end(), smart_equal_first), values.end() );
}
// return collection
ScriptCustomCollectionP ret(new ScriptCustomCollection());
for(auto& v : values) {
ret->value.push_back(v.second);
}
return ret;
}
}
// finding positions, also of substrings
SCRIPT_FUNCTION_WITH_DEP(position_of) {
ScriptValueP of = ctx.getVariable(_("of"));
ScriptValueP in = ctx.getVariable(_("in"));
ScriptValueP order_by = ctx.getVariableOpt(_("order_by"));
ScriptValueP filter = ctx.getVariableOpt(_("filter"));
if (filter == script_nil) filter = ScriptValueP();
SCRIPT_RETURN(position_in_vector(of, in, order_by, filter));
}
SCRIPT_FUNCTION_DEPENDENCIES(position_of) {
ScriptValueP of = ctx.getVariable(_("of"));
ScriptValueP in = ctx.getVariable(_("in"));
ScriptValueP order_by = ctx.getVariableOpt(_("order_by"));
ScriptValueP filter = ctx.getVariableOpt(_("filter"));
ScriptObject<Set*>* s = dynamic_cast<ScriptObject<Set*>* >(in.get());
ScriptObject<CardP>* c = dynamic_cast<ScriptObject<CardP>*>(of.get());
if (s && c) {
// dependency on cards
mark_dependency_member(*s->getValue(), _("cards"), dep);
if (order_by) {
// dependency on order_by function
order_by->dependencies(ctx, dep.makeCardIndependend());
}
if (filter && filter != script_nil) {
// dependency on filter function
filter->dependencies(ctx, dep.makeCardIndependend());
}
}
return dependency_dummy;
};
// finding sizes
int script_length_of(Context& ctx, const ScriptValueP& collection) {
if (ScriptObject<Set*>* setobj = dynamic_cast<ScriptObject<Set*>*>(collection.get())) {
Set* set = setobj->getValue();
SCRIPT_OPTIONAL_PARAM_C_(ScriptValueP, filter);
return set->numberOfCards(filter);
} else {
return collection->itemCount();
}
}
SCRIPT_FUNCTION(length) {
SCRIPT_PARAM_C(ScriptValueP, input);
SCRIPT_RETURN(script_length_of(ctx, input));
}
SCRIPT_FUNCTION(number_of_items) {
SCRIPT_PARAM_C(ScriptValueP, in);
SCRIPT_RETURN(script_length_of(ctx, in));
}
// filtering items from a list
SCRIPT_FUNCTION(filter_list) {
SCRIPT_PARAM_C(ScriptValueP, input);
SCRIPT_PARAM_C(ScriptValueP, filter);
// filter a collection
ScriptCustomCollectionP ret(new ScriptCustomCollection());
ScriptValueP it = input->makeIterator();
while (ScriptValueP v = it->next()) {
ctx.setVariable(SCRIPT_VAR_input, v);
if (filter->eval(ctx)->toBool()) {
ret->value.push_back(v);
}
}
// TODO : somehow preserve keys
return ret;
}
SCRIPT_FUNCTION(sort_list) {
SCRIPT_PARAM_C(ScriptValueP, input);
SCRIPT_PARAM_DEFAULT(ScriptValueP, order_by, script_nil);
SCRIPT_PARAM_DEFAULT(bool, remove_duplicates, false);
return sort_script(ctx, input, *order_by, remove_duplicates);
}
SCRIPT_FUNCTION(random_shuffle) {
SCRIPT_PARAM_C(ScriptValueP, input);
// convert to CustomCollection
ScriptCustomCollectionP ret(new ScriptCustomCollection());
ScriptValueP it = input->makeIterator();
while (ScriptValueP v = it->next()) {
ret->value.push_back(v);
}
// shuffle
random_shuffle(ret->value.begin(), ret->value.end());
return ret;
}
SCRIPT_FUNCTION(random_select) {
SCRIPT_PARAM_C(ScriptValueP, input);
// pick a single one
int itemCount = input->itemCount();
if (itemCount == 0) {
throw ScriptError(_("Can not select a random item from an empty collection"));
}
return input->getIndex( rand() % itemCount );
}
SCRIPT_FUNCTION(random_select_many) {
SCRIPT_PARAM_C(ScriptValueP, input);
SCRIPT_PARAM(int, count) ;
SCRIPT_OPTIONAL_PARAM_C_(ScriptValueP, replace);
bool with_replace = replace && replace->type() != SCRIPT_FUNCTION && replace->toBool();
// pick many
ScriptCustomCollectionP ret(new ScriptCustomCollection);
int itemCount = input->itemCount();
if (with_replace) {
if (itemCount == 0) {
throw ScriptError(String::Format(_("Can not select %d items from an empty collection"), count));
}
for (int i = 0 ; i < count ; ++i) {
ret->value.push_back( input->getIndex( rand() % itemCount ) );
}
} else {
if (count > itemCount) {
throw ScriptError(String::Format(_("Can not select %d items from a collection conaining only %d items"), count, input->itemCount()));
}
// transfer all to ret and shuffle
ScriptValueP it = input->makeIterator();
while (ScriptValueP v = it->next()) {
ret->value.push_back(v);
}
random_shuffle(ret->value.begin(), ret->value.end());
// keep only the first 'count'
ret->value.resize(count);
}
return ret;
}
// ----------------------------------------------------------------------------- : Keywords
SCRIPT_FUNCTION_WITH_DEP(expand_keywords) {
SCRIPT_PARAM_C(String, input);
SCRIPT_PARAM_C(Set*, set);
SCRIPT_OPTIONAL_PARAM_N_(ScriptValueP, _("condition"), match_condition);
SCRIPT_OPTIONAL_PARAM_(ScriptValueP, default_expand);
SCRIPT_PARAM(ScriptValueP, combine);
KeywordDatabase& db = set->keyword_db;
if (db.empty()) {
db.prepare_parameters(set->game->keyword_parameter_types, set->keywords);
db.prepare_parameters(set->game->keyword_parameter_types, set->game->keywords);
db.add(set->keywords);
db.add(set->game->keywords);
}
SCRIPT_OPTIONAL_PARAM_C_(CardP, card);
WITH_DYNAMIC_ARG(keyword_usage_statistics, card ? &card->keyword_usage : nullptr);
try {
SCRIPT_RETURN(db.expand(input, match_condition, default_expand, combine, ctx));
} catch (const Error& e) {
throw ScriptError(_ERROR_2_("in function", e.what().c_str(), _("expand_keywords")));
}
}
SCRIPT_FUNCTION_DEPENDENCIES(expand_keywords) {
SCRIPT_PARAM_C(Set*, set);
SCRIPT_OPTIONAL_PARAM_N_(ScriptValueP, _("condition"), match_condition);
SCRIPT_OPTIONAL_PARAM_(ScriptValueP, default_expand);
SCRIPT_PARAM(ScriptValueP, combine);
if (match_condition) match_condition->dependencies(ctx,dep);
default_expand ->dependencies(ctx,dep);
combine ->dependencies(ctx,dep);
set->game->dependent_scripts_keywords.add(dep); // this depends on the set's keywords
return ctx.getVariable(SCRIPT_VAR_input);
}
SCRIPT_FUNCTION(keyword_usage) {
SCRIPT_PARAM_C(CardP, card);
SCRIPT_OPTIONAL_PARAM_(bool, unique);
// make a list "kw1, kw2, kw3" of keywords used on card
String ret;
for (KeywordUsageStatistics::const_iterator it = card->keyword_usage.begin() ; it != card->keyword_usage.end() ; ++it) {
bool keep = true;
if (unique) {
// prevent duplicates
for (KeywordUsageStatistics::const_iterator it2 = card->keyword_usage.begin() ; it != it2 ; ++it2) {
if (it->second == it2->second) {
keep = false;
break;
}
}
}
if (keep) {
if (!ret.empty()) ret += _(", ");
ret += it->second->keyword;
}
}
SCRIPT_RETURN(ret);
}
// ----------------------------------------------------------------------------- : Rule form
/// Turn a script function into a rule, a.k.a. a delayed closure
SCRIPT_FUNCTION(rule) {
SCRIPT_PARAM(ScriptValueP, input);
return intrusive(new ScriptRule(input));
}
// ----------------------------------------------------------------------------- : Init
void init_script_basic_functions(Context& ctx) {
// debugging
ctx.setVariable(_("trace"), script_trace);
ctx.setVariable(_("warning"), script_warning);
ctx.setVariable(_("error"), script_error);
// conversion
ctx.setVariable(_("to_string"), script_to_string);
ctx.setVariable(_("to_int"), script_to_int);
ctx.setVariable(_("to_real"), script_to_real);
ctx.setVariable(_("to_number"), script_to_number);
ctx.setVariable(_("to_boolean"), script_to_boolean);
ctx.setVariable(_("to_color"), script_to_color);
ctx.setVariable(_("to_date"), script_to_date);
ctx.setVariable(_("to_code"), script_to_code);
// math
ctx.setVariable(_("random_real"), script_random_real);
ctx.setVariable(_("random_int"), script_random_int);
ctx.setVariable(_("random_boolean"), script_random_boolean);
ctx.setVariable(_("abs"), script_abs);
ctx.setVariable(_("sin"), script_sin);
ctx.setVariable(_("cos"), script_cos);
ctx.setVariable(_("tan"), script_tan);
ctx.setVariable(_("sin_deg"), script_sin_deg);
ctx.setVariable(_("cos_deg"), script_cos_deg);
ctx.setVariable(_("tan_deg"), script_tan_deg);
ctx.setVariable(_("exp"), script_exp);
ctx.setVariable(_("log"), script_log);
ctx.setVariable(_("log10"), script_log10);
ctx.setVariable(_("sqrt"), script_sqrt);
ctx.setVariable(_("pow"), script_pow);
// string
ctx.setVariable(_("to_upper_case"), script_to_upper_case);
ctx.setVariable(_("to_lower_case"), script_to_lower_case);
ctx.setVariable(_("to_title_case"), script_to_title_case);
ctx.setVariable(_("to_sentence_case"), script_to_sentence_case);
ctx.setVariable(_("to_upper"), script_to_upper_case); // old name
ctx.setVariable(_("to_lower"), script_to_lower_case); // old name
ctx.setVariable(_("to_title"), script_to_title_case); // old name
ctx.setVariable(_("reverse"), script_reverse_text); // old name
ctx.setVariable(_("reverse_text"), script_reverse_text);
ctx.setVariable(_("trim"), script_trim);
ctx.setVariable(_("substring"), script_substring);
ctx.setVariable(_("contains"), script_contains);
ctx.setVariable(_("format"), script_format);
ctx.setVariable(_("format_rule"), intrusive(new ScriptRule(script_format))); // compatability
ctx.setVariable(_("curly_quotes"), script_curly_quotes);
ctx.setVariable(_("regex_escape"), script_regex_escape);
ctx.setVariable(_("sort_text"), script_sort_text);
ctx.setVariable(_("sort_rule"), intrusive(new ScriptRule(script_sort_text))); // compatability
// tagged string
ctx.setVariable(_("tag_contents"), script_replace_tag_contents); // old name
ctx.setVariable(_("replace_tag_contents"), script_replace_tag_contents);
ctx.setVariable(_("remove_tag"), script_remove_tag);
ctx.setVariable(_("remove_tags"), script_remove_tags);
ctx.setVariable(_("tag_contents_rule"), intrusive(new ScriptRule(script_replace_tag_contents))); // compatability
ctx.setVariable(_("tag_remove_rule"), intrusive(new ScriptRule(script_remove_tag))); // compatability
// collection
ctx.setVariable(_("position"), script_position_of);
ctx.setVariable(_("length"), script_length);
ctx.setVariable(_("number_of_items"), script_number_of_items); // deprecated
ctx.setVariable(_("filter_list"), script_filter_list);
ctx.setVariable(_("sort_list"), script_sort_list);
ctx.setVariable(_("random_shuffle"), script_random_shuffle);
ctx.setVariable(_("random_select"), script_random_select);
ctx.setVariable(_("random_select_many"), script_random_select_many);
// keyword
ctx.setVariable(_("expand_keywords"), script_expand_keywords);
ctx.setVariable(_("expand_keywords_rule"), intrusive(new ScriptRule(script_expand_keywords))); // compatability
ctx.setVariable(_("keyword_usage"), script_keyword_usage);
}