-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgwbbcode.inc.php
2334 lines (2086 loc) · 68.5 KB
/
gwbbcode.inc.php
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
<?php
/***************************************************************************
* gwbbcode.inc.php
* -------------------
* begin : Tuesday, Apr 21, 2005
* copyright : (C) 2006-2007 Pierre 'pikiou' Scelles
* email : [email protected]
*
***************************************************************************/
/***************************************************************************
*
* This program 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; either version 2 of the License, or
* (at your option) any later version.
*
* All images, skill names and descriptions are (C) ArenaNet.
***************************************************************************/
require_once GWBBCODE_ROOT . '/constants.inc.php';
/***************************************************************************
* GLOBALS
***************************************************************************/
// Load the gwbbcode smarty syntax template (*.TPL)
global $gwbbcode_tpl;
if ( !isset( $gwbbcode_tpl ) ) {
$gwbbcode_tpl = load_gwbbcode_smarty_template();
}
// Load the pvp to pve id conversion array
// Ingame templates must not contain pvp skill ids to be valid.
global $pvp_to_pve_skill_list;
if ( !file_exists( SKILLIDSPVP_PATH ) ) {
die( "Missing pvp skill id database." );
}
$pvp_to_pve_skill_list = load( SKILLIDSPVP_PATH );
/***************************************************************************
* MAIN FUNCTION
***************************************************************************/
/**
* Prepares the page and then replaces gwBBCode with HTML
* This function is directly called by: extension\classes\PvXCode.php
* @param $text
* @param bool $build_name
* @return array|string|string[]|null
*/
function parseGwbbcode( $text, $build_name = false ) {
// Timer for the gwBBCode parse duration
$start = microtime( true );
// 1. Add a build name if not specified. Default to the name provided by PvXCode.php.
// This is used in the template download link as the file title.
if ( !empty( $build_name ) && !preg_match( '#(\[build[^\]]*?) name="[^\]"]+"#isS', $text ) ) {
$text = preg_replace( '#(\[build )#is', "\\1name=\"$build_name\" ", $text );
}
// 2: Replace all '['s inside [pre]..[/pre], or [nobb]..[/nobb] by '['
$text = preg_replace_callback( '#\[pre\](.*?)\[\/pre\]#isS', 'pre_replace', $text );
$text = preg_replace_callback( '#\[nobb\](.*?)\[\/nobb\]#isS', 'pre_replace', $text );
// 3: Replace all [Random Skill] by [some random skill name] each time the post is rendered
$text = preg_replace_callback( '#\[Random Skill(.*?)\]#is', 'random_skill_replace', $text );
// 4: [rand seed=465468 players=2]
$text = preg_replace_callback( '#\[rand([^\]]*)\]#isS', 'rand_replace', $text );
// 5: Manage [build=...], or [build_name;template_code]
$text = preg_replace_callback( '#\[build=([^\]]*)\]\]?(\[/build\])?\r?\n?#isS', 'build_id_replace', $text );
$text = preg_replace_callback( '#\[(([^]\r\n]+)(;)([^];\r\n]+))\]\r?\n?#isS', 'build_id_replace', $text );
// 6: Replace all [skill_name] by [skill]skill_name[/skill] if skill_name is a valid skill name
$text = preg_replace_callback( '#\[(.+)\]#isSU', 'skill_name_replace', $text );
// 7: Manage [build]...[/build]
$text = preg_replace_callback( '#\[build ([^\]]*)\](.*?)\[/build\]\r?\n?#isS', 'build_replace', $text );
// 8: Replace all [skillset=attribute_name@attr_level]
$text = preg_replace_callback( '#\[(\[?)skillset=(.*?)\]#isS', 'skillset_replace', $text );
// 9: Manage [skill]...[/skill]
$text = preg_replace_callback( '#\[skill([^\]]*)\](.*?)\[/skill\]#isS', 'skill_replace', $text );
// 10: Manage [gwbbcode version]
$text = preg_replace( '@\[gwbbcode version\]@i', GWBBCODE_VERSION, $text );
// 11: Manage [gwbbcode runtime]
if ( preg_match( '@\[gwbbcode runtime\]@i', $text ) !== false ) {
$text =
preg_replace(
'@\[gwbbcode runtime\]@i',
'Runtime = ' . round( microtime( true ) - $start, 3 ) . ' seconds',
$text
);
}
return $text;
}
/***************************************************************************
* HELPER REPLACEMENT FUNCTIONS
***************************************************************************/
// Replacement function 1:
// Changes any '[' by '[', hence disabling bbcode
function pre_replace( $reg ) {
[ $all, $content ] = $reg;
return str_replace( '[', '[', $content );
}
// Replacement function 2:
// Convert [Random Skill] tags to [a random skill name]
function random_skill_replace( $reg ) {
[ $all, $att ] = $reg;
$skill_list = gws_skill_id_list();
$random_skill_name = array_rand( $skill_list );
$details = gws_details( $skill_list[$random_skill_name] );
return '[' . $details['name'] . "$att]";
}
// Replacement function 3:
// Process [rand seed=465468 players=2]
function rand_replace( $reg ) {
[ $all, $att ] = $reg;
$att = html_safe_decode( $att );
// Get the seed
if ( preg_match( '|seed=([0-9]+)|', $att, $reg ) ) {
$seed = intval( $reg[1] );
} else {
$seed = mt_rand( 1000, 100000 );
}
mt_srand( $seed );
// Get the number of players
if ( preg_match( '|players=([0-9]+)|', $att, $reg ) ) {
$players = intval( $reg[1] );
} else {
$players = 1;
}
// Get PvP ids so we can ignore them
static $pvpSkillIds = [];
if ( !file_exists( SKILLIDSPVP_PATH ) ) {
die( "Missing pvp skill id database." );
}
$pvpSkillIds = load( SKILLIDSPVP_PATH );
// Generate random skills
// Note: Can't use array_filter then array_rand, because array_rand uses an algorithm that doesn't respect
// setting the random seed with mt_srand().
$skills = [];
$used_ids = [];
// Initialise variables
$normal = 0;
$elite = 0;
$total = 0;
$max_normal = ( $players + 1 ) * 14;
$max_elite = ( $players + 1 ) * 3;
$max_total = $max_elite + $max_normal;
// Reject if the limits are excessive.
if ( $max_total > 200 ) {
return "Selected input parameters to [rand ...] requested too many skills ($max_total skills requested).";
}
// Initialise
while ( $total < $max_total ) {
// 2423 is the id of the highest non-pvp only skill.
$id = mt_rand( 0, 2423 );
$skill = gws_details( $id );
// Check skill details exist, that it is not a pvp skill id, and that the id hasn't been stored before
if ( $skill && !isset( $pvpSkillIds[$id] ) && !in_array( $id, $used_ids ) ) {
// Skill exists!
if ( $skill['elite'] == 1 && $elite < $max_elite ) {
$elite++;
$total++;
$skills[] = $skill;
$used_ids[] = $id;
} else {
if ( $skill['elite'] == 0 && $normal < $max_normal ) {
$normal++;
$total++;
$skills[] = $skill;
$used_ids[] = $id;
}
}
}
}
// Sort and output the list of skills
uasort( $skills, "skill_sort_cmp" );
$skill_list = '';
foreach ( $skills as $skill ) {
$skill_list .= '[' . $skill['name'] . ']';
}
return "Random skill set (seed=$seed):\n$skill_list";
}
// Replacement function 4:
// Process the [build=id] element
function build_id_replace( $reg ) {
[ $all, $id ] = $reg;
$new_code = template_to_gwbbcode( $id );
return ( strpos( $new_code, '[' ) === 0 ) ? $new_code : $all;
}
// Replacement function 5:
// Convert [Potential Skill Name] tags to [skill]Potential Skill Name[/skill]
function skill_name_replace( $reg ) {
[ $all, $name ] = $reg;
$name = html_safe_decode( $name );
// '[[Skill Name]' => no icon
if ( $name[0] == '[' ) {
$noicon = true;
$name = substr( $name, 1 );
} else {
$noicon = false;
}
// "name@attr_value|shown_name" -> $attr_val, $shown_name and $name
if ( preg_match( '/@([^\]:\|]+)/', $name, $reg ) ) {
$attr_val = preg_replace( '@[^0-9+-]@', '', $reg[1] );
}
if ( preg_match( '/\|([^\]@:]+)/', $name, $reg ) ) {
// Play it safe
$shown_name = html_safe_decode( $reg[1] );
}
if ( preg_match( '/^([^@\]:\|]+)/', $name, $reg ) ) {
$name = $reg[1];
}
$id = gws_skill_id( $name );
if ( $id !== false ) {
// Handle [shock@8]
$attr = '';
if ( isset( $attr_val ) ) {
$skill = gws_details( $id );
$attr = gws_attribute_name( $skill['attribute'] );
$attr = " $attr=$attr_val";
}
// Handle [shock|a knockdown]
$show = '';
if ( !empty( $shown_name ) ) {
$show = " show=\"$shown_name\"";
$noicon = true;
}
// Handle the difference between [[shock] and [shock]
if ( $noicon ) {
// PHP note: Use of double quotes replaces variables inside with their values
// PHP note: Use of single quotes shows the variable names like JS would.
return "[skill noicon$attr$show]" . $name . '[/skill]';
} else {
return "[skill$attr]" . $name . '[/skill]';
}
} else {
return $all;
}
}
// Replacement function 6:
// Process the [build] element
function build_replace( $reg ) {
global $gwbbcode_tpl;
$gwbbcode_images_folder_url = GWBBCODE_IMAGES_FOLDER_URL;
$pvx_wiki_page_url = PVX_WIKI_PAGE_URL;
$gw_wiki_page_url = GW_WIKI_PAGE_URL;
[ $all, $att, $skills ] = $reg;
$att = str_replace( "\n", "<br/>\n", html_safe_decode( $att ) );
$attr_list_raw = attribute_list_raw( $att );
$attr_list = attribute_list( $att );
$load = mt_rand();
// Professions
$prof = gws_build_profession( $att );
$cursor = 'help';
if ( $prof !== false ) {
$primary = gws_prof_name( $prof['primary'] );
$secondary = gws_prof_name( $prof['secondary'] );
if ( $secondary == $primary ) {
$secondary = 'No profession';
}
// If the secondary profession is specified, remove impossible secondary profession primary attributes,
// or set them to 0 for skill description coherence
if ( $secondary != 'No profession' ) {
unset( $attr_list_raw[gws_main_attribute( $secondary )] );
$att .= ' ' . gws_attribute_name( gws_main_attribute( $secondary ) ) . '=0';
}
} else {
$primary = 'No profession';
$secondary = 'No profession';
}
// After validating profession names, use "Any" as the display name rather than "No profession" if not specified
$primary_display_name = $primary;
$secondary_display_name = $secondary;
if ( $primary_display_name == 'No profession' ) {
$primary_display_name = 'Any';
}
if ( $secondary_display_name == 'No profession' ) {
$secondary_display_name = 'Any';
}
// Attributes
$attributes = '';
foreach ( $attr_list_raw as $attribute_name => $attribute_value ) {
unset( $matches );
$attr = $gwbbcode_tpl['attribute'];
preg_match_all( "#\{(.*?)\}#is", $attr, $matches );
foreach ( $matches[0] as $r => $find ) {
$replace = ${$matches[1][$r]};
$attr = str_replace( $find, $replace, $attr );
}
$attributes .= $attr;
}
$attributes = preg_replace( '/\s*\\+\s*/', ' + ', $attributes );
$skills = str_replace( '[skill', '[skill ' . $att, $skills );
// Build description
$desc = preg_match( '|desc=\\"([^"]+)\\"|', $att, $reg ) ? $reg[1] : '';
$desc = empty( $desc ) ? '' : ( $desc . '<br/>' );
$desc =
str_replace(
'{br}',
'<br/>',
str_replace( '{br/}', '<br/>', str_replace( '{BR}', '<br/>', str_replace( '{BR/}', '<br/>', $desc ) ) )
);
// Primary attribute effect on build
if ( !empty( $attr_list['Strength'] ) ) {
$desc .= '<span class="expert">Your attack skills gain <b>' . $attr_list['Strength'] .
'%</b> armor penetration.</span><br/>';
} else {
if ( !empty( $attr_list['Expertise'] ) ) {
$desc .= '<span class="expert">The energy cost of attack skills, rituals, touch skills and all Ranger skills is reduced by <b>' .
( 4 * $attr_list['Expertise'] ) . '%</b>.</span><br/>';
} else {
if ( !empty( $attr_list['Divine Favor'] ) ) {
$desc .= '<span class="expert">Your allies are healed for ' .
round( 3.2 * $attr_list['Divine Favor'] ) .
' Health whenever you cast Monk spells on them.</span><br/>';
} else {
if ( !empty( $attr_list['Soul Reaping'] ) ) {
$desc .= '<span class="expert">Gain <b>' . $attr_list['Soul Reaping'] .
'</b> Energy whenever a non-Spirit creature near you dies, up to 3 times every 15 seconds.</span><br/>';
} else {
if ( !empty( $attr_list['Fast Casting'] ) ) {
$desc .= '<span class="expert">You activate Spells and Signets <b>' .
( 100 - floor( pow( 0.955, $attr_list['Fast Casting'] ) * 100 ) ) .
'%</b> faster. (No effect for non-Mesmer skills with a cast time less than 2 seconds.)</span><br/>';
$desc .= '<span class="expert">In PvE, the recharge time of your Mesmer Spells is reduced by <b>' .
( 3 * $attr_list['Fast Casting'] ) . '%</b>.</span><br/>';
} else {
if ( !empty( $attr_list['Energy Storage'] ) ) {
$desc .= '<span class="expert">Your maximum Energy is raised by <b>' .
( 3 * $attr_list['Energy Storage'] ) . '</b>.</span><br/>';
} else {
if ( !empty( $attr_list['Critical Strikes'] ) ) {
$desc .= '<span class="expert">You have an additional <b>' .
$attr_list['Critical Strikes'] .
'</b>% chance to critical hit. Whenever you critical hit, you get <b>' .
round( $attr_list['Critical Strikes'] / 5 ) . '</b> Energy.</span><br/>';
} else {
if ( !empty( $attr_list['Spawning Power'] ) ) {
$desc .= '<span class="expert">Creatures you create have <b>' .
( 4 * $attr_list['Spawning Power'] ) .
'%</b> more Health, and weapon spells you cast last <b>' .
( 4 * $attr_list['Spawning Power'] ) . '%</b> longer.</span><br/>';
} else {
if ( !empty( $attr_list['Mysticism'] ) ) {
$desc .= '<span class="expert">The energy cost of Dervish enchantments is reduced by <b>' .
( 4 * $attr_list['Mysticism'] ) . '%</b>.</span><br/>';
$desc .= '<span class="expert">In PvE, gain <b>+' . $attr_list['Mysticism'] .
'</b> armor rating while enchanted.</span><br/>';
} else {
if ( !empty( $attr_list['Leadership'] ) ) {
$desc .= '<span class="expert">You gain 2 Energy for each ally affected by one of your Shouts or Chants (maximum <b>' .
floor( $attr_list['Leadership'] / 2 ) .
'</b> Energy).</span><br/>';
}
}
}
}
}
}
}
}
}
}
// Template: professions
$invalid_template = false;
static $prof_ids =
[
'No profession',
'Warrior',
'Ranger',
'Monk',
'Necromancer',
'Mesmer',
'Elementalist',
'Assassin',
'Ritualist',
'Paragon',
'Dervish',
];
$template = '0111000000';
$template .= int2bin( array_search( $primary, $prof_ids ), 4 );
$template .= int2bin( array_search( $secondary, $prof_ids ), 4 );
// Template: attributes
static $attr_ids =
[
'fas',
'ill',
'dom',
'ins',
'blo',
'dea',
'sou',
'cur',
'air',
'ear',
'fir',
'wat',
'ene',
'hea',
'smi',
'pro',
'div',
'str',
'axe',
'ham',
'swo',
'tac',
'bea',
'exp',
'wil',
'mar',
29 => 'dag',
'dead',
'sha',
'com',
'res',
'cha',
'cri',
'spa',
'spe',
'comma',
'mot',
'lea',
'scy',
'win',
'earthp',
'mys',
];
static $prof_attr_list = [
'Air Magic' => 'Elementalist',
'Earth Magic' => 'Elementalist',
'Energy Storage' => 'Elementalist',
'Fire Magic' => 'Elementalist',
'Water Magic' => 'Elementalist',
'Domination Magic' => 'Mesmer',
'Fast Casting' => 'Mesmer',
'Illusion Magic' => 'Mesmer',
'Inspiration Magic' => 'Mesmer',
'Divine Favor' => 'Monk',
'Healing Prayers' => 'Monk',
'Protection Prayers' => 'Monk',
'Smiting Prayers' => 'Monk',
'Blood Magic' => 'Necromancer',
'Curses' => 'Necromancer',
'Death Magic' => 'Necromancer',
'Soul Reaping' => 'Necromancer',
'Beast Mastery' => 'Ranger',
'Expertise' => 'Ranger',
'Marksmanship' => 'Ranger',
'Wilderness Survival' => 'Ranger',
'Axe Mastery' => 'Warrior',
'Hammer Mastery' => 'Warrior',
'Strength' => 'Warrior',
'Swordsmanship' => 'Warrior',
'Tactics' => 'Warrior',
'Critical Strikes' => 'Assassin',
'Dagger Mastery' => 'Assassin',
'Deadly Arts' => 'Assassin',
'Shadow Arts' => 'Assassin',
'Spawning Power' => 'Ritualist',
'Channeling Magic' => 'Ritualist',
'Communing' => 'Ritualist',
'Restoration Magic' => 'Ritualist',
'Spear Mastery' => 'Paragon',
'Command' => 'Paragon',
'Motivation' => 'Paragon',
'Leadership' => 'Paragon',
'Scythe Mastery' => 'Dervish',
'Wind Prayers' => 'Dervish',
'Earth Prayers' => 'Dervish',
'Mysticism' => 'Dervish',
];
// => First attribute is attribute of the highest level
arsort( $attr_list_raw );
// Prepare a base attribute level and rune bonus list for primary and secondary attributes
$attr_primary = [];
$attr_secondary = [];
$attr_runes = [];
$available_helmet = true;
// Ignore secondary profession main attribute
if ( $secondary != 'No profession' ) {
unset( $attr_list_raw[gws_main_attribute( $secondary )] );
}
foreach ( $attr_list_raw as $attr => $raw_level ) {
// Separate base level and bonus
preg_match( '@^([0-9]+)(\\+[+0-9]+)?@', $raw_level, $reg );
$base_level = $reg[1];
$bonus = $reg[2] ?? '0';
// Primary attributes
if ( $prof_attr_list[$attr] == $primary ) {
$bonus_level = array_sum( explode( '+', $bonus ) );
// Invalid attribute level bonus
if ( $bonus_level > 4 || $bonus_level < 0 || ( $bonus_level == 4 && !$available_helmet ) ) {
$invalid_template = "Invalid attribute level bonus";
} else {
// Does attribute level bonus include a helmet?
if ( $available_helmet && ( $bonus_level == 4 || ( substr_count( $bonus, '+' ) > 1 &&
strpos( $bonus, '+1' ) !== false ) ) ) {
$available_helmet = false;
$attr_primary[$attr] = $base_level;
$attr_runes[$attr] = $bonus_level - 1;
} else {
// No helmet, but runes maybe
$attr_primary[$attr] = $base_level;
$attr_runes[$attr] = $bonus_level;
}
}
} else {
// Secondary attributes
if ( $prof_attr_list[$attr] == $secondary ) {
$attr_secondary[$attr] = $base_level;
}
}
}
// Manage primary attribute levels
$points_secondary = attr_points( $attr_secondary );
if ( !empty( $attr_primary ) ) {
// Set helmet if needed
if ( $available_helmet &&
( max( $attr_primary ) > 12 || ( attr_points( $attr_primary ) + $points_secondary > 200 ) ) ) {
// First attribute is attribute of the highest level, the one needing a helmet the most
$attr_primary[key( $attr_primary )]--;
}
// Assign runes
while ( ( max( $attr_primary ) > 12 || ( attr_points( $attr_primary ) + $points_secondary > 200 ) ) &&
min( $attr_runes ) < 3 ) {
arsort( $attr_primary );
foreach ( $attr_runes as $attr => $rune ) {
if ( $rune < 3 ) {
$attr_primary[$attr]--;
$attr_runes[$attr]++;
break;
}
}
}
}
$attr_list_raw = array_merge( $attr_primary, $attr_secondary );
$template .= int2bin( count( $attr_list_raw ), 4 );
if ( !empty( $attr_list_raw ) &&
( max( $attr_list_raw ) > 12 || min( $attr_list_raw ) < 0 || attr_points( $attr_list_raw ) > 200 ) ) {
$invalid_template = "More attribute levels than attribute points can handle";
}
$attr_bit_size = 5;
$template_attrs = [];
foreach ( $attr_list_raw as $attr => $level ) {
$id = array_search( gws_attribute_name( $attr ), $attr_ids );
$template_attrs[$id] = $level;
if ( $id >= 32 ) {
$attr_bit_size = 6;
}
}
$template .= int2bin( $attr_bit_size - 4, 4 );
ksort( $template_attrs );
foreach ( $template_attrs as $id => $level ) {
$template .= int2bin( $id, $attr_bit_size );
$template .= int2bin( $level, 4 );
}
// Template: Skills
$skill_bit_size = 9;
$skill_id_max = pow( 2, $skill_bit_size );
$template_skills = [];
if ( preg_match_all( '#\[skill[^\]]*\](.*?)\[/skill\][ ]?#isS', $skills, $regs, PREG_SET_ORDER ) ) {
foreach ( $regs as $reg ) {
$skill_name = $reg[1];
$id = gws_skill_id( $skill_name );
if ( $id !== false ) {
$skill = gws_details( $id );
if ( ( $skill['profession'] == $primary || $skill['profession'] == $secondary ||
$skill['prof'] == '?' ) && ( array_search( $id, $template_skills ) === false || $id == 0 ) ) {
if ( $id >= $skill_id_max ) {
$skill_bit_size = floor( log( $id, 2 ) ) + 1;
$skill_id_max = pow( 2, $skill_bit_size );
}
} else {
if ( in_array( $id, $template_skills ) ) {
$invalid_template = "Skill $skill_name can't be specified twice in the same build";
} else {
$invalid_template = "Skill $skill_name is not from one of the build professions";
}
}
// Keep the list of skills independently of their build validity
$template_skills[] = $id;
}
}
}
$template .= int2bin( $skill_bit_size - 8, 4 );
if ( count( $template_skills ) > 8 ) {
$invalid_template = "You specified more skills than a build can handle";
} else {
foreach ( $template_skills as $id ) {
$template .= int2bin( getSkillIdPvE( $id ), $skill_bit_size );
}
// Complete with 0s
$template .= str_repeat( str_repeat( '0', $skill_bit_size ), 8 - count( $template_skills ) );
}
// Template: Build name as populated by template_to_gwbbcode()
$build_name = gws_build_name( $att );
// Template: Manage template save link
if ( preg_match( "/ nosave/i", $att ) ) {
$template_html = '';
} else {
if ( $invalid_template === false ) {
// Prepare template bbcode
// Added a 0 since that's what GW does
$template_code = bin_to_template( $template . '0' );
// Prepare template name
$template_name = $build_name;
} else {
// Display error message beneath build template
$template_error_msg = htmlspecialchars( $invalid_template );
}
}
// Replace all "{var_name}" with $var_name until there are none to replace =
// (i.e a tag replacement can contain other tags)
$tpl = $gwbbcode_tpl['build'];
do {
$prev_tpl = $tpl;
// "{{skill_description}}" is replaced by $gwbbcode_tpl['skill_description']
$tpl = preg_replace_callback( "#\{\{(.*?)\}\}#is", static function ( $m ) {
return $gwbbcode_tpl[$m[1]] ?? $m[0];
}, $tpl );
// "{desc}" is replaced by $desc
unset( $matches );
preg_match_all( "#\{(.*?)\}#is", $tpl, $matches );
foreach ( $matches[0] as $r => $find ) {
if ( isset( ${$matches[1][$r]} ) ) {
$replace = ${$matches[1][$r]};
} else {
$replace = '';
}
$tpl = str_replace( $find, $replace, $tpl );
}
} while ( $prev_tpl != $tpl );
// Ensure there is a carriage return after [build][/build] tags so that bullets are always respected
return $tpl . "\r\n";
}
// Replacement function 7:
// Convert [skillset=attribute@attr_level] tags to a list of skills of the appropriate attribute
function skillset_replace( $reg ) {
global $gwbbcode_tpl;
$tpl = $gwbbcode_tpl['skillset'];
[ $all, $noicon, $name ] = $reg;
$name = html_safe_decode( $name );
// '[[...]' => no icon
$noicon = empty( $noicon ) ? '' : ' noicon';
$noicon_comma = empty( $noicon ) ? '' : ', ';
// "name@attr_value" -> $name and $attr_val
$attr = '';
if ( preg_match( '|([^@\]]+)@([^\]]+)|', $name, $reg ) ) {
$name = $reg[1];
$attr_val = preg_replace( '@[^0-9+-]@', '', $reg[2] );
}
// Get the list of skills
$bbcode = [];
$attr = gws_attribute_name( $name );
if ( $attr ) {
// Load all skills
static $skill_list = [];
if ( !file_exists( SKILLS_PATH ) ) {
die( "Missing skill details database." );
}
// Prepare bbcode prefix
$skill_list = load( SKILLS_PATH );
$attr_bbcode = " $attr=$attr_val";
// Native php filter function is more effective than a loop
$skill_list_filtered = array_filter( $skill_list, static function ( $var ) use ( $attr ) {
return ( $var['attr'] == $attr );
} );
foreach ( $skill_list_filtered as $id ) {
$bbcode[] = "[skill$noicon$attr_bbcode]{$id['name']}[/skill]";
}
}
return infuse_values( $gwbbcode_tpl['skillset'], [
'skillset_value' => implode( $noicon_comma, $bbcode ),
] );
}
// Replacement function 8:
// Process the [skill] elements
function skill_replace( $reg ) {
global $gwbbcode_tpl;
$gwbbcode_images_folder_url = GWBBCODE_IMAGES_FOLDER_URL;
$pvx_wiki_page_url = PVX_WIKI_PAGE_URL;
$gw_wiki_page_url = GW_WIKI_PAGE_URL;
[ $all, $att, $name ] = $reg;
$att = html_safe_decode( $att );
$name = html_safe_decode( $name );
// Handle alternative text
$shown_name = $name;
if ( preg_match( '/show="([^\]]*)"/', $att, $reg ) ) {
// Play it safe
$shown_name = html_safe_decode( $reg[1] );
$att = preg_replace( '/[ ]*show="[^\]]*"/', '', $att );
}
// Exit if skill doesn't exist
if ( ( $id = gws_skill_id( $name ) ) === false ) {
return $all;
}
$details = gws_details( $id );
if ( $details === false ) {
die( "Missing skill. Id=$id; Name=$name" );
}
extract( $details, EXTR_OVERWRITE );
$load = mt_rand();
// Blank/Optional skill slot
if ( $name == 'No Skill' ) {
$tpl = $gwbbcode_tpl['blank_icon'];
} else {
// Skill slot
// Get the skill attribute level
$attr_list = attribute_list( $att );
$attr_value = $attr_list[$attribute] ?? '';
// Skill name or image on which to move cursor
$name_link = str_replace( "\"", """, $name );
if ( strstr( $att, 'noicon' ) !== false ) {
if ( $shown_name !== $name ) {
$tpl = $gwbbcode_tpl['noicon_showname'];
} else {
$tpl = $gwbbcode_tpl['noicon'];
}
} else {
$tpl = $gwbbcode_tpl['icon'];
}
// Append the hidden tooltip element
$tpl .= $gwbbcode_tpl['skill'];
// Initialise array of requirements
$required = [];
// Format adrenaline
if ( $adrenaline != 0 ) {
$required[] = infuse_values( $gwbbcode_tpl['requirement'], [
'type' => 'adre',
'value' => $adrenaline,
] );
}
// Format sacrifice
if ( $sacrifice != 0 ) {
$required[] = infuse_values( $gwbbcode_tpl['requirement'], [
'type' => 'sacr',
'value' => $sacrifice . '%',
] );
}
// Format upkeep
if ( $eregen != 0 ) {
$required[] = infuse_values( $gwbbcode_tpl['requirement'], [
'type' => 'eregen',
'value' => -$eregen,
] );
}
// Format overcast
if ( $overcast != 0 ) {
$required[] = infuse_values( $gwbbcode_tpl['requirement'], [
'type' => 'over',
'value' => $overcast,
] );
}
// Format energy
if ( $energy != 0 ) {
$energy_change = false;
// Handle expertise
$expert_energy = calc_expertise( $name, $attr_list, $type, $energy, $profession, $desc );
if ( $expert_energy && $expert_energy != $energy ) {
$energy_html = infuse_values( $gwbbcode_tpl['modified_requirement_value'], [
'initial_value' => $energy,
'modified_value' => $expert_energy,
] );
$energy_change = true;
}
// Handle mysticism
$mystic_energy = calc_mysticism( $attr_list, $type, $energy, $profession );
if ( $mystic_energy && $mystic_energy != $energy ) {
$energy_html = infuse_values( $gwbbcode_tpl['modified_requirement_value'], [
'initial_value' => $energy,
'modified_value' => $mystic_energy,
] );
$energy_change = true;
}
// Default
if ( $energy_change == false ) {
$energy_html = $energy;
}
// Insert values into template
$required[] = infuse_values( $gwbbcode_tpl['requirement'], [
'type' => 'ener',
'value' => $energy_html,
] );
}
// Format casting time
if ( $casting != 0 ) {
switch ( $casting ) {
case 0.25:
$casting_frac = '¼';
break;
case 0.50:
$casting_frac = '½';
break;
case 0.75:
$casting_frac = '¾';
break;
default:
$casting_frac = $casting;
}
// Handle fast casting
$fast_casting = calc_fastcasting( $attr_list, $type, $casting, $profession );
if ( $fast_casting && $fast_casting != $casting ) {
$fast_casting = sprintf( '%.1f', $fast_casting );
$casting_html = infuse_values( $gwbbcode_tpl['modified_requirement_value'], [
'initial_value' => $casting_frac,
'modified_value' => $fast_casting,
] );
} else {
$casting_html = $casting_frac;
}
$required[] = infuse_values( $gwbbcode_tpl['requirement'], [
'type' => 'cast',
'value' => $casting_html,
] );
}
// Format recharge time
if ( $recharge != 0 ) {
$required[] = infuse_values( $gwbbcode_tpl['requirement'], [
'type' => 'rech',
'value' => $recharge,
] );
}
// Concatenate requirements
$required = implode( '', $required );
// Campaign names
$campaign_names = [
'Core',
'Prophecies',
'Factions',
'Nightfall',
'EotN',
];
if ( isset( $campaign_names[$chapter] ) ) {
$chapter = $campaign_names[$chapter];
}
// PvE only
$pve_only = $pve_only ? 'PvE only.' : '';
// Descriptions variables -> green and adapted to their attribute. (0..12..16 -> green 8)
$extra_desc = '';
gws_adapt_description( $desc, $extra_desc, $name, $attribute, $profession, $attr_list, $type, $pve_only );
$attr_html =
$attribute == 'No Attribute' ? $gwbbcode_tpl['tpl_skill_no_attr'] : $gwbbcode_tpl['tpl_skill_attr'];
$extra_desc = empty( $extra_desc )
? ''
: infuse_values( $gwbbcode_tpl['tpl_extra_desc'], [
'extra_desc' => $extra_desc,
] );
// Change the skill aspect if skill is elite
if ( isset( $elite ) && $elite ) {
$elite_or_normal = 'elite_skill';
$type = 'Elite ' . $type;
} else {
$elite_or_normal = 'normal_skill';
}
// Profession icon - used on the faded tooltip background image
if ( $prof == '?' ) {
$prof_img = "No_profession-faded";
} else {
$prof_img = "$profession-faded";
}
}
// Replace all "{var_name}" by $var_name till there is none to replace
// (i.e a tag replacement can contain other tags)
do {
$prev_tpl = $tpl;
// "{{skill_description}}" is replaced by $gwbbcode_tpl['skill_description']
unset( $matches );
preg_match_all( "#\{\{(.*?)\}\}#is", $tpl, $matches );
foreach ( $matches[0] as $r => $find ) {
if ( isset( $gwbbcode_tpl[$matches[1][$r]] ) ) {
$replace = $gwbbcode_tpl[$matches[1][$r]];
$tpl = str_replace( $find, $replace, $tpl );
}
}
// "{desc}" is replaced by $desc
unset( $matches );
preg_match_all( "#\{(.*?)\}#is", $tpl, $matches );
foreach ( $matches[0] as $r => $find ) {
$replace = ${$matches[1][$r]};
$tpl = str_replace( $find, $replace, $tpl );
}
} while ( $prev_tpl != $tpl );
return $tpl;
}
/***************************************************************************
* HELPER DATABASE FUNCTIONS
***************************************************************************/
// Database function 1:
// Returns either the id of a $skill_name, or false
// Also works if $skill_name is an abbreviation (within reason)
function gws_skill_id( $skill_name ) {
$ret = false;
// Handle abbreviations
static $abbr_db = [];
$name_id = preg_replace( '|[\'"!]|', '', strtolower( $skill_name ) );
if ( empty( $abbr_db ) ) {
if ( ( $abbr_db = @load( SKILLABBRS_PATH ) ) === false ) {
die( "Missing abbreviation database." );
}
}
if ( isset( $abbr_db[$name_id] ) ) {
$name_id = preg_replace( '|[\'"!]|', '', strtolower( $abbr_db[$name_id] ) );