ftp://ftp.demon.nl/disk1/redhat-contrib/libc5/SRPMS/xscreensaver-2.14-1.src.rpm
[xscreensaver] / hacks / glx / rubik.c
1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* rubik --- Shows a auto-solving Rubik's cube */
3
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)rubik.c       4.04 97/07/28 xlockmore";
6
7 #endif
8
9 #undef DEBUG_LISTS
10 #undef LMN
11
12 /*-
13  * Permission to use, copy, modify, and distribute this software and its
14  * documentation for any purpose and without fee is hereby granted,
15  * provided that the above copyright notice appear in all copies and that
16  * both that copyright notice and this permission notice appear in
17  * supporting documentation.
18  *
19  * This file is provided AS IS with no warranties of any kind.  The author
20  * shall have no liability with respect to the infringement of copyrights,
21  * trade secrets or any patents by this file or any part thereof.  In no
22  * event will the author be liable for any lost revenue or profits or
23  * other special, indirect and consequential damages.
24  *
25  * This mode shows a auto-solving rubik's cube "puzzle". If somebody
26  * intends to make a game or something based on this code, please let me
27  * know first, my e-mail address is provided in this comment. Marcelo.
28  *
29  * Thanks goes also to Brian Paul for making it possible and inexpensive
30  * to use OpenGL at home.
31  *
32  * Since I'm not a native English speaker, my apologies for any grammatical
33  * mistake.
34  *
35  * My e-mail addresses are
36  * vianna@cat.cbpf.br 
37  *         and
38  * m-vianna@usa.net
39  *
40  * Marcelo F. Vianna (Jul-31-1997)
41  *
42  * Revision History:
43  * 08-Aug-97: Now has some internals from xrubik by David Bagley
44  *            This should make it easier to add features.
45  * 02-Aug-97: Now behaves more like puzzle.c: first show the cube being
46  *            shuffled and then being solved. A mode specific option was added:
47  *            "+/-hideshuffling" to provide the original behavior (in which
48  *            only the solution is shown).
49  *            The color labels corners are now rounded.
50  *            Optimized the cubit() routine using glLists.
51  * 01-Aug-97: Shuffling now avoids movements that undoes the previous movement
52  *            and three consecutive identical moves (which is pretty stupid).
53  *            improved the "cycles" option in replacement of David's hack,
54  *            now rp->anglestep is a GLfloat, so this option selects the
55  *            "exact" number of frames that a rotation (movement) takes to
56  *            complete.
57  * 30-Jul-97: Initial release, there is no algorithm to solve the puzzle,
58  *            instead, it randomly shuffle the cube and then make the
59  *            movements in the reverse order.
60  *            The mode was written in 1 day (I got sick and had the day off).
61  *            There was not much to do since I could not leave home... :)
62  *
63  */
64
65 /*-
66  * Color labels mapping:
67  * =====================
68  *
69  *                       +------------+
70  *                       |0-->        |
71  *                       ||           |
72  *                       |v           |
73  *                       |   TOP(0)   |
74  *                       |            |
75  *                       |            |
76  *                       |           8|
77  *           +-----------+------------+-----------+
78  *           |0-->       |0-->        |0-->       |
79  *           ||          ||           ||          |
80  *           |v          |v           |v          |
81  *           |  LEFT(1)  |  FRONT(2)  |  RIGHT(3) |
82  *           |           |            |           |
83  *           |           |            |           |
84  *           |          8|           8|          8|
85  *           +-----------+------------+-----------+
86  *                       |0-->        |
87  *                       ||           |
88  *                       |v           |
89  *                       |  BOTTOM(4) |  rp->faces[N][X+AVSIZE*Y]=
90  *                       |            |         rp->cubeLoc[N][X+AVSIZE*Y]=
91  *                       |            | 
92  *                       |           8|         +---+---+---+
93  *                       +------------+         |   |   |   |
94  *                       |0-->        |         | 0 | 1 | 2 |
95  *                       ||           |         |---+---+---+
96  *                       |v           |         |  xxxxx(N) |
97  *                       |   BACK(5)  |         | 3 | 4 | 5 |
98  *                       |            |         +---+---+---+
99  *                       |            |         |   |   |  |
100  *                       |           8|         | 6 | 7 | 8 |
101  *                       +------------+         +---+---+---+
102  *
103  *  Map to 3d
104  *  FRONT  => X, Y
105  *  BACK   => X, Y
106  *  LEFT   => Z, Y
107  *  RIGHT  => Z, Y
108  *  TOP    => X, Z
109  *  BOTTOM => X, Z
110  */
111
112 /*-
113  * PURIFY 3.0a on SunOS4 reports an unitialized memory read on each of
114  * the glCallList() functions below when using MesaGL 2.1.  This has
115  * been fixed in MesaGL 2.2 and later releases.
116  */
117
118 /*-
119  * due to a Bug/feature in VMS X11/Intrinsic.h has to be placed before xlock.
120  * otherwise caddr_t is not defined correctly
121  */
122 #include <X11/Intrinsic.h>
123
124 #ifdef STANDALONE
125 # define PROGCLASS      "Rubik"
126 # define HACK_INIT      init_rubik
127 # define HACK_DRAW      draw_rubik
128 # define rubik_opts     xlockmore_opts
129 # define DEFAULTS       "*delay: 40000 \n"              \
130                                         "*count: -30 \n"                \
131                                         "*cycles: 5 \n"                 \
132                                         "*size:  -6 \n"
133 # include "xlockmore.h"                         /* from the xscreensaver distribution */
134 #else /* !STANDALONE */
135 # include "xlock.h"                                     /* from the xlockmore distribution */
136 #endif /* !STANDALONE */
137
138 #ifdef USE_GL
139
140 #define DEF_HIDESHUFFLING     "False"
141
142 static Bool hideshuffling;
143
144 static XrmOptionDescRec opts[] =
145 {
146   {"-hideshuffling", ".rubik.hideshuffling", XrmoptionNoArg, (caddr_t) "on"},
147   {"+hideshuffling", ".rubik.hideshuffling", XrmoptionNoArg, (caddr_t) "off"}
148 };
149
150 static argtype vars[] =
151 {
152         {(caddr_t *) & hideshuffling, "hideshuffling", "Hideshuffling", DEF_HIDESHUFFLING, t_Bool}
153 };
154
155 static OptionStruct desc[] =
156 {
157         {"-/+hideshuffling", "turn on/off hidden shuffle phase"}
158 };
159
160 ModeSpecOpt rubik_opts =
161 {2, opts, 1, vars, desc};
162
163 #define VectMul(X1,Y1,Z1,X2,Y2,Z2) (Y1)*(Z2)-(Z1)*(Y2),(Z1)*(X2)-(X1)*(Z2),(X1)*(Y2)-(Y1)*(X2)
164 #define sqr(A)                     ((A)*(A))
165
166 #ifndef Pi
167 #define Pi                         M_PI
168 #endif
169
170
171 #define ACTION_SOLVE    1
172 #define ACTION_SHUFFLE  0
173
174 #define DELAY_AFTER_SHUFFLING  5
175 #define DELAY_AFTER_SOLVING   20
176
177 /*************************************************************************/
178
179 #define MINSIZE 2
180 #ifdef LMN                      /* LxMxN not completed yet... */
181 #define MAXSIZEX (rp->sizex)
182 #define MAXSIZEY (rp->sizey)
183 #define MAXSIZEZ (rp->sizez)
184 #define AVSIZE (rp->avsize)
185 #define MAXSIZE (rp->maxsize)
186 #define AVSIZESQ (rp->avsizeSq)
187 #define MAXSIZESQ (rp->maxsizeSq)
188 #else
189 #define MAXSIZEX (rp->size)
190 #define MAXSIZEY (rp->size)
191 #define MAXSIZEZ (rp->size)
192 #define AVSIZE (rp->size)
193 #define MAXSIZE (rp->size)
194 #define AVSIZESQ (rp->sizeSq)
195 #define MAXSIZESQ (rp->sizeSq)
196 #endif
197 #define MAXSIZEXY (MAXSIZEX*MAXSIZEY)
198 #define MAXSIZEZY (MAXSIZEZ*MAXSIZEY)
199 #define MAXSIZEXZ (MAXSIZEX*MAXSIZEZ)
200 #define LASTX (MAXSIZEX-1)
201 #define LASTY (MAXSIZEY-1)
202 #define LASTZ (MAXSIZEZ-1)
203 /* These are not likely to change but... */
204 #define FIRSTX 0
205 #define FIRSTY 0
206 #define FIRSTZ 0
207
208 #define Scale4Window               (0.9/AVSIZE)
209 #define Scale4Iconic               (2.1/AVSIZE)
210
211 #define MAXORIENT 4             /* Number of orientations of a square */
212 #define MAXFACES 6              /* Number of faces */
213
214 /* Directions relative to the face of a cubie */
215 #define TOP 0
216 #define RIGHT 1
217 #define BOTTOM 2
218 #define LEFT 3
219 #define CW (MAXORIENT+1)
220 #define CCW (2*MAXORIENT-1)
221
222 #define TOP_FACE 0
223 #define LEFT_FACE 1
224 #define FRONT_FACE 2
225 #define RIGHT_FACE 3
226 #define BOTTOM_FACE 4
227 #define BACK_FACE 5
228 #define NO_FACE (MAXFACES)
229 #define NO_ROTATION (2*MAXORIENT)
230 #define NO_DEPTH MAXSIZE
231
232 #define REVX(a) (MAXSIZEX - a - 1)
233 #define REVY(a) (MAXSIZEY - a - 1)
234 #define REVZ(a) (MAXSIZEZ - a - 1)
235
236 typedef struct _RubikLoc {
237         int         face;
238         int         rotation;   /* Not used yet */
239 } RubikLoc;
240
241 typedef struct _RubikRowNext {
242         int         face, direction, sideFace;
243 } RubikRowNext;
244
245 typedef struct _RubikMove {
246         int         face, direction;
247         int         position;
248 } RubikMove;
249
250 typedef struct _RubikSlice {
251         int         face, rotation;
252         int         depth;
253 } RubikSlice;
254
255 /*-
256  * Pick a face and a direction on face the next face and orientation
257  * is then known.
258  */
259 static RubikLoc slideNextRow[MAXFACES][MAXORIENT] =
260 {
261         {
262                 {5, TOP},
263                 {3, RIGHT},
264                 {2, TOP},
265                 {1, LEFT}},
266         {
267                 {0, RIGHT},
268                 {2, TOP},
269                 {4, LEFT},
270                 {5, BOTTOM}},
271         {
272                 {0, TOP},
273                 {3, TOP},
274                 {4, TOP},
275                 {1, TOP}},
276         {
277                 {0, LEFT},
278                 {5, BOTTOM},
279                 {4, RIGHT},
280                 {2, TOP}},
281         {
282                 {2, TOP},
283                 {3, LEFT},
284                 {5, TOP},
285                 {1, RIGHT}},
286         {
287                 {4, TOP},
288                 {3, BOTTOM},
289                 {0, TOP},
290                 {1, BOTTOM}}
291 };
292
293 /*-
294  * Examine cubie 0 on each face, its 4 movements (well only 2 since the
295  * other 2 will be opposites) and translate it into slice movements).
296  * Beware.. using this for NxNxN makes some assumptions that referenced
297  * cubes are along the diagonal top-left to bottom-right.
298  * CW = DEEP Depth CCW == SHALLOW Depth with reference to faces 0, 1, and 2
299  */
300 static RubikLoc rotateSlice[MAXFACES][MAXORIENT / 2] =
301 {
302         {
303                 {1, CCW},
304                 {2, CW},
305         },
306         {
307                 {2, CW},
308                 {0, CCW},
309         },
310         {
311                 {1, CCW},
312                 {0, CCW},
313         },
314         {
315                 {2, CCW},
316                 {0, CCW},
317         },
318         {
319                 {1, CCW},
320                 {2, CCW},
321         },
322         {
323                 {1, CCW},
324                 {0, CW},
325         }
326 };
327
328 /*-
329  * Rotate face clockwise by a number of orients, then the top of the
330  * face then points to this face
331  */
332 static int  rowToRotate[MAXFACES][MAXORIENT] =
333 {
334         {3, 2, 1, 5},
335         {2, 4, 5, 0},
336         {3, 4, 1, 0},
337         {5, 4, 2, 0},
338         {3, 5, 1, 2},
339         {3, 0, 1, 4}
340 };
341
342 /* 
343  * This translates a clockwise move to something more manageable
344  */
345 static RubikRowNext rotateToRow[MAXFACES] =     /*CW to min face */
346 {
347         {1, LEFT, TOP},
348         {0, BOTTOM, LEFT},
349         {0, RIGHT, BOTTOM},
350         {0, TOP, RIGHT},
351         {1, RIGHT, BOTTOM},
352         {0, LEFT, TOP}
353 };
354
355 typedef struct {
356         GLint       WindH, WindW;
357         GLfloat     step;
358         RubikMove  *moves;
359         int         storedmoves;
360         int         shufflingmoves;
361 #ifdef LMN                      /* Under construction */
362         int         sizex, sizey, sizez;
363         int         avsize, maxsize;
364         int         avsizeSq, maxsizeSq;
365 #else
366         int         size, sizeSq;
367 #endif
368         int         action;
369         int         done;
370         GLfloat     anglestep;
371         RubikLoc   *cubeLoc[MAXFACES];
372         RubikLoc   *rowLoc[MAXORIENT];
373         RubikMove   movement;
374         GLfloat     rotatestep;
375         GLXContext  glx_context;
376         int         AreObjectsDefined[1];
377 } rubikstruct;
378
379 static float front_shininess[] =
380 {60.0};
381 static float front_specular[] =
382 {0.7, 0.7, 0.7, 1.0};
383 static float ambient[] =
384 {0.0, 0.0, 0.0, 1.0};
385 static float diffuse[] =
386 {1.0, 1.0, 1.0, 1.0};
387 static float position0[] =
388 {1.0, 1.0, 1.0, 0.0};
389 static float position1[] =
390 {-1.0, -1.0, 1.0, 0.0};
391 static float lmodel_ambient[] =
392 {0.5, 0.5, 0.5, 1.0};
393 static float lmodel_twoside[] =
394 {GL_TRUE};
395
396 static float MaterialRed[] =
397 {0.5, 0.0, 0.0, 1.0};
398 static float MaterialGreen[] =
399 {0.0, 0.5, 0.0, 1.0};
400 static float MaterialBlue[] =
401 {0.0, 0.0, 0.5, 1.0};
402 static float MaterialYellow[] =
403 {0.7, 0.7, 0.0, 1.0};
404 static float MaterialOrange[] =
405 {0.9, 0.45, 0.36, 1.0};
406
407 #if 0
408 static float MaterialMagenta[] =
409 {0.7, 0.0, 0.7, 1.0};
410 static float MaterialCyan[] =
411 {0.0, 0.7, 0.7, 1.0};
412
413 #endif
414 static float MaterialWhite[] =
415 {0.8, 0.8, 0.8, 1.0};
416 static float MaterialGray[] =
417 {0.2, 0.2, 0.2, 1.0};
418 static float MaterialGray3[] =
419 {0.3, 0.3, 0.3, 1.0};
420 static float MaterialGray4[] =
421 {0.4, 0.4, 0.4, 1.0};
422 static float MaterialGray5[] =
423 {0.5, 0.5, 0.5, 1.0};
424 static float MaterialGray6[] =
425 {0.6, 0.6, 0.6, 1.0};
426 static float MaterialGray7[] =
427 {0.7, 0.7, 0.7, 1.0};
428
429 static rubikstruct *rubik = NULL;
430 static GLuint objects;
431
432 #define ObjCubit        0
433
434 static void
435 pickcolor(int C, int mono)
436 {
437         switch (C) {
438                 case TOP_FACE:
439                         if (mono)
440                                 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray3);
441                         else
442                                 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialRed);
443                         break;
444                 case LEFT_FACE:
445                         if (mono)
446                                 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray6);
447                         else
448                                 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialYellow);
449                         break;
450                 case FRONT_FACE:
451                         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialWhite);
452                         break;
453                 case RIGHT_FACE:
454                         if (mono)
455                                 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray4);
456                         else
457                                 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGreen);
458                         break;
459                 case BOTTOM_FACE:
460                         if (mono)
461                                 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray7);
462                         else
463                                 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialOrange);
464                         break;
465                 case BACK_FACE:
466                         if (mono)
467                                 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray5);
468                         else
469                                 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialBlue);
470                         break;
471 #if 0
472                         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialCyan);
473                         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialMagenta);
474 #endif
475         }
476 }
477
478
479 static void
480 draw_cubit(ModeInfo * mi,
481            int back, int front, int left, int right, int bottom, int top)
482 {
483         rubikstruct *rp = &rubik[MI_SCREEN(mi)];
484         int         mono = MI_WIN_IS_MONO(mi);
485
486         if (!rp->AreObjectsDefined[ObjCubit]) {
487                 glNewList(objects + ObjCubit, GL_COMPILE_AND_EXECUTE);
488                 glBegin(GL_QUADS);
489                 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray);
490                 glNormal3f(0.00, 0.00, 1.00);
491                 glVertex3f(-0.45, -0.45, 0.50);
492                 glVertex3f(0.45, -0.45, 0.50);
493                 glVertex3f(0.45, 0.45, 0.50);
494                 glVertex3f(-0.45, 0.45, 0.50);
495                 glNormal3f(0.00, 0.00, -1.00);
496                 glVertex3f(-0.45, 0.45, -0.50);
497                 glVertex3f(0.45, 0.45, -0.50);
498                 glVertex3f(0.45, -0.45, -0.50);
499                 glVertex3f(-0.45, -0.45, -0.50);
500                 glNormal3f(-1.00, 0.00, 0.00);
501                 glVertex3f(-0.50, -0.45, 0.45);
502                 glVertex3f(-0.50, 0.45, 0.45);
503                 glVertex3f(-0.50, 0.45, -0.45);
504                 glVertex3f(-0.50, -0.45, -0.45);
505                 glNormal3f(1.00, 0.00, 0.00);
506                 glVertex3f(0.50, -0.45, -0.45);
507                 glVertex3f(0.50, 0.45, -0.45);
508                 glVertex3f(0.50, 0.45, 0.45);
509                 glVertex3f(0.50, -0.45, 0.45);
510                 glNormal3f(0.00, -1.00, 0.00);
511                 glVertex3f(0.45, -0.50, -0.45);
512                 glVertex3f(0.45, -0.50, 0.45);
513                 glVertex3f(-0.45, -0.50, 0.45);
514                 glVertex3f(-0.45, -0.50, -0.45);
515                 glNormal3f(0.00, 1.00, 0.00);
516                 glVertex3f(-0.45, 0.50, -0.45);
517                 glVertex3f(-0.45, 0.50, 0.45);
518                 glVertex3f(0.45, 0.50, 0.45);
519                 glVertex3f(0.45, 0.50, -0.45);
520                 glNormal3f(-1.00, -1.00, 0.00);
521                 glVertex3f(-0.45, -0.50, -0.45);
522                 glVertex3f(-0.45, -0.50, 0.45);
523                 glVertex3f(-0.50, -0.45, 0.45);
524                 glVertex3f(-0.50, -0.45, -0.45);
525                 glNormal3f(1.00, 1.00, 0.00);
526                 glVertex3f(0.45, 0.50, -0.45);
527                 glVertex3f(0.45, 0.50, 0.45);
528                 glVertex3f(0.50, 0.45, 0.45);
529                 glVertex3f(0.50, 0.45, -0.45);
530                 glNormal3f(-1.00, 1.00, 0.00);
531                 glVertex3f(-0.50, 0.45, -0.45);
532                 glVertex3f(-0.50, 0.45, 0.45);
533                 glVertex3f(-0.45, 0.50, 0.45);
534                 glVertex3f(-0.45, 0.50, -0.45);
535                 glNormal3f(1.00, -1.00, 0.00);
536                 glVertex3f(0.50, -0.45, -0.45);
537                 glVertex3f(0.50, -0.45, 0.45);
538                 glVertex3f(0.45, -0.50, 0.45);
539                 glVertex3f(0.45, -0.50, -0.45);
540                 glNormal3f(0.00, -1.00, -1.00);
541                 glVertex3f(-0.45, -0.45, -0.50);
542                 glVertex3f(0.45, -0.45, -0.50);
543                 glVertex3f(0.45, -0.50, -0.45);
544                 glVertex3f(-0.45, -0.50, -0.45);
545                 glNormal3f(0.00, 1.00, 1.00);
546                 glVertex3f(-0.45, 0.45, 0.50);
547                 glVertex3f(0.45, 0.45, 0.50);
548                 glVertex3f(0.45, 0.50, 0.45);
549                 glVertex3f(-0.45, 0.50, 0.45);
550                 glNormal3f(0.00, -1.00, 1.00);
551                 glVertex3f(-0.45, -0.50, 0.45);
552                 glVertex3f(0.45, -0.50, 0.45);
553                 glVertex3f(0.45, -0.45, 0.50);
554                 glVertex3f(-0.45, -0.45, 0.50);
555                 glNormal3f(0.00, 1.00, -1.00);
556                 glVertex3f(-0.45, 0.50, -0.45);
557                 glVertex3f(0.45, 0.50, -0.45);
558                 glVertex3f(0.45, 0.45, -0.50);
559                 glVertex3f(-0.45, 0.45, -0.50);
560                 glNormal3f(-1.00, 0.00, -1.00);
561                 glVertex3f(-0.50, -0.45, -0.45);
562                 glVertex3f(-0.50, 0.45, -0.45);
563                 glVertex3f(-0.45, 0.45, -0.50);
564                 glVertex3f(-0.45, -0.45, -0.50);
565                 glNormal3f(1.00, 0.00, 1.00);
566                 glVertex3f(0.50, -0.45, 0.45);
567                 glVertex3f(0.50, 0.45, 0.45);
568                 glVertex3f(0.45, 0.45, 0.50);
569                 glVertex3f(0.45, -0.45, 0.50);
570                 glNormal3f(1.00, 0.00, -1.00);
571                 glVertex3f(0.45, -0.45, -0.50);
572                 glVertex3f(0.45, 0.45, -0.50);
573                 glVertex3f(0.50, 0.45, -0.45);
574                 glVertex3f(0.50, -0.45, -0.45);
575                 glNormal3f(-1.00, 0.00, 1.00);
576                 glVertex3f(-0.45, -0.45, 0.50);
577                 glVertex3f(-0.45, 0.45, 0.50);
578                 glVertex3f(-0.50, 0.45, 0.45);
579                 glVertex3f(-0.50, -0.45, 0.45);
580                 glEnd();
581                 glBegin(GL_TRIANGLES);
582                 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray);
583                 glNormal3f(1.00, 1.00, 1.00);
584                 glVertex3f(0.45, 0.45, 0.50);
585                 glVertex3f(0.50, 0.45, 0.45);
586                 glVertex3f(0.45, 0.50, 0.45);
587                 glNormal3f(-1.00, -1.00, -1.00);
588                 glVertex3f(-0.45, -0.50, -0.45);
589                 glVertex3f(-0.50, -0.45, -0.45);
590                 glVertex3f(-0.45, -0.45, -0.50);
591                 glNormal3f(-1.00, 1.00, 1.00);
592                 glVertex3f(-0.45, 0.45, 0.50);
593                 glVertex3f(-0.45, 0.50, 0.45);
594                 glVertex3f(-0.50, 0.45, 0.45);
595                 glNormal3f(1.00, -1.00, -1.00);
596                 glVertex3f(0.50, -0.45, -0.45);
597                 glVertex3f(0.45, -0.50, -0.45);
598                 glVertex3f(0.45, -0.45, -0.50);
599                 glNormal3f(1.00, -1.00, 1.00);
600                 glVertex3f(0.45, -0.45, 0.50);
601                 glVertex3f(0.45, -0.50, 0.45);
602                 glVertex3f(0.50, -0.45, 0.45);
603                 glNormal3f(-1.00, 1.00, -1.00);
604                 glVertex3f(-0.50, 0.45, -0.45);
605                 glVertex3f(-0.45, 0.50, -0.45);
606                 glVertex3f(-0.45, 0.45, -0.50);
607                 glNormal3f(-1.00, -1.00, 1.00);
608                 glVertex3f(-0.45, -0.45, 0.50);
609                 glVertex3f(-0.50, -0.45, 0.45);
610                 glVertex3f(-0.45, -0.50, 0.45);
611                 glNormal3f(1.00, 1.00, -1.00);
612                 glVertex3f(0.50, 0.45, -0.45);
613                 glVertex3f(0.45, 0.45, -0.50);
614                 glVertex3f(0.45, 0.50, -0.45);
615                 glEnd();
616                 glEndList();
617                 rp->AreObjectsDefined[ObjCubit] = 1;
618 #ifdef DEBUG_LISTS
619                 (void) printf("Cubit drawn SLOWLY\n");
620 #endif
621         } else {
622                 glCallList(objects + ObjCubit);
623 #ifdef DEBUG_LISTS
624                 (void) printf("Cubit drawn quickly\n");
625 #endif
626         }
627
628         if (back != NO_FACE) {
629                 glBegin(GL_POLYGON);
630                 pickcolor(back, mono);
631                 glNormal3f(0.00, 0.00, -1.00);
632                 glVertex3f(-0.35, 0.40, -0.51);
633                 glVertex3f(0.35, 0.40, -0.51);
634                 glVertex3f(0.40, 0.35, -0.51);
635                 glVertex3f(0.40, -0.35, -0.51);
636                 glVertex3f(0.35, -0.40, -0.51);
637                 glVertex3f(-0.35, -0.40, -0.51);
638                 glVertex3f(-0.40, -0.35, -0.51);
639                 glVertex3f(-0.40, 0.35, -0.51);
640                 glEnd();
641         }
642         if (front != NO_FACE) {
643                 glBegin(GL_POLYGON);
644                 pickcolor(front, mono);
645                 glNormal3f(0.00, 0.00, 1.00);
646                 glVertex3f(-0.35, -0.40, 0.51);
647                 glVertex3f(0.35, -0.40, 0.51);
648                 glVertex3f(0.40, -0.35, 0.51);
649                 glVertex3f(0.40, 0.35, 0.51);
650                 glVertex3f(0.35, 0.40, 0.51);
651                 glVertex3f(-0.35, 0.40, 0.51);
652                 glVertex3f(-0.40, 0.35, 0.51);
653                 glVertex3f(-0.40, -0.35, 0.51);
654                 glEnd();
655         }
656         if (left != NO_FACE) {
657                 glBegin(GL_POLYGON);
658                 pickcolor(left, mono);
659                 glNormal3f(-1.00, 0.00, 0.00);
660                 glVertex3f(-0.51, -0.35, 0.40);
661                 glVertex3f(-0.51, 0.35, 0.40);
662                 glVertex3f(-0.51, 0.40, 0.35);
663                 glVertex3f(-0.51, 0.40, -0.35);
664                 glVertex3f(-0.51, 0.35, -0.40);
665                 glVertex3f(-0.51, -0.35, -0.40);
666                 glVertex3f(-0.51, -0.40, -0.35);
667                 glVertex3f(-0.51, -0.40, 0.35);
668                 glEnd();
669         }
670         if (right != NO_FACE) {
671                 glBegin(GL_POLYGON);
672                 pickcolor(right, mono);
673                 glNormal3f(1.00, 0.00, 0.00);
674                 glVertex3f(0.51, -0.35, -0.40);
675                 glVertex3f(0.51, 0.35, -0.40);
676                 glVertex3f(0.51, 0.40, -0.35);
677                 glVertex3f(0.51, 0.40, 0.35);
678                 glVertex3f(0.51, 0.35, 0.40);
679                 glVertex3f(0.51, -0.35, 0.40);
680                 glVertex3f(0.51, -0.40, 0.35);
681                 glVertex3f(0.51, -0.40, -0.35);
682                 glEnd();
683         }
684         if (bottom != NO_FACE) {
685                 glBegin(GL_POLYGON);
686                 pickcolor(bottom, mono);
687                 glNormal3f(0.00, -1.00, 0.00);
688                 glVertex3f(0.40, -0.51, -0.35);
689                 glVertex3f(0.40, -0.51, 0.35);
690                 glVertex3f(0.35, -0.51, 0.40);
691                 glVertex3f(-0.35, -0.51, 0.40);
692                 glVertex3f(-0.40, -0.51, 0.35);
693                 glVertex3f(-0.40, -0.51, -0.35);
694                 glVertex3f(-0.35, -0.51, -0.40);
695                 glVertex3f(0.35, -0.51, -0.40);
696                 glEnd();
697         }
698         if (top != NO_FACE) {
699                 glBegin(GL_POLYGON);
700                 pickcolor(top, mono);
701                 glNormal3f(0.00, 1.00, 0.00);
702                 glVertex3f(-0.40, 0.51, -0.35);
703                 glVertex3f(-0.40, 0.51, 0.35);
704                 glVertex3f(-0.35, 0.51, 0.40);
705                 glVertex3f(0.35, 0.51, 0.40);
706                 glVertex3f(0.40, 0.51, 0.35);
707                 glVertex3f(0.40, 0.51, -0.35);
708                 glVertex3f(0.35, 0.51, -0.40);
709                 glVertex3f(-0.35, 0.51, -0.40);
710                 glEnd();
711         }
712         glEnd();
713 }
714
715
716 static      RubikSlice
717 convertMove(rubikstruct * rp, RubikMove move)
718 {
719         RubikSlice  slice;
720         RubikLoc    plane;
721
722         plane = rotateSlice[(int) move.face][move.direction % 2];
723         slice.face = plane.face;
724         slice.rotation = plane.rotation;
725         if (slice.rotation == CW)       /* I just know this to be true... */
726                 slice.depth = AVSIZESQ - 1 - move.position;
727         else
728                 slice.depth = move.position;
729         slice.depth = slice.depth / AVSIZE;
730         /* If slice.depth = 0 then face 0, face 1, or face 2 moves */
731         if (move.direction / 2)
732                 slice.rotation = (plane.rotation == CW) ? CCW : CW;
733         return slice;
734 }
735
736 /* Assume for the moment that the size is at least 2 */
737 static void
738 draw_cube(ModeInfo * mi)
739 {
740 #define S1 1
741 #define SX ((GLint)S1*(MAXSIZEX-1))
742 #define SY ((GLint)S1*(MAXSIZEY-1))
743 #define SZ ((GLint)S1*(MAXSIZEZ-1))
744 #define HALFX (((GLfloat)MAXSIZEX-1.0)/2.0)
745 #define HALFY (((GLfloat)MAXSIZEY-1.0)/2.0)
746 #define HALFZ (((GLfloat)MAXSIZEZ-1.0)/2.0)
747 #define MIDX(a) (((GLfloat)(2*a-MAXSIZEX+1))/2.0)
748 #define MIDY(a) (((GLfloat)(2*a-MAXSIZEY+1))/2.0)
749 #define MIDZ(a) (((GLfloat)(2*a-MAXSIZEZ+1))/2.0)
750         rubikstruct *rp = &rubik[MI_SCREEN(mi)];
751         RubikSlice  slice;
752         GLfloat     rotatestep;
753         int         i, j, k;
754
755         if (rp->movement.face == NO_FACE) {
756                 slice.face = NO_FACE;
757                 slice.rotation = NO_ROTATION;
758                 slice.depth = NO_DEPTH;
759         } else {
760                 slice = convertMove(rp, rp->movement);
761         }
762         rotatestep = (slice.rotation == CCW) ? rp->rotatestep : -rp->rotatestep;
763
764 /*-
765  * The glRotatef() routine transforms the coordinate system for every future
766  * vertex specification (this is not so simple, but by now comprehending this
767  * is sufficient). So if you want to rotate the inner slice, you can draw
768  * one slice, rotate the anglestep for the centerslice, draw the inner slice,
769  * rotate reversely and draw the other slice.
770  * There is a sequence for drawing cubies for each axis being moved...
771  */
772         switch (slice.face) {
773                 case NO_FACE:
774                 case TOP_FACE:  /* BOTTOM_FACE too */
775                         glPushMatrix();
776                         if (slice.depth == MAXSIZEY - 1)
777                                 glRotatef(rotatestep, 0, HALFY, 0);
778
779                         glTranslatef(-HALFX, -HALFY, -HALFZ);
780                         draw_cubit(mi,
781                                    rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * FIRSTY].face, NO_FACE,
782                                    rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face, NO_FACE,
783                                    rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * LASTZ].face, NO_FACE);
784                         for (k = 1; k < MAXSIZEZ - 1; k++) {
785                                 glTranslatef(0, 0, S1);
786                                 draw_cubit(mi,
787                                            NO_FACE, NO_FACE,
788                                            rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * LASTY].face, NO_FACE,
789                                            rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
790                         }
791                         glTranslatef(0, 0, S1);
792                         draw_cubit(mi,
793                                    NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * LASTY].face,
794                                    rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * LASTY].face, NO_FACE,
795                                    rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
796                         for (i = 1; i < MAXSIZEX - 1; i++) {
797                                 glTranslatef(S1, 0, -SZ);
798                                 draw_cubit(mi,
799                                            rp->cubeLoc[BACK_FACE][i + MAXSIZEX * FIRSTY].face, NO_FACE,
800                                            NO_FACE, NO_FACE,
801                                            rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * LASTZ].face, NO_FACE);
802                                 for (k = 1; k < MAXSIZEZ - 1; k++) {
803                                         glTranslatef(0, 0, S1);
804                                         draw_cubit(mi,
805                                                    NO_FACE, NO_FACE,
806                                                    NO_FACE, NO_FACE,
807                                                    rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * REVZ(k)].face, NO_FACE);
808                                 }
809                                 glTranslatef(0, 0, S1);
810                                 draw_cubit(mi,
811                                            NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * LASTY].face,
812                                            NO_FACE, NO_FACE,
813                                            rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * FIRSTZ].face, NO_FACE);
814                         }
815                         glTranslatef(1, 0, -SZ);
816                         draw_cubit(mi,
817                                    rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * FIRSTY].face, NO_FACE,
818                                    NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * LASTY].face,
819                                    rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * LASTZ].face, NO_FACE);
820                         for (k = 1; k < MAXSIZEZ - 1; k++) {
821                                 glTranslatef(0, 0, S1);
822                                 draw_cubit(mi,
823                                            NO_FACE, NO_FACE,
824                                            NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * LASTY].face,
825                                            rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
826                         }
827                         glTranslatef(0, 0, S1);
828                         draw_cubit(mi,
829                                    NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * LASTY].face,
830                                    NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face,
831                                    rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
832                         glPopMatrix();
833                         for (j = 1; j < MAXSIZEY - 1; j++) {
834                                 glPushMatrix();
835                                 if (slice.depth == REVY(j))
836                                         glRotatef(rotatestep, 0, HALFY, 0);
837                                 glTranslatef(-HALFX, MIDY(j), -HALFZ);
838                                 draw_cubit(mi,
839                                            rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * j].face, NO_FACE,
840                                            rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
841                                            NO_FACE, NO_FACE);
842                                 for (k = 1; k < MAXSIZEZ - 1; k++) {
843                                         glTranslatef(0, 0, S1);
844                                         draw_cubit(mi,
845                                                    NO_FACE, NO_FACE,
846                                                    rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * REVY(j)].face, NO_FACE,
847                                                    NO_FACE, NO_FACE);
848                                 }
849                                 glTranslatef(0, 0, S1);
850                                 draw_cubit(mi,
851                                            NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * REVY(j)].face,
852                                            rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
853                                            NO_FACE, NO_FACE);
854                                 for (i = 1; i < MAXSIZEX - 1; i++) {
855                                         glTranslatef(1, 0, -SZ);
856                                         draw_cubit(mi,
857                                                    rp->cubeLoc[BACK_FACE][i + MAXSIZEX * j].face, NO_FACE,
858                                                    NO_FACE, NO_FACE,
859                                                    NO_FACE, NO_FACE);
860                                         /* Center */
861                                         glTranslatef(0, 0, SZ);
862                                         draw_cubit(mi,
863                                                    NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * REVY(j)].face,
864                                                    NO_FACE, NO_FACE,
865                                                    NO_FACE, NO_FACE);
866                                 }
867                                 glTranslatef(S1, 0, -SZ);
868                                 draw_cubit(mi,
869                                            rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * j].face, NO_FACE,
870                                            NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face,
871                                            NO_FACE, NO_FACE);
872                                 for (k = 1; k < MAXSIZEZ - 1; k++) {
873                                         glTranslatef(0, 0, S1);
874                                         draw_cubit(mi,
875                                                    NO_FACE, NO_FACE,
876                                                    NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * REVY(j)].face,
877                                                    NO_FACE, NO_FACE);
878                                 }
879                                 glTranslatef(0, 0, S1);
880                                 draw_cubit(mi,
881                                            NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * REVY(j)].face,
882                                            NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face,
883                                            NO_FACE, NO_FACE);
884                                 glPopMatrix();
885                         }
886                         if (slice.depth == 0)
887                                 glRotatef(rotatestep, 0, HALFY, 0);
888
889                         glTranslatef(-HALFX, HALFY, -HALFZ);
890                         draw_cubit(mi,
891                                    rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * LASTY].face, NO_FACE,
892                                    rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
893                                    NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face);
894                         for (k = 1; k < MAXSIZEZ - 1; k++) {
895                                 glTranslatef(0, 0, S1);
896                                 draw_cubit(mi,
897                                            NO_FACE, NO_FACE,
898                                            rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * FIRSTY].face, NO_FACE,
899                                            NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * k].face);
900                         }
901                         glTranslatef(0, 0, S1);
902                         draw_cubit(mi,
903                                    NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * FIRSTY].face,
904                                    rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
905                                    NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * LASTZ].face);
906                         for (i = 1; i < MAXSIZEX - 1; i++) {
907                                 glTranslatef(S1, 0, -SZ);
908                                 draw_cubit(mi,
909                                            rp->cubeLoc[BACK_FACE][i + MAXSIZEX * LASTY].face, NO_FACE,
910                                            NO_FACE, NO_FACE,
911                                            NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * FIRSTZ].face);
912                                 for (k = 1; k < MAXSIZEZ - 1; k++) {
913                                         glTranslatef(0, 0, S1);
914                                         draw_cubit(mi,
915                                                    NO_FACE, NO_FACE,
916                                                    NO_FACE, NO_FACE,
917                                                    NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * k].face);
918                                 }
919                                 glTranslatef(0, 0, S1);
920                                 draw_cubit(mi,
921                                            NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * FIRSTY].face,
922                                            NO_FACE, NO_FACE,
923                                            NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * LASTZ].face);
924                         }
925                         glTranslatef(S1, 0, -SZ);
926                         draw_cubit(mi,
927                                    rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * LASTY].face, NO_FACE,
928                                    NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face,
929                                    NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * FIRSTZ].face);
930                         for (k = 1; k < MAXSIZEZ - 1; k++) {
931                                 glTranslatef(0, 0, S1);
932                                 draw_cubit(mi,
933                                            NO_FACE, NO_FACE,
934                                            NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * FIRSTY].face,
935                                            NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * k].face);
936                         }
937                         glTranslatef(0, 0, S1);
938                         draw_cubit(mi,
939                                    NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * FIRSTY].face,
940                                    NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face,
941                                    NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * LASTZ].face);
942                         break;
943                 case LEFT_FACE: /* RIGHT_FACE too */
944                         /* rotatestep is negative because the RIGHT face is the default here */
945                         glPushMatrix();
946                         if (slice.depth == 0)
947                                 glRotatef(-rotatestep, HALFX, 0, 0);
948
949                         glTranslatef(-HALFX, -HALFY, -HALFZ);
950                         draw_cubit(mi,
951                                    rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * FIRSTY].face, NO_FACE,
952                                    rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face, NO_FACE,
953                                    rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * LASTZ].face, NO_FACE);
954                         for (j = 1; j < MAXSIZEY - 1; j++) {
955                                 glTranslatef(0, S1, 0);
956                                 draw_cubit(mi,
957                                            rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * j].face, NO_FACE,
958                                            rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
959                                            NO_FACE, NO_FACE);
960                         }
961                         glTranslatef(0, S1, 0);
962                         draw_cubit(mi,
963                                    rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * LASTY].face, NO_FACE,
964                                    rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
965                                    NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face);
966                         for (k = 1; k < MAXSIZEZ - 1; k++) {
967                                 glTranslatef(0, -SY, S1);
968                                 draw_cubit(mi,
969                                            NO_FACE, NO_FACE,
970                                            rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * LASTY].face, NO_FACE,
971                                            rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
972                                 for (j = 1; j < MAXSIZEY - 1; j++) {
973                                         glTranslatef(0, S1, 0);
974                                         draw_cubit(mi,
975                                                    NO_FACE, NO_FACE,
976                                                    rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * REVY(j)].face, NO_FACE,
977                                                    NO_FACE, NO_FACE);
978                                 }
979                                 glTranslatef(0, S1, 0);
980                                 draw_cubit(mi,
981                                            NO_FACE, NO_FACE,
982                                            rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * FIRSTY].face, NO_FACE,
983                                            NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * k].face);
984                         }
985                         glTranslatef(0, -SY, S1);
986                         draw_cubit(mi,
987                                    NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * LASTY].face,
988                                    rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * LASTY].face, NO_FACE,
989                                    rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
990                         for (j = 1; j < MAXSIZEY - 1; j++) {
991                                 glTranslatef(0, S1, 0);
992                                 draw_cubit(mi,
993                                            NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * REVY(j)].face,
994                                            rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
995                                            NO_FACE, NO_FACE);
996                         }
997                         glTranslatef(0, S1, 0);
998                         draw_cubit(mi,
999                                    NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * FIRSTY].face,
1000                                    rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
1001                                    NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * LASTZ].face);
1002                         glPopMatrix();
1003                         for (i = 1; i < MAXSIZEX - 1; i++) {
1004                                 glPushMatrix();
1005                                 if (slice.depth == i)
1006                                         glRotatef(-rotatestep, HALFX, 0, 0);
1007                                 glTranslatef(MIDX(i), -HALFY, -HALFZ);
1008                                 draw_cubit(mi,
1009                                            rp->cubeLoc[BACK_FACE][i + MAXSIZEX * FIRSTY].face, NO_FACE,
1010                                            NO_FACE, NO_FACE,
1011                                            rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * LASTZ].face, NO_FACE);
1012                                 for (j = 1; j < MAXSIZEY - 1; j++) {
1013                                         glTranslatef(0, S1, 0);
1014                                         draw_cubit(mi,
1015                                                    rp->cubeLoc[BACK_FACE][i + MAXSIZEX * j].face, NO_FACE,
1016                                                    NO_FACE, NO_FACE,
1017                                                    NO_FACE, NO_FACE);
1018                                 }
1019                                 glTranslatef(0, S1, 0);
1020                                 draw_cubit(mi,
1021                                            rp->cubeLoc[BACK_FACE][i + MAXSIZEX * LASTY].face, NO_FACE,
1022                                            NO_FACE, NO_FACE,
1023                                            NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * FIRSTZ].face);
1024                                 for (k = 1; k < MAXSIZEZ - 1; k++) {
1025                                         glTranslatef(0, -SY, S1);
1026                                         draw_cubit(mi,
1027                                                    NO_FACE, NO_FACE,
1028                                                    NO_FACE, NO_FACE,
1029                                                    rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * REVZ(k)].face, NO_FACE);
1030                                         /* Center */
1031                                         glTranslatef(0, SY, 0);
1032                                         draw_cubit(mi,
1033                                                    NO_FACE, NO_FACE,
1034                                                    NO_FACE, NO_FACE,
1035                                                    NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * k].face);
1036                                 }
1037                                 glTranslatef(0, -SY, S1);
1038                                 draw_cubit(mi,
1039                                            NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * LASTY].face,
1040                                            NO_FACE, NO_FACE,
1041                                            rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * FIRSTZ].face, NO_FACE);
1042                                 for (j = 1; j < MAXSIZEY - 1; j++) {
1043                                         glTranslatef(0, S1, 0);
1044                                         draw_cubit(mi,
1045                                                    NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * REVY(j)].face,
1046                                                    NO_FACE, NO_FACE,
1047                                                    NO_FACE, NO_FACE);
1048                                 }
1049                                 glTranslatef(0, S1, 0);
1050                                 draw_cubit(mi,
1051                                            NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * FIRSTY].face,
1052                                            NO_FACE, NO_FACE,
1053                                            NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * LASTZ].face);
1054                                 glPopMatrix();
1055                         }
1056                         if (slice.depth == MAXSIZEX - 1)
1057                                 glRotatef(-rotatestep, HALFX, 0, 0);
1058                         glTranslatef(HALFX, -HALFY, -HALFZ);
1059                         draw_cubit(mi,
1060                                    rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * FIRSTY].face, NO_FACE,
1061                                    NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * LASTY].face,
1062                                    rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * LASTZ].face, NO_FACE);
1063                         for (j = 1; j < MAXSIZEY - 1; j++) {
1064                                 glTranslatef(0, S1, 0);
1065                                 draw_cubit(mi,
1066                                            rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * j].face, NO_FACE,
1067                                            NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face,
1068                                            NO_FACE, NO_FACE);
1069                         }
1070                         glTranslatef(0, S1, 0);
1071                         draw_cubit(mi,
1072                                    rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * LASTY].face, NO_FACE,
1073                                    NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face,
1074                                    NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * FIRSTZ].face);
1075                         for (k = 1; k < MAXSIZEZ - 1; k++) {
1076                                 glTranslatef(0, -SY, S1);
1077                                 draw_cubit(mi,
1078                                            NO_FACE, NO_FACE,
1079                                            NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * LASTY].face,
1080                                            rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
1081                                 for (j = 1; j < MAXSIZEY - 1; j++) {
1082                                         glTranslatef(0, S1, 0);
1083                                         draw_cubit(mi,
1084                                                    NO_FACE, NO_FACE,
1085                                                    NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * REVY(j)].face,
1086                                                    NO_FACE, NO_FACE);
1087                                 }
1088                                 glTranslatef(0, S1, 0);
1089                                 draw_cubit(mi,
1090                                            NO_FACE, NO_FACE,
1091                                            NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * FIRSTY].face,
1092                                            NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * k].face);
1093                         }
1094                         glTranslatef(0, -SY, S1);
1095                         draw_cubit(mi,
1096                                    NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * LASTY].face,
1097                                    NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face,
1098                                    rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
1099                         for (j = 1; j < MAXSIZEY - 1; j++) {
1100                                 glTranslatef(0, S1, 0);
1101                                 draw_cubit(mi,
1102                                            NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * REVY(j)].face,
1103                                            NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face,
1104                                            NO_FACE, NO_FACE);
1105                         }
1106                         glTranslatef(0, S1, 0);
1107                         draw_cubit(mi,
1108                                    NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * FIRSTY].face,
1109                                    NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face,
1110                                    NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * LASTZ].face);
1111                         break;
1112                 case FRONT_FACE:        /* BACK_FACE too */
1113                         glPushMatrix();
1114                         if (slice.depth == MAXSIZEZ - 1)
1115                                 glRotatef(rotatestep, 0, 0, HALFZ);
1116
1117                         glTranslatef(-HALFX, -HALFY, -HALFZ);
1118                         draw_cubit(mi,
1119                                    rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * FIRSTY].face, NO_FACE,
1120                                    rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face, NO_FACE,
1121                                    rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * LASTZ].face, NO_FACE);
1122                         for (i = 1; i < MAXSIZEX - 1; i++) {
1123                                 glTranslatef(S1, 0, 0);
1124                                 draw_cubit(mi,
1125                                            rp->cubeLoc[BACK_FACE][i + MAXSIZEX * FIRSTY].face, NO_FACE,
1126                                            NO_FACE, NO_FACE,
1127                                            rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * LASTZ].face, NO_FACE);
1128                         }
1129                         glTranslatef(S1, 0, 0);
1130                         draw_cubit(mi,
1131                                    rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * FIRSTY].face, NO_FACE,
1132                                    NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * LASTY].face,
1133                                    rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * LASTZ].face, NO_FACE);
1134                         for (j = 1; j < MAXSIZEY - 1; j++) {
1135                                 glTranslatef(-SX, S1, 0);
1136                                 draw_cubit(mi,
1137                                            rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * j].face, NO_FACE,
1138                                            rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
1139                                            NO_FACE, NO_FACE);
1140                                 for (i = 1; i < MAXSIZEX - 1; i++) {
1141                                         glTranslatef(S1, 0, 0);
1142                                         draw_cubit(mi,
1143                                                    rp->cubeLoc[BACK_FACE][i + MAXSIZEX * j].face, NO_FACE,
1144                                                    NO_FACE, NO_FACE,
1145                                                    NO_FACE, NO_FACE);
1146                                 }
1147                                 glTranslatef(S1, 0, 0);
1148                                 draw_cubit(mi,
1149                                            rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * j].face, NO_FACE,
1150                                            NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face,
1151                                            NO_FACE, NO_FACE);
1152                         }
1153                         glTranslatef(-SX, S1, 0);
1154                         draw_cubit(mi,
1155                                    rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * LASTY].face, NO_FACE,
1156                                    rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
1157                                    NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face);
1158                         for (i = 1; i < MAXSIZEX - 1; i++) {
1159                                 glTranslatef(S1, 0, 0);
1160                                 draw_cubit(mi,
1161                                            rp->cubeLoc[BACK_FACE][i + MAXSIZEX * LASTY].face, NO_FACE,
1162                                            NO_FACE, NO_FACE,
1163                                            NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * FIRSTZ].face);
1164                         }
1165                         glTranslatef(S1, 0, 0);
1166                         draw_cubit(mi,
1167                                    rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * LASTY].face, NO_FACE,
1168                                    NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face,
1169                                    NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * FIRSTZ].face);
1170                         glPopMatrix();
1171                         for (k = 1; k < MAXSIZEZ - 1; k++) {
1172                                 glPushMatrix();
1173                                 if (slice.depth == REVZ(k))
1174                                         glRotatef(rotatestep, 0, 0, HALFZ);
1175                                 glTranslatef(-HALFX, -HALFY, MIDZ(k));
1176                                 draw_cubit(mi,
1177                                            NO_FACE, NO_FACE,
1178                                            rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * LASTY].face, NO_FACE,
1179                                            rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
1180                                 for (i = 1; i < MAXSIZEX - 1; i++) {
1181                                         glTranslatef(S1, 0, 0);
1182                                         draw_cubit(mi,
1183                                                    NO_FACE, NO_FACE,
1184                                                    NO_FACE, NO_FACE,
1185                                                    rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * REVZ(k)].face, NO_FACE);
1186                                 }
1187                                 glTranslatef(S1, 0, 0);
1188                                 draw_cubit(mi,
1189                                            NO_FACE, NO_FACE,
1190                                            NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * LASTY].face,
1191                                            rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
1192                                 for (j = 1; j < MAXSIZEY - 1; j++) {
1193                                         glTranslatef(-SX, S1, 0);
1194                                         draw_cubit(mi,
1195                                                    NO_FACE, NO_FACE,
1196                                                    rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * REVY(j)].face, NO_FACE,
1197                                                    NO_FACE, NO_FACE);
1198                                         /* Center */
1199                                         glTranslatef(SX, 0, 0);
1200                                         draw_cubit(mi,
1201                                                    NO_FACE, NO_FACE,
1202                                                    NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * REVY(j)].face,
1203                                                    NO_FACE, NO_FACE);
1204                                 }
1205                                 glTranslatef(-SX, S1, 0);
1206                                 draw_cubit(mi,
1207                                            NO_FACE, NO_FACE,
1208                                            rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * FIRSTY].face, NO_FACE,
1209                                            NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * k].face);
1210                                 for (i = 1; i < MAXSIZEX - 1; i++) {
1211                                         glTranslatef(S1, 0, 0);
1212                                         draw_cubit(mi,
1213                                                    NO_FACE, NO_FACE,
1214                                                    NO_FACE, NO_FACE,
1215                                                    NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * k].face);
1216                                 }
1217                                 glTranslatef(S1, 0, 0);
1218                                 draw_cubit(mi,
1219                                            NO_FACE, NO_FACE,
1220                                            NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * FIRSTY].face,
1221                                            NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * k].face);
1222                                 glPopMatrix();
1223                         }
1224                         if (slice.depth == 0)
1225                                 glRotatef(rotatestep, 0, 0, HALFZ);
1226                         glTranslatef(-HALFX, -HALFY, HALFZ);
1227                         draw_cubit(mi,
1228                                    NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * LASTY].face,
1229                                    rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * LASTY].face, NO_FACE,
1230                                    rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
1231                         for (i = 1; i < MAXSIZEX - 1; i++) {
1232                                 glTranslatef(S1, 0, 0);
1233                                 draw_cubit(mi,
1234                                            NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * LASTY].face,
1235                                            NO_FACE, NO_FACE,
1236                                            rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * FIRSTZ].face, NO_FACE);
1237                         }
1238                         glTranslatef(S1, 0, 0);
1239                         draw_cubit(mi,
1240                                    NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * LASTY].face,
1241                                    NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face,
1242                                    rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
1243                         for (j = 1; j < MAXSIZEY - 1; j++) {
1244                                 glTranslatef(-SX, S1, 0);
1245                                 draw_cubit(mi,
1246                                            NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * REVY(j)].face,
1247                                            rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
1248                                            NO_FACE, NO_FACE);
1249                                 for (i = 1; i < MAXSIZEX - 1; i++) {
1250                                         glTranslatef(S1, 0, 0);
1251                                         draw_cubit(mi,
1252                                                    NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * REVY(j)].face,
1253                                                    NO_FACE, NO_FACE,
1254                                                    NO_FACE, NO_FACE);
1255                                 }
1256                                 glTranslatef(S1, 0, 0);
1257                                 draw_cubit(mi,
1258                                            NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * REVY(j)].face,
1259                                            NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face,
1260                                            NO_FACE, NO_FACE);
1261                         }
1262                         glTranslatef(-SX, S1, 0);
1263                         draw_cubit(mi,
1264                                    NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * FIRSTY].face,
1265                                    rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
1266                                    NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * LASTZ].face);
1267                         for (i = 1; i < MAXSIZEX - 1; i++) {
1268                                 glTranslatef(S1, 0, 0);
1269                                 draw_cubit(mi,
1270                                            NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * FIRSTY].face,
1271                                            NO_FACE, NO_FACE,
1272                                            NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * LASTZ].face);
1273                         }
1274                         glTranslatef(S1, 0, 0);
1275                         draw_cubit(mi,
1276                                    NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * FIRSTY].face,
1277                                    NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face,
1278                                    NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * LASTZ].face);
1279                         break;
1280         }
1281 #undef S1
1282 }
1283
1284 /* From David Bagley's xrubik.  Used by permission. ;)  */
1285 static void
1286 readRC(rubikstruct * rp, int face, int dir, int h, int orient, int size)
1287 {
1288         int         g;
1289
1290         if (dir == TOP || dir == BOTTOM)
1291                 for (g = 0; g < size; g++)
1292                         rp->rowLoc[orient][g] =
1293                                 rp->cubeLoc[face][g * size + h];
1294         else                    /* dir == RIGHT || dir == LEFT */
1295                 for (g = 0; g < size; g++)
1296                         rp->rowLoc[orient][g] =
1297                                 rp->cubeLoc[face][h * size + g];
1298 }
1299
1300 static void
1301 rotateRC(rubikstruct * rp, int rotate, int orient, int size)
1302 {
1303         int         g;
1304
1305         for (g = 0; g < size; g++)
1306                 rp->rowLoc[orient][g].rotation =
1307                         (rp->rowLoc[orient][g].rotation + rotate) % MAXORIENT;
1308 }
1309
1310 static void
1311 reverseRC(rubikstruct * rp, int orient, int size)
1312 {
1313         int         g;
1314         RubikLoc    temp;
1315
1316         for (g = 0; g < size / 2; g++) {
1317                 temp = rp->rowLoc[orient][size - 1 - g];
1318                 rp->rowLoc[orient][size - 1 - g] = rp->rowLoc[orient][g];
1319                 rp->rowLoc[orient][g] = temp;
1320         }
1321 }
1322
1323 static void
1324 writeRC(rubikstruct * rp, int face, int dir, int h, int orient, int size)
1325 {
1326         int         g, position;
1327
1328         if (dir == TOP || dir == BOTTOM) {
1329                 for (g = 0; g < size; g++) {
1330                         position = g * size + h;
1331                         rp->cubeLoc[face][position] = rp->rowLoc[orient][g];
1332                         /* DrawSquare(face, position); */
1333                 }
1334         } else {                /* dir == RIGHT || dir == LEFT */
1335                 for (g = 0; g < size; g++) {
1336                         position = h * size + g;
1337                         rp->cubeLoc[face][position] = rp->rowLoc[orient][g];
1338                         /* DrawSquare(face, position); */
1339                 }
1340         }
1341 }
1342
1343 static void
1344 rotateFace(rubikstruct * rp, int face, int direction)
1345 {
1346         int         position, i, j;
1347         RubikLoc   *faceLoc = NULL;
1348
1349         if ((faceLoc = (RubikLoc *) malloc(AVSIZESQ * sizeof (RubikLoc))) == NULL)
1350                 (void) fprintf(stderr,
1351                  "Could not allocate memory for rubik face position info\n");
1352         /* Read Face */
1353         for (position = 0; position < AVSIZESQ; position++)
1354                 faceLoc[position] = rp->cubeLoc[face][position];
1355         /* Write Face */
1356         for (position = 0; position < AVSIZESQ; position++) {
1357                 i = position % AVSIZE;
1358                 j = position / AVSIZE;
1359                 rp->cubeLoc[face][position] = (direction == CW) ?
1360                         faceLoc[(AVSIZE - i - 1) * AVSIZE + j] :
1361                         faceLoc[i * AVSIZE + AVSIZE - j - 1];
1362                 rp->cubeLoc[face][position].rotation =
1363                         (rp->cubeLoc[face][position].rotation + direction - MAXORIENT) %
1364                         MAXORIENT;
1365                 /* DrawSquare(face, position); */
1366         }
1367         if (faceLoc != NULL)
1368                 (void) free((void *) faceLoc);
1369 }
1370
1371 static void
1372 moveRubik(rubikstruct * rp, int face, int direction, int position)
1373 {
1374         int         newFace, newDirection, rotate, reverse = False;
1375         int         h, k, newH = 0;
1376         int         i, j;
1377
1378         if (direction == CW || direction == CCW) {
1379                 direction = (direction == CCW) ?
1380                         (rotateToRow[face].direction + 2) % MAXORIENT :
1381                         rotateToRow[face].direction;
1382                 i = j = (rotateToRow[face].sideFace == RIGHT ||
1383                       rotateToRow[face].sideFace == BOTTOM) ? AVSIZE - 1 : 0;
1384                 face = rotateToRow[face].face;
1385                 position = j * AVSIZE + i;
1386         }
1387         i = position % AVSIZE;
1388         j = position / AVSIZE;
1389         h = (direction == TOP || direction == BOTTOM) ? i : j;
1390         /* rotate sides CW or CCW */
1391         if (h == AVSIZE - 1) {
1392                 newDirection = (direction == TOP || direction == BOTTOM) ?
1393                         TOP : RIGHT;
1394                 if (direction == TOP || direction == RIGHT)
1395                         rotateFace(rp, rowToRotate[face][newDirection], CW);
1396                 else            /* direction == BOTTOM || direction == LEFT */
1397                         rotateFace(rp, rowToRotate[face][newDirection], CCW);
1398         }
1399         if (h == 0) {
1400                 newDirection = (direction == TOP || direction == BOTTOM) ?
1401                         BOTTOM : LEFT;
1402                 if (direction == TOP || direction == RIGHT)
1403                         rotateFace(rp, rowToRotate[face][newDirection], CCW);
1404                 else            /* direction == BOTTOM  || direction == LEFT */
1405                         rotateFace(rp, rowToRotate[face][newDirection], CW);
1406         }
1407         /* Slide rows */
1408         readRC(rp, face, direction, h, 0, AVSIZE);
1409         for (k = 1; k <= MAXORIENT; k++) {
1410                 newFace = slideNextRow[face][direction].face;
1411                 rotate = slideNextRow[face][direction].rotation;
1412                 newDirection = (rotate + direction) % MAXORIENT;
1413                 switch (rotate) {
1414                         case TOP:
1415                                 newH = h;
1416                                 reverse = False;
1417                                 break;
1418                         case RIGHT:
1419                                 if (newDirection == TOP || newDirection == BOTTOM) {
1420                                         newH = AVSIZE - 1 - h;
1421                                         reverse = False;
1422                                 } else {        /* newDirection == RIGHT || newDirection == LEFT */
1423                                         newH = h;
1424                                         reverse = True;
1425                                 }
1426                                 break;
1427                         case BOTTOM:
1428                                 newH = AVSIZE - 1 - h;
1429                                 reverse = True;
1430                                 break;
1431                         case LEFT:
1432                                 if (newDirection == TOP || newDirection == BOTTOM) {
1433                                         newH = h;
1434                                         reverse = True;
1435                                 } else {        /* newDirection == RIGHT || newDirection == LEFT */
1436                                         newH = AVSIZE - 1 - h;
1437                                         reverse = False;
1438                                 }
1439                                 break;
1440                         default:
1441                                 (void) printf("moveRubik: rotate %d\n", rotate);
1442                 }
1443                 if (k != MAXORIENT)
1444                         readRC(rp, newFace, newDirection, newH, k, AVSIZE);
1445                 rotateRC(rp, rotate, k - 1, AVSIZE);
1446                 if (reverse == True)
1447                         reverseRC(rp, k - 1, AVSIZE);
1448                 writeRC(rp, newFace, newDirection, newH, k - 1, AVSIZE);
1449                 face = newFace;
1450                 direction = newDirection;
1451                 h = newH;
1452         }
1453 }
1454
1455 #ifdef DEBUG
1456 void
1457 printCube(rubikstruct * rp)
1458 {
1459         int         face, position;
1460
1461         for (face = 0; face < MAXFACES; face++) {
1462                 for (position = 0; position < AVSIZESQ; position++) {
1463                         (void) printf("%d %d  ", rp->cubeLoc[face][position].face,
1464                                       rp->cubeLoc[face][position].rotation);
1465                         if (!((position + 1) % AVSIZE))
1466                                 (void) printf("\n");
1467                 }
1468                 (void) printf("\n");
1469         }
1470         (void) printf("\n");
1471 }
1472
1473 #endif
1474
1475 static void
1476 evalmovement(ModeInfo * mi, RubikMove movement)
1477 {
1478         rubikstruct *rp = &rubik[MI_SCREEN(mi)];
1479
1480 #ifdef DEBUG
1481         printCube(rp);
1482 #endif
1483         if (movement.face < 0 || movement.face >= MAXFACES)
1484                 return;
1485
1486         moveRubik(rp, movement.face, movement.direction, movement.position);
1487
1488 }
1489
1490 static      Bool
1491 compare_moves(rubikstruct * rp, RubikMove move1, RubikMove move2, Bool opp)
1492 {
1493         RubikSlice  slice1, slice2;
1494
1495         slice1 = convertMove(rp, move1);
1496         slice2 = convertMove(rp, move2);
1497         if (slice1.face == slice2.face &&
1498             slice1.depth == slice2.depth) {
1499                 if (slice1.rotation == slice2.rotation) {       /* CW or CCW */
1500                         if (!opp)
1501                                 return True;
1502                 } else {
1503                         if (opp)
1504                                 return True;
1505                 }
1506         }
1507         return False;
1508 }
1509
1510 static void
1511 shuffle(ModeInfo * mi)
1512 {
1513         rubikstruct *rp = &rubik[MI_SCREEN(mi)];
1514         int         i, face, position;
1515         RubikMove   move;
1516
1517         AVSIZE = MI_SIZE(mi);
1518         if (AVSIZE < -MINSIZE)
1519                 AVSIZE = NRAND(-AVSIZE - MINSIZE + 1) + MINSIZE;
1520         else if (AVSIZE < MINSIZE)
1521                 AVSIZE = MINSIZE;
1522         /* Let me waste a little space for the moment */
1523         /* Future cube to be LxMxN and not just NxNxN, but not done yet */
1524         AVSIZESQ = AVSIZE * AVSIZE;
1525 #ifdef LMN
1526         MAXSIZEX = AVSIZE;
1527         MAXSIZEY = AVSIZE;
1528         MAXSIZEZ = AVSIZE;
1529         MAXSIZE = AVSIZE;
1530         MAXSIZESQ = AVSIZESQ;
1531 #endif
1532
1533         for (face = 0; face < MAXFACES; face++) {
1534                 if (rp->cubeLoc[face] != NULL)
1535                         (void) free((void *) rp->cubeLoc[face]);
1536                 if ((rp->cubeLoc[face] =
1537                   (RubikLoc *) malloc(AVSIZESQ * sizeof (RubikLoc))) == NULL)
1538                         (void) fprintf(stderr,
1539                                        "Could not allocate memory for rubik cube position info\n");
1540                 for (position = 0; position < AVSIZESQ; position++) {
1541                         rp->cubeLoc[face][position].face = face;
1542                         rp->cubeLoc[face][position].rotation = TOP;
1543                 }
1544         }
1545         for (i = 0; i < MAXORIENT; i++) {
1546                 if (rp->rowLoc[i] != NULL)
1547                         (void) free((void *) rp->rowLoc[i]);
1548                 if ((rp->rowLoc[i] =
1549                      (RubikLoc *) malloc(AVSIZE * sizeof (RubikLoc))) == NULL)
1550                         (void) fprintf(stderr,
1551                                        "Could not allocate memory for rubik row position info\n");
1552         }
1553         rp->storedmoves = MI_BATCHCOUNT(mi);
1554         if (rp->storedmoves < 0) {
1555                 if (rp->moves != NULL)
1556                         (void) free((void *) rp->moves);
1557                 rp->moves = NULL;
1558                 rp->storedmoves = NRAND(-rp->storedmoves) + 1;
1559         }
1560         if ((rp->storedmoves) && (rp->moves == NULL))
1561                 if ((rp->moves =
1562                      (RubikMove *) calloc(rp->storedmoves + 1, sizeof (RubikMove))) == NULL)
1563                         (void) fprintf(stderr,
1564                         "Could not allocate memory for rubik move buffer\n");
1565
1566         if (MI_CYCLES(mi) <= 1) {
1567                 rp->anglestep = 90.0;
1568         } else {
1569                 rp->anglestep = 90.0 / (GLfloat) (MI_CYCLES(mi));
1570         }
1571
1572         for (i = 0; i < rp->storedmoves; i++) {
1573                 int         condition;
1574
1575                 do {
1576                         move.face = NRAND(6);
1577                         move.direction = NRAND(4);      /* Exclude CW and CCW, its ok */
1578                         /*
1579                          * Randomize position along diagonal, each plane gets an equal chance.
1580                          * This trick will only work for NxNxN cubes
1581                          * draw_cube DEPENDS on that they are chosen this way.
1582                          */
1583                         move.position = NRAND(AVSIZE) * (AVSIZE + 1);
1584
1585
1586                         condition = 1;
1587
1588                         if (i > 0)      /* avoid immediate undoing moves */
1589                                 if (compare_moves(rp, move, rp->moves[i - 1], True))
1590                                         condition = 0;
1591                         if (i > 1)      /* avoid 3 consecutive identical moves */
1592                                 if (compare_moves(rp, move, rp->moves[i - 1], False) &&
1593                                     compare_moves(rp, move, rp->moves[i - 2], False))
1594                                         condition = 0;
1595                         /*
1596                            * Still some silly moves being made....
1597                          */
1598                 } while (!condition);
1599                 if (hideshuffling)
1600                         evalmovement(mi, move);
1601                 rp->moves[i] = move;
1602         }
1603         rp->movement.face = NO_FACE;
1604         rp->rotatestep = 0;
1605         rp->action = hideshuffling ? ACTION_SOLVE : ACTION_SHUFFLE;
1606         rp->shufflingmoves = 0;
1607         rp->done = 0;
1608 }
1609
1610 static void
1611 reshape(ModeInfo * mi, int width, int height)
1612 {
1613         rubikstruct *rp = &rubik[MI_SCREEN(mi)];
1614
1615         glViewport(0, 0, rp->WindW = (GLint) width, rp->WindH = (GLint) height);
1616         glMatrixMode(GL_PROJECTION);
1617         glLoadIdentity();
1618         glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);
1619         glMatrixMode(GL_MODELVIEW);
1620
1621         rp->AreObjectsDefined[ObjCubit] = 0;
1622 }
1623
1624 static void
1625 pinit(ModeInfo * mi)
1626 {
1627         glClearDepth(1.0);
1628         glClearColor(0.0, 0.0, 0.0, 1.0);
1629         glColor3f(1.0, 1.0, 1.0);
1630
1631         glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
1632         glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
1633         glLightfv(GL_LIGHT0, GL_POSITION, position0);
1634         glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
1635         glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
1636         glLightfv(GL_LIGHT1, GL_POSITION, position1);
1637         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
1638         glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
1639         glEnable(GL_LIGHTING);
1640         glEnable(GL_LIGHT0);
1641         glEnable(GL_LIGHT1);
1642         glEnable(GL_DEPTH_TEST);
1643         glEnable(GL_NORMALIZE);
1644         glEnable(GL_CULL_FACE);
1645
1646         glShadeModel(GL_FLAT);
1647         glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
1648         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
1649
1650         shuffle(mi);
1651 }
1652
1653 void
1654 init_rubik(ModeInfo * mi)
1655 {
1656         int         screen = MI_SCREEN(mi);
1657         rubikstruct *rp;
1658
1659         if (rubik == NULL) {
1660                 if ((rubik = (rubikstruct *) calloc(MI_NUM_SCREENS(mi),
1661                                               sizeof (rubikstruct))) == NULL)
1662                         return;
1663         }
1664         rp = &rubik[screen];
1665         rp->step = NRAND(90);
1666
1667         if ((rp->glx_context = init_GL(mi)) != NULL) {
1668
1669                 reshape(mi, MI_WIN_WIDTH(mi), MI_WIN_HEIGHT(mi));
1670                 objects = glGenLists(1);
1671                 pinit(mi);
1672         }
1673 }
1674
1675 void
1676 draw_rubik(ModeInfo * mi)
1677 {
1678         rubikstruct *rp = &rubik[MI_SCREEN(mi)];
1679         Display    *display = MI_DISPLAY(mi);
1680         Window      window = MI_WINDOW(mi);
1681
1682         if (!rp->glx_context)
1683                 return;
1684
1685         glDrawBuffer(GL_BACK);
1686         glXMakeCurrent(display, window, rp->glx_context);
1687
1688         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1689
1690         glPushMatrix();
1691
1692         glTranslatef(0.0, 0.0, -10.0);
1693
1694         if (!MI_WIN_IS_ICONIC(mi)) {
1695                 glScalef(Scale4Window * rp->WindH / rp->WindW, Scale4Window, Scale4Window);
1696         } else {
1697                 glScalef(Scale4Iconic * rp->WindH / rp->WindW, Scale4Iconic, Scale4Iconic);
1698         }
1699
1700         glRotatef(rp->step * 100, 1, 0, 0);
1701         glRotatef(rp->step * 95, 0, 1, 0);
1702         glRotatef(rp->step * 90, 0, 0, 1);
1703
1704         if (rp->action == ACTION_SHUFFLE) {
1705                 if (rp->done) {
1706                         if (++rp->rotatestep > DELAY_AFTER_SHUFFLING) {
1707                                 rp->movement.face = NO_FACE;
1708                                 rp->rotatestep = 0;
1709                                 rp->action = ACTION_SOLVE;
1710                                 rp->done = 0;
1711                         }
1712                 } else {
1713                         if (rp->movement.face == NO_FACE) {
1714                                 if (rp->shufflingmoves < rp->storedmoves) {
1715                                         rp->rotatestep = 0;
1716                                         rp->movement = rp->moves[rp->shufflingmoves];
1717                                 } else {
1718                                         rp->rotatestep = 0;
1719                                         rp->done = 1;
1720                                 }
1721                         } else {
1722                                 rp->rotatestep += rp->anglestep;
1723                                 if (rp->rotatestep > 90) {
1724                                         evalmovement(mi, rp->movement);
1725                                         rp->shufflingmoves++;
1726                                         rp->movement.face = NO_FACE;
1727                                 }
1728                         }
1729                 }
1730         } else {
1731                 if (rp->done) {
1732                         if (++rp->rotatestep > DELAY_AFTER_SOLVING)
1733                                 shuffle(mi);
1734                 } else {
1735                         if (rp->movement.face == NO_FACE) {
1736                                 if (rp->storedmoves > 0) {
1737                                         rp->rotatestep = 0;
1738                                         rp->movement = rp->moves[rp->storedmoves - 1];
1739                                         rp->movement.direction = (rp->movement.direction + (MAXORIENT / 2)) %
1740                                                 MAXORIENT;
1741                                 } else {
1742                                         rp->rotatestep = 0;
1743                                         rp->done = 1;
1744                                 }
1745                         } else {
1746                                 rp->rotatestep += rp->anglestep;
1747                                 if (rp->rotatestep > 90) {
1748                                         evalmovement(mi, rp->movement);
1749                                         rp->storedmoves--;
1750                                         rp->movement.face = NO_FACE;
1751                                 }
1752                         }
1753                 }
1754         }
1755
1756         draw_cube(mi);
1757
1758         glPopMatrix();
1759
1760         glFlush();
1761
1762         glXSwapBuffers(display, window);
1763
1764         rp->step += 0.05;
1765 }
1766
1767 void
1768 change_rubik(ModeInfo * mi)
1769 {
1770         rubikstruct *rp = &rubik[MI_SCREEN(mi)];
1771
1772         if (!rp->glx_context)
1773                 return;
1774         pinit(mi);
1775 }
1776
1777 void
1778 release_rubik(ModeInfo * mi)
1779 {
1780         if (rubik != NULL) {
1781                 int         screen;
1782
1783                 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
1784                         rubikstruct *rp = &rubik[screen];
1785                         int         i;
1786
1787                         for (i = 0; i < MAXFACES; i++)
1788                                 if (rp->cubeLoc[i] != NULL)
1789                                         (void) free((void *) rp->cubeLoc[i]);
1790                         for (i = 0; i < MAXORIENT; i++)
1791                                 if (rp->rowLoc[i] != NULL)
1792                                         (void) free((void *) rp->rowLoc[i]);
1793                         if (rp->moves != NULL)
1794                                 (void) free((void *) rp->moves);
1795                 }
1796                 (void) free((void *) rubik);
1797                 rubik = NULL;
1798         }
1799         FreeAllGL(MI_DISPLAY(mi));
1800 }
1801
1802 #endif