Everywhere NPV - by Crooked Shim

By: bdhtrn on Oct 10th, 2011  |  syntax: Linden Scripting  |  size: 7.87 KB  |  hits: 82  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. /*
  2. Everywhere NPV - Open Source Edition
  3. Copyright (C) 2010 Steven Lawson / Crooked Shim - crookedshim@gmail.com
  4.  
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. Please see <http: //www.gnu.org/licenses/> for a copy of the
  16. GNU General Public License.
  17. */
  18.  
  19. vector LLSITTARGET_CORRECTION = <0.0, 0.0, -0.35>;
  20. vector SIT_TARGET = <0.0, 0.0, 0.1>;
  21.  
  22. integer gUsedYetByOwner = TRUE;
  23.  
  24. string NORMAL_ANIMATION_NAME = "hover";
  25. integer gCurrentAnimation;
  26. integer RUN_NORMAL_ANIM = -0xAA00A;
  27. integer STOP_ANIMS = -0xAA00C;
  28.  
  29. start_animation(integer anim_mode)
  30. {
  31.     if (llGetPermissions() & PERMISSION_TRIGGER_ANIMATION)
  32.     {
  33.         list animations = llGetAnimationList(llGetPermissionsKey());
  34.         integer cur_anim = (animations != []) - 1;
  35.         while (cur_anim >= 0) llStopAnimation(llList2Key(animations, cur_anim--));
  36.  
  37.         gCurrentAnimation = anim_mode;
  38.  
  39.         if (anim_mode == STOP_ANIMS) return;
  40.         else if (anim_mode == RUN_NORMAL_ANIM) llStartAnimation(NORMAL_ANIMATION_NAME);
  41.     }
  42. }
  43.  
  44. vector gMovementDirection = ZERO_VECTOR;
  45. integer gRotationDirection = 0;
  46.  
  47. float gMovementSpeed = 15.0;
  48. float gRotationSpeed = 0.8;
  49. float gAcceleration = 1.0;
  50. float gMaxSpeed = 50.0;
  51.  
  52. integer gSwitchingSims = FALSE;
  53.  
  54. integer TIMER_NOT_RUNNING = FALSE;
  55. integer TIMER_NORMAL_MOVEMENT = 1;
  56. integer gTimerMode = TIMER_NOT_RUNNING;
  57.  
  58. //pragma inline
  59. start_and_fire_timer(float interval, integer timer_mode)
  60. {
  61.     gTimerMode = timer_mode;
  62.     llResetTime();
  63.     llSetTimerEvent(interval);
  64.     run_timer();
  65. }
  66. //pragma inline
  67. stop_timer()
  68. {
  69.     llSetTimerEvent(0.0);
  70.     gTimerMode = TIMER_NOT_RUNNING;
  71. }
  72. run_timer()
  73. {
  74.     float this_speed = gMovementSpeed + gAcceleration * llPow(llGetTime(), 2.0);
  75.     if (this_speed > gMaxSpeed) this_speed = gMaxSpeed;
  76.  
  77.     gPointerPos += gMovementDirection * this_speed * gMaxMovementClock * gPointerRot;
  78.     gPointerRot = llEuler2Rot(llRot2Euler(gPointerRot) + <0.0, 0.0, gRotationDirection * gRotationSpeed * gMaxMovementClock * PI>);
  79.  
  80.     request_movement(gPointerPos, gPointerRot);
  81. }
  82.  
  83. //pragma inline
  84. safe_posJump_addit_noDelay(vector target_pos, list addit)
  85. {
  86.     vector start_pos = llGetPos();
  87.     llSetLinkPrimitiveParamsFast(LINK_THIS, [PRIM_POSITION, <1.304382E+19, 1.304382E+19, 0.0>, PRIM_POSITION, target_pos, PRIM_POSITION, start_pos, PRIM_POSITION, target_pos] + addit);
  88. }
  89.  
  90. integer gWasOutOfSim = FALSE;
  91. //pragma inline
  92. request_movement(vector target_pos, rotation target_rot)
  93. {
  94.     vector cropped = crop_vector(target_pos, TRUE);
  95.  
  96.     if (cropped != target_pos)
  97.     {
  98.         vector move_target = target_pos - llGetPos();
  99.         if (!gWasOutOfSim)
  100.         {
  101.             if (!llEdgeOfWorld(llGetPos(), llVecNorm(move_target)))
  102.             {
  103.                 if (crop_vector(target_pos, FALSE) != target_pos)
  104.                 {
  105.                     llOwnerSay("Attempting to switch sims. Deactivating movement engine.");
  106.                     gSwitchingSims = TRUE;
  107.                     stop_timer();
  108.                     safe_posJump_addit_noDelay(target_pos + llVecNorm(move_target) * gMovementSpeed * 1.01, []);
  109.                     llSleep(3.0);
  110.                     llOwnerSay("Reactivating movement engine.");
  111.                     gSwitchingSims = FALSE;
  112.                     return;
  113.                 }
  114.             }
  115.         }
  116.         safe_posJump_addit_noDelay(cropped, [PRIM_ROTATION, ZERO_ROTATION]);
  117.         if (llVecMag(move_target) > 53.0) move_target = llVecNorm(move_target) * 53.0;
  118.         llSetLinkPrimitiveParamsFast(llGetNumberOfPrims(), [PRIM_POSITION, move_target, PRIM_ROTATION, target_rot]);
  119.         gWasOutOfSim = TRUE;
  120.     }
  121.     else
  122.     {
  123.         if (gWasOutOfSim)
  124.         {
  125.             gWasOutOfSim = FALSE;
  126.             llSetLinkPrimitiveParamsFast(llGetNumberOfPrims(), [PRIM_POSITION, SIT_TARGET, PRIM_ROTATION, ZERO_ROTATION]);
  127.         }
  128.         safe_posJump_addit_noDelay(cropped, [PRIM_ROTATION, target_rot]);
  129.     }
  130. }
  131.  
  132. //pragma inline
  133. vector crop_vector(vector a, integer checkvert)
  134. {
  135.     if (a.x > 255.99) a.x = 255.99;
  136.     else if (a.x < 0.1) a.x = 0.1;
  137.     if (a.y > 255.99) a.y = 255.99;
  138.     else if (a.y < 0.1) a.y = 0.1;
  139.     if (checkvert)
  140.     {
  141.         if (a.z > 4095.99) a.z = 4095.99;
  142.         float ground_height = llGround(a - llGetPos());
  143.         if (a.z < ground_height) a.z = ground_height;
  144.     }
  145.     return a;
  146. }
  147. integer is_at_edge(vector a)
  148. {
  149.     return a.x < 1 || a.x > 255 || a.y < 1 || a.y > 255;
  150. }
  151.  
  152. vector gPointerPos;
  153. rotation gPointerRot;
  154.  
  155. float gMaxMovementClock = 0.04;
  156.  
  157. default
  158. {
  159.     state_entry()
  160.     {
  161.         llVolumeDetect(TRUE);
  162.  
  163.         llSetSitText("Activate");
  164.         llSetClickAction(CLICK_ACTION_SIT);
  165.  
  166.         vector owner_size = llGetAgentSize(llGetOwner());
  167.         llSitTarget(SIT_TARGET + LLSITTARGET_CORRECTION, ZERO_ROTATION);
  168.  
  169.     }
  170.     changed(integer change)
  171.     {
  172.         if (change & CHANGED_LINK)
  173.         {
  174.             if (llAvatarOnSitTarget() == llGetOwner())
  175.             {
  176.                 gUsedYetByOwner = TRUE;
  177.                 llRequestPermissions(llGetOwner(), PERMISSION_TAKE_CONTROLS | PERMISSION_TRIGGER_ANIMATION | PERMISSION_CONTROL_CAMERA);
  178.                 llSetAlpha(0.0, ALL_SIDES);
  179.             }
  180.             else if (llAvatarOnSitTarget() != NULL_KEY)
  181.             {
  182.                 llUnSit(llAvatarOnSitTarget());
  183.             }
  184.             else if (llAvatarOnSitTarget() == NULL_KEY && gUsedYetByOwner)
  185.             {
  186.                 start_animation(STOP_ANIMS);
  187.                 llDie();
  188.             }
  189.         }
  190.     }
  191.     run_time_permissions(integer permissions)
  192.     {
  193.         if (permissions & PERMISSION_TRIGGER_ANIMATION)
  194.         {
  195.             start_animation(RUN_NORMAL_ANIM);
  196.         }
  197.         if (permissions & PERMISSION_TAKE_CONTROLS)
  198.         {
  199.             llTakeControls(CONTROL_BACK | CONTROL_FWD | CONTROL_LEFT | CONTROL_RIGHT | CONTROL_ROT_LEFT | CONTROL_ROT_RIGHT | CONTROL_DOWN | CONTROL_UP, TRUE, FALSE);
  200.         }
  201.         if (permissions & PERMISSION_CONTROL_CAMERA)
  202.         {
  203.             llSetCameraParams([CAMERA_ACTIVE,TRUE,CAMERA_BEHINDNESS_ANGLE,5.0,CAMERA_BEHINDNESS_LAG,0.1,CAMERA_DISTANCE,7.0,CAMERA_FOCUS_LAG,0.1,CAMERA_FOCUS_OFFSET,<0.0,0.0,1.0>,CAMERA_FOCUS_THRESHOLD,0.5,CAMERA_PITCH,5.0]);
  204.         }
  205.     }
  206.     control(key name, integer levels, integer edges)
  207.     {
  208.         if (gSwitchingSims) return;
  209.  
  210.         vector movementDirection = ZERO_VECTOR;
  211.         integer rotationDirection = 0;
  212.  
  213.         if (levels & CONTROL_BACK) movementDirection.x--;
  214.         else if (levels & CONTROL_FWD) movementDirection.x++;
  215.  
  216.         if (levels & CONTROL_DOWN) movementDirection.z--;
  217.         else if (levels & CONTROL_UP) movementDirection.z++;
  218.  
  219.         if (levels & CONTROL_LEFT) movementDirection.y++;
  220.         else if (levels & CONTROL_RIGHT) movementDirection.y--;
  221.  
  222.         if (levels & CONTROL_ROT_LEFT) rotationDirection++;
  223.         else if (levels & CONTROL_ROT_RIGHT) rotationDirection--;
  224.  
  225.         gMovementDirection = movementDirection;
  226.         gRotationDirection = rotationDirection;
  227.  
  228.         if (levels && gTimerMode != TIMER_NORMAL_MOVEMENT)
  229.         {
  230.             list od = llGetObjectDetails(llGetOwner(), [OBJECT_POS, OBJECT_ROT]);
  231.  
  232.             gPointerPos = llList2Vector(od, 0);
  233.             gPointerRot = llList2Rot(od, 1);
  234.  
  235.             start_and_fire_timer(gMaxMovementClock, TIMER_NORMAL_MOVEMENT);
  236.         }
  237.         else if (!levels) stop_timer();
  238.     }
  239.     timer()
  240.     {
  241.         run_timer();
  242.     }
  243. }
  244.