From ba9511e2ff1d01398f88c576119a13227df1e05b Mon Sep 17 00:00:00 2001 From: Sei Lisa Date: Wed, 21 Dec 2016 01:18:25 +0100 Subject: [PATCH] Implement llFrand and llGenerateKey when in Calc mode. --- lslopt/lslbasefuncs.py | 27 +++++++++++++++++++++++---- lslopt/lslextrafuncs.py | 11 +++++++++-- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/lslopt/lslbasefuncs.py b/lslopt/lslbasefuncs.py index d0891d1..c36b8a1 100644 --- a/lslopt/lslbasefuncs.py +++ b/lslopt/lslbasefuncs.py @@ -1051,11 +1051,30 @@ def llFloor(f): return -2147483648 return int(math.floor(f)) -# not implemented as it does not give the same output for the same input -#def llFrand(lim): +if lslcommon.IsCalc: + import time + from hashlib import md5 + import random + def llFrand(lim): + assert isfloat(lim) + lim = F32(lim) # apply constraints + val = random.random() * lim + # Truncate, rather than rounding + m, e = math.frexp(val) + m = math.floor(m * 8388608.0) / 8388608.0 + val = F32(math.ldexp(m, e)) + if val == lim: + val = 0. + return val -# not implemented as it does not give the same output for the same input -#def llGenerateKey(): + def llGenerateKey(): + s = md5((u'%.17g %f %f' % (time.time(), random.random(), + random.random())).encode('utf8') + ).hexdigest() + return s[:8] + '-' + s[8:12] + '-' + s[12:16] + '-' + s[16:20] + '-' + s[20:32] + +# Otherwise they're not implemented, as they don't give the same output for +# the same input. def llGetListEntryType(lst, pos): assert islist(lst) diff --git a/lslopt/lslextrafuncs.py b/lslopt/lslextrafuncs.py index 1af8535..43ba7b3 100644 --- a/lslopt/lslextrafuncs.py +++ b/lslopt/lslextrafuncs.py @@ -20,8 +20,8 @@ import lslcommon from lslcommon import Key #, Vector, Quaternion from lslbasefuncs import ELSLCantCompute, isinteger, iskey, islist, \ - isvector, isstring, NULL_KEY, ZERO_VECTOR, ZERO_ROTATION, cond -#isfloat, isrotation + isfloat, isvector, isstring, NULL_KEY, ZERO_VECTOR, ZERO_ROTATION, cond +#isrotation TouchEvents = ('touch', 'touch_start', 'touch_end') DetectionEvents = ('touch', 'touch_start', 'touch_end', @@ -147,6 +147,13 @@ def llEdgeOfWorld(v1, v2): return 1 raise ELSLCantCompute +if not lslcommon.IsCalc: + def llFrand(f): + assert isfloat(f) + if f == 0.: + return 0. + raise ELSLCantCompute + def llGetAgentInfo(id): assert iskey(id) if not cond(id):