From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Dieter Deyke Newsgroups: gmane.emacs.devel Subject: [PATCH] elpa/packages/sokoban/sokoban.el Date: Tue, 12 Feb 2019 13:30:18 +0100 Message-ID: <87wom59okl.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="166116"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Tue Feb 12 13:35:32 2019 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gtXHg-000h4D-2O for ged-emacs-devel@m.gmane.org; Tue, 12 Feb 2019 13:35:32 +0100 Original-Received: from localhost ([127.0.0.1]:38466 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gtXHe-0005Uf-Jf for ged-emacs-devel@m.gmane.org; Tue, 12 Feb 2019 07:35:30 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:42797) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gtXHL-0005FC-1q for emacs-devel@gnu.org; Tue, 12 Feb 2019 07:35:17 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gtXDA-00045v-IN for emacs-devel@gnu.org; Tue, 12 Feb 2019 07:30:54 -0500 Original-Received: from [195.159.176.226] (port=36514 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gtXD0-0003em-KV for emacs-devel@gnu.org; Tue, 12 Feb 2019 07:30:45 -0500 Original-Received: from list by blaine.gmane.org with local (Exim 4.89) (envelope-from ) id 1gtXCj-000b3c-D0 for emacs-devel@gnu.org; Tue, 12 Feb 2019 13:30:25 +0100 X-Injected-Via-Gmane: http://gmane.org/ Cancel-Lock: sha1:MHJT6tj/VbWm4jh0ZhGSILrlXFs= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 195.159.176.226 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:233250 Archived-At: OK to commit this? ===== Commit message: ===== * sokoban/sokoban.el: Add save/load capability ===== Patch: ===== diff --git a/sokoban.el b/sokoban.el index 57aab47..128d59a 100644 --- a/sokoban.el +++ b/sokoban.el @@ -4,7 +4,7 @@ ;; Author: Glynn Clements ;; Maintainer: Dieter Deyke -;; Version: 1.4.7 +;; Version: 1.4.8 ;; Comment: While we set lexical-binding, it currently doesn't make use ;; of closures, which is why it can still work in Emacs-23.1. ;; Package-Requires: ((emacs "23.1") (cl-lib "0.5")) @@ -509,6 +509,8 @@ static char * player_on_target_xpm[] = { (define-key map "r" 'sokoban-restart-level) (define-key map "g" 'sokoban-goto-level) (define-key map "F" 'fit-frame-to-buffer) + (define-key map "s" 'sokoban-save) + (define-key map "l" 'sokoban-load) (define-key map [left] 'sokoban-move-left) (define-key map [right] 'sokoban-move-right) @@ -868,12 +870,58 @@ static char * player_on_target_xpm[] = { (setq sokoban-level 0) (sokoban-next-level)) +(defvar sokoban-grid-state) + +(defconst sokoban-state-variables '( + sokoban-level + sokoban-level-map + sokoban-targets + sokoban-x + sokoban-y + sokoban-moves + sokoban-pushes + sokoban-done + sokoban-undo-list + sokoban-grid-state + )) +(defun sokoban-save (filename) + "Save current Sokoban state." + (interactive "FSave file: ") + (let ((buf (current-buffer))) + (setq sokoban-grid-state nil) + (dotimes (y sokoban-height) + (dotimes (x sokoban-width) + (push (gamegrid-get-cell x y) sokoban-grid-state))) + (setq sokoban-grid-state (reverse sokoban-grid-state)) + (with-temp-file filename + (dolist (var sokoban-state-variables) + (print + (with-current-buffer buf (eval var)) + (current-buffer)))))) + +(defun sokoban-load (filename) + "Restore saved Sokoban state." + (interactive "fLoad file: ") + (let ((buf (current-buffer))) + (with-temp-buffer + (insert-file-contents filename) + (goto-char (point-min)) + (dolist (var sokoban-state-variables) + (let ((value (read (current-buffer)))) + (with-current-buffer buf (set var value)))))) + (dotimes (y sokoban-height) + (dotimes (x sokoban-width) + (gamegrid-set-cell x y (pop sokoban-grid-state)))) + (sokoban-draw-score)) + (easy-menu-define sokoban-popup-menu nil "Popup menu for Sokoban mode." '("Sokoban Commands" ["Restart this level" sokoban-restart-level] ["Start new game" sokoban-start-game] ["Go to specific level" sokoban-goto-level] - ["Fit frame to buffer" fit-frame-to-buffer])) + ["Fit frame to buffer" fit-frame-to-buffer] + ["Save current state" sokoban-save] + ["Restore saved state" sokoban-load])) (define-key sokoban-mode-map [down-mouse-3] sokoban-popup-menu) (define-derived-mode sokoban-mode special-mode "Sokoban" @@ -903,6 +951,8 @@ sokoban-mode keybindings: \\[sokoban-restart-level] Restarts the current level \\[sokoban-goto-level] Jumps to a specified level \\[fit-frame-to-buffer] Fit frame to buffer +\\[sokoban-save] Save current state +\\[sokoban-load] Restore saved state \\[sokoban-move-left] Move one square to the left \\[sokoban-move-right] Move one square to the right \\[sokoban-move-up] Move one square up -- Dieter Deyke mailto:dieter.deyke@gmail.com Get my Gnupg key: gpg --keyserver keys.gnupg.net --recv-keys B116EA20