00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "hopfield_algo.h"
00037
00038
00039
00041
00042
00043
00044
00045
00047 int DirAdd[16][2]={ { 0,-1},
00048 {-1, 0}, { 1, 0},
00049 { 0, 1},
00051 {-1,-1}, { 1,-1},
00052
00053 {-1, 1}, { 1, 1},
00055 {-1,-2}, { 1,-2},
00056 {-2,-1}, { 2,-1},
00057
00058 {-2, 1}, { 2, 1},
00059 {-1, 2}, { 1, 2}
00060 };
00061 int nDirect=8;
00063
00065
00066 int g_MapWidth=-1;
00067 int g_MapHeight=-1;
00068
00069 int g_FlyingHeight=100;
00070
00071 short * g_Terrain=NULL;
00072 int** g_SearchMap=NULL;
00073
00074 THREAT_STRUCT g_Threats[100];
00075 int g_ThreatsCount=0;
00076
00077 int g_Sx=-1;
00078 int g_Sy=-1;
00079 int g_Ex=-1;
00080 int g_Ey=-1;
00082
00084
00085 int **g_DisMap=NULL;
00086 double **g_Map=NULL;
00087 POINT *g_Road=NULL;
00088 POINT *g_Serial=NULL;
00089 int g_nSerial=0;
00090 int g_nStep=0;
00091 int g_MaxRepNumber=2;
00092 int g_A=10;
00093 double g_InitValue=1e200;
00095
00097
00098 bool InitThreat()
00099 {
00100 char Temp[255];
00101 FILE* fp=fopen("src//micros_hopfield-master//map//Environment.ppf","r");
00102 if (fp!=NULL)
00103 {
00104 fscanf(fp,"%s\n",Temp);
00105 g_MapWidth=atoi(Temp);
00106 fscanf(fp,"%s\n",Temp);
00107 g_MapHeight=atoi(Temp);
00108 fscanf(fp,"%s\n",Temp);
00109 g_FlyingHeight=atoi(Temp);
00110 fscanf(fp,"%s\n",Temp);
00111 g_ThreatsCount=atoi(Temp);
00112 for (int i=0;i<g_ThreatsCount;i++)
00113 {
00114 fscanf(fp,"%s\n",Temp);
00115 g_Threats[i].center.x=atoi(Temp);
00116 fscanf(fp,"%s\n",Temp);
00117 g_Threats[i].center.y=atoi(Temp);
00118 fscanf(fp,"%s\n",Temp);
00119 g_Threats[i].range=atoi(Temp);
00120 }
00121 fclose(fp);
00122 cout<<"PPF File Read OK!"<<endl;
00123 return true;
00124 }
00125
00126 cout<<"PPF File Open Error!"<<endl;
00127 return false;
00128 }
00129
00130
00131 void InitTerrain()
00132 {
00133 if (g_Terrain!=NULL) delete g_Terrain;
00134 int DataSize=g_MapWidth*g_MapHeight;
00135 g_Terrain = new short[DataSize];
00136 FILE* fp = fopen("src//micros_hopfield-master//map//Terrain.raw","rb");
00137 if (fp)
00138 {
00139 if (fread(g_Terrain,DataSize*2,1,fp)==DataSize*2);
00140 {
00141 fclose(fp);
00142 cout<<"Terrain File Open OK!"<<endl;
00143 return ;
00144 }
00145 }
00146 if (g_Terrain!=NULL) delete g_Terrain;
00147 g_Terrain=NULL;
00148 cout<<"Terrain File Open Error!"<<endl;
00149 }
00150
00151
00152 void BuildSearchMap()
00153 {
00154
00155 if (g_SearchMap!=NULL)
00156 {
00157 for (int i=0;i<g_MapHeight;i++)
00158 {
00159 delete g_SearchMap[i];
00160 }
00161 delete g_SearchMap;
00162 g_SearchMap=NULL;
00163 }
00164 g_SearchMap=new int*[g_MapHeight];
00165 for (int i=0;i<g_MapHeight;i++)
00166 {
00167 g_SearchMap[i]=new int[g_MapWidth];
00168 memset(g_SearchMap[i],0,sizeof(int)*g_MapWidth);
00169 }
00170
00171
00172 if (g_Terrain!=NULL)
00173 {
00174 for (int i=0;i<g_MapHeight;i++)
00175 for (int j=0;j<g_MapWidth;j++)
00176 {
00177 if (g_Terrain[i*g_MapWidth+j]>=g_FlyingHeight)
00178 g_SearchMap[i][j]=-1;
00179 }
00180 }
00181
00182
00183 for (int i=0;i<g_ThreatsCount;i++)
00184 {
00185 for (int ix=0;ix<g_Threats[i].range;ix++)
00186 for (int iy=0;iy<g_Threats[i].range;iy++)
00187 {
00188 if (ix*ix+iy*iy<g_Threats[i].range*g_Threats[i].range)
00189 {
00190 g_SearchMap[g_Threats[i].center.y-iy][g_Threats[i].center.x-ix]=-1;
00191 g_SearchMap[g_Threats[i].center.y+iy][g_Threats[i].center.x-ix]=-1;
00192 g_SearchMap[g_Threats[i].center.y-iy][g_Threats[i].center.x+ix]=-1;
00193 g_SearchMap[g_Threats[i].center.y+iy][g_Threats[i].center.x+ix]=-1;
00194 }
00195 }
00196 }
00197 }
00198
00199 void ConnectionSearch()
00200 {
00201 int NowInput=0;
00202 int NowOutput=1;
00203
00204 g_DisMap[g_Ey][g_Ex]=1;
00205 g_Serial[0].x=g_Ex;
00206 g_Serial[0].y=g_Ey;
00207
00208 while (NowInput<NowOutput)
00209 {
00210 for (int i=0;i<nDirect;i++)
00211 {
00212 int X=g_Serial[NowInput].x+DirAdd[i][0];
00213 int Y=g_Serial[NowInput].y+DirAdd[i][1];
00214 if ((X>=0)&&(X<g_MapWidth)&&(Y>=0)&&(Y<g_MapHeight))
00215 {
00216 if (g_DisMap[Y][X]==0)
00217 {
00218 g_Map[Y][X]=0.0f;
00219 g_Serial[NowOutput].x=X;
00220 g_Serial[NowOutput].y=Y;
00221 g_DisMap[Y][X]=g_DisMap[g_Serial[NowInput].y][g_Serial[NowInput].x]+1;
00222
00223 NowOutput++;
00224 }
00225 }
00226 }
00227 NowInput++;
00228 }
00229 g_nSerial=NowOutput-1;
00230 }
00231
00232
00233 void FieldBuilding()
00234 {
00235 for (int RepNumber=0;RepNumber<g_MaxRepNumber;RepNumber++)
00236 {
00237 int nowdis=1;
00238 int nowpos=0;
00239
00240 int i=0;
00241 do {
00242 for (int j=0;j<g_A;j++)
00243 {
00244 for (i=nowpos;i<g_nSerial;i++)
00245 {
00246 if (g_DisMap[g_Serial[i].y][g_Serial[i].x]>nowdis) break;
00247 double Total=0;
00248 if (i==0)
00249 {
00250 Total=g_InitValue;
00251 }
00252
00253 for (int k=0;k<nDirect;k++)
00254 {
00255 int X=g_Serial[i].x+DirAdd[k][0];
00256 int Y=g_Serial[i].y+DirAdd[k][1];
00257 if ((X>=0)&&(X<g_MapWidth)&&(Y>=0)&&(Y<g_MapHeight))
00258 {
00259 if (g_DisMap[Y][X]>=0)
00260 {
00261 Total+=g_Map[Y][X];
00262 }
00263 }
00264 }
00265
00266 Total/=g_A;
00267 g_Map[g_Serial[i].y][g_Serial[i].x]=Total;
00268 }
00269 }
00270 nowdis++;
00271 nowpos=i;
00272 } while (nowpos<g_nSerial);
00273 }
00274 }
00275
00276
00277 void RoadSearch()
00278 {
00279 POINT m_Start;
00280 m_Start.x=g_Sx;
00281 m_Start.y=g_Sy;
00282 g_nStep=0;
00283 do
00284 {
00285 bool noget=true;
00286 double max=-1;
00287 POINT newstart={0,0};
00288
00289 for (int k=0;k<nDirect;k++)
00290 {
00291 int X=m_Start.x+DirAdd[k][0];
00292 int Y=m_Start.y+DirAdd[k][1];
00293 if ((X>=0)&&(X<g_MapWidth)&&(Y>=0)&&(Y<g_MapHeight))
00294 {
00295 if (((g_Map[Y][X]>=max)||(noget))&&(g_DisMap[Y][X]>0))
00296 {
00297 noget=false;
00298 max=g_Map[Y][X];
00299 newstart.x=X;
00300 newstart.y=Y;
00301 }
00302 }
00303 }
00304
00305 m_Start.x=newstart.x;
00306 m_Start.y=newstart.y;
00307
00308 g_Road[g_nStep]=m_Start;
00309
00310 g_nStep++;
00311 }while(((m_Start.x!=g_Ex)||(m_Start.y!=g_Ey))&&(g_nStep<g_MapWidth*g_MapHeight));
00312 }
00314
00315
00316
00318
00319
00320
00321 void Clean()
00322 {
00323 if (g_Map!=NULL)
00324 {
00325 for (int i=0;i<g_MapHeight;i++)
00326 {
00327 delete g_Map[i];
00328 delete g_DisMap[i];
00329 }
00330 delete g_Map;
00331 g_Map=NULL;
00332
00333 delete g_DisMap;
00334 g_DisMap=NULL;
00335
00336 delete g_Serial;
00337 g_Serial=NULL;
00338
00339 delete g_Road;
00340 g_Road=NULL;
00341 }
00342 }
00343
00344
00345 bool InitData()
00346 {
00347 Clean();
00348
00349 g_nSerial=0;
00350 g_Serial=new POINT[g_MapWidth*g_MapHeight];
00351 g_Road=new POINT[g_MapWidth*g_MapHeight];
00352 g_DisMap=new int*[g_MapHeight];
00353 g_Map=new double*[g_MapHeight];
00354
00355 for (int i=0;i<g_MapHeight;i++)
00356 {
00357 g_DisMap[i]=new int[g_MapWidth];
00358 g_Map[i]=new double[g_MapWidth];
00359
00360 memcpy(g_DisMap[i],g_SearchMap[i],sizeof(int)*g_MapWidth);
00361 }
00362
00363 return true;
00364 }
00365
00366
00367 bool Search()
00368 {
00369 printf("Hopfield Planing...\n");
00370 ConnectionSearch();
00371 FieldBuilding();
00372 RoadSearch();
00373 return true;
00374 }
00375
00376
00377 int GetRoad(POINT** Road)
00378 {
00379 *Road=new POINT[g_nStep];
00380 memcpy(*Road,g_Road,sizeof(POINT)*g_nStep);
00381 return g_nStep;
00382 }
00383
00384
00385
00386