<commit_before>﻿using HamstarHelpers.Helpers.Debug;
using HamstarHelpers.Helpers.UI;
using Microsoft.Xna.Framework;
using System;
using Terraria;


namespace HamstarHelpers.Helpers.HUD {
	/// <summary>
	/// Assorted static "helper" functions pertaining to the HUD map.
	/// </summary>
	public partial class HUDMapHelpers {
		/// <summary>
		/// Gets the scale (zoom) of the full screen map.
		/// </summary>
		/// <returns></returns>
		public static float GetFullMapScale() {
			return Main.mapFullscreenScale / Main.UIScale;
		}


		////////////////

		/// <summary>
		/// Returns a screen position of a given world position as if projected onto the fullscreen map.
		/// </summary>
		/// <param name="worldPosition"></param>
		/// <returns>A tuple indicating the screen-relative position and whether the point is within the screen
		/// boundaries.</returns>
		public static (Vector2 ScreenPosition, bool IsOnScreen) GetFullMapPositionAsScreenPosition( Vector2 worldPosition ) {    //Main.mapFullscreen
			return HUDMapHelpers.GetFullMapPositionAsScreenPosition( new Rectangle( (int)worldPosition.X, (int)worldPosition.Y, 0, 0 ) );
		}

		/// <summary>
		/// Returns a screen position of a given world position as if projected onto the fullscreen map.
		/// </summary>
		/// <param name="worldArea"></param>
		/// <returns>A tuple indicating the screen-relative position and whether the point is within the screen
		/// boundaries.</returns>
		public static (Vector2 ScreenPosition, bool IsOnScreen) GetFullMapPositionAsScreenPosition( Rectangle worldArea ) {    //Main.mapFullscreen
			float mapScale = HUDMapHelpers.GetFullMapScale();
			var scrSize = UIHelpers.GetScreenSize();

			//float offscrLitX = 10f * mapScale;
			//float offscrLitY = 10f * mapScale;

			float mapFullscrX = Main.mapFullscreenPos.X * mapScale;
			float mapFullscrY = Main.mapFullscreenPos.Y * mapScale;
			float mapX = -mapFullscrX + (float)(Main.screenWidth / 2);
			float mapY = -mapFullscrY + (float)(Main.screenHeight / 2);

			float originMidX = (worldArea.X / 16f) * mapScale;
			float originMidY = (worldArea.Y / 16f) * mapScale;

			originMidX += mapX;
			originMidY += mapY;

			var scrPos = new Vector2( originMidX, originMidY );
			bool isOnscreen = originMidX >= 0 &&
				originMidY >= 0 &&
				originMidX < scrSize.Item1 &&
				originMidY < scrSize.Item2;

			return ( scrPos, isOnscreen );
		}


		////////////////

		/// <summary>
		/// Gets the scaled dimensions of a given width and height as if projectected onto the full screen map.
		/// </summary>
		/// <param name="width"></param>
		/// <param name="height"></param>
		/// <returns></returns>
		public static Vector2 GetSizeOnFullMap( float width, float height ) {   //Main.mapFullscreen 
			float baseX = Main.screenPosition.X;
			float baseY = Main.screenPosition.Y;

			Vector2 mapBasePos = HUDMapHelpers.GetFullMapPositionAsScreenPosition( new Rectangle( (int)baseX, (int)baseY, 0, 0 ) ).ScreenPosition;
			Vector2 mapNewPos = HUDMapHelpers.GetFullMapPositionAsScreenPosition( new Rectangle( (int)(baseX + width), (int)(baseY + height), 0, 0 ) ).ScreenPosition;

			return mapNewPos - mapBasePos;
		}


		////////////////

		/// <summary>
		/// Outputs the world tile position of a given screen position in the fullscreen map.
		/// </summary>
		/// <param name="scrPosX"></param>
		/// <param name="scrPosY"></param>
		/// <param name="tilePos"></param>
		/// <returns>`true` if the tile is within world bounds.</returns>
		public static bool GetFullscreenMapTileOfScreenPosition( int scrPosX, int scrPosY, out (int x, int y) tilePos ) {
			float mapScale = Main.mapFullscreenScale;
			float minX = 10f;
			float minY = 10f;
			float maxX = (float)( Main.maxTilesX - 10 );
			float maxY = (float)( Main.maxTilesY - 10 );

			float mapPosX = Main.mapFullscreenPos.X * mapScale;
			float mapPosY = Main.mapFullscreenPos.Y * mapScale;

			float scrOriginX = (float)(Main.screenWidth / 2) - mapPosX;
			scrOriginX += minX * mapScale;
			float scrOriginY = (float)(Main.screenHeight / 2) - mapPosY;
			scrOriginY += minX * mapScale;

			int tileX = (int)(((float)scrPosX - scrOriginX) / mapScale + minX);
			int tileY = (int)(((float)scrPosY - scrOriginY) / mapScale + minY);

			tilePos = (tileX, tileY);
			return tileX >= minX
				&& tileX < maxX
				&& tileY >= minY
				&& tileY < maxY;
		}


