# Line of Sight for python plot picking

Discussion in 'Civ4 - Creation & Customization' started by Merkava120, Aug 20, 2016.

1. ### Merkava120Oberleutnant

Joined:
Feb 2, 2013
Messages:
312
Location:
Not Turkey
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?

2. ### Leoreth古典部の会員Moderator

Joined:
Aug 23, 2009
Messages:
32,708
Gender:
Male
Location:
Kamiyama
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.

3. ### Merkava120Oberleutnant

Joined:
Feb 2, 2013
Messages:
312
Location:
Not Turkey
That's exactly what I was looking for!