To the news: Push box game automatic solution algorithm design (II.)

Source: Internet
Author: User
Tags prev sprintf

In this section we say that the legendary AX algorithm, in fact, has uploaded similar small pieces before, here we go to dissect it

After all, in the game program, we have to move from one point to another, and get the shortest path, similar to the algorithm there are several, the implementation of the same efficiency is similar, but most of them can not get the trajectory


First, moving from one point to another, the quickest is to go straight, just like a little boy in love with a girl, the best way is to go straight to her and say: I love you

But the ideal state, almost no, detours that is inevitable through, there are curves, in fact, more beautiful ...


So detour how to go, is not first to see the background of Chairman Mao, then go to Sanya Sun, then come back to tell her outside the world is beautiful, no, no, no ...

The more curved the curve, the closer the direct, the better, so the steps are:


1. To the surrounding 4 or 8 directions, all points that are not boundaries, record the distance from the point to the target, the distance from the beginning to the point, and the last point here

2. According to the two distance and minimum criteria, "recursive" all points to the target point

3. Reverse the path, which is the nearest curve path


Q: Will it reach the same point, there are different distances

A: Yes, but according to the nearest distance criterion, the farther distance will be discarded.


Q: Why is recursion quoted

A: Implementing a stack with a queue is recursive, but does not deplete the thread stack


In order to facilitate in the push box to detect whether it can hit a point, I modified the code implementation does not record the previous point, speed up the execution

However, this function was not used later, the main code is as follows:

Alpha.cpp

