1 | // Copyright (C) 2012-2015 ChaosForge Ltd
|
---|
2 | // http://chaosforge.org/
|
---|
3 | //
|
---|
4 | // This file is part of Nova libraries.
|
---|
5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
6 |
|
---|
7 | #include "nv/rogue/fov_recursive_shadowcasting.hh"
|
---|
8 |
|
---|
9 | #include "nv/stl/math.hh"
|
---|
10 |
|
---|
11 | static int nv_rogue_rs_mult[4][8] = {
|
---|
12 | { 1, 0, 0,-1,-1, 0, 0, 1},
|
---|
13 | { 0, 1,-1, 0, 0,-1, 1, 0},
|
---|
14 | { 0, 1, 1, 0, 0,-1,-1, 0},
|
---|
15 | { 1, 0, 0, 1,-1, 0, 0,-1},
|
---|
16 | };
|
---|
17 |
|
---|
18 | void nv::rogue::fov_recursive_shadowcasting::run( const position& p, uint16 radius )
|
---|
19 | {
|
---|
20 | m_position = p;
|
---|
21 | m_radius = radius;
|
---|
22 |
|
---|
23 | if ( m_radius == 0 )
|
---|
24 | {
|
---|
25 | position max_radius = m_size-m_position;
|
---|
26 | max_radius.x=glm::max(max_radius.x,m_position.x);
|
---|
27 | max_radius.y=glm::max(max_radius.y,m_position.y);
|
---|
28 | m_radius = (int)glm::length((glm::vec2)max_radius)+1;
|
---|
29 | }
|
---|
30 | m_radius2 = m_radius * m_radius;
|
---|
31 | for ( int oct=0; oct < 8; oct++ ) cast_light( 1, 1.0, 0.0, oct );
|
---|
32 | m_map->set_visible( p, true );
|
---|
33 | }
|
---|
34 |
|
---|
35 | void nv::rogue::fov_recursive_shadowcasting::cast_light( int row, float start, float end, int oct )
|
---|
36 | {
|
---|
37 | if ( start < end ) return;
|
---|
38 |
|
---|
39 | int xx = nv_rogue_rs_mult[0][oct];
|
---|
40 | int xy = nv_rogue_rs_mult[1][oct];
|
---|
41 | int yx = nv_rogue_rs_mult[2][oct];
|
---|
42 | int yy = nv_rogue_rs_mult[3][oct];
|
---|
43 |
|
---|
44 | float new_start = 0.0f;
|
---|
45 | for ( int j = row; j < m_radius + 1; j++ )
|
---|
46 | {
|
---|
47 | int dx=-j-1;
|
---|
48 | int dy=-j;
|
---|
49 | bool blocked = false;
|
---|
50 | while ( dx++ <= 0 )
|
---|
51 | {
|
---|
52 | ivec2 c( dx*xx+dy*xy, dx*yx+dy*yy );
|
---|
53 | c += m_position;
|
---|
54 | if ( c.x >= 0 && c.x < m_size.x && c.y >= 0 && c.y < m_size.y )
|
---|
55 | {
|
---|
56 | float l_slope = ( dx - 0.5f ) / ( dy + 0.5f );
|
---|
57 | float r_slope = ( dx + 0.5f ) / ( dy - 0.5f );
|
---|
58 | if ( start < r_slope ) continue;
|
---|
59 | if ( end > l_slope ) break;
|
---|
60 | bool transparent = m_map->is_transparent( c );
|
---|
61 |
|
---|
62 | if ( dx*dx+dy*dy <= m_radius2 && (m_light_walls || transparent))
|
---|
63 | {
|
---|
64 | m_map->set_visible( c, true );
|
---|
65 | }
|
---|
66 | if ( blocked )
|
---|
67 | {
|
---|
68 | if ( !transparent )
|
---|
69 | {
|
---|
70 | new_start = r_slope;
|
---|
71 | continue;
|
---|
72 | }
|
---|
73 | blocked = false;
|
---|
74 | start = new_start;
|
---|
75 | }
|
---|
76 | else
|
---|
77 | {
|
---|
78 | if ( !transparent && j < m_radius )
|
---|
79 | {
|
---|
80 | blocked = true;
|
---|
81 | cast_light( j+1, start, l_slope, oct );
|
---|
82 | new_start = r_slope;
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|
87 | if ( blocked ) break;
|
---|
88 | }
|
---|
89 |
|
---|
90 | }
|
---|
91 |
|
---|