		////////////////

		/// <summary>
		/// Zooms in to find the top left tile position of the current map view.
		/// </summary>
		/// <returns></returns>
		public static (int TileX, int TileY) FindTopLeftTileOfFullscreenMap() {
			return HUDMapHelpers.FindTopLeftTileOfFullscreenMapStartingAt( Main.maxTilesX / 2, Main.maxTilesY / 2 );
		}

		private static (int TileX, int TileY) FindTopLeftTileOfFullscreenMapStartingAt( int tileX, int tileY ) {
			int prevLeftX = 0;
			int prevRightX = Main.maxTilesX;
			int prevTopY = 0;
			int prevBotY = Main.maxTilesY;

			//

			void IncreaseX( ref int x ) {
				prevLeftX = x;
				x += ( prevRightX - x ) / 2;
			}
			void DecreaseX( ref int x ) {
				prevRightX = x;
				x += ( prevLeftX - x ) / 2;
			}
			void IncreaseY( ref int y ) {
				prevTopY = y;
				y += ( prevBotY - y ) / 2;
			}
			void DecreaseY( ref int y ) {
				prevBotY = y;
				y += ( prevTopY - y ) / 2;
			}

			//

			(Vector2 ScreenPosition, bool) mapPos;

			while( true ) {
				bool foundX = false;
				mapPos = HUDMapHelpers.GetFullMapPositionAsScreenPosition( new Vector2( tileX, tileY ) );

				if( (int)mapPos.ScreenPosition.X < 0 ) {
					IncreaseX( ref tileX );
				} else if( (int)mapPos.ScreenPosition.X > 0 ) {
					DecreaseX( ref tileX );
				} else {
					foundX = true;
				}

				if( (int)mapPos.ScreenPosition.Y < 0 ) {
					IncreaseY( ref tileY );
				} else if( (int)mapPos.ScreenPosition.Y > 0 ) {
					DecreaseY( ref tileY );
				} else if( foundX ) {
					return (tileX, tileY);
				}
			}
		}
	}
}<commit_msg>v5.6.0 - Fixed HUDMapHelpers.FindTopLeftTileOfFullscreenMap() scale<commit_after>﻿using HamstarHelpers.Helpers.Debug;
using HamstarHelpers.Helpers.UI;
using Microsoft.Xna.Framework;
using System;
using Terraria;


namespace HamstarHelpers.Helpers.HUD {
	/// <summary>
	/// Assorted static "helper" functions pertaining to the HUD map.
	/// </summary>
	public partial class HUDMapHelpers {
		/// <summary>
		/// Gets the scale (zoom) of the full screen map.
		/// </summary>
		/// <returns></returns>
		public static float GetFullMapScale() {
			return Main.mapFullscreenScale / Main.UIScale;
		}


		////////////////

		/// <summary>
		/// Returns a screen position of a given world position as if projected onto the fullscreen map.
		/// </summary>
		/// <param name="worldPosition"></param>
		/// <returns>A tuple indicating the screen-relative position and whether the point is within the screen
		/// boundaries.</returns>
		public static (Vector2 ScreenPosition, bool IsOnScreen) GetFullMapPositionAsScreenPosition( Vector2 worldPosition ) {    //Main.mapFullscreen
			return HUDMapHelpers.GetFullMapPositionAsScreenPosition( new Rectangle( (int)worldPosition.X, (int)worldPosition.Y, 0, 0 ) );
		}

		/// <summary>
		/// Returns a screen position of a given world position as if projected onto the fullscreen map.
		/// </summary>
		/// <param name="worldArea"></param>
		/// <returns>A tuple indicating the screen-relative position and whether the point is within the screen
		/// boundaries.</returns>
		public static (Vector2 ScreenPosition, bool IsOnScreen) GetFullMapPositionAsScreenPosition( Rectangle worldArea ) {    //Main.mapFullscreen
			float mapScale = HUDMapHelpers.GetFullMapScale();
			var scrSize = UIHelpers.GetScreenSize();

			//float offscrLitX = 10f * mapScale;
			//float offscrLitY = 10f * mapScale;

			float mapFullscrX = Main.mapFullscreenPos.X * mapScale;
			float mapFullscrY = Main.mapFullscreenPos.Y * mapScale;
			float mapX = -mapFullscrX + (float)(Main.screenWidth / 2);
			float mapY = -mapFullscrY + (float)(Main.screenHeight / 2);

			float originMidX = (worldArea.X / 16f) * mapScale;
			float originMidY = (worldArea.Y / 16f) * mapScale;

			originMidX += mapX;
			originMidY += mapY;

			var scrPos = new Vector2( originMidX, originMidY );
			bool isOnscreen = originMidX >= 0 &&
				originMidY >= 0 &&
				originMidX < scrSize.Item1 &&
				originMidY < scrSize.Item2;

			return ( scrPos, isOnscreen );
		}


		////////////////

