-
Notifications
You must be signed in to change notification settings - Fork 4
/
ducpel.el
1275 lines (1086 loc) · 43 KB
/
ducpel.el
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
;;; ducpel.el --- Logic game with sokoban elements
;; Copyright (C) 2014 Alex Kost
;; Author: Alex Kost <[email protected]>
;; Created: 31 Mar 2014
;; Version: 0.1
;; Package-Requires: ((cl-lib "0.5"))
;; URL: https://github.com/alezost/ducpel
;; Keywords: games
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; To install the game manually, you need:
;;
;; - "ducpel.el" (this file);
;; - "ducpel-glyphs.el" (it generates default images);
;; - directory with levels.
;;
;; Add the following to your emacs init file:
;;
;; (add-to-list 'load-path "/path/to/ducpel-dir")
;; (autoload 'ducpel "ducpel" nil t)
;;
;; Also if you keep levels separately:
;;
;; (setq ducpel-levels-directory "/path/to/ducpel-levels-dir")
;; After that you can "M-x ducpel" and enjoy. Use:
;;
;; - arrow keys to move your man;
;; - TAB to switch to another man;
;; - "u" to undo a move;
;; - SPC to activate a special cell (exit or teleport);
;; - "R" to restart the level;
;; - "N"/"P"/"L" to go to the next/previous/particular level.
;; At any time you can replay your moves by pressing "rc" (2 keys). If
;; you feel that a level is impassable, you may surrender (and see a
;; solution) by pressing "rS".
;; Contact the maintainer please, if you found a better solution (with
;; less moves) for some level or if you made an interesting level that
;; can become a part of ducpel.
;; For full documentation, see <https://github.com/alezost/ducpel>.
;;; Code:
(require 'cl-lib)
(require 'gamegrid)
;;; User options
(defgroup ducpel nil
"Logic game."
:group 'games)
(defcustom ducpel-use-glyphs t
"If non-nil, use glyphs when available."
:type 'boolean
:group 'ducpel)
(defcustom ducpel-buffer-name "*ducpel*"
"Name of the ducpel buffer."
:type 'string
:group 'ducpel)
(defcustom ducpel-replay-pause 0.5
"Number of seconds to wait between moves during replaying.
To replay the moves, use
\\[ducpel-replay-current] in a ducpel buffer."
:type 'number
:group 'ducpel)
(defcustom ducpel-levels-directory
(expand-file-name "levels"
(file-name-directory (locate-library "ducpel")))
"Directory with level files."
:type 'directory
:group 'ducpel)
(defcustom ducpel-user-levels-directory user-emacs-directory
"Directory with additional level files.
To load a level from this directory, use
\\[ducpel-load-level-from-file] in a ducpel buffer."
:type 'directory
:group 'ducpel)
(defcustom ducpel-user-saves-directory user-emacs-directory
"Directory with saves of moves.
To replay saved moves from this directory, use
\\[ducpel-replay-from-file] in a ducpel buffer."
:type 'directory
:group 'ducpel)
(defcustom ducpel-default-level 1
"Default level."
:type 'integer
:group 'ducpel)
;;; Constants
;; Cell types
(defconst ducpel-empty 0)
(defconst ducpel-wall 1)
(defconst ducpel-impassable 2)
(defconst ducpel-floor 3)
(defconst ducpel-man 4)
(defconst ducpel-active-man 5)
(defconst ducpel-box 6)
;; Floor/box types
(defconst ducpel-simple 0)
(defconst ducpel-exit 1)
(defconst ducpel-teleport 2)
(defconst ducpel-left 3)
(defconst ducpel-right 4)
(defconst ducpel-up 5)
(defconst ducpel-down 6)
(defconst ducpel-horizontal 7)
(defconst ducpel-vertical 8)
(defconst ducpel-multi 9)
(defconst ducpel-cell-types
(list ducpel-empty ducpel-wall ducpel-impassable
ducpel-floor ducpel-man ducpel-active-man ducpel-box)
"List of available cell types.")
(defconst ducpel-floor-types
(list ducpel-simple ducpel-exit ducpel-teleport
ducpel-left ducpel-right ducpel-up ducpel-down
ducpel-horizontal ducpel-vertical ducpel-multi)
"List of available floor/box types.")
;; The maximum count of cell characters is limited to 256.
;; Currently we have:
;;
;; - by 1 for empty, wall and impassable cells;
;; - 10 floors;
;; - 20 men (active and passive on each floor);
;; - 100 boxes (each box type on each floor).
;;
;; So there is a space to avoid printable ASCII characters and some
;; control characters (see (info "(elisp) Usual Display")) to be able to
;; write something in a ducpel buffer along with having the map of
;; glyphs. If the count of cell types is increased significantly, we
;; will have to use printable chars and thus to refuse writing text in
;; the buffer (and perhaps to use the modeline instead).
(defconst ducpel-init-cell-char-alist
(let ((len (length ducpel-floor-types))
(floor-shift 126))
(list
(cons ducpel-empty 23)
(cons ducpel-wall 24)
(cons ducpel-impassable 25)
(cons ducpel-floor floor-shift)
(cons ducpel-man (+ len floor-shift))
(cons ducpel-active-man (+ (* 2 len) floor-shift))
(cons ducpel-box (+ (* 3 len) floor-shift))))
"Alist of initial cell characters for the cell types.
Car of each assoc is a cell type. Cdr is a cell character.")
;; Move types
(defconst ducpel-left-move #b0001)
(defconst ducpel-right-move #b0010)
(defconst ducpel-up-move #b0100)
(defconst ducpel-down-move #b1000)
(defconst ducpel-action 3)
(defconst ducpel-move-type-alist
(list
(cons ducpel-left ducpel-left-move)
(cons ducpel-right ducpel-right-move)
(cons ducpel-up ducpel-up-move)
(cons ducpel-down ducpel-down-move)
(cons ducpel-horizontal (+ ducpel-left-move ducpel-right-move))
(cons ducpel-vertical (+ ducpel-up-move ducpel-down-move))
(cons ducpel-multi (+ ducpel-left-move ducpel-right-move
ducpel-up-move ducpel-down-move)))
"Alist of possible moves for the floor types.
Car of each assoc is a floor type. Cdr is a move type.")
(defconst ducpel-break-wall-power 3
"Power (minimum count of pushing men) required to break a wall.")
;; Constants for parsing level maps
(defconst ducpel-map-re "^;+ *Map")
(defconst ducpel-objects-re "^;+ *Objects")
(defconst ducpel-solution-re "^;+ *Solution")
(defconst ducpel-ignored-line-re
(rx line-start
(or (and ";" (* any))
(* blank))
line-end)
"Regexp for ignored lines in level files.")
(defconst ducpel-empty-map-char ?\s)
(defconst ducpel-wall-map-char ?#)
(defconst ducpel-impassable-map-char ?@)
(defconst ducpel-floor-map-chars ".ETLRUDHVM")
(defconst ducpel-box-map-chars "betlrudhvm")
(defconst ducpel-man-map-char ?p)
(defconst ducpel-active-man-map-char ?P)
;;; General variables
(defvar ducpel-men []
"Array of coordinates of the men on the current level.
Each element of the list is a list of the form (X Y).")
(defvar ducpel-active-man-index 0
"Index of the active man in `ducpel-men'.")
(defvar ducpel-teleports nil
"List of coordinates of the teleports on the current level.
Each element of the list is a list of the form (X Y).")
(defvar ducpel-undo-list nil
"List of full undo information.
Each element of the list has a form:
(CELLS MEN ACTIVE TELEPORTS)
CELLS has a form of `ducpel-undo-current-cells'.
MEN has a form of `ducpel-undo-current-men'.
ACTIVE has a form of `ducpel-undo-current-active-index'.
TELEPORTS has a form of `ducpel-undo-current-teleports'.")
(defvar ducpel-undo-current-cells nil
"List of changes of the cells made after the last move.
Each element of the list has a form:
(X Y CHAR)
X, Y - coordinates of the changed cell;
CHAR is a gamegrid character of the changed cell.
If nil, it means the cells were not changed.")
(defvar ducpel-undo-current-men []
"Array of men coordinates changed after the last move.
Has a form of `ducpel-men'. If an element of the array is nil,
it means the coordinates of the man were not changed.")
(defvar ducpel-undo-current-active-index nil
"Index of the man that was active after the last move.
If nil, it means the active man was not changed.")
(defvar ducpel-undo-current-teleports nil
"List of coordinates of the teleports after the last move.
Has a form of `ducpel-teleports'.
If nil, it means teleports were not changed.")
(defvar ducpel-moves 0
"The number of moves for the current level.")
(defvar ducpel-done 0
"The number of men went to a better world.")
(defvar ducpel-moves-history nil
"List of moves for the current level.
Each element of the list has a form:
(MAN MOVE-TYPE)
MAN is the index (from `ducpel-men') of a man who made the move.
For the meaning of MOVE-TYPE, see `ducpel-do'.
Car of the list is the last move; the last element of the list is
the first move.")
(defvar ducpel-solution nil
"List of moves to solve the current level.
Has a form of `ducpel-moves-history'.")
(defvar ducpel-level-data nil
"Data of the current level map.
2-dimensional matrix (vector of vectors) of the width
`ducpel-width' and the height `ducpel-height' that contains cell
characters for the current level.")
(defvar ducpel-level nil
"Index of the current level.")
(defvar ducpel-level-file nil
"Name of file with a map of the current level.")
(defvar ducpel-width 0
"Width of the current level map.")
(defvar ducpel-height 0
"Height of the current level map.")
;;; Cells
(defvar ducpel-cell-plists (make-vector 256 nil)
"Array of property lists for all possible cell characters.
Properties in property lists:
`:type' - type of the cell - element from `ducpel-cell-types';
`:floor'/`:box' (optional) - type of the floor/box - element from
`ducpel-floor-types'.")
(defun ducpel-get-cell-char-by-plist (&rest plist)
"Return cell character by the property list PLIST."
(let ((type (plist-get plist :type))
(floor-index (or (plist-get plist :floor) 0))
(box-index (or (plist-get plist :box) 0))
(len (length ducpel-floor-types)))
(let ((init-char (cdr (assoc type ducpel-init-cell-char-alist))))
(+ init-char
floor-index
(* box-index len)))))
(defun ducpel-init-cell-plists ()
"Fill `ducpel-cell-plists'."
(cl-flet ((pset (&rest plist)
(aset ducpel-cell-plists
(apply 'ducpel-get-cell-char-by-plist plist)
plist)))
(pset :type ducpel-empty)
(pset :type ducpel-wall)
(pset :type ducpel-impassable)
(dolist (floor ducpel-floor-types)
(pset :type ducpel-floor :floor floor)
(pset :type ducpel-man :floor floor)
(pset :type ducpel-active-man :floor floor)
(dolist (box ducpel-floor-types)
(pset :type ducpel-box :floor floor :box box)))))
(ducpel-init-cell-plists)
(defun ducpel-get-cell-plist-by-cell-char (char)
"Return cell property list by the cell character CHAR."
(aref ducpel-cell-plists char))
(defun ducpel-get-cell-plist-by-xy (x y)
"Return cell property list by the cell coordinates X, Y."
(ducpel-get-cell-plist-by-cell-char
(gamegrid-get-cell x y)))
(defun ducpel-set-cell (x y &rest plist)
"Set cell at X, Y to the cell defined by property list PLIST.
Return cell character of the set cell."
(let* ((old-char (gamegrid-get-cell x y))
(new-char (apply 'ducpel-get-cell-char-by-plist plist)))
(gamegrid-set-cell x y new-char)
(push (list x y old-char) ducpel-undo-current-cells)
new-char))
;;; Men
(defun ducpel-get-man-index-by-shift (shift &optional index)
"Return new index by shifting man INDEX with SHIFT.
If INDEX is nil, use `ducpel-active-man-index'."
(ducpel-get-index-by-shift
(length ducpel-men)
(or index ducpel-active-man-index)
shift))
(defun ducpel-get-man-xy (&optional index)
"Return coordinates of a man.
INDEX is a number of the man in `ducpel-men'. If INDEX is nil,
use `ducpel-active-man-index'.
Returning value is a list of the form (X Y)."
(or index
(setq index ducpel-active-man-index))
(aref ducpel-men index))
(defun ducpel-get-man-index-by-xy (x y)
"Return index of a man placed on X, Y cell."
(or (ducpel-get-index-by-element
ducpel-men (list x y) 'equal 'noerror)
(error "No man with %d, %d coordinates"
x y)))
(defun ducpel-set-man-xy (from-x from-y to-x to-y)
"Set coordinates of a man from FROM-X, FROM-Y to TO-X, TO-Y."
(let ((index (ducpel-get-man-index-by-xy from-x from-y)))
(aset ducpel-undo-current-men index (list from-x from-y))
(aset ducpel-men index (list to-x to-y))))
(defun ducpel-delete-man (index)
"Delete man INDEX from the current map."
(cl-multiple-value-bind (x y)
(ducpel-get-man-xy index)
(aset ducpel-undo-current-men index (list x y))
(aset ducpel-men index nil)
(let ((plist (ducpel-get-cell-plist-by-xy x y)))
(ducpel-set-cell x y
:type ducpel-floor
:floor (plist-get plist :floor)))))
(defun ducpel-set-active-man (index)
"Try to set a man INDEX active.
INDEX is a number of the man in `ducpel-men'. If the man does
not exist, try to set the next man active, and so on.
Return index of the new active man or nil if no man was set."
(unless (and (= index ducpel-active-man-index)
(aref ducpel-men index))
(ducpel-set-active-man-1
index (ducpel-get-man-index-by-shift -1 index))))
(defun ducpel-set-active-man-1 (index exit-index)
"Set a man active.
INDEX is a number of the man in `ducpel-men'. If the man does
not exist, try to set the next man active, and so on until the
man with index EXIT-INDEX will not be achieved. In this case,
return nil; otherwise return index of the new active man."
(unless (= index exit-index)
(cl-multiple-value-bind (new-x new-y)
(ducpel-get-man-xy index)
(if (null new-x)
(ducpel-set-active-man-1
(ducpel-get-man-index-by-shift 1 index)
exit-index)
(cl-multiple-value-bind (old-x old-y)
(ducpel-get-man-xy)
(when old-x ; previously active man could be "Done" already
(let ((old-plist (ducpel-get-cell-plist-by-xy old-x old-y)))
(ducpel-set-cell old-x old-y
:type ducpel-man
:floor (plist-get old-plist :floor)))))
(let ((new-plist (ducpel-get-cell-plist-by-xy new-x new-y)))
(ducpel-set-cell new-x new-y
:type ducpel-active-man
:floor (plist-get new-plist :floor)))
(or ducpel-undo-current-active-index
(setq ducpel-undo-current-active-index
ducpel-active-man-index))
(setq ducpel-active-man-index index)))))
(defun ducpel-get-active-cell-xy ()
"Return coordinates of the cell with the active man.
Returning value is a list of the form (X Y)."
(aref ducpel-men ducpel-active-man-index))
(defun ducpel-get-active-cell-plist ()
"Return cell plist of the cell with the active man."
(apply 'ducpel-get-cell-plist-by-xy
(ducpel-get-active-cell-xy)))
(defun ducpel-next-man ()
"Select next man."
(interactive)
(ducpel-set-active-man (ducpel-get-man-index-by-shift 1)))
(defun ducpel-previous-man ()
"Select previous man."
(interactive)
(ducpel-set-active-man (ducpel-get-man-index-by-shift -1)))
;;; Doing (moves and actions)
(defun ducpel-do (move-type)
"Try to make a move or perform an action with active man.
Save undo history if the move/action was successful.
MOVE-TYPE is one of the following constants: `ducpel-action',
`ducpel-left-move', `ducpel-right-move', `ducpel-up-move',
`ducpel-down-move'."
(unless (ducpel-done-p t)
(let ((man ducpel-active-man-index))
(when (if (eql move-type ducpel-action)
(ducpel-do-action)
(ducpel-do-move move-type))
(ducpel-add-move)
(push (list man move-type) ducpel-moves-history)
(ducpel-undo-save-current)))))
(defun ducpel-do-action ()
"Perform an action on the current cell.
Return non-nil if the action was successful."
(let* ((plist (ducpel-get-active-cell-plist))
(floor (plist-get plist :floor))
success)
(cond
((eql floor ducpel-exit)
(ducpel-delete-man ducpel-active-man-index)
(ducpel-set-active-man (ducpel-get-man-index-by-shift 1))
(ducpel-check-done)
(ducpel-print-done)
(ducpel-done-p t)
(setq success t))
((eql floor ducpel-teleport)
(if (null (cdr ducpel-teleports))
;; If a single teleport on the map
(message "This strange thing looks broken.")
(if (ducpel-teleport-active-man)
(setq success t)
(message "Hm, perhaps the teleport is blocked."))))
(t (message "Nothing interesting here.")))
success))
(defun ducpel-do-move (direction)
"Move active man in the DIRECTION.
For the meaning of DIRECTION, see `ducpel-cell-can-move-p'.
Return non-nil if the move was successful."
(cl-multiple-value-bind (x y)
(ducpel-get-man-xy)
(ducpel-move x y direction)))
(defun ducpel-teleport-active-man ()
"Try to teleport active man to a free teleport cell.
If the next teleport after the current one is blocked, try the
next after it and so on.
Return non-nil, if teleportation was successful."
(let* ((active-xy (ducpel-get-active-cell-xy))
(next-teleports (member active-xy ducpel-teleports)))
(or next-teleports
(error "Active man is not on the teleport cell"))
;; Getting next free teleport: if the rest teleports are blocked,
;; continue searching from the beginning of `ducpel-teleports'.
(let ((xy (or (ducpel-teleport-get-free-cell (cdr next-teleports))
(ducpel-teleport-get-free-cell
(cl-loop for teleport in ducpel-teleports
until (equal teleport active-xy)
collect teleport)))))
(when xy
(let ((from-x (car active-xy))
(from-y (cadr active-xy))
(to-x (car xy))
(to-y (cadr xy)))
(ducpel-set-cell
to-x to-y
:type ducpel-active-man :floor ducpel-teleport)
(ducpel-set-cell
from-x from-y
:type ducpel-floor :floor ducpel-teleport)
(ducpel-set-man-xy from-x from-y to-x to-y)
t)))))
(defun ducpel-teleport-get-free-cell (cells)
"Return first free cell from a list of coordinates CELLS.
Cell is free if it is a floor with no object (man or box) on it.
Return nil if none of the cells is free."
(cl-loop for cell in cells
if (eql (plist-get
(apply 'ducpel-get-cell-plist-by-xy cell)
:type)
ducpel-floor)
return cell))
(defun ducpel-cell-can-move-p (floor-type direction)
"Return non-nil, if a cell with FLOOR-TYPE can move in the DIRECTION.
Direction should have a value of one of the following constants:
`ducpel-left-move', `ducpel-right-move',
`ducpel-up-move', `ducpel-down-move'."
(let ((moves (cdr (assoc floor-type ducpel-move-type-alist))))
(and moves
(/= 0 (logand moves direction)))))
(defun ducpel-get-xy (from-x from-y direction &optional val)
"Return coordinates by shifting FROM-X, FROM-Y to the DIRECTION by VAL.
For the meaning of DIRECTION, see `ducpel-cell-can-move-p'.
If VAL is nil, shift coordinates by 1.
Returning value is a list of the form (X Y)."
(let ((x from-x)
(y from-y)
(val (or val 1)))
(cond
((eql direction ducpel-left-move) (cl-decf x val))
((eql direction ducpel-right-move) (cl-incf x val))
((eql direction ducpel-up-move) (cl-decf y val))
((eql direction ducpel-down-move) (cl-incf y val)))
(list x y)))
(defun ducpel-get-last-empty-xy (x y direction)
"Return last cell of `ducpel-empty' type by moving from X, Y in DIRECTION.
For the meaning of DIRECTION, see `ducpel-cell-can-move-p'.
Returning value is a list of coordinates of the last empty cell."
(let (next-x next-y)
(while (progn
(cl-multiple-value-setq (next-x next-y)
(ducpel-get-xy x y direction))
(let* ((char (gamegrid-get-cell next-x next-y))
(plist (ducpel-get-cell-plist-by-cell-char char))
(type (plist-get plist :type)))
(eql type ducpel-empty)))
(setq x next-x
y next-y))
(list x y)))
(defun ducpel-check-done ()
"Count and set `ducpel-done'."
(let ((done 0))
(dotimes (i (length ducpel-men))
(or (aref ducpel-men i) (cl-incf done)))
(setq ducpel-done done)))
(defun ducpel-done-p (&optional show-message)
"Return non-nil if current level is passed.
If SHOW-MESSAGE is non-nil, also show a message in minibuffer."
(let ((done (= ducpel-done (length ducpel-men))))
(and done
show-message
;; FIXME Do not hardcode the bindings
(message "DONE! Press 'r c' to replay, 'r s' to save, 'R' to restart, 'N' for the next level."))
done))
(defun ducpel-add-move ()
"Increase the current count of moves."
(cl-incf ducpel-moves)
(ducpel-print-moves))
(defun ducpel-remove-move ()
"Decrease the current count of moves."
(cl-decf ducpel-moves)
(ducpel-print-moves))
(defun ducpel-action ()
"Perform an action on the current cell."
(interactive)
(ducpel-do ducpel-action))
(defun ducpel-move-left ()
"Move one cell left."
(interactive)
(ducpel-do ducpel-left-move))
(defun ducpel-move-right ()
"Move one cell right."
(interactive)
(ducpel-do ducpel-right-move))
(defun ducpel-move-up ()
"Move one cell up."
(interactive)
(ducpel-do ducpel-up-move))
(defun ducpel-move-down ()
"Move one cell down."
(interactive)
(ducpel-do ducpel-down-move))
;; The following variables are used only during a move by
;; `ducpel-move-<smth>-to-<smth>' functions and are set by
;; `ducpel-move'.
(defvar ducpel-from-x nil)
(defvar ducpel-from-y nil)
(defvar ducpel-from-char nil)
(defvar ducpel-from-plist nil)
(defvar ducpel-from-type nil)
(defvar ducpel-to-x nil)
(defvar ducpel-to-y nil)
(defvar ducpel-to-char nil)
(defvar ducpel-to-plist nil)
(defvar ducpel-to-type nil)
(defvar ducpel-power nil)
(defvar ducpel-direction nil)
(defun ducpel-move (x y direction &optional power)
"Move cell at X, Y in the DIRECTION with POWER.
For the meaning of DIRECTION, see `ducpel-cell-can-move-p'.
Return non-nil if the shift was successful, nil otherwise."
(let* ((ducpel-from-x x)
(ducpel-from-y y)
(ducpel-power (or power 0))
(ducpel-direction direction)
(ducpel-from-char (gamegrid-get-cell x y))
(ducpel-from-plist (ducpel-get-cell-plist-by-cell-char
ducpel-from-char))
(ducpel-from-type (plist-get ducpel-from-plist :type))
success)
;; Most cell types can't be moved
(unless (memql ducpel-from-type
(list ducpel-empty ducpel-wall
ducpel-impassable ducpel-floor))
(cl-multiple-value-bind (ducpel-to-x ducpel-to-y)
(ducpel-get-xy ducpel-from-x ducpel-from-y
ducpel-direction)
(let* ((ducpel-to-char (gamegrid-get-cell ducpel-to-x ducpel-to-y))
(ducpel-to-plist (ducpel-get-cell-plist-by-cell-char
ducpel-to-char))
(ducpel-to-type (plist-get ducpel-to-plist :type)))
(cond
;; If a move is successful, redraw only the destination cell
;; (`ducpel-to-x', `ducpel-to-y'). If it was a move of the
;; active man, also redraw the departure cell
;; (`ducpel-from-x', `ducpel-from-y').
;; We want to move a MAN
((eql ducpel-from-type ducpel-man)
(cl-incf ducpel-power)
(when (or (ducpel-move-object-to-floor)
(ducpel-move-object-to-wall))
(ducpel-set-man-xy ducpel-from-x ducpel-from-y
ducpel-to-x ducpel-to-y)
(setq success t)))
;; We want to move an ACTIVE MAN
((eql ducpel-from-type ducpel-active-man)
(cl-incf ducpel-power)
(let ((new-from-plist
(cond
((or (ducpel-move-object-to-floor)
(ducpel-move-object-to-wall))
(list :type ducpel-floor
:floor (plist-get ducpel-from-plist :floor)))
((ducpel-move-man-to-emty)
(list :type ducpel-empty)))))
(when new-from-plist
(ducpel-set-man-xy ducpel-from-x ducpel-from-y
ducpel-to-x ducpel-to-y)
(apply 'ducpel-set-cell
ducpel-from-x ducpel-from-y new-from-plist)
(setq success t))))
;; We want to move a BOX
((and (eql ducpel-from-type ducpel-box)
(> ducpel-power 0))
(cl-decf ducpel-power)
(when (or (ducpel-move-object-to-floor)
(ducpel-move-object-to-wall)
(ducpel-move-box-to-empty))
(setq success t)))))))
success))
(defun ducpel-move-object-to-floor ()
"Try to move an object (man or box) to a floor.
If a destination cell contains another object, try to move it at first.
If the move is possible, redraw the destination cell and
return non-nil."
(when (or (eql ducpel-to-type ducpel-floor)
(and (or (eql ducpel-to-type ducpel-man)
(eql ducpel-to-type ducpel-box))
(ducpel-move ducpel-to-x ducpel-to-y
ducpel-direction ducpel-power)))
(ducpel-set-cell ducpel-to-x ducpel-to-y
:type ducpel-from-type
:floor (plist-get ducpel-to-plist :floor)
:box (plist-get ducpel-from-plist :box))))
(defun ducpel-move-object-to-wall ()
"Try to move an object (man or box) to a wall.
If the move is possible, redraw the destination cell and
return non-nil."
(when (and (eql ducpel-to-type ducpel-wall)
(>= ducpel-power ducpel-break-wall-power))
(ducpel-set-cell ducpel-to-x ducpel-to-y
:type ducpel-from-type :floor ducpel-simple)))
(defun ducpel-move-man-to-emty ()
"Try to move a man to an empty cell.
If the move is possible, redraw the destination cell and
return non-nil."
(when (and (eql ducpel-to-type ducpel-empty)
(ducpel-cell-can-move-p
(plist-get ducpel-from-plist :floor) ducpel-direction))
(cl-multiple-value-setq (ducpel-to-x ducpel-to-y)
(ducpel-get-last-empty-xy ducpel-to-x ducpel-to-y
ducpel-direction))
(ducpel-set-cell ducpel-to-x ducpel-to-y
:type ducpel-from-type
:floor (plist-get ducpel-from-plist :floor))))
(defun ducpel-move-box-to-empty ()
"Try to move a box to an empty cell.
If the move is possible, redraw the destination cell and
return non-nil."
(when (eql ducpel-to-type ducpel-empty)
(when (eql (plist-get ducpel-from-plist :box)
ducpel-teleport)
(setq ducpel-undo-current-teleports ducpel-teleports)
(push (list ducpel-to-x ducpel-to-y)
ducpel-teleports))
(ducpel-set-cell ducpel-to-x ducpel-to-y
:type ducpel-floor
:floor (plist-get ducpel-from-plist :box))))
;;; Undoing
;; To restore the previous state of the grid, we need to keep track of
;; changed cells, coordinates of the men and index of an active man.
(defun ducpel-undo-reset-current ()
"Reset current undo data to the default values."
(setq ducpel-undo-current-cells nil
ducpel-undo-current-teleports nil
ducpel-undo-current-men (make-vector (length ducpel-men) nil)
ducpel-undo-current-active-index nil))
(defun ducpel-undo-init ()
"Initialize undo data."
(setq ducpel-undo-list nil)
(ducpel-undo-reset-current))
(defun ducpel-undo-save-current ()
"Add undo info about the current move to `ducpel-undo-list'."
(push (list ducpel-undo-current-cells
ducpel-undo-current-men
ducpel-undo-current-active-index
ducpel-undo-current-teleports)
ducpel-undo-list)
(ducpel-undo-reset-current))
(defun ducpel-undo-changes (cells men active teleports)
"Undo changes from CELLS, MEN, ACTIVE and TELEPORTS.
For the meaning of arguments, see `ducpel-undo-list'."
(mapc (lambda (change)
(apply 'gamegrid-set-cell change))
cells)
(dotimes (i (length men))
(let ((man (aref men i)))
(and man
(aset ducpel-men i man))))
(and active
(setq ducpel-active-man-index active))
(and teleports
(setq ducpel-teleports teleports)))
(defun ducpel-undo ()
"Undo previous move or action."
(interactive)
;; Undo possible switching of the men made since the last move
(ducpel-undo-changes ducpel-undo-current-cells
ducpel-undo-current-men
ducpel-undo-current-active-index
ducpel-undo-current-teleports)
(ducpel-undo-reset-current)
;; Undo the last move
(let ((move-changes (pop ducpel-undo-list)))
(when move-changes
(apply 'ducpel-undo-changes move-changes)
(ducpel-remove-move)
(pop ducpel-moves-history)
(ducpel-check-done)
(ducpel-print-done))))
;;; Replaying
(defun ducpel-replay (&optional moves)
"Replay MOVES.
If MOVES is nil, use `ducpel-moves-history'."
(interactive)
(setq moves (reverse (or moves ducpel-moves-history)))
(ducpel-restart-level)
(dolist (move moves)
(sit-for ducpel-replay-pause)
(ducpel-set-active-man (car move))
(ducpel-do (cadr move))))
(defalias 'ducpel-replay-current 'ducpel-replay
"Replay current moves.")
(defun ducpel-replay-solution ()
"Replay solution of the current level."
(interactive)
(if ducpel-solution
(and (y-or-n-p "Do you REALLY want to see a solution of the level?")
(ducpel-replay ducpel-solution))
(message "No solution for the current map.")))
(defun ducpel-replay-from-file (file)
"Replay saved moves from FILE.
Interactively, prompt for FILE."
(interactive
(list (read-file-name "Load replay from file: "
ducpel-user-saves-directory)))
(load file)
(ducpel-replay))
(defun ducpel-save-replay (file)
"Save current moves to FILE.
Interactively, prompt for FILE."
(interactive
(list (read-file-name "Save replay to file: "
ducpel-user-saves-directory)))
(or ducpel-moves-history
(user-error "Do a single move at least"))
(with-temp-buffer
(insert ";; Saved moves for a ducpel level.\n"
(format ";; Level file: %s\n\n" ducpel-level-file)
"(setq ducpel-moves-history '")
(princ ducpel-moves-history (current-buffer))
(insert ")\n")
(set (make-local-variable 'version-control) 'never)
(write-file file t)))
;;; Display options
(defvar ducpel-glyphs-function nil
"Function returning alist of glyph specifications used in gamegrid.
Associations of the alist should have the form:
(PLIST . GLYPHS)
PLIST is a unique cell property list, see `ducpel-cell-plists'.
GLYPHS is a gamegrid specification for the PLIST.
Gamegrid specifications are lists of the form:
(GLYPH-SPEC FACE-SPEC COLOR-SPEC)
They are used for `gamegrid-display-options' (see
`gamegrid-initialize-display' for details).")
;; Avoid compilation warning about `ducpel-glyphs-default'
(declare-function ducpel-glyphs-default "ducpel-glyphs" nil)
(defun ducpel-get-glyphs ()
"Return alist with glyph specifications."
(if ducpel-glyphs-function
(funcall ducpel-glyphs-function)
(require 'ducpel-glyphs)
(ducpel-glyphs-default)))
(defun ducpel-get-display-options ()
"Return array suitable for `gamegrid-display-options'."
(let ((options (make-vector 256 nil))
(glyph-alist (ducpel-get-glyphs)))
(dolist (assoc glyph-alist)
(aset options
(apply 'ducpel-get-cell-char-by-plist (car assoc))
(cdr assoc)))
options))
;;; Printing info
(defvar ducpel-print-level-line 1)
(defvar ducpel-print-moves-line 2)
(defvar ducpel-print-done-line 3)
(defun ducpel-print-string (string dy)
"Print STRING in the current gamegrid.
DY is a number of line after `ducpel-height'."
(goto-char (point-min))
(let ((lines (forward-line (+ ducpel-height dy)))
(inhibit-read-only t))
;; Go to the line even if it does not exist
(insert (make-string lines ?\n))
(delete-region (point) (line-end-position))
(insert string)
(and (eobp) (insert ?\n))))
(defun ducpel-print-level ()
"Print current level."
(ducpel-print-string
(format "Level: %s" (or ducpel-level ducpel-level-file))
ducpel-print-level-line))
(defun ducpel-print-moves ()
"Print current count of moves."
(ducpel-print-string
(format "Moves: %d" ducpel-moves)
ducpel-print-moves-line))
(defun ducpel-print-done ()
"Print current count of men."
(ducpel-print-string
(format "Done: %d/%d" ducpel-done (length ducpel-men))
ducpel-print-done-line))
(defun ducpel-print-info ()
"Print all current info in the gamegrid."
(ducpel-print-level)
(ducpel-print-moves)
(ducpel-print-done))
;;; Parsing levels
(defvar ducpel-map-char-alist nil
"Alist of characters used in level maps and cell plists.")
(defvar ducpel-objects-char-alist nil
"Alist of characters used in level maps for objects and cell plists.")
(defun ducpel-init-map-char-alist ()