* Gets the average distance between current team members, and set the * position of the team to the average position. * * Stack: *none*. * * @param script The script engine to operate on. * @return The average distance. */
| 130 | * @return The average distance. |
| 131 | */ |
| 132 | uint16 Script_Team_GetAverageDistance(ScriptEngine *script) |
| 133 | { |
| 134 | uint16 averageX = 0; |
| 135 | uint16 averageY = 0; |
| 136 | uint16 count = 0; |
| 137 | uint16 distance = 0; |
| 138 | Team *t; |
| 139 | PoolFindStruct find; |
| 140 | |
| 141 | VARIABLE_NOT_USED(script); |
| 142 | |
| 143 | t = g_scriptCurrentTeam; |
| 144 | |
| 145 | find.houseID = t->houseID; |
| 146 | find.index = 0xFFFF; |
| 147 | find.type = 0xFFFF; |
| 148 | |
| 149 | while (true) { |
| 150 | Unit *u; |
| 151 | |
| 152 | u = Unit_Find(&find); |
| 153 | if (u == NULL) break; |
| 154 | if (t->index != u->team - 1) continue; |
| 155 | count++; |
| 156 | averageX += (u->o.position.x >> 8) & 0x3f; |
| 157 | averageY += (u->o.position.y >> 8) & 0x3f; |
| 158 | } |
| 159 | |
| 160 | if (count == 0) return 0; |
| 161 | averageX /= count; |
| 162 | averageY /= count; |
| 163 | |
| 164 | Tile_MakeXY(t->position, averageX, averageY); |
| 165 | |
| 166 | find.houseID = t->houseID; |
| 167 | find.index = 0xFFFF; |
| 168 | find.type = 0xFFFF; |
| 169 | |
| 170 | while (true) { |
| 171 | Unit *u; |
| 172 | |
| 173 | u = Unit_Find(&find); |
| 174 | if (u == NULL) break; |
| 175 | if (t->index != u->team - 1) continue; |
| 176 | distance += Tile_GetDistanceRoundedUp(u->o.position, t->position); |
| 177 | } |
| 178 | |
| 179 | distance /= count; |
| 180 | |
| 181 | if (t->target == 0 || t->targetTile == 0) return distance; |
| 182 | |
| 183 | if (Tile_GetDistancePacked(Tile_PackXY(averageX, averageY), Tools_Index_GetPackedTile(t->target)) <= 10) t->targetTile = 2; |
| 184 | |
| 185 | return distance; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Unknown function 0543. |
nothing calls this directly
no test coverage detected