From 3750f86d0726c10abcd25b6b8dc03517e03b2c42 Mon Sep 17 00:00:00 2001
From: Tim Felgentreff <timfelgentreff@gmail.com>
Date: Wed, 16 Feb 2022 21:25:55 +0100
Subject: [PATCH] Add FindNextResource lua function

---
 src/unit/script_unit.cpp | 43 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/src/unit/script_unit.cpp b/src/unit/script_unit.cpp
index 9d8d595dc..9fb115bed 100644
--- a/src/unit/script_unit.cpp
+++ b/src/unit/script_unit.cpp
@@ -627,6 +627,10 @@ static int CclMoveUnit(lua_State *l)
 	Vec2i ipos;
 	CclGetPos(l, &ipos.x, &ipos.y, 2);
 
+	if (!unit->Removed) {
+		unit->Remove(unit->Container);
+	}
+
 	if (UnitCanBeAt(*unit, ipos)) {
 		unit->Place(ipos);
 	} else {
@@ -1462,6 +1466,44 @@ static int CclSelectSingleUnit(lua_State *l)
 	return 0;
 }
 
+/**
+**  Find the next reachable resource unit that gives resource starting from a worker.
+**  Optional third argument is the range to search.
+**
+**  @param l  Lua state.
+**
+** Example:
+**
+** <div class="example"><code>
+**		peon = CreateUnit("unit-peon", 5, {58, 8})
+**      goldmine = <strong>FindNextResource(peon, 0)</strong></code></div>
+*/
+static int CclFindNextResource(lua_State *l)
+{
+	const int nargs = lua_gettop(l);
+	if (nargs < 2 || nargs > 3) {
+		LuaError(l, "incorrect argument count");
+	}
+
+	lua_pushvalue(l, 1);
+	CUnit *unit = CclGetUnit(l);
+	lua_pop(l, 1);
+
+	lua_pushvalue(l, 2);
+	const int resource = CclGetResourceByName(l);
+	lua_pop(l, 1);
+
+	const int range = nargs == 3 ? LuaToNumber(l, 3) : 1000;
+
+	CUnit *resourceUnit = UnitFindResource(*unit, *unit, range, resource);
+	if (resourceUnit) {
+		lua_pushnumber(l, UnitNumber(*unit));
+	} else {
+		lua_pushnil(l);
+	}
+	return 1;
+}
+
 /**
 **  Enable/disable simplified auto targeting 
 **
@@ -1504,6 +1546,7 @@ void UnitCclRegister()
 	lua_register(Lua, "OrderUnit", CclOrderUnit);
 	lua_register(Lua, "KillUnit", CclKillUnit);
 	lua_register(Lua, "KillUnitAt", CclKillUnitAt);
+	lua_register(Lua, "FindNextResource", CclFindNextResource);
 
 	lua_register(Lua, "GetUnits", CclGetUnits);
 	lua_register(Lua, "GetUnitsAroundUnit", CclGetUnitsAroundUnit);