简体   繁体   中英

strategy game help: fog of war

im trying to make a real time strategy game such as starcraft or age of empires. my maps woud have to support up to about 1500 entities. My problem arrises with how to implement a fog of war without lagging the game. The method that i initialy tried was to simply caclulate the distance to all surrounding area of a unit everytime it moved but as i expected this lagged since many units would be constantly moving. If anyone knows a faster algorythim for a fog of war please help. the maps would be tile based and stored in an array.

A quite basic implementation can be as follows:

  • Visibility is given by a value v[i,j] for each tile (i,j) . Any value below a certain threshold lies within the fog.

  • The values are updated with regular time steps (note: for such a thing no high accuracy or high frequency is needed besides for very special cases) using the following two steps:

    1. blur the current map v[i,j]
    2. for each unit increase the value v[unit_i, unit_j] by a constant amount. You can also add a constant amount if a unit is on a square (no matter how many of them are there).

Another solution: binning for your entities.

You create a relatively spare grid, or even a quad-tree. Given coordinates (x,y) , it allows you to find in log(d) steps all entities which are in the same (or neighbouring) cells, where d is the depth of your quad-tree.

With some help of ropes (pointers from leaf nodes pointing to adjacent cells), accessing a neighbour can be done in a constant time.

To learn if a given map tile is visible or not, you just need a query into your quad-tree.

Also, a quad-tree may be useful for other tasks unrelated to fog-of-war. Eg you might want to find the nearest "worker" to the given coordinates (x,y) or you want to apply some area-damage to all units in a region.

Every time a unit moves, you can probably assume that it moves to neighboring tile, can't you? In this case you can also assume that visible area of this unit also moves by one tile in the same direction, so you should have no trouble determining nor updating an area that is supposed to be visible. Depending on sight radius of a unit, updating only values that need updating can probably save a lot of CPU power.

The problem would be area that is supposed to be blurred - there can be more units seeing the same tile but this can be solved by doing what Howard suggests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM