Author Topic: Automating Line-of-Sight Calculation?  (Read 2615 times)

rocqueforte

  • Corporal
  • *
  • Posts: 60
Automating Line-of-Sight Calculation?
« on: 27 February 2014, 00:29:03 »
Has anyone got an algorithm (preferably easy to understand!) that will calculate the line-of-sight path {1} between two Battletech hexes? I'm not looking for the algorithm to do anything fancy, like take the underlying terrain on the map(s) into account - I just want to know what the valid path is (or paths are) between two hexes without having to put a piece of string between two hexes on a mapboard  :)

I've tried googling for this, and while there are several very good resources out there for doing hex line-of-sight calculations (such as http://www.redblobgames.com/grids/hexagons/), they either use map boards that don't use the same co-ordinate system the BattleTech maps do (and I'd really like to avoid having to convert between two different co-ordinate systems {2}), or the maths does my head in (especially when one solution talks about turning the 2D map board into a 3D cube...ugh!).

{1} Or paths, for those instances where there might be 2 (or more) equally valid paths between two hexes.
For example, when calculating the path between hex 0605 and hex 1005 the paths:
0605 -> 0705 -> 0805 -> 0905 -> 1005
and
0605 -> 0706 -> 0805 -> 0906 -> 1005
are equally valid (and as a side note, one would assume that 0605 -> 0705 -> 0805 -> 0906 -> 1005 would be invalid, since the path either has to favour either all the "above the line" hexes OR all the "below the line" hexes when faced with a choice between two hexes for its path (hope that makes sense!))

{2} In the late 90's, in an effort to teach myself C, I used the algorithm written by Clark Verbrugge {3} to solve this problem, applying a transformation function to convert his hex co-ordinate system to the one Battletech maps use. Unfortunately, looking at the code now, my C code is pretty much "write only"...

{3} See http://www.sable.mcgill.ca/~clump/research.html

Obvious

  • Master Sergeant
  • *
  • Posts: 261
Re: Automating Line-of-Sight Calculation?
« Reply #1 on: 27 February 2014, 00:51:23 »
Well, MegaMek is open-source Java.

This is the LoS code:
http://sourceforge.net/p/megamek/code/HEAD/tree/trunk/megamek/src/megamek/common/LosEffects.java
See the ASCII drawing here:
http://sourceforge.net/p/megamek/code/HEAD/tree/trunk/megamek/src/megamek/common/LosEffects.java#l737

Edit: You are probably actually looking for the "Coords" code.  See starting around line 355:
http://sourceforge.net/p/megamek/code/HEAD/tree/trunk/megamek/src/megamek/common/Coords.java

Not really the easy to understand part, though.
« Last Edit: 27 February 2014, 01:02:20 by Obvious »
It is better to be lucky than an idiot.

rocqueforte

  • Corporal
  • *
  • Posts: 60
Re: Automating Line-of-Sight Calculation?
« Reply #2 on: 03 March 2014, 08:58:18 »
Appears that my google-fu was off the other day when I looked for a solution for this - http://www.webwargaming.org/lineofsight.shtml has a very workable solution to this problem (the solution doesn't always fully work where the line-of-sight just nicks the edges of one or more hexes...for example, when working out line of sight between hexes 0505 and 0401, it correctly plots a path through 0504, 0403, 0503, 0402 and 0401, but reports the range as 4 hexes). Now, just need to find some time to tweak the code to handle multiple paths (e.g. the two equally valid paths between hexes 0204 and 0401), incorporate vertical line-of-sight rules and (most importantly) read in map data so the effects of terrain features can also be computed....hmm, maybe something for me to work on over Xmas :(
« Last Edit: 03 March 2014, 20:06:54 by rocqueforte »

 

Register