1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* rubik --- Shows an auto-solving Rubik's cube */
5 static const char sccsid[] = "@(#)rubik.c 5.01 2001/03/01 xlockmore";
9 * Permission to use, copy, modify, and distribute this software and its
10 * documentation for any purpose and without fee is hereby granted,
11 * provided that the above copyright notice appear in all copies and that
12 * both that copyright notice and this permission notice appear in
13 * supporting documentation.
15 * This file is provided AS IS with no warranties of any kind. The author
16 * shall have no liability with respect to the infringement of copyrights,
17 * trade secrets or any patents by this file or any part thereof. In no
18 * event will the author be liable for any lost revenue or profits or
19 * other special, indirect and consequential damages.
21 * This mode shows an auto-solving rubik's cube "puzzle". If somebody
22 * intends to make a game or something based on this code, please let me
23 * know first, my e-mail address is provided in this comment. Marcelo.
25 * Thanks goes also to Brian Paul for making it possible and inexpensive
26 * to use OpenGL at home.
28 * Since I'm not a native English speaker, my apologies for any grammatical
31 * My e-mail address is
32 * mfvianna@centroin.com.br
34 * Marcelo F. Vianna (Jul-31-1997)
37 * 05-Apr-2002: Removed all gllist uses (fix some bug with nvidia driver)
38 * 01-Mar-2001: Added FPS stuff - Eric Lassauge <lassauge@mail.dotcom.fr>
39 * 01-Nov-2000: Allocation checks
40 * 27-Apr-1999: LxMxN stuff added.
41 * 26-Sep-1998: Added some more movement (the cube does not stay in the screen
42 * center anymore. Also fixed the scale problem immediately after
43 * shuffling when the puzzle is solved.
44 * 08-Aug-1997: Now has some internals from xrubik by David Bagley
45 * This should make it easier to add features.
46 * 02-Aug-1997: Now behaves more like puzzle.c: first show the cube being
47 * shuffled and then being solved. A mode specific option was
49 * "+/-hideshuffling" to provide the original behavior (in which
50 * only the solution is shown).
51 * The color labels corners are now rounded.
52 * Optimized the cubit() routine using glLists.
53 * 01-Aug-1997: Shuffling now avoids movements that undoes the previous
54 * movement and three consecutive identical moves (which is
56 * improved the "cycles" option in replacement of David's hack,
57 * now rp->anglestep is a GLfloat, so this option selects the
58 * "exact" number of frames that a rotation (movement) takes to
60 * 30-Jul-1997: Initial release, there is no algorithm to solve the puzzle,
61 * instead, it randomly shuffle the cube and then make the
62 * movements in the reverse order.
63 * The mode was written in 1 day (I got sick and had the day off).
64 * There was not much to do since I could not leave home... :)
68 * Color labels mapping:
69 * =====================
77 * +-----------+-----------+-----------+
80 * |v LEFT(1) |v FRONT(2) |v RIGHT(3) |
83 * +-----------+-----------+-----------+
89 * +-----------+ +---+---+---+
90 * |0--> | | 0 | 1 | 2 |
92 * |v BACK(5) | | 3 | 4 | 5 |
95 * +-----------+ +---+---+---+
108 # define DEFAULTS "*delay: 40000 \n" \
110 "*showFPS: False \n" \
113 # define refresh_rubik 0
114 # define rubik_handle_event 0
115 # include "xlockmore.h" /* from the xscreensaver distribution */
116 #else /* !STANDALONE */
117 # include "xlock.h" /* from the xlockmore distribution */
119 #endif /* !STANDALONE */
123 #define DEF_SIZEX "0"
124 #define DEF_SIZEY "0"
125 #define DEF_SIZEZ "0"
126 #define DEF_HIDESHUFFLING "False"
131 static Bool hideshuffling;
133 static XrmOptionDescRec opts[] =
135 {"-sizex", ".rubik.sizex", XrmoptionSepArg, 0},
136 {"-sizey", ".rubik.sizey", XrmoptionSepArg, 0},
137 {"-sizez", ".rubik.sizez", XrmoptionSepArg, 0},
138 {"-hideshuffling", ".rubik.hideshuffling", XrmoptionNoArg, "on"},
139 {"+hideshuffling", ".rubik.hideshuffling", XrmoptionNoArg, "off"}
142 static argtype vars[] =
144 {&sizex, "sizex", "SizeX", DEF_SIZEX, t_Int},
145 {&sizey, "sizey", "SizeY", DEF_SIZEY, t_Int},
146 {&sizez, "sizez", "SizeZ", DEF_SIZEZ, t_Int},
147 {&hideshuffling, "hideshuffling", "Hideshuffling", DEF_HIDESHUFFLING, t_Bool}
150 static OptionStruct desc[] =
152 {"-sizex num", "number of cubies along x axis (overrides size)"},
153 {"-sizey num", "number of cubies along y axis (overrides size)"},
154 {"-sizez num", "number of cubies along z axis (overrides size)"},
155 {"-/+hideshuffling", "turn on/off hidden shuffle phase"}
158 ENTRYPOINT ModeSpecOpt rubik_opts =
159 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
162 ModStruct rubik_description =
163 {"rubik", "init_rubik", "draw_rubik", "release_rubik",
164 "draw_rubik", "change_rubik", (char *) NULL, &rubik_opts,
165 10000, -30, 5, -6, 64, 1.0, "",
166 "Shows an auto-solving Rubik's Cube", 0, NULL};
170 #define VectMul(X1,Y1,Z1,X2,Y2,Z2) (Y1)*(Z2)-(Z1)*(Y2),(Z1)*(X2)-(X1)*(Z2),(X1)*(Y2)-(Y1)*(X2)
171 #define sqr(A) ((A)*(A))
178 #define ACTION_SOLVE 1
179 #define ACTION_SHUFFLE 0
181 #define DELAY_AFTER_SHUFFLING 5
182 #define DELAY_AFTER_SOLVING 20
184 /*************************************************************************/
187 #define MAXSIZEX (rp->sizex)
188 #define MAXSIZEY (rp->sizey)
189 #define MAXSIZEZ (rp->sizez)
190 #define AVSIZE ((rp->sizex+rp->sizey+rp->sizez)/3.0) /* Use of this should be minimized */
191 #define MAXMAXSIZE (MAX(MAXSIZEX,MAX(MAXSIZEY,MAXSIZEZ)))
192 #define MAXSIZEXY (MAXSIZEX*MAXSIZEY)
193 #define MAXSIZEYZ (MAXSIZEY*MAXSIZEZ)
194 #define MAXSIZEZX (MAXSIZEZ*MAXSIZEX)
195 #define LASTX (MAXSIZEX-1)
196 #define LASTY (MAXSIZEY-1)
197 #define LASTZ (MAXSIZEZ-1)
198 /* These are not likely to change but... */
203 #define Scale4Window (0.9/AVSIZE)
204 #define Scale4Iconic (2.1/AVSIZE)
206 #define MAXORIENT 4 /* Number of orientations of a square */
207 #define MAXFACES 6 /* Number of faces */
209 /* Directions relative to the face of a cubie */
214 #define CW (MAXORIENT+1)
215 #define HALF (MAXORIENT+2)
216 #define CCW (2*MAXORIENT-1)
222 #define BOTTOM_FACE 4
224 #define NO_FACE (MAXFACES)
225 #define NO_ROTATION (2*MAXORIENT)
226 #define NO_DEPTH MAXMAXSIZE
228 #define REVX(a) (MAXSIZEX - a - 1)
229 #define REVY(a) (MAXSIZEY - a - 1)
230 #define REVZ(a) (MAXSIZEZ - a - 1)
233 #define CUBEROUND (CUBELEN-0.05)
234 #define STICKERLONG (CUBEROUND-0.05)
235 #define STICKERSHORT (STICKERLONG-0.05)
236 #define STICKERDEPTH (CUBELEN+0.01)
240 typedef struct _RubikLoc {
242 int rotation; /* Not used yet */
245 typedef struct _RubikRowNext {
246 int face, direction, sideFace;
249 typedef struct _RubikMove {
254 typedef struct _RubikSlice {
260 * Pick a face and a direction on face the next face and orientation
263 static const RubikLoc slideNextRow[MAXFACES][MAXORIENT] =
298 * Examine cubie 0 on each face, its 4 movements (well only 2 since the
299 * other 2 will be opposites) and translate it into slice movements).
300 * CW = DEEP Depth CCW == SHALLOW Depth with reference to faces 0, 1, and 2
302 static const RubikLoc rotateSlice[MAXFACES][MAXORIENT / 2] =
331 * Rotate face clockwise by a number of orients, then the top of the
332 * face then points to this face
334 static const int rowToRotate[MAXFACES][MAXORIENT] =
345 * This translates a clockwise move to something more manageable
347 static const RubikRowNext rotateToRow[MAXFACES] = /*CW to min face */
364 int sizex, sizey, sizez;
365 float avsize, avsizeSq;
369 RubikLoc *cubeLoc[MAXFACES];
370 RubikLoc *rowLoc[MAXORIENT];
373 GLfloat PX, PY, VX, VY;
374 GLXContext *glx_context;
377 static const float front_shininess[] = {60.0};
378 static const float front_specular[] = {0.7, 0.7, 0.7, 1.0};
379 static const float ambient[] = {0.0, 0.0, 0.0, 1.0};
380 static const float diffuse[] = {1.0, 1.0, 1.0, 1.0};
381 static const float position0[] = {1.0, 1.0, 1.0, 0.0};
382 static const float position1[] = {-1.0, -1.0, 1.0, 0.0};
383 static const float lmodel_ambient[] = {0.5, 0.5, 0.5, 1.0};
384 static const float lmodel_twoside[] = {GL_TRUE};
386 static const float MaterialRed[] = {0.5, 0.0, 0.0, 1.0};
387 static const float MaterialGreen[] = {0.0, 0.5, 0.0, 1.0};
388 static const float MaterialBlue[] = {0.0, 0.0, 0.5, 1.0};
389 static const float MaterialYellow[] = {0.7, 0.7, 0.0, 1.0};
390 static const float MaterialOrange[] = {0.9, 0.45, 0.36, 1.0};
393 static float MaterialMagenta[] = {0.7, 0.0, 0.7, 1.0};
394 static float MaterialCyan[] = {0.0, 0.7, 0.7, 1.0};
397 static const float MaterialWhite[] = {0.8, 0.8, 0.8, 1.0};
398 static const float MaterialGray[] = {0.2, 0.2, 0.2, 1.0};
399 static const float MaterialGray3[] = {0.3, 0.3, 0.3, 1.0};
400 static const float MaterialGray4[] = {0.4, 0.4, 0.4, 1.0};
401 static const float MaterialGray5[] = {0.5, 0.5, 0.5, 1.0};
402 static const float MaterialGray6[] = {0.6, 0.6, 0.6, 1.0};
403 static const float MaterialGray7[] = {0.7, 0.7, 0.7, 1.0};
405 static rubikstruct *rubik = (rubikstruct *) NULL;
409 pickcolor(int C, int mono)
414 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray3);
416 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialRed);
420 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray6);
422 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialYellow);
425 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialWhite);
429 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray4);
431 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGreen);
435 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray7);
437 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialOrange);
441 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray5);
443 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialBlue);
446 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialCyan);
447 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialMagenta);
453 faceSizes(rubikstruct * rp, int face, int * sizeOfRow, int * sizeOfColumn)
458 *sizeOfRow = MAXSIZEX;
459 *sizeOfColumn = MAXSIZEZ;
463 *sizeOfRow = MAXSIZEZ;
464 *sizeOfColumn = MAXSIZEY;
468 *sizeOfRow = MAXSIZEX;
469 *sizeOfColumn = MAXSIZEY;
475 checkFaceSquare(rubikstruct * rp, int face)
477 int sizeOfRow, sizeOfColumn;
479 faceSizes(rp, face, &sizeOfRow, &sizeOfColumn);
480 return (sizeOfRow == sizeOfColumn);
481 /* Cubes can be made square with a 4x2 face where 90 degree turns
482 * should be permitted but that is kind of complicated for me.
483 * This can be done in 2 ways where the side of the cubies are
484 * the same size and one where one side (the side with half the
485 * number of cubies) is twice the size of the other. The first is
486 * complicated because faces of cubies can go under other faces.
487 * The second way is similar to "banded cubes" where scotch tape
488 * restricts the moves of some cubes. Here you have to keep track
489 * of the restrictions and show banded cubies graphically as one
495 sizeFace(rubikstruct * rp, int face)
497 int sizeOfRow, sizeOfColumn;
499 faceSizes(rp, face, &sizeOfRow, &sizeOfColumn);
500 return (sizeOfRow * sizeOfColumn);
504 sizeRow(rubikstruct * rp, int face)
506 int sizeOfRow, sizeOfColumn; /* sizeOfColumn not used */
508 faceSizes(rp, face, &sizeOfRow, &sizeOfColumn);
513 draw_stickerless_cubit(rubikstruct *rp)
516 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray);
517 /* Put sticker here */
518 glNormal3f(0.00, 0.00, 1.00);
519 glVertex3f(-CUBEROUND, -CUBEROUND, CUBELEN);
520 glVertex3f(CUBEROUND, -CUBEROUND, CUBELEN);
521 glVertex3f(CUBEROUND, CUBEROUND, CUBELEN);
522 glVertex3f(-CUBEROUND, CUBEROUND, CUBELEN);
523 glNormal3f(0.00, 0.00, -1.00);
524 glVertex3f(-CUBEROUND, CUBEROUND, -CUBELEN);
525 glVertex3f(CUBEROUND, CUBEROUND, -CUBELEN);
526 glVertex3f(CUBEROUND, -CUBEROUND, -CUBELEN);
527 glVertex3f(-CUBEROUND, -CUBEROUND, -CUBELEN);
528 glNormal3f(-1.00, 0.00, 0.00);
529 glVertex3f(-CUBELEN, -CUBEROUND, CUBEROUND);
530 glVertex3f(-CUBELEN, CUBEROUND, CUBEROUND);
531 glVertex3f(-CUBELEN, CUBEROUND, -CUBEROUND);
532 glVertex3f(-CUBELEN, -CUBEROUND, -CUBEROUND);
533 glNormal3f(1.00, 0.00, 0.00);
534 glVertex3f(CUBELEN, -CUBEROUND, -CUBEROUND);
535 glVertex3f(CUBELEN, CUBEROUND, -CUBEROUND);
536 glVertex3f(CUBELEN, CUBEROUND, CUBEROUND);
537 glVertex3f(CUBELEN, -CUBEROUND, CUBEROUND);
538 glNormal3f(0.00, -1.00, 0.00);
539 glVertex3f(CUBEROUND, -CUBELEN, -CUBEROUND);
540 glVertex3f(CUBEROUND, -CUBELEN, CUBEROUND);
541 glVertex3f(-CUBEROUND, -CUBELEN, CUBEROUND);
542 glVertex3f(-CUBEROUND, -CUBELEN, -CUBEROUND);
543 glNormal3f(0.00, 1.00, 0.00);
544 glVertex3f(-CUBEROUND, CUBELEN, -CUBEROUND);
545 glVertex3f(-CUBEROUND, CUBELEN, CUBEROUND);
546 glVertex3f(CUBEROUND, CUBELEN, CUBEROUND);
547 glVertex3f(CUBEROUND, CUBELEN, -CUBEROUND);
550 glNormal3f(-1.00, -1.00, 0.00);
551 glVertex3f(-CUBEROUND, -CUBELEN, -CUBEROUND);
552 glVertex3f(-CUBEROUND, -CUBELEN, CUBEROUND);
553 glVertex3f(-CUBELEN, -CUBEROUND, CUBEROUND);
554 glVertex3f(-CUBELEN, -CUBEROUND, -CUBEROUND);
555 glNormal3f(1.00, 1.00, 0.00);
556 glVertex3f(CUBEROUND, CUBELEN, -CUBEROUND);
557 glVertex3f(CUBEROUND, CUBELEN, CUBEROUND);
558 glVertex3f(CUBELEN, CUBEROUND, CUBEROUND);
559 glVertex3f(CUBELEN, CUBEROUND, -CUBEROUND);
560 glNormal3f(-1.00, 1.00, 0.00);
561 glVertex3f(-CUBELEN, CUBEROUND, -CUBEROUND);
562 glVertex3f(-CUBELEN, CUBEROUND, CUBEROUND);
563 glVertex3f(-CUBEROUND, CUBELEN, CUBEROUND);
564 glVertex3f(-CUBEROUND, CUBELEN, -CUBEROUND);
565 glNormal3f(1.00, -1.00, 0.00);
566 glVertex3f(CUBELEN, -CUBEROUND, -CUBEROUND);
567 glVertex3f(CUBELEN, -CUBEROUND, CUBEROUND);
568 glVertex3f(CUBEROUND, -CUBELEN, CUBEROUND);
569 glVertex3f(CUBEROUND, -CUBELEN, -CUBEROUND);
570 glNormal3f(0.00, -1.00, -1.00);
571 glVertex3f(-CUBEROUND, -CUBEROUND, -CUBELEN);
572 glVertex3f(CUBEROUND, -CUBEROUND, -CUBELEN);
573 glVertex3f(CUBEROUND, -CUBELEN, -CUBEROUND);
574 glVertex3f(-CUBEROUND, -CUBELEN, -CUBEROUND);
575 glNormal3f(0.00, 1.00, 1.00);
576 glVertex3f(-CUBEROUND, CUBEROUND, CUBELEN);
577 glVertex3f(CUBEROUND, CUBEROUND, CUBELEN);
578 glVertex3f(CUBEROUND, CUBELEN, CUBEROUND);
579 glVertex3f(-CUBEROUND, CUBELEN, CUBEROUND);
580 glNormal3f(0.00, -1.00, 1.00);
581 glVertex3f(-CUBEROUND, -CUBELEN, CUBEROUND);
582 glVertex3f(CUBEROUND, -CUBELEN, CUBEROUND);
583 glVertex3f(CUBEROUND, -CUBEROUND, CUBELEN);
584 glVertex3f(-CUBEROUND, -CUBEROUND, CUBELEN);
585 glNormal3f(0.00, 1.00, -1.00);
586 glVertex3f(-CUBEROUND, CUBELEN, -CUBEROUND);
587 glVertex3f(CUBEROUND, CUBELEN, -CUBEROUND);
588 glVertex3f(CUBEROUND, CUBEROUND, -CUBELEN);
589 glVertex3f(-CUBEROUND, CUBEROUND, -CUBELEN);
590 glNormal3f(-1.00, 0.00, -1.00);
591 glVertex3f(-CUBELEN, -CUBEROUND, -CUBEROUND);
592 glVertex3f(-CUBELEN, CUBEROUND, -CUBEROUND);
593 glVertex3f(-CUBEROUND, CUBEROUND, -CUBELEN);
594 glVertex3f(-CUBEROUND, -CUBEROUND, -CUBELEN);
595 glNormal3f(1.00, 0.00, 1.00);
596 glVertex3f(CUBELEN, -CUBEROUND, CUBEROUND);
597 glVertex3f(CUBELEN, CUBEROUND, CUBEROUND);
598 glVertex3f(CUBEROUND, CUBEROUND, CUBELEN);
599 glVertex3f(CUBEROUND, -CUBEROUND, CUBELEN);
600 glNormal3f(1.00, 0.00, -1.00);
601 glVertex3f(CUBEROUND, -CUBEROUND, -CUBELEN);
602 glVertex3f(CUBEROUND, CUBEROUND, -CUBELEN);
603 glVertex3f(CUBELEN, CUBEROUND, -CUBEROUND);
604 glVertex3f(CUBELEN, -CUBEROUND, -CUBEROUND);
605 glNormal3f(-1.00, 0.00, 1.00);
606 glVertex3f(-CUBEROUND, -CUBEROUND, CUBELEN);
607 glVertex3f(-CUBEROUND, CUBEROUND, CUBELEN);
608 glVertex3f(-CUBELEN, CUBEROUND, CUBEROUND);
609 glVertex3f(-CUBELEN, -CUBEROUND, CUBEROUND);
611 glBegin(GL_TRIANGLES);
612 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray);
613 /* Corners of cubit */
614 glNormal3f(1.00, 1.00, 1.00);
615 glVertex3f(CUBEROUND, CUBEROUND, CUBELEN);
616 glVertex3f(CUBELEN, CUBEROUND, CUBEROUND);
617 glVertex3f(CUBEROUND, CUBELEN, CUBEROUND);
618 glNormal3f(-1.00, -1.00, -1.00);
619 glVertex3f(-CUBEROUND, -CUBELEN, -CUBEROUND);
620 glVertex3f(-CUBELEN, -CUBEROUND, -CUBEROUND);
621 glVertex3f(-CUBEROUND, -CUBEROUND, -CUBELEN);
622 glNormal3f(-1.00, 1.00, 1.00);
623 glVertex3f(-CUBEROUND, CUBEROUND, CUBELEN);
624 glVertex3f(-CUBEROUND, CUBELEN, CUBEROUND);
625 glVertex3f(-CUBELEN, CUBEROUND, CUBEROUND);
626 glNormal3f(1.00, -1.00, -1.00);
627 glVertex3f(CUBELEN, -CUBEROUND, -CUBEROUND);
628 glVertex3f(CUBEROUND, -CUBELEN, -CUBEROUND);
629 glVertex3f(CUBEROUND, -CUBEROUND, -CUBELEN);
630 glNormal3f(1.00, -1.00, 1.00);
631 glVertex3f(CUBEROUND, -CUBEROUND, CUBELEN);
632 glVertex3f(CUBEROUND, -CUBELEN, CUBEROUND);
633 glVertex3f(CUBELEN, -CUBEROUND, CUBEROUND);
634 glNormal3f(-1.00, 1.00, -1.00);
635 glVertex3f(-CUBELEN, CUBEROUND, -CUBEROUND);
636 glVertex3f(-CUBEROUND, CUBELEN, -CUBEROUND);
637 glVertex3f(-CUBEROUND, CUBEROUND, -CUBELEN);
638 glNormal3f(-1.00, -1.00, 1.00);
639 glVertex3f(-CUBEROUND, -CUBEROUND, CUBELEN);
640 glVertex3f(-CUBELEN, -CUBEROUND, CUBEROUND);
641 glVertex3f(-CUBEROUND, -CUBELEN, CUBEROUND);
642 glNormal3f(1.00, 1.00, -1.00);
643 glVertex3f(CUBELEN, CUBEROUND, -CUBEROUND);
644 glVertex3f(CUBEROUND, CUBEROUND, -CUBELEN);
645 glVertex3f(CUBEROUND, CUBELEN, -CUBEROUND);
651 draw_cubit(ModeInfo * mi,
652 int back, int front, int left, int right, int bottom, int top)
654 rubikstruct *rp = &rubik[MI_SCREEN(mi)];
655 int mono = MI_IS_MONO(mi);
657 if (!draw_stickerless_cubit(rp))
659 if (back != NO_FACE) {
661 pickcolor(back, mono);
662 glNormal3f(0.00, 0.00, -1.00);
663 glVertex3f(-STICKERSHORT, STICKERLONG, -STICKERDEPTH);
664 glVertex3f(STICKERSHORT, STICKERLONG, -STICKERDEPTH);
665 glVertex3f(STICKERLONG, STICKERSHORT, -STICKERDEPTH);
666 glVertex3f(STICKERLONG, -STICKERSHORT, -STICKERDEPTH);
667 glVertex3f(STICKERSHORT, -STICKERLONG, -STICKERDEPTH);
668 glVertex3f(-STICKERSHORT, -STICKERLONG, -STICKERDEPTH);
669 glVertex3f(-STICKERLONG, -STICKERSHORT, -STICKERDEPTH);
670 glVertex3f(-STICKERLONG, STICKERSHORT, -STICKERDEPTH);
673 if (front != NO_FACE) {
675 pickcolor(front, mono);
676 glNormal3f(0.00, 0.00, 1.00);
677 glVertex3f(-STICKERSHORT, -STICKERLONG, STICKERDEPTH);
678 glVertex3f(STICKERSHORT, -STICKERLONG, STICKERDEPTH);
679 glVertex3f(STICKERLONG, -STICKERSHORT, STICKERDEPTH);
680 glVertex3f(STICKERLONG, STICKERSHORT, STICKERDEPTH);
681 glVertex3f(STICKERSHORT, STICKERLONG, STICKERDEPTH);
682 glVertex3f(-STICKERSHORT, STICKERLONG, STICKERDEPTH);
683 glVertex3f(-STICKERLONG, STICKERSHORT, STICKERDEPTH);
684 glVertex3f(-STICKERLONG, -STICKERSHORT, STICKERDEPTH);
687 if (left != NO_FACE) {
689 pickcolor(left, mono);
690 glNormal3f(-1.00, 0.00, 0.00);
691 glVertex3f(-STICKERDEPTH, -STICKERSHORT, STICKERLONG);
692 glVertex3f(-STICKERDEPTH, STICKERSHORT, STICKERLONG);
693 glVertex3f(-STICKERDEPTH, STICKERLONG, STICKERSHORT);
694 glVertex3f(-STICKERDEPTH, STICKERLONG, -STICKERSHORT);
695 glVertex3f(-STICKERDEPTH, STICKERSHORT, -STICKERLONG);
696 glVertex3f(-STICKERDEPTH, -STICKERSHORT, -STICKERLONG);
697 glVertex3f(-STICKERDEPTH, -STICKERLONG, -STICKERSHORT);
698 glVertex3f(-STICKERDEPTH, -STICKERLONG, STICKERSHORT);
701 if (right != NO_FACE) {
703 pickcolor(right, mono);
704 glNormal3f(1.00, 0.00, 0.00);
705 glVertex3f(STICKERDEPTH, -STICKERSHORT, -STICKERLONG);
706 glVertex3f(STICKERDEPTH, STICKERSHORT, -STICKERLONG);
707 glVertex3f(STICKERDEPTH, STICKERLONG, -STICKERSHORT);
708 glVertex3f(STICKERDEPTH, STICKERLONG, STICKERSHORT);
709 glVertex3f(STICKERDEPTH, STICKERSHORT, STICKERLONG);
710 glVertex3f(STICKERDEPTH, -STICKERSHORT, STICKERLONG);
711 glVertex3f(STICKERDEPTH, -STICKERLONG, STICKERSHORT);
712 glVertex3f(STICKERDEPTH, -STICKERLONG, -STICKERSHORT);
715 if (bottom != NO_FACE) {
717 pickcolor(bottom, mono);
718 glNormal3f(0.00, -1.00, 0.00);
719 glVertex3f(STICKERLONG, -STICKERDEPTH, -STICKERSHORT);
720 glVertex3f(STICKERLONG, -STICKERDEPTH, STICKERSHORT);
721 glVertex3f(STICKERSHORT, -STICKERDEPTH, STICKERLONG);
722 glVertex3f(-STICKERSHORT, -STICKERDEPTH, STICKERLONG);
723 glVertex3f(-STICKERLONG, -STICKERDEPTH, STICKERSHORT);
724 glVertex3f(-STICKERLONG, -STICKERDEPTH, -STICKERSHORT);
725 glVertex3f(-STICKERSHORT, -STICKERDEPTH, -STICKERLONG);
726 glVertex3f(STICKERSHORT, -STICKERDEPTH, -STICKERLONG);
729 if (top != NO_FACE) {
731 pickcolor(top, mono);
732 glNormal3f(0.00, 1.00, 0.00);
733 glVertex3f(-STICKERLONG, STICKERDEPTH, -STICKERSHORT);
734 glVertex3f(-STICKERLONG, STICKERDEPTH, STICKERSHORT);
735 glVertex3f(-STICKERSHORT, STICKERDEPTH, STICKERLONG);
736 glVertex3f(STICKERSHORT, STICKERDEPTH, STICKERLONG);
737 glVertex3f(STICKERLONG, STICKERDEPTH, STICKERSHORT);
738 glVertex3f(STICKERLONG, STICKERDEPTH, -STICKERSHORT);
739 glVertex3f(STICKERSHORT, STICKERDEPTH, -STICKERLONG);
740 glVertex3f(-STICKERSHORT, STICKERDEPTH, -STICKERLONG);
746 /* Convert move to weird general notation */
748 convertMove(rubikstruct * rp, RubikMove move, RubikSlice * slice)
751 int sizeOfRow, sizeOfColumn;
753 plane = rotateSlice[(int) move.face][move.direction % 2];
754 (*slice).face = plane.face;
755 (*slice).rotation = plane.rotation;
757 faceSizes(rp, move.face, &sizeOfRow, &sizeOfColumn);
758 if (plane.face == 1 || /* VERTICAL */
759 (plane.face == 2 && (move.face == 1 || move.face == 3))) {
760 if ((*slice).rotation == CW)
761 (*slice).depth = sizeOfRow - 1 - move.position %
764 (*slice).depth = move.position % sizeOfRow;
765 } else { /* (plane.face == 0 || *//* HORIZONTAL *//*
766 (plane.face == 2 && (move.face == 0 || move.face == 4))) */
767 if ((*slice).rotation == CW)
768 (*slice).depth = sizeOfColumn - 1 - move.position /
771 (*slice).depth = move.position / sizeOfRow;
773 /* If (*slice).depth = 0 then face 0, face 1, or face 2 moves */
774 if (move.direction / 2)
775 (*slice).rotation = ((*slice).rotation == CW) ? CCW : CW;
778 /* Assume the size is at least 2, or its just not challenging... */
780 draw_cube(ModeInfo * mi)
783 #define SX ((GLint)S1*(MAXSIZEX-1))
784 #define SY ((GLint)S1*(MAXSIZEY-1))
785 #define SZ ((GLint)S1*(MAXSIZEZ-1))
786 #define HALFX (((GLfloat)MAXSIZEX-1.0)/2.0)
787 #define HALFY (((GLfloat)MAXSIZEY-1.0)/2.0)
788 #define HALFZ (((GLfloat)MAXSIZEZ-1.0)/2.0)
789 #define MIDX(a) (((GLfloat)(2*a-MAXSIZEX+1))/2.0)
790 #define MIDY(a) (((GLfloat)(2*a-MAXSIZEY+1))/2.0)
791 #define MIDZ(a) (((GLfloat)(2*a-MAXSIZEZ+1))/2.0)
792 #define DRAW_CUBIT(mi,b,f,l,r,bm,t) if (!draw_cubit(mi,b,f,l,r,bm,t)) return False
793 rubikstruct *rp = &rubik[MI_SCREEN(mi)];
798 if (rp->movement.face == NO_FACE) {
799 slice.face = NO_FACE;
800 slice.rotation = NO_ROTATION;
801 slice.depth = NO_DEPTH;
803 convertMove(rp, rp->movement, &slice);
805 rotatestep = (slice.rotation == CCW) ? rp->rotatestep : -rp->rotatestep;
809 * The glRotatef() routine transforms the coordinate system for every future
810 * vertex specification (this is not so simple, but by now comprehending this
811 * is sufficient). So if you want to rotate the inner slice, you can draw
812 * one slice, rotate the anglestep for the centerslice, draw the inner slice,
813 * rotate reversely and draw the other slice.
814 * There is a sequence for drawing cubies for each axis being moved...
816 switch (slice.face) {
818 case TOP_FACE: /* BOTTOM_FACE too */
820 if (slice.depth == MAXSIZEY - 1)
821 glRotatef(rotatestep, 0, HALFY, 0);
823 glTranslatef(-HALFX, -HALFY, -HALFZ);
825 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * FIRSTY].face, NO_FACE,
826 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face, NO_FACE,
827 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * LASTZ].face, NO_FACE);
828 for (k = 1; k < MAXSIZEZ - 1; k++) {
829 glTranslatef(0, 0, S1);
832 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * LASTY].face, NO_FACE,
833 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
835 glTranslatef(0, 0, S1);
837 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * LASTY].face,
838 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * LASTY].face, NO_FACE,
839 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
840 for (i = 1; i < MAXSIZEX - 1; i++) {
841 glTranslatef(S1, 0, -SZ);
843 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * FIRSTY].face, NO_FACE,
845 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * LASTZ].face, NO_FACE);
846 for (k = 1; k < MAXSIZEZ - 1; k++) {
847 glTranslatef(0, 0, S1);
851 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * REVZ(k)].face, NO_FACE);
853 glTranslatef(0, 0, S1);
855 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * LASTY].face,
857 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * FIRSTZ].face, NO_FACE);
859 glTranslatef(S1, 0, -SZ);
861 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * FIRSTY].face, NO_FACE,
862 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * LASTY].face,
863 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * LASTZ].face, NO_FACE);
864 for (k = 1; k < MAXSIZEZ - 1; k++) {
865 glTranslatef(0, 0, S1);
868 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * LASTY].face,
869 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
871 glTranslatef(0, 0, S1);
873 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * LASTY].face,
874 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face,
875 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
877 for (j = 1; j < MAXSIZEY - 1; j++) {
879 if (slice.depth == REVY(j))
880 glRotatef(rotatestep, 0, HALFY, 0);
881 glTranslatef(-HALFX, MIDY(j), -HALFZ);
883 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * j].face, NO_FACE,
884 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
886 for (k = 1; k < MAXSIZEZ - 1; k++) {
887 glTranslatef(0, 0, S1);
890 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * REVY(j)].face, NO_FACE,
893 glTranslatef(0, 0, S1);
895 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * REVY(j)].face,
896 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
898 for (i = 1; i < MAXSIZEX - 1; i++) {
899 glTranslatef(1, 0, -SZ);
901 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * j].face, NO_FACE,
905 glTranslatef(0, 0, SZ);
907 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * REVY(j)].face,
911 glTranslatef(S1, 0, -SZ);
913 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * j].face, NO_FACE,
914 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face,
916 for (k = 1; k < MAXSIZEZ - 1; k++) {
917 glTranslatef(0, 0, S1);
920 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * REVY(j)].face,
923 glTranslatef(0, 0, S1);
925 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * REVY(j)].face,
926 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face,
930 if (slice.depth == 0)
931 glRotatef(rotatestep, 0, HALFY, 0);
933 glTranslatef(-HALFX, HALFY, -HALFZ);
935 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * LASTY].face, NO_FACE,
936 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
937 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face);
938 for (k = 1; k < MAXSIZEZ - 1; k++) {
939 glTranslatef(0, 0, S1);
942 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * FIRSTY].face, NO_FACE,
943 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * k].face);
945 glTranslatef(0, 0, S1);
947 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * FIRSTY].face,
948 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
949 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * LASTZ].face);
950 for (i = 1; i < MAXSIZEX - 1; i++) {
951 glTranslatef(S1, 0, -SZ);
953 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * LASTY].face, NO_FACE,
955 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * FIRSTZ].face);
956 for (k = 1; k < MAXSIZEZ - 1; k++) {
957 glTranslatef(0, 0, S1);
961 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * k].face);
963 glTranslatef(0, 0, S1);
965 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * FIRSTY].face,
967 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * LASTZ].face);
969 glTranslatef(S1, 0, -SZ);
971 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * LASTY].face, NO_FACE,
972 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face,
973 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * FIRSTZ].face);
974 for (k = 1; k < MAXSIZEZ - 1; k++) {
975 glTranslatef(0, 0, S1);
978 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * FIRSTY].face,
979 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * k].face);
981 glTranslatef(0, 0, S1);
983 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * FIRSTY].face,
984 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face,
985 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * LASTZ].face);
987 case LEFT_FACE: /* RIGHT_FACE too */
988 /* rotatestep is negative because the RIGHT face is the default here */
990 if (slice.depth == 0)
991 glRotatef(-rotatestep, HALFX, 0, 0);
993 glTranslatef(-HALFX, -HALFY, -HALFZ);
995 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * FIRSTY].face, NO_FACE,
996 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face, NO_FACE,
997 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * LASTZ].face, NO_FACE);
998 for (j = 1; j < MAXSIZEY - 1; j++) {
999 glTranslatef(0, S1, 0);
1001 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * j].face, NO_FACE,
1002 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
1005 glTranslatef(0, S1, 0);
1007 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * LASTY].face, NO_FACE,
1008 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
1009 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face);
1010 for (k = 1; k < MAXSIZEZ - 1; k++) {
1011 glTranslatef(0, -SY, S1);
1014 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * LASTY].face, NO_FACE,
1015 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
1016 for (j = 1; j < MAXSIZEY - 1; j++) {
1017 glTranslatef(0, S1, 0);
1020 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * REVY(j)].face, NO_FACE,
1023 glTranslatef(0, S1, 0);
1026 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * FIRSTY].face, NO_FACE,
1027 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * k].face);
1029 glTranslatef(0, -SY, S1);
1031 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * LASTY].face,
1032 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * LASTY].face, NO_FACE,
1033 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
1034 for (j = 1; j < MAXSIZEY - 1; j++) {
1035 glTranslatef(0, S1, 0);
1037 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * REVY(j)].face,
1038 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
1041 glTranslatef(0, S1, 0);
1043 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * FIRSTY].face,
1044 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
1045 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * LASTZ].face);
1047 for (i = 1; i < MAXSIZEX - 1; i++) {
1049 if (slice.depth == i)
1050 glRotatef(-rotatestep, HALFX, 0, 0);
1051 glTranslatef(MIDX(i), -HALFY, -HALFZ);
1053 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * FIRSTY].face, NO_FACE,
1055 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * LASTZ].face, NO_FACE);
1056 for (j = 1; j < MAXSIZEY - 1; j++) {
1057 glTranslatef(0, S1, 0);
1059 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * j].face, NO_FACE,
1063 glTranslatef(0, S1, 0);
1065 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * LASTY].face, NO_FACE,
1067 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * FIRSTZ].face);
1068 for (k = 1; k < MAXSIZEZ - 1; k++) {
1069 glTranslatef(0, -SY, S1);
1073 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * REVZ(k)].face, NO_FACE);
1075 glTranslatef(0, SY, 0);
1079 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * k].face);
1081 glTranslatef(0, -SY, S1);
1083 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * LASTY].face,
1085 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * FIRSTZ].face, NO_FACE);
1086 for (j = 1; j < MAXSIZEY - 1; j++) {
1087 glTranslatef(0, S1, 0);
1089 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * REVY(j)].face,
1093 glTranslatef(0, S1, 0);
1095 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * FIRSTY].face,
1097 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * LASTZ].face);
1100 if (slice.depth == MAXSIZEX - 1)
1101 glRotatef(-rotatestep, HALFX, 0, 0);
1102 glTranslatef(HALFX, -HALFY, -HALFZ);
1104 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * FIRSTY].face, NO_FACE,
1105 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * LASTY].face,
1106 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * LASTZ].face, NO_FACE);
1107 for (j = 1; j < MAXSIZEY - 1; j++) {
1108 glTranslatef(0, S1, 0);
1110 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * j].face, NO_FACE,
1111 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face,
1114 glTranslatef(0, S1, 0);
1116 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * LASTY].face, NO_FACE,
1117 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face,
1118 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * FIRSTZ].face);
1119 for (k = 1; k < MAXSIZEZ - 1; k++) {
1120 glTranslatef(0, -SY, S1);
1123 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * LASTY].face,
1124 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
1125 for (j = 1; j < MAXSIZEY - 1; j++) {
1126 glTranslatef(0, S1, 0);
1129 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * REVY(j)].face,
1132 glTranslatef(0, S1, 0);
1135 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * FIRSTY].face,
1136 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * k].face);
1138 glTranslatef(0, -SY, S1);
1140 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * LASTY].face,
1141 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face,
1142 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
1143 for (j = 1; j < MAXSIZEY - 1; j++) {
1144 glTranslatef(0, S1, 0);
1146 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * REVY(j)].face,
1147 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face,
1150 glTranslatef(0, S1, 0);
1152 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * FIRSTY].face,
1153 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face,
1154 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * LASTZ].face);
1156 case FRONT_FACE: /* BACK_FACE too */
1158 if (slice.depth == MAXSIZEZ - 1)
1159 glRotatef(rotatestep, 0, 0, HALFZ);
1161 glTranslatef(-HALFX, -HALFY, -HALFZ);
1163 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * FIRSTY].face, NO_FACE,
1164 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face, NO_FACE,
1165 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * LASTZ].face, NO_FACE);
1166 for (i = 1; i < MAXSIZEX - 1; i++) {
1167 glTranslatef(S1, 0, 0);
1169 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * FIRSTY].face, NO_FACE,
1171 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * LASTZ].face, NO_FACE);
1173 glTranslatef(S1, 0, 0);
1175 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * FIRSTY].face, NO_FACE,
1176 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * LASTY].face,
1177 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * LASTZ].face, NO_FACE);
1178 for (j = 1; j < MAXSIZEY - 1; j++) {
1179 glTranslatef(-SX, S1, 0);
1181 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * j].face, NO_FACE,
1182 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
1184 for (i = 1; i < MAXSIZEX - 1; i++) {
1185 glTranslatef(S1, 0, 0);
1187 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * j].face, NO_FACE,
1191 glTranslatef(S1, 0, 0);
1193 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * j].face, NO_FACE,
1194 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face,
1197 glTranslatef(-SX, S1, 0);
1199 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * LASTY].face, NO_FACE,
1200 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
1201 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face);
1202 for (i = 1; i < MAXSIZEX - 1; i++) {
1203 glTranslatef(S1, 0, 0);
1205 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * LASTY].face, NO_FACE,
1207 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * FIRSTZ].face);
1209 glTranslatef(S1, 0, 0);
1211 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * LASTY].face, NO_FACE,
1212 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face,
1213 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * FIRSTZ].face);
1215 for (k = 1; k < MAXSIZEZ - 1; k++) {
1217 if (slice.depth == REVZ(k))
1218 glRotatef(rotatestep, 0, 0, HALFZ);
1219 glTranslatef(-HALFX, -HALFY, MIDZ(k));
1222 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * LASTY].face, NO_FACE,
1223 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
1224 for (i = 1; i < MAXSIZEX - 1; i++) {
1225 glTranslatef(S1, 0, 0);
1229 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * REVZ(k)].face, NO_FACE);
1231 glTranslatef(S1, 0, 0);
1234 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * LASTY].face,
1235 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
1236 for (j = 1; j < MAXSIZEY - 1; j++) {
1237 glTranslatef(-SX, S1, 0);
1240 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * REVY(j)].face, NO_FACE,
1243 glTranslatef(SX, 0, 0);
1246 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * REVY(j)].face,
1249 glTranslatef(-SX, S1, 0);
1252 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * FIRSTY].face, NO_FACE,
1253 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * k].face);
1254 for (i = 1; i < MAXSIZEX - 1; i++) {
1255 glTranslatef(S1, 0, 0);
1259 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * k].face);
1261 glTranslatef(S1, 0, 0);
1264 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * FIRSTY].face,
1265 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * k].face);
1268 if (slice.depth == 0)
1269 glRotatef(rotatestep, 0, 0, HALFZ);
1270 glTranslatef(-HALFX, -HALFY, HALFZ);
1272 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * LASTY].face,
1273 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * LASTY].face, NO_FACE,
1274 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
1275 for (i = 1; i < MAXSIZEX - 1; i++) {
1276 glTranslatef(S1, 0, 0);
1278 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * LASTY].face,
1280 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * FIRSTZ].face, NO_FACE);
1282 glTranslatef(S1, 0, 0);
1284 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * LASTY].face,
1285 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face,
1286 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
1287 for (j = 1; j < MAXSIZEY - 1; j++) {
1288 glTranslatef(-SX, S1, 0);
1290 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * REVY(j)].face,
1291 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
1293 for (i = 1; i < MAXSIZEX - 1; i++) {
1294 glTranslatef(S1, 0, 0);
1296 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * REVY(j)].face,
1300 glTranslatef(S1, 0, 0);
1302 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * REVY(j)].face,
1303 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face,
1306 glTranslatef(-SX, S1, 0);
1308 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * FIRSTY].face,
1309 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
1310 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * LASTZ].face);
1311 for (i = 1; i < MAXSIZEX - 1; i++) {
1312 glTranslatef(S1, 0, 0);
1314 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * FIRSTY].face,
1316 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * LASTZ].face);
1318 glTranslatef(S1, 0, 0);
1320 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * FIRSTY].face,
1321 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face,
1322 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * LASTZ].face);
1329 /* From David Bagley's xrubik. Used by permission. ;) */
1331 readRC(rubikstruct * rp, int face, int dir, int h, int orient, int size)
1335 sizeOfRow = sizeRow(rp, face);
1336 if (dir == TOP || dir == BOTTOM)
1337 for (g = 0; g < size; g++)
1338 rp->rowLoc[orient][g] =
1339 rp->cubeLoc[face][g * sizeOfRow + h];
1340 else /* dir == RIGHT || dir == LEFT */
1341 for (g = 0; g < size; g++)
1342 rp->rowLoc[orient][g] =
1343 rp->cubeLoc[face][h * sizeOfRow + g];
1347 rotateRC(rubikstruct * rp, int rotate, int orient, int size)
1351 for (g = 0; g < size; g++)
1352 rp->rowLoc[orient][g].rotation =
1353 (rp->rowLoc[orient][g].rotation + rotate) % MAXORIENT;
1357 reverseRC(rubikstruct * rp, int orient, int size)
1362 for (g = 0; g < size / 2; g++) {
1363 temp = rp->rowLoc[orient][size - 1 - g];
1364 rp->rowLoc[orient][size - 1 - g] = rp->rowLoc[orient][g];
1365 rp->rowLoc[orient][g] = temp;
1370 writeRC(rubikstruct * rp, int face, int dir, int h, int orient, int size)
1372 int g, position, sizeOfRow;
1374 sizeOfRow = sizeRow(rp, face);
1375 if (dir == TOP || dir == BOTTOM) {
1376 for (g = 0; g < size; g++) {
1377 position = g * sizeOfRow + h;
1378 rp->cubeLoc[face][position] = rp->rowLoc[orient][g];
1379 /* DrawSquare(face, position); */
1381 } else { /* dir == RIGHT || dir == LEFT */
1382 for (g = 0; g < size; g++) {
1383 position = h * sizeOfRow + g;
1384 rp->cubeLoc[face][position] = rp->rowLoc[orient][g];
1385 /* DrawSquare(face, position); */
1391 rotateFace(rubikstruct * rp, int face, int direction)
1393 int position, i, j, sizeOfRow, sizeOfColumn, sizeOnPlane;
1396 faceSizes(rp, face, &sizeOfRow, &sizeOfColumn);
1397 sizeOnPlane = sizeOfRow * sizeOfColumn;
1398 if ((faceLoc = (RubikLoc *) malloc(sizeOnPlane *
1399 sizeof (RubikLoc))) == NULL) {
1403 for (position = 0; position < sizeOnPlane; position++)
1404 faceLoc[position] = rp->cubeLoc[face][position];
1406 for (position = 0; position < sizeOnPlane; position++) {
1407 i = position % sizeOfRow;
1408 j = position / sizeOfRow;
1409 if (direction == CW)
1410 rp->cubeLoc[face][position] =
1411 faceLoc[(sizeOfRow - i - 1) * sizeOfRow + j];
1412 else if (direction == CCW)
1413 rp->cubeLoc[face][position] =
1414 faceLoc[i * sizeOfRow + sizeOfColumn - j - 1];
1415 else /* (direction == HALF) */
1416 rp->cubeLoc[face][position] =
1417 faceLoc[sizeOfRow - i - 1 + (sizeOfColumn - j - 1) * sizeOfRow];
1418 rp->cubeLoc[face][position].rotation =
1419 (rp->cubeLoc[face][position].rotation +
1420 direction - MAXORIENT) % MAXORIENT;
1421 /* DrawSquare(face, position); */
1423 if (faceLoc != NULL)
1424 (void) free((void *) faceLoc);
1428 /* Yeah this is big and ugly */
1430 slideRC(int face, int direction, int h, int sizeOnOppAxis,
1431 int *newFace, int *newDirection, int *newH,
1432 int *rotate, Bool *reverse)
1434 *newFace = slideNextRow[face][direction].face;
1435 *rotate = slideNextRow[face][direction].rotation;
1436 *newDirection = (*rotate + direction) % MAXORIENT;
1443 if (*newDirection == TOP || *newDirection == BOTTOM) {
1444 *newH = sizeOnOppAxis - 1 - h;
1446 } else { /* *newDirection == RIGHT || *newDirection == LEFT */
1452 *newH = sizeOnOppAxis - 1 - h;
1456 if (*newDirection == TOP || *newDirection == BOTTOM) {
1459 } else { /* *newDirection == RIGHT || *newDirection == LEFT */
1460 *newH = sizeOnOppAxis - 1 - h;
1465 (void) printf("slideRC: rotate %d\n", *rotate);
1472 moveRubik(rubikstruct * rp, int face, int direction, int position)
1474 int newFace, newDirection, rotate, reverse;
1476 int i, j, sizeOfRow, sizeOfColumn, sizeOnAxis, sizeOnOppAxis;
1478 faceSizes(rp, face, &sizeOfRow, &sizeOfColumn);
1479 if (direction == CW || direction == CCW) {
1480 direction = (direction == CCW) ?
1481 (rotateToRow[face].direction + 2) % MAXORIENT :
1482 rotateToRow[face].direction;
1483 if (rotateToRow[face].sideFace == RIGHT) {
1484 i = j = sizeOfColumn - 1;
1485 } else if (rotateToRow[face].sideFace == BOTTOM) {
1486 i = j = sizeOfRow - 1;
1490 face = rotateToRow[face].face;
1491 position = j * sizeOfRow + i;
1493 i = position % sizeOfRow;
1494 j = position / sizeOfRow;
1495 h = (direction == TOP || direction == BOTTOM) ? i : j;
1496 if (direction == TOP || direction == BOTTOM) {
1497 sizeOnAxis = sizeOfColumn;
1498 sizeOnOppAxis = sizeOfRow;
1500 sizeOnAxis = sizeOfRow;
1501 sizeOnOppAxis = sizeOfColumn;
1503 /* rotate sides CW or CCW or HALF) */
1505 if (h == sizeOnOppAxis - 1) {
1506 newDirection = (direction == TOP || direction == BOTTOM) ?
1508 if (rp->degreeTurn == 180) {
1509 if (!rotateFace(rp, rowToRotate[face][newDirection], HALF))
1511 } else if (direction == TOP || direction == RIGHT) {
1512 if (!rotateFace(rp, rowToRotate[face][newDirection], CW))
1514 } else { /* direction == BOTTOM || direction == LEFT */
1515 if (!rotateFace(rp, rowToRotate[face][newDirection], CCW))
1520 newDirection = (direction == TOP || direction == BOTTOM) ?
1522 if (rp->degreeTurn == 180) {
1523 if (!rotateFace(rp, rowToRotate[face][newDirection], HALF))
1525 } else if (direction == TOP || direction == RIGHT) {
1526 if (!rotateFace(rp, rowToRotate[face][newDirection], CCW))
1528 } else { /* direction == BOTTOM || direction == LEFT */
1529 if (!rotateFace(rp, rowToRotate[face][newDirection], CW))
1533 /* Slide rows or columns */
1534 readRC(rp, face, direction, h, 0, sizeOnAxis);
1535 if (rp->degreeTurn == 180) {
1536 int sizeOnDepthAxis;
1538 slideRC(face, direction, h, sizeOnOppAxis,
1539 &newFace, &newDirection, &newH, &rotate, &reverse);
1540 sizeOnDepthAxis = sizeFace(rp, newFace) / sizeOnOppAxis;
1541 readRC(rp, newFace, newDirection, newH, 1, sizeOnDepthAxis);
1542 rotateRC(rp, rotate, 0, sizeOnAxis);
1543 if (reverse == True)
1544 reverseRC(rp, 0, sizeOnAxis);
1546 direction = newDirection;
1548 for (k = 2; k <= MAXORIENT + 1; k++) {
1549 slideRC(face, direction, h, sizeOnOppAxis,
1550 &newFace, &newDirection, &newH, &rotate, &reverse);
1551 if (k != MAXORIENT && k != MAXORIENT + 1)
1552 readRC(rp, newFace, newDirection, newH, k,
1553 (k % 2) ? sizeOnDepthAxis : sizeOnAxis);
1554 rotateRC(rp, rotate, k - 2,
1555 (k % 2) ? sizeOnDepthAxis : sizeOnAxis);
1556 if (k != MAXORIENT + 1)
1557 rotateRC(rp, rotate, k - 1,
1558 (k % 2) ? sizeOnAxis : sizeOnDepthAxis);
1559 if (reverse == True) {
1560 reverseRC(rp, k - 2,
1561 (k % 2) ? sizeOnDepthAxis : sizeOnAxis);
1562 if (k != MAXORIENT + 1)
1563 reverseRC(rp, k - 1,
1564 (k % 2) ? sizeOnAxis : sizeOnDepthAxis);
1566 writeRC(rp, newFace, newDirection, newH, k - 2,
1567 (k % 2) ? sizeOnDepthAxis : sizeOnAxis);
1569 direction = newDirection;
1573 for (k = 1; k <= MAXORIENT; k++) {
1574 slideRC(face, direction, h, sizeOnOppAxis,
1575 &newFace, &newDirection, &newH, &rotate, &reverse);
1577 readRC(rp, newFace, newDirection, newH, k, sizeOnAxis);
1578 rotateRC(rp, rotate, k - 1, sizeOnAxis);
1579 if (reverse == True)
1580 reverseRC(rp, k - 1, sizeOnAxis);
1581 writeRC(rp, newFace, newDirection, newH, k - 1, sizeOnAxis);
1583 direction = newDirection;
1592 printCube(rubikstruct * rp)
1594 int face, position, sizeOfRow, sizeOfColumn;
1596 for (face = 0; face < MAXFACES; face++) {
1597 faceSizes(rp, face, &sizeOfRow, &sizeOfColumn);
1598 for (position = 0; position < sizeOfRow * sizeOfColumn; position++) {
1599 (void) printf("%d %d ", rp->cubeLoc[face][position].face,
1600 rp->cubeLoc[face][position].rotation);
1601 if (!((position + 1) % sizeOfRow))
1602 (void) printf("\n");
1604 (void) printf("\n");
1606 (void) printf("\n");
1612 evalmovement(ModeInfo * mi, RubikMove movement)
1614 rubikstruct *rp = &rubik[MI_SCREEN(mi)];
1619 if (movement.face < 0 || movement.face >= MAXFACES)
1621 if (!moveRubik(rp, movement.face, movement.direction, movement.position))
1627 compare_moves(rubikstruct * rp, RubikMove move1, RubikMove move2, Bool opp)
1629 RubikSlice slice1, slice2;
1631 convertMove(rp, move1, &slice1);
1632 convertMove(rp, move2, &slice2);
1633 if (slice1.face == slice2.face &&
1634 slice1.depth == slice2.depth) {
1635 if (slice1.rotation == slice2.rotation) { /* CW or CCW */
1647 shuffle(ModeInfo * mi)
1649 rubikstruct *rp = &rubik[MI_SCREEN(mi)];
1650 int i, face, position;
1658 i = NRAND(-i - MINSIZE + 1) + MINSIZE;
1659 else if (i < MINSIZE)
1662 if (LRAND() % 2 && !sizey && !sizez) { /* Make normal (NxNxN) cubes more likely */
1663 MAXSIZEX = MAXSIZEY = MAXSIZEZ = i;
1671 i = NRAND(-i - MINSIZE + 1) + MINSIZE;
1672 else if (i < MINSIZE)
1674 if (LRAND() % 2 && !sizez) { /* Make more MxNxN more likely than LxMxN */
1675 MAXSIZEY = MAXSIZEZ = i;
1683 i = NRAND(-i - MINSIZE + 1) + MINSIZE;
1684 else if (i < MINSIZE)
1690 for (face = 0; face < MAXFACES; face++) {
1691 if (rp->cubeLoc[face] != NULL)
1692 (void) free((void *) rp->cubeLoc[face]);
1693 if ((rp->cubeLoc[face] = (RubikLoc *) malloc(sizeFace(rp, face) *
1694 sizeof (RubikLoc))) == NULL) {
1697 for (position = 0; position < sizeFace(rp, face); position++) {
1698 rp->cubeLoc[face][position].face = face;
1699 rp->cubeLoc[face][position].rotation = TOP;
1702 for (i = 0; i < MAXORIENT; i++) {
1703 if (rp->rowLoc[i] != NULL)
1704 (void) free((void *) rp->rowLoc[i]);
1705 /* The following is reused so make it the biggest size */
1706 if ((rp->rowLoc[i] = (RubikLoc *) malloc(MAXMAXSIZE *
1707 sizeof (RubikLoc))) == NULL) {
1711 rp->storedmoves = MI_COUNT(mi);
1712 if (rp->storedmoves < 0) {
1713 if (rp->moves != NULL)
1714 (void) free((void *) rp->moves);
1715 rp->moves = (RubikMove *) NULL;
1716 rp->storedmoves = NRAND(-rp->storedmoves) + 1;
1718 if ((rp->storedmoves) && (rp->moves == NULL))
1719 if ((rp->moves = (RubikMove *) calloc(rp->storedmoves + 1,
1720 sizeof (RubikMove))) == NULL) {
1723 if (MI_CYCLES(mi) <= 1) {
1724 rp->anglestep = 90.0;
1726 rp->anglestep = 90.0 / (GLfloat) (MI_CYCLES(mi));
1729 for (i = 0; i < rp->storedmoves; i++) {
1733 move.face = NRAND(MAXFACES);
1734 move.direction = NRAND(MAXORIENT); /* Exclude CW and CCW, its ok */
1735 move.position = NRAND(sizeFace(rp, move.face));
1736 rp->degreeTurn = (checkFaceSquare(rp,
1737 rowToRotate[move.face][move.direction])) ? 90 : 180;
1739 if (i > 0) { /* avoid immediate undoing moves */
1740 if (compare_moves(rp, move, rp->moves[i - 1], True))
1742 if (rp->degreeTurn == 180 &&
1743 compare_moves(rp, move, rp->moves[i - 1], False))
1746 if (i > 1) /* avoid 3 consecutive identical moves */
1747 if (compare_moves(rp, move, rp->moves[i - 1], False) &&
1748 compare_moves(rp, move, rp->moves[i - 2], False))
1751 * Still some silly moves being made....
1753 } while (!condition);
1755 if (!evalmovement(mi, move))
1757 rp->moves[i] = move;
1760 if (NRAND(100) < 50)
1763 if (NRAND(100) < 50)
1765 rp->movement.face = NO_FACE;
1767 rp->action = hideshuffling ? ACTION_SOLVE : ACTION_SHUFFLE;
1768 rp->shufflingmoves = 0;
1774 reshape_rubik(ModeInfo * mi, int width, int height)
1776 rubikstruct *rp = &rubik[MI_SCREEN(mi)];
1778 glViewport(0, 0, rp->WindW = (GLint) width, rp->WindH = (GLint) height);
1779 glMatrixMode(GL_PROJECTION);
1781 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);
1782 glMatrixMode(GL_MODELVIEW);
1787 pinit(ModeInfo * mi)
1790 glClearColor(0.0, 0.0, 0.0, 1.0);
1791 glColor3f(1.0, 1.0, 1.0);
1793 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
1794 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
1795 glLightfv(GL_LIGHT0, GL_POSITION, position0);
1796 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
1797 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
1798 glLightfv(GL_LIGHT1, GL_POSITION, position1);
1799 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
1800 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
1801 glEnable(GL_LIGHTING);
1802 glEnable(GL_LIGHT0);
1803 glEnable(GL_LIGHT1);
1804 glEnable(GL_DEPTH_TEST);
1805 glEnable(GL_NORMALIZE);
1806 glEnable(GL_CULL_FACE);
1808 glShadeModel(GL_FLAT);
1809 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
1810 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
1812 return (shuffle(mi));
1816 free_rubik(rubikstruct *rp)
1820 for (i = 0; i < MAXFACES; i++)
1821 if (rp->cubeLoc[i] != NULL) {
1822 (void) free((void *) rp->cubeLoc[i]);
1823 rp->cubeLoc[i] = (RubikLoc *) NULL;
1825 for (i = 0; i < MAXORIENT; i++)
1826 if (rp->rowLoc[i] != NULL) {
1827 (void) free((void *) rp->rowLoc[i]);
1828 rp->rowLoc[i] = (RubikLoc *) NULL;
1830 if (rp->moves != NULL) {
1831 (void) free((void *) rp->moves);
1832 rp->moves = (RubikMove *) NULL;
1837 release_rubik(ModeInfo * mi)
1839 if (rubik != NULL) {
1842 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
1843 rubikstruct *rp = &rubik[screen];
1847 (void) free((void *) rubik);
1848 rubik = (rubikstruct *) NULL;
1854 init_rubik(ModeInfo * mi)
1858 if (rubik == NULL) {
1859 if ((rubik = (rubikstruct *) calloc(MI_NUM_SCREENS(mi),
1860 sizeof (rubikstruct))) == NULL)
1863 rp = &rubik[MI_SCREEN(mi)];
1864 rp->step = NRAND(90);
1865 rp->PX = ((float) LRAND() / (float) MAXRAND) * 2.0 - 1.0;
1866 rp->PY = ((float) LRAND() / (float) MAXRAND) * 2.0 - 1.0;
1868 if ((rp->glx_context = init_GL(mi)) != NULL) {
1870 reshape_rubik(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
1871 glDrawBuffer(GL_BACK);
1874 if (MI_IS_VERBOSE(mi)) {
1875 (void) fprintf(stderr,
1876 "Could not allocate memory for rubik\n");
1886 draw_rubik(ModeInfo * mi)
1888 Bool bounced = False;
1889 Display *display = MI_DISPLAY(mi);
1890 Window window = MI_WINDOW(mi);
1895 rp = &rubik[MI_SCREEN(mi)];
1896 if (rp->cubeLoc[0] == NULL)
1899 MI_IS_DRAWN(mi) = True;
1900 if (!rp->glx_context)
1903 glXMakeCurrent(display, window, *(rp->glx_context));
1905 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1909 glTranslatef(0.0, 0.0, -10.0);
1915 rp->PY += (-1) - (rp->PY);
1920 rp->PY -= (rp->PY) - 1;
1925 rp->PX += (-1) - (rp->PX);
1930 rp->PX -= (rp->PX) - 1;
1935 rp->VX += ((float) LRAND() / (float) MAXRAND) * 0.02 - 0.01;
1936 rp->VX += ((float) LRAND() / (float) MAXRAND) * 0.02 - 0.01;
1946 if (!MI_IS_ICONIC(mi)) {
1947 glTranslatef(rp->PX, rp->PY, 0);
1948 glScalef(Scale4Window * rp->WindH / rp->WindW, Scale4Window, Scale4Window);
1950 glScalef(Scale4Iconic * rp->WindH / rp->WindW, Scale4Iconic, Scale4Iconic);
1953 glRotatef(rp->step * 100, 1, 0, 0);
1954 glRotatef(rp->step * 95, 0, 1, 0);
1955 glRotatef(rp->step * 90, 0, 0, 1);
1957 if (!draw_cube(mi)) {
1961 if (MI_IS_FPS(mi)) do_fps (mi);
1962 glXSwapBuffers(display, window);
1964 if (rp->action == ACTION_SHUFFLE) {
1966 if (++rp->rotatestep > DELAY_AFTER_SHUFFLING) {
1967 rp->movement.face = NO_FACE;
1969 rp->action = ACTION_SOLVE;
1973 if (rp->movement.face == NO_FACE) {
1974 if (rp->shufflingmoves < rp->storedmoves) {
1976 rp->movement = rp->moves[rp->shufflingmoves];
1982 if (rp->rotatestep == 0) {
1983 if (rp->movement.direction == CW || rp->movement.direction == CCW)
1984 rp->degreeTurn = (checkFaceSquare(rp, rp->movement.face)) ? 90 : 180;
1986 rp->degreeTurn = (checkFaceSquare(rp, rowToRotate[rp->movement.face][rp->movement.direction])) ? 90 : 180;
1988 rp->rotatestep += rp->anglestep;
1989 if (rp->rotatestep > rp->degreeTurn) {
1990 if (!evalmovement(mi, rp->movement)) {
1992 if (MI_IS_VERBOSE(mi)) {
1993 (void) fprintf(stderr,
1994 "Could not allocate memory for rubik\n");
1998 rp->shufflingmoves++;
1999 rp->movement.face = NO_FACE;
2005 if (++rp->rotatestep > DELAY_AFTER_SOLVING)
2008 if (MI_IS_VERBOSE(mi)) {
2009 (void) fprintf(stderr,
2010 "Could not allocate memory for rubik\n");
2015 if (rp->movement.face == NO_FACE) {
2016 if (rp->storedmoves > 0) {
2018 rp->movement = rp->moves[rp->storedmoves - 1];
2019 rp->movement.direction = (rp->movement.direction +
2020 (MAXORIENT / 2)) % MAXORIENT;
2026 if (rp->rotatestep == 0) {
2027 if (rp->movement.direction == CW || rp->movement.direction == CCW)
2028 rp->degreeTurn = (checkFaceSquare(rp, rp->movement.face)) ? 90 : 180;
2030 rp->degreeTurn = (checkFaceSquare(rp, rowToRotate[rp->movement.face][rp->movement.direction])) ? 90 : 180;
2032 rp->rotatestep += rp->anglestep;
2033 if (rp->rotatestep > rp->degreeTurn) {
2034 if (!evalmovement(mi, rp->movement)) {
2036 if (MI_IS_VERBOSE(mi)) {
2037 (void) fprintf(stderr,
2038 "Could not allocate memory for rubik\n");
2043 rp->movement.face = NO_FACE;
2058 change_rubik(ModeInfo * mi)
2064 rp = &rubik[MI_SCREEN(mi)];
2066 if (!rp->glx_context)
2070 if (MI_IS_VERBOSE(mi)) {
2071 (void) fprintf(stderr,
2072 "Could not allocate memory for rubik\n");
2077 #endif /* !STANDALONE */
2081 XSCREENSAVER_MODULE ("Rubik", rubik)