File alpha.cpp//Note://A * pathfinding algorithm module, determine whether two points between the passage, and get the path and the weight of each node//expansion does not save the path to speed up, do not use Dijkstra, see http://www.2cto.com/kf/201104/87378. html//****************************************************************************************************//# Include <stdlib.h> #include <malloc.h> #define __LATTICE_DLL_INC_//DLL Internal compilation # include "Api.h"//Create a scene, and initialize V32api pscene __stdcall alphainit (Long Dwsizex, long Dwsizey, long dwmaxcost, UINT dwFlags) {pscene PS = (pscene) malloc (sizeof (SCENE)); if (PS = = null) {return null;} V32clear32 (PS, 0, sizeof (SCENE)),//memset, automatic calculation of the number of DWORD Ps->nodecount = Dwsizex * dwsizey;//number of nodes ps->nodes = (pnode) malloc (Ps->nodecount * sizeof (NODE)), if (ps->nodes = = NULL) {alphaexit (PS, 0xFFFFFFFF); return NULL;} Ps->matrix = (pbyte) malloc (ps->nodecount * sizeof (BYTE));//Scene Matrix if (Ps->matrix = = NULL) {Alphaexit (PS, 0xFFFFFFFF); return NULL;} Ps->steps = (pstep) malLOC (Ps->nodecount * sizeof (STEP)), if (ps->steps = = NULL) {alphaexit (PS, 0xFFFFFFFF); return NULL;} if (dwmaxcost <= 0) {dwmaxcost = Dwsizex * dwsizey;//adjacent lattice travels one by one}ps->maxcost = Dwmaxcost * cdc_prop_near;//max Consumption (distance value) ps- >sizey = Dwsizey;ps->sizex = Dwsizex;ps->flags = Dwflags;return PS;} Edit scene Data V32api int __stdcall alphaedit (pscene pscene, Long dwposx, long dwposy, void *lpvalue) {pbyte p;if (Dwposx < 0 & amp;& Dwposy < 0) {//Read P = pscene->scene;if (p = = NULL) p = pscene->matrix;* (pbyte) lpvalue = P[dwposy * Pscene ->sizex + Dwposx];return 2;} if (Dwposx < 0 | | Dwposy < 0) {//Use external data pointer pscene->scene = (pbyte) Lpvalue;return 3;} Set the byte value if (dwposx >= Pscene->sizex | | dwposy >= pscene->sizey) {//memcpy (Pscene->matrix, LpValue, Pscene->nodecount); return 0;} p = pscene->scene;if (p = = null) p = pscene->matrix;if (Lpvalue = = null) {return p[dwposy * Pscene->sizex + DWPOSX]; Read}else{p[dwposy * Pscene->sizex + dwposx] = * (pbyte) lpvalue;//write}return 1;} Inline long Alphacalc (Pstar pStar1, Pstar pStar2) {//direct use of coordinate differences and substitution for trigonometric functions if (Pstar1->x > Pstar2->x) {if (pstar1-> Y > Pstar2->y) return (pstar1->x-pstar2->x) + (pstar1->y-pstar2->y); Elsereturn (pstar1->x-pstar2 -&GT;X) + (pstar2->y-pstar1->y);} Else{if (Pstar1->y > Pstar2->y) return (pstar2->x-pstar1->x) + (pstar1->y-pstar2->y); Elsereturn ( PSTAR2-&GT;X-PSTAR1-&GT;X) + (pstar2->y-pstar1->y);} return DX + DY;} inline void Alphanode (Pscene pscene, Long dwindex, long dwprevid, long dwcost, long dwlast, long dwposx, long Dwposy) {//Open Start and loop each call once, dwindex after the call to add, initially 0if (dwindex >= pscene->nodecount) Return;//pscene->nodes[dwindex]. Does Star = *pstar;//Create a temporary struct?? Pscene->nodes[dwindex]. star.x = Dwposx;pscene->nodes[dwindex]. STAR.Y = Dwposy;pscene->nodes[dwindex]. Prev = Dwprevid;if (Dwprevid! =-1) pscene->nodes[dwindex]. Step = Pscene->nodes[dwprevid]. Step + 1;elsepscene->nodes[dwindex]. Step = 1;p Scene->nodes[dwindex]. Cost = Dwcost;pscene->nodes[dwindex]. Last = Dwlast * 10;//Each dwlen recalculation to get the number of squares pscene->nodes[dwindex]. Flags = Snf_prop_ready;return;} Inline BYTE fnalphaunit (pscene pscene, Pstar PS) {BYTE *punit = pscene->scene;if (PUnit = = NULL) PUnit = Pscene->matri X;return punit[ps->y * Pscene->sizex + ps->x];} Inline Pstep fnalphastep (pscene pscene, Pstar PS) {return &pscene->steps[ps->y * Pscene->sizex + Ps->X];} pathfinding Specify scene v32api int __stdcall AlphaStar (pscene pscene, Pstar Lpstart, Pstar lptarget, long *pdwstep) {pstep Pstep;long LCur Cost, nmaxcost;//temporary consumption, max list long I, j;//temp & Looping Varstar TPS, cps;//Test pos, current Poslong Dwvalue;long Dwin Dex = 0;long Dwnode = 0;//current node number long Dwloop;long dwlen;//and end distance/check for memory address accessableif (Lpstart = = NULL || Lptarget = = NULL) {return 0;//invalid coordinates}dwlen = Alphacalc (Lpstart, lptarget);//Dwlen =δx +δyif (Dwlen = = 0) {return-1;// Same coordinates}//zero step memory (cell prop list) V32clear32 (pscene->steps, 0, Pscene->nodecount * sizeof (STEP));//Add the first point dwnode = 0; Alphanode (Pscene, Dwnode,-1, 0, Dwlen, lpstart->x, lpstart->y);d wnode++;//Enter Loop-check around Cellswhile (1) { Nmaxcost = pscene->maxcost;//cannot be larger than this dwindex = -1;for (dwloop = 0; dwloop < Dwnode; dwloop++) {if (PSCENE-&GT;NODES[DW Loop]. Flags! = snf_prop_error) {//Find not closed in the smallest distance and the point lcurcost = Pscene->nodes[dwloop]. Cost + Pscene->nodes[dwloop]. Last;if (Lcurcost < nmaxcost) {nmaxcost = lcurcost;//adjust maximum distance dwindex = dwloop;//Save node ordinal//break;//All nodes are calculated}}}if (dwindex = =-1) {return-2;//There is no path exist!} Cps. X = Pscene->nodes[dwindex]. Star.x;cps. Y = Pscene->nodes[dwindex]. Star.y;if (CPS. X = = lptarget->x) && (CPS. Y = = lptarget->y) {break;//The current point is already an endpoint, jump out of the while Loop}//sprintf (Sztext, "select Best cell:[%d,%d] for check:", CPS. X, CPS. Y); for (i =-1; I <= 1; i++) {for (j =-1; J <= 1; j + +) {//if (i = = 0 && j = 0) continue;//allow diagonal, as long as two different 0 (i.e. not Self) if (i = = 0 && J = = 0) conTinue;if (i! = 0 && J! = 0) continue;//prohibit diagonal, must have only one zero {[(I & j) = = 0]&&[(I | j)! = 0]}tps. X = CPS. X + I;tps. Y = CPS. Y + j;if (TPs. X < 0) continue;//left cross-border if (TPs. X >= Pscene->sizex) continue;//right cross-border if (TPs. Y < 0) continue;//top border out of bounds if (TPs. Y >= Pscene->sizey) continue;//cross-border//the point coordinates within the matrix range if (Fnalphaunit (Pscene, &tps) & (BYTE) pscene->flags) { continue;//elimination of special bits is still impassable (standalone program: Allow only spaces or targets)}pstep = Fnalphastep (Pscene, &AMP;TPS); switch (pstep->flags) {case Ssf_ prop_unknow://It ' v not been checkdwvalue = Pscene->nodes[dwindex]. cost;//if (i * j! = 0)//dwvalue + = cdc_prop_away;//horizontal ordinate all nonzero, diagonal//elsedwvalue + = cdc_prop_near;//Any one coordinate is zero, adjacent Dwlen = Alpha Calc (&tps, lptarget);//dwlen =δx +δyalphanode (Pscene, Dwnode, Dwindex, Dwvalue, Dwlen, TPS. X, TPs. Y);p step->flags = ssf_prop_opened;//Open Itpstep->index = dwnode++;//sprintf (sztext, "Add cell:[%d,%d" for check: ", TPs. X, TPs. Y); Break;case ssf_prop_opened://This cell is Validdwvalue = Pstep->indEx;dwlen = Pscene->nodes[dwindex]. Cost;dwlen + = cdc_prop_near;//can only be adjacent if (Dwlen < Pscene->nodes[dwvalue]. Cost) {Pscene->nodes[dwvalue]. Cost = Dwlen;pscene->nodes[dwvalue]. Prev = Dwindex;pscene->nodes[dwvalue]. Step = Pscene->nodes[dwindex]. Step + 1;//change level}//sprintf (sztext, "Change PID for cell:[%d,%d] to%d.", TPs. X, TPs. Y, NID); Break;default://end if (Lpogrid[tps. X][tps. Y]. State: break;} End if (Lpcell. End if (TPs. X.. End If (((I..} Next J}//next i//It would continue if any path and not at targetpstep = Fnalphastep (Pscene, &cps);p scene->nodes[d Windex]. Flags = Snf_prop_error;pstep->flags = ssf_prop_closed;//Close it//sprintf (Sztext, "close cell:[%d,%d] OK.", CPS. X, CPS. Y);} The function malloc () and realloc () time consumption is large and space is exchanged for time dwvalue = Pscene->nodes[dwindex]. Cost + Pscene->nodes[dwindex]. Last;if (Pdwstep = = NULL) {//No result details are required return dwvalue;//return cost to get here}//sprintf (sztext, ' best path found ' cost%d . ", dwvalue);p Scene->starcount = Pscene->nodeS[dwindex]. Step; Safefree (pscene->stars);//release Old Pathpscene->stars = (Pstar) malloc (pscene->starcount * sizeof (STAR)); if (Pscene->stars = = NULL) {return-3;//out of memory}//... dwloop = Pscene->starcount;*pdwstep = dwloop;//s ET the stepswhile (dwloop > 0)//It can also base on Dwindex{dwloop--;//pscene->m_pstarpath[dwloop]. X = Pscene->nodes[dwindex]. Star.x;//pscene->m_pstarpath[dwloop]. Y = Pscene->nodes[dwindex]. Star.y;pscene->stars[dwloop] = Pscene->nodes[dwindex]. Star;dwindex = Pscene->nodes[dwindex]. prev;//parent id//sprintf (Sztext, "the%d cell:[%d,%d] added to path.", I, lproad[i]. X, Lproad[i]. Y);} Return dwvalue;//return cost to get Here}v32api int __stdcall alphadata (pscene pscene, UINT dwpropid, long dwindex) {VAR V ; v.pvalue = (void*) Pscene;v.dwvalue + = dwpropid;if (Dwindex < 0) {return (int) *v.plvalue;//Direct position member}v.dwvalue = * V.pdwvalue;v.dwvalue + = dwindex;return (int) *v.plvalue;} Destroys a scene, frees resources V32api int __stdcall Alphaexit (Pscene Pscene, UINT dwFlags) {if (pscene) {safefree (pscene->stars);p scene->starcount = 0; Safefree (pscene->steps);p scene->scene = null;//External Data pointer Safefree (PSCENE-&GT;MATRIX); Safefree (pscene->nodes);p scene->nodecount = 0;free (pscene);//pscene = NULL;} return 1;}

Where V32API is a macro definition and therefore relies on V32.dll in v32/api.h

This is my base class for a base class library, including common C-language functions and C + + wrapper classes, and some macro definitions

Functions are mostly inline-assembled bare functions, and macro definitions are generally used to quickly release memory, such as

#define SAFEDELETE (P) if (p) {delete p; p = NULL;}

C + + just encapsulates the C function as a class, while extending the C + + effects, such as smart pointers, see the header file


To the news: Push box game automatic solution algorithm design (II.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.