-
Notifications
You must be signed in to change notification settings - Fork 10
/
player_actions.pl
1291 lines (982 loc) · 41 KB
/
player_actions.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: 08/02/11
% Last saved: 23/04/11
%
% Status:
%
% play_land is: 100%
% cast_spell: 80% [no timing checks, other_choices]
% activate_ability: 80% [no timing checks, other_choices]
% ^ am I forgetting something?
%
% Doing:
% Updating to new objects
% Done for activate_abilities
% Todo
% add timing checks in choose_spell, activate_ability
% redefine mana_cost and mana_string in terms of the MGL interpreter.
% Complete pay_cost (to cover all possible costs) (use DCG?)
%
% NOTES:
/* Once finished redefining all:
bring cast_spell in line with mgl_interpreter.pl
changing order of clauses
adding choose_targets
change determine_abilities
add make_choices
When casting spells or activating abilities, the player is prompted
to tap a source for mana. This is not strictly correct. The player
should be offered a chance to activate a mana ability. There is a
problem to adjust this right now, because a call to
activate_abilities would allow a player to activate a non-mana
ability.
*/
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% 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.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Player actions facts %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
:- dynamic played_land/2.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%played_land/2 29/01/11
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* played_land(+Player, +Yes/No) */
% Remembers whether a player has played a land this turn
played_land('Player 1', 'no').
played_land('Glee-min', 'no').
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Player actions rules %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%play_land/4 11/02/11
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* play_land(+Player, +Step, +Phase, -Play) */
% Checks that it's legal to put a land into play, does so if it is
% and returns an appropriate Play character
%%%% Notes %%%%
%%%%%%%%%%%%%%%
/*
Some of the tests are redundant, since the option to play land is
only presented in legal situations. However, this may catch illegal
"[l]and" input. Anyway, it's extra safety.
*/
%%%%%%%%%%%%%%play_land/3 (1) 29/01/11
% Is this the player's turn?
play_land(Active_player, Priority_player, _Step, _Phase, 99):-
(Priority_player = [Player, _Land] -> true ; Priority_player = Player),
% ^ The local cut in A->B here prevents backtracking in the A;B on
% a failed next clause >
Active_player \= Player,
output(play_land, ['wrong_turn']).
%%%%%%%%%%%%%%play_land/3 (2) 29/01/11
% Is this a main phase?
play_land(_Active_player, _Priority_player, _Step, Phase, 99):-
(
not Phase == 'First Main',
not Phase == 'Second Main'
),
output(play_land, ['wrong_phase']).
%%%%%%%%%%%%%%play_land/3 (3) 29/01/11
% Has the player already played a land this turn?
play_land(_Active_player, Priority_player, _Step, _Phase, 99):-
(Priority_player = [Player, _Land] -> true ; Priority_player = Player),
played_land(Player, 'yes'),
output(play_land, ['land_limit']).
%%%%%%%%%%%%%%play_land/3 (4) 29/01/11
% Is the stack empty?
play_land(_Active_player, _Priority_player, _Step, Phase, 99):-
not zone('Stack', []),
output(play_land, ['stack_not_empty']).
%%%%%%%%%%%%%%play_land/3 (5.1) 22/03/11
% Preparation for move_land input
play_land(_Active_player, ['Glee-min', Land], _Step, _Phase, Play):-
append(['cancel'], [Land], Full),
prompt_switches(Full, Switches),
play_land(['Glee-min', Land], Switches, Play).
%%%%%%%%%%%%%%play_land/3 (5.2) 29/01/11
% List the land in the player's hand
play_land(_Active_player, Priority_player, _Step, _Phase, Play):-
zone(Priority_player, 'Hand', Cards),
findall(Card, (member(Card, Cards),
card([card_name Card, _, type_line Type, _, _, _, _, _]),
Type = [_, ['Land'], _]
),
Land),
sort(Land, Sorted),
append(['cancel'], Sorted, Full),
prompt_switches(Full, Switches),
play_land(Priority_player, Switches, Play).
%%%%%%%%%%%%%%play_land/2 04/02/11
%%%%%%%%%%%%%%%%%%%%%%%
/* play_land(+Player, +Land, -Play) */
% Puts a land in play, or cancels; returns the Play character
%%%%%%%%%%%%%%play_land/2 (1) 04/02/11
% The player has no lands in hand.
% Contents of Land are: [[cancel - c],['[c]ancel']]
play_land(Player, [[cancel - c],['[c]ancel']], 99):-
output(play_land, ['no_lands_in_hand']).
%%%%%%%%%%%%%%play_land/2 (2) 22/03/11
play_land(['Glee-min', Land_card], Switches, 108):-
Switches = [[Cancel | Land], _Switches],
member(Land_card - Switch, Land),
% ^ maps the Land card to the switch representing it
% The human player does his or her own mapping ;)
atom_codes(Switch, [Code]),
move_land('Glee-min',[Cancel | Land], Code).
% ^ This used to send the mapping for Cancel as undefined
% Was that the source of the land bug? It did seem to be
% trying to play creatures instead of land...
% play_land(['Glee-min', _Land_card], Switches, 99).
% ^ This should not be necessary!
%%%%%%%%%%%%%%play_land/2 (3) 04/02/11
% Move the land card from the player's hand to the player's
% side of the battlefield.
play_land(Player, [Map, Switches], Play):-
Context = [Player, Map, Switches],
input(play_land, Context, Input) ->
(
Input = 99, Play = 99; % Cancel
move_land(Player, Map, Input),
Play = 108
);
play_land(Player, [Map, Switches], Play).
% ^ Go back if the input was wrong.
%%%%%%%%%%%%%%move_land/2 04/02/11
%%%%%%%%%%%%%%%%%%%%%%%
% Choose the right land card to move
move_land(Player, Map, Input):-
atom_codes(Switch, [Input]),
member(Land_card - Switch , Map),
move_to_zone(Player, Land_card, 'Hand', 'Battlefield'),
retractall(played_land(Player, _)),
asserta(played_land(Player, 'yes')),
output(play_land, ['played_land', Player, Land_card]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%cast_spell/5 20/03/11
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* cast_spell(+Active, +Priority, +Step, +Phase, -Play) */
% Cast a spell from the Player's Hand or another Zone
% Play is bound to 99 (cancel or fail), 115 (spell cast) or
% 97 (ability activated).
% This needs to check for timing restrictions (Step/Phase)!
cast_spell(_Active, Priority, _Step, _Phase, Play):-
% choose_spell(Active, Priority, Step, Phase, 'Hand', Spell),
choose_spell(Priority, 'Hand', Spell, _Text_box),
% ^ This doesn't really need to return Abilities...
% (currently it does, in its last arg)
% Now should also check timing restrictions.
(Priority = [Player | _rest] ; Priority = Player),
save_player(Player, Saved_state),
cast_spell(Priority, Saved_state, Spell, Play),
casting_outcome(Player, Saved_state, Spell, Play ).
%%%%%%%%%%%%%%choose_spell/4 06/02/11
%%%%%%%%%%%%%%%%%%%%%%%
/* choose_spell(+Controller, ?Zone -Spell, -Rules_text) */
% Determines the Spell to be cast, and returns its Rules text
% and possibly the Zone it is in
%%%% Notes %%%%
%%%%%%%%%%%%%%%
/*
If the Zone is not instantiated, the spell is in a Zone other than
the currently topmost zone in the database, the
'Input == 111, fail, !' clause in choose_spell/3 will cause
choose_spell/3 to backtrack into a new zone/3 and find a new
set of cards. That is a feature- the player may indeed be looking
for a spell in that Zone.
However, most spells will only be castable from the Hand
so I should be checking that a spell is in the right zone
to be cast from.
I'll implement this when I get to a card that requires it. Currently
it just fails (when called from cast_spell).
*/
%%%%%%%%%%%%%%choose_spell (1) 16/03/11
% Gleemin has already chosen a spell to play
choose_spell(['Glee-min', Spell | _Rest], _Zone, Spell, _Abilities).
%%%%%%%%%%%%%%choose_spell (2) 16/03/11
% Ask the Player which Spell to play
choose_spell(Player, Zone, Spell, Rules):-
zone(Player, Zone, Cards),
findall(Spell, (member(Spell, Cards),
card([card_name Spell, _, type_line Type, _, _, _, _, _]),
Type \= [_, ['Land'], _]
), Hand),
sort(Hand, Sorted),
(
Sorted \= [],
append(['other', 'cancel'], Sorted, Full),
prompt_switches(Full, Switches),
%^ Switches is really: [Map, Switches]!
choose_spell([Switches, Player], Spell, Rules);
choose_spell([no_spells, Player, Zone], Spell, Rules)
% ^ No spells in hand...
).
% append(['other', 'cancel'], Hand, Full),
% ^ When I sort and then append, I don't see my whole hand, obviously.
% I can add a number at the end of each card name as in: [C]ard(3)
% to inform the player of how many copies of the card are in Hand.
% But not now.
%%%%%%%%%%%%%%choose_spell/3 06/02/11
%%%%%%%%%%%%%%%%%%%%%%%
/* choose_spell(Context, Input, Abilities) */
% Handles input and output for parent, choose_spell/4
% There are no spells in the player's hand.
choose_spell([no_spells, Player, Zone], [], []):-
output(cast_spell, [no_spells, Player, Zone]).
% Ask the Player which Spell to play
choose_spell([[Map, Switches], Player], Spell, Rules):-
Context = [Map, Switches], %also Player? For "P1 plays Sp1"?
% Switches = [Cancel | [Other | Rest] ], %check rigorously
input('cast_spell', Context, Input) ->
(
Input == 99, Spell = [], Rules = []; %true; % cancel
Input == 111, fail, !; % other
atom_codes(Switch, [Input]),
member(Spell - Switch , Map),
card([card_name Spell, _, _, text_box Rules, _, _, _, _])
);
choose_spell([[Map, Switches], Player], Spell, Rules).
%%%%%%%%%%%%%%cast_spell/4 20/03/11
%%%%%%%%%%%%%%%%%%%%%%%
/* cast_spell(+Player, +Saved_state, +Spell, -Play) */
% Puts the card on the stack and continues casting the spell
%%%%%%%%%%%%%%cast_spell/4 (1) 20/03/11
cast_spell(_Player, _Saved, [], 99).
% ^ no spell chosen ie, casting cancelled.
%%%%%%%%%%%%%%cast_spell/4 (1) 20/03/11
cast_spell(Priority, _Saved, Spell, Play):-
(Priority = [Player, Spell | _rest] ; Priority = Player),
move_to_zone(Player, Spell, 'Hand', 'Stack'),
casting_choices(Priority, Play),
pay_spell_costs(Priority, Spell, Play).
%%%%%%%%%%%%%%casting_choices/2 20/03/11
%%%%%%%%%%%%%%%%%%%%%%%
/* casting_choices(+Player) */
% Lets the player make choices while
% casting a spell.
%%%%%%%%%%%%%%casting_choices/1 (1) 20/03/11
casting_choices(Priority, Play):-
zone('Stack', [Spell_object | _rest]),
Spell_object = object(Name - _Id, _Rest),
determine_abilities(Name, Abilities),
% ^ not in the current cast_spell def
% Also, btw, I should add a clause to determine_abilities/2
% to find abilities of MGL _Objects_ (not just by name)
%other_choices(Player, Spell_object, Abilities, Choices),
(Priority = [Player | _REst] ; Priority = Player),
% ^ temp fix, until I update choose_targets
choose_targets(Player, Spell_object, Abilities, Play).
% Undefined if no targets chosen or choosing canc^^^elled
%%%%%%%%%%%%%%casting_choices/1 (1) 20/03/11
% casting_choices failed or cancelled:
% casting_choices(_Player, 99).
%%%%%%%%%%%%%%pay_spell_costs/3 20/03/11
%%%%%%%%%%%%%%%%%%%%%%%
/* pay_spell_costs(+Player, +Spell, -Play) */
% Lets the player pay the cost of a spell
%%%%%%%%%%%%%%pay_spell_costs/3 (1) 04/04/11
pay_spell_costs(_Player, _Spell, Play):-
\+ type(Play,0), Play = 99.
%%%%%%%%%%%%%%pay_spell_costs/3 (2) 20/03/11
pay_spell_costs(Player, Spell, Play):-
determine_cost(Spell, _Abilities, Cost),
pay_cost(Cost, Player, Spell, Play).
%%%%%%%%%%%%%%pay_spell_costs/3 (2) 20/03/11
% pay_spell_costs failed or was cancelled.
pay_spell_costs(_Player, _Spell, 99).
%%%%%%%%%%%%%%other_choices/3 20/03/11
%%%%%%%%%%%%%%%%%%%%%%%
/* other_choices(+Player, +Spell, +Abilities, -Choices) */
% Lets the player make choices, besides targets for a spell
% Coming soon.
other_choices(_Player, _Spell_object, _Abilities, []).
%%%%%%%%%%%%%%casting_outcome/4 20/03/11
%%%%%%%%%%%%%%%%%%%%%%%
/* casting_outcome(+Player, +Saved_state, +Spell, +Play) */
% Deals with output for cast_spell. Also restores a player
% to a previous state if casting was cancelled or failed.
%%%%%%%%%%%%%%casting_outcome/4 (1) 20/03/11
% casting_outcome(Player, Saved_state, Spell, Play )
casting_outcome(Player, _Saved, Spell, 115 ):-
output(cast_spell, ['spell_cast', Player, Spell, 'Hand']).
%%%%%%%%%%%%%%casting_outcome/4 (1) 20/03/11
casting_outcome(Player, Saved, [], 99 ):-
restore_player(Player, Saved),
% write(`Spell casting canceled`), nl.
output(cast_spell, [spell_canceled, Player]).
%%%%%%%%%%%%%%casting_outcome/4 (1) 20/03/11
casting_outcome(Player, Saved, Spell, 99 ):-
restore_player(Player, Saved),
output(cast_spell, ['spell_fails', Player, Spell, 'Hand']).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%activate_ability/5 31/03/11
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* activate_ability(+Active_player, +Priority_player, +Step, +Phase, -Action) */
% Allows a player to activate an activated ability of a permanent,
% or of an object in a different zone (not implemented yet)
activate_ability(Active, Priority, Step, Phase, Play):-
which_ability(Active, Priority, Step, Phase, Source, Ability),
(Priority = [Player | _rest] ; Priority = Player),
% ^ If Glee-min is the priority player, some information about
% the ability and how to activate it will be included here.
save_player(Player, Saved_state),
ability_activation(Priority, Source, Ability, Play),
activation_outcome(Player, Saved_state, Source, Ability, Play ).
%%%%%%%%%%%%%%which_ability/5 31/03/11
%%%%%%%%%%%%%%%%%%%%%%%
/* which_ability(+Active_player, +Priority_player, +Step, +Phase, -Ability) */
% Lets the player choose an ability to activate
which_ability(_Active, Priority, _Step, _Phase, Source, Ability):-
choose_ability_source(Priority, Source),
choose_ability(Priority, Source, Ability).
%%%%%%%%%%%%%%choose_ability_source/2 31/03/11
%%%%%%%%%%%%%%%%%%%%%%%
/* choose_ability_source(+Player, -Source) */
% Lets the player choose an object that has an activated ability
choose_ability_source(Player, Ability_source):-
activated_abilities_sources(Player, Map, Switches, Identified),
ability_source(Player, Map, Switches, Identified, Ability_source).
%%%%%%%%%%%%%%choose_ability_source/2 31/03/11
%%%%%%%%%%%%%%%%%%%%%%%
/* choose_ability_source(+Player, -Source) */
% Lets the player choose an object that has an activated ability
activated_abilities_sources(Player, Map, Switches, Identified):-
zone(Player, 'Battlefield', Objects),
findall([Name, Id],
( member(Object, Objects),
object_handle(Object, Handle),
object_template(Handle, text_box, Abilities),
( one(( member(Ability, Abilities),
% ^ Prolog will report an Object once for each activated
% ability it has. We only need its name one time.
phrase(activated_ability, Ability)) )),
Handle = Name - Id ),
Sources),
findall(Name, member([Name, Id], Sources), Names),
append([cancel], Names, Full),
prompt_switches(Full, Switched),
Switched = [Map, Switches],
identify_object(Map, Sources, 1, [], Identified).
%%%%%%%%%%%%%%ability_source/5 31/03/11
%%%%%%%%%%%%%%%%%%%%%%%
/* ability_source(+Player, +Map, +Switches, +Identified, -Source) */
% Handles player input for choose_ability_source
ability_source(_Player, Map, Switches, Identified, Source):-
Context = [Map, Switches, Identified],
input(ability_source, Context, Input) ->
atom_codes(Char, [Input]),
ability_source(Char, Map, Identified, Source);
ability_source(Input, Map, Switches, Identified, Source).
%%%%%%%%%%%%%%ability_source/5 31/03/11
%%%%%%%%%%%%%%%%%%%%%%%
/* ability_source(+Input, +Map, +Identified, -Source) */
% Identifies the source of an ability chosen by a player
%%%%%%%%%%%%%%ability_source/5 (1) 31/03/11
% The player cancels activation
ability_source(Switch, Map, _Identified, []):-
member('cancel' - Switch, Map).
%%%%%%%%%%%%%%ability_source/5 (2) 31/03/11
ability_source(Switch, _Map, Identified, Name - Id):-
member([Name - Switch, Id], Identified).
%%%%%%%%%%%%%%choose_ability/3 31/03/11
%%%%%%%%%%%%%%%%%%%%%%%
/* choose_ability(+Player, +Source, -Ability) */
% Lets the player choose one of the activated abilities of a Source
%%%%%%%%%%%%%%choose_ability/3 (1) 31/03/11
% The player has cancelled activation
choose_ability(_Player, [], []).
%%%%%%%%%%%%%%choose_ability/3 (2) 31/03/11
choose_ability(Player, Source, Ability):-
object_template(Source, text_box, Abilities),
map_ability(Abilities, Map, Switches),
choose_ability(Player, Abilities, Map, Switches, Ability).
%%%%%%%%%%%%%%map_ability/3 31/03/11
%%%%%%%%%%%%%%%%%%%%%%%
/* map_ability(+Abilities, +Map, -Switches) */
% Maps abilities for player input
map_ability(Abilities, Map, Switches):-
findall(Ability, member(Ability, Abilities), ABilities),
ABilities \= [] -> % What if there are no abilties to activate?
findall(Activated,( member(Activated, ABilities),
one(phrase(activated_ability, Activated))),
Activated_Abilities ),
atom_word_list(Activated_Abilities, [], Atomic),
append([cancel], Atomic, Full),
prompt_switches(Full, [Map, Switches]).
%%%%%%%%%%%%%%choose_ability/5 31/03/11
%%%%%%%%%%%%%%%%%%%%%%%
/* choose_ability(Player, Abilities, Map, Switches, Ability) */
% Handles user input for choose_ability/3
choose_ability(Player, Abilities, Map, Switches, Ability):-
Context = [Map, Switches],
input(choose_ability, Context, Input) ->
atom_codes(Char, [Input]),
ability(Char, Map, Abilities, Ability);
choose_ability(Player, Abilities, Map, Switches, Ability).
%%%%%%%%%%%%%%ability/4 31/03/11
%%%%%%%%%%%%%%%%%%%%%%%
/* ability(Switch, Map, Abilities, Ability) */
% Identifieds the Ability chosen by the player and matches it
% back to its word-list form for the MGL interpeter
%%%%%%%%%%%%%%ability/4 (1) 31/03/11
ability(Switch, Map, _Abilities, []):-
member('cancel' - Switch, Map).
%%%%%%%%%%%%%%ability/4 (1) 31/03/11
ability(Switch, [_Cancel | Map], Abilities, Ability):-
member(_Atomic - Switch, Map, Position),
member(Ability, Abilities, Position).
%%%%%%%%%%%%%%ability_activation/4 01/04/11
%%%%%%%%%%%%%%%%%%%%%%%
/* ability_activation(+Player, +Source, +Ability, -Play) */
% Creates the ability as an object on the stack and continues
% with its activation
%%%%%%%%%%%%%%ability_activation/4 (0) 01/04/11
% Activation cancelled
ability_activation(_Priority, [], [], 99).
%%%%%%%%%%%%%%ability_activation/4 (0) 01/04/11
% No source chosen
ability_activation(_Priority, [], _Ability, 99).
%%%%%%%%%%%%%%ability_activation/4 (0) 01/04/11
% No ability chosen
ability_activation(_Priority, _Source, [], 99).
%%%%%%%%%%%%%%ability_activation/4 (1) 01/04/11
ability_activation(Priority, Source, Ability, Play):-
mana_ability(_Source_name, Ability, Mana),
add_to_pool(Priority, Mana, _Pool),
pay_activation_costs(Priority, Ability, Source, Play).
%%%%%%%%%%%%%%ability_activation/4 (2) 01/04/11
ability_activation(Priority, Source, Ability, Play):-
(Priority = [Player, Ability | _rest] ; Priority = Player),
ability_effect(Ability, Effect),
create_in_zone(Player, Effect, 'Stack'),
activation_choices(Priority, Effect, Play),
pay_activation_costs(Priority, Ability, Source, Play).
% OK, ugly problem here. This needs to determine the effect
% and separate it from the rest of the ability but "this does
% something" effects don't work properly ("this" needs fixing).
%%%%%%%%%%%%%%ability_effect/2 06/04/11
%%%%%%%%%%%%%%%%%%%%%%%
/* ability_effect(+Ability, -Effect) */
% Separates an ability's activation cost from its effect.
% Will probably fail on multi-effect abilities.
ability_effect(Ability, Effect):-
sublist(Cost, Ability),
phrase(cost, Cost),
append(Cost, [:], Full),
append(Full, Effect, Ability),
phrase(effect, Effect).
/* My GOD! but this is unnecessary!
Use:
phrase(cost, Ability, [:|Effect]),
append(Cost, [:|Effect], Ability)
as for determine_cost/2 */
%%%%%%%%%%%%%%activation_choices/2 01/04/11
%%%%%%%%%%%%%%%%%%%%%%%
/* activation_choices(+Player, +Ability) */
% Allows the player to choose targets and make any
% other choices required for an activated ability
activation_choices(Priority, Effect, Play):-
zone('Stack', [Ability_object | _rest]),
%other_choices(Player, Ability_object, Choices),
(Priority = [Player | _REst] ; Priority = Player),
% ^ temp fix, until I update choose_targets with glee_min clauses
choose_targets(Player, Ability_object, [Effect], Play).
% ^ Effect needs to be in a list: choose_targets calls member/2 on it
% because objects may have more than one effect or ability
%%%%%%%%%%%%%%pay_activation_costs/4 01/04/11
%%%%%%%%%%%%%%%%%%%%%%%
/* pay_activation_costs(+Player, +Ability, +Source, -Play) */
% Pays the activation cost(s) for an ability
%%%%%%%%%%%%%%pay_activation_costs/4 (1) 01/04/11
pay_activation_costs(_Player, _Ability, _Source, Play):-
\+ type(Play,0), Play = 99.
%%%%%%%%%%%%%%pay_activation_costs/4 (2) 01/04/11
pay_activation_costs(Player, Ability, Source, Play):-
determine_cost([], Ability, Cost),
object_handle(Object, Source),
pay_cost(Cost, Player, Object, Play).
%%%%%%%%%%%%%%pay_activation_costs/4 (3) 01/04/11
% The player has cancelled the activation
pay_activation_costs(_Player, _Ability, _Source, 99).
%%%%%%%%%%%%%%activation_outcome/4 01/04/11
%%%%%%%%%%%%%%%%%%%%%%%
/* activation_outcome(+Player, +Saved_state, +Ability, -Play) */
% Reports the outcome of an ability's activation.
% Also restores a player to a previous state if
% activation was cancelled or failed.
%%%%%%%%%%%%%%activation_outcome/4 (1) 01/04/11
% Remember: if pay_cost has failed, pay_activation_cost will
% return with Play = 99 and not bind to this clause
activation_outcome(Player, _Saved_state, Source, Ability, 97):-
output(activate_ability, [ability_activated, Player, Source, Ability, _Zone]).
% ^ Need to report the zone an ability was activated in
%%%%%%%%%%%%%%activation_outcome/4 (2) 01/04/11
activation_outcome(Player, Saved_state, Source, [], 99):-
restore_player(Player, Saved_state),
output(activate_ability, [activation_cancelled, Player, Source, _Zone]).
%%%%%%%%%%%%%%activation_outcome/4 (3) 01/04/11
activation_outcome(Player, Saved_state, Source, Ability, 99):-
restore_player(Player, Saved_state),
output(activate_ability, [activation_fails, Player, Source, Ability, _Zone]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%pay_cost/3 06/02/2011
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* pay_cost(+Cost, +Controller, +Object, -Result) */
% Pay the Cost for an activated ability of a Permanent or to cast
% a spell. If paying a cost fails, all payments are undone
% Result is either 115/97 (spell cast/ ability activated),
% or 99 (cancelled/ failed) (yeah, I don't like the ambiguity either)
%%%%%%%%%%%%%%pay_cost/3 (0) 20/02/2011
pay_cost([], Player, Permanent, Play):- !.
%%%%%%%%%%%%%%pay_cost/3 (1) 20/02/2011
% The cost is to tap the permanent
pay_cost(['tap' | Rest], Player, Permanent, Play):-
tap_untap(Permanent, Player, 'tap'),
pay_cost(Rest, Player, Permanent, Play).
pay_cost(['tap' | _Rest], _Player, Permanent, 99):-
output(pay_cost, [Permanent, 'tap_failed']).
%%%%%%%%%%%%%%pay_cost/3 (2) 20/02/2011
% The cost is to untap the permanent
pay_cost(['untap' | Rest], Player, Permanent, Play):-
tap_untap(Permanent, Player, 'untap'),
pay_cost(Rest, Player, Permanent, Play).
pay_cost(['untap' | _Rest], _Player, Permanent, 99):-
output(pay_cost, [Permanent, 'untap_failed']).
%%%%%%%%%%%%%%pay_cost/3 (2) 20/02/2011
% The cost is a mana cost
pay_cost([Cost | Rest], Player, Object, Play):-
mana_cost(Cost),
pay_mana_cost(Cost, Rest, Player, Object, Play),
pay_cost(Rest, Player, Object, Play).
pay_cost([_Cost | _Rest], _Player, _Object, 99).
%%%%%%%%%%%%%%pay_mana_cost/5 20/03/2011
%%%%%%%%%%%%%%%%%%%%%%%
pay_mana_cost(Cost, Rest, Player, Object, Play):-
mana_sources(Player, Switches, Identified),
tap_for_mana(Player, Switches, Identified, Play),
spend_mana(Player, Cost, Play),
pay_cost(Rest, Player, Object, Play).
pay_mana_cost(_Cost, _Rest, _Player, Object, 99):-
output(pay_cost, [Object, 'mana_failed']).
%%%%%%%%%%%%%%mana_cost/3 07/02/2011
%%%%%%%%%%%%%%%%%%%%%%%
/* mana_cost(+Cost) */
% Checks that a cost is a mana cost.
% A mana cost is a (non-negative) number,
% optionally followed by a string of mana symbols.
mana_cost(Cost):-
string_to_list(Cost, [First | Rest]),
((number(First), First >= 0,
mana_string(Rest));
mana_string([First | Rest])).
%%%%%%%%%%%%%%mana_string/1 07/02/2011
%%%%%%%%%%%%%%%%%%%%%%%
/* mana_string(+String) */
% Checks whether an atom is a string of mana symbols.
% A mana string is a string of mana symbols.
mana_string(Rest) :-
\+ (
member(Symbol, Rest),
Symbol \== w,
Symbol \== u,
Symbol \== b,
Symbol \== r,
Symbol \== g
).
%%%%%%%%%%%%%%mana_sources/2 22/02/2011
%%%%%%%%%%%%%%%%%%%%%%%
/* mana_sources(+Player, -Switches, -Identified) */
% Returns the mana sources on the Player's side of the battlefield
% Identified is a list of [Permanent - Switch, Id]'s
%%%%%%%%%%%%%%mana_sources/3 (1) 22/03/2011
mana_sources( [ 'Glee-min' | _rest ], _Switches, _Identified).
%%%%%%%%%%%%%%mana_sources/3 (2) 22/03/2011
mana_sources(Player, Switches, Identified):-
findall([Permanent, Id], (zone(Player, 'Battlefield', Cards),
member(object(Permanent - Id, State), Cards),
mana_ability(Permanent, _Ability, _Mana),
\+ member(tapped, State)),
Sources), %write(Sources), nl,
findall(Permanent, member([Permanent , Id], Sources), Permanents),
append(['cancel', 'skip'], Permanents, Full),
prompt_switches(Full, Switches), %write(Switched), nl,
Switches = [Map, _SwitcheS],
identify_object(Map, Sources, 2, [], Identified).
%%%% Notes %%%%
%%%%%%%%%%%%%%%
/*
In the call to identify_object/4
Maybe I can remove non-cancel/skip key-value pairs from Map
append Identified [key - value, Id] lists in their place and
send it all on... but it may cause problems further down?
Currently I'm working with two lists, Map and Identified, in
subsequent calls and it's a bit confusing.
*/
%%%%%%%%%%%%%%identify_object/4 22/02/2011
%%%%%%%%%%%%%%%%%%%%%%%
/* identify_object(+Map, +Copies, +Padding, [], -Identified) */
% Matches card copies and their Ids to a map of Card - Switch 's.
% Returns a list of [Name - Switch, Id]'s used to identify card
% copies for user input/output.
% Padding is the number of additional options added to the Map of
% Card - Switch, eg. [c]ancel, etc.
%%%% Notes %%%%
%%%%%%%%%%%%%%%
/*
There doesn't seem to be a problem with non-land cards on the battlefield.
Those don't get reported by mana_sources anyway.
*/
%%%%%%%%%%%%%%identify_object/4 (2) 04/04/2011
% Some predicates send a list containing non-objects, namely
% players and zones. Those should be weeded out for now
identify_object(Map, Copies, Padding, Temp, Identified):-
member(Object, Copies, Copies_position),
(player(Object) ; zone(Object, _Objects)),
Map_position is Copies_position + Padding,
member(Object - Switch, Map, Map_position),
append(Temp, [[Object - Switch]], New_temp),
remove(Object - Switch, Map, New_map),
remove(Object, Copies, New_copies),
identify_object(New_map, New_copies, Padding, New_temp, Identified).
%%%%%%%%%%%%%%identify_object/4 (0) 22/02/2011
identify_object(Map, [], Padding, Identified, Identified).
%%%%%%%%%%%%%%identify_object/4 (1) 22/02/2011
identify_object(Map, Copies, Padding, Temp, Identified):-
member([Copy, Id], Copies, Copies_position),
Map_position is Copies_position + Padding,
member(Copy - Switch, Map, Map_position),
append(Temp, [[Copy - Switch, Id]], New_temp),
remove(Copy - Switch, Map, New_map),
remove([Copy, Id], Copies, New_copies),
identify_object(New_map, New_copies, Padding, New_temp, Identified).
%%%%%%%%%%%%%%tap_for_mana/4 21/02/2011
%%%%%%%%%%%%%%%%%%%%%%%
/* tap_for_mana(+Player, +Switches, +Identified, -Play) */
% Handles player input for tap_for_mana
%%%%%%%%%%%%%%tap_for_mana/4 (1) 21/02/2011
tap_for_mana(Player, _Switches, _Identified, Play):-
Player = ['Glee-min',_,_,_,Switches, Identified, Sources,_],
Switches = [Map, _switches], % _switches only needed for human player input
tap_for_mana('Glee-min', Map, Identified, Sources, Play).
%%%%%%%%%%%%%%tap_for_mana/4 (2) 21/02/2011
tap_for_mana(Player, [Map, Switches], Identified, Play):-
Context = [Player, Map, Switches, Identified],
string_input(tap_for_mana, Context, Input) ->
atom_to_list(Input, List),
tap_for_mana(Player, Map, Identified, List, Play);
tap_for_mana(Player, [Map, Switches], Identified, Play).
% This fails for Permanents that call for a mana payment or an other
% additional cost for producing mana (not just tapping). The problem
% is in output/2 for this (still ?). Check it out.
%%%%%%%%%%%%%%tap_for_mana/4 20/02/2011
%%%%%%%%%%%%%%%%%%%%%%%
/* tap_for_mana(+Player, +Map, +Identified, -Input) */
% Fails on [c]ancel (because there is no "[c]ancel" on the battlefield.
% [s]kip succeeds and lets you draw mana from your pool.
%%%%%%%%%%%%%%tap_for_mana/3 (0) 21/02/2011
% All chosen permanents have been tapped
tap_for_mana(Player, Map, Identified, [], _Play).
% ^ leaving Play free for spend_mana to bind
%%%%%%%%%%%%%%tap_for_mana/3 (1) 21/02/2011
% Input = s. Skip tapping any permanents for mana and go
% straight to drawing mana from your pool.
tap_for_mana(_Player, Map, _Identified, Input, _Play):-
% ^ leaving Play free for spend_mana to bind
Input = [Switch], % remove brackets...
member('skip' - Switch, Map).
%^ This will allow to enter a string containing 'skip'; a feature.
% But test it.
%%%%%%%%%%%%%%tap_for_mana/3 (X) 20/03/2011
% Input = c. Don't tap any permanents for mana and
% cancel casting the spell
tap_for_mana(_Player, Map, _Identified, Input, 99):-
Input = [Switch], % remove brackets...
member('cancel' - Switch, Map).
% Normally, Input will be [c], in this case, since cancel and skip
% will be first in line to receive switches, so I could just watch
% for [c] in the head; but, just in case, I'm making sure.
%%%%%%%%%%%%%%tap_for_mana/3 (2) 21/02/2011
% The permanent the Player is attempting to tap is already tapped
tap_for_mana(Player, Map, Identified, [Switch | Rest], _):-
member(Permanent - Switch , Map),
% ^ From the Switch we find the name of the Permanent
member([Permanent - Switch, Id], Identified),
% ^ From the Name and Switch we find the object's Id
zone(Player, 'Battlefield', Cards), %redundant!
member(object(Permanent - Id, State), Cards),
member('tapped', State), % the Permanent is already tapped
remove(Permanent - Switch, Map, New_map),
remove([Permanent - Switch, Id], Identified, New_identified),
tap_for_mana(Player, New_map, New_identified, Rest, _).
% This should not really happen (tapped permanents are not
% reported
%%%%%%%%%%%%%%tap_for_mana/3 (3) 21/02/2011
% The Permanent is untapped. Draw the mana its mana ability can
% produce and tap it. Well, the other way around.
tap_for_mana(Player, Map, Identified, [Switch | Rest], _):-
member(Permanent - Switch , Map),
member([Permanent - Switch, Id], Identified),
zone(Player, 'Battlefield', Cards),
member(object(Permanent - Id, State), Cards),
(member('untapped', State) ; State = [] ),
mana_ability(Permanent, _Ability, Mana),
% ^ I'll have to choose abilities for permanents with multiple
% mana abilities
tap_untap(object(Permanent - Id, State), Player, 'tap'),
add_to_pool(Player, Mana, _New_pool),
remove(Permanent - Switch, Map, New_map),
remove([Permanent - Switch, Id], Identified, New_identified),
tap_for_mana(Player, New_map, New_identified, Rest, _).
%%%% Notes %%%%
%%%%%%%%%%%%%%%
/*
Currently, the player won't get to see permanents that
are already tapped, so the second clause here is
probably redundant.
*/
%%%%%%%%%%%%%%spend_mana/3 20/02/2011
%%%%%%%%%%%%%%%%%%%%%%%
/* spend_mana(+Player, +Cost, -Play) */
% Lets the Player spend mana to pay a Cost; verifies that the correct
% cost has been payed (well, that actually happens in output/2)
%%%%%%%%%%%%%%spend_mana/3 (1) 20/03/2011
% Glee-min's mana pool should now have the correct
% amount of mana in it, or else something has gone wrong.
spend_mana(['Glee-min',_,_,_,_,_,_,Mana], Cost, _Play):-
Mana = Cost, % should be!
take_from_pool('Glee-min', Mana, _New_pool).
%%%%%%%%%%%%%%spend_mana/4 (3) 20/03/2011
% tap_for_mana failed, cancelled or skipped
spend_mana(_Player, _Cost, Play):-
\+ type(Play, 0),
Play = 99.
%%%%%%%%%%%%%%spend_mana/4 (4) 20/03/2011
% Empty mana pool
% spend_mana(Player, _Cost, 99):-
% mana_pool(Player, [c - 0,w - 0,u - 0,b - 0,r - 0,g - 0]).
% ^ Yep, OK, done that. But leave this in just in case.
%%%%%%%%%%%%%%spend_mana/4 (5) 20/03/2011
spend_mana(Player, Cost, Play):-
mana_pool(Player, Mana_pool),
Context = [Mana_pool, Cost],
(string_input(spend_mana, Context, Mana) -> %, !,
match_cost(Cost, Mana),
take_from_pool(Player, Mana, New_pool),
output(draw_mana, [drawn_mana, Player, Mana, New_pool]);
spend_mana(Player, Cost, Play)).
%%%%%%%%%%%%%%spend_mana/4 (6) 20/03/2011
spend_mana(Player, _Cost, 99):-
mana_pool(Player, Pool),
output(draw_mana, [failed_to_draw_mana, Player, Pool]).