1 /*
2     Copyright 2003-2011 Tom Rune Flo
3 
4     This file is part of CAVEZ OF PHEAR
5 
6     CAVEZ OF PHEAR is free software: you can redistribute it and/or modify it under the terms of the GNU
7     General Public License as published by the Free Software Foundation, either version 3 of the
8     License, or (at your option) any later version.
9 
10     CAVEZ OF PHEAR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
11     even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12     General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License along with CAVEZ OF PHEAR. If not,
15     see http://www.gnu.org/licenses/.
16  */
17 
18 #include "editor.h"
19 
20 #include "frame.h"
21 #include "map/map.h"
22 #include "game/rules.h"
23 #include "misc.h"
24 
25 #include <ncurses.h>
26 #include <stdlib.h>
27 #include <signal.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <ctype.h>
31 
32 extern char map[MAP_YSIZE][MAP_XSIZE];
33 int lock;
34 int last_obj;
35 
36 void editor_draw_status(void);
37 
editor_main(const char * file)38 int editor_main(const char* file) {
39 	if (!file) {
40 		fprintf(stderr, "usage: phear -e <file>\n");
41 		exit(1);
42 	}
43 
44 	if (!load_map(file)) {
45 		clear_map();
46 	}
47 
48 	curses_start();
49 	curs_set(1);
50 
51 	if (COLS < 80 || LINES < 25) {
52 		bail("error: your terminal size must be at least 80x25");
53 	}
54 
55 	signal(SIGINT, sigint_handler);
56 	signal(SIGWINCH, sigwinch_handler);
57 
58 	for (int x = 0; x < MAP_XSIZE; x++) {
59 		map[0][x] = MAP_WALL;
60 		map[MAP_YSIZE - 1][x] = MAP_WALL;
61 	}
62 	for (int y = 0; y < MAP_YSIZE; y++) {
63 		map[y][0] = MAP_WALL;
64 		map[y][MAP_XSIZE - 1] = MAP_WALL;
65 		map[y][1] = MAP_WALL;
66 		map[y][MAP_XSIZE - 2] = MAP_WALL;
67 	}
68 
69 	int x = 2;
70 	int y = 2;
71 
72 	lock = 0;
73 
74 	last_obj = 0;
75 
76 	editor_draw_status();
77 
78 	while (1) {
79 		if (count_map_objects(MAP_PLAYER) == 0) {
80 			map[1][1] = MAP_PLAYER;
81 		}
82 
83 		draw_map();
84 		refresh();
85 
86 		int input = mvgetch(y, x);
87 
88 		if (input == KEY_UP) {
89 			y--;
90 		}
91 		if (input == KEY_DOWN) {
92 			y++;
93 		}
94 		if (input == KEY_LEFT) {
95 			x--;
96 		}
97 		if (input == KEY_RIGHT) {
98 			x++;
99 		}
100 
101 		if (y < 2) {
102 			y = MAP_YSIZE - 1;
103 		}
104 		if (y > MAP_YSIZE - 1) {
105 			y = 2;
106 		}
107 		if (x < 2) {
108 			x = MAP_XSIZE - 3;
109 		}
110 		if (x > MAP_XSIZE - 3) {
111 			x = 2;
112 		}
113 
114 		if (input == '\n' || input == ' ' || input == KEY_ENTER) {
115 			if (last_obj != MAP_PLAYER) {
116 				map[y - 1][x] = last_obj;
117 			}
118 		}
119 
120 		if (lock == 1) {
121 			if (input == KEY_RIGHT || input == KEY_LEFT || input == KEY_UP || input == KEY_DOWN) {
122 				if (last_obj != MAP_PLAYER) {
123 					map[y - 1][x] = last_obj;
124 				}
125 			}
126 		}
127 
128 		if (input == KEY_DC || input == 0x7f) {
129 			map[y - 1][x] = MAP_EMPTY;
130 		}
131 
132 		if (input == '0' || input == '|') {
133 			map[y - 1][x] = MAP_EMPTY;
134 			last_obj = MAP_EMPTY;
135 		}
136 		if (input == '1') {
137 			map[y - 1][x] = MAP_DIRT;
138 			last_obj = MAP_DIRT;
139 		}
140 		if (input == '2') {
141 			map[y - 1][x] = MAP_STONE;
142 			last_obj = MAP_STONE;
143 		}
144 		if (input == '3') {
145 			map[y - 1][x] = MAP_DIAMOND;
146 			last_obj = MAP_DIAMOND;
147 		}
148 		if (input == '4') {
149 			map[y - 1][x] = MAP_WALL;
150 			last_obj = MAP_WALL;
151 		}
152 		if (input == '5') {
153 			map[y - 1][x] = MAP_MONEY;
154 			last_obj = MAP_MONEY;
155 		}
156 		if (input == '6') {
157 			map[y - 1][x] = MAP_BOMBPK;
158 			last_obj = MAP_BOMBPK;
159 		}
160 		if (input == '7') {
161 			map[y - 1][x] = MAP_MONSTER;
162 			last_obj = MAP_MONSTER;
163 		}
164 		if (input == '9') {
165 			/* last_obj = MAP_PLAYER; */
166 			for (int yy = 0; yy < MAP_YSIZE; yy++) {
167 				for (int xx = 0; xx < MAP_XSIZE; xx++) {
168 					if (map[yy][xx] == MAP_PLAYER) {
169 						map[yy][xx] = MAP_EMPTY;
170 					}
171 				}
172 			}
173 			map[y - 1][x] = MAP_PLAYER;
174 			map[1][1] = MAP_WALL;
175 		}
176 
177 		if (tolower(input) == 's') {
178 			beep();
179 			curs_set(0);
180 			if (!save_map(file)) {
181 				msgbox("ERROR: Unable to open file for writing!");
182 			} else {
183 				msgbox("Saved successfully!");
184 			}
185 			curs_set(1);
186 		}
187 
188 		/* if(tolower(input) == 'x') {
189 		 *   beep();
190 		 *   if(save_map(file) != 1) {
191 		 *     curses_stop();
192 		 *     exit(0);
193 		 *   }
194 		 * }  */
195 
196 		if (tolower(input) == 'q') {
197 			curs_set(0);
198 
199 			for (;;) {
200 				int rval = tolower(msgbox("Are you sure you want to quit? (Yes/No)"));
201 				if (rval == 'y' || rval == '\n' || rval == ' ') {
202 					curses_stop();
203 					exit(0);
204 				} else if (rval == 'n') {
205 					curs_set(1);
206 					break;
207 				}
208 			}
209 		}
210 
211 
212 		if (tolower(input) == 'l') {
213 			if (lock == 1) {
214 				lock = 0;
215 			} else if (lock == 0) {
216 				lock = 1;
217 			}
218 		}
219 
220 		editor_draw_status();
221 	}
222 
223 	return EXIT_SUCCESS;
224 }
225 
editor_draw_status(void)226 void editor_draw_status(void) {
227 	attrset(COLOR_PAIR(COLOR_GREEN));
228 	mvprintw(0, calc_center(strlen("CAVEZ of PHEAR " VERSION " EDITOR")), "CAVEZ of PHEAR " VERSION " EDITOR");
229 
230 	attrset(COLOR_PAIR(COLOR_MAGENTA));
231 	mvprintw(24, 0, "0 EMPTY  1 #  2 O  3 *  4 :  5 $  6 %%  7 M  9 Z     l LOCK ON/OFF s SAVE  q QUIT");
232 /* mvaddch(24, 13, CHR_DIRT);
233  * mvaddch(24, 18, CHR_STONE);
234  * mvaddch(24, 23, CHR_DIAMOND);
235  * mvaddch(24, 28, CHR_WALL);
236  * mvaddch(24, 33, CHR_MONEY);
237  * mvaddch(24, 38, CHR_PLAYER); */
238 
239 	attrset(COLOR_PAIR(COLOR_MAGENTA) | A_NORMAL);
240 	mvprintw(0, 60, "OBJECT:");
241 
242 	switch (last_obj) {
243 	case MAP_EMPTY:
244 		mvaddch(0, 68, get_map_symbol(last_obj));
245 		attrset(COLOR_PAIR(COLOR_WHITE) | A_NORMAL);
246 		mvprintw(24, 2, "EMPTY");
247 		mvaddch(24, 0, '0' | COLOR_PAIR(COLOR_WHITE) | A_BOLD);
248 		break;
249 
250 	case MAP_DIRT:
251 		mvaddch(0, 68, get_map_symbol(last_obj));
252 		mvaddch(24, 11, get_map_symbol(last_obj));
253 		mvaddch(24, 9, '1' | COLOR_PAIR(COLOR_WHITE) | A_BOLD);
254 		break;
255 
256 	case MAP_WALL:
257 		mvaddch(0, 68, get_map_symbol(last_obj));
258 		mvaddch(24, 26, get_map_symbol(last_obj));
259 		mvaddch(24, 24, '4' | COLOR_PAIR(COLOR_WHITE) | A_BOLD);
260 		break;
261 
262 	case MAP_STONE:
263 		mvaddch(0, 68, get_map_symbol(last_obj));
264 		mvaddch(24, 16, get_map_symbol(last_obj));
265 		mvaddch(24, 14, '2' | COLOR_PAIR(COLOR_WHITE) | A_BOLD);
266 		break;
267 
268 	case MAP_DIAMOND:
269 		mvaddch(0, 68, get_map_symbol(last_obj));
270 		mvaddch(24, 21, get_map_symbol(last_obj));
271 		mvaddch(24, 19, '3' | COLOR_PAIR(COLOR_WHITE) | A_BOLD);
272 		break;
273 
274 	case MAP_MONEY:
275 		mvaddch(0, 68, get_map_symbol(last_obj));
276 		mvaddch(24, 31, get_map_symbol(last_obj));
277 		mvaddch(24, 29, '5' | COLOR_PAIR(COLOR_WHITE) | A_BOLD);
278 		break;
279 
280 	case MAP_BOMBPK:
281 		mvaddch(0, 68, get_map_symbol(last_obj));
282 		mvaddch(24, 36, get_map_symbol(last_obj));
283 		mvaddch(24, 34, '6' | COLOR_PAIR(COLOR_WHITE) | A_BOLD);
284 		break;
285 
286 	case MAP_MONSTER:
287 		mvaddch(0, 68, get_map_symbol(last_obj));
288 		mvaddch(24, 41, get_map_symbol(last_obj));
289 		mvaddch(24, 39, '7' | COLOR_PAIR(COLOR_WHITE) | A_BOLD);
290 		break;
291 
292 	case MAP_PLAYER:
293 		exit(0);
294 		mvaddch(24, 36, get_map_symbol(last_obj));
295 		break;
296 	}
297 
298 
299 	attrset(COLOR_PAIR(COLOR_MAGENTA) | A_NORMAL);
300 	mvprintw(0, 71, "LOCK:");
301 
302 	if (lock == 0) {
303 		attrset(COLOR_PAIR(COLOR_WHITE) | A_NORMAL);
304 		mvprintw(0, 77, "OFF");
305 		mvprintw(24, 62, "OFF");
306 	}
307 	if (lock == 1) {
308 		attrset(COLOR_PAIR(COLOR_WHITE) | A_BOLD);
309 		mvprintw(0, 77, "ON ");
310 		mvprintw(24, 59, "ON");
311 	}
312 
313 	attrset(COLOR_PAIR(COLOR_MAGENTA));
314 	mvprintw(0, 0, "*: %d $: %d SCORE: %d   ", count_map_objects(MAP_DIAMOND),
315 	         count_map_objects(MAP_MONEY), (count_map_objects(MAP_DIAMOND) * POINTS_DIAMOND) +
316 	         (count_map_objects(MAP_MONEY) * POINTS_MONEY));
317 
318 	attrset(A_NORMAL);
319 }
320