-
Notifications
You must be signed in to change notification settings - Fork 6
/
Toolkit.cs
1092 lines (1015 loc) · 28.8 KB
/
Toolkit.cs
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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Noxico
{
public static class Toolkit
{
public static TextInfo ti = CultureInfo.InvariantCulture.TextInfo;
private static List<Tuple<Regex, int>> hyphenationRules;
/// <summary>
/// Returns the amount of change between two strings according to the Levenshtein method.
/// </summary>
public static int GetLevenshteinDistance(this string s, string t)
{
var n = s.Length;
var m = t.Length;
var d = new int[n + 1, m + 1];
var cost = 0;
if (n == 0)
return m;
if (m == 0)
return n;
for (int i = 0; i <= n; d[i, 0] = i++) ;
for (int j = 0; j <= m; d[0, j] = j++) ;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cost = (t[j - 1] == s[i - 1]) ? 0 : 1;
d[i, j] = System.Math.Min(System.Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1),
d[i - 1, j - 1] + cost);
}
}
return d[n, m];
}
/// <summary>
/// Returns the amount of change between two strings according to the Hamming method.
/// </summary>
public static int GetHammingDistance(this string s, string t)
{
if (s.Length != t.Length)
throw new ArgumentException("Subject strings in a Hamming distance calculation should be of equal length.");
return s.Zip(t, (c1, c2) => c1 == c2 ? 0 : 1).Sum();
}
/// <summary>
/// Picks a single item from an array, at random.
/// </summary>
public static T PickOne<T>(this T[] options)
{
return options[Random.Next(options.Length)];
}
/// <summary>
/// Picks a single item from a List, at random.
/// </summary>
public static T PickOne<T>(this List<T> options)
{
return options[Random.Next(options.Count)];
}
/// <summary>
/// Picks a single item from a token list, weighted.
/// </summary>
public static Token PickWeighted(this List<Token> tokens)
{
if (tokens.Count == 0) return null;
if (tokens.Count == 1) return tokens[0];
var useWeight = false;
var weights = new float[tokens.Count];
for (var i = 0; i < tokens.Count; i++)
{
weights[i] = -1;
if (tokens[i].HasToken("weight"))
{
weights[i] = tokens[i].GetToken("weight").Value;
useWeight = true;
}
}
if (!useWeight)
return tokens.PickOne();
var numWithWeigths = weights.Count(x => x > -1);
var totalWithWeights = weights.Where(x => x > -1).Sum();
var defaultWeight = (1.0f - totalWithWeights) / numWithWeigths;
for (var i = 0; i < weights.Length; i++)
if (weights[i] < 0) //== -1)
weights[i] = defaultWeight;
var w = weights.Sum();
var r = (float)(Random.NextDouble() * w);
for (var i = 0; i < weights.Length; i++)
{
if (r < weights[i])
return tokens[i];
r -= weights[i];
}
//fuck it, go unweighted.
return tokens.PickOne();
}
/// <summary>
/// Returns the given number as a word, from "one" up to "twelve". 13 and higher are returned as-is.
/// </summary>
public static string Count(this float num)
{
var words = i18n.GetArray("counts");
var i = (int)Math.Floor(num);
if (i < words.Length)
return words[i];
return i.ToString();
}
/// <summary>
/// Returns the given number as a word, from "first" up to "twelfth". 13 and higher are passed to Ordinal.
/// </summary>
public static string CountOrdinal(this float num)
{
var words = i18n.GetArray("countsordinal");
var i = (int)Math.Floor(num);
if (i < words.Length)
return words[i];
return i18n.Ordinal(num);
}
/// <summary>
/// Returns the given number as a word, from "first" up to "twelfth". 13 and higher are passed to Ordinal.
/// </summary>
public static string CountOrdinal(this int num)
{
var words = i18n.GetArray("countsordinal");
if (num < words.Length)
return words[num];
return i18n.Ordinal(num);
}
public static bool IsBlank(this string text)
{
return string.IsNullOrWhiteSpace(text);
}
public static string IsBlank(this string text, string ifItIs, string ifNot)
{
return string.IsNullOrWhiteSpace(text) ? ifItIs : ifNot;
}
public static string OrEmpty(this string text)
{
return (text == null) ? string.Empty : text;
}
public static string Join<T>(this T[] values)
{
return string.Join(", ", values);
}
public static string Join<T>(this IEnumerable<T> values)
{
return string.Join(", ", values);
}
public static bool StartsWith(this string text, char ch)
{
return (text.Length > 0 && text[0] == ch);
}
public static bool EndsWith(this string text, char ch)
{
return (text.Length > 0 && text[text.Length - 1] == ch);
}
public static bool StartsWithVowel(this string text)
{
if (string.IsNullOrWhiteSpace(text))
return false;
var vowels = "AEIOUaeiou".ToCharArray();
if (vowels.Contains(text[0]))
return true;
return false;
}
public static string Titlecase(this string text)
{
return ti.ToTitleCase(text.ToLowerInvariant());
}
public static string Lowercase(this string text)
{
var ret = new StringBuilder();
foreach (var ch in text)
{
if (ch >= 0x41 && ch <= 0xA5)
ret.Append(ch.ToString().ToLowerInvariant());
else
ret.Append(ch);
}
return ret.ToString();
}
public static string Disemvowel(this string text)
{
var vowels = "AEIOUaeiou".ToCharArray();
var ret = new StringBuilder();
foreach (var c in text)
{
if (vowels.Contains(c))
continue;
ret.Append(c);
}
return ret.ToString().Trim();
}
public static string Wordwrap(this string text, int length = 80)
{
var words = new List<Word>();
var lines = new List<string>();
text = text.Normalize();
#region Hyphenator
if (hyphenationRules == null)
{
var rulesRoot = Mix.GetTokenTree("i18n.tml").First(t => t.Name == "hyphenation");
hyphenationRules = new List<Tuple<Regex, int>>();
foreach (var rule in rulesRoot.Tokens)
{
var newTuple = Tuple.Create(new Regex(rule.GetToken("pattern").Text), (int)rule.GetToken("cutoff").Value);
hyphenationRules.Add(newTuple);
}
}
var newText = new StringBuilder();
foreach (var inWord in text.Split(' '))
{
if (inWord.Contains('\u00AD') || inWord.Length < 6)
{
newText.Append(inWord);
newText.Append(' ');
continue;
}
var word = inWord;
foreach (var rule in hyphenationRules)
{
if (rule.Item2 == -1 && rule.Item1.IsMatch(word))
break;
while (rule.Item1.IsMatch(word))
{
var match = rule.Item1.Match(word);
word = word.Substring(0, match.Index + rule.Item2) + '\u00AD' + word.Substring(match.Index + rule.Item2);
}
}
newText.Append(word);
newText.Append(' ');
}
text = newText.ToString();
text = text.Replace("\u00AD\u00AD", "\u00AD");
text = Regex.Replace(text, "<(?:[\\w^]+)(\u00AD)(?:[\\w^]+)>", (m => m.Captures[0].Value.Replace("\u00AD", string.Empty)));
#endregion
var currentWord = new StringBuilder();
var breakIt = false;
var spaceAfter = false;
var mandatory = false;
var softHyphen = false;
var color = Color.Transparent;
for (var i = 0; i < text.Length; i++)
{
var ch = text[i];
var nextCh = (i < text.Length - 1) ? text[i + 1] : '\0';
if (ch == '<' && nextCh == 'c')
{
if (text[i + 2] == '>')
{
color = Color.Transparent;
i += 2;
continue;
}
var colorToken = new StringBuilder();
for (var j = i + 2; j < text.Length; j++)
{
if (text[j] == '>')
{
color = Color.FromName(colorToken.ToString());
i = j;
break;
}
colorToken.Append(text[j]);
}
continue;
}
if ((ch == '\r' && nextCh != '\n') || ch == '\n')
{
breakIt = true;
mandatory = true;
}
else if (char.IsWhiteSpace(ch) && ch != '\u00A0')
{
breakIt = true;
spaceAfter = true;
}
else if (ch == '\u00AD')
{
breakIt = true;
softHyphen = true;
}
else if (char.IsPunctuation(ch) && !(ch == '(' || ch == ')' || ch == '\''))
{
currentWord.Append(ch);
breakIt = true;
}
else
currentWord.Append(ch);
if (breakIt)
{
var newWord = new Word()
{
Content = currentWord.ToString().Trim(),
SpaceAfter = spaceAfter,
MandatoryBreak = mandatory,
SoftHyphen = softHyphen,
Color = color,
};
breakIt = false;
spaceAfter = false;
mandatory = false;
softHyphen = false;
words.Add(newWord);
currentWord.Clear();
}
}
if (currentWord.ToString() != string.Empty)
{
var newWord = new Word()
{
Content = currentWord.ToString().Trim(),
SpaceAfter = currentWord.ToString().EndsWith(' '),
MandatoryBreak = false,
SoftHyphen = softHyphen,
Color = color,
};
words.Add(newWord);
}
var line = new StringBuilder();
var spaceLeft = length;
color = Color.Transparent;
for (var i = 0; i < words.Count; i++)
{
var word = words[i];
var next = (i < words.Count - 1) ? words[i + 1] : null;
//Check for words longer than length? Should not happen with autohyphenator.
//Reinsert color change without changing line length.
if (word.Color != color)
{
color = word.Color;
line.AppendFormat("<c{0}>", color == Color.Transparent ? string.Empty : color.Name);
}
if (word.Content == "\u2029")
{
lines.Add(line.ToString().Trim());
lines.Add(string.Empty);
line.Clear();
spaceLeft = length;
continue;
}
if (word.SoftHyphen)
{
if (next != null && spaceLeft - word.Length - next.Content.TrimEnd().Length <= 0)
{
word.Content += '-';
word.SoftHyphen = false;
}
}
line.Append(word.Content);
spaceLeft -= word.Length;
if (next != null && spaceLeft - next.Content.TrimEnd().Length <= 0)
{
if (!line.ToString().Trim().IsBlank())
lines.Add(line.ToString().Trim());
line.Clear();
spaceLeft = length;
}
else
{
if (word.SpaceAfter)
{
line.Append(' ');
spaceLeft--;
}
}
if (word.MandatoryBreak)
{
lines.Add(line.ToString().Trim());
line.Clear();
spaceLeft = length;
continue;
}
}
if (!line.ToString().Trim().IsBlank())
lines.Add(line.ToString());
return string.Join("\n", lines.ToArray()) + '\n';
}
public static string SmartQuote(this string text, SpeechFilter filter = null)
{
var ret = new StringBuilder();
var open = false;
var quoted = new StringBuilder();
text = text.Replace("...", "\x137");
foreach (var ch in text)
{
if (ch == '\"')
{
//ret.Append(open ? '\x139' : '\x138');
if (!open)
{
quoted.Clear();
open = true;
ret.Append('\x138');
}
else
{
var q = quoted.ToString();
if (q.StartsWith("<nofilter>"))
ret.Append(q.Substring(10));
else if (filter == null)
ret.Append(q);
else
ret.Append(filter(q));
quoted.Clear();
open = false;
ret.Append('\x139');
}
}
else
{
if (open)
quoted.Append(ch);
else
ret.Append(ch);
}
}
return ret.ToString();
}
public static string FoldEntities(this string text)
{
var tag = new Regex(@"\<g(?<chr>\w{1,4})>");
var entity = new Regex(@"&#x(?<chr>\w{1,4});");
while (tag.IsMatch(text))
{
var match = tag.Match(text);
text = text.Replace(match.Value, ((char)int.Parse(match.Groups["chr"].Value, NumberStyles.HexNumber)).ToString());
}
while (entity.IsMatch(text))
{
var match = entity.Match(text);
text = text.Replace(match.Value, ((char)int.Parse(match.Groups["chr"].Value, NumberStyles.HexNumber)).ToString());
}
return text;
}
/// <summary>
/// Use in a ForEach loop.
/// </summary>
public static IEnumerable<Point> Line(int x0, int y0, int x1, int y1, bool connectedDiagonals = false)
{
bool steep = Math.Abs(y1 - y0) > Math.Abs(x1 - x0);
if (steep)
{
int t;
t = x0; // swap x0 and y0
x0 = y0;
y0 = t;
t = x1; // swap x1 and y1
x1 = y1;
y1 = t;
}
if (x0 > x1)
{
int t;
t = x0; // swap x0 and x1
x0 = x1;
x1 = t;
t = y0; // swap y0 and y1
y0 = y1;
y1 = t;
}
int dx = x1 - x0;
int dy = Math.Abs(y1 - y0);
int error = dx / 2;
int ystep = (y0 < y1) ? 1 : -1;
int y = y0;
int lastX = x0, lastY = y0;
for (int x = x0; x <= x1; x++)
{
if (connectedDiagonals)
yield return new Point((steep ? lastY : x), (steep ? x : lastY));
lastX = x;
lastY = y;
yield return new Point((steep ? y : x), (steep ? x : y));
error = error - dy;
if (error < 0)
{
y += ystep;
error += dx;
}
}
yield break;
}
public static void PredictLocation(int oldX, int oldY, Direction targetDirection, ref int newX, ref int newY)
{
newX = oldX;
newY = oldY;
switch (targetDirection)
{
case Direction.North:
newY--;
break;
case Direction.East:
newX++;
break;
case Direction.South:
newY++;
break;
case Direction.West:
newX--;
break;
}
}
public static Color LoadColorFromFile(BinaryReader stream)
{
var r = stream.ReadByte();
var g = stream.ReadByte();
var b = stream.ReadByte();
return Color.FromArgb(r, g, b);
}
public static void SaveToFile(this Color color, BinaryWriter stream)
{
stream.Write((byte)color.R);
stream.Write((byte)color.G);
stream.Write((byte)color.B);
}
/// <summary>
/// Converts a NoxML string to HTML. Badly.
/// </summary>
public static string ToHtml(this string text)
{
var html = new StringBuilder();
text = text.Replace("\n", "\r\n").Replace("\r\r", "\r");
var lines = text.Split('\n');
var glyph = @"\<g([0-9a-fA-F]{4})\>";
var color = @"<c(?:(?:(?<fore>\w+)(?:(?:,(?<back>\w+))?))?)>";
html.Append("<pre>");
foreach (var line in lines)
{
var s = line;
//if (s.Equals(lines[0]))
// s = "<h3>" + s.Trim() + "</h3>";
var colorClosers = 0;
while (Regex.IsMatch(s, color))
{
var match = Regex.Match(s, color);
if (match.Groups["fore"] == null)
{
s = s.Substring(0, match.Index) + "</span>" + s.Substring(match.Index + match.Length);
colorClosers--;
}
else
{
var col = Color.FromName(match.Groups["fore"].ToString());
s = s.Substring(0, match.Index) + "<span style=\"color: rgb(" + col.R + "," + col.G + "," + col.B + ");\">" + s.Substring(match.Index + match.Length);
colorClosers++;
}
}
while (Regex.IsMatch(s, glyph))
{
s = Regex.Replace(s, glyph, @"&#x$1;");
}
html.Append(s);
while (colorClosers > 0)
{
html.Append("</span>");
colorClosers--;
}
}
html.Append("</pre>");
return html.ToString();
}
public static string ToUnicode(this string text)
{
var sb = new StringBuilder(text.Length);
var table = NoxicoGame.IngameToUnicode;
foreach (var ch in text)
{
if (ch < ' ' || ch > table.Length)
sb.Append(ch);
else
sb.Append(table[ch]);
}
return sb.ToString();
}
/// <summary>
/// Darkens a color in some stupid way.
/// </summary>
public static Color Darken(this Color color, double divisor = 2)
{
if ((int)divisor == 0)
divisor = 1;
var rD = color.R / divisor;
var gD = color.G / divisor;
var bD = color.B / divisor;
var r = color.R - rD;
var g = color.G - gD;
var b = color.B - bD;
if (r < 0)
r = 0;
if (g < 0)
g = 0;
if (b < 0)
b = 0;
return Color.FromArgb((int)r, (int)g, (int)b);
}
/// <summary>
/// Darkens a color to produce a nighttime palette.
/// </summary>
public static Color Night(this Color color)
{
return Color.FromArgb(color.R / 3, color.G / 4, color.B / 2);
}
/// <summary>
/// Stolen from XNA. Linearly interpolates between two colors.
/// </summary>
public static Color Lerp(int sR, int sG, int sB, int dR, int dG, int dB, double amount)
{
if (amount < 0)
amount = 0;
else if (amount > 1)
amount = 1;
double iR = (dR - sR) / 100.0, iG = (dG - sG) / 100.0, iB = (dB - sB) / 100.0;
var a = (int)(amount * 100);
int tR = sR + (int)(iR * a);
int tG = sG + (int)(iG * a);
int tB = sB + (int)(iB * a);
return Color.FromArgb(tR, tG, tB);
}
public static Color Lerp(Color source, Color dest, double amount)
{
return Lerp(source.R, source.G, source.B, dest.R, dest.G, dest.B, amount);
}
/// <summary>
/// Darkens a color in a more sane manner.
/// </summary>
public static Color LerpDarken(this Color color, double amount)
{
return Lerp(color, Color.Black, amount);
}
public static int Distance(int fromX, int fromY, int toX, int toY)
{
var dX = Math.Abs(fromX - toX);
var dY = Math.Abs(fromY - toY);
return (dX < dY) ? dY : dX;
}
public static int Clamp(this int v, int min, int max)
{
if (v < min) return min;
if (v > max) return max;
return v;
}
public static double Clamp(this double v, double min, double max)
{
if (v < min) return min;
if (v > max) return max;
return v;
}
public static float Clamp(this float v, float min, float max)
{
if (v < min) return min;
if (v > max) return max;
return v;
}
/// <summary>
/// From Nethack. True if it's Friday the 13th.
/// </summary>
public static bool IsFriday13()
{
return (DateTime.Now.DayOfWeek == DayOfWeek.Friday && DateTime.Now.Day == 13);
}
/// <summary>
/// Returns the phase of the moon. Taken straight from Nethack's hacklib.c.
/// </summary>
/// <returns>An integer from 0-7, where 0 is new moon and 4 is full moon.</returns>
public static int MoonPhase()
{
/*
* moon period = 29.53058 days ~= 30, year = 365.2422 days
* days moon phase advances on first day of year compared to preceding year
* = 365.2422 - 12*29.53058 ~= 11
* years in Metonic cycle (time until same phases fall on the same days of
* the month) = 18.6 ~= 19
* moon phase on first day of year (epact) ~= (11*(year%19) + 29) % 30
* (29 as initial condition)
* current phase in days = first day phase + days elapsed in year
* 6 moons ~= 177 days
* 177 ~= 8 reported phases * 22
* + 11/22 for rounding
*/
var diy = DateTime.Now.DayOfYear;
var goldn = (DateTime.Now.Year % 19) + 1;
var epact = (11 * goldn + 18) % 30;
if ((epact == 25 && goldn > 11) || epact == 24)
epact++;
return ((((((diy + epact) * 6) + 11) % 177) / 22) & 7);
}
public static bool IsNight()
{
return NoxicoGame.InGameTime.Hour < 6 || NoxicoGame.InGameTime.Hour > 21;
}
public static void FoldCostumeRandoms(Token token)
{
if (token == null)
return;
while (token.HasToken("random"))
{
var rnd = token.GetToken("random");
var pick = rnd.Tokens.PickOne();
token.Tokens.Remove(rnd);
foreach (var t in pick.Tokens)
token.Tokens.Add(t);
//rnd = pick;
}
}
public static void FoldCostumeVariables(Token token, string[] vars = null)
{
if (token == null)
return;
if (vars == null)
vars = new string[100];
while (token.HasToken("setvar"))
{
var setvar = token.GetToken("setvar");
var id = (int)setvar.GetToken("id").Value;
var value = setvar.GetToken("value");
vars[id] = value.Text;
token.RemoveToken("setvar");
}
while (token.HasToken("var"))
{
var getvar = token.GetToken("var");
var id = (int)getvar.Value;
if (vars[id].IsBlank())
token.RemoveToken("var");
else
getvar.Name = vars[id];
}
if (!token.Text.IsBlank() && token.Text.Trim().StartsWith("var "))
{
var id = int.Parse(token.Text.Trim().Substring(4));
token.Text = vars[id].IsBlank("<invalid token>", vars[id]);
}
foreach (var child in token.Tokens)
FoldCostumeVariables(child, vars);
}
public static string TranslateKey(KeyBinding binding, bool longhand = false, bool withColors = true)
{
if (Vista.GamepadAvailable)
{
if (binding == KeyBinding.Interact || binding == KeyBinding.Accept)
return withColors ? "<cGreen>A<c>" : "A";
if (binding == KeyBinding.Activate || binding == KeyBinding.Back)
return withColors ? "<cRed>B<c>" : "B";
if (binding == KeyBinding.Items)
return withColors ? "<cBlue>X<c>" : "X";
if (binding == KeyBinding.Fly)
return withColors ? "<cYellow>Y<c>" : "Y";
if (binding == KeyBinding.Rest)
return withColors ? "<cSilver>\xA9<c>" : "Left";
if (binding == KeyBinding.Travel || binding == KeyBinding.TabFocus)
return withColors ? "<cSilver>\xAB<c>" : "Right";
if (binding == KeyBinding.Pause)
return "Start";
}
return TranslateKey(NoxicoGame.RawBindings[binding], longhand, shifted: NoxicoGame.KeyBindingMods[binding]);
}
public static string TranslateKey(System.Windows.Forms.Keys key, bool longhand = false, bool shifted = false)
{
return TranslateKey(key.ToString(), longhand, shifted);
}
public static string TranslateKey(string key, bool longhand = false, bool shifted = false)
{
var shift = shifted ? "Shift+" : string.Empty;
key = key.ToUpperInvariant();
if (key.StartsWith("OEM"))
key = key.Substring(3);
var specials = new Dictionary<string, string>()
{
{ "LEFT", "\x1B" },
{ "UP", "\x18" },
{ "RIGHT", "\x1A" },
{ "DOWN", "\x19" },
{ "RETURN", "Ret." },
{ "ENTER", "Ret." },
{ "QUESTION", "/" },
{ "PERIOD", "." },
{ "COMMA", "," },
{ "QUOTES", "'" },
{ "SEMICOLON", ";" },
{ "ESCAPE", "Esc." },
{ "OPENBRACKETS", "[" },
{ "1", ";" },
{ "5", "\\" },
{ "6", "]" },
{ "7", "'" },
{ "MINUS", "-" },
{ "PLUS", "+" },
{ "TILDE", "`" },
{ "BACKSLASH", "\\" },
{ "DIVIDE", "/" },
{ "MULTIPLY", "*" },
{ "SUBTRACT", "-" },
{ "ADD", "+" }
};
if (longhand)
{
specials = new Dictionary<string, string>()
{
{ "QUESTION", "/" },
{ "COMMA", "," },
{ "QUOTES", "'" },
{ "SEMICOLON", ";" },
{ "1", ";" },
{ "5", "'\\" },
{ "6", "]" },
{ "7", "'" },
{ "RETURN", "Return" },
{ "ENTER", "Return" }
};
}
if (key.Length == 2 && key[0] == 'D' && char.IsDigit(key[1]))
return shift + key.Substring(1);
if (specials.ContainsKey(key))
return shift + specials[key].Titlecase();
return shift + key.Titlecase();
}
public static string InitialCase(this string text)
{
var initial = text[0];
if (char.IsLower(initial))
return initial.ToString().ToUpperInvariant() + text.Substring(1);
return text;
}
public static void SaveExpectation(BinaryWriter stream, string expectation)
{
stream.Write(expectation.ToCharArray());
}
public static void ExpectFromFile(BinaryReader stream, string expected, string friendly)
{
var found = stream.ReadChars(expected.Length);
if ((new string(found)) != expected)
throw new Exception("Expected to find " + friendly + " data.");
}
public static string ToID(this string name)
{
return Regex.Replace(name.ToLower(), "(^[A-Z])", string.Empty);
}
public static Direction Opposite(Direction current)
{
if (current == Direction.North)
return Direction.South;
else if (current == Direction.East)
return Direction.West;
else if (current == Direction.South)
return Direction.North;
else if (current == Direction.West)
return Direction.East;
return Direction.North;
}
#region PillowShout's additions
public static void VerifyBodyplan(TokenCarrier bodyPlan, string name)
{
var plan = bodyPlan;
var missing = new List<string>();
//Rewrite by Kawa to allow ONE exception to list ALL missing tokens. It certainly reduced the amount of restarts when fixing all these missing tokens...
if (plan.HasToken("beast"))
return;
foreach (var t in new[] { "culture", "namegen", "terms", "tallness", "hair", "skin", "eyes", "ears", "face", "teeth", "tongue" })
if (!plan.HasToken(t))
missing.Add(t);
if (!(plan.HasToken("legs") || plan.HasToken("snaketail") || plan.HasToken("slimeblob")))
missing.Add("legs, snaketail, or slimeblob");
if (plan.HasToken("legs") & !(plan.HasToken("quadruped") || plan.HasToken("taur")))
{
if (!plan.HasToken("hips"))
missing.Add("hips");
if (!plan.HasToken("waist"))
missing.Add("waist");
}
if (plan.HasToken("ass"))
{
foreach (var t in new[] { "ass/size", "ass/looseness" })
if (plan.Path(t) == null)
missing.Add(t);
}
else
missing.Add("ass");
if (!(plan.HasToken("maleonly") || plan.HasToken("neuteronly")))
{
if (plan.HasToken("vagina"))
{
var vaginas = plan.Tokens.FindAll(x => x.Name == "vagina");
foreach (var v in vaginas)
{
foreach (var t in new[] { "clit", "looseness", "wetness" })
if (!v.HasToken(t))
missing.Add("vagina/" + t);
}
}
else
missing.Add("vagina");
if (plan.HasToken("breasts"))
{
var boobs = plan.GetToken("breasts");
foreach (var t in new[] { "amount", "size" })
if (!boobs.HasToken(t))
missing.Add("breasts/" + t);
}
else if (!plan.HasToken("quadruped"))
{
//Only consider missing breasts a problem if the character is not a quadruped.
missing.Add("breasts");
}
}
if (!(plan.HasToken("femaleonly") || plan.HasToken("neuteronly")))
{
if (plan.HasToken("penis"))
{
var penises = plan.Tokens.FindAll(x => x.Name == "penis");
foreach (var p in penises)
{
foreach (var t in new[] { "thickness", "length" })
if (!p.HasToken(t))
missing.Add("penis/" + t);
}
}
else
missing.Add("penis");
if (plan.HasToken("balls"))
{
foreach (var t in new[] { "balls/size", "balls/amount" })
if (plan.Path(t) == null)
missing.Add(t);
}
else
missing.Add("balls");