// Dragon Block - an Agent Node for Dragon AI Connect: one building block an agent rezzes to build things. // Put this script in a plain cube named "Dragon Block", make the cube copyable, and put it in your agent's contents // next to dragon_build.lsl. Add a texture pack: drop full-permission textures into the cube (e.g. from your // Library > Textures) and the agent can use them by name. Never rez it yourself: the agent rezzes it, tells it what // to become and where to go, and it stays there as part of the build, listening for changes. // // rez start parameter: the build channel. -> {"e":"ready","tex":["name|key",...]} to the rezzer until it has a job // (a texture's key is known only when it's full permission, like the textures in your Library) // <- job {"b":build,"n":index,"p":"","s":"","r":"" or "q":"" (exact rotation), // "t":box|cylinder|sphere|torus|prism|tube|ring, "tp":"","sh":"","dm":"","tw":"", // "h":hollow 0-0.95,"c":"","cl":"","a":alpha,"x":texture name or key,"rp":"", // "tm":metres per tile (repeats worked out per face from the block's size), // "g":glow,"f":fullbright,"ph":phantom,"py":physics,"l":"","li":intensity,"lr":radius, // "tc":report touches,"co":report collisions, // "d":door split|slide,"dt":touch|near|collide,"do":seconds open,"nm":name, // "st":"" ("0" = no seat),"sr":"","sx":sit text, // "pt":[particle rule, type, value, ...] ([] = stop),"sd":looping sound key ("" = stop),"sv":volume, // "ta":"mode,face,cols,rows,start,length,rate" (texture animation; mode 0 = off), // "sn":shine 0-3 (glossy: ceramic, glass, metal, water), // "sa":animation while seated, "an":animation on touch (Second Life's built-in ones by name; "" = none), // "mt":metallic 0-1,"ro":roughness 0-1,"em":"" (PBR: needs a plain material named "PBR" // in this cube; the texture and colour are carried over; older viewers keep the plain look), // "nr":normal map key (PBR surface relief, tiled like the texture; "" = none),"th":metres a tile covers // vertically when the texture isn't square (bricks),"of":"" (a part of a picture)} // -> {"e":"placed","b","n","k"} | {"e":"failed","b","n","why"} | {"e":"touch"|"collide","b","n","by","nm"} // <- later, for its build: {"b":build,"e":"set",...same fields} | {"e":"move","by":""} | {"e":"die"} | // {"e":"open"} | {"e":"close"} (optionally "n":[indexes] for some of the blocks only) integer gCh; key gAgent; key gRezzer; string gBuild; integer gN = -1; integer gAsked; integer gTouch; integer gCollide; string gDoor; string gDoorBy = "touch"; float gDoorOpenFor = 5.0; integer gOpen; vector gSize; string gType = "box"; float gMt = -1.0; float gRo = -1.0; string gEm; string gNr; integer gPbr; // PBR finish (gPbr: it's on) string gSitAnim; string gTouchAnim; // built-in animations: while seated / on touch key gAnimFor; string gAnimPlaying; integer gAnimSeat; // who it's animating, with what, and why string BLANK = "5748decc-f629-461c-9a36-a35a221fe21f"; string PLYWOOD = "89556747-24cb-43ed-920b-47caed15465f"; string CLEAR = "8dcd4a48-2d37-4909-9f78-f7a9eb4ef903"; anim_stop() // stop what it's playing on whoever it's playing it on (if they're still here) { if (gAnimFor != NULL_KEY && gAnimPlaying != "" && llGetPermissionsKey() == gAnimFor && (llGetPermissions() & PERMISSION_TRIGGER_ANIMATION) && llGetAgentSize(gAnimFor) != ZERO_VECTOR) llStopAnimation(gAnimPlaying); gAnimFor = NULL_KEY; gAnimPlaying = ""; gAnimSeat = FALSE; } anim_for(key av, string a, integer seated) // sitting grants the permission at once; a touch asks once { anim_stop(); gAnimFor = av; gAnimPlaying = a; gAnimSeat = seated; llRequestPermissions(av, PERMISSION_TRIGGER_ANIMATION); } string pbr_material() // the plain PBR material in this cube, if there is one that's been saved { // (a brand-new material has no content until it's opened and saved once) list names = ["PBR", "Dragon PBR"]; integer i; for (i = 0; i < 2; ++i) { string n = llList2String(names, i); if (llGetInventoryType(n) != INVENTORY_NONE && llGetInventoryKey(n) != NULL_KEY) return n; } return ""; } pbr() // the PBR finish on every face, the face's own texture, colour and see-through carried over { string m = pbr_material(); if (m == "") return; llSetRenderMaterial(m, ALL_SIDES); // (this clears earlier settings: they're all set again below) list q; integer f; integer n = llGetNumberOfSides(); for (f = 0; f < n; ++f) { list t = llGetLinkPrimitiveParams(LINK_THIS, [PRIM_TEXTURE, f, PRIM_COLOR, f]); float al = llList2Float(t, 5); integer mode = PRIM_GLTF_ALPHA_MODE_OPAQUE; if (al < 0.99) mode = PRIM_GLTF_ALPHA_MODE_BLEND; q += [PRIM_GLTF_BASE_COLOR, f, llList2String(t, 0), llList2Vector(t, 1), llList2Vector(t, 2), llList2Float(t, 3), llsRGB2Linear(llList2Vector(t, 4)), al, mode, 0.5, FALSE]; if (gNr != "") // surface relief, tiled exactly like the texture q += [PRIM_GLTF_NORMAL, f, gNr, llList2Vector(t, 1), llList2Vector(t, 2), llList2Float(t, 3)]; } llSetLinkPrimitiveParamsFast(LINK_THIS, q); list o; if (gMt >= 0) o += [OVERRIDE_GLTF_METALLIC_FACTOR, gMt]; if (gRo >= 0) o += [OVERRIDE_GLTF_ROUGHNESS_FACTOR, gRo]; if (gEm != "") o += [OVERRIDE_GLTF_EMISSIVE_FACTOR, llsRGB2Linear((vector)gEm)]; if (o != []) llSetLinkGLTFOverrides(LINK_THIS, ALL_SIDES, o); gPbr = TRUE; } say(string m) { if (gAgent) llRegionSayTo(gAgent, gCh, m); } list textures() // "name|key" for each texture inside, as many as fit in one message (1,024 characters) { list out; integer i; integer used = 40; for (i = 0; i < llGetInventoryNumber(INVENTORY_TEXTURE); ++i) { string n = llGetInventoryName(INVENTORY_TEXTURE, i); key k = llGetInventoryKey(n); // known only for full-permission textures (your Library's are) string ks = ""; if (k) ks = (string)k; string e = llGetSubString(n, 0, 40) + "|" + ks; used += llStringLength(e) + 3; if (used > 900) return out; out += e; } return out; } string tex(string x) { string low = llToLower(x); if (low == "blank" || low == "white") return BLANK; if (low == "plywood" || low == "wood") return PLYWOOD; if (low == "transparent" || low == "clear") return CLEAR; if (llGetInventoryType(x) == INVENTORY_TEXTURE) return x; if ((key)x) return x; return ""; } particles(string rules) // [rule, type, value, ...]: type i (integer), f (float), v (vector), k (key); [] = off { list r = llJson2List(rules); list p; integer i; for (i = 0; i + 2 < llGetListLength(r); i += 3) { string t = llList2String(r, i + 1); string v = llList2String(r, i + 2); p += llList2Integer(r, i); if (t == "i") p += (integer)v; else if (t == "f") p += (float)v; else if (t == "k") p += (key)v; else p += (vector)v; } llParticleSystem(p); } integer has(string j, string k) { return llJsonGetValue(j, [k]) != JSON_INVALID; } string val(string j, string k) { return llJsonGetValue(j, [k]); } shape(string j) { list p; if (has(j, "s")) { gSize = (vector)val(j, "s"); if (gSize.x > 64) gSize.x = 64; if (gSize.y > 64) gSize.y = 64; if (gSize.z > 64) gSize.z = 64; if (gSize.x < 0.01) gSize.x = 0.01; if (gSize.y < 0.01) gSize.y = 0.01; if (gSize.z < 0.01) gSize.z = 0.01; p += [PRIM_SIZE, gSize]; } if (has(j, "q")) p += [PRIM_ROTATION, (rotation)val(j, "q")]; // exact (triangles, roofs) else if (has(j, "r")) p += [PRIM_ROTATION, llEuler2Rot((vector)val(j, "r") * DEG_TO_RAD)]; integer reshape = has(j, "t") || has(j, "h") || has(j, "c") || has(j, "tp") || has(j, "sh") || has(j, "dm") || has(j, "tw"); if (reshape) { string t = val(j, "t"); if (t == JSON_INVALID) t = gType; gType = t; float h = (float)val(j, "h"); if (h > 0.95) h = 0.95; vector c = <0, 1, 0>; vector tp = <1, 1, 0>; vector sh = ZERO_VECTOR; vector dm = <0, 1, 0>; vector tw = ZERO_VECTOR; if (has(j, "c")) c = (vector)val(j, "c"); if (has(j, "tp")) tp = (vector)val(j, "tp"); // taper: the top's size, 1 = none, 0 = to a point if (has(j, "sh")) sh = (vector)val(j, "sh"); // shear: moves the top sideways (-0.5 to 0.5) if (has(j, "dm")) dm = (vector)val(j, "dm"); // dimple (spheres) if (has(j, "tw")) tw = (vector)val(j, "tw"); // twist if (t == "cylinder") p += [PRIM_TYPE, PRIM_TYPE_CYLINDER, PRIM_HOLE_DEFAULT, c, h, tw, tp, sh]; else if (t == "sphere") p += [PRIM_TYPE, PRIM_TYPE_SPHERE, PRIM_HOLE_DEFAULT, c, h, tw, dm]; else if (t == "torus" || t == "ring" || t == "tube") { integer pt = PRIM_TYPE_TORUS; if (t == "ring") pt = PRIM_TYPE_RING; if (t == "tube") pt = PRIM_TYPE_TUBE; p += [PRIM_TYPE, pt, PRIM_HOLE_DEFAULT, c, h, tw, <1, 0.25, 0>, sh, <0, 1, 0>, ZERO_VECTOR, 1.0, 0.0, 0.0]; } else if (t == "prism") p += [PRIM_TYPE, PRIM_TYPE_PRISM, PRIM_HOLE_DEFAULT, c, h, tw, tp, sh]; else p += [PRIM_TYPE, PRIM_TYPE_BOX, PRIM_HOLE_SQUARE, c, h, tw, tp, sh]; } if (has(j, "x")) { string k = tex(val(j, "x")); vector rp = <1, 1, 0>; if (has(j, "rp")) rp = (vector)val(j, "rp"); float tm = (float)val(j, "tm"); // the texture's real size (metres a tile): repeat to fit float th = (float)val(j, "th"); // ...and its height, when it isn't square (bricks) if (th <= 0) th = tm; if (k != "") { if (tm > 0) { vector z = gSize; if (z == ZERO_VECTOR) z = llGetScale(); if (gType == "box") // each face by its own size: no stretched planks p += [PRIM_TEXTURE, 0, k, , ZERO_VECTOR, 0.0, PRIM_TEXTURE, 5, k, , ZERO_VECTOR, 0.0, PRIM_TEXTURE, 1, k, , ZERO_VECTOR, 0.0, PRIM_TEXTURE, 3, k, , ZERO_VECTOR, 0.0, PRIM_TEXTURE, 2, k, , ZERO_VECTOR, 0.0, PRIM_TEXTURE, 4, k, , ZERO_VECTOR, 0.0]; else { float a = z.x; if (z.y > a) a = z.y; p += [PRIM_TEXTURE, ALL_SIDES, k, , ZERO_VECTOR, 0.0]; } } else { vector of = ZERO_VECTOR; // a part of a bigger picture (a plant's bark, branch...) if (has(j, "of")) of = (vector)val(j, "of"); p += [PRIM_TEXTURE, ALL_SIDES, k, rp, of, 0.0]; } } } if (has(j, "cl") || has(j, "a")) { vector cl = <1, 1, 1>; float a = 1.0; if (has(j, "cl")) cl = (vector)val(j, "cl"); else cl = llList2Vector(llGetLinkPrimitiveParams(LINK_THIS, [PRIM_COLOR, 0]), 0); if (has(j, "a")) a = (float)val(j, "a"); p += [PRIM_COLOR, ALL_SIDES, cl, a]; } if (has(j, "g")) p += [PRIM_GLOW, ALL_SIDES, (float)val(j, "g")]; if (has(j, "sn")) p += [PRIM_BUMP_SHINY, ALL_SIDES, (integer)val(j, "sn"), PRIM_BUMP_NONE]; if (has(j, "f")) p += [PRIM_FULLBRIGHT, ALL_SIDES, val(j, "f") == "1" || val(j, "f") == JSON_TRUE]; if (has(j, "l")) p += [PRIM_POINT_LIGHT, TRUE, (vector)val(j, "l"), (float)val(j, "li") + 0.6 * !has(j, "li"), (float)val(j, "lr") + 10.0 * !has(j, "lr"), 0.75]; if (has(j, "ph")) p += [PRIM_PHANTOM, val(j, "ph") == "1" || val(j, "ph") == JSON_TRUE]; if (has(j, "py")) p += [PRIM_PHYSICS, val(j, "py") == "1" || val(j, "py") == JSON_TRUE]; if (p != []) llSetLinkPrimitiveParamsFast(LINK_THIS, p); if (has(j, "mt")) gMt = (float)val(j, "mt"); if (has(j, "ro")) gRo = (float)val(j, "ro"); if (has(j, "em")) gEm = val(j, "em"); if (has(j, "nr")) gNr = val(j, "nr"); if (has(j, "mt") || has(j, "ro") || has(j, "em") || has(j, "nr") || (gPbr && (has(j, "x") || has(j, "cl") || has(j, "a")))) pbr(); // (a recolour or new texture is carried over too) if (has(j, "tc")) gTouch = (integer)val(j, "tc") || val(j, "tc") == JSON_TRUE; if (has(j, "co")) gCollide = (integer)val(j, "co") || val(j, "co") == JSON_TRUE; if (has(j, "nm")) llSetObjectName(llGetSubString(val(j, "nm"), 0, 62)); if (has(j, "st")) { // a seat: where you sit on it and which way you face if (val(j, "st") == "0") { llSitTarget(ZERO_VECTOR, ZERO_ROTATION); llSetClickAction(CLICK_ACTION_NONE); } else { vector sr = ZERO_VECTOR; if (has(j, "sr")) sr = (vector)val(j, "sr"); llSitTarget((vector)val(j, "st"), llEuler2Rot(sr * DEG_TO_RAD)); if (!gTouch) llSetClickAction(CLICK_ACTION_SIT); // one click sits (unless touches are reported) } } if (has(j, "sx")) llSetSitText(val(j, "sx")); if (has(j, "sa")) { gSitAnim = val(j, "sa"); key av = llAvatarOnSitTarget(); if (gAnimSeat) anim_stop(); if (av != NULL_KEY && gSitAnim != "") anim_for(av, gSitAnim, TRUE); } if (has(j, "an")) { gTouchAnim = val(j, "an"); if (!gAnimSeat) anim_stop(); } if (has(j, "pt")) particles(val(j, "pt")); // runs for as long as the block is there if (has(j, "ta")) { // texture animation: scroll, spin, pulse or flipbook frames list t = llCSV2List(val(j, "ta")); llSetTextureAnim(llList2Integer(t, 0), llList2Integer(t, 1), llList2Integer(t, 2), llList2Integer(t, 3), llList2Float(t, 4), llList2Float(t, 5), llList2Float(t, 6)); } if (has(j, "sd")) { // a sound on a loop (by key: any object can play it) if (val(j, "sd") == "") llStopSound(); else { float vol = 1.0; if (has(j, "sv")) vol = (float)val(j, "sv"); llLoopSound(val(j, "sd"), vol); } } if (has(j, "d")) { gDoor = val(j, "d"); if (has(j, "dt")) gDoorBy = val(j, "dt"); if (has(j, "do")) gDoorOpenFor = (float)val(j, "do"); if (gDoorOpenFor < 1) gDoorOpenFor = 5.0; if (gDoorBy == "near") llSensorRepeat("", NULL_KEY, AGENT, 3.0, PI, 1.0); else llSensorRemove(); } } door(integer open) // a one-prim door: it reshapes instead of moving (split: opens from the middle; slide: to one side) { if (gDoor == "" || open == gOpen) return; gOpen = open; integer i; for (i = 1; i <= 8; ++i) { float f = (float)i / 8.0; if (!open) f = 1.0 - f; if (gDoor == "slide") llSetLinkPrimitiveParamsFast(LINK_THIS, [PRIM_SLICE, <0.0, 1.0 - 0.95 * f, 0.0>]); else llSetLinkPrimitiveParamsFast(LINK_THIS, [PRIM_TYPE, PRIM_TYPE_BOX, PRIM_HOLE_SQUARE, <0, 1, 0>, 0.95 * f, ZERO_VECTOR, <1, 1, 0>, ZERO_VECTOR]); llSleep(0.05); } llSetLinkPrimitiveParamsFast(LINK_THIS, [PRIM_PHANTOM, open]); // walk through while it's open if (open) llSetTimerEvent(gDoorOpenFor); } report(string e, key by) { say(llList2Json(JSON_OBJECT, ["e", e, "b", gBuild, "n", gN, "by", by, "nm", llKey2Name(by)])); } integer mine(string m) // a command for this build, and for this block (all of them when there's no "n") { if (val(m, "b") != gBuild) return FALSE; string ns = val(m, "n"); if (ns == JSON_INVALID) return TRUE; return llListFindList(llJson2List(ns), [(string)gN]) != -1; } default { on_rez(integer p) { if (p == 0) return; // taken and rezzed again by hand: it keeps being what it was gCh = p; gN = -1; gBuild = ""; gAgent = NULL_KEY; llListen(gCh, "", NULL_KEY, ""); gRezzer = llList2Key(llGetObjectDetails(llGetKey(), [OBJECT_REZZER_KEY]), 0); llRegionSayTo(gRezzer, gCh, llList2Json(JSON_OBJECT, ["e", "ready", "tex", llList2Json(JSON_ARRAY, textures())])); gAsked = 1; llSetTimerEvent(1.5); } listen(integer ch, string name, key id, string m) { if (llGetOwnerKey(id) != llGetOwner()) return; // only its owner's agents if (gN < 0) { // the job if (id != gRezzer || !has(m, "n")) return; gAgent = id; gBuild = val(m, "b"); gN = (integer)val(m, "n"); llSetTimerEvent(0); vector p = (vector)val(m, "p"); integer ok = !(p.x < 0 || p.x > 256 || p.y < 0 || p.y > 256 || p.z > 4000); // LSL evaluates every term, if (ok) ok = llSetRegionPos(p); // so move only when in bounds if (!ok) { say(llList2Json(JSON_OBJECT, ["e", "failed", "b", gBuild, "n", gN, "why", "can't go to " + (string)p])); llDie(); return; } // start from a plain prim, whatever the Dragon Block cube itself looks like (glow, colour, light...) llSetLinkPrimitiveParamsFast(LINK_THIS, [PRIM_GLOW, ALL_SIDES, 0.0, PRIM_FULLBRIGHT, ALL_SIDES, FALSE, PRIM_COLOR, ALL_SIDES, <1, 1, 1>, 1.0, PRIM_POINT_LIGHT, FALSE, ZERO_VECTOR, 0.0, 0.0, 0.0, PRIM_TEXTURE, ALL_SIDES, BLANK, <1, 1, 0>, ZERO_VECTOR, 0.0, // plain white: colour shows true PRIM_BUMP_SHINY, ALL_SIDES, PRIM_SHINY_NONE, PRIM_BUMP_NONE]); llParticleSystem([]); llStopSound(); llSetTextureAnim(FALSE, ALL_SIDES, 0, 0, 0.0, 0.0, 1.0); if (pbr_material() != "") llSetRenderMaterial((string)NULL_KEY, ALL_SIDES); // plain until a finish shape(m); if (!has(m, "nm")) llSetObjectName("Dragon Block " + gBuild + "#" + (string)gN); say(llList2Json(JSON_OBJECT, ["e", "placed", "b", gBuild, "n", gN, "k", llGetKey()])); return; } if (!mine(m)) return; gAgent = id; string e = val(m, "e"); if (e == "die") llDie(); else if (e == "set") shape(m); else if (e == "move") llSetRegionPos(llGetPos() + (vector)val(m, "by")); else if (e == "open") door(TRUE); else if (e == "close") door(FALSE); else if (e == "ping") say(llList2Json(JSON_OBJECT, ["e", "placed", "b", gBuild, "n", gN, "k", llGetKey()])); } touch_start(integer t) { if (gN < 0) return; if (gDoor != "" && gDoorBy == "touch") door(!gOpen); if (gTouch) report("touch", llDetectedKey(0)); if (gTouchAnim != "") { // dance (or wave...) here; touch again to stop key av = llDetectedKey(0); if (av == gAnimFor && !gAnimSeat) anim_stop(); else if (!gAnimSeat) anim_for(av, gTouchAnim, FALSE); } } changed(integer c) { if (!(c & CHANGED_LINK) || gN < 0) return; key av = llAvatarOnSitTarget(); if (av != NULL_KEY) { if (gSitAnim != "" && !(gAnimSeat && av == gAnimFor)) anim_for(av, gSitAnim, TRUE); } else if (gAnimSeat) anim_stop(); // they stood up } run_time_permissions(integer p) { if (!(p & PERMISSION_TRIGGER_ANIMATION) || llGetPermissionsKey() != gAnimFor || gAnimPlaying == "") return; if (gAnimSeat) { if (llAvatarOnSitTarget() != gAnimFor) { gAnimFor = NULL_KEY; gAnimPlaying = ""; gAnimSeat = FALSE; return; } llStopAnimation("sit"); // the default sit, replaced by the seat's own } llStartAnimation(gAnimPlaying); } collision_start(integer t) { if (gN < 0) return; if (gDoor != "" && gDoorBy == "collide") door(TRUE); if (gCollide) report("collide", llDetectedKey(0)); } sensor(integer n) { if (gDoor != "" && gDoorBy == "near") door(TRUE); } timer() { if (gN < 0) { // no job yet: ask again, a few times, then go if (gAsked >= 6) llDie(); ++gAsked; llRegionSayTo(gRezzer, gCh, llList2Json(JSON_OBJECT, ["e", "ready", "tex", llList2Json(JSON_ARRAY, textures())])); return; } llSetTimerEvent(0); door(FALSE); // doors close by themselves } }