FACINGFF.ASM 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 : FACINGFF.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_Facing256 -- Determines facing to reach a position. *
  35. ;* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - *
  36. IDEAL
  37. P386
  38. MODEL USE32 FLAT
  39. GLOBAL C Desired_Facing256 :NEAR
  40. ; INCLUDE "wwlib.i"
  41. INCLUDE "..\include\gbuffer.inc"
  42. CODESEG
  43. ;***************************************************************************
  44. ;* Desired_Facing256 -- Desired facing algorithm 0..255 resolution. *
  45. ;* *
  46. ;* This is a desired facing algorithm that has a resolution of 0 *
  47. ;* through 255. *
  48. ;* *
  49. ;* INPUT: srcx,srcy -- Source coordinate. *
  50. ;* *
  51. ;* dstx,dsty -- Destination coordinate. *
  52. ;* *
  53. ;* OUTPUT: Returns with the desired facing to face the destination *
  54. ;* coordinate from the position of the source coordinate. North *
  55. ;* is 0, East is 64, etc. *
  56. ;* *
  57. ;* WARNINGS: This routine is slower than the other forms of desired *
  58. ;* facing calculation. Use this routine when accuracy is *
  59. ;* required. *
  60. ;* *
  61. ;* HISTORY: *
  62. ;* 12/24/1991 JLB : Adapted. *
  63. ;*=========================================================================*/
  64. ; LONG cdecl Desired_Facing256(LONG srcx, LONG srcy, LONG dstx, LONG dsty)
  65. PROC Desired_Facing256 C near
  66. USES ebx, ecx, edx
  67. ARG srcx:DWORD
  68. ARG srcy:DWORD
  69. ARG dstx:DWORD
  70. ARG dsty:DWORD
  71. xor ebx,ebx ; Facing number.
  72. ; Determine absolute X delta and left/right direction.
  73. mov ecx,[dstx]
  74. sub ecx,[srcx]
  75. jge short ??xnotneg
  76. neg ecx
  77. mov ebx,11000000b ; Set bit 7 and 6 for leftward.
  78. ??xnotneg:
  79. ; Determine absolute Y delta and top/bottom direction.
  80. mov eax,[srcy]
  81. sub eax,[dsty]
  82. jge short ??ynotneg
  83. xor ebx,01000000b ; Complement bit 6 for downward.
  84. neg eax
  85. ??ynotneg:
  86. ; Set DX=64 for quadrants 0 and 2.
  87. mov edx,ebx
  88. and edx,01000000b
  89. xor edx,01000000b
  90. ; Determine if the direction is closer to the Y axis and make sure that
  91. ; CX holds the larger of the two deltas. This is in preparation for the
  92. ; divide.
  93. cmp eax,ecx
  94. jb short ??gotaxis
  95. xchg eax,ecx
  96. xor edx,01000000b ; Closer to Y axis so make DX=64 for quad 0 and 2.
  97. ??gotaxis:
  98. ; If closer to the X axis then add 64 for quadrants 0 and 2. If
  99. ; closer to the Y axis then add 64 for quadrants 1 and 3. Determined
  100. ; add value is in DX and save on stack.
  101. push edx
  102. ; Make sure that the division won't overflow. Reduce precision until
  103. ; the larger number is less than 256 if it appears that an overflow
  104. ; will occur. If the high byte of the divisor is not zero, then this
  105. ; guarantees no overflow, so just abort shift operation.
  106. test eax,0FFFFFF00h
  107. jnz short ??nooverflow
  108. ??again:
  109. test ecx,0FFFFFF00h
  110. jz short ??nooverflow
  111. shr ecx,1
  112. shr eax,1
  113. jmp short ??again
  114. ??nooverflow:
  115. ; Make sure that the division won't underflow (divide by zero). If
  116. ; this would occur, then set the quotient to $FF and skip divide.
  117. or ecx,ecx
  118. jnz short ??nounderflow
  119. mov eax,0FFFFFFFFh
  120. jmp short ??divcomplete
  121. ; Derive a pseudo angle number for the octant. The angle is based
  122. ; on $00 = angle matches long axis, $00 = angle matches $FF degrees.
  123. ??nounderflow:
  124. xor edx,edx
  125. shld edx,eax,8 ; shift high byte of eax into dl
  126. shl eax,8
  127. div ecx
  128. ??divcomplete:
  129. ; Integrate the 5 most significant bits into the angle index. If DX
  130. ; is not zero, then it is 64. This means that the dividend must be negated
  131. ; before it is added into the final angle value.
  132. shr eax,3
  133. pop edx
  134. or edx,edx
  135. je short ??noneg
  136. dec edx
  137. neg eax
  138. ??noneg:
  139. add eax,edx
  140. add eax,ebx
  141. and eax,0FFH
  142. ret
  143. ENDP Desired_Facing256
  144. END