Simple tile based line of site for strategy game?

Started by
16 comments, last by Dragonsoulj 10 years, 6 months ago

Burrick, I took a look at Bresenham, but wouldn't that have speed problems? As I see it, I'd have to run the Bresenham routine for every square within range of one of the player's units. I can't even limit it to testing tiles that have units on them, since I need to know if I tile is visible so I can color it black to show it's not in the line of sight. Is that kind of algorithm feasible from a speed standpoint?

If your tile max search range is limited you can prebuild an array of all relevant sweep vectors (all lines of sight) which would be stepped thru from th eorigin point outward (brute force). I did this for games with tile interactions/distances of 32 --- effectively 32+32 sweep vectors (of 32 steps each) per quadrant and reuse of that vector table switch x for y appropriately for all 4 quadrants. For smaller such pattern I even simply created static array values for the sweep/stepping for the search.

--------------------------------------------[size="1"]Ratings are Opinion, not Fact
Advertisement

I'm developing a turn based strategy game, and I'm trying to figure out the best way to figure out what tiles are visible to the users. So if the user has say, 50 units in their army, they can see any tile that any of those 50 units has a line of sight to that isn't blocked by an obstacle tile. I'm looking for something that's not too inefficient, since it's a server based game and I'd rather not bog down the server with a lot of calculations. Can anyone point me to some resources, if possible some code? It's in C#, and would be done entirely in an asp.net project (so no access to DirectX or XNA) libraries, etc.

Thanks!

I'd like to add that, in a tiled turn-based strategy game, line of sight is pretty much up to you to design. A turn-based strategy game is fairly abstract. You're basically making a fancy board game, not a simulation. I'd look at the rules for board games for inspiration, and then figure out how to port a board game mechanic I like into software.

For example, I remember one system with a pretty simple line-of-sight rule - the D&D miniatures game. I think it's got a slightly different name and set of rules now but I'm sure Wizards of the Coast still hosts the rulebook for free somewhere on its website. I think the rule is that you can see another square if you can draw a line from any corner of your square to any corner of the target square without passing over an obstacle square. I think you could see past corners as long as both sides of the corner are not blocked. If you cannot picture that, I'm sure if you find a rulebook there will be diagrams. So, to brute-force the test, all you need is to intersect a line and a square, and then iterate over corners.

My main point is that there is no one true algorithm for what you want. You need to play game designer a bit more before you play software engineer. Of course your software engineer half might tell your game designer half that his idea is impossible to program, but currently I think you should focus on what you want the rule to be.


For example, I remember one system with a pretty simple line-of-sight rule - the D&D miniatures game. I think it's got a slightly different name and set of rules now but I'm sure Wizards of the Coast still hosts the rulebook for free somewhere on its website. I think the rule is that you can see another square if you can draw a line from any corner of your square to any corner of the target square without passing over an obstacle square. I think you could see past corners as long as both sides of the corner are not blocked. If you cannot picture that, I'm sure if you find a rulebook there will be diagrams. So, to brute-force the test, all you need is to intersect a line and a square, and then iterate over corners.

Dungeons and Dragons v 3.5 and Pathfinder roughly use that line of sight idea. Each has an system reference document that you can look up their line of sight checks. http://d20srd.com and http://d20pfsrd.com.

http://www.wizards.com/default.asp?x=dnd/glossary&term=Glossary_dnd_lineofsight&alpha=

For Wizard's official ruling.

wodinoneeye, the prebuild idea's a great one. I think it's also a good point that I can decide what the rules are before I code. Definitely it's worth checking out how board games and table top RPGS handle it, particularly since I'm trying for a turn based strategy game based on tiles rather than a real time RTS.

You might also be interested in this wiki article about the field of vision in roguelikes:

http://roguebasin.roguelikedevelopment.org/index.php?title=Field_of_Vision

There are some strategies explained. Perhaps they would fit your game?

I found the ruling for Line of Sight/Effect for Pathfinder:

Line of Effect

A line of effect is a straight, unblocked path that indicates what a spell can affect. A line of effect is canceled by a solid barrier. It's like line of sight for ranged weapons, except that it's not blocked by fog, darkness, and other factors that limit normal sight. A line of effect starts from any corner of your square and extends to the limit of its range or until it strikes a barrier that would block it. A line-shaped spell affects all creatures in squares through which the line passes.

Line of Sight

A line of sight is the same as a Line of Effect but with the additional restriction that that it is blocked by fog, darkness, and other factors that limit normal sight (such as Concealment).

How does that work? Do you do the calculation 4 times, one for each corner, or do you just pick any corner?

I found the ruling for Line of Sight/Effect for Pathfinder:

Line of Effect

A line of effect is a straight, unblocked path that indicates what a spell can affect. A line of effect is canceled by a solid barrier. It's like line of sight for ranged weapons, except that it's not blocked by fog, darkness, and other factors that limit normal sight. A line of effect starts from any corner of your square and extends to the limit of its range or until it strikes a barrier that would block it. A line-shaped spell affects all creatures in squares through which the line passes.

Line of Sight

A line of sight is the same as a Line of Effect but with the additional restriction that that it is blocked by fog, darkness, and other factors that limit normal sight (such as Concealment).

In Dungeons & Dragons, the attacker has to prove he has a clear shot.

To do this, he can choose any one corner of his square and must show that lines from that corner to all corners of the defender's square are clear of obstacles.

It's a pen-and-paper game so the players have to do all the checks. In a computer, you would most likely want to check all four corners of attacker's square with four corners of the defender's square (16 checks total) if you wanted to follow DnD's rules. But you don't have to.

If you look at the link in my signature, you'll see a long since abandoned game where I actually used this very system for determing line of sight and line of effect. In my Sacculus The Wargame turn-based strategy, I also used Bresenham for field of vision, but I did not check individual corners, only entire tiles. It is not computationally expensive because you only move unit at a time and you need not check others (because their positions did not change).

How does that work? Do you do the calculation 4 times, one for each corner, or do you just pick any corner?

I found the ruling for Line of Sight/Effect for Pathfinder:

Line of Effect

A line of effect is a straight, unblocked path that indicates what a spell can affect. A line of effect is canceled by a solid barrier. It's like line of sight for ranged weapons, except that it's not blocked by fog, darkness, and other factors that limit normal sight. A line of effect starts from any corner of your square and extends to the limit of its range or until it strikes a barrier that would block it. A line-shaped spell affects all creatures in squares through which the line passes.

Line of Sight

A line of sight is the same as a Line of Effect but with the additional restriction that that it is blocked by fog, darkness, and other factors that limit normal sight (such as Concealment).

You do check all 4 corners, but only until you find one that works. In Dungeons and Dragons (and Pathfinder), a regular person stands in a 5 feet by 5 feet square. They don't fill up that square, that's just the size that grants unhampered movement, so in essence, they can move and look from (or shoot from) any corner of their square without the game's rules saying that they have moved any non-negligible distance. To help understand, a person can squeeze into an area that is 2.5 feet by 2.5 feet square before getting stuck (ie they can't squeeze into a space any smaller).

This topic is closed to new replies.

Advertisement