I'm trying to translate a program from Python to LSL (SecondLife scripting) to find a number I can't access from functions. This number is my linden balance (the money in game.) The only way I can determinate that amount from my script is by transferring amounts of money to myself and to check if the paiement was successful or not.
In order to do so, I found a dychotomy python script that works perfectly :
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)
The problem is that I can't compare answerto the number i'm looking for. I only have fail and success :
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();
}
}
That script works so far, but it will never stop as it nevers knows when to stop. I was thinking about comparing a and b but the space between them is variable as well (from 0 to 10). So i'm stuck at it.
I thought about testing answer+1 and if it fail it means it's the highest amount but I can't now where to test it.
Do you guys have any idea ?
Doing things this way is no faster than counting up from 0 and checking every possible value; every iteration will only reduce the bounds by one. More importantly, llTransferLindenDollars is rate-limited to 1 per second so you'd sooner hit the limit than find any useful result anyway. Assuming someone has L$20, if you used the Python script and started with L$100,000,000, it would take over three years for the routine to complete.
So, since most SL users do not have L$50,000,000, it would probably be faster to just count from zero and keep checking until a failure is obtained:
This is still incredibly slow and practically useless for anyone with more than a few L$, in part for security reasons - scripts knowing an account's balance would mean that it could drain the entire account instantly without an error. There is simply no quick way to determine an account's L$ balance unless the account is a bot and whatever bot service you use has an API function that can do that.
Also FYI, it's poor practice to precede user-defined functions with "ll"; the "ll" functions should only be the built-in ones, for readability's sake.