-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworldmap.cpp
228 lines (203 loc) · 6.43 KB
/
worldmap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "worldmap.h"
void worldmap::display_newmap(){
if( enabled ){
wipescreen();
for( int i=0 ; i<_y ; i++ ){
for( int j=0 ; j<_x ; j++ ){
std::cout << _world[i][j];
}
std::cout << "\n";
}
}
}
dot& worldmap::findlast( const dot& d_new ){
for( unsigned i=0; i<residents.size() ; ++i ){
if( residents[i] == d_new ) return residents[i];
}
}
void worldmap::kill( dot& d ){
d.flag = death;
printat( d.x, d.y , death); // For Graphics
old: _world[ d.y ][ d.x ] = death; // For Logic
residents.erase( std::find( residents.begin(),residents.end(), d ) );
}
void worldmap::auto_add_residents( int amount ){
std::vector<dot> auto_dots;
Random r;
r.seed();
char flag = '!';
dot tmp;
bool flag_exist = false;
for( int i=0 ; i<amount ; i++, flag++ ){
flag = (char)r.range((int)'!',(int)'~');
if( flag == _empty || flag == death ){
--i; continue;
}
flag_exist = false;
for( int i=0; i<auto_dots.size() ; ++i){
if( auto_dots[i].flag == flag ){
flag_exist = true;
}
}
if( flag_exist ){
--i; continue;
}
tmp = dot( r.range( _x_min, _x_max -1 ), r.range( _y_min, _y_max -1 ), flag );
for( unsigned index=0 ; index<auto_dots.size() ; index++ ){
while( tmp == auto_dots[index] ){
tmp = dot( r.range( _x_min, _x_max -1 ), r.range( _y_min, _y_max -1 ), flag );
}
}
std::cout<< "Adding Resident: " << tmp << std::endl;
auto_dots.push_back( tmp );
}
for( unsigned index=0 ; index<auto_dots.size() ; index++ ){
add( auto_dots[index] );
}
}
void worldmap::add( dot& d ){
bool overlay = false;
if( d.x < _x_max && d.x >= _x_min && d.y < _y_max && d.y >= _y_min ){
if( _world[ d.y ][ d.x ] != _empty && _world[ d.y ][ d.x ] != death ){
overlay = true;
if( _world[ d.y ][ d.x ] != d.flag ){
dot tmp = findlast( d );
if( tmp.strength > d.strength ){
printat( d.x, d.y, tmp.flag );
_world[ d.y ][ d.x ] = tmp.flag;
// it's not "Kill d"
// it's "don't add d"
}
else if( tmp.strength < d.strength ){
kill( tmp );
printat( d.x, d.y, d.flag );
_world[ d.y ][ d.x ] = d.flag;
d.strength-=30;
residents.push_back(d);
}
else{
kill( tmp );
printat( d.x, d.y, death);
_world[ d.y ][ d.x ] = death;
}
}
}
else{
residents.push_back( d );
printat( d.x, d.y, d.flag);
_world[ d.y ][ d.x ] = d.flag;
}
}
}
void worldmap::setstate( bool state ){
enabled = state;
talk.enabled = enabled;
}
bool worldmap::getstate(){
return enabled;
}
void worldmap::display(){
int i=0;
int j=0;
while( enabled ){
++generations;
if( generations >= max_generations ){
break;
}
if( _debug ) getchar();
for( unsigned i=0 ; i<residents.size() ; ++i){
residents[i].age++;
residents[i].is_newborn = false;
}
for( unsigned i=0 ; i<residents.size() ; ++i){
if( residents[i].age > max_age ) kill( residents[i] );
}
// Print Changed Dots
/*
Replace The Commented Codes with a Better Implementation
system("cls");
for( i=0 ; i<_y ; i++ ){
for( j=0 ; j<_x ; j++ ){
std::cout << _world[i][j];
}
std::cout << "\n";
}
*/
spread_residents();
gotoxy( 0, _y_max + 2 );
std::cout<< "\n\nGeneration: " << generations << std::endl;
Sleep( refresh_rate );
}
}
bool worldmap::blocked( dot& d, int direction ){
if( direction == 1 ){ // left
if( d.x == 0 ) return true;
if( _world[ d.y ][ d.x-1 ] == d.flag ) return true;
return false;
}
if( direction == 2 ){ // right
if( d.x >= _x_max-1 ) return true;
if( _world[ d.y ][ d.x+1 ] == d.flag ) return true;
return false;
}
if( direction == 3 ){ // up
if( d.y == 0 ) return true;
if( _world[ d.y-1 ][ d.x ] == d.flag ) return true;
return false;
}
if( direction == 4 ){ // down
if( d.y >= _y_max-1 ) return true;
if( _world[ d.y+1 ][ d.x ] == d.flag ) return true;
return false;
}
}
void worldmap::spread_residents(){
Random r;
//r.seed();
int direction;
dot tmp;
for( unsigned i=0 ; i<residents.size() ; i++){
direction = r.range(1,4);
if( residents[i].flag == death ) continue;
if( blocked( residents[i], direction ) ) continue;
if( residents[i].is_newborn ) continue;
if( direction == 1 ){ // left
tmp = dot ( residents[i].x -1 , residents[i].y, residents[i].flag );
}
else if( direction == 2 ){ // right
tmp = dot ( residents[i].x +1 , residents[i].y, residents[i].flag );
}
else if( direction == 3 ){ // up
tmp = dot ( residents[i].x , residents[i].y - 1, residents[i].flag );
}
else if( direction == 4 ){ // down
tmp = dot ( residents[i].x , residents[i].y + 1, residents[i].flag );
}
add( tmp );
}
// iterate through residents
// skip dead residents
// generate new dot ( using direction )
// ( keep flag, random strength )
// add the new dot
// detect collision
// start killing
}
worldmap::worldmap( int x, int y )
: _x(x), _y(y), enabled( true )
{
talk.enabled = enabled;
_x_min = 0;
_y_min = 0;
_x_max = x;
_y_max = y;
_world = new char*[y];
for( int i=0 ; i<y ; i++ ){
_world[i] = new char[x];
}
for( int i=0 ; i<y ; i++){
for( int j=0 ; j<x ; j++ ){
_world[i][j] = _empty;
}
}
}