Debian Codegraph @

engine/map/minimap.cpp

raw
1 // ____ _ __
2 // / __ )____ _____ | | / /___ ___________
3 // / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
4 // / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
5 // /_____/\____/____/ |__/|__/\__,_/_/ /____/
6 //
7 // A futuristic real-time strategy game.
8 // This file is part of Bos Wars.
9 //
10 /**@name minimap.cpp - The minimap. */
11 //
12 // (c) Copyright 1998-2010 by Lutz Sammer and Jimmy Salmon
13 //
14 // This program is free software; you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation; only version 2 of the License.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 // 02111-1307, USA.
27 //
28
29 //@{
30
31 /*----------------------------------------------------------------------------
32 -- Includes
33 ----------------------------------------------------------------------------*/
34
35 #include <string.h>
36
37 #include "stratagus.h"
38 #include "video.h"
39 #include "map.h"
40 #include "minimap.h"
41 #include "unittype.h"
42 #include "player.h"
43 #include "unit.h"
44 #include "ui.h"
45 #include "editor.h"
46 #include "patch.h"
47 #include "patch_type.h"
48
49 /*----------------------------------------------------------------------------
50 -- Defines
51 ----------------------------------------------------------------------------*/
52
53 #define MINIMAP_FAC (16 * 3) /// integer scale factor
54
55 /// unit attacked are shown red for at least this amount of cycles
56 #define ATTACK_RED_DURATION (1 * CYCLES_PER_SECOND)
57 /// unit attacked are shown blinking for this amount of cycles
58 #define ATTACK_BLINK_DURATION (7 * CYCLES_PER_SECOND)
59
60 #define SCALE_PRECISION 100
61
62
63 /*----------------------------------------------------------------------------
64 -- Variables
65 ----------------------------------------------------------------------------*/
66
67 static GLuint MinimapTexture;
68 static unsigned char *MinimapSurfaceGL;
69 static unsigned char *MinimapTerrainSurfaceGL;
70 static int MinimapTextureWidth;
71 static int MinimapTextureHeight;
72 static SDL_Surface *MinimapSurface; /// generated minimap
73 static SDL_Surface *MinimapTerrainSurface; /// generated minimap terrain
74
75 static int *Minimap2MapX; /// fast conversion table
76 static int *Minimap2MapY; /// fast conversion table
77 static int Map2MinimapX[MaxMapWidth]; /// fast conversion table
78 static int Map2MinimapY[MaxMapHeight]; /// fast conversion table
79
80 // MinimapScale:
81 // 32x32 64x64 96x96 128x128 256x256 512x512 ...
82 // *4 *2 *4/3 *1 *1/2 *1/4
83 static int MinimapScaleX; /// Minimap scale to fit into window rounded up
84 static int MinimapScaleY; /// Minimap scale to fit into window rounded up
85 static int lowMinimapScaleX; /// Minimap scale to fit into window rounded down
86 static int lowMinimapScaleY; /// Minimap scale to fit into window rounded down
87
88 #define MAX_MINIMAP_EVENTS 8
89
90 struct MinimapEvent {
91 int X;
92 int Y;
93 int Size;
94 } MinimapEvents[MAX_MINIMAP_EVENTS];
95 int NumMinimapEvents;
96
97
98 /*----------------------------------------------------------------------------
99 -- Functions
100 ----------------------------------------------------------------------------*/
101
102 /**
103 ** Create the minimap texture
104 */
105 static void CreateMinimapTexture(void)
106 {
107 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
108 glGenTextures(1, &MinimapTexture);
109 glBindTexture(GL_TEXTURE_2D, MinimapTexture);
110 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
111 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
112 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
113 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
114 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, MinimapTextureWidth,
115 MinimapTextureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE,
116 MinimapSurfaceGL);
117 }
118
119 /**
120 ** Create a minimap from the tiles of the map.
121 */
122 void CMinimap::Create(void)
123 {
124 int n;
125 int maxSize = std::max(Map.Info.MapWidth, Map.Info.MapHeight);
126
127 MinimapScaleX = (W * MINIMAP_FAC + maxSize - 1) / maxSize;
128 MinimapScaleY = (H * MINIMAP_FAC + maxSize - 1) / maxSize;
129 lowMinimapScaleX = (W * MINIMAP_FAC) / maxSize;
130 lowMinimapScaleY = (H * MINIMAP_FAC) / maxSize;
131
132 XOffset = (W - (Map.Info.MapWidth * lowMinimapScaleX) / MINIMAP_FAC + 1) / 2;
133 YOffset = (H - (Map.Info.MapHeight * lowMinimapScaleY) / MINIMAP_FAC + 1) / 2;
134
135 //
136 // Calculate minimap fast lookup tables.
137 //
138 Minimap2MapX = new int[W];
139 memset(Minimap2MapX, 0, W * sizeof(int));
140 Minimap2MapY = new int[H];
141 memset(Minimap2MapY, 0, H * sizeof(int));
142 for (n = XOffset; n < W - XOffset; ++n) {
143 Minimap2MapX[n] = ((n - XOffset) * MINIMAP_FAC) / MinimapScaleX;
144 }
145 for (n = YOffset; n < H - YOffset; ++n) {
146 Minimap2MapY[n] = (((n - YOffset) * MINIMAP_FAC) / MinimapScaleY);
147 }
148 for (n = 0; n < Map.Info.MapWidth; ++n) {
149 Map2MinimapX[n] = (n * lowMinimapScaleX) / MINIMAP_FAC;
150 }
151 for (n = 0; n < Map.Info.MapHeight; ++n) {
152 Map2MinimapY[n] = (n * lowMinimapScaleY) / MINIMAP_FAC;
153 }
154
155 if (!UseOpenGL) {
156 SDL_PixelFormat *f = TheScreen->format;
157 MinimapTerrainSurface = SDL_CreateRGBSurface(SDL_SWSURFACE,
158 W, H, f->BitsPerPixel, f->Rmask, f->Gmask, f->Bmask, f->Amask);
159 MinimapSurface = SDL_CreateRGBSurface(SDL_SWSURFACE,
160 W, H, f->BitsPerPixel, f->Rmask, f->Gmask, f->Bmask, f->Amask);
161 } else {
162 for (MinimapTextureWidth = 1; MinimapTextureWidth < W; MinimapTextureWidth <<= 1) {
163 }
164 for (MinimapTextureHeight = 1; MinimapTextureHeight < H; MinimapTextureHeight <<= 1) {
165 }
166 MinimapTerrainSurfaceGL = new unsigned char[MinimapTextureWidth * MinimapTextureHeight * 4];
167 MinimapSurfaceGL = new unsigned char[MinimapTextureWidth * MinimapTextureHeight * 4];
168 memset(MinimapSurfaceGL, 0, MinimapTextureWidth * MinimapTextureHeight * 4);
169 memset(MinimapTerrainSurfaceGL, 0, MinimapTextureWidth * MinimapTextureHeight * 4);
170 CreateMinimapTexture();
171 }
172
173 UpdateTerrain();
174
175 NumMinimapEvents = 0;
176 }
177
178 /**
179 ** Free OpenGL minimap
180 */
181 void CMinimap::FreeOpenGL()
182 {
183 glDeleteTextures(1, &MinimapTexture);
184 }
185
186 /**
187 ** Reload OpenGL minimap
188 */
189 void CMinimap::Reload()
190 {
191 CreateMinimapTexture();
192 }
193
194 /**
195 ** Get the color from the patch to be used in the minimap
196 */
197 CColor GetColor(CPatch *patch, int xoffset, int yoffset, int mx, int my, int scalex, int scaley)
198 {
199 //
200 // Pixel 7,6 7,14, 15,6 15,14 are taken for the minimap picture.
201 //
202 int x = (xoffset * TileSizeX + 7 + ((mx * SCALE_PRECISION) % scalex) / SCALE_PRECISION * 8);
203 int y = (yoffset * TileSizeY + 6 + ((my * SCALE_PRECISION) % scaley) / SCALE_PRECISION * 8);
204
205 Uint8 r = 0, g = 0, b = 0, a = 0;
206 SDL_Surface *s = patch->getType()->getGraphic()->Surface;
207 SDL_PixelFormat *f = s->format;
208
209 //
210 // Convert the pixel to r,g,b,a
211 //
212 Uint8 *pixel = &((Uint8 *)s->pixels)[x * f->BytesPerPixel + y * s->pitch];
213 switch (f->BytesPerPixel) {
214 case 1:
215 SDL_GetRGBA(*pixel, f, &r, &g, &b, &a);
216 break;
217 case 2:
218 SDL_GetRGBA(*(Uint16 *)pixel, f, &r, &g, &b, &a);
219 break;
220 case 3:
221 r = pixel[f->Rshift >> 3];
222 g = pixel[f->Gshift >> 3];
223 b = pixel[f->Bshift >> 3];
224 a = 0xFF;
225 break;
226 case 4:
227 SDL_GetRGBA(*(Uint32 *)pixel, f, &r, &g, &b, &a);
228 break;
229 }
230
231 return CColor(r, g, b, a);
232 }
233
234 /**
235 ** Set MinimapTerrain pixel
236 */
237 void SetMinimapTerrainPixel(int x, int y, CColor color)
238 {
239 if (!UseOpenGL) {
240 int bpp = MinimapTerrainSurface->format->BytesPerPixel;
241 Uint8 *p = &((Uint8 *)MinimapTerrainSurface->pixels)[x * bpp + y * MinimapTerrainSurface->pitch];
242 Uint32 c = Video.MapRGBA(MinimapTerrainSurface->format, color.R, color.G, color.B, color.A);
243 if (bpp == 2) {
244 *(Uint16 *)p = c;
245 } else {
246 *(Uint32 *)p = c;
247 }
248 } else {
249 *(Uint32 *)&(MinimapTerrainSurfaceGL[(x + y * MinimapTextureWidth) * 4]) = Video.MapRGBA(0, color.R, color.G, color.B, color.A);
250 }
251 }
252
253 /**
254 ** Update a mini-map from the tiles of the map.
255 */
256 void CMinimap::UpdateTerrain()
257 {
258 int scalex = MinimapScaleX * SCALE_PRECISION / MINIMAP_FAC;
259 if (scalex == 0) {
260 scalex = 1;
261 }
262 int scaley = MinimapScaleY * SCALE_PRECISION / MINIMAP_FAC;
263 if (scaley == 0) {
264 scaley = 1;
265 }
266
267 if (!UseOpenGL) {
268 SDL_LockSurface(MinimapTerrainSurface);
269 }
270
271 for (int my = YOffset; my < H - YOffset; ++my) {
272 for (int mx = XOffset; mx < W - XOffset; ++mx) {
273 CPatch *patch;
274 int xoffset, yoffset;
275 CColor color = ColorBlack;
276
277 patch = Map.PatchManager.getPatch(Minimap2MapX[mx], Minimap2MapY[my], &xoffset, &yoffset);
278 if (patch) {
279 color = GetColor(patch, xoffset, yoffset, mx, my, scalex, scaley);
280 }
281 SetMinimapTerrainPixel(mx, my, color);
282 }
283 }
284
285 if (!UseOpenGL) {
286 SDL_UnlockSurface(MinimapTerrainSurface);
287 }
288 }
289
290 /**
291 ** Clear the minimap
292 */
293 static void ClearMinimap()
294 {
295 if (!UseOpenGL) {
296 SDL_FillRect(MinimapSurface, NULL, SDL_MapRGB(MinimapSurface->format, 0, 0, 0));
297 } else {
298 memset(MinimapSurfaceGL, 0, MinimapTextureWidth * MinimapTextureHeight * 4);
299 }
300 }
301
302 /**
303 ** Copy the minimap terrain to the minimap
304 */
305 static void CopyMinimapTerrain()
306 {
307 if (!UseOpenGL) {
308 SDL_BlitSurface(MinimapTerrainSurface, NULL, MinimapSurface, NULL);
309 } else {
310 memcpy(MinimapSurfaceGL, MinimapTerrainSurfaceGL, MinimapTextureWidth * MinimapTextureHeight * 4);
311 }
312 }
313
314 /**
315 ** Set a pixel in the minimap
316 */
317 static void SetMinimapPixel(int x, int y, Uint32 color)
318 {
319 if (!UseOpenGL) {
320 int bpp = MinimapSurface->format->BytesPerPixel;
321 Uint8 *p = &((Uint8 *)MinimapSurface->pixels)[x * bpp + y * MinimapSurface->pitch];
322 if (bpp == 2) {
323 *(Uint16 *)p = color;
324 } else {
325 *(Uint32 *)p = color;
326 }
327 } else {
328 *(Uint32 *)&(MinimapSurfaceGL[(x + y * MinimapTextureWidth) * 4]) = color;
329 }
330 }
331
332 /**
333 ** Draw a unit on the minimap.
334 */
335 static void DrawUnitOn(CUnit *unit, bool red_phase)
336 {
337 const CUnitType *type;
338 int mx, my;
339 int w, h;
340 int origh;
341 Uint32 color;
342
343 if (Editor.Running || ReplayRevealMap || unit->IsVisible(ThisPlayer)) {
344 type = unit->Type;
345 } else {
346 type = unit->Seen.Type;
347 // This will happen for radar if the unit has not been seen and we
348 // have it on radar.
349 if (!type) {
350 type = unit->Type;
351 }
352 }
353
354 //
355 // Figure out what color to use
356 //
357 if (unit->Player->Index == PlayerNumNeutral) {
358 color = Video.MapRGB(TheScreen->format,
359 type->NeutralMinimapColorRGB.r,
360 type->NeutralMinimapColorRGB.g,
361 type->NeutralMinimapColorRGB.b);
362 } else if (unit->Player == ThisPlayer && !Editor.Running) {
363 if (unit->Attacked && unit->Attacked + ATTACK_BLINK_DURATION > GameCycle &&
364 (red_phase || unit->Attacked + ATTACK_RED_DURATION > GameCycle)) {
365 color = ColorRed;
366 } else if (UI.Minimap.ShowSelected && unit->Selected) {
367 color = ColorWhite;
368 } else {
369 color = ColorGreen;
370 }
371 } else {
372 color = unit->Player->Color;
373 }
374
375 //
376 // Find which pixels to draw
377 //
378 mx = 1 + UI.Minimap.XOffset + Map2MinimapX[unit->X];
379 my = 1 + UI.Minimap.YOffset + Map2MinimapY[unit->Y];
380
381 w = Map2MinimapX[type->TileWidth];
382 if (mx + w >= UI.Minimap.W) { // clip right side
383 w = UI.Minimap.W - mx;
384 }
385
386 h = Map2MinimapY[type->TileHeight];
387 if (my + h >= UI.Minimap.H) { // clip bottom side
388 h = UI.Minimap.H - my;
389 }
390
391 //
392 // Draw the color on the minimap
393 //
394 origh = h;
395 while (w-- >= 0) {
396 while (h-- >= 0) {
397 SetMinimapPixel(mx + w, my + h, color);
398 }
399 h = origh;
400 }
401 }
402
403 /**
404 ** Update the minimap with the current game information
405 */
406 void CMinimap::Update()
407 {
408 static bool red_phase = false;
409 int mx, my;
410 int visiontype;
411
412 // red phase occurs every other second
413 if (red_phase != (bool)((FrameCounter / FRAMES_PER_SECOND) & 1)) {
414 red_phase = !red_phase;
415 }
416
417 // Clear Minimap background if not transparent
418 if (!Transparent) {
419 ClearMinimap();
420 }
421
422 // Draw the terrain
423 if (WithTerrain) {
424 CopyMinimapTerrain();
425 }
426
427 if (!UseOpenGL) {
428 SDL_LockSurface(MinimapSurface);
429 SDL_LockSurface(MinimapTerrainSurface);
430 }
431
432 // Hide unexplored tiles and use dithering for fog
433 for (my = 0; my < H; ++my) {
434 for (mx = 0; mx < W; ++mx) {
435 if (!ReplayRevealMap) {
436 visiontype = Map.IsTileVisible(ThisPlayer, Minimap2MapX[mx], Minimap2MapY[my]);
437
438 // visiontype: 0 unexplored, 1 explored, >1 visible.
439 if (visiontype == 0) {
440 SetMinimapPixel(mx, my, ColorBlack);
441 } else if (visiontype == 1 && ((mx & 1) != (my & 1))) {
442 // TODO: we could do real blending here instead of dithering
443 SetMinimapPixel(mx, my, ColorBlack);
444 }
445 }
446 }
447 }
448
449 if (!UseOpenGL) {
450 SDL_UnlockSurface(MinimapTerrainSurface);
451 }
452
453 //
454 // Draw units on map
455 //
456 for (int n = 0; n < NumUnits; ++n) {
457 if (Units[n]->IsVisibleOnMinimap()) {
458 DrawUnitOn(Units[n], red_phase);
459 }
460 }
461
462 if (!UseOpenGL) {
463 SDL_UnlockSurface(MinimapSurface);
464 }
465 }
466
467 /**
468 ** Draw the minimap events
469 */
470 static void DrawEvents()
471 {
472 for (int i = 0; i < NumMinimapEvents; ++i) {
473 Video.DrawTransCircleClip(ColorWhite,
474 MinimapEvents[i].X, MinimapEvents[i].Y,
475 MinimapEvents[i].Size, 192);
476
477 // Decrease the size of the circle
478 MinimapEvents[i].Size -= 1;
479
480 // Remove the event
481 if (MinimapEvents[i].Size < 2) {
482 MinimapEvents[i] = MinimapEvents[--NumMinimapEvents];
483 --i;
484 }
485 }
486 }
487
488 /**
489 ** Draw the minimap on the screen
490 */
491 void CMinimap::Draw(int vx, int vy)
492 {
493 if (!UseOpenGL) {
494 SDL_Rect drect = {
495 static_cast<Sint16>(X),
496 static_cast<Sint16>(Y),
497 0, // SDL_BlitSurface ignores the width and height.
498 0
499 };
500 SDL_BlitSurface(MinimapSurface, NULL, TheScreen, &drect);
501 } else {
502 glBindTexture(GL_TEXTURE_2D, MinimapTexture);
503 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, MinimapTextureWidth, MinimapTextureHeight,
504 GL_RGBA, GL_UNSIGNED_BYTE, MinimapSurfaceGL);
505
506 glBegin(GL_QUADS);
507 glTexCoord2f(0.0f, 0.0f);
508 glVertex2i(X, Y);
509 glTexCoord2f(0.0f, (float)H / MinimapTextureHeight);
510 glVertex2i(X, Y + H);
511 glTexCoord2f((float)W / MinimapTextureWidth, (float)H / MinimapTextureHeight);
512 glVertex2i(X + W, Y + H);
513 glTexCoord2f((float)W / MinimapTextureWidth, 0.0f);
514 glVertex2i(X + W, Y);
515 glEnd();
516 }
517
518 DrawEvents();
519 }
520
521
522 /**
523 ** Convert minimap cursor X position to tile map coordinate.
524 **
525 ** @param x Screen X pixel coordinate.
526 **
527 ** @return Tile X coordinate.
528 */
529 int CMinimap::Screen2MapX(int x)
530 {
531 int tx = (((x - X - XOffset) * MINIMAP_FAC) / MinimapScaleX);
532 tx = std::max(tx, 0);
533 tx = std::min(tx, Map.Info.MapWidth - 1);
534 return tx;
535 }
536
537 /**
538 ** Convert minimap cursor Y position to tile map coordinate.
539 **
540 ** @param y Screen Y pixel coordinate.
541 **
542 ** @return Tile Y coordinate.
543 */
544 int CMinimap::Screen2MapY(int y)
545 {
546 int ty = (((y - Y - YOffset) * MINIMAP_FAC) / MinimapScaleY);
547 ty = std::max(ty, 0);
548 ty = std::min(ty, Map.Info.MapHeight - 1);
549 return ty;
550 }
551
552 /**
553 ** Destroy mini-map.
554 */
555 void CMinimap::Destroy()
556 {
557 if (!UseOpenGL) {
558 SDL_FreeSurface(MinimapTerrainSurface);
559 MinimapTerrainSurface = NULL;
560 } else {
561 delete[] MinimapTerrainSurfaceGL;
562 MinimapTerrainSurfaceGL = NULL;
563 }
564 if (!UseOpenGL) {
565 if (MinimapSurface) {
566 SDL_FreeSurface(MinimapSurface);
567 MinimapSurface = NULL;
568 }
569 } else {
570 if (MinimapSurfaceGL) {
571 glDeleteTextures(1, &MinimapTexture);
572 delete[] MinimapSurfaceGL;
573 MinimapSurfaceGL = NULL;
574 }
575 }
576 delete[] Minimap2MapX;
577 Minimap2MapX = NULL;
578 delete[] Minimap2MapY;
579 Minimap2MapY = NULL;
580 }
581
582 /**
583 ** Draw minimap cursor.
584 **
585 ** @param vx View point X position.
586 ** @param vy View point Y position.
587 */
588 void CMinimap::DrawCursor(int vx, int vy)
589 {
590 // Determine and save region below minimap cursor
591 int x = X + XOffset + (vx * lowMinimapScaleX) / MINIMAP_FAC;
592 int y = Y + YOffset + (vy * lowMinimapScaleY) / MINIMAP_FAC;
593 int w = (UI.SelectedViewport->MapWidth * lowMinimapScaleX) / MINIMAP_FAC;
594 int h = (UI.SelectedViewport->MapHeight * lowMinimapScaleY) / MINIMAP_FAC;
595
596 // Draw cursor as rectangle (Note: unclipped, as it is always visible)
597 Video.DrawTransRectangle(UI.ViewportCursorColor, x, y, w, h, 128);
598 }
599
600 /**
601 ** Add a minimap event
602 **
603 ** @param x Map X tile position
604 ** @param y Map Y tile position
605 */
606 void CMinimap::AddEvent(int x, int y)
607 {
608 if (NumMinimapEvents == MAX_MINIMAP_EVENTS) {
609 return;
610 }
611
612 MinimapEvent *minimapEvent = &MinimapEvents[NumMinimapEvents++];
613
614 minimapEvent->X = X + XOffset + (x * lowMinimapScaleX) / MINIMAP_FAC;
615 minimapEvent->Y = Y + YOffset + (y * lowMinimapScaleY) / MINIMAP_FAC;
616 minimapEvent->Size = (W < H) ? W / 3 : H / 3;
617 }
618
619 //@}