|
| 1 | +#include "common-chax.h" |
| 2 | +#include "skill-system.h" |
| 3 | +#include "constants/skills.h" |
| 4 | +#include "constants/texts.h" |
| 5 | +#include "weapon-range.h" |
| 6 | +#include "heros-movement.h" |
| 7 | + |
| 8 | +static void try_add_target(struct Unit *unit) |
| 9 | +{ |
| 10 | + if (UnitOnMapAvaliable(unit) && AreUnitsAllied(gSubjectUnit->index, unit->index)) { |
| 11 | + if (!CanUnitCrossTerrain(unit, gBmMapTerrain[gActiveUnit->yPos][gActiveUnit->xPos])) |
| 12 | + return; |
| 13 | + |
| 14 | + if (!CanUnitCrossTerrain(gActiveUnit, gBmMapTerrain[unit->yPos][unit->xPos])) |
| 15 | + return; |
| 16 | + |
| 17 | + AddTarget(unit->xPos, unit->yPos, unit->index, 1); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +static void make_target_list(struct Unit *unit) |
| 22 | +{ |
| 23 | + int x = unit->xPos; |
| 24 | + int y = unit->yPos; |
| 25 | + |
| 26 | + gSubjectUnit = unit; |
| 27 | + |
| 28 | + ForEachAdjacentUnit(x, y, try_add_target); |
| 29 | +} |
| 30 | + |
| 31 | +u8 Swap_Usability(const struct MenuItemDef *def, int number) |
| 32 | +{ |
| 33 | + if (gActiveUnit->state & US_CANTOING) |
| 34 | + return MENU_NOTSHOWN; |
| 35 | + |
| 36 | + if (!HasSelectTarget(gActiveUnit, make_target_list)) |
| 37 | + return MENU_DISABLED; |
| 38 | + |
| 39 | + return MENU_ENABLED; |
| 40 | +} |
| 41 | + |
| 42 | +static u8 select_target_on_switchin(ProcPtr proc, struct SelectTarget* target) |
| 43 | +{ |
| 44 | + int x, y; |
| 45 | + |
| 46 | + BmMapFill(gBmMapMovement, -1); |
| 47 | + BmMapFill(gBmMapRange, 0); |
| 48 | + |
| 49 | + x = target->x; |
| 50 | + y = target->y; |
| 51 | + |
| 52 | + gBmMapRange[y][x] = 1; |
| 53 | + DisplayMoveRangeGraphics(MOVLIMITV_RMAP_BLUE); |
| 54 | + return 0; |
| 55 | +} |
| 56 | + |
| 57 | +static u8 select_target_on_select(ProcPtr proc, struct SelectTarget *target) |
| 58 | +{ |
| 59 | + gActionData.xOther = target->x; |
| 60 | + gActionData.yOther = target->y; |
| 61 | + gActionData.targetIndex = target->uid; |
| 62 | + |
| 63 | + HideMoveRangeGraphics(); |
| 64 | + |
| 65 | + BG_Fill(gBG2TilemapBuffer, 0); |
| 66 | + BG_EnableSyncByMask(BG2_SYNC_BIT); |
| 67 | + |
| 68 | +#if defined(SID_Swap) && (COMMON_SKILL_VALID(SID_Swap)) |
| 69 | + gActionData.unk08 = SID_Swap; |
| 70 | +#endif |
| 71 | + |
| 72 | + gActionData.unitActionType = CONFIG_UNIT_ACTION_EXPA_ExecSkill; |
| 73 | + |
| 74 | + return TARGETSELECTION_ACTION_ENDFAST | TARGETSELECTION_ACTION_END | TARGETSELECTION_ACTION_SE_6A | TARGETSELECTION_ACTION_CLEARBGS; |
| 75 | +} |
| 76 | + |
| 77 | +static u8 select_target_on_cancel(ProcPtr proc, struct SelectTarget* target) |
| 78 | +{ |
| 79 | + HideMoveRangeGraphics(); |
| 80 | + |
| 81 | + return GenericSelection_BackToUM(proc, target); |
| 82 | +} |
| 83 | + |
| 84 | +static const struct SelectInfo select_info = { |
| 85 | + .onInit = NULL, |
| 86 | + .onEnd = NULL, |
| 87 | + .onSwitchIn = select_target_on_switchin, |
| 88 | + .onSelect = select_target_on_select, |
| 89 | + .onCancel = select_target_on_cancel, |
| 90 | + .onHelp = NULL, |
| 91 | +}; |
| 92 | + |
| 93 | +u8 Swap_OnSelected(struct MenuProc *menu, struct MenuItemProc *item) |
| 94 | +{ |
| 95 | + if (item->availability == MENU_DISABLED) { |
| 96 | + MenuFrozenHelpBox(menu, MSG_HMU_ERROR_TERRAIN); |
| 97 | + return MENU_ACT_SND6B; |
| 98 | + } |
| 99 | + |
| 100 | + ClearBg0Bg1(); |
| 101 | + |
| 102 | + make_target_list(gActiveUnit); |
| 103 | + BmMapFill(gBmMapMovement, -1); |
| 104 | + |
| 105 | + StartSubtitleHelp( |
| 106 | + NewTargetSelection(&select_info), |
| 107 | + GetStringFromIndex(MSG_HMU_SELECTTARGET_SUBTITLEHELP)); |
| 108 | + |
| 109 | + return MENU_ACT_SKIPCURSOR | MENU_ACT_END | MENU_ACT_SND6A; |
| 110 | +} |
| 111 | + |
| 112 | +static void action_init(struct ProcHmu *proc) |
| 113 | +{ |
| 114 | + struct Unit *unit = GetUnit(gActionData.targetIndex); |
| 115 | + |
| 116 | + proc->mu1 = GetUnitMu(gActiveUnit); |
| 117 | + if (!proc->mu1) { |
| 118 | + HideUnitSprite(gActiveUnit); |
| 119 | + proc->mu1 = StartMu(gActiveUnit); |
| 120 | + } |
| 121 | + SetMuFacing(proc->mu1, GetFacingDirection(gActiveUnit->xPos, gActiveUnit->yPos, gActionData.xOther, gActionData.yOther)); |
| 122 | + |
| 123 | + proc->mu2 = GetUnitMu(unit); |
| 124 | + if (!proc->mu2) { |
| 125 | + HideUnitSprite(unit); |
| 126 | + proc->mu2 = StartMu(unit); |
| 127 | + } |
| 128 | + SetMuFacing(proc->mu2, GetFacingDirection(gActionData.xOther, gActionData.yOther, gActiveUnit->xPos, gActiveUnit->yPos)); |
| 129 | + |
| 130 | + DisableMuCamera(proc->mu2); |
| 131 | + proc->timer1 = proc->timer2 = 0; |
| 132 | +} |
| 133 | + |
| 134 | +static void action_loop(struct ProcHmu *proc) |
| 135 | +{ |
| 136 | + if (proc->timer1 < 1) { |
| 137 | + Mu_OnStateMovement(proc->mu1); |
| 138 | + |
| 139 | + if (proc->mu1->move_clock_q4 == 0) |
| 140 | + proc->timer1++; |
| 141 | + } |
| 142 | + |
| 143 | + if (proc->timer2 < 1) { |
| 144 | + Mu_OnStateMovement(proc->mu2); |
| 145 | + |
| 146 | + if (proc->mu2->move_clock_q4 == 0) |
| 147 | + proc->timer2++; |
| 148 | + } |
| 149 | + |
| 150 | + if (proc->timer1 >= 1 && proc->timer2 >= 1) |
| 151 | + Proc_Break(proc); |
| 152 | +} |
| 153 | + |
| 154 | +static void action_end(struct ProcHmu *proc) |
| 155 | +{ |
| 156 | + struct Unit *unit = GetUnit(gActionData.targetIndex); |
| 157 | + |
| 158 | + unit->xPos = gActiveUnit->xPos; |
| 159 | + unit->yPos = gActiveUnit->yPos; |
| 160 | + |
| 161 | + gActionData.xMove = gActiveUnit->xPos = gActionData.xOther; |
| 162 | + gActionData.yMove = gActiveUnit->yPos = gActionData.yOther; |
| 163 | +} |
| 164 | + |
| 165 | +static const struct ProcCmd proc_scr_action[] = { |
| 166 | + PROC_YIELD, |
| 167 | + PROC_CALL(action_init), |
| 168 | + PROC_YIELD, |
| 169 | + PROC_REPEAT(action_loop), |
| 170 | + PROC_YIELD, |
| 171 | + PROC_CALL(action_end), |
| 172 | + PROC_END |
| 173 | +}; |
| 174 | + |
| 175 | +static void callback_anim(ProcPtr proc) |
| 176 | +{ |
| 177 | + Proc_StartBlocking(proc_scr_action, proc); |
| 178 | +} |
| 179 | + |
| 180 | +bool Action_Swap(ProcPtr parent) |
| 181 | +{ |
| 182 | + int sid = gActionData.unk08; |
| 183 | + |
| 184 | + if (SkillListTester(gActiveUnit, sid)) |
| 185 | + NewMuSkillAnimOnActiveUnitWithDeamon(parent, sid, callback_anim, NULL); |
| 186 | + else |
| 187 | + callback_anim(parent); |
| 188 | + |
| 189 | + return true; |
| 190 | +} |
0 commit comments