#pragma warning (disable:4786) #include #include "map.hpp" #include "move.hpp" #include "movecell.hpp" float pathmapcell[MAPCELLSIZE][MAPCELLSIZE]; FixedHeap CALCCELLELEMENT::m_EmptyCalcCellElement(1); void InitMoveCellData (void) { } void CALCCELLELEMENT::operator =(FibHeapNode& RHS) { CALCCELLELEMENT& fred = (CALCCELLELEMENT&) RHS; x = fred.x; y = fred.y; } bool CALCCELLELEMENT::operator ==(FibHeapNode& RHS) { CALCCELLELEMENT& fred = (CALCCELLELEMENT&) RHS; if (x != fred.x) return false; if (y != fred.y) return false; return true; } bool CALCCELLELEMENT::operator <(FibHeapNode& RHS) { CALCCELLELEMENT& fred = (CALCCELLELEMENT&) RHS; if (pathmapcell[x][y] < pathmapcell[fred.x][fred.y]) return true; if (pathmapcell[x][y] > pathmapcell[fred.x][fred.y]) return false; if (fred.x < x) return false; if (x < fred.x) return true; if (fred.y < y) return false; if (y < fred.y) return true; return true; } float CCellPath::GetOneCost (int sx,int sy,int dx,int dy) { float toret; if (dx<0) return -1; if (dy<0) return -1; if (dx>=MAPCELLSIZE) return -1; if (dy>=MAPCELLSIZE) return -1; sx += g_MapCells[m_Cell.x][m_Cell.y].Left (); dx += g_MapCells[m_Cell.x][m_Cell.y].Left (); sy += g_MapCells[m_Cell.x][m_Cell.y].Top (); dy += g_MapCells[m_Cell.x][m_Cell.y].Top (); if (!Passable (dx,dy)) return -1; toret = abs (g_Map[sx][sy].weight - g_Map[dx][dy].weight)+1; // toret *= 0.5; return toret; /* int deltax,deltay; deltax = dx - sx; deltay = dy - sy; if (deltax == 0) return toret*0.5; if (deltay == 0) return toret*0.5; return toret * 0.75; */ } float CCellPath::PathCostGuess (int sx,int sy,int dx,int dy) { int t1,t2; t1 = dx - sx; t2 = dy - dx; __asm { mov eax,t1 cdq xor eax,edx sub eax,edx mov t1,eax mov eax,t2 cdq xor eax,edx sub eax,edx mov t2,eax } // return t1+t2; return t2 < t1 ? (t1+(t2>>1)) : (t2+(t1>>1)); } CCellPath::CCellPath (CPoint start,CPoint cell,CPoint end) { assert (start.x>=0); assert (start.x=0); assert (start.y=0); assert (start.x=0); assert (start.y=0); for (y=0;y=0); for (x=0;xx; y = offsetcell[i][1] + temp->y; float cost = GetOneCost (temp->x,temp->y,x,y); if (cost == -1) continue; cost += PathCostGuess (x,y,m_End[0].x,m_End[0].y); if ((pathmapcell[x][y] == 0) || (pathmapcell[x][y] > (pathmapcell[temp->x][temp->y]+cost))) { pathmapcell[x][y] = pathmapcell[temp->x][temp->y]+cost; calc.Insert (new CALCCELLELEMENT(x,y)); } if (ValidEnd (x,y)) { end = CPoint (x,y); done = true; break; } } delete temp; } while (temp = (CALCCELLELEMENT *) calc.ExtractMin ()) delete temp; if (!done) return false; done = false; int curx,cury,min; curx = end.x; cury = end.y; m_RealEnd = end; m_Path.push (PATHCELLELEMENT (curx,cury)); min = -1; while (!done) { min = -1; assert (pathmapcell[curx][cury] != 0); for (i=0;i= MAPCELLSIZE) continue; if (y < 0) continue; if (y >= MAPCELLSIZE) continue; if (pathmapcell[x][y] == 0) continue; if (pathmapcell[x][y] < pathmapcell[curx][cury]) { if (min == -1) min = i; else { if (pathmapcell[x][y] < pathmapcell[curx+offsetcell[min][0]][cury+offsetcell[min][1]]) min = i; } } } assert (min != -1); m_Path.push (PATHCELLELEMENT (curx+offsetcell[min][0],cury+offsetcell[min][1])); if (((curx+offsetcell[min][0]) == m_Start.x) && ((cury+offsetcell[min][1]) == m_Start.y)) { // m_RealEnd.x = curx+offsetcell[min][0]; // m_RealEnd.y = cury+offsetcell[min][1]; done = true; continue; } curx += offsetcell[min][0]; cury += offsetcell[min][1]; } return true; }