// Dragon Build - part of the Dragon Library for a Dragon AI Connect agent (Mind plan and up). // Put it next to dragon_api.lsl, with a copyable cube named "Dragon Block" (holding dragon_block.lsl and, if you like, // a texture pack) in the same contents. 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 designs, this script places. // Guide: https://dragonaiconnect.com/docs/runtime/toolkit // // It rezzes Dragon Blocks one at a time (Second Life only rezzes within 10 m, so each block appears here and then // flies to its spot), hands each its job, and passes messages between the blocks and the server. // // 9110 <- API {"do":"build","b":build,"ch":channel,"blocks":[jobs],"more":1} (more: ask for the next batch) // {"do":"blocks","b":build,"ch":channel,"cmd":{...}} (change, move, open, remove...) // with "u" and "tok" (the build's pass): this script then fetches the next batches and reports // to the server itself (POST u: {"b","tok","event":more|placed|failed|done|touch|collide,...}), // big batches, one request at a time, so dragon_api stays free for chat // 9118 -> API {"type":"hello","part":"build"} // {"type":"build","event":"placed","b","ns":[...],"tex":[...]} | "failed" | "more" | "done" // {"type":"build","event":"touch"|"collide","b","n","t":who,"nm":name} // 9113 <- API "announce yourself" / "status" string BLOCK = "Dragon Block"; integer gCh; integer gListen; string gBuild; integer gMore; list gQueue; // jobs waiting (JSON strings) list gRezzed; // strided [block key, job]: rezzed, waiting for its "ready" list gNs; list gTex; integer gBusy; string gUrl; string gTok; // the build's pass: with it, this script talks to the server itself (big batches, list gHq; key gHreq = NULL_KEY; // off dragon_api) one request at a time; gHq waits, gHreq is in flight float gRezAt; hello() { llMessageLinked(LINK_THIS, 9118, llList2Json(JSON_OBJECT, ["type", "hello", "part", "build"]), NULL_KEY); } report(integer type, string ev) // what's in this object, "name|key" (keys for full-permission ones), for the { // account's library: drop textures or sounds into your agent and every agent you own list out; integer i; integer used; // can use them integer n = llGetInventoryNumber(type); for (i = 0; i < n; ++i) { string t = llGetInventoryName(type, i); key k = llGetInventoryKey(t); string ks = ""; if (k) ks = (string)k; string e = llGetSubString(t, 0, 40) + "|" + ks; if (used + llStringLength(e) > 1500) { // in parts that fit a message llMessageLinked(LINK_THIS, 9118, llList2Json(JSON_OBJECT, ["type", "build", "event", ev, "tex", llList2Json(JSON_ARRAY, out)]), NULL_KEY); out = []; used = 0; } out += e; used += llStringLength(e) + 3; } if (out != []) llMessageLinked(LINK_THIS, 9118, llList2Json(JSON_OBJECT, ["type", "build", "event", ev, "tex", llList2Json(JSON_ARRAY, out)]), NULL_KEY); } textures() { report(INVENTORY_TEXTURE, "textures"); report(INVENTORY_SOUND, "sounds"); } tell(list kv) { if (gTok == "") { // an older server: through dragon_api llMessageLinked(LINK_THIS, 9118, llList2Json(JSON_OBJECT, ["type", "build", "b", gBuild] + kv), NULL_KEY); return; } gHq = llList2List(gHq + [llList2Json(JSON_OBJECT, ["b", gBuild, "tok", gTok] + kv)], -12, -1); hsend(); } hsend() { if (gHreq != NULL_KEY) return; // one at a time if (gHq == []) return; gHreq = llHTTPRequest(gUrl, [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/json", HTTP_BODY_MAXLENGTH, 16384, HTTP_VERIFY_CERT, TRUE], llList2String(gHq, 0)); if (gHreq == NULL_KEY) llSetTimerEvent(3.0); // Second Life's throttle: try again shortly } list placed() // the blocks that landed since the last report, to send along with another message { list kv = ["ns", llList2Json(JSON_ARRAY, gNs), "tex", llList2Json(JSON_ARRAY, gTex)]; // numbers only: small gNs = []; gTex = []; return kv; } flush() { if (gNs == []) return; tell(["event", "placed", "ns", llList2Json(JSON_ARRAY, gNs), "tex", llList2Json(JSON_ARRAY, gTex)]); gNs = []; gTex = []; } channel(integer ch) { if (ch == gCh && gListen) return; if (gListen) llListenRemove(gListen); gCh = ch; gListen = llListen(gCh, "", NULL_KEY, ""); } next() { if (gQueue == []) { if (gRezzed != []) return; // still waiting for blocks to say ready if (gMore) { gMore = FALSE; tell(["event", "more"] + placed()); } // one request: what landed + more else if (gBusy) { gBusy = FALSE; tell(["event", "done"] + placed()); } return; } if (llGetInventoryType(BLOCK) != INVENTORY_OBJECT) { tell(["event", "failed", "why", "no 'Dragon Block' in my contents"]); gQueue = []; gMore = FALSE; gBusy = FALSE; return; } llRezObject(BLOCK, llGetPos() + <0.0, 0.0, 1.5>, ZERO_VECTOR, ZERO_ROTATION, gCh); } default { state_entry() { hello(); textures(); } on_rez(integer p) { llResetScript(); } changed(integer c) { if (c & (CHANGED_OWNER | CHANGED_REGION)) llResetScript(); if (c & CHANGED_INVENTORY) textures(); // new textures dropped in: tell the library } object_rez(key k) { gRezzed += [k, llList2String(gQueue, 0)]; gQueue = llDeleteSubList(gQueue, 0, 0); gRezAt = llGetTime(); llSetTimerEvent(10.0); // a block that never says ready is skipped next(); } listen(integer ch, string name, key id, string m) { if (llGetOwnerKey(id) != llGetOwner()) return; string e = llJsonGetValue(m, ["e"]); if (e == "ready") { integer i = llListFindList(gRezzed, [id]); if (i == -1) return; string job = llList2String(gRezzed, i + 1); gRezzed = llDeleteSubList(gRezzed, i, i + 1); if (gTex == []) gTex = llJson2List(llJsonGetValue(m, ["tex"])); llRegionSayTo(id, gCh, job); if (gQueue == []) next(); return; } if (e == "placed") { gNs += (integer)llJsonGetValue(m, ["n"]); if (llGetListLength(gNs) >= 40) flush(); // a big batch: report it else if (gQueue == []) { if (gRezzed == []) { if (!gMore) llSetTimerEvent(2.0); } } // the last ones: gather 2 s return; } if (e == "failed") { tell(["event", "failed", "ns", llList2Json(JSON_ARRAY, [(integer)llJsonGetValue(m, ["n"])]), "why", llJsonGetValue(m, ["why"])]); return; } if (e == "touch" || e == "collide") llMessageLinked(LINK_THIS, 9118, llList2Json(JSON_OBJECT, ["type", "build", "event", e, "b", llJsonGetValue(m, ["b"]), "n", (integer)llJsonGetValue(m, ["n"]), "t", llJsonGetValue(m, ["by"]), "nm", llJsonGetValue(m, ["nm"])]), NULL_KEY); } timer() { llSetTimerEvent(0); if (gRezzed != []) { if (llGetTime() - gRezAt > 10.0) { gRezzed = []; next(); } // never said ready: skip them else llSetTimerEvent(3.0); } if (gNs != []) { if (gQueue == []) { if (gRezzed == []) flush(); } } // stragglers, in one report if (gHq != []) { if (gHreq == NULL_KEY) hsend(); } // a report the throttle held back } http_response(key req, integer status, list meta, string body) { if (req != gHreq) return; gHreq = NULL_KEY; if (status == 200) { gHq = llDeleteSubList(gHq, 0, 0); gMore = llJsonGetValue(body, ["more"]) == "1"; string blocks = llJsonGetValue(body, ["blocks"]); body = ""; // free each copy as soon as it's used (64 KB in all) if (blocks != JSON_INVALID) { // the next batch list got = llJson2List(blocks); blocks = ""; gQueue += got; got = []; if (gRezzed == []) next(); } } else if (status == 403 || status == 404) gHq = []; // the build is gone: nothing to report else llSetTimerEvent(3.0); // the server or network hiccuped: retry hsend(); } link_message(integer sender, integer num, string str, key id) { if (num == 9113) { if (str == "status") { llOwnerSay("dragon-build, " + (string)(llGetFreeMemory() / 1024) + " KB free, blocks: " + llList2String(["missing (put a copyable 'Dragon Block' in my contents)", "ready"], llGetInventoryType(BLOCK) == INVENTORY_OBJECT) + ", queued " + (string)llGetListLength(gQueue)); return; } hello(); textures(); return; } if (num != 9110) return; integer i; string a = llJsonGetValue(str, [0]); while (a != JSON_INVALID && i < 8) { string w = llJsonGetValue(a, ["do"]); if (w == "build") { channel((integer)llJsonGetValue(a, ["ch"])); gBuild = llJsonGetValue(a, ["b"]); gTok = ""; gUrl = ""; if (llJsonGetValue(a, ["tok"]) != JSON_INVALID) { gTok = llJsonGetValue(a, ["tok"]); gUrl = llJsonGetValue(a, ["u"]); } gMore = llJsonGetValue(a, ["more"]) == "1"; gQueue += llJson2List(llJsonGetValue(a, ["blocks"])); if (!gBusy) { gBusy = TRUE; next(); } else if (gRezzed == [] && llGetListLength(gQueue) == llGetListLength(llJson2List(llJsonGetValue(a, ["blocks"])))) next(); } else if (w == "blocks") { channel((integer)llJsonGetValue(a, ["ch"])); llRegionSay(gCh, llJsonGetValue(a, ["cmd"])); } a = llJsonGetValue(str, [++i]); } } }