U
    W^h

                  
   @  s   d Z ddlmZ ddlZddlmZmZ dddddddZddddd	d
dZddddddddZ	ddddddddZ
ddddddZddddddddddZd dddddddddddZdddddddddd	ddZdS )!z/
Utility helpers shared across server modules.
    )annotationsN)TupleOptionalfloat)vlohireturnc                 C  s   t |t|| S N)maxmin)r   r   r    r   %/root/workspace/PixelWarzone/utils.pyclamp
   s    r   )abtr	   c                 C  s   | ||  |  S r
   r   )r   r   r   r   r   r   lerp   s    r   )x1y1x2y2r	   c                 C  s   t ||  || S r
   mathhypot)r   r   r   r   r   r   r   distance   s    r   c                 C  s    ||  }|| }|| ||  S r
   r   )r   r   r   r   dxdyr   r   r   distance_sq   s    r   zTuple[float, float])xyr	   c                 C  s(   t | |}|dkrdS | | || fS )Nr   )        r!   r   )r   r    nr   r   r   	normalize   s    r#   )pxpyr   r   r   r   r	   c                 C  s   || }|| }|dkr4|dkr4t | | || S | | | || |  || ||   }tdtd|}|||  }	|||  }
t | |	 ||
 S )zCompute the minimum distance from point (px, py) to the line segment [(x1, y1), (x2, y2)].

    Used by Iaido path checks and wall/block interactions.
    r   r!         ?)r   r   r   r   )r$   r%   r   r   r   r   r   r   r   Zproj_xZproj_yr   r   r   _dist_point_to_segment$   s    (r'   zOptional[float]bool)cxcyrrcxrcyhalf_whalf_hr	   c           	      C  sF   |dkr|}t | || || }t ||| || }t| ||||k S )u   圆( cx,cy,r ) 与 轴对齐矩形(中心 rcx,rcy, 半宽/半高) 是否相交。
    当 half_h 为空时，使用正方形半边长 half_w。
    N)r   r   )	r)   r*   r+   r,   r-   r.   r/   Z	closest_xZ	closest_yr   r   r   circle_aabb_overlap4   s
    r0   )	r$   r%   oxoydirxdiryradiushalf_angle_radr	   c                 C  sp   | | }|| }	t ||	}
|
|kr(dS t||\}}|dkrJ|dkrJdS || |	|  |
p^d }|t |kS )u   判断点 (px,py) 是否位于以 (ox,oy) 为圆心，朝向(dirx,diry)，半角为 half_angle_rad，半径为 radius 的扇形内。Fr   r&   )r   r   r#   cos)r$   r%   r1   r2   r3   r4   r5   r6   vxvydistZndxZndyZ	cos_thetar   r   r   	in_sectorB   s    r;   )N)__doc__
__future__r   r   typingr   r   r   r   r   r   r#   r'   r0   r;   r   r   r   r   <module>   s    