/
data_structures.cpp
2028 lines (1765 loc) · 62.7 KB
/
data_structures.cpp
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
/********************************************************************************\
** **
** Copyright (C) 2011 Alasdair Morrison <tgmg@g-java.com> **
** Copyright (C) 2011 Polygone **
** **
** This file is a part of the ENIGMA Development Environment. **
** **
** **
** ENIGMA is free software: you can redistribute it and/or modify it under the **
** terms of the GNU General Public License as published by the Free Software **
** Foundation, version 3 of the license or any later version. **
** **
** This application and its source code is distributed AS-IS, WITHOUT ANY **
** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS **
** FOR A PARTICULAR PURPOSE. See the GNU General Public License for more **
** details. **
** **
** You should have recieved a copy of the GNU General Public License along **
** with this code. If not, see <http://www.gnu.org/licenses/> **
** **
** ENIGMA is an environment designed to create games and other programs with a **
** high-level, fully compilable language. Developers of ENIGMA or anything **
** associated with ENIGMA are in no way responsible for its users or **
** applications created by its users, or damages caused by the environment **
** or programs made in the environment. **
** **
\********************************************************************************/
#include <float.h>
#include <algorithm>
#include <map>
#include <deque>
#include <vector>
#include <sstream>
#include <string>
#include <floatcomp.h>
using namespace std;
#include "include.h"
static inline double maxv(double a, double b) { return (a > b) ? a : b; }
static inline double minv(double a, double b) { return (a < b) ? a : b; }
static inline unsigned maxv(unsigned a, unsigned b) { return (a > b) ? a : b; }
static inline unsigned minv(unsigned a, unsigned b) { return (a < b) ? a : b; }
static inline int maxv(int a, int b) { return (a > b) ? a : b; }
static inline int minv(int a, int b) { return (a < b) ? a : b; }
template<typename t> bool tequal(t v1, t v2) { return v1 == v2; }
template<> bool tequal(float v1, float v2) { return fequal(v1, v2); }
template<> bool tequal(double v1, double v2) { return fequal(v1, v2); }
template <typename t>
class grid
{
unsigned int xgrid, ygrid;
t *grid_array;
public:
grid() {}
grid(const unsigned int w, const unsigned int h) {
ygrid = h; xgrid = w; grid_array = new t[w*h];
}
~grid() {}
void destroy()
{
delete[] grid_array;
}
void clear(const t val)
{
for (unsigned i = 0; i < ygrid; i++)
for (unsigned ii = 0; ii < xgrid; ii++)
grid_array[i * xgrid + ii] = val;
}
void resize(unsigned w, unsigned h)
{
grid<variant> temp(w, h);
const unsigned int wm = minv(xgrid, w), hm = minv(ygrid, h);
for (unsigned i = 0; i < hm; i++)
for (unsigned ii = 0; ii < wm; ii++)
temp.grid_array[i * w + ii] = grid_array[i * xgrid + ii];
delete[] grid_array;
(*this) = temp;
}
void copy(const grid& copy_id)
{
delete[] grid_array;
grid_array = new t[copy_id.ygrid*copy_id.xgrid];
xgrid = copy_id.xgrid;
ygrid = copy_id.ygrid;
for (unsigned i = 0; i < ygrid; i++)
for (unsigned ii = 0; ii < xgrid; ii++)
grid_array[i * xgrid + ii] = copy_id.grid_array[i * copy_id.xgrid + ii];
}
unsigned int width()
{
return xgrid;
}
unsigned int height()
{
return ygrid;
}
void insert(const unsigned int x, const unsigned int y, const t val)
{
if (x < xgrid && y < ygrid)
grid_array[y * xgrid + x] = val;
}
void add(const unsigned int x, const unsigned int y, const t val)
{
if (x < xgrid && y < ygrid)
grid_array[y * xgrid + x] += val;
}
void multiply(const unsigned int x, const unsigned int y, const double val)
{
if (x < xgrid && y < ygrid)
grid_array[y * xgrid + x] *= val;
}
void insert_region(const unsigned int x1, const unsigned int y1, unsigned int x2, const unsigned int y2, const t val)
{
const int tx1 = minv(x1, x2), ty1 = minv(y1, y2), tx2 = maxv(x1, x2), ty2 = maxv(y1, y2), xd = xgrid - tx1, yd = ygrid - ty1;
if (xd > 0 && yd > 0)
{
const int px1 = maxv(tx1, 0), py1 = maxv(ty1, 0), px2 = minv(tx2 + 1, (int)xgrid), py2 = minv(ty2 + 1, (int)ygrid);
for (int i = py1; i < py2; i++)
for (int ii = px1; ii < px2; ii++)
grid_array[i * xgrid + ii] = val;
}
}
void add_region(const unsigned int x1, const unsigned int y1, unsigned int x2, const unsigned int y2, const t val)
{
const int tx1 = minv(x1, x2), ty1 = minv(y1, y2), tx2 = maxv(x1, x2), ty2 = maxv(y1, y2), xd = xgrid - tx1, yd = ygrid - ty1;
if (xd > 0 && yd > 0)
{
const int px1 = maxv(tx1, 0), py1 = maxv(ty1, 0), px2 = minv(tx2 + 1, (int)xgrid), py2 = minv(ty2 + 1, (int)ygrid);
for (int i = py1; i < py2; i++)
for (int ii = px1; ii < px2; ii++)
grid_array[i * xgrid + ii] += val;
}
}
void multiply_region(const unsigned int x1, const unsigned int y1, unsigned int x2, const unsigned int y2, const double val)
{
const int tx1 = minv(x1, x2), ty1 = minv(y1, y2), tx2 = maxv(x1, x2), ty2 = maxv(y1, y2), xd = xgrid - tx1, yd = ygrid - ty1;
if (xd > 0 && yd > 0)
{
const int px1 = maxv(tx1, 0), py1 = maxv(ty1, 0), px2 = minv(tx2 + 1, (int)xgrid), py2 = minv(ty2 + 1, (int)ygrid);
for (int i = py1; i < py2; i++)
for (int ii = px1; ii < px2; ii++)
grid_array[i * xgrid + ii] *= val;
}
}
void insert_disk(const double x, const double y, const double r, const t val)
{
const double rr = r*r;
const int tx1 = int(x - r), ty1 = int(y - r), tx2 = int(x + r + 1), ty2 = int(y + r + 1);
if (tx2 >= 0 && ty2 >=0 && tx1 < int(xgrid) && ty1 < int(ygrid))
{
const int px1 = maxv(tx1, 0), py1 = maxv(ty1, 0), px2 = minv(tx2, (int)xgrid), py2 = minv(ty2, (int)ygrid);
for (int i = py1; i < py2; i++)
for (int ii = px1; ii < px2; ii++)
if ((x - ii)*(x - ii) + (y - i)*(y - i) <= rr)
grid_array[i * xgrid + ii] = val;
}
}
void add_disk(const double x, const double y, const double r, const t val)
{
const double rr = r*r;
const int tx1 = int(x - r), ty1 = int(y - r), tx2 = int(x + r + 1), ty2 = int(y + r + 1);
if (tx2 >= 0 && ty2 >=0 && tx1 < int(xgrid) && ty1 < int(ygrid))
{
const int px1 = maxv(tx1, 0), py1 = maxv(ty1, 0), px2 = minv(tx2, (int)xgrid), py2 = minv(ty2, (int)ygrid);
for (int i = py1; i < py2; i++)
for (int ii = px1; ii < px2; ii++)
if ((x - ii)*(x - ii) + (y - i)*(y - i) <= rr)
grid_array[i * xgrid + ii] += val;
}
}
void multiply_disk(const double x, const double y, const double r, const double val)
{
const double rr = r*r;
const int tx1 = int(x - r), ty1 = int(y - r), tx2 = int(x + r + 1), ty2 = int(y + r + 1);
if (tx2 >= 0 && ty2 >=0 && tx1 < int(xgrid) && ty1 < int(ygrid))
{
const int px1 = maxv(tx1, 0), py1 = maxv(ty1, 0), px2 = minv(tx2, (int)xgrid), py2 = minv(ty2, (int)ygrid);
for (int i = py1; i < py2; i++)
for (int ii = px1; ii < px2; ii++)
if ((x - ii)*(x - ii) + (y - i)*(y - i) <= rr)
grid_array[i * xgrid + ii] *= val;
}
}
void insert_grid_region(const grid& source_id, const unsigned int sx1, const unsigned int sy1, const unsigned int sx2, const unsigned int sy2, const unsigned int x, const unsigned int y)
{
if (x < xgrid && y < ygrid)
{
const int tx1 = minv(sx1, sx2), ty1 = minv(sy1, sy2), tx2 = maxv(sx1, sx2), ty2 = maxv(sy1, sy2), xd = source_id.xgrid - tx1, yd = source_id.ygrid - ty1;
if (xd > 0 && yd > 0)
{
const int upx = minv(tx2 - tx1 + 1, minv(int(xgrid - x), xd)), upy = minv(ty2 - ty1 + 1, minv(int(ygrid - y), yd));
for (int i = 0; i < upy; i++)
for (int ii = 0; ii < upx; ii++)
grid_array[(y + i)*xgrid + (x + ii)] = source_id.grid_array[(ty1 + i)*source_id.xgrid + (tx1 + ii)];
}
}
}
void add_grid_region(const grid& source_id, const unsigned int sx1, const unsigned int sy1, const unsigned int sx2, const unsigned int sy2, const unsigned int x, const unsigned int y)
{
if (x < xgrid && y < ygrid)
{
const int tx1 = minv(sx1, sx2), ty1 = minv(sy1, sy2), tx2 = maxv(sx1, sx2), ty2 = maxv(sy1, sy2), xd = source_id.xgrid - tx1, yd = source_id.ygrid - ty1;
if (xd > 0 && yd > 0)
{
const int upx = minv(tx2 - tx1 + 1, minv(int(xgrid - x), xd)), upy = minv(ty2 - ty1 + 1, minv(int(ygrid - y), yd));
for (int i = 0; i < upy; i++)
for (int ii = 0; ii < upx; ii++)
grid_array[(y + i)*xgrid + (x + ii)] += source_id.grid_array[(ty1 + i)*source_id.xgrid + (tx1 + ii)];
}
}
}
void multiply_grid_region(const grid& source_id, const unsigned int sx1, const unsigned int sy1, const unsigned int sx2, const unsigned int sy2, const unsigned int x, const unsigned int y)
{
if (x < xgrid && y < ygrid)
{
const int tx1 = minv(sx1, sx2), ty1 = minv(sy1, sy2), tx2 = maxv(sx1, sx2), ty2 = maxv(sy1, sy2), xd = source_id.xgrid - tx1, yd = source_id.ygrid - ty1;
if (xd > 0 && yd > 0)
{
const int upx = minv(tx2 - tx1 + 1, minv(int(xgrid - x), xd)), upy = minv(ty2 - ty1 + 1, minv(int(ygrid - y), yd));
for (int i = 0; i < upy; i++)
for (int ii = 0; ii < upx; ii++)
grid_array[(y + i)*xgrid + (x + ii)] *= source_id.grid_array[(ty1 + i)*source_id.xgrid + (tx1 + ii)];
}
}
}
t find(unsigned int x, unsigned int y)
{
return (grid_array[y * xgrid + x]);
}
t find_region_sum(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2)
{
const int tx1 = minv(x1, x2), ty1 = minv(y1, y2), tx2 = maxv(x1, x2), ty2 = maxv(y1, y2), xd = xgrid - tx1, yd = ygrid - ty1;
if (xd > 0 && yd > 0)
{
const int px1 = maxv(tx1, 0), py1 = maxv(ty1, 0), px2 = minv(tx2 + 1, (int)xgrid), py2 = minv(ty2 + 1, (int)ygrid);
variant sum = 0;
for (int i = py1; i < py2; i++)
for (int ii = px1; ii < px2; ii++)
sum += grid_array[i * xgrid + ii];
return sum;
}
return t();
}
t find_region_max(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2)
{
const int tx1 = minv(x1, x2), ty1 = minv(y1, y2), tx2 = maxv(x1, x2), ty2 = maxv(y1, y2), xd = xgrid - tx1, yd = ygrid - ty1;
t val_check;
if (xd > 0 && yd > 0)
{
const int px1 = maxv(tx1, 0), py1 = maxv(ty1, 0), px2 = minv(tx2 + 1, (int)xgrid), py2 = minv(ty2 + 1, (int)ygrid);
t max_check = grid_array[py1 * xgrid + px1];
for (int i = py1; i < py2; i++)
for (int ii = px1; ii < px2; ii++)
{
val_check = grid_array[i * xgrid + ii];
if (val_check > max_check)
max_check = val_check;
}
return max_check;
}
return t();
}
t find_region_min(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2)
{
const int tx1 = minv(x1, x2), ty1 = minv(y1, y2), tx2 = maxv(x1, x2), ty2 = maxv(y1, y2), xd = xgrid - tx1, yd = ygrid - ty1;
t val_check;
if (xd > 0 && yd > 0)
{
const int px1 = maxv(tx1, 0), py1 = maxv(ty1, 0), px2 = minv(tx2 + 1, (int)xgrid), py2 = minv(ty2 + 1, (int)ygrid);
t min_check = grid_array[py1 * xgrid + px1];
for (int i = py1; i < py2; i++)
for (int ii = px1; ii < px2; ii++)
{
val_check = grid_array[i * xgrid + ii];
if (val_check < min_check)
min_check = val_check;
}
return min_check;
}
return t();
}
t find_region_mean(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2)
{
const int tx1 = minv(x1, x2), ty1 = minv(y1, y2), tx2 = maxv(x1, x2), ty2 = maxv(y1, y2), xd = xgrid - tx1, yd = ygrid - ty1;
if (xd > 0 && yd > 0)
{
const int px1 = maxv(tx1, 0), py1 = maxv(ty1, 0), px2 = minv(tx2 + 1, (int)xgrid), py2 = minv(ty2 + 1, (int)ygrid);
t sum = 0;
for (int i = py1; i < py2; i++)
for (int ii = px1; ii < px2; ii++)
sum += grid_array[i * xgrid + ii];
const double region_size = (py2 - py1)*(px2 - px1);
return sum/region_size;
}
return t();
}
t find_disk_sum(const double x, const double y, const double r)
{
const double rr = r*r;
const int tx1 = int(x - r), ty1 = int(y - r), tx2 = int(x + r + 1), ty2 = int(y + r + 1);
if (tx2 >= 0 && ty2 >=0 && tx1 < int(xgrid) && ty1 < int(ygrid))
{
const int px1 = maxv(tx1, 0), py1 = maxv(ty1, 0), px2 = minv(tx2, (int)xgrid), py2 = minv(ty2, (int)ygrid);
t sum = t();
for (int i = py1; i < py2; i++)
for (int ii = px1; ii < px2; ii++)
if ((x - ii)*(x - ii) + (y - i)*(y - i) <= rr)
sum += grid_array[i * xgrid + ii];
return sum;
}
return t();
}
t find_disk_max(const double x, const double y, const double r)
{
const double rr = r*r;
const int tx1 = int(x - r), ty1 = int(y - r), tx2 = int(x + r + 1), ty2 = int(y + r + 1);
if (tx2 >= 0 && ty2 >=0 && tx1 < int(xgrid) && ty1 < int(ygrid))
{
const int px1 = maxv(tx1, 0), py1 = maxv(ty1, 0), px2 = minv(tx2, (int)xgrid), py2 = minv(ty2, (int)ygrid);
t max_check = grid_array[py1 * xgrid + px1];
for (int i = py1; i < py2; i++)
for (int ii = px1; ii < px2; ii++)
if ((x - ii)*(x - ii) + (y - i)*(y - i) <= rr)
{
const double val_check = grid_array[i * xgrid + ii];
if (val_check > max_check)
max_check = val_check;
}
return max_check;
}
return t();
}
t find_disk_min(const double x, const double y, const double r)
{
const double rr = r*r;
const int tx1 = int(x - r), ty1 = int(y - r), tx2 = int(x + r + 1), ty2 = int(y + r + 1);
if (tx2 >= 0 && ty2 >=0 && tx1 < int(xgrid) && ty1 < int(ygrid))
{
const int px1 = maxv(tx1, 0), py1 = maxv(ty1, 0), px2 = minv(tx2, (int)xgrid), py2 = minv(ty2, (int)ygrid);
t min_check = grid_array[lrint(y) * xgrid + lrint(x)];
for (int i = py1; i < py2; i++)
for (int ii = px1; ii < px2; ii++)
if ((x - ii)*(x - ii) + (y - i)*(y - i) <= rr)
{
const double val_check = grid_array[i * xgrid + ii];
if (val_check < min_check)
min_check = val_check;
}
return min_check;
}
return t();
}
t find_disk_mean(const double x, const double y, const double r)
{
const double rr = r*r;
const int tx1 = int(x - r), ty1 = int(y - r), tx2 = int(x + r + 1), ty2 = int(y + r + 1);
if (tx2 >= 0 && ty2 >=0 && tx1 < int(xgrid) && ty1 < int(ygrid))
{
const int px1 = maxv(tx1, 0), py1 = maxv(ty1, 0), px2 = minv(tx2, (int)xgrid), py2 = minv(ty2, (int)ygrid);
t sum = t();
double region_size = 0;
for (int i = py1; i < py2; i++)
for (int ii = px1; ii < px2; ii++)
if ((x - ii)*(x - ii) + (y - i)*(y - i) <= rr)
{
sum += grid_array[i * xgrid + ii];
++region_size;
}
return sum/region_size;
}
return t();
}
bool value_region_exists(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, const t val)
{
const int tx1 = minv(x1, x2), ty1 = minv(y1, y2), tx2 = maxv(x1, x2), ty2 = maxv(y1, y2), xd = xgrid - tx1, yd = ygrid - ty1;
if (xd > 0 && yd > 0)
{
const int px1 = maxv(tx1, 0), py1 = maxv(ty1, 0), px2 = minv(tx2 + 1, (int)xgrid), py2 = minv(ty2 + 1, (int)ygrid);
for (int i = py1; i < py2; i++)
for (int ii = px1; ii < px2; ii++)
if (tequal(grid_array[i * xgrid + ii], val))
return true;
}
return false;
}
int value_region_x(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, const t val)
{
const int tx1 = minv(x1, x2), ty1 = minv(y1, y2), tx2 = maxv(x1, x2), ty2 = maxv(y1, y2), xd = xgrid - tx1, yd = ygrid - ty1;
if (xd > 0 && yd > 0)
{
const int px1 = maxv(tx1, 0), py1 = maxv(ty1, 0), px2 = minv(tx2 + 1, (int)xgrid), py2 = minv(ty2 + 1, (int)ygrid);
for (int i = py1; i < py2; i++)
for (int ii = px1; ii < px2; ii++)
if (tequal(grid_array[i * xgrid + ii], val))
return ii;
}
return 0;
}
int value_region_y(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, const t val)
{
const int tx1 = minv(x1, x2), ty1 = minv(y1, y2), tx2 = maxv(x1, x2), ty2 = maxv(y1, y2), xd = xgrid - tx1, yd = ygrid - ty1;
if (xd > 0 && yd > 0)
{
const int px1 = maxv(tx1, 0), py1 = maxv(ty1, 0), px2 = minv(tx2 + 1, (int)xgrid), py2 = minv(ty2 + 1, (int)ygrid);
for (int i = py1; i < py2; i++)
for (int ii = px1; ii < px2; ii++)
if (tequal(grid_array[i * xgrid + ii], val))
return i;
}
return 0;
}
bool value_disk_exists(const double x, const double y, const double r, const t val)
{
const double rr = r*r;
const int tx1 = int(x - r), ty1 = int(y - r), tx2 = int(x + r + 1), ty2 = int(y + r + 1);
if (tx2 >= 0 && ty2 >=0 && tx1 < int(xgrid) && ty1 < int(ygrid))
{
const int px1 = maxv(tx1, 0), py1 = maxv(ty1, 0), px2 = minv(tx2, (int)xgrid), py2 = minv(ty2, (int)ygrid);
for (int i = py1; i < py2; i++)
for (int ii = px1; ii < px2; ii++)
if ((x - ii)*(x - ii) + (y - i)*(y - i) <= rr)
if (tequal(grid_array[i * xgrid + ii], val))
return true;
}
return false;
}
int value_disk_x(const double x, const double y, const double r, const t val)
{
const double rr = r*r;
const int tx1 = int(x - r), ty1 = int(y - r), tx2 = int(x + r + 1), ty2 = int(y + r + 1);
if (tx2 >= 0 && ty2 >=0 && tx1 < int(xgrid) && ty1 < int(ygrid))
{
const int px1 = maxv(tx1, 0), py1 = maxv(ty1, 0), px2 = minv(tx2, (int)xgrid), py2 = minv(ty2, (int)ygrid);
for (int i = py1; i < py2; i++)
for (int ii = px1; ii < px2; ii++)
if ((x - ii)*(x - ii) + (y - i)*(y - i) <= rr)
if (tequal(grid_array[i * xgrid + ii], val))
return i;
}
return 0;
}
int value_disk_y(const double x, const double y, const double r, const t val)
{
const double rr = r*r;
const int tx1 = int(x - r), ty1 = int(y - r), tx2 = int(x + r + 1), ty2 = int(y + r + 1);
if (tx2 >= 0 && ty2 >=0 && tx1 < int(xgrid) && ty1 < int(ygrid))
{
const int px1 = maxv(tx1, 0), py1 = maxv(ty1, 0), px2 = minv(tx2, (int)xgrid), py2 = minv(ty2, (int)ygrid);
for (int i = py1; i < py2; i++)
for (int ii = px1; ii < px2; ii++)
if ((x - ii)*(x - ii) + (y - i)*(y - i) <= rr)
if (tequal(grid_array[i * xgrid + ii], val))
return ii;
}
return 0;
}
void shuffle()
{
random_shuffle(grid_array, grid_array + (xgrid*ygrid - 1));
}
};
/* ds_grids */
static map<unsigned int, grid<variant> > ds_grids;
static unsigned int ds_grids_maxid = 0;
namespace enigma_user
{
unsigned int ds_grid_create(const unsigned int w, const unsigned int h)
{
//Creates a new grid. The function returns an integer as an id that must be used in all other functions to access the particular grid.
pair<map<unsigned int, grid<variant> >::iterator, bool> ins = ds_grids.insert(pair<unsigned int, grid<variant> >(ds_grids_maxid++, grid<variant>(w, h)));
ins.first->second.clear(0);
return ds_grids_maxid-1;
}
void ds_grid_destroy(const unsigned int id)
{
//Destroys the grid
ds_grids[id].destroy();
ds_grids.erase(ds_grids.find(id));
}
void ds_grid_clear(const unsigned int id, const variant val)
{
//Clears the grid with the given id, to the indicated value
ds_grids[id].clear(val);
}
void ds_grid_copy(const unsigned int id, const unsigned int source)
{
//Copies the source grid onto the grid
ds_grids[id].copy(ds_grids[source]);
}
void ds_grid_resize(const unsigned int id, const unsigned int w, const unsigned int h)
{
ds_grids[id].resize(w, h);
}
unsigned int ds_grid_width(const unsigned int id)
{
//Returns the width of the grid
return ds_grids[id].width();
}
unsigned int ds_grid_height(const unsigned int id)
{
//Returns the height of the grid
return ds_grids[id].height();
}
void ds_grid_set(const unsigned int id, const unsigned int x, const unsigned int y, const variant val)
{
//Sets the indicated cell in the grid with the given id, to the indicated value
ds_grids[id].insert(x, y, val);
}
void ds_grid_add(const unsigned int id, const unsigned int x, const unsigned int y, const variant val)
{
//Add the value to the cell in the region in the grid with the given id. For strings this corresponds to concatenation
ds_grids[id].add(x, y, val);
}
void ds_grid_multiply(const unsigned int id, const unsigned int x, const unsigned int y, const double val)
{
//Multiplies the value to the cells in the region in the grid with the given id
ds_grids[id].multiply(x, y, val);
}
void ds_grid_set_region(const unsigned int id, const unsigned int x1, const unsigned int y1, const unsigned int x2, const unsigned int y2, const variant val)
{
//Sets the all cells in the region in the grid with the given id, to the indicated value
ds_grids[id].insert_region(x1, y1, x2, y2, val);
}
void ds_grid_add_region(const unsigned int id, const unsigned int x1, const unsigned int y1, const unsigned int x2, const unsigned int y2, const variant val)
{
//Add the value to the cell in the region in the grid with the given id.
ds_grids[id].add_region(x1, y1, x2, y2, val);
}
void ds_grid_multiply_region(const unsigned int id, const unsigned int x1, const unsigned int y1, const unsigned int x2, const unsigned int y2, const double val)
{
//Multiplies the value to the cells in the region in the grid with the given id. Is only valid for numbers
ds_grids[id].multiply_region(x1, y1, x2, y2, val);
}
void ds_grid_set_disk(const unsigned int id, const double x, const double y, const double r, const variant val)
{
//Sets all cells in the disk with center (xm,ym) and radius r
ds_grids[id].insert_disk(x, y, r, val);
}
void ds_grid_add_disk(const unsigned int id, const double x, const double y, const double r, const variant val)
{
//Add the value to all cells in the disk with center (xm,ym) and radius r
ds_grids[id].add_disk(x, y, r, val);
}
void ds_grid_multiply_disk(const unsigned int id, const double x, const double y, const double r, const double val)
{
//Multiply the value to all cells in the disk with center (xm,ym) and radius r
ds_grids[id].multiply_disk(x, y, r, val);
}
void ds_grid_set_grid_region(const unsigned int id, const unsigned int source, const unsigned int x1, const unsigned int y1, const unsigned int x2, const unsigned int y2, const unsigned int xpos, const unsigned int ypos)
{
//Copies the contents of the cells in the region in grid source to grid id. xpos and ypos indicate the place where the region must be placed in the grid
ds_grids[id].insert_grid_region(ds_grids[source], x1, y1, x2, y2, xpos, ypos);
}
void ds_grid_add_grid_region(const unsigned int id, const unsigned int source, const unsigned int x1, const unsigned int y1, const unsigned int x2, const unsigned int y2, const unsigned int xpos, const unsigned int ypos)
{
//Adds the contents of the cells in the region in grid source to grid id. xpos and ypos indicate the place where the region must be added in the grid
ds_grids[id].add_grid_region(ds_grids[source], x1, y1, x2, y2, xpos, ypos);
}
void ds_grid_multiply_grid_region(const unsigned int id, const unsigned int source, const unsigned int x1, const unsigned int y1, const unsigned int x2, const unsigned int y2, const unsigned int xpos, const unsigned int ypos)
{
//Multiplies the contents of the cells in the region in grid source to grid id. xpos and ypos indicate the place where the region must be multiplied in the grid
ds_grids[id].multiply_grid_region(ds_grids[source], x1, y1, x2, y2, xpos, ypos);
}
variant ds_grid_get(const unsigned int id, const unsigned int x, const unsigned int y)
{
//Returns the value of the indicated cell in the grid with the given id
return ((x < ds_grids[id].width() && y < ds_grids[id].height()) ? ds_grids[id].find(x, y) : variant());
}
variant ds_grid_get_sum(const unsigned int id, const unsigned int x1, const unsigned int y1, const unsigned int x2, const unsigned int y2)
{
//Returns the sum of the values of the cells in the region in the grid with the given id
return (((x1 < ds_grids[id].width() || x2 < ds_grids[id].width()) && (y1 < ds_grids[id].height() || y2 < ds_grids[id].height())) ? ds_grids[id].find_region_sum(x1, y1, x2, y2) : variant());
}
variant ds_grid_get_max(const unsigned int id, const unsigned int x1, const unsigned int y1, const unsigned int x2, const unsigned int y2)
{
//Returns the max of the values of the cells in the region in the grid with the given id
return (((x1 < ds_grids[id].width() || x2 < ds_grids[id].width()) && (y1 < ds_grids[id].height() || y2 < ds_grids[id].height())) ? ds_grids[id].find_region_max(x1, y1, x2, y2) : variant());
}
variant ds_grid_get_min(const unsigned int id, const unsigned int x1, const unsigned int y1, const unsigned int x2, const unsigned int y2)
{
//Returns the min of the values of the cells in the region in the grid with the given id
return (((x1 < ds_grids[id].width() || x2 < ds_grids[id].width()) && (y1 < ds_grids[id].height() || y2 < ds_grids[id].height())) ? ds_grids[id].find_region_min(x1, y1, x2, y2) : variant());
}
variant ds_grid_get_mean(const unsigned int id, const unsigned int x1, const unsigned int y1, const unsigned int x2, const unsigned int y2)
{
//Returns the mean of the values of the cells in the region in the grid with the given id
return (((x1 < ds_grids[id].width() || x2 < ds_grids[id].width()) && (y1 < ds_grids[id].height() || y2 < ds_grids[id].height())) ? ds_grids[id].find_region_mean(x1, y1, x2, y2) : variant());
}
variant ds_grid_get_disk_sum(const unsigned int id, const double x, const double y, const double r)
{
//Returns the sum of the values of the cells in the disk
return (ds_grids[id].find_disk_sum(x, y, r));
}
variant ds_grid_get_disk_max(const unsigned int id, const double x, const double y, const double r)
{
//Returns the max of the values of the cells in the disk.
return (ds_grids[id].find_disk_max(x, y, r));
}
variant ds_grid_get_disk_min(const unsigned int id, const double x, const double y, const double r)
{
//Returns the min of the values of the cells in the disk.
return (ds_grids[id].find_disk_min(x, y, r));
}
variant ds_grid_get_disk_mean(const unsigned int id, const double x, const double y, const double r)
{
//Returns the mean of the values of the cells in the disk
return (ds_grids[id].find_disk_mean(x, y, r));
}
bool ds_grid_value_exists(const unsigned int id, const unsigned int x1, const unsigned int y1, const unsigned int x2, const unsigned int y2, const variant val)
{
//Returns whether the value appears somewhere in the region
return (((x1 < ds_grids[id].width() || x2 < ds_grids[id].width()) && (y1 < ds_grids[id].height() || y2 < ds_grids[id].height())) ? ds_grids[id].value_region_exists(x1, y1, x2, y2, val) : false);
}
int ds_grid_value_x(const unsigned int id, const unsigned int x1, const unsigned int y1, const unsigned int x2, const unsigned int y2, const variant val)
{
//Returns the x-coordinate of the cell in which the value appears in the region
return (((x1 < ds_grids[id].width() || x2 < ds_grids[id].width()) && (y1 < ds_grids[id].height() || y2 < ds_grids[id].height())) ? ds_grids[id].value_region_x(x1, y1, x2, y2, val) : 0);
}
int ds_grid_value_y(const unsigned int id, const unsigned int x1, const unsigned int y1, const unsigned int x2, const unsigned int y2, const variant val)
{
//Returns the y-coordinate of the cell in which the value appears in the region
return (((x1 < ds_grids[id].width() || x2 < ds_grids[id].width()) && (y1 < ds_grids[id].height() || y2 < ds_grids[id].height())) ? ds_grids[id].value_region_y(x1, y1, x2, y2, val) : 0);
}
bool ds_grid_value_disk_exists(const unsigned int id, const double x, const double y, const double r, const variant val)
{
//Returns whether the value appears somewhere in the disk
return (ds_grids[id].value_disk_exists(x, y, r, val));
}
bool ds_grid_value_disk_x(const unsigned int id, const double x, const double y, const double r, const variant val)
{
//Returns the x-coordinate of the cell in which the value appears in the disk
return (ds_grids[id].value_disk_x(x, y, r, val));
}
bool ds_grid_value_disk_y(const unsigned int id, const double x, const double y, const double r, const variant val)
{
//Returns the y-coordinate of the cell in which the value appears in the disk
return (ds_grids[id].value_disk_y(x, y, r, val));
}
void ds_grid_shuffle(const unsigned int id)
{
//Shuffles the values in the grid such that they end up in a random order
ds_grids[id].shuffle();
}
bool ds_grid_exists(const unsigned int id)
{
//returns whether the grid exists
return (ds_grids.find(id) != ds_grids.end());
}
unsigned int ds_grid_duplicate(const unsigned int source)
{
//creates and returns a new grid containing a copy of the source grid
ds_grids.insert(pair<unsigned int, grid<variant> >(ds_grids_maxid++, grid<variant>(0, 0)));
ds_grids[ds_grids_maxid-1].copy(ds_grids[source]);
return ds_grids_maxid-1;
}
std::string ds_grid_write(const unsigned int id)
{
std::stringstream ss;
ss.flags(std::ios::hex | std::ios::uppercase | std::ios::internal);
ss.width(4);
ss.fill('0');
grid<variant> dsGrid = ds_grids[id];
// Write size
ss << std::hex << dsGrid.width();
ss.width(4); ss << std::hex << dsGrid.height();
//ds_grids[id].find(x, y)
for (unsigned y = 0; y < dsGrid.height(); ++y)
{
for (unsigned x = 0; x < dsGrid.width(); ++x)
{
// Write coords
ss.width(4); ss << x;
ss.width(4); ss << y;
variant vari = dsGrid.find(x, y);
// Write type
ss.width(2);
ss << (unsigned int)((vari.type == ty_real) ? 0x00 : 0x01);
// Write data
if (vari.type == ty_real)
{
ss.width(16);
char* b = (char*)&vari.rval.d;
for (unsigned i = 0; i < sizeof(double); ++i)
ss << b[i];
}
else
{
ss.width(4); ss << vari.sval.length();
ss.width(1);
for (size_t j = 0; j < vari.sval.length(); ++j)
ss << vari.sval[j];
}
}
}
return ss.str();
}
void ds_grid_read(const unsigned int id, std::string value)
{
std::stringstream ss;
int i = 8;
int width, height;
// Read count
ss << std::hex << value.substr(0, 4);
ss >> width;
ss.clear();
ss << std::hex << value.substr(4, 4);
ss >> height;
ss.clear();
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
int xx, yy, type;
// Read x/y
ss << std::hex << value.substr(i, 4);
ss >> xx;
ss.clear();
i += 4;
ss << std::hex << value.substr(i, 4);
ss >> yy;
ss.clear();
i += 4;
// Read type
ss << std::hex << value.substr(i, 2);
ss >> type;
ss.clear();
i += 2;
if (type == 0)
{
variant vari;
vari.type = ty_real;
string b;
ss << std::hex << value.substr(i, 16);
ss >> b;
double d = atof(b.c_str());
ss.clear();
i += 16;
vari.rval.d = d;
ds_grids[id].add(xx, yy, vari);
}
else
{
variant vari;
vari.type = ty_string;
int len;
// Read length
ss << std::hex << value.substr(i, 4);
ss >> len;
ss.clear();
i += 4;
vari.sval = value.substr(i, len);
i += len;
ds_grids[id].add(xx, yy, vari);
}
}
}
}
}
/* ds_maps */
static map<unsigned int, multimap<variant, variant> > ds_maps;
static unsigned int ds_maps_maxid = 0;
namespace enigma_user
{
unsigned int ds_map_create()
{
//Creates a new map. The function returns an integer as an id that must be used in all other functions to access the particular map.
ds_maps.insert(pair<unsigned int, multimap<variant, variant> >(ds_maps_maxid++, multimap<variant, variant>()));
return ds_maps_maxid-1;
}
void ds_map_destroy(const unsigned int id)
{
//Destroys the map
ds_maps.erase(ds_maps.find(id));
}
void ds_map_clear(const unsigned int id)
{
//Clears all values from the map
ds_maps[id].clear();
}
void ds_map_copy(const unsigned int id, const unsigned int source)
{
//Copies the source map onto the map
ds_maps[id] = ds_maps[source];
}
unsigned int ds_map_size(const unsigned int id)
{
//Returns the size of the map
return ds_maps[id].size();
}
bool ds_map_empty(const unsigned int id)
{
//Returns whether the map contains no values
return ds_maps[id].empty();
}
void ds_map_add(const unsigned int id, const variant key, const variant val)
{
//Adds the value and corresponding key to the map.
ds_maps[id].insert(pair<variant, variant>(key, val));
}
void ds_map_replace(const unsigned int id, const variant key, const variant val)
{
//TODO: Studio made it so this function will add the value if it is not in the map.
//GM 8.1 does not have this behaviour. This has also been tested.
//We will probably not add support for that, but it may cause issues down the road.
//If a decision is made, you should probably ask Josh for clarification, but when the
//decision is made, please replace my comment here whether or not we support it.
//If this function is changed to behave this way, please fix it in the Asynchronous dialog
//extension which had to create a special function to replace a value adding it if it does
//not exist in the global async_load map.
//Replaces the value corresponding with the key with a new value
multimap<variant, variant>::iterator it = ds_maps[id].find(key);
if (it != ds_maps[id].end())
{
ds_maps[id].erase(it);
ds_maps[id].insert(pair<variant, variant>(key, val));
}
}
//NOTE: Special function, see todo comment above.
void ds_map_replaceanyway(const unsigned int id, const variant key, const variant val)
{
//Replaces the value corresponding with the key with a new value, adding it if it was not found in the map.
multimap<variant, variant>::iterator it = ds_maps[id].find(key);
if (it != ds_maps[id].end())
{
ds_maps[id].erase(it);
}
ds_maps[id].insert(pair<variant, variant>(key, val));
}
void ds_map_delete(const unsigned int id, const variant key)
{
//Deletes the key and the corresponding value from the map
multimap<variant, variant>::iterator it = ds_maps[id].find(key);
if (it != ds_maps[id].end())
{
ds_maps[id].erase(it);
}
}
void ds_map_delete(const unsigned int id, const variant first, const variant last)
{
//Deletes the keys and corresponding values in the range between first and last
multimap<variant, variant>::iterator itf = ds_maps[id].find(first), itl = ds_maps[id].find(last);
if (itf != ds_maps[id].end() && itl != ds_maps[id].end())
{
ds_maps[id].erase(itf, itl);
}
}
bool ds_map_exists(const unsigned int id, const variant key)
{
//returns whether the key exists in the map
return (ds_maps[id].find(key) != ds_maps[id].end());
}
variant ds_map_find_value(const unsigned int id, const variant key)
{
//Returns the value corresponding to the key in the map
multimap<variant, variant>::iterator it = ds_maps[id].find(key);
return ((it == ds_maps[id].end()) ? variant() : (*it).second);
}
variant ds_map_find_previous(const unsigned int id, const variant key)
{
//Returns the largest key in the map smaller than the indicated key
multimap<variant, variant>::reverse_iterator rit;
variant key_check;
for (rit = ds_maps[id].rbegin(); rit != ds_maps[id].rend(); rit++)
{
if ((key_check = (*rit).first) < key)
{
return key_check;
}
}
return 0;
}
variant ds_map_find_next(const unsigned int id, const variant key)
{
//Returns the smallest key in the map larger than the indicated key
multimap<variant, variant>::iterator it;
variant key_check;
for (it = ds_maps[id].begin(); it != ds_maps[id].end(); it++)
{
if ((key_check = (*it).first) > key)
{
return key_check;
}
}
return 0;
}
variant ds_map_find_first(const unsigned int id)
{
//Returns the smallest key in the map
multimap<variant, variant>::iterator it = ds_maps[id].begin();
return (it == ds_maps[id].end()) ? variant() : (*it).first;
}
variant ds_map_find_last(const unsigned int id)
{
//Returns the largest key in the map
multimap<variant, variant>::reverse_iterator rit = ds_maps[id].rbegin();
return ((rit == ds_maps[id].rend()) ? variant() : (*rit).first);
}
bool ds_map_exists(const unsigned int id)
{
//returns whether the map exists
return (ds_maps.find(id) != ds_maps.end());
}
unsigned int ds_map_duplicate(const unsigned int source)
{
//creates and returns a new map containing a copy of the source map
ds_maps.insert(pair<unsigned int, multimap<variant, variant> >(++ds_maps_maxid, multimap<variant, variant>()));