NPC Recorder Script v 4.5.lsl 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
  1. // :SHOW:1
  2. // :CATEGORY:NPC
  3. // :NAME:All In One NPC Recorder and Player
  4. // :AUTHOR:Ferd Frederix
  5. // :KEYWORDS:NPC, Puppeteer
  6. // :ID:27
  7. // :REV: 4.5
  8. // :WORLD:OpenSim
  9. // :DESCRIPTION:
  10. // All in one NPC recorder player.
  11. // Supports both absolute and relative paths and many new commands
  12. // Add animations named "Fly, Walk, Stand and Run"
  13. // Click Prim to use.
  14. // Should be worn as a HUD to record.
  15. // Put it on the ground and click Sensor or Start NPC when done.
  16. // :CODE:
  17. // This is Rev 4.5 09/26/2015
  18. // Revision History
  19. // Rev 1.1 10-2-2014 @Sit did not work. Minor tweaks to casting for lslEditor
  20. // Rev 1.2 10-14-2014 @ sit had wrong type.
  21. // Rev 1.3 relative movement fixed for @fly
  22. // Rev 1.4 4-3-2014 allow anyone to use this, non owners and non group members can only start and stop.
  23. // Rev 1.5 5-17-2014 set sensor to auto start on reboot of sim
  24. // Rev 1.6 5-24-2014 move menu so you can get it by touching, removed many of the KeyValues to RAM for efficiency
  25. // Rev 1.7 CHANGED_REGION_START, not CHANGED_REGION_START (Opensim difference)
  26. // Rev 1.8 tuned up Kill NPC, added more flexible upgrader
  27. // Rev 1.9 Better script injection by link message// Rev 2.0 Added osSetSpeed so you can speed up or slow down an NPC.
  28. // Rev 2.1 No laggy sensor used exept to sit on stuff
  29. // Rev 2.2 Various sensor fixes
  30. // Rev 2.3 Sets No Sensor in menu, must be started by hand
  31. // Rev 2.4 - reserved for patches to 2.3 if needed
  32. // Rev 3.0 Refactor out into subs, not states to make command injection easier
  33. // New command: @appearance=Notecardname so you can switch to a new notecard on the fly
  34. // New command: @speed=1.0 which slows up ( < 1 ) or speeds up ( > 1)
  35. // Rev 3.1 Commands are not interruptible by Link Message
  36. // Rev 3.2 Sensor patches for consistency in removing the NPC
  37. // Rev 3.3 Added Touch command by Neo.Cortex@hbase42/hopto/org:8002
  38. // Added Menu 3 for notecard and appearance commands
  39. // Rev 3.4 animation timer cannot be zero or it shuts off timer tweaked
  40. // solves the NPC starting up when no sensor is set.
  41. // Rev 3.5 fixes saving to !Path notecard
  42. // Rev 3.6 08-11-2015 @delete acts like @stop. TYjhe NPC now rezzes after an @go back in where it was deleted
  43. // Rev 3.7 08-11-2015 @attach command added to load an attachment from the inventory to the NPC
  44. // Rev 3.8 08-17-2015 process queued commands one at a time without calling ProcessNPCLine on link message
  45. // Rev 3.9 08-23-2011 Queued command fixes including @delete which were not always working
  46. // Rev 4.0 09-15-2015 Fixes for Sensor functions which continually rezzed a NPC when no one was around.
  47. // Rev 4.1 09-20-2015 Added a Listener so link messages are not needed
  48. // Rev 4.2 09-23-2015 Added @teleport=<vector>
  49. // Rev 4.3 09-24-2015 Added @reset to restart the NPC at the very start of the !Path notecard
  50. // @teleport works for relative and absolute modes
  51. // Rev 4.4 09-26-2015 if it could not find the (deleted) NPC, it could not restart
  52. // Rev 4.5 09-29-2015 remove wait for STATE == 0
  53. //*******************************************************************//
  54. // Instructions on how to use this are at http://www.outworldz.com/opensim/posts/NPC/
  55. // This is an OpenSim-only script.
  56. // Author: Ferd Frederix aka Fred Beckhusen - fred@mitsi.com
  57. ////////////////////////////////////////////////////////////////////////////////////////////
  58. // Original code was Copyright (C) 2013 Wizardry and Steamworks - License: GNU GPLv3 //
  59. ///////////////////////////////////////////////////////////////////////////////////////////
  60. // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
  61. // rights of fair usage, the disclaimer and warranty conditions. //
  62. ///////////////////////////////////////////////////////////////////////////////////////////
  63. // The original NPC controller was from http://was.fm/opensim:npc
  64. // Extensive additions and bug fixes by Fred Beckhusen, aka Ferd Frederix
  65. // llSensor had two params swapped
  66. // @Wander would wander where it had rezzed, not where it was.
  67. // There was no 'no_sensor' event in sit, so if a @sit failed, the NPC got stuck
  68. // The animation and walks always stopped old, then started new. It should be start new, then stop old so the default stand would be suppressed.
  69. // New code:
  70. // Merged with new Route recorder and notecard writer
  71. // If the NPC failed to reach a destination it never moved on.
  72. // Added WAIT global to tune this
  73. // Exposed many tunable variables and ported the code
  74. // Added floating point to times in notecard.
  75. // Added @sound, @randsound, @whisper, @shout, and @cmd controls.
  76. // notecards integers are not floats for better control
  77. //
  78. // Link Messages may be used to perform external control by injecting @commands into the stream of actions
  79. // Example:
  80. // To chat something, such as with a chat robot
  81. // llMessageLinked(LINK_SET,0,"@npc_say=Hello","");
  82. // This script assumes that NPCs and OSSl scripting is enabled in the OpenSim configuration.
  83. // In order to enable them, the following changes must be made in the OpenSim.ini configuration file:
  84. //
  85. // ; Turn on OSSL
  86. // AllowOSFunctions = true
  87. // OSFunctionThreatLevel = Severe
  88. //[NPC]
  89. // ;# {Enabled} {} {Enable Non Player Character (NPC) facilities} {true false}
  90. // Enabled = true
  91. //
  92. // and then the server has to be restarted.
  93. // please note that there are better ways to enable NPC in the latest Opensim.
  94. // Commands: All commands begin with an @ sign. All other lines are ignored
  95. // @commands may have optional parameters. The syntax is always:
  96. // @cmd=parm1|parm2
  97. // NaN in the table below meand Not a Number. This means there is no parameter
  98. //Command First Parameter Second Parameter Description
  99. //@spawn name location (vector) Rezzes an NPC with name at a location.
  100. //@appearance NoteCardName NaN switch the NPC appearance to a new notecard
  101. //@walk destination (vector) NaN Makes the NPC walk to destination.
  102. //@fly destination (vector) NaN Makes the NPC fly to destination.
  103. //@land destination (vector) NaN Makes the NPC land at destination.
  104. //@say string NaN Makes the NPC speak a phrase.
  105. //@whisper string NaN Makes the NPC whisper a phrase.
  106. //@shout string NaN Makes the NPC shout a phrase.
  107. //@pause seconds (float) NaN Makes the NPC wait for a multiple of seconds.
  108. //@wander radius (float) cycles (integer) Makes the NPC wander in radius, for cycles seconds.
  109. //@delete NaN NaN Removes the NPC. Requires a link message to continue
  110. //@goto label (string) NaN Jump to the label label in the script.
  111. //@animate animation (string) time (float) Makes the NPC trigger the animation animation for time seconds.
  112. //@sound sound_name NaN plays a sound from inventory
  113. //@randsound NaN NaN Plays a random sound from inventory
  114. //@rotate degrees (float) NaN Rotate the NPC degrees around the Z axis.
  115. //@sit primitive name NaN Sit on a primitive with a given name.
  116. //@touch primitive name NaN Touch on a primitive with a given name.
  117. //@stand NaN NaN If sitting on a primitive, stand up.
  118. //@cmd channel (integer) string Says string on channel, for controlling external gadgets
  119. //@stop NaN NaN Halts the NPC script indefinitely. Can be started with a link message
  120. //@go NaN NaN Continues on next notecard line, for use in link messages
  121. //@speed speed (float) NaN from 0 to N, where 1.0 ius a normal speed of an avatar. 0.2 is a turtle.
  122. //@notecard notename (string) NaN load a new Path notecard
  123. //@attach InventoryName attachmentPoint load an attachment from the inventory to the NPC onto point
  124. //@teleport destination (vector) NaN Makes the NPC teleport to destination in the same sim. They cannot tp to another sim or across the HG
  125. //@reset NaN NaN Deletes the NPC, starts the !Path notecard over.
  126. // Constant attachmentPoint Comment
  127. // ATTACH_CHEST 1 chest/sternum
  128. // ATTACH_HEAD 2 head
  129. // ATTACH_LSHOULDER 3 left shoulder
  130. // ATTACH_RSHOULDER 4 right shoulder
  131. // ATTACH_LHAND 5 left hand
  132. // ATTACH_RHAND 6 right hand
  133. // ATTACH_LFOOT 7 left foot
  134. // ATTACH_RFOOT 8 right foot
  135. // ATTACH_BACK 9 back
  136. // ATTACH_PELVIS 10 pelvis
  137. // ATTACH_MOUTH 11 mouth
  138. // ATTACH_CHIN 12 chin
  139. // ATTACH_LEAR 13 left ear
  140. // ATTACH_REAR 14 right ear
  141. // ATTACH_LEYE 15 left eye
  142. // ATTACH_REYE 16 right eye
  143. // ATTACH_NOSE 17 nose
  144. // ATTACH_RUARM 18 right upper arm
  145. // ATTACH_RLARM 19 right lower arm
  146. // ATTACH_LUARM 20 left upper arm
  147. // ATTACH_LLARM 21 left lower arm
  148. // ATTACH_RHIP 22 right hip
  149. // ATTACH_RULEG 23 right upper leg
  150. // ATTACH_RLLEG 24 right lower leg
  151. // ATTACH_LHIP 25 left hip
  152. // ATTACH_LULEG 26 left upper leg
  153. // ATTACH_LLLEG 27 left lower leg
  154. // ATTACH_BELLY 28 belly/stomach/tummy
  155. // ATTACH_LEFT_PEC 29 left pectoral
  156. // ATTACH_RIGHT_PEC 30 right pectoral
  157. // ATTACH_HUD_CENTER_2 31 HUD Center 2
  158. // ATTACH_HUD_TOP_RIGHT 32 HUD Top Right
  159. // ATTACH_HUD_TOP_CENTER 33 HUD Top
  160. // ATTACH_HUD_TOP_LEFT 34 HUD Top Left
  161. // ATTACH_HUD_CENTER_1 35 HUD Center
  162. // ATTACH_HUD_BOTTOM_LEFT 36 HUD Bottom Left
  163. // ATTACH_HUD_BOTTOM 37 HUD Bottom
  164. // ATTACH_HUD_BOTTOM_RIGHT 38 HUD Bottom Right
  165. // ATTACH_NECK 39 neck
  166. // ATTACH_AVATAR_CENTER 40 avatar center/root
  167. //////////////////////////////////////////////////////////
  168. // DEBUG //
  169. //////////////////////////////////////////////////////////
  170. integer debug = TRUE; // set to TRUE or FALSE for debug chat on various actions
  171. integer LSLEditor = TRUE; // set to to TRUE to working in LSLEditor, FALSE for in-world.
  172. // you must also include the NPC commands found in the other script since LSLEditor does not support OpenSim
  173. integer iTitleText = FALSE; // set to TRUE to see debug info in text above the controller
  174. //////////////////////////////////////////////////////////
  175. // TUNABLE CONFIGURATION //
  176. //////////////////////////////////////////////////////////
  177. integer allowListener = TRUE; // set to TRUE to anable a command listener. Usually, this is setto FALSE
  178. integer link_Channel = 4223; // some random number you want to talk to this gadget on. Best if large and negative
  179. float TIMER = 2; // faster = less jerky stopping. How often the system checks the distance traveled. Fastest you can go is 0.5 seconds
  180. float QUICK = 1; // when we need to move to the next state, we use a QUICK timer
  181. string Appearance = "!Appearance"; // The name of the recorded Appearance notecard
  182. string Notecard = "!Path"; // The name of the recorded routes
  183. integer allowUsers = FALSE; // If true, any user can get a Start NPC and Stop NPC menu. Only groups and owners can get all commands if TRUE, or FALSE
  184. float MAXDIST = 2.0; // how close a NPC has to get to a dest pos to continue to next state. Do not lower this too much, as it may miss the target
  185. integer WANDERRAND = TRUE; // set to TRUE and they will pause during wanders a random number of seconds
  186. float WANDERTIME = 3.0; // how long they stand after each @wander,if WANDERRAND is FALSE. If WANDERRAND is TRUE, this is the max time
  187. integer WAIT = 30; // wait for this number of seconds for the NPC to reach a destination (for safety). If it fails to reach a target, it will move on after this time.
  188. float RANGE = 50; // 1 to N meters - anyone this close to the controller will start NPCS if Sensor button is clicked
  189. float REZTIME = 2.0; // wait this long for NPC to rez in, then start the process
  190. string STAND = "Stand"; // the name of the default Stand animation
  191. string WALK = "Walk"; // the name of the default Walk animation
  192. string FLY = "Fly"; // the name of the default Fly animation
  193. string RUN = "Run"; // the name of the default Run animation
  194. string LAND = "Land"; // the name of the default land animation ( for birds only)
  195. float OffsetZ = 0.5; // appear 0.5 meter above ground, this is added to all destinations to keep them from sinking in.
  196. float SPEEDMULT =0.5; // 1.0 = regular avatar speed. Smaller numbers slow down walks. Large numbers speed them up.
  197. integer FLIGHT = 299; // For controlling wings. A channel that is shouted at when flight starts and ends. "flying" or "landing"
  198. // DESCRIPTIONS FIELDS HAVE TO SURVIVE A RESET
  199. // These vars are stored by saving them with KeyValueSet
  200. // "pr" is a 0 if it is set for Owner Only, 1 for Group control
  201. // "se" is "on" if Started
  202. // "co" = "R" or "A" for relative or absolute addressing mode
  203. // "key" = NPC key
  204. // These Globals used to be stored in description. Moved to RAM in V1.6
  205. float RAMPause; // @pause param
  206. float RAMwd ; // @wander distance
  207. integer RAMwc; // @wander count
  208. float RAMrot; // @rotate
  209. string RAMsit; // @sit primname
  210. string RAMtouch; // @touch primname
  211. string RAManimationName; // @animate animation (string) time (float)
  212. float RAManimationTime;
  213. // other globals section
  214. integer iChannel; // a listen channel, randomly assigned
  215. integer iHandle; // the handle to it
  216. // NPC controls
  217. vector newDest ; // tmp storage for the walks
  218. integer iWaitCounter ; // wait for this number of seconds for the NPC to reach a desrtination
  219. string sNPCName; // the name of the NPC that may be in world. So we can remove it.
  220. integer bNPC_STOP = FALSE; // boolean to reuse a listener
  221. integer Stopped = FALSE; // set to TRUE by link messages so we do not remember them
  222. float fTimerVal ; // how long we wait when wandering (calculated)
  223. float NPCEnabled; // true if the NPC is suppodes to be running
  224. // OS_NPC_CREATOR_OWNED will create an 'owned' NPC that will only respond to osNpc* commands issued from scripts that have the same owner as the one that created the NPC.
  225. // OS_NPC_NOT_OWNED will create an 'unowned' NPC that will respond to any script that has OSSL permissions to call osNpc* commands.
  226. integer NPCOptions = OS_NPC_CREATOR_OWNED; // only yhe owner of this box can control this NPC.
  227. integer walkstate = 0; // helps us reshare the walk state for run, fly and land - a bit of a hack, but it saves RAM. Has to be done this way because some bits of NPCWalkOption are asserted as 0
  228. integer NPCWalkOption; // Some notes for what happens to NPCWalkOption:
  229. // OS_NPC_FLY - Fly the avatar to the given position. The avatar will not land unless the OS_NPC_LAND_AT_TARGET option is also given.
  230. // OS_NPC_NO_FLY - Do not fly to the target. The NPC will attempt to walk to the location. If it's up in the air then the avatar will keep bouncing hopeless until another move target is given or the move is stopped
  231. //OS_NPC_LAND_AT_TARGET - If given and the avatar is flying, then it will land when it reaches the target. If OS_NPC_NO_FLY is given then this option has no effect.
  232. // OS_NPC_RUNNING - if given, NPC avatar moves at running/fast flying speed, otherwise moves at walking/slow flying speed.
  233. // menus
  234. string mSensor="Sense is Off"; // Sensor or "No Sensor"
  235. list lAtButtons = ["Menu","-", ">>", "@run", "@walk", "@fly", "@land", "@wander", "@sit", "@stand","@animate","@rotate"];
  236. list lMenu2 = ["<<", "@comment", ">>>", "@stop", "@say", "@whisper","@shout","@sound","@randsound","@cmd", "@pause", "@delete"];
  237. list lMenu3 = ["<<<","@notecard","@appearance", "@touch", "@speed", "@attach", "@teleport","-", "-", "-", "-", "-" ];
  238. string sCommand; // place to store a command for two-prompted ones
  239. string sParam2; // place to store a prompt for two-prompted ones
  240. string priPub = "Owner Only"; // Private or Group
  241. key kUserKey; // the person who is controlling the avatar, not the Owner
  242. // the command lists
  243. list lCommands; // commands are stored here
  244. list lNpcCommandList; // Storage for the NPC script.
  245. string npcAction; // Storage for the next action. @cmd=0|hello, this becomes @cmd
  246. string npcParams; // Storage for the param, @cmd=0|hello, this becomes 0|hello
  247. // misc vars
  248. string sNotecard; // commands are stored here temporarily for dumping
  249. vector vWanderPos; // a place to wander to
  250. string lastANIM ; // last animation run
  251. // Sensor
  252. integer avatarPresent; // Sensor sets this flag when people are within Range.
  253. // Coordinate control
  254. vector vInitialPos ; // Vector that will be filled by the script with the initial starting position in region coordinates.
  255. vector vDestPos = ZERO_VECTOR; // Storage for destination position.
  256. string relAbs = "Relative"; // absolute vs relative positioning
  257. // STATES
  258. integer MENU ; // processing a dialog box state, may be concurrent with STATE
  259. integer STATE; // state storage
  260. integer MakeNotecard = 1; // displaying a text box for NPC name
  261. integer RecordPath = 2; // displaying a path notecard menu
  262. integer NobodyHome = 3; // looking for an avatar
  263. integer Spawning = 4; // spawning an avatar
  264. integer Animate = 5; // animation timer needed
  265. integer Walking = 6; // Hey! I am walking here!
  266. integer Wander = 7; // Wandering around neeeds a timer, too
  267. integer WanderHold = 8; // We reached a wander point
  268. integer DoProcess = 9; // Set this to make it process a new command
  269. integer Touch = 10; // Timer is busy sensing something to touch
  270. integer Sit = 11; // Timer is busy sensing something to sit on
  271. integer Paused = 12; // Timer is busy pausing
  272. key gNpcKey = NULL_KEY; // global key storage for the one NPC, to save CPU cycles
  273. list Stack ; // a command stack from link message input
  274. integer SensorFunc = 0; // define which function shall be triggered inside the sensor function
  275. // 0 means none, 1 sit, 2 touch
  276. ///////////////////////////////////////////////////////////////////////////
  277. // FUNCTIONS //
  278. ///////////////////////////////////////////////////////////////////////////
  279. TimerEvent(float timesent)
  280. {
  281. if (LSLEditor)
  282. timesent *= 5; // slow thinggs doen when the LSLEDITOR is in use
  283. DEBUG("Setting timer: " + (string) timesent);
  284. llSetTimerEvent(timesent);
  285. }
  286. // for 4.1 parse a message from a Listen or a Link message
  287. ParseMsg(string str) {
  288. DEBUG("Command In:" + str);
  289. if (str=="@go") {
  290. SetStop(FALSE); // Let's run the notecard
  291. DEBUG("@go running");
  292. DoProcessNPCLine();
  293. } else {
  294. Stack += [str]; // take anything, the controller will filter away non @ stuff
  295. DoProcessNPCLine(); // v 4.5 remove wait for STATE == 0
  296. }
  297. }
  298. SetStop(integer what)
  299. {
  300. DEBUG("Stopped set to " + (string ) what);
  301. Stopped = what;
  302. }
  303. // Do* functions are much like states from the old V2 scripts.
  304. // Save a Path notecard
  305. DoSave()
  306. {
  307. STATE = MakeNotecard;
  308. makeText("Stand where you want the NPC to appear, and enter the NPC Name");
  309. }
  310. // This function is used to record the path for the NPC
  311. // Each command can take 0, 1, or 2 params
  312. DoMenuForCommands() {
  313. makeMenu(lAtButtons);
  314. }
  315. // No one is here when sensors were on, so we kill off the NPC
  316. DoNobodyHome()
  317. {
  318. DEBUG("Nobody Home");
  319. STATE = NobodyHome;
  320. if (NPCKey() != NULL_KEY) {
  321. osNpcRemove(NPCKey());
  322. SaveKey(NULL_KEY);
  323. }
  324. TimerEvent(5); // keep ticking to sense avatars
  325. }
  326. // Create a NPC
  327. StateSpawn() {
  328. DEBUG("state spawn");
  329. STATE = Spawning;
  330. NPCEnabled = TRUE; // in world
  331. // see if there is already one out there.
  332. if (NPCKey() != NULL_KEY) {
  333. DEBUG("Already living");
  334. return;
  335. }
  336. list name = llParseString2List(sNPCName, [" "], []);
  337. vector vRezPos = vInitialPos;
  338. if (relAbs == "Relative"){
  339. vRezPos += llGetPos();
  340. }
  341. DEBUG("Rezzing the NPC:" + (string) vRezPos);
  342. key aKey = osNpcCreate(llList2String(name, 0), llList2String(name, 1), vRezPos, Appearance, NPCOptions);
  343. SaveKey(aKey); // save in desceription and global, too
  344. osSetSpeed(aKey,SPEEDMULT); // 1.9 speed multiplier
  345. TimerEvent(REZTIME);
  346. NPCAnimate(STAND);
  347. }
  348. DoRotate() {
  349. DEBUG("@rotate=" + (string) RAMrot);
  350. osNpcSetRot(NPCKey(), llEuler2Rot(<0,0,RAMrot> * DEG_TO_RAD));
  351. }
  352. StateSit() {
  353. DEBUG ("state sit - looking for " + RAMsit);
  354. STATE=Sit;
  355. SensorFunc = 1; //triggers osNpcSit
  356. llSensor(RAMsit, "", PASSIVE|ACTIVE|SCRIPTED, 96, PI);
  357. }
  358. StateTouch() {
  359. DEBUG ("state touch - looking for " + RAMtouch);
  360. STATE = Touch;
  361. SensorFunc = 2; //triggers osNpcTouch
  362. llSensor(RAMtouch, "", PASSIVE|ACTIVE|SCRIPTED, 96, PI);
  363. }
  364. DoStand() {
  365. DEBUG("state stand");
  366. osNpcStand(NPCKey());
  367. }
  368. StateAnimate() {
  369. DEBUG("state animate");
  370. STATE = Animate;
  371. NPCAnimate(RAManimationName);
  372. if (RAManimationTime <=0 ) // V 3.4 tweak
  373. RAManimationTime = 1;
  374. TimerEvent(RAManimationTime);
  375. }
  376. StateWalk() {
  377. DEBUG("NPCWalkOption = " + (string) NPCWalkOption);
  378. STATE = Walking;
  379. // walk, fly, run, land
  380. if (walkstate == 1) {
  381. NPCAnimate(WALK);
  382. } else if (walkstate == 2) {
  383. llShout(FLIGHT,"flying");
  384. NPCAnimate(FLY);
  385. } else if (walkstate == 3) {
  386. NPCAnimate(RUN);
  387. } else if (walkstate == 4) {
  388. NPCAnimate(LAND);
  389. }
  390. newDest = vDestPos ;
  391. newDest.z += OffsetZ;
  392. // notecard is stored as offsets from this box with relative addressing. Convert to absolute
  393. if (relAbs == "Relative"){
  394. newDest += llGetPos();
  395. }
  396. DEBUG("Moveto:" + (string) newDest);
  397. osNpcMoveToTarget(NPCKey(), newDest, NPCWalkOption);
  398. iWaitCounter = WAIT; // wait 60 seconds to get to a destination.
  399. TimerEvent(TIMER);
  400. }
  401. StateWander(){
  402. DEBUG("state wander");
  403. STATE = Wander;
  404. vector point = CirclePoint(RAMwd);
  405. DEBUG("CirclePoint:" + (string) point);
  406. vWanderPos = vDestPos + point;
  407. DEBUG("vWanderPos:" + (string) vWanderPos);
  408. fTimerVal = WANDERTIME; // default time to pause after each wander
  409. if (WANDERRAND)
  410. fTimerVal = llFrand(WANDERTIME) + 1; // override, they want random times
  411. NPCAnimate(WALK);
  412. DEBUG("Wander to:" + (string) vWanderPos);
  413. osNpcMoveToTarget(NPCKey(), vWanderPos, NPCWalkOption);
  414. iWaitCounter = WAIT; // wait 60 seconds to get to a destination.
  415. TimerEvent(TIMER);
  416. }
  417. StateWanderhold() {
  418. DEBUG("Wander Hold");
  419. STATE = WanderHold;
  420. // now that we have reached a wander spot, slow the timer down to the desired value
  421. TimerEvent(fTimerVal);
  422. }
  423. // @pause=10 will do nothing for 10 seconds
  424. DoPause() {
  425. STATE =Paused;
  426. if (RAMPause < 0.1)
  427. RAMPause = 0.1;
  428. DEBUG("@pause=" + (string)RAMPause);
  429. TimerEvent(RAMPause);
  430. }
  431. // @stop makes the NPC stop moving in whatever state it is in. You have to linkmessage to get moving again
  432. DoStop() {
  433. DEBUG("NPC is Stopped");
  434. STATE = 0; // accept commands
  435. SetStop(TRUE); // Link controlled - we mnust have a @go to continue with notecards
  436. TimerEvent(0);
  437. Stack = []; // v3.8
  438. }
  439. // @delete removes the NPC forever. Next command starts it up again at the beginning
  440. DoDelete() {
  441. DEBUG("state delete");
  442. STATE = 0; // accept commands
  443. osNpcRemove(NPCKey());
  444. SaveKey(NULL_KEY);
  445. TimerEvent(0);
  446. Stack = []; // v3.8
  447. }
  448. // change the appearance of the NPC
  449. DoAppearance(string notecard) {
  450. DEBUG("state appearance");
  451. if (llGetInventoryType(notecard) == INVENTORY_NOTECARD){
  452. DEBUG("Load appearance " + notecard);
  453. osNpcLoadAppearance(NPCKey(),notecard);
  454. }
  455. }
  456. // Change the avatar speed
  457. DoSpeed(string speed) {
  458. float newspeed = (float) speed;
  459. if (newspeed > 0.1 && newspeed < 5.0) {// sanity check
  460. osSetSpeed(NPCKey(),newspeed);
  461. }
  462. }
  463. DoTeleport(string params) {
  464. list Data = llParseString2List(params, ["|"], []);
  465. string itemName = llList2String(Data, 0);
  466. vector Dest = (vector) itemName;
  467. if (Dest != ZERO_VECTOR) {
  468. if (relAbs == "Relative"){
  469. Dest += llGetPos();
  470. }
  471. osTeleportAgent( NPCKey(), llGetRegionName(), Dest, ZERO_VECTOR );
  472. } else {
  473. llSay(DEBUG_CHANNEL,"Attempt to teleport to <0,0,0> probably not what you intended: @teleport=<vector>");
  474. }
  475. }
  476. DoNewNote (string card) {
  477. DEBUG("Load Notecard " + card);
  478. NPCReadNoteCard(card);
  479. SetStop(FALSE);
  480. }
  481. DoAttach(string params) {
  482. list Data = llParseString2List(params, ["|"], []);
  483. string itemName = llList2String(Data, 0);
  484. integer attachmentPoint = (integer) llList2String(Data, 1);
  485. if (attachmentPoint > 0
  486. && attachmentPoint < 40
  487. && llGetInventoryType(itemName) == INVENTORY_OBJECT
  488. )
  489. {
  490. osForceAttachToOtherAvatarFromInventory(NPCKey(),itemName,attachmentPoint);
  491. }
  492. }
  493. // This loops over the notecard, processing each command
  494. DoProcessNPCLine() {
  495. DEBUG("ProcessNPCLine, stopped = " + (string) Stopped);
  496. STATE = DoProcess;
  497. // auto load a notecard
  498. if (! llGetListLength(lNpcCommandList)) {
  499. DEBUG("Read Notecard");
  500. NPCReadNoteCard(Notecard);
  501. SetStop(FALSE);
  502. }
  503. // look for link messages on the stack
  504. string next = llList2String(Stack,0); // lets see if there is anything from a link message
  505. if (llStringLength(next))
  506. {
  507. Stack = llDeleteSubList(Stack,0,0);
  508. ProcessCmd(next); //lets do this command instead.
  509. return;
  510. }
  511. // @stop issued?
  512. if (Stopped) {
  513. TimerEvent(0);
  514. DEBUG("Stopped, waiting for input");
  515. STATE = 0;
  516. return;
  517. }
  518. // No, we have an @go for liftoff
  519. next = llList2String(lNpcCommandList, 0); // get the next command
  520. DEBUG("Execute:" + next);
  521. lNpcCommandList = llDeleteSubList(lNpcCommandList, 0, 0); // delete it
  522. if (llGetListLength(lNpcCommandList) == 0) {
  523. DEBUG("EOF");
  524. }
  525. ProcessCmd(next);
  526. }
  527. ProcessCmd(string cmd) {
  528. DEBUG("ProcessCmd:" + cmd);
  529. if (llGetSubString(cmd, 0, 0) != "@") {
  530. DEBUG("ignoring");
  531. TimerEvent(QUICK); // this is so we do not recurse the stack
  532. STATE = 0;
  533. return;
  534. }
  535. list data = llParseString2List(cmd, ["="], []);
  536. npcAction = llToLower(llStringTrim(llList2String(data, 0), STRING_TRIM));
  537. DEBUG("Action:" + npcAction);
  538. npcParams = llStringTrim(llList2String(data, 1), STRING_TRIM);
  539. DEBUG("Params:" + npcParams);
  540. @commands;
  541. ProcessSensor();
  542. if(npcAction == "@spawn" && avatarPresent) {
  543. DEBUG("@spawn");
  544. list spawnData = llParseString2List(npcParams, ["|"], []);
  545. sNPCName =llList2String(spawnData, 0); // V 1.6 name in RAM
  546. list spawnDest = llParseString2List(llList2String(spawnData, 1), ["<", ",", ">"], []);
  547. vInitialPos.x = llList2Float(spawnDest, 0);
  548. vInitialPos.y = llList2Float(spawnDest, 1);
  549. vInitialPos.z = llList2Float(spawnDest, 2);
  550. DEBUG("Coords for NPC at " + (string) vInitialPos);
  551. StateSpawn();
  552. return;
  553. }
  554. if (! avatarPresent){
  555. DoNobodyHome();
  556. DEBUG("No avatar nearby");
  557. STATE = 0;
  558. return;
  559. } else {
  560. if ( NPCKey() == NULL_KEY) {
  561. StateSpawn();
  562. }
  563. }
  564. if(npcAction == "@stop") {
  565. DoStop();
  566. STATE = 0;
  567. return;
  568. }
  569. else if(npcAction == "@goto") {
  570. DEBUG("goto");
  571. integer lastIdx = llGetListLength(lNpcCommandList)-1;
  572. lNpcCommandList = llDeleteSubList(lNpcCommandList, lastIdx, lastIdx);
  573. // Wind commands till goto label.
  574. @wind;
  575. string next1 = llList2String(lNpcCommandList, 0);
  576. lNpcCommandList = llDeleteSubList(lNpcCommandList, 0, 0);
  577. lNpcCommandList += next1;
  578. if(next1 != npcParams) jump wind;
  579. // Wind the label too.
  580. next1 = llList2String(lNpcCommandList, 0);
  581. lNpcCommandList = llDeleteSubList(lNpcCommandList, 0, 0);
  582. lNpcCommandList += next1;
  583. // Get next command.
  584. list data1 = llParseString2List(next1, ["="], []);
  585. npcAction = llToLower(llStringTrim(llList2String(data1, 0), STRING_TRIM));
  586. npcParams = llStringTrim(llList2String(data1, 1), STRING_TRIM);
  587. // Reschedule.
  588. jump commands;
  589. }
  590. else if(npcAction == "@sound") {
  591. DEBUG("sound");
  592. llTriggerSound(npcParams,1.0);
  593. }
  594. else if(npcAction == "@randsound") {
  595. DEBUG("@randsound");
  596. integer N = llGetInventoryNumber(INVENTORY_SOUND);
  597. integer rand = llCeil(llFrand(N)) -1; // pick a random sound
  598. string toPlay = llGetInventoryName(INVENTORY_SOUND,rand);
  599. llTriggerSound(toPlay,1.0);
  600. }
  601. else if(npcAction == "@walk") {
  602. DEBUG("@walk");
  603. GetDest(npcParams);
  604. walkstate = 1;// walking
  605. NPCWalkOption = OS_NPC_NO_FLY ;
  606. StateWalk();
  607. return;
  608. }
  609. else if(npcAction == "@fly") {
  610. GetDest(npcParams);
  611. walkstate = 2;// flying
  612. NPCWalkOption = OS_NPC_FLY ;
  613. StateWalk();
  614. return;
  615. }
  616. else if(npcAction == "@run") {
  617. DEBUG("@run");
  618. GetDest(npcParams);
  619. walkstate = 3;// running
  620. NPCWalkOption = OS_NPC_NO_FLY | OS_NPC_RUNNING;
  621. StateWalk();
  622. return;
  623. }
  624. else if(npcAction == "@land") {
  625. DEBUG("@land");
  626. GetDest(npcParams);
  627. walkstate = 4;// landing
  628. NPCWalkOption= OS_NPC_FLY | OS_NPC_LAND_AT_TARGET ;
  629. StateWalk();
  630. return;
  631. }
  632. else if(npcAction == "@say") {
  633. DEBUG("@say " + npcParams);
  634. osNpcSay(NPCKey(), 0, npcParams);
  635. }
  636. else if(npcAction == "@shout") {
  637. DEBUG("@shout");
  638. osNpcShout(NPCKey(),0, npcParams);
  639. }
  640. else if(npcAction == "@whisper") {
  641. DEBUG("@whisper " + npcParams);
  642. osNpcWhisper(NPCKey(),0, npcParams);
  643. }
  644. // speak a command on a channel, so you can open doors and control stuff.
  645. else if(npcAction == "@cmd") {
  646. DEBUG("@cmd");
  647. list dataToSpeak = llParseString2List(npcParams, ["|"], []);
  648. string channel = llList2String(dataToSpeak,0);
  649. DEBUG("Channel:"+(string) channel);
  650. integer iChannel = (integer) channel;
  651. string stringToSpeak = llList2String(dataToSpeak,1);
  652. llSay(iChannel, stringToSpeak);
  653. }
  654. // stop everything
  655. else if(npcAction == "@pause") {
  656. RAMPause = (float) npcParams;
  657. DoPause();
  658. return;
  659. }
  660. else if(npcAction == "@wander") {
  661. list wanderData = llParseString2List(npcParams, ["|"], []);
  662. RAMwd = (float) llList2String(wanderData, 0);
  663. RAMwc = (integer) llList2String(wanderData, 1);
  664. vDestPos = osNpcGetPos(NPCKey()); // set the wander start
  665. DEBUG("Starting at " + (string) vDestPos);
  666. StateWander();
  667. return;
  668. }
  669. else if(npcAction == "@rotate") {
  670. RAMrot = (float) npcParams;
  671. DoRotate();
  672. }
  673. else if(npcAction == "@sit") {
  674. RAMsit= npcParams;
  675. StateSit();
  676. return;
  677. }
  678. else if(npcAction == "@touch") {
  679. RAMtouch= npcParams;
  680. StateTouch();
  681. return;
  682. }
  683. else if(npcAction == "@stand") {
  684. DoStand();
  685. }
  686. else if(npcAction == "@delete") {
  687. DoDelete();
  688. SetStop(TRUE); // Link controlled - we mnust have a @go to continue with notecards
  689. return;
  690. }
  691. else if(npcAction == "@animate") {
  692. list animateData = llParseString2List(npcParams, ["|"], []);
  693. RAManimationName = llList2String(animateData, 0);
  694. RAManimationTime = (float) llList2String(animateData, 1);
  695. StateAnimate();
  696. return;
  697. }
  698. else if(npcAction == "@appearance" ){
  699. DoAppearance(npcParams);
  700. }
  701. else if (npcAction =="@speed") {
  702. DoSpeed(npcParams);
  703. }
  704. else if (npcAction =="@notecard") {
  705. DoNewNote(npcParams);
  706. Notecard = npcParams;
  707. }
  708. else if (npcAction == "@attach")
  709. {
  710. DoAttach(npcParams);
  711. }
  712. else if (npcAction == "@teleport")
  713. {
  714. DoTeleport(npcParams);
  715. }
  716. else if (npcAction == "@reset")
  717. {
  718. DoDelete();
  719. SetStop(FALSE); // a @resst will restart the original !Path after deleting the notecard.
  720. }
  721. STATE = 0;
  722. TimerEvent(QUICK); // yeah I know, not possible this fast, we just go as fast as we can go - this is so we do not recurse the stack
  723. }
  724. /////////////////// UTILITY Functions, not state-like //////////////////
  725. // DEBUG(string) will chat a string or display it as hovertext if debug == TRUE
  726. DEBUG(string str) {
  727. if (debug && ! LSLEditor)
  728. llOwnerSay( str); // Send the owner debug info
  729. if (debug && LSLEditor)
  730. llSay(0, str); // Send to the Console in LSLEDitor
  731. if (iTitleText) {
  732. llSetText(str,<1.0,1.0,1.0>,1.0); // show hovertext
  733. }
  734. }
  735. GetDest(string npcParams) {
  736. list dest = llParseString2List(npcParams, ["<", ",", ">"], []);
  737. vDestPos.x = llList2Float(dest, 0);
  738. vDestPos.y = llList2Float(dest, 1);
  739. vDestPos.z = llList2Float(dest, 2);
  740. }
  741. NPCReadNoteCard(string Note) {
  742. DEBUG("NPCReadNoteCard");
  743. lNpcCommandList = llParseString2List(osGetNotecard(Note), ["\n"], []);
  744. }
  745. integer SenseAvatar()
  746. {
  747. //Returns a strided list of the UUID, position, and name of each avatar in the region
  748. list avatars = llGetAgentList(AGENT_LIST_REGION ,[]);
  749. integer numOfAvatars = llGetListLength(avatars);
  750. if (numOfAvatars == 0)
  751. {
  752. DEBUG("No people");
  753. return 0;
  754. }
  755. //DEBUG("Located " + (string)numOfAvatars + " avatars and NPC's");
  756. integer nAvatars;
  757. integer i;
  758. for( i = 0;i < numOfAvatars; i++) {
  759. key aviKey = llList2Key(avatars,i);
  760. if (!osIsNpc(aviKey)) {
  761. list detail = llGetObjectDetails(aviKey,[OBJECT_POS]);
  762. vector pos = llList2Vector(detail,0);
  763. float dist = llVecDist(pos, llGetPos());
  764. if (dist < RANGE)
  765. {
  766. nAvatars++;
  767. DEBUG("In range:" + llKey2Name(aviKey));
  768. }
  769. }
  770. }
  771. //DEBUG("Located " + (string) nAvatars + " avatars");
  772. return nAvatars;
  773. }
  774. // return TRUE if the avatar is owner when private is set, or TRUE if the avatar is in the same group and GROUP is set.
  775. integer checkPerms() {
  776. integer group = (integer) KeyValueGet("pr");
  777. if (! group)
  778. priPub = "Owner Only";
  779. else
  780. priPub = "Group";
  781. if (llDetectedKey(0) == llGetOwner()){
  782. kUserKey = llDetectedKey(0);
  783. return TRUE;
  784. }
  785. if ( group && llDetectedGroup(0)) {
  786. kUserKey = llDetectedKey(0);
  787. return TRUE;
  788. }
  789. kUserKey = llDetectedKey(0);
  790. return FALSE;
  791. }
  792. NPCAnimate(string anim)
  793. {
  794. DEBUG("Start Anim: " + anim);
  795. if (llGetInventoryType(anim) == INVENTORY_ANIMATION ) {
  796. if (lastANIM != anim) {
  797. if(llStringLength(lastANIM)) {
  798. osNpcStopAnimation(NPCKey(), lastANIM);
  799. }
  800. osNpcPlayAnimation(NPCKey(), anim);
  801. lastANIM = anim;
  802. }
  803. } else {
  804. llSay(DEBUG_CHANNEL, "No animation named " + anim);
  805. }
  806. }
  807. // Kill a NPC by Name
  808. Kill(string param)
  809. {
  810. integer count;
  811. list avatars = osGetAvatarList(); // Returns a strided list of the UUID, position, and name of each avatar in the region except the owner.\
  812. integer i;
  813. integer j = llGetListLength(avatars);
  814. for (i=0 ; i <= j; i+=3){
  815. string desired = llList2String(avatars,i+2);
  816. desired = llStringTrim(desired,STRING_TRIM); // should not be needed but is needed
  817. if (desired == param){
  818. vector v = llList2Vector(avatars,i+1);
  819. key target = llList2Key(avatars,i); // get the UUID of the avatar
  820. osNpcRemove(target);
  821. llOwnerSay("Removed " + param+ " at location " + (string) v);
  822. count++;
  823. }
  824. }
  825. NPCEnabled = FALSE; // not in world
  826. SaveKey(NULL_KEY ); // Rev 4.4
  827. if (count)
  828. llOwnerSay("Removed " + (string) count + " NPC's");
  829. else
  830. llOwnerSay("Could not locate " + param);
  831. }
  832. // return a String for the position we are at. Strings used as the caller wants strings
  833. string Pos()
  834. {
  835. vector where = llGetPos(); // find the box position
  836. where.z += OffsetZ; // use the ground position + an offset
  837. if (LSLEditor)
  838. where = <128,128,31 + llFrand(1)>; // force center of sim when editing
  839. // if attached the height will be too high by 1/2 the agent size
  840. if (llGetAttached()) {
  841. vector size = llGetAgentSize(llGetOwner());
  842. float Z = size.z;
  843. where.z -= Z/2;
  844. }
  845. // DEBUG("Pos= " + (string) where);
  846. return (string) where;
  847. }
  848. // setup a menu with a timer for timeouts, called by all make*()
  849. menu()
  850. {
  851. llListenRemove(iHandle);
  852. iChannel = llCeil(llFrand(100000) + 20000);
  853. iHandle = llListen(iChannel,"","","");
  854. TimerEvent(30.0);
  855. MENU = TRUE;
  856. }
  857. // make a text box
  858. makeText(string Param)
  859. {
  860. menu();
  861. llTextBox(kUserKey, Param, iChannel);
  862. }
  863. // top level menu
  864. makeMainMenu()
  865. {
  866. menu();
  867. list buttons = ["Appearance","Recording","Save","Help","-","Erase RAM", priPub,relAbs,"-","Stop NPC",mSensor,"Start NPC"];
  868. llDialog(kUserKey,(string) llGetListLength(lCommands) + " Records",buttons,iChannel);
  869. }
  870. // Rev 1.4
  871. // top level menu for non group/ non owners
  872. makeUserMenu()
  873. {
  874. if (!allowUsers) return;
  875. menu();
  876. list buttons = ["Start NPC","Stop NPC"];
  877. llDialog(kUserKey,"Choose",buttons,iChannel);
  878. }
  879. // programmable menu for @commands
  880. makeMenu(list buttons)
  881. {
  882. menu();
  883. llDialog(kUserKey,(string) llGetListLength(lCommands) + " Record",buttons,iChannel);
  884. }
  885. // make one or two text boxes with prompts
  886. Text(string cmd, string p1, string p2)
  887. {
  888. sCommand = cmd;
  889. sParam2 = "";
  890. if (llStringLength(p2))
  891. sParam2 = p2;
  892. makeText(p1);
  893. }
  894. // Set the Avatar Present flag - if sensors are off and we are forece run, there will be one present.
  895. ProcessSensor()
  896. {
  897. integer SensorOn;
  898. if ("on" == KeyValueGet("se"))
  899. {
  900. SensorOn = TRUE; // we need to scan for avatars
  901. } else {
  902. SensorOn = FALSE; // we need to scan for avatars
  903. }
  904. DEBUG("Sensor:" + (string) SensorOn);
  905. integer n = SenseAvatar();
  906. DEBUG("Avatars:" + (string) n);
  907. if (SensorOn && n)
  908. avatarPresent = TRUE; // someone is here and we need to tell the system to run
  909. else if (SensorOn && !n)
  910. avatarPresent = FALSE; // someone is not here and we need to tell the system to stop
  911. else { // sensor is off, lete see if there is a NPC. If so, we are ON
  912. DEBUG("NPCEnabled:" + (string) NPCEnabled);
  913. if (NPCEnabled)
  914. avatarPresent = TRUE;
  915. else
  916. avatarPresent = FALSE;
  917. }
  918. // start up from when when no one is near
  919. if (avatarPresent && STATE == NobodyHome)
  920. STATE = 0;
  921. DEBUG("Avatar Present: " + (string) avatarPresent);
  922. }
  923. vector CirclePoint(float radius) {
  924. float x = llFrand(radius *2) - radius; // +/- radius, randomized
  925. float y = llFrand(radius *2) - radius; // +/- radius, randomized
  926. return <x, y, 0>; // so this should always happen
  927. }
  928. string KeyValueGet(string var) {
  929. list dVars = llParseString2List(llGetObjectDesc(), ["&"], []);
  930. do {
  931. list data = llParseString2List(llList2String(dVars, 0), ["="], []);
  932. string k = llList2String(data, 0);
  933. if(k != var) jump continue;
  934. //DEBUG("got " + var + " = " + llList2String(data, 1));
  935. return llList2String(data, 1);
  936. @continue;
  937. dVars = llDeleteSubList(dVars, 0, 0);
  938. } while(llGetListLength(dVars));
  939. return "";
  940. }
  941. KeyValueSet(string var, string val) {
  942. //DEBUG("set " + var + " = " + val);
  943. list dVars = llParseString2List(llGetObjectDesc(), ["&"], []);
  944. if(llGetListLength(dVars) == 0)
  945. {
  946. llSetObjectDesc(var + "=" + val);
  947. return;
  948. }
  949. list result = [];
  950. do {
  951. list data = llParseString2List(llList2String(dVars, 0), ["="], []);
  952. string k = llList2String(data, 0);
  953. if(k == "") jump continue;
  954. if(k == var && val == "") jump continue;
  955. if(k == var) {
  956. result += k + "=" + val;
  957. val = "";
  958. jump continue;
  959. }
  960. string v = llList2String(data, 1);
  961. if(v == "") jump continue;
  962. result += k + "=" + v;
  963. @continue;
  964. dVars = llDeleteSubList(dVars, 0, 0);
  965. } while(llGetListLength(dVars));
  966. if(val != "") result += var + "=" + val;
  967. llSetObjectDesc(llDumpList2String(result, "&"));
  968. }
  969. // clear RAM
  970. Clr() {
  971. lCommands = [];
  972. llOwnerSay("RAM Memory cleared. Notecards, if any, are not modified.");
  973. makeMainMenu();
  974. }
  975. integer checkNoteCards()
  976. {
  977. // Check that they have saved an Appeaance and Path notecard
  978. integer num = llGetInventoryNumber(INVENTORY_NOTECARD); // how many notecards overall
  979. integer i;
  980. integer count;
  981. for (; i < num; i++){
  982. if (llGetInventoryName(INVENTORY_NOTECARD,i) == Notecard)
  983. count++;
  984. if (llGetInventoryName(INVENTORY_NOTECARD,i) == Appearance)
  985. count++;
  986. }
  987. DEBUG("Checked " + (string) count + " Notecards");
  988. // if we have both, run the NPC
  989. return count;
  990. }
  991. Update(string SName) {
  992. // delete all NPC* scripts except myself
  993. integer i;
  994. integer j = llGetInventoryNumber(INVENTORY_SCRIPT);
  995. for (i = 0; i < j; i++) {
  996. string targetName = llGetInventoryName(INVENTORY_SCRIPT,i);
  997. string match = llGetSubString(targetName,0,2);
  998. if (match == SName && llGetScriptName() != targetName){
  999. llOwnerSay("Upgrading " + targetName);
  1000. if (! LSLEditor){ // lets not kill the editor
  1001. llRemoveInventory(targetName);
  1002. }
  1003. }
  1004. }
  1005. }
  1006. // Get all default saved params from the Description
  1007. GetSwitches()
  1008. {
  1009. string rA = KeyValueGet("co"); // Get the remembered menu setting for Abs Vs Relative
  1010. if (rA == "A")
  1011. relAbs = "Absolute";
  1012. else if (rA == "R")
  1013. relAbs = "Relative";
  1014. else
  1015. relAbs = "Absolute";
  1016. // reenable NPC if sensor is on.
  1017. if ("on" == KeyValueGet("se"))
  1018. {
  1019. NPCEnabled = TRUE;
  1020. mSensor = "Sense is On";
  1021. ProcessSensor(); // fake 1 avatar to get it rezzed
  1022. } else {
  1023. mSensor = "Sense is Off";
  1024. }
  1025. }
  1026. SaveKey(key akey)
  1027. {
  1028. DEBUG("Saving Key of " + (string) akey);
  1029. KeyValueSet("key", akey);
  1030. if (akey != (key) KeyValueGet("key") )
  1031. {
  1032. DEBUG("Fatal error, cannot save key");
  1033. }
  1034. gNpcKey = akey;
  1035. }
  1036. key NPCKey()
  1037. {
  1038. key akey = gNpcKey; // from cached copy
  1039. // gNpcKey saves a lot of CPU processing by caching the key, if blank we get it from the description
  1040. if (gNpcKey == NULL_KEY)
  1041. {
  1042. //DEBUG("Get DKey");
  1043. akey = KeyValueGet("key"); // from Description of the prim
  1044. }
  1045. // DEBUG("NPC KEY:" + (string) akey);
  1046. return akey;
  1047. }
  1048. /////////////////// CODE BEGINS //////////////////
  1049. default
  1050. {
  1051. changed(integer change) {
  1052. if(change & CHANGED_REGION_START) {
  1053. llResetScript();
  1054. }
  1055. }
  1056. on_rez(integer start_param)
  1057. {
  1058. llResetScript();
  1059. }
  1060. state_entry() {
  1061. llSetText("",<1,1,1>,1.0); // clr all hovertext- we may not be using it.
  1062. DoDelete(); // kill any NPC that is out running
  1063. Update("NPC"); // If dragged and ropped into a prim with any script named "NPC...", this will replace it.
  1064. GetSwitches(); // Get all default saved params from the Description
  1065. // 4.1 allow listeners to send us commands
  1066. if (allowListener)
  1067. llListen(link_Channel,"","","");
  1068. TimerEvent(TIMER);
  1069. }
  1070. touch_start(integer n)
  1071. { // if touched, make a menu
  1072. if (checkPerms()) {
  1073. if (RecordPath == STATE) {
  1074. makeMenu(lAtButtons);
  1075. } else {
  1076. makeMainMenu();
  1077. }
  1078. } else {
  1079. makeUserMenu();
  1080. }
  1081. }
  1082. // menu listener
  1083. listen(integer iChannel, string name, key id, string message) {
  1084. // process @commands that come in via the listener
  1085. if (iChannel == link_Channel)
  1086. {
  1087. ParseMsg(message);
  1088. return;
  1089. }
  1090. if (MENU) {
  1091. llListenRemove(iHandle);
  1092. MENU = 0; // menu is off
  1093. iHandle = 0;
  1094. }
  1095. if (message == "Stop NPC")
  1096. {
  1097. lNpcCommandList = []; // force reload of notecard
  1098. NPCEnabled = FALSE;
  1099. if (NPCKey() != NULL_KEY){
  1100. Kill(sNPCName);
  1101. sNPCName = "";
  1102. } else {
  1103. bNPC_STOP = TRUE;
  1104. makeText("Enter name of an NPC to stop");
  1105. }
  1106. }
  1107. else if (message == "Menu" ) {
  1108. makeMainMenu();
  1109. }
  1110. else if (message == "Erase RAM"){
  1111. Clr();
  1112. }
  1113. else if (message == "Relative"){
  1114. relAbs = "Absolute";
  1115. KeyValueSet("co","A"); // remember coordinates = A
  1116. Clr();
  1117. }
  1118. else if (message == "Absolute"){
  1119. relAbs = "Relative";
  1120. KeyValueSet("co","R"); // remember coordinates = R
  1121. Clr();
  1122. }
  1123. else if (message == "Recording"){
  1124. DoMenuForCommands(); // show them the recording menu
  1125. }
  1126. else if (message == "Owner Only") {
  1127. priPub = "Group";
  1128. KeyValueSet("pr","1");
  1129. llOwnerSay("Group members have control");
  1130. makeMainMenu();
  1131. }
  1132. else if (message == "Group") {
  1133. priPub = "Owner Only";
  1134. KeyValueSet("pr","0");
  1135. llOwnerSay("Only you have control");
  1136. makeMainMenu();
  1137. }
  1138. else if (message == "Sense is On") {
  1139. mSensor ="Sense is Off";
  1140. KeyValueSet("se", "off");
  1141. llOwnerSay(mSensor);
  1142. makeMainMenu();
  1143. }
  1144. else if (message == "Sense is Off") {
  1145. mSensor ="Sense is On";
  1146. llOwnerSay(mSensor);
  1147. KeyValueSet("se", "on");
  1148. NPCEnabled = FALSE;
  1149. integer count = checkNoteCards();
  1150. if (count >= 2) {
  1151. DEBUG("Notecards ok, DoProcessNPCLine");
  1152. DoProcessNPCLine();
  1153. return;
  1154. }
  1155. if (LSLEditor) {
  1156. DoProcessNPCLine();
  1157. return;
  1158. }
  1159. llOwnerSay("You have not saved a recording and/or appearance, so you cannot start a NPC");
  1160. makeMainMenu();
  1161. }
  1162. else if (message == "Appearance") {
  1163. llRemoveInventory(Appearance); // delete the notecard
  1164. osAgentSaveAppearance(kUserKey,Appearance); // make the ntecard
  1165. llOwnerSay("Your outfit has been saved");
  1166. makeMainMenu();
  1167. }
  1168. else if (message == "Save") {
  1169. if (llGetListLength(lCommands) == 0) {
  1170. llOwnerSay("Nothing recorded, you need to make a recording first");
  1171. makeMainMenu();
  1172. return;
  1173. }
  1174. DoSave();
  1175. }
  1176. else if (message == "Help"){
  1177. llLoadURL(kUserKey,"Click to view help","http://www.outworldz.com/opensim/posts/NPC/");
  1178. makeMainMenu();
  1179. }
  1180. else if (message == "Start NPC") {
  1181. integer count = checkNoteCards();
  1182. NPCEnabled = TRUE;
  1183. if (LSLEditor) {
  1184. DoProcessNPCLine();
  1185. return;
  1186. }
  1187. if (count >= 2) {
  1188. DEBUG("Notecards approved , calling DoProcessNPCLine");
  1189. SetStop(FALSE); // Let's run the notecard
  1190. DoProcessNPCLine();
  1191. return;
  1192. }
  1193. llOwnerSay("You have not saved a recording or maybe an appearance, so we cannot start a NPC");
  1194. }
  1195. else if (bNPC_STOP){
  1196. bNPC_STOP = FALSE;
  1197. Kill(message);
  1198. }
  1199. else if (message == ">>"){
  1200. makeMenu(lMenu2);
  1201. }
  1202. else if (message == ">>>"){
  1203. makeMenu(lMenu3);
  1204. }
  1205. else if (message == "<<") {
  1206. makeMenu(lAtButtons);
  1207. }
  1208. else if (message == "<<<") {
  1209. makeMenu(lMenu2);
  1210. }
  1211. else if (message == "@comment"){
  1212. Text("# ","Enter a comment","");
  1213. }
  1214. else if (message == "@stop"){
  1215. lCommands += "@stop"+ "\n";
  1216. makeMenu(lAtButtons);
  1217. }
  1218. else if (message == "@run"){
  1219. lCommands += "@run=" + Pos() + "\n";
  1220. llOwnerSay("Recorded position: " + Pos());
  1221. makeMenu(lAtButtons);
  1222. }
  1223. else if (message == "@fly"){
  1224. lCommands += "@fly=" + Pos() + "\n";
  1225. llOwnerSay("Recorded position: " + Pos());
  1226. makeMenu(lAtButtons);
  1227. }
  1228. else if (message == "@land"){
  1229. lCommands += "@land=" + Pos() + "\n";
  1230. llOwnerSay("Recorded position: " + Pos());
  1231. makeMenu(lAtButtons);
  1232. }
  1233. else if (message == "@walk") {
  1234. lCommands += "@walk=" + Pos() + "\n";
  1235. llOwnerSay("Recorded position: " + Pos());
  1236. makeMenu(lAtButtons);
  1237. }
  1238. else if (message == "@stop"){
  1239. lCommands += "@stop"+ "\n";
  1240. makeMenu(lAtButtons);
  1241. }
  1242. else if (message == "@sound"){
  1243. Text("@sound=","Enter a sound name or UUID to trigger","");
  1244. }
  1245. else if (message == "@randsound"){
  1246. lCommands += "@randsound"+ "\n";
  1247. makeMenu(lAtButtons);
  1248. }
  1249. else if (message == "@say") {
  1250. Text("@say=","Enter what the NPC will say","");
  1251. }
  1252. else if (message == "@whisper"){
  1253. Text("@whisper=","Enter what the NPC will whisper","");
  1254. }
  1255. else if (message == "@shout"){
  1256. Text("@shout=","Enter what the NPC will shout","");
  1257. }
  1258. else if (message == "@wander") {
  1259. Text("@wander=","Enter radius to wander","Enter number of wanders");
  1260. }
  1261. else if (message == "@pause") {
  1262. Text("@pause=","Enter time to pause","");
  1263. }
  1264. else if (message == "@rotate") {
  1265. Text("@rotate=","Enter degrees to rotate","");
  1266. }
  1267. else if (message == "@sit"){
  1268. Text("@sit=","Enter name of object to sit on","");
  1269. }
  1270. else if (message == "@teleport"){
  1271. lCommands += "@teleport=" + Pos() + "\n";
  1272. llOwnerSay("teleport to position: " + Pos());
  1273. makeMenu(lMenu3);
  1274. }
  1275. else if (message == "@touch"){
  1276. Text("@touch=","Enter name of object to touch","");
  1277. }
  1278. else if (message == "@cmd"){
  1279. Text("@cmd=","Enter cjhannel to speak on","Enter text to speak");
  1280. }
  1281. else if (message == "@stand"){
  1282. lCommands += "@stand\n";
  1283. llOwnerSay("Stand Recorded");
  1284. makeMenu(lAtButtons);
  1285. }
  1286. else if (message == "@animate"){
  1287. Text("@animate=","Enter animation name to play","Enter time to play the animation");
  1288. }
  1289. else if (message == "@attach"){
  1290. Text("@animate=","Enter inventory name to attach","Enter number of the attachment point (1-40)");
  1291. }
  1292. else if (message == "@speed"){
  1293. Text("@speed=","Enter a speed for the NPC, 1=100% normal speed, 0.5=50% speed","");
  1294. }
  1295. // Save NPC name
  1296. else if (MakeNotecard == STATE) {
  1297. sNPCName = message; // in case we need to kill it.
  1298. vector vDest = (vector) Pos();
  1299. if (relAbs == "Relative")
  1300. {
  1301. vDest -= llGetPos(); // just an offset for relative
  1302. }
  1303. sNotecard = "@spawn=" + message + "|" + (string) vDest + "\n";
  1304. integer i;
  1305. integer j = llGetListLength(lCommands);
  1306. for (; i < j; i++){
  1307. // get the command to save to the notecard
  1308. string line = llList2String(lCommands,i);
  1309. if (relAbs == "Absolute") {
  1310. sNotecard += line; // add the un-modified string to the notecard
  1311. } else {
  1312. // since we have to record absolute coords since we do not know where the box goes until they press Save,
  1313. // we process the absolute to relative conversion for walks here
  1314. list parts = llParseString2List(line,["="],[]); //get the @command
  1315. if (llList2String(parts,0) == "@walk") {
  1316. vector vec = (vector) llList2String(parts,1) - llGetPos();
  1317. sNotecard += "@walk=" + (string) vec + "\n";
  1318. }
  1319. else if (llList2String(parts,0) == "@fly") {
  1320. vector vec = (vector) llList2String(parts,1) - llGetPos();
  1321. sNotecard += "@fly=" + (string) vec + "\n";
  1322. }
  1323. else if (llList2String(parts,0) == "@run") {
  1324. vector vec = (vector) llList2String(parts,1) - llGetPos();
  1325. sNotecard += "@run=" + (string) vec + "\n";
  1326. }
  1327. else if (llList2String(parts,0) == "@land") {
  1328. vector vec = (vector) llList2String(parts,1) - llGetPos();
  1329. sNotecard += "@land=" + (string) vec + "\n";
  1330. }
  1331. else {
  1332. sNotecard += line; // add the un-modified string to the notecard
  1333. }
  1334. }
  1335. }
  1336. llRemoveInventory(Notecard); // delete the old notecard
  1337. osMakeNotecard(Notecard,sNotecard); // Makes the notecard.
  1338. llSay(0,sNotecard);
  1339. llOwnerSay("Commands notecard has been written");
  1340. STATE = 0;
  1341. } // MakeNotecard
  1342. else if (! llStringLength(sParam2)) {
  1343. lCommands += sCommand + message + "\n";
  1344. llOwnerSay("Recorded");
  1345. makeMenu(lAtButtons);
  1346. }
  1347. else if (llStringLength(sParam2)){
  1348. sCommand = sCommand + message + "|";
  1349. llOwnerSay("Recorded");
  1350. makeText(sParam2);
  1351. sParam2 = "";
  1352. }
  1353. }
  1354. timer(){
  1355. // DEBUG("tick");
  1356. // if llDialog is up, kill the listener for the dialog box.
  1357. if (iHandle) {
  1358. llOwnerSay("Menu timed out");
  1359. llListenRemove(iHandle);
  1360. iHandle = 0;
  1361. return; // ^^^^^^^^^^^^^^^^^^^^^^^
  1362. }
  1363. // if NoBodyHome, we are sensing for an avatar
  1364. else if (NobodyHome == STATE) {
  1365. ProcessSensor();
  1366. return;
  1367. }
  1368. // if we are spawning, we need time to rez the NPC, then start processing NPC Commands.
  1369. else if (Spawning == STATE) {
  1370. STATE = 0;
  1371. TimerEvent(TIMER);
  1372. }
  1373. // We end aniamtions with a timer
  1374. else if (Animate == STATE){
  1375. NPCAnimate(STAND);
  1376. TimerEvent(TIMER);
  1377. }
  1378. else if (Walking == STATE) {
  1379. if (--iWaitCounter) {
  1380. if (llVecDist(osNpcGetPos(NPCKey()), newDest) > MAXDIST) {
  1381. return;
  1382. }
  1383. }
  1384. DEBUG("At Destination: " + (string) newDest);
  1385. // walk, fly, run, land
  1386. if (walkstate == 1) {
  1387. NPCAnimate(STAND);
  1388. NPCWalkOption = OS_NPC_NO_FLY;
  1389. } else if (walkstate == 2) {
  1390. // nothing
  1391. } else if (walkstate == 3) {
  1392. NPCAnimate(STAND);
  1393. NPCWalkOption = OS_NPC_NO_FLY;
  1394. } else if (walkstate == 4) {
  1395. llShout(FLIGHT,"landing");
  1396. NPCAnimate(STAND);
  1397. NPCWalkOption = OS_NPC_NO_FLY;
  1398. }
  1399. }
  1400. // Wandering timer
  1401. else if (Wander == STATE) {
  1402. if (--iWaitCounter) { // wait 60 seconds to get to a destination.
  1403. if (llVecDist(osNpcGetPos(NPCKey()), vWanderPos) > MAXDIST)
  1404. return;
  1405. }
  1406. // see if wander counter == 0, if so, stop walking, go to stand and process next line
  1407. if(RAMwc == 0) {
  1408. NPCAnimate(STAND);
  1409. DEBUG("Wander ended, calling DoProcessNPCLine");
  1410. STATE = 0;
  1411. DoProcessNPCLine();
  1412. return;
  1413. }
  1414. // one less time to wander around
  1415. RAMwc--;
  1416. NPCAnimate(STAND);
  1417. TimerEvent(TIMER);
  1418. StateWanderhold();
  1419. return;
  1420. }
  1421. // Wandering requires us to re-wander when we reach a destination
  1422. else if (WanderHold == STATE) {
  1423. StateWander();
  1424. TimerEvent(TIMER);
  1425. return;
  1426. }
  1427. else if (DoProcess == STATE) {
  1428. TimerEvent(QUICK);
  1429. }
  1430. STATE = 0;
  1431. // We always process a NPC line at end of timer.
  1432. DEBUG("Tick end, calling DoProcessNPCLine");
  1433. DoProcessNPCLine();
  1434. }
  1435. // sensors are used for sitting on prims
  1436. // Neo Cortex: added different SensorFunc states to trigger sit or touch
  1437. sensor(integer num) {
  1438. if (SensorFunc == 1) {
  1439. osNpcSit(NPCKey(), llDetectedKey(0), OS_NPC_SIT_NOW);
  1440. DEBUG("Seated, calling DoProcessNPCLine");
  1441. SensorFunc = 0;
  1442. } else if (SensorFunc == 2) {
  1443. osNpcTouch(NPCKey(), llDetectedKey(0), LINK_THIS);
  1444. DEBUG("Touched, calling DoProcessNPCLine");
  1445. SensorFunc = 0;
  1446. }
  1447. DoProcessNPCLine();
  1448. }
  1449. no_sensor(){
  1450. DEBUG ("no target prim located, calling DoProcessNPCLine");
  1451. SensorFunc = 0;
  1452. DoProcessNPCLine();
  1453. }
  1454. link_message(integer sender, integer num, string str, key id){
  1455. ParseMsg(str);
  1456. }
  1457. }