// Dragon Motion - part of the Dragon Library for a Dragon AI Connect agent (Mind plan and up). // Put it next to dragon_api.lsl. The Library is a set of small scripts, one per kind of ability, each with // its own 64 KB: add only the ones you want. No logic and no secrets: the server decides. // Guide: https://dragonaiconnect.com/docs/runtime/library // // 9110 <- API {"do":"move","m":follow|orbit|look_at|go_to|wander|come_back|path|continue|teleport|stop|make_smooth, // "t","s","d","h","r","rpm","w","p","pts":[...],"laps","pause","who"} // 9118 -> API {"type":"hello","part":"motion"} on start; {"type":"motion","event":point|done|lost,"n","total","lap"} // 9113 <- API "announce yourself" / "status" // path: visit the points in order (offsets around the target, who it keeps up with as they move, or region // positions without one), "laps" times; with "pause" it stops at each point and waits for "continue". // A point "|" is an offset around that person instead (a figure 8 around two people). // teleport: jump at once to "p" (or by the offset "b" from here), or next to "t" facing them (llSetRegionPos; anyone sitting on it comes along). // // It picks the best way this object can move, and falls back by itself: // 1 SMOOTH llSetKeyframedMotion. Needs this script in the root prim, not Physical, and Prim Equivalency // (root physics shape Convex Hull, or mesh). An old-style "Prim" root is switched to Convex Hull // only if that doesn't raise the land impact. // 2 GLIDE physical: floats (no gravity, no tipping) and glides with llMoveToTarget. On a bump it turns // physics off and steps clear for 2 s, then glides again. Always non-physical again when it stops. // 3 STEPS small direct llSetRegionPos steps. Works for any non-physical object. integer SMOOTH = 1; integer GLIDE = 2; integer STEPS = 3; integer gHow; // how this object moves (0 = not decided yet) string gMode; // "" idle, follow, orbit, look_at, go_to, wander, come_back key gT; float gS; float gD; float gH; float gR; float gRpm; float gW; float gAng; vector gGoal; vector gHome; vector gRest; float gDt = 0.5; integer gCheck; // a smooth step was sent: next tick, make sure it really happened vector gTried; rotation gTriedRot; integer gBumpUntil; // glide: stepping clear of something we bumped into, until then integer gWarned; list gPts; // path points (vector strings) integer gIdx; integer gLap; integer gLaps; integer gPause; integer gWait; key gWho; // who asked: told privately at each stop and at the end event_(string e, integer n) { llMessageLinked(LINK_THIS, 9118, llList2Json(JSON_OBJECT, ["type", "motion", "event", e, "n", n, "total", llGetListLength(gPts), "lap", gLap + 1]), NULL_KEY); } tell(string t) { if (gWho) llRegionSayTo(gWho, 0, t); } key gAnchor; // whose position the current path point is around (NULL_KEY: the target, or none) vector point(integer i) // the region position of path point i (it may be anchored to its own person) { string s = llList2String(gPts, i); integer bar = llSubStringIndex(s, "|"); gAnchor = gT; if (bar > 0) { gAnchor = (key)llGetSubString(s, 0, bar - 1); s = llGetSubString(s, bar + 1, -1); } vector base; if (gAnchor != NULL_KEY) { base = llList2Vector(llGetObjectDetails(gAnchor, [OBJECT_POS]), 0); if (base == ZERO_VECTOR) return ZERO_VECTOR; // they left: the caller stops } return base + (vector)s + <0, 0, gH>; } integer mayGo(vector p) // a place it can really be: inside the region, on land that lets objects in, build and run { if (p.x < 2 || p.x > 254 || p.y < 2 || p.y > 254 || p.z > 4000) return FALSE; // never at or past the edge list d = llGetParcelDetails(p, [PARCEL_DETAILS_OWNER, PARCEL_DETAILS_GROUP]); if (llList2Key(d, 0) == llGetOwner()) return TRUE; // the owner's own land integer f = llGetParcelFlags(p); integer group = llList2Key(d, 1) == llList2Key(llGetObjectDetails(llGetKey(), [OBJECT_GROUP]), 0); integer entry = (f & PARCEL_FLAG_ALLOW_ALL_OBJECT_ENTRY) != 0; integer build = (f & PARCEL_FLAG_ALLOW_CREATE_OBJECTS) != 0; integer scripts = (f & PARCEL_FLAG_ALLOW_SCRIPTS) != 0; if (group) { if (f & PARCEL_FLAG_ALLOW_GROUP_OBJECT_ENTRY) entry = TRUE; if (f & PARCEL_FLAG_ALLOW_CREATE_GROUP_OBJECTS) build = TRUE; if (f & PARCEL_FLAG_ALLOW_GROUP_SCRIPTS) scripts = TRUE; } if (!entry) return FALSE; if (!build) return FALSE; if (!scripts) return FALSE; // its scripts would stop there: it would be stuck if (llGetParcelPrimCount(p, PARCEL_COUNT_TOTAL, FALSE) >= llGetParcelMaxPrims(p, FALSE)) return FALSE; // full return TRUE; } vector grounded(vector p) // not below the ground there { float g = llGround(p - llGetRootPosition()) + 0.5; if (p.z < g) p.z = g; return p; } hop_to(vector to, vector look) // teleport within the region in one go { halt(); if (llGetStatus(STATUS_PHYSICS)) physics(FALSE); if (to.x < 2) to.x = 2; if (to.x > 254) to.x = 254; if (to.y < 2) to.y = 2; if (to.y > 254) to.y = 254; to = grounded(to); if (!mayGo(to)) { event_("failed", 0); llOwnerSay("I won't teleport to " + (string)to + ": that land doesn't let objects in, build or run scripts, or it's full."); return; } if (llSetRegionPos(to)) { if (look != ZERO_VECTOR) llSetLinkPrimitiveParamsFast(LINK_ROOT, [PRIM_ROTATION, face(look - to)]); gRest = llGetRootPosition(); event_("done", 1); } else { event_("failed", 0); llOwnerSay("I couldn't teleport to " + (string)to + " (the parcel there may not allow it)."); } } hello() { llMessageLinked(LINK_THIS, 9118, "{\"type\":\"hello\",\"part\":\"motion\"}", NULL_KEY); } float li() { return llList2Float(llGetObjectDetails(llGetKey(), [OBJECT_PRIM_EQUIVALENCE]), 0); } integer mesh() { list t = llGetLinkPrimitiveParams(LINK_ROOT, [PRIM_TYPE]); if (llList2Integer(t, 0) != PRIM_TYPE_SCULPT) return FALSE; return (llList2Integer(t, 2) & 7) == PRIM_SCULPT_TYPE_MESH; } decide() { if (gHow) return; gHow = SMOOTH; if (llGetLinkNumber() > 1) { gHow = GLIDE; return; } // smooth only works from the root prim if (mesh()) return; if (llList2Integer(llGetLinkPrimitiveParams(LINK_ROOT, [PRIM_PHYSICS_SHAPE_TYPE]), 0) != PRIM_PHYSICS_SHAPE_PRIM) return; float before = li(); llSetLinkPrimitiveParamsFast(LINK_ROOT, [PRIM_PHYSICS_SHAPE_TYPE, PRIM_PHYSICS_SHAPE_CONVEX]); if (li() > before) { // costs land impact: put it back and glide instead llSetLinkPrimitiveParamsFast(LINK_ROOT, [PRIM_PHYSICS_SHAPE_TYPE, PRIM_PHYSICS_SHAPE_PRIM]); gHow = GLIDE; } } physics(integer on) { if (on) { llSetStatus(STATUS_ROTATE_X | STATUS_ROTATE_Y, FALSE); // stay upright llSetStatus(STATUS_PHYSICS, TRUE); llSetBuoyancy(1.0); // float: no gravity if (!llGetStatus(STATUS_PHYSICS)) { gHow = STEPS; llOwnerSay("I can't turn physical here (too many prims, or the parcel), so I'll move in small steps."); } } else { llStopMoveToTarget(); llStopLookAt(); llSetStatus(STATUS_PHYSICS, FALSE); } } vector above(vector v, vector me) // a point she can actually reach: inside the region, above the ground { float g = llGround(v - me) + 0.3; if (v.z < g) v.z = g; return v; } rotation face(vector dir) { dir.z = 0; if (llVecMag(dir) < 0.01) return llGetRootRotation(); return llRotBetween(<1, 0, 0>, llVecNorm(dir)); } halt() { if (gMode != "") { if (gHow == SMOOTH) llSetKeyframedMotion([], [KFM_COMMAND, KFM_CMD_STOP]); if (llGetStatus(STATUS_PHYSICS)) physics(FALSE); } gMode = ""; gT = NULL_KEY; gRest = llGetRootPosition(); gCheck = FALSE; llSetTimerEvent(0); } tick() { gDt = 0.5; if (gHow != SMOOTH) gDt = 0.25; llSetTimerEvent(gDt); } start(string a) { string m = llJsonGetValue(a, ["m"]); if (m == "stop") { halt(); return; } if (m == "continue") { if (gMode == "path") { gWait = FALSE; tick(); } return; } if (m == "teleport") { if (llGetAttached()) { llOwnerSay("I can't teleport while I'm worn."); return; } string tk = llJsonGetValue(a, ["t"]); if (tk != JSON_INVALID) { // next to a person, facing them vector tp2 = llList2Vector(llGetObjectDetails((key)tk, [OBJECT_POS]), 0); if (tp2 == ZERO_VECTOR) { llOwnerSay("I can't find them in this region."); return; } if (llJsonGetValue(a, ["h"]) != JSON_INVALID) // straight above them (0: exactly there) hop_to(tp2 + <0, 0, (float)llJsonGetValue(a, ["h"])>, ZERO_VECTOR); else { vector off = llGetRootPosition() - tp2; off.z = 0; if (llVecMag(off) < 0.01) off = <1, 0, 0>; hop_to(tp2 + llVecNorm(off) * 1.5, tp2); } } else if (llJsonGetValue(a, ["b"]) != JSON_INVALID) // an offset from where it really is hop_to(llGetRootPosition() + (vector)llJsonGetValue(a, ["b"]), ZERO_VECTOR); else hop_to((vector)llJsonGetValue(a, ["p"]), ZERO_VECTOR); return; } if (m == "make_smooth") { // the owner asked: Prim Equivalency for the whole object (may change land impact) halt(); llSetLinkPrimitiveParamsFast(LINK_ROOT, [PRIM_PHYSICS_SHAPE_TYPE, PRIM_PHYSICS_SHAPE_CONVEX]); if (llGetNumberOfPrims() > 1) llSetLinkPrimitiveParamsFast(LINK_ALL_CHILDREN, [PRIM_PHYSICS_SHAPE_TYPE, PRIM_PHYSICS_SHAPE_NONE]); gHow = SMOOTH; llOwnerSay("Set up for smooth motion (root Convex Hull, other prims None). Check my land impact."); return; } if (llGetAttached()) { llOwnerSay("I can't move around while I'm worn."); return; } if (gMode == "") { if (llGetStatus(STATUS_PHYSICS)) { llOwnerSay("I'm already Physical; untick Physical in the Build window and ask again."); return; } vector here = llGetRootPosition(); if (llVecDist(here, gRest) > 0.5) gHome = here; // moved by hand since: that's home now decide(); if (gHow == GLIDE) physics(TRUE); } vector me = llGetRootPosition(); gMode = m; gT = NULL_KEY; gWarned = FALSE; gBumpUntil = 0; string t = llJsonGetValue(a, ["t"]); if (t != JSON_INVALID) gT = (key)t; gS = (float)llJsonGetValue(a, ["s"]); gD = (float)llJsonGetValue(a, ["d"]); gH = (float)llJsonGetValue(a, ["h"]); gR = (float)llJsonGetValue(a, ["r"]); gRpm = (float)llJsonGetValue(a, ["rpm"]); gW = (float)llJsonGetValue(a, ["w"]); if (gS <= 0) gS = 3; gGoal = me; if (m == "go_to") { gGoal = (vector)llJsonGetValue(a, ["p"]); if (llJsonGetValue(a, ["b"]) != JSON_INVALID) gGoal = llGetRootPosition() + (vector)llJsonGetValue(a, ["b"]); } if (m == "come_back") gGoal = gHome; if (m == "path") { gPts = llJson2List(llJsonGetValue(a, ["pts"])); gIdx = 0; gLap = 0; gWait = FALSE; gLaps = (integer)llJsonGetValue(a, ["laps"]); if (gLaps < 1) gLaps = 1; gPause = llJsonGetValue(a, ["pause"]) == "1"; gWho = (key)llJsonGetValue(a, ["who"]); if (gPts == []) { halt(); return; } } if (m == "orbit") { vector tp = llList2Vector(llGetObjectDetails(gT, [OBJECT_POS]), 0); gAng = llAtan2(me.y - tp.y, me.x - tp.x); // start the circle where we already are } tick(); } hop(vector to, rotation want) // one direct step (STEPS, or GLIDE stepping clear of a bump) { llSetLinkPrimitiveParamsFast(LINK_ROOT, [PRIM_ROTATION, want]); if (llVecDist(to, llGetRootPosition()) < 0.1) return; if (llSetRegionPos(to)) return; if (gWarned) return; gWarned = TRUE; string why = "the parcel there doesn't allow it, or it's outside the region"; if (to.z < llGround(to - llGetRootPosition())) why = "that's below the ground"; llOwnerSay("I couldn't move to " + (string)to + ": " + why + "."); } step() { vector me = llGetRootPosition(); if (gCheck) { // the last smooth step neither moved nor turned us: Second Life refused it gCheck = FALSE; if (llVecDist(me, gTried) + llAngleBetween(llGetRootRotation(), gTriedRot) < 0.02) { gHow = GLIDE; llOwnerSay("Smooth motion isn't allowed for this object, so I'll glide (physical) instead."); physics(TRUE); tick(); } } if (gBumpUntil) { if (llGetUnixTime() >= gBumpUntil) { gBumpUntil = 0; physics(TRUE); } // clear: glide again } vector p = me; vector look; vector tp; if (gT != NULL_KEY) { tp = llList2Vector(llGetObjectDetails(gT, [OBJECT_POS]), 0); if (tp == ZERO_VECTOR) { event_("lost", gIdx); halt(); llOwnerSay("I lost sight of them, so I stopped."); return; } } if (gMode == "path") { if (gWait) return; vector at = point(gIdx); if (at == ZERO_VECTOR) { event_("lost", gIdx); halt(); llOwnerSay("I lost sight of them, so I stopped."); return; } gGoal = above(at, me); if (llVecDist(me, gGoal) < 0.3) { // reached a point integer total = llGetListLength(gPts); ++gIdx; if (gIdx >= total) { gIdx = 0; ++gLap; if (gLap >= gLaps) { event_("done", total); tell("Finished the path."); halt(); return; } } if (gPause) { gWait = TRUE; event_("point", gIdx); tell("Point " + (string)gIdx + " of " + (string)total + ". Say continue when you're ready."); return; } at = point(gIdx); if (at == ZERO_VECTOR) { event_("lost", gIdx); halt(); return; } gGoal = above(at, me); } p = gGoal; look = p - me; if (gAnchor != NULL_KEY) look = llList2Vector(llGetObjectDetails(gAnchor, [OBJECT_POS]), 0) - me; } else if (gT != NULL_KEY) { look = tp - me; if (gMode == "go_to") { // go to a person: stop next to them, facing them vector to = me - tp; to.z = 0; if (llVecMag(to) < 0.01) to = <1, 0, 0>; if (llVecDist(me, tp) <= gD + 0.4) { event_("done", 1); halt(); return; } p = tp + llVecNorm(to) * gD + <0, 0, gH>; } else if (gMode == "follow") { vector off = me - tp; off.z = 0; if (llVecMag(off) < 0.01) off = <1, 0, 0>; if (llVecDist(me, tp) > gD + 0.5) p = tp + llVecNorm(off) * gD + <0, 0, gH>; } else if (gMode == "orbit") { gAng += TWO_PI * gRpm / 60.0 * gDt; p = tp + ; } } else { if (gMode == "wander") { if (llVecDist(me, gGoal) < 0.3) gGoal = gHome + ; } else if (llVecDist(me, gGoal) < 0.15) { event_("done", 1); halt(); return; } // go_to / come_back: arrived p = gGoal; look = p - me; } if (p.x < 2) p.x = 2; if (p.x > 254) p.x = 254; if (p.y < 2) p.y = 2; if (p.y > 254) p.y = 254; float g = llGround(p - me) + 0.3; if (p.z < g) p.z = g; vector v = p - me; float lim = gS * gDt; if (llVecMag(v) > lim) v = llVecNorm(v) * lim; rotation want = face(look); if (llVecMag(v) < 0.1) { // skip tiny corrections if (llAngleBetween(llGetRootRotation(), want) < 0.1) return; } if (gHow == SMOOTH) { gTried = me; gTriedRot = llGetRootRotation(); gCheck = TRUE; llSetKeyframedMotion([v, want / llGetRootRotation(), gDt], []); } else if (gHow == GLIDE) { if (gBumpUntil) hop(me + v, want); else { llMoveToTarget(me + v * 2, 0.4); llRotLookAt(want, 1.0, 0.4); } // aim a little ahead: smooth } else hop(me + v, want); } default { state_entry() { gHome = gRest = llGetRootPosition(); hello(); } on_rez(integer p) { llResetScript(); } changed(integer c) { if (c & CHANGED_OWNER) llResetScript(); } timer() { if (gMode != "") step(); } collision_start(integer n) // gliding into something: go non-physical and step clear for 2 s { if (gHow != GLIDE) return; if (gMode == "") return; if (gBumpUntil) return; physics(FALSE); gBumpUntil = llGetUnixTime() + 2; } link_message(integer sender, integer num, string str, key id) { if (num == 9113) { if (str == "status") { string how = "not decided yet (on the first move)"; if (gHow == SMOOTH) how = "smooth"; else if (gHow == GLIDE) how = "glide (physical)"; else if (gHow == STEPS) how = "small steps"; llOwnerSay("dragon-motion, " + (string)(llGetFreeMemory() / 1024) + " KB free, moves: " + how); return; } hello(); return; } if (num != 9110) return; integer i; string a = llJsonGetValue(str, [0]); while (a != JSON_INVALID && i < 8) { if (llJsonGetValue(a, ["do"]) == "move") start(a); a = llJsonGetValue(str, [++i]); } } }