// Dragon Sense - 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":"query","id","q",...} 9117 -> API {"id","result"} // 9118 -> API deep search: {"type":"search","id","event":"step"|"done",...} // 9118 -> API {"type":"hello","part":"sense"} on start 9113 <- API "announce yourself" string gSenseId; // query id waiting for a sensor result // Region search. A sensor reaches 96 m and returns the 16 nearest, so the region is searched like a search party: // a 3x3 grid (extra points around any full scan), in layers every 150 m from each point's ground for a deep search. // 1. If objects can be rezzed here and a "Dragon Scout" (an Agent Node) is in the contents: scouts do it (a crew // of up to 4 for a deep search, each taking every 4th layer), and this object stays put. // 2. Else, if worn: it asks to be put down (an attachment can't move itself). // 3. Else it jumps itself, and goes home after (home is saved, so a restart mid-search still gets it home). // Never within 2 m of the region's edge, below the ground, or onto land where objects can't enter, be built or run // scripts, or where the parcel is full. A standard search answers at once; a deep one reports steps as it goes. integer gSweeping; // 0 no, 1 jumping itself, 2 its scout is searching list gPts; // points still to visit (worked out a layer at a time) vector gHome; string gSweepId; string gSweepName; list gFound; // strided [key, name, pos] integer gReported; // gFound entries already reported in a step integer gLevel; // the current layer (deep search) float gTop; // deep search: up to this height; 0 = a standard search at its own height integer gAll; // keep going after the first match integer gVisited; integer gAwait; // waiting for a sensor result integer gCh; integer gListen; integer gLastMsg; // A deep search uses a crew of scouts (4 by default, fewer if the parcel is nearly full): each takes every Nth layer. list gScouts; // the crew, in rez order (index = lane) list gLvl; // per lane: the highest layer it has finished (-1 = none yet) list gHeard; // per lane: 1 once that scout has reported anything (until then its job is resent) integer gCrew; integer gDoneCnt; integer gStepped; integer gStartAt; // when the crew was sent (to notice if none appears) key gBornReq; // an avatar query waiting for its born date string gBornId; string gBornJson; report(string body) { llMessageLinked(LINK_THIS, 9118, body, NULL_KEY); } answer(string id, string result) { llMessageLinked(LINK_THIS, 9117, llJsonSetValue(llList2Json(JSON_OBJECT, ["id", id]), ["result"], result), NULL_KEY); } string flags(key k) { integer i = llGetAgentInfo(k); string f; if (i & AGENT_AWAY) f += "away "; if (i & AGENT_BUSY) f += "busy "; if (i & AGENT_SITTING) f += "sitting "; if (i & AGENT_FLYING) f += "flying "; if (i & AGENT_TYPING) f += "typing "; return llStringTrim(f, STRING_TRIM); } string qRegion() { return llList2Json(JSON_OBJECT, ["name", llGetRegionName(), "agents", llGetRegionAgentCount(), "agent_limit", llGetEnv("agent_limit"), "fps", llGetRegionFPS(), "time_dilation", llGetRegionTimeDilation(), "estate", llGetEnv("estate_name"), "type", llGetEnv("region_product_name"), "max_prims", llGetEnv("region_max_prims"), "corner", (string)llGetRegionCorner()]); } string qParcel() { vector p = llGetPos(); list d = llGetParcelDetails(p, [PARCEL_DETAILS_NAME, PARCEL_DETAILS_DESC, PARCEL_DETAILS_OWNER, PARCEL_DETAILS_GROUP, PARCEL_DETAILS_AREA, PARCEL_DETAILS_LANDING_POINT]); return llList2Json(JSON_OBJECT, ["name", llList2String(d, 0), "description", llGetSubString(llList2String(d, 1), 0, 200), "owner", llList2String(d, 2), "owner_name", llKey2Name(llList2Key(d, 2)), "group", llList2String(d, 3), "area_m2", llList2Integer(d, 4), "prims_used", llGetParcelPrimCount(p, PARCEL_COUNT_TOTAL, FALSE), "prims_max", llGetParcelMaxPrims(p, FALSE), "music", llGetParcelMusicURL(), "landing_point", (string)llList2Vector(d, 5)]); } string person(key k, integer full) { vector p = llList2Vector(llGetObjectDetails(k, [OBJECT_POS]), 0); list o = ["n", llGetDisplayName(k), "k", k, "d", (integer)llVecDist(llGetPos(), p), "status", flags(k), "group", llSameGroup(k), "friend", llIsFriend(k), "lang", llGetAgentLanguage(k)]; if (full) { // where they stand (feet) and which way they face, for building or moving around them vector sz = llGetAgentSize(k); vector e = llRot2Euler(llList2Rot(llGetObjectDetails(k, [OBJECT_ROT]), 0)); // (no .z on a call in LSL) vector feet = p; feet.z -= sz.z / 2; o += ["username", llGetUsername(k), "pos", (string)p, "feet", (string)feet, "facing", (string)((integer)(e.z * RAD_TO_DEG)), "attachments", llGetListLength(llGetAttachedList(k))]; } return llList2Json(JSON_OBJECT, o); } string qAvatars() { list ag = llGetAgentList(AGENT_LIST_REGION, []); list out; integer i; for (i = 0; i < llGetListLength(ag) && i < 25; ++i) out += person(llList2Key(ag, i), FALSE); return llList2Json(JSON_OBJECT, ["count", llGetListLength(ag), "avatars", llList2Json(JSON_ARRAY, out)]); } string qEnvironment() { return llList2Json(JSON_OBJECT, ["day_seconds", llGetTimeOfDay(), "sun", (string)llGetSunDirection(), "wind", (string)llWind(ZERO_VECTOR), "water", llWater(ZERO_VECTOR), "ground", llGround(ZERO_VECTOR)]); } string qSelf() { return llList2Json(JSON_OBJECT, ["name", llGetObjectName(), "pos", (string)llGetPos(), "rot_deg", (string)(llRot2Euler(llGetRot()) * RAD_TO_DEG), "corner", (string)llGetRegionCorner(), "prims", llGetNumberOfPrims(), "sense_kb_free", llGetFreeMemory() / 1024]); } 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; } vector reachable(vector p) // the point, or the nearest spot around it it may go to (ZERO_VECTOR: none) { if (mayGo(p)) return p; list tries = [<30, 0, 0>, <-30, 0, 0>, <0, 30, 0>, <0, -30, 0>, <60, 0, 0>, <-60, 0, 0>, <0, 60, 0>, <0, -60, 0>]; integer i; for (i = 0; i < 8; ++i) { vector q = grounded(p + llList2Vector(tries, i)); if (mayGo(q)) return q; // a scan reaches 96 m: from next door it still covers the blocked land } return ZERO_VECTOR; } layer(integer level) // the 3x3 grid, from the corner: at its own height (standard), or above each point's ground { gPts = []; integer x; integer y; vector me = llGetRootPosition(); for (x = 0; x < 3; ++x) for (y = 0; y < 3; ++y) { vector p = <43.0 + 85.0 * x, 43.0 + 85.0 * y, gHome.z>; if (gTop > 0) { p.z = llGround(p - me) + 50.0 + 150.0 * level; // 150 m: no gaps even at a grid square's corners if (p.z - 75.0 <= gTop) gPts += p; } else gPts += p; } } list people() // deep search: look where people are first (skyboxes usually have someone in them) { list out; integer i; list ag = llGetAgentList(AGENT_LIST_REGION, []); vector me = llGetPos(); for (i = 0; i < llGetListLength(ag) && llGetListLength(out) < 8; ++i) { vector p = llList2Vector(llGetObjectDetails(llList2Key(ag, i), [OBJECT_POS]), 0); if (p.z - llGround(p - me) > 100.0) { // up in the sky: the ground layers don't reach there if (p.z - 75.0 <= gTop) { integer j; integer near; for (j = 0; j < llGetListLength(out); ++j) if (llVecDist(p, llList2Vector(out, j)) < 60.0) near = TRUE; if (!near) out += p; } } } return out; } enlist(key k) // a new scout joins the crew: its lane is its place in the list { if (llListFindList(gScouts, [k]) != -1) return; if (llGetListLength(gScouts) >= gCrew) return; gScouts += k; gLvl += -1; gHeard += 0; } sendJob(integer lane) { llRegionSayTo(llList2Key(gScouts, lane), gCh, llList2Json(JSON_OBJECT, ["name", gSweepName, "up_to", gTop, "all", llList2String(["0", "1"], gAll), "lane", lane, "of", gCrew])); } addHit(key k, string nm, vector at) { if (llListFindList(gFound, [k]) != -1) return; if (gSweepName == "" || llSubStringIndex(llToLower(nm), gSweepName) != -1) { if (llGetListLength(gFound) < 150) gFound += [k, nm, at]; } } step(integer upto) // deep search: what's new since the last step (the server IMs the owner) { list fresh; integer i; for (i = gReported; i < llGetListLength(gFound) && llGetListLength(fresh) < 5; i += 3) fresh += llList2Json(JSON_OBJECT, ["n", llList2String(gFound, i + 1), "d", (integer)llVecDist(gHome, llList2Vector(gFound, i + 2))]); gReported = llGetListLength(gFound); report(llList2Json(JSON_OBJECT, ["type", "search", "id", gSweepId, "event", "step", "upto", upto, "count", llGetListLength(gFound) / 3, "new", llList2Json(JSON_ARRAY, fresh)])); } done(string payload) // a standard search answers the waiting question; a deep one sends its final report { if (gTop <= 0) answer(gSweepId, payload); else report(llJsonSetValue(llList2Json(JSON_OBJECT, ["type", "search", "id", gSweepId, "event", "done"]), ["result"], payload)); } finish() { if (gSweeping == 1) { llSetRegionPos(gHome); llLinksetDataDelete("dragon_search_home"); } integer c; if (gSweeping == 2) { integer heard; for (c = 0; c < llGetListLength(gHeard); ++c) heard += llList2Integer(gHeard, c); llOwnerSay("Search done: " + (string)(llGetListLength(gFound) / 3) + " found. Scouts that appeared: " + (string)llGetListLength(gScouts) + " of " + (string)gCrew + ", answered: " + (string)heard + "."); } for (c = 0; c < llGetListLength(gScouts); ++c) llRegionSayTo(llList2Key(gScouts, c), gCh, "{\"e\":\"stop\"}"); gScouts = []; gLvl = []; gHeard = []; if (gListen) llListenRemove(gListen); gListen = 0; list ranked; integer i; for (i = 0; i < llGetListLength(gFound); i += 3) ranked += [llVecDist(gHome, llList2Vector(gFound, i + 2)), i]; ranked = llListSort(ranked, 2, TRUE); list out; for (i = 0; i < llGetListLength(ranked) && i < 20; i += 2) { integer j = llList2Integer(ranked, i + 1); out += llList2Json(JSON_OBJECT, ["k", llList2Key(gFound, j), "n", llList2String(gFound, j + 1), "pos", (string)llList2Vector(gFound, j + 2), "d", (integer)llList2Float(ranked, i)]); } string covered = "around my height (" + (string)llRound(gHome.z - 96) + " to " + (string)llRound(gHome.z + 96) + " m)"; if (gTop > 0) covered = "from the ground up to about " + (string)llRound(gTop) + " m"; string by = "jumping myself"; if (gSweeping == 2) by = "my Dragon Scout"; if (gCrew > 1) by = (string)gCrew + " Dragon Scouts"; done(llList2Json(JSON_OBJECT, ["count", llGetListLength(gFound) / 3, "searched", covered, "by", by, "points", gVisited, "found", llList2Json(JSON_ARRAY, out)])); gSweeping = 0; gFound = []; gReported = 0; llSetTimerEvent(0); } sweepNext() // the self-search: the next point it may go to, the next layer, or done { integer more = TRUE; while (more) { if (gFound != []) { if (gTop > 0) { if (!gAll) { finish(); return; } } // a deep search stops when it's found } while (gPts != []) { vector p = reachable(grounded(llList2Vector(gPts, 0))); gPts = llDeleteSubList(gPts, 0, 0); if (p != ZERO_VECTOR) { if (llSetRegionPos(p)) { ++gVisited; gAwait = TRUE; llSensor("", NULL_KEY, ACTIVE | PASSIVE | SCRIPTED, 96.0, PI); llSetTimerEvent(5.0); // backstop: a scan that never answers can't stall the search return; } } } more = FALSE; if (gTop > 0) { if (gLevel % 3 == 2) step((integer)(50 + 150 * gLevel + 75)); if (gLevel < 30) { ++gLevel; layer(gLevel); if (gPts != []) more = TRUE; } } } finish(); } query(string a) { string id = llJsonGetValue(a, ["id"]); string q = llJsonGetValue(a, ["q"]); if (q == "region") answer(id, qRegion()); else if (q == "parcel") answer(id, qParcel()); else if (q == "avatars") answer(id, qAvatars()); else if (q == "environment") answer(id, qEnvironment()); else if (q == "self") answer(id, qSelf()); else if (q == "avatar") { key k = (key)llJsonGetValue(a, ["avatar"]); if (llGetAgentSize(k) == ZERO_VECTOR) answer(id, "\"not in this region\""); else { // the born date (when they joined Second Life) arrives in dataserver gBornId = id; gBornJson = person(k, TRUE); gBornReq = llRequestAgentData(k, DATA_BORN); } } else if (q == "search") { if (gSweeping) { answer(id, "\"I'm already searching\""); return; } gSweepId = id; gFound = []; gReported = 0; gVisited = 0; gLevel = 0; gCrew = 1; gSweepName = llToLower(llStringTrim(llJsonGetValue(a, ["name"]), STRING_TRIM)); if (gSweepName == JSON_INVALID) gSweepName = ""; gTop = (float)llJsonGetValue(a, ["up_to"]); if (gTop > 4000) gTop = 4000; gAll = llJsonGetValue(a, ["all"]) == "1"; gHome = llGetPos(); // worn: the avatar's position if (llGetInventoryType("Dragon Scout") == INVENTORY_OBJECT) { if (mayGo(gHome)) { // 1. rezzing is allowed here: scouts search gSweeping = 2; gCh = -1000000 - (integer)llFrand(1000000000.0); gListen = llListen(gCh, "", NULL_KEY, ""); gScouts = []; gLvl = []; gHeard = []; gDoneCnt = 0; gStepped = -1; gLastMsg = llGetUnixTime(); gCrew = 1; if (gTop > 0) { // deep: a crew, as many as the parcel has room for gCrew = (integer)llJsonGetValue(a, ["crew"]); if (gCrew < 1) gCrew = 4; if (gCrew > 8) gCrew = 8; integer room = llGetParcelMaxPrims(gHome, FALSE) - llGetParcelPrimCount(gHome, PARCEL_COUNT_TOTAL, FALSE); if (llList2Key(llGetParcelDetails(gHome, [PARCEL_DETAILS_OWNER]), 0) != llGetOwner()) { if (gCrew > room) gCrew = room; } if (gCrew < 1) gCrew = 1; } integer c; for (c = 0; c < gCrew; ++c) // one at a time; each is told its lane as it appears llRezObject("Dragon Scout", gHome + <0, 0, 1.0 + 0.3 * c>, ZERO_VECTOR, ZERO_ROTATION, gCh); gStartAt = llGetUnixTime(); llOwnerSay("Search: sending " + (string)gCrew + " Dragon Scout(s) for \"" + gSweepName + "\"."); llSetTimerEvent(2.0); // soon: resend any job a scout missed return; } } if (llGetAttached()) { // 2. worn, and no scout can be rezzed here done("\"I'm worn, so I can't search the region from here: drop me on the ground first, or use me where objects can be rezzed with a Dragon Scout in my contents.\""); return; } if (llGetStatus(STATUS_PHYSICS)) { done("\"I'm set to Physical, so I can't jump around to search.\""); return; } gSweeping = 1; // 3. it searches by jumping itself gHome = llGetRootPosition(); llLinksetDataWrite("dragon_search_home", (string)gHome); layer(0); if (gTop > 0) gPts = people() + gPts; // people's heights first, then the grid from the ground sweepNext(); } else if (q == "sense") { string t = llJsonGetValue(a, ["type"]); integer mask = AGENT; if (t == "objects") mask = ACTIVE | PASSIVE; else if (t == "scripted") mask = SCRIPTED; else if (t == "all") mask = AGENT | ACTIVE | PASSIVE; gSenseId = id; llSensor(llJsonGetValue(a, ["name"]), NULL_KEY, mask, (float)llJsonGetValue(a, ["range"]), PI); } } default { state_entry() { string h = llLinksetDataRead("dragon_search_home"); // restarted in the middle of a search: go home if (h != "") { if (!llGetAttached()) llSetRegionPos((vector)h); llLinksetDataDelete("dragon_search_home"); } report("{\"type\":\"hello\",\"part\":\"sense\"}"); } object_rez(key k) { if (gSweeping == 2) enlist(k); } // a scout is out (it also says "ready") listen(integer ch, string name, key id, string msg) // the scout's reports { if (gSweeping != 2) return; string e = llJsonGetValue(msg, ["e"]); if (e == "ready") { // the scout is listening: (re)send its job if (llGetOwnerKey(id) != llGetOwner()) return; if ((integer)llJsonGetValue(msg, ["v"]) < 4) { if (llListFindList(gScouts, [id]) == -1) llOwnerSay("Search: the Dragon Scout in my contents has an old script. Replace dragon_scout.lsl inside it (deep searches need the new one)."); } enlist(id); integer at = llListFindList(gScouts, [id]); if (at != -1) sendJob(at); gLastMsg = llGetUnixTime(); return; } integer lane = llListFindList(gScouts, [id]); if (lane == -1) return; if (e == "step") { // only old scouts send this: their progress can't be combined if (!llList2Integer(gHeard, lane)) llOwnerSay("Search: an old Dragon Scout script answered. Replace dragon_scout.lsl inside the Dragon Scout in my contents."); } gLastMsg = llGetUnixTime(); if (!llList2Integer(gHeard, lane)) llOwnerSay("Search: scout " + (string)(lane + 1) + " of " + (string)gCrew + " is searching."); gHeard = llListReplaceList(gHeard, [1], lane, lane); if (e == "hit") { addHit((key)llJsonGetValue(msg, ["k"]), llJsonGetValue(msg, ["n"]), (vector)llJsonGetValue(msg, ["p"])); if (gTop > 0) { if (!gAll) { if (gFound != []) finish(); } } // found: the whole crew stops } else if (e == "alive") { // a layer done: report once every lane has covered the next ~450 m gLvl = llListReplaceList(gLvl, [(integer)llJsonGetValue(msg, ["lvl"])], lane, lane); integer k = gStepped + 1; if (llGetListLength(gLvl) < gCrew) return; // the whole crew isn't out yet while (llList2Integer(gLvl, k % gCrew) >= k) { if (k % 3 == 2) step((integer)(50 + 150 * k + 75)); gStepped = k; ++k; } } else if (e == "done") { gVisited += (integer)llJsonGetValue(msg, ["pts"]); ++gDoneCnt; if (gDoneCnt >= llGetListLength(gScouts)) finish(); } } on_rez(integer p) { llResetScript(); } changed(integer c) { if (c & CHANGED_OWNER) llResetScript(); } link_message(integer sender, integer num, string str, key id) { if (num == 9113) { if (str == "status") { llOwnerSay("dragon-sense, " + (string)(llGetFreeMemory() / 1024) + " KB free"); return; } report("{\"type\":\"hello\",\"part\":\"sense\"}"); return; } if (num != 9110) return; integer i; string a = llJsonGetValue(str, [0]); while (a != JSON_INVALID && i < 8) { if (llJsonGetValue(a, ["do"]) == "query") query(a); a = llJsonGetValue(str, [++i]); } } sensor(integer n) { if (gSweeping == 1) { gAwait = FALSE; integer s; for (s = 0; s < n; ++s) addHit(llDetectedKey(s), llDetectedName(s), llDetectedPos(s)); if (n >= 16) { // a full scan: look again a little to each side, so crowded spots don't hide things vector c = llGetRootPosition(); if (llGetListLength(gPts) < 24) gPts = [c + <40, 0, 0>, c + <-40, 0, 0>, c + <0, 40, 0>, c + <0, -40, 0>] + gPts; } llSetTimerEvent(0.1); return; } list out; integer i; for (i = 0; i < n && i < 16; ++i) { string t = "object"; if (llDetectedType(i) & AGENT) t = "avatar"; list o = ["n", llDetectedName(i), "k", llDetectedKey(i), "type", t, "d", (integer)llVecDist(llGetPos(), llDetectedPos(i)), "pos", (string)llDetectedPos(i)]; if (t == "object") { // its size and top, so things can be placed on it, inside its edges list bb = llGetBoundingBox(llDetectedKey(i)); vector lo = llList2Vector(bb, 0); vector hi = llList2Vector(bb, 1); vector p = llDetectedPos(i); o += ["size", (string)(hi - lo), "top", (string)(p.z + hi.z)]; } out += llList2Json(JSON_OBJECT, o); } answer(gSenseId, llList2Json(JSON_OBJECT, ["count", n, "found", llList2Json(JSON_ARRAY, out)])); } no_sensor() { if (gSweeping == 1) { gAwait = FALSE; llSetTimerEvent(0.1); return; } answer(gSenseId, "{\"count\":0,\"found\":[]}"); } timer() { if (gSweeping == 1) { llSetTimerEvent(0); gAwait = FALSE; sweepNext(); return; } if (gSweeping == 2) { // the scouts stopped reporting (deleted, returned...): finish with what they found integer c; for (c = 0; c < llGetListLength(gScouts); ++c) // a scout that hasn't reported yet may have missed its job if (!llList2Integer(gHeard, c)) sendJob(c); if (gScouts == []) { if (llGetUnixTime() - gStartAt > 8) { // none appeared: say why it might be, and stop llOwnerSay("Search: no Dragon Scout appeared. Check that the object in my contents is named exactly 'Dragon Scout', is copyable, and has dragon_scout.lsl in it."); finish(); return; } } integer quiet = 45; if (gTop <= 0) quiet = 15; // a standard search is quick: don't keep anyone waiting if (llGetUnixTime() - gLastMsg > quiet) finish(); else llSetTimerEvent(3.0); } } dataserver(key q, string data) { if (q != gBornReq) return; answer(gBornId, llJsonSetValue(gBornJson, ["born"], data)); gBornReq = NULL_KEY; } }