The range of motion that is implemented in a chess game

Source: Internet
Author: User
Tags extend

The following is not practiced, so it is probably wrong to find useful beginner friends read it:)
I hope the expert pointing twos:)

Brief introduction:
In the standard SLG game, when the mouse is pressed at a character, the character is centered and a diamond-shaped movable area is generated around the perimeter, as follows:

0
000
00s00
000
0

This graphic should have written a drawing program when you first started to learn Pascal (whether someone misses it or not). )。 The graph is the same as the SLG extension path.

First, how to generate the path:
Start with the character's position, extend to the surrounding four directions, and then expand the point. Extending from near to far from where the character is located (similar to wide-width preference).

Second, the expansion will encounter problems:
1, when the expansion to a point, the movement of the character is not.
2, when the expansion encountered a barrier point.
3, when the extension of this node out of the map.
4, the expansion of the time encountered a character just standing at this point (with 2. )。
5. The extended point has been extended. When the node is extended, each node is extended around, resulting in duplicate nodes.

When these problems are encountered, we are not dealing with these nodes. Using the allpath[] array in the program records each of the nodes of the same extension, not dealing with the problem nodes means not adding them to the allpath[] array. How do we extend the four nodes around a node, using the coordinates of this node to add an offset, the direction is as follows:

3
0 2
1

The offset is defined as follows:
int offx[4] = {-1, 0, 1, 0};
int offy[4] = {0, 1, 0,-1};

The coordinates of the contiguous four nodes that extend one node are:
for (int i=0; i<4; i)
{
temp.x = temp1.x Offx[i];
TEMP.Y = Temp1.y Offy[i];
}

Third, about the structure of the map:
1, the map of the two-dimensional coordinates, used to determine the location of each tile in the map.
2, SLG also to introduce a variable decrease that the figure after the block after the movement of his reduced value of the force. For example, a character now has a moving force of curmp=5, with the decrease=2 of the tile that is being picked up, and if the character moves here, then its movement force becomes curmp-decrease.
3, Flag domain: The width first seems to have this variable, with it, each point is guaranteed to be extended only once. Prevents a point from being extended multiple times. (a point that is only extended once can really get the right result.) )
4. Whether a tile on a map can be passed, we use a block representative. 1---can not be passed; 0---can be passed.

In this way, we can define a simple array of map structures:

#define MAP_MAX_WIDTH 50
#define Map_max_height 50
typedef struct tagtile{
int x,y,decrease,flag,block;
}tile,*lptile;
TILE Pmap[map_max_width][map_max_height];

These are sequential arrays, and it is better to use dynamic allocations. After all, you cannot know beforehand the width and height of a map.

Iv. about the path:
The extension path in SLG games is an area (centered around the people, and of course, when the characters move, there is only one path). The paths to these extensions must be stored and all have a good structure. I have defined a structure that is not very good:

typedef struct tagnode{
int x, y; Expands the coordinates of a point in the path in the map.
int CURMP; The character's current movement force after this point.
}node,*lpnode;

The structure above is the structure that defines a point in the extension path. The extension path is a collection of points and is therefore defined with the following array:

NODE Allpath[path_max_length];

Where the path_max_length represents the number of points that extend the path, we do not know how many points are included in the path of the extension, so defining a larger number makes the array non-overflow:

#define PATH_MAX_LENGTH 200

The above array is useful, and later extensions are implemented by it, which should have two variables Nodecount represent how many points are in the current array. Of course, the points in the array are divided into two parts, some of which are already expanded, stored in front of the array, and the other part is the nodes that are waiting to be expanded, and why the expanded node and the node to be expanded appear behind the array. Here's an example:

The current character coordinates are x, Y, and the moving force is MP. It is stored in the Allpath array, where the starting node is the node of the extension. At this point we expand its four direction, for the legitimate node (if not out of the map, there is no barrier ...). ), we put them into the Allpath array, when the newly added node (the child node of the starting node) is the extension node, and the starting node becomes the expanded nodes. The next time we expand the node, we can no longer extend the start node because it is a node that has already been expanded. We only extend those newly added nodes (to expand the node), and so on. So how do we know which nodes have been expanded, and which are the ones that are expanding. We use another variable, Cutflag, where the previous node of the subscript represented by the variable is an expanded one, after which it is to be expanded.

Five, the following is the basic framework (only to extend the reach of a person):

int nodecount=0; The number of points in the Allpath array containing the nodes to be expanded and the nodes that have already been expanded
int cutflag=0; Used to divide the nodes that have been expanded and the nodes to be expanded
NODE temp; A point in the path (temporary)
temp.x=prole[cur]->x; Suppose there is a class about the character that represents the current character
temp.y=prole[cur]->y;
temp.curmp=prole[cur]->mp; Max MP of the character
Allpath[nodecount]=temp; Starting point into Allpath, where the starting point is a node of equal expansion

while (Curflag<nodecount)//array of nodes yet to be expanded
{
int n=nodecount; Records the number of current array nodes.
for (int i=cutflag;i<nodecount;i)//traverse the node to be expanded
{
for (int j=0;j<4;j)//Take one step around the node to be expanded
{
Get data from adjacent points
Temp.x=allpath[i].x Offx[j];
Temp.y=allpath[i].y Offy[j];
Temp.curmp=allpath[i].curmp-pmap[allpath[i].x][allpath[i].y].decrease;
The following is the process of detecting whether a problem point, if it is a problem point, do not join the Allpath array, continue processing other points
if (Pmap[temp.x][temp.y].block)
Continue There is a barrier to processing the next node
if (temp.curmp<0)
Continue There's no moving power.
if (temp.x<0| | temp.x>=map_max_width| | temp.y<0| | Temp.y>=map_max_height)
Continue Out of the range of the map
if (Pmap[temp.x][temp.y].flag)
Continue Nodes that have been expanded
After a few layers of detection, no problem of the node filtered out, you can join Allpath
Allpath[nodecount]=temp;
}
pmap[allpath[i].x][allpath[i].y].flag=1; To mark a node that has already been expanded as an expanded node
}
Cutflag=n; Moves the dividing line subscript value of the expanded node and the node to be expanded to a new dividing line
}
for (int i=0;i<nodecount;i)
pmap[allpath[i].x][allpath[i].y].bflag=0; Tags marked as extended nodes are set back to the node to be expanded.

Related Article

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.