Example #1
0
int NpcScriptInterface::luagetDistanceTo(lua_State* L)
{
    //getDistanceTo(uid)
    ScriptEnvironment* env = getScriptEnv();

    Npc* npc = env->getNpc();
    if (!npc) {
        reportErrorFunc(getErrorDesc(LUA_ERROR_THING_NOT_FOUND));
        lua_pushnil(L);
        return 1;
    }

    uint32_t uid = getNumber<uint32_t>(L, -1);

    Thing* thing = env->getThingByUID(uid);
    if (!thing) {
        reportErrorFunc(getErrorDesc(LUA_ERROR_THING_NOT_FOUND));
        lua_pushnil(L);
        return 1;
    }

    const Position& thingPos = thing->getPosition();
    const Position& npcPos = npc->getPosition();
    if (npcPos.z != thingPos.z) {
        lua_pushnumber(L, -1);
    } else {
        int32_t dist = std::max<int32_t>(Position::getDistanceX(npcPos, thingPos), Position::getDistanceY(npcPos, thingPos));
        lua_pushnumber(L, dist);
    }
    return 1;
}
Example #2
0
int NpcScriptInterface::luagetDistanceTo(lua_State *L)
{
	//getDistanceTo(uid)
	uint32_t uid = popNumber(L);

	ScriptEnviroment* env = getScriptEnv();

	Npc* npc = env->getNpc();
	Thing* thing = env->getThingByUID(uid);
	if(thing && npc){
		Position thing_pos = thing->getPosition();
		Position npc_pos = npc->getPosition();
		if(npc_pos.z != thing_pos.z){
			lua_pushnumber(L, -1);
		}
		else{
			int32_t dist = std::max(std::abs(npc_pos.x - thing_pos.x), std::abs(npc_pos.y - thing_pos.y));
			lua_pushnumber(L, dist);
		}
	}
	else{
		reportErrorFunc(getErrorDesc(LUA_ERROR_THING_NOT_FOUND));
		lua_pushnil(L);
	}

	return 1;
}