* Gets the best target for the given unit. * * @param unit The Unit to get the best target for. * @param mode How to determine the best target. * @return The encoded index of the best target or 0 if none found. */
| 2394 | * @return The encoded index of the best target or 0 if none found. |
| 2395 | */ |
| 2396 | uint16 Unit_FindBestTargetEncoded(Unit *unit, uint16 mode) |
| 2397 | { |
| 2398 | Structure *s; |
| 2399 | Unit *target; |
| 2400 | |
| 2401 | if (unit == NULL) return 0; |
| 2402 | |
| 2403 | s = NULL; |
| 2404 | |
| 2405 | if (mode == 4) { |
| 2406 | s = Unit_FindBestTargetStructure(unit, mode); |
| 2407 | |
| 2408 | if (s != NULL) return Tools_Index_Encode(s->o.index, IT_STRUCTURE); |
| 2409 | |
| 2410 | target = Unit_FindBestTargetUnit(unit, mode); |
| 2411 | |
| 2412 | if (target == NULL) return 0; |
| 2413 | return Tools_Index_Encode(target->o.index, IT_UNIT); |
| 2414 | } |
| 2415 | |
| 2416 | target = Unit_FindBestTargetUnit(unit, mode); |
| 2417 | |
| 2418 | if (unit->o.type != UNIT_DEVIATOR) s = Unit_FindBestTargetStructure(unit, mode); |
| 2419 | |
| 2420 | if (target != NULL && s != NULL) { |
| 2421 | uint16 priority; |
| 2422 | |
| 2423 | priority = Unit_GetTargetUnitPriority(unit, target); |
| 2424 | |
| 2425 | if (Unit_GetTargetStructurePriority(unit, s) >= priority) return Tools_Index_Encode(s->o.index, IT_STRUCTURE); |
| 2426 | return Tools_Index_Encode(target->o.index, IT_UNIT); |
| 2427 | } |
| 2428 | |
| 2429 | if (target != NULL) return Tools_Index_Encode(target->o.index, IT_UNIT); |
| 2430 | if (s != NULL) return Tools_Index_Encode(s->o.index, IT_STRUCTURE); |
| 2431 | |
| 2432 | return 0; |
| 2433 | } |
| 2434 | |
| 2435 | /** |
| 2436 | * Check if the Unit belonged the the current human, and do some extra tasks. |
no test coverage detected