Advertisement
adri1

Map Mover 1.0 by adri1

Apr 13th, 2014
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.47 KB | None | 0 0
  1. // Map Mover 1.0 by adri1
  2.  
  3. #define MAX_MAPMOVER_OBJECTS (500)
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10. //-------------------------------------------------
  11. //
  12. // This is an example of using the AttachCameraToObject function
  13. // to create a no-clip flying camera.
  14. //
  15. // h02 2012
  16. //
  17. // SA-MP 0.3e and above
  18. //
  19. //-------------------------------------------------
  20.  
  21. #include <a_samp>
  22. #include <sscanf2>
  23. // Players Move Speed
  24. #define MOVE_SPEED 100.0
  25. #define ACCEL_RATE 0.03
  26.  
  27. // Players Mode
  28. #define CAMERA_MODE_NONE 0
  29. #define CAMERA_MODE_FLY 1
  30.  
  31. // Key state definitions
  32. #define MOVE_FORWARD 1
  33. #define MOVE_BACK 2
  34. #define MOVE_LEFT 3
  35. #define MOVE_RIGHT 4
  36. #define MOVE_FORWARD_LEFT 5
  37. #define MOVE_FORWARD_RIGHT 6
  38. #define MOVE_BACK_LEFT 7
  39. #define MOVE_BACK_RIGHT 8
  40.  
  41. // Enumeration for storing data about the player
  42. enum noclipenum
  43. {
  44. cameramode,
  45. flyobject,
  46. mode,
  47. lrold,
  48. udold,
  49. lastmove,
  50. Float:accelmul
  51. }
  52. new noclipdata[MAX_PLAYERS][noclipenum];
  53.  
  54. //--------------------------------------------------
  55.  
  56. public OnFilterScriptInit()
  57. {
  58. return 1;
  59. }
  60.  
  61. public OnFilterScriptExit()
  62. {
  63. // If any players are still in edit mode, boot them out before the filterscript unloads
  64. for(new x; x<MAX_PLAYERS; x++)
  65. {
  66. if(noclipdata[x][cameramode] == CAMERA_MODE_FLY) CancelFlyMode(x);
  67. }
  68. return 1;
  69. }
  70.  
  71. //--------------------------------------------------
  72.  
  73. public OnPlayerConnect(playerid)
  74. {
  75. // Reset the data belonging to this player slot
  76. noclipdata[playerid][cameramode] = CAMERA_MODE_NONE;
  77. noclipdata[playerid][lrold] = 0;
  78. noclipdata[playerid][udold] = 0;
  79. noclipdata[playerid][mode] = 0;
  80. noclipdata[playerid][lastmove] = 0;
  81. noclipdata[playerid][accelmul] = 0.0;
  82. return 1;
  83. }
  84.  
  85.  
  86. /* CODE */
  87.  
  88.  
  89. new Object_String[512];
  90. new Player_MovingMap[MAX_PLAYERS];
  91. new Player_MovingObjects[MAX_PLAYERS];
  92. new ObjectCenter;
  93. new mapnamefile[24];
  94. enum OBJECT_INFORMATION
  95. {
  96. ModelID,
  97. Float:PositionX,
  98. Float:PositionY,
  99. Float:PositionZ,
  100. Float:RotationX,
  101. Float:RotationY,
  102. Float:RotationZ,
  103. Float:OffSetX,
  104. Float:OffSetY,
  105. Float:OffSetZ,
  106. ObjectID
  107. };
  108. new MAPMOVER_OBJECTS[MAX_MAPMOVER_OBJECTS][OBJECT_INFORMATION];
  109.  
  110. stock LoadMap(playerid, mapname[])
  111. {
  112. if(Player_MovingMap[playerid] == 0) return 1;
  113. new File:Handler = fopen(mapname, io_read);
  114. if(!Handler) return ShowPlayerDialog(playerid, 801, DIALOG_STYLE_MSGBOX, "Map Mover", "\n\n\tEl mapa no se ha podido cargar:\n\tNo se encuentra el archivo.\n\n", "OK", "");
  115. new Index;
  116. while(fread(Handler, Object_String))
  117. {
  118. StripNewLine(Object_String);
  119. if(strfind(Object_String, "CreateObject(", true) != -1) strdel(Object_String, 0, 13);
  120. else if(strfind(Object_String, "CreateDynamicObject(", true) != -1) strdel(Object_String, 0, 20);
  121. if(strfind(Object_String, ");", true) != -1)
  122. {
  123. new start = strfind(Object_String, ");", true);
  124. strdel(Object_String, start, 500);
  125. }
  126. if(!sscanf(Object_String, "p<,>dffffff",
  127. MAPMOVER_OBJECTS[Index][ModelID],
  128. MAPMOVER_OBJECTS[Index][PositionX], MAPMOVER_OBJECTS[Index][PositionY], MAPMOVER_OBJECTS[Index][PositionZ],
  129. MAPMOVER_OBJECTS[Index][RotationX], MAPMOVER_OBJECTS[Index][RotationY], MAPMOVER_OBJECTS[Index][RotationZ]))
  130. {
  131.  
  132. new objectid = CreateObject(MAPMOVER_OBJECTS[Index][ModelID],
  133. MAPMOVER_OBJECTS[Index][PositionX], MAPMOVER_OBJECTS[Index][PositionY], MAPMOVER_OBJECTS[Index][PositionZ],
  134. MAPMOVER_OBJECTS[Index][RotationX], MAPMOVER_OBJECTS[Index][RotationY], MAPMOVER_OBJECTS[Index][RotationZ]);
  135. MAPMOVER_OBJECTS[Index][ObjectID] = objectid;
  136. Index++;
  137. }
  138. }
  139. fclose(Handler);
  140.  
  141. if(Index == 0) ShowPlayerDialog(playerid, 801, DIALOG_STYLE_MSGBOX, "Map Mover", "\n\n\tNo se encuentra ningún mapa en ese archivo.\n\n", "OK", "");
  142. else
  143. {
  144. ShowPlayerDialog(playerid, 802, DIALOG_STYLE_MSGBOX, "Map Mover", "\n\n\tMapa cargado correctamente.\n\n", "OK", "");
  145. format(mapnamefile, 24, "%s", mapname);
  146. }
  147. return true;
  148. }
  149.  
  150.  
  151.  
  152. stock StripNewLine(string[]) //DracoBlue (bugfix idea by Y_Less)
  153. {
  154. new len = strlen(string);
  155. if (string[0]==0) return ;
  156. if ((string[len - 1] == '\n') || (string[len - 1] == '\r')) {
  157. string[len - 1] = 0;
  158. if (string[0]==0) return ;
  159. if ((string[len - 2] == '\n') || (string[len - 2] == '\r')) string[len - 2] = 0;
  160. }
  161. }
  162.  
  163. public OnPlayerDisconnect(playerid, reason)
  164. {
  165. if(Player_MovingMap[playerid] == 1)
  166. {
  167. for(new i = 0; i != MAX_MAPMOVER_OBJECTS; i++)
  168. {
  169. if(MAPMOVER_OBJECTS[i][ModelID] != 0)
  170. {
  171. DestroyObject(MAPMOVER_OBJECTS[i][ObjectID]);
  172. MAPMOVER_OBJECTS[i][ModelID] = 0;
  173. MAPMOVER_OBJECTS[i][PositionX] = 0.0;
  174. MAPMOVER_OBJECTS[i][PositionY] = 0.0;
  175. MAPMOVER_OBJECTS[i][PositionZ] = 0.0;
  176. MAPMOVER_OBJECTS[i][RotationX] = 0.0;
  177. MAPMOVER_OBJECTS[i][RotationY] = 0.0;
  178. MAPMOVER_OBJECTS[i][RotationZ] = 0.0;
  179. MAPMOVER_OBJECTS[i][OffSetX] = 0.0;
  180. MAPMOVER_OBJECTS[i][OffSetY] = 0.0;
  181. MAPMOVER_OBJECTS[i][OffSetZ] = 0.0;
  182. MAPMOVER_OBJECTS[i][ObjectID] = 0;
  183. }
  184. }
  185. DestroyObject(ObjectCenter);
  186. ObjectCenter = 0;
  187. Player_MovingObjects[playerid] = 0;
  188. Player_MovingMap[playerid] = 0;
  189. }
  190. return 1;
  191. }
  192.  
  193. public OnPlayerCommandText(playerid, cmdtext[])
  194. {
  195. if(!strcmp(cmdtext, "/map", true))
  196. {
  197. if(GetPVarType(playerid, "FlyMode"))
  198. {
  199. if(Player_MovingObjects[playerid] == 1) return SendClientMessage(playerid, -1, "{FFFF00}Map Mover: {FFFFFF}Termina de mover el mapa antes de mostrar el menú.");
  200. if(Player_MovingMap[playerid] == 1) ShowPlayerDialog(playerid, 803, DIALOG_STYLE_LIST, "Map Mover", "1. Localizar\n2. Re/Colocar centro\n3. Editar posiciones\n4. Exportar mapa\n5. Salir (sin guardar)", "OK", "");
  201. }
  202. else
  203. {
  204. if(Player_MovingMap[playerid] == 1) return 1;
  205. Player_MovingMap[playerid] = 1;
  206. ShowPlayerDialog(playerid, 800, DIALOG_STYLE_INPUT, "Map Mover", "\n\nIntroduzca el nombre del archivo .txt con el map\nque ha sido colocado en la carpeta scriptfiles.\n\n\t(Debe de introducir la extensión .txt)\n\n", "Cargar", "X");
  207. FlyMode(playerid);
  208. }
  209. return 1;
  210. }
  211. return 0;
  212. }
  213.  
  214. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  215. {
  216. if(dialogid == 800)
  217. {
  218. if(response)
  219. {
  220. ShowPlayerDialog(playerid, 801, DIALOG_STYLE_MSGBOX, "Map Mover", "\n\n\tCargando Mapa...\n\n", "OK", "");
  221. LoadMap(playerid, inputtext);
  222. }
  223. else
  224. {
  225. Player_MovingMap[playerid] = 0;
  226. CancelFlyMode(playerid);
  227. }
  228. }
  229. if(dialogid == 801) ShowPlayerDialog(playerid, 800, DIALOG_STYLE_INPUT, "Map Mover", "\n\nIntroduzca el nombre del archivo .txt con el map\nque ha sido colocado en la carpeta scriptfiles.\n\n\t(Debe de introducir la extensión .txt)\n\n", "Cargar", "X");
  230. if(dialogid == 802) ShowPlayerDialog(playerid, 803, DIALOG_STYLE_LIST, "Map Mover", "1. Localizar\n2. Re/Colocar centro\n3. Editar posiciones\n4. Exportar mapa\n5. Salir (sin guardar)", "OK", "");
  231. if(dialogid == 803)
  232. {
  233. if(response)
  234. {
  235. switch(listitem)
  236. {
  237. case 0:
  238. {
  239. if(ObjectCenter == 0)
  240. {
  241. new Float:CameraX = MAPMOVER_OBJECTS[0][PositionX];
  242. new Float:CameraY = MAPMOVER_OBJECTS[0][PositionY];
  243. new Float:CameraZ = MAPMOVER_OBJECTS[0][PositionZ];
  244. SetPlayerObjectPos(playerid, noclipdata[playerid][flyobject], CameraX, CameraY, CameraZ);
  245. SendClientMessage(playerid, -1, "{FFFF00}Map Mover: {FFFFFF}Use nuevamente el comando {FFFF00}/map {FFFFFF}para mostrar el menú principal.");
  246. }
  247. else
  248. {
  249. new Float:CameraX;
  250. new Float:CameraY;
  251. new Float:CameraZ;
  252. GetObjectPos(ObjectCenter, CameraX, CameraY, CameraZ);
  253. SetPlayerObjectPos(playerid, noclipdata[playerid][flyobject], CameraX, CameraY, CameraZ);
  254. SendClientMessage(playerid, -1, "{FFFF00}Map Mover: {FFFFFF}Use nuevamente el comando {FFFF00}/map {FFFFFF}para mostrar el menú principal.");
  255. }
  256. }
  257. case 1:
  258. {
  259. if(ObjectCenter == 0)
  260. {
  261. new Float:CameraX;
  262. new Float:CameraY;
  263. new Float:CameraZ;
  264. GetPlayerObjectPos(playerid, noclipdata[playerid][flyobject], CameraX, CameraY, CameraZ);
  265. ObjectCenter = CreateObject(1220, CameraX, CameraY, CameraZ, 0.0, 0.0, 0.0);
  266. SetObjectMaterial(ObjectCenter, 0, 18646, "matcolours", "white");
  267. EditObject(playerid, ObjectCenter);
  268. SendClientMessage(playerid, -1, "{FFFF00}Map Mover: {FFFFFF}Coloce el objeto en el centro del mapa cargado.");
  269. }
  270. else
  271. {
  272. SetObjectMaterial(ObjectCenter, 0, 18646, "matcolours", "white");
  273. EditObject(playerid, ObjectCenter);
  274. SendClientMessage(playerid, -1, "{FFFF00}Map Mover: {FFFFFF}Coloce el objeto en el centro del mapa cargado.");
  275. }
  276. }
  277. case 2:
  278. {
  279. if(ObjectCenter == 0) return SendClientMessage(playerid, -1, "{FFFF00}Map Mover: {FFFFFF}Debes de colocar el centro primero.");
  280. new Float:CenterX, Float:CenterY, Float:CenterZ;
  281. GetObjectPos(ObjectCenter, CenterX, CenterY, CenterZ);
  282. for(new i = 0; i != MAX_MAPMOVER_OBJECTS; i++)
  283. {
  284. if(MAPMOVER_OBJECTS[i][ModelID] != 0)
  285. {
  286. new Float:aOffSetX, Float:aOffSetY, Float:aOffSetZ;
  287. aOffSetX = floatsub(MAPMOVER_OBJECTS[i][PositionX], CenterX);
  288. aOffSetY = floatsub(MAPMOVER_OBJECTS[i][PositionY], CenterY);
  289. aOffSetZ = floatsub(MAPMOVER_OBJECTS[i][PositionZ], CenterZ);
  290. MAPMOVER_OBJECTS[i][OffSetX] = aOffSetX;
  291. MAPMOVER_OBJECTS[i][OffSetY] = aOffSetY;
  292. MAPMOVER_OBJECTS[i][OffSetZ] = aOffSetZ;
  293. AttachObjectToObject(MAPMOVER_OBJECTS[i][ObjectID], ObjectCenter, aOffSetX, aOffSetY, aOffSetZ, MAPMOVER_OBJECTS[i][RotationX], MAPMOVER_OBJECTS[i][RotationY], MAPMOVER_OBJECTS[i][RotationZ], true);
  294. }
  295. }
  296. Player_MovingObjects[playerid] = 1;
  297. EditObject(playerid, ObjectCenter);
  298. }
  299. case 3:
  300. {
  301. if(ObjectCenter == 0) return SendClientMessage(playerid, -1, "{FFFF00}Map Mover: {FFFFFF}No se ha notado ningún cambio.");
  302. new f_name[24];
  303. format(f_name, 24, "%s_converted.txt", mapnamefile);
  304. new File:codefile = fopen(f_name, io_write);
  305. if(codefile)
  306. {
  307. new Year, Month, Day;
  308. getdate(Year, Month, Day);
  309. new Hour, Minute, Second;
  310. gettime(Hour, Minute, Second);
  311. new intro[128]; format(intro, 128, "Map Mover ~ %s ~ %02d/%02d/%d ~ %02d:%02d:%02d\r\n\r\n", mapnamefile, Day, Month, Year, Hour, Minute, Second);
  312. fwrite(codefile, intro);
  313. for(new i = 0; i != MAX_MAPMOVER_OBJECTS; i++)
  314. {
  315. if(MAPMOVER_OBJECTS[i][ModelID] != 0)
  316. {
  317. new line[128];
  318. format(line, 128, "CreateObject(%d, %f, %f, %f, %f, %f, %f);\r\n", MAPMOVER_OBJECTS[i][ModelID], MAPMOVER_OBJECTS[i][PositionX], MAPMOVER_OBJECTS[i][PositionY], MAPMOVER_OBJECTS[i][PositionZ], MAPMOVER_OBJECTS[i][RotationX], MAPMOVER_OBJECTS[i][RotationY], MAPMOVER_OBJECTS[i][RotationZ]);
  319. fwrite(codefile, line);
  320. }
  321. }
  322. fwrite(codefile, "\r\n");
  323. fwrite(codefile, "\r\n");
  324. fwrite(codefile, "Map Mover 1.0 by adri1.");
  325. fclose(codefile);
  326. }
  327.  
  328. }
  329. case 4:
  330. {
  331. for(new i = 0; i != MAX_MAPMOVER_OBJECTS; i++)
  332. {
  333. if(MAPMOVER_OBJECTS[i][ModelID] != 0)
  334. {
  335. DestroyObject(MAPMOVER_OBJECTS[i][ObjectID]);
  336. MAPMOVER_OBJECTS[i][ModelID] = 0;
  337. MAPMOVER_OBJECTS[i][PositionX] = 0.0;
  338. MAPMOVER_OBJECTS[i][PositionY] = 0.0;
  339. MAPMOVER_OBJECTS[i][PositionZ] = 0.0;
  340. MAPMOVER_OBJECTS[i][RotationX] = 0.0;
  341. MAPMOVER_OBJECTS[i][RotationY] = 0.0;
  342. MAPMOVER_OBJECTS[i][RotationZ] = 0.0;
  343. MAPMOVER_OBJECTS[i][OffSetX] = 0.0;
  344. MAPMOVER_OBJECTS[i][OffSetY] = 0.0;
  345. MAPMOVER_OBJECTS[i][OffSetZ] = 0.0;
  346. MAPMOVER_OBJECTS[i][ObjectID] = 0;
  347. }
  348. }
  349. DestroyObject(ObjectCenter);
  350. Player_MovingObjects[playerid] = 0;
  351. Player_MovingMap[playerid] = 0;
  352. ObjectCenter = 0;
  353. CancelFlyMode(playerid);
  354. CancelEdit(playerid);
  355. }
  356. }
  357. }
  358. else SendClientMessage(playerid, -1, "{FFFF00}Map Mover: {FFFFFF}Use nuevamente el comando {FFFF00}/map {FFFFFF}para mostrar el menú principal.");
  359. }
  360. return 0;
  361. }
  362.  
  363. public OnPlayerEditObject(playerid, playerobject, objectid, response, Float:fX, Float:fY, Float:fZ, Float:fRotX, Float:fRotY, Float:fRotZ)
  364. {
  365. if(response == EDIT_RESPONSE_FINAL || response == EDIT_RESPONSE_CANCEL)
  366. {
  367. if(objectid == ObjectCenter)
  368. {
  369. SetObjectPos(objectid, fX, fY, fZ);
  370. SetObjectRot(objectid, fRotX, fRotY, fRotZ);
  371. if(Player_MovingObjects[playerid] == 1)
  372. {
  373. for(new i = 0; i != MAX_MAPMOVER_OBJECTS; i++)
  374. {
  375. if(MAPMOVER_OBJECTS[i][ModelID] != 0)
  376. {
  377. new Float:PX, Float:PY, Float:PZ;
  378. new Float:RX, Float:RY, Float:RZ;
  379. AttachObjectToObjectEx(ObjectCenter, MAPMOVER_OBJECTS[i][OffSetX], MAPMOVER_OBJECTS[i][OffSetY], MAPMOVER_OBJECTS[i][OffSetZ], MAPMOVER_OBJECTS[i][RotationX], MAPMOVER_OBJECTS[i][RotationY], MAPMOVER_OBJECTS[i][RotationZ], PX, PY, PZ, RX, RY, RZ);
  380.  
  381. MAPMOVER_OBJECTS[i][OffSetX] = 0.0;
  382. MAPMOVER_OBJECTS[i][OffSetY] = 0.0;
  383. MAPMOVER_OBJECTS[i][OffSetZ] = 0.0;
  384.  
  385. DestroyObject(MAPMOVER_OBJECTS[i][ObjectID]);
  386. MAPMOVER_OBJECTS[i][ObjectID] = CreateObject(MAPMOVER_OBJECTS[i][ModelID], PX, PY, PZ, RX, RY, fRotZ <= -90.0 ? (RZ+fRotZ) : (RZ+fRotZ));
  387. MAPMOVER_OBJECTS[i][PositionX] = PX;
  388. MAPMOVER_OBJECTS[i][PositionY] = PY;
  389. MAPMOVER_OBJECTS[i][PositionZ] = PZ;
  390. MAPMOVER_OBJECTS[i][RotationX] = RX;
  391. MAPMOVER_OBJECTS[i][RotationY] = RY;
  392. MAPMOVER_OBJECTS[i][RotationZ] = RZ+fRotZ;
  393. }
  394. }//-90 a -180
  395. SetObjectRot(objectid, 0.0, 0.0, 0.0);
  396. Player_MovingObjects[playerid] = 0;
  397. return 1;
  398. }
  399. SetObjectMaterial(ObjectCenter, 0, 0, "null", "null");
  400. SendClientMessage(playerid, -1, "{FFFF00}Map Mover: {FFFFFF}Use nuevamente el comando {FFFF00}/map {FFFFFF}para mostrar el menú principal.");
  401. SetObjectRot(objectid, 0.0, 0.0, 0.0);
  402. }
  403. }
  404. return 1;
  405. }
  406.  
  407. AttachObjectToObjectEx(attachoid, Float:off_x, Float:off_y, Float:off_z, Float:rot_x, Float:rot_y, Float:rot_z, &Float:X, &Float:Y, &Float:Z, &Float:RX, &Float:RY, &Float:RZ) // By Stylock - http://forum.sa-mp.com/member.php?u=114165
  408. {
  409. static
  410. Float:sin[3],
  411. Float:cos[3],
  412. Float:pos[3],
  413. Float:rot[3];
  414. GetObjectPos(attachoid, pos[0], pos[1], pos[2]);
  415. GetObjectRot(attachoid, rot[0], rot[1], rot[2]);
  416. cos[0] = floatcos(rot[0], degrees); cos[1] = floatcos(rot[1], degrees); cos[2] = floatcos(rot[2], degrees); sin[0] = floatsin(rot[0], degrees); sin[1] = floatsin(rot[1], degrees); sin[2] = floatsin(rot[2], degrees);
  417. pos[0] = pos[0] + off_x * cos[1] * cos[2] - off_x * sin[0] * sin[1] * sin[2] - off_y * cos[0] * sin[2] + off_z * sin[1] * cos[2] + off_z * sin[0] * cos[1] * sin[2];
  418. pos[1] = pos[1] + off_x * cos[1] * sin[2] + off_x * sin[0] * sin[1] * cos[2] + off_y * cos[0] * cos[2] + off_z * sin[1] * sin[2] - off_z * sin[0] * cos[1] * cos[2];
  419. pos[2] = pos[2] - off_x * cos[0] * sin[1] + off_y * sin[0] + off_z * cos[0] * cos[1];
  420. rot[0] = asin(cos[0] * cos[1]); rot[1] = atan2(sin[0], cos[0] * sin[1]) + rot_z; rot[2] = atan2(cos[1] * cos[2] * sin[0] - sin[1] * sin[2], cos[2] * sin[1] - cos[1] * sin[0] * -sin[2]);
  421. cos[0] = floatcos(rot[0], degrees); cos[1] = floatcos(rot[1], degrees); cos[2] = floatcos(rot[2], degrees); sin[0] = floatsin(rot[0], degrees); sin[1] = floatsin(rot[1], degrees); sin[2] = floatsin(rot[2], degrees);
  422. rot[0] = asin(cos[0] * sin[1]); rot[1] = atan2(cos[0] * cos[1], sin[0]); rot[2] = atan2(cos[2] * sin[0] * sin[1] - cos[1] * sin[2], cos[1] * cos[2] + sin[0] * sin[1] * sin[2]);
  423. cos[0] = floatcos(rot[0], degrees); cos[1] = floatcos(rot[1], degrees); cos[2] = floatcos(rot[2], degrees); sin[0] = floatsin(rot[0], degrees); sin[1] = floatsin(rot[1], degrees); sin[2] = floatsin(rot[2], degrees);
  424. rot[0] = atan2(sin[0], cos[0] * cos[1]) + rot_x; rot[1] = asin(cos[0] * sin[1]); rot[2] = atan2(cos[2] * sin[0] * sin[1] + cos[1] * sin[2], cos[1] * cos[2] - sin[0] * sin[1] * sin[2]);
  425. cos[0] = floatcos(rot[0], degrees); cos[1] = floatcos(rot[1], degrees); cos[2] = floatcos(rot[2], degrees); sin[0] = floatsin(rot[0], degrees); sin[1] = floatsin(rot[1], degrees); sin[2] = floatsin(rot[2], degrees);
  426. rot[0] = asin(cos[1] * sin[0]); rot[1] = atan2(sin[1], cos[0] * cos[1]) + rot_y; rot[2] = atan2(cos[0] * sin[2] - cos[2] * sin[0] * sin[1], cos[0] * cos[2] + sin[0] * sin[1] * sin[2]);
  427. X = pos[0];
  428. Y = pos[1];
  429. Z = pos[2];
  430. RX = rot[0];
  431. RY = rot[1];
  432. RZ = rot[2];
  433. }
  434.  
  435. //--------------------------------------------------
  436.  
  437. public OnPlayerUpdate(playerid)
  438. {
  439. if(noclipdata[playerid][cameramode] == CAMERA_MODE_FLY)
  440. {
  441. new keys,ud,lr;
  442. GetPlayerKeys(playerid,keys,ud,lr);
  443. if(noclipdata[playerid][mode] && (GetTickCount() - noclipdata[playerid][lastmove] > 100))
  444. {
  445. // If the last move was > 100ms ago, process moving the object the players camera is attached to
  446. MoveCamera(playerid);
  447. }
  448.  
  449. // Is the players current key state different than their last keystate?
  450. if(noclipdata[playerid][udold] != ud || noclipdata[playerid][lrold] != lr)
  451. {
  452. if((noclipdata[playerid][udold] != 0 || noclipdata[playerid][lrold] != 0) && ud == 0 && lr == 0)
  453. { // All keys have been released, stop the object the camera is attached to and reset the acceleration multiplier
  454. StopPlayerObject(playerid, noclipdata[playerid][flyobject]);
  455. noclipdata[playerid][mode] = 0;
  456. noclipdata[playerid][accelmul] = 0.0;
  457. }
  458. else
  459. { // Indicates a new key has been pressed
  460.  
  461. // Get the direction the player wants to move as indicated by the keys
  462. noclipdata[playerid][mode] = GetMoveDirectionFromKeys(ud, lr);
  463.  
  464. // Process moving the object the players camera is attached to
  465. MoveCamera(playerid);
  466. }
  467. }
  468. noclipdata[playerid][udold] = ud; noclipdata[playerid][lrold] = lr; // Store current keys pressed for comparison next update
  469. return 0;
  470. }
  471. return 1;
  472. }
  473.  
  474. //--------------------------------------------------
  475.  
  476. stock GetMoveDirectionFromKeys(ud, lr)
  477. {
  478. new direction = 0;
  479.  
  480. if(lr < 0)
  481. {
  482. if(ud < 0) direction = MOVE_FORWARD_LEFT; // Up & Left key pressed
  483. else if(ud > 0) direction = MOVE_BACK_LEFT; // Back & Left key pressed
  484. else direction = MOVE_LEFT; // Left key pressed
  485. }
  486. else if(lr > 0) // Right pressed
  487. {
  488. if(ud < 0) direction = MOVE_FORWARD_RIGHT; // Up & Right key pressed
  489. else if(ud > 0) direction = MOVE_BACK_RIGHT; // Back & Right key pressed
  490. else direction = MOVE_RIGHT; // Right key pressed
  491. }
  492. else if(ud < 0) direction = MOVE_FORWARD; // Up key pressed
  493. else if(ud > 0) direction = MOVE_BACK; // Down key pressed
  494.  
  495. return direction;
  496. }
  497.  
  498. //--------------------------------------------------
  499.  
  500. stock MoveCamera(playerid)
  501. {
  502. new Float:FV[3], Float:CP[3];
  503. GetPlayerCameraPos(playerid, CP[0], CP[1], CP[2]); // Cameras position in space
  504. GetPlayerCameraFrontVector(playerid, FV[0], FV[1], FV[2]); // Where the camera is looking at
  505.  
  506. // Increases the acceleration multiplier the longer the key is held
  507. if(noclipdata[playerid][accelmul] <= 1) noclipdata[playerid][accelmul] += ACCEL_RATE;
  508.  
  509. // Determine the speed to move the camera based on the acceleration multiplier
  510. new Float:speed = MOVE_SPEED * noclipdata[playerid][accelmul];
  511.  
  512. // Calculate the cameras next position based on their current position and the direction their camera is facing
  513. new Float:X, Float:Y, Float:Z;
  514. GetNextCameraPosition(noclipdata[playerid][mode], CP, FV, X, Y, Z);
  515. MovePlayerObject(playerid, noclipdata[playerid][flyobject], X, Y, Z, speed);
  516.  
  517. // Store the last time the camera was moved as now
  518. noclipdata[playerid][lastmove] = GetTickCount();
  519. return 1;
  520. }
  521.  
  522. //--------------------------------------------------
  523.  
  524. stock GetNextCameraPosition(move_mode, Float:CP[3], Float:FV[3], &Float:X, &Float:Y, &Float:Z)
  525. {
  526. // Calculate the cameras next position based on their current position and the direction their camera is facing
  527. #define OFFSET_X (FV[0]*6000.0)
  528. #define OFFSET_Y (FV[1]*6000.0)
  529. #define OFFSET_Z (FV[2]*6000.0)
  530. switch(move_mode)
  531. {
  532. case MOVE_FORWARD:
  533. {
  534. X = CP[0]+OFFSET_X;
  535. Y = CP[1]+OFFSET_Y;
  536. Z = CP[2]+OFFSET_Z;
  537. }
  538. case MOVE_BACK:
  539. {
  540. X = CP[0]-OFFSET_X;
  541. Y = CP[1]-OFFSET_Y;
  542. Z = CP[2]-OFFSET_Z;
  543. }
  544. case MOVE_LEFT:
  545. {
  546. X = CP[0]-OFFSET_Y;
  547. Y = CP[1]+OFFSET_X;
  548. Z = CP[2];
  549. }
  550. case MOVE_RIGHT:
  551. {
  552. X = CP[0]+OFFSET_Y;
  553. Y = CP[1]-OFFSET_X;
  554. Z = CP[2];
  555. }
  556. case MOVE_BACK_LEFT:
  557. {
  558. X = CP[0]+(-OFFSET_X - OFFSET_Y);
  559. Y = CP[1]+(-OFFSET_Y + OFFSET_X);
  560. Z = CP[2]-OFFSET_Z;
  561. }
  562. case MOVE_BACK_RIGHT:
  563. {
  564. X = CP[0]+(-OFFSET_X + OFFSET_Y);
  565. Y = CP[1]+(-OFFSET_Y - OFFSET_X);
  566. Z = CP[2]-OFFSET_Z;
  567. }
  568. case MOVE_FORWARD_LEFT:
  569. {
  570. X = CP[0]+(OFFSET_X - OFFSET_Y);
  571. Y = CP[1]+(OFFSET_Y + OFFSET_X);
  572. Z = CP[2]+OFFSET_Z;
  573. }
  574. case MOVE_FORWARD_RIGHT:
  575. {
  576. X = CP[0]+(OFFSET_X + OFFSET_Y);
  577. Y = CP[1]+(OFFSET_Y - OFFSET_X);
  578. Z = CP[2]+OFFSET_Z;
  579. }
  580. }
  581. }
  582. //--------------------------------------------------
  583.  
  584. stock CancelFlyMode(playerid)
  585. {
  586. DeletePVar(playerid, "FlyMode");
  587. CancelEdit(playerid);
  588. TogglePlayerSpectating(playerid, false);
  589.  
  590. DestroyPlayerObject(playerid, noclipdata[playerid][flyobject]);
  591. noclipdata[playerid][cameramode] = CAMERA_MODE_NONE;
  592. return 1;
  593. }
  594.  
  595. //--------------------------------------------------
  596.  
  597. stock FlyMode(playerid)
  598. {
  599. // Create an invisible object for the players camera to be attached to
  600. new Float:X, Float:Y, Float:Z;
  601. GetPlayerPos(playerid, X, Y, Z);
  602. noclipdata[playerid][flyobject] = CreatePlayerObject(playerid, 19300, X, Y, Z, 0.0, 0.0, 0.0);
  603.  
  604. // Place the player in spectating mode so objects will be streamed based on camera location
  605. TogglePlayerSpectating(playerid, true);
  606. // Attach the players camera to the created object
  607. AttachCameraToPlayerObject(playerid, noclipdata[playerid][flyobject]);
  608.  
  609. SetPVarInt(playerid, "FlyMode", 1);
  610. noclipdata[playerid][cameramode] = CAMERA_MODE_FLY;
  611. return 1;
  612. }
  613.  
  614. //--------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement