fork/training-mode-ce

git clone https://git.julia.blue/fork/training-mode-ce

AitchAdd tech chase event (#317)1da5d99

nix
11.0 KiB388 linesraw
1#include "events.h"
2
3static Vec2 ledge_positions[2];
4
5static void GetLedgePositions(Vec2 coords_out[2]) {
6    static char ledge_ids[34][2] = {
7        { 0xFF, 0xFF }, { 0xFF, 0xFF }, { 0x03, 0x07 }, { 0x33, 0x36 },
8        { 0x03, 0x0D }, { 0x29, 0x45 }, { 0x05, 0x11 }, { 0x09, 0x1A },
9        { 0x02, 0x06 }, { 0x15, 0x17 }, { 0x00, 0x00 }, { 0x43, 0x4C },
10        { 0x00, 0x00 }, { 0x00, 0x00 }, { 0x0E, 0x0D }, { 0x00, 0x00 },
11        { 0x00, 0x05 }, { 0x1E, 0x2E }, { 0x0C, 0x0E }, { 0x02, 0x04 },
12        { 0x03, 0x05 }, { 0x00, 0x00 }, { 0x06, 0x12 }, { 0x00, 0x00 },
13        { 0xD7, 0xE2 }, { 0x00, 0x00 }, { 0x00, 0x00 }, { 0x00, 0x00 },
14        { 0x03, 0x05 }, { 0x03, 0x0B }, { 0x06, 0x10 }, { 0x00, 0x05 },
15        { 0x00, 0x02 }, { 0x01, 0x01 },
16    };
17
18    int stage_id = Stage_GetExternalID();
19    char left_id = ledge_ids[stage_id][0];
20    char right_id = ledge_ids[stage_id][1];
21
22    Vec3 pos;
23    Stage_GetLeftOfLineCoordinates(left_id, &pos);
24    coords_out[0] = (Vec2) { pos.X, pos.Y };
25    Stage_GetRightOfLineCoordinates(right_id, &pos);
26    coords_out[1] = (Vec2) { pos.X, pos.Y };
27}
28
29static void UpdatePosition(GOBJ *fighter) {
30    FighterData *data = fighter->userdata;
31
32    Vec3 pos = data->phys.pos;
33    data->coll_data.topN_Curr = pos;
34    data->coll_data.topN_CurrCorrect = pos;
35    data->coll_data.topN_Prev = pos;
36    data->coll_data.topN_Proj = pos;
37    data->coll_data.coll_test = *stc_colltest;
38
39    JOBJ *jobj = fighter->hsd_object;
40    jobj->trans = data->phys.pos;
41    JOBJ_SetMtxDirtySub(jobj);
42
43    // Update static hmn block coords
44    Fighter_SetPosition(data->ply, data->flags.ms, &data->phys.pos);
45}
46
47static bool FindGroundNearPlayer(GOBJ *fighter, Vec3 *pos, int *line_idx) {
48    FighterData *data = fighter->userdata;
49    float x = data->phys.pos.X;
50    float y1 = data->phys.pos.Y + 10;
51    float y2 = data->phys.pos.Y - 100;
52
53    Vec3 _line_unk;
54    int _line_kind;
55
56    return GrColl_RaycastGround(pos, line_idx, &_line_kind, &_line_unk,
57            -1, -1, -1, 0, x, y1, x, y2, 0);
58}
59
60static void PlacePlayerOnGround(GOBJ *fighter) {
61    FighterData *data = fighter->userdata;
62
63    Vec3 pos;
64    int line_idx;
65    if (FindGroundNearPlayer(fighter, &pos, &line_idx)) {
66        data->phys.pos.X = pos.X;
67        data->phys.pos.Y = pos.Y;
68        data->coll_data.ground_index = line_idx;
69    }
70    UpdatePosition(fighter);
71    EnvironmentCollision_WaitLanding(fighter);
72    Fighter_SetGrounded(data);
73}
74
75static void UpdateCameraBox(GOBJ *fighter) {
76    Fighter_UpdateCameraBox(fighter);
77
78    FighterData *data = fighter->userdata;
79    CmSubject *subject = data->camera_subject;
80    subject->boundleft_curr = subject->boundleft_proj;
81    subject->boundright_curr = subject->boundright_proj;
82
83    Match_CorrectCamera();
84}
85
86void Exit(GOBJ *menu);
87void Reset(int side_idx);
88
89enum menu_options {
90    OPT_LINES,
91};
92
93enum ledge_direction {
94    DIRECTION_RANDOM,
95    DIRECTION_LEFT,
96    DIRECTION_RIGHT,
97};
98
99static EventOption Options_Main[] = {
100    {
101        .kind = OPTKIND_TOGGLE,
102        .name = "Enable Line Guides",
103        .desc = {"Draws horizontal lines at the ledges and at your",
104                 "Calculated ledgegrab position"},
105    },
106    {
107        .kind = OPTKIND_INFO,
108        .name = "Help",
109        .desc = {"D-Pad left/right to reset manually to that side"}
110    },
111    {
112        .kind = OPTKIND_FUNC,
113        .name = "Exit",
114        .desc = {"Return to the Event Select Screen."},
115        .OnSelect = Exit,
116    },
117};
118
119static EventMenu Menu_Main = {
120    .name = "Sweetspot Training",
121    .option_num = sizeof(Options_Main) / sizeof(EventOption),
122    .options = Options_Main,
123};
124
125enum event_state {
126    STATE_ENDLAG,
127    STATE_WAIT_ATTACK,
128    STATE_ATTACK_THINK,
129    STATE_RESET,
130}; 
131
132static struct sideb {
133    bool attempted;
134    Vec3 pos;
135    int dir;
136} sideb = { 0 };
137static int reset_timer = 0;
138static int state = STATE_ENDLAG;
139
140void Draw_Lines() {
141    GOBJ *hmn = Fighter_GetGObj(0);
142    FighterData *hmn_data = hmn->userdata;
143    CollData *coll_data = &hmn_data->coll_data;
144
145    if (!Options_Main[OPT_LINES].val)
146        return;
147
148    COBJ *cur_cam = COBJ_GetCurrent();
149    CObj_SetCurrent(*stc_matchcam_cobj);
150
151    PRIM_DrawMode draw_mode = {
152        .line_width = 16,
153        .z_compare_enable = true,
154        .z_logic_eq = true,
155        .z_logic_lt = true,
156        .shape = PRIM_SHAPE_LINE_STRIP,
157    };
158    PRIM_BlendMode blend_mode = { 0 };
159
160    // Melee calculation for the top of the ledgegrab box
161    float ledgegrab_offset = coll_data->cliffgrab_y_offset + 0.5 * coll_data->cliffgrab_height;
162    if (hmn_data->kind == FTKIND_FOX) {
163        // Only Fox drops a small amount during sideb before moving
164        ledgegrab_offset -= 1.0 / 6.0;
165    }
166
167    float y = hmn_data->phys.pos.Y + ledgegrab_offset; 
168    float x = hmn_data->phys.pos.X; 
169    u32 color = 0x0000ffff;
170    if (sideb.attempted) {
171        x = sideb.pos.X;
172        y = sideb.pos.Y + ledgegrab_offset;
173
174        Vec2 target_ledge;
175        if (sideb.dir == 1)
176            target_ledge = ledge_positions[0];
177        else
178            target_ledge = ledge_positions[1];
179
180        float delta = y - target_ledge.Y;
181
182        if (delta <= 0) { // failure (SD)
183            color = 0xff0000ff;
184        } else if (hmn_data->state_id == ASID_CLIFFCATCH
185            || hmn_data->state_id == ASID_CLIFFWAIT) { // success
186            color = 0x00ff00ff;
187        }
188    }
189
190    // draw line at top of ledgegrab box
191    PRIM_NEW(2, draw_mode, blend_mode);
192    PRIM_DRAW(-300, y, 0, color);
193    PRIM_DRAW(300, y, 0, color);
194
195    // draw line from left edge
196    color = 0xffffffff;
197    x = ledge_positions[0].X;
198    y = ledge_positions[0].Y;
199    PRIM_NEW(2, draw_mode, blend_mode);
200    PRIM_DRAW(x, y, 0, color);
201    PRIM_DRAW(x - 100, y, 0, color);
202
203    // draw line from right edge
204    x = ledge_positions[1].X;
205    y = ledge_positions[1].Y;
206    PRIM_NEW(2, draw_mode, blend_mode);
207    PRIM_DRAW(x, y, 0, color);
208    PRIM_DRAW(x + 100, y, 0, color);
209
210    PRIM_CLOSE();
211    CObj_SetCurrent(cur_cam);
212}
213
214void Event_Init(GOBJ* gobj) {
215    GetLedgePositions(ledge_positions);
216    GOBJ *draw_gobj = GObj_Create(0, 0, 0);
217    GObj_AddGXLink(draw_gobj, Draw_Lines, 3, 0);
218}
219
220void Event_Think(GOBJ *menu) {
221    if (event_vars->game_timer == 1) {
222        event_vars->Savestate_Save_v1(event_vars->savestate, Savestate_Silent);
223        Reset(0);
224    }
225
226    // Reset timer is 0 when off, otherwise counts down until reset
227    if (reset_timer == 1) {
228        reset_timer = 0;
229        Reset(2);
230    }
231    if (reset_timer > 1)
232        reset_timer--;
233    
234    GOBJ *hmn = Fighter_GetGObj(0);
235    FighterData *hmn_data = hmn->userdata;
236    GOBJ *cpu = Fighter_GetGObj(1);
237    FighterData *cpu_data = cpu->userdata;
238
239    // Reset left/right when user presses dpad manually
240    if (hmn_data->input.down & HSD_BUTTON_DPAD_LEFT) {
241        Reset(0);
242    } else if (hmn_data->input.down & HSD_BUTTON_DPAD_RIGHT) {
243        Reset(1);
244    }
245
246    // Reset when dead
247    if (hmn_data->flags.dead)
248        Reset(2);
249
250    if (hmn_data->state_id == 350) { // Aerial sideb startup for fox/falco
251        sideb.attempted = true;
252        sideb.pos = hmn_data->phys.pos;
253        sideb.dir = hmn_data->facing_direction;
254    }
255
256    // Begin CPU AI logic
257    Fighter_ZeroCPUInputs(cpu_data);
258
259    // always hold crouch when state is higher than endlag
260    if (state > STATE_ENDLAG)
261        cpu_data->cpu.lstickY = -127;
262
263    switch (state) {
264        case STATE_ENDLAG:
265            if (cpu_data->state_id == ASID_WAIT) {
266                cpu_data->cpu.lstickY = -127;
267                state = STATE_WAIT_ATTACK;
268            }
269            break;
270        case STATE_WAIT_ATTACK:
271            if (hmn_data->state_id == 0x15F) { // side b asid
272                // input dtilt
273                cpu_data->cpu.lstickY = -38;
274                cpu_data->cpu.held = PAD_BUTTON_A;
275                state = STATE_ATTACK_THINK;
276            } else if (hmn_data->state_id == 0x162 // up b asid
277                    || hmn_data->state_id == ASID_ESCAPEAIR) {
278                SFX_Play(0xAF);
279                reset_timer = 30;
280                state = STATE_RESET;
281            }
282            break;
283        case STATE_ATTACK_THINK:
284            if (cpu_data->state_id != ASID_ATTACKLW3) break;
285            if (cpu_data->state.frame == 7) {
286                FrameSpeedChange(cpu, 0);       // freeze cpu
287                if (hmn_data->victim) {         // if hit
288                    FrameSpeedChange(cpu, 1.0); // unfreeze
289                    reset_timer = 30;
290                    state = STATE_RESET;
291                }
292            }
293            if (hmn_data->state_id == ASID_CLIFFCATCH) {
294                SFX_Play(0xAD);
295                FrameSpeedChange(cpu, 1.0);
296                reset_timer = 60;
297                state = STATE_RESET;
298            }
299            break;
300        case STATE_RESET:
301            break;
302    }
303
304}
305
306void Exit(GOBJ *menu) {
307    stc_match->state = 3;
308    Match_EndVS();
309}
310
311void Reset(int side_idx) {
312    if (side_idx == 2)
313        side_idx = HSD_Randi(2);
314    if (side_idx != 0 && side_idx != 1)
315        assert("invalid side");
316
317    event_vars->Savestate_Load_v1(event_vars->savestate, Savestate_Silent);
318    sideb.attempted = false;
319
320    GOBJ *hmn = Fighter_GetGObj(0);
321    FighterData *hmn_data = hmn->userdata;
322    GOBJ *cpu = Fighter_GetGObj(1);
323    FighterData *cpu_data = cpu->userdata;
324
325    int side = side_idx * 2 - 1;
326    hmn_data->facing_direction = -side;
327    cpu_data->facing_direction = side;
328
329    hmn_data->phys.pos.X = side * 12 + ledge_positions[side_idx].X;
330    hmn_data->phys.pos.Y = 6;
331    cpu_data->phys.pos.X = side * -15 + ledge_positions[side_idx].X;
332    cpu_data->phys.pos.Y = 0;
333
334    UpdatePosition(hmn);
335    UpdatePosition(cpu);
336    PlacePlayerOnGround(cpu);
337
338    // cpu state
339    cpu_data->gfx = 0;
340    FSmash_GetASForAnalogAngle(cpu);
341    Fighter_ApplyAnimation(cpu, 11, 1, 0);
342    cpu_data->state.frame = 11;
343    cpu_data->script.script_event_timer = 0;
344    Fighter_SubactionFastForward(cpu);
345    Fighter_UpdateStateFrameInfo(cpu);
346    Fighter_HitboxDisableAll(cpu);
347    cpu_data->script.script_current = 0;
348    UpdateCameraBox(cpu);
349
350    // hmn state
351    ActionStateChange(0, 1, 0, hmn, ASID_DAMAGEFLYN, 0x40, 0);
352    Fighter_UpdateStateFrameInfo(hmn);
353    UpdateCameraBox(hmn);
354    float dmg = 50 * HSD_Randf() + 50;
355    hmn_data->dmg.percent = dmg;
356    Fighter_SetHUDDamage(hmn_data->ply, dmg);
357    
358    // hmn knockback
359    float angle = (15 * HSD_Randf() + 45) * M_1DEGREE;
360    float mag = 30 * HSD_Randf() + 105;
361    float vel = mag * (*stc_ftcommon)->force_applied_to_kb_mag_multiplier;
362    hmn_data->phys.kb_vel.X = cos(angle) * vel * side;
363    hmn_data->phys.kb_vel.Y = sin(angle) * vel;
364    float kb_frames = (float)(int)((*stc_ftcommon)->x154 * mag);
365
366    union IntToFloat {
367        int i;
368        float f;
369    } itf;
370    itf.f = kb_frames;
371    hmn_data->state_var.state_var1 = itf.i;
372
373    hmn_data->flags.hitstun = 1;
374    Fighter_EnableCollUpdate(hmn_data);
375
376    // give both hitlag
377    hmn_data->dmg.hitlag_frames = 10;
378    hmn_data->flags.hitlag = 1;
379    hmn_data->flags.hitlag_unk = 1;
380    cpu_data->dmg.hitlag_frames = 10;
381    cpu_data->flags.hitlag = 1;
382    cpu_data->flags.hitlag_unk = 1;
383
384    reset_timer = 0;
385    state = STATE_ENDLAG;
386}
387
388EventMenu *Event_Menu = &Menu_Main;