-
Notifications
You must be signed in to change notification settings - Fork 0
/
mine.pl
982 lines (903 loc) · 22.4 KB
/
mine.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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (c) David Hildenbrand, Tobias Schoknecht
% ----------------------------------------------------------------------------
% "THE BEER-WARE LICENSE" (Revision 42):
% [email protected] and [email protected] wrote this file.
% As long as you retain this notice you can do whatever you want with this
% stuff. If we meet some day, and you think this stuff is worth it, you can
% buy us a beer in return David Hildenbrand, Tobias Schoknecht
% ----------------------------------------------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This is a text-based adventure game.
% The game is set within an old mine whose exit
% collapsed due to a tremor.
%
% The task in the game is to find objects
% which may help to find a way to leave the
% mine.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Code structure
% - Static configuration data
% - Initialization code
% - Game logic
% - Game state actions
% - Inventory actions
% - Item combination rules
% - Take action
% - Put/Drop action
% - Use actions
% - Look action
% - Examine action
% - Movement and connection
% - Position actions
% - Blocked messages
% - Game state messages
% - (Re)Start Game code
% - Command processing
% - Commands
% - Output formatting helper
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Static configuration data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% General object information
object(desk).
object(map).
object(flashlight).
object(box).
object(dynamite).
object(pickaxe).
object(mine_chariot).
object(helmet).
object(pouch).
object(goldnugget).
object(key).
object(shovel).
object(bunch_of_rocks).
object(railway_switch).
object(fused_dynamite).
object(fuse_cord).
object(fuse).
object(flute).
% Objects that can contain another object
container(desk).
container(box).
container(pouch).
% General location definition
location(exit).
location(tunnel1).
location(tunnel2).
location(tunnel3).
location(tunnel4).
location(break_chamber).
location(gold_vein_chamber).
location(silver_vein_chamber).
% Connections between locations
simpleConn(exit, tunnel1).
simpleConn(tunnel1, tunnel2).
simpleConn(tunnel1, break_chamber).
simpleConn(tunnel2, silver_vein_chamber).
simpleConn(tunnel2, gold_vein_chamber).
simpleConn(tunnel2, tunnel3).
simpleConn(tunnel3, tunnel4).
% Takeable items
takeable(fuse).
takeable(fuse_cord).
takeable(fused_dynamite).
takeable(map).
takeable(flashlight).
takeable(pickaxe).
takeable(dynamite).
takeable(helmet).
takeable(pouch).
takeable(goldnugget).
takeable(key).
takeable(shovel).
takeable(flute).
% Usable items
usable(fuse).
usable(key).
usable(flute).
usable(railway_switch).
usable(flashlight).
% Combinable items
combinable(dynamite, fuse_cord, fused_dynamite).
% Combinable rules
bothWayCombinable(X, Y, Z) :-
combinable(X, Y, Z),
!.
bothWayCombinable(X, Y, Z) :-
combinable(Y, X, Z).
% Object stores are containers, locations and inventory
object_store(inventory).
object_store(X) :-
container(X).
object_store(X) :-
location(X).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initialization code
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%init
init :-
init_basic,
init_items,
init_text.
%start text
init_text :-
line,
write('You are exploring an old mine.'),
nl,
write('A sudden tremor occured and some rocks are falling down.'),
nl,
write('You are heading for the exit.'),
nl,
nl,
write('It is blocked.'),
nl,
line,
print_pos->
true;
true.
%init basic
init_basic :-
asserta(light_state(off)),
asserta(game_state(started)),
asserta(position(tunnel1)),
asserta(blocked(tunnel1, exit)),
asserta(blocked_object(desk)).
%initialize items
init_items :-
asserta(contains(flashlight, inventory)),
asserta(contains(fuse_cord, desk)),
asserta(contains(desk, break_chamber)),
asserta(contains(map, break_chamber)),
asserta(contains(box, gold_vein_chamber)),
asserta(contains(dynamite, box)),
asserta(contains(pickaxe, gold_vein_chamber)),
asserta(contains(fuse, silver_vein_chamber)),
asserta(contains(mine_chariot, silver_vein_chamber)),
asserta(contains(helmet, silver_vein_chamber)),
asserta(contains(pouch, tunnel4)),
asserta(contains(goldnugget, pouch)),
asserta(contains(key, pouch)),
asserta(contains(shovel, tunnel4)),
asserta(contains(bunch_of_rocks, tunnel3)),
asserta(contains(flute, tunnel1)),
asserta(contains(railway_switch, tunnel2)).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Game logic
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Game state actions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Update the game state
set_game_state(X) :-
retractall(game_state(_)),
asserta(game_state(X)).
% Update the flashlight state
set_light_state(X) :-
retractall(light_state(_)),
asserta(light_state(X)).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Inventory actions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Easy access to the inventory
inventory(X) :-
contains(X, inventory).
% Add something to the inventory
add_to_inventory(X) :-
asserta(contains(X, inventory)),
write(X),
writeln(' was added to the inventory.').
% Remove something from the inventory
remove_from_inventory(X) :-
remove_from_inventory_action(X),
retract(contains(X, inventory)),
write(X),
writeln(' was removed from the inventory.').
% Light will be turned of when removed from inventory
remove_from_inventory_action(flashlight):-
set_light_state(off),
!.
% Catch-all for objects without an action
remove_from_inventory_action(_).
% Print the content of the inventory
print_inventory :-
inventory(_)->
(writeln('Your inventory contains:'),
print_inventory_list;
!);
writeln('Your inventory is empty.').
print_inventory_list :-
inventory(X),
tab,
writeln(X),
fail.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Item combination rules
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Combine two items and add the result to the inventar
combine(X, Y) :-
inventory(X),
inventory(Y),
bothWayCombinable(X, Y, Z),
add_to_inventory(Z),
remove_from_inventory(X),
remove_from_inventory(Y),
!.
combine(X, _) :-
not(object(X)),
write(X),
writeln(' is not an object.'),
!.
combine(_, Y) :-
not(object(Y)),
write(Y),
writeln(' is not an object.'),
!.
combine(X, _) :-
not(inventory(X)),
write(X),
writeln(' is not in your inventory.'),
!.
combine(_, Y) :-
not(inventory(Y)),
write(Y),
writeln(' is not in your inventory.'),
!.
combine(X, Y) :-
inventory(X),
inventory(Y),
not(bothWayCombinable(X, Y, _)),
write('Cannot combine '),
write(X),
write(' and '),
write(Y),
writeln('.').
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Take action
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%take from current position
take(X) :-
position(Z),
take_from_any(X, Z)->
true;
true.
%take from any object store except the inventory
take_from_any(_, X) :-
location(X),
not(position(X)),
writeln('Cannot take objects out of different locations!'),
!.
take_from_any(_, X) :-
blocked_object(X),
blocked_message(X),
!.
take_from_any(_, inventory) :-
writeln('Cannot take something which is already in the inventory!'),
!.
take_from_any(_, Z) :-
not(container(Z)),
not(location(Z)),
write(Z),
writeln(' is not a valid place for an object.'),
!.
take_from_any(X, _) :-
not(object(X)),
write(X),
writeln(' is not a valid object!'),
!.
take_from_any(X, _) :-
not(takeable(X)),
write(X),
writeln(' cannot be carried around!'),
!.
take_from_any(X, Z) :-
not(contains(X, Z)),
write(X),
write(' not found at '),
write(Z),
writeln('!'),
!.
take_from_any(X, Z) :-
container(Z),
retract(contains(X, Z)),
add_to_inventory(X),
!.
take_from_any(X, Z) :-
position(Z),
retract(contains(X, Z)),
add_to_inventory(X),
!.
take_from_any(X, _) :-
write(X),
writeln(' is not reachable from here!'),
!.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Put/Drop action
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Put something back
put(X) :-
position(Z),
put_to_any(X, Z)->
true;
true.
put_message(fused_dynamite, Y) :-
location(Y),
writeln('Use the fuse in your inventory to blow the dynamite.'), !.
put_message(X, Y) :-
write('Put '),
write(X),
write(' to '),
write(Y),
writeln('.').
% Put to any position or container
put_to_any(_, X) :-
blocked_object(X),
blocked_message(X),
!.
put_to_any(X, inventory) :-
contains(X,inventory),
write(X),
writeln(' is already in your inventory'),
!.
put_to_any(X, _) :-
not(object(X)),
write(X),
writeln(' is not a valid object.'),
!.
put_to_any(_, Y) :-
not(container(Y)),
not(location(Y)),
write(Y),
writeln(' is not a valid place for an object.'),
!.
put_to_any(_, Y) :-
container(Y),
position(Z),
not(contains(Y, Z)),
not(inventory(Y)),
writeln('Cannot beam objects to containers which are not around here.'),
!.
put_to_any(X, Y) :-
container(Y),
asserta(contains(X, Y)),
remove_from_inventory(X),
put_message(X, Y),
!.
put_to_any(_, Y) :-
not(position(Y)),
writeln('Cannot beam objects to places which are not around.'),
!.
put_to_any(X, Y) :-
position(Y),
asserta(contains(X, Y)),
remove_from_inventory(X),
put_message(X, Y),
!.
put_to_any(X, _) :-
write('Cannot place '),
write(X),
writeln(' here').
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Use actions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Use an item which is in the inventory or at the current position
use(X) :-
not(object(X)),
write(X),
writeln(' is not a valid object.'),
!.
use(X) :-
not(usable(X)),
writeln('I really don\'t know what to do with this thing!'),
!.
use(X) :-
not(inventory(X)),
position(Z),
not(contains(X, Z)),
write(X),
writeln(' is not reachable from here.'),
!.
use(flute) :-
writeln('You\'re playing the most awesome melody ever!'),
!.
use(railway_switch) :-
writeln('Your trying the best you can but it won\'t move.'),
!.
%%%%%%%%%%fuse action
use(fuse) :-
contains(fused_dynamite, Y),
location(Y),
position(Y),
set_game_state(failed),
writeln('Daaaaaaaaaaaaaamn, you blew yourself! ( ;) )'),
!.
use(fuse) :-
contains(fused_dynamite, Y),
location(Y),
not(position(Y)),
Y \= tunnel1,
%remove all objects in the room
retractall(contains(_, Y)),
writeln('Wow, that was an explosion! But the exit is still blocked!'),
!.
use(fuse) :-
contains(fused_dynamite, Y),
not(position(Y)),
Y = tunnel1,
retractall(contains(_, Y)),
writeln('Kabooooooooooom, what an explosion!'),
retract(blocked(tunnel1, exit)),
!.
use(fuse) :-
writeln('Place the fused_dynamite in a room to be able to use the fuse!'),
!.
%%%%%%%%%%%
% Flashlight will only be usable in the inventory
use(flashlight) :-
inventory(flashlight)->
writeln('Wow, light!'),
set_light_state(on),
!;
writeln('The flashlight needs to be in your inventory to use it!').
% Catch all: usable object, but needs something to use it on
use(_) :-
writeln('You need something to apply this object on.').
%use an object onto another object
use_on(X, _) :-
not(object(X)),
write(X),
writeln(' is not a valid object'),
!.
use_on(X, _) :-
not(inventory(X)),
write(X),
writeln(' is not in your inventory.'),
!.
use_on(X, Y) :-
not(usable(X)),
write('I really don\'t know how to use '),
write(X),
write(' on '),
write(Y),
writeln('.'),
!.
use_on(_, Y) :-
not(object(Y)),
write(Y),
writeln(' is not a valid object'),
!.
use_on(X, Y) :-
position(Z),
contains(Y, Z),
use_on_object(X, Y),
!.
use_on(X, Y) :-
inventory(Y),
use_on_object(X, Y),
!.
use_on(_, Y) :-
write(Y),
writeln(' is not reachable from here'),
!.
use_on_object(key, desk) :-
retract(blocked_object(desk))->
writeln('Desk unlocked!');
writeln('Desk is already unlocked!').
use_on_object(X, Y) :-
write('Don\'t know how to use '),
write(X),
write(' on '),
write(Y),
writeln('.'),
!.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Look action
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Look around
look_around :-
light_state(L),
look_around_ext(L).
look_around_ext(on) :-
position(X),
contains(_, X)->
writeln('You can see:'),
print_viewable_objects;
writeln('Nothing in here!'),
true.
look_around_ext(off) :-
writeln('Still dark. Can\'t see a thing!').
print_viewable_objects :-
position(X),
contains(Y, X),
tab,
writeln(Y),
fail.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Examine action
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Examine objects
examine_object(Y) :-
not(object(Y)),
write(Y),
writeln(' is not a valid object.'),
!.
examine_object(Y) :-
not(inventory(Y)),
position(Z),
not(contains(Y, Z)),
write(Y),
writeln(' is not reachable from here.'),
!.
examine_object(X) :-
blocked_object(X),
blocked_message(X),
!.
examine_object(Y) :-
not(container(Y)),
write('The '),
write(Y),
writeln(' is really a beautiful object!'),
!.
examine_object(Y) :-
contains(_, Y)->
write(Y),
writeln(' contains the following objects:'),
examine(Y);
write(Y),
writeln(' is empty.').
examine(Y) :-
inventory(Y),
examine_object_list(Y),
!.
examine(Y) :-
position(X),
contains(Y, X),
examine_object_list(Y),
!.
examine_object_list(Y) :-
contains(Z, Y),
tab,
writeln(Z),
fail.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Movement and connection
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Two way connections
connection(X, Y) :-
simpleConn(X, Y).
connection(X, Y) :-
simpleConn(Y, X).
% Two way block check
way_blocked(X, Y) :-
blocked(X, Y).
way_blocked(X, Y) :-
blocked(Y, X).
% Movement rules
movement_rule(X, Y) :-
not(way_blocked(X, Y)).
% Two way movement rules
mv_r(X, Y) :-
movement_rule(X, Y).
mv_r(X, Y) :-
movement_rule(Y, X).
single_move(X, Y) :-
connection(X, Y),
mv_r(X, Y)->
writeln('changing the room'),
retract(position(X)),
asserta(position(Y));
blocked_way_message(X, Y), fail.
% Move action
goto(X) :-
not(location(X)),
write(X),
writeln(' is not a valid location.').
goto(X) :-
position(X),
write('You are already in '),
writeln(X),
!.
goto(X) :-
location(X),
position(Y),
not(connection(X, Y)),
write('You cannot go to '),
write(X),
writeln(' directly.').
goto(exit) :-
position(Y),
single_move(Y, exit)->
set_game_state(won),
!;
!.
goto(X) :-
location(X),
position(Y),
single_move(Y, X)->
line,
print_pos,
!;
!.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Position actions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Print map
printMap :-
writeln(' /--------------|--------------------------|------'),
writeln(' / break_chamber | tunnel1 | exit'),
writeln(' | /-----|------------/ /---------|------'),
writeln(' | / / /'),
writeln(' \\-------/ /---/'),
writeln(' / /'),
writeln(' /------------------|------/ |'),
writeln('/ gold_vein_chamber | tunnel2 \\'),
writeln('| /----|---/ /\\ \\'),
writeln('\\-------------/ / / \\ \\'),
writeln(' /---/ \\---\\'),
writeln(' / / \\ \\-------\\'),
writeln(' / / | \\--------\\'),
writeln(' tunnel3--|- | | silver_vein_chamber \\'),
writeln(' | | | |'),
writeln(' / / \\ /'),
writeln(' /-----------/---/ \\-------------------/'),
writeln(' | tunnel4 /'),
writeln(' | /---------/'),
writeln(' \\---/').
% Print position and possible ways
print_pos :-
light_state(L),
print_position(L).
print_position(on) :-
position(X),
write('You are in '),
write(X),
write('.'),
nl,
print_possible_paths;
true.
print_position(off) :-
light_state(off),
writeln('It is pitch dark in here. You can\'t see anything.').
print_possible_paths :-
position(X),
writeln('You can go to the following areas: '),
connection(X, Y),
tab,
writeln(Y),
fail.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Blocked messages
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Message for blocked locations and objects
blocked_message(desk) :-
writeln('The desk is locked. You need a key to open the desk.'),
!.
blocked_message(exit) :-
write('The way is blocked. You can smell'),
write(' the fresh air streaming in the tunnel. '),
writeln('Find a way to remove the stones.'),
writeln('You shall not pass!'),
!.
blocked_message(_).
% Detailed block reason
blocked_way_message(_, exit) :-
blocked_message(exit).
blocked_way_message(exit, _) :-
blocked_message(exit).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Game state messages
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
print_fail_message :-
writeln('GAME OVER!!!! Please come again!').
print_end_message :-
writeln('End of game. Thank you for playing!').
print_success_message :-
writeln('DAAAAMN, you WON!!!! You are a REAL HERO!!! BRACE YOURSELF!!!!').
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (Re)Start Game code
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initialize Game
start_game :-
init,
start_interactive_mode.
% Restart the game
restart_game :-
(retractall(game_state(_));true),
(retractall(position(_));true),
(retractall(blocked(_, _));true),
(retractall(contains(_, _));true),
(retractall(blocked_object(_));true),
(retractall(light_state(_));true),
start_game.
% Main loop
start_interactive_mode :-
repeat,
line,
read_command(Y),
line,
process_command(Y),
((game_state(end), print_end_message);
(game_state(failed), print_fail_message);
(game_state(won), print_success_message)),
halt.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Command processing
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Read in the user command
read_command(Y) :-
write('--> '),
readln(Y).
% Process a command
process_command(Y) :-
preprocess_command(Y, Z),
command(Z)->
true;
writeln('What makes sis command?! Enter help for more information.').
% Preprocess the command
preprocess_command(A, B) :-
command_lowercase(A, T),
command_clean(T, B).
%write('Processed command: '),
%writeln(B).
% Convert words to lower case
command_lowercase([], []).
command_lowercase([A|B], [C|D]) :-
downcase_atom(A, C),
command_lowercase(B, D).
% Remove words that will be ignored from the list
command_clean([A|B], C) :-
ignored_word(A),
command_clean(B, C).
command_clean([], []).
command_clean([A|B], [A|C]) :-
not(ignored_word(A)),
command_clean(B, C).
% The words that will be filtered out
ignored_word(';').
ignored_word(', ').
ignored_word('.').
ignored_word('!').
ignored_word('?').
ignored_word('the').
ignored_word('a').
ignored_word('this').
ignored_word('that').
ignored_word('hell').
ignored_word('heck').
ignored_word('fuck').
ignored_word('fuckin').
ignored_word('fucking').
ignored_word('and').
ignored_word('to').
ignored_word('into').
ignored_word('onto').
ignored_word('out').
ignored_word('of').
ignored_word('from').
ignored_word('on').
ignored_word('with').
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Commands
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% End the game
command([stop]) :-
command([end]).
command([exit]) :-
command([end]).
command([quit]) :-
command([end]).
command([halt]) :-
command([end]).
command([end]) :-
set_game_state(end).
% Reload the file - for debugging
% command([reload]) :-
% writeln('Reloading file "mine.pl"'),
% consult(mine),
% restart_game.
% Restart the game
command([restart]) :-
restart_game.
% Display the help message
command([help]) :-
writeln('Available commands are:'),
tab,
writeln('"help" - Display this help'),
tab,
writeln('"end" - End the game'),
tab,
writeln('"go to X" - Go to position X'),
tab,
writeln('"where am I" - Where the hell are you?'),
tab,
writeln('"look around" - Take a look around.'),
tab,
%writeln('"reload" - Reload the file (debugging purpose)'),
%tab,
writeln('"restart" - Restart the game'),
tab,
writeln('"take X" - Take something'),
tab,
writeln('"take X out of Y" - Take something out of something'),
tab,
writeln('"put X into Y" - Put something into something.'),
tab,
writeln('"drop X" - Drop something'),
tab,
writeln('"use X" - Use something'),
tab,
writeln('"combine X Y" - Combine two things creating a third one.'),
tab,
writeln('"examine X" - Examine an object.'),
tab,
writeln('"show map" - Display the map.'),
tab,
writeln('"show inventory" - Display the inventory.').
command([go, R]) :-
goto(R);
true.
% Display location information
command([where, am, i]) :-
print_pos;
true.
% Take a look around
command([look, around]) :-
look_around;
true.
% Take something
command([take, R]) :-
take(R);
true.
command([take, R, Z]) :-
take_from_any(R, Z);
true.
% Drop something
command([drop, R]) :-
put(R);
true.
% Put something to some destination
command([put,A,inventory]) :-
take(A),
!.
command([put, A, B]) :-
put_to_any(A, B);
true.
% Use something
command([use, R]) :-
use(R);
true.
% Use something on something
command([use, A, B]) :-
use_on(A, B);
true.
% Combine objects
command([combine, A, B]) :-
combine(A, B);
true.
% Examine something
command([examine, R]) :-
examine_object(R);
true.
% Show the map
command([map]) :-
command([show, map]).
command([show, map]) :-
inventory(map)->
printMap;
(light_state(on)->
writeln('Hard to show a non-existing map. Use your imagination!');
writeln('Can\'t even see my own hands!')
),
true.
% Show the inventory
command(['inventory']) :-
command([show, 'inventory']).
command([show, 'inventory']) :-
print_inventory.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Output formatting helper
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tab :-
writef('\t').
line :-
writeln('---------------------------------------------------------------------------').