#include "aliens.h" using std::vector; Aliens::Aliens(int TmpDestx, int TmpDesty, int afw, int afh, int alcellsize) { cellsize = alcellsize; DestPoint.setX(TmpDestx); DestPoint.setY(TmpDesty); fw = afw; fh = afh; AliensCnt = -1; CurWave = 0; Cells = new int*[fw]; TmpCells = new int*[fw]; for (int i = 0; i < fw; i++) { Cells[i] = new int[fh]; TmpCells[i] = new int[fh]; } clearCells(); } void Aliens::clearCells() { for (int i = 0; i < fw; i++) for (int j = 0; j < fh; j++) Cells[i][j] = 0; } bool Aliens::AddAlien() { bool ok; Alien al; AliensCnt++; //if (CurWave < 0) CurWave = 0; //if (AliensCnt > curAliens.size()) ReDim Preserve Aliens(AliensCnt) As Alien //Aliens(AliensCnt) = SrcAliens(CurWave) curAliens.push_back(al); //srcAliens[CurWave]); // curAliens[AliensCnt].Level = CurWave + 1; curAliens[AliensCnt].Position.pnt.setX(0); curAliens[AliensCnt].Position.pnt.setY(fh/2); //(int)(fh/2+(1-0.58)*6); DestPoint.setX(fw - 1); DestPoint.setY(fh / 2); //curAliens[AliensCnt].Position.pnt.y(); curAliens[AliensCnt].DestPnt = DestPoint; // curAliens[AliensCnt].MaxFrame = 0; //srcAliens[CurWave].MaxFrame; // curAliens[AliensCnt].PicFrame = 0; for (int i = 0; i < fw; i++) { for (int j = 0; j < fh; j++) TmpCells[i][j] = Cells[i][j]; } ok = CreatePath(&curAliens[AliensCnt]); return ok; } bool Aliens::CreatePath(Alien * al) { QPointF tp; deque tmpPnt; //tmpPnt.clear(); // AliensTmpDestX = fw - 1;//al->Destpnt.x(); // AliensTmpDestY = fh / 2;//al->Destpnt.y(); // al->Position.pnt.x() = 0; // al->Position.pnt.y() = fh / 2; if (WaveTrace(al)) { for (int j=0; j<4; j++) { tmpPnt.clear(); tp = al->path[0]; tmpPnt.push_back(tp); for (unsigned int i = 0; i < al->path.size() - 1; i++) { tp.setX((al->path[i].x() + al->path[i + 1].x()) / 2); tp.setY((al->path[i].y() + al->path[i + 1].y()) / 2); tmpPnt.push_back(tp); } tp = al->path[al->path.size() - 1]; tmpPnt.push_back(tp); al->path.swap(tmpPnt); al->Position.pnt.setX(al->path.at(0).x()*cellsize); al->Position.pnt.setY(al->path.at(0).y()*cellsize); } int i = 0; tmpPnt.clear(); while(1) { tp = al->path[0]; break; } return true; } return false; //return WaveTrace(al); } /*bool Aliens::PathIntersect(Alien* Al, Rectangle rect) { //PathIntersect = False for (int i = Al->PathIndex; i<=Al->path.size(); i++) // To UBound(Al.path(), 1) if (Al->path[i].x + 0.5 >= rect.x0 - 1 && Al->path[i].x + 0.5 <= rect.x1 + 1) if (Al->path[i].y + 0.5 >= rect.y0 - 1 && Al->path[i].y + 0.5 <= rect.y1 + 1) return true; //PathIntersect = True //Exit Function return false; }*/ bool Aliens::WaveTrace(Alien * al) { bool stop = false; int step = 2; QPoint cp, tp; QRect fr(0, 0, fw, fh); vector tmpp, curp; cp = al->Position.pnt; curp.push_back(cp); TmpCells[cp.x()][cp.y()] = 1; while (!stop) { tmpp = curp; curp.clear(); stop = true; for (unsigned int i = 0; i < tmpp.size(); i++) { cp = tmpp[i]; if (cp == al->DestPnt) { TmpCells[cp.x()][cp.y()] = step; qDebug() << "true"; InvWaveTrace(cp, step, al); qDebug() << al->path.size(); return true; } tp.setX(cp.x() - 1); tp.setY(cp.y()); if (fr.contains(tp) && TmpCells[tp.x()][tp.y()] == 0) { TmpCells[tp.x()][tp.y()] = step; curp.push_back(tp); stop = false; } tp.setX(cp.x() + 1); if (fr.contains(tp) && TmpCells[tp.x()][tp.y()] == 0) { TmpCells[tp.x()][tp.y()] = step; curp.push_back(tp); stop = false; } tp.setX(cp.x()); tp.setY(cp.y() - 1); if (fr.contains(tp) && TmpCells[tp.x()][tp.y()] == 0) { TmpCells[tp.x()][tp.y()] = step; curp.push_back(tp); stop = false; } tp.setY(cp.y() + 1); if (fr.contains(tp) && TmpCells[tp.x()][tp.y()] == 0) { TmpCells[tp.x()][tp.y()] = step; curp.push_back(tp); stop = false; } } step++; } qDebug() << "false"; return false; } void Aliens::InvWaveTrace(QPoint cp, int cnt, Alien * al) { QPoint wp, Ppnt; int Ind, c, AliensTmpDestX, AliensTmpDestY, xpp, ypp, xnn, ynn; unsigned char chk; Ppnt = wp = cp; xnn=0; xpp=0; ynn=0; ypp=0; cnt--; al->path.push_front(Ppnt); AliensTmpDestX = al->Position.pnt.x(); AliensTmpDestY = al->Position.pnt.y(); while (cnt > 1) { cnt--; chk = 0; Ind = 0; c = 0; if (wp.x() - 1 >= 0 && TmpCells[wp.x()-1][wp.y()] == cnt) { chk = chk | 0x01; c++; } if (wp.x() + 1 < fw && TmpCells[wp.x()+1][wp.y()] == cnt) { chk = chk | 0x02; c++; } if (wp.y() - 1 >= 0 && TmpCells[wp.x()][wp.y()-1] == cnt) { chk = chk | 0x04; c++; } if (wp.y() + 1 < fh && TmpCells[wp.x()][wp.y()+1] == cnt) { chk = chk | 0x08; c++; } if (c == 0 || chk == 0) qDebug() << "ERROR!!!"; if (c > 1) { if ((chk & 0x01)==0x01 && (chk & 0x04)==0x04) { if (xnn <= ynn && Ind == 0){ wp.rx()--; xnn++; if (xnn == ynn) xnn++; Ind = 1; } else if (Ind == 0) { wp.ry()--; ynn++; ynn++; Ind = 1; } } if ((chk & 0x02)==0x02 && (chk & 0x04)==0x04) { if (xpp <= ynn && Ind == 0){ wp.rx()++; xpp++; if (xpp == ynn) xpp++; Ind = 1; } else if (Ind == 0) { wp.ry()--; ynn++; ynn++; Ind = 1; } } if ((chk & 0x01)==0x01 && (chk & 0x08)==0x08) { if (xnn <= ypp && Ind == 0){ wp.rx()--; xnn++; if (xnn == ypp) xnn++; Ind = 1; } else if (Ind == 0) { wp.ry()++; ypp++; ypp++; Ind = 1; } } if ((chk & 0x02)==0x02 && (chk & 0x08)==0x08) { if (xpp <= ypp && Ind == 0){ wp.rx()++; xpp++; if (xpp == ypp) xpp++; Ind = 1; } else if (Ind == 0) { wp.ry()++; ypp++; ypp++; Ind = 1; } } } if (c == 1 || Ind == 0) { xnn=0; xpp=0; ynn=0; ypp=0; if ((chk & 0x01)==0x01) { wp.rx()--; xnn++; } else if ((chk & 0x02)==0x02) { wp.rx()++; xpp++; } else if ((chk & 0x04)==0x04) { wp.ry()--; ynn++; } else if ((chk & 0x08)==0x08) { wp.ry()++; ypp++; } } Ppnt = wp; al->path.push_front(Ppnt); } } void Aliens::clearAliens() { AliensCnt = - 1; curAliens.clear(); }