NPC Custom for Namaka.lsl 59 KB

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