Sectors_Loader.cpp
Go to the documentation of this file.
1 #include "Sectors_Loader.hpp"
2 #include "../Defs.hpp"
3 
4 using namespace iv;
5 
6 namespace comp
7 {
8 
9 Sectors_Loader::Sectors_Loader( iv::Instance * inst, Sectors * sectors, iv::Attr< iv::float2 > * position, float load_range, int sector_size ) :
10  cm( inst, this, "Sectors_Loader" ),
11  sectors( sectors ),
12  position( position ),
13  load_range( load_range ),
14  sector_size( sector_size ),
15  current_begin( 0, 0 ),
16  current_end( 0, 0 )
17 {
18 }
19 
21 {
22  return this->cm.instance();
23 }
24 
25 void Sectors_Loader::Load( iv::int2 block )
26 {
27  this->cm.log( SRC_INFO, ::comp::Defs::Log::Sectors, "Load block ", block, "." );
28  this->sectors->Load( &this->cm, block );
29 }
30 
31 void Sectors_Loader::Unload( iv::int2 block )
32 {
33  this->cm.log( SRC_INFO, ::comp::Defs::Log::Sectors, "Unload block ", block, "." );
34  this->sectors->Unload( &this->cm, block );
35 }
36 
38 {
39  //--------------------- compute block ranges --------------------------------
40  auto center_position = this->position->Get();
41 
42  int2 current_begin = this->current_begin;
43  int2 current_end = this->current_end;
44  int2 target_begin = sig_div( int2( center_position - float2( this->load_range, this->load_range ) ), this->sector_size );
45  int2 target_end = sig_div( int2( center_position + float2( this->load_range, this->load_range ) ), this->sector_size );
46 
47  int max_begin_x = std::max( current_begin.x, target_begin.x );
48  int min_end_x = std::min( current_end.x, target_end.x );
49 
50  //--------------------- unload blocks -------------------
51  // left unload
52  for( int x = current_begin.x; x < std::min( current_end.x, target_begin.x ); x++ )
53  for( int y = current_begin.y; y < current_end.y; y++ )
54  this->Unload( int2( x, y ) );
55 
56  // right unload
57  for( int x = std::max( target_end.x, current_begin.x ); x < current_end.x; x++ )
58  for( int y = current_begin.y; y < current_end.y; y++ )
59  this->Unload( int2( x, y ) );
60 
61  // top unload
62  for( int y = current_begin.y; y < std::min( current_end.y, target_begin.y ); y++ )
63  for( int x = max_begin_x; x < min_end_x; x++ )
64  this->Unload( int2( x, y ) );
65 
66  // bottom unload
67  for( int y = std::max( target_end.y, current_begin.y ); y < current_end.y; y++ )
68  for( int x = max_begin_x; x < min_end_x; x++ )
69  this->Unload( int2( x, y ) );
70 
71  //------------------ load blocks -----------------------
72  // left load
73  for( int x = target_begin.x; x < std::min( target_end.x, current_begin.x ); x++ )
74  for( int y = target_begin.y; y < target_end.y; y++ )
75  this->Load( int2( x, y ) );
76 
77  // right load
78  for( int x = std::max( current_end.x, target_begin.x ); x < target_end.x; x++ )
79  for( int y = target_begin.y; y < target_end.y; y++ )
80  this->Load( int2( x, y ) );
81 
82  // top load
83  for( int y = target_begin.y; y < std::min( target_end.y, current_begin.y ); y++ )
84  for( int x = max_begin_x; x < min_end_x; x++ )
85  this->Load( int2( x, y ) );
86 
87  // bottom load
88  for( int y = std::max( current_end.y, target_begin.y ); y < target_end.y; y++ )
89  for( int x = max_begin_x; x < min_end_x; x++ )
90  this->Load( int2( x, y ) );
91 
92  //------------------ store new state ----------------
93  this->current_begin = target_begin;
94  this->current_end = target_end;
95 }
96 
97 }