SungAli

Script for Failed to Perform Mandatory Yield error report.

Jun 4th, 2026
153
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.05 KB | None | 0 0
  1. --Debugging Tools: lead comment:   --[[ -> feature disabled  ---[[ -> feature enabled
  2.  
  3. ---[=[ Name, Version, and Description management.
  4. Name = `A Simple Sun-Earth-Moon emulator.`
  5. Version = "0.3"
  6. Desc = [[
  7. Emulates Earth orbiting Sun while rotating on its own axis,
  8. and Moon orbiting Earth while rotating on its axis.
  9.   ]] .. `{if bit32.btest(ll.GetObjectPermMask(MASK_EVERYONE), PERM_COPY) then "Feel free to take a copy" else "" }`
  10.  
  11. ll.SetObjectName( `{Name}: {Version}` ); ll.SetObjectDesc( Desc ); ll.SetText( `{Name}: {Version}\n \n{Desc}`, vector(0.5,0.5,1), 1 )
  12. --]=]
  13.    
  14. ---[[ Redirect print Debug channel to avoid spamming chat.  Must be enabled if DBG block below is enabled.
  15.     function concat( ... ) -- Return stringized values as a comma separated sting. Avoid using tables so explicit nils are preserved.
  16.         local count = select("#", ...); if count == 0 then return "" end--if
  17.         local result = tostring(select(1, ...)) for i = 2, count do result ..= `, {tostring(select(i, ...))}` end--for
  18.         return result
  19.     end--concat
  20.  
  21.     local rawprint = print -- always enabled just in case some code is using rawprint to bypass the redirect below
  22.     local function print(...) ll.Shout( DEBUG_CHANNEL, concat(...) )  end--print
  23. --[=[]]  --Switch to do-nothing dummy versions when above is disabled.
  24.     local rawprint = print -- always enabled just in case some code is using rawprint to bypass the redirect above
  25. --]=]
  26.  
  27.  
  28. ---[[ Send DBG messages to the Debug Channel to sync with error messages and avoid spamming Nearby Chat
  29.     local DBG_buffer = ""
  30.     local function rawDBG( msg ) ll.Shout( DEBUG_CHANNEL, msg ) end--rawDBG this function exist to allow redirecton of all DBG output to a single destination in one place
  31.  
  32.     local function DBGb( ... ) DBG_buffer ..= concat(...).."; "; end--DBGb  (Buffer message for output wiht next DBG or DBGs call, allows multiple messages on a line.)    
  33.     local function DBGs( ... ) local n,l = debug.info(2, "nl"); rawDBG( `{ll.GetTimestamp()}: {l} {n}: {DBG_buffer} {concat(...)}` );DBG_buffer=""; ll.Sleep(0.2) end--DBG  (with timestamp) & Delay
  34.     local function DBG( ... ) local n,l = debug.info(2, "nl"); rawDBG( `{ll.GetTimestamp()}: {l} {n}: {DBG_buffer} {concat(...)}` ); DBG_buffer=""; end--DBG  (with timestamp)
  35.  
  36.     local function DBG_dump( label, val, lvl ) --dump any data structure
  37.         lvl = lvl or 0
  38.         local indent = string.rep( "  ", lvl )
  39.         local leader = indent .. `({typeof(label)}) {tostring(label)}: `
  40.         if lvl == 0 then  local n,l = debug.info(2, "nl"); leader =  `{ll.GetTimestamp()}: {l} {n}:\n` .. leader end--if (with timestamp)
  41.         typ = typeof( val )
  42.         if typ == "table" then
  43.             rawDBG( `{leader}({typ}) {tostring(val)} `.."{" )
  44.             lvl += 1
  45.             for k,v in pairs(val) do DBG_dump( k, v, lvl ) end--for
  46.             rawDBG( indent .. "}" )
  47.         else rawDBG( `{leader}({typ}) {tostring(val)}` )
  48.         end--if
  49.     end--DBG_dump
  50.    
  51.   local function DBG_dump_list( label, list )  local n,l = debug.info(2, "nl"); rawDBG( `{ll.GetTimestamp()}: {l} {n}: {label}:  {concat(unpack(list))}` ) end--dump a list (1D array) on one line.
  52.  
  53. local function DBG_err( ... )
  54.     local n,l = debug.info(2, "nl")
  55.     rawDBG( `{ll.GetTimestamp()}: {l} {n}: {label}: Error Caught by xpcall`, ... )
  56.     rawDBG( debug.traceback( "Stack Trace", 2) )
  57. end--DBG_err
  58.  
  59. --[=[]]  --Switch to do-nothing dummy versions when above is disabled.
  60.     local function rawDBG() end--rawDBG disabled
  61.     local DBG, DBG_dump, DBG_dump_list = rawDBG, rawDBG, rawDBG --Other DBG functions disabled.
  62.     local DBG_err = print --don't disable error reporting entirely.
  63. --]=]
  64.  
  65. -->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Code start <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  66.  
  67. ---- Constants
  68. --- Common long function name shortcuts, etc.  Saves typing and makes code more compact and easier to read in my opinion
  69. local SLPPF = ll.SetLinkPrimitiveParamsFast
  70. local Z_NORMAL = vector(0,0,1)
  71.  
  72. --- Initial Placement and Scale Parameters.
  73. local links = { Sun = 2, Earth = 3, Moon = 4 } --Hard coded because this is a quick demo and there are only 3.
  74. local smallest_size, largest_size = 0.1, 3 -- lunar radius in the emulation, sun-earth distance in the emulation
  75.  
  76. --- Emulation parameters: Given size, position, and time values will be adjusted to emulation sizes and times during initialization.
  77. -- Since there are only three objects and each recieves different treatment, there doesn't seem to be any point in collecting parameters into a common
  78. -- table like I might if I were dealing with larger sets of similar objects.
  79. function tilt2vec( axis, tilt ) return ll.Rot2Up( ll.AxisAngle2Rot(axis, tilt) ); end --tilt2vec Convenience function for setting parameters below.
  80. local Sun, Earth, Moon
  81. local Sun =   { radius = 695700, rot_period = 601.2 }                                                           --Km, hours
  82. local Earth = { radius =   6371, rot_period =  23.93, orbit = 149597871, orb_period = 8766.14,                  -- Km, hours, Km, hours,
  83.                 axial_tilt = tilt2vec(vector(1,0,0), 0.4091)                                                    -- direction vector (=dv)
  84.               }
  85. local Moon =  { radius =   1737, rot_period = 655.72, orbit =    384400, orb_period =  655.72,                  -- Km, hours, Km, hours,
  86.                 axial_tilt = tilt2vec(vector(-1,0,0), 0.1169), orbital_tilt = tilt2vec(vector(0,1,0), 0.0897) --dv, dv
  87.               }
  88. --Reference Frame Adjustments.
  89. Moon.axial_tilt *= ll.RotBetween(Z_NORMAL, Moon.orbital_tilt) --Moon tilt as given is relative to its orbital plane, convert to object reference frame.
  90. --Additional parameters that depend on other parameters.
  91. Earth.base =  { pos = vector(1,0,0), rot = ll.RotBetween(Z_NORMAL, Earth.axial_tilt) }
  92. Moon.base =   { pos = vector(1,0,0), rot = ll.RotBetween(Z_NORMAL, Moon.axial_tilt) }
  93. --[[Note: I haven't actually determined the relation of Moon's axial tilt relative to its orbit compared to its orbit's tilt relative to Earth's.  Since the
  94.     visible effect of libation is small anyway, and this emulation has far bigger inaccuracies, such as neglecting orbital eccentricities, I'm using a right
  95.     angle value as close enough for the purpose and getting on with my life.
  96. --]]
  97.  
  98. ---Timing Constants
  99. local tick, time_scale = 0.1, 16 -- emulation clock rate, emulation hours per second.
  100. local time_step = time_scale*tick -- emulation hours per tick
  101.  
  102. -----values collected for common power scaling.
  103. local sizes = { Sun.radius, Earth.radius, Earth.orbit, Moon.radius, Moon.orbit }
  104.  
  105. ---- Script Scope Variables
  106. Sun.cur =   {} -- Sun's current rotation in the emulation, pos is constant at ZERO_VECTOR
  107. Earth.cur = {} -- Earth's current postion and rotation
  108. Moon.cur =  {} -- And Moon's
  109. -- Note: It's a little weird, building the tables piecemeal this way, but there's no good reason to make separate tables of constants and variables.
  110. -- Think of it as being quasi-object oriented without going full-bore.  Perhaps I can come up with a better top level script organization.
  111.  
  112. local time = 0 -- Current Emulaton time.
  113.  
  114. ----Functions.
  115. --- Fractional Power Scaler
  116. --[[
  117. A common problem with emulating physical systems is the difference in sizes of the things emulated.  In this case the smallest size is the radius of Moon,
  118. a mere 1737 Km, compared to the radius of Earth's orbit, 149597871 Km, a ratio of over 86124 to 1.  This means if we did things to scale and made  Moon
  119.  the smallest we could make it, then our emulator would need a 4x4 square of sims to fit.  My solution to this is to raise all distances/sizes to a suitable
  120.  power between 0 and 1 (It works with powers greater than one too when you want to exaggerate differences), so that smaller distances are still smaller than
  121.  larger distances, but the difference between the smallest and largest is not so unmanagable.
  122.  The scaler takes an array of values to scale, and a table with two of three possible named arguments, Min, Max, and Ratio.  If all three are supplied,
  123.  Ratio will be set to Max/Min.  It returns an array with the values scaled as required.
  124.  For now limit everything to positive values.  It's possible scale negative values accordingly, and return 0 as 0, but that's beyond the scope of what
  125.  this function needs and I'm already way beyond the original intent of this supposedly simple demo. Same with more error checks and warnings.
  126.  Could add a smooshing factor too, something like a linear interpolation between this and a linear rescale, but not now. KISS.
  127.  The error checking below may be excessive in this context.
  128.  <<Yeah, I use comment blocks like this to keep myself from going overboard and to remind myself about possible future enhancements.>>
  129. --]]
  130.  
  131. local function fractional_power_scale( data, args )
  132.     local min, max, ratio = args.min, args.max, args.ratio
  133.     assert( (min and max) or (min and ratio) or (max and ratio),
  134.         "fractional_power_scale arguement error: at least two of min, max, and ratio must be provided.\n"..
  135.         `Values recieved: min = {min}, max = {max}, ratio = {ratio}`
  136.     )
  137.     min = min or max/ratio; max = max or min*ratio
  138.     if min > max then min, max = max, min; print("Warning: min and max found reversed: corrected"); end--if --Correctable error.
  139.     ratio = max/min
  140.     assert( ratio > 1 and min > 0 and max > 0 and min < max,
  141.         "fractional_power_scale arguement error: min may not be equal to max, ratio may not be 1, all must be > 0.\n"..
  142.         `Values computed from given arguments: min = {min}, max = {max}, ratio = {ratio}`
  143.     )
  144.     local data_min, data_max = math.huge, -math.huge -- find our working input range.
  145.     for i, v in ipairs(data) do
  146.         assert( v > 0, "fractional_power_scale data error: This version of the function only scales positive values.\n"..`Datum {i} = {v}` )
  147.         if v < data_min then data_min = v end--if
  148.         if v > data_max then data_max = v end--if
  149.     end--for
  150.     local power = math.log(ratio) / math.log( data_max/data_min )
  151.     local result, scale = {}, min / data_min^power
  152.     for i, v in ipairs(data) do result[i] = v^power * scale end--for
  153.     return result
  154. end--fractional_power_scale
  155.  
  156. ---Emulation Step
  157. local function emulation_step()
  158.     time += time_step
  159.     Earth.cur.pos =  Earth.base.pos * ll.AxisAngle2Rot(Z_NORMAL, time/Earth.orb_period)
  160.     Earth.cur.orb_rot = Earth.base.rot * ll.AxisAngle2Rot(Z_NORMAL, time/Earth.orb_period)
  161.     SLPPF( links.Earth, { PRIM_POS_LOCAL,Earth.cur.pos } )
  162.     SLPPF( links.Moon,  { PRIM_POS_LOCAL, Earth.cur.pos + (Moon.base.pos-Earth.base.pos) * ll.AxisAngle2Rot(Moon.orbital_tilt, time/Moon.orb_period) * Earth.cur.orb_rot } )
  163. end--emulation_step
  164.  
  165. ---- Initialization
  166. ---Adjust Constants to working values.
  167. local scaled_sizes = fractional_power_scale( sizes, { min = smallest_size, max = largest_size } )
  168. Sun.radius      = scaled_sizes[1]
  169. Earth.radius    = scaled_sizes[2]; Earth.orbit  = scaled_sizes[3]; Earth.base.pos *= Earth.orbit
  170. Moon.radius     = scaled_sizes[4]; Moon.orbit   = scaled_sizes[5]; Moon.base.pos = Earth.base.pos + Moon.base.pos*Moon.orbit
  171. --Periods in radians instead of full circles per unit time.
  172. Sun.rot_period      /= TWO_PI
  173. Earth.rot_period    /= TWO_PI; Earth.orb_period /= TWO_PI;
  174. Moon.rot_period     /= TWO_PI; Moon.orb_period  /= TWO_PI;
  175.  
  176. ---Set base positions and orientations and start rotations.
  177. SLPPF( links.Sun,   { PRIM_SIZE,   Sun.radius*vector(2,2,2), PRIM_OMEGA, Z_NORMAL, time_scale/Sun.rot_period, 1 } )
  178. SLPPF( links.Earth, { PRIM_SIZE, Earth.radius*vector(2,2,2), PRIM_POS_LOCAL, Earth.base.pos, PRIM_ROT_LOCAL, Earth.base.rot,
  179.                       PRIM_OMEGA, ll.Rot2Up(Earth.base.rot), time_scale/Earth.rot_period, 1 } )
  180. SLPPF( links.Moon,  { PRIM_SIZE,  Moon.radius*vector(2,2,2), PRIM_POS_LOCAL,  Moon.base.pos, PRIM_ROT_LOCAL,  Moon.base.rot,
  181.                       PRIM_OMEGA, ll.Rot2Up(Moon.base.rot), time_scale/Moon.rot_period, 1 } )
  182.  
  183. ---Go
  184. local function DBG_im_owner( msg ) ll.MessageLinked( LINK_THIS, 0xDB, msg, "" ) end--DBG_im_owner
  185.  
  186. local function DBG_es_err( ... )
  187.     local n,l = debug.info(3, "nl")
  188.     DBG_im_owner( `{ll.GetTimestamp()}: {l} {n} Error Caught by xpcall: {concat(...)}` )
  189.     DBG_im_owner( `{debug.traceback( "Stack Trace", 3)}` )
  190.     ll.SetScriptState( ll.GetScriptName(), false );
  191. end--DBG_err
  192.  
  193. local function DBG_emulation_step()
  194.     local success, val = xpcall( emulation_step, DBG_es_err )
  195.     if not success then --error in DBG_es_err
  196.         local n,l = debug.info(1, "nl")
  197.         DBG_im_owner( `{ll.GetTimestamp()}: {l} {n} Error Caught by alt: {val}` )
  198.         DBG_im_owner( `{debug.traceback( "Stack Trace", 1)}` )
  199.         ll.SetScriptState( ll.GetScriptName(), false );
  200.     end--if
  201.  end--DBG_emulation_step
  202.  
  203. LLTimers:every( tick, emulation_step )
  204. --LLTimers:every( tick, DBG_emulation_step )
  205.  
  206. local function DBG_report()
  207.     DBG_im_owner( `time: {time}, Epos: {ll.GetLinkPrimitiveParams( links.Earth, {PRIM_POS_LOCAL})[1] }, Mpos: {ll.GetLinkPrimitiveParams( links.Moon, {PRIM_POS_LOCAL})[1] }` )
  208. end
  209.  
  210. --DBG_report(); LLTimers:every( 600, DBG_report )
  211.  
  212.  
Advertisement
Comments
  • SungAli
    172 days
    # text 0.10 KB | 0 0
    1. https://feedback.secondlife.com/slua-alpha/p/failed-to-perform-mandatory-yield-error-in-long-running-script
Add Comment
Please, Sign In to add comment