xs_egg.lsl 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. // :CATEGORY:XS Pet
  2. // :NAME:XS Pet Xundra Snowpaw Original Quail
  3. // :AUTHOR:Xundra Snowpaw
  4. // :CREATED:2013-09-06
  5. // :EDITED:2013-09-18 15:39:11
  6. // :ID:989
  7. // :NUM:1479
  8. // :REV:1
  9. // :WORLD:Second Life, Opensim
  10. // :DESCRIPTION:
  11. // Original Pet Quail
  12. // :CODE:
  13. float VERSION = 0.22;
  14. integer API_CHANNEL = -999198;
  15. string SECRET_PASSWORD = "top secret";
  16. integer SECRET_NUMBER = 99999;
  17. key YOUR_UUID = "00000000-0000-0000-0000-000000000000";
  18. integer ANIMAL_CHANNEL = -999192;
  19. integer EGG_CHANNEL = -999193;
  20. integer BOX_CHANNEL = -999195;
  21. string SPECIAL = "Normal";
  22. //integer TIME_TO_HATCH = 7200;
  23. // DON'T CHANGE THE FOLLOWING! (Unless you know what you are doing!)
  24. integer XTEA_DELTA = 0x9E3779B9; // (sqrt(5) - 1) * 2^31
  25. integer xtea_num_rounds = 6;
  26. list xtea_key = [0, 0, 0, 0];
  27. integer hex2int(string hex) {
  28. if(llGetSubString(hex,0,1) == "0x")
  29. return (integer)hex;
  30. if(llGetSubString(hex,0,0) == "x")
  31. return (integer)("0"+hex);
  32. return(integer)("0x"+hex);
  33. }
  34. // Convers any string to a 32 char MD5 string and then to a list of
  35. // 4 * 32 bit integers = 128 bit Key. MD5 ensures always a specific
  36. // 128 bit key is generated for any string passed.
  37. list xtea_key_from_string( string str )
  38. {
  39. str = llMD5String(str,0); // Use Nonce = 0
  40. return [ hex2int(llGetSubString( str, 0, 7)),
  41. hex2int(llGetSubString( str, 8, 15)),
  42. hex2int(llGetSubString( str, 16, 23)),
  43. hex2int(llGetSubString( str, 24, 31))];
  44. }
  45. // Encipher two integers and return the result as a 12-byte string
  46. // containing two base64-encoded integers.
  47. string xtea_encipher( integer v0, integer v1 )
  48. {
  49. integer num_rounds = xtea_num_rounds;
  50. integer sum = 0;
  51. do {
  52. // LSL does not have unsigned integers, so when shifting right we
  53. // have to mask out sign-extension bits.
  54. v0 += (((v1 << 4) ^ ((v1 >> 5) & 0x07FFFFFF)) + v1) ^ (sum + llList2Integer(xtea_key, sum & 3));
  55. sum += XTEA_DELTA;
  56. v1 += (((v0 << 4) ^ ((v0 >> 5) & 0x07FFFFFF)) + v0) ^ (sum + llList2Integer(xtea_key, (sum >> 11) & 3));
  57. } while( num_rounds = ~-num_rounds );
  58. //return only first 6 chars to remove "=="'s and compact encrypted text.
  59. return llGetSubString(llIntegerToBase64(v0),0,5) +
  60. llGetSubString(llIntegerToBase64(v1),0,5);
  61. }
  62. // Decipher two base64-encoded integers and return the FIRST 30 BITS of
  63. // each as one 10-byte base64-encoded string.
  64. string xtea_decipher( integer v0, integer v1 )
  65. {
  66. integer num_rounds = xtea_num_rounds;
  67. integer sum = XTEA_DELTA*xtea_num_rounds;
  68. do {
  69. // LSL does not have unsigned integers, so when shifting right we
  70. // have to mask out sign-extension bits.
  71. v1 -= (((v0 << 4) ^ ((v0 >> 5) & 0x07FFFFFF)) + v0) ^ (sum + llList2Integer(xtea_key, (sum>>11) & 3));
  72. sum -= XTEA_DELTA;
  73. v0 -= (((v1 << 4) ^ ((v1 >> 5) & 0x07FFFFFF)) + v1) ^ (sum + llList2Integer(xtea_key, sum & 3));
  74. } while ( num_rounds = ~-num_rounds );
  75. return llGetSubString(llIntegerToBase64(v0), 0, 4) +
  76. llGetSubString(llIntegerToBase64(v1), 0, 4);
  77. }
  78. // Encrypt a full string using XTEA.
  79. string xtea_encrypt_string( string str )
  80. {
  81. // encode string
  82. str = llStringToBase64(str);
  83. // remove trailing =s so we can do our own 0 padding
  84. integer i = llSubStringIndex( str, "=" );
  85. if ( i != -1 )
  86. str = llDeleteSubString( str, i, -1 );
  87. // we don't want to process padding, so get length before adding it
  88. integer len = llStringLength(str);
  89. // zero pad
  90. str += "AAAAAAAAAA=";
  91. string result;
  92. i = 0;
  93. do {
  94. // encipher 30 (5*6) bits at a time.
  95. result += xtea_encipher(llBase64ToInteger(llGetSubString(str, i, i + 4) + "A="), llBase64ToInteger(llGetSubString(str, i+5, i + 9) + "A="));
  96. i+=10;
  97. } while ( i < len );
  98. return result;
  99. }
  100. // Decrypt a full string using XTEA
  101. string xtea_decrypt_string( string str ) {
  102. integer len = llStringLength(str);
  103. integer i=0;
  104. string result;
  105. //llOwnerSay(str);
  106. do {
  107. integer v0;
  108. integer v1;
  109. v0 = llBase64ToInteger(llGetSubString(str, i, i + 5) + "==");
  110. i+= 6;
  111. v1 = llBase64ToInteger(llGetSubString(str, i, i + 5) + "==");
  112. i+= 6;
  113. result += xtea_decipher(v0, v1);
  114. } while ( i < len );
  115. // Replace multiple trailing zeroes with a single one
  116. i = llStringLength(result) - 1;
  117. while ( llGetSubString(result, i - 1, i) == "AA" ){
  118. result = llDeleteSubString(result, i, i);
  119. i--;
  120. }
  121. i = llStringLength(result) - 1;
  122. // while (llGetSubString(result, i, i + 1) == "A" ) {
  123. // i--;
  124. // }
  125. result = llGetSubString(result, 0, i+1);
  126. i = llStringLength(result);
  127. integer mod = i%4; //Depending on encoded length diffrent appends are needed
  128. if(mod == 1) result += "A==";
  129. else if(mod == 2 ) result += "==";
  130. else if(mod == 3) result += "=";
  131. return llBase64ToString(result);
  132. }
  133. string base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  134. vector colour1;
  135. vector colour2;
  136. integer sex;
  137. integer gen;
  138. integer shine;
  139. float glow;
  140. integer api_listener;
  141. integer locked;
  142. integer born_hatched;
  143. integer owner_touch;
  144. integer menu_listener;
  145. set_colours(vector c1, vector c2, integer shine, float glow)
  146. {
  147. llSetPrimitiveParams([PRIM_COLOR, ALL_SIDES, c2, 1.0, PRIM_BUMP_SHINY, ALL_SIDES, shine, PRIM_BUMP_NONE, PRIM_GLOW, ALL_SIDES, glow]);
  148. llSetLinkPrimitiveParams(2, [PRIM_COLOR, ALL_SIDES, c1, 1.0, PRIM_BUMP_SHINY, ALL_SIDES, shine, PRIM_BUMP_NONE, PRIM_GLOW, ALL_SIDES, glow]);
  149. }
  150. say_details()
  151. {
  152. string myshine;
  153. if (shine == 0) {
  154. myshine = "None";
  155. } else
  156. if (shine == 1) {
  157. myshine = "Low";
  158. } else
  159. if (shine == 2) {
  160. myshine = "Medium";
  161. } else
  162. if (shine == 3) {
  163. myshine = "High";
  164. }
  165. llWhisper(0, "Head/Body Colour: " + (string)colour1);
  166. llWhisper(0, "Tail/Wing Colour: " + (string)colour2);
  167. llWhisper(0, "Shine: " + myshine);
  168. llWhisper(0, "Glow: " + (string)((integer)(glow * 100)) + "%");
  169. llWhisper(0, "Special: " + SPECIAL);
  170. llWhisper(0, "Generation: " + (string)gen);
  171. llSay(API_CHANNEL, "XSQuail^" + (string)llGetKey() + "^" + (string)colour1 + "^" + (string)colour2 + "^" + (string)shine + "^" + (string)glow + "^" + SPECIAL + "^" + (string)gen + "^XSQuailEnd");
  172. }
  173. default
  174. {
  175. state_entry()
  176. {
  177. llSetText("", <1,1,1>, 1.0);
  178. llSetColor(<1,1,1>, ALL_SIDES);
  179. llSetLinkColor(2, <1,1,1>, ALL_SIDES);
  180. llParticleSystem([]);
  181. xtea_key = xtea_key_from_string(SECRET_PASSWORD);
  182. }
  183. changed(integer change) {
  184. if (change & CHANGED_ALLOWED_DROP) {
  185. if (llGetInventoryType("Quail") != INVENTORY_NONE && llGetInventoryType("XS Egg") != INVENTORY_NONE) {
  186. llAllowInventoryDrop(FALSE);
  187. }
  188. }
  189. }
  190. on_rez(integer param)
  191. {
  192. llSetText("Rezzing...", <1,0,0>, 1.0);
  193. llSleep(5.0);
  194. if (param == 0) {
  195. if (llGetOwner() == YOUR_UUID) {
  196. integer colour = (integer)llFrand(9.0) + 1;
  197. if (colour == 1) {
  198. colour1 = <1,0,0>;
  199. colour2 = <1,0,0>;
  200. } else
  201. if (colour == 2) {
  202. colour1 = <1,0,0>;
  203. colour2 = <0,1,0>;
  204. } else
  205. if (colour == 3) {
  206. colour1 = <1,0,0>;
  207. colour2 = <0,0,1>;
  208. } else
  209. if (colour == 4) {
  210. colour1 = <0,1,0>;
  211. colour2 = <1,0,0>;
  212. } else
  213. if (colour == 5) {
  214. colour1 = <0,1,0>;
  215. colour2 = <0,1,0>;
  216. } else
  217. if (colour == 6) {
  218. colour1 = <0,1,0>;
  219. colour2 = <0,0,1>;
  220. } else
  221. if (colour == 7) {
  222. colour1 = <0,0,1>;
  223. colour2 = <1,0,0>;
  224. } else
  225. if (colour == 8) {
  226. colour1 = <0,0,1>;
  227. colour2 = <0,1,0>;
  228. } else
  229. if (colour == 9) {
  230. colour1 = <0,0,1>;
  231. colour2 = <0,0,1>;
  232. }
  233. set_colours(colour1, colour2, shine, glow);
  234. state rezzed;
  235. } else {
  236. llWhisper(0, "Oh no! This egg was broken in your inventory! Please use an egg-cup next time.");
  237. state dead;
  238. }
  239. } else
  240. if (param == SECRET_NUMBER) {
  241. menu_listener = llListen(EGG_CHANNEL, "", "", "");
  242. if (llGetOwner() != YOUR_UUID) {
  243. llAllowInventoryDrop(TRUE);
  244. }
  245. llSay(ANIMAL_CHANNEL, xtea_encrypt_string("XSPET^EGG_READY^" + (string)llGetKey() + "^SMILE"));
  246. llSay(BOX_CHANNEL, xtea_encrypt_string("XSPET^" + (string)llGetKey() + "^EGG_READY^SMILE"));
  247. }
  248. }
  249. listen(integer channel, string sender, key id, string msg) {
  250. list data = llParseString2List(xtea_decrypt_string(msg), ["^"], []);
  251. if (llList2String(data, 0) == "XSPET" && llList2Key(data, 1) == llGetKey()) {
  252. if (llList2String(data, 2) == "UNBOX") {
  253. colour1 = (vector)llList2String(data, 3);
  254. colour2 = (vector)llList2String(data, 4);
  255. gen = llList2Integer(data,5);
  256. shine = llList2Integer(data, 6);
  257. glow = llList2Float(data, 7);
  258. SPECIAL = llList2String(data, 8);
  259. llListenRemove(menu_listener);
  260. set_colours(colour1, colour2, shine, glow);
  261. state rezzed;
  262. } else
  263. if (llList2String(data, 2) == "BORN") {
  264. vector fcolour1 = (vector)llList2String(data, 3);
  265. vector fcolour2 = (vector)llList2String(data, 4);
  266. integer fgen = llList2Integer(data,5);
  267. integer fshine = llList2Integer(data, 6);
  268. float fglow = llList2Float(data, 7);
  269. integer fglow_gene = llList2Integer(data, 8);
  270. vector mcolour1 = (vector)llList2String(data, 9);
  271. vector mcolour2 = (vector)llList2String(data, 10);
  272. integer mgen = llList2Integer(data, 11);
  273. integer mshine = llList2Integer(data, 12);
  274. float mglow = llList2Float(data, 13);
  275. integer mglow_gene = llList2Integer(data, 14);
  276. integer mix = (integer)llFrand(2) + 1;
  277. float deviation1 = (llFrand(10) - 5) / 100;
  278. float deviation2 = (llFrand(10) - 5) / 100;
  279. if (mix == 1) {
  280. colour1.x = (fcolour1.x / 2 + mcolour1.x / 2) + deviation1;
  281. colour1.y = (fcolour1.y / 2 + mcolour1.y / 2) + deviation1;
  282. colour1.z = (fcolour1.z / 2 + mcolour1.z / 2) + deviation1;
  283. colour2.x = (fcolour2.x / 2 + mcolour2.x / 2) + deviation2;
  284. colour2.y = (fcolour2.y / 2 + mcolour2.y / 2) + deviation2;
  285. colour2.z = (fcolour2.z / 2 + mcolour2.z / 2) + deviation2;
  286. } else {
  287. colour1.x = (fcolour1.x / 2 + mcolour2.x / 2) + deviation1;
  288. colour1.y = (fcolour1.y / 2 + mcolour2.y / 2) + deviation1;
  289. colour1.z = (fcolour1.z / 2 + mcolour2.z / 2) + deviation1;
  290. colour2.x = (fcolour2.x / 2 + mcolour1.x / 2) + deviation2;
  291. colour2.y = (fcolour2.y / 2 + mcolour1.y / 2) + deviation2;
  292. colour2.z = (fcolour2.z / 2 + mcolour1.z / 2) + deviation2;
  293. }
  294. if (colour1.x < 0)
  295. colour1.x = 0;
  296. if (colour1.x > 1)
  297. colour1.x = 1;
  298. if (colour1.y < 0)
  299. colour1.y = 0;
  300. if (colour1.y > 1)
  301. colour1.y = 1;
  302. if (colour1.z < 0)
  303. colour1.z = 0;
  304. if (colour1.z > 1)
  305. colour1.z = 1;
  306. if (colour2.x < 0)
  307. colour2.x = 0;
  308. if (colour2.x > 1)
  309. colour2.x = 1;
  310. if (colour2.y < 0)
  311. colour2.y = 0;
  312. if (colour2.y > 1)
  313. colour2.y = 1;
  314. if (colour2.z < 0)
  315. colour2.z = 0;
  316. if (colour2.z > 1)
  317. colour2.z = 1;
  318. if (mgen > fgen) {
  319. gen = mgen + 1;
  320. } else {
  321. gen = fgen + 1;
  322. }
  323. // TODO: shine / glow
  324. shine = 0;
  325. if (gen > 2) {
  326. integer pos = (integer)llFrand(100) + 1;
  327. if (fshine == mshine && fshine > 0) {
  328. if (pos < 25) {
  329. shine = fshine + 1;
  330. if (shine > 3) {
  331. shine = 3;
  332. }
  333. }
  334. } else {
  335. if (pos < 10) {
  336. // increase shine
  337. if (mshine > fshine) {
  338. shine = mshine + 1;
  339. } else
  340. if (fshine > mshine) {
  341. shine = fshine + 1;
  342. } else {
  343. shine = 1;
  344. }
  345. if (shine > 3) {
  346. shine = 3;
  347. }
  348. }
  349. }
  350. }
  351. glow = 0.0;
  352. if (mglow <= 0.3 && fglow <= 0.3) {
  353. if (mglow > fglow) {
  354. glow = mglow - 0.1;
  355. } else if (fglow > mglow) {
  356. glow = fglow - 0.1;
  357. }
  358. }
  359. if (fglow_gene >= 10 && mglow_gene >= 10) {
  360. integer poss = (integer)llFrand(100) + 1;
  361. if (poss < 25) {
  362. glow = 0.3;
  363. }
  364. } else if (fglow_gene >= 10 || mglow_gene >= 10) {
  365. integer poss = (integer)llFrand(100) + 1;
  366. if (poss < 10) {
  367. glow = 0.3;
  368. }
  369. }
  370. llListenRemove(menu_listener);
  371. born_hatched = 1;
  372. set_colours(colour1, colour2, shine, glow);
  373. llSetTimerEvent(1.0);
  374. }
  375. }
  376. }
  377. timer() {
  378. if (llGetInventoryType("Quail") != INVENTORY_NONE && llGetInventoryType("XS Egg") != INVENTORY_NONE) {
  379. state rezzed;
  380. }
  381. }
  382. }
  383. state rezzed
  384. {
  385. on_rez(integer param)
  386. {
  387. if (llGetOwner() != YOUR_UUID) {
  388. llWhisper(0, "Oh no! This egg was broken in your inventory! Please use an egg-cup next time.");
  389. state dead;
  390. }
  391. }
  392. state_entry()
  393. {
  394. if (SPECIAL == "Normal") {
  395. llSetText("", <1,1,1>, 1.0);
  396. } else {
  397. llSetText(SPECIAL, <1,1,1>, 1.0);
  398. }
  399. api_listener = llListen(API_CHANNEL, "", "", "");
  400. }
  401. touch_start(integer num)
  402. {
  403. owner_touch = 0;
  404. say_details();
  405. if (llDetectedKey(0) == llGetOwner()) {
  406. owner_touch = 1;
  407. integer chan = (integer)llFrand(100000.0) + 1000;
  408. menu_listener = llListen(chan, "", llGetOwner(), "");
  409. llDialog(llGetOwner(), "I'm an egg. What would you like to do with me?", ["Hatch", "Package", "Nothing"], chan);
  410. }
  411. llSetTimerEvent(30.0);
  412. }
  413. timer()
  414. {
  415. llSetTimerEvent(0);
  416. if (owner_touch == 1) {
  417. llOwnerSay("Menu timeout.");
  418. llListenRemove(menu_listener);
  419. owner_touch = 0;
  420. }
  421. }
  422. listen(integer channel, string sender, key id, string msg)
  423. {
  424. if (channel == API_CHANNEL) {
  425. list data = llParseString2List(msg, ["^"], []);
  426. if (llList2String(data, 0) == "XSQuail" && llList2Key(data, 1) == llGetKey()) {
  427. say_details();
  428. }
  429. } else {
  430. if (id == llGetOwner()) {
  431. llSetTimerEvent(0);
  432. llListenRemove(menu_listener);
  433. if (msg == "Hatch") {
  434. state hatching;
  435. } else
  436. if (msg == "Package") {
  437. state package;
  438. }
  439. }
  440. }
  441. }
  442. state_exit() {
  443. llListenRemove(api_listener);
  444. }
  445. }
  446. state hatching
  447. {
  448. state_entry()
  449. {
  450. llRezObject("Quail", llGetPos() + <0.0, 0.0, 0.25>, ZERO_VECTOR, ZERO_ROTATION, SECRET_NUMBER);
  451. }
  452. object_rez(key id)
  453. {
  454. menu_listener = llListen(EGG_CHANNEL, "", id, "");
  455. }
  456. listen(integer channel, string sender, key id, string msg)
  457. {
  458. list decrypted = llParseString2List(xtea_decrypt_string(msg), ["^"], []);
  459. if (llList2String(decrypted, 0) == "XSPET" && llList2String(decrypted, 1) == "READY" && llList2String(decrypted, 2) == (string)id) {
  460. llGiveInventory(id, "Quail");
  461. llGiveInventory(id, "XS Egg");
  462. sex = (integer)llFrand(2) + 1;
  463. llSay(ANIMAL_CHANNEL, xtea_encrypt_string("XSPET^CONFIG^" + (string)id + "^" + (string)colour1 + "^" + (string)colour2 + "^" + (string)sex + "^" + (string)shine + "^" + (string)glow + "^" + (string)gen + "^0^0^Quail^0^" + SPECIAL));
  464. llDie();
  465. }
  466. }
  467. }
  468. state package
  469. {
  470. state_entry() {
  471. llSetTimerEvent(20.0);
  472. llWhisper(0, "Looking for an egg cup...");
  473. menu_listener = llListen(EGG_CHANNEL, "", "", "");
  474. llSay(BOX_CHANNEL, xtea_encrypt_string("XSPET^" + (string)llGetKey() + "^CUP_PING^" + (string)VERSION));
  475. }
  476. timer()
  477. {
  478. llListenRemove(menu_listener);
  479. llSetTimerEvent(0);
  480. llWhisper(0, "No egg cup found.");
  481. state rezzed;
  482. }
  483. listen(integer channel, string sender, key id, string msg)
  484. {
  485. list data = llParseString2List(xtea_decrypt_string(msg), ["^"], []);
  486. if (llList2String(data, 0) == "XSPET" && llList2Key(data, 1) == llGetKey() && llList2String(data, 2) == "CUP_PONG" && llList2Float(data, 3) >= VERSION && locked == 0) {
  487. llSay(BOX_CHANNEL, xtea_encrypt_string("XSPET^" + (string)id + "^" + (string)colour1 + "^" + (string)colour2 + "^" + (string)shine + "^" + (string)glow + "^" + (string)gen + "^" + SPECIAL));
  488. locked = 1;
  489. } else
  490. if (llList2String(data, 0) == "XSPET" && llList2Key(data, 1) == llGetKey() && llList2String(data, 2) == "CUP_DIE") {
  491. llDie();
  492. }
  493. }
  494. }
  495. state dead
  496. {
  497. state_entry()
  498. {
  499. llSetText("Broken egg.", <1,0,0>, 1.0);
  500. }
  501. }