/
kernel.c
3916 lines (3652 loc) · 133 KB
/
kernel.c
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
/**
IreCore kernel
*** EDIT AT YOUR OWN RISK! ***
All kernel source goes here exposed to the air, for your convenience to edit.
But it is NOT recommended if you are not definitely sure how to edit.
Copyright (C) 2014, 2015 Aean <v#aean.net>
All rights reserved.
IreCore is distributed under the terms of The MIT License.
You should have received a copy of the MIT License along with this program.
If not, see <http://opensource.org/licenses/mit-license.php>.
*/
/*
TODO list:
Touch of the Grave (Undead trait) is a mystery. Need more feedbacks since I play nelf only.
Now implemented as 20%-15s ICD, (0.5*ap) damage scales with enrage, mastery and vers. Do not crit.
*/
#define SHOW_LOG
#define passive_power_regen 0
#if !defined(__OPENCL_VERSION__)
/* Front-end gives these defines. */
#define STRICT_GCD 1
#define WBR_NEVER_EXPIRE 1
#define AVATAR_LIKE_BLOODBATH 0
#define SYNC_MELEE 1
#define RNG_MWC64X
#define vary_combat_length 20.0f
#define max_length 450.0f
#define initial_health_percentage 100.0f
#define death_pct 0.0f
#define iterations 1
#define power_max 120.0f
#define num_enemies 20
#define GLYPH_OF_RAGINGWIND 1
#define PLATE_SPECIALIZATION 1
#define SINGLE_MINDED 0
#define BUFF_STR_AGI_INT 1
#define BUFF_AP 1
#define BUFF_CRIT 1
#define BUFF_HASTE 1
#define BUFF_MASTERY 1
#define BUFF_MULT 1
#define BUFF_VERS 1
#define BUFF_SP 1
#define BUFF_STA 1
#define BUFF_POTION 1
#define BUFF_BLOODLUST 1
#define RACE RACE_UNDEAD
#define MH_LOW 1659
#define MH_HIGH 2490
#define MH_SPEED 3.6f
#define MH_TYPE WEAPON_2H
#define OH_LOW 1659
#define OH_HIGH 2490
#define OH_SPEED 3.6f
#define OH_TYPE WEAPON_2H
#define TALENT_TIER3 2
#define TALENT_TIER4 1
#define TALENT_TIER6 2
#define TALENT_TIER7 1
#define archmages_incandescence 0
#define archmages_greater_incandescence 0
//#define legendary_ring 2500
#define t17_2pc 0
#define t17_4pc 0
#define t18_2pc 1
#define t18_4pc 1
#define thunderlord_mh 0
#define thunderlord_oh 0
#define bleedinghollow_mh 0
#define bleedinghollow_oh 0
#define shatteredhand_mh 1
#define shatteredhand_oh 1
//#define trinket_vial_of_convulsive_shadows 2033
//#define trinket_forgemasters_insignia 181
//#define trinket_horn_of_screaming_spirits 2652
//#define trinket_scabbard_of_kyanos 2200
//#define trinket_badge_of_victory 1456
//#define trinket_insignia_of_victory 867
//#define trinket_tectus_beating_heart 2304
//#define trinket_formidable_fang 1743
//#define trinket_draenic_stone 1414
//#define trinket_skull_of_war 2120
//#define trinket_mote_of_the_mountain 1517
//#define trinket_worldbreakers_resolve 220
//#define trinket_discordant_chorus 20564
#define trinket_empty_drinking_horn 259
//#define trinket_unending_hunger 54
//#define trinket_spores_of_alacrity 2304
//#define trinket_bonemaws_big_toe 1767
//#define trinket_emberscale_talisman 1767
#define trinket_gronntooth_war_horn 1152
#define ENEMY_IS_DEMONIC 1
#endif /* !defined(__OPENCL_VERSION__) */
/* Debug on Host! */
#if !defined(__OPENCL_VERSION__)
#ifndef _DEBUG
#define _DEBUG
#endif /* _DEBUG */
#endif /* !defined(__OPENCL_VERSION__) */
/* Codes enclosed in 'hostonly' will be discarded at OpenCL devices, and vice versa. */
#if defined(__OPENCL_VERSION__)
#define hostonly(code)
#define deviceonly(code) code
#else
#define hostonly(code) code
#define deviceonly(code)
#endif /* defined(__OPENCL_VERSION__) */
#if defined(_MSC_VER)
#define msvconly(code) code
#else
#define msvconly(code)
#endif /* defined(_MSC_VER) */
/* Macro guard word. */
#define safemacro(v) do{v;}while(0)
/* Std C Library. */
#if !defined(__OPENCL_VERSION__)
int printf( const char* format, ... );
void abort( void );
void* malloc( unsigned long long );
void free( void* );
#define KRNL_STR2(v) #v
#define KRNL_STR(v) KRNL_STR2(v)
#endif /* !defined(__OPENCL_VERSION__) */
/* Diagnostic. */
#if defined(_DEBUG) && !defined(__OPENCL_VERSION__)
#if defined(_MSC_VER)
#define hfuncname __FUNCTION__
#else
#define hfuncname __func__
#endif /* defined(_MSC_VER) */
#define assert(expression) if(!(expression)){ \
printf("Assertion failed: %s, function %s, file %s, line %d.\n", \
KRNL_STR(expression), hfuncname ,__FILE__, __LINE__); \
abort(); }else
#else
#define assert(expression)
#endif /* defined(_DEBUG) && !defined(__OPENCL_VERSION__) */
/* Unified typename. */
#if defined(__OPENCL_VERSION__)
#define kbool bool
#define k8s char
#define k8u uchar
#define k16s short
#define k16u ushort
#define k32s int
#define k32u uint
#define k64s long
#define k64u ulong
#define K64S_C(num) (num##L)
#define K64U_C(num) (num##UL)
#else
#define kbool int
#define k8s signed char
#define k8u unsigned char
#define k16s short int
#define k16u unsigned short int
#define k32s long int
#define k32u unsigned long int
#define k64s long long int
#define k64u unsigned long long int
#define K64S_C(num) (num##LL)
#define K64U_C(num) (num##ULL)
#define convert_ushort_sat(num) ((num) < 0 ? (k16u)0 : (num) > 0xffff ? (k16u)0xffff : (k16u)(num))
#define convert_ushort_rtp(num) ((k16u)(num) + !!((float)(num) - (float)(k16u)(num)))
#define convert_uint_rtp(num) ((k32u)(num) + !!((float)(num) - (float)(k32u)(num)))
#define convert_uint_rtz(num) ((k32u)(num))
float convert_float_rtp( k64u x ) {
union {
k32u u;
float f;
} u;
u.f = ( float )x;
if( ( k64u )u.f < x && x < K64U_C( 0xffffff8000000000 ) )
u.u++;
return u.f;
}
#endif /* defined(__OPENCL_VERSION__) */
/*
Assumed char is exactly 8 bit.
Most std headers will pollute the namespace, thus we do not use CHAR_BIT,
which is defined in <limits.h>.
*/
#define K64U_MSB ( K64U_C( 1 ) << (sizeof(k64u) * 8 - 1) )
/* Unified compile hint. */
#if defined(__OPENCL_VERSION__)
#define kdeclspec(attr) __attribute__((attr))
#define hdeclspec(attr)
#else
#define kdeclspec(attr)
#if defined(_MSC_VER)
#define hdeclspec __declspec
#else
#define hdeclspec(attr) __attribute__((attr))
#endif /* defined(_MSC_VER) */
#endif /* defined(__OPENCL_VERSION__) */
/* get_global_id() on Host. */
#if !defined(__OPENCL_VERSION__)
#if defined(_MSC_VER)
#define htlsvar __declspec(thread)
#else
#define htlsvar __thread
#endif /* defined(_MSC_VER) */
htlsvar int global_idx[3] = {0};
int get_global_id( int dim ) {
return global_idx[dim];
}
void set_global_id( int dim, int idx ) {
global_idx[dim] = idx;
}
#endif /* !defined(__OPENCL_VERSION__) */
/* Const Pi. */
#if !defined(M_PI)
#define M_PI 3.14159265358979323846
#endif /* !defined(M_PI) */
/* Math utilities on Host. */
#if !defined(__OPENCL_VERSION__)
double cos( double x );
#define cospi(x) cos( (double)(x) * M_PI )
double sqrt( double x );
double log( double x );
double clamp( double val, double min, double max ) {
return val < min ? min : val > max ? max : val;
}
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define mix(x, y, a) ((x) + (( (y) - (x) ) * (a)))
#define mad_hi(x,y,c) ((k32u)(((k64u)(x) * (k64u)(y)) >> 32) + (c))
#endif /* !defined(__OPENCL_VERSION__) */
/* Seed struct which holds the current state. */
#if defined(RNG_MT127)
typedef struct {
k32u mt[4]; /* State words. */
k32u mti; /* State counter: must be within [0,3]. */
} seed_t;
#elif defined(RNG_MWC64X)
typedef struct {
k32u x;
k32u c;
} seed_t;
#else
typedef struct {
k32u holdrand;
} seed_t;
#endif
typedef k32u time_t;
#define FROM_SECONDS( sec ) ((time_t)convert_uint_rtp((float)(sec) * 1000.0f))
#define FROM_MILLISECONDS( msec ) ((time_t)(msec))
#define TO_SECONDS( timestamp ) (convert_float_rtp((k32u)timestamp) * 0.001f)
#define TIME_OFFSET( time ) ((time_t)((k32s)rti->timestamp + (k32s)time))
#define TIME_DISTANT( time ) ((time_t)max((k32s)(time) - (k32s)(rti->timestamp), 0))
#define UP( time_to_check ) ( rti->player.time_to_check && rti->player.time_to_check > rti->timestamp )
#define REMAIN( time_to_check ) ((time_t)max(((k32s)rti->player.time_to_check - (k32s)rti->timestamp), 0))
/* Event queue. */
#define EQ_SIZE_EXP (num_enemies < 15 ? 7 : 8)
#define EQ_SIZE ((1 << EQ_SIZE_EXP) - 1)
typedef struct {
time_t time;
k32u target_id;
k32u routine;
} _event_t;
typedef struct {
k32u count;
time_t power_suffice;
_event_t event[EQ_SIZE];
} event_queue_t;
hostonly(
static k32u maxqueuelength = 0;
)
#define RACE_NONE 0
#define RACE_HUMAN 1
#define RACE_DWARF 2
#define RACE_GNOME 3
#define RACE_NIGHTELF_DAY 4
#define RACE_NIGHTELF_NIGHT 5
#define RACE_DRAENEI 6
#define RACE_WORGEN 7
#define RACE_ORC 8
#define RACE_TROLL 9
#define RACE_TAUREN 10
#define RACE_UNDEAD 11
#define RACE_BLOODELF 12
#define RACE_GOBLIN 13
#define RACE_PANDAREN 14
deviceonly( __constant ) k32s racial_base_str[] = {
0, 0, 5, -5, -4, -4, 66, 3, 3, 1, 5, -1, -3, -3, 0,
};
/* Declarations from class modules. */
typedef struct {
time_t cd;
} ICD_t;
typedef struct {
time_t lasttimeattemps;
time_t lasttimeprocs;
} RPPM_t;
typedef struct {
time_t cd;
} bloodthirst_t;
typedef struct {
k32u stack;
time_t expire;
} ragingblow_t;
typedef struct {
k32u stack;
time_t expire;
} meatcleaver_t;
typedef struct {
time_t expire;
} ragingwind_t;
typedef struct {
time_t expire;
} enrage_t;
typedef struct {
k32u stack;
time_t expire;
} bloodsurge_t;
typedef struct {
time_t expire;
RPPM_t proc;
} suddendeath_t;
typedef struct {
time_t cd;
} berserkerrage_t;
typedef struct {
time_t cd;
time_t expire;
} recklessness_t;
typedef struct {
time_t cd;
} stormbolt_t;
typedef struct {
time_t cd;
} shockwave_t;
typedef struct {
time_t cd;
} dragonroar_t;
typedef struct {
time_t cd;
time_t expire;
} avatar_t;
typedef struct {
time_t cd;
time_t expire;
} bloodbath_t;
typedef struct {
float pool;
float ticks;
time_t dot_start;
} bloodbath_dot_t;
typedef struct {
time_t cd;
time_t expire;
} bladestorm_t;
typedef struct {
time_t cd;
time_t expire;
} ravager_t;
typedef struct {
time_t cd;
} siegebreaker_t;
typedef struct {
time_t expire;
time_t cd;
} potion_t;
typedef struct {
time_t expire;
k32u stack;
} rampage_t;
typedef struct {
time_t expire;
RPPM_t proc;
} incandescence_t;
typedef struct {
time_t expire;
time_t cd;
float dmg_snapshot;
} thorasus_the_stone_heart_of_draenor_t;
typedef struct {
time_t expire;
k32u extend;
RPPM_t proc;
} thunderlord_t;
typedef struct {
time_t expire;
RPPM_t proc;
} bleedinghollow_t;
typedef struct {
RPPM_t proc;
} shatteredhand_t;
typedef struct {
time_t expire;
float ticks;
} shatteredhand_dot_t;
typedef struct {
time_t cd;
} arcanetorrent_t;
typedef struct {
time_t cd;
time_t expire;
} berserking_t;
typedef struct {
time_t cd;
time_t expire;
} bloodfury_t;
typedef struct weapon_t {
float speed;
k32u low;
k32u high;
k32u type;
} weapon_t;
#define WEAPON_2H 0
#define WEAPON_1H 1
#define WEAPON_DAGGER 2
deviceonly( __constant ) weapon_t weapon[] = {
{
MH_SPEED,
MH_LOW,
MH_HIGH,
MH_TYPE,
},
{
OH_SPEED,
OH_LOW,
OH_HIGH,
OH_TYPE,
},
};
deviceonly( __constant ) float normalized_weapon_speed[] = {
3.3f,
2.4f,
1.7f,
};
typedef struct stat_t {
k32u gear_str;
k32u gear_crit;
k32u gear_haste;
k32u gear_mastery;
k32u gear_mult;
k32u gear_vers;
float crit;
float haste;
float mastery;
float mult;
float vers;
k32u str;
k32u ap;
} stat_t;
/* Player struct, filled by the class module. */
typedef struct {
float power;
#if (passive_power_regen)
float power_regen;
#endif
stat_t stat;
//#if (TALENT_TIER3 != 3)
bloodthirst_t bloodthirst;
//#endif
ragingblow_t ragingblow;
enrage_t enrage;
bloodsurge_t bloodsurge;
berserkerrage_t berserkerrage;
recklessness_t recklessness;
meatcleaver_t meatcleaver;
#if (GLYPH_OF_RAGINGWIND)
ragingwind_t ragingwind;
#endif
#if (TALENT_TIER3 == 2)
suddendeath_t suddendeath;
#endif
#if (TALENT_TIER4 == 1)
stormbolt_t stormbolt;
#endif
#if (TALENT_TIER4 == 2)
shockwave_t shockwave;
#endif
#if (TALENT_TIER4 == 3)
dragonroar_t dragonroar;
#endif
#if (TALENT_TIER6 == 1)
avatar_t avatar;
#endif
#if (TALENT_TIER6 == 2)
bloodbath_t bloodbath;
#endif
#if (TALENT_TIER6 == 3)
bladestorm_t bladestorm;
#endif
#if (TALENT_TIER7 == 2)
ravager_t ravager;
#endif
#if (TALENT_TIER7 == 3)
siegebreaker_t siegebreaker;
#endif
#if (BUFF_POTION == 1)
potion_t potion;
#endif
#if (t17_4pc)
rampage_t rampage;
#endif
#if (archmages_incandescence || archmages_greater_incandescence)
incandescence_t incandescence;
#endif
#if defined(legendary_ring)
thorasus_the_stone_heart_of_draenor_t thorasus_the_stone_heart_of_draenor;
#endif
#if (thunderlord_mh)
thunderlord_t enchant_mh;
#endif
#if (thunderlord_oh)
thunderlord_t enchant_oh;
#endif
#if (bleedinghollow_mh)
bleedinghollow_t enchant_mh;
#endif
#if (bleedinghollow_oh)
bleedinghollow_t enchant_oh;
#endif
#if (shatteredhand_mh)
shatteredhand_t enchant_mh;
#endif
#if (shatteredhand_oh)
shatteredhand_t enchant_oh;
#endif
#if (RACE == RACE_BLOODELF)
arcanetorrent_t arcanetorrent;
#endif
#if (RACE == RACE_TROLL)
berserking_t berserking;
#endif
#if (RACE == RACE_ORC)
bloodfury_t bloodfury;
#endif
#if (RACE == RACE_UNDEAD)
ICD_t touch_of_the_grave;
#endif
#if defined(trinket_vial_of_convulsive_shadows)
struct {
time_t cd;
time_t expire;
} vial_of_convulsive_shadows;
#endif
#if defined(trinket_forgemasters_insignia)
struct {
time_t expire;
k32u stack;
RPPM_t proc;
} forgemasters_insignia;
#endif
#if defined(trinket_horn_of_screaming_spirits)
struct {
time_t expire;
RPPM_t proc;
} horn_of_screaming_spirits;
#endif
#if defined(trinket_scabbard_of_kyanos)
struct {
time_t cd;
time_t expire;
} scabbard_of_kyanos;
#endif
#if defined(trinket_badge_of_victory)
struct {
time_t cd;
time_t expire;
} badge_of_victory;
#endif
#if defined(trinket_insignia_of_victory)
struct {
time_t expire;
ICD_t proc;
} insignia_of_victory;
#endif
#if defined(trinket_tectus_beating_heart)
struct {
time_t expire;
RPPM_t proc;
} tectus_beating_heart;
#endif
#if defined(trinket_formidable_fang)
struct {
time_t expire;
RPPM_t proc;
} formidable_fang;
#endif
#if defined(trinket_draenic_stone)
struct {
time_t expire;
ICD_t proc;
} draenic_stone;
#endif
#if defined(trinket_skull_of_war)
struct {
time_t expire;
ICD_t proc;
} skull_of_war;
#endif
#if defined(trinket_mote_of_the_mountain)
struct {
time_t expire;
RPPM_t proc;
} mote_of_the_mountain;
#endif
#if defined(trinket_worldbreakers_resolve)
struct {
time_t expire;
k32u stack;
k32u target;
} worldbreakers_resolve;
#endif
#if defined(trinket_discordant_chorus)
RPPM_t discordant_chorus;
#endif
#if defined(trinket_unending_hunger)
struct {
time_t expire;
k32u stack;
RPPM_t proc;
} unending_hunger;
#endif
#if defined(trinket_spores_of_alacrity)
struct {
time_t expire;
RPPM_t proc;
} spores_of_alacrity;
#endif
#if defined(trinket_bonemaws_big_toe)
struct {
time_t cd;
time_t expire;
} bonemaws_big_toe;
#endif
#if defined(trinket_emberscale_talisman)
struct {
time_t cd;
time_t expire;
} emberscale_talisman;
#endif
#if defined(trinket_gronntooth_war_horn)
struct {
time_t expire;
RPPM_t proc;
} gronntooth_war_horn;
#endif
time_t gcd;
k32u target;
}
player_t;
typedef struct {
k32u id;
#if (TALENT_TIER6 == 2)
bloodbath_dot_t bloodbath;
#endif
#if (shatteredhand_mh)
shatteredhand_dot_t enchant_mh;
#endif
#if (shatteredhand_oh)
shatteredhand_dot_t enchant_oh;
#endif
#if defined(trinket_empty_drinking_horn)
struct {
time_t expire;
k32u stack;
} empty_drinking_horn;
#endif
} enemy_t;
/* Runtime info struct, each thread preserves its own. */
typedef struct {
time_t timestamp;
seed_t seed;
event_queue_t eq;
float damage_collected;
player_t player;
enemy_t enemy[num_enemies];
time_t expected_combat_length;
}
rtinfo_t;
/* Formated time print. */
hostonly(
void tprint( rtinfo_t* rti ) {
printf( "%02d:%02d.%03d ", rti->timestamp / 60000, ( rti->timestamp % 60000 ) / 1000, ( ( rti->timestamp % 60000 ) % 1000 ) );
}
)
#if defined(SHOW_LOG)
#define lprintf(v) hostonly(safemacro({tprint(rti);printf v;printf("\n");}))
#else
#define lprintf(v)
#endif /* defined(SHOW_LOG) */
/* Declaration Action Priority List (APL) */
void scan_apl( rtinfo_t* rti ); /* Implement is generated by front-end. */
/*
Event routine entries. Each class module implement its own.
Each type of event should be assigned to a routine number.
Given routine number 'routine_entries' select the corresponding function to call.
*/
void routine_entries( rtinfo_t* rti, _event_t e );
/** Routine number 0xFF indicates the end of simulation. */
#define EVENT_END_SIMULATION ((k32u)-1)
/*
Class modules may need an initializer, link it here.
*/
void module_init( rtinfo_t* rti );
/* Initialize RNG */
#if defined(RNG_MT127)
void rng_init( rtinfo_t* rti, k32u seed ) {
rti->seed.mti = 0; /* Reset counter */
/* Use a LCG to fill state matrix. See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
rti->seed.mt[0] = seed & 0xffffffffUL;
seed = ( 1812433253UL * ( seed ^ ( seed >> 30 ) ) ) + 1;
rti->seed.mt[1] = seed & 0xffffffffUL;
seed = ( 1812433253UL * ( seed ^ ( seed >> 30 ) ) ) + 2;
rti->seed.mt[2] = seed & 0xffffffffUL;
/* Due to multiple run for same kernel, set thread id into state words to avoid seed overlapping. */
seed = ( k32u )get_global_id( 0 );
seed = ( 1812433253UL * ( seed ^ ( seed >> 30 ) ) ) + 3;
rti->seed.mt[3] = seed & 0xffffffffUL;
}
#elif defined(RNG_MWC64X)
void rng_init( rtinfo_t* rti, k32u seed ) {
rti->seed.x = seed;
/* Due to multiple run for same kernel, set thread id into state words to avoid seed overlapping. */
rti->seed.c = ( k32u )get_global_id( 0 );
}
#else
void rng_init( rtinfo_t* rti, k32u seed ) {
seed = ( 1812433253UL * ( seed ^ ( seed >> 30 ) ) ) + 1;
/* Due to multiple run for same kernel, set thread id into state words to avoid seed overlapping. */
rti->seed.holdrand = seed + ( k32u )get_global_id( 0 );
}
#endif
/* Generate one IEEE-754 single precision float point uniformally distributed in the interval [.0f, 1.0f). */
#if defined(RNG_MT127)
/*
Minimalist Mersenne Twister for OpenCL
MT by Makoto Matsumoto, See http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
*/
float uni_rng( rtinfo_t* rti ) {
k32u y; /* Should be a register. */
assert( rti->seed.mti < 4 ); /* If not, RNG is uninitialized. */
/* Concat lower-right and upper-left state bits. */
y = ( rti->seed.mt[rti->seed.mti] & 0xfffffffeU ) | ( rti->seed.mt[( rti->seed.mti + 1 ) & 3] & 0x00000001U );
/* Compute next state with the recurring equation. */
y = rti->seed.mt[rti->seed.mti] = rti->seed.mt[( rti->seed.mti + 2 ) & 3] ^ ( y >> 1 ) ^ ( 0xfa375374U & -( k32s )( y & 0x1U ) );
/* Increase the counter */
rti->seed.mti = ( rti->seed.mti + 1 ) & 3;
/* Tempering */
y ^= ( y >> 12 );
y ^= ( y << 7 ) & 0x33555f00U;
y ^= ( y << 15 ) & 0xf5f78000U;
y ^= ( y >> 18 );
/* Mask to IEEE-754 format [1.0f, 2.0f). */
y = ( ( y & 0x3fffffffU ) | 0x3f800000U );
return ( *( float* )&y ) - 1.0f; /* Decrease to [.0f, 1.0f). */
}
#elif defined(RNG_MWC64X)
/*
Part of MWC64X by David Thomas, dt10@imperial.ac.uk
This is provided under BSD, full license is with the main package.
See http://www.doc.ic.ac.uk/~dt10/research
*/
float uni_rng( rtinfo_t* rti ) {
k32u Xn = 4294883355U * rti->seed.x + rti->seed.c;
k32u Cn = mad_hi( 4294883355U, rti->seed.x, ( k32u )( Xn < rti->seed.c ) );
rti->seed.x = Xn;
rti->seed.c = Cn;
k32u y = Xn ^ Cn;
/* Mask to IEEE-754 format [1.0f, 2.0f). */
y = ( ( y & 0x3fffffffU ) | 0x3f800000U );
return ( *( float* )&y ) - 1.0f; /* Decrease to [.0f, 1.0f). */
}
#else
/*
Classic 32-bit LCG known as "rand".
*/
float uni_rng( rtinfo_t* rti ) {
rti->seed.holdrand = rti->seed.holdrand * 214013 + 2531011;
k32u y = rti->seed.holdrand;
/* Mask to IEEE-754 format [1.0f, 2.0f). */
y = ( ( y & 0x3fffffffU ) | 0x3f800000U );
return ( *( float* )&y ) - 1.0f; /* Decrease to [.0f, 1.0f). */
}
#endif
/* Generate one IEEE-754 single precision float point with standard normal distribution. */
float stdnor_rng( rtinfo_t* rti ) {
/*
The max number generated by uni_rng() should equal to:
as_float( 0x3fffffff ) - 1.0f => as_float( 0x3f7ffffe )
Thus, we want transform the interval to (.0f, 1.0f], we should add:
1.0f - as_float( 0x3f7ffffe ) => as_float( 0x34000000 )
Which is representable by decimal 1.1920929E-7.
With a minimal value 1.1920929E-7, the max vaule stdnor_rng could give is approximately 5.64666.
*/
return ( float )( sqrt( -2.0f * log( uni_rng( rti ) + 1.1920929E-7 ) ) * cospi( 2.0f * uni_rng( rti ) ) );
/*
To get another individual normally distributed number in pair, replace 'cospi' to 'sinpi'.
It's simply thrown away here, because of diverge penalty.
With only one thread in a warp need to recalculate, the whole warp must diverge.
*/
}
/* Enqueue an event into EQ. */
_event_t* eq_enqueue( rtinfo_t* rti, time_t trigger, k32u routine, k32u target_id ) {
k32u i = ++( rti->eq.count );
_event_t* p = &( rti->eq.event[-1] );
assert( rti->eq.count <= EQ_SIZE ); /* Full check. */
/* Count max queue length on debug. */
hostonly(
maxqueuelength = max( maxqueuelength, rti->eq.count );
)
/*
There are two circumstances which could cause the assert below fail:
1. Devs got something wrong in the class module, enqueued an event happens before 'now'.
2. Time register is about to overflow, the triggering delay + current timestamp have exceeded the max representable time.
Since the later circumstance is not a fault, we would just throw the event away and continue quietly.
When you are exceeding the max time limits, all new events will be thrown, and finally you will get an empty EQ,
then the empty checks on EQ will fail.
*/
if ( rti->timestamp <= trigger ) {
for ( ; i > 1 && p[i >> 1].time > trigger; i >>= 1 )
p[i] = p[i >> 1];
p[i] = ( _event_t ) {
.time = trigger, .routine = routine, .target_id = target_id
};
return &p[i];
}
return 0;
}
/* Enqueue a power suffice event into EQ. */
void eq_enqueue_ps( rtinfo_t* rti, time_t trigger ) {
if ( trigger >= rti->timestamp )
if ( !rti->eq.power_suffice || rti->eq.power_suffice > trigger )
rti->eq.power_suffice = trigger;
}
/* Power gain. */
void power_gain( rtinfo_t* rti, float power ) {
rti->player.power = min( power_max, rti->player.power + power );
}
/* Power check. */
kbool power_check( rtinfo_t* rti, float cost ) {
if ( cost <= rti->player.power ) return 1;
#if (passive_power_regen)
if ( rti->player.power_regen > 0 )
eq_enqueue_ps( rti, TIME_OFFSET( FROM_SECONDS( ( cost - rti->player.power ) / rti->player.power_regen ) ) );
#endif
return 0;
}
#if (TALENT_TIER7 == 1)
void anger_management_count( rtinfo_t* rti, float rage );
#endif
/* Power consume. */
void power_consume( rtinfo_t* rti, float cost ) {
assert( power_check( rti, cost ) ); /* Power should suffice. */
rti->player.power -= cost;
#if (TALENT_TIER7 == 1)
anger_management_count( rti, cost );
#endif
}
/* Execute the top priority. */
int eq_execute( rtinfo_t* rti ) {
k32u i, child;
_event_t min, last;
_event_t* p = &rti->eq.event[-1];
assert( rti->eq.count ); /* Empty check. */
assert( rti->eq.count <= EQ_SIZE ); /* Not zero but negative? */
assert( rti->timestamp <= p[1].time ); /* Time won't go back. */
assert( !rti->eq.power_suffice || rti->timestamp <= rti->eq.power_suffice ); /* Time won't go back. */
///* If time jumps over 1 second, insert a check point (as a power suffice event). */
//if ( FROM_SECONDS( 1 ) < TIME_DISTANT( p[1].time ) &&
// ( !rti->eq.power_suffice || FROM_SECONDS( 1 ) < TIME_DISTANT( rti->eq.power_suffice ) ) )
// rti->eq.power_suffice = TIME_OFFSET( FROM_SECONDS( 1 ) );
/* When time elapse, trigger a full scanning at APL. */
if ( rti->timestamp < p[1].time
&& ( !rti->eq.power_suffice || rti->timestamp <= rti->eq.power_suffice )
) {
if ( !STRICT_GCD || !UP( gcd ) ) /* Strict GCD: Do not scal APL if GCD is up. */
scan_apl( rti ); /* This may change p[1]. */
/* Check again. */
assert( rti->eq.count );
assert( rti->eq.count <= EQ_SIZE );
assert( rti->timestamp <= p[1].time );
assert( !rti->eq.power_suffice || rti->timestamp <= rti->eq.power_suffice );
}
min = p[1];
if ( !rti->eq.power_suffice || rti->eq.power_suffice >= min.time ) {
/* Delete from heap. */
last = p[rti->eq.count--];
for( i = 1; i << 1 <= rti->eq.count; i = child ) {
child = i << 1;
if ( child != rti->eq.count && rti->eq.event[child].time < p[child].time )
child++;
if ( last.time > p[child].time )
p[i] = p[child];
else
break;
}
p[i] = last;
/* Now 'min' contains the top priority. Execute it. */
#if ( passive_power_regen )
power_gain( rti, TO_SECONDS( min.time - rti->timestamp ) * rti->player.power_regen );
#endif
rti->timestamp = min.time;
if ( min.routine == EVENT_END_SIMULATION ) /* Finish the simulation here. */
return 0;
/* TODO: Some preparations? */
routine_entries( rti, min );
/* TODO: Some finishing works? */
} else {
/* Invoke power suffice routine. */
#if ( passive_power_regen )
power_gain( rti, TO_SECONDS( rti->eq.power_suffice - rti->timestamp ) * rti->player.power_regen );
#endif
rti->timestamp = rti->eq.power_suffice;
rti->eq.power_suffice = 0;
/*
Power suffices would not make any impact, just a reserved APL scanning.
The scanning will occur when timestamp elapses next time, not immediately.
*/
}
return 1;
}
float enemy_health_percent( rtinfo_t* rti ) {
/*
What differs from SimulationCraft, OpenCL iterations are totally parallelized.
It's impossible to determine mob initial health by the results from previous iterations.
The best solution here is to use a linear mix of time to approximate the health precent,
which is used in SimC for the very first iteration.
*/
time_t remainder = max( ( k32u )FROM_SECONDS( 0 ), ( k32u )( rti->expected_combat_length - rti->timestamp ) );
return mix( death_pct, initial_health_percentage, ( float )remainder / ( float )rti->expected_combat_length );
}
void proc_ICD( rtinfo_t* rti, ICD_t* state, float chance, time_t cooldown, k32u routnum, k32u target_id ) {
if ( ( !state->cd || state->cd <= rti->timestamp ) && uni_rng( rti ) < chance ) {
state->cd = TIME_OFFSET( cooldown );
eq_enqueue( rti, rti->timestamp, routnum, target_id );
}
}
void proc_PPM( rtinfo_t* rti, float PPM, weapon_t* weapon, k32u routnum, k32u target_id ) {
if ( uni_rng( rti ) < ( PPM * weapon->speed / 60.0f ) ) {
eq_enqueue( rti, rti->timestamp, routnum, target_id );
}