NPC Controller - All In One Rev 4.0.lsl 56 KB

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