Line of Sight for python plot picking

Merkava120

Oberleutnant
Joined
Feb 2, 2013
Messages
450
Location
Socially distant
I know I've been posting an awful lot of python questions, and I've been getting better at figuring them out myself, but this one has me stumped. I don't even know how to approach this with any kind of programming.

I'm experimenting with making a python button to allow units to shoot each other (so that I can modify it whatever way I want) - and I've got most of it figured out. Line of sight, though, is giving me grief.

I need to put some stuff in canpickplot in CvGameUtil to check if something is in between a shooter and the plot you're looking at, and if there is something blocking the shooter, to return false.

I was thinking I'd make a line formula using the target tile's coordinates and the shooter's coordinates and then loop through tiles touching that line - but I've no idea how to tell through programming whether or not a line crosses a tile.

For example, if a shooter's at (3,2) and aiming at a unit on (2,4), a line drawn between the two crosses over (3,3) and (2,3). How do I tell the game that?
 
The game cannot solve this problem for you, you have to implement the line of sight algorithm yourself. How to go about it depends of what your definition of being in line of sight is.

I found this Stackoverflow discussion, which gives a reasonable approach to line of sight on a square grid and links to an algorithm that appears to be rather easy to implement.
 
Top Bottom