xs_movement.lsl 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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:10
  6. // :ID:989
  7. // :NUM:1476
  8. // :REV:1
  9. // :WORLD:Second Life, Opensim
  10. // :DESCRIPTION:
  11. // Original Pet Quail
  12. // :CODE:
  13. float GROWTH_AMOUNT = 0.10;
  14. vector home_location;
  15. float roam_distance;
  16. list food_bowls;
  17. list food_bowl_keys;
  18. list food_bowl_time;
  19. vector destination;
  20. integer sex_dest = 0;
  21. integer food_dest = 0;
  22. float tolerance = 0.15;
  23. float increment = 0.1;
  24. integer rest;
  25. integer age;
  26. float zoffset;
  27. integer sleep_last_check ;
  28. integer sound;
  29. integer sleep_disabled;
  30. face_target(vector lookat) {
  31. vector fwd = llVecNorm(lookat-llGetPos()); //I assume you know the last and current position
  32. vector lft = llVecNorm(<0,0,1>%fwd); //cross with <0,0,1> is parallel to ground
  33. rotation rot = llAxes2Rot(fwd,lft,fwd%lft);
  34. llSetRot(rot);
  35. }
  36. integer sleeping()
  37. {
  38. vector sun = llGetSunDirection();
  39. if (!sleep_disabled) {
  40. if (sun.z < 0) {
  41. if (sleep_last_check == 0) {
  42. // close eyes
  43. llMessageLinked(LINK_SET, 990, "", "");
  44. }
  45. sleep_last_check = 1;
  46. } else {
  47. if (sleep_last_check == 1) {
  48. // open eyes
  49. llMessageLinked(LINK_SET, 991, "", "");
  50. }
  51. sleep_last_check = 0;
  52. }
  53. return sleep_last_check;
  54. } else {
  55. if (sleep_last_check == 1) {
  56. llMessageLinked(LINK_SET, 991, "", "");
  57. sleep_last_check = 0;
  58. }
  59. return 0;
  60. }
  61. }
  62. default
  63. {
  64. link_message(integer sender, integer num, string str, key id)
  65. {
  66. if (num == 800) {
  67. state running;
  68. }
  69. }
  70. }
  71. state running
  72. {
  73. state_entry()
  74. {
  75. home_location = <0,0,0>;
  76. roam_distance = 0;
  77. destination = <0,0,0>;
  78. age = 0;
  79. sleep_last_check = 0;
  80. sound = 1;
  81. }
  82. timer()
  83. {
  84. if (!sleeping()) {
  85. if (sound) {
  86. llMessageLinked(LINK_SET, 1001, "", "");
  87. }
  88. sound = !sound;
  89. vector my_pos = llGetPos();
  90. if (llVecDist(<destination.x, destination.y, 0>, <my_pos.x, my_pos.y, 0>) <= tolerance || destination == <0,0,0>) {
  91. // if at food_destination send 900 msg
  92. if (food_dest > 0) {
  93. llMessageLinked(LINK_SET, 900, (string)food_dest, llList2Key(food_bowl_keys, 0));
  94. }
  95. if (sex_dest > 0) {
  96. llMessageLinked(LINK_SET, 967, "", "");
  97. }
  98. // pick a new destination
  99. sex_dest = 0;
  100. food_dest = 0;
  101. destination.x = (llFrand(roam_distance * 2) - roam_distance) + home_location.x;
  102. destination.y = (llFrand(roam_distance * 2) - roam_distance) + home_location.y;
  103. destination.z = home_location.z + zoffset;
  104. //llOwnerSay("Moving to " + (string)destination + " but first, a rest!");
  105. rest = (integer)llFrand(60.0);
  106. // face it
  107. face_target(destination);
  108. }
  109. if (rest == 0) {
  110. // move towards destination
  111. vector position = llGetPos();
  112. float dis_x = llFabs(destination.x - position.x);
  113. float dis_y = llFabs(destination.y - position.y);
  114. float angle = llAtan2(dis_y, dis_x);
  115. float inc_x = llCos(angle) * increment;
  116. float inc_y = llSin(angle) * increment;
  117. if (inc_x > increment) {
  118. llOwnerSay("BUG: X" + (string)inc_x + " > " + (string)increment);
  119. }
  120. if (inc_y > increment) {
  121. llOwnerSay("BUG: Y" + (string)inc_y + " > " + (string)increment);
  122. }
  123. if (destination.x > position.x) {
  124. position.x += inc_x;
  125. } else {
  126. position.x -= inc_x;
  127. }
  128. if (destination.y > position.y) {
  129. position.y += inc_y;
  130. } else {
  131. position.y -= inc_y;
  132. }
  133. position.z = home_location.z + zoffset;
  134. llSetPos(position);
  135. } else {
  136. rest--;
  137. }
  138. }
  139. }
  140. link_message(integer sender, integer num, string str, key id)
  141. {
  142. if (num == 903) {
  143. if (sex_dest == 0) {
  144. // move to food bowl, then send 900
  145. if (llGetListLength(food_bowl_keys) > 0) {
  146. if (roam_distance == 0 || home_location == <0,0,0>) {
  147. llOwnerSay("I'm hungry, but I'm not homed so I can not move! Attempting to use Jedi Mind Powers to teleport food to my belly.");
  148. llMessageLinked(LINK_SET, 900, str, llList2Key(food_bowl_keys, 0));
  149. } else {
  150. // find nearest food bowl
  151. integer i;
  152. destination = (vector)llList2String(food_bowls, 0);
  153. for (i=1;i<llGetListLength(food_bowls);i++) {
  154. if (llVecDist(destination, llGetPos()) > llVecDist((vector)llList2String(food_bowls, i), llGetPos())) {
  155. destination = (vector)llList2String(food_bowls, i);
  156. }
  157. }
  158. destination.z = home_location.z + zoffset;
  159. // set destination,
  160. // face it
  161. face_target(destination);
  162. food_dest = (integer)str;
  163. rest = 0;
  164. //llMessageLinked(LINK_SET, 900, str, llList2Key(food_bowl_keys, 0));
  165. }
  166. } else {
  167. llOwnerSay("I'm hungry, but I can't seem to find any food bowls at present.");
  168. }
  169. }
  170. } else
  171. if (num == 910) {
  172. list values = llParseString2List(str, ["^"], []);
  173. home_location = (vector)llList2String(values, 0);
  174. roam_distance = llList2Float(values, 1);
  175. vector current_loc = llGetPos();
  176. food_bowls = [];
  177. food_bowl_keys = [];
  178. food_bowl_time = [];
  179. destination = <0,0,0>;
  180. food_dest = 0;
  181. llSetPos(<current_loc.x, current_loc.y, home_location.z + zoffset>);
  182. llSetTimerEvent(5.0);
  183. } else
  184. if (num == 911) {
  185. if (rest < (integer)str) {
  186. rest = (integer)str;
  187. }
  188. } else
  189. if (num == 920) {
  190. food_bowls = [];
  191. food_bowl_keys = [];
  192. food_bowl_time = [];
  193. } else
  194. if (num == 921) {
  195. vector food_loc = (vector)str;
  196. if (llVecDist(home_location, food_loc) <= roam_distance && llFabs(llFabs(home_location.z) - llFabs(food_loc.z)) < 2) {
  197. if(llListFindList(food_bowls, (list)str) == -1) {
  198. integer id_pos = llListFindList(food_bowl_keys, (list)id);
  199. if (id_pos == -1) {
  200. food_bowls += str;
  201. food_bowl_keys += id;
  202. food_bowl_time += llGetUnixTime();
  203. } else {
  204. food_bowls = llListReplaceList(food_bowls, [str], id_pos, id_pos);
  205. food_bowl_time = llListReplaceList(food_bowl_time, [llGetUnixTime()], id_pos, id_pos);
  206. }
  207. }
  208. integer iter;
  209. iter = 0;
  210. while(iter<llGetListLength(food_bowls)) {
  211. if (llGetUnixTime() - llList2Integer(food_bowl_time, iter) > 3600) {//3600
  212. food_bowls = llDeleteSubList(food_bowls, iter, iter);
  213. food_bowl_keys = llDeleteSubList(food_bowl_keys, iter, iter);
  214. food_bowl_time = llDeleteSubList(food_bowl_time, iter, iter);
  215. } else {
  216. iter++;
  217. }
  218. }
  219. if (llGetListLength(food_bowls) > 0) {
  220. llMessageLinked(LINK_SET, 8000, "", "");
  221. }
  222. }
  223. } else
  224. if (num == 966) {
  225. destination = (vector)str;
  226. face_target(destination);
  227. rest = 0;
  228. food_dest = 0;
  229. sex_dest = 1;
  230. } else
  231. if (num == 940) {
  232. integer heightm;
  233. age += (integer)str;
  234. heightm = age;
  235. if (heightm > 7)
  236. heightm = 7;
  237. float new_scale = (GROWTH_AMOUNT * heightm) + 1.0;
  238. float legsz = 0.064 * new_scale;
  239. float legso = 0.052399 * new_scale;
  240. zoffset = legsz / 2 + legso;
  241. } else
  242. if (num == 7999) {
  243. sleep_disabled = (integer)str;
  244. }
  245. }
  246. }