-
Notifications
You must be signed in to change notification settings - Fork 11
/
state.js
934 lines (864 loc) · 35.1 KB
/
state.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
/* state9.js
TM AI
Copyright (C) 2013-2014 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.
*/
//The game state machine
//Callback state enum. For debugging only.
//Each state goes through the progression executeActor -> (callback) -> transitionState -> gameLoopNonBlocking --> ...
//This shows in which stage it is, for debuggin.
CS_NONE = 0;
CS_EXECUTE = 1;
CS_ACTOR = 2;
CS_CALLBACK = 3;
CS_TRANSITION = 4;
CS_GAMELOOP = 5;
//This is just there for debugging, has no actually function.
function getCallbackStateName(state) {
switch(state) {
case CS_NONE: return 'none';
case CS_EXECUTE: return 'execute';
case CS_ACTOR: return 'actor';
case CS_CALLBACK: return 'callback';
case CS_TRANSITION: return 'transition';
case CS_GAMELOOP: return 'gameloop';
}
return 'unknown';
}
var callbackState = CS_NONE;
// for running in JS console
function getCallBackStateDebugString() {
return 'round: ' + state.round +
', player: ' + state.currentPlayer +
', leechi: ' + state.leechi +
', leecharray: ' + encodeNestedArray(state.leecharray) +
', state type: ' + getGameStateCodeName(state.type) +
', next_type: ' + getGameStateCodeName(state.next_type) +
', callback state: ' + getCallbackStateName(callbackState);
}
//Constructor
var State = function() {
this.type = S_PRE;
this.typestack = []; //TODO: also support different currentPlayer and other vars on the stack. TODO: replace everything with stack and put all variables on it.
this.currentPlayerStack = [];
this.next_type = S_NONE;
this.round = 0; //the round for actions. 0 = pre-rounds, 1-6 = game rounds.
this.startPlayer = 0;
this.currentPlayer = 0;
this.prevPlayer = 0;
this.showResourcesPlayer = 0;
this.numHandledForState = 0; //counter for bookkeeping for how much of something already done, e.g. to know when each player chose a faction
this.leecharray = []; //2D leech array described at function getLeechEffect (array of [playerIndex, amount])
this.leechi = 0; //index in leech array
this.leechtaken = 0;
// game rule options. TODO: newcultistsrule etc... belongs more in the Game class than here. Move it?
this.newcultistsrule = true; // getting 1 power if nobody leeches from them
this.towntilepromo2013 = true; // new town tiles
this.bonustilepromo2013 = true; // new bonus tile
this.fireice = true; // Fire & Ice expansion
this.fireiceerrata = true; // Official change of rules for shapeshifters and riverwalkers.
// https://www.boardgamegeek.com/thread/1456706/official-change-rules
// Variable turnorder by Lou
this.turnorder = true; //LOU variable turn order is now default
this.currentOrder = 0; //turn position in the order list
this.passOrder = 0; //turn position of next pass
this.turnMatrix = [[],[]]; //current round, next round
//if ONE, when a new game initializes, AI is Lou AILou instead of AILode.
//DONE: maybe later allow selecting individual AI of every bot
this.aiAlgorithm = 1; //LOU start with aiAlgorithm as default
this.worldMap = 4; //Fire & Ice World
};
function logPlayerNameFun(player) {
return logColored ? getFullNameColored(player) : getFullName(player);
}
var state = new State();
function getCurrentPlayer() {
return game.players[state.currentPlayer];
}
var nextButtonFun = null; //for shortcut
var fastButtonFun = null; //for shortcut
var fastestButtonFun = null; //for shortcut
var fastMode = false;
var fastestMode = false;
var nscount = 0;
var nsfuncount = 0;
var showingNextButtonPanel = false;
// When this function is called, everything must be set up and ready for the next state (which will now become the current state) - this may mean initNewStateType must be called before it
// This function may show a "next" button to the player or handle UI in other ways, that is, if this function did nothing, the game would just keep going on with no possible interaction
// progress = whether the state change progresses the game (= should pause for the player)
function gameLoopNonBlocking(type, progress) {
callbackState = CS_GAMELOOP;
state.type = type; //set that here so that the state in savegame is correct when using the "save" button now.
var nsfun = function(type) {
var noActorCallback = function() {
popupElement.innerHTML = ''; // Remove any human UI that remained
state.transitionState();
gameLoopNonBlocking(state.type, true);
};
var executeResultCallback = function(player, result) {
displayLog();
var error = state.executeResult(player, result);
if(error == '') noActorCallback();
return error;
};
displayLog();
nextButtonFun = null;
fastButtonFun = null;
showingNextButtonPanel = false;
state.executeActor(executeResultCallback, noActorCallback);
if(game.players.length > 0) {
drawHud();
drawMap();
}
};
// Do not show "next" button if fast mode, or if not a progression state
var fast = !progress || fastMode || type == S_PRE;
if(fast) {
// Turn off fastMode again when it was the action of the current player again.
if(getCurrentPlayer() && getCurrentPlayer().human && !fastestMode && state.type != S_LEECH) fastMode = false;
// First let previous player's functions all finish
// Without this, some stuff breaks, due to actor functions still doing some things after calling callbacks (mostly the human actor though)
// This makes it slower, but in fact nicer, it makes the screen being redrawn between every action of every AI player, which gives a nice progressive update animation.
// A possible speedup (losing the animation effect) is to only do the setTimeout if the current player is the human player
window.setTimeout(bind(nsfun, type), 0);
} else {
// Slow (that is, requiring user interaction)
window.setTimeout(function() {
nextButtonFun = bind(nsfun, type);
fastButtonFun = bind(function(type) {
fastMode = true;
nsfun(type);
}, type);
fastestButtonFun = bind(function(type) {
fastMode = true;
fastestMode = true;
nsfun(type);
}, type)
showingNextButtonPanel = true;
displayLog();
drawHud();
drawMap();
}, 0);
}
}
function gameLoopBlocking(isFinishedFun) {
state.initNewStateType(state.type);
for(;;) {
var callbackcalled = false;
var executeResultCallback = function(player, result) {
var error = state.executeResult(player, result);
state.transitionState();
callbackcalled = true;
return error;
};
var noActorCallback = function() {
callbackcalled = true;
state.transitionState();
};
state.executeActor(executeResultCallback, noActorCallback);
if(!callbackcalled && state.type != S_GAME_OVER && !isFinishedFun()) {
console.log('error: callback must be called in blocking gameloop. ' + getCallBackStateDebugString());
addLog('error: callback must be called in blocking gameloop');
return;
}
if(isFinishedFun()) return;
}
}
function getInitialDwellingsLeft(player) {
var numplaced = 8 - player.b_d;
return getNumInitialDwellings(player) - numplaced;
}
function getInitialDwellingsDone(player) {
return 8 - player.b_d;
}
var startTime;
//TODO: this function probably belongs in rules.js (but not the addLog things)
function addEndGameScore() {
//cult tracks
for(var i = C_F; i <= C_A; i++) {
addLog('');
var scores = getCultEndScores(i);
for(var j = 0; j < scores.length; j++) {
if(scores[j] != 0) {
addLog(logPlayerNameFun(game.players[j]) + ' gets ' + scores[j] + ' VP from ' + getCultName(i) + ' cult');
game.players[j].addVP(scores[j], 'cult', ['fire', 'water', 'earth', 'air'][i - C_F]);
}
}
}
//network
var networkscores = getNetworkEndScores();
addLog('');
for(var j = 0; j < networkscores.length; j++) {
if(networkscores[j][0] != 0) {
addLog(logPlayerNameFun(game.players[j]) + ' gets ' + networkscores[j][0] + ' VP from network size ' + networkscores[j][1]);
game.players[j].addVP(networkscores[j][0], 'network', 'network');
}
}
//expansion final scoring
var finalscores = getFinalScores();
addLog('');
for(var j = 0; j < finalscores.length; j++) {
if(finalscores[j][0] != 0) {
addLog(logPlayerNameFun(game.players[j]) + ' gets ' + finalscores[j][0] + ' VP from ' + finalScoringCodeNames[game.finalscoring] + ' ' + finalscores[j][1]);
game.players[j].addVP(finalscores[j][0], 'final', 'final');
}
}
//resources
var scores = getResourceEndScores();
addLog('');
for(var i = 0; i < game.players.length; i++) {
addLog(logPlayerNameFun(game.players[i]) + ' gets ' + scores[i] + ' VP from resources');
game.players[i].addVP(scores[i], 'resources', 'resources');
}
//log final scores
addLog('');
addLog(logUpsideDown ? 'FINAL SCORES ^' : 'FINAL SCORES');
for(var i = 0; i < game.players.length; i++) {
addLog(logPlayerNameFun(game.players[i]) + ': ' + game.players[i].vp);
}
addLog('');
var date = new Date();
var endTime = date.getTime();
addLog(' TIME - elapsed run time: ' + (endTime-startTime)/1000. + ' seconds ');
}
function wrapPlayer(i) {
return wrap(i, 0, game.players.length);
}
function getOrder(i) {
return state.turnMatrix[0][i];
}
State.prototype.selectNextActionPlayer_ = function() {
var count = 0;
while(true) {
if(count > game.players.length) throw new Error('this function should not be called if everyone passed');
// Variable turnorder by Lou
if(this.turnorder && this.round > 1) {
this.currentOrder++;
if(this.currentOrder == game.players.length) this.currentOrder = 0;
this.currentPlayer = this.turnMatrix[0][this.currentOrder];
if(this.currentPlayer < 0) throw new Error('selectNextActionPlayer should not return negative');
if(!game.players[this.currentPlayer].passed) break; //LOU previous problem area
count++;
} else {
this.currentPlayer = wrapPlayer(this.currentPlayer + 1);
if(!game.players[this.currentPlayer].passed) break;
count++;
}
}
};
//returns false if all dwellings are placed
State.prototype.selectNextInitialDwellingPlayer_ = function() {
//tiers:
//0: busy placing first dwellings.
//1: all except chaos have 1 dwelling.
//2: all except chaos have 2 dwellings.
//3: nomads have 3th dwelling
//4: chaos magicians have dwelling. Everything done.
var tier = 4;
for(var i = 0; i < game.players.length; i++) {
var player = game.players[i];
var done = getInitialDwellingsDone(player);
if(tier >= 1 && player.faction != F_CHAOS && done < 1) { tier = 0; break; }
if(tier >= 2 && player.faction != F_CHAOS && done < 2) tier = 1;
if(tier >= 3 && player.faction == F_NOMADS && done < 3) tier = 2;
if(tier >= 4 && player.faction == F_CHAOS && done == 0) tier = 3;
}
if(tier == 4) {
return false;
} else {
if(tier == 0) {
this.currentPlayer = wrapPlayer(this.currentPlayer + 1);
if(game.players[this.currentPlayer].faction == F_CHAOS) this.currentPlayer = wrapPlayer(this.currentPlayer + 1);
}
else if(tier == 1) {
for(var i = 0; i < game.players.length; i++) {
var j = wrapPlayer(this.startPlayer - 1 - i);
if(game.players[j].faction != F_CHAOS && getInitialDwellingsDone(game.players[j]) < 2) { this.currentPlayer = j; break; }
}
}
else if(tier == 2) {
for(var i = 0; i < game.players.length; i++) {
if(game.players[i].faction == F_NOMADS) { this.currentPlayer = i; break; }
}
}
else if(tier == 3) {
for(var i = 0; i < game.players.length; i++) {
if(game.players[i].faction == F_CHAOS) { this.currentPlayer = i; break; }
}
}
}
return true;
};
// switches to new kind of state type (chooses correct starting player for it, returns the given type so that you can assign it)
// Set current player to start player for that state type, if relevant
// Only call when starting a next phase of the game (e.g. placing initial dwellings, or starting a new round), but not when switching from power leeching back to actions in current round.
// In other words, only call when the concept of a starting player for this kind of phase is relevant.
State.prototype.initNewStateType = function(type) {
if(type == S_PRE) {
}
else if(type == S_INIT_FACTION) {
var date = new Date();
startTime = date.getTime();
recalculateColorMaps(); // there may already be colors in it if preset factions were set
this.currentPlayer = this.startPlayer;
this.numHandledForState = 0;
}
else if(type == S_INIT_FACTION_COLOR) {
}
else if(type == S_INIT_FAVOR) {
}
else if(type == S_INIT_AUX_COLOR) {
this.currentPlayer = this.startPlayer;
this.numHandledForState = 0;
}
else if(type == S_INIT_DWELLING) {
this.currentPlayer = wrapPlayer(this.startPlayer - 1); // so that selectNextInitialDwellingPlayer_ makes it correct
this.selectNextInitialDwellingPlayer_();
}
else if(type == S_INIT_BONUS) {
this.currentPlayer = wrapPlayer(this.startPlayer - 1); //last player chooses bonus tile first
}
else if(type == S_ACTION) {
this.currentPlayer = this.startPlayer;
this.currentOrder = 0;
}
else if(type == S_LEECH) {
}
else if(type == S_CULT) {
}
else if(type == S_CULTISTS) {
}
else if(type == S_ROUND_END_DIG) {
this.currentPlayer = this.startPlayer;
this.currentOrder = 0;
}
else if(type == S_GAME_OVER) {
//Nothing to do here.
}
return type;
};
//The inner if-else part of transitionState
State.prototype.transitionStateCore_ = function(next_state) {
if(this.type == S_PRE) {
next_state = this.initNewStateType(S_INIT_FACTION);
}
else if(this.type == S_INIT_FACTION) {
next_state = S_INIT_FACTION_COLOR;
}
else if(this.type == S_INIT_FACTION_COLOR) {
next_state = S_INIT_FAVOR;
}
else if(this.type == S_INIT_FAVOR) {
var nextplayer = wrapPlayer(this.currentPlayer + 1);
if(this.numHandledForState >= game.players.length /*this means we went fully around*/) {
recalculateColorMaps();
next_state = this.initNewStateType(S_INIT_AUX_COLOR);
} else {
next_state = S_INIT_FACTION;
this.currentPlayer = nextplayer;
}
}
else if(this.type == S_INIT_AUX_COLOR) {
var nextplayer = wrapPlayer(this.currentPlayer + 1);
if(this.numHandledForState >= game.players.length /*this means we went fully around*/) {
createAuxColorToPlayerMap();
next_state = this.initNewStateType(S_INIT_DWELLING);
} else {
this.currentPlayer = nextplayer;
}
}
else if(this.type == S_INIT_DWELLING) {
if(!this.selectNextInitialDwellingPlayer_()) {
calculateTownClusters();
next_state = this.initNewStateType(S_INIT_BONUS);
}
}
else if(this.type == S_INIT_BONUS) {
var nextplayer = wrapPlayer(this.currentPlayer - 1);
if(game.players[nextplayer].bonustile != T_NONE) {
startNewRound();
this.passOrder = 0;
next_state = this.initNewStateType(S_ACTION);
} else {
this.currentPlayer = nextplayer;
}
}
else if(this.type == S_ACTION) {
var passcount = 0;
var firstpass = -1;
for(var i = 0; i < game.players.length; i++) {
if(game.players[i].passed) {
passcount++;
firstpass = i;
}
}
if(passcount == 1) {
this.startPlayer = firstpass;
}
if(passcount == game.players.length) {
this.currentPlayer = this.startPlayer;
this.currentOrder = 0;
if(this.round == 6 && this.next_type == S_ACTION) {
addEndGameScore();
next_state = S_GAME_OVER;
} else {
next_state = this.initNewStateType(S_ROUND_END_DIG);
}
} else {
if(this.next_type == S_ACTION) {
this.selectNextActionPlayer_();
}
}
}
else if(this.type == S_LEECH) {
this.leechi++;
if(this.leechi >= this.leecharray.length) {
// leeching done. Reset state and handle cultists.
var player = game.players[this.currentPlayer];
var cultists = player && player.faction == F_CULTISTS;
var shapeshifters = player.getFaction().codename == 'shapeshifters';
var taken = this.leechtaken > 0;
if(cultists && taken) {
setHelp('player ' + player.name + ' choose cultists track');
next_state = S_CULTISTS;
} else if(state.fireiceerrata && shapeshifters && taken) {
setHelp('player ' + player.name + ' convert 1vp to 1 power token?');
next_state = S_CULTISTS;
} else {
// check if players actually declined. If they were full on power, it does not count as decline
var declined = false;
if(!taken) {
for(var i = 0; i < this.leecharray.length; i++) {
var leecher = game.players[this.leecharray[i][0]];
if(leecher.pw0 > 0 || leecher.pw1 > 0) {
declined = true;
break;
}
}
}
if(taken || declined) {
player.getFaction().getGaveLeechIncome(player, taken);
// TODO: add log if other faction gave resources too
if(cultists && declined) addLog(logPlayerNameFun(player) + ' receives one extra pw because everyone declined the cultists' + getGreyedResourcesLogString(player));
}
next_state = S_ACTION;
}
this.leechtaken = 0;
this.leechi = 0;
this.leecharray = [];
if(next_state == S_ACTION) this.selectNextActionPlayer_();
}
}
else if(this.type == S_CULTISTS) {
// Cultists leeching is a one time state that goes to next action immediately after.
next_state = S_ACTION;
this.selectNextActionPlayer_();
}
else if(this.type == S_CULT) {
// Nothing to do, should go to previous state with stack
}
else if(this.type == S_PRIEST_COLOR) {
// Nothing to do, should go to previous state with stack
}
else if(this.type == S_ROUND_END_DIG) {
for(;;) {
this.currentPlayer = wrapPlayer(this.currentPlayer + 1);
if(this.currentPlayer == this.startPlayer) {
startNewRound();
this.passOrder = 0;
next_state = this.initNewStateType(S_ACTION);
break;
}
var digs = getRoundBonusDigs(game.players[this.currentPlayer], this.round);
if(digs > 0) break;
}
}
else if(this.type == S_GAME_OVER) {
//Nothing to do here.
}
return next_state;
};
// Go from the current state to the next state (which may be the same, or different, type)
// Some code seems duplicated here (e.g. selectNextInitialDwellingPlayer_ called in two places), but that is
// because every combination of from- and to- state may require its own code path.
State.prototype.transitionState = function() {
if(this.type != S_PRIEST_COLOR && game.players[this.currentPlayer].priestorcolor > 0) {
this.typestack.push(this.type);
this.currentPlayerStack.push(this.currentPlayer);
this.type = S_PRIEST_COLOR;
return;
}
if(this.type != S_PRIEST_COLOR && game.players[this.prevPlayer].priestorcolor > 0) {
this.typestack.push(this.type);
this.currentPlayerStack.push(this.currentPlayer);
this.type = S_PRIEST_COLOR;
this.currentPlayer = this.prevPlayer;
return;
}
// This if is specifically for having free cult income from spades at the end of the round (for acolytes)
if((this.type == S_ROUND_END_DIG || this.type == S_CULT) && game.players[this.currentPlayer].freecult > 0) {
// TODO: support this with state stack instead
if(this.type != S_CULT) {
this.typestack.push(this.type);
this.currentPlayerStack.push(this.currentPlayer);
}
this.type = S_CULT;
return;
}
if(this.typestack.length > 0) {
this.type = this.typestack.pop();
this.currentPlayer = this.currentPlayerStack.pop();
}
var prevPlayer = this.currentPlayer;
this.prevPlayer = this.currentPlayer;
callbackState = CS_TRANSITION;
var next_state = this.next_type == S_NONE ? this.type : this.next_type;
next_state = this.transitionStateCore_(next_state);
//gameLoopNonBlocking(next_state, true);
this.type = next_state;
// TODO: this is so hacky, improve it
if(this.type != S_PRIEST_COLOR && (game.players[prevPlayer].priestorcolor > 0 || game.players[this.currentPlayer].priestorcolor > 0)) {
var p = (game.players[prevPlayer].priestorcolor > 0 ? prevPlayer : this.currentPlayer);
this.typestack.push(this.type);
this.currentPlayerStack.push(this.currentPlayer);
this.type = S_PRIEST_COLOR;
this.currentPlayer = p;
return;
}
};
//Actually executes the current state after choosing right player, possible transitions other next states, etc...
//the callback and the transitionIfNoActorCallback must do at least transitionState in them. noActorCallback is used if no actor handles the state with a callback to let it transition anyway.
State.prototype.executeActor = function(callback, transitionIfNoActorCallback) {
state.showResourcesPlayer = state.currentPlayer;
state.next_type = S_NONE;
callbackState = CS_EXECUTE;
if(this.type == S_PRE) {
renderPreScreen(200, 300, startGameButtonFun, startRandomGameButtonFun, startBeginnerGameButtonFun, startQuickGameButtonFun);
//transitionIfNoActorCallback();
}
else if(this.type == S_INIT_FACTION) {
this.numHandledForState++;
var player = game.players[this.currentPlayer];
if(player.faction == F_NONE) {
setHelp('player ' + player.name + ' choose faction');
callbackState = CS_ACTOR;
player.actor.chooseFaction(this.currentPlayer, callback);
} else {
// The player already has a faction, this happens when a "preset" game type was chosen were factions were picked or random
trySetFaction(player, player.getFaction()); //inits it (such as setting player.color)
initPlayerFaction(player);
addLog(logPlayerNameFun(player) + ' preassigned faction: ' + getFactionCodeName(player.getFaction()) + getGreyedResourcesLogString(player));
transitionIfNoActorCallback();
}
}
else if(this.type == S_INIT_FACTION_COLOR) {
var player = game.players[this.currentPlayer];
if(player.color == X || player.color == W || player.color == Z) {
setHelp('player ' + player.name + ' choose faction color');
callbackState = CS_ACTOR;
player.actor.chooseAuxColor(this.currentPlayer, callback);
} else {
transitionIfNoActorCallback();
}
}
else if(this.type == S_INIT_FAVOR) {
var player = game.players[this.currentPlayer];
if(player.getFaction().hasInitialFavorTile()) {
setHelp('player ' + player.name + ' choose initial favor tile');
callbackState = CS_ACTOR;
player.actor.chooseInitialFavorTile(this.currentPlayer, callback);
} else {
transitionIfNoActorCallback();
}
}
else if(this.type == S_INIT_AUX_COLOR) {
this.numHandledForState++;
var player = game.players[this.currentPlayer];
if(player.color == O) {
setHelp('player ' + player.name + ' choose faction color');
callbackState = CS_ACTOR;
player.actor.chooseAuxColor(this.currentPlayer, callback);
} else {
if(player.auxcolor == N) player.auxcolor = player.color;
transitionIfNoActorCallback();
}
}
else if(this.type == S_INIT_DWELLING) {
setHelp('player ' + game.players[this.currentPlayer].name + ' place initial dwelling');
callbackState = CS_ACTOR;
game.players[this.currentPlayer].actor.chooseInitialDwelling(this.currentPlayer, callback);
}
else if(this.type == S_INIT_BONUS) {
setHelp('player ' + game.players[this.currentPlayer].name + ' choose bonus tile');
callbackState = CS_ACTOR;
game.players[this.currentPlayer].actor.chooseInitialBonusTile(this.currentPlayer, callback);
}
else if(this.type == S_ACTION) {
var player = game.players[this.currentPlayer];
setHelp('player ' + player.name + ' action');
actionEl.innerHTML = '';
callbackState = CS_ACTOR;
player.actor.doAction(this.currentPlayer, callback);
}
else if(this.type == S_LEECH) {
var leech = this.leecharray[this.leechi];
setHelp('player ' + game.players[leech[0]].name + ' leech decision');
var amount = actualLeechAmount(game.players[leech[0]], leech[1]);
if(amount > 0) {
callbackState = CS_ACTOR;
state.showResourcesPlayer = leech[0];
game.players[leech[0]].actor.leechPower(leech[0], this.currentPlayer, amount, amount - 1, this.round, this.leechtaken, this.leecharray.length - this.leechi - 1, callback);
} else {
addLog(logPlayerNameFun(game.players[leech[0]]) + ' could not leech, power already full' + getGreyedResourcesLogString(game.players[leech[0]]));
transitionIfNoActorCallback();
}
}
else if(this.type == S_CULTISTS) {
if(state.fireiceerrata && game.players[this.currentPlayer] && game.players[this.currentPlayer].getFaction().codename == 'shapeshifters') {
state.showResourcesPlayer = state.currentPlayer;
state.next_type = S_NONE;
callbackState = CS_ACTOR;
game.players[this.currentPlayer].actor.chooseShapeshiftersConversion(this.currentPlayer, callback);
} else {
callbackState = CS_ACTOR;
game.players[this.currentPlayer].actor.chooseCultistTrack(this.currentPlayer, callback);
}
}
else if(this.type == S_CULT) {
callbackState = CS_ACTOR;
game.players[this.currentPlayer].actor.chooseCultistTrack(this.currentPlayer, callback);
}
else if(this.type == S_PRIEST_COLOR) {
callbackState = CS_ACTOR;
game.players[this.currentPlayer].actor.chooseAuxColor(this.currentPlayer, callback);
}
else if(this.type == S_ROUND_END_DIG) {
var j = this.currentPlayer;
var player = game.players[j];
var digs = getRoundBonusDigs(player, this.round);
if(digs > 0) {
var res = player.getActionIncome(A_SPADE);
if(res[R_SPADE] > 0) {
player.spades = digs; // initPlayerTemporaryTurnState at beginning of every turn action attempt will set it back to 0, so no need to decrease it here later in the callback
callbackState = CS_ACTOR;
player.actor.doRoundBonusSpade(j, callback);
} else {
addIncome(player, mulIncome(res, digs));
transitionIfNoActorCallback();
}
} else {
transitionIfNoActorCallback();
}
}
else if(this.type == S_GAME_OVER) {
setHelp('');
drawMap();
drawHud();
}
};
function getGreyedResourcesLogString(player) {
return ' <span style="color:#bbb">[' + getPlayerResourcesString(player, false) +
', ' + player.cult[0] + '/' + player.cult[1] + '/' + player.cult[2] + '/' + player.cult[3] + ' cult' +
', ' + player.vp + 'vp]</span>'
}
function getCultDifferenceString(before, after) {
var result = '';
for(var i = C_F; i <= C_A; i++) {
var b = before[i - C_F];
var a = after[i - C_F];
if(b != a) {
if(result.length > 0) result += ' ';
var diff = a - b;
if(diff > 0) result += '+';
if(diff != 1) result += diff;
result += getCultName(i);
}
}
return result;
}
//Actually executes the current state after choosing right player, possible transitions other next states, etc...
State.prototype.executeResult = function(playerIndex, result) {
var player = game.players[playerIndex];
callbackState = CS_CALLBACK;
if(this.type == S_PRE) {
}
else if(this.type == S_INIT_FACTION) {
var faction = result;
var error = trySetFaction(player, faction);
if(error == '') {
initPlayerFaction(player);
addLog(logPlayerNameFun(player) + ' chose faction: ' + getFactionCodeName(faction) + getGreyedResourcesLogString(player));
}
else addLog(logPlayerNameFun(player) + ' chose illegal faction: ' + getFactionCodeName(faction));
recalculateColorMaps();
return error;
}
else if(this.type == S_INIT_FACTION_COLOR) {
var color = result;
var error = '';
// TODO: extract this to function in rules.js
if(!(color >= CIRCLE_BEGIN && color <= CIRCLE_END)) {
error = 'invalid color';
} else if(colorToPlayerMap[color] != undefined) {
error = 'color already chosen';
} else {
if(player.color == X) player.woodcolor = player.auxcolor = color;
else if(player.color == Z) {
player.auxcolor = color;
player.woodcolor = color;
player.colors[color - R] = true;
}
else player.auxcolor = color;
recalculateColorMaps();
}
if(error == '') {
addLog(logPlayerNameFun(player) + ' chose faction color: ' + getColorName(color) + getGreyedResourcesLogString(player));
}
else addLog(logPlayerNameFun(player) + ' chose illegal faction color: ' + getColorName(color));
return error;
}
else if(this.type == S_INIT_FAVOR) {
var tile = result;
var error = giveFavorTile(player, tile);
if(error == '') addLog(logPlayerNameFun(player) + ' took favor tile ' + getTileCodeName(tile) + getGreyedResourcesLogString(player));
else addLog(logPlayerNameFun(player) + ' attempted illegal favor tile ' + tile + '. Error: ' + error);
return error;
}
else if(this.type == S_INIT_AUX_COLOR) {
var color = result;
var error = '';
// TODO: extract this to function in rules.js
if(!(color >= CIRCLE_BEGIN && color <= CIRCLE_END)) {
error = 'invalid color';
} else if(colorToPlayerMap[color] != undefined) {
error = 'color already chosen';
} else {
player.auxcolor = color;
createAuxColorToPlayerMap();
}
if(error == '') {
addLog(logPlayerNameFun(player) + ' chose faction color: ' + getColorName(color) + getGreyedResourcesLogString(player));
}
else addLog(logPlayerNameFun(player) + ' chose illegal faction color: ' + getColorName(color));
return error;
}
else if(this.type == S_INIT_DWELLING) {
var x = result[0];
var y = result[1];
var error = placeInitialDwelling(player, x, y);
if(error == '') addLog(logPlayerNameFun(player) + ' placed initial dwelling at ' + printCo(x, y) + getGreyedResourcesLogString(player));
else addLog(logPlayerNameFun(player) + ' attempted illegal initial dwelling ' + printCo(x, y) + '. Error: ' + error);
return error;
}
else if(this.type == S_INIT_BONUS) {
var tile = result;
var error = giveBonusTile(player, tile);
if(error == '') addLog(logPlayerNameFun(player) + ' took bonus tile ' + getTileCodeName(tile) + getGreyedResourcesLogString(player));
else addLog(logPlayerNameFun(player) + ' attempted illegal bonus tile ' + tile + '. Error: ' + error);
return error;
}
else if(this.type == S_ACTION) {
var actions = result;
var resbefore = [player.c, player.w, player.p, player.pw2, player.vp];
var cultbefore = [player.cult[0], player.cult[1], player.cult[2], player.cult[3]];
var tokensbefore = player.pw0 + player.pw1 + player.pw2;
var error = tryActions(player, actions);
var resafter = [player.c, player.w, player.p, player.pw2, player.vp];
var cultafter = [player.cult[0], player.cult[1], player.cult[2], player.cult[3]];
var tokensafter = player.pw0 + player.pw1 + player.pw2;
subtractIncome(resafter, resbefore);
var resstring = incomeToStringWithPluses(resafter);
var cultstring = getCultDifferenceString(cultbefore, cultafter);
var diffstring = resstring + (resstring.length > 0 && cultstring.length > 0 ? ' ' : '') + (tokensbefore == tokensafter ? '' : ' burnt:' + (tokensbefore - tokensafter)) + cultstring;
if(diffstring.length > 0) diffstring = ' [' + diffstring + ']';
if(error == '') addLog(logPlayerNameFun(player) + ' Action: <b>' + actionsToString(actions) + '</b>' + diffstring + getGreyedResourcesLogString(player));
else addLog(logPlayerNameFun(player) + ' attempted illegal action: ' + actionsToString(actions) + ' Error: ' + error);
if(error == '') {
var leech = getLeechEffect(player.index, actions);
if(leech.length > 0) {
state.leecharray = leech;
state.leechi = 0;
state.leechtaken = 0;
state.next_type = S_LEECH;
} else {
state.next_type = S_ACTION;
}
}
return error;
}
else if(this.type == S_LEECH) {
var leech = result;
var amount = this.leecharray[this.leechi][1];
if(leech) {
leechPower(player, amount);
addLog(logPlayerNameFun(player) + ' leeched ' + amount + ' from ' + logPlayerNameFun(getCurrentPlayer()) + getGreyedResourcesLogString(player));
state.leechtaken++;
} else {
addLog(logPlayerNameFun(player) + ' declined to leech ' + amount + ' from ' + logPlayerNameFun(getCurrentPlayer()) + getGreyedResourcesLogString(player));
}
}
else if(this.type == S_CULTISTS) {
if(state.fireiceerrata && player.getFaction().codename == 'shapeshifters') {
callbackState = CS_CALLBACK;
if(result) {
player.vp--;
player.pw2++;
}
addLog(logPlayerNameFun(player) + (result ? 'converted 1vp to a power token' : 'declined to convert 1vp to a power token') + getGreyedResourcesLogString(player));
} else {
var cult = result;
giveCult(player, cult, 1);
addLog(logPlayerNameFun(player) + ' used cultists ability on ' + getCultName(cult) + getGreyedResourcesLogString(player));
}
}
else if(this.type == S_CULT) {
var cult = result;
giveCult(player, cult, 1);
player.freecult--;
addLog(logPlayerNameFun(player) + ' chose cult track ' + getCultName(cult) + getGreyedResourcesLogString(player));
}
else if(this.type == S_PRIEST_COLOR) {
var color = result;
var error = unlockColorPriest(player, color);
if(error == '') {
if(color == Z) addLog(logPlayerNameFun(player) + ' chose priest instead of color. ' + getGreyedResourcesLogString(player));
else addLog(logPlayerNameFun(player) + ' chose priest color: ' + getColorName(color) + getGreyedResourcesLogString(player));
}
else addLog(logPlayerNameFun(player) + ' chose illegal priest color: ' + getColorName(color));
return error;
}
else if(this.type == S_ROUND_END_DIG) {
var digs = result;
var error = canDoRoundBonusDigs(player, digs);
if(error != '') {
addLog(logPlayerNameFun(player) + ' attempted illegal round bonus dig at: ' + printCos(digs) + '. Error: ' + error);
return error; //the error is ignored for humans, so that human player can click invalid tiles if no valid ones exist. TODO: throw errors instead and give UI to skip it instead
}
for(var i = 0; i < digs.length; i++) {
error = tryRoundBonusDig(player, digs[i][0], digs[i][1], digs[i][2]);
if(error == '') {
addLog(logPlayerNameFun(player) + ' did round bonus dig at ' + printCo(digs[i][1], digs[i][2]) + getGreyedResourcesLogString(player));
} else {
addLog(logPlayerNameFun(player) + ' attempted illegal round bonus dig at: ' + printCos(digs) + '. Error: ' + error);
return error;
}
}
return error;
}
else if(this.type == S_GAME_OVER) {
}
return '';
};