* Tries to add the closest unit to the current team. * * Stack: *none*. * * @param script The script engine to operate on. * @return The amount of space left in current team. */
| 68 | * @return The amount of space left in current team. |
| 69 | */ |
| 70 | uint16 Script_Team_AddClosestUnit(ScriptEngine *script) |
| 71 | { |
| 72 | Team *t; |
| 73 | Unit *closest = NULL; |
| 74 | Unit *closest2 = NULL; |
| 75 | uint16 minDistance = 0; |
| 76 | uint16 minDistance2 = 0; |
| 77 | PoolFindStruct find; |
| 78 | |
| 79 | VARIABLE_NOT_USED(script); |
| 80 | |
| 81 | t = g_scriptCurrentTeam; |
| 82 | |
| 83 | if (t->members >= t->maxMembers) return 0; |
| 84 | |
| 85 | find.houseID = t->houseID; |
| 86 | find.index = 0xFFFF; |
| 87 | find.type = 0xFFFF; |
| 88 | |
| 89 | while (true) { |
| 90 | Unit *u; |
| 91 | Team *t2; |
| 92 | uint16 distance; |
| 93 | |
| 94 | u = Unit_Find(&find); |
| 95 | if (u == NULL) break; |
| 96 | if (!u->o.flags.s.byScenario) continue; |
| 97 | if (u->o.type == UNIT_SABOTEUR) continue; |
| 98 | if (g_table_unitInfo[u->o.type].movementType != t->movementType) continue; |
| 99 | if (u->team == 0) { |
| 100 | distance = Tile_GetDistance(t->position, u->o.position); |
| 101 | if (distance >= minDistance && minDistance != 0) continue; |
| 102 | minDistance = distance; |
| 103 | closest = u; |
| 104 | continue; |
| 105 | } |
| 106 | |
| 107 | t2 = Team_Get_ByIndex(u->team - 1); |
| 108 | if (t2->members > t2->minMembers) continue; |
| 109 | |
| 110 | distance = Tile_GetDistance(t->position, u->o.position); |
| 111 | if (distance >= minDistance2 && minDistance2 != 0) continue; |
| 112 | minDistance2 = distance; |
| 113 | closest2 = u; |
| 114 | } |
| 115 | |
| 116 | if (closest == NULL) closest = closest2; |
| 117 | if (closest == NULL) return 0; |
| 118 | |
| 119 | Unit_RemoveFromTeam(closest); |
| 120 | return Unit_AddToTeam(closest, t); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Gets the average distance between current team members, and set the |
nothing calls this directly
no test coverage detected