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

Function Script_Unit_GoToClosestStructure

src/script/unit.c:1786–1824  ·  view source on GitHub ↗

* Makes the current unit to go to the closest structure of the given type. * * Stack: 1 - The type of the structure. * * @param script The script engine to operate on. * @return The value 1 if and only if a structure has been found. */

Source from the content-addressed store, hash-verified

1784 * @return The value 1 if and only if a structure has been found.
1785 */
1786uint16 Script_Unit_GoToClosestStructure(ScriptEngine *script)
1787{
1788 Unit *u;
1789 Structure *s = NULL;
1790 PoolFindStruct find;
1791 uint16 distanceMin =0;
1792
1793 u = g_scriptCurrentUnit;
1794
1795 find.houseID = Unit_GetHouseID(u);
1796 find.index = 0xFFFF;
1797 find.type = STACK_PEEK(1);
1798
1799 while (true) {
1800 Structure *s2;
1801 uint16 distance;
1802
1803 s2 = Structure_Find(&find);
1804
1805 if (s2 == NULL) break;
1806 if (s2->state != STRUCTURE_STATE_IDLE) continue;
1807 if (s2->o.linkedID != 0xFF) continue;
1808 if (s2->o.script.variables[4] != 0) continue;
1809
1810 distance = Tile_GetDistanceRoundedUp(s2->o.position, u->o.position);
1811
1812 if (distance >= distanceMin && distanceMin != 0) continue;
1813
1814 distanceMin = distance;
1815 s = s2;
1816 }
1817
1818 if (s == NULL) return 0;
1819
1820 Unit_SetAction(u, ACTION_MOVE);
1821 Unit_SetDestination(u, Tools_Index_Encode(s->o.index, IT_STRUCTURE));
1822
1823 return 1;
1824}
1825
1826/**
1827 * Transform an MCV into Construction Yard.

Callers

nothing calls this directly

Calls 6

Unit_GetHouseIDFunction · 0.85
Structure_FindFunction · 0.85
Unit_SetActionFunction · 0.85
Unit_SetDestinationFunction · 0.85
Tools_Index_EncodeFunction · 0.85

Tested by

no test coverage detected