// Dragon Watch - 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":"rules","rules":[...]}: the watches to run, checked every 5 s // kinds: near, arrive, crowd, parcel (someone on the owner's land: for security) // 9118 -> API {"type":"hello","part":"watch"} on start; {"type":"rule","rule","who"|"count"} when one fires // 9113 <- API "announce yourself" list gRules; // compact rule JSON strings from the server list gSeen; // strided ["rule|avatar", until]: per-avatar cooldown list gCrowd; // rule ids whose crowd threshold is currently reached list gPresent; // avatars in the region at the last look (for "arrive" watches) integer gArrive; // any arrive watch? integer gPrimed; // gPresent filled once, so the first look reports nobody report(string body) { llMessageLinked(LINK_THIS, 9118, body, NULL_KEY); } integer passes(key k, string f) { if (f == "anyone") return TRUE; if (k == llGetOwner()) return FALSE; if (f == "not_owner") return TRUE; if (llSameGroup(k)) return FALSE; if (f == "not_group") return TRUE; return !llIsFriend(k); // stranger: not the owner, not in its group, not a friend } watch() { integer now = llGetUnixTime(); integer i; for (i = 0; i < llGetListLength(gSeen); i += 2) // forget expired cooldowns if (llList2Integer(gSeen, i + 1) < now) { gSeen = llDeleteSubList(gSeen, i, i + 1); i -= 2; } list ag = llGetAgentList(AGENT_LIST_REGION, []); list fresh; // arrived since the last look if (gArrive) { if (gPrimed) { for (i = 0; i < llGetListLength(ag); ++i) if (llListFindList(gPresent, [llList2Key(ag, i)]) == -1) fresh += llList2Key(ag, i); } gPresent = llList2List(ag, 0, 79); gPrimed = TRUE; } for (i = 0; i < llGetListLength(gRules); ++i) { string r = llList2String(gRules, i); string id = llJsonGetValue(r, ["id"]); string kind = llJsonGetValue(r, ["k"]); if (kind == "parcel") { // someone on the owner's land (security: the server decides what happens) string pf = llJsonGetValue(r, ["f"]); list on; integer q; for (q = 0; q < llGetListLength(ag) && llGetListLength(on) < 5; ++q) { key pk = llList2Key(ag, q); if (llOverMyLand(pk)) { if (passes(pk, pf)) { if (llListFindList(gSeen, [id + "|" + (string)pk]) == -1) { on += llList2Json(JSON_OBJECT, ["k", pk, "n", llGetDisplayName(pk)]); gSeen = llList2List(gSeen + [id + "|" + (string)pk, now + (integer)llJsonGetValue(r, ["cd"])], -60, -1); } } } } if (on != []) report(llList2Json(JSON_OBJECT, ["type", "rule", "rule", (integer)id, "who", llList2Json(JSON_ARRAY, on)])); } else if (kind == "arrive") { string f = llJsonGetValue(r, ["f"]); list who; integer j; for (j = 0; j < llGetListLength(fresh) && llGetListLength(who) < 5; ++j) { key k = llList2Key(fresh, j); if (passes(k, f)) { if (llListFindList(gSeen, [id + "|" + (string)k]) == -1) { who += llList2Json(JSON_OBJECT, ["k", k, "n", llGetDisplayName(k)]); gSeen = llList2List(gSeen + [id + "|" + (string)k, now + (integer)llJsonGetValue(r, ["cd"])], -60, -1); } } } if (who != []) report(llList2Json(JSON_OBJECT, ["type", "rule", "rule", (integer)id, "who", llList2Json(JSON_ARRAY, who)])); } else if (kind == "near") { vector c = llGetPos(); string cs = llJsonGetValue(r, ["c"]); if (cs != "" && cs != JSON_INVALID) c = (vector)cs; float range = (float)llJsonGetValue(r, ["r"]); string f = llJsonGetValue(r, ["f"]); list who; integer j; for (j = 0; j < llGetListLength(ag) && llGetListLength(who) < 5; ++j) { key k = llList2Key(ag, j); integer d = (integer)llVecDist(c, llList2Vector(llGetObjectDetails(k, [OBJECT_POS]), 0)); if (d <= (integer)range) { if (passes(k, f)) { if (llListFindList(gSeen, [id + "|" + (string)k]) == -1) { who += llList2Json(JSON_OBJECT, ["k", k, "n", llGetDisplayName(k), "d", d]); gSeen = llList2List(gSeen + [id + "|" + (string)k, now + (integer)llJsonGetValue(r, ["cd"])], -60, -1); } } } } if (who != []) report(llList2Json(JSON_OBJECT, ["type", "rule", "rule", (integer)id, "who", llList2Json(JSON_ARRAY, who)])); } else if (kind == "crowd") { // crowd: fire once when the region reaches n avatars, again only after it drops below integer n = llGetRegionAgentCount(); integer on = llListFindList(gCrowd, [id]); if (n >= (integer)llJsonGetValue(r, ["n"])) { if (on == -1) { gCrowd += id; report(llList2Json(JSON_OBJECT, ["type", "rule", "rule", (integer)id, "count", n])); } } else if (on != -1) gCrowd = llDeleteSubList(gCrowd, on, on); } } } default { state_entry() { report("{\"type\":\"hello\",\"part\":\"watch\"}"); llSetTimerEvent(5.0); } on_rez(integer p) { llResetScript(); } changed(integer c) { if (c & (CHANGED_OWNER | CHANGED_REGION_START)) llResetScript(); } timer() { if (gRules != []) watch(); } link_message(integer sender, integer num, string str, key id) { if (num == 9113) { if (str == "status") { llOwnerSay("dragon-watch, " + (string)(llGetFreeMemory() / 1024) + " KB free"); return; } report("{\"type\":\"hello\",\"part\":\"watch\"}"); return; } if (num != 9110) return; integer i; string a = llJsonGetValue(str, [0]); while (a != JSON_INVALID && i < 8) { if (llJsonGetValue(a, ["do"]) == "rules") { string rs = llJsonGetValue(a, ["rules"]); gRules = llJson2List(rs); gSeen = []; gCrowd = []; gPresent = []; gPrimed = FALSE; gArrive = (llSubStringIndex(rs, "\"arrive\"") != -1); } a = llJsonGetValue(str, [++i]); } } }