1 /*
2     cannoneer.c
3 
4     Copyright (C) 1987, 1989  Eckhard Kruse
5     Copyright (C) 2010  Thomas Huth
6 
7     This program is free software: you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation, either version 3 of the License, or
10     (at your option) any later version.
11 
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16 
17     You should have received a copy of the GNU General Public License
18     along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include <math.h>
22 #include <SDL.h>
23 
24 #include "i18n.h"
25 #include "ballergui.h"
26 #include "baller1.h"
27 #include "sdlgui.h"
28 #include "screen.h"
29 #include "sdlgfx.h"
30 
31 
32 static void draw_cannoneer(int x, int y, int w, int h);
33 
34 
35 static short wi, pv;    /* Winkel und Pulver */
36 
37 /* The cannoneer dialog data: */
38 
39 #define WL2 3		/* Winkel um 10 verkleinern */
40 #define WL1 4		/* Winkel um 1 verkleinern */
41 #define WR1 6
42 #define WR2 7
43 #define PL2 9
44 #define PL1 10
45 #define PR1 12
46 #define PR2 13
47 #define SOK 14
48 #define SAB 15
49 
50 // #define WINK TBD
51 // #define PULV TBD
52 
53 static char dlg_winkel[4];
54 static char dlg_pulver[3];
55 
56 static SGOBJ cannoneerdlg[] =
57 {
58 	{ SGBOX, 0, 0, 0,0, 42,15, NULL },
59 	{ SGTEXT, 0, 0, 18,1, 6,1, N_("Cannon") },
60 
61 	{ SGTEXT, 0, 0, 2,3, 7,1, N_("Angle:") },
62 	{ SGBUTTON, SG_EXIT, 0, 12,3, 4,1, "\x04\04" },   // 2 arrows left
63 	{ SGBUTTON, SG_EXIT, 0, 17,3, 3,1, "\x04" },      // Arrow left
64 	{ SGTEXT, 0, 0, 22,3, 4,1, dlg_winkel },
65 	{ SGBUTTON, SG_EXIT, 0, 26,3, 3,1, "\x03" },      // Arrow right
66 	{ SGBUTTON, SG_EXIT, 0, 30,3, 4,1, "\x03\x03" },  // 2 arrows right
67 
68 	{ SGTEXT, 0, 0, 2,5, 7,1, N_("Gunpowder:") },
69 	{ SGBUTTON, SG_EXIT, 0, 12,5, 4,1, "\x04\04" },   // 2 arrows left
70 	{ SGBUTTON, SG_EXIT, 0, 17,5, 3,1, "\x04" },      // Arrow left
71 	{ SGTEXT, 0, 0, 22,5, 4,1, dlg_pulver },
72 	{ SGBUTTON, SG_EXIT, 0, 26,5, 3,1, "\x03" },      // Arrow right
73 	{ SGBUTTON, SG_EXIT, 0, 30,5, 4,1, "\x03\x03" },  // 2 arrows right
74 
75 	{ SGBUTTON, SG_DEFAULT, 0, 33,11, 8,1, "OK" },
76 	{ SGBUTTON, SG_CANCEL, 0, 33,13, 8,1, "Cancel" },
77 
78 	{ SGBOX, 0, 0, 2,7, 30,7, NULL },
79 	{ SGUSER, 0, 0, 2,7, 30,7, (void*)draw_cannoneer },
80 
81 	{ -1, 0, 0, 0,0, 0,0, NULL }
82 };
83 
84 
85 /**
86  * Draw cannoneer
87  */
draw_cannoneer(int x,int y,int w,int h)88 static void draw_cannoneer(int x, int y, int w, int h)
89 {
90 	static short fig[]={ 0,0,15,20,30,20,20,15,10,0,10,-30,18,-18,20,-5,24,-6,
91 	                     20,-25,10,-40,0,-45,
92 	                     -10,-40,-20,-25,-24,-6,-20,-5,-18,-18,-10,-30,-10,0,
93 	                     -20,15,-30,20,-15,20, -1,-1
94 	                   }; /* Daten für das Männchen */
95 	int xk,yk;
96 	int fl;
97 	int i;
98 	double s,c;
99 	SDL_Rect rect;
100 
101 	rect.x = x + 1;
102 	rect.y = y + 1;
103 	rect.w = w - 2;
104 	rect.h = h - 2;
105 	SDL_FillRect(surf, &rect, SDL_MapRGB(surf->format,0xff,0xff,0xff));
106 
107 	xk = x + w / 2;
108 	yk = y + h / 2 + 40;
109 
110 	/* Draw the cannoneer man */
111 	color(1);
112 	filledCircleColor(surf, xk-88*f, yk-60, 15, 0x000000ff);
113 
114 	i=0;
115 	while ( fig[i]!=-1 )
116 	{
117 		xy[i]=xk-88*f+fig[i];
118 		i++;
119 		xy[i]=yk-5+fig[i];
120 		i++;
121 	}
122 	xy[i++]=xy[0];
123 	xy[i++]=xy[1];
124 	scr_fillarea(i/2-1, xy);
125 
126 	/* Draw the cannon */
127 	color( 1 );
128 	filledCircleColor(surf, xk, yk, 15, 0x000000ff);
129 
130 	s=sin(wi/P57);
131 	c=cos(wi/P57);
132 	fl=-f;
133 	if ( wi>90 )
134 	{
135 		fl=-fl;
136 		c=-c;
137 	}
138 	xy[0]=xk+fl*(c*14+s*14);
139 	xy[1]=yk+s*14-c*14;
140 	xy[2]=xk+fl*(c*14+s*40);
141 	xy[3]=yk+s*14-c*40;
142 	xy[4]=xk-fl*(c*55-s*40);
143 	xy[5]=yk-s*55-c*40;
144 	xy[6]=xk-fl*(c*55-s*14);
145 	xy[7]=yk-s*55-c*14;
146 	xy[8]=xy[0];
147 	xy[9]=xy[1];
148 	scr_fillarea(4, xy);
149 }
150 
151 
152 /**
153  * Kanonenobjektbaum, Wahl von Winkel und Pulver
154  */
sch_obj(short k)155 int sch_obj(short k)
156 {
157 	short i = 0;
158 	char *aw,*ap;
159 
160 	dlg_winkel[0] = dlg_pulver[0] = 0;
161 	aw = dlg_winkel;
162 	ap = dlg_pulver;
163 	*(ap+2)=0;
164 
165 	SDLGui_CenterDlg(cannoneerdlg);
166 
167 	wi=ka[n][k].w;
168 	pv=ka[n][k].p;
169 
170 	scr_sf_interior(1);
171 
172 	do
173 	{
174 		if (pv > pu[n])
175 		{
176 			pv=pu[n];
177 		}
178 		*aw=48+wi/100;
179 		*(aw+1)=48+wi%100/10;
180 		*(aw+2)=48+wi%10;
181 		if (wi < 100)
182 		{
183 			*aw=*(aw+1);
184 			*(aw+1)=*(aw+2);
185 			*(aw+2)=0;
186 		}
187 		*ap=48+pv/10;
188 		*(ap+1)=48+pv%10;
189 		if (pv<10)
190 		{
191 			*ap=*(ap+1);
192 			*(ap+1)=0;
193 		}
194 
195 		i = SDLGui_DoDialog(cannoneerdlg, NULL);
196 
197 		wi-=10*(i==WL2)-10*(i==WR2)+(i==WL1)-(i==WR1);
198 		if ( wi<0 ) wi=0;
199 		if ( wi>180 ) wi=180;
200 		pv-= 3*(i==PL2)- 3*(i==PR2)+(i==PL1)-(i==PR1);
201 		if ( pv<5 ) pv=5;
202 		if ( pv>20 ) pv=20;
203 	}
204 	while (i != SOK && i != SAB);
205 
206 	SDL_UpdateRect(surf, 0,0, 0,0);
207 
208 	ka[n][k].w=wi;
209 	ka[n][k].p=pv;
210 
211 	return (i == SOK);
212 }
213