Advertisement
Madi_Perth

Untitled

Dec 12th, 2023
966
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Linden Scripting 11.71 KB | Source Code | 0 0
  1. /*
  2. This file contains a series of defines and functions that will
  3. allow you to use linkSetData memory for lists.  These lists will
  4. not have all the versitlity of normal LSL lists.
  5.  
  6. The format in LinksetData memory are as follows.
  7.  
  8. The first entry in a list is the master header file.  It is
  9. defined as the following.
  10.  
  11. linkset listName is the list listName and the data is the number of
  12. entries the list.
  13.  
  14. <listName> -> <list entries number>
  15.  
  16. the format of each list entry is as follows
  17.  
  18. <listName>|<entrynumber>:<data>
  19.  
  20. */
  21. #include "color.lsl"
  22. #define DEBUG
  23.  
  24. #define BLINK(a,b,c) if((a%2)) llSetColor(b, ALL_SIDES); else llSetColor(c, ALL_SIDES);
  25.  
  26. DS(string sayd)
  27. {
  28. #ifdef DEBUG
  29.     llOwnerSay(sayd);
  30. #endif
  31. }
  32.  
  33. // http://wiki.secondlife.com/wiki/hex
  34.  
  35. string XDIGITS = "0123456789abcdef"; // could be "0123456789ABCDEF"
  36.  
  37. string hexes(integer bits)
  38. {
  39.     string nybbles = "";
  40.     while (bits)
  41.     {
  42.         integer lsn = bits & 0xF; // least significant nybble
  43.         string nybble = llGetSubString(XDIGITS, lsn, lsn);
  44.         nybbles = nybble + nybbles;
  45.         bits = bits >> 4; // discard the least significant bits at right
  46.         bits = bits & 0xfffFFFF; // discard the sign bits at left
  47.     }
  48.     return nybbles;
  49. }
  50.  
  51. string hex(integer value)
  52. {
  53.     if (value < 0)
  54.     {
  55.         return "-0x" + hexes(-value);
  56.     }
  57.     else if (value == 0)
  58.     {
  59.         return "0x0"; // hexes(value) == "" when (value == 0)
  60.     }
  61.     else // if (0 < value)
  62.     {
  63.         return "0x" + hexes(value);
  64.     }
  65. }
  66.  
  67. DLS(string linkList)
  68. {
  69.     list tl;
  70.     tl = llLinksetDataFindKeys("^" +linkList +"\\|", 0, 10);
  71.     tl = llListSort(tl, 0, 1);
  72.     DS(llDumpList2String(tl, "\n"));
  73. }
  74.  
  75. // The first step is the define the linkSetList entry
  76. integer linkSetDefine(string listName)
  77. {
  78.     // test to see if entry already exists, if so return error
  79.     // of -1
  80.     if(llLinksetDataRead(listName) != "")
  81.         return -1;
  82.     else
  83.         llLinksetDataWrite(listName, "0");
  84.     return 1; // linkSetList Created.
  85. }
  86.  
  87. // add a entry to the linkset entry and increment the
  88. // the count by one when adding.   create the linkSet
  89. // if it does not exist. any data other than a string
  90. // must be typecast to a string.
  91. integer addLinkData(string listName, string data)
  92. {
  93.     integer tcList;
  94.     // if listName exists, add link
  95.     // and increment count.
  96.     if(linkSetDefine(listName) == 1)
  97.         tcList = incrmLinkCount(listName, 0);
  98.     else
  99.         tcList = incrmLinkCount(listName, 1);
  100.     llLinksetDataWrite(listName +"|"+ (string) tcList +":"+data, "0");    
  101.  
  102.     return tcList;
  103. }
  104.  
  105. // get count total from base and increase it by 'count'
  106. integer incrmLinkCount(string listName, integer count)
  107. {
  108.     integer currentNum = (integer) llLinksetDataRead(listName);
  109.    
  110.     currentNum += count;
  111.     llLinksetDataWrite(listName, (string) currentNum);
  112.  
  113.     return currentNum;
  114. }
  115.  
  116. // Return the number of entries in a linkSetList
  117. integer GetlinkListLength(string listName)
  118. {
  119.     // return error if linkSetList does not exits of -1
  120.     integer listLength = (integer) llLinksetDataRead(listName);
  121.     if(listLength == 0)
  122.         return -1;
  123.     else
  124.         return (listLength + 1);
  125. }
  126.  
  127. /*
  128. The following fuctions return entries in a linkList.
  129. None of them currently support negative indexes.
  130. */
  131.  
  132. // returns a string that is at index in src
  133. string linkList2String(string src, integer index)
  134. {
  135.     string found;
  136.     string searchFor = src+"\\|"+(string) index +":";
  137.  
  138.     found = (string) llLinksetDataFindKeys("^" + searchFor, 0, 1);
  139.     return llGetSubString(found, (llSubStringIndex(found, ":") + 1), -1);
  140. }
  141.  
  142. // returns a integer that is at index in src
  143. integer linkList2Integer(string src, integer index)
  144. {
  145.     string found;
  146.     string searchFor = src+"\\|"+(string) index +":";
  147.  
  148.     found = (string) llLinksetDataFindKeys("^" + searchFor, 0, 1);
  149.     return (integer) llGetSubString(found, (llSubStringIndex(found, ":") + 1), -1);
  150. }
  151.  
  152. // returns a float that is at index in src
  153. float linkList2Float(string src, integer index)
  154. {
  155.     string found;
  156.     string searchFor = src+"\\|"+(string) index +":";
  157.  
  158.     found = (string) llLinksetDataFindKeys("^" + searchFor, 0, 1);
  159.     return (float) llGetSubString(found, (llSubStringIndex(found, ":") + 1), -1);
  160. }
  161.  
  162. // returns a key that is at index in src
  163. key linkList2Key(string src, integer index)
  164. {
  165.     string found;
  166.     string searchFor = src+"\\|"+(string) index +":";
  167.  
  168.     found = (string) llLinksetDataFindKeys("^" + searchFor, 0, 1);
  169.     return (key) llGetSubString(found, (llSubStringIndex(found, ":") + 1), -1);
  170. }
  171.  
  172. // returns a rotation that is at index in src
  173. rotation linkList2Rot(string src, integer index)
  174. {
  175.     string found;
  176.     string searchFor = src+"\\|"+(string) index +":";
  177.  
  178.     found = (string) llLinksetDataFindKeys("^" + searchFor, 0, 1);
  179.     return (rotation) llGetSubString(found, (llSubStringIndex(found, ":") + 1), -1);
  180. }
  181.  
  182. // returns a vector that is at index in src
  183. vector linkList2Vector(string src, integer index)
  184. {
  185.     string found;
  186.     string searchFor = src+"\\|"+(string) index +":";
  187.  
  188.     found = (string) llLinksetDataFindKeys("^" + searchFor, 0, 1);
  189.     return (vector) llGetSubString(found, (llSubStringIndex(found, ":") + 1), -1);
  190. }
  191.  
  192. // Returns a list that is a subset of a linkList.
  193. // be aware of memory limtations since linkLists can
  194. // be significnally larger than script memory.
  195. //
  196. // if the end parameter is a -1 the function will return
  197. // everything from start to the end of the linkList.  
  198. // linkList2List(listName, 0, -1) will return the entire linkList
  199. // as a list.  
  200. list linkList2List(string listName, integer start, integer end)
  201. {
  202.     // check see if linkList exists, return emply list if does not.
  203.     if(llLinksetDataRead(listName) == "")
  204.         return [""];
  205.     // linkList exist, but start is out of bounds,
  206.     // return a empty list.
  207.     integer listLength = GetlinkListLength(listName);
  208.     if(start > listLength)
  209.         return [""];
  210.     // if end of linkList is longer than linkList, set end to
  211.     // listLength
  212.     if(end > listLength || end == -1)
  213.         end = listLength;
  214.  
  215.     integer i;
  216.     list    tl;
  217.     for(i = start; i < (end + 1); i++)
  218.     {
  219.         // DS("i : " + (string)i);
  220.         tl += linkList2String(listName, i);
  221.     }
  222.     return tl;
  223. }
  224.  
  225. // Returns a string with the linklist converted to string
  226. // with seperator between entries.
  227. string linkListDump2String(string listName, string seperator)
  228. {
  229.     integer i;  
  230.     integer endofList = GetlinkListLength(listName);
  231.     string rc;
  232.  
  233.     for (i = 0; i < endofList; i++)
  234.         rc += linkList2String(listName, i) + seperator;
  235.     return rc;
  236. }
  237.  
  238. // returns a string with the linkList converted to string
  239. // with commas(,) between entries
  240. string linkList2CSV(string linkName)
  241. {
  242.     return linkListDump2String(linkName, ",");
  243. }
  244.  
  245. // removes entries from start to end of linkName. returning the new size of
  246. // linkName;
  247. // if a -1 is used for the end parameter then the linkList will be deleted
  248. // from start to end of the linkLIst
  249. integer linkListDeleteSubList(string linkName, integer start, integer end)
  250. {
  251.     integer i;
  252.     list tl;
  253.     integer tmpEnd = (integer) llLinksetDataRead(linkName);
  254.     string  tmpData;
  255.  
  256.     if(end == -1)
  257.         end = tmpEnd;
  258.     // remove entries from linkName, start to end
  259.     for(i = start; i < (end + 1); ++i)
  260.         tl = llLinksetDataDeleteFound("^" + linkName+"\\|"+(string) i +":", "");
  261.  
  262.     llLinksetDataWrite(linkName, (string) (start-1));
  263.     for(i = (end+1); i < (tmpEnd+1); i++)
  264.     {
  265.         tmpData = linkList2String(linkName, i);
  266.         addLinkData(linkName, tmpData);
  267.         tl = llLinksetDataDeleteFound("^" + linkName+"\\|"+(string) i +":", "");
  268.     }
  269.     return (integer) llLinksetDataRead(linkName);
  270. }
  271.  
  272. // The following takes list (src) and puts it in linkList (dest) starting at
  273. // position start.  If start is a -1, then the list is appended to the end
  274. // of the linkLinks.  The function returns the new length of the linkList.
  275. integer listInsertLinkList (string linkName, list src, integer start)
  276. {
  277.     integer i;
  278.     string tmplink = (string) llGenerateKey();
  279.  
  280.     // if start is longer than linkName, then just append
  281.     // the output to linkName.
  282.     if(start == -1 || start > GetlinkListLength(linkName))
  283.     {
  284.         for(i = 0; i < llGetListLength(src); i++)
  285.             addLinkData(linkName, llList2String(src, i));
  286.         DLS(linkName);
  287.     }
  288.     else // insert src into linkList at position indicated by start
  289.     {
  290.         // move linkName from 0 to start to tmplink
  291.         for(i = 0; i < start; ++i)
  292.             addLinkData(tmplink, linkList2String(linkName, i));
  293.         // add list src to the end of tmplink
  294.         for(i = 0; i < llGetListLength(src); i++)
  295.             addLinkData(tmplink, llList2String(src, i));
  296.         for(i = start; i < GetlinkListLength(linkName); i++)
  297.             addLinkData(tmplink, linkList2String(linkName, i));
  298.  
  299.         // delete old linkList, then move data from tmplink to
  300.         // listLink.
  301.         linkListRemove(linkName);
  302.         for (i = 0; i< GetlinkListLength(tmplink); i++)
  303.             addLinkData(linkName, linkList2String(tmplink, i));
  304.         linkListRemove(tmplink);
  305.     }
  306.  
  307.     return GetlinkListLength(linkName);
  308. }
  309.  
  310. // function to remove a linkList
  311. // returns -1 if not successful
  312. // returns 1 if successful
  313. integer linkListRemove(string linkName)
  314. {
  315.     llLinksetDataDeleteFound("^" + linkName+"\\|", "");
  316.     llLinksetDataDeleteFound("^" + linkName, "");
  317.  
  318.     return 1;
  319. }
  320.  
  321. // returns the index of the first instance of needle
  322. // in linkList of linkName
  323. integer linkListFind(string linkName, string needle)
  324. {
  325.     string findTmp = (string) llLinksetDataFindKeys("^" + linkName + "\\|" + ".*:.*" + needle +".*", 0, 1);
  326.     list tmpList = llParseString2List(findTmp, [":", "|"], []);
  327.     if(llGetListLength(tmpList) < 1)
  328.         return -1;
  329.     else
  330.         return llList2Integer(tmpList, 1);
  331. }
  332.  
  333. integer i;
  334.  
  335. default
  336. {
  337.     state_entry()
  338.     {
  339.         llSetTimerEvent(0.2);
  340.         // addLinkData("test", "fuckme");
  341.         // llLinksetDataReset();
  342.         // DLS("test");
  343.         DS((string) linkListFind("test", "ff"));
  344.     }
  345.  
  346.     timer()
  347.     {
  348.         llSetText((string) llLinksetDataAvailable()
  349.         + "\n"+(string) llLinksetDataCountKeys()
  350.         + "\n"+(string) llGetFreeMemory(), FUCHSIA, TRUE);
  351.         llSetColor(WHITE, ALL_SIDES );
  352.     }
  353.  
  354.     touch_start(integer num_detected)
  355.     {
  356.         llSetTimerEvent(0);
  357.         llResetTime();
  358.     }
  359.    
  360.     touch( integer num_detected )
  361.     {
  362.         if(llGetTime() > 2)
  363.             llSetColor(GREEN, ALL_SIDES);
  364.     }
  365.    
  366.     touch_end(integer num_detected)
  367.     {
  368.         llSetTimerEvent(0.5);
  369.         if(llGetTime() > 2)
  370.         {
  371.             llLinksetDataReset();
  372.             llSetColor(WHITE, ALL_SIDES );
  373.         }
  374.         else
  375.         {
  376.             llSetColor(BLUE, ALL_SIDES );
  377.             llResetTime();
  378.  
  379.             for(i = 0; i < 500; ++i)
  380.             {
  381.                 addLinkData("test", hex(i));
  382.                 // addLinkData("goat", (string) ZERO_VECTOR);
  383.                 DS((string) i +" : " +hex(i));
  384.             }
  385.             DS((string)llGetTime());
  386.             // DS(llDumpList2String(llLinksetDataListKeys(0,0), "\n"));
  387.             llSetColor(WHITE, ALL_SIDES );
  388.         }
  389.     }
  390.  
  391.     linkset_data(integer action, string name, string value)
  392.     {
  393.         if(llLinksetDataAvailable()%2)
  394.             llSetColor(BLUE, ALL_SIDES );
  395.         else
  396.             llSetColor(RED, ALL_SIDES );
  397.         llSetText((string) llLinksetDataAvailable()
  398.         + "\n"+(string) llLinksetDataCountKeys()
  399.         + "\n"+(string) llGetFreeMemory(), FUCHSIA, TRUE);
  400.     }
  401. }
  402.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement