1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* rubik --- Shows an auto-solving Rubik's cube */
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)rubik.c 5.01 2001/03/01 xlockmore";
10 * Permission to use, copy, modify, and distribute this software and its
11 * documentation for any purpose and without fee is hereby granted,
12 * provided that the above copyright notice appear in all copies and that
13 * both that copyright notice and this permission notice appear in
14 * supporting documentation.
16 * This file is provided AS IS with no warranties of any kind. The author
17 * shall have no liability with respect to the infringement of copyrights,
18 * trade secrets or any patents by this file or any part thereof. In no
19 * event will the author be liable for any lost revenue or profits or
20 * other special, indirect and consequential damages.
22 * This mode shows an auto-solving rubik's cube "puzzle". If somebody
23 * intends to make a game or something based on this code, please let me
24 * know first, my e-mail address is provided in this comment. Marcelo.
26 * Thanks goes also to Brian Paul for making it possible and inexpensive
27 * to use OpenGL at home.
29 * Since I'm not a native English speaker, my apologies for any grammatical
32 * My e-mail address is
33 * mfvianna@centroin.com.br
35 * Marcelo F. Vianna (Jul-31-1997)
38 * 05-Apr-2002: Removed all gllist uses (fix some bug with nvidia driver)
39 * 01-Mar-2001: Added FPS stuff - Eric Lassauge <lassauge@mail.dotcom.fr>
40 * 01-Nov-2000: Allocation checks
41 * 27-Apr-1999: LxMxN stuff added.
42 * 26-Sep-1998: Added some more movement (the cube does not stay in the screen
43 * center anymore. Also fixed the scale problem immediately after
44 * shuffling when the puzzle is solved.
45 * 08-Aug-1997: Now has some internals from xrubik by David Bagley
46 * This should make it easier to add features.
47 * 02-Aug-1997: Now behaves more like puzzle.c: first show the cube being
48 * shuffled and then being solved. A mode specific option was
50 * "+/-hideshuffling" to provide the original behavior (in which
51 * only the solution is shown).
52 * The color labels corners are now rounded.
53 * Optimized the cubit() routine using glLists.
54 * 01-Aug-1997: Shuffling now avoids movements that undoes the previous
55 * movement and three consecutive identical moves (which is
57 * improved the "cycles" option in replacement of David's hack,
58 * now rp->anglestep is a GLfloat, so this option selects the
59 * "exact" number of frames that a rotation (movement) takes to
61 * 30-Jul-1997: Initial release, there is no algorithm to solve the puzzle,
62 * instead, it randomly shuffle the cube and then make the
63 * movements in the reverse order.
64 * The mode was written in 1 day (I got sick and had the day off).
65 * There was not much to do since I could not leave home... :)
69 * Color labels mapping:
70 * =====================
78 * +-----------+-----------+-----------+
81 * |v LEFT(1) |v FRONT(2) |v RIGHT(3) |
84 * +-----------+-----------+-----------+
90 * +-----------+ +---+---+---+
91 * |0--> | | 0 | 1 | 2 |
93 * |v BACK(5) | | 3 | 4 | 5 |
96 * +-----------+ +---+---+---+
109 * due to a Bug/feature in VMS X11/Intrinsic.h has to be placed before xlock.
110 * otherwise caddr_t is not defined correctly
112 #include <X11/Intrinsic.h>
117 # define PROGCLASS "Rubik"
118 # define HACK_INIT init_rubik
119 # define HACK_DRAW draw_rubik
120 # define HACK_RESHAPE reshape
121 # define rubik_opts xlockmore_opts
122 # define DEFAULTS "*delay: 40000 \n" \
124 "*showFPS: False \n" \
127 # include "xlockmore.h" /* from the xscreensaver distribution */
128 #else /* !STANDALONE */
129 # include "xlock.h" /* from the xlockmore distribution */
131 #endif /* !STANDALONE */
135 #define DEF_SIZEX "0"
136 #define DEF_SIZEY "0"
137 #define DEF_SIZEZ "0"
138 #define DEF_HIDESHUFFLING "False"
143 static Bool hideshuffling;
145 static XrmOptionDescRec opts[] =
147 {(char *) "-sizex", (char *) ".rubik.sizex", XrmoptionSepArg, (caddr_t) NULL},
148 {(char *) "-sizey", (char *) ".rubik.sizey", XrmoptionSepArg, (caddr_t) NULL},
149 {(char *) "-sizez", (char *) ".rubik.sizez", XrmoptionSepArg, (caddr_t) NULL},
150 {(char *) "-hideshuffling", (char *) ".rubik.hideshuffling", XrmoptionNoArg, (caddr_t) "on"},
151 {(char *) "+hideshuffling", (char *) ".rubik.hideshuffling", XrmoptionNoArg, (caddr_t) "off"}
154 static argtype vars[] =
156 {(caddr_t *) & sizex, (char *) "sizex", (char *) "SizeX", (char *) DEF_SIZEX, t_Int},
157 {(caddr_t *) & sizey, (char *) "sizey", (char *) "SizeY", (char *) DEF_SIZEY, t_Int},
158 {(caddr_t *) & sizez, (char *) "sizez", (char *) "SizeZ", (char *) DEF_SIZEZ, t_Int},
159 {(caddr_t *) & hideshuffling, (char *) "hideshuffling", (char *) "Hideshuffling", (char *) DEF_HIDESHUFFLING, t_Bool}
162 static OptionStruct desc[] =
164 {(char *) "-sizex num", (char *) "number of cubies along x axis (overrides size)"},
165 {(char *) "-sizey num", (char *) "number of cubies along y axis (overrides size)"},
166 {(char *) "-sizez num", (char *) "number of cubies along z axis (overrides size)"},
167 {(char *) "-/+hideshuffling", (char *) "turn on/off hidden shuffle phase"}
170 ModeSpecOpt rubik_opts =
171 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
174 ModStruct rubik_description =
175 {"rubik", "init_rubik", "draw_rubik", "release_rubik",
176 "draw_rubik", "change_rubik", (char *) NULL, &rubik_opts,
177 10000, -30, 5, -6, 64, 1.0, "",
178 "Shows an auto-solving Rubik's Cube", 0, NULL};
182 #define VectMul(X1,Y1,Z1,X2,Y2,Z2) (Y1)*(Z2)-(Z1)*(Y2),(Z1)*(X2)-(X1)*(Z2),(X1)*(Y2)-(Y1)*(X2)
183 #define sqr(A) ((A)*(A))
190 #define ACTION_SOLVE 1
191 #define ACTION_SHUFFLE 0
193 #define DELAY_AFTER_SHUFFLING 5
194 #define DELAY_AFTER_SOLVING 20
196 /*************************************************************************/
199 #define MAXSIZEX (rp->sizex)
200 #define MAXSIZEY (rp->sizey)
201 #define MAXSIZEZ (rp->sizez)
202 #define AVSIZE ((rp->sizex+rp->sizey+rp->sizez)/3.0) /* Use of this should be minimized */
203 #define MAXMAXSIZE (MAX(MAXSIZEX,MAX(MAXSIZEY,MAXSIZEZ)))
204 #define MAXSIZEXY (MAXSIZEX*MAXSIZEY)
205 #define MAXSIZEYZ (MAXSIZEY*MAXSIZEZ)
206 #define MAXSIZEZX (MAXSIZEZ*MAXSIZEX)
207 #define LASTX (MAXSIZEX-1)
208 #define LASTY (MAXSIZEY-1)
209 #define LASTZ (MAXSIZEZ-1)
210 /* These are not likely to change but... */
215 #define Scale4Window (0.9/AVSIZE)
216 #define Scale4Iconic (2.1/AVSIZE)
218 #define MAXORIENT 4 /* Number of orientations of a square */
219 #define MAXFACES 6 /* Number of faces */
221 /* Directions relative to the face of a cubie */
226 #define CW (MAXORIENT+1)
227 #define HALF (MAXORIENT+2)
228 #define CCW (2*MAXORIENT-1)
234 #define BOTTOM_FACE 4
236 #define NO_FACE (MAXFACES)
237 #define NO_ROTATION (2*MAXORIENT)
238 #define NO_DEPTH MAXMAXSIZE
240 #define REVX(a) (MAXSIZEX - a - 1)
241 #define REVY(a) (MAXSIZEY - a - 1)
242 #define REVZ(a) (MAXSIZEZ - a - 1)
245 #define CUBEROUND (CUBELEN-0.05)
246 #define STICKERLONG (CUBEROUND-0.05)
247 #define STICKERSHORT (STICKERLONG-0.05)
248 #define STICKERDEPTH (CUBELEN+0.01)
252 typedef struct _RubikLoc {
254 int rotation; /* Not used yet */
257 typedef struct _RubikRowNext {
258 int face, direction, sideFace;
261 typedef struct _RubikMove {
266 typedef struct _RubikSlice {
272 * Pick a face and a direction on face the next face and orientation
275 static RubikLoc slideNextRow[MAXFACES][MAXORIENT] =
310 * Examine cubie 0 on each face, its 4 movements (well only 2 since the
311 * other 2 will be opposites) and translate it into slice movements).
312 * CW = DEEP Depth CCW == SHALLOW Depth with reference to faces 0, 1, and 2
314 static RubikLoc rotateSlice[MAXFACES][MAXORIENT / 2] =
343 * Rotate face clockwise by a number of orients, then the top of the
344 * face then points to this face
346 static int rowToRotate[MAXFACES][MAXORIENT] =
357 * This translates a clockwise move to something more manageable
359 static RubikRowNext rotateToRow[MAXFACES] = /*CW to min face */
376 int sizex, sizey, sizez;
377 float avsize, avsizeSq;
381 RubikLoc *cubeLoc[MAXFACES];
382 RubikLoc *rowLoc[MAXORIENT];
385 GLfloat PX, PY, VX, VY;
386 GLXContext *glx_context;
389 static float front_shininess[] =
391 static float front_specular[] =
392 {0.7, 0.7, 0.7, 1.0};
393 static float ambient[] =
394 {0.0, 0.0, 0.0, 1.0};
395 static float diffuse[] =
396 {1.0, 1.0, 1.0, 1.0};
397 static float position0[] =
398 {1.0, 1.0, 1.0, 0.0};
399 static float position1[] =
400 {-1.0, -1.0, 1.0, 0.0};
401 static float lmodel_ambient[] =
402 {0.5, 0.5, 0.5, 1.0};
403 static float lmodel_twoside[] =
406 static float MaterialRed[] =
407 {0.5, 0.0, 0.0, 1.0};
408 static float MaterialGreen[] =
409 {0.0, 0.5, 0.0, 1.0};
410 static float MaterialBlue[] =
411 {0.0, 0.0, 0.5, 1.0};
412 static float MaterialYellow[] =
413 {0.7, 0.7, 0.0, 1.0};
414 static float MaterialOrange[] =
415 {0.9, 0.45, 0.36, 1.0};
418 static float MaterialMagenta[] =
419 {0.7, 0.0, 0.7, 1.0};
420 static float MaterialCyan[] =
421 {0.0, 0.7, 0.7, 1.0};
424 static float MaterialWhite[] =
425 {0.8, 0.8, 0.8, 1.0};
426 static float MaterialGray[] =
427 {0.2, 0.2, 0.2, 1.0};
428 static float MaterialGray3[] =
429 {0.3, 0.3, 0.3, 1.0};
430 static float MaterialGray4[] =
431 {0.4, 0.4, 0.4, 1.0};
432 static float MaterialGray5[] =
433 {0.5, 0.5, 0.5, 1.0};
434 static float MaterialGray6[] =
435 {0.6, 0.6, 0.6, 1.0};
436 static float MaterialGray7[] =
437 {0.7, 0.7, 0.7, 1.0};
439 static rubikstruct *rubik = (rubikstruct *) NULL;
443 pickcolor(int C, int mono)
448 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray3);
450 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialRed);
454 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray6);
456 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialYellow);
459 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialWhite);
463 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray4);
465 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGreen);
469 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray7);
471 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialOrange);
475 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray5);
477 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialBlue);
480 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialCyan);
481 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialMagenta);
487 faceSizes(rubikstruct * rp, int face, int * sizeOfRow, int * sizeOfColumn)
492 *sizeOfRow = MAXSIZEX;
493 *sizeOfColumn = MAXSIZEZ;
497 *sizeOfRow = MAXSIZEZ;
498 *sizeOfColumn = MAXSIZEY;
502 *sizeOfRow = MAXSIZEX;
503 *sizeOfColumn = MAXSIZEY;
509 checkFaceSquare(rubikstruct * rp, int face)
511 int sizeOfRow, sizeOfColumn;
513 faceSizes(rp, face, &sizeOfRow, &sizeOfColumn);
514 return (sizeOfRow == sizeOfColumn);
515 /* Cubes can be made square with a 4x2 face where 90 degree turns
516 * should be permitted but that is kind of complicated for me.
517 * This can be done in 2 ways where the side of the cubies are
518 * the same size and one where one side (the side with half the
519 * number of cubies) is twice the size of the other. The first is
520 * complicated because faces of cubies can go under other faces.
521 * The second way is similar to "banded cubes" where scotch tape
522 * restricts the moves of some cubes. Here you have to keep track
523 * of the restrictions and show banded cubies graphically as one
529 sizeFace(rubikstruct * rp, int face)
531 int sizeOfRow, sizeOfColumn;
533 faceSizes(rp, face, &sizeOfRow, &sizeOfColumn);
534 return (sizeOfRow * sizeOfColumn);
538 sizeRow(rubikstruct * rp, int face)
540 int sizeOfRow, sizeOfColumn; /* sizeOfColumn not used */
542 faceSizes(rp, face, &sizeOfRow, &sizeOfColumn);
547 draw_stickerless_cubit(rubikstruct *rp)
550 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray);
551 /* Put sticker here */
552 glNormal3f(0.00, 0.00, 1.00);
553 glVertex3f(-CUBEROUND, -CUBEROUND, CUBELEN);
554 glVertex3f(CUBEROUND, -CUBEROUND, CUBELEN);
555 glVertex3f(CUBEROUND, CUBEROUND, CUBELEN);
556 glVertex3f(-CUBEROUND, CUBEROUND, CUBELEN);
557 glNormal3f(0.00, 0.00, -1.00);
558 glVertex3f(-CUBEROUND, CUBEROUND, -CUBELEN);
559 glVertex3f(CUBEROUND, CUBEROUND, -CUBELEN);
560 glVertex3f(CUBEROUND, -CUBEROUND, -CUBELEN);
561 glVertex3f(-CUBEROUND, -CUBEROUND, -CUBELEN);
562 glNormal3f(-1.00, 0.00, 0.00);
563 glVertex3f(-CUBELEN, -CUBEROUND, CUBEROUND);
564 glVertex3f(-CUBELEN, CUBEROUND, CUBEROUND);
565 glVertex3f(-CUBELEN, CUBEROUND, -CUBEROUND);
566 glVertex3f(-CUBELEN, -CUBEROUND, -CUBEROUND);
567 glNormal3f(1.00, 0.00, 0.00);
568 glVertex3f(CUBELEN, -CUBEROUND, -CUBEROUND);
569 glVertex3f(CUBELEN, CUBEROUND, -CUBEROUND);
570 glVertex3f(CUBELEN, CUBEROUND, CUBEROUND);
571 glVertex3f(CUBELEN, -CUBEROUND, CUBEROUND);
572 glNormal3f(0.00, -1.00, 0.00);
573 glVertex3f(CUBEROUND, -CUBELEN, -CUBEROUND);
574 glVertex3f(CUBEROUND, -CUBELEN, CUBEROUND);
575 glVertex3f(-CUBEROUND, -CUBELEN, CUBEROUND);
576 glVertex3f(-CUBEROUND, -CUBELEN, -CUBEROUND);
577 glNormal3f(0.00, 1.00, 0.00);
578 glVertex3f(-CUBEROUND, CUBELEN, -CUBEROUND);
579 glVertex3f(-CUBEROUND, CUBELEN, CUBEROUND);
580 glVertex3f(CUBEROUND, CUBELEN, CUBEROUND);
581 glVertex3f(CUBEROUND, CUBELEN, -CUBEROUND);
584 glNormal3f(-1.00, -1.00, 0.00);
585 glVertex3f(-CUBEROUND, -CUBELEN, -CUBEROUND);
586 glVertex3f(-CUBEROUND, -CUBELEN, CUBEROUND);
587 glVertex3f(-CUBELEN, -CUBEROUND, CUBEROUND);
588 glVertex3f(-CUBELEN, -CUBEROUND, -CUBEROUND);
589 glNormal3f(1.00, 1.00, 0.00);
590 glVertex3f(CUBEROUND, CUBELEN, -CUBEROUND);
591 glVertex3f(CUBEROUND, CUBELEN, CUBEROUND);
592 glVertex3f(CUBELEN, CUBEROUND, CUBEROUND);
593 glVertex3f(CUBELEN, CUBEROUND, -CUBEROUND);
594 glNormal3f(-1.00, 1.00, 0.00);
595 glVertex3f(-CUBELEN, CUBEROUND, -CUBEROUND);
596 glVertex3f(-CUBELEN, CUBEROUND, CUBEROUND);
597 glVertex3f(-CUBEROUND, CUBELEN, CUBEROUND);
598 glVertex3f(-CUBEROUND, CUBELEN, -CUBEROUND);
599 glNormal3f(1.00, -1.00, 0.00);
600 glVertex3f(CUBELEN, -CUBEROUND, -CUBEROUND);
601 glVertex3f(CUBELEN, -CUBEROUND, CUBEROUND);
602 glVertex3f(CUBEROUND, -CUBELEN, CUBEROUND);
603 glVertex3f(CUBEROUND, -CUBELEN, -CUBEROUND);
604 glNormal3f(0.00, -1.00, -1.00);
605 glVertex3f(-CUBEROUND, -CUBEROUND, -CUBELEN);
606 glVertex3f(CUBEROUND, -CUBEROUND, -CUBELEN);
607 glVertex3f(CUBEROUND, -CUBELEN, -CUBEROUND);
608 glVertex3f(-CUBEROUND, -CUBELEN, -CUBEROUND);
609 glNormal3f(0.00, 1.00, 1.00);
610 glVertex3f(-CUBEROUND, CUBEROUND, CUBELEN);
611 glVertex3f(CUBEROUND, CUBEROUND, CUBELEN);
612 glVertex3f(CUBEROUND, CUBELEN, CUBEROUND);
613 glVertex3f(-CUBEROUND, CUBELEN, CUBEROUND);
614 glNormal3f(0.00, -1.00, 1.00);
615 glVertex3f(-CUBEROUND, -CUBELEN, CUBEROUND);
616 glVertex3f(CUBEROUND, -CUBELEN, CUBEROUND);
617 glVertex3f(CUBEROUND, -CUBEROUND, CUBELEN);
618 glVertex3f(-CUBEROUND, -CUBEROUND, CUBELEN);
619 glNormal3f(0.00, 1.00, -1.00);
620 glVertex3f(-CUBEROUND, CUBELEN, -CUBEROUND);
621 glVertex3f(CUBEROUND, CUBELEN, -CUBEROUND);
622 glVertex3f(CUBEROUND, CUBEROUND, -CUBELEN);
623 glVertex3f(-CUBEROUND, CUBEROUND, -CUBELEN);
624 glNormal3f(-1.00, 0.00, -1.00);
625 glVertex3f(-CUBELEN, -CUBEROUND, -CUBEROUND);
626 glVertex3f(-CUBELEN, CUBEROUND, -CUBEROUND);
627 glVertex3f(-CUBEROUND, CUBEROUND, -CUBELEN);
628 glVertex3f(-CUBEROUND, -CUBEROUND, -CUBELEN);
629 glNormal3f(1.00, 0.00, 1.00);
630 glVertex3f(CUBELEN, -CUBEROUND, CUBEROUND);
631 glVertex3f(CUBELEN, CUBEROUND, CUBEROUND);
632 glVertex3f(CUBEROUND, CUBEROUND, CUBELEN);
633 glVertex3f(CUBEROUND, -CUBEROUND, CUBELEN);
634 glNormal3f(1.00, 0.00, -1.00);
635 glVertex3f(CUBEROUND, -CUBEROUND, -CUBELEN);
636 glVertex3f(CUBEROUND, CUBEROUND, -CUBELEN);
637 glVertex3f(CUBELEN, CUBEROUND, -CUBEROUND);
638 glVertex3f(CUBELEN, -CUBEROUND, -CUBEROUND);
639 glNormal3f(-1.00, 0.00, 1.00);
640 glVertex3f(-CUBEROUND, -CUBEROUND, CUBELEN);
641 glVertex3f(-CUBEROUND, CUBEROUND, CUBELEN);
642 glVertex3f(-CUBELEN, CUBEROUND, CUBEROUND);
643 glVertex3f(-CUBELEN, -CUBEROUND, CUBEROUND);
645 glBegin(GL_TRIANGLES);
646 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray);
647 /* Corners of cubit */
648 glNormal3f(1.00, 1.00, 1.00);
649 glVertex3f(CUBEROUND, CUBEROUND, CUBELEN);
650 glVertex3f(CUBELEN, CUBEROUND, CUBEROUND);
651 glVertex3f(CUBEROUND, CUBELEN, CUBEROUND);
652 glNormal3f(-1.00, -1.00, -1.00);
653 glVertex3f(-CUBEROUND, -CUBELEN, -CUBEROUND);
654 glVertex3f(-CUBELEN, -CUBEROUND, -CUBEROUND);
655 glVertex3f(-CUBEROUND, -CUBEROUND, -CUBELEN);
656 glNormal3f(-1.00, 1.00, 1.00);
657 glVertex3f(-CUBEROUND, CUBEROUND, CUBELEN);
658 glVertex3f(-CUBEROUND, CUBELEN, CUBEROUND);
659 glVertex3f(-CUBELEN, CUBEROUND, CUBEROUND);
660 glNormal3f(1.00, -1.00, -1.00);
661 glVertex3f(CUBELEN, -CUBEROUND, -CUBEROUND);
662 glVertex3f(CUBEROUND, -CUBELEN, -CUBEROUND);
663 glVertex3f(CUBEROUND, -CUBEROUND, -CUBELEN);
664 glNormal3f(1.00, -1.00, 1.00);
665 glVertex3f(CUBEROUND, -CUBEROUND, CUBELEN);
666 glVertex3f(CUBEROUND, -CUBELEN, CUBEROUND);
667 glVertex3f(CUBELEN, -CUBEROUND, CUBEROUND);
668 glNormal3f(-1.00, 1.00, -1.00);
669 glVertex3f(-CUBELEN, CUBEROUND, -CUBEROUND);
670 glVertex3f(-CUBEROUND, CUBELEN, -CUBEROUND);
671 glVertex3f(-CUBEROUND, CUBEROUND, -CUBELEN);
672 glNormal3f(-1.00, -1.00, 1.00);
673 glVertex3f(-CUBEROUND, -CUBEROUND, CUBELEN);
674 glVertex3f(-CUBELEN, -CUBEROUND, CUBEROUND);
675 glVertex3f(-CUBEROUND, -CUBELEN, CUBEROUND);
676 glNormal3f(1.00, 1.00, -1.00);
677 glVertex3f(CUBELEN, CUBEROUND, -CUBEROUND);
678 glVertex3f(CUBEROUND, CUBEROUND, -CUBELEN);
679 glVertex3f(CUBEROUND, CUBELEN, -CUBEROUND);
685 draw_cubit(ModeInfo * mi,
686 int back, int front, int left, int right, int bottom, int top)
688 rubikstruct *rp = &rubik[MI_SCREEN(mi)];
689 int mono = MI_IS_MONO(mi);
691 if (!draw_stickerless_cubit(rp))
693 if (back != NO_FACE) {
695 pickcolor(back, mono);
696 glNormal3f(0.00, 0.00, -1.00);
697 glVertex3f(-STICKERSHORT, STICKERLONG, -STICKERDEPTH);
698 glVertex3f(STICKERSHORT, STICKERLONG, -STICKERDEPTH);
699 glVertex3f(STICKERLONG, STICKERSHORT, -STICKERDEPTH);
700 glVertex3f(STICKERLONG, -STICKERSHORT, -STICKERDEPTH);
701 glVertex3f(STICKERSHORT, -STICKERLONG, -STICKERDEPTH);
702 glVertex3f(-STICKERSHORT, -STICKERLONG, -STICKERDEPTH);
703 glVertex3f(-STICKERLONG, -STICKERSHORT, -STICKERDEPTH);
704 glVertex3f(-STICKERLONG, STICKERSHORT, -STICKERDEPTH);
707 if (front != NO_FACE) {
709 pickcolor(front, mono);
710 glNormal3f(0.00, 0.00, 1.00);
711 glVertex3f(-STICKERSHORT, -STICKERLONG, STICKERDEPTH);
712 glVertex3f(STICKERSHORT, -STICKERLONG, STICKERDEPTH);
713 glVertex3f(STICKERLONG, -STICKERSHORT, STICKERDEPTH);
714 glVertex3f(STICKERLONG, STICKERSHORT, STICKERDEPTH);
715 glVertex3f(STICKERSHORT, STICKERLONG, STICKERDEPTH);
716 glVertex3f(-STICKERSHORT, STICKERLONG, STICKERDEPTH);
717 glVertex3f(-STICKERLONG, STICKERSHORT, STICKERDEPTH);
718 glVertex3f(-STICKERLONG, -STICKERSHORT, STICKERDEPTH);
721 if (left != NO_FACE) {
723 pickcolor(left, mono);
724 glNormal3f(-1.00, 0.00, 0.00);
725 glVertex3f(-STICKERDEPTH, -STICKERSHORT, STICKERLONG);
726 glVertex3f(-STICKERDEPTH, STICKERSHORT, STICKERLONG);
727 glVertex3f(-STICKERDEPTH, STICKERLONG, STICKERSHORT);
728 glVertex3f(-STICKERDEPTH, STICKERLONG, -STICKERSHORT);
729 glVertex3f(-STICKERDEPTH, STICKERSHORT, -STICKERLONG);
730 glVertex3f(-STICKERDEPTH, -STICKERSHORT, -STICKERLONG);
731 glVertex3f(-STICKERDEPTH, -STICKERLONG, -STICKERSHORT);
732 glVertex3f(-STICKERDEPTH, -STICKERLONG, STICKERSHORT);
735 if (right != NO_FACE) {
737 pickcolor(right, mono);
738 glNormal3f(1.00, 0.00, 0.00);
739 glVertex3f(STICKERDEPTH, -STICKERSHORT, -STICKERLONG);
740 glVertex3f(STICKERDEPTH, STICKERSHORT, -STICKERLONG);
741 glVertex3f(STICKERDEPTH, STICKERLONG, -STICKERSHORT);
742 glVertex3f(STICKERDEPTH, STICKERLONG, STICKERSHORT);
743 glVertex3f(STICKERDEPTH, STICKERSHORT, STICKERLONG);
744 glVertex3f(STICKERDEPTH, -STICKERSHORT, STICKERLONG);
745 glVertex3f(STICKERDEPTH, -STICKERLONG, STICKERSHORT);
746 glVertex3f(STICKERDEPTH, -STICKERLONG, -STICKERSHORT);
749 if (bottom != NO_FACE) {
751 pickcolor(bottom, mono);
752 glNormal3f(0.00, -1.00, 0.00);
753 glVertex3f(STICKERLONG, -STICKERDEPTH, -STICKERSHORT);
754 glVertex3f(STICKERLONG, -STICKERDEPTH, STICKERSHORT);
755 glVertex3f(STICKERSHORT, -STICKERDEPTH, STICKERLONG);
756 glVertex3f(-STICKERSHORT, -STICKERDEPTH, STICKERLONG);
757 glVertex3f(-STICKERLONG, -STICKERDEPTH, STICKERSHORT);
758 glVertex3f(-STICKERLONG, -STICKERDEPTH, -STICKERSHORT);
759 glVertex3f(-STICKERSHORT, -STICKERDEPTH, -STICKERLONG);
760 glVertex3f(STICKERSHORT, -STICKERDEPTH, -STICKERLONG);
763 if (top != NO_FACE) {
765 pickcolor(top, mono);
766 glNormal3f(0.00, 1.00, 0.00);
767 glVertex3f(-STICKERLONG, STICKERDEPTH, -STICKERSHORT);
768 glVertex3f(-STICKERLONG, STICKERDEPTH, STICKERSHORT);
769 glVertex3f(-STICKERSHORT, STICKERDEPTH, STICKERLONG);
770 glVertex3f(STICKERSHORT, STICKERDEPTH, STICKERLONG);
771 glVertex3f(STICKERLONG, STICKERDEPTH, STICKERSHORT);
772 glVertex3f(STICKERLONG, STICKERDEPTH, -STICKERSHORT);
773 glVertex3f(STICKERSHORT, STICKERDEPTH, -STICKERLONG);
774 glVertex3f(-STICKERSHORT, STICKERDEPTH, -STICKERLONG);
780 /* Convert move to weird general notation */
782 convertMove(rubikstruct * rp, RubikMove move, RubikSlice * slice)
785 int sizeOfRow, sizeOfColumn;
787 plane = rotateSlice[(int) move.face][move.direction % 2];
788 (*slice).face = plane.face;
789 (*slice).rotation = plane.rotation;
791 faceSizes(rp, move.face, &sizeOfRow, &sizeOfColumn);
792 if (plane.face == 1 || /* VERTICAL */
793 (plane.face == 2 && (move.face == 1 || move.face == 3))) {
794 if ((*slice).rotation == CW)
795 (*slice).depth = sizeOfRow - 1 - move.position %
798 (*slice).depth = move.position % sizeOfRow;
799 } else { /* (plane.face == 0 || *//* HORIZONTAL *//*
800 (plane.face == 2 && (move.face == 0 || move.face == 4))) */
801 if ((*slice).rotation == CW)
802 (*slice).depth = sizeOfColumn - 1 - move.position /
805 (*slice).depth = move.position / sizeOfRow;
807 /* If (*slice).depth = 0 then face 0, face 1, or face 2 moves */
808 if (move.direction / 2)
809 (*slice).rotation = ((*slice).rotation == CW) ? CCW : CW;
812 /* Assume the size is at least 2, or its just not challenging... */
814 draw_cube(ModeInfo * mi)
817 #define SX ((GLint)S1*(MAXSIZEX-1))
818 #define SY ((GLint)S1*(MAXSIZEY-1))
819 #define SZ ((GLint)S1*(MAXSIZEZ-1))
820 #define HALFX (((GLfloat)MAXSIZEX-1.0)/2.0)
821 #define HALFY (((GLfloat)MAXSIZEY-1.0)/2.0)
822 #define HALFZ (((GLfloat)MAXSIZEZ-1.0)/2.0)
823 #define MIDX(a) (((GLfloat)(2*a-MAXSIZEX+1))/2.0)
824 #define MIDY(a) (((GLfloat)(2*a-MAXSIZEY+1))/2.0)
825 #define MIDZ(a) (((GLfloat)(2*a-MAXSIZEZ+1))/2.0)
826 #define DRAW_CUBIT(mi,b,f,l,r,bm,t) if (!draw_cubit(mi,b,f,l,r,bm,t)) return False
827 rubikstruct *rp = &rubik[MI_SCREEN(mi)];
832 if (rp->movement.face == NO_FACE) {
833 slice.face = NO_FACE;
834 slice.rotation = NO_ROTATION;
835 slice.depth = NO_DEPTH;
837 convertMove(rp, rp->movement, &slice);
839 rotatestep = (slice.rotation == CCW) ? rp->rotatestep : -rp->rotatestep;
843 * The glRotatef() routine transforms the coordinate system for every future
844 * vertex specification (this is not so simple, but by now comprehending this
845 * is sufficient). So if you want to rotate the inner slice, you can draw
846 * one slice, rotate the anglestep for the centerslice, draw the inner slice,
847 * rotate reversely and draw the other slice.
848 * There is a sequence for drawing cubies for each axis being moved...
850 switch (slice.face) {
852 case TOP_FACE: /* BOTTOM_FACE too */
854 if (slice.depth == MAXSIZEY - 1)
855 glRotatef(rotatestep, 0, HALFY, 0);
857 glTranslatef(-HALFX, -HALFY, -HALFZ);
859 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * FIRSTY].face, NO_FACE,
860 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face, NO_FACE,
861 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * LASTZ].face, NO_FACE);
862 for (k = 1; k < MAXSIZEZ - 1; k++) {
863 glTranslatef(0, 0, S1);
866 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * LASTY].face, NO_FACE,
867 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
869 glTranslatef(0, 0, S1);
871 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * LASTY].face,
872 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * LASTY].face, NO_FACE,
873 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
874 for (i = 1; i < MAXSIZEX - 1; i++) {
875 glTranslatef(S1, 0, -SZ);
877 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * FIRSTY].face, NO_FACE,
879 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * LASTZ].face, NO_FACE);
880 for (k = 1; k < MAXSIZEZ - 1; k++) {
881 glTranslatef(0, 0, S1);
885 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * REVZ(k)].face, NO_FACE);
887 glTranslatef(0, 0, S1);
889 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * LASTY].face,
891 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * FIRSTZ].face, NO_FACE);
893 glTranslatef(S1, 0, -SZ);
895 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * FIRSTY].face, NO_FACE,
896 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * LASTY].face,
897 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * LASTZ].face, NO_FACE);
898 for (k = 1; k < MAXSIZEZ - 1; k++) {
899 glTranslatef(0, 0, S1);
902 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * LASTY].face,
903 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
905 glTranslatef(0, 0, S1);
907 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * LASTY].face,
908 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face,
909 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
911 for (j = 1; j < MAXSIZEY - 1; j++) {
913 if (slice.depth == REVY(j))
914 glRotatef(rotatestep, 0, HALFY, 0);
915 glTranslatef(-HALFX, MIDY(j), -HALFZ);
917 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * j].face, NO_FACE,
918 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
920 for (k = 1; k < MAXSIZEZ - 1; k++) {
921 glTranslatef(0, 0, S1);
924 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * REVY(j)].face, NO_FACE,
927 glTranslatef(0, 0, S1);
929 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * REVY(j)].face,
930 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
932 for (i = 1; i < MAXSIZEX - 1; i++) {
933 glTranslatef(1, 0, -SZ);
935 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * j].face, NO_FACE,
939 glTranslatef(0, 0, SZ);
941 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * REVY(j)].face,
945 glTranslatef(S1, 0, -SZ);
947 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * j].face, NO_FACE,
948 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face,
950 for (k = 1; k < MAXSIZEZ - 1; k++) {
951 glTranslatef(0, 0, S1);
954 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * REVY(j)].face,
957 glTranslatef(0, 0, S1);
959 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * REVY(j)].face,
960 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face,
964 if (slice.depth == 0)
965 glRotatef(rotatestep, 0, HALFY, 0);
967 glTranslatef(-HALFX, HALFY, -HALFZ);
969 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * LASTY].face, NO_FACE,
970 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
971 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face);
972 for (k = 1; k < MAXSIZEZ - 1; k++) {
973 glTranslatef(0, 0, S1);
976 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * FIRSTY].face, NO_FACE,
977 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * k].face);
979 glTranslatef(0, 0, S1);
981 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * FIRSTY].face,
982 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
983 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * LASTZ].face);
984 for (i = 1; i < MAXSIZEX - 1; i++) {
985 glTranslatef(S1, 0, -SZ);
987 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * LASTY].face, NO_FACE,
989 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * FIRSTZ].face);
990 for (k = 1; k < MAXSIZEZ - 1; k++) {
991 glTranslatef(0, 0, S1);
995 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * k].face);
997 glTranslatef(0, 0, S1);
999 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * FIRSTY].face,
1001 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * LASTZ].face);
1003 glTranslatef(S1, 0, -SZ);
1005 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * LASTY].face, NO_FACE,
1006 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face,
1007 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * FIRSTZ].face);
1008 for (k = 1; k < MAXSIZEZ - 1; k++) {
1009 glTranslatef(0, 0, S1);
1012 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * FIRSTY].face,
1013 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * k].face);
1015 glTranslatef(0, 0, S1);
1017 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * FIRSTY].face,
1018 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face,
1019 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * LASTZ].face);
1021 case LEFT_FACE: /* RIGHT_FACE too */
1022 /* rotatestep is negative because the RIGHT face is the default here */
1024 if (slice.depth == 0)
1025 glRotatef(-rotatestep, HALFX, 0, 0);
1027 glTranslatef(-HALFX, -HALFY, -HALFZ);
1029 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * FIRSTY].face, NO_FACE,
1030 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face, NO_FACE,
1031 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * LASTZ].face, NO_FACE);
1032 for (j = 1; j < MAXSIZEY - 1; j++) {
1033 glTranslatef(0, S1, 0);
1035 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * j].face, NO_FACE,
1036 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
1039 glTranslatef(0, S1, 0);
1041 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * LASTY].face, NO_FACE,
1042 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
1043 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face);
1044 for (k = 1; k < MAXSIZEZ - 1; k++) {
1045 glTranslatef(0, -SY, S1);
1048 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * LASTY].face, NO_FACE,
1049 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
1050 for (j = 1; j < MAXSIZEY - 1; j++) {
1051 glTranslatef(0, S1, 0);
1054 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * REVY(j)].face, NO_FACE,
1057 glTranslatef(0, S1, 0);
1060 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * FIRSTY].face, NO_FACE,
1061 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * k].face);
1063 glTranslatef(0, -SY, S1);
1065 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * LASTY].face,
1066 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * LASTY].face, NO_FACE,
1067 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
1068 for (j = 1; j < MAXSIZEY - 1; j++) {
1069 glTranslatef(0, S1, 0);
1071 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * REVY(j)].face,
1072 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
1075 glTranslatef(0, S1, 0);
1077 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * FIRSTY].face,
1078 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
1079 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * LASTZ].face);
1081 for (i = 1; i < MAXSIZEX - 1; i++) {
1083 if (slice.depth == i)
1084 glRotatef(-rotatestep, HALFX, 0, 0);
1085 glTranslatef(MIDX(i), -HALFY, -HALFZ);
1087 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * FIRSTY].face, NO_FACE,
1089 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * LASTZ].face, NO_FACE);
1090 for (j = 1; j < MAXSIZEY - 1; j++) {
1091 glTranslatef(0, S1, 0);
1093 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * j].face, NO_FACE,
1097 glTranslatef(0, S1, 0);
1099 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * LASTY].face, NO_FACE,
1101 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * FIRSTZ].face);
1102 for (k = 1; k < MAXSIZEZ - 1; k++) {
1103 glTranslatef(0, -SY, S1);
1107 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * REVZ(k)].face, NO_FACE);
1109 glTranslatef(0, SY, 0);
1113 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * k].face);
1115 glTranslatef(0, -SY, S1);
1117 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * LASTY].face,
1119 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * FIRSTZ].face, NO_FACE);
1120 for (j = 1; j < MAXSIZEY - 1; j++) {
1121 glTranslatef(0, S1, 0);
1123 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * REVY(j)].face,
1127 glTranslatef(0, S1, 0);
1129 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * FIRSTY].face,
1131 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * LASTZ].face);
1134 if (slice.depth == MAXSIZEX - 1)
1135 glRotatef(-rotatestep, HALFX, 0, 0);
1136 glTranslatef(HALFX, -HALFY, -HALFZ);
1138 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * FIRSTY].face, NO_FACE,
1139 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * LASTY].face,
1140 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * LASTZ].face, NO_FACE);
1141 for (j = 1; j < MAXSIZEY - 1; j++) {
1142 glTranslatef(0, S1, 0);
1144 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * j].face, NO_FACE,
1145 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face,
1148 glTranslatef(0, S1, 0);
1150 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * LASTY].face, NO_FACE,
1151 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face,
1152 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * FIRSTZ].face);
1153 for (k = 1; k < MAXSIZEZ - 1; k++) {
1154 glTranslatef(0, -SY, S1);
1157 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * LASTY].face,
1158 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
1159 for (j = 1; j < MAXSIZEY - 1; j++) {
1160 glTranslatef(0, S1, 0);
1163 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * REVY(j)].face,
1166 glTranslatef(0, S1, 0);
1169 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * FIRSTY].face,
1170 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * k].face);
1172 glTranslatef(0, -SY, S1);
1174 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * LASTY].face,
1175 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face,
1176 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
1177 for (j = 1; j < MAXSIZEY - 1; j++) {
1178 glTranslatef(0, S1, 0);
1180 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * REVY(j)].face,
1181 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face,
1184 glTranslatef(0, S1, 0);
1186 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * FIRSTY].face,
1187 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face,
1188 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * LASTZ].face);
1190 case FRONT_FACE: /* BACK_FACE too */
1192 if (slice.depth == MAXSIZEZ - 1)
1193 glRotatef(rotatestep, 0, 0, HALFZ);
1195 glTranslatef(-HALFX, -HALFY, -HALFZ);
1197 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * FIRSTY].face, NO_FACE,
1198 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face, NO_FACE,
1199 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * LASTZ].face, NO_FACE);
1200 for (i = 1; i < MAXSIZEX - 1; i++) {
1201 glTranslatef(S1, 0, 0);
1203 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * FIRSTY].face, NO_FACE,
1205 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * LASTZ].face, NO_FACE);
1207 glTranslatef(S1, 0, 0);
1209 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * FIRSTY].face, NO_FACE,
1210 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * LASTY].face,
1211 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * LASTZ].face, NO_FACE);
1212 for (j = 1; j < MAXSIZEY - 1; j++) {
1213 glTranslatef(-SX, S1, 0);
1215 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * j].face, NO_FACE,
1216 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
1218 for (i = 1; i < MAXSIZEX - 1; i++) {
1219 glTranslatef(S1, 0, 0);
1221 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * j].face, NO_FACE,
1225 glTranslatef(S1, 0, 0);
1227 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * j].face, NO_FACE,
1228 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face,
1231 glTranslatef(-SX, S1, 0);
1233 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * LASTY].face, NO_FACE,
1234 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
1235 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face);
1236 for (i = 1; i < MAXSIZEX - 1; i++) {
1237 glTranslatef(S1, 0, 0);
1239 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * LASTY].face, NO_FACE,
1241 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * FIRSTZ].face);
1243 glTranslatef(S1, 0, 0);
1245 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * LASTY].face, NO_FACE,
1246 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face,
1247 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * FIRSTZ].face);
1249 for (k = 1; k < MAXSIZEZ - 1; k++) {
1251 if (slice.depth == REVZ(k))
1252 glRotatef(rotatestep, 0, 0, HALFZ);
1253 glTranslatef(-HALFX, -HALFY, MIDZ(k));
1256 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * LASTY].face, NO_FACE,
1257 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
1258 for (i = 1; i < MAXSIZEX - 1; i++) {
1259 glTranslatef(S1, 0, 0);
1263 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * REVZ(k)].face, NO_FACE);
1265 glTranslatef(S1, 0, 0);
1268 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * LASTY].face,
1269 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
1270 for (j = 1; j < MAXSIZEY - 1; j++) {
1271 glTranslatef(-SX, S1, 0);
1274 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * REVY(j)].face, NO_FACE,
1277 glTranslatef(SX, 0, 0);
1280 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * REVY(j)].face,
1283 glTranslatef(-SX, S1, 0);
1286 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * FIRSTY].face, NO_FACE,
1287 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * k].face);
1288 for (i = 1; i < MAXSIZEX - 1; i++) {
1289 glTranslatef(S1, 0, 0);
1293 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * k].face);
1295 glTranslatef(S1, 0, 0);
1298 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * FIRSTY].face,
1299 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * k].face);
1302 if (slice.depth == 0)
1303 glRotatef(rotatestep, 0, 0, HALFZ);
1304 glTranslatef(-HALFX, -HALFY, HALFZ);
1306 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * LASTY].face,
1307 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * LASTY].face, NO_FACE,
1308 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
1309 for (i = 1; i < MAXSIZEX - 1; i++) {
1310 glTranslatef(S1, 0, 0);
1312 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * LASTY].face,
1314 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * FIRSTZ].face, NO_FACE);
1316 glTranslatef(S1, 0, 0);
1318 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * LASTY].face,
1319 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face,
1320 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
1321 for (j = 1; j < MAXSIZEY - 1; j++) {
1322 glTranslatef(-SX, S1, 0);
1324 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * REVY(j)].face,
1325 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
1327 for (i = 1; i < MAXSIZEX - 1; i++) {
1328 glTranslatef(S1, 0, 0);
1330 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * REVY(j)].face,
1334 glTranslatef(S1, 0, 0);
1336 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * REVY(j)].face,
1337 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face,
1340 glTranslatef(-SX, S1, 0);
1342 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * FIRSTY].face,
1343 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
1344 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * LASTZ].face);
1345 for (i = 1; i < MAXSIZEX - 1; i++) {
1346 glTranslatef(S1, 0, 0);
1348 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * FIRSTY].face,
1350 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * LASTZ].face);
1352 glTranslatef(S1, 0, 0);
1354 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * FIRSTY].face,
1355 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face,
1356 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * LASTZ].face);
1363 /* From David Bagley's xrubik. Used by permission. ;) */
1365 readRC(rubikstruct * rp, int face, int dir, int h, int orient, int size)
1369 sizeOfRow = sizeRow(rp, face);
1370 if (dir == TOP || dir == BOTTOM)
1371 for (g = 0; g < size; g++)
1372 rp->rowLoc[orient][g] =
1373 rp->cubeLoc[face][g * sizeOfRow + h];
1374 else /* dir == RIGHT || dir == LEFT */
1375 for (g = 0; g < size; g++)
1376 rp->rowLoc[orient][g] =
1377 rp->cubeLoc[face][h * sizeOfRow + g];
1381 rotateRC(rubikstruct * rp, int rotate, int orient, int size)
1385 for (g = 0; g < size; g++)
1386 rp->rowLoc[orient][g].rotation =
1387 (rp->rowLoc[orient][g].rotation + rotate) % MAXORIENT;
1391 reverseRC(rubikstruct * rp, int orient, int size)
1396 for (g = 0; g < size / 2; g++) {
1397 temp = rp->rowLoc[orient][size - 1 - g];
1398 rp->rowLoc[orient][size - 1 - g] = rp->rowLoc[orient][g];
1399 rp->rowLoc[orient][g] = temp;
1404 writeRC(rubikstruct * rp, int face, int dir, int h, int orient, int size)
1406 int g, position, sizeOfRow;
1408 sizeOfRow = sizeRow(rp, face);
1409 if (dir == TOP || dir == BOTTOM) {
1410 for (g = 0; g < size; g++) {
1411 position = g * sizeOfRow + h;
1412 rp->cubeLoc[face][position] = rp->rowLoc[orient][g];
1413 /* DrawSquare(face, position); */
1415 } else { /* dir == RIGHT || dir == LEFT */
1416 for (g = 0; g < size; g++) {
1417 position = h * sizeOfRow + g;
1418 rp->cubeLoc[face][position] = rp->rowLoc[orient][g];
1419 /* DrawSquare(face, position); */
1425 rotateFace(rubikstruct * rp, int face, int direction)
1427 int position, i, j, sizeOfRow, sizeOfColumn, sizeOnPlane;
1430 faceSizes(rp, face, &sizeOfRow, &sizeOfColumn);
1431 sizeOnPlane = sizeOfRow * sizeOfColumn;
1432 if ((faceLoc = (RubikLoc *) malloc(sizeOnPlane *
1433 sizeof (RubikLoc))) == NULL) {
1437 for (position = 0; position < sizeOnPlane; position++)
1438 faceLoc[position] = rp->cubeLoc[face][position];
1440 for (position = 0; position < sizeOnPlane; position++) {
1441 i = position % sizeOfRow;
1442 j = position / sizeOfRow;
1443 if (direction == CW)
1444 rp->cubeLoc[face][position] =
1445 faceLoc[(sizeOfRow - i - 1) * sizeOfRow + j];
1446 else if (direction == CCW)
1447 rp->cubeLoc[face][position] =
1448 faceLoc[i * sizeOfRow + sizeOfColumn - j - 1];
1449 else /* (direction == HALF) */
1450 rp->cubeLoc[face][position] =
1451 faceLoc[sizeOfRow - i - 1 + (sizeOfColumn - j - 1) * sizeOfRow];
1452 rp->cubeLoc[face][position].rotation =
1453 (rp->cubeLoc[face][position].rotation +
1454 direction - MAXORIENT) % MAXORIENT;
1455 /* DrawSquare(face, position); */
1457 if (faceLoc != NULL)
1458 (void) free((void *) faceLoc);
1462 /* Yeah this is big and ugly */
1464 slideRC(int face, int direction, int h, int sizeOnOppAxis,
1465 int *newFace, int *newDirection, int *newH,
1466 int *rotate, Bool *reverse)
1468 *newFace = slideNextRow[face][direction].face;
1469 *rotate = slideNextRow[face][direction].rotation;
1470 *newDirection = (*rotate + direction) % MAXORIENT;
1477 if (*newDirection == TOP || *newDirection == BOTTOM) {
1478 *newH = sizeOnOppAxis - 1 - h;
1480 } else { /* *newDirection == RIGHT || *newDirection == LEFT */
1486 *newH = sizeOnOppAxis - 1 - h;
1490 if (*newDirection == TOP || *newDirection == BOTTOM) {
1493 } else { /* *newDirection == RIGHT || *newDirection == LEFT */
1494 *newH = sizeOnOppAxis - 1 - h;
1499 (void) printf("slideRC: rotate %d\n", *rotate);
1506 moveRubik(rubikstruct * rp, int face, int direction, int position)
1508 int newFace, newDirection, rotate, reverse;
1510 int i, j, sizeOfRow, sizeOfColumn, sizeOnAxis, sizeOnOppAxis;
1512 faceSizes(rp, face, &sizeOfRow, &sizeOfColumn);
1513 if (direction == CW || direction == CCW) {
1514 direction = (direction == CCW) ?
1515 (rotateToRow[face].direction + 2) % MAXORIENT :
1516 rotateToRow[face].direction;
1517 if (rotateToRow[face].sideFace == RIGHT) {
1518 i = j = sizeOfColumn - 1;
1519 } else if (rotateToRow[face].sideFace == BOTTOM) {
1520 i = j = sizeOfRow - 1;
1524 face = rotateToRow[face].face;
1525 position = j * sizeOfRow + i;
1527 i = position % sizeOfRow;
1528 j = position / sizeOfRow;
1529 h = (direction == TOP || direction == BOTTOM) ? i : j;
1530 if (direction == TOP || direction == BOTTOM) {
1531 sizeOnAxis = sizeOfColumn;
1532 sizeOnOppAxis = sizeOfRow;
1534 sizeOnAxis = sizeOfRow;
1535 sizeOnOppAxis = sizeOfColumn;
1537 /* rotate sides CW or CCW or HALF) */
1539 if (h == sizeOnOppAxis - 1) {
1540 newDirection = (direction == TOP || direction == BOTTOM) ?
1542 if (rp->degreeTurn == 180) {
1543 if (!rotateFace(rp, rowToRotate[face][newDirection], HALF))
1545 } else if (direction == TOP || direction == RIGHT) {
1546 if (!rotateFace(rp, rowToRotate[face][newDirection], CW))
1548 } else { /* direction == BOTTOM || direction == LEFT */
1549 if (!rotateFace(rp, rowToRotate[face][newDirection], CCW))
1554 newDirection = (direction == TOP || direction == BOTTOM) ?
1556 if (rp->degreeTurn == 180) {
1557 if (!rotateFace(rp, rowToRotate[face][newDirection], HALF))
1559 } else if (direction == TOP || direction == RIGHT) {
1560 if (!rotateFace(rp, rowToRotate[face][newDirection], CCW))
1562 } else { /* direction == BOTTOM || direction == LEFT */
1563 if (!rotateFace(rp, rowToRotate[face][newDirection], CW))
1567 /* Slide rows or columns */
1568 readRC(rp, face, direction, h, 0, sizeOnAxis);
1569 if (rp->degreeTurn == 180) {
1570 int sizeOnDepthAxis;
1572 slideRC(face, direction, h, sizeOnOppAxis,
1573 &newFace, &newDirection, &newH, &rotate, &reverse);
1574 sizeOnDepthAxis = sizeFace(rp, newFace) / sizeOnOppAxis;
1575 readRC(rp, newFace, newDirection, newH, 1, sizeOnDepthAxis);
1576 rotateRC(rp, rotate, 0, sizeOnAxis);
1577 if (reverse == True)
1578 reverseRC(rp, 0, sizeOnAxis);
1580 direction = newDirection;
1582 for (k = 2; k <= MAXORIENT + 1; k++) {
1583 slideRC(face, direction, h, sizeOnOppAxis,
1584 &newFace, &newDirection, &newH, &rotate, &reverse);
1585 if (k != MAXORIENT && k != MAXORIENT + 1)
1586 readRC(rp, newFace, newDirection, newH, k,
1587 (k % 2) ? sizeOnDepthAxis : sizeOnAxis);
1588 rotateRC(rp, rotate, k - 2,
1589 (k % 2) ? sizeOnDepthAxis : sizeOnAxis);
1590 if (k != MAXORIENT + 1)
1591 rotateRC(rp, rotate, k - 1,
1592 (k % 2) ? sizeOnAxis : sizeOnDepthAxis);
1593 if (reverse == True) {
1594 reverseRC(rp, k - 2,
1595 (k % 2) ? sizeOnDepthAxis : sizeOnAxis);
1596 if (k != MAXORIENT + 1)
1597 reverseRC(rp, k - 1,
1598 (k % 2) ? sizeOnAxis : sizeOnDepthAxis);
1600 writeRC(rp, newFace, newDirection, newH, k - 2,
1601 (k % 2) ? sizeOnDepthAxis : sizeOnAxis);
1603 direction = newDirection;
1607 for (k = 1; k <= MAXORIENT; k++) {
1608 slideRC(face, direction, h, sizeOnOppAxis,
1609 &newFace, &newDirection, &newH, &rotate, &reverse);
1611 readRC(rp, newFace, newDirection, newH, k, sizeOnAxis);
1612 rotateRC(rp, rotate, k - 1, sizeOnAxis);
1613 if (reverse == True)
1614 reverseRC(rp, k - 1, sizeOnAxis);
1615 writeRC(rp, newFace, newDirection, newH, k - 1, sizeOnAxis);
1617 direction = newDirection;
1626 printCube(rubikstruct * rp)
1628 int face, position, sizeOfRow, sizeOfColumn;
1630 for (face = 0; face < MAXFACES; face++) {
1631 faceSizes(rp, face, &sizeOfRow, &sizeOfColumn);
1632 for (position = 0; position < sizeOfRow * sizeOfColumn; position++) {
1633 (void) printf("%d %d ", rp->cubeLoc[face][position].face,
1634 rp->cubeLoc[face][position].rotation);
1635 if (!((position + 1) % sizeOfRow))
1636 (void) printf("\n");
1638 (void) printf("\n");
1640 (void) printf("\n");
1646 evalmovement(ModeInfo * mi, RubikMove movement)
1648 rubikstruct *rp = &rubik[MI_SCREEN(mi)];
1653 if (movement.face < 0 || movement.face >= MAXFACES)
1655 if (!moveRubik(rp, movement.face, movement.direction, movement.position))
1661 compare_moves(rubikstruct * rp, RubikMove move1, RubikMove move2, Bool opp)
1663 RubikSlice slice1, slice2;
1665 convertMove(rp, move1, &slice1);
1666 convertMove(rp, move2, &slice2);
1667 if (slice1.face == slice2.face &&
1668 slice1.depth == slice2.depth) {
1669 if (slice1.rotation == slice2.rotation) { /* CW or CCW */
1681 shuffle(ModeInfo * mi)
1683 rubikstruct *rp = &rubik[MI_SCREEN(mi)];
1684 int i, face, position;
1692 i = NRAND(-i - MINSIZE + 1) + MINSIZE;
1693 else if (i < MINSIZE)
1696 if (LRAND() % 2 && !sizey && !sizez) { /* Make normal (NxNxN) cubes more likely */
1697 MAXSIZEX = MAXSIZEY = MAXSIZEZ = i;
1705 i = NRAND(-i - MINSIZE + 1) + MINSIZE;
1706 else if (i < MINSIZE)
1708 if (LRAND() % 2 && !sizez) { /* Make more MxNxN more likely than LxMxN */
1709 MAXSIZEY = MAXSIZEZ = i;
1717 i = NRAND(-i - MINSIZE + 1) + MINSIZE;
1718 else if (i < MINSIZE)
1724 for (face = 0; face < MAXFACES; face++) {
1725 if (rp->cubeLoc[face] != NULL)
1726 (void) free((void *) rp->cubeLoc[face]);
1727 if ((rp->cubeLoc[face] = (RubikLoc *) malloc(sizeFace(rp, face) *
1728 sizeof (RubikLoc))) == NULL) {
1731 for (position = 0; position < sizeFace(rp, face); position++) {
1732 rp->cubeLoc[face][position].face = face;
1733 rp->cubeLoc[face][position].rotation = TOP;
1736 for (i = 0; i < MAXORIENT; i++) {
1737 if (rp->rowLoc[i] != NULL)
1738 (void) free((void *) rp->rowLoc[i]);
1739 /* The following is reused so make it the biggest size */
1740 if ((rp->rowLoc[i] = (RubikLoc *) malloc(MAXMAXSIZE *
1741 sizeof (RubikLoc))) == NULL) {
1745 rp->storedmoves = MI_COUNT(mi);
1746 if (rp->storedmoves < 0) {
1747 if (rp->moves != NULL)
1748 (void) free((void *) rp->moves);
1749 rp->moves = (RubikMove *) NULL;
1750 rp->storedmoves = NRAND(-rp->storedmoves) + 1;
1752 if ((rp->storedmoves) && (rp->moves == NULL))
1753 if ((rp->moves = (RubikMove *) calloc(rp->storedmoves + 1,
1754 sizeof (RubikMove))) == NULL) {
1757 if (MI_CYCLES(mi) <= 1) {
1758 rp->anglestep = 90.0;
1760 rp->anglestep = 90.0 / (GLfloat) (MI_CYCLES(mi));
1763 for (i = 0; i < rp->storedmoves; i++) {
1767 move.face = NRAND(MAXFACES);
1768 move.direction = NRAND(MAXORIENT); /* Exclude CW and CCW, its ok */
1769 move.position = NRAND(sizeFace(rp, move.face));
1770 rp->degreeTurn = (checkFaceSquare(rp,
1771 rowToRotate[move.face][move.direction])) ? 90 : 180;
1773 if (i > 0) { /* avoid immediate undoing moves */
1774 if (compare_moves(rp, move, rp->moves[i - 1], True))
1776 if (rp->degreeTurn == 180 &&
1777 compare_moves(rp, move, rp->moves[i - 1], False))
1780 if (i > 1) /* avoid 3 consecutive identical moves */
1781 if (compare_moves(rp, move, rp->moves[i - 1], False) &&
1782 compare_moves(rp, move, rp->moves[i - 2], False))
1785 * Still some silly moves being made....
1787 } while (!condition);
1789 if (!evalmovement(mi, move))
1791 rp->moves[i] = move;
1794 if (NRAND(100) < 50)
1797 if (NRAND(100) < 50)
1799 rp->movement.face = NO_FACE;
1801 rp->action = hideshuffling ? ACTION_SOLVE : ACTION_SHUFFLE;
1802 rp->shufflingmoves = 0;
1808 reshape(ModeInfo * mi, int width, int height)
1810 rubikstruct *rp = &rubik[MI_SCREEN(mi)];
1812 glViewport(0, 0, rp->WindW = (GLint) width, rp->WindH = (GLint) height);
1813 glMatrixMode(GL_PROJECTION);
1815 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);
1816 glMatrixMode(GL_MODELVIEW);
1821 pinit(ModeInfo * mi)
1824 glClearColor(0.0, 0.0, 0.0, 1.0);
1825 glColor3f(1.0, 1.0, 1.0);
1827 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
1828 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
1829 glLightfv(GL_LIGHT0, GL_POSITION, position0);
1830 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
1831 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
1832 glLightfv(GL_LIGHT1, GL_POSITION, position1);
1833 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
1834 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
1835 glEnable(GL_LIGHTING);
1836 glEnable(GL_LIGHT0);
1837 glEnable(GL_LIGHT1);
1838 glEnable(GL_DEPTH_TEST);
1839 glEnable(GL_NORMALIZE);
1840 glEnable(GL_CULL_FACE);
1842 glShadeModel(GL_FLAT);
1843 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
1844 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
1846 return (shuffle(mi));
1850 free_rubik(rubikstruct *rp)
1854 for (i = 0; i < MAXFACES; i++)
1855 if (rp->cubeLoc[i] != NULL) {
1856 (void) free((void *) rp->cubeLoc[i]);
1857 rp->cubeLoc[i] = (RubikLoc *) NULL;
1859 for (i = 0; i < MAXORIENT; i++)
1860 if (rp->rowLoc[i] != NULL) {
1861 (void) free((void *) rp->rowLoc[i]);
1862 rp->rowLoc[i] = (RubikLoc *) NULL;
1864 if (rp->moves != NULL) {
1865 (void) free((void *) rp->moves);
1866 rp->moves = (RubikMove *) NULL;
1871 release_rubik(ModeInfo * mi)
1873 if (rubik != NULL) {
1876 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
1877 rubikstruct *rp = &rubik[screen];
1881 (void) free((void *) rubik);
1882 rubik = (rubikstruct *) NULL;
1888 init_rubik(ModeInfo * mi)
1892 if (rubik == NULL) {
1893 if ((rubik = (rubikstruct *) calloc(MI_NUM_SCREENS(mi),
1894 sizeof (rubikstruct))) == NULL)
1897 rp = &rubik[MI_SCREEN(mi)];
1898 rp->step = NRAND(90);
1899 rp->PX = ((float) LRAND() / (float) MAXRAND) * 2.0 - 1.0;
1900 rp->PY = ((float) LRAND() / (float) MAXRAND) * 2.0 - 1.0;
1902 if ((rp->glx_context = init_GL(mi)) != NULL) {
1904 reshape(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
1905 glDrawBuffer(GL_BACK);
1908 if (MI_IS_VERBOSE(mi)) {
1909 (void) fprintf(stderr,
1910 "Could not allocate memory for rubik\n");
1920 draw_rubik(ModeInfo * mi)
1922 Bool bounced = False;
1923 Display *display = MI_DISPLAY(mi);
1924 Window window = MI_WINDOW(mi);
1929 rp = &rubik[MI_SCREEN(mi)];
1930 if (rp->cubeLoc[0] == NULL)
1933 MI_IS_DRAWN(mi) = True;
1934 if (!rp->glx_context)
1937 glXMakeCurrent(display, window, *(rp->glx_context));
1939 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1943 glTranslatef(0.0, 0.0, -10.0);
1949 rp->PY += (-1) - (rp->PY);
1954 rp->PY -= (rp->PY) - 1;
1959 rp->PX += (-1) - (rp->PX);
1964 rp->PX -= (rp->PX) - 1;
1969 rp->VX += ((float) LRAND() / (float) MAXRAND) * 0.02 - 0.01;
1970 rp->VX += ((float) LRAND() / (float) MAXRAND) * 0.02 - 0.01;
1980 if (!MI_IS_ICONIC(mi)) {
1981 glTranslatef(rp->PX, rp->PY, 0);
1982 glScalef(Scale4Window * rp->WindH / rp->WindW, Scale4Window, Scale4Window);
1984 glScalef(Scale4Iconic * rp->WindH / rp->WindW, Scale4Iconic, Scale4Iconic);
1987 glRotatef(rp->step * 100, 1, 0, 0);
1988 glRotatef(rp->step * 95, 0, 1, 0);
1989 glRotatef(rp->step * 90, 0, 0, 1);
1991 if (!draw_cube(mi)) {
1995 if (MI_IS_FPS(mi)) do_fps (mi);
1996 glXSwapBuffers(display, window);
1998 if (rp->action == ACTION_SHUFFLE) {
2000 if (++rp->rotatestep > DELAY_AFTER_SHUFFLING) {
2001 rp->movement.face = NO_FACE;
2003 rp->action = ACTION_SOLVE;
2007 if (rp->movement.face == NO_FACE) {
2008 if (rp->shufflingmoves < rp->storedmoves) {
2010 rp->movement = rp->moves[rp->shufflingmoves];
2016 if (rp->rotatestep == 0) {
2017 if (rp->movement.direction == CW || rp->movement.direction == CCW)
2018 rp->degreeTurn = (checkFaceSquare(rp, rp->movement.face)) ? 90 : 180;
2020 rp->degreeTurn = (checkFaceSquare(rp, rowToRotate[rp->movement.face][rp->movement.direction])) ? 90 : 180;
2022 rp->rotatestep += rp->anglestep;
2023 if (rp->rotatestep > rp->degreeTurn) {
2024 if (!evalmovement(mi, rp->movement)) {
2026 if (MI_IS_VERBOSE(mi)) {
2027 (void) fprintf(stderr,
2028 "Could not allocate memory for rubik\n");
2032 rp->shufflingmoves++;
2033 rp->movement.face = NO_FACE;
2039 if (++rp->rotatestep > DELAY_AFTER_SOLVING)
2042 if (MI_IS_VERBOSE(mi)) {
2043 (void) fprintf(stderr,
2044 "Could not allocate memory for rubik\n");
2049 if (rp->movement.face == NO_FACE) {
2050 if (rp->storedmoves > 0) {
2052 rp->movement = rp->moves[rp->storedmoves - 1];
2053 rp->movement.direction = (rp->movement.direction +
2054 (MAXORIENT / 2)) % MAXORIENT;
2060 if (rp->rotatestep == 0) {
2061 if (rp->movement.direction == CW || rp->movement.direction == CCW)
2062 rp->degreeTurn = (checkFaceSquare(rp, rp->movement.face)) ? 90 : 180;
2064 rp->degreeTurn = (checkFaceSquare(rp, rowToRotate[rp->movement.face][rp->movement.direction])) ? 90 : 180;
2066 rp->rotatestep += rp->anglestep;
2067 if (rp->rotatestep > rp->degreeTurn) {
2068 if (!evalmovement(mi, rp->movement)) {
2070 if (MI_IS_VERBOSE(mi)) {
2071 (void) fprintf(stderr,
2072 "Could not allocate memory for rubik\n");
2077 rp->movement.face = NO_FACE;
2091 change_rubik(ModeInfo * mi)
2097 rp = &rubik[MI_SCREEN(mi)];
2099 if (!rp->glx_context)
2103 if (MI_IS_VERBOSE(mi)) {
2104 (void) fprintf(stderr,
2105 "Could not allocate memory for rubik\n");