-1

我正在尝试将程序从 Python 转换为 LSL(SecondLife 脚本)以找到我无法从函数中访问的数字。这个数字是我的菩提树余额(游戏中的钱)。我可以从脚本中确定该金额的唯一方法是向自己转帐金额并检查付款是否成功。

为了做到这一点,我找到了一个完美运行的二分法 python 脚本:

from random import randint

n = 100000000 # Maximum amount possible
amountToFind = randint(1,n)
a = 1
b = n
nbAnswers = 0

while b-a >=0: 
    answer= (a+b)//2
    nbAnswers += 1
    if answer== amountToFind:
        print("The correct answer is:", answer)
        break 
    elif answer> amountToFind:
        b = answer- 1
    else:
        a = answer+ 1

print("The number of steps to get the correct amount was :", nbAnswers)
print(amountToFind)

问题是我无法与answer我正在寻找的数字进行比较。我只有失败和成功:

llCheckLindenBalance()
{
        rep = (a+b)/2;
        transactionId = llTransferLindenDollars(owner, rep);
}

touch_start(integer total_number)
{
    llCheckLindenBalance();
}

transaction_result(key id, integer success, string data)
{
    if(id != transactionId)
        return;



    if(success) // rep < amount or rep == amount
    {

        a = rep+1;
        llOwnerSay("Last amount " +(string)rep);
        llCheckLindenBalance();

    }
    else // FAIL - rep < amount
    {
       b = rep-1;
       llCheckLindenBalance();
    }
}

该脚本到目前为止有效,但它永远不会停止,因为它永远不知道何时停止。我正在考虑比较ab但它们之间的空间也是可变的(从 0 到 10)。所以我坚持下去。我考虑过测试answer+1,如果失败,则意味着它是最高数量,但我现在无法在哪里测试它。

你们有什么想法吗?

4

1 回答 1

2

以这种方式做事并不比从 0 开始计数并检查每个可能的值快;每次迭代只会将边界减少一个。更重要的是,llTransferLindenDollars 的速率限制为每秒 1 次,因此您会尽快达到限制,而不是找到任何有用的结果。假设某人有 20 美元,如果您使用 Python 脚本并以 1 亿美元开始,则需要三年多的时间才能完成例程。

因此,由于大多数 SL 用户没有 L$50,000,000,因此从零开始计数并继续检查直到出现故障可能会更快:

integer amount;
key trans_id;

default
{
    touch_start(integer total_number)
    {
        amount = 0;
        trans_id = llTransferLindenDollars(llGetOwner(), amount);
    }
    transaction_result(key id, integer success, string data)
    {
        if (id != trans_id) return;
        if (success)
        {
            llOwnerSay("Last amount: " + (string)amount);
            llSleep(1.0); // avoid rate limiting
            trans_id = llTransferLindenDollars(llGetOwner(), ++amount);
        }
        else if (data == "LINDENDOLLAR_INSUFFICIENTFUNDS")
        { // this is the error we care about
            llOwnerSay("L$ balance: " + (string)(amount - 1));
        }
        else
        { // some other error
            llOwnerSay("Error: " + data);
            llSleep(10.0); // wait and retry
            trans_id = llTransferLindenDollars(llGetOwner(), amount);
        }
    }
}

对于拥有超过几个 L$ 的任何人来说,这仍然非常缓慢并且实际上毫无用处,部分原因是出于安全原因 - 知道帐户余额的脚本意味着它可以立即耗尽整个帐户而不会出现错误。根本没有快速的方法来确定帐户的 L$ 余额,除非该帐户是机器人,并且您使用的任何机器人服务都具有可以执行此操作的 API 函数。

另外仅供参考,在用户定义的函数之前加上“ll”是不好的做法;为了便于阅读,“ll”函数应该只是内置函数。

于 2020-04-27T20:43:12.020 回答