NPC Rev 4.lsl 57 KB

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