Debian Codegraph @

src/cdogs/automap.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (C) 1995 Ronny Wester
5 Copyright (C) 2003 Jeremy Chin
6 Copyright (C) 2003 Lucas Martin-King
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 This file incorporates work covered by the following copyright and
23 permission notice:
24
25 Copyright (c) 2013-2014, 2016, 2018-2020, 2024 Cong Xu
26 All rights reserved.
27
28 Redistribution and use in source and binary forms, with or without
29 modification, are permitted provided that the following conditions are met:
30
31 Redistributions of source code must retain the above copyright notice, this
32 list of conditions and the following disclaimer.
33 Redistributions in binary form must reproduce the above copyright notice,
34 this list of conditions and the following disclaimer in the documentation
35 and/or other materials provided with the distribution.
36
37 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
38 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
41 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
47 POSSIBILITY OF SUCH DAMAGE.
48 */
49 #include "automap.h"
50
51 #include <stdio.h>
52 #include <string.h>
53
54 #include "actors.h"
55 #include "config.h"
56 #include "draw/draw.h"
57 #include "draw/draw_actor.h"
58 #include "draw/drawtools.h"
59 #include "font.h"
60 #include "gamedata.h"
61 #include "map.h"
62 #include "mission.h"
63 #include "objs.h"
64 #include "pic_manager.h"
65 #include "pickup.h"
66
67 #define MAP_SCALE_DEFAULT 2
68 #define MASK_ALPHA 128;
69
70 color_t colorWall = {72, 152, 72, 255};
71 color_t colorFloor = {12, 92, 12, 255};
72 color_t colorRoom = {24, 112, 24, 255};
73 color_t colorExit = {255, 255, 255, 255};
74
75 static void DisplayPlayer(
76 SDL_Renderer *renderer, const TActor *player, struct vec2i pos,
77 const int scale)
78 {
79 const struct vec2i playerPos = Vec2ToTile(player->thing.Pos);
80 pos = svec2i_add(pos, svec2i_scale(playerPos, (float)scale));
81 if (scale >= 2)
82 {
83 DrawHead(renderer, ActorGetCharacter(player), DIRECTION_DOWN, pos);
84 }
85 else
86 {
87 DrawPoint(pos, colorWhite);
88 }
89 }
90
91 static void DisplayObjective(
92 Thing *t, int objectiveIndex, struct vec2i pos, int scale, int flags)
93 {
94 const struct vec2i objectivePos = Vec2ToTile(t->Pos);
95 const Objective *o =
96 CArrayGet(&gMission.missionData->Objectives, objectiveIndex);
97 color_t color = o->color;
98 pos = svec2i_add(pos, svec2i_scale(objectivePos, (float)scale));
99 if (flags & AUTOMAP_FLAGS_MASK)
100 {
101 color.a = MASK_ALPHA;
102 }
103 if (scale >= 2)
104 {
105 DrawCross(&gGraphicsDevice, pos, color);
106 }
107 else
108 {
109 DrawPoint(pos, color);
110 }
111 }
112
113 static void DisplayExits(
114 const Map *m, const struct vec2i pos, const int scale, const int flags)
115 {
116 CA_FOREACH(const Exit, e, m->exits)
117 if (e->Hidden)
118 continue;
119 const struct vec2i exitPos =
120 svec2i_add(svec2i_scale(e->R.Pos, (float)scale), pos);
121 const struct vec2i exitSize = svec2i_scale(e->R.Size, (float)scale);
122
123 color_t color = colorExit;
124 if (flags & AUTOMAP_FLAGS_MASK)
125 {
126 color.a = MASK_ALPHA;
127 }
128 DrawRectangle(&gGraphicsDevice, exitPos, exitSize, color, false);
129 CA_FOREACH_END()
130 }
131
132 static void DisplaySummary(void)
133 {
134 char sScore[20];
135 struct vec2i pos;
136 pos.y = gGraphicsDevice.cachedConfig.Res.y - 5 - FontH();
137
138 CA_FOREACH(const Objective, o, gMission.missionData->Objectives)
139 if (ObjectiveIsRequired(o) || o->done > 0)
140 {
141 color_t textColor = colorWhite;
142 pos.x = 5;
143 // Objective color dot
144 DrawRectangle(
145 &gGraphicsDevice, svec2i(pos.x, pos.y + 3), svec2i(2, 2), o->color,
146 false);
147
148 pos.x += 5;
149
150 sprintf(sScore, "(%d)", o->done);
151
152 if (!ObjectiveIsRequired(o))
153 {
154 textColor = colorPurple;
155 }
156 else if (ObjectiveIsComplete(o))
157 {
158 textColor = colorRed;
159 }
160 pos = FontStrMask(o->Description, pos, textColor);
161 pos.x += 5;
162 FontStrMask(sScore, pos, textColor);
163 pos.y -= (FontH() + 1);
164 }
165 CA_FOREACH_END()
166 }
167
168 static color_t DoorColor(const int x, const int y)
169 {
170 const int l = MapGetDoorKeycardFlag(&gMap, svec2i(x, y));
171 return KeyColor(l);
172 }
173
174 void DrawDot(Thing *t, color_t color, struct vec2i pos, int scale)
175 {
176 const struct vec2i dotPos = Vec2ToTile(t->Pos);
177 pos = svec2i_add(pos, svec2i_scale(dotPos, (float)scale));
178 DrawRectangle(&gGraphicsDevice, pos, svec2i(scale, scale), color, false);
179 }
180
181 static void DrawMap(
182 Map *map, struct vec2i center, struct vec2i centerOn, struct vec2i size,
183 int scale, int flags)
184 {
185 int x, y;
186 struct vec2i mapPos =
187 svec2i_add(center, svec2i_scale(centerOn, (float)-scale));
188 for (y = 0; y < gMap.Size.y; y++)
189 {
190 int i;
191 for (i = 0; i < scale; i++)
192 {
193 for (x = 0; x < gMap.Size.x; x++)
194 {
195 Tile *tile = MapGetTile(map, svec2i(x, y));
196 if (tile->Class->Pic != NULL &&
197 (tile->isVisited || (flags & AUTOMAP_FLAGS_SHOWALL)))
198 {
199 int j;
200 for (j = 0; j < scale; j++)
201 {
202 struct vec2i drawPos = svec2i(
203 mapPos.x + x * scale + j,
204 mapPos.y + y * scale + i);
205 color_t color = colorTransparent;
206 switch (tile->Class->Type)
207 {
208 case TILE_CLASS_WALL:
209 color = colorWall;
210 break;
211 case TILE_CLASS_DOOR:
212 color = DoorColor(x, y);
213 break;
214 case TILE_CLASS_FLOOR:
215 color =
216 tile->Class->IsRoom ? colorRoom : colorFloor;
217 break;
218 default:
219 CASSERT(false, "Unknown tile class type");
220 break;
221 }
222 if (!ColorEquals(color, colorTransparent))
223 {
224 if (flags & AUTOMAP_FLAGS_MASK)
225 {
226 color.a = MASK_ALPHA;
227 }
228 DrawPoint(drawPos, color);
229 }
230 }
231 }
232 }
233 }
234 }
235 if (flags & AUTOMAP_FLAGS_MASK)
236 {
237 const color_t color = {255, 255, 255, 128};
238 DrawRectangle(
239 &gGraphicsDevice,
240 svec2i_subtract(center, svec2i_scale_divide(size, 2)), size, color,
241 false);
242 }
243 }
244
245 static void DrawThing(
246 Thing *t, Tile *tile, struct vec2i pos, int scale, int flags);
247 static void DrawObjectivesAndKeys(
248 Map *map, struct vec2i pos, int scale, int flags)
249 {
250 for (int y = 0; y < map->Size.y; y++)
251 {
252 for (int x = 0; x < map->Size.x; x++)
253 {
254 Tile *tile = MapGetTile(map, svec2i(x, y));
255 CA_FOREACH(ThingId, tid, tile->things)
256 DrawThing(ThingIdGetThing(tid), tile, pos, scale, flags);
257 CA_FOREACH_END()
258 }
259 }
260 }
261 static void DrawThing(
262 Thing *t, Tile *tile, struct vec2i pos, int scale, int flags)
263 {
264 if ((t->flags & THING_OBJECTIVE) != 0)
265 {
266 const int obj = ObjectiveFromThing(t->flags);
267 const Objective *o = CArrayGet(&gMission.missionData->Objectives, obj);
268 if (!(o->Flags & OBJECTIVE_HIDDEN) || (flags & AUTOMAP_FLAGS_SHOWALL))
269 {
270 if ((o->Flags & OBJECTIVE_POSKNOWN) || tile->isVisited ||
271 (flags & AUTOMAP_FLAGS_SHOWALL))
272 {
273 DisplayObjective(t, obj, pos, scale, flags);
274 }
275 }
276 }
277 else if (t->kind == KIND_PICKUP && tile->isVisited)
278 {
279 const Pickup *p = CArrayGet(&gPickups, t->id);
280 const int keyFlags = PickupClassGetKeys(p->class);
281 if (keyFlags != 0)
282 {
283 const color_t dotColor = KeyColor(keyFlags);
284 DrawDot(t, dotColor, pos, scale);
285 }
286 }
287 }
288
289 void AutomapDraw(
290 GraphicsDevice *g, SDL_Renderer *renderer, const int flags,
291 const bool showExit)
292 {
293 if (renderer == NULL)
294 {
295 renderer = g->gameWindow.renderer;
296 }
297 struct vec2i mapCenter =
298 svec2i(g->cachedConfig.Res.x / 2, g->cachedConfig.Res.y / 2);
299 struct vec2i centerOn = svec2i(gMap.Size.x / 2, gMap.Size.y / 2);
300 // Set the map scale to fit on screen
301 int mapScale = MAP_SCALE_DEFAULT;
302 // TODO: allow fractional scales for really big maps / really small screens
303 if (gMap.Size.x * mapScale > g->cachedConfig.Res.x ||
304 gMap.Size.y * mapScale > g->cachedConfig.Res.y)
305 {
306 mapScale--;
307 }
308 struct vec2i pos =
309 svec2i_add(mapCenter, svec2i_scale(centerOn, -(float)mapScale));
310
311 // Draw faded green overlay
312 const color_t mask = {0, 128, 0, 128};
313 DrawRectangle(g, svec2i_zero(), g->cachedConfig.Res,
314 mask, true);
315
316 DrawMap(&gMap, mapCenter, centerOn, gMap.Size, mapScale, flags);
317 DrawObjectivesAndKeys(&gMap, pos, mapScale, flags);
318
319 CA_FOREACH(const PlayerData, p, gPlayerDatas)
320 if (!IsPlayerAlive(p))
321 {
322 continue;
323 }
324 DisplayPlayer(renderer, ActorGetByUID(p->ActorUID), pos, mapScale);
325 CA_FOREACH_END()
326
327 if (showExit)
328 {
329 DisplayExits(&gMap, pos, mapScale, flags);
330 }
331 DisplaySummary();
332 }
333
334 // Draw mini automap
335 void AutomapDrawRegion(
336 SDL_Renderer *renderer, Map *map, struct vec2i pos,
337 const struct vec2i size, const struct vec2i mapCenter, const int flags,
338 const bool showExit)
339 {
340 const int scale = 1;
341 const Rect2i oldClip = GraphicsGetClip(renderer);
342 GraphicsSetClip(renderer, Rect2iNew(pos, size));
343 pos = svec2i_add(pos, svec2i_scale_divide(size, 2));
344 DrawMap(map, pos, mapCenter, size, scale, flags);
345 const struct vec2i centerOn =
346 svec2i_add(pos, svec2i_scale(mapCenter, (float)-scale));
347 CA_FOREACH(const PlayerData, p, gPlayerDatas)
348 if (!IsPlayerAlive(p))
349 {
350 continue;
351 }
352 const TActor *player = ActorGetByUID(p->ActorUID);
353 DisplayPlayer(renderer, player, centerOn, scale);
354 CA_FOREACH_END()
355 DrawObjectivesAndKeys(&gMap, centerOn, scale, flags);
356 if (showExit)
357 {
358 DisplayExits(map, centerOn, scale, flags);
359 }
360 GraphicsSetClip(renderer, oldClip);
361 }