3 * Copyright (c) 2008,2009,2010,2012,2013,2016,2017 Jice & Mingos & rmtew
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * The name of Jice or Mingos may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY JICE, MINGOS AND RMTEW ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL JICE, MINGOS OR RMTEW BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 /* This source code has been modified from its original Doryen library
29 * implementation to work in Spelunk! - Philip Pavlick
32 /* This code was taken from Spelunk! by Philip Pavlick, available at
33 * https://github.com/Crawldragon/Spelunk
38 // "I would have men of such constancy put to sea, that their business might be everything and their intent everywhere; for that's it that always makes a good voyage of nothing."
39 // --William Shakespeare, twelfth night
44 private immutable int[][] mult
= [
51 void cast_light(ref Map map
, uint cx
, uint cy
, uint row
, float start
,
52 float end
, int radius
, uint r2
, uint xx
, uint xy
, uint yx
,
53 uint yy
, bool light_walls
) {
55 if (start
< end
) return;
57 foreach (j
; row
.. radius
+1) {
68 float l_slope
, r_slope
;
70 l_slope
=(dx
-0.5f)/(dy
+0.5f);
71 r_slope
=(dx
+0.5f)/(dy
-0.5f);
73 if (start
< r_slope
) {
75 } else if (end
> l_slope
) {
79 if (dx
*dx
+dy
*dy
<= r2
&& (light_walls ||
!(map
[Y
, X
].blocks_light
))) {
80 map
[Y
, X
].visible
= true;
83 if (map
[Y
, X
].blocks_light
) {
91 if ((map
[Y
, X
].blocks_light
) && j
< radius
) {
93 cast_light(map
,cx
,cy
,j
+1,start
,l_slope
,radius
,r2
,xx
,xy
,yx
,
103 void do_fov(ref Map map
, uint x
, uint y
, uint radius
, bool light_walls
= true) {
106 map
.for_all((ref Tile x
) => x
.visible
= false);
109 //int radius_x=map_x;
110 //int radius_y=map_y;
111 radius
= cast(uint)(sqrt(cast(float)(map_x
*map_x
) + (map_y
* map_y
))) + 1;
116 // recursive shadow casting
117 foreach (oct
; 0 .. 8) {
118 cast_light(map
,x
,y
,1,1.0,0.0,radius
,r2
,
119 mult
[0][oct
],mult
[1][oct
],mult
[2][oct
],mult
[3][oct
],light_walls
);
121 map
[y
, x
].visible
= true;