home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / bbs / moria / shading.txt < prev    next >
Text File  |  1990-05-20  |  9KB  |  212 lines

  1. From ucbvax!tut.cis.ohio-state.edu!mailrus!uflorida!gatech!ncsuvx!ecemwl!jnh Mon Sep 25 11:47:55 PDT 1989
  2. Status: RO
  3.  
  4. Article 1484 of rec.games.programmer:
  5. Path: ucbvax!tut.cis.ohio-state.edu!mailrus!uflorida!gatech!ncsuvx!ecemwl!jnh
  6. >From: jnh@ecemwl.ncsu.edu (Joseph N. Hall)
  7. Newsgroups: rec.games.programmer
  8. Subject: Shading and line-of-sight calculation _en_masse_...
  9. Keywords: very very fast
  10. Message-ID: <4036@ncsuvx.ncsu.edu>
  11. Date: 25 Sep 89 17:10:09 GMT
  12. Sender: news@ncsuvx.ncsu.edu
  13. Reply-To: jnh@ecemwl.UUCP (Joseph N. Hall)
  14. Organization: North Carolina State University
  15. Lines: 192
  16.  
  17. Here is a rough presentation of the technique for calculating shading and
  18. visibility that I had mentioned earlier.
  19.  
  20. ...
  21.  
  22. (summary)
  23.  
  24. A Fast Algorithm for Calculating Shading and Visibility in a 
  25. Two-Dimensional Field
  26.  
  27. By Joseph Hall
  28. Applications Programmer, North Carolina State University
  29.  
  30. This document copyright 1989 by Joseph Hall.  It may be reproduced in 
  31. entirety for distribution for any purpose, so long as no fee whatsoever 
  32. is charged for its distribution and no attempt is made to restrict its 
  33. distribution.  No other use is allowed without permission from the author.
  34. Permission from the author must be obtained if a substantial portion of
  35. this document is to be included in another copyrighted work.
  36.  
  37. As the author of this document, I hereby release the ALGORITHMS described
  38. herein into the public domain.  This release does not apply to the actual
  39. text of this document.
  40.  
  41. ---
  42.  
  43. Interactive terminal-based "rogue-like" games such as Hack, Moria, Omega,
  44. and, of course, the original Rogue, feature a player character traveling 
  45. through a maze.  The maze usually comprises several levels and is usually
  46. laid out on a grid of squares or "tiles."  Each tile contains one of several
  47. distinct features, e.g., a piece of wall, floor, door, etc., and may also
  48. contain objects and/or creatures, if it is not solid.
  49.  
  50. Hack and Rogue handle lighting and visibility quite simply.  All corridors
  51. and walls are "visible" once they have been seen.  Rooms are square and are
  52. either "lit" or "dark."  A player carrying a lamp can see with a radius of
  53. 1 tile if he is in a corridor (which is always dark) or in a dark room.
  54. A player cannot see the occupants of a room until he steps into that room.
  55. These conditions eliminate the possible complexity of line-of-sight and
  56. shading computations, but detract somewhat from the "realism" of the game.
  57.  
  58. Moria, on the other hand, allows for line-of-sight considerations.  A player
  59. can see whatever is standing or resting on a tile is it is both lit and
  60. can be seen from his current location, i.e., if there are no "solid" tiles,
  61. such as walls or closed doors, intervening.  Thus a player can see some of
  62. the contents of a room as he approaches its entrance, and more as he gets
  63. closer.  Moria does not, however, allow for lights of radius greater than
  64. one tile, and only the player is allowed to carry a light.  Again, all rooms
  65. are either lit or not lit, and corridors are dark, although certain player
  66. actions can permanently light portions of corridors and permanently light
  67. or darken portions of rooms.
  68.  
  69. One can see the desirability of a more complex scheme, where the player
  70. is allowed a lamp of variable radius, other creatures can carry lamps, and
  71. rooms are lit by lamps with finite radius.  Such a scheme is not trivial to
  72. implement, at least from the standpoint of the bookkeeping required, but the
  73. greatest difficulty is the amount of calculation required, which can easily
  74. take long enough on a microcomputer to remove the interactive feel of
  75. the game.
  76.  
  77. Consider:
  78.  
  79. Whenever the player moves, and thus his viewpoint changes, the visibility
  80. of the entire area surrounding him must be recalculated.  This area will be
  81. either the visible area on the screen or the portion of it within a limited
  82. "sight radius" of the player.  A sight radius of at least 25 tiles is
  83. desirable, and this could entail calculations for pi * 25 * 25 tiles, or
  84. about 2000 tiles.
  85.  
  86. Additionally, whenever a light source moves (when carried by the player or
  87. by another creature), the lighting status of the area within the effective
  88. radius of the light source must be recalculated.  Although a radius of 1-5
  89. tiles is probably optimum for players and other creatures, there may be a
  90. number of these light sources on screen at the same time, and larger radii
  91. also have some application.
  92.  
  93. Finally, considerable recalculation is required whenever the solidity of a 
  94. visible tile changes, e.g., when a door opens or closes.
  95.  
  96. The obvious approach to all of the above situations is to calculate both
  97. visibility and lighting status on a tile-by-tile basis using an ordinary
  98. "line-of-sight" routine.  That is, for each light source on screen, calculate
  99. whether it lights a tile within its radius by seeing whether a line of sight
  100. exists between it and the tile; similarly, once the lighting status of all
  101. tiles on screen is known, calculate whether the player can see them by
  102. checking the line of sight from the player to each of the surrounding tiles.
  103.  
  104. The difficulty here is that the line-of-sight routine must check each of the
  105. tiles intervening between the player/light source and destination.  This
  106. makes the calculations described above roughly O(n^3), which is generally
  107. unsuitable.
  108.  
  109. A previous posting on USENET suggested using "rays" emanating from the player
  110. or light source, one ray to each screen border tile or each tile of limiting
  111. circumference.  The algorithm involves checking the solidity of tiles along
  112. each ray, beginning at the player or light source, and marking them visible
  113. until a solid object is encountered.  While this is fast and efficient, it
  114. is incorrect.  To wit:
  115.  
  116.       . |          . |        |
  117.     . . |        . . |          . |
  118.   . . . |      . . . *        * * *                . . .
  119. @ . x . |    @ . x * *    @ . x * *    @ . . . .    @ . .
  120.  
  121.    (1)           (2)           (3)           (4)           (5)
  122.  
  123.  
  124. Here, @ is the center of a light source, x is a solid object, '*' represents
  125. a shaded tile, '.' is a lit tile, and '|' is a boundary.  (1) shows the system
  126. without shading.  (2) is the correct shading.  (3) is the shading generated
  127. by the above algorithm.  (4) and (5) are the lines of sight to the border that
  128. cause the incorrect shading to be generated.  The correct shading will be
  129. generated only for the border tiles, and there will be some inaccuracies in
  130. the remaining shading.
  131.  
  132. The author has, however, found an efficient technique that relies on
  133. tables of pre-calculated, rasterized shading.
  134.  
  135. Consider this situation:
  136.  
  137.           .          .          .          *
  138.         . .        . .        . .        * *        
  139.           . . .          . . .          . . *          * * *
  140.         . 3 . .        . . . .        . . * *        . 3 * .
  141.       . . 2 . .      . . . . .      . . 2 * *      . . . . .
  142.     @ . . 1 . .    @ . . 1 * *    @ . . . . .    @ . . . . .
  143.  
  144.          (6)         (7)         (8)         (9)
  145.  
  146. '1,' '2,' and '3' represent solid objects.  (7), (8) and (9) are the shading
  147. generated by the individual objects.  The total shading can be generated by
  148. overlaying (7), (8) and (9):
  149.  
  150.           *
  151.         * *
  152.           * * *
  153.         . 3 * *
  154.       . . 2 * *
  155.     @ . . 1 * *
  156.  
  157.         (10)
  158.  
  159. Thus the problem of calculating shading for an area can be reduced to one of
  160. "summing" the shadows that its individual tiles create.  This procedure is
  161. straightforward and won't be detailed in this short report.
  162.  
  163. HOW TO STORE the pre-calculated shadows is a matter to consider, however.
  164. One might expect a full set of shadows, say, out to a radius of 32, to
  165. occupy an inordinate amount of space, or, if tightly compressed, to present
  166. problems in retrieval.  But this turns out to be not nearly so bad.
  167.  
  168. Symmetry considerations, first, reduce the number of shadows that must be
  169. stored by a factor of 8, since only one "octant" (45-degree slice), as
  170. shown above, need be calculated.
  171.  
  172. The shadows can be stored as a series of "rasters," using the following
  173. representation for each shadow:
  174.  
  175.     byte
  176.     1    # of rasters in this shadow
  177.     2    #1 start
  178.     3    #1 end
  179.     4    #2 start
  180.     5    #2 end
  181.     ...
  182.  
  183. (7), (8) and (9) can be translated as follows:
  184.  
  185.     (7)    1 4-5
  186.     (8)    3 4-5 4-5 5-5
  187.     (9)    4 4-4 3-5 4-5 5-5
  188.  
  189. The full set of radius-32 shadows can, in fact, be stored in a readily-
  190. accessible table of LESS THAN 9000 BYTES.
  191.  
  192. ...
  193.  
  194. I have written a prototype that uses this shading technique.  Missing
  195. certain optimizations in its current version, it still calculates a 32 x 32
  196. area in a relatively-constant 50 milliseconds on an 8MHz 68000.  The
  197. most efficient conventional LOS-based version that I have been able to write
  198. takes about 800 milliseconds. (!)
  199.  
  200. I am working on a cleaner version of the prototype and table generator and
  201. will present them and a detailed report later (a couple of weeks?) in
  202. rec.games.programmer.
  203.  
  204.  
  205. v   v sssss|| joseph hall                      || 4116 Brewster Drive
  206.  v v s   s || jnh@ecemwl.ncsu.edu (Internet)   || Raleigh, NC  27606
  207.   v   sss  || SP Software/CAD Tool Developer, Mac Hacker and Keyboardist
  208. -----------|| Disclaimer: NCSU may not share my views, but is welcome to.
  209.  
  210.  
  211.  
  212.