Instant strategic game search

Source: Internet
Author: User

Principle and Implementation Technology of the path-Finding Algorithm in Real-Time Strategic games

Author: Shen Lu

A few years ago, when I was at school, I often played a "red alarm" online with my classmates in the dormitory ", when you play more, you have been exploring the hidden programming secrets behind real-time strategic games such as the red alarm. Recently, I found a period of free time and finally put my previous ideas into practice. I used VC to write a prototype of an instant strategy game (the execution program in the attachment uses the algorithm described in this article ). The principles and implementation technologies of the path-Finding Algorithm in Real-Time Strategic games are written here.

Imagine that when you are sitting in front of a computer and directing thousands of troops and horses on the screen, you suddenly find that the tank cars stop when they encounter obstacles, you will be dissatisfied with their stupid behaviors. Therefore, in instant strategy computer games, real-time path finding algorithms are used for movable objects (such as tanks and soldiers in the Red Police) calculate a "smart" route.

1. path Finding Algorithm for a single object

The most basic problem to be solved in the path finding algorithm is to avoid obstacles. The easiest way to achieve this is (see Figure 1 ):

  1. Draw a straight line A from the start point to the end point.
  2. Along the Line A toward the end, the obstacle will walk clockwise around the obstacle until it hits the line.
  3. Repeat Step 2 to reach the destination.

Figure 1

(Note: The red dot indicates the starting point, the blue dot indicates the ending point, and the yellow line indicates,
Black Line marked as B, red line marked as B)

The path calculated by this algorithm is not the shortest path. Sometimes it is silly, but it is very fast and can meet real-time requirements in games on low-grade PCs, the path Finding Algorithm in the "Red Police" is based on this (we will not introduce it here if we use the advanced a * Algorithm in the Age of Empire ). The preceding algorithm can be improved to make the walking route of a movable object more reasonable.

(1)
As shown in figure 1, it is clear that a movable object should go around the obstacle in the clockwise direction instead of the clockwise direction. Therefore, when encountering an obstacle, you must first determine the direction of the surrounding area. If you can walk in a shorter path in the clockwise direction than in the clockwise direction, you can walk around the obstacle in the clockwise direction, and vice versa.

(2)
Try to take a straight line path to reduce the detour path. 1. Calculate the travel route B according to the above algorithm. Then, when the movable object moves around the obstacle on Route B, each step will draw a straight line C from the current position to the end, if you can move forward along the straight line C to meet a certain segment of Route B that has not yet passed (no obstacle in the middle), stop the detour and directly follow the straight line C to Route B, this shortens the process.

(3)
When the end point is surrounded by obstacles, it is never possible for a movable object to reach the end point. Whether it is clockwise or counterclockwise, it can only return to the original place and cannot travel. In this case, take a week around the obstacle and choose to stop from the nearest destination.

In the specific implementation of this algorithm, the path finding algorithm mainly involves the generation of straight lines and the routes surrounding obstacles. Linear raster generation can be referred to any computer graphics textbook. The following is an algorithm for generating the route around the obstacle (clockwise direction is used as an example ).

Figure 2

  1. When walking around an obstacle, you must first identify the direction of the obstacle relative to the movable object and mark it as an integer I (see figure 2 ). For example, if an obstacle is on the right side of a movable object, it is recorded as direction 5.
  2. Then, move the object to view J = (I + n) mod 8 (n = 1 .. 7) until no obstacle is found at J. If the obstacle is on the top (7), the movable object first looks at the top left (0
    = (7 + 1) mod 8) can it pass? If not, try the left direction (1 = (7 + 2) mod 8 ), you can't test the other directions by the arrows shown in the figure until you find the exit and take a step in this direction. If the obstacle is located at the bottom left (2), the obstacle is listed below (3), bottom right (4), right side (5), top right (6), top right (7), and top left (0), left side (1) and other seven directions to check whether there is a path. As for the handling of obstacles relative to other locations of movable objects, such push is taken.
  3. Repeat steps 1 and 2 to walk around the obstacle clockwise. For details about the pseudo-language description of this algorithm, refer to repeatedly calling the clockwisewalkonestep function to clockwise bypass the obstacle for one week.

