Name the tiles to make everything better because I'm a bloody idiot
[SmugglerRL.git] / src / tcod_fov.d
blobcfd386749b801dd1673eda52aeb8993f6989e7aa
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.
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
36 import stdlib;
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
40 import constants;
41 import map;
42 import tile;
44 private immutable int[][] mult = [
45 [1,0,0,-1,-1,0,0,1],
46 [0,1,-1,0,0,-1,1,0],
47 [0,1,1,0,0,-1,-1,0],
48 [1,0,0,1,-1,0,0,-1],
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) {
54 float new_start=0.0f;
55 if (start < end) return;
57 foreach (j; row .. radius+1) {
58 int dx=-j-1;
59 int dy=-j;
60 bool blocked=false;
61 while (dx <= 0) {
62 int X, Y;
63 dx++;
65 X=cx+dx*xx+dy*xy;
66 Y=cy+dx*yx+dy*yy;
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) {
74 continue;
75 } else if (end > l_slope) {
76 break;
79 if (dx*dx+dy*dy <= r2 && (light_walls || !(map[Y, X].blocks_light))) {
80 map[Y, X].visible = true;
82 if (blocked) {
83 if (map[Y, X].blocks_light) {
84 new_start=r_slope;
85 continue;
86 } else {
87 blocked=false;
88 start=new_start;
90 } else {
91 if ((map[Y, X].blocks_light) && j < radius ) {
92 blocked=true;
93 cast_light(map,cx,cy,j+1,start,l_slope,radius,r2,xx,xy,yx,
94 yy,light_walls);
95 new_start=r_slope;
99 if (blocked) break;
103 void do_fov(ref Map map, uint x, uint y, uint radius, bool light_walls = true) {
104 uint r2;
105 // clean the map
106 map.for_all((ref Tile x) => x.visible = false);
108 if (radius == 0) {
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;
114 r2=radius*radius;
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;