From http://www.jwz.org/xscreensaver/xscreensaver-5.35.tar.gz
[xscreensaver] / hacks / pacman.h
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*-
3  * Copyright (c) 2002 by Edwin de Jong <mauddib@gmx.net>.
4  *
5  * Permission to use, copy, modify, and distribute this software and its
6  * documentation for any purpose and without fee is hereby granted,
7  * provided that the above copyright notice appear in all copies and that
8  * both that copyright notice and this permission notice appear in
9  * supporting documentation.
10  *
11  * This file is provided AS IS with no warranties of any kind.  The author
12  * shall have no liability with respect to the infringement of copyrights,
13  * trade secrets or any patents by this file or any part thereof.  In no
14  * event will the author be liable for any lost revenue or profits or
15  * other special, indirect and consequential damages.
16  *
17  * Revision History:
18  *  3-May-2002: Added AI to pacman and ghosts, slowed down ghosts.
19  * 26-Nov-2001: Random level generator added
20  * 01-Nov-2000: Allocation checks
21  * 04-Jun-1997: Compatible with xscreensaver
22  *
23  */
24
25 #ifndef __PACMAN_H__
26 #define __PACMAN_H__
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include "xlockmoreI.h"
33
34 #if defined(HAVE_GDK_PIXBUF) || defined(HAVE_XPM) || defined(HAVE_JWXYZ)
35 # define USE_PIXMAP
36 #include "xpm-pixmap.h"
37 # else
38 # if defined(USE_PIXMAP)
39 #  undef USE_PIXMAP
40 # endif
41 #endif
42
43 #define LEVHEIGHT       32U
44 #define LEVWIDTH        40U
45
46 #define TILEWIDTH 5U
47 #define TILEHEIGHT 5U
48
49 #define GETNB(n) ((1 << (n)) - 1)
50 #define TESTNB(v, n) (((1 << (n)) & v) != 0x00)
51 #define SNB(v, n) ((v) |= (1 << (n)))
52 #define UNSNB(v, n) ((v) &= ~(1 << (n)))
53 #define GHOSTS 4U
54 #if defined(USE_PIXMAP)
55 #define MAXMOUTH 3
56 #else
57 #define MAXMOUTH 11
58 #endif
59 #define MAXGPOS 2
60 #define MAXGDIR 4
61 #define MAXGWAG 2
62 #define MAXGFLASH 2
63 #define MINGRIDSIZE 4
64 #define MINSIZE 3
65 #define NOWHERE 16383
66 #define START ((LRAND() & 1) ? 1 : 3)
67 #define MINDOTPERC 10
68
69 #define YELLOW (MI_NPIXELS(mi) / 6)
70 #define GREEN (23 * MI_NPIXELS(mi) / 64)
71 #define BLUE (45 * MI_NPIXELS(mi) / 64)
72 #define WHITE (MI_NPIXELS(mi))
73
74 #define LINEWIDTH       4
75 #define HLINEWIDTH      1
76 #define JAILHEIGHT      7
77 #define JAILWIDTH       8
78
79 #define GETFACTOR(x, y) ((x) > (y) ? 1 : ((x) < (y) ? -1 : 0))
80 #define SIGN(x) GETFACTOR((x), 0)
81 #define TRACEVECS 40
82 #define PAC_DEATH_FRAMES 8
83
84 #define GHOST_TRACE ( LEVWIDTH * LEVHEIGHT )
85
86 #define DIRVECS 4
87 #define NUM_BONUS_DOTS 4
88
89 typedef struct
90 {
91     int vx, vy;
92 } tracevec_struct;
93
94 typedef enum
95     { inbox = 0, goingout, randdir, chasing, hiding, goingin } GhostState;
96 typedef enum
97     { ps_eating = 0, ps_chasing, ps_hiding, ps_random, ps_dieing } PacmanState;
98 typedef enum
99 { GHOST_DANGER, GHOST_EATEN } GameState;
100
101 typedef struct
102 {
103     volatile unsigned int col, row;
104     unsigned int lastbox, nextcol, nextrow;
105     int dead;
106     int cfactor, rfactor;
107     int cf, rf;
108     int oldcf, oldrf;
109     volatile int timeleft;
110     GhostState aistate;
111     int speed;
112     XPoint delta;
113     XPoint err;
114     int flash_scared;
115     int trace_idx;
116     tracevec_struct trace[GHOST_TRACE];
117     int home_idx;
118     volatile int home_count;
119     tracevec_struct way_home[GHOST_TRACE];
120     volatile int wait_pos; /* a cycle before calculating the position */
121 #if 0  /* Used for debugging */
122     int ndirs;
123     int oldndirs;
124 #endif
125
126 #if 0 /* Used for debugging */
127     char last_stat[1024];
128 #endif
129
130 } ghoststruct;
131
132 typedef struct
133 {
134     unsigned int col, row;
135     unsigned int lastbox, nextcol, nextrow;
136     int mouthstage, mouthdirection;
137     int cfactor, rfactor;
138     int cf, rf;
139     int oldcf, oldrf;
140     int oldlx, oldly;
141     int justate;
142     PacmanState aistate;
143     tracevec_struct trace[TRACEVECS];
144     int cur_trace;
145     int state_change;
146     int roundscore;
147     int speed;
148     int lastturn;
149     XPoint delta;
150     XPoint err;
151     int deaths;
152     int init_row;
153 } pacmanstruct;
154
155
156 typedef struct
157 {
158     unsigned int x, y;
159     int eaten;
160 } bonus_dot;
161
162
163 /* This are tiles which can be placed to create a level. */
164 struct tiles {
165     char block[TILEWIDTH * TILEHEIGHT + 1];
166     unsigned dirvec[4];
167     unsigned ndirs;
168     unsigned simular_to;
169 };
170
171 typedef struct
172 {
173     unsigned short width, height;
174     unsigned short nrows, ncols;
175     short xs, ys, xb, yb;
176     short incx, incy;
177     GC stippledGC;
178     int graphics_format;
179     pacmanstruct pacman;
180     ghoststruct *ghosts;
181     unsigned int nghosts;
182     Pixmap pacmanPixmap[4][MAXMOUTH];
183     Pixmap pacmanMask[4][MAXMOUTH];
184     Pixmap pacman_ds[PAC_DEATH_FRAMES]; /* pacman death sequence */
185     Pixmap pacman_ds_mask[PAC_DEATH_FRAMES];
186     Pixmap ghostPixmap[4][MAXGDIR][MAXGWAG];
187     Pixmap ghostMask;
188     Pixmap s_ghostPixmap[MAXGFLASH][MAXGWAG];   /* Scared ghost Pixmaps */
189     Pixmap ghostEyes[MAXGDIR];
190     char level[LEVHEIGHT * LEVWIDTH];
191     unsigned int wallwidth;
192     unsigned int dotsleft;
193     int spritexs, spriteys, spritedx, spritedy;
194
195     GameState gamestate;
196     unsigned int timeleft;
197
198     char last_pac_stat[1024];
199
200     /* draw_pacman_sprite */
201     int pm_mouth;
202     int pm_mouth_delay;
203     int pm_open_mouth;
204     int pm_death_frame;
205     int pm_death_delay;
206
207         /* draw_ghost_sprite */
208     int gh_wag;
209     int gh_wag_count;
210
211     /* flash_bonus_dots */
212     int bd_flash_count;
213     int bd_on;
214
215     /* pacman_tick */
216     int ghost_scared_timer;
217     int flash_timer;
218     PacmanState old_pac_state;
219
220     /* pacman_level.c */
221     bonus_dot bonus_dots[NUM_BONUS_DOTS];
222     struct tiles *tiles;
223
224 } pacmangamestruct;
225
226 extern pacmangamestruct *pacman_games;
227 extern Bool pacman_trackmouse;
228
229 typedef char lev_t[LEVHEIGHT][LEVWIDTH + 1];
230
231 #endif /* __PACMAN_H__ */