		/// <summary>
		/// Gets the scaled dimensions of a given width and height as if projectected onto the full screen map.
		/// </summary>
		/// <param name="width"></param>
		/// <param name="height"></param>
		/// <returns></returns>
		public static Vector2 GetSizeOnFullMap( float width, float height ) {   //Main.mapFullscreen 
			float baseX = Main.screenPosition.X;
			float baseY = Main.screenPosition.Y;

			Vector2 mapBasePos = HUDMapHelpers.GetFullMapPositionAsScreenPosition( new Rectangle( (int)baseX, (int)baseY, 0, 0 ) ).ScreenPosition;
			Vector2 mapNewPos = HUDMapHelpers.GetFullMapPositionAsScreenPosition( new Rectangle( (int)(baseX + width), (int)(baseY + height), 0, 0 ) ).ScreenPosition;

			return mapNewPos - mapBasePos;
		}


		////////////////

		/// <summary>
		/// Outputs the world tile position of a given screen position in the fullscreen map.
		/// </summary>
		/// <param name="scrPosX"></param>
		/// <param name="scrPosY"></param>
		/// <param name="tilePos"></param>
		/// <returns>`true` if the tile is within world bounds.</returns>
		public static bool GetFullscreenMapTileOfScreenPosition( int scrPosX, int scrPosY, out (int x, int y) tilePos ) {
			float mapScale = Main.mapFullscreenScale;
			float minX = 10f;
			float minY = 10f;
			float maxX = (float)( Main.maxTilesX - 10 );
			float maxY = (float)( Main.maxTilesY - 10 );

			float mapPosX = Main.mapFullscreenPos.X * mapScale;
			float mapPosY = Main.mapFullscreenPos.Y * mapScale;

			float scrOriginX = (float)(Main.screenWidth / 2) - mapPosX;
			scrOriginX += minX * mapScale;
			float scrOriginY = (float)(Main.screenHeight / 2) - mapPosY;
			scrOriginY += minX * mapScale;

			int tileX = (int)(((float)scrPosX - scrOriginX) / mapScale + minX);
			int tileY = (int)(((float)scrPosY - scrOriginY) / mapScale + minY);

			tilePos = (tileX, tileY);
			return tileX >= minX
				&& tileX < maxX
				&& tileY >= minY
				&& tileY < maxY;
		}


		////////////////

		/// <summary>
		/// Zooms in to find the top left tile position of the current map view.
		/// </summary>
		/// <returns></returns>
		public static (int TileX, int TileY, bool IsOnScreen) FindTopLeftTileOfFullscreenMap() {
			return HUDMapHelpers.FindTopLeftTileOfFullscreenMapStartingAt( Main.maxTilesX / 2, Main.maxTilesY / 2 );
		}

		private static (int TileX, int TileY, bool IsOnScreen) FindTopLeftTileOfFullscreenMapStartingAt( int tileX, int tileY ) {
			int prevLeftX = Main.maxTilesX * -32;
			int prevRightX = Main.maxTilesX * 32;
			int prevTopY = Main.maxTilesY * -32;
			int prevBotY = Main.maxTilesY * 32;

			//

			void IncreaseX( ref int x ) {
				prevLeftX = x;
				x += ( prevRightX - x ) / 2;
			}
			void DecreaseX( ref int x ) {
				prevRightX = x;
				x += ( prevLeftX - x ) / 2;
			}
			void IncreaseY( ref int y ) {
				prevTopY = y;
				y += ( prevBotY - y ) / 2;
			}
			void DecreaseY( ref int y ) {
				prevBotY = y;
				y += ( prevTopY - y ) / 2;
			}

			//

			(Vector2 ScreenPosition, bool) mapPos;

			while( true ) {
				bool foundX = false;
				mapPos = HUDMapHelpers.GetFullMapPositionAsScreenPosition( new Vector2( tileX << 4, tileY << 4 ) );
//LogHelpers.LogOnce( "x:"+tileX+", y:"+tileY+" -- lx:"+prevLeftX+", rx:"+prevRightX+", uy:"+prevTopY+", dy:"+prevBotY+" -- sx:"+mapPos.ScreenPosition.ToString());

				if( (int)mapPos.ScreenPosition.X < 0 ) {
					IncreaseX( ref tileX );
				} else if( (int)mapPos.ScreenPosition.X > 0 ) {
					DecreaseX( ref tileX );
				} else {
					foundX = true;
				}

				if( (int)mapPos.ScreenPosition.Y < 0 ) {
					IncreaseY( ref tileY );
				} else if( (int)mapPos.ScreenPosition.Y > 0 ) {
					DecreaseY( ref tileY );
				} else if( foundX ) {
					bool isOnScreen = tileX < 0 || tileY < 0;
					return (tileX, tileY, isOnScreen);
				}
			}
		}
	}
}
{'fraction_non_alphanumeric': 0.13865373551409552, 'fraction_numerical': 0.00945179584120983, 'mean_word_length': 2.9557867360208063, 'pattern_counts': {'":': 0, '<': 101, '<?xml version=': 0, '>': 95, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6591767', 'n_tokens_mistral': 4678, 'n_tokens_neox': 3993, 'n_words': 1092}