MCPcopy Create free account
hub / github.com/OpenDUNE/OpenDUNE / Structure_FindFreePosition

Function Structure_FindFreePosition

src/structure.c:1253–1299  ·  view source on GitHub ↗

* Find a free spot for units next to a structure. * @param s Structure that needs a free spot. * @param checkForSpice Spot should be as close to spice as possible. * @return Position of the free spot, or \c 0 if no free spot available. */

Source from the content-addressed store, hash-verified

1251 * @return Position of the free spot, or \c 0 if no free spot available.
1252 */
1253uint16 Structure_FindFreePosition(Structure *s, bool checkForSpice)
1254{
1255 const StructureInfo *si;
1256 uint16 packed;
1257 uint16 spicePacked; /* Position of the spice, or 0 if not used or if no spice. */
1258 uint16 bestPacked;
1259 uint16 bestDistance; /* If > 0, distance to the spice from bestPacked. */
1260 uint16 i, j;
1261
1262 if (s == NULL) return 0;
1263
1264 si = &g_table_structureInfo[s->o.type];
1265 packed = Tile_PackTile(Tile_Center(s->o.position));
1266
1267 spicePacked = (checkForSpice) ? Map_SearchSpice(packed, 10) : 0;
1268 bestPacked = 0;
1269 bestDistance = 0;
1270
1271 i = Tools_Random_256() & 0xF;
1272 for (j = 0; j < 16; j++, i = (i + 1) & 0xF) {
1273 uint16 offset;
1274 uint16 curPacked;
1275 uint16 type;
1276 Tile *t;
1277
1278 offset = g_table_structure_layoutTilesAround[si->layout][i];
1279 if (offset == 0) continue;
1280
1281 curPacked = packed + offset;
1282 if (!Map_IsValidPosition(curPacked)) continue;
1283
1284 type = Map_GetLandscapeType(curPacked);
1285 if (type == LST_WALL || type == LST_ENTIRELY_MOUNTAIN || type == LST_PARTIAL_MOUNTAIN) continue;
1286
1287 t = &g_map[curPacked];
1288 if (t->hasUnit || t->hasStructure) continue;
1289
1290 if (!checkForSpice) return curPacked;
1291
1292 if (bestDistance == 0 || Tile_GetDistancePacked(curPacked, spicePacked) < bestDistance) {
1293 bestPacked = curPacked;
1294 bestDistance = Tile_GetDistancePacked(curPacked, spicePacked);
1295 }
1296 }
1297
1298 return bestPacked;
1299}
1300
1301/**
1302 * Remove the structure from the map, free it, and clean up after it.

Callers 3

Calls 6

Tile_CenterFunction · 0.85
Map_SearchSpiceFunction · 0.85
Tools_Random_256Function · 0.85
Map_IsValidPositionFunction · 0.85
Map_GetLandscapeTypeFunction · 0.85
Tile_GetDistancePackedFunction · 0.85

Tested by

no test coverage detected