// Dragon Give - part of the Dragon Library for a Dragon AI Connect agent. 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, this script hands things out. Guide: https://dragonaiconnect.com/docs/runtime/library // // Hands: give_item, give_folder, rez. Also tells the server what's in the object's inventory (so the AI // knows what it can give) and the region's maturity rating (for the teen safeguard), and uploads the FAQ and Knowledge notecards when they change. // 9110 <- API actions; 9111 world -> API; 9116 notecard chunk -> API; 9112 hello -> API; 9113 <- API list gNc; // strided [notecard, asset key at last upload] list gNcQ; string gNcName; integer gNcLine; integer gNcChunk; key gNcReq; string gNcBuf; string gRating = "UNKNOWN"; key gRatingReq; // the region's maturity: PG (General), MATURE (Moderate), ADULT hello(string why) { string m = "dragon-give, " + (string)(llGetFreeMemory() / 1024) + " KB free"; if (why == "status") { llOwnerSay(m); return; } llMessageLinked(LINK_THIS, 9112, m, NULL_KEY); } string names(integer type) { list out; integer i; for (i = 0; i < llGetInventoryNumber(type) && i < 40; ++i) out += llGetInventoryName(type, i); return llList2Json(JSON_ARRAY, out); } world() // rarely changes: the server stores it instead of it being sent with every request { llMessageLinked(LINK_THIS, 9111, llList2Json(JSON_OBJECT, [ "region", llGetRegionName(), "rating", gRating, "parcel", llList2String(llGetParcelDetails(llGetPos(), [PARCEL_DETAILS_NAME]), 0), "inventory", llList2Json(JSON_OBJECT, [ "object", names(INVENTORY_OBJECT), "notecard", names(INVENTORY_NOTECARD), "landmark", names(INVENTORY_LANDMARK), "clothing", names(INVENTORY_CLOTHING), "animation", names(INVENTORY_ANIMATION), "sound", names(INVENTORY_SOUND)])]), NULL_KEY); } ncNext() { while (gNcQ != []) { gNcName = llList2String(gNcQ, 0); gNcQ = llDeleteSubList(gNcQ, 0, 0); key now = llGetInventoryKey(gNcName); integer i = llListFindList(gNc, [gNcName]); key before = NULL_KEY; if (i != -1) { before = llList2Key(gNc, i + 1); gNc = llDeleteSubList(gNc, i, i + 1); } integer here = llGetInventoryType(gNcName) == INVENTORY_NOTECARD; if (here) gNc += [gNcName, now]; if (now == NULL_KEY || now != before || !here) { gNcChunk = 0; gNcBuf = ""; if (here) { gNcLine = 0; gNcReq = llGetNotecardLine(gNcName, 0); return; } if (before != NULL_KEY) ncSend(TRUE); // removed: clear it on the server } } } ncSend(integer last) { string kind = "knowledge"; if (gNcName == "FAQ") kind = "faq"; llMessageLinked(LINK_THIS, 9116, llList2Json(JSON_OBJECT, ["kind", kind, "chunk", gNcChunk, "last", llList2String([JSON_FALSE, JSON_TRUE], last), "text", gNcBuf]), NULL_KEY); ++gNcChunk; gNcBuf = ""; } refresh() { world(); gRatingReq = llRequestSimulatorData(llGetRegionName(), DATA_SIM_RATING); // world() again when it arrives gNcQ = ["FAQ", "Knowledge"]; ncNext(); } perform(string a) { string w = llJsonGetValue(a, ["do"]); key to = (key)llJsonGetValue(a, ["to"]); string it = llJsonGetValue(a, ["item"]); if (w == "give_item") { if (to) { if (llGetInventoryType(it) != INVENTORY_NONE) llGiveInventory(to, it); } } else if (w == "give_folder") { list items = llJson2List(llJsonGetValue(a, ["items"])); list ok; integer i; for (i = 0; i < llGetListLength(items); ++i) if (llGetInventoryType(llList2String(items, i)) != INVENTORY_NONE) ok += llList2String(items, i); if (to) { if (ok != []) llGiveInventoryList(to, llJsonGetValue(a, ["folder"]), ok); } } else if (w == "rez") { if (llGetInventoryType(it) == INVENTORY_OBJECT) llRezObject(it, llGetPos() + <0.0, 0.0, 1.0>, ZERO_VECTOR, llGetRot(), 0); } } default { state_entry() { hello(""); refresh(); } on_rez(integer p) { llResetScript(); } changed(integer c) { if (c & CHANGED_OWNER) llResetScript(); if (c & (CHANGED_INVENTORY | CHANGED_REGION)) refresh(); } dataserver(key q, string data) { if (q == gRatingReq) { gRating = data; world(); return; } if (q != gNcReq) return; if (data == EOF) { ncSend(TRUE); ncNext(); return; } if (llStringLength(gNcBuf) + llStringLength(data) > 1800) ncSend(FALSE); gNcBuf += data + "\n"; gNcReq = llGetNotecardLine(gNcName, ++gNcLine); } link_message(integer sender, integer num, string str, key id) { if (num == 9113) { hello(str); if (str != "status") world(); return; } if (num != 9110) return; integer i; string a = llJsonGetValue(str, [0]); while (a != JSON_INVALID && i < 8) { perform(a); a = llJsonGetValue(str, [++i]); } } }