Advertisement
salahzar

Alter Ego NPC

Dec 21st, 2014
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ///////////////////////////////////////////////////////////////////////////
  2. //  Copyright (C) Wizardry and Steamworks 2012 - License: GNU GPLv3      //
  3. //  Please see: http://www.gnu.org/licenses/gpl.html for legal details,  //
  4. //  rights of fair usage, the disclaimer and warranty conditions.        //
  5. ///////////////////////////////////////////////////////////////////////////
  6.  
  7. //////////////////////////////////////////////////////////
  8. //                  CONFIGURATION                       //
  9. //////////////////////////////////////////////////////////
  10.  
  11. // Specifies the radius of the circle in which the NPC
  12. // will travel in.
  13. float MOVEMENT_RANGE = 5.0;
  14. // How often to check that the NPC has reached a
  15. // waypoint.
  16. float POLL_TIME = 1;
  17. // Set this to the name of the animation for the walk
  18. // equence. This animation has to be placed in the
  19. // same primitive as this script.
  20. string ANIMATION_WALK="Walk";
  21. // Set this to the name of the animation for the stand
  22. // sequence. This animation has to be placed in the
  23. // same primitive as this script.
  24. string ANIMATION_STAND="Stand";
  25. // How much time, in seconds, does a standing animation
  26. // cycle take?
  27. float STANDING_ANIMATION_CYCLE_TIME = 20;
  28. // How many cycles to wait, randomly?
  29. integer STANDING_ANIMATION_CYCLES = 3;
  30.  
  31. ///////////////////////////////////////////////////////////////////////////
  32. //                              INTERNALS                                //
  33. ///////////////////////////////////////////////////////////////////////////
  34.  
  35. vector wasCirclePoint(float radius) {
  36.     float x = llPow(-1, 1 + (integer) llFrand(2)) * llFrand(radius*2);
  37.     float y = llPow(-1, 1 + (integer) llFrand(2)) * llFrand(radius*2);
  38.     if(llPow(x,2) + llPow(y,2) <= llPow(radius,2))
  39.         return <x, y, 0>;
  40.     return wasCirclePoint(radius);
  41. }
  42.  
  43. // Vector that will be filled by the script with
  44. // the initial starting position in region coordinates.
  45. vector iPos = ZERO_VECTOR;
  46. // Storage for destination position.
  47. vector dPos = ZERO_VECTOR;
  48. // Key of the NPC
  49. key npcKey = NULL_KEY;
  50.  
  51. default {
  52.     state_entry() {
  53.         llSetStatus(STATUS_PHANTOM, TRUE);
  54.         osNpcRemove(llGetObjectDesc());
  55.         iPos = llGetPos();
  56.         osAgentSaveAppearance(llGetOwner(), "appearance");
  57.         npcKey = osNpcCreate("Alter", "Ego", iPos, "appearance");
  58.         llSetObjectDesc(npcKey);
  59.         // It seems that 1 second is a magic wait-time that must
  60.         // separate the osNpcCreate from other commands or else
  61.         // the NPC rezzes as a cloud.
  62.         llSetTimerEvent(1);
  63.     }
  64.     timer() {
  65.         llSetTimerEvent(0);
  66.         llSay(0,"load appearance");
  67.         osNpcLoadAppearance(npcKey, "appearance");
  68.         state wander;
  69.     }
  70. }
  71.  
  72. state wander
  73. {
  74.     state_entry() {
  75.         llSay(0,"wandering");
  76.         dPos = iPos + wasCirclePoint(MOVEMENT_RANGE);
  77.         osNpcPlayAnimation(npcKey, ANIMATION_WALK);
  78.         osNpcMoveToTarget(npcKey, dPos, OS_NPC_NO_FLY);
  79.         llSetTimerEvent(POLL_TIME);
  80.     }
  81.  
  82.     timer() {
  83.         // Another magic value (2), it seems that even on flat ground
  84.     // the NPC never really reaches the destination position.
  85.     // Instead, it reaches some point that is always (experimentally)
  86.     // smaller than 2 meters. Hence the comparison.
  87.         if (llVecDist(osNpcGetPos(npcKey), dPos) > 2) return;
  88.         osNpcStopAnimation(npcKey, ANIMATION_WALK);
  89.         osNpcPlayAnimation(npcKey, ANIMATION_STAND);
  90.         llSetTimerEvent(0);
  91.         state wait;
  92.     }
  93. }
  94.  
  95. state wait {
  96.     state_entry() {
  97.         llSetTimerEvent(STANDING_ANIMATION_CYCLE_TIME * 1+llFrand(STANDING_ANIMATION_CYCLES-1));
  98.     }
  99.     timer() {
  100.         llSetTimerEvent(0);
  101.         osNpcStopAnimation(npcKey, ANIMATION_STAND);
  102.         state wander;
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement