Debian Codegraph @

src/equip_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) 2013-2015, 2018, 2020-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 "equip_menu.h"
30
31 #include <assert.h>
32
33 #include <cdogs/ai_coop.h>
34 #include <cdogs/draw/draw_actor.h>
35 #include <cdogs/draw/drawtools.h>
36 #include <cdogs/draw/nine_slice.h>
37 #include <cdogs/font.h>
38
39 #include "material.h"
40
41 #define NO_GUN_LABEL "(None)"
42 #define AMMO_LABEL "Ammo..."
43 #define UTIL_LABEL "Utilities..."
44 #define END_MENU_LABEL "(End)"
45 #define EQUIP_MENU_WIDTH 80
46 #define EQUIP_MENU_SLOT_HEIGHT 40
47 #define UTIL_MENU_SLOT_HEIGHT 12
48 #define EQUIP_MENU_MAX_ROWS 4
49 #define SLOT_BORDER 3
50 #define AMMO_LEVEL_W 2
51
52 static bool InSlot(const WeaponClass *wc, const int slot)
53 {
54 int slotStart, slotEnd;
55 GunTypeGetSlotStartEnd(wc->Type, &slotStart, &slotEnd);
56 return slot >= slotStart && slot <= slotEnd;
57 }
58
59 static int CountNumGuns(const EquipMenu *data, const int slot)
60 {
61 if (slot >= MAX_WEAPONS)
62 {
63 return 0;
64 }
65 // Count total guns
66 int numGuns = 0;
67 CA_FOREACH(const WeaponClass *, wc, data->weapons)
68 if (!InSlot(*wc, slot))
69 {
70 continue;
71 }
72 numGuns++;
73 CA_FOREACH_END()
74 return numGuns;
75 }
76
77 static bool IsSlotDisabled(const EquipMenu *data, const int slot)
78 {
79 if (slot < 0 || slot > data->endSlot)
80 {
81 return true;
82 }
83 if (slot < MAX_WEAPONS)
84 {
85 return CountNumGuns(data, slot) == 0;
86 }
87 const PlayerData *pData = PlayerDataGetByUID(data->PlayerUID);
88 // Disable end option if nothing equipped
89 if (slot == data->endSlot)
90 {
91 return PlayerGetNumWeapons(pData) == 0;
92 }
93 if (slot == data->ammoSlot)
94 {
95 return !PlayerUsesAnyAmmo(pData);
96 }
97 if (slot == data->utilSlot)
98 {
99 return false;
100 }
101 return false;
102 }
103
104 static void DrawEquipSlot(
105 const EquipMenu *data, GraphicsDevice *g, const int slot,
106 const char *label, const struct vec2i pos, const FontAlign align)
107 {
108 const bool selected = data->slot == slot;
109 color_t color = data->equipping ? colorDarkGray : colorWhite;
110 color_t mask = color;
111 // Allow space for price if buy/sell enabled
112 const int h =
113 EQUIP_MENU_SLOT_HEIGHT + (gCampaign.Setting.BuyAndSell ? FontH() : 0);
114 if (selected)
115 {
116 if (data->equipping)
117 {
118 color = colorYellow;
119 mask = colorWhite;
120 }
121 else
122 {
123 // Add 1px padding
124 const struct vec2i bgPos = svec2i_subtract(pos, svec2i_one());
125 const struct vec2i bgSize =
126 svec2i(EQUIP_MENU_WIDTH / 2 + 2, h + 2);
127 DrawRectangle(g, bgPos, bgSize, colorSelectedBG, true);
128
129 color = colorRed;
130 }
131 }
132 if (IsSlotDisabled(data, slot))
133 {
134 color = mask = colorDarkGray;
135 }
136 const PlayerData *pData = PlayerDataGetByUID(data->PlayerUID);
137
138 int y = pos.y;
139
140 const int bgSpriteIndex = (slot & 1) + (int)selected * 2;
141 const Pic *slotBG = CArrayGet(&data->slotBGSprites->pics, bgSpriteIndex);
142 const struct vec2i bgPos = svec2i(pos.x, y);
143 Draw9Slice(
144 g, slotBG,
145 Rect2iNew(
146 svec2i(bgPos.x + 1, bgPos.y + 1),
147 svec2i(EQUIP_MENU_WIDTH / 2, h - 2)),
148 11, (slot & 1) ? 4 : 13, 12, (slot & 1) ? 13 : 4, true, mask,
149 SDL_FLIP_NONE);
150
151 const FontOpts fopts = {
152 align, ALIGN_START, svec2i(EQUIP_MENU_WIDTH / 2, FontH()),
153 svec2i(3, 1), color};
154 FontStrOpt(label, pos, fopts);
155
156 const Pic *gunIcon = pData->guns[slot]
157 ? pData->guns[slot]->Icon
158 : PicManagerGetPic(&gPicManager, "peashooter");
159 // Draw icon at center of slot
160 const struct vec2i slotSize = svec2i(EQUIP_MENU_WIDTH / 2, h);
161 const struct vec2i gunPos = svec2i_subtract(
162 svec2i_add(bgPos, svec2i_scale_divide(slotSize, 2)),
163 svec2i_scale_divide(gunIcon->size, 2));
164 PicRender(
165 gunIcon, g->gameWindow.renderer, gunPos,
166 pData->guns[slot] ? mask : colorBlack, 0, svec2_one(), SDL_FLIP_NONE,
167 Rect2iZero());
168
169 DrawWeaponAmmo(
170 g, pData, pData->guns[slot], mask, svec2i(bgPos.x, bgPos.y + FontH()),
171 svec2i(slotSize.x, slotSize.y - 2 * FontH() - 1));
172
173 const Pic *arrow = PicManagerGetPic(&gPicManager, "hud/arrow");
174 const struct vec2i metaPos =
175 svec2i(pos.x + EQUIP_MENU_WIDTH / 2 - 6, y + 13);
176 const struct vec2i arrowPos = svec2i(metaPos.x - 4, metaPos.y);
177 switch (data->SlotMeta[slot])
178 {
179 case META_NONE:
180 break;
181 case META_NEW:
182 DrawCross(g, metaPos, colorGreen);
183 break;
184 case META_UPGRADE:
185 PicRender(
186 arrow, g->gameWindow.renderer, arrowPos, colorGreen, 0,
187 svec2_one(), SDL_FLIP_NONE, Rect2iZero());
188 break;
189 case META_DOWNGRADE:
190 PicRender(
191 arrow, g->gameWindow.renderer, arrowPos, colorRed, 0, svec2_one(),
192 SDL_FLIP_VERTICAL, Rect2iZero());
193 break;
194 default:
195 CASSERT(false, "unexpected");
196 break;
197 }
198
199 // Draw price
200 if (gCampaign.Setting.BuyAndSell && data->equipping && pData->guns[slot] &&
201 pData->guns[slot]->Price != 0)
202 {
203 const FontOpts foptsP = {
204 ALIGN_CENTER, ALIGN_START, svec2i(EQUIP_MENU_WIDTH / 2, FontH()),
205 svec2i(2, 2), selected ? colorGray : colorDarkGray};
206 char buf[256];
207 sprintf(buf, "$%d", pData->guns[slot]->Price);
208 FontStrOpt(buf, svec2i_add(pos, svec2i(0, FontH() + 2)), foptsP);
209 }
210
211 y += h - FontH() - 1;
212 const char *gunName =
213 pData->guns[slot] ? pData->guns[slot]->name : NO_GUN_LABEL;
214 const FontOpts fopts2 = {
215 align, ALIGN_START, svec2i(EQUIP_MENU_WIDTH / 2, FontH()),
216 svec2i(3, 0), color};
217 FontStrOpt(gunName, svec2i(pos.x, y), fopts2);
218 }
219 static void DrawEquipMenu(
220 const menu_t *menu, GraphicsDevice *g, const struct vec2i pos,
221 const struct vec2i size, const void *data)
222 {
223 UNUSED(menu);
224 const EquipMenu *d = data;
225 const PlayerData *pData = PlayerDataGetByUID(d->PlayerUID);
226 // Allow space for price if buy/sell enabled
227 const int h =
228 EQUIP_MENU_SLOT_HEIGHT + (gCampaign.Setting.BuyAndSell ? FontH() : 0);
229 int y = pos.y;
230
231 // Draw player cash
232 if (gCampaign.Setting.BuyAndSell)
233 {
234 const struct vec2i fpos = AnimatedCounterDraw(
235 &d->Cash, svec2i_add(pos, svec2i(0, -FontH() - 2)));
236 // Draw cost difference to buy selected item
237 if (d->equipping)
238 {
239 int costDiff = 0;
240 if (d->slot < MAX_WEAPONS)
241 {
242 const WeaponMenu *weaponMenu = &d->weaponMenus[d->slot];
243 costDiff = WeaponMenuSelectedCostDiff(weaponMenu);
244 }
245 else if (d->slot == d->ammoSlot)
246 {
247 costDiff = AmmoMenuSelectedCostDiff(&d->ammoMenu);
248 }
249 else if (d->slot == d->utilSlot)
250 {
251 costDiff = UtilMenuSelectedCostDiff(&d->utilMenu);
252 }
253 if (costDiff != 0)
254 {
255 // Draw price diff
256 const FontOpts foptsD = {
257 ALIGN_START, ALIGN_START, svec2i_zero(), svec2i_zero(),
258 costDiff > 0 ? colorRed : colorGreen};
259 char buf[256];
260 sprintf(buf, "%s%d", costDiff < 0 ? "+" : "", -costDiff);
261 FontStrOpt(buf, fpos, foptsD);
262 }
263 }
264 }
265
266 DrawEquipSlot(d, g, 0, "I", svec2i(pos.x, y), ALIGN_START);
267 DrawEquipSlot(
268 d, g, 1, "II", svec2i(pos.x + EQUIP_MENU_WIDTH / 2, y), ALIGN_END);
269 y += h;
270 DrawEquipSlot(d, g, MELEE_SLOT, "Melee", svec2i(pos.x, y), ALIGN_START);
271 DrawEquipSlot(
272 d, g, 3, "Bombs", svec2i(pos.x + EQUIP_MENU_WIDTH / 2, y), ALIGN_END);
273 y += h + 8;
274 if (d->ammoSlot >= 0)
275 {
276 const bool selected = d->slot == d->ammoSlot;
277 const bool disabled = !PlayerUsesAnyAmmo(pData);
278 DisplayMenuItem(
279 g,
280 Rect2iNew(
281 svec2i(CENTER_X(pos, size, FontStrSize(AMMO_LABEL).x), y),
282 FontStrSize(AMMO_LABEL)),
283 AMMO_LABEL, selected, disabled, colorWhite);
284 y += UTIL_MENU_SLOT_HEIGHT;
285 }
286 if (d->utilSlot >= 0)
287 {
288 const bool selected = d->slot == d->utilSlot;
289 DisplayMenuItem(
290 g,
291 Rect2iNew(
292 svec2i(CENTER_X(pos, size, FontStrSize(UTIL_LABEL).x), y),
293 FontStrSize(UTIL_LABEL)),
294 UTIL_LABEL, selected, false, colorWhite);
295 y += UTIL_MENU_SLOT_HEIGHT;
296 }
297
298 y += 8;
299 const WeaponClass *gun = NULL;
300 if (d->display.GunIdx >= 0 && d->display.GunIdx < MAX_WEAPONS)
301 {
302 gun = pData->guns[d->display.GunIdx];
303 }
304 DrawCharacterSimple(
305 &pData->Char, svec2i(pos.x + EQUIP_MENU_WIDTH / 2, pos.y + h + 8),
306 DIRECTION_DOWN, false, false, gun);
307
308 const bool endDisabled = IsSlotDisabled(d, d->endSlot) || d->equipping;
309 const bool endSelected = d->slot == d->endSlot;
310 DisplayMenuItem(
311 g,
312 Rect2iNew(
313 svec2i(CENTER_X(pos, size, FontStrSize(END_MENU_LABEL).x), y),
314 FontStrSize(END_MENU_LABEL)),
315 END_MENU_LABEL, endSelected, endDisabled, colorWhite);
316 }
317 static int HandleInputEquipMenu(int cmd, void *data)
318 {
319 EquipMenu *d = data;
320 PlayerData *p = PlayerDataGetByUID(d->PlayerUID);
321
322 int newSlot = d->slot;
323 if (Button1(cmd))
324 {
325 MenuPlaySound(MENU_SOUND_ENTER);
326 return 1;
327 }
328 else if (Button2(cmd))
329 {
330 if (d->slot < MAX_WEAPONS)
331 {
332 PlayerRemoveWeapon(p, d->slot);
333 MenuPlaySound(MENU_SOUND_SWITCH);
334 AnimatedCounterReset(&d->Cash, p->Totals.Score);
335 }
336 }
337 else if (Left(cmd))
338 {
339 if (d->slot < MAX_WEAPONS && (d->slot & 1) == 1)
340 {
341 newSlot--;
342 if (IsSlotDisabled(data, newSlot))
343 {
344 // Try switching up-left or down-left
345 if (!IsSlotDisabled(data, newSlot - 2))
346 {
347 newSlot -= 2;
348 }
349 else if (!IsSlotDisabled(data, newSlot + 2))
350 {
351 newSlot += 2;
352 }
353 }
354 }
355 }
356 else if (Right(cmd))
357 {
358 if (d->slot < MAX_WEAPONS && (d->slot & 1) == 0)
359 {
360 newSlot++;
361 if (IsSlotDisabled(data, newSlot))
362 {
363 // Try switching up-right or down-right
364 if (!IsSlotDisabled(data, newSlot - 2))
365 {
366 newSlot -= 2;
367 }
368 else if (!IsSlotDisabled(data, newSlot + 2))
369 {
370 newSlot += 2;
371 }
372 }
373 }
374 }
375 else if (Up(cmd))
376 {
377 if (d->slot >= MAX_WEAPONS)
378 {
379 // Keep going back until we find an enabled slot
380 do
381 {
382 newSlot--;
383 } while (newSlot > 0 && IsSlotDisabled(data, newSlot));
384 }
385 else if (d->slot >= 2)
386 {
387 newSlot -= 2;
388 if (IsSlotDisabled(data, newSlot))
389 {
390 // Try switching to the other slot above (up-left or up-right),
391 // or skip the row and keep going back until an enabled slot
392 if (!IsSlotDisabled(data, newSlot ^ 1))
393 {
394 newSlot ^= 1;
395 }
396 else
397 {
398 do
399 {
400 newSlot--;
401 } while (newSlot > 0 && IsSlotDisabled(data, newSlot));
402 }
403 }
404 }
405 }
406 else if (Down(cmd))
407 {
408 if (d->slot < d->endSlot)
409 {
410 if (d->slot < MAX_WEAPONS)
411 {
412 newSlot = MIN(d->endSlot, d->slot + 2);
413 if (IsSlotDisabled(data, newSlot))
414 {
415 // Try switching to the other slot below (down-left or
416 // down-right), or skip the row and keep going forward
417 // until an enabled slot
418 if (!IsSlotDisabled(data, newSlot ^ 1))
419 {
420 newSlot ^= 1;
421 }
422 else
423 {
424 do
425 {
426 newSlot++;
427 } while (newSlot < d->endSlot &&
428 IsSlotDisabled(data, newSlot));
429 }
430 }
431 }
432 else
433 {
434 do
435 {
436 newSlot++;
437 } while (newSlot < d->endSlot &&
438 IsSlotDisabled(data, newSlot));
439 }
440 }
441 }
442
443 if (newSlot != d->slot && !IsSlotDisabled(d, newSlot))
444 {
445 d->slot = newSlot;
446 MenuPlaySound(MENU_SOUND_SWITCH);
447 }
448
449 // Display gun based on menu index
450 if (d->slot < MAX_WEAPONS)
451 {
452 d->display.GunIdx = d->slot;
453 }
454
455 return 0;
456 }
457
458 static bool HasWeapon(const CArray *weapons, const WeaponClass *wc);
459 static void ResetSlotMeta(EquipMenu *menu)
460 {
461 const PlayerData *pData = PlayerDataGetByUID(menu->PlayerUID);
462
463 // Update SlotMeta
464 for (int i = 0; i < MAX_WEAPONS; i++)
465 {
466 menu->SlotMeta[i] = META_NONE;
467 const WeaponClass *slotWC = pData->guns[i];
468 CA_FOREACH(const WeaponClass *, wc, menu->weapons)
469 if (!InSlot(*wc, i))
470 {
471 continue;
472 }
473 const bool isNew =
474 *(const bool *)CArrayGet(&menu->weaponIsNew, _ca_index);
475 DrawGunMeta meta = isNew ? META_NEW : META_NONE;
476 if (slotWC != NULL)
477 {
478 // Check if this slot has an upgrade/downgrade
479 if (slotWC == WeaponClassGetPrerequisite(*wc))
480 {
481 meta = META_UPGRADE;
482 }
483 else if (WeaponClassGetPrerequisite(slotWC) == *wc)
484 {
485 meta = META_DOWNGRADE;
486 }
487 }
488 menu->SlotMeta[i] = MAX(menu->SlotMeta[i], meta);
489 CA_FOREACH_END()
490 }
491 }
492 static bool HasWeapon(const CArray *weapons, const WeaponClass *wc)
493 {
494 if (weapons == NULL)
495 {
496 return true;
497 }
498 CA_FOREACH(const WeaponClass *, wc2, *weapons)
499 if (*wc2 == wc)
500 {
501 return true;
502 }
503 CA_FOREACH_END()
504 return false;
505 }
506
507 void EquipMenuCreate(
508 EquipMenu *menu, const CArray *weapons, const CArray *prevWeapons,
509 const int numPlayers, const int player, const int playerUID,
510 EventHandlers *handlers, GraphicsDevice *graphics)
511 {
512 struct vec2i pos;
513 int w = graphics->cachedConfig.Res.x;
514 int h = graphics->cachedConfig.Res.y;
515
516 menu->display.PlayerUID = playerUID;
517 menu->display.currentMenu = NULL;
518 menu->display.Dir = DIRECTION_DOWN;
519 menu->PlayerUID = playerUID;
520 PlayerData *pData = PlayerDataGetByUID(playerUID);
521 menu->Cash = AnimatedCounterNew("Cash: $", pData->Totals.Score);
522 menu->Cash.incRatio = 0.2f;
523 menu->slotBGSprites =
524 PicManagerGetSprites(&gPicManager, "hud/gun_slot_bg");
525 CArrayCopy(&menu->weapons, weapons);
526 CArrayInit(&menu->weaponIsNew, sizeof(bool));
527 CA_FOREACH(const WeaponClass *, wc, menu->weapons)
528 const bool isNew = !HasWeapon(prevWeapons, *wc);
529 CArrayPushBack(&menu->weaponIsNew, &isNew);
530 CA_FOREACH_END()
531 ResetSlotMeta(menu);
532
533 switch (numPlayers)
534 {
535 case 1:
536 // Single menu, entire screen
537 pos = svec2i(w / 2, 0);
538 menu->size = svec2i(w / 2, h);
539 break;
540 case 2:
541 // Two menus, side by side
542 pos = svec2i(player * w / 2 + w / 4, 0);
543 menu->size = svec2i(w / 4, h);
544 break;
545 case 3:
546 case 4:
547 // Four corners
548 pos = svec2i((player & 1) * w / 2 + w / 4, (player / 2) * h / 2);
549 menu->size = svec2i(w / 4, h / 2);
550 break;
551 default:
552 CASSERT(false, "not implemented");
553 pos = svec2i(w / 2, 0);
554 menu->size = svec2i(w / 2, h);
555 break;
556 }
557 // Check how many util menu items there are
558 menu->endSlot = MAX_WEAPONS;
559 menu->ammoSlot = -1;
560 menu->utilSlot = -1;
561 if (gCampaign.Setting.BuyAndSell)
562 {
563 if (gCampaign.Setting.Ammo)
564 {
565 menu->ammoSlot = menu->endSlot;
566 menu->endSlot++;
567 // Auto-refill free ammo
568 for (int i = 0; i < AmmoGetNumClasses(&gAmmo); i++)
569 {
570 const Ammo *ammo = AmmoGetById(&gAmmo, i);
571 if (ammo->Price == 0)
572 {
573 PlayerAddAmmo(pData, i, ammo->Max, true);
574 }
575 }
576 }
577 // Can always buy lives; buying HP depends on game settings
578 menu->utilSlot = menu->endSlot;
579 menu->endSlot++;
580 }
581
582 // Create equipped weapons menu
583 const struct vec2i weaponsSize =
584 svec2i(EQUIP_MENU_WIDTH, EQUIP_MENU_SLOT_HEIGHT * 2 + FontH());
585 const struct vec2i weaponsPos = svec2i(
586 pos.x - EQUIP_MENU_WIDTH,
587 CENTER_Y(pos, menu->size, weaponsSize.y) - 12);
588
589 MenuSystemInit(&menu->ms, handlers, graphics, weaponsPos, weaponsSize);
590 menu->ms.align = MENU_ALIGN_LEFT;
591 MenuAddExitType(&menu->ms, MENU_TYPE_RETURN);
592
593 // Count number of each gun type, and disable extra menu items
594 int numGuns = 0;
595 int numMelee = 0;
596 int numGrenades = 0;
597 CA_FOREACH(const WeaponClass *, wc, menu->weapons)
598 if ((*wc)->Type == GUNTYPE_GRENADE)
599 {
600 numGrenades++;
601 }
602 else if ((*wc)->Type == GUNTYPE_MELEE)
603 {
604 numMelee++;
605 }
606 else
607 {
608 numGuns++;
609 }
610 CA_FOREACH_END()
611 for (int i = 0; i < MELEE_SLOT; i++)
612 {
613 menu->EquipEnabled[i] = i < numGuns;
614 }
615 menu->EquipEnabled[MELEE_SLOT] = numMelee > 0;
616 for (int i = 0; i < MAX_GRENADES; i++)
617 {
618 menu->EquipEnabled[i + MAX_GUNS] = i < numGrenades;
619 }
620
621 // Pre-select the End menu
622 menu->slot = menu->endSlot;
623 menu->ms.root = menu->ms.current =
624 MenuCreateCustom("", DrawEquipMenu, HandleInputEquipMenu, menu);
625
626 // For AI players, pre-pick their weapons and go straight to menu end
627 if (pData->inputDevice == INPUT_DEVICE_AI)
628 {
629 menu->ms.current = NULL;
630 menu->equipping = false;
631 AICoopSelectWeapons(pData, player, weapons);
632 }
633 else
634 {
635 for (int i = 0; i < MAX_WEAPONS; i++)
636 {
637 WeaponMenuCreate(
638 &menu->weaponMenus[i], &menu->weapons, &menu->weaponIsNew,
639 playerUID, i, pos, menu->size, handlers, graphics);
640 }
641 AmmoMenuCreate(
642 &menu->ammoMenu, playerUID, pos, menu->size, handlers, graphics);
643 UtilMenuCreate(
644 &menu->utilMenu, playerUID, pos, menu->size, handlers, graphics);
645 }
646 }
647
648 void EquipMenuTerminate(EquipMenu *menu)
649 {
650 for (int i = 0; i < MAX_WEAPONS; i++)
651 {
652 WeaponMenuTerminate(&menu->weaponMenus[i]);
653 }
654 AmmoMenuTerminate(&menu->ammoMenu);
655 UtilMenuTerminate(&menu->utilMenu);
656 MenuSystemTerminate(&menu->ms);
657 CArrayTerminate(&menu->weapons);
658 CArrayTerminate(&menu->weaponIsNew);
659 AnimatedCounterTerminate(&menu->Cash);
660 }
661
662 void EquipMenuUpdate(EquipMenu *menu, const int cmd)
663 {
664 AnimatedCounterUpdate(&menu->Cash, 1);
665 PlayerData *p = PlayerDataGetByUID(menu->PlayerUID);
666 if (menu->slot < MAX_WEAPONS && menu->weaponMenus[menu->slot].Active)
667 {
668 WeaponMenuUpdate(&menu->weaponMenus[menu->slot], cmd);
669 menu->equipping = menu->weaponMenus[menu->slot].Active;
670 // If weapons changed, reset menus
671 if (!menu->equipping)
672 {
673 for (int i = 0; i < MAX_WEAPONS; i++)
674 {
675 WeaponMenuReset(&menu->weaponMenus[i]);
676 }
677 AmmoMenuReset(&menu->ammoMenu);
678 ResetSlotMeta(menu);
679 }
680 }
681 else if (menu->slot == menu->ammoSlot && menu->ammoMenu.Active)
682 {
683 if (AmmoMenuUpdate(&menu->ammoMenu, cmd))
684 {
685 AnimatedCounterReset(&menu->Cash, p->Totals.Score);
686 }
687 menu->equipping = menu->ammoMenu.Active;
688 }
689 else if (menu->slot == menu->utilSlot && menu->utilMenu.Active)
690 {
691 if (UtilMenuUpdate(&menu->utilMenu, cmd))
692 {
693 AnimatedCounterReset(&menu->Cash, p->Totals.Score);
694 }
695 menu->equipping = menu->utilMenu.Active;
696 }
697 else
698 {
699 MenuProcessCmd(&menu->ms, cmd);
700 if (menu->slot < menu->endSlot)
701 {
702 if (MenuIsExit(&menu->ms))
703 {
704 // Open weapon selection menu
705 menu->equipping = true;
706 if (menu->slot < MAX_WEAPONS)
707 {
708 WeaponMenuActivate(&menu->weaponMenus[menu->slot]);
709 }
710 else if (menu->slot == menu->ammoSlot)
711 {
712 AmmoMenuActivate(&menu->ammoMenu);
713 }
714 else if (menu->slot == menu->utilSlot)
715 {
716 UtilMenuActivate(&menu->utilMenu);
717 }
718 else
719 {
720 CASSERT(false, "unknown menu slot");
721 }
722 menu->ms.current = menu->ms.root;
723 }
724 }
725 }
726
727 // Disable menu based on equipping state
728 MenuSetDisabled(menu->ms.root, menu->equipping);
729 if (!menu->equipping)
730 {
731 AnimatedCounterReset(&menu->Cash, p->Totals.Score);
732 }
733 }
734
735 bool EquipMenuIsDone(const EquipMenu *menu)
736 {
737 return menu->ms.current == NULL && !menu->equipping &&
738 menu->slot == menu->endSlot;
739 }
740
741 void EquipMenuDraw(const EquipMenu *menu)
742 {
743 MenuDisplay(&menu->ms);
744 if (menu->slot < MAX_WEAPONS)
745 {
746 WeaponMenuDraw(&menu->weaponMenus[menu->slot]);
747 }
748 else if (menu->slot == menu->ammoSlot)
749 {
750 AmmoMenuDraw(&menu->ammoMenu);
751 }
752 else if (menu->slot == menu->utilSlot)
753 {
754 UtilMenuDraw(&menu->utilMenu);
755 }
756 }