1 /* -*- c++ -*-
2 FILE: Macro.cpp
3 RCS REVISION: $Revision: 1.18 $
4 
5 COPYRIGHT: (c) 1999 -- 2003 Melinda Green, Don Hatch, and Jay Berkenbilt - Superliminal Software
6 
7 LICENSE: Free to use and modify for non-commercial purposes as long as the
8     following conditions are adhered to:
9     1) Obvious credit for the source of this code and the designs it embodies
10        are clearly made, and
11     2) Ports and derived versions of 4D Magic Cube programs are not distributed
12        without the express written permission of the authors.
13 
14 DESCRIPTION:
15     Implementation of the Macro class
16 */
17 
18 #include "Macro.h"
19 
20 #include "History.h"
21 #include "Math4d.h"
22 #include "Polymgr.h"
23 #include "Preferences.h"
24 
25 static char *
Strdup(char * s)26 Strdup(char *s)
27 {
28     char *t;
29     if (!s || !(t = new char[strlen(s) + 1]))
30         return NULL;
31     return strcpy(t, s);
32 }
33 
Macro(char * name,int nrefs,int refs[MAXREFS][4],Preferences & prefs,PolygonManager4D * polymgr)34 Macro::Macro(char *name, int nrefs, int refs[MAXREFS][4],
35              Preferences& prefs, PolygonManager4D* polymgr) :
36     name(Strdup(name)),
37     polymgr(polymgr)
38 {
39     assert(nrefs <= MAXREFS);
40     this->setRefs(nrefs, refs);
41     this->moves = new History(prefs, polymgr);
42 }
43 
~Macro()44 Macro::~Macro()
45 {
46     delete this->name;
47     delete this->moves;
48 }
49 
50 void
setUIData(void * ui_data)51 Macro::setUIData(void* ui_data)
52 {
53     this->ui_data = ui_data;
54 }
55 
56 void*
getUIData()57 Macro::getUIData()
58 {
59     return this->ui_data;
60 }
61 
62 void
setName(char * name)63 Macro::setName(char *name)
64 {
65     delete [] this->name;
66     this->name = Strdup(name);
67 }
68 
69 
70 void
goToBeginning()71 Macro::goToBeginning()
72 {
73     this->moves->goToBeginning();
74 }
75 
76 void
goToEnd()77 Macro::goToEnd()
78 {
79     this->moves->goToEnd();
80 }
81 
82 void
setRefs(int nrefs,int refs[MAXREFS][4])83 Macro::setRefs(int nrefs, int refs[MAXREFS][4])
84 {
85     assert(nrefs <= MAXREFS);
86     this->nrefs = nrefs;
87     int j;
88     for (j = 0; j < nrefs; ++j)
89         SET4(this->refs[j], refs[j]);
90 }
91 
92 void
addMove(struct stickerspec * sticker,int direction,int slicesmask)93 Macro::addMove(struct stickerspec *sticker, int direction, int slicesmask)
94 {
95     this->moves->apply(sticker, direction, slicesmask);
96 }
97 
98 // Return true on success, false if at end of the macro
99 bool
getMove(int dir_in,int det,int mat[4][4],struct stickerspec * grip,int * dir_out,int * slicesmask)100 Macro::getMove(int dir_in, int det, int mat[4][4],
101                struct stickerspec* grip,
102                int* dir_out, int* slicesmask)
103 {
104     bool status = false;
105 
106     if (dir_in < 0)
107         status = this->moves->undo(grip, dir_out, slicesmask) == 1;
108     else
109         status = this->moves->redo(grip, dir_out, slicesmask) == 1;
110     if (status)
111     {
112         VXM4i(grip->coords, grip->coords, mat);
113         polymgr->fillStickerspecFromCoordsAndLength(grip, 3);
114         *dir_out *= det;
115     }
116     return status;
117 }
118 
119 char *
getName()120 Macro::getName()
121 {
122     return this->name;
123 }
124 
125 bool
get4dMatThatRotatesThese3ToMy3(int ref0[4],int ref1[4],int ref2[4],int mat[4][4])126 Macro::get4dMatThatRotatesThese3ToMy3(int ref0[4], int ref1[4], int ref2[4],
127                                       int mat[4][4])
128 {
129     return (Math4d::get4dMatThatRotatesThese3ToThose3
130             (this->refs[0], this->refs[1], this->refs[2],
131              ref0, ref1, ref2, mat) == 1);
132 }
133 
134 void
dump(FILE * fp)135 Macro::dump(FILE *fp)
136 {
137     struct stickerspec sticker;
138     int i;
139     fprintf(fp, "@%s@(", name);
140     for (i = 0; i < nrefs; ++i)
141     {
142         SET4(sticker.coords, refs[i]);
143         polymgr->fillStickerspecFromCoords(&sticker);
144         fprintf(fp, "%d%d", sticker.face, sticker.id_within_face);
145         if (i + 1 < nrefs)
146             fprintf(fp, " ");
147     }
148     fprintf(fp, ") ");
149     moves->dump(fp);
150 }
151 
152 bool
readHistory(FILE * fp)153 Macro::readHistory(FILE* fp)
154 {
155     return this->moves->read(fp) == 1;
156 }
157 
158 // Local Variables:
159 // c-basic-offset: 4
160 // c-comment-only-line-offset: 0
161 // c-file-offsets: ((defun-block-intro . +) (block-open . 0) (substatement-open . 0) (statement-cont . +) (statement-case-open . +4) (arglist-intro . +) (arglist-close . +) (inline-open . 0))
162 // indent-tabs-mode: nil
163 // End:
164