summaryrefslogtreecommitdiff
path: root/src/frame.cpp
blob: 48b568776761e790f84d77912ba3cd5aca911755 (plain) (blame)
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
/*
 * =====================================================================================
 *
 *       Filename:  frame.cpp
 *
 *    Description:  Implementation of frames 
 *
 *        Version:  1.0
 *        Created:  01/07/2023 10:11:54 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Cara Salter (muirrum), cara@devcara.com
 *   Organization:  
 *
 * =====================================================================================
 */

#include <stdlib.h>
#include "frame.hpp"
#include "noise.hpp"
#include <ncurses.h>

Frame::Frame(int nr_rows, int nr_cols, int row_0, int col_0) {
        _has_super = FALSE;
        _super = NULL;
        _w = newwin(nr_rows, nr_cols, row_0, col_0);
        _height = nr_rows;
        _width = nr_cols;
        _row = row_0;
        _col = col_0;
}

Frame::Frame(Frame &sw, int nr_rows, int nr_cols, int row_0, int col_0) {
    _has_super = TRUE;
    _super = sw.win();

    _w = derwin(sw.win(), nr_rows, nr_cols, row_0, col_0);
    _height = nr_rows;
    _width = nr_cols;
    _row = row_0;
    _col = col_0;
}

Frame::~Frame() {
    delwin(_w);
}

// Place a mob in the window
void Frame::place_mob(Mob &x) {
    mvwaddch(_w, x.row(), x.col(), x.symbol());
}

void Frame::place_mob(Mob &x, int row_0, int col_0) {
    if ((row_0 >= 0 && row_0 < _height) && (col_0 >= 0 && col_0 < _width)) {
        // Get element at target position
        char target = mvwinch(_w, row_0, col_0);
        if (target == '~' || target == '#' || target == 'S') {
            return;
        }
        erase(x);
        mvwaddch(_w, row_0, col_0, x.symbol());
        x.move(row_0, col_0);
    }
}

void Frame::erase(Mob &x) {
    mvwaddch(_w, x.row(), x.col(), ' ');
}

void Frame::center(Mob &x) {
	if(_has_super) {
		int rr = _row, cc = _col, hh, ww;
		int _r = x.row() - _height/2;
		int _c = x.col() - _width/2;

		getmaxyx(_super, hh, ww);

		if(_c + _width >= ww) {
			int delta = ww - (_c + _width);
			cc = _c + delta;
		}
		else {
			cc = _c;
		}

		if(_r +_height >= hh) {
			int delta = hh - (_r +_height);
			rr = _r + delta;
		}
		else {
			rr = _r;
		}

		if (_r < 0) {
			rr = 0;
		}

		if (_c < 0) {
			cc = 0;
		}


		move(rr, cc);
	}
}

// Refresh window
void Frame::refresh() {
    if (_has_super) {
        touchwin(_super);
    }
    wrefresh(_w);
}

// Move a window to a new position
void Frame::move(int r, int c) {
    if (_has_super) {
        mvderwin(_w, r, c);
        _row = r;
        _col = c;
        ::refresh();
    }
}

void Frame::fill_window() {
    int mid_x = _width/2;
    int mid_y = _height/2;

    // first region w 0s
    for (int y = 0; y < mid_y; ++y) {
        for(int x = 0; x < mid_x; ++x) {
            mvwaddch(_w, y, x, '0');
        }
    }
    for (int y = 0; y < mid_y; ++y) {
        for(int x = mid_x; x < _width; ++x) {
            mvwaddch(_w, y, x, '1');
        }
    }
    for (int y = mid_y; y < _height; ++y) {
        for(int x = 0; x < mid_x; ++x) {
            mvwaddch(_w, y, x, '2');
        }
    }
    for (int y = mid_y; y < _height; ++y) {
        for(int x = mid_x; x < _width; ++x) {
            mvwaddch(_w, y, x, '3');
        }
    }

    for (int y = 0; y < _height; ++y) {
        mvwaddch(_w, y, 0, '|');
        mvwaddch(_w, y, _width - 1, '|');
    }

    for (int x = 0; x < _width; ++ x) {
        mvwaddch(_w, 0, x, '-');
        mvwaddch(_w, _height - 1, x, '-');
    }
}

void Frame::gen_Perlin(const unsigned int &seed) {
    PerlinNoise pn(seed);

    for (int i = 0; i < _height; ++i) {
        for (int j = 0; j < _width; ++j) {
            double x = (double)j/((double)_width);
            double y = (double)i/((double) _height);

            double n = pn.noise(10 * x, 10 * y, 0.8);

            // Water
            if (n < 0.35) {
                mvwaddch(_w, i, j, '~');
            } else if (n >= 0.35 && n < 0.6) {
                // Floors
                mvwaddch(_w, i, j, '.');
            } else if (n >= 0.6 && n < 0.8) {
                // Walls
                mvwaddch(_w, i, j, '#');
            } else {
                // Ice
                mvwaddch(_w, i, j, 'S');
            }
        }
    }
}

WINDOW* Frame::win() {
    return _w;
}

WINDOW* Frame::super() {
    return _super;
}

bool Frame::has_super() {
    return _has_super;
}

int Frame::height() {
    return _height;
}

int Frame::width() {
    return _width;
}

int Frame::row() {
    return _row;
}

int Frame::col() {
    return _col;
}