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.
27 * This source code has been modified from its original Doryen library
28 * implementation by Philip Pavlick to work in SwashRL. The SwashRL license
31 * Copyright (c) 2015-2020 Philip Pavlick. See '3rdparty.txt' for other
32 * licenses. All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions are met:
36 * * Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * * Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * * Neither the name of the SwashRL project nor the names of its
42 * contributors may be used to endorse or promote products derived from
43 * this software without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
46 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
49 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55 * POSSIBILITY OF SUCH DAMAGE.
58 // fov.d: Defines functions and variables related to field-of-vision
63 import std
.math
: sqrt
;
65 // Arrays of integers used by the recursive shadowcasting algorithm. I'll be
66 // totally honest here, I (Philip Pavlick) have no idea what these do, but
67 // they work. For more information, see the link to the original recursive
68 // shadowcasting article provided in the comment lines for the functions
70 static int[][] mult
= [
77 // Recursively calculates field-of-vision in a sector of the given map.
78 // For more information, see:
79 // http://www.roguebasin.com/index.php?title=FOV_using_recursive_shadowcasting
80 void cast_light( Map
* to_display
, int cx
, int cy
, int row
, float start
,
81 float end
, int radius
, int r2
, int xx
, int xy
, int yx
,
82 int yy
, int id
, bool light_walls
)
88 foreach (j
; row
.. radius
+1)
99 if (X
< MAP_X
&& Y
< MAP_Y
)
101 float l_slope
,r_slope
;
102 l_slope
=(dx
-0.5f)/(dy
+0.5f);
103 r_slope
=(dx
+0.5f)/(dy
-0.5f);
104 if( start
< r_slope
)
107 else if( end
> l_slope
)
110 if ( dx
*dx
+dy
*dy
<= r2
111 && (light_walls ||
!(to_display
.t
[Y
][X
].block_vision
))
114 to_display
.v
[Y
][X
] = true;
115 to_display
.t
[Y
][X
].seen
= true;
119 if (to_display
.t
[Y
][X
].block_vision
)
132 if ((to_display
.t
[Y
][X
].block_vision
) && j
< radius
)
135 cast_light(to_display
,cx
,cy
,j
+1,start
,l_slope
,radius
,r2
,xx
,xy
,yx
,
136 yy
,id
+1,light_walls
);
148 // Calculates field-of-vision using the Recursive Shadowcasting algorithm.
149 // For more information, see:
150 // http://www.roguebasin.com/index.php?title=FOV_using_recursive_shadowcasting
151 void TCOD_map_compute_fov_recursive_shadowcasting(
152 Map
* to_display
, int player_x
, int player_y
, int max_radius
,
157 foreach (c
; 0 .. MAP_Y
)
158 { foreach (d
; 0 .. MAP_X
)
159 { to_display
.v
[c
][d
] = false;
162 if ( max_radius
== 0 )
164 int max_radius_x
=MAP_X
-player_x
;
165 int max_radius_y
=MAP_Y
-player_y
;
166 max_radius_x
=max_radius_x
> player_x ? max_radius_x
: player_x
;
167 max_radius_y
=max_radius_y
> player_y ? max_radius_y
: player_y
;
168 max_radius
= cast(int)(sqrt(cast(float)(max_radius_x
*max_radius_x
)
169 +(max_radius_y
*max_radius_y
))) +1;
171 r2
=max_radius
*max_radius
;
172 /* recursive shadow casting */
173 foreach (oct
; 0 .. 8)
175 cast_light(to_display
,player_x
,player_y
,1,1.0,0.0,max_radius
,r2
,
176 mult
[0][oct
],mult
[1][oct
],mult
[2][oct
],mult
[3][oct
],0,light_walls
);
178 to_display
.v
[player_y
][player_x
] = true;
181 // This function was added specifically for SwashRL to quickly do the call to
182 // `TCOD_map_compute_fov_recursive_shadowcasting` that would most frequently
184 void calc_visible( Map
* to_display
, uint viewer_x
, uint viewer_y
)
186 TCOD_map_compute_fov_recursive_shadowcasting(
187 to_display
, viewer_x
, viewer_y
, 0, true );