Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <a_samp>
- #define B_MAXLENGTH 15
- #define B_NUMBER 1
- #define B_XY 0.2
- #define B_XY_LIGHT_PLANES 0.5
- #define B_ANGV_LIGHT_PLANES 2.0
- #define B_XY_HEAVY_PLANES 6.0
- #define B_ANGV_HEAVY_PLANES 8.0
- #define B_Z 0.1
- #define B_MAXXY 5000
- #define B_TXT_SHOW_DELAY 7000
- #define B_TXT_HIDE_DELAY 2500
- #define B_DELAY 1000
- #define B_NONE -1
- new Text: b_textdraw;
- new Float: b_p1x[B_NUMBER][B_MAXLENGTH];
- new Float: b_p1y[B_NUMBER][B_MAXLENGTH];
- new Float: b_p2x[B_NUMBER][B_MAXLENGTH];
- new Float: b_p2y[B_NUMBER][B_MAXLENGTH];
- new b_players[MAX_PLAYERS] = { B_NONE, ... };
- new b_length[B_NUMBER];
- new b_maxid;
- new Float: b_vx[MAX_PLAYERS];
- new Float: b_vy[MAX_PLAYERS];
- new b_isoutside[MAX_PLAYERS];
- new b_showtimers[MAX_PLAYERS];
- forward BoundariesTextdrawShowTimer(playerid);
- forward BoundariesTextdrawHideTimer(playerid);
- forward BoundariesTimer(playerid);
- public BoundariesTextdrawShowTimer(playerid) {
- TextDrawShowForPlayer(playerid, b_textdraw);
- SetTimerEx("BoundariesTextdrawHideTimer", B_TXT_HIDE_DELAY, false, "d", playerid);
- }
- public BoundariesTextdrawHideTimer(playerid) {
- TextDrawHideForPlayer(playerid, b_textdraw);
- }
- public BoundariesTimer() {
- for (new playerid = 0; playerid < MAX_PLAYERS; playerid++) if (b_isoutside[playerid]) CalculateVelocity(playerid);
- }
- stock EdgeCrossesLine(Float: e1p1x, Float: e1p1y, Float: e1p2x, Float: e1p2y, Float: e2p1x, Float: e2p1y, Float: e2p2x, Float: e2p2y) {
- new Float: etmpx = e1p2x - e1p1x, Float: etmpy = e1p2y - e1p1y,
- Float: product1 = etmpx * (e2p1y - e1p1y) - (e2p1x - e1p1x) * etmpy, Float: product2 = etmpx * (e2p2y - e1p1y) - (e2p2x - e1p1x) * etmpy;
- return (floatabs(product1) < EPS) || (floatabs(product2) < EPS) || ((product1 < 0) ^ (product2 < 0));
- }
- stock BoxesIntersect(Float: e1p1x, Float: e1p1y, Float: e1p2x, Float: e1p2y, Float: e2p1x, Float: e2p1y, Float: e2p2x, Float: e2p2y) {
- return (e1p1x < e1p2x ? e1p1x : e1p2x) <= (e2p1x > e2p2x ? e2p1x : e2p2x) && (e1p1x > e1p2x ? e1p1x : e1p2x) >= (e2p1x < e2p2x ? e2p1x : e2p2x) &&
- (e1p1y < e1p2y ? e1p1y : e1p2y) <= (e2p1y > e2p2y ? e2p1y : e2p2y) && (e1p1y > e1p2y ? e1p1y : e1p2y) >= (e2p1y < e2p2y ? e2p1y : e2p2y);
- }
- stock Intersect(Float: e1p1x, Float: e1p1y, Float: e1p2x, Float: e1p2y, Float: e2p1x, Float: e2p1y, Float: e2p2x, Float: e2p2y) {
- return BoxesIntersect(e1p1x, e1p1y, e1p2x, e1p2y, e2p1x, e2p1y, e2p2x, e2p2y) && EdgeCrossesLine(e1p1x, e1p1y, e1p2x, e1p2y, e2p1x, e2p1y, e2p2x, e2p2y) &&
- EdgeCrossesLine(e2p1x, e2p1y, e2p2x, e2p2y, e1p1x, e1p1y, e1p2x, e1p2y);
- }
- stock PlayerWithinBoundaries(playerid, bid) {
- new Float: x, Float: y, Float: rayy;
- new i, ctr = 0, flag = 0;
- GetPlayerPos(playerid, x, y, rayy);
- rayy = y;
- for (i = 0; i < b_length[bid]; i++) {
- if (floatabs(b_p1y[bid][i] - y) < EPS || floatabs(b_p2y[bid][i] - y) < EPS) { flag = 1; rayy += EPS; }
- if (Intersect(x, rayy, B_MAXXY, rayy, b_p1x[bid][i], b_p1y[bid][i], b_p2x[bid][i], b_p2y[bid][i])) ctr++;
- if (flag) { flag = 0; rayy -= EPS; }
- }
- return ctr%2;
- }
- stock InitBoundaries() {
- b_textdraw = TextDrawCreate(320, 216, "STAY WITHIN THE ~r~WORLD BOUNDRIES");
- TextDrawAlignment(b_textdraw, 2);
- TextDrawSetShadow(b_textdraw, 0);
- TextDrawSetOutline(b_textdraw, 2);
- TextDrawLetterSize(b_textdraw, 0.6, 2.9);
- TextDrawFont(b_textdraw, 2);
- SetTimer("BoundariesTimer", B_DELAY, true);
- }
- stock AssignBoundaries(playerid, bid) {
- if (b_players[playerid] != B_NONE) {
- TextDrawHideForPlayer(playerid, b_textdraw);
- KillTimer(b_showtimers[playerid]);
- }
- b_vx[playerid] = b_vy[playerid] = b_isoutside[playerid] = b_showtimers[playerid] = 0;
- b_players[playerid] = bid;
- }
- stock RemoveBoundaries(playerid) {
- AssignBoundaries(playerid, B_NONE);
- }
- stock CreateBoundaries(Float: array[], points) {
- if (b_maxid >= B_NUMBER || points < 6) return B_NONE;
- b_length[b_maxid] = points/2;
- for (new i = 0; i < b_length[b_maxid] - 1; i++) {
- b_p1x[b_maxid][i] = array[i*2];
- b_p1y[b_maxid][i] = array[i*2 + 1];
- b_p2x[b_maxid][i] = array[(i + 1)*2];
- b_p2y[b_maxid][i] = array[(i + 1)*2 + 1];
- }
- b_p1x[b_maxid][b_length[b_maxid] - 1] = array[b_length[b_maxid]*2 - 2];
- b_p1y[b_maxid][b_length[b_maxid] - 1] = array[b_length[b_maxid]*2 - 1];
- b_p2x[b_maxid][b_length[b_maxid] - 1] = array[0];
- b_p2y[b_maxid][b_length[b_maxid] - 1] = array[1];
- return b_maxid++;
- }
- stock CalculateVelocity(playerid, Float: b_xy = B_XY) {
- new Float: x, Float: y, Float: z, Float: clx, Float: cly, Float: clx_tmp, Float: cly_tmp, Float: mindistance = B_MAXXY*B_MAXXY, Float: distance;
- GetPlayerPos(playerid, x, y, z);
- for (new i = 0; i < b_length[b_players[playerid]]; i++) {
- if (floatabs(b_p2x[b_players[playerid]][i] - b_p1x[b_players[playerid]][i]) < EPS) {
- clx_tmp = b_p1x[b_players[playerid]][i];
- cly_tmp = y;
- }
- else if (floatabs(b_p2y[b_players[playerid]][i] - b_p1y[b_players[playerid]][i]) < EPS) {
- clx_tmp = x;
- cly_tmp = b_p1y[b_players[playerid]][i];
- }
- else {
- new Float: k1, Float: k2, Float: b1, Float: b2;
- k1 = (b_p2y[b_players[playerid]][i] - b_p1y[b_players[playerid]][i])/(b_p2x[b_players[playerid]][i] -
- b_p1x[b_players[playerid]][i]);
- b1 = b_p1y[b_players[playerid]][i] - k1*b_p1x[b_players[playerid]][i];
- k2 = -1/k1;
- b2 = y - k2*x;
- clx_tmp = (b2 - b1)/(k1 - k2);
- cly_tmp = k2*clx_tmp + b2;
- }
- if (clx_tmp < (b_p1x[b_players[playerid]][i] < b_p2x[b_players[playerid]][i] ? b_p1x[b_players[playerid]][i] :
- b_p2x[b_players[playerid]][i]) ||
- clx_tmp > (b_p1x[b_players[playerid]][i] > b_p2x[b_players[playerid]][i] ? b_p1x[b_players[playerid]][i] :
- b_p2x[b_players[playerid]][i]) ||
- cly_tmp < (b_p1y[b_players[playerid]][i] < b_p2y[b_players[playerid]][i] ? b_p1y[b_players[playerid]][i] :
- b_p2y[b_players[playerid]][i]) ||
- cly_tmp > (b_p1y[b_players[playerid]][i] > b_p2y[b_players[playerid]][i] ? b_p1y[b_players[playerid]][i] :
- b_p2y[b_players[playerid]][i])) {
- distance = (x - b_p1x[b_players[playerid]][i])*(x - b_p1x[b_players[playerid]][i]) +
- (y - b_p1y[b_players[playerid]][i])*(y - b_p1y[b_players[playerid]][i]);
- z = (x - b_p2x[b_players[playerid]][i])*(x - b_p2x[b_players[playerid]][i]) +
- (y - b_p2y[b_players[playerid]][i])*(y - b_p2y[b_players[playerid]][i]);
- if (distance < z) {
- clx_tmp = b_p1x[b_players[playerid]][i];
- cly_tmp = b_p1y[b_players[playerid]][i];
- }
- else {
- distance = z;
- clx_tmp = b_p2x[b_players[playerid]][i];
- cly_tmp = b_p2y[b_players[playerid]][i];
- }
- }
- else distance = (x - clx_tmp)*(x - clx_tmp) + (y - cly_tmp)*(y - cly_tmp);
- if (distance < mindistance) {
- mindistance = distance;
- clx = clx_tmp;
- cly = cly_tmp;
- }
- }
- if (floatabs(x - clx) > floatabs(y - cly)) {
- if (x > clx) b_vx[playerid] = -b_xy;
- else b_vx[playerid] = b_xy;
- if (y > cly) b_vy[playerid] = -floatabs((y - cly)/(x - clx))*b_xy;
- else b_vy[playerid] = floatabs((y - cly)/(x - clx))*b_xy;
- }
- else {
- if (y > cly) b_vy[playerid] = -b_xy;
- else b_vy[playerid] = b_xy;
- if (x > clx) b_vx[playerid] = -floatabs((x - clx)/(y - cly))*b_xy;
- else b_vx[playerid] = floatabs((x - clx)/(y - cly))*b_xy;
- }
- }
- stock ProcessBoundaries(playerid) {
- if (PlayerWithinBoundaries(playerid, b_players[playerid])) {
- if (b_isoutside[playerid]) {
- b_isoutside[playerid] = false;
- b_vx[playerid] = b_vy[playerid] = 0;
- KillTimer(b_showtimers[playerid]);
- }
- }
- else {
- if (!b_isoutside[playerid]) {
- new vid = GetPlayerVehicleID(playerid);
- new vm = GetVehicleModel(vid);
- b_isoutside[playerid] = true;
- TextDrawShowForPlayer(playerid, b_textdraw);
- SetTimerEx("BoundariesTextdrawHideTimer", B_TXT_HIDE_DELAY, false, "d", playerid);
- b_showtimers[playerid] = SetTimerEx("BoundariesTextdrawShowTimer", B_TXT_SHOW_DELAY, true, "d", playerid);
- if (vm && (vm == 460 || vm == 476 || vm == 511 || vm == 512 || vm == 513 || vm == 519 || vm == 520 || vm == 553 || vm == 577 || vm == 592 || vm == 593)) {
- new Float: x, Float: y, Float: z, Float: angv, Float: b_xy;
- if (vm == 511 || vm == 553 || vm == 577 || vm == 592) {
- angv = B_ANGV_HEAVY_PLANES;
- b_xy = B_XY_HEAVY_PLANES;
- }
- else {
- angv = B_ANGV_LIGHT_PLANES;
- b_xy = B_XY_LIGHT_PLANES;
- }
- GetVehicleVelocity(vid, x, y, z);
- CalculateVelocity(playerid, b_xy);
- if (atan2(x*b_vy[playerid] - y*b_vx[playerid], x*b_vx[playerid] + y*b_vy[playerid]) > 0) SetVehicleAngularVelocity(vid, 0.0, 0.0, angv);
- else SetVehicleAngularVelocity(vid, 0.0, 0.0, -angv);
- }
- else {
- new s_veh = GetPlayerSurfingVehicleID(playerid);
- new s_mod = GetVehicleModel(s_veh);
- if (s_veh != INVALID_VEHICLE_ID && (s_mod == 449 || s_mod == 537 || s_mod == 538 || s_mod == 569 || s_mod == 570)) {
- new Float: x, Float: y, Float: z;
- GetPlayerPos(playerid, x, y, z);
- SetPlayerPos(playerid, x, y, z + 2);
- }
- CalculateVelocity(playerid);
- }
- }
- if (IsPlayerInAnyVehicle(playerid)) SetVehicleVelocity(GetPlayerVehicleID(playerid), b_vx[playerid], b_vy[playerid], B_Z);
- else SetPlayerVelocity(playerid, b_vx[playerid], b_vy[playerid], B_Z);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement