Skip to content

Commit a1d2e69

Browse files
committed
Add the retail owned-companion foundation
1 parent cabfaa3 commit a1d2e69

49 files changed

Lines changed: 1456 additions & 51 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎roadmap.md‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@ does. UI state only emits actions; persistent spell ownership remains in
7979
general world event queue. Both tabs coexist with right-side Inventory,
8080
replace Special Item on the left, and share the same camera and input offset.
8181

82+
The player's owned companion has its first complete C99 owner now. Table 60
83+
selects one of the six companion types, tables 800 through 805 build its
84+
level-backed profile, and only that type's idle, walk, run, artwork, and shadow
85+
cells are retained from PARTNER. It enters beside the hero, follows through the
86+
same collision-aware edge controller used by other actors, routes around live
87+
PEOPLE, and keeps following while inactive. Space and the exact bottom-left HUD
88+
strip toggle the retail active/inactive state. The active type, all six level
89+
and experience rows, and the defeated countdown restore from their original
90+
save fields. Combat, death presentation, timed revival, Moon, swapping, and
91+
companion dialogue remain later slices rather than being folded into the
92+
follower.
93+
8294
Selecting a retail save now
8395
streams and verifies its `ShadowFlare0005` envelope without allocating a
8496
payload buffer. The plain player record and exact backpack, belt, visible
@@ -91,7 +103,7 @@ handoffs such as Ostare's starter items stay complete. Mine count, walk/run
91103
pace, scenario, and authored entry reach the world owner as well. The three
92104
Remote Town saves also pass the complete asset-and-owner restore path. The
93105
remaining save work belongs to systems which are not in the small runtime yet:
94-
companions, warehouse pages, automatic-item pages, and writing saves.
106+
warehouse pages, automatic-item pages, and writing saves.
95107
A slice is only done after the C99/TWL/TAL tests, a release build, a practical
96108
check when visible behavior changed, and a fresh measured budget when assets
97109
changed.

‎shadowflare/CMakeLists.txt‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ endfunction()
3535
add_library(
3636
shadowflare_core STATIC
3737
assets/character_create_assets.c
38+
assets/companion_assets.c
3839
assets/ground_item_assets.c
3940
assets/ground_item_requests.c
4041
assets/inventory_item_assets.c
@@ -50,6 +51,7 @@ add_library(
5051
core/coordinates.c
5152
core/retail_random.c
5253
data/caf.c
54+
data/companion_parameters.c
5355
data/bmp.c
5456
data/gnd.c
5557
data/item.c
@@ -78,6 +80,7 @@ add_library(
7880
data/table.c
7981
data/voc.c
8082
game/character_create.c
83+
game/companion.c
8184
game/belt.c
8285
game/collision.c
8386
game/equipment.c
@@ -88,6 +91,7 @@ add_library(
8891
game/load_game.c
8992
game/movement.c
9093
game/player.c
94+
game/player_companion.c
9195
game/player_magic.c
9296
game/player_elements.c
9397
game/player_item.c
@@ -115,6 +119,7 @@ add_library(
115119
render/renderer.c
116120
screens/character_create_screen.c
117121
screens/gameplay_actor.c
122+
screens/gameplay_companion.c
118123
screens/gameplay_ground_item.c
119124
screens/gameplay_scene.c
120125
screens/gameplay_screen.c
@@ -127,6 +132,8 @@ add_library(
127132
ui/conversation_input.c
128133
ui/conversation_layout.c
129134
ui/gameplay_character_panel.c
135+
ui/gameplay_companion_hud.c
136+
ui/gameplay_companion_hud_input.c
130137
ui/gameplay_belt.c
131138
ui/gameplay_equipment_layout.c
132139
ui/gameplay_hud.c

‎shadowflare/README.md‎

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,19 @@ and applies the emitted selection or assignment. Samples 57 and 58 use a small
144144
general world-event queue and dedicated gameplay-interface assets before TAL
145145
plays them at the runtime edge.
146146

147+
The hero's owned companion now comes from the retail companion tables and
148+
PARTNER archives instead of borrowing a town dog. A fixed six-row player owner
149+
keeps each type's level and experience, while the active profile supplies its
150+
name, movement speeds, life, resource, and draw strengths. The live actor uses
151+
the same integer collision query and cardinal edge controller as the player
152+
and PEOPLE actors, including dynamic actor blockers and retail's close, walk,
153+
run, and far-relocation distances. It starts inactive but still follows;
154+
Space, a controller shoulder button, or the exact bottom-left HUD strip toggles
155+
the authored active/inactive marker. Save loading restores the active type,
156+
all six progression rows, and the defeated countdown. Attacks, damage, death,
157+
revival, Moon, and swapping are intentionally left for their own gameplay
158+
slices.
159+
147160
Choosing a retail save now restores more than its load-screen summary. The
148161
`ShadowFlare0005` envelope is decoded and checksummed as a stream, so even a
149162
large save never needs a second payload-sized buffer. The complete plain
@@ -240,8 +253,8 @@ it is invalid.
240253

241254
## Current screen budgets
242255

243-
The complete title currently uses 1,454,573 bytes of the 7 MiB main arena,
244-
leaving 5,885,459 bytes free. Its screen-scoped artwork accounts for 1,101,182
256+
The complete title currently uses 1,458,533 bytes of the 7 MiB main arena,
257+
leaving 5,881,499 bytes free. Its screen-scoped artwork accounts for 1,101,182
245258
bytes. The rest includes TWL/TAL state, game and screen metadata, persistent
246259
8-bit menu music and effects, and one reusable 60,000-byte decode buffer. The
247260
video pool contains only the 614,400-byte RGB555 framebuffer, leaving 3,579,904
@@ -254,22 +267,22 @@ nonblank case, all ten smoke streams together decode at most 57,864 bytes into
254267
the same reusable buffer during one rendered frame.
255268

256269
Character creation releases all title-only artwork before loading its own
257-
assets. It uses 767,496 bytes of the main arena, leaving 6,572,536 bytes free;
270+
assets. It uses 771,456 bytes of the main arena, leaving 6,568,576 bytes free;
258271
414,105 bytes of that total are character-screen artwork and font data. Shared
259272
NJP parts are decoded only once even when several patterns reference them, and
260273
a static character screen is not filled again until a visible state changes.
261274

262-
The load-game screen uses 727,712 bytes of the main arena, leaving 6,612,320
275+
The load-game screen uses 731,672 bytes of the main arena, leaving 6,608,360
263276
bytes free. Its screen-scoped artwork, font, and selected save preview account
264277
for 374,321 bytes. Save headers stay in a fixed six-entry catalog, while only
265278
the selected 391x114 thumbnail occupies memory. Changing selection decodes the
266279
new preview into the same 89,148-byte RGB555 buffer; idle frames perform no
267280
file access and do not refill the framebuffer.
268281

269-
The complete Remote Town gameplay screen uses 5,423,692 bytes of the main
270-
arena, leaving 1,916,340 bytes free. Its screen-owned scenario, script, map,
271-
player, PEOPLE, ground-item, inventory-panel, equipment, and UI data and
272-
artwork account for 5,070,301
282+
The complete Remote Town gameplay screen uses 6,744,124 bytes of the main
283+
arena, leaving 595,908 bytes free. Its screen-owned scenario, script, map,
284+
player, owned companion, PEOPLE, ground-item, inventory-panel, equipment, and
285+
UI data and artwork account for 6,386,773
273286
bytes. GND rendering data is decoded directly from its compressed three-plane
274287
stream into two bytes
275288
per tile, so the 300x300 town grid occupies 180,000 bytes instead of retaining
@@ -321,6 +334,14 @@ The interpreter uses fixed temporary, persistent, quest, and 16-frame call
321334
stack storage. Command semantics, operand access, and the resumable status loop
322335
are separate small files rather than one growing interpreter source.
323336

337+
The owned companion is prepared just as narrowly. The active save row chooses
338+
one PARTNER resource, and its loader keeps only charts zero through two for the
339+
eight ordinary directions, deduplicating every referenced NJP and SDW pattern.
340+
That companion slice accounts for most of the latest gameplay increase, so the
341+
remaining 595,908-byte headroom is now a hard warning for upcoming combat and
342+
effect work: later slices must retire or stream existing screen data rather
343+
than quietly raising the arena limit.
344+
324345
World pointer hit testing lives in `ui/`, where the loaded sparse actor cells
325346
are already available. It produces a small actor intent before each game
326347
update; `game/` owns the actual approach and distance rule. The common font is
@@ -337,7 +358,8 @@ the speaking actor. No renderer or target backend knows about script choices.
337358
The bottom HUD follows that rule as well. `data/table.c` walks compressed or
338359
plain retail parameter tables through a numeric callback without allocating a
339360
database. `game/player.c` owns the selected starting values, while
340-
`ui/gameplay_hud.c` composes the 18 currently required `Bar.njp` patterns.
361+
`ui/gameplay_hud.c` and `ui/gameplay_companion_hud.c` compose the 22 currently
362+
required `Bar.njp` patterns.
341363
Only the active patterns and their 19 referenced parts are decoded. The
342364
renderer remains unaware of gauges, levels, pace, or HUD hit areas.
343365

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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/companion_assets.h"
21+
22+
#include "assets/retail_paths.h"
23+
24+
#include <stdio.h>
25+
#include <string.h>
26+
27+
static bool sf_companion_asset_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(relative, sizeof(relative), format, resource_id);
32+
return length >= 0 && (size_t) length < sizeof(relative) &&
33+
sf_retail_path_join(path, capacity, root, relative);
34+
}
35+
36+
static bool sf_companion_add_pattern(
37+
int32_t *patterns, uint16_t *count, int32_t pattern) {
38+
uint16_t index;
39+
if (pattern < 0) return true;
40+
for (index = 0u; index < *count; ++index)
41+
if (patterns[index] == pattern) return true;
42+
if (*count >= SF_NJP_SPARSE_PATTERN_LIMIT) return false;
43+
patterns[(*count)++] = pattern;
44+
return true;
45+
}
46+
47+
bool sf_companion_assets_load(
48+
SfCompanionAssets *assets, const char *data_root,
49+
const SfCompanionProfile *profile, SfArena *arena) {
50+
SfCafAnimationSelection selections[
51+
SF_COMPANION_ANIMATION_COUNT * SF_COMPANION_DIRECTION_COUNT];
52+
int32_t artwork_patterns[SF_NJP_SPARSE_PATTERN_LIMIT];
53+
int32_t shadow_patterns[SF_NJP_SPARSE_PATTERN_LIMIT];
54+
uint8_t selected_parts[SF_COMPANION_PART_LIMIT];
55+
uint16_t artwork_count = 0u;
56+
uint16_t shadow_count = 0u;
57+
uint8_t available_parts = UINT8_MAX;
58+
uint8_t chart;
59+
uint8_t direction;
60+
uint8_t part;
61+
char path[SF_RETAIL_PATH_CAPACITY];
62+
size_t mark;
63+
bool success = false;
64+
if (!assets || !data_root || !profile || profile->resource_id < 0 || !arena)
65+
return false;
66+
mark = sf_arena_mark(arena);
67+
memset(assets, 0, sizeof(*assets));
68+
assets->resource_id = profile->resource_id;
69+
if (!sf_companion_asset_path(
70+
path, sizeof(path), data_root,
71+
sf_retail_companion_paths.animation_format, profile->resource_id))
72+
goto done;
73+
for (chart = 0u; chart < SF_COMPANION_ANIMATION_COUNT; ++chart) {
74+
for (direction = 0u; direction < SF_COMPANION_DIRECTION_COUNT;
75+
++direction) {
76+
const uint8_t index = (uint8_t) (
77+
chart * SF_COMPANION_DIRECTION_COUNT + direction);
78+
uint8_t count;
79+
if (!sf_caf_chart_direction_part_count(path, chart, direction, &count))
80+
goto done;
81+
if (count < available_parts) available_parts = count;
82+
selections[index].chart = chart;
83+
selections[index].direction = direction;
84+
}
85+
}
86+
if (available_parts > SF_COMPANION_PART_LIMIT)
87+
available_parts = SF_COMPANION_PART_LIMIT;
88+
if (available_parts == 0u) goto done;
89+
for (part = 0u; part < available_parts; ++part) {
90+
selected_parts[part] = part;
91+
assets->selected_parts = (uint8_t) (
92+
assets->selected_parts | (uint8_t) (1u << part));
93+
}
94+
if (!sf_caf_load_selected_animations(
95+
path, selections,
96+
SF_COMPANION_ANIMATION_COUNT * SF_COMPANION_DIRECTION_COUNT,
97+
selected_parts, available_parts, arena, &assets->animations[0][0]))
98+
goto done;
99+
for (chart = 0u; chart < SF_COMPANION_ANIMATION_COUNT; ++chart) {
100+
for (direction = 0u; direction < SF_COMPANION_DIRECTION_COUNT;
101+
++direction) {
102+
const SfCafSelectedAnimation *animation =
103+
&assets->animations[chart][direction];
104+
for (part = 0u; part < animation->part_count; ++part) {
105+
uint16_t frame;
106+
for (frame = 0u; frame < animation->frame_count; ++frame) {
107+
const SfCafCell *cell = &animation->parts[part].cells[frame];
108+
if (((cell->status & 8) == 0 && !sf_companion_add_pattern(
109+
artwork_patterns, &artwork_count, cell->pattern)) ||
110+
((cell->status & 8) != 0 && !sf_companion_add_pattern(
111+
shadow_patterns, &shadow_count, cell->pattern))) goto done;
112+
}
113+
}
114+
}
115+
}
116+
if (artwork_count == 0u || shadow_count == 0u ||
117+
!sf_companion_asset_path(
118+
path, sizeof(path), data_root,
119+
sf_retail_companion_paths.artwork_format, profile->resource_id) ||
120+
!sf_njp_load_sparse_patterns(
121+
path, artwork_patterns, artwork_count, arena, &assets->artwork) ||
122+
!sf_companion_asset_path(
123+
path, sizeof(path), data_root,
124+
sf_retail_companion_paths.shadow_format, profile->resource_id) ||
125+
!sf_njp_load_sparse_patterns(
126+
path, shadow_patterns, shadow_count, arena, &assets->shadows))
127+
goto done;
128+
assets->memory_bytes = sf_arena_mark(arena) - mark;
129+
success = true;
130+
done:
131+
if (!success) {
132+
(void) sf_arena_rewind(arena, mark);
133+
memset(assets, 0, sizeof(*assets));
134+
}
135+
return success;
136+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
#ifndef SHADOWFLARE_ASSETS_COMPANION_ASSETS_H
21+
#define SHADOWFLARE_ASSETS_COMPANION_ASSETS_H
22+
23+
#include "core/arena.h"
24+
#include "data/caf.h"
25+
#include "data/companion_parameters.h"
26+
#include "data/njp.h"
27+
28+
#include <stdbool.h>
29+
#include <stddef.h>
30+
#include <stdint.h>
31+
32+
#define SF_COMPANION_ANIMATION_COUNT 3u
33+
#define SF_COMPANION_DIRECTION_COUNT 8u
34+
#define SF_COMPANION_PART_LIMIT 8u
35+
36+
typedef struct SfCompanionAssets {
37+
SfCafSelectedAnimation animations
38+
[SF_COMPANION_ANIMATION_COUNT][SF_COMPANION_DIRECTION_COUNT];
39+
SfNjpSparseResource artwork;
40+
SfNjpSparseResource shadows;
41+
int32_t resource_id;
42+
size_t memory_bytes;
43+
uint8_t selected_parts;
44+
} SfCompanionAssets;
45+
46+
bool sf_companion_assets_load(
47+
SfCompanionAssets *assets, const char *data_root,
48+
const SfCompanionProfile *profile, SfArena *arena);
49+
50+
#endif

‎shadowflare/assets/gameplay_assets.c‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ bool sf_gameplay_assets_load(
211211
SfGameplayAssets *assets, const char *data_root,
212212
int32_t scenario_id, int32_t entry_key, uint8_t player_gender,
213213
int32_t player_level,
214+
int32_t companion_type, int32_t companion_level,
214215
const uint8_t *appearance_parts, uint8_t appearance_part_count,
215216
const SfItemReference *visible_items, uint8_t visible_item_count,
216217
const SfItemReference *retained_items, uint8_t retained_item_count,
@@ -222,9 +223,10 @@ bool sf_gameplay_assets_load(
222223
char path[SF_RETAIL_PATH_CAPACITY];
223224
static const uint8_t font_pattern = 0u;
224225
static const uint8_t speech_patterns[5] = {0u, 1u, 2u, 3u, 4u};
225-
static const uint8_t hud_patterns[18] = {
226+
static const uint8_t hud_patterns[22] = {
226227
0u, 3u, 7u, 8u, 10u, 11u, 14u, 15u,
227-
19u, 20u, 21u, 22u, 23u, 24u, 25u, 26u, 27u, 28u
228+
19u, 20u, 21u, 22u, 23u, 24u, 25u, 26u, 27u, 28u,
229+
29u, 30u, 31u, 32u
228230
};
229231
static const uint8_t inventory_patterns[37] = {
230232
0u, 1u, 2u, 3u, 5u, 6u, 14u, 15u, 16u, 32u,
@@ -292,7 +294,7 @@ bool sf_gameplay_assets_load(
292294
!sf_retail_path_join(
293295
path, sizeof(path), data_root, sf_retail_game_paths.hud) ||
294296
!sf_njp_load_decoded_patterns(
295-
path, hud_patterns, 18u, arena, &assets->hud) ||
297+
path, hud_patterns, 22u, arena, &assets->hud) ||
296298
!sf_retail_path_join(
297299
path, sizeof(path), data_root, sf_retail_game_paths.status) ||
298300
!sf_njp_stream_decoded_patterns(
@@ -311,6 +313,8 @@ bool sf_gameplay_assets_load(
311313
sf_retail_game_paths.parameter_tables) ||
312314
!sf_player_initial_parameters_load(
313315
path, player_gender, player_level, &assets->player_parameters) ||
316+
!sf_companion_profile_load(
317+
path, companion_type, companion_level, &assets->companion_profile) ||
314318
!sf_spell_parameters_load(path, assets->spell_parameters) ||
315319
!sf_gameplay_sound_assets_load(
316320
&assets->sounds, data_root, arena) ||
@@ -321,6 +325,8 @@ bool sf_gameplay_assets_load(
321325
assets, data_root, player_gender,
322326
appearance_parts, appearance_part_count,
323327
visible_items, visible_item_count, arena) ||
328+
!sf_companion_assets_load(
329+
&assets->companion, data_root, &assets->companion_profile, arena) ||
324330
!sf_scenario_actor_assets_load(
325331
&assets->actors, data_root, &assets->scenario, arena) ||
326332
!sf_inventory_item_assets_load(

0 commit comments

Comments
 (0)