1 /*
2     settings.c
3 
4     Copyright (C) 2011  Thomas Huth
5 
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include <math.h>
21 #include <SDL.h>
22 
23 #include "i18n.h"
24 #include "ballergui.h"
25 #include "baller1.h"
26 #include "baller2.h"
27 #include "sdlgui.h"
28 #include "screen.h"
29 #include "settings.h"
30 
31 
32 #define P1_HUMAN		6
33 #define P1_COMPUTER		7
34 #define P1_AISTRATEGYLEFT	10
35 #define P1_AISTRATEGYSTR	11
36 #define P1_AISTRATEGYRIGHT	12
37 #define P1_AISTRENGTHLEFT	15
38 #define P1_AISTRENGTHSTR	16
39 #define P1_AISTRENGTHRIGHT	17
40 #define P2_HUMAN		22
41 #define P2_COMPUTER		23
42 #define P2_AISTRATEGYLEFT	26
43 #define P2_AISTRATEGYSTR	27
44 #define P2_AISTRATEGYRIGHT	28
45 #define P2_AISTRENGTHLEFT	31
46 #define P2_AISTRENGTHSTR	32
47 #define P2_AISTRENGTHRIGHT	33
48 #define ROUNDS_20	35
49 #define ROUNDS_50	36
50 #define ROUNDS_100	37
51 #define ROUNDS_NOLIMIT	38
52 #define GIVEUPALLOWED	39
53 #define REPAIRALLOWED	40
54 #define NEWGAME		41
55 #define CONTINUE	42
56 #define QUITGAME	43
57 
58 static char dlg_p1level[2] = "2";
59 static char dlg_p2level[2] = "3";
60 
61 static SGOBJ settingsdlg[] =
62 {
63 	{ SGBOX, 0, 0, 0,0, 71,22, NULL },
64 	{ SGTEXT, 0, 0, 27,1, 13,1, N_("Settings") },
65 
66 	{ SGBOX, 0, 0, 1,3, 34,11, NULL },
67 	{ SGTEXT, 0, 0, 12,4, 9,1, N_("Player 1") },
68 	{ SGTEXT, 0, 0, 3,6, 5,1, N_("Name:") },
69 	{ SGEDITFIELD, 0, 0, 9,6, 22,1, nsp1 },
70 	{ SGRADIOBUT, 0, SG_SELECTED, 3,8, 8,1, N_("Human") },
71 	{ SGRADIOBUT, 0, 0, 15,8, 10,1, N_("Computer") },
72 	{ SGTEXT, 0, 0, 3,10, 15,1, N_("AI strategy:") },
73 	{ SGBOX, 0, 0, 19,10, 15,1, NULL },
74 	{ SGBUTTON, SG_EXIT, 0, 19,10, 2,1, SGARROWLEFTSTR },
75 	{ SGTEXT, 0, 0, 22,10, 8,1, NULL },
76 	{ SGBUTTON, SG_EXIT, 0, 32,10, 2,1, SGARROWRIGHTSTR },
77 	{ SGTEXT, 0, 0, 3,12, 15,1, N_("AI strength:") },
78 	{ SGBOX, 0, 0, 23,12, 7,1, NULL },
79 	{ SGBUTTON, SG_EXIT, 0, 23,12, 2,1, SGARROWLEFTSTR },
80 	{ SGTEXT, 0, 0, 26,12, 1,1, dlg_p1level },
81 	{ SGBUTTON, SG_EXIT, 0, 28,12, 2,1, SGARROWRIGHTSTR },
82 
83 	{ SGBOX, 0, 0, 36,3, 34,11, NULL },
84 	{ SGTEXT, 0, 0, 47,4, 9,1, N_("Player 2") },
85 	{ SGTEXT, 0, 0, 38,6, 5,1, N_("Name:") },
86 	{ SGEDITFIELD, 0, 0, 44,6, 22,1, nsp2 },
87 	{ SGRADIOBUT, 0, SG_SELECTED, 38,8, 8,1, N_("Human") },
88 	{ SGRADIOBUT, 0, 0, 50,8, 10,1, N_("Computer") },
89 	{ SGTEXT, 0, 0, 38,10, 15,1, N_("AI strategy:") },
90 	{ SGBOX, 0, 0, 54,10, 15,1, NULL },
91 	{ SGBUTTON, SG_EXIT, 0, 54,10, 2,1, SGARROWLEFTSTR },
92 	{ SGTEXT, 0, 0, 57,10, 8,1, NULL },
93 	{ SGBUTTON, SG_EXIT, 0, 67,10, 2,1, SGARROWRIGHTSTR },
94 	{ SGTEXT, 0, 0, 38,12, 15,1, N_("AI strength:") },
95 	{ SGBOX, 0, 0, 58,12, 7,1, NULL },
96 	{ SGBUTTON, SG_EXIT, 0, 58,12, 2,1, SGARROWLEFTSTR },
97 	{ SGTEXT, 0, 0, 61,12, 1,1, dlg_p2level },
98 	{ SGBUTTON, SG_EXIT, 0, 63,12, 2,1, SGARROWRIGHTSTR },
99 
100 	{ SGTEXT, 0, 0, 3,15, 20,1, N_("Maximum rounds:") },
101 	{ SGRADIOBUT, 0, 0, 25,15, 4,1, "20" },
102 	{ SGRADIOBUT, 0, 0, 31,15, 4,1, "50" },
103 	{ SGRADIOBUT, 0, 0, 37,15, 5,1, "100" },
104 	{ SGRADIOBUT, 0, SG_SELECTED, 44,15, 12,1, N_("unlimited") },
105 
106 	{ SGCHECKBOX, 0, SG_SELECTED, 3,17, 26,1, N_("King may capitulate") },
107 	{ SGCHECKBOX, 0, SG_SELECTED, 36,17, 22,1, N_("Players may build") },
108 
109 	{ SGBUTTON, 0, 0, 2,20, 20,1, N_("New game") },
110 	{ SGBUTTON, SG_DEFAULT, 0, 24,20, 19,1, N_("Continue game") },
111 	{ SGBUTTON, 0, 0, 45,20, 20,1, N_("Exit program") },
112 
113 	{ -1, 0, 0, 0,0, 0,0, NULL }
114 };
115 
116 
117 /**
118  * Settings dialog
119  */
settings(void)120 int settings(void)
121 {
122 	int retbut;
123 	int done = 0;
124 
125 	SDLGui_CenterDlg(settingsdlg);
126 
127 	do
128 	{
129 		settingsdlg[P1_AISTRATEGYSTR].txt = (char *)cn[cw[0]];
130 		settingsdlg[P2_AISTRATEGYSTR].txt = (char *)cn[cw[1]];
131 		dlg_p1level[0] = '1' + cx[0];
132 		dlg_p2level[0] = '1' + cx[1];
133 		retbut = SDLGui_DoDialog(settingsdlg, NULL);
134 		switch (retbut)
135 		{
136 		 case P1_AISTRATEGYLEFT:
137 			if (cw[0] > 0)
138 				cw[0] -= 1;
139 			break;
140 		 case P1_AISTRATEGYRIGHT:
141 			if (cw[0] < 6)
142 				cw[0] += 1;
143 			break;
144 		 case P1_AISTRENGTHLEFT:
145 			if (cx[0] > 0)
146 				cx[0] -= 1;
147 			break;
148 		 case P1_AISTRENGTHRIGHT:
149 			if (cx[0] < 3)
150 				cx[0] += 1;
151 			break;
152 		 case P2_AISTRATEGYLEFT:
153 			if (cw[1] > 0)
154 				cw[1] -= 1;
155 			break;
156 		 case P2_AISTRATEGYRIGHT:
157 			if (cw[1] < 6)
158 				cw[1] += 1;
159 			break;
160 		 case P2_AISTRENGTHLEFT:
161 			if (cx[1] > 0)
162 				cx[1] -= 1;
163 			break;
164 		 case P2_AISTRENGTHRIGHT:
165 			if (cx[1] < 3)
166 				cx[1] += 1;
167 			break;
168 		 default:
169 		 	done = 1;
170 		}
171 	} while (!done);
172 
173 	SDL_UpdateRect(surf, 0,0, 0,0);
174 
175 	if (settingsdlg[P1_HUMAN].state & SG_SELECTED)
176 	{
177 		if (settingsdlg[P2_HUMAN].state & SG_SELECTED)
178 			mod = 0;	/* Human vs. human */
179 		else
180 			mod = 1;	/* Human vs. computer */
181 	}
182 	else
183 	{
184 		if (settingsdlg[P2_HUMAN].state & SG_SELECTED)
185 			mod = 2;	/* Computer vs. human */
186 		else
187 			mod = 3;	/* Computer vs. computer */
188 	}
189 	if (mod < 2)
190 		l_nam = nsp1;
191 	else
192 		l_nam = cn[cw[0]];
193 	if (mod & 1)
194 		r_nam = cn[cw[1]];
195 	else
196 		r_nam = nsp2;
197 
198 	/* Max. amount of rounds */
199 	if (settingsdlg[ROUNDS_20].state & SG_SELECTED)
200 		max_rund = 20;
201 	else if (settingsdlg[ROUNDS_50].state & SG_SELECTED)
202 		max_rund = 50;
203 	else if (settingsdlg[ROUNDS_100].state & SG_SELECTED)
204 		max_rund = 100;
205 	else if (settingsdlg[ROUNDS_NOLIMIT].state & SG_SELECTED)
206 		max_rund = 32767;
207 
208 	/* Is the king allowed to give up? */
209 	au_kap = settingsdlg[GIVEUPALLOWED].state & SG_SELECTED;
210 
211 	/* Is repairing allowed? */
212 	an_erl = settingsdlg[REPAIRALLOWED].state & SG_SELECTED;
213 
214 	/* New game? */
215 	if (retbut == NEWGAME)
216 	{
217 		dlg_new_game();
218 	}
219 	/* Quit game? */
220 	else if (retbut == QUITGAME || retbut == SDLGUI_QUIT)
221 	{
222 		return DlgAlert_Query(_("Quit Ballerburg?"), _("Yes"), _("No"));
223 	}
224 
225 	return 0;
226 }
227 
228 
229 /* ---------------------------------------------------------------- */
230 
231 static void draw_castle1(int x, int y, int w, int h);
232 static void draw_castle2(int x, int y, int w, int h);
233 
234 #define NEW_P1_PREV	3
235 #define NEW_P1_NEXT	4
236 #define NEW_P2_PREV	6
237 #define NEW_P2_NEXT	7
238 #define NEW_OK		8
239 #define NEW_ABORT	9
240 
241 static SGOBJ newgamedlg[] =
242 {
243 	{ SGBOX, 0, 0, 0,0, 58,20, NULL },
244 	{ SGTEXT, 0, 0, 24,1, 12,1, N_("New game") },
245 
246 	{ SGTEXT, 0, 0, 9,3, 23,1, N_("Player 1") },
247 	{ SGBUTTON, SG_EXIT, 0, 2,16, 12,1, "\x04" },     // Arrow left
248 	{ SGBUTTON, SG_EXIT, 0, 16,16, 12,1, "\x03" },    // Arrow right
249 
250 	{ SGTEXT, 0, 0, 37,3, 23,1, N_("Player 2") },
251 	{ SGBUTTON, SG_EXIT, 0, 30,16, 12,1, "\x04" },    // Arrow left
252 	{ SGBUTTON, SG_EXIT, 0, 44,16, 12,1, "\x03" },    // Arrow right
253 
254 	{ SGBUTTON, SG_DEFAULT, 0, 19,18, 8,1, "OK" },
255 	{ SGBUTTON, SG_CANCEL, 0, 31,18, 8,1, "Cancel" },
256 
257 	{ SGBOX, 0, 0, 2,5, 26,10, NULL },
258 	{ SGUSER, 0, 0, 2,5, 26,10, (void*)draw_castle1 },
259 
260 	{ SGBOX, 0, 0, 30,5, 26,10, NULL },
261 	{ SGUSER, 0, 0, 30,5, 26,10, (void*)draw_castle2 },
262 
263 	{ -1, 0, 0, 0,0, 0,0, NULL }
264 };
265 
266 
267 /**
268  * Draw castle
269  */
draw_castle(int n,int x,int y,int w,int h)270 static void draw_castle(int n, int x, int y, int w, int h)
271 {
272 	SDL_Rect rect;
273 	short oy0,oy1;
274 
275 	rect.x = x + 1;
276 	rect.y = y + 1;
277 	rect.w = w - 2;
278 	rect.h = h - 2;
279 	SDL_FillRect(surf, &rect, SDL_MapRGB(surf->format,0xff,0xff,0xff));
280 
281 
282 	oy0 = by[0];
283 	oy1 = by[1];
284 
285 	bx[0]=x;
286 	by[0]=y+h;
287 	bx[1]=x+w;
288 	by[1]=y+h;
289 
290 	burg(n+1);
291 
292 	by[0]=oy0;
293 	by[1]=oy1;
294 }
295 
296 
draw_castle1(int x,int y,int w,int h)297 static void draw_castle1(int x, int y, int w, int h)
298 {
299 	draw_castle(1, x, y, w, h);
300 }
301 
draw_castle2(int x,int y,int w,int h)302 static void draw_castle2(int x, int y, int w, int h)
303 {
304 	draw_castle(2, x, y, w, h);
305 }
306 
307 
308 /**
309  * Dialog for "new game"
310  */
dlg_new_game(void)311 void dlg_new_game(void)
312 {
313 
314 	int retbut;
315 	short ob0, ob1, ol[8];
316 
317 	/* Sichern der alten Werte */
318 	ob0 = bur[0];
319 	ob1 = bur[1];
320 	ol[0]=ge[0];
321 	ol[1]=ge[1];
322 	ol[2]=pu[0];
323 	ol[3]=pu[1];
324 	ol[4]=ku[0];
325 	ol[5]=ku[1];
326 	ol[6]=vo[0];
327 	ol[7]=vo[1];
328 
329 	SDLGui_CenterDlg(newgamedlg);
330 
331 	do
332 	{
333 		retbut = SDLGui_DoDialog(newgamedlg, NULL);
334 		switch (retbut)
335 		{
336 		case NEW_P1_PREV:
337 			bur[0] = (bur[0] - 1 + b_anz) % b_anz;
338 			break;
339 		case NEW_P1_NEXT:
340 			bur[0] = (bur[0] + 1 + b_anz) % b_anz;
341 			break;
342 		case NEW_P2_PREV:
343 			bur[1] = (bur[1] - 1 + b_anz) % b_anz;
344 			break;
345 		case NEW_P2_NEXT:
346 			bur[1] = (bur[1] + 1 + b_anz) % b_anz;
347 			break;
348 		}
349 	}
350 	while (retbut != NEW_OK && retbut != NEW_ABORT);
351 
352 	ge[0]=ol[0];
353 	ge[1]=ol[1];
354 	pu[0]=ol[2];
355 	pu[1]=ol[3];
356 	ku[0]=ol[4];
357 	ku[1]=ol[5];
358 	vo[0]=ol[6];
359 	vo[1]=ol[7];
360 	fn();
361 
362 	if (retbut == NEW_OK)
363 	{
364 		neues();
365 		werdran(1);
366 	}
367 	else /* if (retbut == NEW_ABORT) */
368 	{
369 		bur[0]=ob0;
370 		bur[1]=ob1;
371 	}
372 
373 	SDL_UpdateRect(surf, 0,0, 0,0);
374 }
375