vehicle script.lsl 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // :SHOW:1
  2. // :CATEGORY:Tour
  3. // :NAME:OpenSim Tour Car
  4. // :AUTHOR:Ferd Frederix
  5. // :KEYWORDS:
  6. // :CREATED:2014-09-07 10:50:19
  7. // :EDITED:2015-05-29 11:26:03
  8. // :ID:1040
  9. // :NUM:1626
  10. // :REV:2
  11. // :WORLD:Opensim or Secondlife
  12. // :DESCRIPTION:
  13. // Tour car prim for Opensim
  14. // :CODE:
  15. integer _debug = 0;
  16. integer REZ = 3454; // shouts Rex on this channel so a rezzer can produce another car, as this one will die
  17. float INTERVAL = 0.5; // How often to check for progress
  18. float TOLERANCE = 2; // how close to the destination we have to get
  19. float SPEED = 5.0; // speed of vehicle
  20. integer DieAtEnd = FALSE; // set to FALSE for endless loop, ser to TRUE to poof
  21. vector TARGET = <0.0, -0.6,0.6>; // sit position on the root prim. Set this to <0,0,0> if you want to not to be able to sit on the base
  22. vector ROT = <0, 0, 180>; // and the rotation, if they sit
  23. list lCoordinate;
  24. list lRotation ;
  25. list lText;
  26. // notecard reading
  27. integer iIndexLines;
  28. integer i;
  29. string NOTECARD = "Route"; // the notecard for configuring
  30. key kNoteCardLines; // the key of the notecard
  31. key kGetIndexLines; // the key of the current line
  32. integer count = 0;
  33. vector startPos;
  34. rotation startRot;
  35. vector TargetLocation;
  36. rotation TargetRotation;
  37. Goto()
  38. {
  39. if (_debug)
  40. llOwnerSay("Looking at Target Location = " + (string) TargetLocation);
  41. vector ownPosition = llGetPos();
  42. float dist = llVecDist(ownPosition, TargetLocation);
  43. float rate = dist / SPEED;
  44. rotation ownRotation = llGetRot();
  45. llSetKeyframedMotion(
  46. [(TargetLocation - ownPosition) + <0.0, 0.0, -.5> * TargetRotation,
  47. NormRot(TargetRotation/ownRotation), rate],
  48. []);
  49. }
  50. rotation NormRot(rotation Q)
  51. {
  52. float MagQ = llSqrt(Q.x*Q.x + Q.y*Q.y +Q.z*Q.z + Q.s*Q.s);
  53. return
  54. <Q.x/MagQ, Q.y/MagQ, Q.z/MagQ, Q.s/MagQ>;
  55. }
  56. integer locationLength;
  57. string strip( string str)
  58. {
  59. string out;
  60. integer i;
  61. //llOwnerSay("str = " + str);
  62. for ( ; i < llStringLength(str); i++)
  63. {
  64. out += llGetSubString(str,i,i);
  65. out = llStringTrim(out, STRING_TRIM);
  66. //llOwnerSay("out = " + out + " at " + (string) i);
  67. }
  68. return out;
  69. }
  70. string Getline(list Input, integer line)
  71. {
  72. return strip(llList2String(Input, line));
  73. }
  74. default
  75. {
  76. on_rez(integer param)
  77. {
  78. llResetScript();
  79. }
  80. changed( integer change )
  81. {
  82. if (change & CHANGED_INVENTORY)
  83. {
  84. llResetScript();
  85. }
  86. }
  87. state_entry()
  88. {
  89. llSetLinkPrimitiveParamsFast(LINK_ROOT,
  90. [PRIM_PHYSICS_SHAPE_TYPE, PRIM_PHYSICS_SHAPE_CONVEX,
  91. PRIM_LINK_TARGET, LINK_ALL_CHILDREN,
  92. PRIM_PHYSICS_SHAPE_TYPE, PRIM_PHYSICS_SHAPE_NONE]);
  93. llSetCameraEyeOffset(<2, 5, 2> );
  94. llSetCameraAtOffset(<0, 0, 1>);
  95. rotation rot = llEuler2Rot(ROT * DEG_TO_RAD); // convert the degrees to radians, then convert that vector into a rotation, rot30x
  96. llSitTarget(TARGET, rot); // where they sit
  97. kNoteCardLines = llGetNumberOfNotecardLines(NOTECARD);
  98. kGetIndexLines = llGetNotecardLine(NOTECARD,0);
  99. llSay (0, "Tour guide initialising. Please wait.");
  100. }
  101. // read notecard on bootup
  102. dataserver(key queryid, string data)
  103. {
  104. vector TempLocation;
  105. if (queryid == kNoteCardLines)
  106. {
  107. iIndexLines = (integer) data;
  108. }
  109. if (queryid == kGetIndexLines)
  110. {
  111. if (data != EOF)
  112. {
  113. queryid = llGetNotecardLine(NOTECARD, i);
  114. list lLine = (llParseString2List(data, ["|"], []));
  115. if (_debug ) llOwnerSay("Line = " + llDumpList2String(lLine,":"));
  116. TempLocation = (vector) Getline(lLine,1);
  117. if (TempLocation != ZERO_VECTOR)
  118. {
  119. rotation Rot = (rotation) llList2String(lLine,2);
  120. string text = llList2String(lLine,3);
  121. if (_debug) llOwnerSay((string)TempLocation);
  122. lCoordinate += [TempLocation];
  123. lRotation += [Rot];
  124. lText += [text];
  125. locationLength = (llGetListLength(lCoordinate));
  126. integer InitPerCent = (integer) llRound(((float) locationLength / (float) iIndexLines) * 100);
  127. llSetText("Initialising... \n" + (string) InitPerCent + "%" , <1,1,1>, 1.0);
  128. if (InitPerCent == 100)
  129. {
  130. state Paused;
  131. }
  132. }
  133. else
  134. {
  135. state Paused;
  136. }
  137. i++;
  138. }
  139. kGetIndexLines = llGetNotecardLine(NOTECARD,i);
  140. // (_debug ) llOwnerSay("Got " + (string) i);
  141. }
  142. }
  143. state_exit()
  144. {
  145. llSetText("", <1,1,1>, 1.0);
  146. TargetLocation = llList2Vector(lCoordinate, 0); // Look at 0th
  147. TargetRotation = llList2Rot(lRotation, 0); // Look at 0th
  148. startPos = TargetLocation;
  149. startRot= TargetRotation;
  150. llSetRegionPos(TargetLocation);
  151. llSetRot(TargetRotation);
  152. }
  153. }
  154. state Paused
  155. {
  156. state_entry()
  157. {
  158. llSay(0,"Ready");
  159. }
  160. link_message(integer sender,integer num,string msg, key id)
  161. {
  162. if (msg =="sit")
  163. {
  164. llSay(0,"Please stay seated. Waiting 10 seconds for a passenger");
  165. llSleep(10.0);
  166. llShout(REZ,"rez");
  167. state moving;
  168. }
  169. }
  170. on_rez(integer param)
  171. {
  172. llResetScript();
  173. }
  174. changed( integer change )
  175. {
  176. if (change & CHANGED_INVENTORY)
  177. {
  178. llResetScript();
  179. }
  180. if (change & CHANGED_LINK)
  181. {
  182. key av = llAvatarOnSitTarget();
  183. //llWhisper(0,"Sit by " + (string) av);
  184. if (av) //evaluated as true if not NULL_KEY or invalid
  185. {
  186. llSay(0,"Please stay seated. Waiting 10 seconds for a passenger");
  187. llSleep(10.0);
  188. llShout(REZ,"rez");
  189. state moving;
  190. } else {
  191. state end;
  192. }
  193. }
  194. }
  195. }
  196. state moving
  197. {
  198. state_entry()
  199. {
  200. if (_debug) llOwnerSay("State Moving entered, is pointing to target " + (string) TargetLocation );
  201. string SpeakThis = llList2String(lText, count);
  202. if (llStringLength(SpeakThis))
  203. llSay(0,SpeakThis);
  204. llSetTimerEvent(INTERVAL);
  205. }
  206. changed(integer change)
  207. {
  208. if (change & CHANGED_LINK)
  209. {
  210. key av = llAvatarOnSitTarget();
  211. //llWhisper(0,"Sit by " + (string) av);
  212. if (av) //evaluated as true if not NULL_KEY or invalid
  213. {
  214. //tate moving;
  215. }
  216. else
  217. {
  218. state end;
  219. }
  220. }
  221. if (change & CHANGED_INVENTORY)
  222. {
  223. llResetScript();
  224. }
  225. }
  226. on_rez(integer param)
  227. {
  228. llResetScript();
  229. }
  230. timer()
  231. {
  232. if (llVecMag(llGetPos() - TargetLocation) <= TOLERANCE) {
  233. if (_debug) llOwnerSay("At location: " + (string) llGetPos());
  234. count ++;
  235. if (count >= locationLength) {
  236. state end;
  237. } else {
  238. TargetLocation = llList2Vector(lCoordinate, count); // Look at nth
  239. TargetRotation = llList2Rot(lRotation, count); // Look at nth
  240. string SpeakThis = llList2String(lText, count);
  241. if (llStringLength(SpeakThis))
  242. llSay(0,SpeakThis);
  243. Goto();
  244. }
  245. }
  246. }
  247. state_exit()
  248. {
  249. llSetTimerEvent(0);
  250. }
  251. }
  252. state end
  253. {
  254. state_entry()
  255. {
  256. llMessageLinked(LINK_SET,0,"DOOR OPEN","");
  257. llSetTimerEvent(0);
  258. key av = llAvatarOnSitTarget();
  259. if (av) {//evaluated as true if not NULL_KEY or invalid
  260. llWhisper(0, llKey2Name(av) +", please go into the hotel and check in.");
  261. llUnSit(av);
  262. }
  263. llMessageLinked(LINK_SET,0,"unsit","");
  264. llSetTimerEvent(5);
  265. }
  266. on_rez(integer param)
  267. {
  268. llResetScript();
  269. }
  270. timer()
  271. {
  272. llSetTimerEvent(0);
  273. state Paused;
  274. }
  275. state_exit()
  276. {
  277. TargetLocation = startPos;
  278. TargetRotation = startRot;
  279. llSetRegionPos(startPos);
  280. llSetRot(TargetRotation);
  281. count = 0;
  282. }
  283. }