-
Notifications
You must be signed in to change notification settings - Fork 11
/
rules.js
2123 lines (1866 loc) · 82.3 KB
/
rules.js
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
/* rules11.js
TM AI
Copyright (C) 2013-2016 by Lode Vandevenne
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
//Everything related to game and executing actions. But not the world and player faction related rules, and not the action class.
function isBonusTile(tile) {
return tile > T_BON_BEGIN && tile < T_BON_END;
}
function isFavorTile(tile) {
return tile > T_FAV_BEGIN && tile < T_FAV_END;
}
function isTownTile(tile) {
return tile > T_TW_BEGIN && tile < T_TW_END;
}
function isTownTilePromo2013Tile(tile) {
return tile == T_TW_2VP_2CULT || tile == T_TW_4VP_SHIP || tile == T_TW_11VP;
}
function isBonusTilePromo2013Tile(tile) {
return tile == T_BON_PASSSHIPVP_3PW;
}
function isRoundTilePromo2015Tile(tile) {
return tile == T_ROUND_TE4VP_P2C;
}
function isFireIceFaction(faction) {
return faction.color == O || faction.color == W || faction.color == X || faction.color == Z;
}
// how much of that tile are placed at game setup
function getTileInitialCount(tile) {
if(isFavorTile(tile)) return tile <= T_FAV_3A ? 1 : 3;
if(isTownTile(tile)) return (tile == T_TW_2VP_2CULT || tile == T_TW_11VP) ? 1 : 2;
return 0; //function not relevant for others.
}
function addBonusTileCoins() {
for(var i = T_BON_BEGIN + 1; i < T_BON_END; i++) if(game.bonustiles[i]) game.bonustilecoins[i] = incrUndef(game.bonustilecoins[i], 1);
}
function isFactionOctogonAction(type) {
return type == A_DOUBLE || type == A_GIANTS_2SPADE || type == A_SANDSTORM
|| type == A_WITCHES_D || type == A_SWARMLINGS_TP || type == A_AUREN_CULT;
}
// "isPowerAction"
function isPowerOctogonAction(type) {
return type == A_POWER_BRIDGE || type == A_POWER_1P || type == A_POWER_2W || type == A_POWER_7C || type == A_POWER_SPADE || type == A_POWER_2SPADE;
}
function isTileOctogonAction(type) {
return type == A_BONUS_SPADE || type == A_BONUS_CULT || type == A_FAVOR_CULT;
}
function isOctogonAction(type) {
return isFactionOctogonAction(type) || isPowerOctogonAction(type) || isTileOctogonAction(type);
}
//init both game and player octogons
function initOctogons() {
game.octogons = {};
for(var j = 0; j < game.players.length; j++) {
game.players[j].octogons = {};
}
}
var colorToPlayerMap = {}; //map to the index of the player (cannot point to the player objects themselves due to their backup cloning that changes their addresses) - undefined means the color is not chosen by any player (but 0 is index 0)
function createColorToPlayerMap() {
colorToPlayerMap = {};
for(var i = 0; i < game.players.length; i++) {
colorToPlayerMap[game.players[i].color] = i;
}
}
var woodColorToPlayerMap = {}; //map to the index of the player (cannot point to the player objects themselves due to their backup cloning that changes their addresses) - undefined means the color is not chosen by any player (but 0 is index 0)
function createWoodColorToPlayerMap() {
woodColorToPlayerMap = {};
for(var i = 0; i < game.players.length; i++) {
woodColorToPlayerMap[game.players[i].woodcolor] = i;
}
}
var auxColorToPlayerMap = {}; //map to the index of the player (cannot point to the player objects themselves due to their backup cloning that changes their addresses) - undefined means the color is not chosen by any player (but 0 is index 0)
function createAuxColorToPlayerMap() {
auxColorToPlayerMap = {};
for(var i = 0; i < game.players.length; i++) {
auxColorToPlayerMap[game.players[i].auxcolor] = i;
}
}
function recalculateColorMaps() {
createColorToPlayerMap();
createAuxColorToPlayerMap();
createWoodColorToPlayerMap();
}
function getTownReqPower(player) {
return player.favortiles[T_FAV_2F_6TW] > 0 ? 6 : 7;
}
function getTileIncome(tile) {
if(tile == T_BON_SPADE_2C) return [2,0,0,0,0];
else if(tile == T_BON_CULT_4C) return [4,0,0,0,0];
else if(tile == T_BON_6C) return [6,0,0,0,0];
else if(tile == T_BON_3PW_SHIP) return [0,0,0,3,0];
else if(tile == T_BON_3PW_1W) return [0,1,0,3,0];
else if(tile == T_BON_PASSDVP_2C) return [2,0,0,0,0];
else if(tile == T_BON_PASSTPVP_1W) return [0,1,0,0,0];
else if(tile == T_BON_PASSSHSAVP_2W) return [0,2,0,0,0];
else if(tile == T_BON_1P) return [0,0,1,0,0];
else if(tile == T_BON_PASSSHIPVP_3PW) return [0,0,0,3,0];
else if(tile == T_FAV_2E_1PW1W) return [0,1,0,1,0];
else if(tile == T_FAV_2A_4PW) return [0,0,0,4,0];
else if(tile == T_FAV_1F_3C) return [3,0,0,0,0];
else return null;
}
//TODO: rename these functions sumRes, subRes and mulRes (resources instead of income, since they can also be cost)
// modifies input variable AND returns it. TODO: only return
function sumIncome(income, added) {
for(var i = 0; i < added.length; i++) income[i] = undef0(income[i]) + undef0(added[i]);
return income;
}
// modifies input variable AND returns it. TODO: only return
function subtractIncome(income, removed) {
for(var i = 0; i < removed.length; i++) income[i] = undef0(income[i]) - undef0(removed[i]);
return income;
}
// modifies input variable AND returns it. TODO: only return
function mulIncome(income, times) {
for(var i = 0; i < income.length; i++) income[i] *= times;
return income;
}
//returns [coins, workers, priests, power, vp]
function getIncome(player, includeBonusTile, round) {
var result = getBuildingIncome(player);
for (var tile in player.favortiles) {
if (player.favortiles.hasOwnProperty(tile) && player.favortiles[tile] > 0) {
var tileincome = getTileIncome(tile);
if(tileincome) sumIncome(result, tileincome);
}
}
if(player.bonustile && includeBonusTile) {
var tileincome = getTileIncome(player.bonustile);
if(tileincome) sumIncome(result, tileincome);
}
if(round > 0 && round != 6) sumIncome(result, getRoundBonusResources(player, round));
return result;
}
function addPower(player, pw) {
if(player.pw0 >= pw) {
player.pw0 -= pw;
player.pw1 += pw;
pw = 0;
} else {
pw -= player.pw0;
player.pw1 += player.pw0;
player.pw0 = 0;
}
if(player.pw1 >= pw) {
player.pw1 -= pw;
player.pw2 += pw;
} else {
player.pw2 += player.pw1;
player.pw1 = 0;
}
}
function usePower(player, pw) {
if(player.pw2 < pw) return false;
player.pw2 -= pw;
player.pw0 += pw;
return true;
}
function burnPower(player, pw) {
if(player.pw1 < 2 * pw) return false;
player.pw1 -= 2 * pw;
player.pw2 += pw;
return true;
}
// returns 0 if no, the amount of locked colors if riverwalkers with 1 to 7 locked colors
function mayGetPriestAsColor(player) {
if(player.color != Z) return 0;
var result = 7;
for(var i = CIRCLE_BEGIN; i <= CIRCLE_END; i++) {
if(player.colors[i - R]) result--;
}
return result;
};
function addPriests(player, p) {
if(p <= 0) return;
if(player.color == Z) {
var numunlocked = 7 - mayGetPriestAsColor(player);
while(p > 0 && numunlocked < 7) { // normally p is 1 so while loop is no prob
player.priestorcolor++;
p--;
numunlocked++;
}
}
player.p += p;
if(player.p > player.pp) player.p = player.pp;
}
// Color Z means to unlock it as regular priest
// Returns error string if error, empty string if ok
function unlockColorPriest(player, color) {
var error = '';
if(!(color >= CIRCLE_BEGIN && color <= CIRCLE_END) && color != Z) {
//LOU PROBLEM with POW1P for Riverwalkers since color is not Z
//error = 'invalid unlock color';
addLog ( 'invalid unlock color override, add priest ');
if(player.p < player.pp) player.p++;
player.priestorcolor--;
} else if(player.colors[color - R]) {
error = 'already have this color';
} else {
if(color == Z) {
if(player.p < player.pp) player.p++;
} else {
if(state.fireiceerrata) {
var cheap = colorIsCheapForLava(color);
var error = tryConsume(player, [cheap ? 1 : 2,0,0,0,0]);
if(error) return 'cannot afford the gold for color unlock';
}
player.colors[color - R] = true;
player.pp++; //priest goes to priest pool
}
player.priestorcolor--;
}
return error;
}
//aka "giveResources"
//not to be confused with "sumIncome"
//This can also add VP, if that case give reason
function addIncome(player, income, opt_reason, opt_detail) {
if(income.length < 5) throw 'resource array size must be at least 5';
player.c += income[R_C];
player.w += income[R_W];
addPriests(player, income[R_P]);
addPower(player, income[R_PW]);
player.addVP(income[R_VP], opt_reason, opt_detail);
if(income.length > R_VP + 1) {
if(income[R_PP]) player.pp += income[R_PP];
if(income[R_KEY]) player.keys += income[R_KEY];
if(income[R_SPADE]) player.spades += income[R_SPADE];
if(income[R_PT]) player.pw2 += income[R_PT];
if(income[R_FREECULT]) player.freecult += income[R_FREECULT];
if(income[R_CULT]) player.fixedcult += income[R_CULT];
if(income[R_PT0]) player.pw0 += income[R_PT0];
if(income[R_PT1]) player.pw1 += income[R_PT1];
if(income[R_PT2]) player.pw2 += income[R_PT2];
if(income[R_FIRE]) giveCult(player, C_F, income[R_FIRE]);
if(income[R_WATER]) giveCult(player, C_W, income[R_WATER]);
if(income[R_EARTH]) giveCult(player, C_E, income[R_EARTH]);
if(income[R_AIR]) giveCult(player, C_A, income[R_AIR]);
if(income[R_SPADEVP]) player.spadevp += income[R_SPADEVP];
if(income[R_BRIDGE]) {
var num = Math.min(income[R_BRIDGE], player.bridgepool);
player.bridgepool -= num;
player.bridges += num;
}
}
}
//consumed is an income array
//precondition: player has enough resources
//TODO: give custom VP reason for consumed resources
function consume(player, consumed) {
if(consumed.length < 5) throw 'resource array size must be at least 5';
player.c -= consumed[R_C];
player.w -= consumed[R_W];
player.p -= consumed[R_P];
usePower(player, consumed[R_PW]);
//this can happen for alchemists
if(player.faction == F_ALCHEMISTS) player.addVP(-consumed[R_VP], 'faction', 'faction');
else player.addVP(-consumed[R_VP], 'other', 'other');
if(consumed.length > R_VP + 1) {
if(consumed[R_PP]) player.pp -= consumed[R_PP];
if(consumed[R_KEY]) player.keys -= consumed[R_KEY];
if(consumed[R_SPADE]) player.spades -= consumed[R_SPADE];
var pt = undef0(consumed[R_PT]);
while(pt > 0) {
if(player.pw0) player.pw0--;
else if(player.pw1) player.pw1--;
else if(player.pw2) {
player.pw2--;
player.c++; //convert it automatically because why not. TODO: Don't do this. Do this in Human UI instead. Don't let these actions do this, or it would not correctly handle e.g. snellman games.
}
pt--;
}
if(consumed[R_FREECULT]) player.freecult -= consumed[R_FREECULT]; //an external mechanism must remove it from the right cult track and check the validity
if(consumed[R_CULT]) player.fixedcult -= consumed[R_CULT]; //an external mechanism must remove it from the right cult track and check the validity
if(consumed[R_PT0]) player.pw0 -= consumed[R_PT0];
if(consumed[R_PT1]) player.pw1 -= consumed[R_PT1];
if(consumed[R_PT2]) player.pw2 -= consumed[R_PT2];
if(consumed[R_FIRE]) giveCult(player, C_F, -consumed[R_FIRE]);
if(consumed[R_WATER]) giveCult(player, C_W, -consumed[R_WATER]);
if(consumed[R_EARTH]) giveCult(player, C_E, -consumed[R_EARTH]);
if(consumed[R_AIR]) giveCult(player, C_A, -consumed[R_AIR]);
if(consumed[R_SPADEVP]) player.spadevp -= consumed[R_SPADEVP];
if(consumed[R_BRIDGE]) player.bridges -= consumed[R_BRIDGE];
}
}
function canConsume(player, consumed) {
if(consumed.length < 5) throw 'resource array size must be at least 5';
var result = player.c >= consumed[R_C] && player.w >= consumed[R_W] && player.p >= consumed[R_P]
&& player.pw2 >= consumed[R_PW] && player.vp >= consumed[R_VP];
if(result && consumed.length > R_VP + 1) {
if(consumed[R_PP] && player.pp < consumed[R_PP]) return false;
if(consumed[R_KEY] && player.keys < consumed[R_KEY]) return false;
if(consumed[R_SPADE] && player.spades < consumed[R_SPADE]) return false;
if(consumed[R_PT] && player.pw0 + player.pw1 + player.pw2 < consumed[R_PT]) return false;
/*if(consumed[R_CULT] && (opt_cult == undefined || opt_cult == C_NONE)) return false; //cannot know which cult track is meant, must be specified
if(consumed[R_CULT] && player.cult[opt_cult] < consumed[R_CULT]) return false;*/
if(consumed[R_CULT] && player.cult[C_F] < consumed[R_CULT] && player.cult[C_W] < consumed[R_CULT] &&
player.cult[C_E] < consumed[R_CULT] && player.cult[C_A] < consumed[R_CULT]) return false;
if(consumed[R_PT0] && player.pw0 < consumed[R_PT0]) return false;
if(consumed[R_PT1] && player.pw1 < consumed[R_PT1]) return false;
if(consumed[R_PT2] && player.pw2 < consumed[R_PT2]) return false;
if(consumed[R_FIRE] && player.cult[C_F] < consumed[R_FIRE]) return false;
if(consumed[R_WATER] && player.cult[C_W] < consumed[R_WATER]) return false;
if(consumed[R_EARTH] && player.cult[C_E] < consumed[R_EARTH]) return false;
if(consumed[R_AIR] && player.cult[C_A] < consumed[R_AIR]) return false;
if(consumed[R_SPADEVP] && player.spadevp < consumed[R_SPADEVP]) return false;
if(consumed[R_BRIDGE] && player.bridges < consumed[R_BRIDGE]) return false;
}
return result;
}
//only for cheat/debug buttons
function consumeOverload(player, consumed) {
player.c -= consumed[0];
if(player.c < 0) player.c = 0;
player.w -= consumed[1];
if(player.w < 0) player.w = 0;
player.p -= consumed[2];
if(player.p < 0) player.p = 0;
if(!usePower(player, consumed[3])) {
var pw_left = consumed[3] - player.pw2;
usePower(player, player.pw2);
// For debugging, also use it up from pw1.
var pw = Math.min(player.pw1, pw_left);
player.pw1 -= pw;
player.pw0 += pw;
}
player.vp -= consumed[4];
if(player.vp < 0) player.vp = 0;
}
//returns error or ''
//opt_actiontype is only for error strings
function tryConsume(player, cost, opt_actiontype) {
if(!canConsume(player, cost)) {
if(opt_actiontype) return 'not enough resources for ' + getActionName(opt_actiontype);
else return 'not enough resources';
}
consume(player, cost);
return '';
}
//returns error or ''
function tryConsumeForAction(player, actiontype) {
var cost = player.getActionCost(actiontype);
return tryConsume(player, cost, actiontype);
}
//returns true if the player had enough resources for the conversion
function tryConversion(player, action) {
var actiontype = action.type;
var consumed = null;
var produced = null;
if(actiontype == A_CONVERT_1PW_1C) { consumed = [0,0,0,1,0]; produced = [1,0,0,0,0]; }
else if(actiontype == A_CONVERT_3PW_1W) { consumed = [0,0,0,3,0]; produced = [0,1,0,0,0]; }
else if(actiontype == A_CONVERT_5PW_1P) { consumed = [0,0,0,5,0]; produced = [0,0,1,0,0]; }
else if(actiontype == A_CONVERT_1P_1W) { consumed = [0,0,1,0,0]; produced = [0,1,0,0,0]; }
else if(actiontype == A_CONVERT_1W_1C) { consumed = [0,1,0,0,0]; produced = [1,0,0,0,0]; }
else if(actiontype == A_CONVERT_1VP_1C) {
if(player.faction != F_ALCHEMISTS) return 'only alchemists can do this';
consumed = [0,0,0,0,1];
produced = [1,0,0,0,0];
}
else if(actiontype == A_CONVERT_2C_1VP) {
if(player.faction != F_ALCHEMISTS) return 'only alchemists can do this';
consumed = [2,0,0,0,0];
produced = [0,0,0,0,1];
}
else if(actiontype == A_CONVERT_1W_1P) {
player.darklingconverts--;
consumed = [0,1,0,0,0];
produced = [0,0,1,0,0];
}
else return 'invalid faction or convert action';
if(consumed != null && canConsume(player, consumed)) {
consume(player, consumed);
addIncome(player, produced);
if(player.priestorcolor) {
var error = unlockColorPriest(player, action.color);
if(error != '') return error;
}
if(player.p > player.pp) return 'not enough priests in pool to get priest from this conversion';
return '';
}
return 'could not convert';
}
function tryBuild(player, action) {
if(!action.co) return 'must have build coordinates';
if(player.b_d <= 0) return 'no dwellings left';
var x = action.co[0];
var y = action.co[1];
if(player.transformed && !player.transformcoset[arCo(x, y)]) return 'after transforming, may build only on transformed tile';
if(!inReach(player, x, y, false) && !temporaryTunnelCarpetOk(player, x, y)) {
return 'tile not reachable';
}
if(isOccupied(x, y)) return 'tile occupied';
var cost = player.getFaction().getBuildingCost(B_D, false);
var error = tryConsume(player, cost, action.type);
if(error != '') return error;
var tile = getWorld(x, y);
if(tile == I || tile == N) return 'invalid tile type';
if(tile != (player.color == X ? player.auxcolor : player.color) && !player.colors[tile - R]) return 'tile must have your color to build';
player.b_d--;
setBuilding(x, y, B_D, player.woodcolor);
player.built = true;
player.nodigreachco = [x, y];
return '';
}
function canDoRoundBonusDig(player, type, x, y) {
var reason = [];
if(!player.getFaction().canTakeFactionAction(player, type, reason)) return reason[0] || error;
if(!inReach(player, x, y, false)) return 'tile not reachable'; //no fakirs or dwarves tunneling
if(isOccupied(x, y)) return 'tile occupied';
var tile = getWorld(x, y);
if(tile == I || tile == N) return 'invalid tile type';
return '';
}
// This one takes PREVIOUS round bonus digs into account. "canDoRoundBonusDigs" does not support this. This one must instead be called after executing each previous round dig.
function canDoRoundBonusDig2(player, type, x, y) {
var error = canDoRoundBonusDig(player, type, x, y);
if(error != '') return error;
var tile = getWorld(x, y);
if(tile == player.color && (type == A_GIANTS_TRANSFORM || type == A_TRANSFORM_SPECIAL)) return 'tile already your color';
if(tile == O || tile == W) return 'cannot transform ice or fire';
var dist = digDist(player, tile, player.color);
if(type == A_TRANSFORM_SPECIAL && dist != 1) return 'cannot ice-transform if color too many digs away';
return '';
}
//digs = array where each element is an array [actiontype, x, y], and actiontype is e.g. A_TRANSFORM_CW, ...
function canDoRoundBonusDigs(player, digs) {
var maxnum = getRoundBonusDigs(player, state.round);
if(digs.length > maxnum) return 'too many bonus digs, have max ' + maxnum;
//LOU The Giants can not use one bonus dig, but two digs are good on any one hex
//This is called from state.js, before the Giants check for a single dig(two single digs excluded elsewhere)
//if(player.faction == F_GIANTS && digs.length > 1) return 'giants cannot have more than one round bonus digs';
for(var i = 0; i < digs.length; i++) {
var error = canDoRoundBonusDig(player, digs[i][0], digs[i][1], digs[i][2]);
if(error != '') return error;
}
return '';
}
// Does not take into account the legality of the transform
function getColorAfterTransformAction(incolor, player, type) {
if(type == A_GIANTS_TRANSFORM || type == A_SANDSTORM || type == A_TRANSFORM_SPECIAL || type == A_TRANSFORM_SPECIAL2) {
//if(incolor == W && type == A_TRANSFORM_SPECIAL) return player.auxcolor;
//could as well say of course that giants transform is red, sandstorm is yellow. But let's be generic with player colors.
return player.getMainDigColor();
}
else if(type == A_TRANSFORM_CW) {
var color = incolor + 1;
//if(incolor == W) color = player.auxcolor + 1;
if(color == S + 1) color = R;
return color;
}
else if(type == A_TRANSFORM_CCW) {
var color = incolor - 1;
//if(incolor == W) color = player.auxcolor - 1;
if(color == R - 1) color = S;
return color;
}
return incolor;
}
//non destructive if it fails. Returns error string.
//type is e.g. A_TRANSFORM_CCW, ...
function tryRoundBonusDig(player, type, x, y) {
var error = canDoRoundBonusDig2(player, type, x, y);
if(error != '') return error;
var tile = getWorld(x, y);
setWorld(x, y, getColorAfterTransformAction(tile, player, type));
if(player.faction == F_HALFLINGS) { player.addVP(1, 'faction', 'faction') }
if(player.faction == F_ALCHEMISTS && built_sh(player)) addPower(player, 2);
return '';
}
function getUpgradeActionInputBuilding(action) {
if(action.type == A_UPGRADE_TP || action.type == A_SWARMLINGS_TP) return B_D;
else if(action.type == A_UPGRADE_TE) return B_TP;
else if(action.type == A_UPGRADE_SH) return B_TP;
else if(action.type == A_UPGRADE_SA) return B_TE;
}
function getUpgradeActionOutputBuilding(action) {
if(action.type == A_UPGRADE_TP || action.type == A_SWARMLINGS_TP) return B_TP;
else if(action.type == A_UPGRADE_TE) return B_TE;
else if(action.type == A_UPGRADE_SH) return B_SH;
else if(action.type == A_UPGRADE_SA) return B_SA;
}
function getRoundTile() {
return game.roundtiles[state.round];
}
function tryUpgradeAction(player, action) {
if(!action.co) return 'upgrade action must have coordinate';
var x = action.co[0];
var y = action.co[1];
var tile = getWorld(x, y);
var building = getBuilding(x, y);
if(building[1] != player.woodcolor) return 'wrong tile color';
var b_in = getUpgradeActionInputBuilding(action);
var b_out = getUpgradeActionOutputBuilding(action);
if(action.type == A_UPGRADE_TP || action.type == A_SWARMLINGS_TP) {
if(player.b_tp <= 0) return 'not enough TP left';
}
else if(action.type == A_UPGRADE_TE) {
if(player.b_te <= 0) return 'not enough TE left';
}
else if(action.type == A_UPGRADE_SH) {
if(player.b_sh <= 0) return 'not enough SH left';
}
else if(action.type == A_UPGRADE_SA) {
if(player.b_sa <= 0) return 'not enough SA left';
}
var resources;
if(action.type == A_SWARMLINGS_TP) resources = [0,0,0,0,0]
else resources = player.getFaction().getBuildingCost(b_out, action.type == A_UPGRADE_TP ? hasNeighbor(x, y, player.woodcolor) : false);
if(building[0] != b_in) return 'wrong input building type';
var error = tryConsume(player, resources, action.type);
if(error != '') return error;
if(b_out == B_TP) {
player.b_d++;
player.b_tp--;
}
else if(b_out == B_TE) {
player.b_tp++;
player.b_te--;
}
else if(b_out == B_SH) {
player.b_tp++;
player.b_sh--;
}
else if(b_out == B_SA) {
player.b_te++;
player.b_sa--;
}
setBuilding(x, y, b_out, player.woodcolor);
return '';
}
//returns amount of power received on cult track when going from 'from' to 'to'
function cultPower(from, to) {
var result = 0;
if(from < 3 && to >= 3) result++;
if(from < 5 && to >= 5) result += 2;
if(from < 7 && to >= 7) result += 2;
if(from < 10 && to >= 10) result += 3;
return result;
}
//if you go num steps in the cult track, returns how much steps you really go up (can be less if at the top of the track)
function willGiveCult(player, cult, num) {
var oldcult = player.cult[cult];
var maxcult = (player.keys > 0 && getHighestOtherPlayerValueOnCult(player, cult) < 10) ? 10 : 9;
var newcult = Math.min(maxcult, oldcult + num);
return newcult - oldcult;
}
//aka "addCult"
//gives cult, if too much, ignores it (it is no error)
//also supports negative values, and will give town keys back if necessary
function giveCult(player, cult, num) {
if(num == 0) return;
if(num > 0) {
if(player.cult[cult] == 10) return; //the code below would bring it back to 9 due to key limit and Math.min
var oldcult = player.cult[cult];
var maxcult = (player.keys > 0 && getHighestOtherPlayerValueOnCult(player, cult) < 10) ? 10 : 9;
var newcult = Math.min(maxcult, oldcult + num);
if(newcult == 10 && oldcult < 10) player.keys--;
player.cult[cult] = newcult;
addPower(player, cultPower(oldcult, newcult));
}
else if(num < 0) {
if(player.cult[cult] == 10) player.keys++; //get the town key back
if(player.cult[cult] < num) player.cult[cult] = 0;
else player.cult[cult] += num; //negative so subtracted
}
}
function tryCultPriestAction(player, action) {
if(player.p < 1) return 'not enough priests';
var cp = game.cultp[action.cult];
if(action.cult < 0 || action.cult > 3) return 'invalid cult';
if(action.type == A_CULT_PRIEST3 && cp[0] != N) return 'the 3 is full';
if(action.type == A_CULT_PRIEST2 && cp[1] != N && cp[2] != N && cp[3] != N) return 'the 2s are full';
addIncome(player, player.getActionIncome(action.type));
player.p--;
if(action.type != A_CULT_PRIEST1) {
if(player.pp <= 0) throw new Error('priest pool went wrong');
player.pp--;
}
// put the priests on the tracks
if(action.type == A_CULT_PRIEST3) cp[0] = player.woodcolor;
if(action.type == A_CULT_PRIEST2) {
if(cp[1] == N) cp[1] = player.woodcolor;
else if(cp[2] == N) cp[2] = player.woodcolor;
else if(cp[3] == N) cp[3] = player.woodcolor;
else throw new Error('already full');
}
return '';
}
function tryCultAction(player, action) {
var cp = game.cultp[action.cult];
if(action.cult < 0 || action.cult > 3) return 'invalid cult';
var num = action.type == A_AUREN_CULT ? 2 : 1;
if(action.type == A_BONUS_CULT) {
if(player.bonustile != T_BON_CULT_4C) return 'player does not have bonus cult tile';
if(player.octogons[A_BONUS_CULT]) return 'bonus cult already used';
player.octogons[A_BONUS_CULT] = 1;
}
if(action.type == A_FAVOR_CULT) {
if(!player.favortiles[T_FAV_2W_CULT]) return 'player does not have favor cult tile';
if(player.octogons[A_FAVOR_CULT]) return 'favor cult already used';
player.octogons[A_FAVOR_CULT] = 1;
}
if(action.type == A_AUREN_CULT) {
if(!built_sh(player)) return 'auren cult action requires stronghold';
if(player.octogons[A_AUREN_CULT]) return 'auren cult already used';
player.octogons[A_AUREN_CULT] = 1;
}
addIncome(player, player.getActionIncome(action.type));
return '';
}
//add extra VP (and a few other resources) from the action: everything related to faction powers and favor, bonus and round tiles giving some extra immediate VP or other resource.
//includes alchemists SH power
function addExtrasForAction(player, action) {
if(action.type == A_UPGRADE_TP || action.type == A_SWARMLINGS_TP) {
if(player.favortiles[T_FAV_1W_TPVP]) {
player.addVP(3, 'favor', getTileVPDetail(T_FAV_1W_TPVP));
}
if(getRoundTile() == T_ROUND_TP3VP_4W1DIG || getRoundTile() == T_ROUND_TP3VP_4A1DIG) {
player.addVP(3, 'round', getTileVPDetail(getRoundTile()));
}
}
if(action.type == A_UPGRADE_TE) {
if(getRoundTile() == T_ROUND_TE4VP_P2C) {
player.addVP(4, 'round', getTileVPDetail(getRoundTile()));
}
}
if(action.type == A_BUILD || action.type == A_WITCHES_D) {
if(player.favortiles[T_FAV_1E_DVP]) {
player.addVP(2, 'favor', getTileVPDetail(T_FAV_1E_DVP));
}
if(getRoundTile() == T_ROUND_D2VP_4W1P || getRoundTile() == T_ROUND_D2VP_4F4PW) {
player.addVP(2, 'round', getTileVPDetail(getRoundTile()));
}
}
if(isSpadeConsumingAction(action.type)) {
var num = action.type == A_GIANTS_TRANSFORM ? 2 : 1;
if(getRoundTile() == T_ROUND_DIG2VP_1E1C) {
player.addVP(num * 2, 'round', getTileVPDetail(getRoundTile()));
}
if(player.faction == F_HALFLINGS) {
player.addVP(num, 'faction', 'faction');
}
if(player.faction == F_ALCHEMISTS && built_sh(player)) addPower(player, num * 2);
}
if(player.faction == F_DARKLINGS && action.type == A_SPADE) {
//2VP for digging with priest
player.addVP(2, 'faction', 'faction');
}
if(action.type == A_UPGRADE_SH || action.type == A_UPGRADE_SA) {
if(getRoundTile() == T_ROUND_SHSA5VP_2F1W || getRoundTile() == T_ROUND_SHSA5VP_2A1W) {
player.addVP(5, 'round', getTileVPDetail(getRoundTile()));
}
if(action.type == A_UPGRADE_SH) {
player.getFaction().getOneTimeStrongholdIncome(player);
addIncome(player, player.getFaction().getActionIncome(player, action.type));
//TODO: ensure this is not done more globally elsewhere already. This is currently only used by riverwalkers SH for the two bridges.
}
}
for(var i = 0; i < action.twtiles.length; i++) {
if(getRoundTile() == T_ROUND_TW5VP_4E1DIG) {
player.addVP(5, 'round', getTileVPDetail(getRoundTile()));
}
if(player.faction == F_WITCHES) {
player.addVP(5, 'faction', 'faction');
}
if(player.faction == F_SWARMLINGS) player.w += 3;
if(action.twtiles[i] == T_TW_4VP_SHIP) {
if(player.faction == F_DWARVES) {
// nothing
}
else if(player.faction == F_FAKIRS) {
player.tunnelcarpetdistance++;
}
else {
advanceShipping(player);
}
}
}
}
//amount of digs from the round end based on cult track
function getRoundBonusDigsForCults(cult, round) {
if(round == 0) return 0;
var t = game.roundtiles[round];
if(!t) return 0;
if(t == T_ROUND_TP3VP_4W1DIG) {
return Math.floor(cult[C_W] / 4);
}
else if(t == T_ROUND_TP3VP_4A1DIG) {
return Math.floor(cult[C_A] / 4);
}
else if(t == T_ROUND_TW5VP_4E1DIG) {
return Math.floor(cult[C_E] / 4);
}
return 0;
}
//amount of spades from the round end based on cult track
function getRoundBonusDigs(player, round) {
if(player.color == Z) return 0; // riverwalkers cannot use these digs
return getRoundBonusDigsForCults(player.cult, round);
}
//amount of resources from the round end based on cult track
//priests is amount of priests the player has in total on all cult tracks
function getRoundBonusResourcesForCults(cult, priests, round) {
if(round == 0) return [0,0,0,0,0];
var t = game.roundtiles[round];
if(!t) return [0,0,0,0,0];
if(t == T_ROUND_D2VP_4W1P) {
return [0,0,Math.floor(cult[C_W] / 4),0,0];
}
else if(t == T_ROUND_D2VP_4F4PW) {
return [0,0,0,4*Math.floor(cult[C_F] / 4),0];
}
else if(t == T_ROUND_DIG2VP_1E1C) {
return [cult[C_E],0,0,0,0];
}
else if(t == T_ROUND_SHSA5VP_2F1W) {
return [0,Math.floor(cult[C_F] / 2),0,0,0];
}
else if(t == T_ROUND_SHSA5VP_2A1W) {
return [0,Math.floor(cult[C_A] / 2),0,0,0];
}
else if(t == T_ROUND_TE4VP_P2C) {
return [priests*2,0,0,0,0];
}
return [0,0,0,0,0];
}
//amount of resources from the round end based on cult track
function getRoundBonusResources(player, round) {
return getRoundBonusResourcesForCults(player.cult, getCultPriests(player), round);
}
function getCultPriests(player) {
// a simpler way would normally be "7 - player.pp", but that would not be correct for riverwalkers, where pp is initially 1 instead of 7
var result = 0;
for(var i = C_F; i <= C_A; i++) {
for(var j = 0; j < 4; j++) {
if(game.cultp[i][j] == player.woodcolor) result++;
}
}
return result;
}
//this one does NOT fail and does not consume income. If you already have max shipping, it does nothing.
function advanceShipping(player) {
if(!canAdvanceShip(player)) return;
player.addVP(getAdvanceShipVP(player), 'advance', 'advship');
player.shipping++;
}
//it it a color that can be trasformed at all?
function canTransform(player, x, y) {
var color = getWorld(x, y);
if(color == W || color == O) return false;
return true;
}
//can the player build on this without any transformation?
function canBuildOn(player, x, y) {
var color = getWorld(x, y);
if(color == player.color) return true;
if(player.color == X && color == player.auxcolor) return true; // shapeshifters
if(player.colors[color - R]) return true; // riverwalkers
return false;
}
//execute passing for the player
function passPlayer(player) {
player.passed = true;
// Variable turnorder by Lou
if(state.round != 6 && state.turnorder) {
state.turnMatrix[1][state.passOrder] = player.index;
state.passOrder++;
}
}
//The inner if-else part of tryAction
function tryActionCore_(player, action /*Action object*/) {
var error = '';
if(action.type == A_BURN) {
if(player.pw1 < 2) {
return 'not enough power to burn';
}
burnPower(player, 1);
}
else if(isConvertAction(action)) {
error = tryConversion(player, action);
}
else if(action.type == A_ADV_SHIP) {
if(!canAdvanceShip(player)) return 'already max shipping';
error = tryConsumeForAction(player, action.type);
if(error != '') return error;
advanceShipping(player);
}
else if(action.type == A_ADV_DIG) {
if(!canAdvanceDig(player)) return 'already max digging';
error = tryConsumeForAction(player, action.type);
if(error != '') return error;
player.addVP(6, 'advance', 'advdig');
player.digging++;
}
else if(isSpadeGivingAction(action.type)) {
/*
The following rules for spades are not directly in the rulebook, but posted by a designer on the bgg forum:
Official rules:
Scoring tile "VP's for digging" not in round 5 or 6.
Maximum digging up to six steps in one action (Giants always exactly 2 shovels), which means you can't terraform further than your hometerrain in one action.
You can dig in either direction, even your hometerrain (when it is the starting terrain) can be transformed into another landscape.
I shall put it this way:
You can dig in any direction, but you have to stop at your home terrain (after all the primary goal of terraforming is to reach your hometerrain), so in one action the maximum could be six steps.
You may change your hometerrain, but only, if the landscape is your hometerrain at the beginning of your turn. In this case again six steps is the maximum per action.
I know Frank suggested something different a while ago Possible imbalance, but now the only limitation is, you have to stop at your hometerrain or you have to start at your hometerrain and the maximum is 6 steps of terraforming per action.
*/
if(player.spades >= 6) return 'max 6 spades per turn';
player.mayaddmorespades = true;
player.nodigreachco = null;
player.transformco = null;
if(action.type == A_SPADE) {
var cost = player.getActionCost(action.type);
error = tryConsume(player, cost, action.type);
if(error != '') return error;
addIncome(player, player.getActionIncome(action.type)); //adds the spades
player.overflowspades = false; //overflowing of spades can only when taking 2 spade power action or halflings SH, but the overflow is no longer valid if you pay for extra spades (because you may only pay for extra spades to keep digging the same terrain tile).
}
else if(action.type == A_BONUS_SPADE) {
if(player.octogons[A_BONUS_SPADE]) return 'action already taken';
player.octogons[A_BONUS_SPADE] = 1;
addIncome(player, player.getActionIncome(action.type)); //adds the spades
}
else if(action.type == A_POWER_SPADE) {
game.octogons[action.type] = 1;
error = tryConsumeForAction(player, action.type);
if(error != '') return error;
addIncome(player, player.getActionIncome(action.type)); //adds the spades
}
else if(action.type == A_POWER_2SPADE) {
game.octogons[action.type] = 1;
error = tryConsumeForAction(player, action.type);
if(error != '') return error;
addIncome(player, player.getActionIncome(action.type)); //adds the spades
if(player.spades == 2) player.overflowspades = true;
}
else if(action.type == A_GIANTS_2SPADE) {
if(player.faction != F_GIANTS) return 'must be giants for this action';
if(!built_sh(player)) return 'this action requires SH';
if(player.octogons[A_GIANTS_2SPADE]) return 'action already taken';
player.octogons[A_GIANTS_2SPADE] = 1;
addIncome(player, player.getActionIncome(action.type)); //adds the spades
}
else return 'unknown spade action';
}
else if(isTransformAction(action.type)) {
if(action.type == A_SANDSTORM) player.nodigreachco = null;
var x = action.co[0];
var y = action.co[1];
if(!inReachButDontCountCo(player, x, y, false, player.nodigreachco) &&
!temporaryTunnelCarpetOk(player, x, y)) return 'tile not reachable';
if(isOccupied(x, y)) return 'tile occupied';
if(!canTransform(player, x, y)) return 'cannot transform this tile';
var tile = getWorld(x, y);
if(tile == I || tile == N) return 'invalid tile type';
var cost = player.getFaction().getTransformActionCost(player, action.type, tile);
if(!canConsume(player, cost)) {
if(cost[R_SPADE]) return 'not enough spades to transform';
if(cost[R_PT]) return 'not enough tokens to transform';
if(cost[R_CULT]) return 'not enough cult to transform';