Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2014
695
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.22 KB | None | 0 0
  1. // Analog clock by [uL]Pottus
  2.  
  3. #include <a_samp>
  4. #include <YSI\y_iterate>
  5. #include <streamer>
  6.  
  7.  
  8. #include <zcmd>
  9. CMD:testclock(playerid, arg[])
  10. {
  11. new Float:x, Float:y, Float:z, Float:fa;
  12. GetPlayerPos(playerid, x, y, z);
  13. GetPlayerFacingAngle(playerid, fa);
  14. CreateClock(x, y, z, fa);
  15.  
  16. return 1;
  17. }
  18.  
  19.  
  20. #define MAX_CLOCKS 10
  21. #define CLOCK_UPDATE_TIME 1000
  22.  
  23. enum CLOCKINFO
  24. {
  25. Float:ClockX,
  26. Float:ClockY,
  27. Float:ClockZ,
  28. Float:ClockRX,
  29. Float:ClockRY,
  30. Float:ClockRZ,
  31. Float:ClockFA,
  32. ClockMinuteCenter,
  33. ClockHourCenter,
  34. ClockMinuteHand,
  35. ClockHourHand,
  36. ClockObjects[13],
  37. }
  38.  
  39. static ClockData[MAX_CLOCKS][CLOCKINFO];
  40. static Iterator:Clocks<MAX_CLOCKS>;
  41. static ClockTimer;
  42.  
  43. public OnFilterScriptInit()
  44. {
  45. ClockTimer = SetTimer("UpdateClock", CLOCK_UPDATE_TIME, true);
  46. return 1;
  47. }
  48.  
  49.  
  50. public OnGameModeInit()
  51. {
  52. SetTimer("UpdateClock", CLOCK_UPDATE_TIME, true);
  53. KillTimer(ClockTimer);
  54. return 1;
  55. }
  56.  
  57. public OnFilterScriptExit()
  58. {
  59. foreach(new i : Clocks) i = DestroyClock(i);
  60. KillTimer(ClockTimer);
  61. return 1;
  62. }
  63.  
  64. public OnGameModeExit()
  65. {
  66. foreach(new i : Clocks) i = DestroyClock(i);
  67. return 1;
  68. }
  69.  
  70. stock CreateClock(Float:cx, Float:cy, Float:cz, Float:cangle)
  71. {
  72. new index = Iter_Free(Clocks);
  73. if(index > -1)
  74. {
  75. ClockData[index][ClockFA] = cangle;
  76.  
  77. // Static clock
  78. ClockData[index][ClockObjects][0] = CreateDynamicObject(13649,-0.027,0.309,-0.001,89.999,44.999,-44.999,-1,-1,-1,300.000,300.000);
  79. SetDynamicObjectMaterial(ClockData[index][ClockObjects][0], 0, 10765, "airportgnd_sfse", "white", 0);
  80. SetDynamicObjectMaterial(ClockData[index][ClockObjects][0], 1, 10765, "airportgnd_sfse", "white", 0);
  81. ClockData[index][ClockObjects][1] = CreateDynamicObject(19477,-0.000,-0.309,1.919,0.000,-0.000,-89.999,-1,-1,-1,300.000,300.000);
  82. SetDynamicObjectMaterialText(ClockData[index][ClockObjects][1], 0, "12", 130, "Ariel", 199, 1, -16777216, 0, 1);
  83. ClockData[index][ClockObjects][2] = CreateDynamicObject(19477,-0.000,-0.309,-1.919,0.000,-0.000,-89.999,-1,-1,-1,300.000,300.000);
  84. SetDynamicObjectMaterialText(ClockData[index][ClockObjects][2], 0, "6", 130, "Ariel", 199, 1, -16777216, 0, 1);
  85. ClockData[index][ClockObjects][3] = CreateDynamicObject(19477,-1.919,-0.309,0.000,0.000,-0.000,-89.999,-1,-1,-1,300.000,300.000);
  86. SetDynamicObjectMaterialText(ClockData[index][ClockObjects][3], 0, "9", 130, "Ariel", 199, 1, -16777216, 0, 1);
  87. ClockData[index][ClockObjects][4] = CreateDynamicObject(19477,1.919,-0.309,0.000,0.000,-0.000,-89.999,-1,-1,-1,300.000,300.000);
  88. SetDynamicObjectMaterialText(ClockData[index][ClockObjects][4], 0, "3", 130, "Ariel", 199, 1, -16777216, 0, 1);
  89. ClockData[index][ClockObjects][5] = CreateDynamicObject(19477,-0.960,-0.309,1.662,0.000,-0.000,-89.999,-1,-1,-1,300.000,300.000);
  90. SetDynamicObjectMaterialText(ClockData[index][ClockObjects][5], 0, "11", 130, "Ariel", 199, 1, -16777216, 0, 1);
  91. ClockData[index][ClockObjects][6] = CreateDynamicObject(19477,0.959,-0.309,-1.662,0.000,-0.000,-89.999,-1,-1,-1,300.000,300.000);
  92. SetDynamicObjectMaterialText(ClockData[index][ClockObjects][6], 0, "5", 130, "Ariel", 199, 1, -16777216, 0, 1);
  93. ClockData[index][ClockObjects][7] = CreateDynamicObject(19477,-1.662,-0.309,-0.959,0.000,-0.000,-89.999,-1,-1,-1,300.000,300.000);
  94. SetDynamicObjectMaterialText(ClockData[index][ClockObjects][7], 0, "8", 130, "Ariel", 199, 1, -16777216, 0, 1);
  95. ClockData[index][ClockObjects][8] = CreateDynamicObject(19477,1.662,-0.309,0.960,0.000,-0.000,-89.999,-1,-1,-1,300.000,300.000);
  96. SetDynamicObjectMaterialText(ClockData[index][ClockObjects][8], 0, "2", 130, "Ariel", 199, 1, -16777216, 0, 1);
  97. ClockData[index][ClockObjects][9] = CreateDynamicObject(19477,-1.662,-0.309,0.960,0.000,-0.000,-89.999,-1,-1,-1,300.000,300.000);
  98. SetDynamicObjectMaterialText(ClockData[index][ClockObjects][9], 0, "10", 130, "Ariel", 199, 1, -16777216, 0, 1);
  99. ClockData[index][ClockObjects][10] = CreateDynamicObject(19477,1.662,-0.309,-0.959,0.000,-0.000,-89.999,-1,-1,-1,300.000,300.000);
  100. SetDynamicObjectMaterialText(ClockData[index][ClockObjects][10], 0, "4", 130, "Ariel", 199, 1, -16777216, 0, 1);
  101. ClockData[index][ClockObjects][11] = CreateDynamicObject(19477,-0.960,-0.309,-1.662,0.000,-0.000,-89.999,-1,-1,-1,300.000,300.000);
  102. SetDynamicObjectMaterialText(ClockData[index][ClockObjects][11], 0, "7", 130, "Ariel", 199, 1, -16777216, 0, 1);
  103. ClockData[index][ClockObjects][12] = CreateDynamicObject(19477,0.959,-0.309,1.662,0.000,-0.000,-89.999,-1,-1,-1,300.000,300.000);
  104. SetDynamicObjectMaterialText(ClockData[index][ClockObjects][12], 0, "1", 130, "Ariel", 199, 1, -16777216, 0, 1);
  105.  
  106. // Clock hands
  107. new Float:x, Float:y, Float:z, Float:rx, Float:ry, Float:rz;
  108. AttachPoint(0.000,-0.300,0.000,0.000,0.000,0.000, 0.0, 0.0, 0.0, 0.0, 0.0, cangle, x, y, z, rx, ry, rz);
  109.  
  110. ClockData[index][ClockX] = cx+x;
  111. ClockData[index][ClockY] = cy+y;
  112. ClockData[index][ClockZ] = cz+z;
  113. ClockData[index][ClockRX] = rx;
  114. ClockData[index][ClockRY] = ry;
  115. ClockData[index][ClockRZ] = rz;
  116.  
  117. ClockData[index][ClockMinuteCenter] = CreateObject(1974,x+cx, y+cy, z+cz, rx, ry, rz,300.000);
  118. ClockData[index][ClockHourCenter] = CreateObject(1974,x+cx, y+cy, z+cz, rx, ry, rz,300.000);
  119. SetObjectMaterial(ClockData[index][ClockMinuteCenter], 0, 10765, "airportgnd_sfse", "black64", 0);
  120. SetObjectMaterial(ClockData[index][ClockHourCenter], 0, 10765, "airportgnd_sfse", "black64", 0);
  121.  
  122.  
  123. ClockData[index][ClockMinuteHand] = CreateObject(19482,0.007,-0.322,0.866,0.000,0.000,-90.000,300.000);
  124. SetObjectMaterialText(ClockData[index][ClockMinuteHand], "|", 0, 110, "Ariel", 100, 0, -16777216, 0, 1);
  125. AttachObjectToObject(ClockData[index][ClockMinuteHand], ClockData[index][ClockMinuteCenter],
  126. 0.0,-0.01,0.866,0.000,0.000,-90.000);
  127.  
  128.  
  129. ClockData[index][ClockHourHand] = CreateObject(19482,0.277,-0.322,0.016,90.000,0.000,-90.000,300.000);
  130. SetObjectMaterialText(ClockData[index][ClockHourHand], "|", 0, 120, "Ariel", 110, 0, -16777216, 0, 1);
  131. AttachObjectToObject(ClockData[index][ClockHourHand], ClockData[index][ClockHourCenter],
  132. 0.0,-0.01,0.646,0.000,0.000,-90.000);
  133.  
  134. // Translate clock rotation / set position
  135. for(new i = 0; i < 13; i++)
  136. {
  137. GetDynamicObjectPos(ClockData[index][ClockObjects][i], x, y, z);
  138. GetDynamicObjectRot(ClockData[index][ClockObjects][i], rx, ry, rz);
  139. AttachPoint(x, y, z, rx, ry, rz, 0.0, 0.0, 0.0, 0.0, 0.0, cangle, x, y, z, rx, ry, rz);
  140. SetDynamicObjectPos(ClockData[index][ClockObjects][i], cx+x, cy+y, cz+z);
  141. SetDynamicObjectRot(ClockData[index][ClockObjects][i], rx, ry, rz);
  142. }
  143. Iter_Add(Clocks, index);
  144. return index;
  145.  
  146. }
  147. printf("CreateClock::Too many clocks");
  148. return -1;
  149. }
  150.  
  151. DestroyClock(index)
  152. {
  153. if(Iter_Contains(Clocks, index))
  154. {
  155. DestroyObject(ClockData[index][ClockMinuteHand]);
  156. DestroyObject(ClockData[index][ClockMinuteCenter]);
  157. DestroyObject(ClockData[index][ClockHourHand]);
  158. DestroyObject(ClockData[index][ClockHourCenter]);
  159.  
  160. for(new i = 0; i < 13; i++) DestroyDynamicObject(ClockData[index][ClockObjects][i]);
  161. new next;
  162. Iter_SafeRemove(Clocks, index, next);
  163. return next;
  164. }
  165. printf("DestroyClock::Tried to destroy non-existing clock");
  166. return -1;
  167. }
  168.  
  169.  
  170.  
  171. forward UpdateClock();
  172. public UpdateClock()
  173. {
  174. new Hour, Minute, Second;
  175. gettime(Hour, Minute, Second);
  176. if(Hour >= 12) Hour -= 12;
  177.  
  178.  
  179. new Float:minutedeg, Float:hourdeg;
  180. minutedeg = (float(Minute) * 6.0) + (float(Second) / 10.0);
  181. hourdeg = (float(Hour) * 30.0) + ((float(Minute) * 6.0) / 12.0);
  182.  
  183. // Update clocks
  184. foreach(new i : Clocks)
  185. {
  186. SetObjectRot(ClockData[i][ClockMinuteCenter], ClockData[i][ClockRX], minutedeg, ClockData[i][ClockRZ]);
  187. SetObjectRot(ClockData[i][ClockHourCenter], ClockData[i][ClockRX], hourdeg, ClockData[i][ClockRZ]);
  188. }
  189. return 1;
  190. }
  191.  
  192. // Rotation function
  193. stock AttachPoint(Float:offx, Float:offy, Float:offz, Float:offrx, Float:offry, Float:offrz, Float:px, Float:py, Float:pz, Float:prx, Float:pry, Float:prz, &Float:RetX, &Float:RetY, &Float:RetZ, &Float:RetRX, &Float:RetRY, &Float:RetRZ, sync_rotation = 1)
  194. {
  195. new
  196. Float:g_sin[3],
  197. Float:g_cos[3],
  198. Float:off_x,
  199. Float:off_y,
  200. Float:off_z;
  201.  
  202. EDIT_FloatEulerFix(prx, pry, prz);
  203.  
  204. off_x = offx - px; // static offset
  205. off_y = offy - py; // static offset
  206. off_z = offz - pz; // static offset
  207.  
  208. // Calculate the new position
  209. EDIT_FloatConvertValue(prx, pry, prz, g_sin, g_cos);
  210. RetX = px + off_x * g_cos[1] * g_cos[2] - off_x * g_sin[0] * g_sin[1] * g_sin[2] - off_y * g_cos[0] * g_sin[2] + off_z * g_sin[1] * g_cos[2] + off_z * g_sin[0] * g_cos[1] * g_sin[2];
  211. RetY = py + off_x * g_cos[1] * g_sin[2] + off_x * g_sin[0] * g_sin[1] * g_cos[2] + off_y * g_cos[0] * g_cos[2] + off_z * g_sin[1] * g_sin[2] - off_z * g_sin[0] * g_cos[1] * g_cos[2];
  212. RetZ = pz - off_x * g_cos[0] * g_sin[1] + off_y * g_sin[0] + off_z * g_cos[0] * g_cos[1];
  213.  
  214. if (sync_rotation)
  215. {
  216. // Calculate the new rotation
  217. EDIT_FloatConvertValue(asin(g_cos[0] * g_cos[1]), atan2(g_sin[0], g_cos[0] * g_sin[1]) + offrz, atan2(g_cos[1] * g_cos[2] * g_sin[0] - g_sin[1] * g_sin[2], g_cos[2] * g_sin[1] - g_cos[1] * g_sin[0] * -g_sin[2]), g_sin, g_cos);
  218. EDIT_FloatConvertValue(asin(g_cos[0] * g_sin[1]), atan2(g_cos[0] * g_cos[1], g_sin[0]), atan2(g_cos[2] * g_sin[0] * g_sin[1] - g_cos[1] * g_sin[2], g_cos[1] * g_cos[2] + g_sin[0] * g_sin[1] * g_sin[2]), g_sin, g_cos);
  219. EDIT_FloatConvertValue(atan2(g_sin[0], g_cos[0] * g_cos[1]) + offrx, asin(g_cos[0] * g_sin[1]), atan2(g_cos[2] * g_sin[0] * g_sin[1] + g_cos[1] * g_sin[2], g_cos[1] * g_cos[2] - g_sin[0] * g_sin[1] * g_sin[2]), g_sin, g_cos);
  220.  
  221. RetRX = asin(g_cos[1] * g_sin[0]);
  222. RetRY = atan2(g_sin[1], g_cos[0] * g_cos[1]) + offry;
  223. RetRZ = atan2(g_cos[0] * g_sin[2] - g_cos[2] * g_sin[0] * g_sin[1], g_cos[0] * g_cos[2] + g_sin[0] * g_sin[1] * g_sin[2]);
  224. }
  225. }
  226.  
  227. stock EDIT_FloatConvertValue(Float:rot_x, Float:rot_y, Float:rot_z, Float:sin[3], Float:cos[3])
  228. {
  229. sin[0] = floatsin(rot_x, degrees);
  230. sin[1] = floatsin(rot_y, degrees);
  231. sin[2] = floatsin(rot_z, degrees);
  232. cos[0] = floatcos(rot_x, degrees);
  233. cos[1] = floatcos(rot_y, degrees);
  234. cos[2] = floatcos(rot_z, degrees);
  235. return 1;
  236. }
  237.  
  238. /*
  239. * Fixes a bug that causes objects to not rotate
  240. * correctly when rotating on the Z axis only.
  241. */
  242. stock EDIT_FloatEulerFix(&Float:rot_x, &Float:rot_y, &Float:rot_z)
  243. {
  244. EDIT_FloatGetRemainder(rot_x, rot_y, rot_z);
  245. if((!floatcmp(rot_x, 0.0) || !floatcmp(rot_x, 360.0))
  246. && (!floatcmp(rot_y, 0.0) || !floatcmp(rot_y, 360.0)))
  247. {
  248. rot_y = 0.00000002;
  249. }
  250. return 1;
  251. }
  252.  
  253. stock EDIT_FloatGetRemainder(&Float:rot_x, &Float:rot_y, &Float:rot_z)
  254. {
  255. EDIT_FloatRemainder(rot_x, 360.0);
  256. EDIT_FloatRemainder(rot_y, 360.0);
  257. EDIT_FloatRemainder(rot_z, 360.0);
  258. return 1;
  259. }
  260.  
  261. stock EDIT_FloatRemainder(&Float:remainder, Float:value)
  262. {
  263. if(remainder >= value)
  264. {
  265. while(remainder >= value)
  266. {
  267. remainder = remainder - value;
  268. }
  269. }
  270. else if(remainder < 0.0)
  271. {
  272. while(remainder < 0.0)
  273. {
  274. remainder = remainder + value;
  275. }
  276. }
  277. return 1;
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement