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

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