From f6a32186f7860eeb1e4e343f2a2d4538112d9d75 Mon Sep 17 00:00:00 2001 From: Doubleumc Date: Wed, 13 Dec 2023 04:15:40 -0500 Subject: [PATCH 1/7] initial --- code/__HELPERS/#maths.dm | 36 ------------ code/__HELPERS/unsorted.dm | 58 +++++++++---------- code/modules/cm_marines/radar.dm | 2 +- code/modules/cm_marines/smartgun_mount.dm | 2 +- code/modules/projectiles/projectile.dm | 2 +- code/modules/vehicles/hardpoints/hardpoint.dm | 2 +- 6 files changed, 31 insertions(+), 71 deletions(-) diff --git a/code/__HELPERS/#maths.dm b/code/__HELPERS/#maths.dm index ccd077003e62..81c5613328d5 100644 --- a/code/__HELPERS/#maths.dm +++ b/code/__HELPERS/#maths.dm @@ -106,42 +106,6 @@ GLOBAL_LIST_INIT(sqrtTable, list(1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, return "[round((powerused * 0.000001),0.001)] MW" return "[round((powerused * 0.000000001),0.0001)] GW" -///Calculate the angle between two movables and the west|east coordinate -/proc/get_angle(atom/movable/start, atom/movable/end)//For beams. - if(!start || !end) - return 0 - var/dy =(32 * end.y + end.pixel_y) - (32 * start.y + start.pixel_y) - var/dx =(32 * end.x + end.pixel_x) - (32 * start.x + start.pixel_x) - if(!dy) - return (dx >= 0) ? 90 : 270 - . = arctan(dx/dy) - if(dy < 0) - . += 180 - else if(dx < 0) - . += 360 - -/// Angle between two arbitrary points and horizontal line same as [/proc/get_angle] -/proc/get_angle_raw(start_x, start_y, start_pixel_x, start_pixel_y, end_x, end_y, end_pixel_x, end_pixel_y) - var/dy = (32 * end_y + end_pixel_y) - (32 * start_y + start_pixel_y) - var/dx = (32 * end_x + end_pixel_x) - (32 * start_x + start_pixel_x) - if(!dy) - return (dx >= 0) ? 90 : 270 - . = arctan(dx/dy) - if(dy < 0) - . += 180 - else if(dx < 0) - . += 360 - -///for getting the angle when animating something's pixel_x and pixel_y -/proc/get_pixel_angle(y, x) - if(!y) - return (x >= 0) ? 90 : 270 - . = arctan(x/y) - if(y < 0) - . += 180 - else if(x < 0) - . += 360 - /** * Get a list of turfs in a line from `starting_atom` to `ending_atom`. * diff --git a/code/__HELPERS/unsorted.dm b/code/__HELPERS/unsorted.dm index 0782826a4dc9..65ce48ff3178 100644 --- a/code/__HELPERS/unsorted.dm +++ b/code/__HELPERS/unsorted.dm @@ -99,38 +99,32 @@ var/atom/movable/big_subject = subject . += (big_subject.bound_height - world.icon_size) / 2 -/proc/Get_Angle(atom/start,atom/end, tile_bound = FALSE)//For beams. - if(!start || !end) return 0 - if(!start.z || !end.z) return 0 //Atoms are not on turfs. - var/dx - var/dy - if(tile_bound) - dy=end.y-start.y - dx=end.x-start.x - else - dy = get_pixel_position_y(end) - get_pixel_position_y(start) - dx = get_pixel_position_x(end) - get_pixel_position_x(start) - if(!dy) - return (dx>=0)?90:270 - .=arctan(dx/dy) - if(dy<0) - .+=180 - else if(dx<0) - .+=360 - -/proc/Get_Compass_Dir(atom/start,atom/end)//get_dir() only considers an object to be north/south/east/west if there is zero deviation. This uses rounding instead. - if(!start || !end) return 0 - if(!start.z || !end.z) return 0 //Atoms are not on turfs. - var/dy=end.y-start.y - var/dx=end.x-start.x +/proc/Get_Angle(atom/start, atom/end)//For beams. + if(!start || !end) + return 0 + if(!start.z) + start = get_turf(start) + if(!start) + return 0 //Atoms are not on turfs. + if(!end.z) + end = get_turf(end) + if(!end) + return 0 //Atoms are not on turfs. + var/dy = get_pixel_position_y(end) - get_pixel_position_y(start) + var/dx = get_pixel_position_x(end) - get_pixel_position_x(start) if(!dy) - return (dx>=0)?4:8 - var/angle=arctan(dx/dy) - if(dy<0) - angle+=180 - else if(dx<0) - angle+=360 - + return (dx >= 0) ? 90 : 270 + . = arctan(dx / dy) + if(dy < 0) + . += 180 + else if(dx < 0) + . += 360 + +/proc/Get_Pixel_Angle(dx, dy)//for getting the angle when animating something's pixel_x and pixel_y + var/da = (90 - Atan2(dx, dy)) + return (da >= 0 ? da : da + 360) + +/proc/angle_to_dir(angle) switch(angle) //diagonal directions get priority over straight directions in edge cases if (22.5 to 67.5) return NORTHEAST @@ -151,6 +145,8 @@ else return NORTH +/proc/Get_Compass_Dir(atom/start, atom/end)//get_dir() only considers an object to be north/south/east/west if there is zero deviation. This uses rounding instead. + return angle_to_dir(Get_Angle(start, end)) // Among other things, used by flamethrower and boiler spray to calculate if flame/spray can pass through. // Returns an atom for specific effects (primarily flames and acid spray) that damage things upon contact diff --git a/code/modules/cm_marines/radar.dm b/code/modules/cm_marines/radar.dm index f6751a8fe913..d088b68919b0 100644 --- a/code/modules/cm_marines/radar.dm +++ b/code/modules/cm_marines/radar.dm @@ -95,7 +95,7 @@ if(get_dist_euclidian(here_turf, target_turf) > 24) userot = TRUE - rot = round(get_angle(here_turf, target_turf)) + rot = round(Get_Angle(here_turf, target_turf)) else if(target_turf.z > here_turf.z) pointer="caret-up" diff --git a/code/modules/cm_marines/smartgun_mount.dm b/code/modules/cm_marines/smartgun_mount.dm index 8fb0b42a0123..2854daff2add 100644 --- a/code/modules/cm_marines/smartgun_mount.dm +++ b/code/modules/cm_marines/smartgun_mount.dm @@ -698,7 +698,7 @@ if(!istype(in_chamber, /obj/projectile)) return - var/angle = get_angle(T, U) + var/angle = Get_Angle(T, U) if((dir == NORTH) && (angle > 180) && (abs(360 - angle) > shoot_degree)) // If north and shooting to the left, we do some extra math return diff --git a/code/modules/projectiles/projectile.dm b/code/modules/projectiles/projectile.dm index ef265e0b8095..dc2075472122 100644 --- a/code/modules/projectiles/projectile.dm +++ b/code/modules/projectiles/projectile.dm @@ -298,7 +298,7 @@ //Change the bullet angle to its visual path - var/vis_angle = get_pixel_angle(x = pixel_x_target - pixel_x_source, y = pixel_y_target - pixel_y_source) //naming vars because the proc takes y then x and that's WEIRD + var/vis_angle = Get_Pixel_Angle(pixel_x_target - pixel_x_source, pixel_y_target - pixel_y_source) var/matrix/rotate = matrix() rotate.Turn(vis_angle) apply_transform(rotate) diff --git a/code/modules/vehicles/hardpoints/hardpoint.dm b/code/modules/vehicles/hardpoints/hardpoint.dm index acdefca18fd2..21e3e4b29f89 100644 --- a/code/modules/vehicles/hardpoints/hardpoint.dm +++ b/code/modules/vehicles/hardpoints/hardpoint.dm @@ -688,7 +688,7 @@ if(muzzle_turf == target_turf) return FALSE - var/angle_diff = SIMPLIFY_DEGREES(dir2angle(dir) - get_angle(muzzle_turf, target_turf)) + var/angle_diff = SIMPLIFY_DEGREES(dir2angle(dir) - Get_Angle(muzzle_turf, target_turf)) if(angle_diff < -180) angle_diff += 360 else if(angle_diff > 180) From f91d358c9cf69a0c56afbb9906e47f61b938fa8b Mon Sep 17 00:00:00 2001 From: Doubleumc Date: Thu, 14 Dec 2023 00:32:21 -0500 Subject: [PATCH 2/7] replaces Atan2 with arctan() switching to the native function introduced in 513 --- code/__HELPERS/#maths.dm | 8 +------- code/__HELPERS/unsorted.dm | 2 +- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/code/__HELPERS/#maths.dm b/code/__HELPERS/#maths.dm index 81c5613328d5..6ea534a79923 100644 --- a/code/__HELPERS/#maths.dm +++ b/code/__HELPERS/#maths.dm @@ -8,12 +8,6 @@ GLOBAL_LIST_INIT(sqrtTable, list(1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, // MATH DEFINES -#define Atan2(x, y) (!x && !y ? 0 : \ - (y >= 0 ? \ - arccos(x / sqrt(x*x + y*y)) : \ - -(arccos(x / sqrt(x*x + y*y))) \ - ) \ - ) #define Ceiling(x) (-round(-x)) #define Clamp(val, min_val, max_val) (max(min_val, min(val, max_val))) #define CLAMP01(x) (clamp(x, 0, 1)) @@ -78,7 +72,7 @@ GLOBAL_LIST_INIT(sqrtTable, list(1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, // Convert to polar coordinates var/radius = sqrt(relative_coords[1]**2 + relative_coords[2]**2) - var/phi = Atan2(relative_coords[1], relative_coords[2]) + var/phi = arctan(relative_coords[1], relative_coords[2]) // Rotate the point around the axis phi += degrees diff --git a/code/__HELPERS/unsorted.dm b/code/__HELPERS/unsorted.dm index 65ce48ff3178..d1944507b077 100644 --- a/code/__HELPERS/unsorted.dm +++ b/code/__HELPERS/unsorted.dm @@ -121,7 +121,7 @@ . += 360 /proc/Get_Pixel_Angle(dx, dy)//for getting the angle when animating something's pixel_x and pixel_y - var/da = (90 - Atan2(dx, dy)) + var/da = (90 - arctan(dx, dy)) return (da >= 0 ? da : da + 360) /proc/angle_to_dir(angle) From 18a56135af1f1c8c5d9c4c3501ee0781f30385f8 Mon Sep 17 00:00:00 2001 From: Doubleumc Date: Thu, 14 Dec 2023 00:44:42 -0500 Subject: [PATCH 3/7] projectile angle consistency no longer using its own bespoke copy of angle code --- code/modules/projectiles/projectile.dm | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/code/modules/projectiles/projectile.dm b/code/modules/projectiles/projectile.dm index dc2075472122..f44da069c917 100644 --- a/code/modules/projectiles/projectile.dm +++ b/code/modules/projectiles/projectile.dm @@ -243,20 +243,9 @@ vis_source_pixel_x = process_start_pixel_x vis_source_pixel_y = process_start_pixel_y - angle = 0 // Stolen from Get_Angle() basically var/dx = p_x + aim_turf.x * 32 - source_turf.x * 32 // todo account for firer offsets var/dy = p_y + aim_turf.y * 32 - source_turf.y * 32 - if(!dy) - if(dx >= 0) - angle = 90 - else - angle = 280 - else - angle = arctan(dx/dy) - if(dy < 0) - angle += 180 - else if(dx < 0) - angle += 360 + angle = Get_Pixel_Angle(dx, dy) /obj/projectile/process(delta_time) . = PROC_RETURN_SLEEP From 9e5120770848ba7059ca8a974dbbf5e4097755b0 Mon Sep 17 00:00:00 2001 From: Doubleumc Date: Thu, 14 Dec 2023 01:14:20 -0500 Subject: [PATCH 4/7] simplify Get_Pixel_Angle() --- code/__HELPERS/unsorted.dm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/__HELPERS/unsorted.dm b/code/__HELPERS/unsorted.dm index d1944507b077..6d5dc07244ef 100644 --- a/code/__HELPERS/unsorted.dm +++ b/code/__HELPERS/unsorted.dm @@ -121,7 +121,7 @@ . += 360 /proc/Get_Pixel_Angle(dx, dy)//for getting the angle when animating something's pixel_x and pixel_y - var/da = (90 - arctan(dx, dy)) + var/da = arctan(dy, dx) //y-then-x results in north-clockwise convention: https://en.wikipedia.org/wiki/Atan2#East-counterclockwise,_north-clockwise_and_south-clockwise_conventions,_etc. return (da >= 0 ? da : da + 360) /proc/angle_to_dir(angle) From c40919a3a7e316303fe157a1bef91a371bc3e8fe Mon Sep 17 00:00:00 2001 From: Doubleumc Date: Thu, 14 Dec 2023 01:22:08 -0500 Subject: [PATCH 5/7] simplify Get_Angle() arctan(x, y) can handle nulls and zeroes by itself --- code/__HELPERS/unsorted.dm | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/code/__HELPERS/unsorted.dm b/code/__HELPERS/unsorted.dm index 6d5dc07244ef..23c7a9f2dc08 100644 --- a/code/__HELPERS/unsorted.dm +++ b/code/__HELPERS/unsorted.dm @@ -112,12 +112,8 @@ return 0 //Atoms are not on turfs. var/dy = get_pixel_position_y(end) - get_pixel_position_y(start) var/dx = get_pixel_position_x(end) - get_pixel_position_x(start) - if(!dy) - return (dx >= 0) ? 90 : 270 - . = arctan(dx / dy) - if(dy < 0) - . += 180 - else if(dx < 0) + . = arctan(dy, dx) + if(. < 0) . += 360 /proc/Get_Pixel_Angle(dx, dy)//for getting the angle when animating something's pixel_x and pixel_y From b74d754c7b7076841e5162d31a55845e9c8b4834 Mon Sep 17 00:00:00 2001 From: Doubleumc Date: Thu, 14 Dec 2023 02:00:21 -0500 Subject: [PATCH 6/7] refactor Get_Pixel_Angle() Get_Pixel_Angle() renamed and refactored, Get_Angle() now calls it instead of having its own calcuation --- code/__HELPERS/unsorted.dm | 11 ++++++----- code/modules/projectiles/projectile.dm | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/code/__HELPERS/unsorted.dm b/code/__HELPERS/unsorted.dm index 23c7a9f2dc08..194db09bede8 100644 --- a/code/__HELPERS/unsorted.dm +++ b/code/__HELPERS/unsorted.dm @@ -99,6 +99,7 @@ var/atom/movable/big_subject = subject . += (big_subject.bound_height - world.icon_size) / 2 +/// Calculate the angle between two atoms. Uses north-clockwise convention: NORTH = 0, EAST = 90, etc. /proc/Get_Angle(atom/start, atom/end)//For beams. if(!start || !end) return 0 @@ -112,14 +113,14 @@ return 0 //Atoms are not on turfs. var/dy = get_pixel_position_y(end) - get_pixel_position_y(start) var/dx = get_pixel_position_x(end) - get_pixel_position_x(start) - . = arctan(dy, dx) + return delta_to_angle(dx, dy) + +/// Calculate the angle produced by a pair of x and y deltas. Uses north-clockwise convention: NORTH = 0, EAST = 90, etc. +/proc/delta_to_angle(dx, dy) + . = arctan(dy, dx) //y-then-x results in north-clockwise convention: https://en.wikipedia.org/wiki/Atan2#East-counterclockwise,_north-clockwise_and_south-clockwise_conventions,_etc. if(. < 0) . += 360 -/proc/Get_Pixel_Angle(dx, dy)//for getting the angle when animating something's pixel_x and pixel_y - var/da = arctan(dy, dx) //y-then-x results in north-clockwise convention: https://en.wikipedia.org/wiki/Atan2#East-counterclockwise,_north-clockwise_and_south-clockwise_conventions,_etc. - return (da >= 0 ? da : da + 360) - /proc/angle_to_dir(angle) switch(angle) //diagonal directions get priority over straight directions in edge cases if (22.5 to 67.5) diff --git a/code/modules/projectiles/projectile.dm b/code/modules/projectiles/projectile.dm index f44da069c917..a473c06b2b9d 100644 --- a/code/modules/projectiles/projectile.dm +++ b/code/modules/projectiles/projectile.dm @@ -245,7 +245,7 @@ var/dx = p_x + aim_turf.x * 32 - source_turf.x * 32 // todo account for firer offsets var/dy = p_y + aim_turf.y * 32 - source_turf.y * 32 - angle = Get_Pixel_Angle(dx, dy) + angle = delta_to_angle(dx, dy) /obj/projectile/process(delta_time) . = PROC_RETURN_SLEEP @@ -287,7 +287,7 @@ //Change the bullet angle to its visual path - var/vis_angle = Get_Pixel_Angle(pixel_x_target - pixel_x_source, pixel_y_target - pixel_y_source) + var/vis_angle = delta_to_angle(pixel_x_target - pixel_x_source, pixel_y_target - pixel_y_source) var/matrix/rotate = matrix() rotate.Turn(vis_angle) apply_transform(rotate) From cfd00d48c997bda656bb56de2ead2f852d8ba9e6 Mon Sep 17 00:00:00 2001 From: Doubleumc Date: Wed, 20 Dec 2023 16:40:23 -0500 Subject: [PATCH 7/7] Get_Compass_Dir get_turfs discard pixel offsets so projectile knockback behaves responsibly --- code/__HELPERS/unsorted.dm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/__HELPERS/unsorted.dm b/code/__HELPERS/unsorted.dm index 194db09bede8..7425046da7c7 100644 --- a/code/__HELPERS/unsorted.dm +++ b/code/__HELPERS/unsorted.dm @@ -143,7 +143,7 @@ return NORTH /proc/Get_Compass_Dir(atom/start, atom/end)//get_dir() only considers an object to be north/south/east/west if there is zero deviation. This uses rounding instead. - return angle_to_dir(Get_Angle(start, end)) + return angle_to_dir(Get_Angle(get_turf(start), get_turf(end))) // Among other things, used by flamethrower and boiler spray to calculate if flame/spray can pass through. // Returns an atom for specific effects (primarily flames and acid spray) that damage things upon contact