-
Notifications
You must be signed in to change notification settings - Fork 10
/
turn_sequence.pl
1550 lines (1170 loc) · 91.8 KB
/
turn_sequence.pl
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
% First saved: 02/02/2011
% Last saved: 22/04/2011
%
% Incremental Save
%
% Status:
% OK (now with Creature Combat™!)
% Some confusion with stack_resolves; Document and fix.
%
% Doing:
% Done removing debug_cue output
% Noticed possible incorrect handling of mana abilities by stack/cue
% updating player_actions (with Gleemin in mind)
% Fixed untap (was failing on untapped permanents- watch)
% Todo:
% implement all cleanup step actions.
% implement all state-based actions.
% implement Cleanup step correctly.
% add statistics etc output options to debug.
% Fix the slight fudge.
% step_ends can be skipped in [] steps and let phase_ends
% do its job (check for [] Stack and 'Full' Cue).
% remove asserts of active_player (once not needed any more)
% NOTES:
% Debug -> break/0 is a bit of a hack (it returns the play code for
% a cancelled action). It should return its own code and be
% intercepted by stack and so on.
% Keep an eye on step_ends. It's a bit confusing.
% Fixed receives_priority hacks (keep an eye out)
% The slight fudge: [] steps should not exist and Phase
% actions should be taken before their steps begin.
% noticed that the game doesn't yet know
% that the cards in players' stack, exile and battlefield
% are also in the _shared_ stack, exile and battlefield.
% At some point I'll need to deal with all those unbound
% variables in predicate heads...
% I'd like to return New_play as a list
% in order to remember playing land this turn without
% using asserts/retracts.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Swi Compatibility %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
:- set_prolog_flag(backquoted_string,true).
% Swi LPA backtic string compatibility.
:- style_check(-singleton).
% Stop Swi warning on singleton vars.
:- style_check(-discontiguous).
% Stop Swi warning on discontiguous clauses.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Turn facts %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
:- dynamic active_player/1.
:- dynamic new_game/1.
% :- dynamic turn/7.
% I may not need to do that after all.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%phases/1 16/12/2010
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Phase list
phases(['Beginning', 'First Main', 'Combat', 'Second Main', 'Ending']).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%phase/1 16/12/2010
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% What phases?
phase('Beginning').
phase('First Main').
phase('Combat').
phase('Second Main').
phase('Ending').
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%phase/2 16/12/2010
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% phase(Name, Steps): steps of a phase.
phase('Beginning', [
'Untap',
'Upkeep',
'Draw'
]).
phase('First Main', []).
phase('Combat', [
'Beginning of Combat',
'Declare Attackers',
'Declare Blockers',
'Combat Damage',
'End of Combat'
]).
phase('Second Main', []).
phase('Ending', [
'End',
'Cleanup'
]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%step/1 16/12/2010
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% What steps?
step('Untap').
step('Upkeep').
step('Draw').
step('Beginning of Combat').
step('Declare Attackers').
step('Declare Blockers').
step('Combat Damage').
step('End of Combat').
step('End').
step('Cleanup').
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%step/2 16/12/2010
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% step(Phase, Step): steps by phase.
step('Beginning', 'Untap').
step('Beginning', 'Upkeep').
step('Beginning', 'Draw').
step('First Main', []).
step('Combat', 'Beginning of Combat').
step('Combat', 'Declare Attackers').
step('Combat', 'Declare Blockers').
step('Combat', 'Combat Damage').
step('Combat', 'End of Combat').
step('Second Main', []).
step('Ending', 'End').
step('Ending', 'Cleanup').
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%actions/2 16/12/2010
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%actions/2 (1) 16/12/2010
% actions(Phase, Action), Game Actions by phase.
actions('Beginning', beginning).
actions('First Main', first_main).
actions('Second Main', second_main).
%%%%%%%%%%%%%%actions/2 (2) 16/12/2010
% actions(Step, Action) Game Actions by step.
actions('Untap', untap).
actions('Upkeep', upkeep).
actions('Draw', draw).
actions('Beginning of Combat', begin_combat).
actions('Declare Attackers', declare_attackers).
actions('Declare Blockers', declare_blockers).
actions('Combat Damage', damage).
actions('End of Combat', end_combat).
actions('End', end).
actions('Cleanup', cleanup).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%game_action/2 16/12/2010
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
game_action(untap).
game_action(upkeep).
game_action(draw).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Turn rules (1) %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%turn/7 30/12/2010
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* turn(+Player, +Phase, +Step, +Priority, +Cue, +Play, +Stack) */
% Implements a turn sequence.
%%%%%%%%%%%%%%turn/4 (0) 15/02/2011
% A player has conceded; the turn sequence ends
turn(_, _, _, _, _, 67, _):- !. % 67: [C]oncede
%%%%%%%%%%%%%%turn/4 (1) 08/03/2011
turn(_Player, _Phase, _Step, loss(Player, Condition), _Cue, _Play, _Stack):-
%Loss == loss(Player, Condition),
%Loss =.. [loss,Player,Condition],
write(Player - ` loses the game because of ` - Condition), nl, !.
%output(turn_sequence, [loss, Player]).
%%%%%%%%%%%%%%turn/4 (2) 29/12/2010
turn(Player, Phase, Step, Priority, Cue, Play, Stack):-
turn_begins(Player, Step), !,
phase_begins(Phase, Step), !,
step_begins(Step), !,
game_actions(Phase, Step), !,
receives_priority(Player, Step, Priority, Play, Stack, New_priority), !,
player_actions(Player, New_priority, Step, Phase, New_play), !,
stack_resolves(Stack, New_play, Cue, New_priority, New_cue, New_stack), !,
step_ends(Step, Phase, New_cue, New_priority, New_stack, New_step), !,
phase_ends(Phase, Step, New_phase), !,
turn_ends(Player, Phase, Step, New_player), !,
turn(New_player, New_phase, New_step, New_priority, New_cue, New_play, New_stack).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%turn_begins/2 25/12/2010
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* turn_begins(+Player, +Step) */
% "Every untap step there's a new active player"
% Marks the player whose turn it is as the active player.
%%%%%%%%%%%%%%turn_begins/2 (1) 25/12/2010
% If this is the beginning of the first untap phase in a game,
% the active player is the first player in the
% order_of_play, so don't look for a new active player.
turn_begins(Player, begins('Untap')):-
new_game(yes),
assert_active_player(Player),
output(turn_begins, ['new', Player]),
retractall(new_game(_)),
assert(new_game(no)).
%%%%%%%%%%%%%%turn_begins/2 (2) 25/12/2010
% If it's the beginning of the untap step,
% there's a new active player:
turn_begins(Player, begins('Untap')):-
assert_active_player(Player),
output(turn_begins, ['new', Player]).
%%%%%%%%%%%%%%turn_begins/2 (3) 02/01/2011
% During a Step other than the Untap or Cleanup
% report the active player
turn_begins(Player, Step_state):- % Swi, 19/06/11
Step_state =.. [State, Step],
(State == begins ;
State == ongoing),
(Step \== 'Untap',
Step \== 'Cleanup'),
assert_active_player(Player),
output(turn_begins, ['current', Player]).
% What about Cleanup steps where players receive
% priority? Should report (from another pred).
%%%%%%%%%%%%%%turn_begins/2 (4) 25/12/2010
% Otherwise, there is no new active player
% and no need to report the active player
turn_begins(Player, Step):-
true.
%%%%%%%%%%%%%%assert_active_player/1 28/01/2011
% If there is an active player in the kb
assert_active_player(Player):-
retractall(active_player(_)),
asserta(active_player(Player)).
% If there is no active player in the kb
assert_active_player(Player):-
asserta(active_player(Player)).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%phase_begins/2 25/12/2010
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* phase_begins(+Phase, +Step) */
% "A phase ends at its last Step and then there is a new phase.
%%%%%%%%%%%%%%phase_begins/2 (1) 30/12/2010
% A phase begins when its first step begins
phase_begins(Phase, begins(First_step)):-
phase(Phase, [ First_step | Rest ]),
output(phase_begins, [Phase]).
%%%%%%%%%%%%%%phase_begins/2 (2) 25/12/2010
% A phase with no steps begins here
phase_begins(Phase, begins([])):-
output(phase_begins, [Phase]).
%%%%%%%%%%%%%%phase_begins/3 (2) 25/12/2010
% Otherwise, it continues.
% phase_begins(Phase, State(Step)):- % Swi, 19/06/11
phase_begins(Phase, Step_state):-
Step_state =.. [State, Step],
output(phase_begins, [State, Step, Phase]).
% Could check membership of Step to Phase
% to make sure we're in the right phase/ step.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%step_begins/1 30/12/2010
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* step_begins(+Step) */
% Marks the beginning of the Step
% Also deals with "at end of Step" effects- to be implemented.
%%%%%%%%%%%%%%step_begins/2 (1) 30/12/2010
% Some phases have no steps:
% step_begins(State([])):- % Swi, 19/06/11
step_begins(State_step):-
State_step =.. [_State, []],
output(step_begins, []).
%%%%%%%%%%%%%%step_begins/2 (2) 30/12/2010
% A step begins:
step_begins(begins(Step)):-
output(step_begins, [Step]).
%%%%%%%%%%%%%%step_begins/2 (3) 30/12/2010
% Otherwise, the step goes on
step_begins(ongoing(Step)):-
output(step_begins, [ongoing, Step]).
%%%%%%%%%%%%%%step_begins/2 (4) 30/12/2010
% The end of the step is reported later.
step_begins(ends(Step)):-
true.
%%%% Notes %%%%
%%%%%%%%%%%%%%%
/*
Note rule 103.7a: In a two-player game,
the player who plays first skips the draw step
of his or her first turn.
*/
%%%%%%%%%%%%%%%%%%%%%%%%%%%%game_actions/2 30/12/2010
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* game_actions(+Phase, +Step) */
% Take the necessary game actions in this phase or step.
% This predicate will need to return a game state/ board position.
%%%%%%%%%%%%%%game_actions/2 (1) 30/12/2010
% If this is the beginning of the first step of the current phase
% and there are turn-based_actions to take in this phase
% take them, then take the turn-based actions in the
% first step of that phase.
game_actions(Phase, begins(First_step)):-
phase(Phase, [ First_step | Rest ]),
phase_actions(Phase),
step_actions(First_step).
%%%%%%%%%%%%%%game_actions/2 (2) 30/12/2010
% If the current phase has no steps,
% take the turn-based actions for this phase
% when its non-step "begins".
game_actions(Phase, begins([])):-
phase_actions(Phase).
% Slight fudge. State-based actions
% in phases without steps happen
% when those phases begin or end,
% not when their non-existent steps do.
%%%%%%%%%%%%%%game_actions/2 (3) 30/12/2010
% Otherwise, take the turn-based actions that need to
% be taken when the current step begins.
game_actions(Phase, begins(Step)):-
step_actions(Step).
%%%%%%%%%%%%%%game_actions/2 (X) 09/03/11
% At the end of the End of Combat step, all creaures
% and planeswalkers are removed from combat
game_actions(_Phase, ends('End of Combat')):-
end_actions,
combat_ends.
%%%%%%%%%%%%%%game_actions/2 (X) 09/03/11
% At the end of the Cleanup step, players discard
% down to seven cards, then other cleanup
% actions are performed
game_actions(_Phase, ends('Cleanup')):-
end_actions,
discard_to_max,
clear_damage,
at_end_of_turn.
%%%%%%%%%%%%%%game_actions/2 (4) 30/12/2010
% When a phase or step ends,
% all mana pools empty.
game_actions(Phase, ends(Step)):-
end_actions,
output(end_actions, []).
% Slight fudge again. end-of-phase
% actions will happen at the end
% of a phase.
%%%%%%%%%%%%%%game_actions/2 (5) 30/12/2010
% When a non-step ends,
% all mana pools empty.
game_actions(Phase, ends([])):-
end_actions,
output(end_actions, []).
%%%%%%%%%%%%%%game_actions/2 (6) 30/12/2010
% No turn-based actions are taken
% in the middle of a step or phase.
game_actions(Phase, ongoing(Step)):-
true.
%%%%%%%%%%%%%%phase_actions/1 25/12/2010
%%%%%%%%%%%%%%%%%%%%%%%
/* phase_actions(+Phase) */
% Take the game actions that need to be taken in this phase.
%%%%%%%%%%%%%%phase_actions/1 (1) 25/12/2010
% Take the necessary phase actions.
phase_actions(Phase):-
actions(Phase,Action),
output(phase_actions, [Phase]),
Action.
% ^^ Reflect-Fu!!
% There _is_ no separation
% 'twixt data and operation :P
%%%%%%%%%%%%%%phase_actions/1 (2) 25/12/2010
% If there are no actions in the current phase just succeed
phase_actions(Phase):-
true.
%%%%%%%%%%%%%%beginning/0 25/12/2010
%%%%%%%%%%%%%%%%%%%%%%%
% Beginning phase actions
beginning:- % true.
remove_summoning_sickness.
%%%%%%%%%%%%%%remove_summoning_sickness/0 09/03/11
%%%%%%%%%%%%%%%%%%%%%%%
% all creatures found to have summoning_sickness
% lose it now (they have been under their controller's control
% since the previous turn).
%%%%%%%%%%%%%%remove_summoning_sickness (1) 09/03/11
remove_summoning_sickness:-
active_player(Player),
zone(Player, 'Battlefield', Objects),
member(Object, Objects),
remove_state_all(Object, 'summoning sickness', Clear),
change_state(Object, 'Battlefield', Player, Clear),
fail.
%%%%%%%%%%%%%%remove_summoning_sickness (0) 09/03/11
remove_summoning_sickness.
%%%%%%%%%%%%%%first_main/0 25/12/2010
%%%%%%%%%%%%%%%%%%%%%%%
% First Main Phase actions
first_main:-
true.
%%%%%%%%%%%%%%second_main/0 25/12/2010
%%%%%%%%%%%%%%%%%%%%%%%
% Second Main Phase actions
second_main:-
true.
%%%%%%%%%%%%%%%step_actions 25/12/2010
%%%%%%%%%%%%%%%%%%%%%%%
/* step_actions(+Step) */
% Take the game actions that need to be taken in the current step
%%%%%%%%%%%%%%step_actions/1 (1) 25/12/2010
step_actions(Step):-
actions(Step, Action),
Action,
output(step_actions, [Step]).
%%%%%%%%%%%%%%step_actions/1 (2) 25/12/2010
% If there are no actions in the current step just succeed
step_actions(Step):-
true.
%%%%%%%%%%%%%%%untap/0 25/12/2010
%%%%%%%%%%%%%%%%%%%%%%%
% Untap Step actions
untap:-
active_player(Player),
zone(Player, 'Battlefield', Permanents),
findall(object(Name - Id, State),
(member(object(Name - Id, State), Permanents ),
member(tapped, State))
, Tapped),
untap(Player, Tapped).
% Untap all of a player's permanents.
untap(Player, []).
untap(Player, [Permanent | Rest]):-
tap_untap(Permanent, Player, 'untap'),
untap(Player, Rest).
% Needs checks for 'does not untap...' etc.
%%%%%%%%%%%%%%%upkeep/0 25/12/2010
%%%%%%%%%%%%%%%%%%%%%%%
% Upkeep step actions
upkeep:-
true.
%%%%%%%%%%%%%%%draw/0 25/12/2010
%%%%%%%%%%%%%%%%%%%%%%%
% Draw step actions
draw:-
active_player(Player),
draw_cards(Player, 1).
%%%%%%%%%%%%%%%begin_combat/0 25/12/2010
%%%%%%%%%%%%%%%%%%%%%%%
% Beginning of Combat step actions
begin_combat:- true.
%%%%%%%%%%%%%%%declare_attackers/0 25/12/2010
%%%%%%%%%%%%%%%%%%%%%%%
% Declare Attackers step actions
declare_attackers:-
active_player(Active_player),
declare_attackers(Active_player).
%%%%%%%%%%%%%%%declare_blockers/0 25/12/2010
%%%%%%%%%%%%%%%%%%%%%%%
% Declare Blockers step actions
declare_blockers:-
attackers([]).
declare_blockers:-
player(Non_active_player),
\+ active_player(Non_active_player),
declare_blockers(Non_active_player, _Blockers).
%%%%%%%%%%%%%%%damage/0 25/12/2010
%%%%%%%%%%%%%%%%%%%%%%%
% Damage step actions
damage:-
player(Non_active_player),
\+ active_player(Non_active_player),
attackers_order(Attackers_order),
blockers_order(Blockers_order),
combat_damage(Non_active_player, Blockers_order, Attackers_order).
%%%%%%%%%%%%%%%end_combat/0 25/12/2010
%%%%%%%%%%%%%%%%%%%%%%%
% End Combat step actions
end_combat:- true.
%%%%%%%%%%%%%%%end/0 25/12/2010
%%%%%%%%%%%%%%%%%%%%%%%
% End step actions
end:-
true.
%%%%%%%%%%%%%%%cleanup/0 25/12/2010
%%%%%%%%%%%%%%%%%%%%%%%
% Cleanup step actions
cleanup:- true.
%%%%%%%%%%%%%%end_actions/0 30/12/2010
%%%%%%%%%%%%%%%%%%%%%%%
% At the end of each phase and step, all mana pools empty
% This is really not named appropriately.
end_actions:- %true.
player(Player),
empty_mana_pool(Player),
fail.
end_actions.
%%%%%%%%%%%%%%discard_to_max/0 05/04/11
%%%%%%%%%%%%%%%%%%%%%%%
% The active player discards down to his or her
% maximum hand size
discard_to_max:-
player(Player),
hand_size(Player, Max_hand_size),
zone(Player, 'Hand', Cards),
length(Cards, Length),
Length > Max_hand_size,
prompt_switches(Cards, Switches),
discard_to_max(Player, Max_hand_size, Length, Switches).
% The player doesn't need to discard
discard_to_max.
discard_to_max(Player, Max_hand_size, Length, [Map, Switches]):-
Context = [Max_hand_size, Length, Map, Switches],
string_input(discard_to_max, Context, Input),
atom_to_list(Input, Discards),
Discard is Length - Max_hand_size,
length(Discards, Length_2), Length_2 = Discard ->
discard_to_max(Player, Map, Discards);
discard_to_max(Player, Max_hand_size, Length, [Map, Switches]).
discard_to_max(_Player, _Map, []).
discard_to_max(Player, Map, Discards):-
member(Switch, Discards),
member(Card - Switch, Map),
move_to_zone(Player, Card, 'Hand', 'Graveyard'),
remove(Switch, Discards, New_discards),
remove(Card-Switch, Map, New_map),
discard_to_max(Player, New_map, New_discards).
%%%%%%%%%%%%%%clear_damage/0 09/03/11
%%%%%%%%%%%%%%%%%%%%%%%
% all damage marked on permanents
% (including phased-out permanents)is removed
%%%%%%%%%%%%%%clear_damage (1) 09/03/11
clear_damage:-
zone(Player, 'Battlefield', Objects),
member(Object, Objects),
remove_state_all(Object, damage(_Amount), Clear),
change_state(Object, 'Battlefield', Player, Clear),
fail.
%%%%%%%%%%%%%%clear_damage (0) 09/03/11
clear_damage.
%%%%%%%%%%%%%%at_end_of_turn/0 05/04/11
%%%%%%%%%%%%%%%%%%%%%%%
% all "until end of turn" and "this turn" effects end.
at_end_of_turn:-
findall([Player, Zone, Effect, Handle],
( zone(Player, Zone, Objects),
member(Object, Objects),
object_handle(Object, Handle),
object_state(Handle, State),
( member(Effect, State),
Effect =.. [_,_,Duration],
( Duration = 'at end of turn' ;
Duration = 'until end of turn' ))),
Effects),
at_end_of_turn(Effects).
at_end_of_turn([]).
at_end_of_turn([ [Player, Zone, Effect, Handle] | Effects]):-
remove_state(Handle, Effect, Removed),
change_state(Handle, Zone, Player, Removed),
at_end_of_turn(Effects).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%receives_priority/3 30/12/2010
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* receives_priority(+Player, +Step, +Priority, +Play, +Stack, -New_priority) */
% Determines which player should get priority (=New_priority).
%%%% Notes %%%%
%%%%%%%%%%%%%%%
/*
I think I should only be assigning priority when
ongoing(Step), ongoing(Step)???
Players do get priority at the beginning of steps, no?
Depends. Normally players do receive priority at the beginning
of steps/ phases. The difference is that the game doesn't have
begins/ongoing/ends states. I use them just to know that it's not
time to fire turn-based actions. I could confine priority to
ongoing states... though that's a hack.
Actually, that's how I have it... change it?
*/
%%%%%%%%%%%%%%receives_priority/3 (1) 01/01/2011
% No player receives priority at the end of a step
receives_priority(Player, ends(Step), Priority, Play, Stack, []):-
true.
%%%%%%%%%%%%%%receives_priority/3 (2) 30/12/2010
% No player receives priority during the untap step
% receives_priority(Player, Status('Untap'), Priority, Play, [], []):-
receives_priority(Player, Step_state, Priority, Play, [], []):- % Swi, 19/06/11
Step_state =.. [State, 'Untap'],
( State = begins
->
output(receives_priority, [])
;
true).
%%%%%%%%%%%%%%receives_priority/3 (3) 30/12/2010
% No player receives priority in the Cleanup step
% unless there are objects on the stack.
% receives_priority(Player, Status('Cleanup'), Priority, Play, [], []):-
receives_priority(Player, Step_state, Priority, Play, [], []):- %19/06/11
Step_state =.. [State, 'Cleanup'],
( State = begins
->
output(receives_priority, [])
;
true).
% And this won't work exactly like that- the stack is built
% if there are objects to put on it. This should be implemented
% later on.
%%%%%%%%%%%%%%receives_priority/3 (4) 30/12/2010
% The active Player receives priority at the beginning of
% all other steps and phases.
receives_priority(Player, begins(_Step), _Priority, _Play, _Stack, New_priority):-
state_based_actions(Player, New_priority),
output(receives_priority, [New_priority]).
%%%%%%%%%%%%%%receives_priority/3 (5) 23/12/2010
% The active Player receives priority after a spell or ability
% (other than a mana ability) resolves.
% receives_priority(Player, _State(_Step), _Priority, 112, [resolved | _], New_priority):-
% Swi, 19/06/11
receives_priority(Player, _Step, _Priority, 112, [resolved | _], New_priority):-
state_based_actions(Player, New_priority),
output(receives_priority, ['new', New_priority]).
% ^ hack fixed here; watch.
%%%%%%%%%%%%%%receives_priority/3 (6) 30/12/2010
% During the Step, if a player passes, priority is passed
% to the next player in the order_of_play, after
% the current Priority player
% 'Pass' == 112
% receives_priority(_Player, _State(_Step), Priority, 112, _Stack, Final_priority):-
% Swi, 19/06/11
receives_priority(_Player, _Step, Priority, 112, _Stack, Final_priority):-
state_based_actions(Priority, New_priority),
% ^ This will eiher return the current priority player or
% a loss(Player,Condition) tuple.
pass_priority(New_priority, Final_priority),
% ^ Then this will either find the next player after Priority or
% return the same loss/2 tuple, causing the game to end.
output(receives_priority, ['new', Final_priority]).
%%%%%%%%%%%%%%receives_priority/3 (7) 30/12/2010
% During the step, if the current priority player
% does not pass, he or she receives priority again.
% receives_priority(_Player, _State(_Step), Priority, _Play, _Stack, New_priority):-
% Swi 19/06/11
receives_priority(_Player, _Step, Priority, _Play, _Stack, New_priority):-
state_based_actions(Priority, New_priority),
output(receives_priority, [New_priority]).
%%%%%%%%%%%%%%%pass_priority/2 26/12/2010
%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%pass_priority/2 (X) 08/03/11
% A player has lost the game. No player receives priority
pass_priority(loss(Player, Condition), loss(Player, Condition)).
%%%%%%%%%%%%%%pass_priority/2 (1) 26/12/2010
% Find the next player in the order of play
pass_priority(Current_player, New_player):-
order_of_play(Players),
next(Current_player, New_player, Players).
%%%%%%%%%%%%%%pass_priority/2 (2) 26/12/2010
% Reset the players pointer
pass_priority(Current_player, First_player):-
order_of_play([First_player | Rest]).
% Fixed a hack here. Check older versions if
% something looks wrong.
%%%%%%%%%%%%%%%state_based_actions/1 08/03/11
%%%%%%%%%%%%%%%%%%%%%%%
%%%% Notes %%%%
%%%%%%%%%%%%%%%
/*
If a player should lose the game, a loss/2 state is bound to
the new priority player, which should cause
turn_sequence to terminate.
*/
state_based_actions(Priority, Player):-
zero_life(Priority, Player), %!,
zero_cards(Priority, Player), %!,
ten_poison(Priority, Player), %!,
zero_toughness,
creature_destroyed,
deathtouch_damage.
% More state-based actions exist.
% Still need to add clauses to deal with loss conditions below.
%%%%%%%%%%%%%%%zero_life/1 08/03/11
%%%%%%%%%%%%%%%%%%%%%%%
zero_life(_Priority, loss(Player, zero_life)):-
life_total(Player, Life),
Life =< 0.
zero_life(_,_).
%%%%%%%%%%%%%%%zero_cards/1 08/03/11
%%%%%%%%%%%%%%%%%%%%%%%
zero_cards(_Priority, loss(Player, milled)):-
player_state(Player, State),
member(milled, State).
% This should ^^ be added by whatever attempted to
% draw a card from the Player's empty library.
zero_cards(_,_).
%%%%%%%%%%%%%%%ten_poison/1 08/03/11
%%%%%%%%%%%%%%%%%%%%%%%
ten_poison(_Priority, loss(Player, poisoned)):-
poison_counters(Player, 10).
% A player hasn't lost the game yet.
ten_poison(Player,Player).
% A player has.
ten_poison(_,_).
%%%% Notes %%%%
%%%%%%%%%%%%%%%
/*
If a player should lose, each player-loss check after the check
that is found to be true, will fail on its first clause and
succeed on its last clause. However, if no player should lost the
game, the last check (this one) will succeed on its second clause
and return the priority player's name (bound to the one it was
passed).
*/
%%%%%%%%%%%%%%%zero_toughness/0 08/03/11
%%%%%%%%%%%%%%%%%%%%%%%
% Subsequent clauses don't affect players.
zero_toughness:-
zone('Battlefield', Objects),
findall(Controller - Creature,
(member(Creature, Objects),
creature(Creature, _, _, _, 0, Controller)), % 0 Toughness
Creatures),
bury_creatures(Creatures).
% uncomment once tested
%zero_toughness(_Player).
bury_creatures([]).
bury_creatures([Controller - Creature | Rest]):-
move_to_zone(Controller, Creature, 'Battlefield', 'Graveyard'),
bury_creatures(Rest).
%%%%%%%%%%%%%%%creature_destroyed/0 08/03/11
%%%%%%%%%%%%%%%%%%%%%%%
creature_destroyed:-
zone('Battlefield', Objects),
findall(Controller - Creature,
(member(Creature, Objects),
lethal_damage(Creature, _Damage),
zone(Controller, 'Battlefield', Permanents),
member(Creature, Permanents)),
Creatures),
destroy_creatures(Creatures).
% uncomment once tested
%creature_destroyed(_Player).
%%%%%%%%%%%%%%%deathtouch_damage/0 08/03/11
%%%%%%%%%%%%%%%%%%%%%%%
deathtouch_damage:-
zone('Battlefield', Objects),
findall(Controller - Creature,
(member(Creature, Objects),
Creature = object(_Name - _Id, State),
member(deathtouch, State),
zone(Controller, 'Battlefield', Permanents),
member(Creature, Permanents)),
Creatures),
destroy_creatures(Creatures).
% uncomment once tested.
%deathtouch_damage.
%%%%%%%%%%%%%%%destroy_creatures/0 08/03/11
%%%%%%%%%%%%%%%%%%%%%%%
destroy_creatures([]).
destroy_creatures([Controller - Creature | Rest]):-
destroy_permanent(Controller, Creature),
destroy_creatures(Rest).
% Move to er, zone?
destroy_permanent(Controller, Permanent):-
move_to_zone(Controller, Permanent, 'Battlefield', 'Graveyard').
%%%%%%%%%%%%%%%%%%%%%%%%%%%%player_actions/5 01/02/2011
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* player_actions(+Active_player, +Priority_player, +Step, +Phase, -New_play) */
% The priority player can now take the actions allowed for this Phase and Step
%%%% Notes %%%%
%%%%%%%%%%%%%%%
/* Currently, Step and Phase are unused- play/5 must check that the
player actions taken are valid for the Priority player
to take in the current Step or Phase.
*/
%%%%%%%%%%%%%%player_actions/5 (1) 08/03/11
% When a player loses the game, no more players receive priority.
player_actions(_, loss(_Player, _Condition), _, _, []).
%%%%%%%%%%%%%%player_actions/5 (2) 01/02/2011
% No player actions are taken at the end of a Step
% or phase.
player_actions(_, _, ends(_), _, []):-
true.
%%%% Notes %%%%
%%%%%%%%%%%%%%%
/*
I was leaving New_play unbound here and it was getting bound in
the first clause of resolve_stack (the debug clause- so it was
becoming 100) below. I shouldn't be leaving "don't-know" values
in Play, Stack, Cue etc.
*/
%%%%%%%%%%%%%%player_actions/5 (3) 01/02/2011
% In steps where no player receives priority
% players don't take any actions.