From http://www.jwz.org/xscreensaver/xscreensaver-5.39.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 #include "ximage-loader.h"
35
36 #define LEVHEIGHT       32U
37 #define LEVWIDTH        40U
38
39 #define TILEWIDTH 5U
40 #define TILEHEIGHT 5U
41
42 #define GETNB(n) ((1 << (n)) - 1)
43 #define TESTNB(v, n) (((1 << (n)) & v) != 0x00)
44 #define SNB(v, n) ((v) |= (1 << (n)))
45 #define UNSNB(v, n) ((v) &= ~(1 << (n)))
46 #define GHOSTS 4U
47 #define MAXMOUTH 3
48 #define MAXGPOS 2
49 #define MAXGDIR 4
50 #define MAXGWAG 2
51 #define MAXGFLASH 2
52 #define MINGRIDSIZE 4
53 #define MINSIZE 3
54 #define NOWHERE 16383
55 #define START ((LRAND() & 1) ? 1 : 3)
56 #define MINDOTPERC 10
57
58 #define YELLOW (MI_NPIXELS(mi) / 6)
59 #define GREEN (23 * MI_NPIXELS(mi) / 64)
60 #define BLUE (45 * MI_NPIXELS(mi) / 64)
61 #define WHITE (MI_NPIXELS(mi))
62
63 #define LINEWIDTH       4
64 #define HLINEWIDTH      1
65 #define JAILHEIGHT      7
66 #define JAILWIDTH       8
67
68 #define GETFACTOR(x, y) ((x) > (y) ? 1 : ((x) < (y) ? -1 : 0))
69 #define SIGN(x) GETFACTOR((x), 0)
70 #define TRACEVECS 40
71 #define PAC_DEATH_FRAMES 8
72
73 #define GHOST_TRACE ( LEVWIDTH * LEVHEIGHT )
74
75 #define DIRVECS 4
76 #define NUM_BONUS_DOTS 4
77
78 typedef struct
79 {
80     int vx, vy;
81 } tracevec_struct;
82
83 typedef enum
84     { inbox = 0, goingout, randdir, chasing, hiding, goingin } GhostState;
85 typedef enum
86     { ps_eating = 0, ps_chasing, ps_hiding, ps_random, ps_dieing } PacmanState;
87 typedef enum
88 { GHOST_DANGER, GHOST_EATEN } GameState;
89
90 typedef struct
91 {
92     volatile unsigned int col, row;
93     unsigned int lastbox, nextcol, nextrow;
94     int dead;
95     int cfactor, rfactor;
96     int cf, rf;
97     int oldcf, oldrf;
98     volatile int timeleft;
99     GhostState aistate;
100     int speed;
101     XPoint delta;
102     XPoint err;
103     int flash_scared;
104     int trace_idx;
105     tracevec_struct trace[GHOST_TRACE];
106     int home_idx;
107     volatile int home_count;
108     tracevec_struct way_home[GHOST_TRACE];
109     volatile int wait_pos; /* a cycle before calculating the position */
110 #if 0  /* Used for debugging */
111     int ndirs;
112     int oldndirs;
113 #endif
114
115 #if 0 /* Used for debugging */
116     char last_stat[1024];
117 #endif
118
119 } ghoststruct;
120
121 typedef struct
122 {
123     unsigned int col, row;
124     unsigned int lastbox, nextcol, nextrow;
125     int mouthstage, mouthdirection;
126     int cfactor, rfactor;
127     int cf, rf;
128     int oldcf, oldrf;
129     int oldlx, oldly;
130     int justate;
131     PacmanState aistate;
132     tracevec_struct trace[TRACEVECS];
133     int cur_trace;
134     int state_change;
135     int roundscore;
136     int speed;
137     int lastturn;
138     XPoint delta;
139     XPoint err;
140     int deaths;
141     int init_row;
142 } pacmanstruct;
143
144
145 typedef struct
146 {
147     unsigned int x, y;
148     int eaten;
149 } bonus_dot;
150
151
152 /* This are tiles which can be placed to create a level. */
153 struct tiles {
154     char block[TILEWIDTH * TILEHEIGHT + 1];
155     unsigned dirvec[4];
156     unsigned ndirs;
157     unsigned simular_to;
158 };
159
160 typedef struct
161 {
162     unsigned short width, height;
163     unsigned short nrows, ncols;
164     short xs, ys, xb, yb;
165     short incx, incy;
166     GC stippledGC;
167     int graphics_format;
168     pacmanstruct pacman;
169     ghoststruct *ghosts;
170     unsigned int nghosts;
171     Pixmap pacmanPixmap[4][MAXMOUTH];
172     Pixmap pacmanMask[4][MAXMOUTH];
173     Pixmap pacman_ds[PAC_DEATH_FRAMES]; /* pacman death sequence */
174     Pixmap pacman_ds_mask[PAC_DEATH_FRAMES];
175     Pixmap ghostPixmap[4][MAXGDIR][MAXGWAG];
176     Pixmap ghostMask;
177     Pixmap s_ghostPixmap[MAXGFLASH][MAXGWAG];   /* Scared ghost Pixmaps */
178     Pixmap ghostEyes[MAXGDIR];
179     char level[LEVHEIGHT * LEVWIDTH];
180     unsigned int wallwidth;
181     unsigned int dotsleft;
182     int spritexs, spriteys, spritedx, spritedy;
183
184     GameState gamestate;
185     unsigned int timeleft;
186
187     char last_pac_stat[1024];
188
189     /* draw_pacman_sprite */
190     int pm_mouth;
191     int pm_mouth_delay;
192     int pm_open_mouth;
193     int pm_death_frame;
194     int pm_death_delay;
195
196         /* draw_ghost_sprite */
197     int gh_wag;
198     int gh_wag_count;
199
200     /* flash_bonus_dots */
201     int bd_flash_count;
202     int bd_on;
203
204     /* pacman_tick */
205     int ghost_scared_timer;
206     int flash_timer;
207     PacmanState old_pac_state;
208
209     /* pacman_level.c */
210     bonus_dot bonus_dots[NUM_BONUS_DOTS];
211     struct tiles *tiles;
212
213 } pacmangamestruct;
214
215 extern pacmangamestruct *pacman_games;
216 extern Bool pacman_trackmouse;
217
218 typedef char lev_t[LEVHEIGHT][LEVWIDTH + 1];
219
220 #endif /* __PACMAN_H__ */