life.c revision 919
0N/A * Copyright (c) 1988-91 by Patrick J. Naughton. 0N/A * Permission to use, copy, modify, and distribute this software and its 0N/A * documentation for any purpose and without fee is hereby granted, 0N/A * provided that the above copyright notice appear in all copies and that 0N/A * both that copyright notice and this permission notice appear in 0N/A * supporting documentation. 0N/A * This file is provided AS IS with no warranties of any kind. The author 0N/A * shall have no liability with respect to the infringement of copyrights, 0N/A * trade secrets or any patents by this file or any part thereof. In no 0N/A * event will the author be liable for any lost revenue or profits or 0N/A * other special, indirect and consequential damages. 0N/A * Copyright 1994 Sun Microsystems, Inc. All rights reserved. 0N/A * Use is subject to license terms. 0N/A * Permission is hereby granted, free of charge, to any person obtaining a 0N/A * copy of this software and associated documentation files (the "Software"), 0N/A * to deal in the Software without restriction, including without limitation 0N/A * the rights to use, copy, modify, merge, publish, distribute, sublicense, 0N/A * and/or sell copies of the Software, and to permit persons to whom the 0N/A * Software is furnished to do so, subject to the following conditions: 0N/A * The above copyright notice and this permission notice (including the next 0N/A * paragraph) shall be included in all copies or substantial portions of the 0N/A * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 0N/A * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 0N/A * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 0N/A * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 0N/A * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 0N/A * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 0N/A * DEALINGS IN THE SOFTWARE. 0N/A * life.c - Conway's game of Life for xlock, the X Window System lockscreen. 0N/A * Copyright (c) 1991 by Patrick J. Naughton. * See xlock.c for copying information. * 24-May-91: Added wraparound code from johnson@bugs.comm.mot.com. * Made old cells stay blue. * Made batchcount control the number of generations till restart. * 29-Jul-90: support for multiple screens. * 07-Feb-90: remove bogus semi-colon after #include line. * 15-Dec-89: Fix for proper skipping of {White,Black}Pixel() in colors. * 08-Oct-89: Moved seconds() to an extern. * 20-Sep-89: Written (life algorithm courtesy of Jim Graham, flar@sun.com). 0, 0,
/* width, height */ 0,
XYBitmap, 0,
/* xoffset, format, data */ LSBFirst,
8,
/* byte-order, bitmap-unit */ LSBFirst,
8,
1 /* bitmap-bit-order, bitmap-pad, depth */ #
define min(a, b) ((a)<(b)?(a):(b))
/* Buffer stores the data for each cell. Each cell is stored as * 8 bits representing the presence of a critter in each of it's * surrounding 8 cells. There is an empty row and column around * the whole array to allow stores without bounds checking as well * as an extra row at the end for the fetches into tempbuf. * Tempbuf stores the data for the next two rows so that we know * the state of those critter before he was modified by the fate * of the critters that have already been processed. * Agebuf stores the age of each critter. /* Fates is a lookup table for the fate of a critter. The 256 * entries represent the 256 possible combinations of the 8 * neighbor cells. Each entry is one of BIRTH (create a cell * or leave one alive), SAME (leave the cell alive or dead), * or DEATH (kill anything in the cell). static unsigned char fates[
256];
1,
1,
2,
1,
3,
1,
4,
1,
5,
1,
-
7, -
3, -
6, -
3, -
2, -
3, -
1, -
3, 0, -
3,
1, -
3,
5, -
3,
6, -
3,
-
7, -
2, -
5, -
2, -
3, -
2,
2, -
2,
4, -
2,
6, -
2,
-
5, -
1, -
3, -
1, -
2, -
1,
2, -
1,
4, -
1,
-
7, 0, -
5, 0, -
3, 0,
2, 0,
4, 0,
6, 0,
-
7,
1, -
6,
1, -
2,
1, -
1,
1, 0,
1,
1,
1,
5,
1,
6,
1,
-
6, -
6, -
5, -
6,
6, -
6,
7, -
6,
-
6, -
5, -
5, -
5,
6, -
5,
7, -
5,
-
7,
6, -
5,
6,
6,
6,
8,
6,
-
7,
7, -
5,
7,
6,
7,
8,
7,
-
7,
8, -
6,
8,
7,
8,
8,
8,
-
8, -
6, -
7, -
6, -
4, -
6, -
1, -
6,
-
4, -
5, -
3, -
5, -
2, -
5, -
1, -
5,
-
5, -
4, -
3, -
4, 0, -
4,
2, -
4,
3, -
4,
-
5, -
3, -
1, -
3, 0, -
3,
2, -
3,
3, -
3,
-
8, -
2, -
7, -
2, -
5, -
2, -
2, -
2, 0, -
2,
-
8, -
1, -
7, -
1, -
5, -
1, 0, -
1,
-
4, 0, -
3, 0, -
2, 0, -
1, 0,
-
3, -
1, -
2, -
1, -
1, -
1,
1, -
1,
2, -
1,
3, -
1,
-
3, 0, -
2, 0,
1, 0,
2, 0,
-
3,
1, -
2,
1, -
1,
1,
1,
1,
2,
1,
3,
1,
-
3, -
1, -
2, -
1, -
1, -
1,
1, -
1,
2, -
1,
3, -
1,
-
3, 0, -
2, 0,
2, 0,
3, 0,
-
3,
1, -
2,
1, -
1,
1,
1,
1,
2,
1,
3,
1,
-
15, 0, -
14, 0, -
13, 0, -
12, 0, -
11, 0,
-
10, 0, -
9, 0, -
8, 0, -
7, 0, -
6, 0,
-
5, 0, -
4, 0, -
3, 0, -
2, 0, -
1, 0,
4, 0,
3, 0,
2, 0,
1, 0, 0, 0,
9, 0,
8, 0,
7, 0,
6, 0,
5, 0,
14, 0,
13, 0,
12, 0,
11, 0,
10, 0,
-
7, -
2, -
6, -
2, -
5, -
2, -
4, -
2, -
3, -
2,
-
2, -
2, -
1, -
2, 0, -
2,
1, -
2,
2, -
2,
-
5, -
1, -
4, -
1, -
3, -
1, -
2, -
1, -
1, -
1,
0, -
1,
1, -
1,
2, -
1,
3, -
1,
4, -
1,
-
3, 0, -
2, 0, -
1, 0, 0, 0,
1, 0,
2, 0,
3, 0,
4, 0,
5, 0,
6, 0,
-
10,
1, -
9,
1, -
8,
1, -
7,
1, -
6,
1,
-
5,
1, -
4,
1, -
3,
1, -
2,
1, -
1,
1,
-
10,
2, -
9,
2, -
8,
2, -
7,
2, -
6,
2,
-
5,
2, -
4,
2, -
3,
2, -
2,
2, -
1,
2,
-
5, 0, -
4, 0, -
3, 0, -
2, 0, -
1, 0,
4, 0,
3, 0,
2, 0,
1, 0, 0, 0,
-
4, -
4, -
3, -
4, -
2, -
4, -
1, -
4, 0, -
4,
1, -
4,
2, -
4,
3, -
4,
4, -
4,
-
4, -
2, -
2, -
2, -
1, -
2, 0, -
2,
1, -
2,
2, -
2,
4, -
2,
-
4, -
1, -
2, -
1,
2, -
1,
4, -
1,
-
4, 0, -
2, 0, -
1, 0, 0, 0,
1, 0,
2, 0,
4, 0,
-
4,
1, -
2,
1,
2,
1,
4,
1,
-
4,
2, -
2,
2, -
1,
2, 0,
2,
1,
2,
2,
2,
4,
2,
-
4,
4, -
3,
4, -
2,
4, -
1,
4, 0,
4,
1,
4,
2,
4,
3,
4,
4,
4,
/* if we aren't up to blue yet, then keep aging the cell. */ for (i = 0; i <
256; i++) {
/* copy the first 2 rows to the tempbuf */ /* copy the last row to another buffer for wraparound */ if (!(*(
loc +
1) &
RT)) {
* generate a randomized shooter aimed roughly toward the center of the