FACING8.ASM 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. ;
  2. ; Command & Conquer Red Alert(tm)
  3. ; Copyright 2025 Electronic Arts Inc.
  4. ;
  5. ; This program is free software: you can redistribute it and/or modify
  6. ; it under the terms of the GNU General Public License as published by
  7. ; the Free Software Foundation, either version 3 of the License, or
  8. ; (at your option) any later version.
  9. ;
  10. ; This program is distributed in the hope that it will be useful,
  11. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ; GNU General Public License for more details.
  14. ;
  15. ; You should have received a copy of the GNU General Public License
  16. ; along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. ;
  18. ;***************************************************************************
  19. ;** C O N F I D E N T I A L --- W E S T W O O D A S S O C I A T E S **
  20. ;***************************************************************************
  21. ;* *
  22. ;* Project Name : Support Library *
  23. ;* *
  24. ;* File Name : FACING8.ASM *
  25. ;* *
  26. ;* Programmer : Joe L. Bostic *
  27. ;* *
  28. ;* Start Date : May 8, 1991 *
  29. ;* *
  30. ;* Last Update : February 6, 1995 [BWG] *
  31. ;* *
  32. ;*-------------------------------------------------------------------------*
  33. ;* Functions: *
  34. ;* Desired_Facing8 -- Determines facing to reach a position. *
  35. ;* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - *
  36. IDEAL
  37. P386
  38. MODEL USE32 FLAT
  39. GLOBAL C Desired_Facing8 :NEAR
  40. ; INCLUDE "wwlib.i"
  41. DATASEG
  42. ; 8 direction desired facing lookup table. Build the index according
  43. ; to the following bits:
  44. ;
  45. ; bit 3 = Is y2 < y1?
  46. ; bit 2 = Is x2 < x1?
  47. ; bit 1 = Is the ABS(x2-x1) < ABS(y2-y1)?
  48. ; bit 0 = Is the facing closer to a major axis?
  49. NewFacing8 DB 1,2,1,0,7,6,7,0,3,2,3,4,5,6,5,4
  50. CODESEG
  51. ;***************************************************************************
  52. ;* DESIRED_FACING8 -- Determines facing to reach a position. *
  53. ;* *
  54. ;* This routine will return with the most desirable facing to reach *
  55. ;* one position from another. It is accurate to a resolution of 0 to *
  56. ;* 7. *
  57. ;* *
  58. ;* INPUT: x1,y1 -- Position of origin point. *
  59. ;* *
  60. ;* x2,y2 -- Position of target. *
  61. ;* *
  62. ;* OUTPUT: Returns desired facing as a number from 0..255 with an *
  63. ;* accuracy of 32 degree increments. *
  64. ;* *
  65. ;* WARNINGS: If the two coordinates are the same, then -1 will be *
  66. ;* returned. It is up to you to handle this case. *
  67. ;* *
  68. ;* HISTORY: *
  69. ;* 07/15/1991 JLB : Documented. *
  70. ;* 08/08/1991 JLB : Same position check. *
  71. ;* 08/14/1991 JLB : New algorithm *
  72. ;* 02/06/1995 BWG : Convert to 32-bit *
  73. ;*=========================================================================*
  74. ; long Desired_Facing8(long x1, long y1, long x2, long y2);
  75. PROC Desired_Facing8 C near
  76. USES ebx, ecx, edx
  77. ARG x1:DWORD
  78. ARG y1:DWORD
  79. ARG x2:DWORD
  80. ARG y2:DWORD
  81. xor ebx,ebx ; Index byte (built).
  82. ; Determine Y axis difference.
  83. mov edx,[y1]
  84. mov ecx,[y2]
  85. sub edx,ecx ; DX = Y axis (signed).
  86. jns short ??absy
  87. inc ebx ; Set the signed bit.
  88. neg edx ; ABS(y)
  89. ??absy:
  90. ; Determine X axis difference.
  91. shl ebx,1
  92. mov eax,[x1]
  93. mov ecx,[x2]
  94. sub ecx,eax ; CX = X axis (signed).
  95. jns short ??absx
  96. inc ebx ; Set the signed bit.
  97. neg ecx ; ABS(x)
  98. ??absx:
  99. ; Determine the greater axis.
  100. cmp ecx,edx
  101. jb short ??dxisbig
  102. xchg ecx,edx
  103. ??dxisbig:
  104. rcl ebx,1 ; Y > X flag bit.
  105. ; Determine the closeness or farness of lesser axis.
  106. mov eax,edx
  107. inc eax ; Round up.
  108. shr eax,1
  109. cmp ecx,eax
  110. rcl ebx,1 ; Close to major axis bit.
  111. xor eax,eax
  112. mov al,[NewFacing8+ebx]
  113. ; Normalize to 0..FF range.
  114. shl eax,5
  115. ret
  116. ENDP Desired_Facing8
  117. END