Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Analog clock by [uL]Pottus
- #include <a_samp>
- #include <YSI\y_iterate>
- #include <streamer>
- #include <zcmd>
- CMD:testclock(playerid, arg[])
- {
- new Float:x, Float:y, Float:z, Float:fa;
- GetPlayerPos(playerid, x, y, z);
- GetPlayerFacingAngle(playerid, fa);
- CreateClock(x, y, z, fa);
- return 1;
- }
- #define MAX_CLOCKS 10
- #define CLOCK_UPDATE_TIME 1000
- enum CLOCKINFO
- {
- Float:ClockX,
- Float:ClockY,
- Float:ClockZ,
- Float:ClockRX,
- Float:ClockRY,
- Float:ClockRZ,
- Float:ClockFA,
- ClockMinuteCenter,
- ClockHourCenter,
- ClockMinuteHand,
- ClockHourHand,
- ClockObjects[13],
- }
- static ClockData[MAX_CLOCKS][CLOCKINFO];
- static Iterator:Clocks<MAX_CLOCKS>;
- static ClockTimer;
- public OnFilterScriptInit()
- {
- ClockTimer = SetTimer("UpdateClock", CLOCK_UPDATE_TIME, true);
- return 1;
- }
- public OnGameModeInit()
- {
- SetTimer("UpdateClock", CLOCK_UPDATE_TIME, true);
- KillTimer(ClockTimer);
- return 1;
- }
- public OnFilterScriptExit()
- {
- foreach(new i : Clocks) i = DestroyClock(i);
- KillTimer(ClockTimer);
- return 1;
- }
- public OnGameModeExit()
- {
- foreach(new i : Clocks) i = DestroyClock(i);
- return 1;
- }
- stock CreateClock(Float:cx, Float:cy, Float:cz, Float:cangle)
- {
- new index = Iter_Free(Clocks);
- if(index > -1)
- {
- ClockData[index][ClockFA] = cangle;
- // Static clock
- 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);
- SetDynamicObjectMaterial(ClockData[index][ClockObjects][0], 0, 10765, "airportgnd_sfse", "white", 0);
- SetDynamicObjectMaterial(ClockData[index][ClockObjects][0], 1, 10765, "airportgnd_sfse", "white", 0);
- 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);
- SetDynamicObjectMaterialText(ClockData[index][ClockObjects][1], 0, "12", 130, "Ariel", 199, 1, -16777216, 0, 1);
- 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);
- SetDynamicObjectMaterialText(ClockData[index][ClockObjects][2], 0, "6", 130, "Ariel", 199, 1, -16777216, 0, 1);
- 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);
- SetDynamicObjectMaterialText(ClockData[index][ClockObjects][3], 0, "9", 130, "Ariel", 199, 1, -16777216, 0, 1);
- 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);
- SetDynamicObjectMaterialText(ClockData[index][ClockObjects][4], 0, "3", 130, "Ariel", 199, 1, -16777216, 0, 1);
- 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);
- SetDynamicObjectMaterialText(ClockData[index][ClockObjects][5], 0, "11", 130, "Ariel", 199, 1, -16777216, 0, 1);
- 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);
- SetDynamicObjectMaterialText(ClockData[index][ClockObjects][6], 0, "5", 130, "Ariel", 199, 1, -16777216, 0, 1);
- 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);
- SetDynamicObjectMaterialText(ClockData[index][ClockObjects][7], 0, "8", 130, "Ariel", 199, 1, -16777216, 0, 1);
- 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);
- SetDynamicObjectMaterialText(ClockData[index][ClockObjects][8], 0, "2", 130, "Ariel", 199, 1, -16777216, 0, 1);
- 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);
- SetDynamicObjectMaterialText(ClockData[index][ClockObjects][9], 0, "10", 130, "Ariel", 199, 1, -16777216, 0, 1);
- 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);
- SetDynamicObjectMaterialText(ClockData[index][ClockObjects][10], 0, "4", 130, "Ariel", 199, 1, -16777216, 0, 1);
- 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);
- SetDynamicObjectMaterialText(ClockData[index][ClockObjects][11], 0, "7", 130, "Ariel", 199, 1, -16777216, 0, 1);
- 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);
- SetDynamicObjectMaterialText(ClockData[index][ClockObjects][12], 0, "1", 130, "Ariel", 199, 1, -16777216, 0, 1);
- // Clock hands
- new Float:x, Float:y, Float:z, Float:rx, Float:ry, Float:rz;
- 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);
- ClockData[index][ClockX] = cx+x;
- ClockData[index][ClockY] = cy+y;
- ClockData[index][ClockZ] = cz+z;
- ClockData[index][ClockRX] = rx;
- ClockData[index][ClockRY] = ry;
- ClockData[index][ClockRZ] = rz;
- ClockData[index][ClockMinuteCenter] = CreateObject(1974,x+cx, y+cy, z+cz, rx, ry, rz,300.000);
- ClockData[index][ClockHourCenter] = CreateObject(1974,x+cx, y+cy, z+cz, rx, ry, rz,300.000);
- SetObjectMaterial(ClockData[index][ClockMinuteCenter], 0, 10765, "airportgnd_sfse", "black64", 0);
- SetObjectMaterial(ClockData[index][ClockHourCenter], 0, 10765, "airportgnd_sfse", "black64", 0);
- ClockData[index][ClockMinuteHand] = CreateObject(19482,0.007,-0.322,0.866,0.000,0.000,-90.000,300.000);
- SetObjectMaterialText(ClockData[index][ClockMinuteHand], "|", 0, 110, "Ariel", 100, 0, -16777216, 0, 1);
- AttachObjectToObject(ClockData[index][ClockMinuteHand], ClockData[index][ClockMinuteCenter],
- 0.0,-0.01,0.866,0.000,0.000,-90.000);
- ClockData[index][ClockHourHand] = CreateObject(19482,0.277,-0.322,0.016,90.000,0.000,-90.000,300.000);
- SetObjectMaterialText(ClockData[index][ClockHourHand], "|", 0, 120, "Ariel", 110, 0, -16777216, 0, 1);
- AttachObjectToObject(ClockData[index][ClockHourHand], ClockData[index][ClockHourCenter],
- 0.0,-0.01,0.646,0.000,0.000,-90.000);
- // Translate clock rotation / set position
- for(new i = 0; i < 13; i++)
- {
- GetDynamicObjectPos(ClockData[index][ClockObjects][i], x, y, z);
- GetDynamicObjectRot(ClockData[index][ClockObjects][i], rx, ry, rz);
- AttachPoint(x, y, z, rx, ry, rz, 0.0, 0.0, 0.0, 0.0, 0.0, cangle, x, y, z, rx, ry, rz);
- SetDynamicObjectPos(ClockData[index][ClockObjects][i], cx+x, cy+y, cz+z);
- SetDynamicObjectRot(ClockData[index][ClockObjects][i], rx, ry, rz);
- }
- Iter_Add(Clocks, index);
- return index;
- }
- printf("CreateClock::Too many clocks");
- return -1;
- }
- DestroyClock(index)
- {
- if(Iter_Contains(Clocks, index))
- {
- DestroyObject(ClockData[index][ClockMinuteHand]);
- DestroyObject(ClockData[index][ClockMinuteCenter]);
- DestroyObject(ClockData[index][ClockHourHand]);
- DestroyObject(ClockData[index][ClockHourCenter]);
- for(new i = 0; i < 13; i++) DestroyDynamicObject(ClockData[index][ClockObjects][i]);
- new next;
- Iter_SafeRemove(Clocks, index, next);
- return next;
- }
- printf("DestroyClock::Tried to destroy non-existing clock");
- return -1;
- }
- forward UpdateClock();
- public UpdateClock()
- {
- new Hour, Minute, Second;
- gettime(Hour, Minute, Second);
- if(Hour >= 12) Hour -= 12;
- new Float:minutedeg, Float:hourdeg;
- minutedeg = (float(Minute) * 6.0) + (float(Second) / 10.0);
- hourdeg = (float(Hour) * 30.0) + ((float(Minute) * 6.0) / 12.0);
- // Update clocks
- foreach(new i : Clocks)
- {
- SetObjectRot(ClockData[i][ClockMinuteCenter], ClockData[i][ClockRX], minutedeg, ClockData[i][ClockRZ]);
- SetObjectRot(ClockData[i][ClockHourCenter], ClockData[i][ClockRX], hourdeg, ClockData[i][ClockRZ]);
- }
- return 1;
- }
- // Rotation function
- 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)
- {
- new
- Float:g_sin[3],
- Float:g_cos[3],
- Float:off_x,
- Float:off_y,
- Float:off_z;
- EDIT_FloatEulerFix(prx, pry, prz);
- off_x = offx - px; // static offset
- off_y = offy - py; // static offset
- off_z = offz - pz; // static offset
- // Calculate the new position
- EDIT_FloatConvertValue(prx, pry, prz, g_sin, g_cos);
- 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];
- 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];
- RetZ = pz - off_x * g_cos[0] * g_sin[1] + off_y * g_sin[0] + off_z * g_cos[0] * g_cos[1];
- if (sync_rotation)
- {
- // Calculate the new rotation
- 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);
- 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);
- 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);
- RetRX = asin(g_cos[1] * g_sin[0]);
- RetRY = atan2(g_sin[1], g_cos[0] * g_cos[1]) + offry;
- 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]);
- }
- }
- stock EDIT_FloatConvertValue(Float:rot_x, Float:rot_y, Float:rot_z, Float:sin[3], Float:cos[3])
- {
- sin[0] = floatsin(rot_x, degrees);
- sin[1] = floatsin(rot_y, degrees);
- sin[2] = floatsin(rot_z, degrees);
- cos[0] = floatcos(rot_x, degrees);
- cos[1] = floatcos(rot_y, degrees);
- cos[2] = floatcos(rot_z, degrees);
- return 1;
- }
- /*
- * Fixes a bug that causes objects to not rotate
- * correctly when rotating on the Z axis only.
- */
- stock EDIT_FloatEulerFix(&Float:rot_x, &Float:rot_y, &Float:rot_z)
- {
- EDIT_FloatGetRemainder(rot_x, rot_y, rot_z);
- if((!floatcmp(rot_x, 0.0) || !floatcmp(rot_x, 360.0))
- && (!floatcmp(rot_y, 0.0) || !floatcmp(rot_y, 360.0)))
- {
- rot_y = 0.00000002;
- }
- return 1;
- }
- stock EDIT_FloatGetRemainder(&Float:rot_x, &Float:rot_y, &Float:rot_z)
- {
- EDIT_FloatRemainder(rot_x, 360.0);
- EDIT_FloatRemainder(rot_y, 360.0);
- EDIT_FloatRemainder(rot_z, 360.0);
- return 1;
- }
- stock EDIT_FloatRemainder(&Float:remainder, Float:value)
- {
- if(remainder >= value)
- {
- while(remainder >= value)
- {
- remainder = remainder - value;
- }
- }
- else if(remainder < 0.0)
- {
- while(remainder < 0.0)
- {
- remainder = remainder + value;
- }
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement