|
| 1 | +/* |
| 2 | + * Copyright (C) 2026 Michael Binder and contributors |
| 3 | + * |
| 4 | + * This file is part of OpenShadowFlare. |
| 5 | + * |
| 6 | + * OpenShadowFlare is free software: you can redistribute it and/or modify it |
| 7 | + * under the terms of the GNU General Public License as published by the Free |
| 8 | + * Software Foundation, either version 3 of the License, or (at your option) |
| 9 | + * any later version. |
| 10 | + * |
| 11 | + * OpenShadowFlare is distributed in the hope that it will be useful, but |
| 12 | + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 13 | + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 14 | + * details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License along |
| 17 | + * with OpenShadowFlare. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "assets/scenario_enemy_assets.h" |
| 21 | + |
| 22 | +#include "assets/retail_paths.h" |
| 23 | + |
| 24 | +#include <stdio.h> |
| 25 | +#include <string.h> |
| 26 | + |
| 27 | +static bool sf_enemy_attack_path( |
| 28 | + char *path, size_t capacity, const char *root, |
| 29 | + const char *format, int32_t resource_id) { |
| 30 | + char relative[SF_RETAIL_PATH_CAPACITY]; |
| 31 | + const int length = snprintf( |
| 32 | + relative, sizeof(relative), format, resource_id); |
| 33 | + return length >= 0 && (size_t) length < sizeof(relative) && |
| 34 | + sf_retail_path_join(path, capacity, root, relative); |
| 35 | +} |
| 36 | + |
| 37 | +static bool sf_enemy_attack_add_pattern( |
| 38 | + int32_t *patterns, uint16_t *count, int32_t pattern) { |
| 39 | + uint16_t index; |
| 40 | + if (pattern < 0) return true; |
| 41 | + for (index = 0u; index < *count; ++index) { |
| 42 | + if (patterns[index] == pattern) return true; |
| 43 | + } |
| 44 | + if (*count >= SF_NJP_SPARSE_PATTERN_LIMIT) return false; |
| 45 | + patterns[(*count)++] = pattern; |
| 46 | + return true; |
| 47 | +} |
| 48 | + |
| 49 | +static bool sf_enemy_attack_request( |
| 50 | + const SfMctScenario *scenario, int32_t resource_id, uint16_t chart, |
| 51 | + uint8_t *enabled_parts) { |
| 52 | + uint16_t enemy_index; |
| 53 | + bool found = false; |
| 54 | + for (enemy_index = 0u; enemy_index < scenario->enemy_count; ++enemy_index) { |
| 55 | + const SfMctEnemy *enemy = &scenario->enemies[enemy_index]; |
| 56 | + const int32_t enemy_chart = enemy->post_ai_values[41] + 4; |
| 57 | + uint8_t part; |
| 58 | + if (enemy->resource_id != resource_id || enemy_chart != chart) continue; |
| 59 | + found = true; |
| 60 | + for (part = 0u; part < SF_MCT_PERSON_PART_LIMIT; ++part) { |
| 61 | + if (!enemy->custom_parts || enemy->part_visibility[part] != 0u) |
| 62 | + *enabled_parts = (uint8_t) ( |
| 63 | + *enabled_parts | (uint8_t) (1u << part)); |
| 64 | + } |
| 65 | + } |
| 66 | + return found; |
| 67 | +} |
| 68 | + |
| 69 | +static bool sf_enemy_attack_collect_patterns( |
| 70 | + const SfCafSelectedAnimation *animations, |
| 71 | + int32_t *artwork_patterns, uint16_t *artwork_count, |
| 72 | + int32_t *shadow_patterns, uint16_t *shadow_count) { |
| 73 | + uint8_t direction; |
| 74 | + for (direction = 0u; direction < SF_SCENARIO_ENEMY_DIRECTION_COUNT; |
| 75 | + ++direction) { |
| 76 | + const SfCafSelectedAnimation *animation = &animations[direction]; |
| 77 | + uint8_t part; |
| 78 | + for (part = 0u; part < animation->part_count; ++part) { |
| 79 | + uint16_t frame; |
| 80 | + for (frame = 0u; frame < animation->frame_count; ++frame) { |
| 81 | + const SfCafCell *cell = &animation->parts[part].cells[frame]; |
| 82 | + if (((cell->status & 8) == 0 && !sf_enemy_attack_add_pattern( |
| 83 | + artwork_patterns, artwork_count, cell->pattern)) || |
| 84 | + ((cell->status & 8) != 0 && !sf_enemy_attack_add_pattern( |
| 85 | + shadow_patterns, shadow_count, cell->pattern))) return false; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + return *artwork_count > 0u && *shadow_count > 0u; |
| 90 | +} |
| 91 | + |
| 92 | +bool sf_scenario_enemy_attack_assets_load( |
| 93 | + SfScenarioEnemyAttackAssets *attack, const char *data_root, |
| 94 | + const SfMctScenario *scenario, int32_t resource_id, uint16_t chart, |
| 95 | + SfArena *arena) { |
| 96 | + SfCafAnimationSelection selections[SF_SCENARIO_ENEMY_DIRECTION_COUNT]; |
| 97 | + uint8_t selected_parts[SF_MCT_PERSON_PART_LIMIT]; |
| 98 | + int32_t artwork_patterns[SF_NJP_SPARSE_PATTERN_LIMIT]; |
| 99 | + int32_t shadow_patterns[SF_NJP_SPARSE_PATTERN_LIMIT]; |
| 100 | + uint16_t artwork_count = 0u; |
| 101 | + uint16_t shadow_count = 0u; |
| 102 | + uint8_t selected_count = 0u; |
| 103 | + uint8_t enabled_parts = 0u; |
| 104 | + uint8_t available_parts = UINT8_MAX; |
| 105 | + uint8_t direction; |
| 106 | + uint8_t part; |
| 107 | + char path[SF_RETAIL_PATH_CAPACITY]; |
| 108 | + size_t mark; |
| 109 | + bool success = false; |
| 110 | + if (!attack || !data_root || !scenario || resource_id < 0 || !arena) |
| 111 | + return false; |
| 112 | + mark = sf_arena_mark(arena); |
| 113 | + memset(attack, 0, sizeof(*attack)); |
| 114 | + if (chart > UINT8_MAX || !sf_enemy_attack_request( |
| 115 | + scenario, resource_id, chart, &enabled_parts) || |
| 116 | + !sf_enemy_attack_path( |
| 117 | + path, sizeof(path), data_root, |
| 118 | + sf_retail_enemy_paths.animation_format, resource_id)) goto done; |
| 119 | + for (direction = 0u; direction < SF_SCENARIO_ENEMY_DIRECTION_COUNT; |
| 120 | + ++direction) { |
| 121 | + uint8_t count; |
| 122 | + if (!sf_caf_chart_direction_part_count( |
| 123 | + path, chart, direction, &count)) goto done; |
| 124 | + if (count < available_parts) available_parts = count; |
| 125 | + selections[direction].chart = chart; |
| 126 | + selections[direction].direction = direction; |
| 127 | + } |
| 128 | + if (available_parts > SF_MCT_PERSON_PART_LIMIT) |
| 129 | + available_parts = SF_MCT_PERSON_PART_LIMIT; |
| 130 | + for (part = 0u; part < available_parts; ++part) { |
| 131 | + if ((enabled_parts & (uint8_t) (1u << part)) != 0u) |
| 132 | + selected_parts[selected_count++] = part; |
| 133 | + } |
| 134 | + if (selected_count == 0u || !sf_caf_load_selected_animations( |
| 135 | + path, selections, SF_SCENARIO_ENEMY_DIRECTION_COUNT, |
| 136 | + selected_parts, selected_count, arena, attack->animations) || |
| 137 | + !sf_enemy_attack_collect_patterns( |
| 138 | + attack->animations, artwork_patterns, &artwork_count, |
| 139 | + shadow_patterns, &shadow_count) || |
| 140 | + !sf_enemy_attack_path( |
| 141 | + path, sizeof(path), data_root, |
| 142 | + sf_retail_enemy_paths.artwork_format, resource_id) || |
| 143 | + !sf_njp_load_sparse_patterns( |
| 144 | + path, artwork_patterns, artwork_count, arena, &attack->artwork) || |
| 145 | + !sf_enemy_attack_path( |
| 146 | + path, sizeof(path), data_root, |
| 147 | + sf_retail_enemy_paths.shadow_format, resource_id) || |
| 148 | + !sf_njp_load_sparse_patterns( |
| 149 | + path, shadow_patterns, shadow_count, arena, &attack->shadows)) |
| 150 | + goto done; |
| 151 | + attack->resource_id = resource_id; |
| 152 | + attack->chart = chart; |
| 153 | + attack->memory_bytes = sf_arena_mark(arena) - mark; |
| 154 | + attack->loaded = true; |
| 155 | + success = true; |
| 156 | +done: |
| 157 | + if (!success) { |
| 158 | + (void) sf_arena_rewind(arena, mark); |
| 159 | + memset(attack, 0, sizeof(*attack)); |
| 160 | + } |
| 161 | + return success; |
| 162 | +} |
0 commit comments