Skip to content

Instantly share code, notes, and snippets.

@typhartez
Created December 31, 2020 15:18
Show Gist options
  • Select an option

  • Save typhartez/9352488e4ecdcc4664d6173966126fe0 to your computer and use it in GitHub Desktop.

Select an option

Save typhartez/9352488e4ecdcc4664d6173966126fe0 to your computer and use it in GitHub Desktop.
Base64 for linkmap
// testing base64 packing of link/face definitions based on a list of parts
// base on uriesk work: https://github.com/uriesk/opensim-body-scripts/blob/master/src/body-main.lsl
#define DBG(_msg) llOwnerSay(_msg)
// definition of links (each item represents a link number)
list linkmap = [
1,2,3,4,5,6,7,8,9,10,11,12,13
,14,15
,16,17,18,19,20,21,22,23
,24,25,26,27,28,29,30,31
,32,33,34,35,36,37,38,39,40,41
,42,43,44,45,46,47,48,49,50,51
,52,53,54,55,56,57,58,59,60,61
];
// definition of affected links and faces
// each number is the concatenation of an index in the linkmap and the face
// 9 means ALL_SIDES
string UPPER_PARTS = "04,19,29,39,49,59,69,79,89,99,109,119,129";
string partsToBase64(list parts) {
// build a list of integers containing all links in the linkmap.
// each integer (4 bytes) contains 4 links, one byte is a bitset of faces
// (bit 0 is the face 0)
list ints;
integer c = llCeil((float)llGetListLength(linkmap) / (float)4);
while (~(--c)) ints += 0;
// traverse the parts list to fill bits in the created list
integer i;
string part;
integer link;
integer face;
integer b;
list allFaces;
integer pos;
integer int;
integer bit;
c = llGetListLength(parts);
for (i = 0; i < c; ++i) {
part = llList2String(parts, i);
link = (integer)llGetSubString(part, 0, -2);
face = (integer)llGetSubString(part, -1, -1);
// if face is 9, we expand it to 8 parts, each one its face
if (9 == face) {
allFaces = [];
for (b = 0; 8 > b; ++b) allFaces += (string)link+(string)b;
// replace the part by this list
parts = llListReplaceList(parts, allFaces, i, i);
c += 7;
// update current face to be 0
face = 0;
part = (string)link+(string)face;
}
// now get the byte corresponding to our link/face pair
pos = link / 4; // index in ints list
int = llList2Integer(ints, pos);
// OR the int with the bit offseted by the face number and the byte
int = int | (0x1 << (((link % 4) * 8) + face));
ints = llListReplaceList(ints, [int], pos, pos);
}
// generate the base64 string
string base64;
c = llGetListLength(ints);
for (i = 0; i < c; ++i) base64 += llGetSubString(llIntegerToBase64(llList2Integer(ints, i)), 0, 5);
return base64;
}
list base64ToParts(string base64) {
list parts;
integer i;
integer int;
integer b;
integer link;
integer byte;
integer f;
integer c = llStringLength(base64);
for (i = 0; i < c; i += 6) {
int = llBase64ToInteger(llGetSubString(base64, i, i+5));
for (b = 0; b < 4; ++b) {
link = (i / 6) * 4 + b;
byte = (int >> (b*8)) & 0xFF;
if (0xFF == byte) {
// if all is set, represent all faces at once
parts += (string)link+"9";
jump base64NextByte;
}
// set a part for each face having the bit set
for (f = 0; f < 8; ++f) if ((0x1 << f) & byte) parts += (string)link+(string)f;
@base64NextByte;
}
}
return parts;
}
default {
state_entry() {
llOwnerSay("DO TEST");
list parts = llParseString2List(UPPER_PARTS, [","], []);
llOwnerSay("parts: " + UPPER_PARTS);
string base64 = partsToBase64(parts);
llOwnerSay("base64: ("+(string)llStringLength(base64)+") " + base64);
parts = base64ToParts(base64);
llOwnerSay("parts(back): " + llDumpList2String(parts, ","));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment