finalize revision 0.032
[SwashRL.git] / src / fov.d
bloba8540060035cce739e5f38654df4230a4682eaf8
1 /*
2 * libtcod 1.6.3
3 * Copyright (c) 2008,2009,2010,2012,2013,2016,2017 Jice & Mingos & rmtew
4 * All rights reserved.
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
29 * follows.
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
59 // calculations.
61 import global;
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
69 // below.
70 static int[][] mult = [
71 [1,0,0,-1,-1,0,0,1],
72 [0,1,-1,0,0,-1,1,0],
73 [0,1,1,0,0,-1,-1,0],
74 [1,0,0,1,-1,0,0,-1]
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)
84 float new_start=0.0f;
85 if ( start < end )
86 { return;
88 foreach (j; row .. radius+1)
90 int dx=-j-1;
91 int dy=-j;
92 bool blocked=false;
93 while ( dx <= 0 )
95 int X,Y;
96 dx++;
97 X=cx+dx*xx+dy*xy;
98 Y=cy+dx*yx+dy*yy;
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 )
105 { continue;
107 else if( end > l_slope )
108 { break;
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;
117 if ( blocked )
119 if (to_display.t[Y][X].block_vision)
121 new_start=r_slope;
122 continue;
124 else
126 blocked=false;
127 start=new_start;
130 else
132 if ((to_display.t[Y][X].block_vision) && j < radius )
134 blocked=true;
135 cast_light(to_display,cx,cy,j+1,start,l_slope,radius,r2,xx,xy,yx,
136 yy,id+1,light_walls);
137 new_start=r_slope;
142 if ( blocked )
143 { break;
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,
153 bool light_walls)
155 int r2;
156 /* clean the map */
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
183 // be required.
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 );