source: trunk/src/rogue/fov_recursive_shadowcasting.cc @ 501

Last change on this file since 501 was 496, checked in by epyon, 9 years ago
  • type disambiguition in handle.hh
  • tweak to shadowcasting
File size: 2.2 KB
Line 
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
11static 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
18void 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 = math::max(max_radius,m_position);
27                m_radius = static_cast<int>( math::length( vec2( max_radius ) ) )+1;
28        }
29        m_radius2 = m_radius * m_radius;
30        for ( int oct=0; oct < 8; oct++ ) cast_light( 1, 1.0, 0.0, oct );
31        m_map->set_visible( p, true );
32}
33
34void nv::rogue::fov_recursive_shadowcasting::cast_light( int row, float start, float end, int oct )
35{
36        if ( start < end ) return;
37
38        int xx = nv_rogue_rs_mult[0][oct];
39        int xy = nv_rogue_rs_mult[1][oct];
40        int yx = nv_rogue_rs_mult[2][oct];
41        int yy = nv_rogue_rs_mult[3][oct];
42
43        float new_start = 0.0f;
44        for ( int j = row; j < m_radius + 1; j++ )
45        {
46                int dx=-j-1;
47                int dy=-j;
48                bool blocked = false;
49                while ( dx++ <= 0 )
50                {
51                        ivec2 c( dx*xx+dy*xy, dx*yx+dy*yy );
52                        c += m_position;
53                        if ( c.x >= 0 && c.x < m_size.x && c.y >= 0 && c.y < m_size.y )
54                        {
55                                float l_slope = ( dx - 0.5f ) / ( dy + 0.5f );
56                                float r_slope = ( dx + 0.5f ) / ( dy - 0.5f );
57                                if ( start < r_slope ) continue;
58                                if ( end   > l_slope ) break;
59                                bool transparent = m_map->is_transparent( c );
60
61                                if ( dx*dx+dy*dy <= (m_radius2+1) && (m_light_walls || transparent))
62                                {
63                                        m_map->set_visible( c, true );
64                                }
65                                if ( blocked )
66                                {
67                                        if ( !transparent )
68                                        {
69                                                new_start = r_slope;
70                                                continue;
71                                        }
72                                        blocked = false;
73                                        start = new_start;
74                                }
75                                else
76                                {
77                                        if ( !transparent && j < m_radius )
78                                        {
79                                                blocked = true;
80                                                cast_light( j+1, start, l_slope, oct );
81                                                new_start = r_slope;
82                                        }
83                                }
84                        }
85                }
86                if ( blocked ) break;
87        }
88
89}
90
Note: See TracBrowser for help on using the repository browser.