Boolean clockwisewalkonestep (Int & ncol, Int & nrow, Int & I); <br/>{< br/> int N; </P> <p> for (n = 1; n <= 7; n ++) <br/> {<br/> I = (I + n) moD 8; <br/> If (Map [ncol, nrow]. ispassable (I) = true) <br/> break; <br/>}</P> <p> If (n = 7 & map [ncol, nrow]. ispassable (I) = false) <br/> return false; </P> <p> switch (I) <br/> case 1: <br/>{< br/> ncol = nCol-1; <br/> I = 7; <br/> break; <br/>}< br/> case 2: <br/>{< br/> If (Map [ncol, nrow]. ispassable (3) = false) // take the vertical or horizontal direction as far as possible <br/> {<br/> ncol = nCol-1; <br/> nrow = nrow + 1; <br/> I = 7; <br/>}< br/> else <br/>{< br/> nrow = nrow + 1; <br/> I = 0; <br/>}< br/> break; <br/>}< br/> case 3: <br/>{< br/> nrow = nrow + 1; <br/> I = 1; <br/> break; <br/>}< br/> case 4: <br/>{< br/> If (Map [ncol, nrow]. ispassable (5) = false) <br/>{< br/> ncol = ncol + 1; <br/> nrow = nrow + 1; <br/> I = 1; <br/>}< br/> else <br/>{< br/> ncol = ncol + 1; <br/> I = 2; <br/> end <br/>}< br/> break; <br/>}< br/> case 5: <br/>{< br/> ncol = ncol + 1; <br/> I = 3; <br/> break; <br/>}< br/> case 6: <br/>{< br/> If (Map [ncol, nrow]. ispassable (7) = false) <br/>{< br/> ncol = ncol + 1; <br/> nrow = nRow-1; <br/> I = 3; <br/>}< br/> else <br/> {<br/> nrow = nRow-1; <br/> I = 4; <br/>}< br/> break; <br/>}< br/> case 7: <br/>{< br/> nrow = nRow-1; <br/> I = 5; <br/> break; <br/>}< br/> case 0: <br/>{< br/> If (Map [ncol, nrow]. ispassable (1) = false) <br/>{< br/> ncol = nCol-1; <br/> nrow = nRow-1; <br/> I = 5 <br/>}< br/> else <br/> {<br/> ncol = nCol-1; <br/> I = 6; <br/>}< br/> break; <br/>}< br/> return true; <br/>}< br/>

 

2. Path Generation Technology in dynamic obstacle Environment

The algorithm just described can only be used to generate a walking path for a single movable object in a static obstacle environment. However, in real-time strategy games, a group of tanks are often galloping on a dynamic map. Different tanks are mutually obstacles and movable obstacles when they are walking. therefore, the preceding algorithm must be modified to avoid moving obstacles.

The most intuitive solution is to re-calculate the walking path immediately when a movable obstacle is encountered. The disadvantage of this method is that the path is computed repeatedly, which takes too much time. in fact, when an object hits a movable obstacle in motion, you can wait for a while. If you find that the movable obstacle has been removed, you can continue to move forward according to the original walking path, this reduces the time required to repeat the paths. Conversely, if a mobile obstacle is found to be in the same place, the priority of the two is compared (a priority is assigned to each walking object in advance ), if the priority is lower, the system continues to wait. If the priority is higher, the system immediately recalculates the walking path to avoid moving obstacles. obviously, this method can reduce a lot of repeated computations and speed up execution. for the pseudo-language description of this algorithm, see:

// Traverse all movable objects <br/> for (I = 1; I <= N; I ++) <br/> {<br/> switch (tank [I]. state) {<br/> case run: <br/>{< br/> If (tank [I] receives command) <br/> {<br/> tank [I]. goalpos = destination address; <br/> tank [I]. state = findnewpath; <br/>}</P> <p> If (tank [I]. pos = routelist [tank [I]. getendpos () // reach the destination <br/>{< br/> tank [I]. state = stillness; <br/> break; <br/>}</P> <p> tank [I]. posnext = routelist [tank [I]. getnextpos (); // retrieve the Next Step <br/> If (Map [tank [I]. posnext]. passable = true) <br/>{< br/> tank [I]. state = moving; <br/> tank [I]. currentmovingpos = tank [I]. pos; <br/> map [tank [I]. posnext]. passable = false; <br/> map [tank [I]. pos]. passable = false; <br/>}< br/> else <br/>{< br/> If (Map [tank [I]. posnext]. state = stillness | (tank [I]. waittime> specified time & tank [I]. priornum> map [tank [I]. posnext]. priornum) <br/> tank [I]. state = findnewpath; <br/> else <br/> tank [I]. waittime ++; <br/>}< br/> break; <br/>}< br/> case findnewpath: <br/>{< br/> according to tank [I]. goalpos: calculates the walking path of tank [I] and adds routelist [tank [I]; <br/> If (routelist [tank [I] is null) <br/> tank [I]. state = findnewpath; <br/> else <br/>{< br/> tank [I]. state = run; <br/> tank [I]. waittime = 0; <br/>}< br/> break; <br/>}< br/> case moving: <br/> {<br/> // linear interpolation between POs and posnext <br/> If (tank [I]. currentmovingpos + nstep) <tank [I]. posnext) <br/>{< br/> tank [I]. currentmovingpos + = nstep; <br/> tank [I]. state = moving; <br/>}< br/> else <br/> {<br/> tank [I]. currentmovingpos = tank [I]. posnext; <br/> tank [I]. state = run; <br/> tank [I]. waittime = 0; <br/> map [tank [I]. posnext]. passable = false; <br/> map [tank [I]. pos]. passable = true; <br/> tank [I]. pos = tank [I]. posnext; <br/>}< br/> case stillness: <br/>{< br/> If (tank [I] receives command) <br/> {<br/> tank [I]. goalpos = destination address; <br/> tank [I]. state = findnewpath; <br/>}< br/> break; <br/>}</P> <p> draw the background first. <br/> for (I = 1; I <= N; I ++) <br/> draw (tank [I]. currentmovingpos); <br/>

Figure 3 transition of movable objects

In the preceding algorithm, a movable object has four States (see figure 3): stillness, run, findnewpath, and moving. In the initial situation, the movable object is in the stillness state. After receiving the command, it enters the findnewpath state. Once the travel path is calculated, the movable object is set to run. In the run state, the coordinates of the next step are taken from the walking path list. If there is no obstacle in the position shown in the coordinates, the state changes to moving, and coordinate interpolation is performed between the first and second steps, after the operation, the system returns to the run state. if it finds that the previously calculated walking path is no longer applicable, the system changes from the run state to the findnewpath state. After finding a new path, the system returns to the run state. Finally, when the terminal is reached, the status of the movable object is stillness.

To view the actual results of this algorithm, download the demo program.

 

Author: Shen
Lu

Email: uulushen@public1.sz.js.cn
Mailing address: No. 704, East ganyao Road, Suzhou, Jiangsu, China
Zip code: 215000

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.