FACING16.ASM 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. ; $Header: g:/library/source/rcs/./facing16.asm 1.10 1994/05/20 15:32:36 joe_bostic Exp $
  19. ;***************************************************************************
  20. ;** 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 **
  21. ;***************************************************************************
  22. ;* *
  23. ;* Project Name : Support Library *
  24. ;* *
  25. ;* File Name : FACING16.ASM *
  26. ;* *
  27. ;* Programmer : Joe L. Bostic *
  28. ;* *
  29. ;* Start Date : May 8, 1991 *
  30. ;* *
  31. ;* Last Update : February 6, 1995 [BWG] *
  32. ;* *
  33. ;*-------------------------------------------------------------------------*
  34. ;* Functions: *
  35. ;* Desired_Facing16 -- Converts coordinates into a facing number. *
  36. ;* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - *
  37. IDEAL
  38. P386
  39. MODEL USE32 FLAT
  40. GLOBAL Desired_Facing16 :NEAR
  41. ; INCLUDE "wwlib.i"
  42. DATASEG
  43. ; 16 direction desired facing lookup table. Build the index according
  44. ; to the following bits:
  45. ;
  46. ; bit 4 = Is y2 < y1?
  47. ; bit 3 = Is x2 < x1?
  48. ; bit 2 = Is the ABS(x2-x1) < ABS(y2-y1)?
  49. ; bit 1 = Is the lesser absolute difference very close to zero?
  50. ; bit 0 = Is the lesser absolute difference very close to the greater dist?
  51. NewFacing16 DB 3, 2, 4,-1, 1, 2,0,-1
  52. DB 13,14,12,-1,15,14,0,-1
  53. DB 5, 6, 4,-1, 7, 6,8,-1
  54. DB 11,10,12,-1, 9,10,8,-1
  55. CODESEG
  56. ;***************************************************************************
  57. ;* DESIRED_FACING16 -- Converts coordinates into a facing number. *
  58. ;* *
  59. ;* This converts coordinates into a desired facing number that ranges *
  60. ;* from 0 to 15 (0 equals North and going clockwise). *
  61. ;* *
  62. ;* INPUT: x1,y1 -- Position of origin point. *
  63. ;* *
  64. ;* x2,y2 -- Position of target. *
  65. ;* *
  66. ;* OUTPUT: Returns desired facing as a number from 0 to 255 but *
  67. ;* accurate to 22.5 degree increments. *
  68. ;* *
  69. ;* WARNINGS: If the two coordinates are the same, then -1 will be *
  70. ;* returned. It is up to you to handle this case. *
  71. ;* *
  72. ;* HISTORY: *
  73. ;* 08/14/1991 JLB : Created. *
  74. ;*=========================================================================*
  75. ; long Desired_Facing16(long x1, long y1, long x2, long y2);
  76. PROC Desired_Facing16 C near
  77. USES ebx, ecx, edx
  78. ARG x1:DWORD
  79. ARG y1:DWORD
  80. ARG x2:DWORD
  81. ARG y2:DWORD
  82. xor ebx,ebx ; Index byte (built).
  83. ; Determine Y axis difference.
  84. mov edx,[y1]
  85. mov ecx,[y2]
  86. sub edx,ecx ; DX = Y axis (signed).
  87. jns short ??absy
  88. inc ebx ; Set the signed bit.
  89. neg edx ; ABS(y)
  90. ??absy:
  91. ; Determine X axis difference.
  92. shl ebx,1
  93. mov eax,[x1]
  94. mov ecx,[x2]
  95. sub ecx,eax ; CX = X axis (signed).
  96. jns short ??absx
  97. inc ebx ; Set the signed bit.
  98. neg ecx ; ABS(x)
  99. ??absx:
  100. ; Determine the greater axis.
  101. cmp ecx,edx
  102. jb short ??dxisbig
  103. xchg ecx,edx
  104. ??dxisbig:
  105. rcl ebx,1 ; Y > X flag bit.
  106. ; Determine the closeness or farness of lesser axis.
  107. mov eax,edx
  108. inc eax ; Round up.
  109. shr eax,1
  110. inc eax ; Round up.
  111. shr eax,1 ; 1/4 of greater axis.
  112. cmp ecx,eax
  113. rcl ebx,1 ; Very close to major axis bit.
  114. sub edx,eax
  115. cmp edx,ecx
  116. rcl ebx,1 ; Very far from major axis bit.
  117. xor eax,eax
  118. mov al,[NewFacing16+ebx]
  119. ; Normalize to 0..FF range.
  120. shl eax,4
  121. ret
  122. ENDP Desired_Facing16
  123. END
  124. 
  125.