#ifndef _global_h # include "global.h" #endif #ifndef _puzzle_h # include "puzzle.H" #endif Grid::Grid(int w, int h) { width = w; height = h; p = new Vec2[width*height]; } Grid::~Grid() { delete [] p; } void Grid::Reset(int x, int y) { P(x,y) = Vec2( x*max_width/(width-1), y*max_height/(height-1) ); } void Grid::Init(int maxx, int maxy) { int x,y; max_width = maxx; max_height = maxy; for (x=0;xInit(img_width,img_height); g->Randomize(distortion); if (sfx) { int dir,len; const char *sfx_p=sfx; while( *sfx_p ) { if (sscanf( sfx_p, "%02x%02x%01x%01x", &x, &y, &len, &dir )!=4) { fprintf( stderr, "*** image error in extension 0x12\n" ); exit(0); } g->Reset(x,y); while( len-- ) { switch(dir) { case 0: x++; break; case 1: y++; break; case 2: x--; break; case 3: y--; break; } g->Reset(x,y); } sfx_p+=6; } } // create and initialize setup-structures for all pieces pfs = new PieceFrameSetup[width*height]; pos = new Vec2[width*height]; // setup corner information for (x=0;xP(x,y),g->P(x+1,y),g->P(x+1,y+1),g->P(x,y+1)); pos[x+width*y]=g->P(x,y)+Vec2(offx*(x+1)+img_width/dx/2,offy*(y+1)+img_height/dy/2); } } // setup pin information for (x=0;x=0) { int row=y; int col=x; // query the offset of the center from the grid edge in the original piece Vec2 org_offset(P(x,y).Center()-Vec2(x*img_width/width,y*img_height/height)); // compute the piece edge in the 'straight' position Vec2 new_pos( col*img_width/width+col*straight_setup, row*img_height/height+row*straight_setup ); P(x,y).SetPos( new_pos+org_offset ); } else { P(x,y).SetPos(pos[x+y*width]); } if (shuffle&1) P(x,y).SetDir( ((rand()%(int)(maxang*2))-maxang) + (rand()%4)*90 ); else P(x,y).SetDir(angle*(x+y)); if (side_lock<0) { if (rand()&2) P(x,y).FlipPage(); } else { if (side_lock==1) P(x,y).FlipPage(); } stk->Append( &P(x,y) ); } } delete [] pos; delete [] pfs; #undef PFS } void Puzzle::Redraw() { for (int i=0;iRedraw(); } int Puzzle::CheckForJoin( Piece *pi, int depth ) { int i,j,s; for (i=0;iCheckForJoin(pi)) { if ((s=p[i]->FindStartForJoin(pi))>=0) { // start double buffering p[i]->StoreExtent(); pi->JoinExtent(); p[i]->DoJoin(pi,s,(depth>0)?((p[i]->join_count>=pi->join_count)?0:1):0); // The part was join, so it can be deleted ... for (j=width*height-1;j>=0;j--) { if (p[j]==pi) break; } if (j>=0) DropTile(j); p[i]->UpdateExtent(); if (depthRaise(&P(x,y)); Vec2 dir((rand()%7)-3,(rand()%7)-3); for (int j=0;j<18;j++) { P(x,y).MoveTurn(P(x,y).GetPos()+dir,P(x,y).GetDir()+5.0); if (quit) return; } } #endif }