* Gets the best target structure for the given unit. * * @param unit The Unit to get the best target for. * @param mode How to determine the best target. * @return The best target or NULL if none found. */
| 2273 | * @return The best target or NULL if none found. |
| 2274 | */ |
| 2275 | static Structure *Unit_FindBestTargetStructure(Unit *unit, uint16 mode) |
| 2276 | { |
| 2277 | Structure *best = NULL; |
| 2278 | uint16 bestPriority = 0; |
| 2279 | tile32 position; |
| 2280 | uint16 distance; |
| 2281 | PoolFindStruct find; |
| 2282 | |
| 2283 | if (unit == NULL) return NULL; |
| 2284 | |
| 2285 | position = Tools_Index_GetTile(unit->originEncoded); |
| 2286 | distance = g_table_unitInfo[unit->o.type].fireDistance << 8; |
| 2287 | |
| 2288 | find.houseID = HOUSE_INVALID; |
| 2289 | find.index = 0xFFFF; |
| 2290 | find.type = 0xFFFF; |
| 2291 | |
| 2292 | while (true) { |
| 2293 | Structure *s; |
| 2294 | tile32 curPosition; |
| 2295 | uint16 priority; |
| 2296 | |
| 2297 | s = Structure_Find(&find); |
| 2298 | if (s == NULL) break; |
| 2299 | if (s->o.type == STRUCTURE_SLAB_1x1 || s->o.type == STRUCTURE_SLAB_2x2 || s->o.type == STRUCTURE_WALL) continue; |
| 2300 | |
| 2301 | curPosition.x = s->o.position.x + g_table_structure_layoutTileDiff[g_table_structureInfo[s->o.type].layout].x; |
| 2302 | curPosition.y = s->o.position.y + g_table_structure_layoutTileDiff[g_table_structureInfo[s->o.type].layout].y; |
| 2303 | |
| 2304 | if (mode != 0 && mode != 4) { |
| 2305 | if (mode == 1) { |
| 2306 | if (Tile_GetDistance(unit->o.position, curPosition) > distance) continue; |
| 2307 | } else { |
| 2308 | if (mode != 2) continue; |
| 2309 | if (Tile_GetDistance(position, curPosition) > distance * 2) continue; |
| 2310 | } |
| 2311 | } |
| 2312 | |
| 2313 | priority = Unit_GetTargetStructurePriority(unit, s); |
| 2314 | |
| 2315 | if (priority >= bestPriority) { |
| 2316 | best = s; |
| 2317 | bestPriority = priority; |
| 2318 | } |
| 2319 | } |
| 2320 | |
| 2321 | if (bestPriority == 0) return NULL; |
| 2322 | |
| 2323 | return best; |
| 2324 | } |
| 2325 | |
| 2326 | /** |
| 2327 | * Get the score of entering this tile from a direction. |
no test coverage detected