Debian Codegraph @

src/render_ai.cpp

raw symbols SCIP
1 ///////////////////////////////////////////////
2 //
3 // Snipe2d ludum dare 48h compo entry
4 //
5 // Jari Komppa aka Sol
6 // http://iki.fi/sol
7 //
8 ///////////////////////////////////////////////
9 // License
10 ///////////////////////////////////////////////
11 //
12 // This software is provided 'as-is', without any express or implied
13 // warranty. In no event will the authors be held liable for any damages
14 // arising from the use of this software.
15 //
16 // Permission is granted to anyone to use this software for any purpose,
17 // including commercial applications, and to alter it and redistribute it
18 // freely, subject to the following restrictions:
19 //
20 // 1. The origin of this software must not be misrepresented; you must not
21 // claim that you wrote the original software. If you use this software
22 // in a product, an acknowledgment in the product documentation would be
23 // appreciated but is not required.
24 // 2. Altered source versions must be plainly marked as such, and must not be
25 // misrepresented as being the original software.
26 // 3. This notice may not be removed or altered from any source distribution.
27 //
28 // (eg. same as ZLIB license)
29 //
30 ///////////////////////////////////////////////
31 //
32 // Houses are taken from a satellite picture of glasgow.
33 //
34 // The sources are a mess, as I didn't even try to do anything
35 // really organized here.. and hey, it's a 48h compo =)
36 //
37 #include "snipe2d.h"
38
39 #define PI 3.1415926535897932384626433832795f
40
41 void drawSprite(int aX, int aY, int aSize, int aSprite)
42 {
43 if ( SDL_LockSurface(Game.Screen) < 0 )
44 return;
45 if ( SDL_LockSurface(Game.CharSprite) < 0 )
46 {
47 SDL_UnlockSurface(Game.Screen);
48 return;
49 }
50
51 int i, j, x, yp, y, sx, sy, spriterow;
52 int xinc, yinc;
53 xinc = (32 << SHIFT_AMOUNT) / aSize;
54 yinc = (32 << SHIFT_AMOUNT) / aSize;
55 int spriteofs = aSprite * (Game.CharSprite->pitch / 2) * 32;
56 spriterow = spriteofs;
57 yp = (aY * (Game.Screen->pitch / 2));
58 y = aY;
59 sy = 0;
60 for (i = 0; i < aSize; i++)
61 {
62 x = aX;
63 sx = 0;
64 for (j = 0; j < aSize; j++)
65 {
66 x++;
67 sx += xinc;
68 if (y >= 0 && x >= 0 && y < 480 && x < 640)
69 {
70 // The sprite sequence uses green as alpha; new green is
71 // synthesized as average of red and blue
72 short pix = ((short *)Game.Screen->pixels)[x + yp];
73 short tex = ((short *)Game.CharSprite->pixels)[spriterow + (sx >> SHIFT_AMOUNT)];
74 int alpha = (tex & 0x7E0) >> 5;
75 int alpha1 = 64 - alpha;
76 int r = (((pix >> 11) & 0x1f) * alpha1 + ((tex >> 11) & 0x1f) * alpha) >> 6;
77 int g;
78 if (aSprite == 33)
79 g = (((pix >> 5) & 0x3f) * alpha1) >> 6;
80 else
81 g = (((pix >> 5) & 0x3f) * alpha1 + (((tex >> 11) & 0x1f) + ((tex >> 0) & 0x1f)) * alpha) >> 6;
82 int b = (((pix >> 0) & 0x1f) * alpha1 + ((tex >> 0) & 0x1f) * alpha) >> 6;
83 ((short *)Game.Screen->pixels)[x + yp] = (r << 11) + (g << 5) + b;
84 }
85
86 }
87 yp += Game.Screen->pitch / 2;
88 sy += yinc;
89 spriterow = spriteofs + (sy >> SHIFT_AMOUNT) * (Game.CharSprite->pitch / 2);
90 y++;
91 }
92
93 SDL_UnlockSurface(Game.CharSprite);
94 SDL_UnlockSurface(Game.Screen);
95 }
96
97 void worldtoscreen(int &x, int &y)
98 {
99 x = (int)((x - (Game.MouseX + Game.CenterX)) / Game.CoordScale);
100 y = (int)((y - (Game.MouseY + Game.CenterY)) / Game.CoordScale);
101 }
102
103 void drawCharacter(CHARACTER &c)
104 {
105 if (c.mType == -1) return;
106 int sx = (int)((c.mX - (Game.MouseX + Game.CenterX)) / Game.CoordScale);
107 int sy = (int)((c.mY - (Game.MouseY + Game.CenterY)) / Game.CoordScale);
108 int ax = (int)((c.mX - (Game.MouseX + Game.WobbleX + Game.CenterX)) / Game.CoordScale);
109 int ay = (int)((c.mY - (Game.MouseY + Game.WobbleY + Game.CenterY)) / Game.CoordScale);
110 int size = (int)(2 / Game.CoordScale);
111 if (size < 2)
112 size = 2;
113
114 int mode = 0;
115 if (Game.CoordScale >= 1)
116 {
117 if (c.mType == 2)
118 return; // don't draw pedestrians in big scale
119 mode = 1;
120 size *= 2;
121 }
122 else
123 if (Game.CoordScale <= 0.25f)
124 {
125 mode = -1;
126 if (sx - size < 320 &&
127 sx + size > 320 &&
128 sy - size < 240 &&
129 sy + size > 240)
130 {
131 Game.SightedCharacter = &c;
132 }
133 }
134
135 sx -= size;
136 sy -= size;
137
138 if (sx > -size*2 && sx < 640+size*2 &&
139 sy > -size*2 && sy < 480+size*2)
140 {
141 int color = 0;
142 SDL_Rect r;
143 switch (c.mType)
144 {
145 case 0: // bad guy
146 color = SDL_MapRGB(Game.Screen->format, 0xc0 + (((SDL_GetTicks() >> 8)&1)?0:0x3f), 0, 0);
147 break;
148 case 1: // VIP
149 color = SDL_MapRGB(Game.Screen->format, 0, 0, 0x9f);
150 break;
151 case 2: // pedestrian
152 color = SDL_MapRGB(Game.Screen->format, 0x9f, 0x9f, 0x9f);
153 break;
154 case 3: // hit
155 color = SDL_MapRGB(Game.Screen->format, 0x9f, 0, 0);
156 break;
157 case 4: // miss
158 color = SDL_MapRGB(Game.Screen->format, 0,0,0);
159 break;
160 }
161 if (mode == 0 || mode == 1)
162 {
163 r.x = sx;
164 r.y = sy;
165 r.w = size * 2;
166 r.h = size * 2;
167 SDL_FillRect(Game.Screen, &r, color);
168 }
169 if (mode == -1)
170 {
171
172 // draw actual position
173 if (c.mType == 3)
174 {
175 drawSprite(ax - size / 2,
176 ay - size / 2,
177 size,
178 33);
179 }
180 else
181 if (c.mType == 4)
182 {
183 drawSprite(ax - size / 2,
184 ay - size / 2,
185 size,
186 32);
187 }
188 else
189 {
190 drawSprite(ax - size / 2,
191 ay - size / 2,
192 size,
193 ((int)floor(32-((atan2(c.mXi,c.mYi))/PI)*16+32))%32);
194 }
195
196 int linewidth = (size / 8) + 1;
197 int segmentsize = (size / 2) + 1;
198 int bottomright = (size * 2) - segmentsize + linewidth;
199
200 // draw box
201 r.x = sx;
202 r.y = sy;
203 r.w = segmentsize;
204 r.h = linewidth;
205 SDL_FillRect(Game.Screen, &r, color);
206 r.x = sx;
207 r.y = sy;
208 r.w = linewidth;
209 r.h = segmentsize;
210 SDL_FillRect(Game.Screen, &r, color);
211
212 r.x = sx + bottomright;
213 r.y = sy;
214 r.w = segmentsize;
215 r.h = linewidth;
216 SDL_FillRect(Game.Screen, &r, color);
217 r.x = sx;
218 r.y = sy + bottomright;
219 r.w = linewidth;
220 r.h = segmentsize;
221 SDL_FillRect(Game.Screen, &r, color);
222
223 r.x = sx;
224 r.y = sy + size * 2;
225 r.w = segmentsize;
226 r.h = linewidth;
227 SDL_FillRect(Game.Screen, &r, color);
228 r.x = sx + size * 2;
229 r.y = sy;
230 r.w = linewidth;
231 r.h = segmentsize;
232 SDL_FillRect(Game.Screen, &r, color);
233
234 r.x = sx + bottomright;
235 r.y = sy + size * 2;
236 r.w = segmentsize;
237 r.h = linewidth;
238 SDL_FillRect(Game.Screen, &r, color);
239 r.x = sx + size * 2;
240 r.y = sy + bottomright;
241 r.w = linewidth;
242 r.h = segmentsize;
243 SDL_FillRect(Game.Screen, &r, color);
244
245 }
246 if (mode == 1)
247 {
248 // draw connecting lines to targets or horizontal & vertical
249 // ones if not possible
250 if (c.mType == 1 || c.mType == 0)
251 {
252 int x, y;
253 if (c.mType == CHAR_BADGUY)
254 {
255 if (c.mTarget == -1)
256 {
257 r.x = 0;
258 r.y = sy + size;
259 r.w = 640;
260 r.h = 1;
261 SDL_FillRect(Game.Screen, &r, color);
262 r.x = sx + size;
263 r.y = 0;
264 r.w = 1;
265 r.h = 480;
266 SDL_FillRect(Game.Screen, &r, color);
267 return;
268 }
269 x = (int)Game.characters[c.mTarget].mX;
270 y = (int)Game.characters[c.mTarget].mY;
271 }
272 else
273 {
274 x = Game.spawnpoints[c.mTarget].mX;
275 y = Game.spawnpoints[c.mTarget].mY;
276 }
277 worldtoscreen(x, y);
278 if (x > 1 && x < 639 && y > 1 && y < 479 &&
279 sx + size > 1 && sx + size < 639 && sy + size > 1 && sy + size < 479)
280 drawLine(Game.Screen, sx + size, sy + size, x, y, color);
281 else
282 {
283 r.x = 0;
284 r.y = sy + size;
285 r.w = 640;
286 r.h = 1;
287 SDL_FillRect(Game.Screen, &r, color);
288 r.x = sx + size;
289 r.y = 0;
290 r.w = 1;
291 r.h = 480;
292 SDL_FillRect(Game.Screen, &r, color);
293 }
294
295 }
296 }
297 }
298 else // not on screen
299 {
300 if (c.mType == 0 || c.mType == 1)
301 {
302 sx += size;
303 sy += size;
304 if (sx > 640-5) sx = 640 - 5;
305 if (sy > 480-5) sy = 480 - 5;
306 if (sx < 5) sx = 5;
307 if (sy < 5) sy = 5;
308
309 int color = 0;
310 SDL_Rect r;
311 switch (c.mType)
312 {
313 case 0: // bad guy
314 color = SDL_MapRGB(Game.Screen->format, 0xff, 0, 0);
315 break;
316 case 1: // VIP
317 color = SDL_MapRGB(Game.Screen->format, 0, 0, 0x9f);
318 break;
319 }
320 r.x = sx - 5;
321 r.y = sy - 5;
322 r.w = 10;
323 r.h = 10;
324 SDL_FillRect(Game.Screen, &r, color);
325 }
326 }
327 }