seifcam.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. SeifCam - Camera include by Seif
  3. *-----------------------------------------------*
  4. | This include allows you to move the camera to |
  5. | a certain place smoothly and easily. It even |
  6. | calls a callback when the camera finishes |
  7. | to move at the destination. The callback is |
  8. | 'OnPlayerCameraMoved'. |
  9. *-----------------------------------------------*
  10. */
  11. /*x---------------------------------Important-------------------------------------x*/
  12. //**INCLUDES**//
  13. #include <a_samp>
  14. //**PRAGMAS**//
  15. //**MISC**//
  16. #if !defined function
  17. #define function%0(%1) forward%0(%1); public%0(%1)
  18. #endif
  19. /*x---------------------------------Defining-------------------------------------x*/
  20. //**VARIABLES**//
  21. enum sciInfo
  22. {
  23. Float:sciPOSX,
  24. Float:sciPOSY,
  25. Float:sciPOSZ,
  26. Float:sciLOOKX,
  27. Float:sciLOOKY,
  28. Float:sciLOOKZ,
  29. Float:sciDestX,
  30. Float:sciDestY,
  31. Float:sciDestZ,
  32. Float:sciMaxDest,
  33. Float:sciMove,
  34. bool:sciMoving,
  35. Float:sciFinalDestX,
  36. Float:sciFinalDestY,
  37. Float:sciFinalDestZ,
  38. Float:sciLookDestX,
  39. Float:sciLookDestY,
  40. Float:sciLookDestZ,
  41. Float:sciLookMaxDest,
  42. Float:sciLookMove,
  43. bool:sciLookMoving,
  44. Float:sciLookFinalDestX,
  45. Float:sciLookFinalDestY,
  46. Float:sciLookFinalDestZ,
  47. Float:sciAngle,
  48. Float:sciFirstAngle,
  49. Float:sciFinalAngle,
  50. }
  51. new SeifCamInfo[MAX_PLAYERS][sciInfo];
  52. // **NATIVES** //
  53. /*
  54. native SetPlayerCameraPos(playerid, Float:x, Float:y, Float:z);
  55. native SetPlayerCameraLookAt(playerid, Float:x, Float:y, Float:z);
  56. native MovePlayerCamera(playerid, Float:x, Float:y, Float:z);
  57. native ResetPlayerCamera(playerid);
  58. */
  59. /*x---------------------------------CallBacks-------------------------------------x*/
  60. function SetPlayerCameraPosEx(playerid, Float:x, Float:y, Float:z)
  61. {
  62. if (!IsPlayerConnected(playerid)) return 0;
  63. SeifCamInfo[playerid][sciPOSX] = x;
  64. SeifCamInfo[playerid][sciPOSY] = y;
  65. SeifCamInfo[playerid][sciPOSZ] = z;
  66. SetPlayerCameraPos(playerid, x, y, z);
  67. return 1;
  68. }
  69. function SetPlayerCameraLookAtEx(playerid, Float:x, Float:y, Float:z)
  70. {
  71. if (!IsPlayerConnected(playerid)) return 0;
  72. SeifCamInfo[playerid][sciLOOKX] = x;
  73. SeifCamInfo[playerid][sciLOOKY] = y;
  74. SeifCamInfo[playerid][sciLOOKZ] = z;
  75. SetPlayerCameraLookAt(playerid, x, y, z);
  76. new Float:a;
  77. GetPlayerFacingAngle(playerid, a);
  78. SeifCamInfo[playerid][sciAngle] = a;
  79. return 1;
  80. }
  81. Float:GetPlayerCameraFacingPos(playerid)
  82. {
  83. new Float:x, Float:y, Float:a;
  84. GetPlayerCameraLookAt(playerid, x, y, a);
  85. x += 800.0;
  86. y += 300.0;
  87. new Float:px, Float:py, Float:pz;
  88. GetPlayerCameraLookAt(playerid, px, py, pz);
  89. a = floatabs(atan((py-y)/(px-x)));
  90. //printf("%f", a);
  91. if (px <= x && py >= y) a = floatsub(180, a);
  92. else if (px < x && py < y) a = floatadd(a, 180);
  93. else if (px >= x && py <= y) a = floatsub(360.0, a);
  94. //printf("%f", a);
  95. a = floatsub(a, 90.0);
  96. //printf("%f", a);
  97. if (a >= 360.0) a = floatsub(a, 360.0);
  98. if (a < 0.0) a = 360.0;
  99. //printf("%f", a);
  100. return a;
  101. }
  102. Float:GetPlayerCameraFacingPosEx(playerid, Float:px, Float:py, Float:pz)
  103. {
  104. #pragma unused pz
  105. new Float:x, Float:y, Float:a;
  106. GetPlayerCameraLookAt(playerid, x, y, a);
  107. a = floatabs(atan((py-y)/(px-x)));
  108. //printf("%f", a);
  109. if (px <= x && py >= y) a = floatsub(180, a);
  110. else if (px < x && py < y) a = floatadd(a, 180);
  111. else if (px >= x && py <= y) a = floatsub(360.0, a);
  112. //printf("%f", a);
  113. a = floatsub(a, 90.0);
  114. //printf("%f", a);
  115. if (a >= 360.0) a = floatsub(a, 360.0);
  116. if (a < 0.0) a = 360.0;
  117. //printf("%f", a);
  118. return a;
  119. }
  120. function MovePlayerCamera(playerid, Float:posx, Float:posy, Float:posz/*, Float:lookatx, Float:lookaty, Float:lookatz*/, Float:speed)
  121. {
  122. if (!IsPlayerConnected(playerid)) return 0;
  123. //printf("player %d: %f, %f, %f", playerid, posx, posy, posz);
  124. //if (posx != SeifCamInfo[playerid][sciLOOKX] || posy != SeifCamInfo[playerid][sciLOOKY] || posz != SeifCamInfo[playerid][sciLOOKZ]) return RotatePlayerCamera(playerid, posx, posy, posz, speed);
  125. SetPlayerCameraLookAtEx(playerid, posx, posy, posz);
  126. new Float:oldx = SeifCamInfo[playerid][sciPOSX],
  127. Float:oldy = SeifCamInfo[playerid][sciPOSY],
  128. Float:oldz = SeifCamInfo[playerid][sciPOSZ];
  129. new Float:destX, Float:destY, Float:destZ;
  130. /*new Float:oldlookx = SeifCamInfo[playerid][sciLOOKX],
  131. Float:oldlooky = SeifCamInfo[playerid][sciLOOKY],
  132. Float:oldlookz = SeifCamInfo[playerid][sciLOOKZ];
  133. new Float:destlookX, Float:destlookY, Float:destlookZ;*/
  134. if (posx > oldx) destX = posx-oldx;
  135. else destX = oldx-posx;
  136. if (posy > oldy) destY = posy-oldy;
  137. else destY = oldy-posy;
  138. if (posz > oldz) destZ = posz-oldz;
  139. else destZ = oldz-posz;
  140. /*if (lookatx > oldlookx) destlookX = lookatx-oldlookx;
  141. else destlookX = oldlookx-lookatx;
  142. if (lookaty > oldlooky) destlookY = lookaty-oldlooky;
  143. else destlookY = oldlooky-lookaty;
  144. if (lookatz > oldlookz) destlookZ = lookatz-oldlookz;
  145. else destlookZ = oldlookz-lookatz;*/
  146. new Float:desttime;
  147. if (destX >= destY && destX >= destZ) desttime = destX;
  148. else if (destY >= destX && destY >= destZ) desttime = destY;
  149. else if (destZ >= destX && destZ >= destY) desttime = destZ;
  150. desttime /= speed;
  151. /*new Float:lookdesttime;
  152. if (destlookX >= destlookY && destlookX >= destlookZ) lookdesttime = destlookX;
  153. else if (destlookY >= destlookX && destlookY >= destlookZ) lookdesttime = destlookY;
  154. else if (destlookZ >= destlookX && destlookZ >= destlookY) lookdesttime = destlookZ;*/
  155. //SeifCamInfo[playerid][sciDestX] = x;
  156. //SeifCamInfo[playerid][sciDestY] = y;
  157. //SeifCamInfo[playerid][sciDestZ] = z;
  158. SeifCamInfo[playerid][sciMaxDest] = desttime; // number to divide each destX/Y/Z so you can move the camera to the destination with every coordinate arriving at the same time.
  159. SeifCamInfo[playerid][sciDestX] = destX/desttime;
  160. SeifCamInfo[playerid][sciDestY] = destY/desttime;
  161. SeifCamInfo[playerid][sciDestZ] = destZ/desttime;
  162. SeifCamInfo[playerid][sciMove] = desttime; // amount of times left before arriving to the destination. since desttime is the max number to divide, it's set to it.
  163. SeifCamInfo[playerid][sciMoving] = true;
  164. SeifCamInfo[playerid][sciFinalDestX] = posx;
  165. SeifCamInfo[playerid][sciFinalDestY] = posy;
  166. SeifCamInfo[playerid][sciFinalDestZ] = posz;
  167. //new Float:x, Float:y, Float:z;
  168. //GetPlayerPos(playerid, x, y, z);
  169. SetTimerEx("MoveCamTimer", 50, 0, "d", playerid);
  170. return 1;
  171. }
  172. function RotatePlayerCamera(playerid, Float:posx, Float:posy, Float:posz, Float:speed)
  173. {
  174. SeifCamInfo[playerid][sciLookMaxDest] = speed; // number to divide each destX/Y/Z so you can move the camera to the destination with every coordinate arriving at the same time.
  175. /*SeifCamInfo[playerid][sciLookDestX] = destlookX/lookdesttime;
  176. SeifCamInfo[playerid][sciLookDestY] = destlookY/lookdesttime;
  177. SeifCamInfo[playerid][sciLookDestZ] = destlookZ/lookdesttime;*/
  178. SeifCamInfo[playerid][sciAngle] = GetPlayerCameraFacingPos(playerid); // this will change
  179. SeifCamInfo[playerid][sciFirstAngle] = GetPlayerCameraFacingPos(playerid); // this will stay the same
  180. SeifCamInfo[playerid][sciFinalAngle] = GetPlayerCameraFacingPosEx(playerid, posx, posy, posz); // this will stay the same
  181. //SetPlayerFacingAngle(playerid, GetPlayerCameraFacingPos(playerid));
  182. //SeifCamInfo[playerid][sciLookMove] = ((SeifCamInfo[playerid][sciFirstAngle]>SeifCamInfo[playerid][sciFinalAngle]) ? ((SeifCamInfo[playerid][sciFirstAngle]-SeifCamInfo[playerid][sciFinalAngle])/speed) : ((SeifCamInfo[playerid][sciFinalAngle]-SeifCamInfo[playerid][sciFirstAngle])/speed)); // amount of times left before arriving to the destination angle.
  183. SeifCamInfo[playerid][sciLookMoving] = true;
  184. SeifCamInfo[playerid][sciLookFinalDestX] = posx;
  185. SeifCamInfo[playerid][sciLookFinalDestY] = posy;
  186. SeifCamInfo[playerid][sciLookFinalDestZ] = posz;
  187. printf("move: %f", SeifCamInfo[playerid][sciAngle]);
  188. SetTimerEx("RotCamTimer", 50, 0, "d", playerid);
  189. return 1;
  190. }
  191. function ResetPlayerCamera(playerid)
  192. {
  193. if (!IsPlayerConnected(playerid)) return 0;
  194. SeifCamInfo[playerid][sciMoving] = false;
  195. SetCameraBehindPlayer(playerid);
  196. SeifCamInfo[playerid][sciPOSX] = 0.0;
  197. SeifCamInfo[playerid][sciPOSY] = 0.0;
  198. SeifCamInfo[playerid][sciPOSZ] = 0.0;
  199. SeifCamInfo[playerid][sciLOOKX] = 0.0;
  200. SeifCamInfo[playerid][sciLOOKY] = 0.0;
  201. SeifCamInfo[playerid][sciLOOKZ] = 0.0;
  202. SeifCamInfo[playerid][sciMove] = 0.0;
  203. SeifCamInfo[playerid][sciDestX] = 0.0;
  204. SeifCamInfo[playerid][sciDestY] = 0.0;
  205. SeifCamInfo[playerid][sciDestZ] = 0.0;
  206. SeifCamInfo[playerid][sciFinalDestX] = 0.0;
  207. SeifCamInfo[playerid][sciFinalDestY] = 0.0;
  208. SeifCamInfo[playerid][sciFinalDestZ] = 0.0;
  209. SeifCamInfo[playerid][sciLookMoving] = false;
  210. SeifCamInfo[playerid][sciLookMove] = 0.0;
  211. SeifCamInfo[playerid][sciLookDestX] = 0.0;
  212. SeifCamInfo[playerid][sciLookDestY] = 0.0;
  213. SeifCamInfo[playerid][sciLookDestZ] = 0.0;
  214. SeifCamInfo[playerid][sciLookFinalDestX] = 0.0;
  215. SeifCamInfo[playerid][sciLookFinalDestY] = 0.0;
  216. SeifCamInfo[playerid][sciLookFinalDestZ] = 0.0;
  217. SetCameraBehindPlayer(playerid);
  218. return 1;
  219. }
  220. function GetPlayerCamPos(playerid, &Float:x, &Float:y, &Float:z)
  221. {
  222. x = SeifCamInfo[playerid][sciPOSX];
  223. y = SeifCamInfo[playerid][sciPOSY];
  224. z = SeifCamInfo[playerid][sciPOSZ];
  225. }
  226. function GetPlayerCameraLookAt(playerid, &Float:x, &Float:y, &Float:z)
  227. {
  228. x = SeifCamInfo[playerid][sciLOOKX];
  229. y = SeifCamInfo[playerid][sciLOOKY];
  230. z = SeifCamInfo[playerid][sciLOOKZ];
  231. }
  232. /*Float:GetPlayerCameraFacingPos(playerid)
  233. {
  234. new Float:x = SeifCamInfo[playerid][sciLOOKX],Float:y = SeifCamInfo[playerid][sciLOOKY],Float:a = SeifCamInfo[playerid][sciLOOKZ];
  235. new Float:px = SeifCamInfo[playerid][sciLookFinalDestX], Float:py = SeifCamInfo[playerid][sciLookFinalDestY];
  236. a = floatabs(atan((py-y)/(px-x)));
  237. if (px <= x && py >= y) a = floatsub(180, a);
  238. else if (px < x && py < y) a = floatadd(a, 180);
  239. else if (px >= x && py <= y) a = floatsub(360.0, a);
  240. a = floatsub(a, 90.0);
  241. if (a >= 360.0) a = floatsub(a, 360.0);
  242. return a;
  243. }*/
  244. function MoveCamTimer(playerid)
  245. {
  246. if (!IsPlayerConnected(playerid)) return 0;
  247. if (SeifCamInfo[playerid][sciMoving] == false)
  248. {
  249. OnPlayerCameraMoved(playerid, SeifCamInfo[playerid][sciPOSX], SeifCamInfo[playerid][sciPOSY], SeifCamInfo[playerid][sciPOSZ]);
  250. //SetPlayerCameraLookAt(playerid, sciX, sciY, sciZ);
  251. //printf("player %d DONE: %f, %f, %f", playerid, SeifCamInfo[playerid][sciPOSX], SeifCamInfo[playerid][sciPOSY], SeifCamInfo[playerid][sciPOSZ]);
  252. return 1;
  253. }
  254. if (SeifCamInfo[playerid][sciFinalDestX] > SeifCamInfo[playerid][sciPOSX]) SeifCamInfo[playerid][sciPOSX] += SeifCamInfo[playerid][sciDestX];
  255. else SeifCamInfo[playerid][sciPOSX] -= SeifCamInfo[playerid][sciDestX];
  256. if (SeifCamInfo[playerid][sciFinalDestY] > SeifCamInfo[playerid][sciPOSY]) SeifCamInfo[playerid][sciPOSY] += SeifCamInfo[playerid][sciDestY];
  257. else SeifCamInfo[playerid][sciPOSY] -= SeifCamInfo[playerid][sciDestY];
  258. if (SeifCamInfo[playerid][sciFinalDestZ] > SeifCamInfo[playerid][sciPOSZ]) SeifCamInfo[playerid][sciPOSZ] += SeifCamInfo[playerid][sciDestZ];
  259. else SeifCamInfo[playerid][sciPOSZ] -= SeifCamInfo[playerid][sciDestZ];
  260. SeifCamInfo[playerid][sciMove] -= float(1);
  261. //printf("move: %f", SeifCamInfo[playerid][sciMove]);
  262. if (SeifCamInfo[playerid][sciMove] <= float(1)) SeifCamInfo[playerid][sciMoving] = false;
  263. SetPlayerCameraPos(playerid, SeifCamInfo[playerid][sciPOSX], SeifCamInfo[playerid][sciPOSY], SeifCamInfo[playerid][sciPOSZ]);
  264. SetPlayerCameraLookAt(playerid, SeifCamInfo[playerid][sciLOOKX], SeifCamInfo[playerid][sciLOOKY], SeifCamInfo[playerid][sciLOOKZ]);
  265. SetTimerEx("MoveCamTimer", 50, 0, "d", playerid);
  266. return 1;
  267. }
  268. function RotCamTimer(playerid)
  269. {
  270. if (!IsPlayerConnected(playerid)) return 0;
  271. if (SeifCamInfo[playerid][sciLookMoving] == false)
  272. {
  273. OnPlayerCameraRotated(playerid, SeifCamInfo[playerid][sciLOOKX], SeifCamInfo[playerid][sciLOOKY], SeifCamInfo[playerid][sciLOOKZ]);
  274. return 1;
  275. }
  276. if ((SeifCamInfo[playerid][sciFirstAngle]-SeifCamInfo[playerid][sciFinalAngle]) > 180) SeifCamInfo[playerid][sciAngle] -= SeifCamInfo[playerid][sciLookMaxDest];
  277. else SeifCamInfo[playerid][sciAngle] += SeifCamInfo[playerid][sciLookMaxDest];
  278. SeifCamInfo[playerid][sciLOOKX] -= floatsin(SeifCamInfo[playerid][sciAngle], degrees);
  279. SeifCamInfo[playerid][sciLOOKY] -= floatcos(SeifCamInfo[playerid][sciAngle], degrees);
  280. if (SeifCamInfo[playerid][sciAngle] <= 0.0 && SeifCamInfo[playerid][sciFinalAngle] > 0.0) SeifCamInfo[playerid][sciAngle] = 360.0;
  281. else if (SeifCamInfo[playerid][sciAngle] >= 360.0 && SeifCamInfo[playerid][sciFinalAngle] < 360.0) SeifCamInfo[playerid][sciAngle] = 0.0;
  282. if ((SeifCamInfo[playerid][sciAngle] >= (SeifCamInfo[playerid][sciFinalAngle]-SeifCamInfo[playerid][sciLookMaxDest]) && SeifCamInfo[playerid][sciAngle] <= (SeifCamInfo[playerid][sciFinalAngle]+SeifCamInfo[playerid][sciLookMaxDest])) || SeifCamInfo[playerid][sciAngle] == SeifCamInfo[playerid][sciFinalAngle]) printf("done: %f/%f", SeifCamInfo[playerid][sciAngle], SeifCamInfo[playerid][sciFinalAngle]), SeifCamInfo[playerid][sciLookMoving] = false;
  283. printf("%f/%f", SeifCamInfo[playerid][sciAngle], SeifCamInfo[playerid][sciFinalAngle]);
  284. SetPlayerCameraPos(playerid, SeifCamInfo[playerid][sciPOSX], SeifCamInfo[playerid][sciPOSY], SeifCamInfo[playerid][sciPOSZ]);
  285. SetPlayerCameraLookAt(playerid, SeifCamInfo[playerid][sciLOOKX], SeifCamInfo[playerid][sciLOOKY], SeifCamInfo[playerid][sciLOOKZ]);
  286. SetTimerEx("RotCamTimer", 50, 0, "d", playerid);
  287. return 1;
  288. }
  289. //**FUNCTION RENAMES**//
  290. #define SetPlayerCameraPos SetPlayerCameraPosEx
  291. #define SetPlayerCameraLookAt SetPlayerCameraLookAtEx
  292. //**FUNCTION FORWARDS**//
  293. forward OnPlayerCameraMoved(playerid, Float:destinationX, Float:destinationY, Float:destinationZ);
  294. forward OnPlayerCameraRotated(playerid, Float:destinationX, Float:destinationY, Float:destinationZ);
  295. // you MUST put the following callback in your script that uses this include! Exclude the /* and */ !
  296. /*
  297. public OnPlayerCameraMoved(playerid, Float:destinationX, Float:destinationY, Float:destinationZ)
  298. {
  299. return 1;
  300. }
  301. public OnPlayerCameraRotated(playerid, Float:destinationX, Float:destinationY, Float:destinationZ)
  302. {
  303. return 1;
  304. }
  305. */