| 1398 | } |
| 1399 | |
| 1400 | static int computeNearbyWeight(lua_State *L) |
| 1401 | { |
| 1402 | find_engine(L, 1); |
| 1403 | luaL_checktype(L, 2, LUA_TTABLE); |
| 1404 | luaL_checktype(L, 3, LUA_TTABLE); |
| 1405 | const char *fname = luaL_optstring(L, 4, "nearby_weight"); |
| 1406 | |
| 1407 | std::vector<UnitPath*> units; |
| 1408 | std::vector<float> weights; |
| 1409 | |
| 1410 | lua_pushnil(L); |
| 1411 | |
| 1412 | while (lua_next(L, 3)) |
| 1413 | { |
| 1414 | df::unit *unit; |
| 1415 | if (lua_isnumber(L, -2)) |
| 1416 | unit = df::unit::find(lua_tointeger(L, -2)); |
| 1417 | else |
| 1418 | unit = Lua::CheckDFObject<df::unit>(L, -2); |
| 1419 | if (!unit) |
| 1420 | continue; |
| 1421 | units.push_back(UnitPath::get(unit)); |
| 1422 | weights.push_back(lua_tonumber(L, -1)); |
| 1423 | lua_pop(L, 1); |
| 1424 | } |
| 1425 | |
| 1426 | lua_pushnil(L); |
| 1427 | |
| 1428 | while (lua_next(L, 2)) |
| 1429 | { |
| 1430 | Lua::StackUnwinder frame(L, 1); |
| 1431 | |
| 1432 | lua_getfield(L, frame[1], "unit"); |
| 1433 | df::unit *unit = Lua::CheckDFObject<df::unit>(L, -1); |
| 1434 | |
| 1435 | lua_getfield(L, frame[1], "time"); |
| 1436 | float time = luaL_checknumber(L, lua_gettop(L)); |
| 1437 | |
| 1438 | df::coord pos; |
| 1439 | |
| 1440 | lua_getfield(L, frame[1], "pos"); |
| 1441 | if (lua_isnil(L, -1)) |
| 1442 | { |
| 1443 | if (!unit) luaL_error(L, "either unit or pos is required"); |
| 1444 | pos = UnitPath::get(unit)->posAtTime(time); |
| 1445 | } |
| 1446 | else |
| 1447 | Lua::CheckDFAssign(L, &pos, -1); |
| 1448 | |
| 1449 | float sum = 0.0f; |
| 1450 | |
| 1451 | for (size_t i = 0; i < units.size(); i++) |
| 1452 | { |
| 1453 | if (units[i]->unit == unit) |
| 1454 | continue; |
| 1455 | |
| 1456 | auto diff = units[i]->posAtTime(time) - pos; |
| 1457 | float dist = 1 + sqrtf(diff.x*diff.x + diff.y*diff.y + diff.z*diff.z); |
nothing calls this directly
no test coverage detected