Debian Codegraph @

src/ammo_menu.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4
5 Copyright (c) 2023 Cong Xu
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11 Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
13 Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
28 */
29 #include "ammo_menu.h"
30
31 #include <assert.h>
32
33 #include <cdogs/draw/drawtools.h>
34 #include <cdogs/draw/nine_slice.h>
35 #include <cdogs/font.h>
36
37 #define MENU_WIDTH 80
38 #define EQUIP_MENU_SLOT_HEIGHT 40
39 #define MENU_MAX_ROWS 4
40 #define BUTTON_BG_W 20
41 #define SCROLL_H 12
42 #define SLOT_BORDER 3
43 #define LEVEL_W 2
44 #define ROW_H 10
45
46 static int GetSelectedAmmo(const AmmoMenu *menu)
47 {
48 if (menu->idx / 2 >= (int)menu->ammoIds.size)
49 {
50 return -1;
51 }
52 return *(int *)CArrayGet(&menu->ammoIds, menu->idx / 2);
53 }
54
55 static bool CanBuy(const AmmoMenu *menu, const int ammoId)
56 {
57 const PlayerData *pData = PlayerDataGetByUID(menu->PlayerUID);
58 const Ammo *ammo = AmmoGetById(&gAmmo, ammoId);
59 const int amount = PlayerGetAmmoAmount(pData, ammoId);
60 return ammo->Max > amount && ammo->Price <= pData->Totals.Score;
61 }
62 static bool CanSell(const AmmoMenu *menu, const int ammoId)
63 {
64 const PlayerData *pData = PlayerDataGetByUID(menu->PlayerUID);
65 const int amount = PlayerGetAmmoAmount(pData, ammoId);
66 return amount > 0;
67 }
68
69 int AmmoMenuSelectedCostDiff(const AmmoMenu *menu)
70 {
71 const PlayerData *pData = PlayerDataGetByUID(menu->PlayerUID);
72 const int ammoId = GetSelectedAmmo(menu);
73 if (ammoId >= 0)
74 {
75 const Ammo *ammo = AmmoGetById(&gAmmo, ammoId);
76 const int amount = PlayerGetAmmoAmount(pData, ammoId);
77 const bool buy = (menu->idx & 1) == 0;
78 if (buy && ammo->Max > amount)
79 {
80 return ammo->Price;
81 }
82 else if (amount > 0)
83 {
84 return -ammo->Price;
85 }
86 }
87 return 0;
88 }
89
90 static void AmmoSelect(menu_t *menu, int cmd, void *data)
91 {
92 UNUSED(menu);
93 AmmoMenu *d = data;
94
95 d->SelectResult = AMMO_MENU_NONE;
96 if (Button1(cmd))
97 {
98 const int ammoId = GetSelectedAmmo(d);
99 if (ammoId < 0)
100 {
101 d->SelectResult = AMMO_MENU_CANCEL;
102 MenuPlaySound(MENU_SOUND_BACK);
103 }
104 else
105 {
106 const Ammo *ammo = AmmoGetById(&gAmmo, ammoId);
107 const bool buy = (d->idx & 1) == 0;
108 if (buy && CanBuy(d, ammoId))
109 {
110 SoundPlay(&gSoundDevice, StrSound(ammo->Sound));
111 d->SelectResult = AMMO_MENU_SELECT;
112 }
113 else if (!buy && CanSell(d, ammoId))
114 {
115 SoundPlay(&gSoundDevice, StrSound(ammo->Sound));
116 d->SelectResult = AMMO_MENU_SELECT;
117 }
118 else
119 {
120 SoundPlay(&gSoundDevice, StrSound("ammo_none"));
121 }
122 }
123 }
124 else if (Button2(cmd))
125 {
126 d->SelectResult = AMMO_MENU_CANCEL;
127 MenuPlaySound(MENU_SOUND_BACK);
128 }
129 }
130
131 static int ClampScroll(const AmmoMenu *menu)
132 {
133 // Update menu scroll based on selected ammo
134 const int selectedRow = menu->idx / 2;
135 const int lastRow = (int)menu->ammoIds.size / 2;
136 int minRow = MAX(0, selectedRow - MENU_MAX_ROWS + 1);
137 int maxRow = MIN(selectedRow, MAX(0, lastRow - MENU_MAX_ROWS));
138 // If the selected row is the last row on screen, and we can still
139 // scroll down (i.e. show the scroll down button), scroll down
140 if (selectedRow - menu->scroll == MENU_MAX_ROWS - 1 &&
141 selectedRow < lastRow)
142 {
143 minRow++;
144 }
145 else if (selectedRow == menu->scroll && selectedRow > 0)
146 {
147 maxRow--;
148 }
149 return CLAMP(menu->scroll, minRow, maxRow);
150 }
151
152 static menu_t *CreateMenu(AmmoMenu *data);
153 void AmmoMenuCreate(
154 AmmoMenu *menu, const int playerUID, const struct vec2i pos,
155 const struct vec2i size, EventHandlers *handlers, GraphicsDevice *graphics)
156 {
157 menu->PlayerUID = playerUID;
158 menu->buttonBG = PicManagerGetPic(&gPicManager, "hud/button_bg");
159 menu->idx = -1;
160 AmmoMenuReset(menu);
161
162 MenuSystemInit(&menu->ms, handlers, graphics, pos, size);
163 menu->ms.align = MENU_ALIGN_LEFT;
164 menu->ms.root = menu->ms.current = CreateMenu(menu);
165 MenuSystemAddCustomDisplay(
166 &menu->ms, MenuDisplayPlayerControls, &menu->PlayerUID);
167 }
168 static void DrawMenu(
169 const menu_t *menu, GraphicsDevice *g, const struct vec2i pos,
170 const struct vec2i size, const void *data);
171 static int HandleInputMenu(int cmd, void *data);
172 static menu_t *CreateMenu(AmmoMenu *data)
173 {
174 menu_t *menu = MenuCreateCustom("", DrawMenu, HandleInputMenu, data);
175
176 MenuSetPostInputFunc(menu, AmmoSelect, data);
177
178 return menu;
179 }
180
181 static void DrawAmmoMenuItem(
182 const AmmoMenu *data, GraphicsDevice *g, const int idx, const Ammo *a,
183 const struct vec2i pos, const struct vec2i bgSize);
184 static void DrawMenu(
185 const menu_t *menu, GraphicsDevice *g, const struct vec2i pos,
186 const struct vec2i size, const void *data)
187 {
188 UNUSED(menu);
189 const AmmoMenu *d = data;
190 const int ammoHeight = EQUIP_MENU_SLOT_HEIGHT * 2 + FontH();
191 const int ammoY = CENTER_Y(pos, size, ammoHeight) - 12;
192 const color_t color = d->Active ? colorWhite : colorGray;
193 const struct vec2i bgSize =
194 svec2i(CLAMP(d->size.x * 3 / 4, 40 * 2, 40 * 4), FontH() * 3 + 4);
195 const struct vec2i scrollSize = svec2i(bgSize.x, SCROLL_H);
196 bool scrollDown = false;
197
198 CA_FOREACH(const int, ammoId, d->ammoIds)
199 const Ammo *ammo = AmmoGetById(&gAmmo, *ammoId);
200 if (_ca_index >= d->scroll && _ca_index < d->scroll + MENU_MAX_ROWS)
201 {
202 DrawAmmoMenuItem(
203 d, g, _ca_index, ammo, svec2i(pos.x, pos.y + ammoY), bgSize);
204 }
205 if (_ca_index == d->scroll + MENU_MAX_ROWS)
206 {
207 scrollDown = true;
208 break;
209 }
210 CA_FOREACH_END()
211
212 // Draw scroll buttons
213 const Pic *gradient = PicManagerGetPic(&gPicManager, "hud/gradient");
214 if (d->scroll > 0)
215 {
216 // TODO: test scrolling
217 const Pic *scrollPic = d->buttonBG;
218 const Rect2i scrollRect =
219 Rect2iNew(svec2i(pos.x + 3, pos.y + 1 + ammoY), scrollSize);
220 PicRender(
221 gradient, g->gameWindow.renderer,
222 svec2i(
223 scrollRect.Pos.x + scrollSize.x / 2,
224 pos.y + gradient->size.y / 2 + ammoY + scrollSize.y - 1),
225 colorBlack, 0, svec2((float)scrollSize.x, 1), SDL_FLIP_NONE,
226 Rect2iZero());
227 Draw9Slice(
228 g, scrollPic, scrollRect, 3, 3, 3, 3, true, color, SDL_FLIP_NONE);
229 FontOpts fopts = FontOptsNew();
230 fopts.Area = scrollRect.Size;
231 fopts.HAlign = ALIGN_CENTER;
232 fopts.VAlign = ALIGN_CENTER;
233 fopts.Mask = color;
234 FontStrOpt(ARROW_UP, scrollRect.Pos, fopts);
235 }
236 if (scrollDown)
237 {
238 const Pic *scrollPic = d->buttonBG;
239 const Rect2i scrollRect = Rect2iNew(
240 svec2i(
241 pos.x + 3,
242 pos.y - 1 + ammoY + bgSize.y * MENU_MAX_ROWS - SCROLL_H),
243 scrollSize);
244 PicRender(
245 gradient, g->gameWindow.renderer,
246 svec2i(
247 scrollRect.Pos.x + scrollSize.x / 2,
248 pos.y - gradient->size.y / 2 + ammoY +
249 bgSize.y * MENU_MAX_ROWS - SCROLL_H -
250 gradient->size.y / 2 + 1),
251 colorBlack, 0, svec2((float)scrollSize.x, 1), SDL_FLIP_VERTICAL,
252 Rect2iZero());
253 Draw9Slice(
254 g, scrollPic, scrollRect, 3, 3, 3, 3, true, color, SDL_FLIP_NONE);
255 FontOpts fopts = FontOptsNew();
256 fopts.Area = scrollRect.Size;
257 fopts.HAlign = ALIGN_CENTER;
258 fopts.VAlign = ALIGN_CENTER;
259 fopts.Mask = color;
260 FontStrOpt(ARROW_DOWN, scrollRect.Pos, fopts);
261 }
262 else
263 {
264 // Draw back item
265 DrawAmmoMenuItem(
266 d, g, (int)d->ammoIds.size, NULL, svec2i(pos.x, pos.y + ammoY),
267 bgSize);
268 }
269 }
270 static void DrawAmmoMenuItem(
271 const AmmoMenu *data, GraphicsDevice *g, const int idx, const Ammo *a,
272 const struct vec2i pos, const struct vec2i bgSize)
273 {
274 const bool selected = data->idx / 2 == idx;
275 const PlayerData *pData = PlayerDataGetByUID(data->PlayerUID);
276 const int ammoId = idx < (int)data->ammoIds.size
277 ? *(int *)CArrayGet(&data->ammoIds, idx)
278 : -1;
279 const int ammoAmount = PlayerGetAmmoAmount(pData, ammoId);
280 const struct vec2i bgPos =
281 svec2i(pos.x, pos.y + (idx - data->scroll) * bgSize.y);
282 // Disallow buy/sell if ammo is free
283 const bool enabled = a == NULL || (data->Active && a->Price > 0 &&
284 a->Price <= pData->Totals.Score);
285 color_t color = enabled ? colorWhite : colorGray;
286 if (selected && data->Active)
287 {
288 const color_t cbg = {0, 255, 255, 64};
289 DrawRectangle(g, bgPos, bgSize, cbg, true);
290 color = colorRed;
291 }
292
293 // Draw:
294 // <name> <price>
295 // <icon> <buy> <sell>
296 // <amount>/<max>
297 // With: amount/max coloured rectangle
298
299 int x = bgPos.x + 4;
300 int y = bgPos.y + ROW_H * 2;
301
302 // Ammo amount BG
303 if (a && a->Max > 0 && ammoAmount > 0)
304 {
305 const color_t gaugeBG =
306 AmmoIsLow(a, ammoAmount) ? colorRed : colorBlue;
307 DrawRectangle(
308 g, svec2i_add(svec2i(x, y), svec2i_one()),
309 svec2i(ammoAmount * (bgSize.x - 8) / a->Max, FontH()), gaugeBG,
310 true);
311 }
312
313 y = bgPos.y + ROW_H;
314
315 // Sell/buy buttons
316 if (a != NULL)
317 {
318 const bool sellSelected =
319 selected && data->Active && (data->idx & 1) == 1;
320 const bool canSell = CanSell(data, ammoId);
321 const FontOpts foptsSell = {
322 ALIGN_CENTER, ALIGN_START, svec2i(BUTTON_BG_W, FontH()),
323 svec2i(2, 2), sellSelected ? colorRed : colorGray};
324 x = bgPos.x + bgSize.x - BUTTON_BG_W - 2;
325 const struct vec2i sellPos = svec2i(x, y);
326 Draw9Slice(
327 g, data->buttonBG,
328 Rect2iNew(sellPos, svec2i(BUTTON_BG_W, FontH() + 4)), 3, 3, 3, 3,
329 true,
330 canSell ? (sellSelected ? colorRed : colorMaroon) : colorGray,
331 SDL_FLIP_NONE);
332 FontStrOpt("Sell", sellPos, foptsSell);
333
334 x -= BUTTON_BG_W + 3;
335 const bool buySelected =
336 selected && data->Active && (data->idx & 1) == 0;
337 const bool canBuy = CanBuy(data, ammoId);
338 const FontOpts foptsBuy = {
339 ALIGN_CENTER, ALIGN_START, svec2i(BUTTON_BG_W, FontH()),
340 svec2i(2, 2), buySelected ? colorGreen : colorGray};
341 const struct vec2i buyPos = svec2i(x, y);
342 Draw9Slice(
343 g, data->buttonBG,
344 Rect2iNew(buyPos, svec2i(BUTTON_BG_W, FontH() + 4)), 3, 3, 3, 3,
345 true,
346 canBuy ? (buySelected ? colorGreen : colorOfficeGreen) : colorGray,
347 SDL_FLIP_NONE);
348 FontStrOpt("Buy", buyPos, foptsBuy);
349 }
350
351 y = bgPos.y;
352 x = bgPos.x + 4;
353
354 const FontOpts fopts = {
355 ALIGN_START, ALIGN_START, bgSize, svec2i(2, 2), color};
356
357 // Name
358 FontStrOpt(a ? a->Name : "Back", svec2i(x, y), fopts);
359
360 // Price
361 if (a && a->Price > 0)
362 {
363 const FontOpts foptsP = {
364 ALIGN_END, ALIGN_START, bgSize, svec2i(8, 2),
365 enabled ? (selected ? colorRed : colorGray) : colorDarkGray};
366 char buf[256];
367 sprintf(buf, "$%d", a->Price);
368 FontStrOpt(buf, svec2i(x, y), foptsP);
369 }
370
371 y += ROW_H * 2;
372 x = bgPos.x + 4;
373
374 // Ammo amount BG
375 if (a && a->Max > 0 && ammoAmount > 0)
376 {
377 const color_t gaugeBG =
378 AmmoIsLow(a, ammoAmount) ? colorRed : colorBlue;
379 DrawRectangle(
380 g, svec2i_add(svec2i(x, y), svec2i_one()),
381 svec2i(ammoAmount * (bgSize.x - 8) / a->Max, FontH()), gaugeBG,
382 true);
383 }
384
385 // Amount
386 if (a != NULL)
387 {
388 char buf[256];
389 if (a->Max > 0)
390 {
391 sprintf(buf, "%d/%d", ammoAmount, a->Max);
392 }
393 else
394 {
395 sprintf(buf, "%d", ammoAmount);
396 }
397 const FontOpts foptsA = {
398 ALIGN_END, ALIGN_START, bgSize, svec2i(8, 2), color};
399 FontStrOpt(buf, svec2i(x, y), foptsA);
400 }
401
402 y -= ROW_H;
403
404 // Icon
405 if (a != NULL)
406 {
407 x = bgPos.x + 12;
408 CPicDrawContext c = CPicDrawContextNew();
409 const struct vec2i ammoPos = svec2i_subtract(
410 svec2i(x, bgPos.y + bgSize.y / 2),
411 svec2i_scale_divide(CPicGetPic(&a->Pic, 0)->size, 2));
412 CPicDraw(g, &a->Pic, ammoPos, &c);
413 }
414 }
415 static int HandleInputMenu(int cmd, void *data)
416 {
417 AmmoMenu *d = data;
418
419 const int numAmmo = (int)d->ammoIds.size;
420
421 if (Button1(cmd))
422 {
423 // Do nothing; don't switch away from menu
424 }
425 else if (Button2(cmd))
426 {
427 return 1;
428 }
429 else if (Left(cmd))
430 {
431 if ((d->idx % 2) == 1)
432 {
433 d->idx--;
434 MenuPlaySound(MENU_SOUND_SWITCH);
435 }
436 }
437 else if (Right(cmd))
438 {
439 if (d->idx < numAmmo * 2 && (d->idx % 2) == 0)
440 {
441 d->idx++;
442 MenuPlaySound(MENU_SOUND_SWITCH);
443 }
444 }
445 else if (Up(cmd))
446 {
447 if (d->idx >= 2)
448 {
449 d->idx -= 2;
450 MenuPlaySound(MENU_SOUND_SWITCH);
451 }
452 }
453 else if (Down(cmd))
454 {
455 if (d->idx + 2 < numAmmo * 2 + 2)
456 {
457 d->idx = MIN(numAmmo * 2 + 2, d->idx + 2);
458 MenuPlaySound(MENU_SOUND_SWITCH);
459 }
460 }
461
462 d->scroll = ClampScroll(d);
463
464 return 0;
465 }
466
467 void AmmoMenuTerminate(AmmoMenu *menu)
468 {
469 MenuSystemTerminate(&menu->ms);
470 CArrayTerminate(&menu->ammoIds);
471 }
472
473 void AmmoMenuReset(AmmoMenu *menu)
474 {
475 const PlayerData *pData = PlayerDataGetByUID(menu->PlayerUID);
476 // Get the ammo indices available for this slot
477 CArrayTerminate(&menu->ammoIds);
478 CArrayInit(&menu->ammoIds, sizeof(int));
479 for (int i = 0; i < AmmoGetNumClasses(&gAmmo); i++)
480 {
481 if (!PlayerUsesAmmo(pData, i))
482 {
483 continue;
484 }
485 CArrayPushBack(&menu->ammoIds, &i);
486 }
487 menu->idx = CLAMP(menu->idx, 0, (int)menu->ammoIds.size * 2);
488 }
489
490 void AmmoMenuActivate(AmmoMenu *menu)
491 {
492 menu->Active = true;
493 menu->SelectResult = AMMO_MENU_NONE;
494 }
495
496 bool AmmoMenuUpdate(AmmoMenu *menu, const int cmd)
497 {
498 PlayerData *p = PlayerDataGetByUID(menu->PlayerUID);
499 MenuProcessCmd(&menu->ms, cmd);
500 switch (menu->SelectResult)
501 {
502 case AMMO_MENU_NONE:
503 break;
504 case AMMO_MENU_SELECT: {
505 const int ammoId = GetSelectedAmmo(menu);
506 const bool buy = (menu->idx & 1) == 0;
507 const int amount = AmmoGetById(&gAmmo, ammoId)->Amount;
508 PlayerAddAmmo(p, ammoId, buy ? amount : -amount, false);
509 return true;
510 }
511 break;
512 case AMMO_MENU_CANCEL:
513 menu->ms.current = menu->ms.root;
514 // Switch back to equip menu
515 menu->Active = false;
516 break;
517 default:
518 CASSERT(false, "unhandled case");
519 break;
520 }
521 return false;
522 }
523
524 void AmmoMenuDraw(const AmmoMenu *menu)
525 {
526 MenuDisplay(&menu->ms);
527 }