-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathhar.c
More file actions
2823 lines (2479 loc) · 106 KB
/
Copy pathhar.c
File metadata and controls
2823 lines (2479 loc) · 106 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "audio/audio.h"
#include "controller/controller.h"
#include "formats/af.h"
#include "game/common_defines.h"
#include "game/game_state.h"
#include "game/objects/arena_constraints.h"
#include "game/objects/har.h"
#include "game/objects/projectile.h"
#include "game/objects/scrap.h"
#include "game/protos/intersect.h"
#include "game/scenes/arena.h"
#include "game/utils/serial.h"
#include "resources/af_loader.h"
#include "resources/animation.h"
#include "resources/pilots.h"
#include "utils/allocator.h"
#include "utils/log.h"
#include "utils/miscmath.h"
#include "utils/random.h"
#include "video/damage_tracker.h"
#include "video/vga_state.h"
#include "video/video.h"
#define IS_ZERO(n) (n < 0.8 && n > -0.8)
void har_finished(object *obj);
int har_act(object *obj, int act_type);
void har_spawn_scrap(object *obj, vec2i pos, int amount);
void har_free(object *obj) {
har *h = object_get_userdata(obj);
list_free(&h->har_hooks);
hashmap_free(&h->disabled_animations);
#ifdef DEBUGMODE
surface_free(&h->hit_pixel);
surface_free(&h->har_origin);
#endif
vector_free(&h->child_objects);
omf_free(h);
object_set_userdata(obj, NULL);
}
/* hooks */
void fire_hooks(har *h, har_event event, controller *ctrl) {
iterator it;
har_hook *hook;
list_iter_begin(&h->har_hooks, &it);
foreach(it, hook) {
hook->cb(event, ctrl->gs->sc);
}
if(object_get_userdata(game_state_find_object(ctrl->gs, ctrl->har_obj_id)) == h) {
controller_har_hook(ctrl, event);
}
}
void har_event_jump(har *h, int direction, controller *ctrl) {
// direction is -1, 0 or 1, for backwards, up and forwards
har_event event;
event.type = HAR_EVENT_JUMP;
event.player_id = h->player_id;
event.direction = direction;
fire_hooks(h, event, ctrl);
}
void har_event_air_turn(har *h, controller *ctrl) {
har_event event;
event.type = HAR_EVENT_AIR_TURN;
event.player_id = h->player_id;
fire_hooks(h, event, ctrl);
}
void har_event_walk(har *h, int direction, controller *ctrl) {
// direction is -1, 1, for backwards and forwards
har_event event;
memset(&event, 0, sizeof(event));
event.type = HAR_EVENT_WALK;
event.player_id = h->player_id;
event.direction = direction;
fire_hooks(h, event, ctrl);
}
void har_event_air_attack_done(har *h, controller *ctrl) {
har_event event;
memset(&event, 0, sizeof(event));
event.type = HAR_EVENT_AIR_ATTACK_DONE;
event.player_id = h->player_id;
fire_hooks(h, event, ctrl);
}
void har_event_attack(har *h, af_move *move, controller *ctrl) {
har_event event;
memset(&event, 0, sizeof(event));
event.type = HAR_EVENT_ATTACK;
event.player_id = h->player_id;
event.move = move;
fire_hooks(h, event, ctrl);
}
void har_event_enemy_block(har *h, af_move *move, bool projectile, controller *ctrl) {
har_event event;
memset(&event, 0, sizeof(event));
event.type = projectile ? HAR_EVENT_ENEMY_BLOCK_PROJECTILE : HAR_EVENT_ENEMY_BLOCK;
event.type = HAR_EVENT_ENEMY_BLOCK;
event.player_id = h->player_id;
event.move = move;
fire_hooks(h, event, ctrl);
}
void har_event_block(har *h, af_move *move, bool projectile, controller *ctrl) {
har_event event;
memset(&event, 0, sizeof(event));
event.type = projectile ? HAR_EVENT_BLOCK_PROJECTILE : HAR_EVENT_BLOCK;
event.player_id = h->player_id;
event.move = move;
fire_hooks(h, event, ctrl);
}
void har_event_take_hit(har *h, af_move *move, bool projectile, controller *ctrl) {
har_event event;
memset(&event, 0, sizeof(event));
event.type = projectile ? HAR_EVENT_TAKE_HIT_PROJECTILE : HAR_EVENT_TAKE_HIT;
event.player_id = h->player_id;
event.move = move;
fire_hooks(h, event, ctrl);
}
void har_event_land_hit(har *h, af_move *move, bool projectile, controller *ctrl) {
har_event event;
memset(&event, 0, sizeof(event));
event.type = projectile ? HAR_EVENT_LAND_HIT_PROJECTILE : HAR_EVENT_LAND_HIT;
event.player_id = h->player_id;
event.move = move;
fire_hooks(h, event, ctrl);
}
void har_event_hazard_hit(har *h, bk_info *info, controller *ctrl) {
har_event event;
memset(&event, 0, sizeof(event));
event.type = HAR_EVENT_HAZARD_HIT;
event.player_id = h->player_id;
event.info = info;
fire_hooks(h, event, ctrl);
}
void har_event_enemy_hazard_hit(har *h, controller *ctrl) {
har_event event;
memset(&event, 0, sizeof(event));
event.type = HAR_EVENT_ENEMY_HAZARD_HIT;
event.player_id = h->player_id;
fire_hooks(h, event, ctrl);
}
void har_event_stun(har *h, controller *ctrl) {
har_event event;
memset(&event, 0, sizeof(event));
event.type = HAR_EVENT_STUN;
event.player_id = h->player_id;
fire_hooks(h, event, ctrl);
}
void har_event_enemy_stun(har *h, controller *ctrl) {
har_event event;
memset(&event, 0, sizeof(event));
event.type = HAR_EVENT_ENEMY_STUN;
event.player_id = h->player_id;
fire_hooks(h, event, ctrl);
}
void har_event_recover(har *h, controller *ctrl) {
har_event event;
memset(&event, 0, sizeof(event));
event.type = HAR_EVENT_RECOVER;
event.player_id = h->player_id;
fire_hooks(h, event, ctrl);
}
void har_event_hit_wall(har *h, int wall, controller *ctrl) {
har_event event;
memset(&event, 0, sizeof(event));
event.type = HAR_EVENT_HIT_WALL;
event.player_id = h->player_id;
event.wall = wall;
fire_hooks(h, event, ctrl);
}
void har_event_land(har *h, controller *ctrl) {
har_event event;
memset(&event, 0, sizeof(event));
event.type = HAR_EVENT_LAND;
event.player_id = h->player_id;
fire_hooks(h, event, ctrl);
}
void har_event_defeat(har *h, controller *ctrl) {
har_event event;
memset(&event, 0, sizeof(event));
event.type = HAR_EVENT_DEFEAT;
event.player_id = h->player_id;
fire_hooks(h, event, ctrl);
}
void har_event_scrap(har *h, controller *ctrl) {
har_event event;
memset(&event, 0, sizeof(event));
event.type = HAR_EVENT_SCRAP;
event.player_id = h->player_id;
fire_hooks(h, event, ctrl);
}
void har_event_destruction(har *h, controller *ctrl) {
har_event event;
memset(&event, 0, sizeof(event));
event.type = HAR_EVENT_DESTRUCTION;
event.player_id = h->player_id;
fire_hooks(h, event, ctrl);
}
void har_event_done(har *h, controller *ctrl) {
har_event event;
memset(&event, 0, sizeof(event));
event.type = HAR_EVENT_DONE;
event.player_id = h->player_id;
fire_hooks(h, event, ctrl);
}
void har_stunned_done(object *har_obj) {
har *h = object_get_userdata(har_obj);
if(h->state == STATE_STUNNED) {
// refill endurance
h->endurance = 0;
h->state = STATE_STANDING;
har_set_ani(har_obj, ANIM_IDLE, 1);
}
}
// Simple helper function
void har_set_ani(object *obj, int animation_id, int repeat) {
har *h = object_get_userdata(obj);
af_move *move = af_get_move(h->af_data, animation_id);
char *s = (char *)str_c(&move->move_string);
// uint8_t has_corner_hack = obj->animation_state.shadow_corner_hack;
object_set_animation(obj, &move->ani);
// obj->animation_state.shadow_corner_hack = has_corner_hack;
if(s != NULL && strcmp(s, "!") != 0 && strcmp(s, "0") != 0 && h->delay > 0) {
log_debug("delaying move %d %s by %d ticks", move->id, s, h->delay);
object_set_delay(obj, h->delay);
}
// we shouldn't be idling while defeated
assert(!((animation_id == ANIM_IDLE || animation_id == ANIM_CROUCHING) && h->health <= 0));
if(move->category == CAT_JUMPING) {
h->state = STATE_JUMPING;
}
if(move->category == CAT_LOW || animation_id == ANIM_CROUCHING || animation_id == ANIM_CROUCHING_BLOCK) {
h->height = HEIGHT_CROUCHING;
} else {
h->height = HEIGHT_STANDING;
}
object_set_repeat(obj, repeat);
object_set_stride(obj, 1);
object_dynamic_tick(obj);
// update this so mx/my have correct origins
obj->start = obj->pos;
h->damage_done = 0;
h->damage_received = 0;
h->executing_move = 0;
}
int har_is_active(object *obj) {
har *h = object_get_userdata(obj);
// during scrap/destruction, the defeated har should be rendered frontmost
if(h->state == STATE_DEFEAT) {
return 1;
}
if(h->state == STATE_SCRAP || h->state == STATE_DESTRUCTION || h->state == STATE_VICTORY) {
return 0;
}
return h->executing_move;
}
void har_walk_to(object *obj, int destination) {
har *h = object_get_userdata(obj);
af_move *move = af_get_move(h->af_data, 10);
h->walk_done_anim = obj->cur_animation->id;
h->walk_done_tick = obj->animation_state.current_tick;
float vx = h->fwd_speed * object_get_direction(obj);
log_debug("set velocity to %f", vx);
object_set_vel(obj, vec2f_create(vx, 0));
object_set_animation(obj, &move->ani);
object_set_repeat(obj, 1);
object_set_stride(obj, 1);
h->walk_destination = destination;
object_dynamic_tick(obj);
// update this so mx/my have correct origins
obj->start = obj->pos;
}
int har_is_walking(har *h) {
if(h->state == STATE_WALKTO || h->state == STATE_WALKFROM) {
return 1;
}
return 0;
}
int har_is_crouching(har *h) {
if(h->state == STATE_CROUCHING || h->state == STATE_CROUCHBLOCK) {
return 1;
}
return 0;
}
int har_is_blocking(har *h, af_move *move) {
if(move->category == CAT_CLOSE) {
// throws cannot be blocked
return 0;
}
if(h->state == STATE_BLOCKSTUN) {
return 1;
}
if(h->state == STATE_CROUCHBLOCK && move->category != CAT_JUMPING && h->executing_move == 0) {
return 1;
}
if(h->state == STATE_WALKFROM && move->category != CAT_LOW && h->executing_move == 0) {
return 1;
}
return 0;
}
bool is_in_range(object *obj, af_move *move) {
if(move->successor_id) { // This is a throw with limited range
// CLOSE moves use the successor id field as a distance requirement
float throw_range = (float)obj->gs->match_settings.throw_range / 100.0f;
har *h = object_get_userdata(obj);
object *enemy_obj =
game_state_find_object(obj->gs, game_player_get_har_obj_id(game_state_get_player(obj->gs, !h->player_id)));
if(object_distance(obj, enemy_obj) > move->successor_id * throw_range) {
return false;
}
}
return true;
}
int har_is_invincible(object *obj, af_move *move) {
if(player_frame_isset(obj, "zz")) {
// blocks everything
return 1;
}
switch(move->category) {
case CAT_CLOSE:
if(player_frame_isset(obj, "zg") || obj->cur_animation->id == ANIM_DAMAGE ||
obj->cur_animation->id == ANIM_STANDING_BLOCK || obj->cur_animation->id == ANIM_CROUCHING_BLOCK) {
return 1;
}
break;
case CAT_LOW:
if(player_frame_isset(obj, "zl")) {
return 1;
}
break;
case CAT_MEDIUM:
if(player_frame_isset(obj, "zm")) {
return 1;
}
break;
case CAT_HIGH:
if(player_frame_isset(obj, "zh")) {
return 1;
}
break;
case CAT_JUMPING:
if(player_frame_isset(obj, "zj")) {
return 1;
}
break;
case CAT_PROJECTILE:
if(player_frame_isset(obj, "zp")) {
return 1;
}
break;
}
return 0;
}
// Callback for temporarily disabling an animation
void cb_har_disable_animation(object *parent, uint8_t animation_id, uint16_t ticks, void *userdata) {
object *har_obj = userdata;
har *h = object_get_userdata(har_obj);
hashmap_put_int(&h->disabled_animations, (int)animation_id, &ticks, sizeof(ticks));
}
// Callback for spawning new objects, eg. projectiles
void cb_har_spawn_object(object *parent, int id, vec2i pos, vec2f vel, uint8_t mp_flags, int s, int g, void *userdata) {
object *har_obj = userdata;
har *h = object_get_userdata(har_obj);
vec2i p_pos = object_get_pos(parent);
// can't do this in object, because it hoses the intro
if(pos.x == 0) {
pos.x = p_pos.x; // + p_size.x / 2;
}
if(pos.y == 0) {
pos.y = p_pos.y; // y + p_size.y / 2;
}
// If this is a scrap item, handle it as such ...
if(id == ANIM_SCRAP_METAL || id == ANIM_BOLT || id == ANIM_SCREW || id == ANIM_BURNING_OIL) {
// Use our existing function for spawning scrap
har_spawn_scrap(parent, pos, 12);
return;
}
// ... otherwise expect it is a projectile
af_move *move = af_get_move(h->af_data, id);
if(move != NULL) {
object *obj = omf_calloc(1, sizeof(object));
object_create(obj, parent->gs, pos, vel);
object_set_stl(obj, object_get_stl(parent));
object_set_animation(obj, &move->ani);
object_set_gravity(obj, g / 100.0f);
object_set_pal_offset(obj, object_get_pal_offset(parent));
object_set_pal_limit(obj, object_get_pal_limit(parent));
// Set all projectiles to their own layer + har layer
object_set_layers(obj, LAYER_PROJECTILE | (h->player_id == 0 ? LAYER_HAR2 : LAYER_HAR1));
// To avoid projectile-to-projectile collisions, set them to same group
object_set_group(obj, GROUP_PROJECTILE);
object_set_repeat(obj, 0);
object_set_shadow(obj, 1);
object_set_direction(obj, object_get_direction(parent));
obj->animation_state.enemy_obj_id = parent->animation_state.enemy_obj_id;
projectile_create(obj, parent);
obj->animation_state.enemy_obj_id = parent->animation_state.enemy_obj_id;
// allow projectiles to spawn projectiles, eg. shadow's scrap animation
object_set_spawn_cb(obj, cb_har_spawn_object, har_obj);
object_set_disable_cb(obj, cb_har_disable_animation, har_obj);
// Handle Nova animation where the bot gets destroyed in single player
if(h->id == 10 && id >= 25 && id <= 30) {
projectile_set_wall_bounce(obj, 1);
projectile_stop_on_ground(obj, 1);
}
if(h->state == STATE_SCRAP || h->state == STATE_DESTRUCTION) {
// some scrap animations, like shadow's spawn projectiles that might collide with the wall
// and we don't want them to disappear
projectile_set_invincible(obj);
}
game_state_add_object(parent->gs, obj, RENDER_LAYER_MIDDLE, 0, 0);
}
}
// Callback for destroying objects, eg. projectiles
void cb_har_destroy_object(object *parent, int animation_id, void *userdata) {
object *har_obj = userdata;
har *h = object_get_userdata(har_obj);
// find all projectiles owned by us with the supplied animation id
vector vec;
vector_create(&vec, sizeof(object *));
game_state_get_projectiles(parent->gs, &vec);
object **p;
iterator it;
vector_iter_begin(&vec, &it);
foreach(it, p) {
if(projectile_get_owner(*p) == h->player_id && object_get_animation(*p)->id == animation_id) {
game_state_del_object(parent->gs, *p);
}
}
vector_free(&vec);
}
void har_floor_landing_effects(object *obj, bool play_sound) {
int amount = rand_int(2) + 1;
for(int i = 0; i < amount; i++) {
int variance = rand_int(20) - 10;
vec2i coord = vec2i_create(obj->pos.x + variance + i * 10, obj->pos.y);
object *dust = omf_calloc(1, sizeof(object));
object_create(dust, obj->gs, coord, vec2f_create(0, 0));
object_set_stl(dust, object_get_stl(obj));
object_set_animation(dust, &bk_get_info(game_state_get_scene(obj->gs)->bk_data, 26)->ani);
game_state_add_object(obj->gs, dust, RENDER_LAYER_MIDDLE, 0, 0);
}
// Landing sound
if(play_sound) {
float pos_pan = ((float)obj->pos.x - 160.0f) / 160.0f;
game_state_play_sound(obj->gs, 56, 0.3f, pos_pan, 2.2f);
}
}
char get_last_input(har *har) {
return har->inputs[0];
}
void har_move(object *obj) {
har *h = object_get_userdata(obj);
if(h->is_grabbed > 0 || h->in_stasis_ticks > 0) {
return;
}
obj->pos.x += obj->vel.x;
obj->pos.y += obj->vel.y;
object *enemy_obj =
game_state_find_object(obj->gs, game_player_get_har_obj_id(game_state_get_player(obj->gs, !h->player_id)));
har *enemy_har = object_get_userdata(enemy_obj);
if(h->walk_destination > 0 && h->walk_done_anim &&
((obj->pos.x >= h->walk_destination && object_get_direction(obj) == OBJECT_FACE_RIGHT) ||
(obj->pos.x <= h->walk_destination && object_get_direction(obj) == OBJECT_FACE_LEFT))) {
obj->pos.x = h->walk_destination;
log_debug("reached destination!");
if(obj->animation_state.shadow_corner_hack) {
object_set_direction(obj, object_get_direction(obj) * -1);
}
object_set_vel(obj, vec2f_create(0, 0));
har_set_ani(obj, h->walk_done_anim, 0);
obj->animation_state.current_tick = h->walk_done_tick;
h->walk_destination = -1;
h->walk_done_anim = 0;
h->walk_done_tick = 0;
return;
} else if(h->walk_destination > 0) {
log_debug("still walking to %d, at %f", h->walk_destination, obj->pos.x);
}
// Check for wall hits
if(obj->pos.x <= ARENA_LEFT_WALL || obj->pos.x >= ARENA_RIGHT_WALL) {
h->is_wallhugging = 1;
if(player_frame_isset(obj, "cw") && player_frame_isset(obj, "d")) {
log_debug("disabling d tag on animation because of wall hit");
obj->animation_state.disable_d = 1;
}
} else {
h->is_wallhugging = 0;
}
// Handle floor collisions
if(!player_frame_isset(obj, "h")) {
if(obj->pos.y >= ARENA_FLOOR) {
controller *ctrl = game_player_get_ctrl(game_state_get_player(obj->gs, h->player_id));
obj->pos.y = ARENA_FLOOR;
char last_input = get_last_input(h);
if(player_frame_isset(obj, "cl")) {
af_move *move = af_get_move(h->af_data, obj->cur_animation->id);
object_set_vel(obj, vec2f_create(0, 0));
har_set_ani(obj, move->next_move, 0);
object_set_stride(obj, 1);
} else if((h->state == STATE_SCRAP || h->state == STATE_DESTRUCTION) && obj->vel.y > 0) {
object_set_vel(obj, vec2f_create(0, 0));
har_finished(obj);
har_floor_landing_effects(obj, true);
} else if((h->state == STATE_VICTORY) && obj->vel.y > 0) {
// TODO: A trigger in arena.c often puts us in STATE_VICTORY too early, which can be a problem
// if we're still airborne. This allows us to land somewhat properly.
object_set_vel(obj, vec2f_create(0, 0));
har_set_ani(obj, ANIM_IDLE, 1);
object_set_stride(obj, h->stride);
har_event_land(h, ctrl);
har_floor_landing_effects(obj, true);
} else if(h->state == STATE_JUMPING && enemy_har->is_grabbed == 0 && !player_frame_isset(obj, "cg")) {
// Change animation from jump to walk or idle,
// depending on held inputs
if(last_input == '6') {
h->state = STATE_WALKTO;
har_set_ani(obj, ANIM_WALKING, 1);
object_set_vel(obj, vec2f_create(0, 0));
object_set_stride(obj, h->stride);
har_event_walk(h, 1, ctrl);
} else if(last_input == '4') {
h->state = STATE_WALKFROM;
har_set_ani(obj, ANIM_WALKING, 1);
object_set_vel(obj, vec2f_create(0, 0));
object_set_stride(obj, h->stride);
har_event_walk(h, -1, ctrl);
} else if(last_input == '7' || last_input == '8' || last_input == '9') {
har_set_ani(obj, ANIM_JUMPING, 0);
h->state = STATE_JUMPING;
float vx = 0.0f;
float vy = h->jump_speed;
int jump_dir = 0;
int direction = object_get_direction(obj);
if(last_input == '9') {
vx = (h->fwd_speed * direction);
object_set_tick_pos(obj, 110);
object_set_stride(obj, 7); // Pass 7 frames per tick
jump_dir = 1;
} else if(last_input == '7') {
// If we are jumping backwards, start animation from end
// at -100 frames (seems to be about right)
object_set_playback_direction(obj, PLAY_BACKWARDS);
object_set_tick_pos(obj, -110);
vx = (h->back_speed * direction * -1);
object_set_stride(obj, 7); // Pass 7 frames per tick
jump_dir = -1;
} else {
// we are jumping upwards
object_set_tick_pos(obj, 110);
if(h->id == HAR_GARGOYLE) {
object_set_stride(obj, 7);
}
}
object_set_vel(obj, vec2f_create(vx, vy));
har_event_jump(h, jump_dir, ctrl);
} else {
object_set_vel(obj, vec2f_create(0, 0));
h->state = STATE_STANDING;
har_set_ani(obj, ANIM_IDLE, 1);
object_set_stride(obj, h->stride);
}
har_event_land(h, ctrl);
har_floor_landing_effects(obj, true);
} else if(h->state == STATE_RECOIL) {
if(obj->vel.y > 0) {
// bounce and screenshake if falling fast enough
if(obj->vel.y > 6) {
har_floor_landing_effects(obj, false);
obj->vel.y = -3;
obj->vel.x = obj->vel.x / 2;
if(h->id != 10) {
object_set_custom_string(obj, "l20s4sp13zzN3-zzM100");
obj->gs->screen_shake_vertical = 5; // Multiplied by 5 to make it visible
} else {
// Nova falls harder
object_set_custom_string(obj, "l40s4sp13zzN3-zzM100");
obj->gs->screen_shake_vertical = 15; // Multiplied by 5 to make it visible
}
} else {
obj->vel.y = 0;
obj->vel.x = 0;
har_event_land(h, ctrl);
har_finished(obj);
}
}
if(obj->pos.x < ARENA_LEFT_WALL) {
obj->pos.x = ARENA_LEFT_WALL;
}
if(obj->pos.x > ARENA_RIGHT_WALL) {
obj->pos.x = ARENA_RIGHT_WALL;
}
}
if(h->state != STATE_SCRAP) {
// add some friction from the floor if we're not walking during scrap
// This is important to dampen/eliminate the velocity added from pushing away from the other HAR
// friction decreases velocity by 1 each tick, and sets it to 0 if its under |2|
if(obj->vel.x > 0.0f) {
if(obj->vel.x < 2.0f) {
obj->vel.x = 0.0f;
} else {
obj->vel.x -= 1.0f;
}
} else if(obj->vel.x < 0.0f) {
if(obj->vel.x > -2.0f) {
obj->vel.x = 0.0f;
} else {
obj->vel.x += 1.0f;
}
}
}
if(h->state == STATE_WALKTO) {
har_face_enemy(obj, enemy_obj);
obj->pos.x += (h->fwd_speed * object_get_direction(obj));
} else if(h->state == STATE_WALKFROM) {
har_face_enemy(obj, enemy_obj);
obj->pos.x -= (h->back_speed * object_get_direction(obj));
}
object_apply_controllable_velocity(obj, false, last_input);
} else {
if(game_state_hars_are_alive(obj->gs)) {
obj->vel.y += obj->gravity;
} else {
obj->vel.y += 230.0 / 256.0;
}
// Terminal Velocity
if(obj->vel.y > 13) {
obj->vel.y = 13;
}
}
}
}
void har_take_damage(object *obj, const str *string, float damage, float stun) {
har *h = object_get_userdata(obj);
if(h->state == STATE_VICTORY || h->state == STATE_DONE) {
// can't die if the other guy died first
return;
}
// Got hit, disable stasis activator on this bot
h->in_stasis_ticks = 1;
// Save damage taken
h->last_damage_value = damage;
h->last_stun_value = stun;
// interrupted
h->executing_move = 0;
if(h->linked_obj) {
object *linked = game_state_find_object(obj->gs, h->linked_obj);
if(linked) {
// end the animation of the linked object, so it can go to the successor
linked->animation_state.finished = 1;
}
}
if(vector_size(&h->child_objects)) {
// shadow children need to die when the controlling HAR is hit
iterator it;
uint32_t *child_id;
vector_iter_begin(&h->child_objects, &it);
foreach(it, child_id) {
object *child = game_state_find_object(obj->gs, *child_id);
if(child) {
int next_anim = af_get_move(h->af_data, child->cur_animation->id)->throw_duration;
if(next_anim) {
object_set_animation(child, &af_get_move(h->af_data, next_anim)->ani);
} else {
child->animation_state.finished = 1;
}
}
vector_delete(&h->child_objects, &it);
}
}
game_player *player = game_state_get_player(obj->gs, h->player_id);
// If god mode is not on, take damage
// Throw damage is delayed until after the throw ends
if(!player->god && !h->throw_duration) {
if(player->pilot->photo) {
// in tournament mode, damage is mitigated by armor
// (Armor + 2.5) * .25
log_debug("applying %f to %d modulated by armor %f", damage, h->health,
0.25f * (2.5f + player->pilot->armor));
h->health -= damage / (0.25f * (2.5f + player->pilot->armor));
} else {
h->health -= damage;
}
}
// Handle health changes
if(h->health <= 0) {
h->health = 0;
}
if(!h->throw_duration) {
int stun_amount = stun;
if(h->state == STATE_RECOIL && object_is_airborne(obj)) { // Less stun on rehit and throws
stun_amount /= 2;
}
stun_amount = (stun_amount * 2 + 12) * 256;
log_debug("applying %f stun damage to %f", stun_amount, h->endurance);
h->endurance += stun_amount;
}
if(h->endurance < 1.0f) {
if(h->state == STATE_STUNNED) {
// refill endurance
h->endurance = 0;
}
} else if(h->endurance >= h->endurance_max) {
// Calculate how much dizzy time we have based on the stun limit overage.
// The more negative you go, the more time you're stunned.
h->endurance = ((((h->endurance - h->endurance_max) / 256) * -2.5) - 60) * 256;
}
game_player *other_player = game_state_get_player(obj->gs, !h->player_id);
object *other_har = game_state_find_object(obj->gs, other_player->har_obj_id);
if(h->health == 0) {
// Take a screencap of enemy har
har_screencaps_capture(&other_player->screencaps, other_har, obj, SCREENCAP_BLOW);
// Slow down game more for last shot
log_debug("Slowdown: Slowing from %d to %d.", game_state_get_speed(obj->gs),
h->health == 0 ? game_state_get_speed(obj->gs) - 10 : game_state_get_speed(obj->gs) - 6);
game_state_slowdown(obj->gs, 12,
h->health == 0 ? game_state_get_speed(obj->gs) - 10 : game_state_get_speed(obj->gs) - 6);
} else {
if(player_frame_isset(other_har, "cp")) {
game_state_hit_pause(obj->gs);
}
}
str custom;
str_create(&custom);
// chronos' stasis does not have a hit animation
if(str_size(string) > 0) {
h->state = STATE_RECOIL;
// Set hit animation
object_set_animation(obj, &af_get_move(h->af_data, ANIM_DAMAGE)->ani);
object_set_repeat(obj, 0);
if(h->health <= 0) {
controller *ctrl = game_player_get_ctrl(game_state_get_player(obj->gs, h->player_id));
// trigger the defeat hook immediately
har_event_defeat(h, ctrl);
}
if(h->throw_duration) {
// No special handling
object_set_stride(obj, 1);
} else if(object_is_airborne(obj)) {
log_debug("airborne knockback");
// append the 'airborne knockback' string to the hit string, replacing the final frame
size_t last_line = 0;
if(!str_last_of(string, '-', &last_line)) {
last_line = 0;
}
str_from_slice(&custom, string, 0, last_line);
if(h->endurance < 0 || h->health <= 0) {
// this hit stunned them, so make them hit the floor stunned
str_append_c(&custom, "-L3-M5000");
} else {
str_append_c(&custom, "-L2-M5-L2");
}
obj->vel.y = obj->vertical_velocity_modifier * ((((30.0f - damage) * 0.133333f) + 6.5f) * -1.0);
// TODO there's an alternative formula used in some conditions:
// (((damage * 0.09523809523809523) + 3.5) * -1) * obj->vertical_velocity_modifier
// but we don't know what those conditions are
obj->vel.x =
(((damage * 0.16666666f) + 2.0f) * object_get_direction(obj) * -1) * obj->horizontal_velocity_modifier;
object_set_stride(obj, 1);
} else {
if(h->health <= 0 || h->endurance < 0) {
// taken from MASTER.DAT
size_t last_line = 0;
if(!str_last_of(string, '-', &last_line)) {
last_line = 0;
}
str_from_slice(&custom, string, 0, last_line);
str_append_c(&custom, "-x-20ox-20L1-ox-20L2-x-20zzs4l25sp13M1-zzM2");
}
}
if(str_size(&custom) > 0) {
object_set_custom_string(obj, str_c(&custom));
log_debug("HAR %s animation set to %s", har_get_name(h->id), str_c(&custom));
str_free(&custom);
} else {
object_set_custom_string(obj, str_c(string));
log_debug("HAR %s animation set to %s", har_get_name(h->id), str_c(string));
}
object_dynamic_tick(obj);
// XXX hack - if the first frame has the 'k' tag, treat it as some vertical knockback
// we can't do this in player.c because it breaks the jaguar leap, which also uses the 'k' tag.
// Insanius 3/17/2025 - This is actually mostly correct, the OG checks if the string starts with the 'k' char
const sd_script_frame *frame = sd_script_get_frame(&obj->animation_state.parser, 0);
if(frame != NULL && sd_script_isset(frame, "k")) {
obj->vel.x = -5 * object_get_direction(obj);
obj->vel.y = -8;
}
}
}
// for scrap, nuts, bolts, and sparks (aka burning oil)
static vec2f har_debris_random_vel(object *har_obj, bool is_destruction) {
vec2f vel;
vel.x = rand_float() * 4.0f - 2.0f;
vel.y = rand_float() * 4.0f - 2.0f;
// FIXME: burning oil/sparks in destruction and scrap animations
// FIXME: how does the original game handle scrap velocity in destruction anims?
if(is_destruction) {
// For scrap and destruction animations, make the scrap fly all over the place!
vel.x *= 10.0f;
vel.y *= 10.0f;
} else {
// push scrap and sparks upwards and away from the direction of impact
vel.x -= object_get_direction(har_obj) * 5.1f;
vel.y -= 3.0f;
}
return vel;
}
// gravity for sparks. scrap, nuts, and bolts seem to have different gravity
static inline float har_sparks_random_gravity(object *har_obj) {
return (float)(rand_int(30) + 40) / 100.0;
}
static void har_spawn_oil(object *obj, vec2i pos, int amount, int layer) {
har *h = object_get_userdata(obj);
// burning oil
for(int i = 0; i < amount; i++) {
// Create the object
object *scrap = omf_calloc(1, sizeof(object));
int anim_no = ANIM_BURNING_OIL;
object_create(scrap, obj->gs, pos, har_debris_random_vel(obj, false));
object_set_animation(scrap, &af_get_move(h->af_data, anim_no)->ani);
object_set_stl(scrap, object_get_stl(obj));
object_set_gravity(scrap, har_sparks_random_gravity(obj));
object_set_layers(scrap, LAYER_SCRAP);
object_dynamic_tick(scrap);
scrap_create(scrap);
game_state_add_object(obj->gs, scrap, layer, 0, 0);
}
}
// TODO: This is kind of a hack. It's used to check if either
// HAR is doing destruction. If there is any way to do this better,
// this should be changed.
int is_destruction(game_state *gs) {
har *har_a = object_get_userdata(game_state_find_object(gs, game_state_get_player(gs, 0)->har_obj_id));
har *har_b = object_get_userdata(game_state_find_object(gs, game_state_get_player(gs, 1)->har_obj_id));
return (har_a->state == STATE_DESTRUCTION || har_b->state == STATE_DESTRUCTION);
}
void har_spawn_scrap(object *obj, vec2i pos, int amount) {
// wild ass guess
int oil_amount = amount / 3;
har *h = object_get_userdata(obj);
har_spawn_oil(obj, pos, oil_amount, RENDER_LAYER_TOP);
// scrap metal
// TODO this assumes the default scrap level and does not consider BIG[1-9]
int scrap_amount = 0;
if(amount > 11 && amount < 14) {
scrap_amount = 1;
} else if(amount > 13 && amount < 16) {
scrap_amount = 2;
} else if(amount > 15) {
scrap_amount = 3;
}
for(int i = 0; i < scrap_amount; i++) {
// Create the object
object *scrap = omf_calloc(1, sizeof(object));
int anim_no = rand_int(3) + ANIM_SCRAP_METAL;
object_create(scrap, obj->gs, pos, har_debris_random_vel(obj, h->state == STATE_DEFEAT));
object_set_animation(scrap, &af_get_move(h->af_data, anim_no)->ani);
object_set_stl(scrap, object_get_stl(obj));
object_set_gravity(scrap, 1.0f);
object_set_pal_offset(scrap, object_get_pal_offset(obj));
object_set_pal_limit(obj, object_get_pal_limit(obj));
object_set_layers(scrap, LAYER_SCRAP);
object_set_group(scrap, GROUP_SCRAP);
object_dynamic_tick(scrap);
object_set_shadow(scrap, 1);
scrap_create(scrap);
game_state_add_object(obj->gs, scrap, RENDER_LAYER_TOP, 0, 0);
}
}
void har_block(object *obj, vec2i hit_coord, uint8_t block_stun) {
har *h = obj->userdata;
if(h->state == STATE_WALKFROM) {
object_set_animation(obj, &af_get_move(h->af_data, ANIM_STANDING_BLOCK)->ani);