1 ;;; bubbles.el --- Puzzle game for Emacs
3 ;; Copyright (C) 2007-2012 Free Software Foundation, Inc.
5 ;; Author: Ulf Jasper <ulf.jasper@web.de>
6 ;; URL: http://ulf.epplejasper.de/
7 ;; Created: 5. Feb. 2007
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; Bubbles is a puzzle game. Its goal is to remove as many bubbles as
28 ;; possible in as few moves as possible.
30 ;; Bubbles is an implementation of the "Same Game", similar to "Same
31 ;; GNOME" and many others, see <http://en.wikipedia.org/wiki/SameGame>.
36 ;; Add the following lines to your Emacs startup file (`~/.emacs').
37 ;; (add-to-list 'load-path "/path/to/bubbles/")
38 ;; (autoload 'bubbles "bubbles" "Play Bubbles" t)
40 ;; ======================================================================
48 ;; - Allow for undoing last move.
49 ;; - Bonus for removing all bubbles.
50 ;; - Speed improvements.
51 ;; - Animation enhancements.
52 ;; - Added `bubbles-mode-hook'.
53 ;; - Fixes: Don't move point.
57 ;; - Renamed shift modes and thus names of score files. All
58 ;; high scores are lost, unless you rename the score files from
59 ;; bubbles-shift-... to bubbles-...!
60 ;; - Bugfixes: Check for successful image creation.
61 ;; Disable menus and counter when game is over.
62 ;; Tested with GNU Emacs 22.0.93
65 ;; - Introduced game themes.
66 ;; - Introduced graphics themes (changeable while playing).
68 ;; - Customization: grid size, colors, chars, shift mode.
69 ;; - More keybindings.
70 ;; - Changed shift direction from to-right to to-left.
71 ;; - Bugfixes: Don't remove single-bubble regions;
72 ;; Animation glitches fixed.
73 ;; Tested with GNU Emacs 22.0.93 and 21.4.1.
76 ;; Initial release. Tested with GNU Emacs 22.0.93 and 21.4.1.
78 ;; ======================================================================
82 (defconst bubbles-version
"0.5" "Version number of bubbles.el.")
85 (eval-when-compile (require 'cl
)) ; for 'case
89 ;; Careful with that axe, Eugene! Order does matter in the custom
92 (defcustom bubbles-game-theme
95 The overall game theme specifies a grid size, a set of colors,
97 :type
'(radio (const :tag
"Easy" easy
)
98 (const :tag
"Medium" medium
)
99 (const :tag
"Difficult" difficult
)
100 (const :tag
"Hard" hard
)
101 (const :tag
"User defined" user-defined
))
104 (defun bubbles-set-game-easy ()
105 "Set game theme to 'easy'."
107 (setq bubbles-game-theme
'easy
)
110 (defun bubbles-set-game-medium ()
111 "Set game theme to 'medium'."
113 (setq bubbles-game-theme
'medium
)
116 (defun bubbles-set-game-difficult ()
117 "Set game theme to 'difficult'."
119 (setq bubbles-game-theme
'difficult
)
122 (defun bubbles-set-game-hard ()
123 "Set game theme to 'hard'."
125 (setq bubbles-game-theme
'hard
)
128 (defun bubbles-set-game-userdefined ()
129 "Set game theme to 'user-defined'."
131 (setq bubbles-game-theme
'user-defined
)
134 (defgroup bubbles nil
135 "Bubbles, a puzzle game."
138 (defcustom bubbles-graphics-theme
141 It is safe to choose a graphical theme. If Emacs cannot display
142 images the `ascii' theme will be used."
143 :type
'(radio (const :tag
"Circles" circles
)
144 (const :tag
"Squares" squares
)
145 (const :tag
"Diamonds" diamonds
)
146 (const :tag
"Balls" balls
)
147 (const :tag
"Emacs" emacs
)
148 (const :tag
"ASCII (no images)" ascii
))
151 (defconst bubbles--grid-small
'(10 .
10)
152 "Predefined small bubbles grid.")
154 (defconst bubbles--grid-medium
'(15 .
10)
155 "Predefined medium bubbles grid.")
157 (defconst bubbles--grid-large
'(20 .
15)
158 "Predefined large bubbles grid.")
160 (defconst bubbles--grid-huge
'(30 .
20)
161 "Predefined huge bubbles grid.")
163 (defcustom bubbles-grid-size
165 "Size of bubbles grid."
166 :type
`(radio (const :tag
"Small" ,bubbles--grid-small
)
167 (const :tag
"Medium" ,bubbles--grid-medium
)
168 (const :tag
"Large" ,bubbles--grid-large
)
169 (const :tag
"Huge" ,bubbles--grid-huge
)
170 (cons :tag
"User defined"
171 (integer :tag
"Width")
172 (integer :tag
"Height")))
175 (defconst bubbles--colors-2
'("orange" "violet")
176 "Predefined bubbles color list with two colors.")
178 (defconst bubbles--colors-3
'("lightblue" "palegreen" "pink")
179 "Predefined bubbles color list with three colors.")
181 (defconst bubbles--colors-4
'("firebrick" "sea green" "steel blue" "chocolate")
182 "Predefined bubbles color list with four colors.")
184 (defconst bubbles--colors-5
'("firebrick" "sea green" "steel blue"
185 "sandy brown" "bisque3")
186 "Predefined bubbles color list with five colors.")
188 (defcustom bubbles-colors
190 "List of bubble colors.
191 The length of this list determines how many different bubble
193 :type
`(radio (const :tag
"Red, darkgreen" ,bubbles--colors-2
)
194 (const :tag
"Red, darkgreen, blue" ,bubbles--colors-3
)
195 (const :tag
"Red, darkgreen, blue, orange" ,bubbles--colors-4
)
196 (const :tag
"Red, darkgreen, blue, orange, violet"
198 (repeat :tag
"User defined" color
))
201 (defcustom bubbles-chars
202 '(?
+ ?O ?
# ?X ?. ?
* ?
& ?ยง
)
203 "Characters used for bubbles.
204 Note that the actual number of different bubbles is determined by
205 the number of colors, see `bubbles-colors'."
206 :type
'(repeat character
)
209 (defcustom bubbles-shift-mode
212 Available modes are `shift-default' and `shift-always'."
213 :type
'(radio (const :tag
"Default" default
)
214 (const :tag
"Shifter" always
)
215 ;;(const :tag "Mega Shifter" 'mega)
219 (defcustom bubbles-mode-hook nil
220 "Hook run by Bubbles mode."
224 (defun bubbles-customize ()
225 "Open customization buffer for bubbles."
227 (customize-group 'bubbles
))
229 ;; ======================================================================
230 ;; internal variables
232 (defvar bubbles--score
0
233 "Current Bubbles score.")
235 (defvar bubbles--neighbourhood-score
0
236 "Score of active bubbles neighborhood.")
238 (defvar bubbles--faces nil
239 "List of currently used faces.")
241 (defvar bubbles--playing nil
242 "Play status indicator.")
244 (defvar bubbles--empty-image nil
245 "Image used for removed bubbles (empty grid cells).")
247 (defvar bubbles--images nil
248 "List of images for bubbles.")
250 (defvar bubbles--images-ok nil
251 "Indicate whether images have been created successfully.")
253 (defvar bubbles--col-offset
0
254 "Horizontal offset for centering the bubbles grid.")
256 (defvar bubbles--row-offset
0
257 "Vertical offset for centering the bubbles grid.")
259 (defvar bubbles--save-data nil
260 "List containing bubbles save data (SCORE BUFFERCONTENTS).")
262 (defconst bubbles--image-template-circle
264 static char * dot_xpm[] = {
270 \" .............. \",
271 \" ................ \",
272 \" ................ \",
273 \" .................. \",
274 \" .................. \",
275 \"....................\",
276 \"....................\",
277 \"....................\",
278 \"....................\",
279 \"....................\",
280 \"....................\",
281 \" .................. \",
282 \" .................. \",
283 \" ................ \",
284 \" ................ \",
285 \" .............. \",
289 (defconst bubbles--image-template-square
291 static char * dot_xpm[] = {
295 \"00000000000000000000\",
296 \"01111111111111111110\",
297 \"01111111111111111110\",
298 \"01111111111111111110\",
299 \"01111111111111111110\",
300 \"01111111111111111110\",
301 \"01111111111111111110\",
302 \"01111111111111111110\",
303 \"01111111111111111110\",
304 \"01111111111111111110\",
305 \"01111111111111111110\",
306 \"01111111111111111110\",
307 \"01111111111111111110\",
308 \"01111111111111111110\",
309 \"01111111111111111110\",
310 \"01111111111111111110\",
311 \"01111111111111111110\",
312 \"01111111111111111110\",
313 \"01111111111111111110\",
314 \"00000000000000000000\"};")
316 (defconst bubbles--image-template-diamond
318 static char * dot_xpm[] = {
322 \"00000000011000000000\",
323 \"00000000111100000000\",
324 \"00000001111110000000\",
325 \"00000011111111000000\",
326 \"00000111111111100000\",
327 \"00001111111111110000\",
328 \"00011111111111111000\",
329 \"00111111111111111100\",
330 \"01111111111111111110\",
331 \"11111111111111111111\",
332 \"01111111111111111110\",
333 \"00111111111111111100\",
334 \"00011111111111111000\",
335 \"00001111111111110000\",
336 \"00000111111111100000\",
337 \"00000011111111000000\",
338 \"00000001111110000000\",
339 \"00000000111100000000\",
340 \"00000000011000000000\",
341 \"00000000000000000000\"};")
343 (defconst bubbles--image-template-emacs
345 static char * emacs_24_xpm[] = {
476 \" . . . . . . . . . . . . . . . . . . \",
477 \" + @ # $ % % % % % % % % % % % % % % & * + + \",
478 \" = - ; > > > > > > > > , ' ) > > > > > > ! = \",
479 \"~ ~ { { { { { { { { { { { ] ^ / ( { { { { _ ~ ~ \",
480 \": : < < < < < < < < < < < < [ } } | < < < 1 : : \",
481 \"2 2 3 3 3 3 3 3 3 3 3 3 4 5 } } } 5 3 3 3 6 2 2 \",
482 \"7 7 8 8 8 8 8 8 8 8 9 0 a } } } b 8 8 8 8 c 7 7 \",
483 \"d d e e e e e e e f g } } } h i e e e e e j d d \",
484 \"k k l l l l l m n } } } o p l l l l l l l q k k \",
485 \"r r s s s s t u } } } v w s s s s s s s s x r r \",
486 \"y y z z z z A } } } B z z z z z z z z z z C y y \",
487 \"D D D D D D E F } } G D D D D D D D D D D H D D \",
488 \"I I I I I I I J K } L M N I I I I I I I I O I I \",
489 \"P P P P P P Q R } } } S T P P P P P P P P U P P \",
490 \"V V V V V V W } } X Y V V V V V V V V V V Z V V \",
491 \"` ` ` ` ` ` .} } ..` ` ` ` ` ` ` ` ` ` ` +.` ` \",
492 \"@.@.@.@.@.@.@.#.$.$.%.@.@.@.@.@.@.@.@.@.@.&.@.@.\",
493 \"*.*.*.*.*.*.*.*.=.-.} ;.>.*.*.*.*.*.*.*.*.,.*.*.\",
494 \"'.'.'.'.'.'.'.'.'.'.).!.} !.~.'.'.'.'.'.'.{.'.'.\",
495 \"].].].].].].].].].].].].^.} /.(.].].].].]._.].].\",
496 \":.:.:.:.:.:.:.:.:.:.<.[./.} } }.:.:.:.:.:.|.:.:.\",
497 \" 1.1.1.1.1.1.1.1.2.3.4.5.6.3.1.1.1.1.1.1.1.1. \",
498 \" 7.7.7.7.7.7.7.7.7.7.7.7.7.7.7.7.7.7.7.7.7.7. \",
499 \" 8.8.8.8.8.8.8.8.8.8.8.8.8.8.8.8.8.8. \"};")
501 (defconst bubbles--image-template-ball
503 static char * dot3d_xpm[] = {
695 \" . + @ # $ % & * = . \",
696 \" - ; > , ' ) ! ~ { ] ^ / \",
697 \" ( _ > : < [ } | 1 2 3 4 ] 5 6 ( \",
698 \" 7 ) 1 8 9 0 a b c | : 3 { d e f \",
699 \" g ! h i 0 j k l m n o | 2 ~ p q r s \",
700 \". t ' | u v w x y z A n B 1 ! C D E F . \",
701 \"G H : i I J K L M N z O b | ) P Q R S T \",
702 \"U V W X Y Z ` ...+.y l @.} ' #.$.e %.&.\",
703 \"& H W *.n =.-.;. .L x k 0 [ , #.Q e >.,.\",
704 \"] '.2 ).a k z -.` K w j !.< > ~.d {.].^.\",
705 \"d /.> (._.I k =.:.J v 0 8 : V <.[.}.|.1.\",
706 \"2.3.~ 4.5._.6.n Y I u i 1 > P $.7.8.9.0.\",
707 \"a.d b.V c.(.).*.X i | h ) '.d.e.E ].f.g.\",
708 \"h.i.$.C ~ > 2 W W : ' ! j.d.k.e l.9.m.n.\",
709 \". o.i.d p.q.'.H V H j.r.s.k.e 8.t.^.u.. \",
710 \" v.r w.x.Q s.d.d.y.] z.5 A.8.t.B.C.D. \",
711 \" E.l.F.e i.G.q 5 7.{.r %.H.^.I.J. \",
712 \" ( K.L.%.M.N.N.O.P.S Q.R.S.T.U.( \",
713 \" @ V.W.H.H.9.X.Y.S.I.Z.`. \",
714 \" . +.+++@+C.#+$+D.. \"};")
716 ;; ======================================================================
719 (defsubst bubbles--grid-width
()
720 "Return the grid width for the current game theme."
721 (car (case bubbles-game-theme
725 bubbles--grid-medium
)
731 bubbles-grid-size
))))
733 (defsubst bubbles--grid-height
()
734 "Return the grid height for the current game theme."
735 (cdr (case bubbles-game-theme
739 bubbles--grid-medium
)
745 bubbles-grid-size
))))
747 (defsubst bubbles--colors
()
748 "Return the color list for the current game theme."
749 (case bubbles-game-theme
761 (defsubst bubbles--shift-mode
()
762 "Return the shift mode for the current game theme."
763 (case bubbles-game-theme
773 bubbles-shift-mode
)))
775 (defun bubbles-save-settings ()
776 "Save current customization settings."
778 (custom-set-variables
779 (list 'bubbles-game-theme
`(quote ,bubbles-game-theme
) t
)
780 (list 'bubbles-graphics-theme
`(quote ,bubbles-graphics-theme
) t
))
781 (customize-save-customized))
783 (defsubst bubbles--empty-char
()
784 "The character used for removed bubbles (empty grid cells)."
787 (defun bubbles-set-graphics-theme-ascii ()
788 "Set graphics theme to `ascii'."
790 (setq bubbles-graphics-theme
'ascii
)
791 (bubbles--update-faces-or-images))
793 (defun bubbles-set-graphics-theme-circles ()
794 "Set graphics theme to `circles'."
796 (setq bubbles-graphics-theme
'circles
)
797 (bubbles--initialize-images)
798 (bubbles--update-faces-or-images))
800 (defun bubbles-set-graphics-theme-squares ()
801 "Set graphics theme to `squares'."
803 (setq bubbles-graphics-theme
'squares
)
804 (bubbles--initialize-images)
805 (bubbles--update-faces-or-images))
807 (defun bubbles-set-graphics-theme-diamonds ()
808 "Set graphics theme to `diamonds'."
810 (setq bubbles-graphics-theme
'diamonds
)
811 (bubbles--initialize-images)
812 (bubbles--update-faces-or-images))
814 (defun bubbles-set-graphics-theme-balls ()
815 "Set graphics theme to `balls'."
817 (setq bubbles-graphics-theme
'balls
)
818 (bubbles--initialize-images)
819 (bubbles--update-faces-or-images))
821 (defun bubbles-set-graphics-theme-emacs ()
822 "Set graphics theme to `emacs'."
824 (setq bubbles-graphics-theme
'emacs
)
825 (bubbles--initialize-images)
826 (bubbles--update-faces-or-images))
829 (defvar bubbles-game-theme-menu
830 (let ((menu (make-sparse-keymap "Game Theme")))
831 (define-key menu
[bubbles-set-game-userdefined
]
832 (list 'menu-item
"User defined" 'bubbles-set-game-userdefined
833 :button
'(:radio .
(eq bubbles-game-theme
'user-defined
))))
834 (define-key menu
[bubbles-set-game-hard
]
835 (list 'menu-item
"Hard" 'bubbles-set-game-hard
836 :button
'(:radio .
(eq bubbles-game-theme
'hard
))))
837 (define-key menu
[bubbles-set-game-difficult
]
838 (list 'menu-item
"Difficult" 'bubbles-set-game-difficult
839 :button
'(:radio .
(eq bubbles-game-theme
'difficult
))))
840 (define-key menu
[bubbles-set-game-medium
]
841 (list 'menu-item
"Medium" 'bubbles-set-game-medium
842 :button
'(:radio .
(eq bubbles-game-theme
'medium
))))
843 (define-key menu
[bubbles-set-game-easy
]
844 (list 'menu-item
"Easy" 'bubbles-set-game-easy
845 :button
'(:radio .
(eq bubbles-game-theme
'easy
))))
847 "Map for bubbles game theme menu.")
849 ;; graphics theme menu
850 (defvar bubbles-graphics-theme-menu
851 (let ((menu (make-sparse-keymap "Graphics Theme")))
852 (define-key menu
[bubbles-set-graphics-theme-ascii
]
853 (list 'menu-item
"ASCII" 'bubbles-set-graphics-theme-ascii
854 :button
'(:radio .
(eq bubbles-graphics-theme
'ascii
))))
855 (define-key menu
[bubbles-set-graphics-theme-emacs
]
856 (list 'menu-item
"Emacs" 'bubbles-set-graphics-theme-emacs
857 :button
'(:radio .
(eq bubbles-graphics-theme
'emacs
))))
858 (define-key menu
[bubbles-set-graphics-theme-balls
]
859 (list 'menu-item
"Balls" 'bubbles-set-graphics-theme-balls
860 :button
'(:radio .
(eq bubbles-graphics-theme
'balls
))))
861 (define-key menu
[bubbles-set-graphics-theme-diamonds
]
862 (list 'menu-item
"Diamonds" 'bubbles-set-graphics-theme-diamonds
863 :button
'(:radio .
(eq bubbles-graphics-theme
'diamonds
))))
864 (define-key menu
[bubbles-set-graphics-theme-squares
]
865 (list 'menu-item
"Squares" 'bubbles-set-graphics-theme-squares
866 :button
'(:radio .
(eq bubbles-graphics-theme
'squares
))))
867 (define-key menu
[bubbles-set-graphics-theme-circles
]
868 (list 'menu-item
"Circles" 'bubbles-set-graphics-theme-circles
869 :button
'(:radio .
(eq bubbles-graphics-theme
'circles
))))
871 "Map for bubbles graphics theme menu.")
875 (let ((menu (make-sparse-keymap "Bubbles")))
876 (define-key menu
[bubbles-quit
]
877 (list 'menu-item
"Quit" 'bubbles-quit
))
878 (define-key menu
[bubbles]
879 (list 'menu-item "New game" 'bubbles))
880 (define-key menu [bubbles-separator-1]
882 (define-key menu [bubbles-save-settings]
883 (list 'menu-item "Save all settings" 'bubbles-save-settings))
884 (define-key menu [bubbles-customize]
885 (list 'menu-item "Edit all settings" 'bubbles-customize))
886 (define-key menu [bubbles-game-theme-menu]
887 (list 'menu-item "Game Theme" bubbles-game-theme-menu))
888 (define-key menu [bubbles-graphics-theme-menu]
889 (list 'menu-item "Graphics Theme" bubbles-graphics-theme-menu
890 :enable 'bubbles--playing))
891 (define-key menu [bubbles-separator-2]
893 (define-key menu [bubbles-undo]
894 (list 'menu-item "Undo last move" 'bubbles-undo
895 :enable '(and bubbles--playing (listp buffer-undo-list))))
897 "Map for bubbles menu.")
900 (defvar bubbles-mode-map
901 (let ((map (make-sparse-keymap 'bubbles-mode-map)))
902 ;; (suppress-keymap map t)
903 (define-key map "q" 'bubbles-quit)
904 (define-key map "\n" 'bubbles-plop)
905 (define-key map " " 'bubbles-plop)
906 (define-key map [double-down-mouse-1] 'bubbles-plop)
907 (define-key map [mouse-2] 'bubbles-plop)
908 (define-key map "\C-m" 'bubbles-plop)
909 (define-key map "u" 'bubbles-undo)
910 (define-key map "p" 'previous-line)
911 (define-key map "n" 'next-line)
912 (define-key map "f" 'forward-char)
913 (define-key map "b" 'backward-char)
914 ;; bind menu to mouse
915 (define-key map [down-mouse-3] bubbles-menu)
916 ;; Put menu in menu-bar
917 (define-key map [menu-bar Bubbles] (cons "Bubbles" bubbles-menu))
919 "Mode map for bubbles.")
921 (define-derived-mode bubbles-mode nil "Bubbles"
922 "Major mode for playing bubbles.
923 \\{bubbles-mode-map}"
924 (setq buffer-read-only t
925 show-trailing-whitespace nil)
926 (buffer-disable-undo)
927 (force-mode-line-update)
929 (add-hook 'post-command-hook 'bubbles--mark-neighbourhood t t))
935 The goal is to remove all bubbles with as few moves as possible.
936 \\[bubbles-plop] on a bubble removes that bubble and all
937 connected bubbles of the same color. Unsupported bubbles fall
938 down, and columns that do not contain any bubbles suck the
939 columns on its right towards the left.
941 \\[bubbles-set-game-easy] sets the difficulty to easy.
942 \\[bubbles-set-game-medium] sets the difficulty to medium.
943 \\[bubbles-set-game-difficult] sets the difficulty to difficult.
944 \\[bubbles-set-game-hard] sets the difficulty to hard."
946 (switch-to-buffer (get-buffer-create "*bubbles*"))
947 (when (or (not bubbles--playing)
948 (y-or-n-p "Start new game? "))
949 (setq bubbles--save-data nil)
950 (setq bubbles--playing t)
951 (bubbles--initialize)))
953 (defun bubbles-quit ()
956 (message "bubbles-quit")
959 (declare-function image-size "image.c" (spec &optional pixels frame))
961 (defun bubbles--compute-offsets ()
962 "Update horizontal and vertical offsets for centering the bubbles grid.
963 Set `bubbles--col-offset' and `bubbles--row-offset'."
964 (cond ((and (display-images-p)
966 (not (eq bubbles-graphics-theme 'ascii))
967 (fboundp 'window-inside-pixel-edges))
968 ;; compute offset in units of pixels
969 (let ((bubbles--image-size
970 (car (image-size (car bubbles--images) t))))
971 (setq bubbles--col-offset
973 (max 0 (/ (- (nth 2 (window-inside-pixel-edges))
974 (nth 0 (window-inside-pixel-edges))
975 (* ( + bubbles--image-size 2) ;; margin
976 (bubbles--grid-width))) 2))))
977 (setq bubbles--row-offset
979 (max 0 (/ (- (nth 3 (window-inside-pixel-edges))
980 (nth 1 (window-inside-pixel-edges))
981 (* (+ bubbles--image-size 1) ;; margin
982 (bubbles--grid-height))) 2))))))
984 ;; compute offset in units of chars
985 (setq bubbles--col-offset
986 (max 0 (/ (- (window-width)
987 (bubbles--grid-width)) 2)))
988 (setq bubbles--row-offset
989 (max 0 (/ (- (window-height)
990 (bubbles--grid-height) 2) 2))))))
992 (defun bubbles--remove-overlays ()
993 "Remove all overlays."
994 (if (fboundp 'remove-overlays)
997 (defun bubbles--initialize ()
998 "Initialize Bubbles game."
999 (bubbles--initialize-faces)
1000 (bubbles--initialize-images)
1001 (bubbles--remove-overlays)
1003 (switch-to-buffer (get-buffer-create "*bubbles*"))
1004 (bubbles--compute-offsets)
1005 (let ((inhibit-read-only t))
1006 (set-buffer-modified-p nil)
1009 (add-text-properties
1010 (point-min) (point) (list 'intangible t 'display
1012 (list :height bubbles--row-offset))))
1014 (let ((max-char (length (bubbles--colors))))
1015 (dotimes (i (bubbles--grid-height))
1018 (add-text-properties
1019 p (point) (list 'intangible t
1020 'display (cons 'space
1022 bubbles--col-offset)))))
1023 (dotimes (j (bubbles--grid-width))
1024 (let* ((index (random max-char))
1025 (char (nth index bubbles-chars)))
1027 (add-text-properties (1- (point)) (point) (list 'index index))))
1030 (add-text-properties
1031 (1- (point)) (point) (list 'intangible t 'display
1033 (list :width bubbles--col-offset)))))
1034 (put-text-property (point-min) (point-max) 'pointer 'arrow))
1036 (bubbles--reset-score)
1037 (bubbles--update-faces-or-images)
1039 (setq buffer-undo-list t)
1040 (force-mode-line-update)
1043 (defun bubbles--initialize-faces ()
1044 "Prepare faces for playing `bubbles'."
1045 (copy-face 'default 'bubbles--highlight-face)
1046 (set-face-background 'bubbles--highlight-face "#8080f4")
1047 (when (display-color-p)
1048 (setq bubbles--faces
1049 (mapcar (lambda (color)
1050 (let ((fname (intern (format "bubbles--face-%s" color))))
1051 (unless (facep fname)
1052 (copy-face 'default fname)
1053 (set-face-foreground fname color))
1055 (bubbles--colors)))))
1057 (defsubst bubbles--row (pos)
1058 "Return row of point POS."
1062 (1- (count-lines (point-min) (point)))))
1064 (defsubst bubbles--col (pos)
1065 "Return column of point POS."
1068 (1- (current-column))))
1070 (defun bubbles--goto (row col)
1071 "Move point to bubble at coordinates ROW and COL."
1074 (>= row (bubbles--grid-height))
1075 (>= col (bubbles--grid-width)))
1076 ;; Error! return nil
1079 (goto-char (point-min))
1080 (forward-line (1+ row))
1081 (forward-char (1+ col))
1084 (defun bubbles--char-at (row col)
1085 "Return character at bubble ROW and COL."
1087 (if (bubbles--goto row col)
1088 (char-after (point))
1091 (defun bubbles--mark-direct-neighbours (row col char)
1092 "Mark direct neighbors of bubble at ROW COL with same CHAR."
1095 (when (and (bubbles--goto row col)
1096 (eq char (char-after (point)))
1097 (not (get-text-property (point) 'active)))
1098 (add-text-properties (point) (1+ (point))
1099 '(active t face 'bubbles--highlight-face))
1101 (bubbles--mark-direct-neighbours row (1+ col) char)
1102 (bubbles--mark-direct-neighbours row (1- col) char)
1103 (bubbles--mark-direct-neighbours (1+ row) col char)
1104 (bubbles--mark-direct-neighbours (1- row) col char))))
1107 (defun bubbles--mark-neighbourhood (&optional pos)
1108 "Mark neighborhood of point.
1109 Use optional parameter POS instead of point if given."
1110 (when bubbles--playing
1111 (unless pos (setq pos (point)))
1113 (let ((char (char-after pos))
1114 (inhibit-read-only t)
1115 (row (bubbles--row (point)))
1116 (col (bubbles--col (point))))
1117 (add-text-properties (point-min) (point-max)
1118 '(face default active nil))
1120 (when (and row col (not (eq char (bubbles--empty-char))))
1121 (setq count (bubbles--mark-direct-neighbours row col char))
1123 (add-text-properties (point-min) (point-max)
1124 '(face default active nil))
1126 (bubbles--update-neighbourhood-score count))
1127 (put-text-property (point-min) (point-max) 'pointer 'arrow)
1128 (bubbles--update-faces-or-images)
1130 (error (message "Bubbles: Internal error %s" err)))))
1132 (defun bubbles--neighbourhood-available ()
1133 "Return t if another valid neighborhood is available."
1136 (dotimes (i (bubbles--grid-height))
1137 (dotimes (j (bubbles--grid-width))
1138 (let ((c (bubbles--char-at i j)))
1139 (if (and (not (eq c (bubbles--empty-char)))
1140 (or (eq c (bubbles--char-at (1+ i) j))
1141 (eq c (bubbles--char-at i (1+ j)))))
1142 (throw 'found t)))))
1145 (defun bubbles--count ()
1146 "Count remaining bubbles."
1149 (dotimes (i (bubbles--grid-height))
1150 (dotimes (j (bubbles--grid-width))
1151 (let ((c (bubbles--char-at i j)))
1152 (if (not (eq c (bubbles--empty-char)))
1153 (setq count (1+ count)))))))
1156 (defun bubbles--reset-score ()
1157 "Reset bubbles score."
1158 (setq bubbles--neighbourhood-score 0
1160 (bubbles--update-score))
1162 (defun bubbles--update-score ()
1163 "Calculate and display new bubbles score."
1164 (setq bubbles--score (+ bubbles--score bubbles--neighbourhood-score))
1165 (bubbles--show-scores))
1167 (defun bubbles--update-neighbourhood-score (size)
1168 "Calculate and display score of active neighborhood from its SIZE."
1170 (setq bubbles--neighbourhood-score (expt (- size 1) 2))
1171 (setq bubbles--neighbourhood-score 0))
1172 (bubbles--show-scores))
1174 (defun bubbles--show-scores ()
1175 "Display current scores."
1177 (goto-char (or (next-single-property-change (point-min) 'status)
1179 (let ((inhibit-read-only t)
1181 (delete-region (point) (point-max))
1182 (insert (format "Selected: %4d\n" bubbles--neighbourhood-score))
1184 (add-text-properties (1- (point)) (point)
1185 (list 'intangible t 'display
1187 (list :width bubbles--col-offset))))
1188 (insert (format "Score: %4d" bubbles--score))
1189 (put-text-property pos (point) 'status t))))
1191 (defun bubbles--game-over ()
1192 "Finish bubbles game."
1193 (bubbles--update-faces-or-images)
1194 (setq bubbles--playing nil
1195 bubbles--save-data nil)
1196 ;; add bonus if all bubbles were removed
1197 (when (= 0 (bubbles--count))
1198 (setq bubbles--score (+ bubbles--score (* (bubbles--grid-height)
1199 (bubbles--grid-width))))
1200 (bubbles--show-scores))
1201 ;; Game over message
1202 (goto-char (point-max))
1203 (let* ((inhibit-read-only t))
1205 (add-text-properties (1- (point)) (point)
1206 (list 'intangible t 'display
1208 (list :width bubbles--col-offset))))
1209 (insert "Game Over!"))
1211 (gamegrid-add-score (format "bubbles-%s-%d-%d-%d-scores"
1212 (symbol-name (bubbles--shift-mode))
1213 (length (bubbles--colors))
1214 (bubbles--grid-width) (bubbles--grid-height))
1217 (defun bubbles-plop ()
1218 "Remove active bubbles region."
1220 (when (and bubbles--playing
1221 (> bubbles--neighbourhood-score 0))
1222 (setq bubbles--save-data (list bubbles--score (buffer-string)))
1223 (let ((inhibit-read-only t))
1224 ;; blank out current neighbourhood
1225 (let ((row (bubbles--row (point)))
1226 (col (bubbles--col (point))))
1227 (goto-char (point-max))
1230 (while (get-text-property (point) 'active)
1232 (insert (bubbles--empty-char))
1233 (add-text-properties (1- (point)) (point) (list 'removed t
1235 (bubbles--goto row col))
1237 (bubbles--update-score)
1238 ;; update display and wait
1239 (bubbles--update-faces-or-images)
1244 (let ((something-dropped nil))
1246 (dotimes (i (bubbles--grid-height))
1247 (dotimes (j (bubbles--grid-width))
1249 (while (get-text-property (point) 'removed)
1250 (setq something-dropped (or (bubbles--shift 'top i j)
1251 something-dropped))))))
1252 ;; update display and wait
1253 (bubbles--update-faces-or-images)
1254 (when something-dropped
1258 (put-text-property (point-min) (point-max) 'removed nil)
1260 (goto-char (point-min))
1261 (let ((removed-string (format "%c" (bubbles--empty-char))))
1262 (while (search-forward removed-string nil t)
1263 (put-text-property (1- (point)) (point) 'removed t))))
1264 (let ((shifted nil))
1265 (cond ((eq (bubbles--shift-mode) 'always)
1267 (dotimes (i (bubbles--grid-height))
1268 (dotimes (j (bubbles--grid-width))
1270 (while (get-text-property (point) 'removed)
1271 (setq shifted (or (bubbles--shift 'right i j)
1273 (bubbles--update-faces-or-images)
1275 (t ;; default shift-mode
1277 (dotimes (j (bubbles--grid-width))
1278 (bubbles--goto (1- (bubbles--grid-height)) j)
1279 (let ((shifted-cols 0))
1280 (while (get-text-property (point) 'removed)
1281 (setq shifted-cols (1+ shifted-cols))
1282 (bubbles--shift 'right (1- (bubbles--grid-height)) j))
1283 (dotimes (k shifted-cols)
1284 (let ((i (- (bubbles--grid-height) 2)))
1286 (setq shifted (or (bubbles--shift 'right i j)
1288 (setq i (1- i))))))))))
1291 (bubbles--update-faces-or-images)
1293 (put-text-property (point-min) (point-max) 'removed nil)
1294 (unless (bubbles--neighbourhood-available)
1295 (bubbles--game-over)))
1297 (setq buffer-undo-list '((apply bubbles-undo . nil)))
1298 (force-mode-line-update)
1301 (defun bubbles-undo ()
1304 (when bubbles--save-data
1305 (let ((inhibit-read-only t)
1308 (insert (cadr bubbles--save-data))
1309 (bubbles--update-faces-or-images)
1310 (setq bubbles--score (car bubbles--save-data))
1312 (setq buffer-undo-list t)
1313 (force-mode-line-update)
1316 (defun bubbles--shift (from row col)
1317 "Move bubbles FROM one side to position ROW COL.
1318 Return t if new char is non-empty."
1320 (when (bubbles--goto row col)
1321 (let ((char-new (bubbles--empty-char))
1326 (cond ((eq from 'top)
1327 (setq trow (1- row)))
1329 (setq tcol (1- col)))
1331 (setq tcol (1+ col))))
1333 (when (bubbles--goto trow tcol)
1334 (setq char-new (char-after (point)))
1335 (setq removed (get-text-property (point) 'removed))
1336 (setq index (get-text-property (point) 'index))
1337 (bubbles--shift from trow tcol)))
1340 (add-text-properties (1- (point)) (point) (list 'index index
1342 (not (eq char-new (bubbles--empty-char)))))))
1344 (defun bubbles--initialize-images ()
1345 "Prepare images for playing `bubbles'."
1346 (when (and (display-images-p)
1347 (not (eq bubbles-graphics-theme 'ascii)))
1348 (let ((template (case bubbles-graphics-theme
1349 (circles bubbles--image-template-circle)
1350 (balls bubbles--image-template-ball)
1351 (squares bubbles--image-template-square)
1352 (diamonds bubbles--image-template-diamond)
1353 (emacs bubbles--image-template-emacs))))
1354 (setq bubbles--empty-image
1355 (create-image (replace-regexp-in-string
1356 "^\"\\(.*\\)\t.*c .*\",$"
1357 "\"\\1\tc None\"," template)
1361 (setq bubbles--images
1362 (mapcar (lambda (color)
1363 (let* ((rgb (color-values color))
1369 (goto-char (point-min))
1371 "^\"[0-9]+ [0-9]+ \\(.*?\\) .*\",$" nil t)
1372 (goto-char (point-min))
1373 (while (re-search-forward
1374 "^\"\\(.*\\)\t.*c \\(#.*\\)\",$" nil t)
1375 (let* ((crgb (color-values (match-string 2)))
1379 (brightness (/ (+ r g b) 3.0 256 256))
1380 (val (sin (* brightness (/ float-pi 2))))
1384 ;;(rr (/ (+ red r) 2))
1385 ;;(gg (/ (+ green g) 2))
1386 ;;(bb (/ (+ blue b) 2))
1387 (color (format "#%02x%02x%02x"
1388 (/ rr 256) (/ gg 256)
1390 (replace-match (format "\"\\1\tc %s\","
1392 (create-image (buffer-string) 'xpm t
1396 (bubbles--colors))))
1398 (setq bubbles--images-ok bubbles--empty-image)
1400 (setq bubbles--images-ok (and bubbles--images-ok elt)))
1403 (defun bubbles--update-faces-or-images ()
1404 "Update faces and/or images, depending on graphics mode."
1405 (bubbles--set-faces)
1406 (bubbles--show-images))
1408 (defun bubbles--set-faces ()
1409 "Update faces in the bubbles buffer."
1410 (unless (and (display-images-p)
1412 (not (eq bubbles-graphics-theme 'ascii)))
1413 (when (display-color-p)
1415 (let ((inhibit-read-only t))
1416 (dotimes (i (bubbles--grid-height))
1417 (dotimes (j (bubbles--grid-width))
1419 (let ((face (nth (get-text-property (point) 'index)
1421 (when (get-text-property (point) 'active)
1422 (set-face-foreground 'bubbles--highlight-face "#ff0000")
1423 (setq face 'bubbles--highlight-face))
1424 (put-text-property (point) (1+ (point))
1427 (defun bubbles--show-images ()
1428 "Update images in the bubbles buffer."
1429 (bubbles--remove-overlays)
1430 (if (and (display-images-p)
1432 (not (eq bubbles-graphics-theme 'ascii)))
1434 (goto-char (point-min))
1436 (let ((inhibit-read-only t))
1437 (dotimes (i (bubbles--grid-height))
1438 (dotimes (j (bubbles--grid-width))
1440 (let ((index (or (get-text-property (point) 'index) -1)))
1441 (let ((img bubbles--empty-image))
1443 (setq img (nth index bubbles--images)))
1444 (put-text-property (point) (1+ (point))
1445 'display (cons img nil)))))
1448 (let ((inhibit-read-only t))
1449 (goto-char (point-min))
1451 (let ((disp-prop (get-text-property (point) 'display)))
1452 (if (and (listp disp-prop)
1453 (listp (car disp-prop))
1454 (eq (caar disp-prop) 'image))
1455 (put-text-property (point) (1+ (point)) 'display nil))
1457 (put-text-property (point-min) (point-max) 'pointer 'arrow)))))
1461 ;;; bubbles.el ends here