Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1.  
  2.  
  3. float last_alt;
  4. float last_time;
  5. float last_move;
  6. float boost;
  7. integer controls;
  8.  
  9. float MIN_SPEED = 2.0;
  10. float WANT_SPEED = 20.0;
  11. float MAX_SPEED = 25.0;
  12. float MIN_TIME = 1.0;
  13. float DEFAULT_BOOST = 0.5;
  14.  
  15. float FAST_TICK = 0.1;
  16. float SLOW_TICK = 1.0;
  17. float LONG_TIME = 5.0; // reset boost if no work in a long time
  18.  
  19. float MIN_BOOST_HEIGHT = 72.0;
  20. float MIN_BOOST_CLEARANCE = 36.0;
  21.  
  22. integer flying = -1;
  23. integer falling = -1;
  24. integer hovering = -1;
  25. integer dimmed = -1;
  26.  
  27. integer HUD_OFF = 0;
  28. integer HUD_ALWAYS = -1;
  29. integer HUD_ON = 1;
  30. integer hud_state = 1; // default HUD_ON
  31.  
  32. string last_hud_text;
  33. integer hud_active = 0;
  34. float last_hud_time = 0;
  35. show_hud()
  36. {
  37. if(hud_state == HUD_ALWAYS)
  38. hud_active = 1;
  39. else if(hud_state == HUD_ON)
  40. {
  41. if(hud_active >= 0 && flying == 0 && hovering == 0 && falling == 0) hud_active = 0;
  42. else if(hud_active <= 0 && (flying > 0 || hovering > 0 || falling > 0)) hud_active = 1;
  43. }
  44. else if(hud_active == 1)
  45. hud_active = 0;
  46.  
  47. if(hud_active == 1) {
  48. float time = llGetTime();
  49. if(last_hud_time > time - 1) return;
  50. vector v = llGetVel();
  51. vector p = llGetPos();
  52. string hover_text = "";
  53. if(hovering && controls == 0)
  54. hover_text = " (hover)";
  55. string boost_text = "Standby";
  56. if(!hovering)
  57. boost_text = "Passive";
  58. if(falling)
  59. boost_text = "Inactive";
  60. if(boost > 0 && controls > 0)
  61. boost_text = "Boost "+((string)llFloor(boost * 100.0 / 6.0 + 0.5))+"%";
  62. string hud_text = "";
  63. if(hud_state == HUD_ALWAYS)
  64. hud_text = "\nHUD locked";
  65.  
  66. string t =
  67. "<"+((string)llFloor(p.x+0.5))+","+((string)llFloor(p.y+0.5))+","+((string)llFloor(p.z+0.5))+">"+
  68. " ("+((string)(llFloor(p.z-llGround(<0,0,0>)+0.5)))+"m AGL)\n"+
  69. "<"+((string)llFloor(v.x+0.5))+","+((string)llFloor(v.y+0.5))+","+((string)llFloor(v.z+0.5))+">"+
  70. " ("+((string)llFloor(llVecMag(v)))+"m/s)\n"+
  71. boost_text+hover_text+hud_text;
  72. if(last_hud_text != t) {
  73. vector color = <1,1,1>;
  74. if(hud_state == HUD_ALWAYS)
  75. color = <.3,1,.3>;
  76. // llSetText(t, color, 1);
  77. last_hud_text = t;
  78. last_hud_time = time;
  79. }
  80. } else if(hud_active == 0) {
  81. last_hud_text = "";
  82. last_hud_time = 0;
  83. // llSetText("", ZERO_VECTOR, 0);
  84. hud_active = -1;
  85. }
  86. }
  87. init2(){ llReleaseControls();}
  88.  
  89. set_hover(integer active)
  90. {
  91. if(active == hovering) return;
  92. hovering = active;
  93. if(hovering)
  94. llSetForce(<0,0,9.8> * llGetMass(), FALSE);
  95. else
  96. llSetForce(<0,0,0>, FALSE);
  97. }
  98.  
  99. float last_tick = -1;
  100. set_tick(float tick)
  101. {
  102. if(tick == last_tick) return;
  103. last_tick = tick;
  104. llSetTimerEvent(tick);
  105. }
  106.  
  107. float last_boost_height;
  108. float average_boost;
  109. check_boost()
  110. {
  111. flying = 1;
  112. falling = 0;
  113.  
  114. integer info = llGetAgentInfo(llGetOwner());
  115.  
  116. if((info & AGENT_FLYING) == 0)
  117. {
  118. set_hover(FALSE);
  119. falling = (info & AGENT_IN_AIR) != 0;
  120. flying = 0;
  121. boost = 0;
  122. set_tick(SLOW_TICK);
  123. return;
  124. }
  125.  
  126. vector pos = llGetPos();
  127. if(pos.z < last_boost_height / 2) // trim running average of boost if big altitude drop
  128. average_boost = average_boost * (pos.z / last_boost_height)
  129. + DEFAULT_BOOST * (1.0 - pos.z / last_boost_height);
  130.  
  131. if(pos.z < MIN_BOOST_HEIGHT || pos.z - llGround(<0,0,0>) < MIN_BOOST_CLEARANCE)
  132. {
  133. set_hover(FALSE);
  134. set_tick(SLOW_TICK);
  135. boost = 0;
  136. return;
  137. }
  138.  
  139. set_hover(TRUE);
  140.  
  141. if(controls <= 0) return;
  142.  
  143. vector vel = llGetVel();
  144. float time = llGetTime();
  145. float speed = vel.z;
  146. float target = WANT_SPEED;
  147. float window = WANT_SPEED / 20;
  148.  
  149. if(speed > 0)
  150. last_move = time;
  151.  
  152. if(time - last_time >= LONG_TIME)
  153. boost = 0;
  154. else
  155. {
  156. if(speed < target - window)
  157. {
  158. if(boost == 0)
  159. boost = average_boost;
  160. if(time - last_move > MIN_TIME) boost += 0.4;
  161. else if(speed < target * 0.25) boost += 0.2;
  162. else if(speed < target * 0.5 ) boost += 0.1;
  163. else if(speed < target * 0.75) boost += 0.05;
  164. else if(speed < target - window * 4) boost += 0.02;
  165. else boost += 0.01;
  166. }
  167. else if(speed > MAX_SPEED) boost -= 0.5;
  168. else if(speed > target + window * 4) boost -= 0.1;
  169. else if(speed > target + window * 2) boost -= 0.03;
  170. else if(speed > target + window) boost -= 0.01;
  171.  
  172. if(boost <= 0)
  173. boost = 0;
  174. if(boost > 0) {
  175. llApplyImpulse(<0,0,boost> * llGetMass(), FALSE);
  176. average_boost = average_boost * 0.9 + boost * 0.1; // 10 sample running average
  177. last_boost_height = pos.z;
  178. }
  179. }
  180.  
  181. if(boost) set_tick(FAST_TICK);
  182. else set_tick(SLOW_TICK);
  183. last_alt = pos.z;
  184. last_time = time;
  185. }
  186.  
  187. take_controls()
  188. {
  189. llTakeControls(CONTROL_UP|CONTROL_DOWN,TRUE,TRUE);
  190. }
  191.  
  192. request_perms()
  193. {
  194. if(llGetPermissions() & PERMISSION_TAKE_CONTROLS)
  195. take_controls();
  196. else
  197. llRequestPermissions(llGetOwner(), PERMISSION_TAKE_CONTROLS);
  198. }
  199.  
  200. init()
  201. {
  202. boost = 0;
  203.  
  204. flying = -1;
  205. falling = -1;
  206. hovering = -1;
  207. dimmed = -1;
  208. hud_state = HUD_ON;
  209.  
  210. set_tick(SLOW_TICK);
  211. if(llGetAttached()) request_perms();
  212. }
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219. key uuid;integer on = FALSE;
  220.  
  221.  
  222.  
  223.  
  224.  
  225. integer knob=0;
  226.  
  227.  
  228.  
  229. key Owner;
  230.  
  231. default
  232. {
  233. state_entry()
  234. {
  235. Owner = llGetOwner();
  236. }
  237.  
  238.  
  239.  
  240.  
  241.  
  242. run_time_permissions(integer mask)
  243. {
  244. if(mask & PERMISSION_TAKE_CONTROLS) take_controls();
  245.  
  246.  
  247. }control(key id, integer level, integer edge)
  248. {
  249. controls = 0;
  250. if(level & CONTROL_UP) controls++;
  251. if(level & CONTROL_DOWN) controls--;
  252. check_boost();
  253. }
  254.  
  255. timer()
  256. {
  257. check_boost();
  258. show_hud();
  259. if(flying && boost > 0)
  260. {
  261. if(dimmed != 0)
  262.  
  263. dimmed = 0;
  264. }
  265. else
  266. {
  267. integer dimness = 4;
  268. if(hud_state == HUD_ON) dimness = 5;
  269. else if(hud_state == HUD_ALWAYS) dimness = 6;
  270. if(dimmed != dimness) {
  271. float alpha = ((float)dimness) / 10;
  272.  
  273. }
  274. dimmed = dimness;
  275. }
  276. }
  277. touch_start(integer det)
  278. {
  279. key id = llDetectedKey(0);
  280. if(id == Owner)
  281. {
  282. if(knob==0)
  283. {
  284.  
  285.  
  286. init();
  287.  
  288.  
  289. knob=1;llOwnerSay ("Im activating flight mode");
  290.  
  291. }else if(knob==1)
  292. {knob=0; init2();
  293.  
  294. llOwnerSay("flight mode off :");knob=0;
  295.  
  296. }}}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement