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

Function Script_Structure_FindTargetUnit

src/script/structure.c:303–365  ·  view source on GitHub ↗

* Find a Unit which is within range and not an ally. * * Stack: 1 - Range to find a target in (amount of tiles multiplied with 256). * * @param script The script engine to operate on. * @return The Unit Index of the closest unit within range and not friendly, * or 0 if none exists. */

Source from the content-addressed store, hash-verified

301 * or 0 if none exists.
302 */
303uint16 Script_Structure_FindTargetUnit(ScriptEngine *script)
304{
305 PoolFindStruct find;
306 Structure *s;
307 Unit *u;
308 uint32 distanceCurrent;
309 uint32 targetRange;
310 tile32 position;
311
312 s = g_scriptCurrentStructure;
313 targetRange = STACK_PEEK(1);
314 distanceCurrent = 32000;
315 u = NULL;
316
317 find.houseID = HOUSE_INVALID;
318 find.index = 0xFFFF;
319 find.type = 0xFFFF;
320
321 /* ENHANCEMENT -- The original code calculated distances from the top-left corner of the structure. */
322 if (g_dune2_enhanced) {
323 position = Tile_Center(s->o.position);
324 } else {
325 position = s->o.position;
326 }
327
328 while (true) {
329 uint16 distance;
330 Unit *uf;
331
332 uf = Unit_Find(&find);
333 if (uf == NULL) break;
334
335 if (House_AreAllied(s->o.houseID, Unit_GetHouseID(uf))) continue;
336
337 if (uf->o.type != UNIT_ORNITHOPTER) {
338 if ((uf->o.seenByHouses & (1 << s->o.houseID)) == 0) continue;
339 }
340
341 distance = Tile_GetDistance(uf->o.position, position);
342 if (distance >= distanceCurrent) continue;
343
344 if (g_dune2_enhanced) {
345 if (uf->o.type == UNIT_ORNITHOPTER) {
346 if (distance > targetRange * 3) continue;
347 } else {
348 if (distance > targetRange) continue;
349 }
350 } else {
351 if (uf->o.type == UNIT_ORNITHOPTER) {
352 if (distance >= targetRange * 3) continue;
353 } else {
354 if (distance >= targetRange) continue;
355 }
356 }
357
358 /* ENHANCEMENT -- The original code swapped the assignment, making it do nothing, Now it finds the closest unit to shoot at, what seems to be the intention */
359 if (g_dune2_enhanced) distanceCurrent = distance;
360 u = uf;

Callers

nothing calls this directly

Calls 6

Tile_CenterFunction · 0.85
Unit_FindFunction · 0.85
House_AreAlliedFunction · 0.85
Unit_GetHouseIDFunction · 0.85
Tile_GetDistanceFunction · 0.85
Tools_Index_EncodeFunction · 0.85

Tested by

no test coverage detected