GameMaker Get size of overlap

zim32

Member
I'm trying to make a reverse tower defense and I want to calculate amount of track that is within the towers range. So I can check which point has the best coverage for the tower so the ai can pick the best spot to place the tower. Anyone know a good way to do this?
 

Pixel-Team

Master of Pixel-Fu
This is a theoretical approach, so you'd have to code this yourself, but what I would do is iterate through each position on the map, or a reduced portion of the map if you wanted to limit the positions to those along a road or areas where the entities would approach. When you look at a tile, make sure it doesn't have blocking scenery, or is being occupied by an existing tower. Then using that tile and the tower's range, I would calculate the top/left most tile within its circle, and the bottom/right most tile within its circle. I would iterate through those tiles and add a score for each tile that is road. Store this score and the tile you are testing your tower on together. At the end of testing all the tiles on the map, you then iterate through all the tested tiles which contained a score and find the highest score. It there are tests that had the same amount of road, then randomly pick. But this is your optimal tower position. Obviously this is theoretical because I don't have your code here, so you have the hard part. I wish you all the best.
 

zim32

Member
This is a theoretical approach, so you'd have to code this yourself, but what I would do is iterate through each position on the map, or a reduced portion of the map if you wanted to limit the positions to those along a road or areas where the entities would approach. When you look at a tile, make sure it doesn't have blocking scenery, or is being occupied by an existing tower. Then using that tile and the tower's range, I would calculate the top/left most tile within its circle, and the bottom/right most tile within its circle. I would iterate through those tiles and add a score for each tile that is road. Store this score and the tile you are testing your tower on together. At the end of testing all the tiles on the map, you then iterate through all the tested tiles which contained a score and find the highest score. It there are tests that had the same amount of road, then randomly pick. But this is your optimal tower position. Obviously this is theoretical because I don't have your code here, so you have the hard part. I wish you all the best.
I was hoping for a more precise solution for like it if it only covers half a track tile in a spot I just count it as half a point. After all their might end up being a spot where it will barely cover like 4 different tiles corners and it will be less effective than say a spot covering two tiles(depending on tower fire rate and speed of it's enemies).
 

Pixel-Team

Master of Pixel-Fu
I was hoping for a more precise solution for like it if it only covers half a track tile in a spot I just count it as half a point. After all their might end up being a spot where it will barely cover like 4 different tiles corners and it will be less effective than say a spot covering two tiles(depending on tower fire rate and speed of it's enemies).
So for that, you would test the 4 points within a tile against the circle to determine the score
 
Top