Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.4 KiB

  1. //========= Copyright � 1996-2009, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Utility functions for cell coordinate calculations
  4. // to reduce bandwidth usage
  5. //
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #ifndef CELLCOORD_H
  9. #define CELLCOORD_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "worldsize.h"
  14. // Given a world coord, return the cell it should be in
  15. inline int CellFromCoord( int cellwidth, float f )
  16. {
  17. // We handle each side of zero difference to reduce precision errors
  18. if ( f < 0.0f )
  19. {
  20. return Float2Int( f + MAX_COORD_INTEGER ) / cellwidth;
  21. }
  22. else
  23. {
  24. return Float2Int(f) / cellwidth + ( MAX_COORD_INTEGER / cellwidth );
  25. }
  26. }
  27. // Given a cell and a world coord, return the offset into the cell
  28. // cell should have been returned from CellFromCoord with the same f, we don't
  29. // recompute here
  30. inline float CellInCoord( int cellwidth, int cell, float f )
  31. {
  32. float r;
  33. int c = abs( cell * cellwidth - MAX_COORD_INTEGER ) ;
  34. if ( f < 0.0f )
  35. {
  36. r = c + f;
  37. }
  38. else
  39. {
  40. r = f - c;
  41. }
  42. // Pecision errors can futz the edges
  43. return clamp( r, 0.0f, (float)cellwidth );
  44. }
  45. // Given a cell and an offset in that cell, reconstructor the world coord
  46. inline float CoordFromCell( int cellwidth, int cell, float f )
  47. {
  48. int cellPos = ( cell * cellwidth );
  49. float r = ( cellPos - MAX_COORD_INTEGER ) + f;
  50. return r;
  51. }
  52. #endif //CELLCOORDCONVERTER_H