Multi-thread path finding method and device, storage medium and electronic device
Through the multi-thread pathfinding method, pathfinding data blocks are distributed to multiple threads for processing, which solves the game lag problem caused by the large amount of calculation of the pathfinding algorithm in the existing technology, and improves user experience and performance.
Patent Information
- Application Number
- CN202310344769.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-03-31
- Publication Date
- 2024-10-01
AI Technical Summary
The automatic pathfinding algorithm in existing games requires a large amount of calculations in complex scenes, resulting in a high frame ratio, causing game lags, and affecting the user experience. This is especially obvious on low-end machines and cannot meet the project's usage needs.
Using a multi-threaded pathfinding method, by constructing pathfinding data blocks and creating multiple pathfinding journeys, the calculation amount in the pathfinding process is distributed to multiple threads, reducing CPU Cache failure, reducing frame ratio, and optimizing computing performance.
It effectively alleviates game lagging and improves user experience, especially in complex scenes and performance on low-end machines.
Smart Images

Figure CN118718403A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a multi-threaded pathfinding method and device, a storage medium, and an electronic device. Background Art
[0002] In the related art, many games involve automatic pathfinding in 2D or 3D scenes. Pathfinding in long distances or complex scenes usually involves a lot of calculations. If the calculation is done directly, it will produce a large frame rate, causing game lag and affecting user experience.
[0003] In the related art, there are many solutions for pathfinding algorithms, including A* algorithm, greedy algorithm, jump point algorithm, etc. The greedy pathfinding algorithm directly selects the best one in the local solution space of the current pathfinding, and directly iterates to the next calculation. It has good performance in simple scenes, but complex scenes may lead to unsolvable problems. In the long-distance pathfinding involving complex terrain and large scenes, the amount of direct calculation of the A* algorithm is too large, which will take up a lot of memory and CPU resources. At the same time, the calculation is very frequent during the character movement, which will constantly produce perceptible jams. The performance is particularly obvious on low-end machines and cannot meet the project's usage requirements. The jump point algorithm is essentially an improvement on A*. Some path points are calculated in advance and cached. When finding the path, these path points are used to prune the calculation process of A*, reducing the amount of calculation of A*. However, for 3D scenes, caching jump point information requires a large amount of storage, and the generated jump points appear unnatural in the pathfinding performance.
[0004] With respect to the above-mentioned problems existing in the related technologies, no effective solutions have been found so far. Summary of the invention
[0005] Embodiments of the present invention provide a multi-threaded pathfinding method and device, a storage medium, and an electronic device.
[0006] According to one embodiment of the present invention, there is provided a multi-threaded pathfinding method, comprising: responding to a pathfinding instruction of a game scene, obtaining pathfinding data of the pathfinding instruction, wherein the pathfinding data comprises a pathfinding starting point, a pathfinding end point, and scene pathfinding data of the game scene; using the pathfinding data to construct a pathfinding data block, wherein each pathfinding data block corresponds to a pathfinding path; creating a plurality of pathfinding threads according to the number of the pathfinding data blocks, and using the plurality of pathfinding threads to perform scene pathfinding to obtain a pathfinding path from the pathfinding starting point to the pathfinding end point.
[0007] According to another embodiment of the present invention, a multi-threaded pathfinding device is provided, including: an acquisition module, used to respond to a pathfinding instruction of a game scene, and acquire pathfinding data of the pathfinding instruction, wherein the pathfinding data includes a pathfinding starting point, a pathfinding end point, and scene pathfinding data of the game scene; a construction module, used to use the pathfinding data to construct a pathfinding data block, wherein each pathfinding data block corresponds to a pathfinding path; a pathfinding module, used to create multiple pathfinding threads according to the number of the pathfinding data blocks, and use the multiple pathfinding threads to perform pathfinding to obtain a pathfinding path from the pathfinding starting point to the pathfinding end point.
[0008] According to yet another embodiment of the present invention, a storage medium is provided, in which a computer program is stored, wherein the computer program is configured to execute the steps of any one of the above method embodiments when running.
[0009] According to yet another embodiment of the present invention, there is provided an electronic device, including a memory and a processor, wherein the memory stores a computer program, and the processor is configured to run the computer program to execute the steps in any one of the above method embodiments.
[0010] Through the present invention, a pathfinding instruction of a game scene is responded to, pathfinding data of the pathfinding instruction is obtained, wherein the pathfinding data includes a pathfinding starting point and a pathfinding end point, a pathfinding data block is constructed using the pathfinding data, a plurality of pathfinding threads are created according to the number of pathfinding data blocks, and a plurality of pathfinding threads are used to perform scene pathfinding to obtain a pathfinding path from the pathfinding starting point to the pathfinding end point, and the pathfinding data block is constructed and the pathfinding data is merged as a whole into a whole block of memory, so that the performance loss caused by the CPU cache failure during the pathfinding calculation process based on the pathfinding data can be reduced, and the computing performance can be optimized, and a plurality of pathfinding threads are created according to the number of pathfinding data blocks, so that the frame ratio of the overall pathfinding calculation is reduced, the amount of calculation in the pathfinding process is dispersed to a plurality of threads, and the multi-threading is fully utilized to reduce the frame ratio, thereby solving the technical problem of high frame ratio during pathfinding in the related technology, alleviating the game freeze, and improving the user experience. BRIEF DESCRIPTION OF THE DRAWINGS
[0011] The drawings described herein are used to provide a further understanding of the present invention and constitute a part of this application. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute an improper limitation of the present invention. In the drawings:
[0012] Figure 1 It is a hardware structure block diagram of a multi-threaded path-finding computer according to an embodiment of the present invention;
[0013] Figure 2 is a flowchart of a multi-threaded pathfinding method according to an embodiment of the present invention;
[0014] Figure 3 is an implementation flow chart of an embodiment of the present invention;
[0015] Figure 4 is a structural block diagram of a multi-threaded path-finding device according to an embodiment of the present invention;
[0016] Figure 5 It is a structural diagram of an electronic device according to an embodiment of the present invention. DETAILED DESCRIPTION
[0017] In order to enable those skilled in the art to better understand the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only embodiments of a part of the present application, not all embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in the field without creative work should fall within the scope of protection of the present application. It should be noted that the embodiments in the present application and the features in the embodiments can be combined with each other without conflict.
[0018] It should be noted that the terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, product or device comprising a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0019] Example 1
[0020] The method embodiment provided in the first embodiment of the present application can be executed in a mobile phone, tablet, game console, server, computer or similar electronic terminal. Taking running on a computer as an example, Figure 1 1 is a hardware structure block diagram of a multi-threaded pathfinding computer according to an embodiment of the present invention. Figure 1 As shown, the computer may include one or more ( Figure 1 Only one is shown in the figure) a processor 102 (the processor 102 may include but is not limited to a processing device such as a microprocessor MCU or a programmable logic device FPGA) and a memory 104 for storing data. Optionally, the computer may also include a transmission device 106 and an input / output device 108 for communication functions. It can be understood by those skilled in the art that Figure 1 The structure shown is only for illustration and does not limit the structure of the above-mentioned computer. Figure 1 More or fewer components as shown, or with Figure 1 Different configurations are shown.
[0021] The memory 104 can be used to store computer programs, for example, software programs and modules of application software, such as a computer program corresponding to a multi-threaded pathfinding method in an embodiment of the present invention. The processor 102 executes various functional applications and data processing by running the computer program stored in the memory 104, that is, the above method is implemented. The memory 104 may include a high-speed random access memory, and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some examples, the memory 104 may further include a memory remotely arranged relative to the processor 102, and these remote memories may be connected to the computer via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and a combination thereof. In this embodiment, the processor 102 is used to respond to human-computer interaction instructions and game strategies, and control the target virtual character to perform specified operations to complete the game task. The memory 104 is used to store program scripts, configuration information, attribute information of virtual characters, etc. of electronic games.
[0022] The transmission device 106 is used to receive or send data via a network. The specific example of the above network may include a wireless network provided by a communication provider of the computer. In one example, the transmission device 106 includes a network adapter (Network Interface Controller, referred to as NIC), which can be connected to other network devices through a base station so as to communicate with the Internet. In one example, the transmission device 106 can be a radio frequency (RF) module, which is used to communicate with the Internet wirelessly.
[0023] Optionally, the input and output device 108 further includes a human-computer interaction screen for obtaining human-computer interaction instructions through a human-computer interaction interface, and for presenting a rendered scene image;
[0024] In this embodiment, a multi-threaded path finding method is provided. Figure 2 is a flowchart of a multi-threaded pathfinding method according to an embodiment of the present invention. Figure 2 As shown, the process includes the following steps:
[0025] Step S202, responding to the path-finding instruction of the game scene, obtaining path-finding data of the path-finding instruction, wherein the path-finding data includes a path-finding starting point, a path-finding end point, and scene path-finding data of the game scene;
[0026] Optionally, this embodiment can be applied to virtual game scenes, interactive animations, movies, etc., which include virtual scenes with several objects. The virtual game scene can be a 3D game scene including complex terrain.
[0027] The pathfinding data of this embodiment includes the pathfinding starting point and pathfinding end point, as well as scene pathfinding data and pointers to waypoint data, wherein the scene pathfinding data is pre-constructed, such as constructing scene pathfinding data based on collision bodies and pre-made bounding boxes in the game scene, and the scene pathfinding data includes scene coordinates and walkable ranges. The scene pathfinding data is a collection of specific area passability data in the game scene, which is used to determine whether the current path is traversable. Waypoint data is the location of preset waypoints in the game scene, and connectivity data, which is used to obtain the path between any two waypoints.
[0028] Step S204, constructing a pathfinding data block using the pathfinding data, wherein each pathfinding data block corresponds to a pathfinding path;
[0029] The path-finding data block of this embodiment is a fixed memory space. The data such as scenes required for path-finding are merged into a whole block of memory, which can reduce CPU cache failures to reduce performance loss and optimize computing performance.
[0030] Step S206, creating multiple pathfinding threads according to the number of pathfinding data blocks, and using the multiple pathfinding threads to perform scene pathfinding to obtain a pathfinding path from the pathfinding starting point to the pathfinding end point.
[0031] Through the above steps, the pathfinding instructions of the game scene are responded to, and the pathfinding data of the pathfinding instructions are obtained, wherein the pathfinding data includes a pathfinding starting point and a pathfinding end point. The pathfinding data is used to construct a pathfinding data block, and multiple pathfinding threads are created according to the number of pathfinding data blocks. The scene pathfinding is performed using multiple pathfinding threads to obtain a pathfinding path from the pathfinding starting point to the pathfinding end point. By constructing a pathfinding data block, the pathfinding data is merged as a whole into a whole block of memory, which can reduce the performance loss of CPU cache failure during the pathfinding calculation process based on the pathfinding data, optimize the computing performance, and create multiple pathfinding threads according to the number of pathfinding data blocks. The frame rate of the overall pathfinding calculation is reduced, the amount of calculation in the pathfinding process is dispersed to multiple threads, and multi-threading is fully utilized to reduce the frame rate, which solves the technical problem of high frame rate during pathfinding in related technologies, alleviates game freezes, and improves user experience.
[0032] In this embodiment, after constructing the path-finding data of the path-finding instruction, it also includes: searching for a waypoint set that meets preset conditions according to the path-finding starting point and the path-finding end point; performing path-finding calculations on the waypoint network based on the waypoint set to obtain a path chain of the path-finding data.
[0033] The preset condition of this embodiment can be the optimal path (shortest path) or a relatively optimal path (the path is a straight line, the path has less than a predetermined number of path points). For the starting point and end point of the path finding, find the waypoints with better conditions or the optimal waypoints to reduce the traversal complexity.
[0034] In this embodiment, the pathfinding data also includes scene waypoint data, which is waypoint information pre-constructed according to the scene pathfinding data, including the connection relationship between waypoints and scene coordinate waypoints, and an upper-layer pathfinding network, i.e., a waypoint network, is generated according to the connection relationship. First, the scene pathfinding data is constructed according to the collision body and the pre-made bounding box in the scene, and then the scene waypoint data is constructed according to the logical roads, bridges, etc. in the scene.
[0035] In an embodiment of waypoint pathfinding, pathfinding calculations are performed on a waypoint network based on a waypoint set to obtain a path chain of pathfinding data, including: looping the following steps until pathfinding is completed: selecting any starting waypoint from the starting waypoint set and adding it to the expanded waypoint set; determining whether the starting waypoint is in the terminal waypoint set; if the starting waypoint is in the terminal waypoint set, pathfinding is completed; if the starting waypoint is not in the terminal waypoint set, selecting the optimal waypoint in the current expanded waypoint set, adding adjacent points of the optimal waypoint that are not in the expanded waypoint set or in the closed waypoint set to the expanded waypoint set, and adding the optimal waypoint to the closed waypoint set; wherein the waypoint set includes the starting waypoint set, the expanded waypoint set, the terminal waypoint set, and the closed waypoint set; after pathfinding is completed, the closed waypoint set is used to construct a path chain of pathfinding data.
[0036] The starting point waypoint set, the expanded waypoint set, the terminal waypoint set, and the closed waypoint set of this embodiment are all lists of waypoint nodes in the waypoint pathfinding process, such as the expanded waypoint set (OpenList): a waypoint node set consisting of nodes to be searched in the pathfinding process, the closed waypoint set (CloseList): a waypoint node set consisting of nodes that have been searched in the pathfinding process, the starting point waypoint set is the starting waypoint node set in the pathfinding process, and the terminal waypoint set is the ending waypoint node set in the pathfinding process. The node of this embodiment refers to the grid on the grid map of the waypoint network, which is the smallest unit of pathfinding search. Taking the grid map constructed based on squares as an example, each node is a square.
[0037] In an implementation scenario of this embodiment, the pathfinding data includes a suitable waypoint set that meets preset conditions, and the pathfinding calculation process based on the waypoint set on the waypoint network is as follows:
[0038] Select any waypoint from the starting waypoint set and add it to the expanded waypoint set;
[0039] Determine whether the waypoint added to the expanded waypoint set is in the end waypoint set;
[0040] If it is in it, the pathfinding ends; the current expanded waypoint set has no intersection with the destination waypoint set, and the optimal waypoint is selected from the current expanded waypoint set to expand and continue the pathfinding. Optionally, the optimal waypoint is the waypoint closest to the destination in Euclidean order;
[0041] Add the expanded waypoints to the closed waypoint set and continue looping until the pathfinding is completed.
[0042] After the waypoint pathfinding is completed, the information in the closed waypoint set is traced back from the found end waypoint until the waypoint in the starting waypoint set is reached, forming a path chain. The constructed path chain is returned for subsequent scene pathfinding pruning.
[0043] During the waypoint pathfinding process, when expanding on the waypoint, it supports building a data block of the waypoint data and the start and end point set, sending it to the child thread for pathfinding calculation. The pathfinding process in the waypoint space is also multi-threaded. The calculation is similar to scene pathfinding. The starting point, end point, waypoint data, result set space and other data of the waypoint are built into a data block and sent to the child thread for multi-threaded waypoint pathfinding calculation. Multiple pathfinding can be performed in the same frame to reduce the frame ratio of waypoint pathfinding.
[0044] By adopting the solution of this embodiment, waypoints are used to calculate and prune the pathfinding algorithm, the amount of calculation in the scene pathfinding process is reduced, and the pathfinding efficiency is improved.
[0045] In one implementation of this embodiment, using the pathfinding data to construct the pathfinding data block includes:
[0046] S11, determining whether there is a path chain in the path finding data, wherein the path chain includes a plurality of pruning waypoints between the path finding starting point and the path finding end point;
[0047] S12, if the path finding data has a path chain, use the path chain and the path finding data to construct multiple path finding data blocks; if the path finding data does not have a path chain, use the path finding data to construct a path finding data block.
[0048] In one example, the path finding data contains a path chain, and the path chain and the path finding data are used to construct multiple path finding data blocks, including: constructing a starting point path finding data block with the path finding starting point as the starting point and the path chain starting point as the end point; constructing an nth intermediate path finding data block with the nth path link point as the starting point and the n+1th path link point as the end point, where n∈[1, m-1], m is the number of waypoints in the path chain; constructing an end point path finding data block with the path chain end point as the starting point and the path finding end point as the end point; wherein the multiple path finding data blocks include: a starting point path finding data block, m-1 intermediate path finding data blocks, and an end point path finding data block. Both m and n are positive integers.
[0049] If there is a path chain, from the starting point to the starting waypoint, from the starting waypoint to the first section of the path chain, from the first section to the second section, and so on, finally from the end of the path chain to the end of the path finding, multiple path finding data blocks are constructed. Take m as 3 as an example, the path chain includes 3 waypoints, namely B→C→D, the path finding starting point of the scene path finding is A, and the path finding end point of the scene path finding is E. Then, starting point path finding data block 1 is constructed with A as the starting point and B as the end point, starting point path finding data block 2 is constructed with B as the starting point and C as the end point, starting point path finding data block 3 is constructed with C as the starting point and D as the end point, and starting point path finding data block 4 is constructed with D as the starting point and E as the end point, for a total of 4 path finding data blocks.
[0050] In another example, if there is no path chain in the pathfinding data, the scene pathfinding is performed directly, and a pathfinding data block is constructed using the pathfinding data, including: if there is no path chain in the pathfinding data, directly use the pathfinding starting point as the starting point, the pathfinding end point as the end point, the scene pathfinding data, and the result set space to construct a pathfinding data block. The result set space of this embodiment is a whole block of pre-allocated memory passed in as the result return space, which is used to store the path chain returned by a single child thread during subsequent scene pathfinding, and the path chain is copied therein as the return value. If there are multiple pathfinding data blocks, the main thread integrates the results in the result set space of each child thread to construct the overall path.
[0051] In this embodiment, no matter whether one or multiple pathfinding data blocks are constructed, each pathfinding data block includes a starting point and an end point (the starting point and the end point may be the starting point and the end point of the entire scene pathfinding, or the starting point and the end point of a certain section in the scene pathfinding), scene pathfinding data, and a result set space.
[0052] In one implementation of this embodiment, creating multiple pathfinding threads according to the number of pathfinding data blocks, and using the multiple pathfinding threads to perform scene pathfinding includes:
[0053] S21, counting the number of pathfinding data blocks;
[0054] S22, if there are multiple pathfinding data blocks, create a main thread and multiple pathfinding sub-threads, and copy the pathfinding data blocks as a whole to the corresponding pathfinding sub-threads, wherein each pathfinding sub-thread corresponds to one or more pathfinding data blocks;
[0055] In this embodiment, multiple sub-threads are constructed according to the number of path-finding data blocks, and each path-finding sub-thread can process one path-finding data block. If there are more path-finding data blocks (greater than the preset number), each path-finding sub-thread can also process multiple path-finding data blocks, and the data blocks are copied to the sub-threads for calculation. The data copied to the corresponding sub-threads include the starting point and end point data of the corresponding path-finding data block, and the scene path-finding data and the pointer of the result set space will also be copied to the sub-threads.
[0056] By creating multiple sub-threads, the computing workload is dispersed among multiple sub-threads, and multi-threading is fully utilized to reduce the frame rate, alleviate game lag, and improve user experience.
[0057] Optionally, after creating multiple path-finding sub-threads, the method further includes: configuring the execution order of each path-finding sub-thread in the multiple path-finding sub-threads.
[0058] By configuring the execution order of each pathfinding sub-thread, asynchronous pathfinding of multiple pathfinding sub-threads can be achieved. The sub-threads do not need to perform calculations synchronously immediately, but can wait for several frames to be calculated successively or calculate when the results are needed. This can effectively split the calculation pressure and significantly reduce the frame rate.
[0059] S23, in a single path-finding sub-thread, iteratively execute the following steps until a sub-path chain is constructed: add the starting point to the expansion set; determine whether the starting point has an endpoint; if the starting point has an endpoint, use the closed set to construct the sub-path chain of the current path-finding sub-thread; if the starting point has no endpoint, select the point closest to the endpoint as the expansion point, traverse the adjacent points of the expansion point that are neither in the expansion set nor in the closed set; add the adjacent points to the expansion set, add the expansion point to the closed set, and determine the next starting point;
[0060] S23, after the iteration is completed, the sub-path chain obtained by the current path-finding sub-thread is copied to the result set space;
[0061] S24, after all the path-finding sub-threads have completed iteration, the corresponding sub-path chains are copied from the result set space of all the path-finding sub-threads to the result set space of the main thread and integrated into the overall path chain.
[0062] In this embodiment, one or more pathfinding sub-threads are constructed according to the number of pathfinding data blocks, and the pathfinding data blocks are copied to the sub-threads for calculation and execution of the pathfinding process. In an implementation scenario of this embodiment, for a single sub-thread, the overall process of scene pathfinding includes:
[0063] First, add the starting point to the expansion set;
[0064] Determine whether the newly added point has an end point;
[0065] If there is an end point, build a single thread path chain; if there is no end point, select the point with the closest Euclidean distance to the end point as the expansion point, traverse its adjacent points that are not in the expansion set or the closed set, and traverse the adjacent points to update the expansion set and the closed set. The adjacent points that are neither in the expansion set nor in the closed set will be added to the expansion set for subsequent expansion;
[0066] Add the adjacent points to the expansion set, continue to expand the path, continue to iterate, determine whether there is an end point, until there is an end point, and build a single thread path chain;
[0067] After the calculation is completed and the path chain is constructed, the entire path chain is copied to the result set space of the child thread;
[0068] The main thread detects that all child threads have completed the operation, copies the sub-path chain from the result set space of the child thread back, and integrates all the sub-path chains of all child threads to build the overall path chain.
[0069] The solution of this embodiment can be applied to the A* pathfinding algorithm and its improved algorithm based on A* pathfinding. Waypoints are used to prune the A* pathfinding algorithm calculations to reduce the amount of calculations. At the same time, the scene data required for pathfinding is merged into a whole block of memory to reduce CPU cache failures to reduce performance loss and optimize computing performance. At the same time, the amount of calculation is dispersed into multiple threads, and multi-threading is fully utilized to reduce the frame rate. Figure 3 It is an implementation flow chart of an embodiment of the present invention, including:
[0070] Construct scene waypoint data, and construct pathfinding data in the scene based on the collision body and prefabricated bounding box in the scene. The pathfinding data includes scene coordinates and walkable range.
[0071] Construct scene waypoint data, and construct waypoint scene data based on scene pathfinding data, including the connection relationship between scene coordinate waypoints, and generate an upper-level pathfinding network based on the connection relationship.
[0072] Detect the pathfinding instruction and start pathfinding. During one pathfinding, the following operations are generated:
[0073] Build the pathfinding data in the main thread of the current frame, including the pathfinding starting point, pathfinding end point, scene pathfinding data, pointers to waypoint data, etc.
[0074] For the path finding starting point and path finding end point, look for waypoints with better conditions (such as being able to reach in a straight line or being close in distance) or the best waypoints to reduce the traversal complexity.
[0075] If there is a suitable waypoint set, the waypoint pathfinding is performed on the waypoint network. The calculation process is as follows:
[0076] ⅰ. Select any waypoint from the starting waypoint set and add it to the expanded waypoint set;
[0077] ⅱ. Determine whether the waypoint added to the expansion set is in the end waypoint set. If so, end the pathfinding;
[0078] ⅲ. If the current expansion set has no intersection with the destination waypoint set, select the best waypoint among the waypoints (such as the waypoint closest to the destination in Euclidean terms) to perform expansion pathfinding;
[0079] iv. Add the expanded waypoints to the closed waypoint set;
[0080] ⅴ, start from the process in ii above and repeat the process in a loop;
[0081] ⅵ. Starting from the found end waypoint, trace back the information in the closed waypoint set until reaching the waypoint in the starting waypoint set, forming a path chain.
[0082] ⅶ. Return the constructed path chain for subsequent scene pathfinding and pruning.
[0083] If no suitable waypoint set is found, the scene pathfinding is performed directly.
[0084] The scene pathfinding process is as follows:
[0085] ⅰ. If there is no path chain, directly construct the path finding data block with the starting point, end point, scene path finding data, and result set space;
[0086] ii. If there is a path chain, from the starting point to the starting waypoint, from the starting waypoint to the first section of the path chain, from the first section to the second section, and so on, finally from the end point of the path chain to the end point of the path finding, construct multiple path finding data blocks;
[0087] ⅲ. According to the number of path-finding data blocks, build multiple sub-threads and copy the entire data block to the sub-thread for calculation.
[0088] iv. For each single sub-thread, first add the starting point to the expansion set;
[0089] ⅴ. Determine whether the newly added point has an end point;
[0090] ⅵ. If there is an end point, build a single thread path chain;
[0091] ⅶ. If there is no end point, select the point with the shortest Euclidean distance from the end point as the expansion point, and traverse its adjacent points that are neither in the expansion set nor in the closed set;
[0092] ⅷ. Add these adjacent points to the expansion set and jump back to step v for iteration;
[0093] ⅸ. After the calculation is completed and the path chain is constructed, the entire path chain is copied to the result set space;
[0094] ⅹ. The main thread detects that all child threads have completed the operation, copies the result set space of the child threads back to the child path chain, integrates and builds the overall path chain, and completes a path search.
[0095] This embodiment can use the Burst compiler to compile the path-finding logic code, which can effectively utilize the SIMD (Single Instruction Multiple Data) characteristics of the available CPU and greatly improve the code execution efficiency.
[0096] The solution of this embodiment can effectively improve the pathfinding efficiency, reduce the pathfinding frame rate, and reduce the freeze. It has low requirements on the performance of the player's device and a low threshold, and can improve the performance of the character's pathfinding process on low-end machines.
[0097] Through the description of the above implementation methods, those skilled in the art can clearly understand that the method according to the above embodiment can be implemented by means of software plus a necessary general hardware platform, and of course can also be implemented by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of the present invention, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, a magnetic disk, or an optical disk), and includes a number of instructions for enabling a terminal device (which can be a mobile phone, a computer, a server, or a network device, etc.) to execute the methods described in each embodiment of the present invention.
[0098] Example 2
[0099] In this embodiment, a multi-threaded pathfinding device is also provided to implement the above-mentioned embodiments and preferred implementation modes, which have been described and will not be repeated. As used below, the term "module" can be a combination of software and / or hardware that implements a predetermined function. Although the device described in the following embodiments is preferably implemented in software, the implementation of hardware, or a combination of software and hardware, is also possible and conceivable.
[0100] Figure 4 is a structural block diagram of a multi-threaded path-finding device according to an embodiment of the present invention. Figure 4 As shown, the device includes: an acquisition module 40, a construction module 42, and a first path finding module 44, wherein:
[0101] An acquisition module 40 is used to respond to a path-finding instruction of a game scene and acquire path-finding data of the path-finding instruction, wherein the path-finding data includes a path-finding starting point, a path-finding end point, and scene path-finding data of the game scene;
[0102] A construction module 42, configured to construct a pathfinding data block using the pathfinding data, wherein each pathfinding data block corresponds to a pathfinding path;
[0103] The first pathfinding module 44 is used to create multiple pathfinding threads according to the number of the pathfinding data blocks, and use the multiple pathfinding threads to perform pathfinding to obtain a pathfinding path from the pathfinding starting point to the pathfinding end point.
[0104] Optionally, the construction module includes: a judgment unit, used to judge whether there is a path chain for the path finding data, wherein the path chain includes a plurality of pruning path points between the path finding starting point and the path finding end point; a construction unit, used to construct a plurality of path finding data blocks using the path chain and the path finding data if there is a path chain for the path finding data; and to construct a path finding data block using the path finding data if there is no path chain for the path finding data.
[0105] Optionally, the construction unit includes: a first construction unit, used to construct a starting point pathfinding data block with the pathfinding starting point as the starting point and the path chain starting point as the end point; a second construction unit, used to construct an nth intermediate pathfinding data block with the nth path link point as the starting point and the n+1th path link point as the end point, wherein n∈[1, m-1], m is the number of waypoints in the path chain; a third construction unit, used to construct an end point pathfinding data block with the path chain end point as the starting point and the pathfinding end point as the end point; wherein the multiple pathfinding data blocks include: the starting point pathfinding data block, m-1 intermediate pathfinding data blocks, and the end point pathfinding data block.
[0106] Optionally, the method further includes: a search module, used to search for a waypoint set that meets preset conditions according to the path finding starting point and the path finding end point after the path finding data of the path finding instruction is constructed by the construction module; and a second path finding module, used to perform path finding calculations in a waypoint network based on the waypoint set to obtain a path chain of the path finding data.
[0107] Optionally, the second pathfinding module includes: a loop unit, which is used to loop the following steps until the pathfinding is completed: select any starting waypoint from the starting waypoint set and add it to the expanded waypoint set; determine whether the starting waypoint is in the terminal waypoint set; if the starting waypoint is in the terminal waypoint set, the pathfinding is completed; if the starting waypoint is not in the terminal waypoint set, select the optimal waypoint in the current expanded waypoint set, add the adjacent points of the optimal waypoint that are not in the expanded waypoint set or the closed waypoint set to the expanded waypoint set, and add the optimal waypoint to the closed waypoint set; wherein the waypoint set includes the starting waypoint set, the expanded waypoint set, the terminal waypoint set, and the closed waypoint set; a construction unit, which is used to use the closed waypoint set to construct a path chain of the pathfinding data after the pathfinding is completed.
[0108] Optionally, the first pathfinding module includes: a counting unit, used to count the number of the pathfinding data blocks; a creation unit, used to create a main thread and multiple pathfinding sub-threads if the number of the pathfinding data blocks is multiple, and copy the pathfinding data blocks as a whole to the corresponding pathfinding sub-threads, wherein each pathfinding sub-thread corresponds to one or more pathfinding data blocks; an iteration unit, used to iteratively execute the following steps in a single pathfinding sub-thread until a sub-path chain is constructed: adding the starting point to the expansion set; determining whether the starting point has an end point; if the starting point has an end point, using the closed set to construct the current pathfinding sub-thread A sub-path chain, if the starting point has no end point, select the point closest to the end point as the expansion point, traverse the adjacent points of the expansion point that are neither in the expansion set nor in the closed set; add the adjacent points to the expansion set, add the expansion point to the closed set, and determine the next starting point; a copy unit, used to copy the sub-path chain obtained by the current path-finding sub-thread to the result set space after the iteration is completed; an integration unit, used to copy the corresponding sub-path chains from the result set space of all path-finding sub-threads to the result set space of the main thread after all path-finding sub-threads have completed the iteration, and integrate them into the overall path chain.
[0109] Optionally, the first pathfinding module further includes: a configuration unit, configured to configure the execution order of each pathfinding sub-thread in the multiple pathfinding sub-threads after the creation unit creates the multiple pathfinding sub-threads.
[0110] It should be noted that the above modules can be implemented by software or hardware. For the latter, it can be implemented in the following ways, but not limited to: the above modules are all located in the same processor; or the above modules are located in different processors in any combination.
[0111] Example 3
[0112] The present application also provides an electronic device, Figure 5 is a structural diagram of an electronic device according to an embodiment of the present invention, such as Figure 5 As shown, it includes a processor 51, a communication interface 52, a memory 53 and a communication bus 54, wherein the processor 51, the communication interface 52, and the memory 53 communicate with each other through the communication bus 54, and the memory 53 is used to store computer programs;
[0113] The processor 51 is used to execute the program stored in the memory 53, and implements the following steps: responding to the pathfinding instruction of the game scene, obtaining the pathfinding data of the pathfinding instruction, wherein the pathfinding data includes the pathfinding starting point, the pathfinding end point and the scene pathfinding data of the game scene; using the pathfinding data to construct a pathfinding data block, wherein each pathfinding data block corresponds to a pathfinding path; creating multiple pathfinding threads according to the number of the pathfinding data blocks, and using the multiple pathfinding threads to perform scene pathfinding to obtain the pathfinding path from the pathfinding starting point to the pathfinding end point.
[0114] Optionally, using the pathfinding data to construct a pathfinding data block includes: determining whether there is a path chain in the pathfinding data, wherein the path chain includes a plurality of pruning points between the pathfinding starting point and the pathfinding end point; if there is a path chain in the pathfinding data, using the path chain and the pathfinding data to construct a plurality of pathfinding data blocks; if there is no path chain in the pathfinding data, using the pathfinding data to construct a pathfinding data block.
[0115] Optionally, using the path chain and the pathfinding data to construct multiple pathfinding data blocks includes: constructing a starting point pathfinding data block with the pathfinding starting point as the starting point and the path chain starting point as the end point; constructing an nth intermediate pathfinding data block with the nth path link point as the starting point and the n+1th path link point as the end point, wherein n∈[1, m-1], m is the number of waypoints in the path chain; constructing an end point pathfinding data block with the path chain end point as the starting point and the pathfinding end point as the end point; wherein the multiple pathfinding data blocks include: the starting point pathfinding data block, m-1 intermediate pathfinding data blocks, and the end point pathfinding data block.
[0116] Optionally, after constructing the path-finding data of the path-finding instruction, the method further includes: searching for a waypoint set that meets preset conditions according to the path-finding starting point and the path-finding end point; performing path-finding calculations in a waypoint network based on the waypoint set to obtain a path chain of the path-finding data.
[0117] Optionally, performing pathfinding calculations on a waypoint network based on the waypoint set to obtain a path chain for the pathfinding data includes: looping the following steps until pathfinding is completed: selecting any starting waypoint from the starting waypoint set and adding it to the expanded waypoint set; determining whether the starting waypoint is in the terminal waypoint set; if the starting waypoint is in the terminal waypoint set, pathfinding is completed; if the starting waypoint is not in the terminal waypoint set, selecting an optimal waypoint in the current expanded waypoint set, adding adjacent points of the optimal waypoint that are neither in the expanded waypoint set nor in the closed waypoint set to the expanded waypoint set, and adding the optimal waypoint to the closed waypoint set; wherein the waypoint set includes the starting waypoint set, the expanded waypoint set, the terminal waypoint set, and the closed waypoint set; after pathfinding is completed, the closed waypoint set is used to construct the path chain for the pathfinding data.
[0118] Optionally, multiple pathfinding threads are created according to the number of the pathfinding data blocks, and the multiple pathfinding threads are used to perform scene pathfinding, including: counting the number of the pathfinding data blocks; if the number of the pathfinding data blocks is multiple, creating a main thread and multiple pathfinding sub-threads, and copying the pathfinding data blocks as a whole to the corresponding pathfinding sub-threads, wherein each pathfinding sub-thread corresponds to one or more pathfinding data blocks; in a single pathfinding sub-thread, iteratively executing the following steps until a sub-path chain is constructed: adding the starting point to the expansion set; determining whether the starting point has an end point; if the starting point has an end point, using the closing The closed set constructs the sub-path chain of the current pathfinding sub-thread. If the starting point has no end point, select the point closest to the end point as the expansion point, and traverse the adjacent points of the expansion point that are neither in the expansion set nor in the closed set; add the adjacent points to the expansion set, add the expansion points to the closed set, and determine the next starting point; after the iteration is completed, copy the sub-path chain obtained by the current pathfinding sub-thread to the result set space; after all pathfinding sub-threads have completed the iteration, copy the corresponding sub-path chains from the result set space of all pathfinding sub-threads to the result set space of the main thread, and integrate them into the overall path chain.
[0119] Optionally, after creating multiple path-finding sub-threads, the method further includes: configuring the execution order of each path-finding sub-thread in the multiple path-finding sub-threads.
[0120] The communication bus mentioned in the above terminal can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The communication bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus.
[0121] The communication interface is used for communication between the above terminal and other devices.
[0122] The memory may include a random access memory (RAM) or a non-volatile memory, such as at least one disk memory. Optionally, the memory may also be at least one storage device located away from the aforementioned processor.
[0123] The above-mentioned processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.
[0124] In another embodiment provided in the present application, a computer-readable storage medium is also provided, in which instructions are stored. When the computer-readable storage medium is executed on a computer, the computer executes the multi-threaded pathfinding method described in any one of the above embodiments.
[0125] In another embodiment provided by the present application, a computer program product including instructions is also provided, which, when executed on a computer, enables the computer to execute the multi-threaded pathfinding method described in any one of the above embodiments.
[0126] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented using software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present application is generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions may be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium, for example, the computer instructions may be transmitted from a website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer, server or data center. The computer-readable storage medium may be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrated. The available medium may be a magnetic medium, (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state drive Solid State Disk (SSD)), etc.
[0127] The serial numbers of the above-mentioned embodiments of the present application are for description only and do not represent the advantages or disadvantages of the embodiments.
[0128] In the above embodiments of the present application, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, please refer to the relevant description of other embodiments.
[0129] In the several embodiments provided in this application, it should be understood that the disclosed technical content can be implemented in other ways. Among them, the device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of units or modules, which can be electrical or other forms.
[0130] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0131] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0132] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application, in essence, or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: U disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), mobile hard disk, disk or optical disk and other media that can store program codes.
[0133] The above is only a preferred implementation of the present application. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principles of the present application. These improvements and modifications should also be regarded as the scope of protection of the present application.
Claims
1. A multi-threaded pathfinding method, characterized in that: include: In response to a pathfinding instruction of a game scene, pathfinding data of the pathfinding instruction is obtained, wherein the pathfinding data includes a pathfinding starting point, a pathfinding end point, and scene pathfinding data of the game scene; Using the pathfinding data to construct a pathfinding data block, wherein each pathfinding data block corresponds to a pathfinding path; A plurality of pathfinding threads are created according to the number of the pathfinding data blocks, and the plurality of pathfinding threads are used to perform scene pathfinding to obtain a pathfinding path from the pathfinding starting point to the pathfinding end point.
2. The method according to claim 1, characterized in that: Using the path-finding data to construct a path-finding data block includes: Determine whether there is a path chain in the path finding data, wherein the path chain includes a plurality of pruning waypoints between the path finding starting point and the path finding end point; If the path-finding data has a path chain, the path chain and the path-finding data are used to construct a plurality of path-finding data blocks; if the path-finding data does not have a path chain, the path-finding data is used to construct a single path-finding data block.
3. The method according to claim 2, characterized in that Using the path chain and the path finding data to construct a plurality of path finding data blocks includes: The starting point of the path finding is taken as the starting point and the starting point of the path chain is taken as the end point to construct the starting point path finding data block; Construct the nth intermediate pathfinding data block with the nth path link point as the starting point and the n+1th path link point as the end point, where n∈[1,m-1], m is the number of waypoints in the path link; Take the end point of the path chain as the starting point and the pathfinding end point as the end point to build the end point pathfinding data block; The multiple path-finding data blocks include: the starting point path-finding data block, m-1 intermediate path-finding data blocks, and the end point path-finding data block.
4. The method according to claim 1, characterized in that: After constructing the path-finding data of the path-finding instruction, the method further comprises: Searching for a waypoint set that meets preset conditions according to the path finding starting point and the path finding end point; A path finding calculation is performed on a waypoint network based on the waypoint set to obtain a path chain of the path finding data.
5. The method according to claim 4, characterized in that Performing pathfinding calculation on the waypoint network based on the waypoint set to obtain the path chain of the pathfinding data includes: The following steps are executed in a loop until the pathfinding is finished: any starting point is selected from the starting point set and added to the expanded point set; whether the starting point is in the terminal point set is determined; if the starting point is in the terminal point set, the pathfinding is finished; if the starting point is not in the terminal point set, the optimal point is selected from the current expanded point set, the adjacent points of the optimal point that are not in the expanded point set and the closed point set are added to the expanded point set, and the optimal point is added to the closed point set; wherein the point set includes the starting point set, the expanded point set, the terminal point set, and the closed point set; After the path finding is completed, the closed waypoint set is used to construct a path chain of the path finding data.
6. The method according to claim 1, characterized in that Creating a plurality of pathfinding threads according to the number of the pathfinding data blocks, and using the plurality of pathfinding threads to perform scene pathfinding includes: Counting the number of the path-finding data blocks; If there are multiple pathfinding data blocks, create a main thread and multiple pathfinding sub-threads, and copy the pathfinding data blocks as a whole to the corresponding pathfinding sub-threads, wherein each pathfinding thread corresponds to one or more pathfinding data blocks; In a single path-finding sub-thread, iteratively execute the following steps until a sub-path chain is constructed: add the starting point to the expansion set; determine whether the starting point has an endpoint; if the starting point has an endpoint, use the closed set to construct the sub-path chain of the current path-finding sub-thread; if the starting point has no endpoint, select the point closest to the endpoint as the expansion point, traverse the adjacent points of the expansion point that are neither in the expansion set nor in the closed set; add the adjacent points to the expansion set, add the expansion point to the closed set, and determine the next starting point; After the iteration is completed, the sub-path chain found by the current path-finding sub-thread is copied to the result set space; After all the path-finding sub-threads have completed iteration, the corresponding sub-path chains are copied from the result set spaces of all the path-finding sub-threads to the result set space of the main thread and integrated into the overall path chain.
7. The method according to claim 6, characterized in that After creating multiple path-finding sub-threads, the method further includes: An execution order of each path-finding sub-thread in the plurality of path-finding sub-threads is configured.
8. A multi-threaded pathfinding device, characterized in that: include: An acquisition module, used for responding to a pathfinding instruction of a game scene, and acquiring pathfinding data of the pathfinding instruction, wherein the pathfinding data includes a pathfinding starting point, a pathfinding end point, and scene pathfinding data of the game scene; A construction module, used to construct a pathfinding data block using the pathfinding data, wherein each pathfinding data block corresponds to a pathfinding path; The first pathfinding module is used to create multiple pathfinding threads according to the number of the pathfinding data blocks, and use the multiple pathfinding threads to perform pathfinding to obtain a pathfinding path from the pathfinding starting point to the pathfinding end point.
9. A storage medium, characterized in that: The storage medium stores a computer program, wherein the computer program is configured to execute the method according to any one of claims 1 to 7 when executed.
10. An electronic device comprising a memory and a processor, characterized in that: A computer program is stored in the memory, and the processor is configured to run the computer program to perform the method according to any one of claims 1 to 7.
